gcf-board 0.5.1 → 0.7.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 +14 -12
- package/dist/server/app.js +166 -20
- package/dist/server/chat.js +42 -1
- 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 +28 -120
- package/dist/server/talks.js +553 -0
- package/dist/web/assets/golos-text-cyrillic-ext-wght-normal-hICkpzq7.woff2 +0 -0
- package/dist/web/assets/golos-text-cyrillic-wght-normal-mKek_fSS.woff2 +0 -0
- package/dist/web/assets/golos-text-latin-ext-wght-normal-B-I5lbAQ.woff2 +0 -0
- package/dist/web/assets/golos-text-latin-wght-normal-BS5oQT7X.woff2 +0 -0
- package/dist/web/assets/index-B3u-VrJK.css +1 -0
- package/dist/web/assets/index-BB3i2FaE.js +39 -0
- package/dist/web/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
- package/dist/web/index.html +2 -2
- 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 +834 -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 +437 -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 +12 -3
- package/dist/web/assets/index-BYAEKw51.css +0 -1
- package/dist/web/assets/index-DpzFCrLV.js +0 -36
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// `gcf-harness chat <id> say|answer|interrupt` (docs/Chat-Plan.md): the operator writes into an
|
|
2
|
+
// item's live session, from a console or through the board, which calls this command. The input
|
|
3
|
+
// is checked here and lands as one line of `.gcf-session/runs/<id>.chat-in.jsonl`, which the
|
|
4
|
+
// run's chat reads on; pictures are copied beside it into `<id>.att/` (not git). Only a session
|
|
5
|
+
// that is live takes input: nothing written is lost without a word.
|
|
6
|
+
import { appendFileSync, copyFileSync, lstatSync, mkdirSync, readFileSync } from 'node:fs';
|
|
7
|
+
import { randomBytes } from 'node:crypto';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { chatIn, chatOut, IMAGE_TYPES } from './chat.js';
|
|
10
|
+
|
|
11
|
+
export const TEXT_MAX = 64_000;
|
|
12
|
+
export const IMAGES_MAX = 10;
|
|
13
|
+
export const IMAGE_MAX = 5 * 1024 * 1024;
|
|
14
|
+
/** A question card's id as claude gives it (`toolu_…`, a uuid): never an option's spelling. */
|
|
15
|
+
export const REQUEST_ID = /^[A-Za-z0-9_][\w-]*$/;
|
|
16
|
+
|
|
17
|
+
/** Bad input: exit 1. No live session: exit 3 (the board answers 409). */
|
|
18
|
+
export class ChatRefusal extends Error {
|
|
19
|
+
constructor(message, code = 1) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.code = code;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// The first bytes of each picture type: a renamed file is not a picture.
|
|
26
|
+
const MAGIC = {
|
|
27
|
+
'.png': (b) => b.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 13, 10, 26, 10])),
|
|
28
|
+
'.jpg': (b) => b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff,
|
|
29
|
+
'.jpeg': (b) => b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff,
|
|
30
|
+
'.gif': (b) => b.subarray(0, 4).toString('latin1') === 'GIF8',
|
|
31
|
+
'.webp': (b) =>
|
|
32
|
+
b.subarray(0, 4).toString('latin1') === 'RIFF' &&
|
|
33
|
+
b.subarray(8, 12).toString('latin1') === 'WEBP',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const lstatOr = (f) => {
|
|
37
|
+
try {
|
|
38
|
+
return lstatSync(f);
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A link planted in place of a file or a folder on its way would take the write elsewhere, a
|
|
46
|
+
* link to a file not there yet too; a hard link shares the file with another name: none is
|
|
47
|
+
* written through.
|
|
48
|
+
*/
|
|
49
|
+
function refuseLinks(id, dirs, files) {
|
|
50
|
+
for (const f of [...dirs, ...files]) {
|
|
51
|
+
const st = lstatOr(f);
|
|
52
|
+
if (st?.isSymbolicLink() || (files.includes(f) && st?.isFile() && st.nlink > 1))
|
|
53
|
+
throw new ChatRefusal(`${id}'s chat goes through a link: refused`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The last of item `id`'s session events while a run that lives drives it (its lock): `session`
|
|
59
|
+
* (one runs), `limit` (the run waits for the usage limit's reset, AD-19), `session_end`; null
|
|
60
|
+
* with no run or no chat.
|
|
61
|
+
*/
|
|
62
|
+
function chatState(runs, id, lockFor) {
|
|
63
|
+
if (!lockFor(id)) return null;
|
|
64
|
+
let text;
|
|
65
|
+
try {
|
|
66
|
+
text = readFileSync(chatOut(runs, id), 'utf8');
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const lines = text.split('\n').filter(Boolean);
|
|
72
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
73
|
+
const type = /"type":"(session|session_end|limit)"/.exec(lines[i])?.[1];
|
|
74
|
+
if (type) return type;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Whether item `id` has a live chat session: a run that lives drives it, and its chat's last
|
|
82
|
+
* session event is a start, not an end; or the run waits for the limit's reset, and the session
|
|
83
|
+
* after it reads the input. Between two sessions of a handoff, before the first and after the
|
|
84
|
+
* last, input would reach no one.
|
|
85
|
+
* shortcut: a line written in the instant between the session's last read and its end reaches
|
|
86
|
+
* no one and gets no error either; drainChat catches it only after the item's last session.
|
|
87
|
+
*/
|
|
88
|
+
export function liveSession(runs, id, lockFor) {
|
|
89
|
+
const state = chatState(runs, id, lockFor);
|
|
90
|
+
|
|
91
|
+
return state === 'session' || state === 'limit';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The question card `requestId` still waiting for an answer, from the chat, or null. */
|
|
95
|
+
function openCard(runs, id, requestId) {
|
|
96
|
+
let card = null;
|
|
97
|
+
for (const line of readFileSync(chatOut(runs, id), 'utf8').split('\n')) {
|
|
98
|
+
let e;
|
|
99
|
+
try {
|
|
100
|
+
e = JSON.parse(line);
|
|
101
|
+
} catch {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (e.request_id !== requestId) continue;
|
|
106
|
+
if (e.type === 'question') card = e;
|
|
107
|
+
else if (e.type === 'answer' || e.type === 'dropped') card = null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return card;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The text of a message: its words, or a file of it (`-`: stdin, `read(path)`). */
|
|
114
|
+
function textOf(words, file, read) {
|
|
115
|
+
let text;
|
|
116
|
+
try {
|
|
117
|
+
text = file === undefined ? words.join(' ') : read(file);
|
|
118
|
+
} catch (e) {
|
|
119
|
+
throw new ChatRefusal(`--text-file ${file}: ${e.message}`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!text.trim()) throw new ChatRefusal('say what: the message is empty');
|
|
123
|
+
if (text.length > TEXT_MAX)
|
|
124
|
+
throw new ChatRefusal(`a message is up to ${TEXT_MAX} characters, this one ${text.length}`);
|
|
125
|
+
|
|
126
|
+
return text;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Each picture checked, then copied into `<id>.att/`: their paths, relative to the runs dir. */
|
|
130
|
+
function picturesOf(runs, id, files) {
|
|
131
|
+
if (files.length > IMAGES_MAX) throw new ChatRefusal(`up to ${IMAGES_MAX} pictures a message`);
|
|
132
|
+
const checked = files.map((f) => {
|
|
133
|
+
const ext = path.extname(f).toLowerCase();
|
|
134
|
+
const st = lstatOr(f);
|
|
135
|
+
if (!IMAGE_TYPES[ext]) throw new ChatRefusal(`${f}: a picture is png, jpg, gif or webp`);
|
|
136
|
+
if (!st?.isFile()) throw new ChatRefusal(`${f} is not a file (a link is refused)`);
|
|
137
|
+
if (st.size > IMAGE_MAX) throw new ChatRefusal(`${f} is over 5 MB`);
|
|
138
|
+
let head;
|
|
139
|
+
try {
|
|
140
|
+
head = readFileSync(f).subarray(0, 12);
|
|
141
|
+
} catch (e) {
|
|
142
|
+
throw new ChatRefusal(`${f}: ${e.message}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (!MAGIC[ext](head))
|
|
146
|
+
throw new ChatRefusal(`${f} is not the ${ext.slice(1)} picture its name says`);
|
|
147
|
+
|
|
148
|
+
return { f, ext };
|
|
149
|
+
});
|
|
150
|
+
const att = path.join(runs, `${id}.att`);
|
|
151
|
+
refuseLinks(id, [att], []);
|
|
152
|
+
if (checked.length) mkdirSync(att, { recursive: true });
|
|
153
|
+
|
|
154
|
+
return checked.map(({ f, ext }) => {
|
|
155
|
+
const name = `${randomBytes(4).toString('hex')}${ext}`;
|
|
156
|
+
copyFileSync(f, path.join(att, name));
|
|
157
|
+
|
|
158
|
+
return `${id}.att/${name}`;
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The answers of a card: one text for a card of one question, or a map of them all. */
|
|
163
|
+
function answersOf(card, words, file, read) {
|
|
164
|
+
const questions = (card.questions ?? []).map((q) => q.question);
|
|
165
|
+
if (file === undefined) {
|
|
166
|
+
if (questions.length !== 1)
|
|
167
|
+
throw new ChatRefusal(
|
|
168
|
+
`this card asks ${questions.length} questions: give --answers-file with each`,
|
|
169
|
+
);
|
|
170
|
+
const text = words.join(' ');
|
|
171
|
+
if (!text.trim()) throw new ChatRefusal('answer what: the answer is empty');
|
|
172
|
+
|
|
173
|
+
return { [questions[0]]: text };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
let answers;
|
|
177
|
+
try {
|
|
178
|
+
answers = JSON.parse(read(file));
|
|
179
|
+
} catch {
|
|
180
|
+
throw new ChatRefusal('--answers-file is JSON: {"<question>": "<answer>", …}');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const keys = Object.keys(answers ?? {});
|
|
184
|
+
const ok =
|
|
185
|
+
typeof answers === 'object' &&
|
|
186
|
+
!Array.isArray(answers) &&
|
|
187
|
+
keys.length === questions.length &&
|
|
188
|
+
keys.every((k) => questions.includes(k) && typeof answers[k] === 'string');
|
|
189
|
+
if (!ok)
|
|
190
|
+
throw new ChatRefusal(`answer each question of the card, as text: ${questions.join(' | ')}`);
|
|
191
|
+
|
|
192
|
+
return answers;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Write the operator's `action` into item `id`'s chat: `say` (`words` or `textFile`, `image`
|
|
197
|
+
* paths), `answer` (`words[0]` the request id, the rest its text, or `answersFile`), `interrupt`.
|
|
198
|
+
* `lockFor(id)` is the live run lock that drives it; `read(file)` reads a text file or stdin.
|
|
199
|
+
*/
|
|
200
|
+
export function chatInput(projectDir, id, { action, words = [], ...opts }, { lockFor, read }) {
|
|
201
|
+
if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(id)) throw new ChatRefusal(`${id} is not an item id`);
|
|
202
|
+
const session = path.join(path.resolve(projectDir), '.gcf-session');
|
|
203
|
+
const runs = path.join(session, 'runs');
|
|
204
|
+
const file = chatIn(runs, id);
|
|
205
|
+
if (!['say', 'answer', 'interrupt'].includes(action))
|
|
206
|
+
throw new ChatRefusal(`${action}: the action is say, answer or interrupt`);
|
|
207
|
+
refuseLinks(id, [session, runs], [file, chatOut(runs, id)]);
|
|
208
|
+
const state = chatState(runs, id, lockFor);
|
|
209
|
+
if (state !== 'session' && state !== 'limit')
|
|
210
|
+
throw new ChatRefusal(`${id} has no live chat session: nothing sent`, 3);
|
|
211
|
+
// Nothing runs to stop or answer: the session after the reset would take a stop as its own.
|
|
212
|
+
if (state === 'limit' && action !== 'say')
|
|
213
|
+
throw new ChatRefusal(`${id} waits for the usage limit: only a message waits with it`, 3);
|
|
214
|
+
|
|
215
|
+
const line =
|
|
216
|
+
action === 'interrupt'
|
|
217
|
+
? { type: 'interrupt' }
|
|
218
|
+
: action === 'say'
|
|
219
|
+
? sayLine(runs, id, { words, ...opts }, read)
|
|
220
|
+
: answerLine(runs, id, { words, ...opts }, read);
|
|
221
|
+
appendFileSync(file, `${JSON.stringify(line)}\n`);
|
|
222
|
+
|
|
223
|
+
return line;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* A message's line: its text, its pictures copied beside the chat.
|
|
228
|
+
* @param {{words: string[], textFile?: string, image?: string[]}} m
|
|
229
|
+
*/
|
|
230
|
+
function sayLine(runs, id, m, read) {
|
|
231
|
+
const { words, textFile, image = [] } = m;
|
|
232
|
+
const text = textOf(words, textFile, read);
|
|
233
|
+
const images = picturesOf(runs, id, image);
|
|
234
|
+
// The line's own id, which the session's `say` event carries (AD-35).
|
|
235
|
+
const line = randomBytes(6).toString('hex');
|
|
236
|
+
|
|
237
|
+
return { type: 'say', id: line, text, ...(images.length ? { images } : {}) };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* A card's answer's line: the card still open, an answer to each of its questions.
|
|
242
|
+
* @param {{words: string[], answersFile?: string}} m
|
|
243
|
+
*/
|
|
244
|
+
function answerLine(runs, id, m, read) {
|
|
245
|
+
const { words, answersFile } = m;
|
|
246
|
+
const [requestId, ...rest] = words;
|
|
247
|
+
if (!requestId) throw new ChatRefusal('answer which question: name its request id');
|
|
248
|
+
if (!REQUEST_ID.test(requestId)) throw new ChatRefusal(`${requestId} is not a request id`);
|
|
249
|
+
const card = openCard(runs, id, requestId);
|
|
250
|
+
if (!card) throw new ChatRefusal(`no open question ${requestId}: nothing sent`);
|
|
251
|
+
|
|
252
|
+
return {
|
|
253
|
+
type: 'answer',
|
|
254
|
+
request_id: requestId,
|
|
255
|
+
answers: answersOf(card, rest, answersFile, read),
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** A file's text, or stdin's for `-`. */
|
|
260
|
+
export const readText = (file) => readFileSync(file === '-' ? 0 : file, 'utf8');
|