@ekairos/thread 1.22.16-beta.feature-core-thread-registry-sync.0 → 1.22.18-beta.feature-core-thread-registry-sync.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 +7 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +2 -2
- package/dist/react.d.ts +2 -18
- package/dist/react.js +2 -15
- package/dist/reactors/ai-sdk.chunk-map.d.ts +12 -0
- package/dist/reactors/ai-sdk.chunk-map.js +143 -0
- package/dist/reactors/ai-sdk.reactor.js +5 -1
- package/dist/reactors/scripted.reactor.d.ts +2 -2
- package/dist/reactors/scripted.reactor.js +3 -2
- package/dist/reactors/types.d.ts +5 -5
- package/dist/schema.js +10 -9
- package/dist/steps/do-thread-stream-step.d.ts +6 -2
- package/dist/steps/do-thread-stream-step.js +9 -5
- package/dist/steps/reaction.steps.js +32 -13
- package/dist/steps/store.steps.d.ts +10 -11
- package/dist/steps/store.steps.js +34 -77
- package/dist/steps/stream.steps.d.ts +3 -33
- package/dist/steps/stream.steps.js +7 -68
- package/dist/steps/trace.steps.js +1 -1
- package/dist/stores/instant.documents.d.ts +1 -1
- package/dist/stores/instant.documents.js +1 -1
- package/dist/stores/instant.store.d.ts +7 -3
- package/dist/stores/instant.store.js +32 -86
- package/dist/thread.contract.d.ts +16 -8
- package/dist/thread.contract.js +61 -19
- package/dist/thread.d.ts +1 -1
- package/dist/thread.engine.d.ts +13 -9
- package/dist/thread.engine.js +463 -75
- package/dist/thread.events.d.ts +3 -3
- package/dist/thread.events.js +11 -34
- package/dist/thread.reactor.d.ts +1 -1
- package/dist/thread.store.d.ts +9 -9
- package/dist/thread.stream.d.ts +100 -33
- package/dist/thread.stream.js +72 -63
- package/package.json +3 -2
|
@@ -31,15 +31,13 @@ function parseRunIdFromTriggerId(triggerEventId) {
|
|
|
31
31
|
return parsed || undefined;
|
|
32
32
|
}
|
|
33
33
|
async function resolveWorkflowRunId(params) {
|
|
34
|
-
const
|
|
35
|
-
let runId =
|
|
36
|
-
?
|
|
34
|
+
const envRunId = params.env?.workflowRunId;
|
|
35
|
+
let runId = typeof envRunId === "string" && envRunId.trim()
|
|
36
|
+
? envRunId.trim()
|
|
37
37
|
: "";
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
runId = envRunId.trim();
|
|
42
|
-
}
|
|
38
|
+
const meta = runId ? null : await readWorkflowMetadata();
|
|
39
|
+
if (!runId && meta && meta.workflowRunId !== undefined && meta.workflowRunId !== null) {
|
|
40
|
+
runId = String(meta.workflowRunId);
|
|
43
41
|
}
|
|
44
42
|
if (!runId && params.triggerEventId) {
|
|
45
43
|
const parsed = parseRunIdFromTriggerId(params.triggerEventId);
|
|
@@ -65,13 +63,11 @@ async function resolveWorkflowRunId(params) {
|
|
|
65
63
|
return { runId: runId || undefined, meta };
|
|
66
64
|
}
|
|
67
65
|
function inferDirection(item) {
|
|
68
|
-
const type = typeof item?.type === "string" ? item.type : "";
|
|
69
|
-
if (type
|
|
70
|
-
return "outbound";
|
|
71
|
-
}
|
|
72
|
-
if (type.startsWith("input") || type.startsWith("user")) {
|
|
66
|
+
const type = typeof item?.type === "string" ? String(item.type) : "";
|
|
67
|
+
if (type === "input")
|
|
73
68
|
return "inbound";
|
|
74
|
-
|
|
69
|
+
if (type === "output")
|
|
70
|
+
return "outbound";
|
|
75
71
|
return undefined;
|
|
76
72
|
}
|
|
77
73
|
function shouldDebugThreadStoreSteps() {
|
|
@@ -107,13 +103,13 @@ function logStepDebug(message, payload) {
|
|
|
107
103
|
console.error(`[thread][store.steps] ${message}`, payload);
|
|
108
104
|
}
|
|
109
105
|
/**
|
|
110
|
-
* Initializes/ensures the story context exists
|
|
106
|
+
* Initializes/ensures the story context exists.
|
|
111
107
|
*
|
|
112
108
|
* This is the "context init" boundary for the story engine.
|
|
113
109
|
*/
|
|
114
110
|
export async function initializeContext(env, contextIdentifier, opts) {
|
|
115
111
|
"use step";
|
|
116
|
-
const { getThreadRuntime } = await import("
|
|
112
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
117
113
|
const runtime = await getThreadRuntime(env);
|
|
118
114
|
const { store, db } = runtime;
|
|
119
115
|
// Detect creation explicitly so the engine can run onContextCreated hooks.
|
|
@@ -132,21 +128,6 @@ export async function initializeContext(env, contextIdentifier, opts) {
|
|
|
132
128
|
result = { context: created, isNew: true };
|
|
133
129
|
}
|
|
134
130
|
}
|
|
135
|
-
// If we're running in a non-streaming context (e.g. tests or headless usage),
|
|
136
|
-
// we skip writing stream chunks entirely.
|
|
137
|
-
if (!opts?.silent && opts?.writable) {
|
|
138
|
-
const writer = opts.writable.getWriter();
|
|
139
|
-
try {
|
|
140
|
-
await writer.write({
|
|
141
|
-
type: "data-context-id",
|
|
142
|
-
id: String(result.context.id),
|
|
143
|
-
data: { contextId: String(result.context.id) },
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
finally {
|
|
147
|
-
writer.releaseLock();
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
131
|
const { runId } = await resolveWorkflowRunId({ env, db });
|
|
151
132
|
if (runId) {
|
|
152
133
|
await maybeWriteTraceEvents(env, [
|
|
@@ -168,42 +149,26 @@ export async function initializeContext(env, contextIdentifier, opts) {
|
|
|
168
149
|
}
|
|
169
150
|
export async function updateContextContent(env, contextIdentifier, content) {
|
|
170
151
|
"use step";
|
|
171
|
-
const { getThreadRuntime } = await import("
|
|
152
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
172
153
|
const { store } = await getThreadRuntime(env);
|
|
173
154
|
return await store.updateContextContent(contextIdentifier, content);
|
|
174
155
|
}
|
|
175
156
|
export async function updateContextStatus(env, contextIdentifier, status) {
|
|
176
157
|
"use step";
|
|
177
|
-
const { getThreadRuntime } = await import("
|
|
158
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
178
159
|
const { store } = await getThreadRuntime(env);
|
|
179
160
|
await store.updateContextStatus(contextIdentifier, status);
|
|
180
161
|
}
|
|
181
162
|
export async function saveTriggerItem(env, contextIdentifier, event) {
|
|
182
163
|
"use step";
|
|
183
|
-
const { getThreadRuntime } = await import("
|
|
164
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
184
165
|
const { store } = await getThreadRuntime(env);
|
|
185
166
|
const saved = await store.saveItem(contextIdentifier, event);
|
|
186
167
|
return saved;
|
|
187
168
|
}
|
|
188
|
-
export async function emitContextIdChunk(params) {
|
|
189
|
-
"use step";
|
|
190
|
-
if (!params.writable)
|
|
191
|
-
return;
|
|
192
|
-
const writer = params.writable.getWriter();
|
|
193
|
-
try {
|
|
194
|
-
await writer.write({
|
|
195
|
-
type: "data-context-id",
|
|
196
|
-
id: String(params.contextId),
|
|
197
|
-
data: { contextId: String(params.contextId) },
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
finally {
|
|
201
|
-
writer.releaseLock();
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
169
|
export async function saveTriggerAndCreateExecution(params) {
|
|
205
170
|
"use step";
|
|
206
|
-
const { getThreadRuntime } = await import("
|
|
171
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
207
172
|
const runtime = await getThreadRuntime(params.env);
|
|
208
173
|
const { store, db } = runtime;
|
|
209
174
|
logStepDebug("saveTriggerAndCreateExecution:start", {
|
|
@@ -229,18 +194,6 @@ export async function saveTriggerAndCreateExecution(params) {
|
|
|
229
194
|
const reactionEventId = typeof uuid === "string"
|
|
230
195
|
? uuid
|
|
231
196
|
: `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
232
|
-
try {
|
|
233
|
-
await store.updateContextStatus(params.contextIdentifier, "streaming");
|
|
234
|
-
}
|
|
235
|
-
catch (error) {
|
|
236
|
-
logStepDebug("saveTriggerAndCreateExecution:updateContextStatus:error", {
|
|
237
|
-
contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
|
|
238
|
-
triggerEventId: saved.id,
|
|
239
|
-
reactionEventId,
|
|
240
|
-
error: summarizeStepError(error),
|
|
241
|
-
});
|
|
242
|
-
throw error;
|
|
243
|
-
}
|
|
244
197
|
let execution;
|
|
245
198
|
try {
|
|
246
199
|
execution = await store.createExecution(params.contextIdentifier, saved.id, reactionEventId);
|
|
@@ -342,7 +295,7 @@ export async function saveTriggerAndCreateExecution(params) {
|
|
|
342
295
|
}
|
|
343
296
|
export async function saveReactionItem(env, contextIdentifier, event, opts) {
|
|
344
297
|
"use step";
|
|
345
|
-
const { getThreadRuntime } = await import("
|
|
298
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
346
299
|
const runtime = await getThreadRuntime(env);
|
|
347
300
|
const { store, db } = runtime;
|
|
348
301
|
const saved = await store.saveItem(contextIdentifier, event);
|
|
@@ -409,7 +362,7 @@ export async function saveReactionItem(env, contextIdentifier, event, opts) {
|
|
|
409
362
|
}
|
|
410
363
|
export async function updateItem(env, eventId, event, opts) {
|
|
411
364
|
"use step";
|
|
412
|
-
const { getThreadRuntime } = await import("
|
|
365
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
413
366
|
const runtime = await getThreadRuntime(env);
|
|
414
367
|
const { store, db } = runtime;
|
|
415
368
|
const saved = await store.updateItem(eventId, event);
|
|
@@ -439,26 +392,25 @@ export async function updateItem(env, eventId, event, opts) {
|
|
|
439
392
|
}
|
|
440
393
|
export async function createExecution(env, contextIdentifier, triggerEventId, reactionEventId) {
|
|
441
394
|
"use step";
|
|
442
|
-
const { getThreadRuntime } = await import("
|
|
395
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
443
396
|
const { store } = await getThreadRuntime(env);
|
|
444
397
|
return await store.createExecution(contextIdentifier, triggerEventId, reactionEventId);
|
|
445
398
|
}
|
|
446
399
|
export async function createReactionItem(params) {
|
|
447
400
|
"use step";
|
|
448
|
-
const { getThreadRuntime } = await import("
|
|
401
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
449
402
|
const { store } = await getThreadRuntime(params.env);
|
|
450
403
|
// Generate a new reaction event id inside the step boundary.
|
|
451
404
|
const uuid = globalThis.crypto?.randomUUID?.();
|
|
452
405
|
const reactionEventId = typeof uuid === "string"
|
|
453
406
|
? uuid
|
|
454
407
|
: `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
455
|
-
await store.updateContextStatus(params.contextIdentifier, "streaming");
|
|
456
408
|
const execution = await store.createExecution(params.contextIdentifier, params.triggerEventId, reactionEventId);
|
|
457
409
|
return { reactionEventId, executionId: execution.id };
|
|
458
410
|
}
|
|
459
411
|
export async function completeExecution(env, contextIdentifier, executionId, status) {
|
|
460
412
|
"use step";
|
|
461
|
-
const { getThreadRuntime } = await import("
|
|
413
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
462
414
|
const runtime = await getThreadRuntime(env);
|
|
463
415
|
const { store, db } = runtime;
|
|
464
416
|
await store.completeExecution(contextIdentifier, executionId, status);
|
|
@@ -488,7 +440,7 @@ export async function completeExecution(env, contextIdentifier, executionId, sta
|
|
|
488
440
|
}
|
|
489
441
|
export async function updateExecutionWorkflowRun(params) {
|
|
490
442
|
"use step";
|
|
491
|
-
const { getThreadRuntime } = await import("
|
|
443
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
492
444
|
const runtime = await getThreadRuntime(params.env);
|
|
493
445
|
const db = runtime?.db;
|
|
494
446
|
if (db) {
|
|
@@ -502,17 +454,17 @@ export async function updateExecutionWorkflowRun(params) {
|
|
|
502
454
|
}
|
|
503
455
|
export async function createThreadStep(params) {
|
|
504
456
|
"use step";
|
|
505
|
-
const { getThreadRuntime } = await import("
|
|
457
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
506
458
|
const { store } = await getThreadRuntime(params.env);
|
|
507
459
|
const res = await store.createStep({
|
|
508
460
|
executionId: params.executionId,
|
|
509
461
|
iteration: params.iteration,
|
|
510
462
|
});
|
|
511
|
-
return { stepId: res.id
|
|
463
|
+
return { stepId: res.id };
|
|
512
464
|
}
|
|
513
465
|
export async function updateThreadStep(params) {
|
|
514
466
|
"use step";
|
|
515
|
-
const { getThreadRuntime } = await import("
|
|
467
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
516
468
|
const runtime = await getThreadRuntime(params.env);
|
|
517
469
|
const { store, db } = runtime;
|
|
518
470
|
await store.updateStep(params.stepId, {
|
|
@@ -536,9 +488,14 @@ export async function updateThreadStep(params) {
|
|
|
536
488
|
stepId: String(params.stepId),
|
|
537
489
|
payload: {
|
|
538
490
|
status: params.patch.status,
|
|
491
|
+
kind: params.patch.kind,
|
|
492
|
+
actionName: params.patch.actionName,
|
|
493
|
+
actionInput: params.patch.actionInput,
|
|
494
|
+
actionOutput: params.patch.actionOutput,
|
|
495
|
+
actionError: params.patch.actionError,
|
|
539
496
|
iteration: params.iteration,
|
|
540
|
-
|
|
541
|
-
|
|
497
|
+
actionRequests: params.patch.actionRequests,
|
|
498
|
+
actionResults: params.patch.actionResults,
|
|
542
499
|
continueLoop: params.patch.continueLoop,
|
|
543
500
|
errorText: params.patch.errorText,
|
|
544
501
|
},
|
|
@@ -548,13 +505,13 @@ export async function updateThreadStep(params) {
|
|
|
548
505
|
}
|
|
549
506
|
export async function linkItemToExecutionStep(params) {
|
|
550
507
|
"use step";
|
|
551
|
-
const { getThreadRuntime } = await import("
|
|
508
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
552
509
|
const { store } = await getThreadRuntime(params.env);
|
|
553
510
|
await store.linkItemToExecution({ itemId: params.itemId, executionId: params.executionId });
|
|
554
511
|
}
|
|
555
512
|
export async function saveThreadPartsStep(params) {
|
|
556
513
|
"use step";
|
|
557
|
-
const { getThreadRuntime } = await import("
|
|
514
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
558
515
|
const runtime = await getThreadRuntime(params.env);
|
|
559
516
|
const { store, db } = runtime;
|
|
560
517
|
await store.saveStepParts({ stepId: params.stepId, parts: params.parts });
|
|
@@ -1,37 +1,7 @@
|
|
|
1
1
|
import type { UIMessageChunk } from "ai";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* - Provide a string key like "actions" to set it
|
|
7
|
-
* - Provide null to clear it
|
|
8
|
-
*/
|
|
9
|
-
key: string | null;
|
|
10
|
-
transient?: boolean;
|
|
11
|
-
writable?: WritableStream<UIMessageChunk>;
|
|
12
|
-
}): Promise<void>;
|
|
13
|
-
export declare function writeContextIdChunk(params: {
|
|
14
|
-
contextId: string;
|
|
15
|
-
writable?: WritableStream<UIMessageChunk>;
|
|
16
|
-
}): Promise<void>;
|
|
17
|
-
export declare function writeThreadPing(params: {
|
|
18
|
-
/**
|
|
19
|
-
* Simple ping event to validate that the workflow stream is alive.
|
|
20
|
-
* This is intentionally generic so clients can ignore it safely.
|
|
21
|
-
*/
|
|
22
|
-
label?: string;
|
|
23
|
-
writable?: WritableStream<UIMessageChunk>;
|
|
24
|
-
}): Promise<void>;
|
|
25
|
-
export declare function writeToolOutputs(params: {
|
|
26
|
-
results: Array<{
|
|
27
|
-
toolCallId: string;
|
|
28
|
-
success: true;
|
|
29
|
-
output: unknown;
|
|
30
|
-
} | {
|
|
31
|
-
toolCallId: string;
|
|
32
|
-
success: false;
|
|
33
|
-
errorText: string;
|
|
34
|
-
}>;
|
|
2
|
+
import type { ThreadStreamEvent } from "../thread.stream.js";
|
|
3
|
+
export declare function writeThreadEvents(params: {
|
|
4
|
+
events: ThreadStreamEvent[];
|
|
35
5
|
writable?: WritableStream<UIMessageChunk>;
|
|
36
6
|
}): Promise<void>;
|
|
37
7
|
export declare function closeThreadStream(params: {
|
|
@@ -1,76 +1,15 @@
|
|
|
1
|
-
export async function
|
|
1
|
+
export async function writeThreadEvents(params) {
|
|
2
2
|
"use step";
|
|
3
3
|
const writable = params.writable;
|
|
4
|
-
if (!writable)
|
|
5
|
-
return;
|
|
6
|
-
const writer = writable.getWriter();
|
|
7
|
-
try {
|
|
8
|
-
await writer.write({
|
|
9
|
-
type: "data-context-substate",
|
|
10
|
-
data: { key: params.key },
|
|
11
|
-
transient: params.transient ?? true,
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
finally {
|
|
15
|
-
writer.releaseLock();
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
export async function writeContextIdChunk(params) {
|
|
19
|
-
"use step";
|
|
20
|
-
const writable = params.writable;
|
|
21
|
-
if (!writable)
|
|
22
|
-
return;
|
|
23
|
-
const writer = writable.getWriter();
|
|
24
|
-
try {
|
|
25
|
-
await writer.write({
|
|
26
|
-
type: "data-context-id",
|
|
27
|
-
id: params.contextId,
|
|
28
|
-
data: { contextId: params.contextId },
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
finally {
|
|
32
|
-
writer.releaseLock();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
export async function writeThreadPing(params) {
|
|
36
|
-
"use step";
|
|
37
|
-
const writable = params.writable;
|
|
38
|
-
if (!writable)
|
|
39
|
-
return;
|
|
40
|
-
const writer = writable.getWriter();
|
|
41
|
-
try {
|
|
42
|
-
await writer.write({
|
|
43
|
-
type: "data-thread-ping",
|
|
44
|
-
data: { label: params.label ?? "thread-ping" },
|
|
45
|
-
transient: true,
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
finally {
|
|
49
|
-
writer.releaseLock();
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
export async function writeToolOutputs(params) {
|
|
53
|
-
"use step";
|
|
54
|
-
const writable = params.writable;
|
|
55
|
-
if (!writable)
|
|
4
|
+
if (!writable || !params.events.length)
|
|
56
5
|
return;
|
|
57
6
|
const writer = writable.getWriter();
|
|
58
7
|
try {
|
|
59
|
-
for (const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
output: r.output,
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
await writer.write({
|
|
69
|
-
type: "tool-output-error",
|
|
70
|
-
toolCallId: r.toolCallId,
|
|
71
|
-
errorText: r.errorText,
|
|
72
|
-
});
|
|
73
|
-
}
|
|
8
|
+
for (const event of params.events) {
|
|
9
|
+
await writer.write({
|
|
10
|
+
type: `data-${String(event.type)}`,
|
|
11
|
+
data: event,
|
|
12
|
+
});
|
|
74
13
|
}
|
|
75
14
|
}
|
|
76
15
|
finally {
|
|
@@ -84,7 +84,7 @@ export async function writeThreadTraceEvents(params) {
|
|
|
84
84
|
const strict = envTrace?.strict === true || process.env.EKAIROS_TRACES_STRICT === "1";
|
|
85
85
|
// 1) Local trace persistence (InstantDB source of truth).
|
|
86
86
|
try {
|
|
87
|
-
const { getThreadRuntime } = await import("
|
|
87
|
+
const { getThreadRuntime } = await import("../runtime.js");
|
|
88
88
|
const runtime = await getThreadRuntime(params.env);
|
|
89
89
|
const db = runtime?.db;
|
|
90
90
|
if (db) {
|
|
@@ -10,7 +10,7 @@ export declare function expandEventsWithInstantDocuments(params: {
|
|
|
10
10
|
*/
|
|
11
11
|
maxChars?: number;
|
|
12
12
|
/**
|
|
13
|
-
* Event type used for derived document text. Defaults to "
|
|
13
|
+
* Event type used for derived document text. Defaults to "output".
|
|
14
14
|
*/
|
|
15
15
|
derivedEventType?: ThreadItem["type"];
|
|
16
16
|
}): Promise<ThreadItem[]>;
|
|
@@ -72,7 +72,7 @@ async function ensureDocumentParsedForFile(db, params) {
|
|
|
72
72
|
export async function expandEventsWithInstantDocuments(params) {
|
|
73
73
|
const db = params.db;
|
|
74
74
|
const maxChars = typeof params.maxChars === "number" ? params.maxChars : 120000;
|
|
75
|
-
const derivedEventType = params.derivedEventType ?? "
|
|
75
|
+
const derivedEventType = params.derivedEventType ?? "output";
|
|
76
76
|
const out = [];
|
|
77
77
|
for (const event of params.events) {
|
|
78
78
|
const parts = event?.content?.parts;
|
|
@@ -44,12 +44,16 @@ export declare class InstantStore implements ThreadStore {
|
|
|
44
44
|
iteration: number;
|
|
45
45
|
}): Promise<{
|
|
46
46
|
id: string;
|
|
47
|
-
eventId: string;
|
|
48
47
|
}>;
|
|
49
48
|
updateStep(stepId: string, patch: Partial<{
|
|
50
49
|
status: "running" | "completed" | "failed";
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
kind: "message" | "action_execute" | "action_result";
|
|
51
|
+
actionName: string;
|
|
52
|
+
actionInput: unknown;
|
|
53
|
+
actionOutput: unknown;
|
|
54
|
+
actionError: string;
|
|
55
|
+
actionRequests: any;
|
|
56
|
+
actionResults: any;
|
|
53
57
|
continueLoop: boolean;
|
|
54
58
|
errorText: string;
|
|
55
59
|
updatedAt: Date;
|
|
@@ -121,7 +121,7 @@ export class InstantStore {
|
|
|
121
121
|
id: String(row?.id ?? ""),
|
|
122
122
|
key: typeof row?.key === "string" ? row.key : null,
|
|
123
123
|
name: typeof row?.name === "string" ? row.name : null,
|
|
124
|
-
status: (typeof row?.status === "string" ? row.status : "
|
|
124
|
+
status: (typeof row?.status === "string" ? row.status : "idle"),
|
|
125
125
|
createdAt: row?.createdAt instanceof Date ? row.createdAt : new Date(row?.createdAt ?? Date.now()),
|
|
126
126
|
updatedAt: row?.updatedAt instanceof Date ? row.updatedAt : row?.updatedAt ? new Date(row.updatedAt) : undefined,
|
|
127
127
|
};
|
|
@@ -253,7 +253,7 @@ export class InstantStore {
|
|
|
253
253
|
this.db.tx.thread_threads[threadId].create({
|
|
254
254
|
createdAt: new Date(),
|
|
255
255
|
updatedAt: new Date(),
|
|
256
|
-
status: "
|
|
256
|
+
status: "idle",
|
|
257
257
|
key,
|
|
258
258
|
}),
|
|
259
259
|
]);
|
|
@@ -424,7 +424,7 @@ export class InstantStore {
|
|
|
424
424
|
];
|
|
425
425
|
if (context.threadId) {
|
|
426
426
|
const thread = await this.getThread({ id: context.threadId });
|
|
427
|
-
const nextThreadStatus =
|
|
427
|
+
const nextThreadStatus = "idle";
|
|
428
428
|
if (thread && thread.status !== nextThreadStatus) {
|
|
429
429
|
assertThreadTransition(thread.status, nextThreadStatus);
|
|
430
430
|
}
|
|
@@ -484,10 +484,10 @@ export class InstantStore {
|
|
|
484
484
|
});
|
|
485
485
|
throw error;
|
|
486
486
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
487
|
+
return {
|
|
488
|
+
...event,
|
|
489
|
+
status: "stored",
|
|
490
|
+
};
|
|
491
491
|
}
|
|
492
492
|
async updateItem(eventId, event) {
|
|
493
493
|
const current = await this.getItem(eventId);
|
|
@@ -495,10 +495,11 @@ export class InstantStore {
|
|
|
495
495
|
assertItemTransition(current.status, event.status);
|
|
496
496
|
}
|
|
497
497
|
await this.db.transact([this.db.tx.thread_items[eventId].update(event)]);
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
498
|
+
return {
|
|
499
|
+
...current,
|
|
500
|
+
...event,
|
|
501
|
+
id: eventId,
|
|
502
|
+
};
|
|
502
503
|
}
|
|
503
504
|
async getItem(eventId) {
|
|
504
505
|
const res = await this.db.query({
|
|
@@ -559,6 +560,13 @@ export class InstantStore {
|
|
|
559
560
|
status: "executing",
|
|
560
561
|
});
|
|
561
562
|
const txs = [execCreate];
|
|
563
|
+
if (context.status !== "open") {
|
|
564
|
+
assertContextTransition(context.status, "open");
|
|
565
|
+
txs.push(this.db.tx.thread_contexts[context.id].update({
|
|
566
|
+
status: "open",
|
|
567
|
+
updatedAt: new Date(),
|
|
568
|
+
}));
|
|
569
|
+
}
|
|
562
570
|
txs.push(this.db.tx.thread_executions[executionId].link({ context: context.id }));
|
|
563
571
|
txs.push(this.db.tx.thread_executions[executionId].link({ thread: thread.id }));
|
|
564
572
|
txs.push(this.db.tx.thread_contexts[context.id].link({ currentExecution: executionId }));
|
|
@@ -603,17 +611,19 @@ export class InstantStore {
|
|
|
603
611
|
if (currentExecutionStatus !== status) {
|
|
604
612
|
assertExecutionTransition(currentExecutionStatus, status);
|
|
605
613
|
}
|
|
606
|
-
if (context.status !== "
|
|
607
|
-
assertContextTransition(context.status, "
|
|
614
|
+
if (context.status !== "closed") {
|
|
615
|
+
assertContextTransition(context.status, "closed");
|
|
608
616
|
}
|
|
609
|
-
const nextThreadStatus =
|
|
617
|
+
const nextThreadStatus = "idle";
|
|
610
618
|
if (thread.status !== nextThreadStatus) {
|
|
611
619
|
assertThreadTransition(thread.status, nextThreadStatus);
|
|
612
620
|
}
|
|
613
621
|
const txs = [];
|
|
614
622
|
txs.push(this.db.tx.thread_executions[executionId].update({ status, updatedAt: new Date() }));
|
|
615
|
-
|
|
616
|
-
|
|
623
|
+
txs.push(this.db.tx.thread_contexts[context.id].update({
|
|
624
|
+
status: "closed",
|
|
625
|
+
updatedAt: new Date(),
|
|
626
|
+
}));
|
|
617
627
|
txs.push(this.db.tx.thread_threads[thread.id].update({
|
|
618
628
|
status: nextThreadStatus,
|
|
619
629
|
updatedAt: new Date(),
|
|
@@ -622,14 +632,11 @@ export class InstantStore {
|
|
|
622
632
|
}
|
|
623
633
|
async createStep(params) {
|
|
624
634
|
const stepId = id();
|
|
625
|
-
const eventId = id();
|
|
626
635
|
const txs = [
|
|
627
636
|
this.db.tx.thread_steps[stepId].create({
|
|
628
637
|
createdAt: new Date(),
|
|
629
638
|
status: "running",
|
|
630
639
|
iteration: params.iteration,
|
|
631
|
-
executionId: params.executionId,
|
|
632
|
-
eventId,
|
|
633
640
|
}),
|
|
634
641
|
];
|
|
635
642
|
txs.push(this.db.tx.thread_steps[stepId].link({ execution: params.executionId }));
|
|
@@ -643,14 +650,13 @@ export class InstantStore {
|
|
|
643
650
|
executionId: params.executionId,
|
|
644
651
|
iteration: params.iteration,
|
|
645
652
|
stepId,
|
|
646
|
-
eventId,
|
|
647
653
|
},
|
|
648
654
|
txs,
|
|
649
655
|
error,
|
|
650
656
|
});
|
|
651
657
|
throw error;
|
|
652
658
|
}
|
|
653
|
-
return { id: stepId
|
|
659
|
+
return { id: stepId };
|
|
654
660
|
}
|
|
655
661
|
async updateStep(stepId, patch) {
|
|
656
662
|
if (patch.status) {
|
|
@@ -697,76 +703,16 @@ export class InstantStore {
|
|
|
697
703
|
await this.db.transact(txs);
|
|
698
704
|
}
|
|
699
705
|
async itemsToModelMessages(events) {
|
|
700
|
-
//
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
if (eventIds.length) {
|
|
704
|
-
try {
|
|
705
|
-
// 1) Get steps for these events (eventId is stored on thread_steps)
|
|
706
|
-
const stepsRes = await this.db.query({
|
|
707
|
-
thread_steps: {
|
|
708
|
-
$: {
|
|
709
|
-
where: { eventId: { $in: eventIds } },
|
|
710
|
-
fields: ["id", "eventId"],
|
|
711
|
-
limit: 2000,
|
|
712
|
-
},
|
|
713
|
-
},
|
|
714
|
-
});
|
|
715
|
-
const steps = stepsRes.thread_steps ?? [];
|
|
716
|
-
const stepIdByEventId = new Map();
|
|
717
|
-
const stepIds = [];
|
|
718
|
-
for (const s of steps) {
|
|
719
|
-
const sid = String(s.id);
|
|
720
|
-
const eid = String(s.eventId);
|
|
721
|
-
if (sid && eid) {
|
|
722
|
-
stepIdByEventId.set(eid, sid);
|
|
723
|
-
stepIds.push(sid);
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
// 2) Load parts for those steps
|
|
727
|
-
const partsByStepId = new Map();
|
|
728
|
-
if (stepIds.length) {
|
|
729
|
-
const partsRes = await this.db.query({
|
|
730
|
-
thread_parts: {
|
|
731
|
-
$: {
|
|
732
|
-
where: { stepId: { $in: stepIds } },
|
|
733
|
-
order: { idx: "asc" },
|
|
734
|
-
limit: 5000,
|
|
735
|
-
},
|
|
736
|
-
},
|
|
737
|
-
});
|
|
738
|
-
const rows = partsRes.thread_parts ?? [];
|
|
739
|
-
for (const r of rows) {
|
|
740
|
-
const sid = String(r.stepId);
|
|
741
|
-
const arr = partsByStepId.get(sid) ?? [];
|
|
742
|
-
arr.push(r.part);
|
|
743
|
-
partsByStepId.set(sid, arr);
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
// 3) Attach parts onto events
|
|
747
|
-
eventsWithParts = events.map((e) => {
|
|
748
|
-
const eid = String(e.id);
|
|
749
|
-
const sid = stepIdByEventId.get(eid);
|
|
750
|
-
if (!sid)
|
|
751
|
-
return e;
|
|
752
|
-
const parts = partsByStepId.get(sid);
|
|
753
|
-
if (!parts || parts.length === 0)
|
|
754
|
-
return e;
|
|
755
|
-
return { ...e, content: { ...(e?.content ?? {}), parts } };
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
catch {
|
|
759
|
-
// If schema not pushed yet (or table absent), fallback to embedded parts.
|
|
760
|
-
eventsWithParts = events;
|
|
761
|
-
}
|
|
762
|
-
}
|
|
706
|
+
// `thread_steps` only links to execution.
|
|
707
|
+
// Item reconstruction uses embedded `item.content.parts` as source for model messages.
|
|
708
|
+
const eventsWithParts = events;
|
|
763
709
|
// Default behavior for Instant-backed stories:
|
|
764
|
-
// - Expand file parts into derived `
|
|
710
|
+
// - Expand file parts into derived `message` items (persisting parsed content into document_documents)
|
|
765
711
|
// - Then convert expanded events to model messages
|
|
766
712
|
const expanded = await expandEventsWithInstantDocuments({
|
|
767
713
|
db: this.db,
|
|
768
714
|
events: eventsWithParts,
|
|
769
|
-
derivedEventType: "
|
|
715
|
+
derivedEventType: "output",
|
|
770
716
|
});
|
|
771
717
|
return await convertItemsToModelMessages(expanded);
|
|
772
718
|
}
|