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.9
Permissions in 0.2.9
Files
history.ts4 KB
// Prompt history: in an empty prompt box, Up brings back the prompts sent in
// this chat, the newest first, and Down goes forward again, past the newest
// to the empty box. The field does not tell this plugin where its caret is
// until its text changes, so the arrows keep going through the prompts for
// as long as the box still shows the one they brought back, wherever the
// caret is, and hand back to the field once the user types in it.
import { filePaths, withFiles } from "./segments.ts";
import * as Effect from "effect/Effect";
import type { Controller } from "./controller.ts";
import type { Composer } from "./state.ts";
import { textChanged } from "./data.ts";
export type Recalled = { id: string; text: string; files?: readonly string[] };
export type ArrowKey = "up" | "down";
/**
* The prompts the user sent in this chat, oldest first, as they wrote them (the text the Edit
* button starts from). A message another agent sent is not the user's, and a prompt sent twice in
* a row counts once.
*/
export function promptHistory(c: Controller, chatId: string): Recalled[] {
const entries: Recalled[] = [];
for (const item of c.state.transcripts[chatId]?.items ?? []) {
if (item.role !== "user" || item.from) continue;
const text = c.plainText(item.blocks).trim();
if (!text) continue;
const files = filePaths(item.blocks),
entry = { id: item.id, text, ...(files.length ? { files } : {}) };
if (entries[entries.length - 1]?.text === text) entries[entries.length - 1] = entry;
else entries.push(entry);
}
return entries;
}
/** Which of `entries` the box shows, or -1 when it shows its own text. */
function shownIndex(entries: readonly Recalled[], composer: Composer): number {
const recalled = composer.recalled;
if (recalled?.text !== composer.text) return -1;
for (let at = entries.length - 1; at >= 0; at--) if (entries[at]!.id === recalled.id) return at;
return -1;
}
/**
* Which entry Up or Down shows next, from the one shown (`-1`, the box's own text): Up goes to an
* older prompt and stays on the oldest, Down to a newer one and past the newest to the empty box.
*/
export function nextShown(count: number, shown: number, key: ArrowKey): number {
if (key === "up") return shown < 0 ? count - 1 : Math.max(shown - 1, 0);
return shown < 0 || shown + 1 >= count ? -1 : shown + 1;
}
/**
* The arrows the prompt field hands to this plugin: both while it shows a prompt they brought back,
* Up while it is empty and the chat has prompts to bring back, and none otherwise. An image in the
* box counts as something typed. The field keeps the ones that still move its caret, so in a prompt
* of several lines only Up on the first line and Down on the last arrive (`arrowsAtEdges`).
*/
export function historyKeys(c: Controller, chatId: string): ArrowKey[] {
const composer = c.composer(chatId);
if (composer.recalled?.text === composer.text) return ["up", "down"];
const empty = !composer.text.trim() && !composer.attachments.length && !composer.carriedBlocks?.length;
return empty && promptHistory(c, chatId).length ? ["up"] : [];
}
/**
* Puts the prompt Up or Down takes the box to in it, with the caret at its end. The keys the field
* handed over were the ones of its last render; a key that no longer applies (the user typed since)
* does nothing.
*/
export const recallPrompt = Effect.fn("Chat.recallPrompt")(function* (c: Controller, chatId: string, key: ArrowKey) {
if (!historyKeys(c, chatId).includes(key)) return;
const composer = c.composer(chatId),
entries = promptHistory(c, chatId);
const shown = shownIndex(entries, composer),
next = nextShown(entries.length, shown, key);
if (next === shown) return;
composer.recalled = entries[next];
composer.text = composer.recalled?.text ?? "";
composer.tagged = withFiles(composer.tagged, composer.recalled?.files ?? []);
composer.caret = composer.text.length;
composer.place = { text: composer.text, at: composer.caret };
c.refreshComposer(chatId);
yield* textChanged(c, chatId);
});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.