@thready-web/dsh-bot-plugin 0.1.0-beta.1 → 0.1.0-beta.11
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 +109 -1
- package/client.js +646 -621
- package/dist/approvals.js +160 -0
- package/dist/authorization.js +225 -0
- package/dist/canvas-http.js +248 -0
- package/dist/canvas-skills.js +56 -0
- package/dist/canvas-store.js +794 -0
- package/dist/canvas-tools.js +211 -0
- package/dist/cli.js +32 -12
- package/dist/context.js +60 -17
- package/dist/cron.js +1 -1
- package/dist/cua-access.js +79 -0
- package/dist/desk.js +314 -37
- package/dist/engine.js +178 -84
- package/dist/fs-safe.js +1 -1
- package/dist/host.js +1 -0
- package/dist/http-plugin.js +14 -4
- package/dist/http.js +402 -85
- package/dist/inbox.js +51 -40
- package/dist/index.js +201 -47
- package/dist/live-diff.js +3 -1
- package/dist/live-snapshot.js +2 -13
- package/dist/log/canonical.js +37 -0
- package/dist/log/plan.js +62 -0
- package/dist/log/routes.js +144 -0
- package/dist/log/snapshot-service.js +76 -0
- package/dist/log/source.js +173 -0
- package/dist/memory-catalog.js +798 -0
- package/dist/permissions.js +86 -0
- package/dist/provision.js +2 -0
- package/dist/questions.js +407 -0
- package/dist/runtime-registry.js +4 -4
- package/dist/session-files.js +2 -2
- package/dist/session-previews.js +5 -1
- package/dist/store.js +20 -5
- package/dist/tools.js +150 -32
- package/dist/triggers.js +83 -39
- package/dist/work-admit.js +222 -25
- package/dist/work-child.js +17 -9
- package/dist/work-http.js +52 -2
- package/dist/work-ledger.js +256 -16
- package/dist/work-mock-host.js +9 -6
- package/dist/work-mock.js +29 -19
- package/dist/work-tickets.js +423 -96
- package/dist/work-tools.js +43 -6
- package/dist/work-worker.js +1033 -207
- package/dist/workspace-files.js +319 -0
- package/dist/workspace-image.js +91 -0
- package/fixtures/mock-llm.mjs +17 -2
- package/package.json +16 -5
- package/public/assets/SourceEditor-CJwK846Z.css +1 -0
- package/public/assets/SourceEditor-uu0G7jsM.js +669 -0
- package/public/assets/abap-CRCWOmpq.js +1 -0
- package/public/assets/apex-DnsZk_dE.js +1 -0
- package/public/assets/azcli-1IWB1ccx.js +1 -0
- package/public/assets/bat-DPkNLes8.js +1 -0
- package/public/assets/bicep-Corcdgou.js +2 -0
- package/public/assets/cameligo-CGrWLZr3.js +1 -0
- package/public/assets/clojure-D9WOWImG.js +1 -0
- package/public/assets/codicon-DCmgc-ay.ttf +0 -0
- package/public/assets/coffee-B7EJu28W.js +1 -0
- package/public/assets/cpp-SEyurbux.js +1 -0
- package/public/assets/csharp-BoL64M5l.js +1 -0
- package/public/assets/csp-C46ZqvIl.js +1 -0
- package/public/assets/css-DQU6DXDx.js +3 -0
- package/public/assets/css.worker-DYpkewsP.js +79 -0
- package/public/assets/cssMode-C5DAFF5H.js +4 -0
- package/public/assets/cypher-D84EuPTj.js +1 -0
- package/public/assets/dart-D8lhlL1r.js +1 -0
- package/public/assets/dockerfile-DLk6rpji.js +1 -0
- package/public/assets/ecl-BO6FnfXk.js +1 -0
- package/public/assets/editor.worker-B4pQIWZD.js +12 -0
- package/public/assets/elixir-BRjLKONM.js +1 -0
- package/public/assets/flow9-Cac8vKd7.js +1 -0
- package/public/assets/freemarker2-DExNwSiZ.js +3 -0
- package/public/assets/fsharp-fd1GTHhf.js +1 -0
- package/public/assets/go-O9LJTZXk.js +1 -0
- package/public/assets/graphql-LQdxqEYJ.js +1 -0
- package/public/assets/handlebars-BK7YAvpQ.js +1 -0
- package/public/assets/hcl-DxDQ3s82.js +1 -0
- package/public/assets/html-CdDm_zae.js +1 -0
- package/public/assets/html.worker-tnXziJOq.js +456 -0
- package/public/assets/htmlMode-CLdn9oa1.js +4 -0
- package/public/assets/index-BB-QDSf2.css +1 -0
- package/public/assets/index-CsX-_qbo.js +62 -0
- package/public/assets/ini-BvajGCUy.js +1 -0
- package/public/assets/java-SYsfObOQ.js +1 -0
- package/public/assets/javascript-mLl83_3W.js +1 -0
- package/public/assets/json.worker-aFakAbRR.js +44 -0
- package/public/assets/jsonMode-B1-bihFd.js +10 -0
- package/public/assets/julia-DQXNmw_w.js +1 -0
- package/public/assets/kotlin-qQ0MG-9I.js +1 -0
- package/public/assets/less-GGFNNJHn.js +2 -0
- package/public/assets/lexon-Canl7DCW.js +1 -0
- package/public/assets/liquid-Cv8m0HUb.js +1 -0
- package/public/assets/lua-D28Ae8-K.js +1 -0
- package/public/assets/m3-DPitgjJI.js +1 -0
- package/public/assets/markdown-B811l8j2.js +1 -0
- package/public/assets/mdx-Q12b91rL.js +1 -0
- package/public/assets/mips-CdjsipkG.js +1 -0
- package/public/assets/msdax-CYqgjx_P.js +1 -0
- package/public/assets/mysql-BHd6q0vd.js +1 -0
- package/public/assets/objective-c-B1aVtJYH.js +1 -0
- package/public/assets/pascal-BhNW15KB.js +1 -0
- package/public/assets/pascaligo-5jv8CcQD.js +1 -0
- package/public/assets/perl-DlYyT36c.js +1 -0
- package/public/assets/pgsql-Dy0bjov7.js +1 -0
- package/public/assets/php-120yhfDK.js +1 -0
- package/public/assets/pla-CjnFlu4u.js +1 -0
- package/public/assets/postiats-CQpG440k.js +1 -0
- package/public/assets/powerquery-DdJtto1Z.js +1 -0
- package/public/assets/powershell-Bu_VLpJB.js +1 -0
- package/public/assets/protobuf-IBS6jZEB.js +2 -0
- package/public/assets/pug-kFxLfcjb.js +1 -0
- package/public/assets/python-Kda811W0.js +1 -0
- package/public/assets/qsharp-q7JyzKFN.js +1 -0
- package/public/assets/r-BIFz-_sK.js +1 -0
- package/public/assets/razor-CRuCpdOW.js +1 -0
- package/public/assets/redis-CHOsPHWR.js +1 -0
- package/public/assets/redshift-CBifECDb.js +1 -0
- package/public/assets/restructuredtext-CghPJEOS.js +1 -0
- package/public/assets/ruby-CYWGW-b1.js +1 -0
- package/public/assets/rust-DMDD0SHb.js +1 -0
- package/public/assets/sb-BYAiYHFx.js +1 -0
- package/public/assets/scala-Bqvq8jcR.js +1 -0
- package/public/assets/scheme-Dhb-2j9p.js +1 -0
- package/public/assets/scss-CTwUZ5N7.js +3 -0
- package/public/assets/shell-CsDZo4DB.js +1 -0
- package/public/assets/solidity-CME5AdoB.js +1 -0
- package/public/assets/sophia-RYC1BQQz.js +1 -0
- package/public/assets/sparql-KEyrF7De.js +1 -0
- package/public/assets/sql-BdTr02Mf.js +1 -0
- package/public/assets/st-C7iG7M4S.js +1 -0
- package/public/assets/swift-D7IUmUK8.js +1 -0
- package/public/assets/systemverilog-DgMryOEJ.js +1 -0
- package/public/assets/tcl-PloMZuKG.js +1 -0
- package/public/assets/ts.worker-Bij5J2kb.js +51316 -0
- package/public/assets/tsMode-BGHcz-M3.js +11 -0
- package/public/assets/twig-BfRIq3la.js +1 -0
- package/public/assets/typescript-Db1e0DCT.js +1 -0
- package/public/assets/typespec-CzxlYoT_.js +1 -0
- package/public/assets/vb-BwAE3J76.js +1 -0
- package/public/assets/wgsl-B_1kOXbF.js +298 -0
- package/public/assets/xml-H3zzFmnd.js +1 -0
- package/public/assets/yaml-BQrKvmTF.js +1 -0
- package/public/index.html +2 -2
- package/skills/ui/baseline-ui/SKILL.md +85 -0
- package/skills/ui/bencium-innovative-ux-designer/SKILL.md +227 -0
- package/skills/ui/better-ui/SKILL.md +133 -0
- package/skills/ui/design-lab/SKILL.md +920 -0
- package/skills/ui/frontend-design/SKILL.md +71 -0
- package/skills/ui/impeccable/SKILL.md +93 -0
- package/skills/ui/interface-design/SKILL.md +320 -0
- package/skills/ui/sources.json +66 -0
- package/skills/ui/ui-design/SKILL.md +35 -0
- package/skills/ui/ui-skills-root/SKILL.md +59 -0
- package/skills/ui/visual-design-foundations/SKILL.md +318 -0
- package/skills/ui/web-design-guidelines/SKILL.md +39 -0
- package/public/assets/index-Be82iGwe.css +0 -1
- package/public/assets/index-ByU9a3Lh.js +0 -61
package/dist/log/plan.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { chunkBuffer } from "../cdc.js";
|
|
3
|
+
import { eventNdjson } from "./canonical.js";
|
|
4
|
+
/** FastCDC cuts, snapped forward to the next newline so each chunk is a whole event range. */
|
|
5
|
+
export function alignCutsToLines(data, cuts) {
|
|
6
|
+
const n = data.length;
|
|
7
|
+
const out = [];
|
|
8
|
+
let offset = 0;
|
|
9
|
+
for (const cut of cuts) {
|
|
10
|
+
if (offset >= n)
|
|
11
|
+
break;
|
|
12
|
+
let end = Math.max(offset, cut.offset + cut.length);
|
|
13
|
+
if (end < n) {
|
|
14
|
+
while (end < n && data[end - 1] !== 10)
|
|
15
|
+
end += 1;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
end = n;
|
|
19
|
+
}
|
|
20
|
+
if (end > offset)
|
|
21
|
+
out.push({ offset, length: end - offset });
|
|
22
|
+
offset = end;
|
|
23
|
+
}
|
|
24
|
+
if (offset < n)
|
|
25
|
+
out.push({ offset, length: n - offset });
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
export function planEventChunks(events) {
|
|
29
|
+
const { bytes, lines } = eventNdjson(events);
|
|
30
|
+
if (bytes.length === 0 || lines.length === 0) {
|
|
31
|
+
return { throughSeq: -1, count: 0, totalSize: 0, chunks: [], epoch: '', format: 'ndjson-v1' };
|
|
32
|
+
}
|
|
33
|
+
const cuts = alignCutsToLines(bytes, chunkBuffer(bytes));
|
|
34
|
+
const chunks = [];
|
|
35
|
+
const hashes = [];
|
|
36
|
+
for (const cut of cuts) {
|
|
37
|
+
const body = bytes.subarray(cut.offset, cut.offset + cut.length);
|
|
38
|
+
const covered = lines.filter(line => line.start >= cut.offset && line.end <= cut.offset + cut.length);
|
|
39
|
+
if (covered.length === 0)
|
|
40
|
+
continue;
|
|
41
|
+
const hash = createHash('sha256').update(body).digest('hex');
|
|
42
|
+
hashes.push(hash);
|
|
43
|
+
chunks.push({
|
|
44
|
+
fromSeq: covered[0].seq,
|
|
45
|
+
toSeq: covered[covered.length - 1].seq,
|
|
46
|
+
bytes: cut.length,
|
|
47
|
+
hash,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const epoch = createHash('sha256').update(hashes.join('\n')).digest('hex');
|
|
51
|
+
return {
|
|
52
|
+
throughSeq: lines[lines.length - 1].seq,
|
|
53
|
+
count: lines.length,
|
|
54
|
+
totalSize: bytes.length,
|
|
55
|
+
chunks,
|
|
56
|
+
epoch,
|
|
57
|
+
format: 'ndjson-v1',
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export function sha256Hex(data) {
|
|
61
|
+
return createHash('sha256').update(data).digest('hex');
|
|
62
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { HttpError, RANGE_PAGE_LIMIT } from "./snapshot-service.js";
|
|
2
|
+
export const LOG_ROUTE_PREFIX = '/api/dsh-bot/log';
|
|
3
|
+
export const LOG_MANIFEST_PATH = `${LOG_ROUTE_PREFIX}/manifest`;
|
|
4
|
+
export const LOG_CHUNK_PATH = `${LOG_ROUTE_PREFIX}/chunk`;
|
|
5
|
+
export const LOG_RANGE_PATH = `${LOG_ROUTE_PREFIX}/range`;
|
|
6
|
+
/**
|
|
7
|
+
* Optional `connection` must be read with `ctx.get`, never `ctx.connection`.
|
|
8
|
+
* Cordis throws `cannot get property "connection" without inject` on the
|
|
9
|
+
* property access when the HTTP plugin did not declare that service.
|
|
10
|
+
*/
|
|
11
|
+
export function probeConnection(ctx) {
|
|
12
|
+
if (typeof ctx.get !== 'function')
|
|
13
|
+
return undefined;
|
|
14
|
+
return ctx.get('connection');
|
|
15
|
+
}
|
|
16
|
+
export function registerLogRoutes(ctx, service) {
|
|
17
|
+
const connection = probeConnection(ctx);
|
|
18
|
+
if (connection?.fetch?.register === undefined)
|
|
19
|
+
return false;
|
|
20
|
+
const handlers = logFetchHandlers(service);
|
|
21
|
+
for (const [path, handler] of handlers) {
|
|
22
|
+
ctx.effect?.(() => {
|
|
23
|
+
const dispose = connection.fetch.register({
|
|
24
|
+
path,
|
|
25
|
+
methods: ['GET', 'HEAD'],
|
|
26
|
+
requestBody: 'buffered',
|
|
27
|
+
fetch: handler,
|
|
28
|
+
});
|
|
29
|
+
return typeof dispose === 'function'
|
|
30
|
+
? () => {
|
|
31
|
+
void dispose();
|
|
32
|
+
}
|
|
33
|
+
: undefined;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
export function logFetchHandlers(service) {
|
|
39
|
+
return [
|
|
40
|
+
[LOG_MANIFEST_PATH, request => handleManifest(service, request)],
|
|
41
|
+
[LOG_CHUNK_PATH, request => handleChunk(service, request)],
|
|
42
|
+
[LOG_RANGE_PATH, request => handleRange(service, request)],
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
export async function dispatchLogRequest(service, request) {
|
|
46
|
+
const path = new URL(request.url).pathname.replace(/\/+$/, '') || '/';
|
|
47
|
+
const handler = logFetchHandlers(service).find(([route]) => route === path)?.[1];
|
|
48
|
+
if (!handler)
|
|
49
|
+
return null;
|
|
50
|
+
return handler(request);
|
|
51
|
+
}
|
|
52
|
+
async function handleManifest(service, request) {
|
|
53
|
+
return wrap(request, async (url) => {
|
|
54
|
+
const sessionId = requiredSession(url);
|
|
55
|
+
const throughSeq = readInt(url, 'throughSeq', true);
|
|
56
|
+
const afterSeq = readInt(url, 'afterSeq', true);
|
|
57
|
+
const body = await service.manifest(sessionId, throughSeq ?? undefined, afterSeq ?? undefined);
|
|
58
|
+
return json(200, body);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async function handleChunk(service, request) {
|
|
62
|
+
return wrap(request, async (url) => {
|
|
63
|
+
const sessionId = requiredSession(url);
|
|
64
|
+
const fromSeq = readInt(url, 'fromSeq', false);
|
|
65
|
+
const toSeq = readInt(url, 'toSeq', false);
|
|
66
|
+
const hash = url.searchParams.get('hash') ?? '';
|
|
67
|
+
if (fromSeq == null || toSeq == null)
|
|
68
|
+
throw new HttpError(400, 'VALIDATION', 'fromSeq and toSeq are required.');
|
|
69
|
+
const bytes = await service.chunk(sessionId, fromSeq, toSeq, hash);
|
|
70
|
+
const headers = {
|
|
71
|
+
'content-type': 'application/octet-stream',
|
|
72
|
+
'content-length': String(bytes.length),
|
|
73
|
+
'cache-control': 'private, immutable',
|
|
74
|
+
};
|
|
75
|
+
if (request.method === 'HEAD')
|
|
76
|
+
return new Response(null, { status: 200, headers });
|
|
77
|
+
const out = new Uint8Array(bytes.byteLength);
|
|
78
|
+
out.set(bytes);
|
|
79
|
+
return new Response(out, { status: 200, headers });
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
async function handleRange(service, request) {
|
|
83
|
+
if (request.method === 'HEAD') {
|
|
84
|
+
return new Response(null, {
|
|
85
|
+
status: 200,
|
|
86
|
+
headers: { 'cache-control': 'no-store', 'content-type': 'application/json; charset=utf-8' },
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return wrap(request, async (url) => {
|
|
90
|
+
const sessionId = requiredSession(url);
|
|
91
|
+
const afterSeq = readInt(url, 'afterSeq', true) ?? -1;
|
|
92
|
+
const throughSeq = readInt(url, 'throughSeq', true);
|
|
93
|
+
const limit = readInt(url, 'limit', true) ?? RANGE_PAGE_LIMIT;
|
|
94
|
+
if (limit != null && limit <= 0)
|
|
95
|
+
throw new HttpError(400, 'VALIDATION', 'limit must be a positive integer.');
|
|
96
|
+
const body = await service.range(sessionId, afterSeq, throughSeq ?? undefined, limit ?? RANGE_PAGE_LIMIT);
|
|
97
|
+
return json(200, body);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
async function wrap(request, run) {
|
|
101
|
+
try {
|
|
102
|
+
const response = await run(new URL(request.url));
|
|
103
|
+
if (request.method === 'HEAD')
|
|
104
|
+
return new Response(null, { status: response.status, headers: response.headers });
|
|
105
|
+
return response;
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
if (error instanceof HttpError)
|
|
109
|
+
return json(error.status, { ok: false, code: error.code, message: error.message });
|
|
110
|
+
return json(500, {
|
|
111
|
+
ok: false,
|
|
112
|
+
code: 'UNAVAILABLE',
|
|
113
|
+
message: error instanceof Error ? error.message : String(error),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function requiredSession(url) {
|
|
118
|
+
const sessionId = url.searchParams.get('sessionId') ?? '';
|
|
119
|
+
if (sessionId === '')
|
|
120
|
+
throw new HttpError(400, 'VALIDATION', 'sessionId is required.');
|
|
121
|
+
return sessionId;
|
|
122
|
+
}
|
|
123
|
+
function readInt(url, name, optional) {
|
|
124
|
+
const raw = url.searchParams.get(name);
|
|
125
|
+
if (raw == null || raw === '')
|
|
126
|
+
return optional ? null : null;
|
|
127
|
+
if (!/^-?\d+$/.test(raw))
|
|
128
|
+
throw new HttpError(400, 'VALIDATION', `${name} must be an integer.`);
|
|
129
|
+
const value = Number(raw);
|
|
130
|
+
if (!Number.isSafeInteger(value))
|
|
131
|
+
throw new HttpError(400, 'VALIDATION', `${name} must be an integer.`);
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
function json(status, body) {
|
|
135
|
+
const text = JSON.stringify(body);
|
|
136
|
+
return new Response(text, {
|
|
137
|
+
status,
|
|
138
|
+
headers: {
|
|
139
|
+
'content-type': 'application/json; charset=utf-8',
|
|
140
|
+
'content-length': String(Buffer.byteLength(text)),
|
|
141
|
+
'cache-control': 'no-store',
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { eventNdjson } from "./canonical.js";
|
|
2
|
+
import { planEventChunks, sha256Hex } from "./plan.js";
|
|
3
|
+
export const RANGE_PAGE_LIMIT = 500;
|
|
4
|
+
export const LOG_FORMAT = 'ndjson-v1';
|
|
5
|
+
export class HttpError extends Error {
|
|
6
|
+
status;
|
|
7
|
+
code;
|
|
8
|
+
constructor(status, code, message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function createLogSnapshotService(source) {
|
|
15
|
+
const plans = new Map();
|
|
16
|
+
const inflight = new Map();
|
|
17
|
+
async function planOf(sessionId, throughSeq) {
|
|
18
|
+
const head = await source.head(sessionId);
|
|
19
|
+
const pin = throughSeq == null ? head : throughSeq;
|
|
20
|
+
if (!Number.isInteger(pin) || pin < -1)
|
|
21
|
+
throw new HttpError(400, 'VALIDATION', 'throughSeq must be an integer >= -1.');
|
|
22
|
+
if (pin > head)
|
|
23
|
+
throw new HttpError(409, 'STALE', 'throughSeq is past the current log head.');
|
|
24
|
+
const cached = plans.get(sessionId);
|
|
25
|
+
if (cached && cached.throughSeq === pin)
|
|
26
|
+
return cached;
|
|
27
|
+
const { events } = await source.read(sessionId, -1, pin);
|
|
28
|
+
const planned = { sessionId, ...planEventChunks(events), throughSeq: pin, count: events.length };
|
|
29
|
+
if (pin === -1)
|
|
30
|
+
planned.throughSeq = -1;
|
|
31
|
+
plans.set(sessionId, planned);
|
|
32
|
+
return planned;
|
|
33
|
+
}
|
|
34
|
+
async function chunkBytes(sessionId, fromSeq, toSeq, hash) {
|
|
35
|
+
const key = `${sessionId}:${fromSeq}:${toSeq}:${hash}`;
|
|
36
|
+
const existing = inflight.get(key);
|
|
37
|
+
if (existing)
|
|
38
|
+
return existing;
|
|
39
|
+
const job = (async () => {
|
|
40
|
+
const { events } = await source.read(sessionId, fromSeq - 1, toSeq);
|
|
41
|
+
const { bytes } = eventNdjson(events);
|
|
42
|
+
if (sha256Hex(bytes) !== hash)
|
|
43
|
+
throw new HttpError(409, 'STALE', 'Chunk hash does not match this range.');
|
|
44
|
+
return bytes;
|
|
45
|
+
})();
|
|
46
|
+
inflight.set(key, job);
|
|
47
|
+
try {
|
|
48
|
+
return await job;
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
inflight.delete(key);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
async manifest(sessionId, throughSeq, afterSeq) {
|
|
56
|
+
const plan = await planOf(sessionId, throughSeq);
|
|
57
|
+
const after = afterSeq == null ? -1 : afterSeq;
|
|
58
|
+
const chunks = after < 0 ? plan.chunks : plan.chunks.filter(chunk => chunk.toSeq > after);
|
|
59
|
+
return { ...plan, chunks };
|
|
60
|
+
},
|
|
61
|
+
async chunk(sessionId, fromSeq, toSeq, hash) {
|
|
62
|
+
if (hash === '' || !Number.isInteger(fromSeq) || !Number.isInteger(toSeq) || fromSeq > toSeq) {
|
|
63
|
+
throw new HttpError(400, 'VALIDATION', 'fromSeq, toSeq, and hash are required.');
|
|
64
|
+
}
|
|
65
|
+
return chunkBytes(sessionId, fromSeq, toSeq, hash);
|
|
66
|
+
},
|
|
67
|
+
async range(sessionId, afterSeq, throughSeq, limit) {
|
|
68
|
+
const pageLimit = Math.min(Math.max(1, limit), RANGE_PAGE_LIMIT);
|
|
69
|
+
const { events, throughSeq: pin } = await source.read(sessionId, afterSeq, throughSeq);
|
|
70
|
+
const page = events.slice(0, pageLimit);
|
|
71
|
+
const last = page.length === 0 ? afterSeq : page[page.length - 1].seq;
|
|
72
|
+
const nextAfterSeq = last < pin && page.length === pageLimit ? last : null;
|
|
73
|
+
return { events: page, throughSeq: pin, nextAfterSeq };
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
export function createLogSource(ctx) {
|
|
2
|
+
const source = {
|
|
3
|
+
async head(sessionId) {
|
|
4
|
+
const cut = await observe(ctx, sessionId);
|
|
5
|
+
return cut.throughSeq;
|
|
6
|
+
},
|
|
7
|
+
async read(sessionId, afterSeq, throughSeq) {
|
|
8
|
+
const cut = await observe(ctx, sessionId);
|
|
9
|
+
const end = throughSeq == null ? cut.throughSeq : Math.min(throughSeq, cut.throughSeq);
|
|
10
|
+
const events = cut.events.filter(event => event.seq > afterSeq && event.seq <= end);
|
|
11
|
+
return { events, throughSeq: end };
|
|
12
|
+
},
|
|
13
|
+
async *tail(sessionId, afterSeq, signal) {
|
|
14
|
+
yield* tailEvents(ctx, source, sessionId, afterSeq, signal);
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
return source;
|
|
18
|
+
}
|
|
19
|
+
async function observe(ctx, sessionId, signal) {
|
|
20
|
+
const query = sessionQueryOf(ctx);
|
|
21
|
+
let observation;
|
|
22
|
+
try {
|
|
23
|
+
observation = await query.observeSession(sessionId, {
|
|
24
|
+
projectionMode: 'none',
|
|
25
|
+
...(signal ? { signal } : {}),
|
|
26
|
+
});
|
|
27
|
+
const events = Array.isArray(observation.events)
|
|
28
|
+
? observation.events.map(asLogEvent).filter((event) => event != null)
|
|
29
|
+
: [];
|
|
30
|
+
events.sort((left, right) => left.seq - right.seq);
|
|
31
|
+
const throughSeq = typeof observation.cursor === 'number' ? observation.cursor : lastSeq(events);
|
|
32
|
+
return { events, throughSeq };
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (isMissingSession(error))
|
|
36
|
+
return { events: [], throughSeq: -1 };
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
release(observation);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function sessionQueryOf(ctx) {
|
|
44
|
+
const found = (ctx.sessionQuery ?? ctx.get?.('sessionQuery'));
|
|
45
|
+
if (found?.observeSession == null)
|
|
46
|
+
throw new Error('sessionQuery is not available');
|
|
47
|
+
return found;
|
|
48
|
+
}
|
|
49
|
+
function asLogEvent(value) {
|
|
50
|
+
if (!value || typeof value !== 'object')
|
|
51
|
+
return null;
|
|
52
|
+
const record = value;
|
|
53
|
+
if (typeof record.type !== 'string' || typeof record.seq !== 'number' || !Number.isFinite(record.seq))
|
|
54
|
+
return null;
|
|
55
|
+
const time = typeof record.time === 'number' && Number.isFinite(record.time) ? record.time : 0;
|
|
56
|
+
const event = {
|
|
57
|
+
type: record.type,
|
|
58
|
+
seq: record.seq,
|
|
59
|
+
time,
|
|
60
|
+
data: record.data,
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
...event,
|
|
64
|
+
...(record.surfaceOp !== undefined ? { surfaceOp: record.surfaceOp } : {}),
|
|
65
|
+
...(record.sourceEventSeqs !== undefined ? { sourceEventSeqs: record.sourceEventSeqs } : {}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function lastSeq(events) {
|
|
69
|
+
return events.length === 0 ? -1 : events[events.length - 1].seq;
|
|
70
|
+
}
|
|
71
|
+
function isMissingSession(error) {
|
|
72
|
+
const name = error instanceof Error ? error.name : '';
|
|
73
|
+
const code = error && typeof error === 'object' && 'code' in error ? String(error.code) : '';
|
|
74
|
+
return (name === 'SessionPersistenceNotFoundError' ||
|
|
75
|
+
name === 'SessionNotFoundError' ||
|
|
76
|
+
code.includes('NOT_FOUND') ||
|
|
77
|
+
code.includes('MISSING_SESSION'));
|
|
78
|
+
}
|
|
79
|
+
function release(observation) {
|
|
80
|
+
try {
|
|
81
|
+
observation?.[Symbol.dispose]?.();
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
/* already released */
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function sessionIdOf(session) {
|
|
88
|
+
if (!session || typeof session !== 'object')
|
|
89
|
+
return '';
|
|
90
|
+
const record = session;
|
|
91
|
+
const id = record.id ?? record.sessionId ?? record.header?.id ?? record.header?.sessionId;
|
|
92
|
+
return typeof id === 'string' ? id : '';
|
|
93
|
+
}
|
|
94
|
+
async function* tailEvents(ctx, source, sessionId, afterSeq, signal) {
|
|
95
|
+
const pending = [];
|
|
96
|
+
let notify = null;
|
|
97
|
+
const onEvent = (session, event) => {
|
|
98
|
+
if (sessionIdOf(session) !== sessionId)
|
|
99
|
+
return;
|
|
100
|
+
const row = asLogEvent(event);
|
|
101
|
+
if (!row)
|
|
102
|
+
return;
|
|
103
|
+
pending.push(row);
|
|
104
|
+
notify?.();
|
|
105
|
+
};
|
|
106
|
+
const off = ctx.on?.('session/event', onEvent, { global: true });
|
|
107
|
+
const wait = () => new Promise(resolve => {
|
|
108
|
+
if (signal.aborted) {
|
|
109
|
+
resolve();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
notify = resolve;
|
|
113
|
+
const onAbort = () => {
|
|
114
|
+
signal.removeEventListener('abort', onAbort);
|
|
115
|
+
resolve();
|
|
116
|
+
};
|
|
117
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
118
|
+
});
|
|
119
|
+
try {
|
|
120
|
+
const { events, throughSeq } = await source.read(sessionId, afterSeq);
|
|
121
|
+
let cursor = throughSeq;
|
|
122
|
+
for (const event of events) {
|
|
123
|
+
if (signal.aborted)
|
|
124
|
+
return;
|
|
125
|
+
yield event;
|
|
126
|
+
}
|
|
127
|
+
const replay = pending.filter(event => event.seq > cursor).sort((left, right) => left.seq - right.seq);
|
|
128
|
+
pending.length = 0;
|
|
129
|
+
pending.push(...replay);
|
|
130
|
+
while (!signal.aborted) {
|
|
131
|
+
const index = pending.findIndex(event => event.seq === cursor + 1);
|
|
132
|
+
if (index >= 0) {
|
|
133
|
+
const event = pending.splice(index, 1)[0];
|
|
134
|
+
if (!event)
|
|
135
|
+
continue;
|
|
136
|
+
cursor = event.seq;
|
|
137
|
+
yield event;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
await wait();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
if (typeof off === 'function')
|
|
145
|
+
off();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export function logHasMessage(events, messageId) {
|
|
149
|
+
return events.some(event => (event.type === 'agent/inbox/spliced' && insertedIds(event).includes(messageId)) ||
|
|
150
|
+
(event.type === 'user/message' && messageIdOf(event) === messageId));
|
|
151
|
+
}
|
|
152
|
+
export function spliceSeq(events, messageId) {
|
|
153
|
+
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
154
|
+
const event = events[index];
|
|
155
|
+
if (event && event.type === 'agent/inbox/spliced' && insertedIds(event).includes(messageId))
|
|
156
|
+
return event.seq;
|
|
157
|
+
}
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
function insertedIds(event) {
|
|
161
|
+
const inserted = event.data && typeof event.data === 'object'
|
|
162
|
+
? event.data.inserted
|
|
163
|
+
: undefined;
|
|
164
|
+
if (!Array.isArray(inserted))
|
|
165
|
+
return [];
|
|
166
|
+
return inserted.map(message => message?.id).filter((id) => typeof id === 'string');
|
|
167
|
+
}
|
|
168
|
+
function messageIdOf(event) {
|
|
169
|
+
if (!event.data || typeof event.data !== 'object')
|
|
170
|
+
return '';
|
|
171
|
+
const data = event.data;
|
|
172
|
+
return data.id ?? data.message?.id ?? '';
|
|
173
|
+
}
|