okami.mom/strata

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

Niko1221Task board: Schedule | Board view on Tasks, derivation with tests, loop record, sidebar count, all 8 localesdca9770ef

main
4.1 KiB70 linesraw
1import { tr, localeTag } from './i18n';
2import type { Task } from './tasks';
3import type { Session } from './types';
4import { loopEndNote } from './loop';
5import { sessionTitle } from './session-title';
6
7/**
8 * "Continue in a new chat" on a task: the first message of the new chat. It carries what the task found, what it did and the files it made, in the
9 * words the person reads, so the conversation can go on from the result (a finished task, or one that stopped and needs a person).
10 */
11const when = (iso?: string) => (iso ? new Date(iso).toLocaleString(localeTag(), { dateStyle: 'medium', timeStyle: 'short' }) : '');
12
13/** The task's saved answer as its parts: the answer, the list of what it did, and the note that something needed a yes ("What it did" is how the host writes it, server/task-report.mjs). */
14export function splitTaskOutput(output: string): { found: string; did: string[]; attention: string } {
15  let text = String(output ?? '').trim();
16  let attention = '';
17  const note = /\n\n> ([\s\S]*)$/.exec(text);
18  if (note) { attention = note[1].replace(/\n> /g, '\n').trim(); text = text.slice(0, note.index).trim(); }
19  const cut = /\n\n---\n\n\*\*What it did\*\*\n\n([\s\S]*)$/.exec(text);
20  const did = cut ? cut[1].split('\n').map(l => l.replace(/^-\s*/, '').trim()).filter(Boolean) : [];
21  return { found: (cut ? text.slice(0, cut.index) : text).trim(), did, attention };
22}
23
24const MAX_STEPS = 10, MAX_STEP = 160;
25
26export const taskHandoverTitle = (task: Task) => tr('Continuing from the task “{0}”', [task.title]);
27
28export function taskHandoverMessage(task: Task): string {
29  const failed = !!task.error;
30  const ran = when(task.lastRunAt);
31  const head = `**${taskHandoverTitle(task)}**${ran ? ` ${failed ? tr('(ran {0}, did not finish).', [ran]) : tr('(ran {0}, finished).', [ran])}` : ''}`;
32  const parts = [head];
33  if (failed) parts.push(`**${tr('What went wrong')}**\n\n${String(task.error).trim()}`);
34  const { found, did, attention } = splitTaskOutput(task.output ?? '');
35  if (found) parts.push(`**${tr('What it found')}**\n\n${found}`);
36  if (attention) parts.push(`**${tr('Needs your attention')}**\n\n${attention}`);
37  if (did.length) {
38    // the steps of a long run are many and long: the first ones, shortened
39    const shown = did.slice(0, MAX_STEPS).map(l => `- ${l.length > MAX_STEP ? `${l.slice(0, MAX_STEP - 1)}…` : l}`);
40    if (did.length > MAX_STEPS) shown.push(`- ${tr('…and {0} more steps', [did.length - MAX_STEPS])}`);
41    parts.push(`**${tr('What it did')}**\n\n${shown.join('\n')}`);
42  }
43  const files = (task.files ?? []).filter(f => f && typeof f.path === 'string' && f.path);
44  if (files.length) parts.push(`**${tr('Files')}**\n\n${files.map(f => `- ${f.path}`).join('\n')}`);
45  parts.push(tr('Please help me take it from here.'));
46  return parts.join('\n\n');
47}
48
49export const chatHandoverTitle = (session: Pick<Session, 'title' | 'messages' | 'defaultTitle'>) => tr('Continuing from the chat “{0}”', [sessionTitle(session)]);
50
51/**
52 * "Continue in a new chat" on a chat or a loop that went wrong (the board): what was asked last, what went wrong or what the last answer
53 * was, and, for a loop, how it ended, so the new chat can take it from there.
54 */
55export function chatHandoverMessage(session: Session): string {
56  const real = session.messages.filter(m => !m.local);
57  const answer = [...real].reverse().find(m => m.role === 'assistant');
58  const asked = [...real].reverse().find(m => m.role === 'user');
59  const parts = [`**${chatHandoverTitle(session)}**`];
60  const loop = session.loop;
61  if (loop) {
62    const ended = loop.endedWhy && loop.endedWhy !== 'stopped' && loop.endedWhy !== 'closed' ? `\n\n${loopEndNote(loop.endedWhy, loop.answers)}` : '';
63    parts.push(`**${tr('The loop')}**\n\n${loop.prompt}${ended}`);
64  } else if (asked?.content) parts.push(`**${tr('What I asked')}**\n\n${asked.content.trim().slice(0, 1500)}`);
65  if (answer?.error) parts.push(`**${tr('What went wrong')}**\n\n${String(answer.error).trim()}`);
66  const said = (answer?.content ?? '').trim() || (loop?.lastText ?? '');
67  if (said) parts.push(`**${tr('The last answer')}**\n\n${said.slice(0, 2000)}`);
68  parts.push(tr('Please help me take it from here.'));
69  return parts.join('\n\n');
70}