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
segments.ts9.4 KB
// The pills in a prompt: parts of its text the user picked from a picker
// (a file, a plugin's mention, a skill, a command), each drawn as one piece
// with an x (`tokens` on the prompt input). Pure: offsets are a JavaScript
// string's (UTF-16 units), as the kernel reports them.
import type { Mention } from "./mentions.ts";
export type SegmentKind = "file" | "mention" | "skill" | "command";
export type Segment = { start: number; end: number; kind: SegmentKind };
/// What a pick writes in the text, and so what is sent: `@path`, `@token`,
/// `$name` and `/name`.
export const written: Record<SegmentKind, (name: string) => string> = {
file: (path) => `@${path}`,
mention: (token) => `@${token}`,
skill: (name) => `$${name}`,
command: (name) => `/${name}`,
};
const KINDS = new Set<string>(Object.keys(written));
/// The pill's id for the kernel: its kind and its text, the same for every
/// pill of one thing.
export const segmentId = (text: string, segment: Segment) =>
`${segment.kind}:${text.slice(segment.start, segment.end)}`;
/// The pills the kernel reports, by the kind each id starts with.
export function fromTokens(tokens: readonly { id: string; start: number; end: number }[]): Segment[] {
return tokens.flatMap(({ id, start, end }) => {
const kind = id.slice(0, id.indexOf(":"));
return KINDS.has(kind) ? [{ start, end, kind: kind as SegmentKind }] : [];
});
}
export type Known = {
mentions: readonly Mention[] | undefined;
skills: readonly { name: string }[];
commands: readonly { name: string }[];
};
/// The file a `@` at `at` names: the longest of `files` (paths a pick, a
/// drop or a stored reference named, or typed paths found on disk, which
/// may hold spaces) written there and ending at a space, punctuation or
/// the end.
export function fileAt(text: string, at: number, files: readonly string[]): string | null {
let best: string | null = null;
for (const path of files)
if (
path &&
(!best || path.length > best.length) &&
text.startsWith(path, at + 1) &&
/^(?:$|[\s,.;:!?)\]])/.test(text.slice(at + 1 + path.length, at + 2 + path.length))
)
best = path;
return best;
}
/// The path a `@` at `at` may name when it was typed by hand: the word up
/// to the next space when it has a dot or a slash, without trailing
/// punctuation. It names a file only when the file is there (`@types/node`
/// is a package).
export function typedPath(text: string, at: number): string | null {
const word = /^\S+/.exec(text.slice(at + 1))?.[0] ?? "";
const path = word.replace(/[,.;:]+$/, "");
return /[./]/.test(word) && path ? path : null;
}
/// Whether `text` has `@path` written as a tag: at a word's start, and
/// ending where `fileAt` would end it.
export function writesFile(text: string, path: string): boolean {
for (let at = text.indexOf(`@${path}`); at >= 0; at = text.indexOf(`@${path}`, at + 1))
if ((at === 0 || /\s/.test(text[at - 1] ?? "")) && fileAt(text, at, [path]) === path) return true;
return false;
}
/// Whether a tagged path names a folder: it ends with a slash (`src/view/`),
/// as the file search writes one.
export const isFolder = (path: string) => path.endsWith("/");
/// A tagged path's last name, a folder's without its slash.
export function pathName(path: string) {
const bare = isFolder(path) ? path.slice(0, -1) : path;
return bare.slice(bare.lastIndexOf("/") + 1) || bare;
}
/// The folder a tagged path is in, empty at the top of the workspace.
export function parentOf(path: string) {
const bare = isFolder(path) ? path.slice(0, -1) : path;
return bare.slice(0, Math.max(0, bare.lastIndexOf("/")));
}
/// How a pill looks, in the prompt and in a sent message: its label and
/// its mark, an icon or the file whose icon it takes, from the word it
/// stands for (`@path`, `@token`, `$name`, `/name`).
export function pillLook(kind: SegmentKind, word: string, mentions: readonly Mention[] | undefined): { label: string; icon?: string; file?: string } {
const name = word.slice(1);
if (kind === "file") return isFolder(name) ? { label: pathName(name), icon: "folder" } : { label: pathName(name), file: pathName(name) };
if (kind === "skill") return { label: name, icon: "sparkles" };
if (kind === "command") return { label: name, icon: "square-slash" };
return { label: name, icon: mentions?.find((known) => known.token === name)?.icon ?? "bot" };
}
/// The paths of the file references among `blocks`.
export const filePaths = (blocks: readonly { type: string; path?: string }[]) =>
blocks.flatMap((block) => (block.type === "file_ref" && block.path ? [block.path] : []));
/// `known` with `more` after it, each path once.
export const withFiles = (known: readonly string[] | undefined, more: readonly string[]) => [...new Set([...(known ?? []), ...more])];
/// The pills of a text that says nothing about where they are, such as a
/// restored draft: each word a pick would have written. A `@` word is a
/// file as `fileAt` reads it, as it is when it is sent, so a path in
/// `files` keeps its spaces; a command only starts the text.
export function deriveSegments(text: string, known: Known, files: readonly string[] = []): Segment[] {
const tokens = new Set(known.mentions?.map((mention) => mention.token));
const skills = new Set(known.skills.map((skill) => skill.name));
const commands = new Set(known.commands.map((command) => command.name));
const segments: Segment[] = [];
const pattern = /(^|\s)([@$/])(\S+)/g;
let match;
while ((match = pattern.exec(text))) {
const start = match.index + (match[1] ?? "").length;
const sigil = match[2];
const word = match[3] ?? "";
let kind: SegmentKind | null = null;
let name = word;
if (sigil === "@" && tokens.has(word)) kind = "mention";
else if (sigil === "@" && (name = fileAt(text, start, files) ?? "")) kind = "file";
else if (sigil === "$" && skills.has(word)) kind = "skill";
else if (sigil === "/" && start === 0 && commands.has(word)) kind = "command";
if (kind && name) {
segments.push({ start, end: start + 1 + name.length, kind });
// A path with a space goes on past this word: look on after it.
pattern.lastIndex = Math.max(pattern.lastIndex, start + 1 + name.length);
}
}
return segments;
}
export type Trigger = { sigil: "@" | "$" | "/"; start: number; end: number; query: string };
/// The word before the caret when it starts with `@`, `$` or `/`: what a
/// picker completes. `start` is the sigil, `end` the caret, and the query
/// what is typed between them. Text after the caret stays: a pick never
/// eats a word the caret was typed against. Not inside a pill.
export function triggerAt(text: string, caret: number, segments: readonly Segment[]): Trigger | null {
let start = Math.min(caret, text.length);
while (start > 0 && !/\s/.test(text[start - 1] ?? "")) start--;
const sigil = text[start];
if (sigil !== "@" && sigil !== "$" && sigil !== "/") return null;
if (segments.some((segment) => segment.start < caret && start < segment.end)) return null;
return { sigil, start, end: caret, query: text.slice(start + 1, caret) };
}
/// The text with `range` replaced by a pick's pill and a space after it,
/// the pills moved with it, and the caret after the space.
export function insertSegment(
text: string,
segments: readonly Segment[],
range: { start: number; end: number },
kind: SegmentKind,
pill: string,
) {
const after = text.slice(range.end);
const space = after.startsWith(" ") ? "" : " ";
const delta = pill.length + space.length - (range.end - range.start);
const moved = segments
.filter((segment) => segment.end <= range.start || segment.start >= range.end)
.map((segment) =>
segment.start >= range.end ? { ...segment, start: segment.start + delta, end: segment.end + delta } : segment,
);
moved.push({ start: range.start, end: range.start + pill.length, kind });
moved.sort((a, b) => a.start - b.start);
return {
text: `${text.slice(0, range.start)}${pill}${space}${after}`,
segments: moved,
caret: range.start + pill.length + 1,
};
}
/// The pills after an edit from `prev` to `next` when the field does not
/// say where they are (an older kernel): those the edit did not touch,
/// moved by what it added or took away before them.
export function followEdit(prev: string, next: string, segments: readonly Segment[]): Segment[] {
if (prev === next) return [...segments];
let head = 0;
while (head < prev.length && head < next.length && prev[head] === next[head]) head++;
let tail = 0;
while (
tail < prev.length - head &&
tail < next.length - head &&
prev[prev.length - 1 - tail] === next[next.length - 1 - tail]
)
tail++;
const end = prev.length - tail;
const delta = next.length - prev.length;
return segments
.filter((segment) => segment.end <= head || segment.start >= end)
.map((segment) =>
segment.start >= end ? { ...segment, start: segment.start + delta, end: segment.end + delta } : segment,
);
}
/// The text with `range` taken out, and a space that leaves doubled, the
/// pills moved with it, and the caret where the range was.
export function removeRange(text: string, segments: readonly Segment[], range: { start: number; end: number }) {
const before = text.slice(0, range.start);
const end = (before === "" || /\s$/.test(before)) && text[range.end] === " " ? range.end + 1 : range.end;
const next = before + text.slice(end);
return { text: next, segments: followEdit(text, next, segments), caret: range.start };
}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.