okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221rc1 merge ds-coder (d6a5a7837)4fb068a8c

main
2.1 KiB33 linesraw
1import type { ActivityGroup } from './deliverables';
2import type { ToolDraft } from './types';
3
4/**
5 * A tool call the model is still writing (`message.drafts`, the host's `agent-tool-draft` frames: a whole file in one call, say) drawn through the
6 * open activity line: the verb and the file, its growing size and line count, and the tail of its text when that reads well.
7 */
8
9/** "18 KB", the way the "cut" sentence says it (thousands, not 1,024s), one decimal under 10 KB. */
10export function draftSize(bytes: number): string {
11  if (!(bytes > 0)) return '0 KB';
12  if (bytes < 100) return `${Math.round(bytes)} B`;
13  return bytes >= 10_000 ? `${Math.round(bytes / 1000)} KB` : `${(bytes / 1000).toFixed(1)} KB`;
14}
15
16/** The last lines of a draft's text, if they read well (words and code, not a wall of escapes); at most `count` lines of 160 characters, else none. */
17export function draftTail(preview: string | undefined, count = 3): string[] {
18  const lines = String(preview ?? '').replace(/\t/g, '  ').split(/\r?\n/).map(l => l.replace(/\s+$/, '')).filter(l => l.trim()).slice(-count).map(l => l.slice(0, 160));
19  const text = lines.join('');
20  if (text.length < 12) return [];
21  const printable = [...text].filter(c => c >= ' ' && c !== '\u007f').length / text.length;
22  const wordy = (text.match(/[A-Za-z]{2,}/g) ?? []).length;
23  return printable > 0.95 && wordy >= 2 ? lines : [];
24}
25
26/** Whether a draft has a place for its text (a file or a command): the box is kept from the start so the line does not grow when the text arrives. */
27export const draftHasTail = (d: Pick<ToolDraft, 'scope'>) => d.scope === 'write' || d.scope === 'shell';
28
29/** The activity line for one draft: `label` is the plain sentence ("Writing pagoda.html"), already in the person's language. */
30export function groupFromDraft(draft: ToolDraft, label: string): ActivityGroup {
31  const tail = draftHasTail(draft) ? draftTail(draft.preview) : [];
32  return { id: `draft-${draft.id}`, closed: false, line: label, added: 0, removed: 0, files: [], live: { label, bytes: draft.bytes, lines: draft.lines, ...(draftHasTail(draft) ? { tail: tail.join('\n') } : {}) } };
33}