@tangle-network/agent-app 0.62.4 → 0.63.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 +1 -1
- package/dist/agent-profiles/d1.js +1 -1
- package/dist/agent-profiles/index.d.ts +4 -1
- package/dist/agent-profiles/index.js +1 -1
- package/dist/app-auth/index.js +2 -2
- package/dist/assistant/index.js +7 -7
- package/dist/chat-react/index.js +3 -3
- package/dist/chat-routes/cancellation.d.ts +61 -0
- package/dist/chat-routes/dispatch-first.d.ts +14 -25
- package/dist/chat-routes/early-turn-work.d.ts +10 -0
- package/dist/chat-routes/hybrid-producer.d.ts +4 -3
- package/dist/chat-routes/index.d.ts +2 -1
- package/dist/chat-routes/index.js +86 -1874
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/chat-routes/native-turn-timing.d.ts +15 -0
- package/dist/chat-routes/producer-stream.d.ts +5 -0
- package/dist/chat-routes/route-producer.d.ts +3 -0
- package/dist/chat-routes/route-storage.d.ts +7 -0
- package/dist/chat-routes/router-producer.d.ts +31 -5
- package/dist/chat-routes/turn-admission.d.ts +2 -1
- package/dist/chat-routes/turn-execution.d.ts +6 -11
- package/dist/chat-routes/turn-ownership.d.ts +22 -0
- package/dist/chat-routes/turn-route-contracts.d.ts +112 -105
- package/dist/chat-routes/turn-routes.d.ts +4 -4
- package/dist/chat-routes/wire.d.ts +0 -7
- package/dist/chat-store/index.d.ts +1 -1
- package/dist/chat-store/index.js +21 -3
- package/dist/chat-store/index.js.map +1 -1
- package/dist/chat-store/schema.d.ts +3 -2
- package/dist/chat-store/store.d.ts +8 -0
- package/dist/{chunk-GIEUZBQQ.js → chunk-4CALWPN5.js} +3 -7
- package/dist/chunk-4CALWPN5.js.map +1 -0
- package/dist/chunk-4DBHC63F.js +54 -0
- package/dist/chunk-4DBHC63F.js.map +1 -0
- package/dist/{chunk-G7M2C72Q.js → chunk-5TSRFF5W.js} +1 -1
- package/dist/chunk-5TSRFF5W.js.map +1 -0
- package/dist/chunk-A2NQMVMF.js +485 -0
- package/dist/chunk-A2NQMVMF.js.map +1 -0
- package/dist/{chunk-PCAY23GS.js → chunk-AVBNS64P.js} +172 -2
- package/dist/chunk-AVBNS64P.js.map +1 -0
- package/dist/{chunk-4P5NFBWX.js → chunk-BI5AZAOO.js} +4 -4
- package/dist/chunk-BV3VZEKB.js +483 -0
- package/dist/chunk-BV3VZEKB.js.map +1 -0
- package/dist/{chunk-UW2DKTEM.js → chunk-IDG2XR5O.js} +20 -10
- package/dist/chunk-IDG2XR5O.js.map +1 -0
- package/dist/{chunk-KS4I7KVC.js → chunk-JRTWTADT.js} +184 -123
- package/dist/chunk-JRTWTADT.js.map +1 -0
- package/dist/chunk-KG2Y44XT.js +1 -0
- package/dist/chunk-KG2Y44XT.js.map +1 -0
- package/dist/chunk-MM3GOLHO.js +3365 -0
- package/dist/chunk-MM3GOLHO.js.map +1 -0
- package/dist/chunk-N6URQ6DM.js +225 -0
- package/dist/chunk-N6URQ6DM.js.map +1 -0
- package/dist/{chunk-ISRPOPMM.js → chunk-ND5XWLRU.js} +21 -18
- package/dist/chunk-ND5XWLRU.js.map +1 -0
- package/dist/{chunk-IUVDR3KJ.js → chunk-NHVSO47Q.js} +2 -2
- package/dist/{chunk-FAXSYC2C.js → chunk-RZLCBWGG.js} +2 -2
- package/dist/{chunk-MRZP65DZ.js → chunk-SRWQCWLR.js} +14 -3
- package/dist/chunk-SRWQCWLR.js.map +1 -0
- package/dist/{chunk-JRW6LCIA.js → chunk-V6YGLAQC.js} +2 -2
- package/dist/{chunk-54YIL7WM.js → chunk-WPPDNS63.js} +4 -4
- package/dist/{chunk-CQZVJVME.js → chunk-YCMVNXAX.js} +2 -2
- package/dist/design-canvas/index.js +3 -3
- package/dist/openui/index.js +3 -3
- package/dist/operator/index.js +2 -2
- package/dist/platform/billable-turn-admission.d.ts +13 -9
- package/dist/platform/delegated-keys.d.ts +4 -3
- package/dist/platform/delegated-turn-keys.d.ts +17 -6
- package/dist/platform/index.js +80 -23
- package/dist/platform/index.js.map +1 -1
- package/dist/preset-cloudflare/index.d.ts +2 -0
- package/dist/preset-cloudflare/index.js +344 -0
- package/dist/preset-cloudflare/index.js.map +1 -1
- package/dist/preset-cloudflare/native-completion-owner.d.ts +90 -0
- package/dist/preset-cloudflare/native-completion-store.d.ts +56 -0
- package/dist/preset-cloudflare/native-completion-workflow.d.ts +10 -0
- package/dist/public-consultation/index.js +28 -7
- package/dist/public-consultation/index.js.map +1 -1
- package/dist/rollout/d1.d.ts +103 -3
- package/dist/rollout/d1.js +152 -5
- package/dist/rollout/d1.js.map +1 -1
- package/dist/runtime/index.js +3 -4
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/router-turn.d.ts +4 -0
- package/dist/runtime/router-turn.js +2 -3
- package/dist/sandbox/index.js +3 -4
- package/dist/sandbox/sandbox-run-tool.d.ts +5 -0
- package/dist/sequences/index.js +5 -5
- package/dist/signoff/cli.js +6 -1
- package/dist/signoff/cli.js.map +1 -1
- package/dist/signoff/config.d.ts +3 -1
- package/dist/signoff/index.d.ts +2 -1
- package/dist/signoff/index.js +3 -1
- package/dist/signoff/phases.d.ts +3 -0
- package/dist/signoff/proof.js +3 -0
- package/dist/signoff/proof.js.map +1 -1
- package/dist/signoff/run.d.ts +3 -1
- package/dist/signoff/types.d.ts +13 -0
- package/dist/skills-placement/box.js +5 -5
- package/dist/skills-placement/index.js +2 -2
- package/dist/tools/index.js +1 -1
- package/dist/turn-stream/adapters.d.ts +10 -4
- package/dist/turn-stream/index.js +33 -356
- package/dist/turn-stream/index.js.map +1 -1
- package/dist/turn-trace/index.d.ts +1 -0
- package/dist/turn-trace/index.js +4 -5
- package/dist/turn-trace/phase-timing.d.ts +20 -0
- package/dist/turn-trace/recorder.d.ts +3 -0
- package/dist/turn-trace/turn-facts.d.ts +17 -0
- package/dist/web-react/agent-profile-editor.js +1 -1
- package/dist/web-react/chat-stream.d.ts +0 -7
- package/dist/web-react/index.js +7 -9
- package/dist/work-product/index.js +7 -7
- package/package.json +1 -1
- package/dist/chat-routes/minted-thread.d.ts +0 -63
- package/dist/chunk-6R73V3Y5.js +0 -17
- package/dist/chunk-6R73V3Y5.js.map +0 -1
- package/dist/chunk-CCVWR33L.js +0 -99
- package/dist/chunk-CCVWR33L.js.map +0 -1
- package/dist/chunk-EFQUAWWF.js +0 -1749
- package/dist/chunk-EFQUAWWF.js.map +0 -1
- package/dist/chunk-G7M2C72Q.js.map +0 -1
- package/dist/chunk-GIEUZBQQ.js.map +0 -1
- package/dist/chunk-ISRPOPMM.js.map +0 -1
- package/dist/chunk-KS4I7KVC.js.map +0 -1
- package/dist/chunk-MRZP65DZ.js.map +0 -1
- package/dist/chunk-PCAY23GS.js.map +0 -1
- package/dist/chunk-UW2DKTEM.js.map +0 -1
- package/dist/chunk-VK3F4N4U.js +0 -133
- package/dist/chunk-VK3F4N4U.js.map +0 -1
- /package/dist/{chunk-4P5NFBWX.js.map → chunk-BI5AZAOO.js.map} +0 -0
- /package/dist/{chunk-IUVDR3KJ.js.map → chunk-NHVSO47Q.js.map} +0 -0
- /package/dist/{chunk-FAXSYC2C.js.map → chunk-RZLCBWGG.js.map} +0 -0
- /package/dist/{chunk-JRW6LCIA.js.map → chunk-V6YGLAQC.js.map} +0 -0
- /package/dist/{chunk-54YIL7WM.js.map → chunk-WPPDNS63.js.map} +0 -0
- /package/dist/{chunk-CQZVJVME.js.map → chunk-YCMVNXAX.js.map} +0 -0
|
@@ -32,6 +32,8 @@ import type { KvLike } from '../web/index';
|
|
|
32
32
|
export { ensureCloudflareWorkflowInstance, type CloudflareWorkflowBindingLike, type CloudflareWorkflowInstanceLike, type EnsureCloudflareWorkflowInstanceResult, } from './workflow-instance';
|
|
33
33
|
export { runDetachedTurnWorkflowTick, SANDBOX_TRANSIENT_TICK_POLICY, type DetachedTurnTransientPolicy, type CloudflareWorkflowEventLike, type CloudflareWorkflowSleepDuration, type CloudflareWorkflowStepLike, type DetachedTurnDriveOutcome, type DetachedTurnDriveState, type DetachedTurnTerminalResult, type DetachedTurnWorkflowIdentity, type DetachedTurnWorkflowTickOptions, } from './detached-turn-workflow';
|
|
34
34
|
export { runNativeCompletionWorkflow, type NativeCompletionWorkflowOptions, type NativeCompletionWorkflowPayload, } from './native-completion-workflow';
|
|
35
|
+
export * from './native-completion-store';
|
|
36
|
+
export * from './native-completion-owner';
|
|
35
37
|
export { renderCloudflareHeadersFile, type CloudflareHeadersRule, } from './headers';
|
|
36
38
|
/** A prepared, bound D1 statement. */
|
|
37
39
|
export interface D1PreparedLike {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import "../chunk-KG2Y44XT.js";
|
|
1
2
|
import {
|
|
2
3
|
createFieldCrypto
|
|
3
4
|
} from "../chunk-AWIQQEFP.js";
|
|
@@ -5,6 +6,26 @@ import {
|
|
|
5
6
|
createWorkspaceKeyManager
|
|
6
7
|
} from "../chunk-6JEDSKFH.js";
|
|
7
8
|
import "../chunk-VGS2NXLB.js";
|
|
9
|
+
import {
|
|
10
|
+
observeNativeCompletion
|
|
11
|
+
} from "../chunk-BV3VZEKB.js";
|
|
12
|
+
import {
|
|
13
|
+
assistantRowIdForTurn,
|
|
14
|
+
createAssistantDraftWriter
|
|
15
|
+
} from "../chunk-N6URQ6DM.js";
|
|
16
|
+
import "../chunk-Z4RD5UF7.js";
|
|
17
|
+
import "../chunk-YG66IHDU.js";
|
|
18
|
+
import {
|
|
19
|
+
releaseDurableTurnLock
|
|
20
|
+
} from "../chunk-A2NQMVMF.js";
|
|
21
|
+
import {
|
|
22
|
+
createD1TurnEventStore
|
|
23
|
+
} from "../chunk-ISP6ZH3G.js";
|
|
24
|
+
import "../chunk-RZLCBWGG.js";
|
|
25
|
+
import "../chunk-ZVEEWGDK.js";
|
|
26
|
+
import "../chunk-5TSRFF5W.js";
|
|
27
|
+
import "../chunk-VUBJZEZB.js";
|
|
28
|
+
import "../chunk-6VQCFJWP.js";
|
|
8
29
|
import {
|
|
9
30
|
SANDBOX_TRANSIENT_DEADLINE_MS,
|
|
10
31
|
SandboxTransientDeadlineError,
|
|
@@ -14,8 +35,14 @@ import {
|
|
|
14
35
|
import {
|
|
15
36
|
parseRenderUiSchema
|
|
16
37
|
} from "../chunk-XEAZWJ3W.js";
|
|
38
|
+
import "../chunk-S5SRJJQG.js";
|
|
17
39
|
import "../chunk-WSQFNMIA.js";
|
|
40
|
+
import {
|
|
41
|
+
createTurnTrace
|
|
42
|
+
} from "../chunk-AVBNS64P.js";
|
|
18
43
|
import "../chunk-HY57QRRW.js";
|
|
44
|
+
import "../chunk-TXD5HXLE.js";
|
|
45
|
+
import "../chunk-WL7PZK7Q.js";
|
|
19
46
|
|
|
20
47
|
// src/preset-cloudflare/workflow-instance.ts
|
|
21
48
|
async function ensureCloudflareWorkflowInstance(binding, input) {
|
|
@@ -157,13 +184,323 @@ async function runNativeCompletionWorkflow(options) {
|
|
|
157
184
|
receipt = settledReceipt;
|
|
158
185
|
await options.step.do(`${name}:persist-final-transcript`, () => options.persistTranscript(options.event.payload, receipt, messageId));
|
|
159
186
|
}
|
|
187
|
+
if (options.recordTiming) {
|
|
188
|
+
await options.step.do(`${name}:record-timing`, () => options.recordTiming(options.event.payload, receipt, { settledAt: Date.now() }));
|
|
189
|
+
}
|
|
160
190
|
if (options.finalizeBuffer) {
|
|
161
191
|
await options.step.do(`${name}:finalize-buffer`, () => options.finalizeBuffer(options.event.payload, receipt, messageId));
|
|
162
192
|
}
|
|
163
193
|
await options.step.do(`${name}:release-lock`, () => options.releaseLock(options.event.payload));
|
|
194
|
+
await options.afterRelease?.(options.event.payload, receipt);
|
|
164
195
|
return receipt;
|
|
165
196
|
}
|
|
166
197
|
|
|
198
|
+
// src/preset-cloudflare/native-completion-store.ts
|
|
199
|
+
function createD1NativeCompletionStore(db, table) {
|
|
200
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)) throw new Error("Invalid native completion table");
|
|
201
|
+
const read = async (executionId) => {
|
|
202
|
+
const row = await db.prepare(`SELECT * FROM ${table} WHERE execution_id = ?`).bind(executionId).first();
|
|
203
|
+
return row ? {
|
|
204
|
+
payload: JSON.parse(row.payload),
|
|
205
|
+
checkpoint: JSON.parse(row.checkpoint),
|
|
206
|
+
state: row.state,
|
|
207
|
+
ownerLeaseUntil: row.owner_lease_until,
|
|
208
|
+
closedAt: row.closed_at,
|
|
209
|
+
updatedAt: row.updated_at,
|
|
210
|
+
finalizedAt: row.finalized_at,
|
|
211
|
+
vaultReconciledAt: row.vault_reconciled_at
|
|
212
|
+
} : null;
|
|
213
|
+
};
|
|
214
|
+
const admission = (row) => row ? {
|
|
215
|
+
executionId: row.payload.executionId,
|
|
216
|
+
state: row.state,
|
|
217
|
+
admittedTurnIds: [row.payload.nativeExecutionId],
|
|
218
|
+
ownerLeaseUntil: row.ownerLeaseUntil,
|
|
219
|
+
closedAt: row.closedAt,
|
|
220
|
+
updatedAt: row.updatedAt
|
|
221
|
+
} : null;
|
|
222
|
+
const admissionStore = {
|
|
223
|
+
read: async (id) => admission(await read(id)),
|
|
224
|
+
async renew(id, now) {
|
|
225
|
+
await db.prepare(`UPDATE ${table} SET owner_lease_until = ?, updated_at = ?
|
|
226
|
+
WHERE execution_id = ? AND state = 'open'`).bind(now.getTime() + 12e4, now.getTime(), id).run();
|
|
227
|
+
return admission(await read(id));
|
|
228
|
+
},
|
|
229
|
+
async closeExpired(id, now) {
|
|
230
|
+
await db.prepare(`UPDATE ${table} SET state = 'closed', closed_at = ?, updated_at = ?
|
|
231
|
+
WHERE execution_id = ? AND state = 'open' AND owner_lease_until <= ?`).bind(now.getTime(), now.getTime(), id, now.getTime()).run();
|
|
232
|
+
return admission(await read(id));
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
return {
|
|
236
|
+
read,
|
|
237
|
+
admissionStore,
|
|
238
|
+
async projectionEnded(executionId) {
|
|
239
|
+
await db.prepare(`UPDATE ${table} SET owner_lease_until = ?
|
|
240
|
+
WHERE execution_id = ? AND state = 'open'`).bind(Date.now(), executionId).run();
|
|
241
|
+
},
|
|
242
|
+
async register(payload, checkpoint) {
|
|
243
|
+
await db.prepare(`INSERT INTO ${table}
|
|
244
|
+
(execution_id, payload, checkpoint, owner_lease_until, updated_at) VALUES (?, ?, ?, ?, ?)
|
|
245
|
+
ON CONFLICT(execution_id) DO NOTHING`).bind(
|
|
246
|
+
payload.executionId,
|
|
247
|
+
JSON.stringify(payload),
|
|
248
|
+
JSON.stringify(checkpoint),
|
|
249
|
+
payload.registeredAt + 12e4,
|
|
250
|
+
payload.registeredAt
|
|
251
|
+
).run();
|
|
252
|
+
const retained = await read(payload.executionId);
|
|
253
|
+
if (!retained || retained.payload.workspaceId !== payload.workspaceId || retained.payload.threadId !== payload.threadId || retained.payload.userId !== payload.userId || retained.payload.turnStreamId !== payload.turnStreamId || retained.payload.lock.lockId !== payload.lock.lockId || retained.state !== "open") {
|
|
254
|
+
throw new Error("Native completion identity is already owned by another admission");
|
|
255
|
+
}
|
|
256
|
+
const updated = await db.prepare(`UPDATE ${table} SET payload = ?, checkpoint = ?,
|
|
257
|
+
owner_lease_until = ?, updated_at = ? WHERE execution_id = ? AND state = 'open'`).bind(
|
|
258
|
+
JSON.stringify(payload),
|
|
259
|
+
JSON.stringify(checkpoint),
|
|
260
|
+
Date.now() + 12e4,
|
|
261
|
+
Date.now(),
|
|
262
|
+
payload.executionId
|
|
263
|
+
).run();
|
|
264
|
+
if (updated.meta?.changes !== 1) throw new Error("Native completion admission closed before dispatch");
|
|
265
|
+
},
|
|
266
|
+
async checkpoint(executionId, value) {
|
|
267
|
+
await db.prepare(`UPDATE ${table} SET checkpoint = ?, updated_at = ?
|
|
268
|
+
WHERE execution_id = ? AND finalized_at IS NULL`).bind(JSON.stringify(value), Date.now(), executionId).run();
|
|
269
|
+
},
|
|
270
|
+
async vaultReconciled(executionId) {
|
|
271
|
+
await db.prepare(`UPDATE ${table} SET vault_reconciled_at = ?
|
|
272
|
+
WHERE execution_id = ? AND finalized_at IS NULL`).bind(Date.now(), executionId).run();
|
|
273
|
+
},
|
|
274
|
+
async finalized(executionId) {
|
|
275
|
+
await db.prepare(`UPDATE ${table} SET state = 'closed', finalized_at = ?,
|
|
276
|
+
closed_at = coalesce(closed_at, ?), updated_at = ? WHERE execution_id = ? AND finalized_at IS NULL`).bind(Date.now(), Date.now(), Date.now(), executionId).run();
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// src/preset-cloudflare/native-completion-owner.ts
|
|
282
|
+
var NATIVE_CHECKPOINT_MAX_BYTES = 256 * 1024;
|
|
283
|
+
var encoder = new TextEncoder();
|
|
284
|
+
async function nativeCompletionWorkflowId(executionId, prefix) {
|
|
285
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(executionId));
|
|
286
|
+
return `${prefix}-${Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
287
|
+
}
|
|
288
|
+
function createNativeCompletionTraceSink(attached) {
|
|
289
|
+
const owned = /* @__PURE__ */ new Set();
|
|
290
|
+
return {
|
|
291
|
+
own: (rootSpanId) => {
|
|
292
|
+
owned.add(rootSpanId);
|
|
293
|
+
},
|
|
294
|
+
sink: ((snapshot, labels) => owned.has(snapshot.rootSpanId) ? void 0 : attached(snapshot, labels))
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
function nativeCompletionCheckpoint(trace, timing, turnStartedAt) {
|
|
298
|
+
const now = Date.now();
|
|
299
|
+
const summary = trace.summary(now);
|
|
300
|
+
const checkpoint = {
|
|
301
|
+
requestStartedAt: trace.startedAt,
|
|
302
|
+
phaseStartedAt: timing.startedAt,
|
|
303
|
+
turnStartedAt,
|
|
304
|
+
...summary.firstVisibleMs === void 0 ? {} : { firstVisibleAt: trace.startedAt + summary.firstVisibleMs },
|
|
305
|
+
...summary.firstTextMs === void 0 ? {} : { firstTextAt: trace.startedAt + summary.firstTextMs },
|
|
306
|
+
trace: {
|
|
307
|
+
traceId: trace.traceId,
|
|
308
|
+
executionId: trace.executionId,
|
|
309
|
+
rootSpanId: trace.root.id,
|
|
310
|
+
startedAt: trace.startedAt,
|
|
311
|
+
spans: [],
|
|
312
|
+
summary: { ...summary },
|
|
313
|
+
truncatedSpans: 0
|
|
314
|
+
},
|
|
315
|
+
phasesMs: timing.phases(),
|
|
316
|
+
concurrentMs: timing.concurrent(),
|
|
317
|
+
queries: timing.queries()
|
|
318
|
+
};
|
|
319
|
+
const spans = trace.spans(now);
|
|
320
|
+
let bytes = encoder.encode(JSON.stringify(checkpoint)).byteLength + 1024;
|
|
321
|
+
for (const span of spans) {
|
|
322
|
+
const size = encoder.encode(JSON.stringify(span)).byteLength + 1;
|
|
323
|
+
if (bytes + size > NATIVE_CHECKPOINT_MAX_BYTES) break;
|
|
324
|
+
checkpoint.trace.spans.push(span);
|
|
325
|
+
bytes += size;
|
|
326
|
+
}
|
|
327
|
+
const truncated = spans.length - checkpoint.trace.spans.length;
|
|
328
|
+
checkpoint.trace.truncatedSpans = truncated;
|
|
329
|
+
checkpoint.trace.summary.dropped += truncated;
|
|
330
|
+
if (truncated) checkpoint.checkpointTruncated = true;
|
|
331
|
+
if (encoder.encode(JSON.stringify(checkpoint)).byteLength > NATIVE_CHECKPOINT_MAX_BYTES) {
|
|
332
|
+
throw new Error("Native completion checkpoint metadata exceeds its byte budget");
|
|
333
|
+
}
|
|
334
|
+
return checkpoint;
|
|
335
|
+
}
|
|
336
|
+
function nativeCompletionTerminalTrace(checkpoint, endedAt, error) {
|
|
337
|
+
const original = checkpoint.trace;
|
|
338
|
+
const reducer = createTurnTrace({ startedAt: original.startedAt, executionId: original.executionId, now: () => endedAt });
|
|
339
|
+
const parents = /* @__PURE__ */ new Map([[original.rootSpanId, reducer.root]]);
|
|
340
|
+
for (const span of original.spans) {
|
|
341
|
+
parents.set(span.spanId, reducer.record(
|
|
342
|
+
span.name,
|
|
343
|
+
span.kind,
|
|
344
|
+
span.startMs,
|
|
345
|
+
span.endMs,
|
|
346
|
+
span.attrs,
|
|
347
|
+
parents.get(span.parentSpanId) ?? reducer.root
|
|
348
|
+
));
|
|
349
|
+
}
|
|
350
|
+
if (checkpoint.firstVisibleAt !== void 0) reducer.markFirstVisible(checkpoint.firstVisibleAt);
|
|
351
|
+
if (checkpoint.firstTextAt !== void 0) reducer.markFirstText(checkpoint.firstTextAt);
|
|
352
|
+
const reduced = reducer.finish({ endedAt, ...error ? { error } : {} });
|
|
353
|
+
return {
|
|
354
|
+
...reduced,
|
|
355
|
+
summary: {
|
|
356
|
+
...reduced.summary,
|
|
357
|
+
dropped: original.summary.dropped,
|
|
358
|
+
...original.summary.d1Queries === void 0 ? {} : { d1Queries: original.summary.d1Queries }
|
|
359
|
+
},
|
|
360
|
+
traceId: original.traceId,
|
|
361
|
+
rootSpanId: original.rootSpanId,
|
|
362
|
+
executionId: original.executionId,
|
|
363
|
+
spans: original.spans
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
function createNativeCompletionOwner(options) {
|
|
367
|
+
const store = createD1NativeCompletionStore(options.database, options.table);
|
|
368
|
+
const persistTranscript = async (payload, receipt) => {
|
|
369
|
+
if (receipt.completedTurnIds.length && !receipt.completedTurnIds.includes(payload.nativeExecutionId)) {
|
|
370
|
+
throw new Error("Native completion changed attempts before terminal persistence");
|
|
371
|
+
}
|
|
372
|
+
const messageId = assistantRowIdForTurn(payload.executionId);
|
|
373
|
+
await createAssistantDraftWriter({
|
|
374
|
+
store: options.messages,
|
|
375
|
+
threadId: payload.threadId,
|
|
376
|
+
messageId,
|
|
377
|
+
snapshot: () => null
|
|
378
|
+
}).finalize({
|
|
379
|
+
content: receipt.text,
|
|
380
|
+
parts: receipt.parts,
|
|
381
|
+
model: receipt.servedModel ?? options.model(payload),
|
|
382
|
+
...receipt.usage
|
|
383
|
+
});
|
|
384
|
+
return messageId;
|
|
385
|
+
};
|
|
386
|
+
const finalizeBuffer = async (payload, receipt) => {
|
|
387
|
+
await createD1TurnEventStore(options.database).setStatus(
|
|
388
|
+
payload.turnStreamId,
|
|
389
|
+
receipt.state === "completed" ? "complete" : "error",
|
|
390
|
+
payload.threadId
|
|
391
|
+
);
|
|
392
|
+
};
|
|
393
|
+
const releaseLock = async (payload) => {
|
|
394
|
+
await releaseDurableTurnLock(options.lock.namespace, payload.lock, options.lock.auth);
|
|
395
|
+
};
|
|
396
|
+
const observe = (payload, source, readonly = false) => observeNativeCompletion({
|
|
397
|
+
source,
|
|
398
|
+
admissionStore: readonly ? { ...store.admissionStore, renew: (id) => store.admissionStore.read(id) } : store.admissionStore,
|
|
399
|
+
executionId: payload.executionId,
|
|
400
|
+
sessionId: payload.sessionId,
|
|
401
|
+
turnId: payload.nativeExecutionId,
|
|
402
|
+
registeredAt: payload.registeredAt
|
|
403
|
+
});
|
|
404
|
+
return {
|
|
405
|
+
store,
|
|
406
|
+
persistTranscript,
|
|
407
|
+
finalizeBuffer,
|
|
408
|
+
releaseLock,
|
|
409
|
+
observe,
|
|
410
|
+
reconcile: options.reconcile,
|
|
411
|
+
/** Bind the maintained workflow algorithm; domain effects remain product callbacks. */
|
|
412
|
+
workflow(input) {
|
|
413
|
+
const { event, step } = input, name = options.workflow.prefix;
|
|
414
|
+
const required = async () => {
|
|
415
|
+
input.initialize();
|
|
416
|
+
const row = await store.read(event.payload.executionId);
|
|
417
|
+
if (!row) throw new Error("Native completion admission is missing");
|
|
418
|
+
return row;
|
|
419
|
+
};
|
|
420
|
+
let terminalAt;
|
|
421
|
+
return {
|
|
422
|
+
event,
|
|
423
|
+
step,
|
|
424
|
+
observe: async () => {
|
|
425
|
+
const { payload } = await required();
|
|
426
|
+
return input.observe ? input.observe(payload) : observe(payload, await input.source(payload));
|
|
427
|
+
},
|
|
428
|
+
prepare: async (_initial, receipt) => {
|
|
429
|
+
if ((await required()).vaultReconciledAt) return receipt;
|
|
430
|
+
for (let pass = 0; ; pass++) {
|
|
431
|
+
const complete = await step.do(`${name}:vault-reconciliation:${pass}`, async () => {
|
|
432
|
+
const { payload } = await required(), source = await input.source(payload);
|
|
433
|
+
if (!source) throw new Error("Native completion sandbox is unavailable for reconciliation");
|
|
434
|
+
return options.reconcile(payload, source);
|
|
435
|
+
});
|
|
436
|
+
if (complete) break;
|
|
437
|
+
}
|
|
438
|
+
return receipt;
|
|
439
|
+
},
|
|
440
|
+
persistTranscript: async (_initial, receipt) => persistTranscript((await required()).payload, receipt),
|
|
441
|
+
settle: async (_initial, receipt, messageId) => {
|
|
442
|
+
terminalAt = await step.do(`${name}:terminal-clock`, async () => Date.now());
|
|
443
|
+
await input.settle((await required()).payload, receipt, messageId);
|
|
444
|
+
await step.do(`${name}:timing`, async () => {
|
|
445
|
+
const { payload, checkpoint } = await required();
|
|
446
|
+
await input.timing(payload, checkpoint, nativeCompletionTerminalTrace(checkpoint, terminalAt, receipt.error), receipt);
|
|
447
|
+
});
|
|
448
|
+
},
|
|
449
|
+
finalizeBuffer: async (_initial, receipt) => finalizeBuffer((await required()).payload, receipt),
|
|
450
|
+
releaseLock: async () => releaseLock((await required()).payload),
|
|
451
|
+
afterRelease: async (_initial, receipt) => {
|
|
452
|
+
await step.do(`${name}:root-export`, async () => {
|
|
453
|
+
const { payload, checkpoint } = await required();
|
|
454
|
+
await input.exportTrace(payload, nativeCompletionTerminalTrace(checkpoint, terminalAt, receipt.error));
|
|
455
|
+
});
|
|
456
|
+
await step.do(`${name}:finalized`, () => store.finalized(event.payload.executionId));
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
},
|
|
460
|
+
async register(input) {
|
|
461
|
+
const snapshot = () => nativeCompletionCheckpoint(input.trace, input.timing, input.turnStartedAt);
|
|
462
|
+
let retained = snapshot();
|
|
463
|
+
await store.register(input.payload, retained);
|
|
464
|
+
await ensureCloudflareWorkflowInstance(options.workflow.binding, {
|
|
465
|
+
id: await nativeCompletionWorkflowId(input.payload.executionId, options.workflow.prefix),
|
|
466
|
+
params: input.payload
|
|
467
|
+
});
|
|
468
|
+
await input.handoffCompletion();
|
|
469
|
+
return {
|
|
470
|
+
async projectionEnded(answered) {
|
|
471
|
+
const { executionId } = input.payload;
|
|
472
|
+
await store.checkpoint(executionId, snapshot());
|
|
473
|
+
await store.projectionEnded(executionId);
|
|
474
|
+
if (!answered) return;
|
|
475
|
+
try {
|
|
476
|
+
const row = await store.read(executionId);
|
|
477
|
+
if (!row) return;
|
|
478
|
+
const observation = await observe(row.payload, input.source, true);
|
|
479
|
+
if (observation.state !== "completed" || !await options.reconcile(row.payload, input.source)) return;
|
|
480
|
+
await store.vaultReconciled(executionId);
|
|
481
|
+
await persistTranscript(row.payload, observation.receipt);
|
|
482
|
+
await finalizeBuffer(row.payload, observation.receipt);
|
|
483
|
+
await releaseLock(row.payload);
|
|
484
|
+
} catch (error) {
|
|
485
|
+
console.warn("native completion early release skipped", error instanceof Error ? error.message : String(error));
|
|
486
|
+
}
|
|
487
|
+
},
|
|
488
|
+
async capture(eventType) {
|
|
489
|
+
if (retained.firstVisibleAt !== void 0 && retained.firstTextAt !== void 0) return;
|
|
490
|
+
if (retained.firstVisibleAt !== void 0 && eventType && eventType !== "text" && eventType !== "message.part.updated") return;
|
|
491
|
+
const summary = input.trace.summary();
|
|
492
|
+
const visibleAt = summary.firstVisibleMs === void 0 ? void 0 : input.trace.startedAt + summary.firstVisibleMs;
|
|
493
|
+
const textAt = summary.firstTextMs === void 0 ? void 0 : input.trace.startedAt + summary.firstTextMs;
|
|
494
|
+
if (visibleAt === retained.firstVisibleAt && textAt === retained.firstTextAt) return;
|
|
495
|
+
const next = snapshot();
|
|
496
|
+
await store.checkpoint(input.payload.executionId, next);
|
|
497
|
+
retained = next;
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
|
|
167
504
|
// src/preset-cloudflare/headers.ts
|
|
168
505
|
var HEADER_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
169
506
|
function assertSingleLine(value, label) {
|
|
@@ -476,16 +813,23 @@ function createPresetWorkspaceKeyManager(opts) {
|
|
|
476
813
|
});
|
|
477
814
|
}
|
|
478
815
|
export {
|
|
816
|
+
NATIVE_CHECKPOINT_MAX_BYTES,
|
|
479
817
|
PRESET_MIGRATION_SQL,
|
|
480
818
|
PRESET_TABLES,
|
|
481
819
|
SANDBOX_TRANSIENT_TICK_POLICY,
|
|
482
820
|
createD1KnowledgeStateAccessor,
|
|
821
|
+
createD1NativeCompletionStore,
|
|
822
|
+
createNativeCompletionOwner,
|
|
823
|
+
createNativeCompletionTraceSink,
|
|
483
824
|
createPresetDrizzleSchema,
|
|
484
825
|
createPresetFieldCrypto,
|
|
485
826
|
createPresetToolHandlers,
|
|
486
827
|
createPresetWorkspaceKeyManager,
|
|
487
828
|
createPresetWorkspaceKeyStore,
|
|
488
829
|
ensureCloudflareWorkflowInstance,
|
|
830
|
+
nativeCompletionCheckpoint,
|
|
831
|
+
nativeCompletionTerminalTrace,
|
|
832
|
+
nativeCompletionWorkflowId,
|
|
489
833
|
renderCloudflareHeadersFile,
|
|
490
834
|
runDetachedTurnWorkflowTick,
|
|
491
835
|
runNativeCompletionWorkflow
|