Official
chat
Built-in chat UI (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/chat@0.2.8
Permissions in 0.2.8
Files
picker.test.ts20.4 KB
// The composer's pickers open at the caret anywhere in the text, and what
// they insert becomes a pill the field keeps whole.
import assert from "node:assert/strict";
import test from "node:test";
import * as Effect from "effect/Effect";
import { InputLedger, withController } from "./testing.ts";
import { acceptPicked, onChange, pickerKey } from "./composer.ts";
import { compositionFor, withFileRefs, withSkills } from "./composition.ts";
import { deriveSegments, filePaths, followEdit, fromTokens, insertSegment, triggerAt } from "./segments.ts";
import { entries, segmentsOf } from "./picker.ts";
import { markedMessage } from "./message-text.ts";
import { nodes, chat } from "./ui-for-tests.test.ts";
import { renderComposer } from "./views/composer.tsx";
import type { ChatView } from "./controller.ts";
class SearchLedger extends InputLedger {
searches: string[] = [];
override async call(method: string, raw: unknown): Promise<unknown> {
if (method !== "host/fs.search") return super.call(method, raw);
const query = String((raw as { query: string }).query);
this.searches.push(query);
return { paths: ["src/main.rs", "src/lib/mod.rs", "README.md"].filter((path) => path.includes(query)) };
}
}
test("a picker opens for the word at the caret, wherever it is in the text", () => {
assert.deepEqual(triggerAt("look at @src now", 12, []), { sigil: "@", start: 8, end: 12, query: "src" });
assert.deepEqual(triggerAt("fix it with $de please", 15, []), { sigil: "$", start: 12, end: 15, query: "de" });
assert.deepEqual(triggerAt("then /rev", 9, []), { sigil: "/", start: 5, end: 9, query: "rev" });
assert.deepEqual(triggerAt("/co", 3, []), { sigil: "/", start: 0, end: 3, query: "co" });
// Typed against a word: taking an entry leaves that word after it.
assert.deepEqual(triggerAt("@srcand", 4, []), { sigil: "@", start: 0, end: 4, query: "src" });
assert.equal(insertSegment("@srcand", [], { start: 0, end: 4 }, "file", "@src/a.rs").text, "@src/a.rs and");
assert.equal(triggerAt("and/or", 6, []), null, "a sigil inside a word is text");
assert.equal(triggerAt("mail me@host", 12, []), null);
assert.equal(triggerAt("@a.rs", 5, [{ start: 0, end: 5, kind: "file" }]), null, "not inside a pill");
assert.equal(triggerAt("@a.rsx", 6, [{ start: 0, end: 5, kind: "file" }]), null, "nor typed onto one");
});
test("taking an entry puts its pill in place of the word, moves later pills and puts the caret after it", () => {
const text = "see @ma and $plan";
const next = insertSegment(text, [{ start: 12, end: 17, kind: "skill" }], { start: 4, end: 7 }, "file", "@src/main.rs");
assert.equal(next.text, "see @src/main.rs and $plan");
assert.deepEqual(next.segments, [
{ start: 4, end: 16, kind: "file" },
{ start: 21, end: 26, kind: "skill" },
]);
assert.equal(next.caret, 17, "after the space that was there already");
const end = insertSegment("run /re", [], { start: 4, end: 7 }, "skill", "$review");
assert.equal(end.text, "run $review ");
assert.equal(end.caret, end.text.length);
});
test("without the field's word on them, pills follow the edits that leave them whole", () => {
const pills = [
{ start: 0, end: 5, kind: "file" as const },
{ start: 10, end: 15, kind: "skill" as const },
];
assert.deepEqual(followEdit("@a.rs and $plan", "@a.rs, and $plan", pills), [
{ start: 0, end: 5, kind: "file" },
{ start: 11, end: 16, kind: "skill" },
]);
assert.deepEqual(followEdit("@a.rs and $plan", "@a.r and $plan", pills), [{ start: 9, end: 14, kind: "skill" }]);
assert.deepEqual(fromTokens([{ id: "skill:$plan", start: 3, end: 8 }, { id: "other", start: 0, end: 1 }]), [
{ start: 3, end: 8, kind: "skill" },
]);
});
test("a restored text gets the pills a pick would have made", () => {
const known = {
mentions: [{ token: "codex/gpt", label: "Codex", prompt: "" }],
skills: [{ name: "plan" }],
commands: [{ name: "clear" }],
};
const text = "/clear look at @src/a.rs, ask @codex/gpt with $plan and $HOME /clear";
assert.deepEqual(
deriveSegments(text, known, ["src/a.rs"]).map((segment) => [segment.kind, text.slice(segment.start, segment.end)]),
[
["command", "/clear"],
["file", "@src/a.rs"],
["mention", "@codex/gpt"],
["skill", "$plan"],
],
);
});
/// A file whose path has a space used to split at it: the pill, the
/// reference sent and the draft read back all ended at the space.
test("a tagged path with a space stays one pill, goes as one reference and comes back whole", async () => {
const known = { mentions: [], skills: [], commands: [] };
const text = "see @docs/My Notes.md, then @src/a.rs";
assert.deepEqual(
deriveSegments(text, known, ["docs/My Notes.md", "src/a.rs"]).map((segment) => text.slice(segment.start, segment.end)),
["@docs/My Notes.md", "@src/a.rs"],
);
assert.deepEqual(withFileRefs([{ type: "text", text }], new Set(), ["docs/My Notes.md", "src/a.rs"]), [
{ type: "text", text: "see " },
{ type: "file_ref", path: "docs/My Notes.md" },
{ type: "text", text: ", then " },
{ type: "file_ref", path: "src/a.rs" },
]);
// Untagged, it is no pill at all.
assert.deepEqual(deriveSegments("see @docs/My Notes.md", known), []);
class SpacedLedger extends InputLedger {
override async call(method: string, raw: unknown): Promise<unknown> {
if (method !== "host/fs.search") return super.call(method, raw);
return { paths: ["docs/My Notes.md"] };
}
}
await withController(new SpacedLedger(), (c) =>
Effect.gen(function* () {
c.state.chats.c1 = chat({ id: "c1" });
const composer = c.composer("c1");
yield* onChange(c, "c1", "see @no", { caret: 7, tokens: [] });
for (let tries = 0; composer.filesFor !== "no" && tries < 100; tries++) yield* Effect.sleep(5);
assert.equal(yield* acceptPicked(c, "c1"), true);
assert.equal(composer.text, "see @docs/My Notes.md ");
// A draft keeps its text with its files after it.
const draft = yield* compositionFor(c, "c1", composer.text, [], [], false);
assert.deepEqual(filePaths(draft.blocks), ["docs/My Notes.md"]);
// Read back in a box that never saw the pick, the stored reference makes the pill.
composer.tagged = filePaths(draft.blocks);
composer.segments = undefined;
assert.deepEqual(segmentsOf(c, composer), [{ start: 4, end: 21, kind: "file" }]);
}),
);
});
test("a skill anywhere goes as a skill block where it stands; one that starts the message takes the rest", () => {
const skills = [{ name: "plan" }, { name: "review" }];
assert.deepEqual(withSkills("$review the parser\nand the lexer", skills), [
{ type: "skill", name: "review", input: "the parser\nand the lexer" },
]);
assert.deepEqual(withSkills("fix it with $plan please", skills), [
{ type: "text", text: "fix it with " },
{ type: "skill", name: "plan" },
{ type: "text", text: " please" },
]);
assert.deepEqual(withSkills("costs $5 and $HOME", skills), [{ type: "text", text: "costs $5 and $HOME" }]);
});
test("@ in the middle of the text searches files at the caret, opens the list at the `@`, and taking one makes a pill there", async () => {
const ledger = new SearchLedger();
await withController(ledger, (c) =>
Effect.gen(function* () {
c.state.chats.c1 = chat({ id: "c1" });
const composer = c.composer("c1");
yield* onChange(c, "c1", "look at @ma now", { caret: 11, tokens: [] });
assert.deepEqual(composer.picker, { kind: "files", query: "ma", start: 8, end: 11 });
for (let tries = 0; composer.filesFor !== "ma" && tries < 100; tries++) yield* Effect.sleep(5);
assert.deepEqual(
entries(c, composer).map((entry) => [entry.label, entry.detail]),
[["main.rs", "src"]],
);
const view = { controller: c, ctx: { state: {}, app: { route: "chat", chatId: "c1" }, plugin: "chat", update() {}, run() {} } } as unknown as ChatView;
const popover = () => nodes(renderComposer(view, c.state.chats.c1)).find((node) => node.id === "picker-at-c1");
assert.deepEqual(popover()?.at, { input: "prompt-c1", offset: 8 }, "the list opens at the `@`, not at the box's edge");
assert.equal(yield* acceptPicked(c, "c1"), true);
assert.equal(composer.text, "look at @src/main.rs now");
assert.deepEqual(composer.segments?.list, [{ start: 8, end: 20, kind: "file" }]);
assert.deepEqual(composer.place, { text: composer.text, at: 21 }, "the caret goes after the pill, not to the end");
assert.equal(composer.picker, null);
assert.equal(popover()?.at, undefined);
// The prompt draws the pill and puts the caret there.
const prompt = nodes(renderComposer(view, c.state.chats.c1)).find((node) => node.id === "prompt-c1");
assert.deepEqual(prompt?.tokens, [{ id: "file:@src/main.rs", start: 8, end: 20, label: "main.rs", file: "main.rs" }]);
assert.equal(prompt?.caret, 21);
// The field reports the pill as the user types after it.
yield* onChange(c, "c1", "look at @src/main.rs now!", { caret: 25, tokens: [{ id: "file:@src/main.rs", start: 8, end: 20 }] });
assert.deepEqual(composer.segments?.list, [{ start: 8, end: 20, kind: "file" }]);
assert.equal(composer.picker, null);
}),
);
});
test("a row the keys moved to stays highlighted when the search answers with other rows", async () => {
const gate = Promise.withResolvers<void>();
class SlowLedger extends InputLedger {
override async call(method: string, raw: unknown): Promise<unknown> {
if (method !== "host/fs.search") return super.call(method, raw);
if ((raw as { query: string }).query === "s") return { paths: ["src/main.rs", "src/lib/mod.rs"] };
await gate.promise;
return { paths: ["src/new.rs", "src/main.rs", "src/lib/mod.rs"] };
}
}
await withController(new SlowLedger(), (c) =>
Effect.gen(function* () {
const composer = c.composer("c1");
yield* onChange(c, "c1", "@s", { caret: 2, tokens: [] });
for (let tries = 0; composer.filesFor !== "s" && tries < 100; tries++) yield* Effect.sleep(5);
yield* onChange(c, "c1", "@sr", { caret: 3, tokens: [] });
pickerKey(c, "c1", "down");
const chosen = () => entries(c, composer)[composer.picker?.index ?? 0]?.id;
assert.equal(chosen(), "file-src/lib/mod.rs");
gate.resolve();
for (let tries = 0; composer.filesFor !== "sr" && tries < 100; tries++) yield* Effect.sleep(5);
assert.equal(chosen(), "file-src/lib/mod.rs");
}),
);
});
test("/ starts commands then skills at the start of the text, the app's commands and skills elsewhere, and $ skills anywhere", async (ctx) => {
const ledger = new SearchLedger();
await withController(ledger, (c) =>
Effect.gen(function* () {
ctx.mock.method(c.api, "slashCommands", () => [
{ name: "clear", plugin: "chat", description: "New chat", inputHint: null },
]);
const composer = c.composer("c1");
composer.skills = [
{ name: "code-review", description: "Review a change" },
{ name: "plan", description: "Plan the work, then clear doubts" },
];
const labels = () => entries(c, composer).map((entry) => `${entry.kind}:${entry.label}`);
yield* onChange(c, "c1", "/c", { caret: 2, tokens: [] });
assert.deepEqual(labels(), ["command:clear", "skill:code-review", "skill:plan"], "a name, then a description that holds it");
// A skill the agent also lists as a command shows once, as the command.
composer.commands = [{ name: "plan", description: "Plan the work" }];
yield* onChange(c, "c1", "/pl", { caret: 3, tokens: [] });
assert.deepEqual(labels(), ["command:plan"]);
composer.commands = [];
yield* onChange(c, "c1", "please /c", { caret: 9, tokens: [] });
assert.equal(composer.picker?.kind, "slash");
assert.deepEqual(labels(), ["skill:code-review", "skill:plan"]);
pickerKey(c, "c1", "down");
assert.equal(yield* acceptPicked(c, "c1", false), true);
assert.equal(composer.text, "please $plan ", "a skill from / is written as $name");
yield* onChange(c, "c1", "please $plan and $rev", { caret: 21, tokens: [{ id: "skill:$plan", start: 7, end: 12 }] });
assert.deepEqual(labels(), ["skill:code-review"]);
// A $ no skill matches is plain text: no picker, Enter is free.
yield* onChange(c, "c1", "costs $5", { caret: 8, tokens: [] });
assert.equal(entries(c, composer).length, 0);
}),
);
});
test("Escape closes the picker for its word until the caret leaves it", async () => {
const ledger = new SearchLedger();
await withController(ledger, (c) =>
Effect.gen(function* () {
const composer = c.composer("c1");
const kind = () => composer.picker?.kind ?? null;
composer.skills = [{ name: "plan", description: "" }];
yield* onChange(c, "c1", "use $p", { caret: 6, tokens: [] });
assert.equal(kind(), "skills");
pickerKey(c, "c1", "escape");
yield* onChange(c, "c1", "use $pl", { caret: 7, tokens: [] });
assert.equal(kind(), null, "typing on in the word leaves it closed");
yield* onChange(c, "c1", "use $pl $p", { caret: 10, tokens: [] });
assert.equal(kind(), "skills", "a new word opens it again");
}),
);
});
test("the picker keeps its rows, or its note, while the search for what is typed now is on its way", async () => {
const answers: Record<string, string[]> = { c: ["crates/composer.ts", "core/a.ts"], zz: [] };
const gate = Promise.withResolvers<void>();
class SlowLedger extends InputLedger {
override async call(method: string, raw: unknown): Promise<unknown> {
if (method !== "host/fs.search") return super.call(method, raw);
const query = (raw as { query: string }).query;
if (!(query in answers)) await gate.promise;
return { paths: answers[query] ?? [] };
}
}
await withController(new SlowLedger(), (c) =>
Effect.gen(function* () {
c.state.chats.c1 = chat({ id: "c1" });
const composer = c.composer("c1");
const view = { controller: c, ctx: { state: {}, app: { route: "chat", chatId: "c1" }, plugin: "chat", update() {}, run() {} } } as unknown as ChatView;
const card = () => nodes(renderComposer(view, c.state.chats.c1)).find((node) => node.id === "picker-c1");
const rows = () => entries(c, composer).map((entry) => entry.id);
yield* onChange(c, "c1", "@c", { caret: 2, tokens: [] });
for (let tries = 0; composer.filesFor !== "c" && tries < 100; tries++) yield* Effect.sleep(5);
// The host matches letters in order, not only runs of them.
yield* onChange(c, "c1", "@cmp", { caret: 4, tokens: [] });
assert.deepEqual(rows(), ["file-crates/composer.ts"]);
// No earlier row matches: they all stay rather than the picker closing.
yield* onChange(c, "c1", "@cmpx", { caret: 5, tokens: [] });
assert.deepEqual(rows(), ["file-crates/composer.ts", "file-core/a.ts"]);
assert.ok(card(), "the picker stays open");
// An answer with nothing keeps saying so while the next is on its way.
yield* onChange(c, "c1", "@zz", { caret: 3, tokens: [] });
for (let tries = 0; composer.filesFor !== "zz" && tries < 100; tries++) yield* Effect.sleep(5);
yield* onChange(c, "c1", "@zzz", { caret: 4, tokens: [] });
assert.ok(nodes(card()).some((node) => node.text === "No matching files"));
gate.resolve();
}),
);
});
test("a folder is listed with the files, and taking it tags it and sends it as a folder reference", async () => {
const searched: unknown[] = [];
class FolderLedger extends InputLedger {
override async call(method: string, raw: unknown): Promise<unknown> {
if (method !== "host/fs.search") return super.call(method, raw);
searched.push(raw);
return { paths: ["src/lib/", "src/lib/mod.rs"] };
}
}
await withController(new FolderLedger(), (c) =>
Effect.gen(function* () {
c.state.chats.c1 = chat({ id: "c1" });
const composer = c.composer("c1");
yield* onChange(c, "c1", "see @lib", { caret: 8, tokens: [] });
for (let tries = 0; composer.filesFor !== "lib" && tries < 100; tries++) yield* Effect.sleep(5);
assert.equal((searched[0] as { folders?: boolean }).folders, true, "the search is asked for folders");
const [folder] = entries(c, composer);
assert.deepEqual([folder?.label, folder?.detail, folder?.icon, folder?.file], ["lib", "src", "folder", undefined]);
assert.equal(yield* acceptPicked(c, "c1"), true);
assert.equal(composer.text, "see @src/lib/ ");
const view = { controller: c, ctx: { state: {}, app: { route: "chat", chatId: "c1" }, plugin: "chat", update() {}, run() {} } } as unknown as ChatView;
const prompt = nodes(renderComposer(view, c.state.chats.c1)).find((node) => node.id === "prompt-c1");
assert.deepEqual(prompt?.tokens, [{ id: "file:@src/lib/", start: 4, end: 13, label: "lib", icon: "folder" }]);
const { blocks } = yield* compositionFor(c, "c1", composer.text, []);
assert.deepEqual(filePaths(blocks), ["src/lib/"]);
assert.equal(markedMessage("see @src/lib/", blocks, { mentions: [], skills: [], commands: [] }), 'see [`lib`](<> "divergence:pill:folder")', "a folder opens nothing, so it has no link");
}),
);
});
test("a failed file search says so in the picker", async () => {
class FailingLedger extends InputLedger {
override async call(method: string, raw: unknown): Promise<unknown> {
if (method === "host/fs.search") throw new Error("no access to this workspace");
return super.call(method, raw);
}
}
await withController(new FailingLedger(), (c) =>
Effect.gen(function* () {
c.state.chats.c1 = chat({ id: "c1" });
const composer = c.composer("c1");
yield* onChange(c, "c1", "@src", { caret: 4, tokens: [] });
for (let tries = 0; composer.filesFor !== "src" && tries < 100; tries++) yield* Effect.sleep(5);
assert.match(composer.filesError ?? "", /^Could not search files: .*no access/);
}),
);
});
test("Enter takes the last entry when the list got shorter under the highlight", async () => {
const ledger = new SearchLedger();
await withController(ledger, (c) =>
Effect.gen(function* () {
const composer = c.composer("c1");
composer.text = "@src";
composer.picker = { kind: "files", query: "src", start: 0, end: 4, index: 2 };
composer.files = ["src/main.rs"];
composer.filesFor = "src";
assert.equal(yield* acceptPicked(c, "c1"), true, "the text is not sent past the end of the list");
assert.equal(composer.text, "@src/main.rs ");
}),
);
});
test("a path typed by hand goes as a reference only when the file is there", async () => {
const ledger = new InputLedger();
ledger.files.add("src/main.ts");
await withController(ledger, (c) =>
Effect.gen(function* () {
const { blocks } = yield* compositionFor(c, "c1", "upgrade @types/node, ping @foo.bar and read @src/main.ts.", []);
assert.deepEqual(blocks, [
{ type: "text", text: "upgrade @types/node, ping @foo.bar and read " },
{ type: "file_ref", path: "src/main.ts" },
{ type: "text", text: "." },
]);
}),
);
});
test("a file reference goes where it stands, and a message reads back as written, old ones too", async () => {
assert.deepEqual(withFileRefs([{ type: "text", text: "Use @README.md and @src/a.ts, then @codex/gpt" }], new Set(["@codex/gpt"]), ["README.md", "src/a.ts"]), [
{ type: "text", text: "Use " },
{ type: "file_ref", path: "README.md" },
{ type: "text", text: " and " },
{ type: "file_ref", path: "src/a.ts" },
{ type: "text", text: ", then @codex/gpt" },
]);
assert.deepEqual(withFileRefs([{ type: "skill", name: "review", input: "@a.ts please" }], new Set(), ["a.ts"]), [
{ type: "skill", name: "review", input: "@a.ts please" },
{ type: "file_ref", path: "a.ts" },
]);
await withController(new InputLedger(), (c) =>
Effect.sync(() => {
const placed = withFileRefs([{ type: "text", text: "Read @README.md and say hi" }], new Set(), ["README.md"]);
assert.equal(c.plainText(placed), "Read @README.md and say hi");
// Saved before references stood in place: the path is in the text.
assert.equal(c.plainText([{ type: "text", text: "Read @a.ts now" }, { type: "file_ref", path: "a.ts" }]), "Read @a.ts now");
assert.equal(c.plainText([{ type: "skill", name: "review", input: "the parser" }]), "$review the parser");
assert.equal(
c.plainText([{ type: "text", text: "Ask" }, { type: "text", text: "\n\nDelegate it" }]),
"Ask\n\nDelegate it",
);
}),
);
});Versions
| Version | Published | Plugin API | Size | Permissions | Status |
|---|---|---|---|---|---|
| 0.2.9latest | Nov 21, 2026 | >=5 <6 | 349 KB | 5 permissions | Listed |
| 0.2.8 | Nov 16, 2026 | >=5 <6 | 348.6 KB | 5 permissions | Listed |
| 0.2.7 | Nov 11, 2026 | >=5 <6 | 314.3 KB | 5 permissions | Listed |
| 0.2.6 | Nov 9, 2026 | >=5 <6 | 306.2 KB | 5 permissions | Listed |
| 0.2.5 | Oct 27, 2026 | >=2 <4 | 178.6 KB | 5 permissions | Listed |
No comments yet.