@thready-web/dsh-bot-plugin 0.1.0-beta.1 → 0.1.0-beta.3
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 +31 -0
- package/client.js +646 -621
- package/dist/cli.js +32 -12
- package/dist/context.js +21 -9
- package/dist/cron.js +1 -1
- package/dist/desk.js +75 -26
- package/dist/engine.js +160 -77
- package/dist/fs-safe.js +1 -1
- package/dist/host.js +1 -0
- package/dist/http-plugin.js +6 -2
- package/dist/http.js +61 -23
- package/dist/inbox.js +10 -7
- package/dist/index.js +115 -28
- package/dist/live-diff.js +3 -1
- package/dist/live-snapshot.js +3 -1
- package/dist/provision.js +2 -0
- package/dist/runtime-registry.js +4 -4
- package/dist/session-files.js +1 -1
- package/dist/session-previews.js +5 -1
- package/dist/store.js +20 -5
- package/dist/tools.js +62 -26
- package/dist/triggers.js +83 -39
- package/dist/work-admit.js +124 -8
- package/dist/work-child.js +9 -6
- package/dist/work-http.js +4 -2
- package/dist/work-ledger.js +16 -14
- package/dist/work-mock-host.js +8 -6
- package/dist/work-mock.js +20 -19
- package/dist/work-tickets.js +156 -72
- package/dist/work-tools.js +2 -2
- package/dist/work-worker.js +213 -71
- package/package.json +8 -4
- package/public/assets/{index-ByU9a3Lh.js → index-D0tTt7E4.js} +37 -37
- package/public/assets/{index-Be82iGwe.css → index-PyqSrdBb.css} +1 -1
- package/public/index.html +2 -2
package/dist/engine.js
CHANGED
|
@@ -37,12 +37,14 @@ export class Engine {
|
|
|
37
37
|
if (existing)
|
|
38
38
|
return existing;
|
|
39
39
|
const created = now();
|
|
40
|
-
this.store.db
|
|
40
|
+
this.store.db
|
|
41
|
+
.prepare(`
|
|
41
42
|
INSERT INTO session_bindings (
|
|
42
43
|
session_id, kind, status, schema_version, persona_version_id, fork_parent_session_id,
|
|
43
44
|
fork_seeded, last_view_revision, compaction_epoch, error_code, created_at, updated_at
|
|
44
45
|
) VALUES (?, 'session', 'active', 1, NULL, ?, ?, 0, 0, NULL, ?, ?)
|
|
45
|
-
`)
|
|
46
|
+
`)
|
|
47
|
+
.run(sessionId, fork.parentSessionId ?? null, fork.seeded ? 1 : 0, created, created);
|
|
46
48
|
return this.binding(sessionId);
|
|
47
49
|
}
|
|
48
50
|
/**
|
|
@@ -69,22 +71,29 @@ export class Engine {
|
|
|
69
71
|
const stamped = now();
|
|
70
72
|
if (existing) {
|
|
71
73
|
if (existing.kind !== 'bot') {
|
|
72
|
-
this.store.db
|
|
74
|
+
this.store.db
|
|
75
|
+
.prepare(`UPDATE session_bindings SET kind = 'bot', updated_at = ? WHERE session_id = ?`)
|
|
76
|
+
.run(stamped, sessionId);
|
|
73
77
|
}
|
|
74
78
|
return this.binding(sessionId);
|
|
75
79
|
}
|
|
76
|
-
this.store.db
|
|
80
|
+
this.store.db
|
|
81
|
+
.prepare(`
|
|
77
82
|
INSERT INTO session_bindings (
|
|
78
83
|
session_id, kind, status, schema_version, persona_version_id, fork_parent_session_id,
|
|
79
84
|
fork_seeded, last_view_revision, compaction_epoch, error_code, created_at, updated_at
|
|
80
85
|
) VALUES (?, 'bot', 'active', 1, NULL, NULL, 0, 0, 0, NULL, ?, ?)
|
|
81
|
-
`)
|
|
86
|
+
`)
|
|
87
|
+
.run(sessionId, stamped, stamped);
|
|
82
88
|
return this.binding(sessionId);
|
|
83
89
|
}
|
|
84
90
|
listBots() {
|
|
85
|
-
return this.store.db
|
|
91
|
+
return this.store.db
|
|
92
|
+
.prepare(`
|
|
86
93
|
SELECT session_id, persona_version_id FROM session_bindings WHERE kind = 'bot' ORDER BY created_at
|
|
87
|
-
`)
|
|
94
|
+
`)
|
|
95
|
+
.all()
|
|
96
|
+
.map(row => {
|
|
88
97
|
const persona = row.persona_version_id == null ? null : this.persona(row.persona_version_id);
|
|
89
98
|
const bytes = persona ? this.store.readBlob('persona', persona.content_hash) : null;
|
|
90
99
|
const name = bytes ? splitPersona(bytes.toString('utf8')).name : '';
|
|
@@ -120,10 +129,12 @@ export class Engine {
|
|
|
120
129
|
if (existing)
|
|
121
130
|
return existing;
|
|
122
131
|
const created = now();
|
|
123
|
-
this.store.db
|
|
132
|
+
this.store.db
|
|
133
|
+
.prepare(`
|
|
124
134
|
INSERT INTO persona_versions (content_hash, byte_length, note, created_by, created_at)
|
|
125
135
|
VALUES (?, ?, ?, 'user', ?)
|
|
126
|
-
`)
|
|
136
|
+
`)
|
|
137
|
+
.run(hash, bytes.length, note ?? null, created);
|
|
127
138
|
return this.store.db.prepare('SELECT * FROM persona_versions WHERE content_hash = ?').get(hash);
|
|
128
139
|
}
|
|
129
140
|
/**
|
|
@@ -136,11 +147,13 @@ export class Engine {
|
|
|
136
147
|
else
|
|
137
148
|
this.ensureSession(sessionId);
|
|
138
149
|
if (personaId == null) {
|
|
139
|
-
this.store.db
|
|
150
|
+
this.store.db
|
|
151
|
+
.prepare(`
|
|
140
152
|
UPDATE session_bindings
|
|
141
153
|
SET persona_version_id = NULL, status = 'active', error_code = NULL, updated_at = ?
|
|
142
154
|
WHERE session_id = ?
|
|
143
|
-
`)
|
|
155
|
+
`)
|
|
156
|
+
.run(now(), sessionId);
|
|
144
157
|
return this.binding(sessionId);
|
|
145
158
|
}
|
|
146
159
|
const persona = this.persona(personaId);
|
|
@@ -150,11 +163,13 @@ export class Engine {
|
|
|
150
163
|
this.markError(sessionId, 'HASH_MISMATCH');
|
|
151
164
|
throw Object.assign(new Error('persona blob does not match its hash'), { code: 'HASH_MISMATCH' });
|
|
152
165
|
}
|
|
153
|
-
this.store.db
|
|
166
|
+
this.store.db
|
|
167
|
+
.prepare(`
|
|
154
168
|
UPDATE session_bindings
|
|
155
169
|
SET persona_version_id = ?, status = 'active', error_code = NULL, updated_at = ?
|
|
156
170
|
WHERE session_id = ?
|
|
157
|
-
`)
|
|
171
|
+
`)
|
|
172
|
+
.run(personaId, now(), sessionId);
|
|
158
173
|
return this.binding(sessionId);
|
|
159
174
|
}
|
|
160
175
|
/**
|
|
@@ -162,9 +177,11 @@ export class Engine {
|
|
|
162
177
|
* @param {string} code
|
|
163
178
|
*/
|
|
164
179
|
markError(sessionId, code) {
|
|
165
|
-
this.store.db
|
|
180
|
+
this.store.db
|
|
181
|
+
.prepare(`
|
|
166
182
|
UPDATE session_bindings SET status = 'error', error_code = ?, updated_at = ? WHERE session_id = ?
|
|
167
|
-
`)
|
|
183
|
+
`)
|
|
184
|
+
.run(code, now(), sessionId);
|
|
168
185
|
}
|
|
169
186
|
/**
|
|
170
187
|
* @param {string} sessionId
|
|
@@ -176,9 +193,11 @@ export class Engine {
|
|
|
176
193
|
const current = this.binding(sessionId).last_event_watermark;
|
|
177
194
|
if (current != null && BigInt(current) >= BigInt(watermark))
|
|
178
195
|
return;
|
|
179
|
-
this.store.db
|
|
196
|
+
this.store.db
|
|
197
|
+
.prepare(`
|
|
180
198
|
UPDATE session_bindings SET last_event_watermark = ?, updated_at = ? WHERE session_id = ?
|
|
181
|
-
`)
|
|
199
|
+
`)
|
|
200
|
+
.run(watermark, now(), sessionId);
|
|
182
201
|
}
|
|
183
202
|
/**
|
|
184
203
|
* @param {string} sessionId
|
|
@@ -187,10 +206,11 @@ export class Engine {
|
|
|
187
206
|
checkSource(sessionId, sourceEventId) {
|
|
188
207
|
if (sourceEventId == null || sourceEventId === '')
|
|
189
208
|
return null;
|
|
190
|
-
|
|
209
|
+
const sourceText = typeof sourceEventId === 'string' ? sourceEventId : String(sourceEventId);
|
|
210
|
+
if (!/^\d+$/.test(sourceText))
|
|
191
211
|
return toolError('VALIDATION', 'sourceEventId must be an event sequence number in this session.');
|
|
192
212
|
const watermark = this.binding(sessionId)?.last_event_watermark;
|
|
193
|
-
if (watermark == null || BigInt(
|
|
213
|
+
if (watermark == null || BigInt(sourceText) > BigInt(watermark)) {
|
|
194
214
|
return toolError('VALIDATION', 'sourceEventId is outside the events this session has observed.');
|
|
195
215
|
}
|
|
196
216
|
return null;
|
|
@@ -202,14 +222,19 @@ export class Engine {
|
|
|
202
222
|
freeze(sessionId, turn) {
|
|
203
223
|
const binding = this.ensureSession(sessionId);
|
|
204
224
|
const state = this.stateRow(sessionId);
|
|
205
|
-
const facts = this.store.db
|
|
225
|
+
const facts = this.store.db
|
|
226
|
+
.prepare(`
|
|
206
227
|
SELECT id FROM facts WHERE session_id = ? AND status = 'active'
|
|
207
228
|
ORDER BY priority DESC, updated_at DESC, id DESC
|
|
208
|
-
`)
|
|
209
|
-
|
|
229
|
+
`)
|
|
230
|
+
.all(sessionId)
|
|
231
|
+
.map(row => row.id);
|
|
232
|
+
const skills = this.store.db
|
|
233
|
+
.prepare(`
|
|
210
234
|
SELECT skill_id, version, summary, enabled, revoked, scope
|
|
211
235
|
FROM skill_bindings WHERE session_id = ? ORDER BY skill_id
|
|
212
|
-
`)
|
|
236
|
+
`)
|
|
237
|
+
.all(sessionId);
|
|
213
238
|
const selected = {
|
|
214
239
|
factIds: facts,
|
|
215
240
|
memoryIds: [],
|
|
@@ -219,15 +244,19 @@ export class Engine {
|
|
|
219
244
|
};
|
|
220
245
|
const revision = binding.last_view_revision + 1;
|
|
221
246
|
const frozenAt = now();
|
|
222
|
-
this.store.db
|
|
247
|
+
this.store.db
|
|
248
|
+
.prepare(`
|
|
223
249
|
INSERT INTO view_revisions (
|
|
224
250
|
session_id, revision, frozen_at, event_watermark, persona_version_id, working_state_rev,
|
|
225
251
|
snapshot_hash, compaction_epoch, presence, selected_json
|
|
226
252
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'unknown', ?)
|
|
227
|
-
`)
|
|
228
|
-
|
|
253
|
+
`)
|
|
254
|
+
.run(sessionId, revision, frozenAt, binding.last_event_watermark, binding.persona_version_id, state?.revision ?? 0, 'pending', binding.compaction_epoch, JSON.stringify(selected));
|
|
255
|
+
this.store.db
|
|
256
|
+
.prepare(`
|
|
229
257
|
UPDATE session_bindings SET last_view_revision = ?, updated_at = ? WHERE session_id = ?
|
|
230
|
-
`)
|
|
258
|
+
`)
|
|
259
|
+
.run(revision, frozenAt, sessionId);
|
|
231
260
|
this.openViews.set(sessionId, { revision, turn });
|
|
232
261
|
return this.view(sessionId, revision);
|
|
233
262
|
}
|
|
@@ -246,9 +275,11 @@ export class Engine {
|
|
|
246
275
|
* @param {number} revision
|
|
247
276
|
*/
|
|
248
277
|
view(sessionId, revision) {
|
|
249
|
-
const row = this.store.db
|
|
278
|
+
const row = this.store.db
|
|
279
|
+
.prepare(`
|
|
250
280
|
SELECT * FROM view_revisions WHERE session_id = ? AND revision = ?
|
|
251
|
-
`)
|
|
281
|
+
`)
|
|
282
|
+
.get(sessionId, revision);
|
|
252
283
|
if (!row)
|
|
253
284
|
return null;
|
|
254
285
|
return { ...row, selected: JSON.parse(row.selected_json) };
|
|
@@ -272,9 +303,11 @@ export class Engine {
|
|
|
272
303
|
* @param {'present' | 'unknown' | 'needs_reinject'} presence
|
|
273
304
|
*/
|
|
274
305
|
recordSnapshot(sessionId, revision, hash, presence) {
|
|
275
|
-
this.store.db
|
|
306
|
+
this.store.db
|
|
307
|
+
.prepare(`
|
|
276
308
|
UPDATE view_revisions SET snapshot_hash = ?, presence = ? WHERE session_id = ? AND revision = ?
|
|
277
|
-
`)
|
|
309
|
+
`)
|
|
310
|
+
.run(hash, presence, sessionId, revision);
|
|
278
311
|
}
|
|
279
312
|
/**
|
|
280
313
|
* Adapter-internal counter. Not a DSH field.
|
|
@@ -286,14 +319,18 @@ export class Engine {
|
|
|
286
319
|
if (!binding)
|
|
287
320
|
return;
|
|
288
321
|
const epoch = binding.compaction_epoch + 1;
|
|
289
|
-
this.store.db
|
|
322
|
+
this.store.db
|
|
323
|
+
.prepare(`
|
|
290
324
|
UPDATE session_bindings SET compaction_epoch = ?, updated_at = ? WHERE session_id = ?
|
|
291
|
-
`)
|
|
292
|
-
|
|
325
|
+
`)
|
|
326
|
+
.run(epoch, now(), sessionId);
|
|
327
|
+
this.store.db
|
|
328
|
+
.prepare(`
|
|
293
329
|
UPDATE view_revisions
|
|
294
330
|
SET compaction_epoch = ?, presence = ?
|
|
295
331
|
WHERE session_id = ? AND revision = ?
|
|
296
|
-
`)
|
|
332
|
+
`)
|
|
333
|
+
.run(epoch, presence, sessionId, binding.last_view_revision);
|
|
297
334
|
}
|
|
298
335
|
/**
|
|
299
336
|
* @param {string} sessionId
|
|
@@ -304,10 +341,12 @@ export class Engine {
|
|
|
304
341
|
const binding = this.binding(sessionId);
|
|
305
342
|
if (!binding)
|
|
306
343
|
return;
|
|
307
|
-
this.store.db
|
|
344
|
+
this.store.db
|
|
345
|
+
.prepare(`
|
|
308
346
|
UPDATE view_revisions SET presence = 'present'
|
|
309
347
|
WHERE session_id = ? AND revision = ? AND snapshot_hash != 'pending'
|
|
310
|
-
`)
|
|
348
|
+
`)
|
|
349
|
+
.run(sessionId, binding.last_view_revision);
|
|
311
350
|
}
|
|
312
351
|
/**
|
|
313
352
|
* @param {string} sessionId
|
|
@@ -341,10 +380,12 @@ export class Engine {
|
|
|
341
380
|
* @param {string} sessionId
|
|
342
381
|
*/
|
|
343
382
|
revokedSkills(sessionId) {
|
|
344
|
-
return this.store.db
|
|
383
|
+
return this.store.db
|
|
384
|
+
.prepare(`
|
|
345
385
|
SELECT skill_id, version FROM skill_bindings
|
|
346
386
|
WHERE session_id = ? AND revoked = 1 ORDER BY skill_id
|
|
347
|
-
`)
|
|
387
|
+
`)
|
|
388
|
+
.all(sessionId);
|
|
348
389
|
}
|
|
349
390
|
/**
|
|
350
391
|
* @param {number[]} ids
|
|
@@ -366,9 +407,11 @@ export class Engine {
|
|
|
366
407
|
* @param {number} viewRevision
|
|
367
408
|
*/
|
|
368
409
|
mutate(sessionId, idempotencyKey, payloadHash, toolName, request, apply, viewRevision) {
|
|
369
|
-
const prior = this.store.db
|
|
410
|
+
const prior = this.store.db
|
|
411
|
+
.prepare(`
|
|
370
412
|
SELECT * FROM mutations WHERE scope = ? AND idempotency_key = ?
|
|
371
|
-
`)
|
|
413
|
+
`)
|
|
414
|
+
.get(sessionId, idempotencyKey);
|
|
372
415
|
if (prior) {
|
|
373
416
|
if (prior.payload_hash !== payloadHash)
|
|
374
417
|
return toolError('IDEMPOTENCY_KEY_REUSE', 'The same idempotency key was reused for different content.');
|
|
@@ -386,24 +429,30 @@ export class Engine {
|
|
|
386
429
|
data: data.data,
|
|
387
430
|
export: 'pending',
|
|
388
431
|
};
|
|
389
|
-
const info = this.store.db
|
|
432
|
+
const info = this.store.db
|
|
433
|
+
.prepare(`
|
|
390
434
|
INSERT INTO mutations (
|
|
391
435
|
scope, idempotency_key, payload_hash, tool_name, request_json, result_json, outcome, created_at
|
|
392
436
|
) VALUES (?, ?, ?, ?, ?, ?, 'applied', ?)
|
|
393
|
-
`)
|
|
394
|
-
|
|
437
|
+
`)
|
|
438
|
+
.run(sessionId, idempotencyKey, payloadHash, toolName, JSON.stringify(request), JSON.stringify(result), now());
|
|
439
|
+
this.store.db
|
|
440
|
+
.prepare(`
|
|
395
441
|
INSERT INTO export_outbox (session_id, mutation_id, status, attempts, updated_at)
|
|
396
442
|
VALUES (?, ?, 'pending', 0, ?)
|
|
397
|
-
`)
|
|
443
|
+
`)
|
|
444
|
+
.run(sessionId, Number(info.lastInsertRowid), now());
|
|
398
445
|
return { ...result, mutationId: Number(info.lastInsertRowid) };
|
|
399
446
|
}
|
|
400
447
|
/**
|
|
401
448
|
* @param {string} sessionId
|
|
402
449
|
*/
|
|
403
450
|
async flushExport(sessionId) {
|
|
404
|
-
const rows = this.store.db
|
|
451
|
+
const rows = this.store.db
|
|
452
|
+
.prepare(`
|
|
405
453
|
SELECT * FROM export_outbox WHERE session_id = ? AND status IN ('pending','failed') ORDER BY id
|
|
406
|
-
`)
|
|
454
|
+
`)
|
|
455
|
+
.all(sessionId);
|
|
407
456
|
for (const row of rows)
|
|
408
457
|
await this.exportOne(row);
|
|
409
458
|
}
|
|
@@ -416,16 +465,20 @@ export class Engine {
|
|
|
416
465
|
const dir = join(this.store.root, 'export', row.session_id);
|
|
417
466
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
418
467
|
writeFileSync(join(dir, 'memory.md'), text, { mode: 0o600 });
|
|
419
|
-
this.store.db
|
|
468
|
+
this.store.db
|
|
469
|
+
.prepare(`
|
|
420
470
|
UPDATE export_outbox SET status = 'done', attempts = ?, last_error = NULL, updated_at = ? WHERE id = ?
|
|
421
|
-
`)
|
|
471
|
+
`)
|
|
472
|
+
.run(row.attempts + 1, now(), row.id);
|
|
422
473
|
return { ok: true };
|
|
423
474
|
}
|
|
424
475
|
catch (error) {
|
|
425
476
|
const message = error instanceof Error ? error.message : String(error);
|
|
426
|
-
this.store.db
|
|
477
|
+
this.store.db
|
|
478
|
+
.prepare(`
|
|
427
479
|
UPDATE export_outbox SET status = 'failed', attempts = ?, last_error = ?, updated_at = ? WHERE id = ?
|
|
428
|
-
`)
|
|
480
|
+
`)
|
|
481
|
+
.run(row.attempts + 1, message, now(), row.id);
|
|
429
482
|
return { ok: false, error: message };
|
|
430
483
|
}
|
|
431
484
|
}
|
|
@@ -433,16 +486,22 @@ export class Engine {
|
|
|
433
486
|
* @param {string} sessionId
|
|
434
487
|
*/
|
|
435
488
|
exportMarkdown(sessionId) {
|
|
436
|
-
const facts = this.store.db
|
|
489
|
+
const facts = this.store.db
|
|
490
|
+
.prepare(`
|
|
437
491
|
SELECT id, revision, content, status FROM facts WHERE session_id = ? ORDER BY id
|
|
438
|
-
`)
|
|
439
|
-
|
|
492
|
+
`)
|
|
493
|
+
.all(sessionId);
|
|
494
|
+
const memories = this.store.db
|
|
495
|
+
.prepare(`
|
|
440
496
|
SELECT id, revision, title, content, status FROM memories WHERE session_id = ? ORDER BY id
|
|
441
|
-
`)
|
|
497
|
+
`)
|
|
498
|
+
.all(sessionId);
|
|
442
499
|
const state = this.statePayload(sessionId);
|
|
443
500
|
const lines = [`# session ${sessionId}`, '', '## facts'];
|
|
444
501
|
for (const fact of facts) {
|
|
445
|
-
lines.push(fact.status === 'forgotten'
|
|
502
|
+
lines.push(fact.status === 'forgotten'
|
|
503
|
+
? `- forgotten:${fact.id}`
|
|
504
|
+
: `- ${fact.id} rev ${fact.revision} (${fact.status}): ${fact.content}`);
|
|
446
505
|
}
|
|
447
506
|
lines.push('', '## memories');
|
|
448
507
|
for (const memory of memories) {
|
|
@@ -467,16 +526,20 @@ export class Engine {
|
|
|
467
526
|
if (facts.length === 0 && memories.length === 0 && query.length <= 32) {
|
|
468
527
|
strategy = 'substring';
|
|
469
528
|
const like = `%${query.replace(/[\\%_]/g, char => `\\${char}`)}%`;
|
|
470
|
-
facts = this.store.db
|
|
529
|
+
facts = this.store.db
|
|
530
|
+
.prepare(`
|
|
471
531
|
SELECT id, content, revision, priority FROM facts
|
|
472
532
|
WHERE session_id = ? AND status = 'active' AND content LIKE ? ESCAPE '\\'
|
|
473
533
|
ORDER BY priority DESC, updated_at DESC LIMIT ?
|
|
474
|
-
`)
|
|
475
|
-
|
|
534
|
+
`)
|
|
535
|
+
.all(sessionId, like, limit);
|
|
536
|
+
memories = this.store.db
|
|
537
|
+
.prepare(`
|
|
476
538
|
SELECT id, title, content, revision FROM memories
|
|
477
539
|
WHERE session_id = ? AND status = 'active' AND (content LIKE ? ESCAPE '\\' OR ifnull(title,'') LIKE ? ESCAPE '\\')
|
|
478
540
|
ORDER BY updated_at DESC LIMIT ?
|
|
479
|
-
`)
|
|
541
|
+
`)
|
|
542
|
+
.all(sessionId, like, like, limit);
|
|
480
543
|
}
|
|
481
544
|
return { strategy, facts, memories };
|
|
482
545
|
}
|
|
@@ -504,7 +567,8 @@ export class Engine {
|
|
|
504
567
|
throw Object.assign(new Error('skill blob mismatch'), { code: 'HASH_MISMATCH' });
|
|
505
568
|
}
|
|
506
569
|
const boundAt = now();
|
|
507
|
-
this.store.db
|
|
570
|
+
this.store.db
|
|
571
|
+
.prepare(`
|
|
508
572
|
INSERT INTO skill_bindings (
|
|
509
573
|
session_id, skill_id, scope, version, content_hash, summary, enabled, revoked, bound_at
|
|
510
574
|
) VALUES (?, ?, ?, ?, ?, ?, 1, 0, ?)
|
|
@@ -516,7 +580,8 @@ export class Engine {
|
|
|
516
580
|
enabled = 1,
|
|
517
581
|
revoked = 0,
|
|
518
582
|
bound_at = excluded.bound_at
|
|
519
|
-
`)
|
|
583
|
+
`)
|
|
584
|
+
.run(sessionId, skillId, scope, version, hash, summary, boundAt);
|
|
520
585
|
return this.skillRow(sessionId, skillId);
|
|
521
586
|
}
|
|
522
587
|
/**
|
|
@@ -524,9 +589,11 @@ export class Engine {
|
|
|
524
589
|
* @param {string} skillId
|
|
525
590
|
*/
|
|
526
591
|
skillRow(sessionId, skillId) {
|
|
527
|
-
return this.store.db
|
|
592
|
+
return (this.store.db
|
|
593
|
+
.prepare(`
|
|
528
594
|
SELECT * FROM skill_bindings WHERE session_id = ? AND skill_id = ?
|
|
529
|
-
`)
|
|
595
|
+
`)
|
|
596
|
+
.get(sessionId, skillId) ?? null);
|
|
530
597
|
}
|
|
531
598
|
/**
|
|
532
599
|
* @param {string} sessionId
|
|
@@ -537,11 +604,13 @@ export class Engine {
|
|
|
537
604
|
const row = this.skillRow(sessionId, skillId);
|
|
538
605
|
if (!row)
|
|
539
606
|
throw Object.assign(new Error('skill not bound'), { code: 'NOT_FOUND' });
|
|
540
|
-
const enabled = patch.enabled == null ? row.enabled :
|
|
541
|
-
const revoked = patch.revoked == null ? row.revoked :
|
|
542
|
-
this.store.db
|
|
607
|
+
const enabled = patch.enabled == null ? row.enabled : patch.enabled ? 1 : 0;
|
|
608
|
+
const revoked = patch.revoked == null ? row.revoked : patch.revoked ? 1 : 0;
|
|
609
|
+
this.store.db
|
|
610
|
+
.prepare(`
|
|
543
611
|
UPDATE skill_bindings SET enabled = ?, revoked = ?, bound_at = ? WHERE session_id = ? AND skill_id = ?
|
|
544
|
-
`)
|
|
612
|
+
`)
|
|
613
|
+
.run(enabled, revoked, now(), sessionId, skillId);
|
|
545
614
|
return this.skillRow(sessionId, skillId);
|
|
546
615
|
}
|
|
547
616
|
/**
|
|
@@ -602,7 +671,13 @@ export class Engine {
|
|
|
602
671
|
return this.registerSkill(sessionId, scope, skillId, version, summary.slice(0, 240), bytes);
|
|
603
672
|
}
|
|
604
673
|
recover() {
|
|
605
|
-
const report = {
|
|
674
|
+
const report = {
|
|
675
|
+
bindings: 0,
|
|
676
|
+
errors: [],
|
|
677
|
+
promoted: [],
|
|
678
|
+
ftsRebuilt: false,
|
|
679
|
+
exports: [],
|
|
680
|
+
};
|
|
606
681
|
const personas = this.store.db.prepare('SELECT * FROM persona_versions').all();
|
|
607
682
|
for (const persona of personas) {
|
|
608
683
|
if (!this.personaBlobOk(persona))
|
|
@@ -620,9 +695,11 @@ export class Engine {
|
|
|
620
695
|
report.promoted.push(binding.session_id);
|
|
621
696
|
}
|
|
622
697
|
}
|
|
623
|
-
const skills = this.store.db
|
|
698
|
+
const skills = this.store.db
|
|
699
|
+
.prepare(`
|
|
624
700
|
SELECT * FROM skill_bindings WHERE session_id = ? AND enabled = 1 AND revoked = 0
|
|
625
|
-
`)
|
|
701
|
+
`)
|
|
702
|
+
.all(binding.session_id);
|
|
626
703
|
for (const skill of skills) {
|
|
627
704
|
const bytes = this.store.readBlob('skill', skill.content_hash);
|
|
628
705
|
if (bytes == null || sha256(bytes) !== skill.content_hash) {
|
|
@@ -631,10 +708,12 @@ export class Engine {
|
|
|
631
708
|
}
|
|
632
709
|
}
|
|
633
710
|
if (binding.last_view_revision > 0) {
|
|
634
|
-
this.store.db
|
|
711
|
+
this.store.db
|
|
712
|
+
.prepare(`
|
|
635
713
|
UPDATE view_revisions SET presence = 'unknown'
|
|
636
714
|
WHERE session_id = ? AND revision = ? AND presence = 'present'
|
|
637
|
-
`)
|
|
715
|
+
`)
|
|
716
|
+
.run(binding.session_id, binding.last_view_revision);
|
|
638
717
|
}
|
|
639
718
|
}
|
|
640
719
|
report.ftsRebuilt = rebuildFtsIfNeeded(this.store.db);
|
|
@@ -662,21 +741,25 @@ function searchFts(db, fts, columns, sessionId, table, query, limit) {
|
|
|
662
741
|
const match = `"${query.replaceAll('"', ' ')}"`;
|
|
663
742
|
try {
|
|
664
743
|
if (table === 'facts') {
|
|
665
|
-
return db
|
|
744
|
+
return db
|
|
745
|
+
.prepare(`
|
|
666
746
|
SELECT f.id, f.content, f.revision, f.priority
|
|
667
747
|
FROM facts_fts
|
|
668
748
|
JOIN facts f ON f.id = facts_fts.rowid
|
|
669
749
|
WHERE facts_fts MATCH ? AND f.session_id = ? AND f.status = 'active'
|
|
670
750
|
LIMIT ?
|
|
671
|
-
`)
|
|
751
|
+
`)
|
|
752
|
+
.all(match, sessionId, limit);
|
|
672
753
|
}
|
|
673
|
-
return db
|
|
754
|
+
return db
|
|
755
|
+
.prepare(`
|
|
674
756
|
SELECT m.id, m.title, m.content, m.revision
|
|
675
757
|
FROM memories_fts
|
|
676
758
|
JOIN memories m ON m.id = memories_fts.rowid
|
|
677
759
|
WHERE memories_fts MATCH ? AND m.session_id = ? AND m.status = 'active'
|
|
678
760
|
LIMIT ?
|
|
679
|
-
`)
|
|
761
|
+
`)
|
|
762
|
+
.all(match, sessionId, limit);
|
|
680
763
|
}
|
|
681
764
|
catch {
|
|
682
765
|
return [];
|
package/dist/fs-safe.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { closeSync, constants, fsyncSync, lstatSync, mkdirSync, openSync, readSync, realpathSync, renameSync, writeSync } from 'node:fs';
|
|
1
|
+
import { closeSync, constants, fsyncSync, lstatSync, mkdirSync, openSync, readSync, realpathSync, renameSync, writeSync, } from 'node:fs';
|
|
2
2
|
import { basename, dirname, join, relative, sep } from 'node:path';
|
|
3
3
|
import { createHash, randomBytes } from 'node:crypto';
|
|
4
4
|
const OPEN_FLAGS = constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0);
|
package/dist/host.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/http-plugin.js
CHANGED
|
@@ -22,12 +22,16 @@ export function apply(ctx, config = {}) {
|
|
|
22
22
|
try {
|
|
23
23
|
store.close();
|
|
24
24
|
}
|
|
25
|
-
catch {
|
|
25
|
+
catch {
|
|
26
|
+
/* closed */
|
|
27
|
+
}
|
|
26
28
|
});
|
|
27
29
|
let key = dataDir;
|
|
28
30
|
try {
|
|
29
31
|
key = realpathSync(dataDir);
|
|
30
32
|
}
|
|
31
|
-
catch {
|
|
33
|
+
catch {
|
|
34
|
+
/* not created yet */
|
|
35
|
+
}
|
|
32
36
|
registerHttp(ctx, engine, () => runtimeFor(key) ?? runtimeFor(dataDir), () => workRuntimeFor(key) ?? workRuntimeFor(dataDir), dataDir);
|
|
33
37
|
}
|