Official
sidebar
Built-in sidebar 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/sidebar@0.2.4
Permissions in 0.2.4
Files
preview.ts3.5 KB
// The live preview line of a chat row: the tail of the agent's current
// message or tool title, kept from streamed events without a transcript.
import type { AgentEventKind, TranscriptItem } from "divergence";
export interface Preview {
line: string;
textId: string | null;
text: string;
toolId: string | null;
tool: string;
latest: "text" | "tool" | null;
revision: number;
loading: boolean;
}
export const createPreview = (): Preview => ({
line: "",
textId: null,
text: "",
toolId: null,
tool: "",
latest: null,
revision: 0,
loading: false,
});
const lastLine = (text: string): string =>
text
.split(/\r?\n/)
.map((line) => line.trim())
.filter(Boolean)
.at(-1) ?? "";
// A line of only brackets and marks ("}", ");", "---") says nothing.
const saysSomething = (line: string): boolean => /[\p{L}\p{N}]/u.test(line);
// The last line of a message that reads as prose: code in a fence (or a
// fence still streaming) is passed over, so a reply that ends in a code
// block shows the sentence before it, not its closing brace. A message of
// nothing but code shows its last line that says something.
const messageLine = (text: string): string => {
let fenced = false;
let prose = "";
let code = "";
for (const raw of text.split(/\r?\n/)) {
const line = raw.trim();
if (line.startsWith("```") || line.startsWith("~~~")) {
fenced = !fenced;
continue;
}
if (!saysSomething(line)) continue;
if (fenced) code = line;
else prose = line;
}
return prose || code;
};
// The row draws a message's line as plain text, so its Markdown marks
// would show as characters: a list or heading mark, a link's brackets,
// bold and code. A tool title is a command and keeps them.
const plain = (line: string): string =>
line
.replace(/^(?:#{1,6}\s+|>\s*|[-*+]\s+(?:\[[ xX]\]\s+)?|\d+[.)]\s+)/, "")
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
.replace(/\*\*|`/g, "");
// Keep only the current text tail (long enough to see a code block's
// fences) and tool title, never a second transcript.
// A late tool completion must not replace a newer assistant message.
export function updatePreview(preview: Preview, event: AgentEventKind): boolean {
const before = preview.line;
if (event.event === "text_delta" || event.event === "reasoning_delta") {
preview.text = (
(event.mode === "replace" || preview.textId !== event.itemId ? "" : preview.text) + event.text
).slice(-4096);
preview.textId = event.itemId;
const line = plain(messageLine(preview.text));
if (line) {
preview.line = line;
preview.latest = "text";
}
} else if (event.event === "tool_call_started") {
preview.toolId = event.id;
preview.tool = lastLine(event.title || event.name || "Tool call");
preview.line = preview.tool;
preview.latest = "tool";
} else if (event.event === "tool_call_updated" && event.id === preview.toolId && event.title) {
preview.tool = lastLine(event.title);
if (preview.latest === "tool") preview.line = preview.tool;
} else {
return false;
}
preview.revision++;
return before !== preview.line;
}
export function transcriptPreview(items: readonly TranscriptItem[]): Preview {
const preview = createPreview();
for (const item of items) {
if (item.role === "assistant" || item.role === "reasoning") {
updatePreview(preview, { event: "text_delta", itemId: item.id, text: item.text, mode: "replace" });
} else if (item.role === "tool") {
updatePreview(preview, { ...item.call, event: "tool_call_started" });
}
}
return preview;
}
No comments yet.