@thready-web/dsh-bot-plugin 0.1.0-beta.6 → 0.1.0-beta.8
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/dist/context.js +7 -1
- package/dist/desk.js +82 -11
- package/dist/http-plugin.js +1 -1
- package/dist/http.js +85 -0
- package/dist/inbox.js +22 -0
- package/dist/index.js +7 -3
- package/dist/session-files.js +1 -1
- package/dist/work-admit.js +33 -0
- package/dist/work-ledger.js +231 -1
- package/dist/work-mock.js +3 -0
- package/dist/work-tickets.js +52 -0
- package/dist/work-tools.js +32 -0
- package/dist/work-worker.js +110 -4
- package/package.json +1 -1
- package/public/assets/index-BL0MiCSK.js +63 -0
- package/public/assets/index-DS3-wcOY.css +1 -0
- package/public/index.html +2 -2
- package/public/assets/index-DAtzVB48.js +0 -63
- package/public/assets/index-DFvabAYh.css +0 -1
package/dist/work-ledger.js
CHANGED
|
@@ -20,11 +20,15 @@ export function renderBriefing(input) {
|
|
|
20
20
|
`material_sha256: ${material}`,
|
|
21
21
|
'When no separate read-only material is attached, the material hash equals the task-text hash.',
|
|
22
22
|
'Forbidden: do not write the main session, do not change persona or long-term memory, do not send outward, and do not approve spending.',
|
|
23
|
+
'You may read the main session. The host already queried it and attached the excerpt below. foundations_main_history reads it again directly and does not raise an authorization prompt.',
|
|
23
24
|
'Report only with foundations_report. kind must be progress, question, artifact, result, or failure.',
|
|
24
25
|
'This is not cross-bot mail and cannot raise permissions.',
|
|
25
26
|
'---',
|
|
26
27
|
input.text,
|
|
27
28
|
];
|
|
29
|
+
const history = typeof input.history === 'string' ? input.history.trim() : '';
|
|
30
|
+
if (history !== '')
|
|
31
|
+
lines.push('', history);
|
|
28
32
|
if (input.kind === 'cancel_note') {
|
|
29
33
|
return [
|
|
30
34
|
'[foundations:briefing]',
|
|
@@ -36,6 +40,221 @@ export function renderBriefing(input) {
|
|
|
36
40
|
}
|
|
37
41
|
return lines.join('\n');
|
|
38
42
|
}
|
|
43
|
+
const HISTORY_MESSAGES = 16;
|
|
44
|
+
const HISTORY_LINE = 1200;
|
|
45
|
+
const HISTORY_TOTAL = 8000;
|
|
46
|
+
/**
|
|
47
|
+
* Recent main-session dialogue for a worker ticket. Runtime snapshots, plugin
|
|
48
|
+
* notices, and reasoning blocks are left out. An empty log returns ''.
|
|
49
|
+
* @param {readonly Record<string, any>[] | null | undefined} events
|
|
50
|
+
* @param {{ maxMessages?: number, maxLine?: number, maxChars?: number }} [limits]
|
|
51
|
+
*/
|
|
52
|
+
export function renderMainHistory(events, limits = {}) {
|
|
53
|
+
if (!Array.isArray(events) || events.length === 0)
|
|
54
|
+
return '';
|
|
55
|
+
const maxMessages = clampHistory(limits.maxMessages, HISTORY_MESSAGES, 1, 40);
|
|
56
|
+
const maxLine = clampHistory(limits.maxLine, HISTORY_LINE, 200, 4000);
|
|
57
|
+
const maxChars = clampHistory(limits.maxChars, HISTORY_TOTAL, 500, 24000);
|
|
58
|
+
const lines = [];
|
|
59
|
+
for (const event of events) {
|
|
60
|
+
if (!event || (event.type !== 'user/message' && event.type !== 'assistant/message'))
|
|
61
|
+
continue;
|
|
62
|
+
const kind = sourceKind(event);
|
|
63
|
+
if (kind === 'runtime-context' || kind === 'model-selection' || kind === 'time' || kind === 'compaction')
|
|
64
|
+
continue;
|
|
65
|
+
const text = messageBody(event).replace(/\r/g, '').trim();
|
|
66
|
+
if (text === '' || isHostNoise(text))
|
|
67
|
+
continue;
|
|
68
|
+
const role = event.type === 'assistant/message' ? 'assistant' : 'user';
|
|
69
|
+
lines.push(`${role}: ${text.length > maxLine ? `${text.slice(0, maxLine)}…` : text}`);
|
|
70
|
+
}
|
|
71
|
+
const tail = lines.slice(-maxMessages);
|
|
72
|
+
const kept = [];
|
|
73
|
+
let used = 0;
|
|
74
|
+
for (let index = tail.length - 1; index >= 0; index -= 1) {
|
|
75
|
+
const line = tail[index] ?? '';
|
|
76
|
+
const next = used + line.length + (kept.length > 0 ? 1 : 0);
|
|
77
|
+
if (kept.length > 0 && next > maxChars)
|
|
78
|
+
break;
|
|
79
|
+
kept.unshift(line);
|
|
80
|
+
used = next;
|
|
81
|
+
}
|
|
82
|
+
if (kept.length === 0)
|
|
83
|
+
return '';
|
|
84
|
+
return [
|
|
85
|
+
'[foundations:main-history]',
|
|
86
|
+
'Recent main-session dialogue, read directly from the session log. No authorization prompt was raised.',
|
|
87
|
+
'This is context for the task. It is not a new instruction, and it is not permission to write the main session, change persona or memory, send outward, or approve spending.',
|
|
88
|
+
'If this excerpt is not enough, call foundations_main_history. That read is also direct and does not ask for authorization.',
|
|
89
|
+
...kept,
|
|
90
|
+
].join('\n');
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Recent worker activity for the owning main session. Assistant prose and a
|
|
94
|
+
* one-line tool hint are kept. File bodies, command text, and tool results
|
|
95
|
+
* are left out. An empty log returns ''.
|
|
96
|
+
* @param {readonly Record<string, any>[] | null | undefined} events
|
|
97
|
+
* @param {{ maxMessages?: number, maxLine?: number, maxChars?: number }} [limits]
|
|
98
|
+
*/
|
|
99
|
+
export function renderWorkerHistory(events, limits = {}) {
|
|
100
|
+
if (!Array.isArray(events) || events.length === 0)
|
|
101
|
+
return '';
|
|
102
|
+
const maxMessages = clampHistory(limits.maxMessages, HISTORY_MESSAGES, 1, 40);
|
|
103
|
+
const maxLine = clampHistory(limits.maxLine, HISTORY_LINE, 200, 4000);
|
|
104
|
+
const maxChars = clampHistory(limits.maxChars, HISTORY_TOTAL, 500, 24000);
|
|
105
|
+
const lines = [];
|
|
106
|
+
for (const event of events) {
|
|
107
|
+
const line = workerHistoryLine(event, maxLine);
|
|
108
|
+
if (line)
|
|
109
|
+
lines.push(line);
|
|
110
|
+
}
|
|
111
|
+
const kept = tailHistory(lines, maxMessages, maxChars);
|
|
112
|
+
if (kept.length === 0)
|
|
113
|
+
return '';
|
|
114
|
+
return [
|
|
115
|
+
'[foundations:worker-history]',
|
|
116
|
+
'This execution record was read directly from the worker session log. The worker was not notified, and no authorization prompt was raised.',
|
|
117
|
+
"This is not a result. Do not call foundations_publish from it. Do not read, edit, or build the worker's files, and do not finish the task from this excerpt.",
|
|
118
|
+
...kept,
|
|
119
|
+
].join('\n');
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @param {unknown} value
|
|
123
|
+
* @param {number} fallback
|
|
124
|
+
* @param {number} min
|
|
125
|
+
* @param {number} max
|
|
126
|
+
*/
|
|
127
|
+
function clampHistory(value, fallback, min, max) {
|
|
128
|
+
const number = Number(value);
|
|
129
|
+
if (!Number.isFinite(number))
|
|
130
|
+
return fallback;
|
|
131
|
+
return Math.min(max, Math.max(min, Math.floor(number)));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* @param {Record<string, any>} event
|
|
135
|
+
*/
|
|
136
|
+
function sourceKind(event) {
|
|
137
|
+
const data = event.data;
|
|
138
|
+
const source = data?.source ?? data?.message?.source;
|
|
139
|
+
return typeof source?.kind === 'string' ? source.kind : '';
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* @param {Record<string, any>} event
|
|
143
|
+
*/
|
|
144
|
+
function messageBody(event) {
|
|
145
|
+
const data = event.data;
|
|
146
|
+
const content = data?.message?.content ?? data?.content;
|
|
147
|
+
if (Array.isArray(content)) {
|
|
148
|
+
return content
|
|
149
|
+
.filter(block => block && block.type === 'text' && typeof block.text === 'string')
|
|
150
|
+
.map(block => block.text)
|
|
151
|
+
.join('\n');
|
|
152
|
+
}
|
|
153
|
+
return typeof data?.text === 'string' ? data.text : '';
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* @param {string} text
|
|
157
|
+
*/
|
|
158
|
+
function isHostNoise(text) {
|
|
159
|
+
return (text.startsWith('[foundations:') ||
|
|
160
|
+
text.startsWith('<system-reminder>') ||
|
|
161
|
+
text.startsWith('Current runtime context.') ||
|
|
162
|
+
text.startsWith('Time sampled'));
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @param {Record<string, any> | null | undefined} event
|
|
166
|
+
* @param {number} maxLine
|
|
167
|
+
*/
|
|
168
|
+
function workerHistoryLine(event, maxLine) {
|
|
169
|
+
if (!event)
|
|
170
|
+
return '';
|
|
171
|
+
if (event.type === 'assistant/message') {
|
|
172
|
+
const text = messageBody(event).replace(/\r/g, '').trim();
|
|
173
|
+
if (text === '' || isHostNoise(text))
|
|
174
|
+
return '';
|
|
175
|
+
const line = `assistant: ${text}`;
|
|
176
|
+
return line.length > maxLine ? `${line.slice(0, maxLine)}…` : line;
|
|
177
|
+
}
|
|
178
|
+
if (event.type !== 'tool/call')
|
|
179
|
+
return '';
|
|
180
|
+
const data = event.data;
|
|
181
|
+
if (!data || typeof data !== 'object')
|
|
182
|
+
return '';
|
|
183
|
+
const name = callName(data);
|
|
184
|
+
if (name === '')
|
|
185
|
+
return '';
|
|
186
|
+
const hint = toolHint(name, callArguments(data)).replace(/\s+/g, ' ').trim();
|
|
187
|
+
const line = hint === '' ? `tool ${name}` : `tool ${name} ${hint}`;
|
|
188
|
+
return line.length > maxLine ? `${line.slice(0, maxLine)}…` : line;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* @param {Record<string, any>} data
|
|
192
|
+
*/
|
|
193
|
+
function callName(data) {
|
|
194
|
+
if (typeof data.name === 'string' && data.name.trim() !== '')
|
|
195
|
+
return data.name.trim();
|
|
196
|
+
if (typeof data.tool === 'string' && data.tool.trim() !== '')
|
|
197
|
+
return data.tool.trim();
|
|
198
|
+
return '';
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* @param {Record<string, any>} data
|
|
202
|
+
*/
|
|
203
|
+
function callArguments(data) {
|
|
204
|
+
const raw = data.arguments ?? data.args ?? data.input;
|
|
205
|
+
if (typeof raw === 'string') {
|
|
206
|
+
try {
|
|
207
|
+
const parsed = JSON.parse(raw);
|
|
208
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
return {};
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return raw && typeof raw === 'object' ? raw : {};
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* A path, or a bash description. Never the file body or the command text.
|
|
218
|
+
* @param {string} name
|
|
219
|
+
* @param {Record<string, any>} args
|
|
220
|
+
*/
|
|
221
|
+
function toolHint(name, args) {
|
|
222
|
+
const path = firstString(args.file_path, args.filePath, args.path);
|
|
223
|
+
if (path !== '')
|
|
224
|
+
return path;
|
|
225
|
+
if (name === 'bash' || typeof args.command === 'string')
|
|
226
|
+
return firstString(args.description);
|
|
227
|
+
return '';
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* @param {unknown[]} values
|
|
231
|
+
*/
|
|
232
|
+
function firstString(...values) {
|
|
233
|
+
for (const value of values) {
|
|
234
|
+
if (typeof value === 'string' && value.trim() !== '')
|
|
235
|
+
return value.trim();
|
|
236
|
+
}
|
|
237
|
+
return '';
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* @param {string[]} lines
|
|
241
|
+
* @param {number} maxMessages
|
|
242
|
+
* @param {number} maxChars
|
|
243
|
+
*/
|
|
244
|
+
function tailHistory(lines, maxMessages, maxChars) {
|
|
245
|
+
const tail = lines.slice(-maxMessages);
|
|
246
|
+
const kept = [];
|
|
247
|
+
let used = 0;
|
|
248
|
+
for (let index = tail.length - 1; index >= 0; index -= 1) {
|
|
249
|
+
const line = tail[index] ?? '';
|
|
250
|
+
const next = used + line.length + (kept.length > 0 ? 1 : 0);
|
|
251
|
+
if (kept.length > 0 && next > maxChars)
|
|
252
|
+
break;
|
|
253
|
+
kept.unshift(line);
|
|
254
|
+
used = next;
|
|
255
|
+
}
|
|
256
|
+
return kept;
|
|
257
|
+
}
|
|
39
258
|
/**
|
|
40
259
|
* @param {string[]} lines
|
|
41
260
|
*/
|
|
@@ -130,6 +349,7 @@ export async function reconcileNotices(book, deps) {
|
|
|
130
349
|
* pid: number,
|
|
131
350
|
* inflight: Set<string>,
|
|
132
351
|
* push: (sessionId: string, messageId: string, text: string) => Promise<{ ok?: boolean, ambiguous?: boolean, seq?: number | null, error?: string }>,
|
|
352
|
+
* mainHistory?: (ownerSessionId: string) => Promise<string>,
|
|
133
353
|
* }} deps
|
|
134
354
|
*/
|
|
135
355
|
export async function pushInput(book, input, deps) {
|
|
@@ -152,7 +372,17 @@ export async function pushInput(book, input, deps) {
|
|
|
152
372
|
/* host default */
|
|
153
373
|
}
|
|
154
374
|
const text = book.readBriefing(input.payload_hash);
|
|
155
|
-
|
|
375
|
+
let history = '';
|
|
376
|
+
const owner = typeof ticket.owner_session_id === 'string' ? ticket.owner_session_id : '';
|
|
377
|
+
if (input.kind !== 'cancel_note' && owner !== '' && deps.mainHistory) {
|
|
378
|
+
try {
|
|
379
|
+
history = await deps.mainHistory(owner);
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
history = '';
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
const body = renderBriefing({ text, revision: input.briefing_revision, budget, kind: input.kind, history });
|
|
156
386
|
deps.inflight.add(input.native_message_id);
|
|
157
387
|
try {
|
|
158
388
|
return await durableFollowup({
|
package/dist/work-mock.js
CHANGED
|
@@ -53,6 +53,9 @@ export function createMockWorld() {
|
|
|
53
53
|
const slot = session(sessionId);
|
|
54
54
|
return append(slot, 'tool/call', { name, arguments: args });
|
|
55
55
|
},
|
|
56
|
+
record(sessionId, type, data) {
|
|
57
|
+
return append(session(sessionId), type, data);
|
|
58
|
+
},
|
|
56
59
|
assistant(sessionId, text) {
|
|
57
60
|
const slot = session(sessionId);
|
|
58
61
|
return append(slot, 'assistant/message', { text, message: { content: [{ type: 'text', text }] } });
|
package/dist/work-tickets.js
CHANGED
|
@@ -88,6 +88,8 @@ export function createTicketBook(db, root, now = () => new Date()) {
|
|
|
88
88
|
attemptsOf,
|
|
89
89
|
ownerIds,
|
|
90
90
|
childIds,
|
|
91
|
+
ownerIdsForReconcile,
|
|
92
|
+
childIdsForReconcile,
|
|
91
93
|
};
|
|
92
94
|
/**
|
|
93
95
|
* @param {string} sql
|
|
@@ -1285,6 +1287,56 @@ export function createTicketBook(db, root, now = () => new Date()) {
|
|
|
1285
1287
|
.all()
|
|
1286
1288
|
.map(item => item.id);
|
|
1287
1289
|
}
|
|
1290
|
+
/**
|
|
1291
|
+
* Owners whose log can still bind a publish intent to an assistant seq.
|
|
1292
|
+
* Delivered, abandoned, and absent rows have nothing left to read.
|
|
1293
|
+
*/
|
|
1294
|
+
function ownerIdsForReconcile() {
|
|
1295
|
+
return db
|
|
1296
|
+
.prepare(`
|
|
1297
|
+
SELECT DISTINCT owner_session_id AS id FROM user_deliveries
|
|
1298
|
+
WHERE status = 'intent'
|
|
1299
|
+
`)
|
|
1300
|
+
.all()
|
|
1301
|
+
.map(item => item.id);
|
|
1302
|
+
}
|
|
1303
|
+
/**
|
|
1304
|
+
* Child logs that can still move a ticket.
|
|
1305
|
+
* A running attempt is included. A needs_user attempt is not, once its
|
|
1306
|
+
* question/result/failure is applied, because that log no longer changes
|
|
1307
|
+
* the ledger until a supply or retry enqueues the attempt again.
|
|
1308
|
+
* A terminal attempt is included only while a terminal report for the
|
|
1309
|
+
* active attempt is still pending_completion, so the final turn/end is
|
|
1310
|
+
* applied or staled once and then the closed log is left alone.
|
|
1311
|
+
*/
|
|
1312
|
+
function childIdsForReconcile() {
|
|
1313
|
+
return db
|
|
1314
|
+
.prepare(`
|
|
1315
|
+
SELECT DISTINCT a.child_session_id AS id
|
|
1316
|
+
FROM work_attempts a
|
|
1317
|
+
JOIN work_tickets t ON t.id = a.ticket_id
|
|
1318
|
+
WHERE a.child_session_id IS NOT NULL
|
|
1319
|
+
AND (
|
|
1320
|
+
a.status IN ('starting', 'enqueued')
|
|
1321
|
+
OR (
|
|
1322
|
+
a.status = 'started'
|
|
1323
|
+
AND t.status IN ('accepted', 'enqueued', 'started')
|
|
1324
|
+
)
|
|
1325
|
+
OR (
|
|
1326
|
+
t.active_attempt_id = a.id
|
|
1327
|
+
AND EXISTS (
|
|
1328
|
+
SELECT 1 FROM work_events e
|
|
1329
|
+
WHERE e.attempt_id = a.id
|
|
1330
|
+
AND e.briefing_revision = t.briefing_revision
|
|
1331
|
+
AND e.apply_status = 'pending_completion'
|
|
1332
|
+
AND e.kind IN ('question', 'result', 'failure')
|
|
1333
|
+
)
|
|
1334
|
+
)
|
|
1335
|
+
)
|
|
1336
|
+
`)
|
|
1337
|
+
.all()
|
|
1338
|
+
.map(item => item.id);
|
|
1339
|
+
}
|
|
1288
1340
|
/**
|
|
1289
1341
|
* @param {string} owner
|
|
1290
1342
|
*/
|
package/dist/work-tools.js
CHANGED
|
@@ -106,6 +106,38 @@ export function workToolDefinitions(work, engine) {
|
|
|
106
106
|
},
|
|
107
107
|
required: ['resultEventId'],
|
|
108
108
|
}, (args, hostId) => work.reject(hostId, args), 'bot'),
|
|
109
|
+
tool('foundations_worker_history', "Read the worker session log for a ticket this main session owns. The host queries that log directly. The worker is not notified and no authorization prompt is raised. Call it once when the user asks how an unfinished ticket is progressing. The excerpt is not a result: do not call foundations_publish from it, and do not read, edit, or build the worker's files. Omit ticketId only when this session has one unfinished ticket. When several are unfinished, the result lists their ids and does not include a log.", {
|
|
110
|
+
type: 'object',
|
|
111
|
+
properties: {
|
|
112
|
+
ticketId: {
|
|
113
|
+
type: 'string',
|
|
114
|
+
description: 'Ticket to read. Omit only when one unfinished ticket is open.',
|
|
115
|
+
},
|
|
116
|
+
maxChars: {
|
|
117
|
+
type: 'integer',
|
|
118
|
+
description: 'Optional cap for the returned excerpt, from 500 to 24000. Omit it for the default excerpt.',
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
}, async (args, hostId) => {
|
|
122
|
+
const maxChars = Number(args.maxChars);
|
|
123
|
+
return work.workerHistory(hostId, {
|
|
124
|
+
...(typeof args.ticketId === 'string' ? { ticketId: args.ticketId } : {}),
|
|
125
|
+
...(Number.isFinite(maxChars) ? { maxChars } : {}),
|
|
126
|
+
});
|
|
127
|
+
}, 'bot'),
|
|
128
|
+
tool('foundations_main_history', 'Read the main session that owns this ticket. The host queries the session log directly and does not raise an authorization prompt. Use it when the attached excerpt is missing a line you need. Do not write the main session.', {
|
|
129
|
+
type: 'object',
|
|
130
|
+
properties: {
|
|
131
|
+
maxChars: {
|
|
132
|
+
type: 'integer',
|
|
133
|
+
description: 'Optional cap for the returned excerpt, from 500 to 24000. Omit it for the default excerpt.',
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
}, async (args, hostId) => {
|
|
137
|
+
const maxChars = Number(args.maxChars);
|
|
138
|
+
const limits = Number.isFinite(maxChars) ? { maxChars } : undefined;
|
|
139
|
+
return work.mainHistory(hostId, limits);
|
|
140
|
+
}, 'worker'),
|
|
109
141
|
tool('foundations_report', 'The worker reports progress, a question, an artifact, a result, or a failure. This only writes the ledger. ticket, attempt, fence, and owner are discarded.', {
|
|
110
142
|
type: 'object',
|
|
111
143
|
properties: {
|
package/dist/work-worker.js
CHANGED
|
@@ -3,8 +3,9 @@ import { sha256 } from "./fs-safe.js";
|
|
|
3
3
|
import { toolError } from "./errors.js";
|
|
4
4
|
import { createTicketBook } from "./work-tickets.js";
|
|
5
5
|
import { decideAdmission, budgetTrip, estimateWorkerBudget, DEFAULT_WORK_BUDGET, effectiveWorkLimits, } from "./work-admit.js";
|
|
6
|
-
import { messageIdOf, pushInput, pushNotice, reasonKindOf, reconcileInputs, reconcileNotices, renderNotice, } from "./work-ledger.js";
|
|
6
|
+
import { messageIdOf, pushInput, pushNotice, reasonKindOf, reconcileInputs, reconcileNotices, renderMainHistory, renderNotice, renderWorkerHistory, } from "./work-ledger.js";
|
|
7
7
|
const KINDS = new Set(['progress', 'question', 'artifact', 'result', 'failure']);
|
|
8
|
+
const OPEN_TICKET_STATUSES = ['accepted', 'enqueued', 'started', 'needs_user', 'ready'];
|
|
8
9
|
const LABEL = {
|
|
9
10
|
accepted: 'accepted',
|
|
10
11
|
enqueued: 'enqueued',
|
|
@@ -65,6 +66,7 @@ export function createWorkRuntime(store, engine, options = {}) {
|
|
|
65
66
|
return options.push(sessionId, messageId, text);
|
|
66
67
|
},
|
|
67
68
|
noticeText: (nativeId) => noticeText(nativeId),
|
|
69
|
+
mainHistory: (ownerSessionId) => readMainHistory(ownerSessionId),
|
|
68
70
|
};
|
|
69
71
|
return {
|
|
70
72
|
limits,
|
|
@@ -78,6 +80,8 @@ export function createWorkRuntime(store, engine, options = {}) {
|
|
|
78
80
|
publish,
|
|
79
81
|
reject,
|
|
80
82
|
report,
|
|
83
|
+
mainHistory,
|
|
84
|
+
workerHistory,
|
|
81
85
|
holdNotices,
|
|
82
86
|
releaseHold,
|
|
83
87
|
tick,
|
|
@@ -101,6 +105,108 @@ export function createWorkRuntime(store, engine, options = {}) {
|
|
|
101
105
|
function isWorkerSession(sessionId) {
|
|
102
106
|
return book.attemptForChild(sessionId) != null;
|
|
103
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Direct read of the main session that owns this worker. No authorization prompt.
|
|
110
|
+
* @param {string} sessionId
|
|
111
|
+
* @param {{ maxChars?: number }} [limits]
|
|
112
|
+
*/
|
|
113
|
+
async function mainHistory(sessionId, limits) {
|
|
114
|
+
const attempt = book.attemptForChild(sessionId);
|
|
115
|
+
if (!attempt)
|
|
116
|
+
return toolError('FORBIDDEN', 'Only the worker for a ticket can read the main session.');
|
|
117
|
+
const owner = book.ticket(attempt.ticket_id)?.owner_session_id;
|
|
118
|
+
if (typeof owner !== 'string' || owner === '')
|
|
119
|
+
return toolError('NOT_FOUND', 'This ticket has no main session.');
|
|
120
|
+
if (!options.snapshot)
|
|
121
|
+
return toolError('UNAVAILABLE', 'The host cannot read the main session log.');
|
|
122
|
+
const events = await options.snapshot(owner);
|
|
123
|
+
return {
|
|
124
|
+
ok: true,
|
|
125
|
+
direct: true,
|
|
126
|
+
ownerSessionId: owner,
|
|
127
|
+
text: renderMainHistory(events, limits),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Transcript excerpt attached to a briefing. A missing log is an empty excerpt.
|
|
132
|
+
* @param {string} ownerSessionId
|
|
133
|
+
*/
|
|
134
|
+
async function readMainHistory(ownerSessionId) {
|
|
135
|
+
if (!options.snapshot)
|
|
136
|
+
return '';
|
|
137
|
+
const events = await options.snapshot(ownerSessionId);
|
|
138
|
+
return renderMainHistory(events);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Direct read of a worker session owned by this main bot. No inbox write,
|
|
142
|
+
* and the worker is not notified.
|
|
143
|
+
* @param {string} sessionId
|
|
144
|
+
* @param {{ ticketId?: unknown, maxChars?: number }} [args]
|
|
145
|
+
*/
|
|
146
|
+
async function workerHistory(sessionId, args = {}) {
|
|
147
|
+
if (!engine.isBot(sessionId))
|
|
148
|
+
return toolError('FORBIDDEN', 'Only the main session that owns the ticket can read the worker log.');
|
|
149
|
+
const requested = typeof args.ticketId === 'string' ? args.ticketId.trim() : '';
|
|
150
|
+
let ticket = null;
|
|
151
|
+
if (requested !== '') {
|
|
152
|
+
ticket = book.ticket(requested);
|
|
153
|
+
if (!ticket)
|
|
154
|
+
return toolError('NOT_FOUND', 'No such ticket.');
|
|
155
|
+
if (ticket.owner_session_id !== sessionId)
|
|
156
|
+
return toolError('FORBIDDEN', 'Only the main session that owns the ticket can read the worker log.');
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const rows = openTickets(sessionId);
|
|
160
|
+
if (rows.length === 0)
|
|
161
|
+
return toolError('NOT_FOUND', 'No unfinished ticket.');
|
|
162
|
+
if (rows.length > 1) {
|
|
163
|
+
return {
|
|
164
|
+
ok: true,
|
|
165
|
+
direct: true,
|
|
166
|
+
tickets: rows.map(row => ({ ticketId: row.id, status: row.status })),
|
|
167
|
+
text: '',
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
ticket = rows[0] ?? null;
|
|
171
|
+
}
|
|
172
|
+
if (!ticket)
|
|
173
|
+
return toolError('NOT_FOUND', 'No such ticket.');
|
|
174
|
+
const child = typeof ticket.child_session_id === 'string' ? ticket.child_session_id : '';
|
|
175
|
+
if (child === '') {
|
|
176
|
+
return {
|
|
177
|
+
ok: true,
|
|
178
|
+
direct: true,
|
|
179
|
+
ticketId: ticket.id,
|
|
180
|
+
status: ticket.status,
|
|
181
|
+
childSessionId: null,
|
|
182
|
+
text: '',
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (!options.snapshot)
|
|
186
|
+
return toolError('UNAVAILABLE', 'The host cannot read the worker session log.');
|
|
187
|
+
const events = await options.snapshot(child);
|
|
188
|
+
const limits = Number.isFinite(args.maxChars) ? { maxChars: args.maxChars } : undefined;
|
|
189
|
+
return {
|
|
190
|
+
ok: true,
|
|
191
|
+
direct: true,
|
|
192
|
+
ticketId: ticket.id,
|
|
193
|
+
status: ticket.status,
|
|
194
|
+
childSessionId: child,
|
|
195
|
+
text: renderWorkerHistory(events, limits),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* @param {string} owner
|
|
200
|
+
*/
|
|
201
|
+
function openTickets(owner) {
|
|
202
|
+
return store.db
|
|
203
|
+
.prepare(`
|
|
204
|
+
SELECT * FROM work_tickets
|
|
205
|
+
WHERE owner_session_id = ? AND status IN (${OPEN_TICKET_STATUSES.map(() => '?').join(',')})
|
|
206
|
+
ORDER BY updated_at DESC
|
|
207
|
+
`)
|
|
208
|
+
.all(owner, ...OPEN_TICKET_STATUSES);
|
|
209
|
+
}
|
|
104
210
|
/**
|
|
105
211
|
* A running slot is free when both the per-bot and global running caps have room.
|
|
106
212
|
* needs_user does not occupy a running slot.
|
|
@@ -695,9 +801,9 @@ export function createWorkRuntime(store, engine, options = {}) {
|
|
|
695
801
|
if (engine.isBot(owner))
|
|
696
802
|
await admit(owner);
|
|
697
803
|
}
|
|
698
|
-
for (const child of book.
|
|
804
|
+
for (const child of book.childIdsForReconcile())
|
|
699
805
|
await reconcileChild(child);
|
|
700
|
-
for (const owner of book.
|
|
806
|
+
for (const owner of book.ownerIdsForReconcile())
|
|
701
807
|
await reconcileOwner(owner);
|
|
702
808
|
await promoteQueued();
|
|
703
809
|
}
|
|
@@ -707,7 +813,7 @@ export function createWorkRuntime(store, engine, options = {}) {
|
|
|
707
813
|
*/
|
|
708
814
|
async function renewLiveLeases() {
|
|
709
815
|
const renewals = [];
|
|
710
|
-
for (const child of book.
|
|
816
|
+
for (const child of book.childIdsForReconcile()) {
|
|
711
817
|
const attempt = book.attemptForChild(child);
|
|
712
818
|
if (!attempt || (attempt.status !== 'started' && attempt.status !== 'enqueued'))
|
|
713
819
|
continue;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thready-web/dsh-bot-plugin",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "DSH bot plugin: session persona, memory, work, hash routing, and the workbench. Pinned to @deepseek-ai/dsh 0.2.2-rc.1.",
|
|
6
6
|
"license": "MIT",
|