gcf-board 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -8
- package/dist/server/app.js +161 -20
- package/dist/server/chat.js +13 -0
- package/dist/server/files.js +5 -2
- package/dist/server/mcp.js +28 -0
- package/dist/server/moves.js +11 -0
- package/dist/server/propose.js +96 -0
- package/dist/server/runs.js +23 -115
- package/dist/server/talks.js +498 -0
- package/dist/web/assets/index-D_F7OEyk.js +36 -0
- package/dist/web/index.html +1 -1
- package/node_modules/gcf-session/lib/activity.js +371 -0
- package/node_modules/gcf-session/lib/adapter/claude.js +226 -0
- package/node_modules/gcf-session/lib/adapter/index.js +33 -0
- package/node_modules/gcf-session/lib/chat-input.js +260 -0
- package/node_modules/gcf-session/lib/chat.js +832 -0
- package/node_modules/gcf-session/lib/cli.js +52 -0
- package/node_modules/gcf-session/lib/cost.js +29 -0
- package/node_modules/gcf-session/lib/index.js +58 -0
- package/node_modules/gcf-session/lib/job.js +417 -0
- package/node_modules/gcf-session/lib/limit.js +132 -0
- package/node_modules/gcf-session/lib/lock.js +81 -0
- package/node_modules/gcf-session/lib/proc.js +93 -0
- package/node_modules/gcf-session/lib/session.js +320 -0
- package/node_modules/gcf-session/lib/store.js +28 -0
- package/node_modules/gcf-session/lib/thread.js +423 -0
- package/node_modules/gcf-session/lib/windows.js +46 -0
- package/node_modules/gcf-session/package.json +23 -0
- package/node_modules/gcf-session/types/index.d.ts +152 -0
- package/package.json +10 -3
- package/dist/web/assets/index-DpzFCrLV.js +0 -36
|
@@ -0,0 +1,832 @@
|
|
|
1
|
+
// The chat of a live session (docs/Chat-Plan.md, Harness-PRD §10 "чат"): the session reads its
|
|
2
|
+
// input on stdin and stays open between turns. The operator writes into
|
|
3
|
+
// `.gcf-session/runs/<id>.chat-in.jsonl` (`gcf-harness chat`, which the board calls); what the
|
|
4
|
+
// operator reads goes to `<id>.chat.jsonl`, one event a line: the talk, the questions, the
|
|
5
|
+
// steps of the run, the sessions. The chat reads the session's neutral events and writes its
|
|
6
|
+
// input in the adapter's encoding; the raw stream stays in `<id>.log`.
|
|
7
|
+
import {
|
|
8
|
+
appendFileSync,
|
|
9
|
+
closeSync,
|
|
10
|
+
lstatSync,
|
|
11
|
+
openSync,
|
|
12
|
+
readFileSync,
|
|
13
|
+
readSync,
|
|
14
|
+
statSync,
|
|
15
|
+
} from 'node:fs';
|
|
16
|
+
import path from 'node:path';
|
|
17
|
+
import { CHAT_FLAGS, encode as claudeInput } from './adapter/claude.js';
|
|
18
|
+
|
|
19
|
+
export { CHAT_FLAGS };
|
|
20
|
+
|
|
21
|
+
/** How long a question waits for the operator when neither the flag nor the config says. */
|
|
22
|
+
export const CHAT_TIMEOUT_MIN = 15;
|
|
23
|
+
|
|
24
|
+
/** The version of the chat events, on each `session` event (AD-24): fields and types only add. */
|
|
25
|
+
export const EVENTS_V = 1;
|
|
26
|
+
|
|
27
|
+
/** What the operator's silence says, past the answer timeout, unless the session's work says. */
|
|
28
|
+
export const DEFAULT_ANSWER = 'No answer came in time: take your safe default.';
|
|
29
|
+
|
|
30
|
+
/** The denial of a question card in a session that asks the operator nothing (`ask: none`). */
|
|
31
|
+
export const ASK_NONE = 'answer it yourself: this session asks the operator nothing';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A turn ended with messages the agent has not echoed as read: past this with no new turn, they
|
|
35
|
+
* count as read. An echo that never comes must not hold the session for good.
|
|
36
|
+
*/
|
|
37
|
+
export const READ_GRACE_MS = 10_000;
|
|
38
|
+
|
|
39
|
+
/** The pictures a message may carry, by extension: what claude takes as an image block. */
|
|
40
|
+
export const IMAGE_TYPES = {
|
|
41
|
+
'.png': 'image/png',
|
|
42
|
+
'.jpg': 'image/jpeg',
|
|
43
|
+
'.jpeg': 'image/jpeg',
|
|
44
|
+
'.gif': 'image/gif',
|
|
45
|
+
'.webp': 'image/webp',
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const RECOMMENDED = / \(Recommended\)$/;
|
|
49
|
+
const POLL_MS = 500;
|
|
50
|
+
|
|
51
|
+
export const chatIn = (logs, id) => path.join(logs, `${id}.chat-in.jsonl`);
|
|
52
|
+
export const chatOut = (logs, id) => path.join(logs, `${id}.chat.jsonl`);
|
|
53
|
+
|
|
54
|
+
/** The size of a file, 0 when it is not there: where the operator's next input starts. */
|
|
55
|
+
export function sizeOf(file) {
|
|
56
|
+
try {
|
|
57
|
+
return statSync(file).size;
|
|
58
|
+
} catch {
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The new whole lines of `file` from `from` on, and where the next read starts. */
|
|
64
|
+
export function readFrom(file, from) {
|
|
65
|
+
let fd;
|
|
66
|
+
try {
|
|
67
|
+
fd = openSync(file, 'r');
|
|
68
|
+
} catch {
|
|
69
|
+
return { lines: [], next: from };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
const buf = Buffer.alloc(Math.max(sizeOf(file) - from, 0));
|
|
74
|
+
const n = readSync(fd, buf, 0, buf.length, from);
|
|
75
|
+
// A line still being written waits for its newline; the offset counts bytes, not letters.
|
|
76
|
+
const end = buf.subarray(0, n).lastIndexOf(10) + 1;
|
|
77
|
+
const lines = buf.subarray(0, end).toString('utf8').split('\n').filter(Boolean);
|
|
78
|
+
|
|
79
|
+
return { lines, next: from + end };
|
|
80
|
+
} finally {
|
|
81
|
+
closeSync(fd);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The new whole lines of `file` from `from` on, each with the offset after it: where the read
|
|
87
|
+
* goes on once that line is done.
|
|
88
|
+
*/
|
|
89
|
+
function readEntries(file, from) {
|
|
90
|
+
const { lines, next } = readFrom(file, from);
|
|
91
|
+
if (!lines.length) return [];
|
|
92
|
+
// readFrom drops blank lines: the offsets are counted over the raw bytes.
|
|
93
|
+
const raw = readRaw(file, from, next);
|
|
94
|
+
const out = [];
|
|
95
|
+
let at = from;
|
|
96
|
+
for (const part of raw.split('\n').slice(0, -1)) {
|
|
97
|
+
at += Buffer.byteLength(part) + 1;
|
|
98
|
+
if (part) out.push({ line: part, end: at });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The bytes of `file` from `from` to `to`, as text. */
|
|
105
|
+
function readRaw(file, from, to) {
|
|
106
|
+
const fd = openSync(file, 'r');
|
|
107
|
+
try {
|
|
108
|
+
const buf = Buffer.alloc(to - from);
|
|
109
|
+
readSync(fd, buf, 0, buf.length, from);
|
|
110
|
+
|
|
111
|
+
return buf.toString('utf8');
|
|
112
|
+
} finally {
|
|
113
|
+
closeSync(fd);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A question card's answers past the timeout: each one's recommended option, or `fallback`.
|
|
119
|
+
*/
|
|
120
|
+
export function defaultAnswers(questions = [], fallback = DEFAULT_ANSWER) {
|
|
121
|
+
return Object.fromEntries(
|
|
122
|
+
questions.map((q) => {
|
|
123
|
+
const pick = (q.options ?? []).find((o) => RECOMMENDED.test(o.label ?? ''));
|
|
124
|
+
|
|
125
|
+
return [q.question, pick ? pick.label.replace(RECOMMENDED, '') : fallback];
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const parse = (line) => {
|
|
131
|
+
try {
|
|
132
|
+
return JSON.parse(line);
|
|
133
|
+
} catch {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/** Add an event to what the operator reads of item `id`; a file that cannot be written loses it. */
|
|
139
|
+
export function postTo(file, e) {
|
|
140
|
+
try {
|
|
141
|
+
appendFileSync(file, `${JSON.stringify({ at: new Date().toISOString(), ...e })}\n`);
|
|
142
|
+
} catch {
|
|
143
|
+
// The chat file is the operator's view: the session goes on without it.
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const post = (c, e) => postTo(c.out, e);
|
|
148
|
+
|
|
149
|
+
/** Write one encoded input to the session, while its stdin is open. */
|
|
150
|
+
const send = (c, text) => {
|
|
151
|
+
if (!c.stdin.writableEnded) c.stdin.write(text);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/** The session waits for the operator: the agent's next event says it works again. */
|
|
155
|
+
function waiting(c, kind) {
|
|
156
|
+
c.idle = true;
|
|
157
|
+
post(c, { type: 'waiting', kind });
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The agent's first event after a wait (fix B). */
|
|
161
|
+
function working(c) {
|
|
162
|
+
if (!c.idle) return;
|
|
163
|
+
c.idle = false;
|
|
164
|
+
post(c, { type: 'working' });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Wait for the operator; `onTimeout` answers for them when the answer time passes. */
|
|
168
|
+
function waitFor(c, kind, onTimeout) {
|
|
169
|
+
if (c.wait) return;
|
|
170
|
+
c.pause(true);
|
|
171
|
+
waiting(c, kind);
|
|
172
|
+
c.wait = setTimeout(() => {
|
|
173
|
+
c.wait = null;
|
|
174
|
+
c.pause(false);
|
|
175
|
+
post(c, { type: 'timeout', kind });
|
|
176
|
+
onTimeout();
|
|
177
|
+
}, c.answerMs);
|
|
178
|
+
// The session's own process keeps the run alive; a wait alone must not.
|
|
179
|
+
c.wait.unref();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function stopWaiting(c) {
|
|
183
|
+
if (!c.wait) return;
|
|
184
|
+
clearTimeout(c.wait);
|
|
185
|
+
c.wait = null;
|
|
186
|
+
c.pause(false);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* A project chat waits for the operator's next message; with none in the answer time its
|
|
191
|
+
* session closes (`idle`, AD-11): the next message goes on with it by `--resume`.
|
|
192
|
+
*/
|
|
193
|
+
function idleWait(c) {
|
|
194
|
+
if (c.wait) return;
|
|
195
|
+
c.pause(true);
|
|
196
|
+
waiting(c, 'text');
|
|
197
|
+
c.wait = setTimeout(() => {
|
|
198
|
+
c.wait = null;
|
|
199
|
+
c.pause(false);
|
|
200
|
+
c.why = 'idle';
|
|
201
|
+
c.stdin.end();
|
|
202
|
+
}, c.answerMs);
|
|
203
|
+
c.wait.unref();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The operator's input lines in order, `{end, echo, done}`: a message waits for the agent's
|
|
208
|
+
* echo, the rest is done when taken. `input.acked` is where every line before is done: a
|
|
209
|
+
* session that ends sooner leaves the rest to the next one (AD-35).
|
|
210
|
+
*/
|
|
211
|
+
function advance(c) {
|
|
212
|
+
while (c.pending.length && (!c.pending[0].echo || c.pending[0].done))
|
|
213
|
+
c.input.acked = c.pending.shift().end;
|
|
214
|
+
c.input.onAck?.();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The summary turn of a project chat's handoff (AD-42), at a turn's end: past the share, at a
|
|
219
|
+
* compaction, or on the operator's fresh-agent input. The agent's reply is the summary the
|
|
220
|
+
* fresh session starts from; the operator's input waits meanwhile and reaches that session.
|
|
221
|
+
* A second handoff waits for one operator message to the fresh one: a huge CLAUDE.md would
|
|
222
|
+
* else hand over again at once.
|
|
223
|
+
*/
|
|
224
|
+
function summarize(c, by) {
|
|
225
|
+
if (c.fresh && !c.heard) return false;
|
|
226
|
+
const info = c.handoff().info ?? {};
|
|
227
|
+
c.summary = { text: [], by };
|
|
228
|
+
post(c, { type: 'handoff', ...info, by });
|
|
229
|
+
say(c, c.handover.prompt, 'engine');
|
|
230
|
+
c.summary.timer = setTimeout(() => {
|
|
231
|
+
// Its turn goes on: stopped, so its late reply does not start another summary.
|
|
232
|
+
send(c, c.encode.interrupt());
|
|
233
|
+
c.late = true;
|
|
234
|
+
void summarized(c, null);
|
|
235
|
+
}, c.handover.timeoutMs);
|
|
236
|
+
c.summary.timer.unref();
|
|
237
|
+
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Why a summary turn failed (`e` its result, null at its timeout), as the operator reads it. */
|
|
242
|
+
function summaryFailure(c, e) {
|
|
243
|
+
if (c.limited?.())
|
|
244
|
+
return 'the usage limit cut the summary: ask for a fresh agent after the reset';
|
|
245
|
+
|
|
246
|
+
return e ? 'the summary came back empty or failed' : 'the summary took too long';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The summary turn ended (`e` its result, null at its timeout): its text is saved and the
|
|
251
|
+
* session closes for the fresh one (`handoff`); a failed one keeps the session, said once.
|
|
252
|
+
*/
|
|
253
|
+
async function summarized(c, e) {
|
|
254
|
+
const sum = c.summary;
|
|
255
|
+
if (!sum || sum.done) return;
|
|
256
|
+
sum.done = true;
|
|
257
|
+
clearTimeout(sum.timer);
|
|
258
|
+
const text = sum.text.join('\n').trim();
|
|
259
|
+
let file = null;
|
|
260
|
+
// A summary turn the usage limit cut is no summary: its text is the limit's message.
|
|
261
|
+
if (e?.subtype === 'success' && text && !c.limited?.()) {
|
|
262
|
+
try {
|
|
263
|
+
file = await c.handover.save(text);
|
|
264
|
+
} catch {
|
|
265
|
+
// Not saved: the session stays.
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (!file) {
|
|
270
|
+
c.summary = null;
|
|
271
|
+
c.wanted = null;
|
|
272
|
+
// The next try is at the next turn's end, not now.
|
|
273
|
+
c.hold = true;
|
|
274
|
+
post(c, { type: 'handoff', error: summaryFailure(c, e) });
|
|
275
|
+
|
|
276
|
+
return settle(c);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
c.why = 'handoff';
|
|
280
|
+
c.stdin.end();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The image blocks of a message's pictures, each a path inside the runs directory (`<id>.att/…`,
|
|
285
|
+
* where `gcf-harness chat` put it); null when one is not there or not a picture.
|
|
286
|
+
*/
|
|
287
|
+
function imagesOf(c, paths) {
|
|
288
|
+
const blocks = [];
|
|
289
|
+
for (const p of paths) {
|
|
290
|
+
const file = path.resolve(c.logs, String(p));
|
|
291
|
+
const type = IMAGE_TYPES[path.extname(file).toLowerCase()];
|
|
292
|
+
// Only the runs directory's own files, never through a link.
|
|
293
|
+
const inside = file.startsWith(path.resolve(c.logs) + path.sep);
|
|
294
|
+
/** @type {string | null} */
|
|
295
|
+
let data = null;
|
|
296
|
+
try {
|
|
297
|
+
if (inside && type && lstatSync(file).isFile()) data = readFileSync(file).toString('base64');
|
|
298
|
+
} catch {
|
|
299
|
+
// Gone: refused below.
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (!data) return null;
|
|
303
|
+
blocks.push({ type: 'image', source: { type: 'base64', media_type: type, data } });
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return blocks;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* A message into the session; `by` is the operator, the run (its prompt), the timeout or the
|
|
311
|
+
* engine. An input line's message has its `paths` (pictures), its `id` (which its `say` event
|
|
312
|
+
* carries, AD-35), its `entry` in the input, and `retry` when an earlier session took it.
|
|
313
|
+
* Returns whether it went: a closing session or a missing picture refuses it.
|
|
314
|
+
* @param {{paths?: string[], id?: string, entry?: any, retry?: boolean}} [m]
|
|
315
|
+
*/
|
|
316
|
+
function say(c, text, by, m = {}) {
|
|
317
|
+
const { paths = [], id, entry = null, retry } = m;
|
|
318
|
+
const refused = (why) => {
|
|
319
|
+
post(c, { type: 'error', text: `${why}, not sent: ${text.slice(0, 80)}` });
|
|
320
|
+
|
|
321
|
+
return false;
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
if (c.stdin.writableEnded) return refused('the session is closing');
|
|
325
|
+
const images = imagesOf(c, paths);
|
|
326
|
+
if (!images) return refused('a picture is missing');
|
|
327
|
+
c.sent++;
|
|
328
|
+
c.stopped = false;
|
|
329
|
+
const content = images.length ? [{ type: 'text', text }, ...images] : text;
|
|
330
|
+
send(c, c.encode.message(content));
|
|
331
|
+
// The input line the echo of this message acknowledges, if it came from one.
|
|
332
|
+
c.echoes.push(entry);
|
|
333
|
+
if (entry) entry.echo = true;
|
|
334
|
+
const shown = paths.length ? { images: paths } : {};
|
|
335
|
+
const again = retry ? { retry: true } : {};
|
|
336
|
+
post(c, { type: 'say', by, text, ...shown, n: c.sent, ...(id ? { id } : {}), ...again });
|
|
337
|
+
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Answer a question card; the wait ends with the last one. */
|
|
342
|
+
function answer(c, requestId, answers, by) {
|
|
343
|
+
const input = c.cards.get(requestId);
|
|
344
|
+
if (!input || input.approval)
|
|
345
|
+
return post(c, {
|
|
346
|
+
type: 'error',
|
|
347
|
+
text: `no open question ${requestId}: the answer is not sent`,
|
|
348
|
+
});
|
|
349
|
+
c.cards.delete(requestId);
|
|
350
|
+
send(c, c.encode.answer(requestId, input, answers));
|
|
351
|
+
post(c, { type: 'answer', by, request_id: requestId, answers });
|
|
352
|
+
if (!c.cards.size) stopWaiting(c);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* One line of the operator's input file: a message, a stop of the step, or an answer. `entry`
|
|
357
|
+
* is its place in the input; a line an earlier session took already (`input.retryTo`) is a
|
|
358
|
+
* message sent again (`retry`), or a card's answer that has no card left: skipped.
|
|
359
|
+
*/
|
|
360
|
+
function take(c, line, entry = { end: 0, echo: false, done: false }) {
|
|
361
|
+
c.pending.push(entry);
|
|
362
|
+
const again = entry.end <= (c.input.retryTo ?? -1);
|
|
363
|
+
const e = parse(line);
|
|
364
|
+
if (again && e?.type !== 'say') return advance(c);
|
|
365
|
+
if (!e) post(c, { type: 'error', text: `not JSON, skipped: ${line.slice(0, 80)}` });
|
|
366
|
+
else if (Object.hasOwn(INPUT, e.type)) INPUT[e.type](c, e, { entry, retry: again });
|
|
367
|
+
advance(c);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** The operator's message (`m` its place in the input, AD-35). */
|
|
371
|
+
function heard(c, e, m) {
|
|
372
|
+
if (typeof e.text !== 'string' || !e.text.trim()) return;
|
|
373
|
+
// An open card still waits for its answer: its timeout holds.
|
|
374
|
+
if (!c.cards.size) stopWaiting(c);
|
|
375
|
+
c.defaulted = false;
|
|
376
|
+
if (!c.heard) c.onHeard?.();
|
|
377
|
+
c.heard = true;
|
|
378
|
+
const id = typeof e.id === 'string' ? e.id : undefined;
|
|
379
|
+
// A resumed project chat's changed context goes before the first message (AD-44).
|
|
380
|
+
const text = c.prefix ? `${c.prefix}\n\n${e.text}` : e.text;
|
|
381
|
+
const paths = Array.isArray(e.images) ? e.images : [];
|
|
382
|
+
// The context goes with the message that goes: a refused one leaves it for the next.
|
|
383
|
+
if (say(c, text, 'operator', { paths, id, ...m }) && c.prefix) {
|
|
384
|
+
c.prefix = null;
|
|
385
|
+
c.onPrefix?.();
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** The operator's Stop: the step stops, the session stays. */
|
|
390
|
+
function interrupted(c) {
|
|
391
|
+
send(c, c.encode.interrupt());
|
|
392
|
+
// The operator stopped it to say something: no default answer goes after this turn.
|
|
393
|
+
c.stopped = true;
|
|
394
|
+
post(c, { type: 'interrupt' });
|
|
395
|
+
// Stopped while the session waited for an answer to its text: no turn ends, the wait does. A
|
|
396
|
+
// project chat waits for its next message as before: it idles out, it does not wait for good.
|
|
397
|
+
if (c.wait && !c.cards.size) {
|
|
398
|
+
stopWaiting(c);
|
|
399
|
+
if (c.handover) idleWait(c);
|
|
400
|
+
else waiting(c, 'stop');
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// What each line of the operator's input does, by its type; another type is skipped.
|
|
405
|
+
const INPUT = {
|
|
406
|
+
say: heard,
|
|
407
|
+
interrupt: interrupted,
|
|
408
|
+
answer: (c, e) => {
|
|
409
|
+
if (e.answers) answer(c, e.request_id, e.answers, 'operator');
|
|
410
|
+
},
|
|
411
|
+
allow: (c, e) => void decided(c, e.request_id, { allow: true, always: e.always === true }),
|
|
412
|
+
deny: (c, e) => void decided(c, e.request_id, { allow: false }),
|
|
413
|
+
handoff: (c) => {
|
|
414
|
+
c.onHandoff?.();
|
|
415
|
+
if (c.handover) handoffAsked(c);
|
|
416
|
+
},
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* The operator's fresh agent: at once between turns, else at the turn's end; a message the agent
|
|
421
|
+
* has not taken in yet comes first. A fresh agent that has not heard the operator yet stays.
|
|
422
|
+
*/
|
|
423
|
+
function handoffAsked(c) {
|
|
424
|
+
if (c.summary) return;
|
|
425
|
+
if (c.fresh && !c.heard)
|
|
426
|
+
return post(c, { type: 'handoff', error: 'a fresh agent takes over after your next message' });
|
|
427
|
+
c.wanted = 'operator';
|
|
428
|
+
// A turn, an unread message or a background task first: the summary follows them.
|
|
429
|
+
if (c.tasks.length)
|
|
430
|
+
return post(c, {
|
|
431
|
+
type: 'handoff',
|
|
432
|
+
error: 'a fresh agent takes over once the background tasks end',
|
|
433
|
+
});
|
|
434
|
+
if (c.inTurn || c.read < c.sent) return;
|
|
435
|
+
stopWaiting(c);
|
|
436
|
+
summarize(c, 'operator');
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* The operator's answer to an approval card: the call goes once, or always (its rule saved in
|
|
441
|
+
* the agent's local settings first, AD-2), or is refused. `by` is the operator or the timeout.
|
|
442
|
+
*/
|
|
443
|
+
async function decided(c, requestId, { allow, by = 'operator', always = false }) {
|
|
444
|
+
const card = c.cards.get(requestId);
|
|
445
|
+
if (!card?.approval)
|
|
446
|
+
return post(c, { type: 'error', text: `no open approval ${requestId}: nothing sent` });
|
|
447
|
+
c.cards.delete(requestId);
|
|
448
|
+
if (!c.cards.size) stopWaiting(c);
|
|
449
|
+
const title = card.title;
|
|
450
|
+
if (!allow) {
|
|
451
|
+
send(c, c.encode.deny(requestId, `the operator refused: ${title}`));
|
|
452
|
+
|
|
453
|
+
return post(c, { type: 'denied', request_id: requestId, tool: card.tool, by, title });
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const rule = always ? card.always : null;
|
|
457
|
+
if (rule) {
|
|
458
|
+
try {
|
|
459
|
+
await c.saveAlways(rule);
|
|
460
|
+
} catch (e) {
|
|
461
|
+
post(c, { type: 'error', text: `the rule ${rule} was not saved: ${e.message}` });
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
send(c, c.encode.allow(requestId, card.input));
|
|
466
|
+
post(c, {
|
|
467
|
+
type: 'allowed',
|
|
468
|
+
request_id: requestId,
|
|
469
|
+
by,
|
|
470
|
+
always: Boolean(rule),
|
|
471
|
+
...(rule ? { rule } : {}),
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** A question card the agent dropped (an interrupt ended its turn): no answer goes to it. */
|
|
476
|
+
function dropCard(c, requestId) {
|
|
477
|
+
if (!c.cards.delete(requestId)) return;
|
|
478
|
+
post(c, { type: 'dropped', request_id: requestId });
|
|
479
|
+
if (!c.cards.size) stopWaiting(c);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** `fn` once the grace passes with no new turn. */
|
|
483
|
+
function afterGrace(c, fn) {
|
|
484
|
+
clearTimeout(c.grace);
|
|
485
|
+
c.grace = setTimeout(() => {
|
|
486
|
+
c.grace = null;
|
|
487
|
+
fn();
|
|
488
|
+
}, c.graceMs);
|
|
489
|
+
c.grace.unref();
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Messages it has not echoed as read count as read once the grace passes with no new turn. */
|
|
493
|
+
function graceRead(c) {
|
|
494
|
+
afterGrace(c, () => {
|
|
495
|
+
c.read = c.sent;
|
|
496
|
+
for (const m of c.echoes.splice(0)) if (m) m.done = true;
|
|
497
|
+
advance(c);
|
|
498
|
+
post(c, { type: 'read', n: c.read, assumed: true });
|
|
499
|
+
void settle(c);
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* What follows a turn: a message it has not taken in yet starts its next one; while background
|
|
505
|
+
* tasks run, nothing (the agent's next turn comes when they end, fix B); a session flagged for a
|
|
506
|
+
* handoff, or whose item moved, closes; one the operator stopped waits for them with no default;
|
|
507
|
+
* else the operator gets the answer time, then the default, once.
|
|
508
|
+
*/
|
|
509
|
+
async function settle(c) {
|
|
510
|
+
if (c.closed || c.summary) return;
|
|
511
|
+
// The usage limit cut the turn: the session closes at once, and the job waits for the reset;
|
|
512
|
+
// what the operator writes meanwhile waits for the session after it (AD-19).
|
|
513
|
+
if (c.limited?.()) return c.stdin.end();
|
|
514
|
+
if (c.read < c.sent) return graceRead(c);
|
|
515
|
+
// Background tasks run: the agent's next turn comes as they end. A run whose item made its
|
|
516
|
+
// move needs them no more: a server or a watcher it left must not hold the session.
|
|
517
|
+
if (c.tasks.length) return c.handover ? undefined : closeOverTasks(c);
|
|
518
|
+
|
|
519
|
+
return c.handover ? settleTalk(c) : settleRun(c);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* A run's session with background tasks running: it closes when it was told to hand over, or
|
|
524
|
+
* when its item made its move; else the agent's next turn comes as they end.
|
|
525
|
+
*/
|
|
526
|
+
async function closeOverTasks(c) {
|
|
527
|
+
if (c.handoff().flagged) return c.stdin.end();
|
|
528
|
+
if (!(await c.done?.())) return;
|
|
529
|
+
// The operator may have written meanwhile, or the session ended: that comes first.
|
|
530
|
+
if (c.closed || c.read < c.sent) return;
|
|
531
|
+
c.stdin.end();
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** A project chat after a turn: its summary turn when it is due (AD-42), else the idle wait. */
|
|
535
|
+
function settleTalk(c) {
|
|
536
|
+
const w = c.handoff();
|
|
537
|
+
const by = c.wanted ?? (w.flagged ? (w.info?.compacted ? 'compacted' : 'threshold') : null);
|
|
538
|
+
if (by && !c.hold && summarize(c, by)) return;
|
|
539
|
+
idleWait(c);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** A run's session after a turn: closed, or waiting for the operator, then the default. */
|
|
543
|
+
async function settleRun(c) {
|
|
544
|
+
if (c.handoff().flagged) return c.stdin.end();
|
|
545
|
+
const moved = await c.done?.();
|
|
546
|
+
// The operator may have written meanwhile, a task started, or the session ended.
|
|
547
|
+
if (c.closed || c.read < c.sent || c.tasks.length) return;
|
|
548
|
+
if (moved) return c.stdin.end();
|
|
549
|
+
// The operator's Stop holds even after a default answer went: they are at the screen.
|
|
550
|
+
if (c.stopped) return waiting(c, 'stop');
|
|
551
|
+
if (c.defaulted) return c.stdin.end();
|
|
552
|
+
waitFor(c, 'text', () => {
|
|
553
|
+
c.defaulted = true;
|
|
554
|
+
say(c, c.defaultAnswer, 'timeout');
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/** A turn ended: what it cost the session so far, how full its context is, then what follows. */
|
|
559
|
+
function turnEnd(c, e) {
|
|
560
|
+
c.inTurn = false;
|
|
561
|
+
if (c.summary) return void summarized(c, e);
|
|
562
|
+
// The late end of a summary turn that ran out of time: the next try waits a turn more.
|
|
563
|
+
if (c.late) c.late = false;
|
|
564
|
+
else c.hold = false;
|
|
565
|
+
// claude's `total_cost_usd`: the session's so far, not the turn's.
|
|
566
|
+
post(c, {
|
|
567
|
+
type: 'turn',
|
|
568
|
+
subtype: e.subtype,
|
|
569
|
+
total_cost_usd: e.total_cost_usd ?? null,
|
|
570
|
+
...c.context(),
|
|
571
|
+
});
|
|
572
|
+
for (const id of [...c.cards.keys()]) dropCard(c, id);
|
|
573
|
+
void settle(c);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** A question card waits for the operator, unless the session asks nothing (`ask: none`). */
|
|
577
|
+
function asked(c, e) {
|
|
578
|
+
if (c.ask === 'none') return refuse(c, { ...e, tool: 'AskUserQuestion' }, c.askNone);
|
|
579
|
+
c.cards.set(e.request_id, e.input);
|
|
580
|
+
post(c, { type: 'question', request_id: e.request_id, questions: e.input.questions ?? [] });
|
|
581
|
+
waitCards(c);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/** The open cards wait for the operator; past the answer time a question gets its default, an
|
|
585
|
+
* approval is refused. */
|
|
586
|
+
function waitCards(c) {
|
|
587
|
+
waitFor(c, 'card', () => {
|
|
588
|
+
for (const [rid, card] of [...c.cards])
|
|
589
|
+
if (card.approval) void decided(c, rid, { allow: false, by: 'timeout' });
|
|
590
|
+
else answer(c, rid, defaultAnswers(card.questions, c.defaultAnswer), 'timeout');
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* A tool call the session's permissions leave to someone: the engine refuses it when its
|
|
596
|
+
* policy says (`gate`, AD-29, no card), a run refuses it (nobody approves there), a project
|
|
597
|
+
* chat asks the operator by an approval card (AD-5). "Always" is offered only for a rule the
|
|
598
|
+
* session does not ask for anyway (`asks`): an allow would not beat that ask (AD-2).
|
|
599
|
+
*/
|
|
600
|
+
function permit(c, e) {
|
|
601
|
+
const why = c.gate(e);
|
|
602
|
+
if (why) return refuse(c, e, why, { title: e.title, reason: why });
|
|
603
|
+
if (!c.approvals) return refuse(c, e, `${e.tool} needs an approval no one gives in a run`);
|
|
604
|
+
const always = e.always && !c.asks(e) ? e.always : null;
|
|
605
|
+
c.cards.set(e.request_id, {
|
|
606
|
+
approval: true,
|
|
607
|
+
tool: e.tool,
|
|
608
|
+
input: e.input,
|
|
609
|
+
title: e.title,
|
|
610
|
+
always,
|
|
611
|
+
});
|
|
612
|
+
post(c, {
|
|
613
|
+
type: 'approval',
|
|
614
|
+
request_id: e.request_id,
|
|
615
|
+
kind: e.kind,
|
|
616
|
+
tool: e.tool,
|
|
617
|
+
title: e.title,
|
|
618
|
+
...(e.reason ? { reason: e.reason } : {}),
|
|
619
|
+
...(always ? { always } : {}),
|
|
620
|
+
});
|
|
621
|
+
waitCards(c);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/** Refuse a tool call (`{request_id, tool}`) by the engine's own word, with no card before it. */
|
|
625
|
+
function refuse(c, { request_id: requestId, tool }, message, more = {}) {
|
|
626
|
+
send(c, c.encode.deny(requestId, message));
|
|
627
|
+
post(c, { type: 'denied', tool, by: 'engine', ...more });
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// What the operator reads of each neutral event of the session, by its type.
|
|
631
|
+
const SEEN = {
|
|
632
|
+
// The agent compacted its context by itself.
|
|
633
|
+
compacted: (c) => post(c, { type: 'compacted' }),
|
|
634
|
+
// The main agent's text; a subagent's is the raw log's.
|
|
635
|
+
said: (c, e) => {
|
|
636
|
+
if (e.sub) return;
|
|
637
|
+
// It works on by itself (a read it was assumed not to need): no answer is waited for.
|
|
638
|
+
if (!c.cards.size) stopWaiting(c);
|
|
639
|
+
if (e.text.trim()) post(c, { type: 'agent', text: e.text });
|
|
640
|
+
c.summary?.text.push(e.text);
|
|
641
|
+
},
|
|
642
|
+
// A message echoed as the agent takes it in: read. A late echo after the grace counts once.
|
|
643
|
+
read: (c) => {
|
|
644
|
+
c.read = Math.min(c.read + 1, c.sent);
|
|
645
|
+
const m = c.echoes.shift();
|
|
646
|
+
if (m) m.done = true;
|
|
647
|
+
advance(c);
|
|
648
|
+
post(c, { type: 'read', n: c.read });
|
|
649
|
+
},
|
|
650
|
+
question: asked,
|
|
651
|
+
permission: permit,
|
|
652
|
+
// A deny rule of the settings refused it: no card was ever due.
|
|
653
|
+
refused: (c, e) =>
|
|
654
|
+
post(c, {
|
|
655
|
+
type: 'denied',
|
|
656
|
+
tool: e.tool,
|
|
657
|
+
by: 'rule',
|
|
658
|
+
...(e.reason ? { reason: e.reason } : {}),
|
|
659
|
+
}),
|
|
660
|
+
// A proposal (AD-12): the call of the center's MCP tool, posted when it succeeded.
|
|
661
|
+
tool_call: (c, e) => {
|
|
662
|
+
const m = /^mcp__gcf__propose_(\w+)$/.exec(e.tool);
|
|
663
|
+
if (!e.sub && m) c.proposals.set(e.id, { move: m[1].replaceAll('_', '-'), fields: e.input });
|
|
664
|
+
},
|
|
665
|
+
tool_result: (c, e) => {
|
|
666
|
+
const p = c.proposals.get(e.id);
|
|
667
|
+
if (!p) return;
|
|
668
|
+
c.proposals.delete(e.id);
|
|
669
|
+
if (!e.error) post(c, { type: 'proposal', id: e.id, ...p });
|
|
670
|
+
},
|
|
671
|
+
cancel: (c, e) => dropCard(c, e.request_id),
|
|
672
|
+
result: (c, e) => turnEnd(c, e),
|
|
673
|
+
// While they run, no answer is waited for; claude starts a turn itself as they end. Their end
|
|
674
|
+
// with no new turn after the grace settles the session, so it is not held for good.
|
|
675
|
+
background: (c, e) => {
|
|
676
|
+
c.tasks = e.tasks;
|
|
677
|
+
post(c, { type: 'background', tasks: e.tasks });
|
|
678
|
+
if (c.tasks.length && !c.cards.size) stopWaiting(c);
|
|
679
|
+
if (!c.tasks.length && !c.inTurn) afterGrace(c, () => void settle(c));
|
|
680
|
+
},
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
// The events that say a new turn has begun: the messages it echoes count themselves.
|
|
684
|
+
const TURN = new Set(['said', 'read', 'tool_call', 'tool_result']);
|
|
685
|
+
|
|
686
|
+
/** The neutral events of one line the agent printed. */
|
|
687
|
+
function see(c, events) {
|
|
688
|
+
for (const e of events) {
|
|
689
|
+
if (TURN.has(e.type)) {
|
|
690
|
+
c.inTurn = true;
|
|
691
|
+
clearTimeout(c.grace ?? undefined);
|
|
692
|
+
c.grace = null;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
if ((e.type === 'said' || e.type === 'tool_call') && !e.sub) working(c);
|
|
696
|
+
for (const step of c.steps.see(e)) post(c, step);
|
|
697
|
+
if (Object.hasOwn(SEEN, e.type)) SEEN[e.type](c, e);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* The chat of one session. `stdin` is the agent's; `input.at` is where the operator's input is
|
|
703
|
+
* read from, kept across the item's sessions; `answerMs` is how long a question waits; `ask`
|
|
704
|
+
* what the agent may ask by a card (`none`: each card is denied, with `askNone`); `done()` says
|
|
705
|
+
* the item made its move, so the session may close; `handoff()` is the handoff watch's state
|
|
706
|
+
* (`{flagged}`): a flagged session closes at its turn's end; `context()` the context it holds
|
|
707
|
+
* (`{context, context_pct}`); `pause(on)` stops the session's timeout while it waits for the
|
|
708
|
+
* operator; `prompt` is the first message; `kind` how the session started (`start`, `resume`,
|
|
709
|
+
* `handoff`); `steps.see(e)` the steps an event ends; `defaultAnswer` what silence says.
|
|
710
|
+
* `see(events)` takes the neutral events of each line the agent prints; `event(e)` posts one
|
|
711
|
+
* the run saw; `encode` writes its input as the agent reads it;
|
|
712
|
+
* `close(code, seen)` stops reading and posts the session's end.
|
|
713
|
+
*
|
|
714
|
+
* A project chat (AD-5) sets `approvals`: a permission request is an approval card;
|
|
715
|
+
* `gate(e)` names why the engine refuses a call outright, or null; `asks(e)` whether the
|
|
716
|
+
* session asks for such a call anyway (no "always" then); `saveAlways(rule)` keeps an
|
|
717
|
+
* "always" rule; `onHandoff()` takes the operator's fresh-agent input. `files` names the chat's
|
|
718
|
+
* two files when they are not `<id>.chat*.jsonl` under `logs`.
|
|
719
|
+
*/
|
|
720
|
+
export function openChat(opts) {
|
|
721
|
+
const c = chatState(opts);
|
|
722
|
+
c.input.acked ??= c.input.at;
|
|
723
|
+
const poll = setInterval(() => pollInput(c), POLL_MS);
|
|
724
|
+
// A session that ended under a write to its stdin breaks the pipe: the end is its close.
|
|
725
|
+
c.stdin.on('error', () => {});
|
|
726
|
+
const from = opts.summary ? { summary: opts.summary } : {};
|
|
727
|
+
post(c, { type: 'session', kind: c.kind, v: EVENTS_V, ...from });
|
|
728
|
+
// A project chat starts on the operator's message, not on a prompt of its own.
|
|
729
|
+
// A run's prompt is the run's; a project chat's (after the limit's reset) the engine's.
|
|
730
|
+
if (opts.prompt) say(c, opts.prompt, c.handover ? 'engine' : 'run');
|
|
731
|
+
else {
|
|
732
|
+
// It waits for the operator from the start, and idles out with no word from them.
|
|
733
|
+
c.inTurn = false;
|
|
734
|
+
void settle(c);
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
return {
|
|
738
|
+
see: (events) => see(c, events),
|
|
739
|
+
event: (e) => post(c, e),
|
|
740
|
+
/** Why the chat closed its session: `idle`, `handoff`, or null. */
|
|
741
|
+
why: () => c.why,
|
|
742
|
+
close: (code, seen = {}) => {
|
|
743
|
+
clearInterval(poll);
|
|
744
|
+
closeChat(c, code, seen);
|
|
745
|
+
},
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// The options a chat takes when its session's work names none (openChat).
|
|
750
|
+
const CHAT_DEFAULTS = {
|
|
751
|
+
ask: 'all',
|
|
752
|
+
context: () => ({}),
|
|
753
|
+
kind: 'start',
|
|
754
|
+
graceMs: READ_GRACE_MS,
|
|
755
|
+
steps: { see: () => [] },
|
|
756
|
+
defaultAnswer: DEFAULT_ANSWER,
|
|
757
|
+
askNone: ASK_NONE,
|
|
758
|
+
encode: claudeInput,
|
|
759
|
+
approvals: false,
|
|
760
|
+
gate: () => null,
|
|
761
|
+
asks: () => false,
|
|
762
|
+
saveAlways: async () => {},
|
|
763
|
+
handover: null,
|
|
764
|
+
prefix: null,
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
/** A chat's state: its options, a default for each one not given, and what it keeps track of. */
|
|
768
|
+
function chatState(opts) {
|
|
769
|
+
const o = { ...opts };
|
|
770
|
+
for (const [k, v] of Object.entries(CHAT_DEFAULTS)) if (o[k] === undefined) o[k] = v;
|
|
771
|
+
// The file a fresh session starts from and its first message are the session event's.
|
|
772
|
+
const { summary: _from, prompt: _first, files, ...given } = o;
|
|
773
|
+
const io = files ?? { in: chatIn(o.logs, o.id), out: chatOut(o.logs, o.id) };
|
|
774
|
+
|
|
775
|
+
return {
|
|
776
|
+
...given,
|
|
777
|
+
in: io.in,
|
|
778
|
+
out: io.out,
|
|
779
|
+
fresh: o.fresh ?? o.kind === 'handoff',
|
|
780
|
+
...{ sent: 0, read: 0, cards: new Map(), wait: null, defaulted: false, closed: false },
|
|
781
|
+
...{ stopped: false, grace: null, idle: false, inTurn: true, tasks: [] },
|
|
782
|
+
...{ proposals: new Map(), heard: false, summary: null, wanted: null, why: null },
|
|
783
|
+
...{ hold: false, late: false, pending: [], echoes: [] },
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/** The operator's new input lines, each taken in order. */
|
|
788
|
+
function pollInput(c) {
|
|
789
|
+
// A session that is closing takes no more: the next one reads it.
|
|
790
|
+
if (c.stdin.writableEnded) return;
|
|
791
|
+
for (const { line, end } of readEntries(c.in, c.input.at)) {
|
|
792
|
+
// While the summary turn runs, a message waits for the fresh session, and every line after
|
|
793
|
+
// it with it (the input is read in order); a card's answer before it goes in.
|
|
794
|
+
if (c.summary && parse(line)?.type === 'say') break;
|
|
795
|
+
c.input.at = end;
|
|
796
|
+
take(c, line, { end, echo: false, done: false });
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
/** The chat of a session that ended (`code`, `seen` what it reported): its end posted. */
|
|
801
|
+
function closeChat(c, code, seen) {
|
|
802
|
+
const { input } = c;
|
|
803
|
+
c.closed = true;
|
|
804
|
+
clearTimeout(c.grace ?? undefined);
|
|
805
|
+
clearTimeout(c.summary?.timer);
|
|
806
|
+
// A message the agent never took in goes to the next session, marked as sent again.
|
|
807
|
+
input.retryTo = Math.max(input.retryTo ?? 0, input.at);
|
|
808
|
+
input.at = input.acked ?? input.at;
|
|
809
|
+
// A card the session took with it can be answered no more.
|
|
810
|
+
for (const rid of [...c.cards.keys()]) dropCard(c, rid);
|
|
811
|
+
stopWaiting(c);
|
|
812
|
+
post(c, {
|
|
813
|
+
type: 'session_end',
|
|
814
|
+
code,
|
|
815
|
+
cost_usd: seen.cost?.cost_usd ?? null,
|
|
816
|
+
...(seen.limit ? { limit_resets: seen.limit.resets } : {}),
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
/**
|
|
821
|
+
* The operator's input no session read: written after the item's last one ended. Each line gets
|
|
822
|
+
* an `error` event, so nothing is lost without a word.
|
|
823
|
+
*/
|
|
824
|
+
export function drainChat(logs, id, input) {
|
|
825
|
+
const { lines, next } = readFrom(chatIn(logs, id), input.at);
|
|
826
|
+
input.at = next;
|
|
827
|
+
for (const l of lines)
|
|
828
|
+
postTo(chatOut(logs, id), {
|
|
829
|
+
type: 'error',
|
|
830
|
+
text: `the item's session has ended, not sent: ${l.slice(0, 80)}`,
|
|
831
|
+
});
|
|
832
|
+
}
|