Agents/TUI: align with current pi APIs

This commit is contained in:
Gustavo Madeira Santana
2026-03-27 19:03:35 -04:00
parent c7fbd51890
commit 1fc4d7259f
12 changed files with 96 additions and 48 deletions

View File

@@ -0,0 +1,81 @@
import { describe, expect, it } from "vitest";
import {
FilterableSelectList,
type FilterableSelectItem,
type FilterableSelectListTheme,
} from "./filterable-select-list.js";
const mockTheme: FilterableSelectListTheme = {
selectedPrefix: (t) => `[${t}]`,
selectedText: (t) => `**${t}**`,
description: (t) => `(${t})`,
scrollInfo: (t) => `~${t}~`,
noMatch: (t) => `!${t}!`,
filterLabel: (t) => `>${t}<`,
};
const testItems: FilterableSelectItem[] = [
{
value: "session-1",
label: "first session",
description: "Oldest",
searchText: "alpha",
},
{
value: "session-2",
label: "second session",
description: "Newest",
searchText: "beta",
},
];
describe("FilterableSelectList", () => {
function typeInput(list: FilterableSelectList, text: string) {
for (const ch of text) {
list.handleInput(ch);
}
}
it("clears the active filter before cancelling", () => {
const list = new FilterableSelectList(testItems, 5, mockTheme);
let cancelled = false;
list.onCancel = () => {
cancelled = true;
};
typeInput(list, "beta");
expect(list.getFilterText()).toBe("beta");
expect(list.getSelectedItem()?.value).toBe("session-2");
list.handleInput("\x1b");
expect(cancelled).toBe(false);
expect(list.getFilterText()).toBe("");
expect(list.render(80).join("\n")).toContain("first session");
expect(list.render(80).join("\n")).toContain("second session");
});
it("calls onCancel when escape is pressed with an empty filter", () => {
const list = new FilterableSelectList(testItems, 5, mockTheme);
let cancelled = false;
list.onCancel = () => {
cancelled = true;
};
list.handleInput("\x1b");
expect(cancelled).toBe(true);
});
it("calls onCancel when ctrl+c is pressed with an empty filter", () => {
const list = new FilterableSelectList(testItems, 5, mockTheme);
let cancelled = false;
list.onCancel = () => {
cancelled = true;
};
list.handleInput("\u0003");
expect(cancelled).toBe(true);
});
});

View File

@@ -1,7 +1,7 @@
import type { Component } from "@mariozechner/pi-tui";
import {
getEditorKeybindings,
Input,
getKeybindings,
matchesKey,
type SelectItem,
SelectList,
@@ -110,8 +110,8 @@ export class FilterableSelectList implements Component {
}
// Escape: clear filter or cancel
const kb = getKeybindings();
if (kb.matches(keyData, "tui.select.cancel")) {
const keybindings = getEditorKeybindings();
if (keybindings.matches(keyData, "selectCancel")) {
if (this.filterText) {
this.filterText = "";
this.input.setValue("");

View File

@@ -1,6 +1,6 @@
import {
type Component,
getKeybindings,
getEditorKeybindings,
Input,
isKeyRelease,
matchesKey,
@@ -362,8 +362,8 @@ export class SearchableSelectList implements Component {
return;
}
const kb = getKeybindings();
if (kb.matches(keyData, "tui.select.cancel")) {
const keybindings = getEditorKeybindings();
if (keybindings.matches(keyData, "selectCancel")) {
if (this.onCancel) {
this.onCancel();
}