@ekairos/events 1.22.4-beta.development.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.
Files changed (84) hide show
  1. package/README.md +115 -0
  2. package/dist/codex.d.ts +95 -0
  3. package/dist/codex.js +91 -0
  4. package/dist/context.builder.d.ts +62 -0
  5. package/dist/context.builder.js +143 -0
  6. package/dist/context.config.d.ts +9 -0
  7. package/dist/context.config.js +30 -0
  8. package/dist/context.contract.d.ts +47 -0
  9. package/dist/context.contract.js +132 -0
  10. package/dist/context.d.ts +4 -0
  11. package/dist/context.durable.d.ts +5 -0
  12. package/dist/context.durable.js +13 -0
  13. package/dist/context.engine.d.ts +216 -0
  14. package/dist/context.engine.js +1098 -0
  15. package/dist/context.events.d.ts +55 -0
  16. package/dist/context.events.js +431 -0
  17. package/dist/context.hooks.d.ts +21 -0
  18. package/dist/context.hooks.js +31 -0
  19. package/dist/context.js +3 -0
  20. package/dist/context.parts.d.ts +241 -0
  21. package/dist/context.parts.js +360 -0
  22. package/dist/context.reactor.d.ts +3 -0
  23. package/dist/context.reactor.js +2 -0
  24. package/dist/context.registry.d.ts +13 -0
  25. package/dist/context.registry.js +30 -0
  26. package/dist/context.skill.d.ts +9 -0
  27. package/dist/context.skill.js +1 -0
  28. package/dist/context.step-stream.d.ts +26 -0
  29. package/dist/context.step-stream.js +59 -0
  30. package/dist/context.store.d.ts +85 -0
  31. package/dist/context.store.js +1 -0
  32. package/dist/context.stream.d.ts +148 -0
  33. package/dist/context.stream.js +141 -0
  34. package/dist/context.toolcalls.d.ts +60 -0
  35. package/dist/context.toolcalls.js +117 -0
  36. package/dist/env.d.ts +3 -0
  37. package/dist/env.js +53 -0
  38. package/dist/index.d.ts +18 -0
  39. package/dist/index.js +11 -0
  40. package/dist/mcp.d.ts +1 -0
  41. package/dist/mcp.js +1 -0
  42. package/dist/mirror.d.ts +41 -0
  43. package/dist/mirror.js +1 -0
  44. package/dist/oidc.d.ts +7 -0
  45. package/dist/oidc.js +25 -0
  46. package/dist/polyfills/dom-events.d.ts +1 -0
  47. package/dist/polyfills/dom-events.js +89 -0
  48. package/dist/react.d.ts +42 -0
  49. package/dist/react.js +88 -0
  50. package/dist/reactors/ai-sdk.chunk-map.d.ts +12 -0
  51. package/dist/reactors/ai-sdk.chunk-map.js +143 -0
  52. package/dist/reactors/ai-sdk.reactor.d.ts +33 -0
  53. package/dist/reactors/ai-sdk.reactor.js +65 -0
  54. package/dist/reactors/ai-sdk.step.d.ts +48 -0
  55. package/dist/reactors/ai-sdk.step.js +343 -0
  56. package/dist/reactors/scripted.reactor.d.ts +17 -0
  57. package/dist/reactors/scripted.reactor.js +51 -0
  58. package/dist/reactors/types.d.ts +52 -0
  59. package/dist/reactors/types.js +1 -0
  60. package/dist/runtime.d.ts +19 -0
  61. package/dist/runtime.js +26 -0
  62. package/dist/runtime.step.d.ts +9 -0
  63. package/dist/runtime.step.js +7 -0
  64. package/dist/schema.d.ts +2 -0
  65. package/dist/schema.js +191 -0
  66. package/dist/steps/do-context-stream-step.d.ts +34 -0
  67. package/dist/steps/do-context-stream-step.js +96 -0
  68. package/dist/steps/mirror.steps.d.ts +6 -0
  69. package/dist/steps/mirror.steps.js +48 -0
  70. package/dist/steps/store.steps.d.ts +96 -0
  71. package/dist/steps/store.steps.js +595 -0
  72. package/dist/steps/stream.steps.d.ts +86 -0
  73. package/dist/steps/stream.steps.js +270 -0
  74. package/dist/steps/trace.steps.d.ts +38 -0
  75. package/dist/steps/trace.steps.js +270 -0
  76. package/dist/stores/instant.document-parser.d.ts +6 -0
  77. package/dist/stores/instant.document-parser.js +210 -0
  78. package/dist/stores/instant.documents.d.ts +16 -0
  79. package/dist/stores/instant.documents.js +152 -0
  80. package/dist/stores/instant.store.d.ts +66 -0
  81. package/dist/stores/instant.store.js +575 -0
  82. package/dist/tools-to-model-tools.d.ts +19 -0
  83. package/dist/tools-to-model-tools.js +21 -0
  84. package/package.json +142 -0
@@ -0,0 +1,595 @@
1
+ import { OUTPUT_ITEM_TYPE, WEB_CHANNEL } from "../context.events.js";
2
+ import { writeContextTraceEvents } from "./trace.steps.js";
3
+ import { getClientResumeHookUrl, toolApprovalHookToken, toolApprovalWebhookToken, } from "../context.hooks.js";
4
+ async function maybeWriteTraceEvents(env, traceEvents) {
5
+ if (!traceEvents?.length)
6
+ return;
7
+ try {
8
+ await writeContextTraceEvents({ env, events: traceEvents });
9
+ }
10
+ catch {
11
+ if (process.env.PLAYWRIGHT_TEST === "1") {
12
+ // eslint-disable-next-line no-console
13
+ console.warn("[context/trace] emit failed");
14
+ }
15
+ }
16
+ }
17
+ async function readWorkflowMetadata() {
18
+ try {
19
+ const { getWorkflowMetadata } = await import("workflow");
20
+ return getWorkflowMetadata?.() ?? null;
21
+ }
22
+ catch {
23
+ return null;
24
+ }
25
+ }
26
+ async function resolveWorkflowRunId(params) {
27
+ let runId = "";
28
+ const meta = await readWorkflowMetadata();
29
+ if (meta && meta.workflowRunId !== undefined && meta.workflowRunId !== null) {
30
+ runId = String(meta.workflowRunId);
31
+ }
32
+ if (!runId && params.executionId && params.db) {
33
+ try {
34
+ const q = await params.db.query({
35
+ event_executions: {
36
+ $: { where: { id: String(params.executionId) }, limit: 1 },
37
+ },
38
+ });
39
+ const row = q?.event_executions?.[0];
40
+ if (row?.workflowRunId) {
41
+ runId = String(row.workflowRunId);
42
+ }
43
+ }
44
+ catch {
45
+ // ignore
46
+ }
47
+ }
48
+ return { runId: runId || undefined, meta };
49
+ }
50
+ function inferDirection(item) {
51
+ const type = typeof item?.type === "string" ? String(item.type) : "";
52
+ if (type === "input")
53
+ return "inbound";
54
+ if (type === "output")
55
+ return "outbound";
56
+ return undefined;
57
+ }
58
+ function shouldDebugContextStoreSteps() {
59
+ return process.env.EKAIROS_CONTEXT_DEBUG === "1";
60
+ }
61
+ function summarizeStepError(error) {
62
+ const err = error;
63
+ return {
64
+ name: err?.name,
65
+ message: err?.message,
66
+ code: err?.code,
67
+ status: err?.status ?? err?.statusCode,
68
+ details: err?.details ?? err?.body ?? err?.response ?? err?.data,
69
+ stack: typeof err?.stack === "string"
70
+ ? err.stack.slice(0, 1500)
71
+ : undefined,
72
+ };
73
+ }
74
+ function summarizeContextIdentifierForLog(identifier) {
75
+ return {
76
+ id: identifier && typeof identifier.id === "string"
77
+ ? String(identifier.id)
78
+ : undefined,
79
+ key: identifier && typeof identifier.key === "string"
80
+ ? String(identifier.key)
81
+ : undefined,
82
+ };
83
+ }
84
+ function logStepDebug(message, payload) {
85
+ if (!shouldDebugContextStoreSteps())
86
+ return;
87
+ // eslint-disable-next-line no-console
88
+ console.error(`[context][store.steps] ${message}`, payload);
89
+ }
90
+ /**
91
+ * Initializes/ensures the story context exists.
92
+ *
93
+ * This is the "context init" boundary for the story engine.
94
+ */
95
+ export async function initializeContext(env, contextIdentifier, opts) {
96
+ "use step";
97
+ const { getContextRuntime } = await import("../runtime.js");
98
+ const runtime = await getContextRuntime(env);
99
+ const { store, db } = runtime;
100
+ // Detect creation explicitly so the engine can run onContextCreated hooks.
101
+ let result;
102
+ if (!contextIdentifier) {
103
+ const context = await store.getOrCreateContext(null);
104
+ result = { context, isNew: true };
105
+ }
106
+ else {
107
+ const existing = await store.getContext(contextIdentifier);
108
+ if (existing) {
109
+ result = { context: existing, isNew: false };
110
+ }
111
+ else {
112
+ const created = await store.getOrCreateContext(contextIdentifier);
113
+ result = { context: created, isNew: true };
114
+ }
115
+ }
116
+ const { runId } = await resolveWorkflowRunId({ env, db });
117
+ if (runId) {
118
+ await maybeWriteTraceEvents(env, [
119
+ {
120
+ workflowRunId: runId,
121
+ eventId: `context_entity:${String(result.context.id)}`,
122
+ eventKind: "context.lifecycle",
123
+ eventAt: new Date().toISOString(),
124
+ contextId: String(result.context.id),
125
+ contextKey: result.context.key ?? undefined,
126
+ payload: {
127
+ ...result.context,
128
+ action: result.isNew ? "created" : "updated",
129
+ },
130
+ },
131
+ ]);
132
+ }
133
+ return result;
134
+ }
135
+ export async function updateContextContent(env, contextIdentifier, content) {
136
+ "use step";
137
+ const { getContextRuntime } = await import("../runtime.js");
138
+ const { store } = await getContextRuntime(env);
139
+ return await store.updateContextContent(contextIdentifier, content);
140
+ }
141
+ export async function updateContextStatus(env, contextIdentifier, status) {
142
+ "use step";
143
+ const { getContextRuntime } = await import("../runtime.js");
144
+ const { store } = await getContextRuntime(env);
145
+ await store.updateContextStatus(contextIdentifier, status);
146
+ }
147
+ export async function saveTriggerItem(env, contextIdentifier, event) {
148
+ "use step";
149
+ const { getContextRuntime } = await import("../runtime.js");
150
+ const { store } = await getContextRuntime(env);
151
+ const saved = await store.saveItem(contextIdentifier, event);
152
+ return saved;
153
+ }
154
+ export async function saveTriggerAndCreateExecution(params) {
155
+ "use step";
156
+ const { getContextRuntime } = await import("../runtime.js");
157
+ const runtime = await getContextRuntime(params.env);
158
+ const { store, db } = runtime;
159
+ logStepDebug("saveTriggerAndCreateExecution:start", {
160
+ contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
161
+ triggerEventId: params.triggerEvent?.id,
162
+ triggerEventType: params.triggerEvent?.type,
163
+ triggerEventChannel: params.triggerEvent?.channel,
164
+ triggerEventCreatedAt: params.triggerEvent?.createdAt,
165
+ });
166
+ let saved;
167
+ try {
168
+ saved = await store.saveItem(params.contextIdentifier, params.triggerEvent);
169
+ }
170
+ catch (error) {
171
+ logStepDebug("saveTriggerAndCreateExecution:saveItem:error", {
172
+ contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
173
+ triggerEventId: params.triggerEvent?.id,
174
+ error: summarizeStepError(error),
175
+ });
176
+ throw error;
177
+ }
178
+ const uuid = globalThis.crypto?.randomUUID?.();
179
+ const reactionEventId = typeof uuid === "string"
180
+ ? uuid
181
+ : `${Date.now()}-${Math.random().toString(16).slice(2)}`;
182
+ const reactionShell = {
183
+ id: reactionEventId,
184
+ type: OUTPUT_ITEM_TYPE,
185
+ channel: typeof params.triggerEvent?.channel === "string"
186
+ ? params.triggerEvent.channel
187
+ : WEB_CHANNEL,
188
+ createdAt: new Date().toISOString(),
189
+ status: "pending",
190
+ content: {
191
+ parts: [],
192
+ },
193
+ };
194
+ let savedReaction;
195
+ try {
196
+ savedReaction = await store.saveItem(params.contextIdentifier, reactionShell);
197
+ savedReaction = await store.updateItem(savedReaction.id, {
198
+ ...savedReaction,
199
+ status: "pending",
200
+ });
201
+ }
202
+ catch (error) {
203
+ logStepDebug("saveTriggerAndCreateExecution:saveReaction:error", {
204
+ contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
205
+ reactionEventId,
206
+ error: summarizeStepError(error),
207
+ });
208
+ throw error;
209
+ }
210
+ let execution;
211
+ try {
212
+ execution = await store.createExecution(params.contextIdentifier, saved.id, savedReaction.id);
213
+ }
214
+ catch (error) {
215
+ logStepDebug("saveTriggerAndCreateExecution:createExecution:error", {
216
+ contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
217
+ triggerEventId: saved.id,
218
+ reactionEventId: savedReaction.id,
219
+ error: summarizeStepError(error),
220
+ });
221
+ throw error;
222
+ }
223
+ try {
224
+ await store.linkItemToExecution({
225
+ itemId: saved.id,
226
+ executionId: execution.id,
227
+ });
228
+ await store.linkItemToExecution({
229
+ itemId: savedReaction.id,
230
+ executionId: execution.id,
231
+ });
232
+ }
233
+ catch (error) {
234
+ logStepDebug("saveTriggerAndCreateExecution:linkItemsToExecution:error", {
235
+ contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
236
+ triggerEventId: saved.id,
237
+ reactionEventId: savedReaction.id,
238
+ executionId: execution.id,
239
+ error: summarizeStepError(error),
240
+ });
241
+ throw error;
242
+ }
243
+ const { runId, meta } = await resolveWorkflowRunId({
244
+ env: params.env,
245
+ db,
246
+ executionId: execution.id,
247
+ });
248
+ if (runId && db) {
249
+ try {
250
+ await db.transact([
251
+ db.tx.event_executions[execution.id].update({
252
+ workflowRunId: runId,
253
+ updatedAt: new Date(),
254
+ }),
255
+ ]);
256
+ }
257
+ catch {
258
+ // ignore
259
+ }
260
+ }
261
+ if (runId) {
262
+ let contextKey;
263
+ let contextId;
264
+ try {
265
+ const ctx = await store.getContext(params.contextIdentifier);
266
+ contextKey = ctx?.key ?? undefined;
267
+ contextId = ctx?.id ? String(ctx.id) : undefined;
268
+ }
269
+ catch {
270
+ // ignore
271
+ }
272
+ const events = [
273
+ {
274
+ workflowRunId: runId,
275
+ eventId: `workflow_run:${String(runId)}`,
276
+ eventKind: "workflow.run",
277
+ eventAt: new Date().toISOString(),
278
+ payload: meta ?? null,
279
+ },
280
+ {
281
+ workflowRunId: runId,
282
+ eventId: `context_run:${String(execution.id)}`,
283
+ eventKind: "context.run",
284
+ eventAt: new Date().toISOString(),
285
+ contextId,
286
+ executionId: String(execution.id),
287
+ payload: {
288
+ contextKey: contextKey ?? null,
289
+ contextId: contextId ?? null,
290
+ triggerEventId: saved.id,
291
+ reactionEventId: savedReaction.id,
292
+ workflowMeta: meta ?? null,
293
+ },
294
+ },
295
+ {
296
+ workflowRunId: runId,
297
+ eventId: `context_execution:${String(execution.id)}`,
298
+ eventKind: "context.execution",
299
+ eventAt: new Date().toISOString(),
300
+ contextId,
301
+ executionId: String(execution.id),
302
+ payload: {
303
+ status: "started",
304
+ },
305
+ },
306
+ {
307
+ workflowRunId: runId,
308
+ eventId: `context_item:${String(saved.id)}`,
309
+ eventKind: "context.item",
310
+ eventAt: new Date().toISOString(),
311
+ contextId,
312
+ contextEventId: String(saved.id),
313
+ payload: {
314
+ ...saved,
315
+ direction: "inbound",
316
+ },
317
+ },
318
+ {
319
+ workflowRunId: runId,
320
+ eventId: `context_item:${String(savedReaction.id)}`,
321
+ eventKind: "context.item",
322
+ eventAt: new Date().toISOString(),
323
+ contextId,
324
+ executionId: String(execution.id),
325
+ contextEventId: String(savedReaction.id),
326
+ payload: {
327
+ ...savedReaction,
328
+ direction: "outbound",
329
+ },
330
+ },
331
+ ];
332
+ await maybeWriteTraceEvents(params.env, events);
333
+ }
334
+ return {
335
+ triggerEvent: saved,
336
+ reactionEvent: savedReaction,
337
+ execution: {
338
+ id: execution.id,
339
+ status: "executing",
340
+ },
341
+ };
342
+ }
343
+ export async function saveReactionItem(env, contextIdentifier, event, opts) {
344
+ "use step";
345
+ const { getContextRuntime } = await import("../runtime.js");
346
+ const runtime = await getContextRuntime(env);
347
+ const { store, db } = runtime;
348
+ const saved = await store.saveItem(contextIdentifier, event);
349
+ if (opts?.executionId) {
350
+ await store.linkItemToExecution({
351
+ itemId: saved.id,
352
+ executionId: opts.executionId,
353
+ });
354
+ }
355
+ const contextId = opts?.contextId ??
356
+ (typeof contextIdentifier?.id === "string"
357
+ ? String(contextIdentifier.id)
358
+ : undefined);
359
+ const { runId, meta } = await resolveWorkflowRunId({
360
+ env,
361
+ db,
362
+ executionId: opts?.executionId,
363
+ });
364
+ if (runId) {
365
+ const events = [
366
+ {
367
+ workflowRunId: runId,
368
+ eventId: `context_item:${String(saved.id)}`,
369
+ eventKind: "context.item",
370
+ eventAt: new Date().toISOString(),
371
+ contextId,
372
+ executionId: opts?.executionId,
373
+ contextEventId: String(saved.id),
374
+ payload: {
375
+ ...saved,
376
+ direction: "outbound",
377
+ },
378
+ },
379
+ ];
380
+ if (opts?.executionId && opts.reviewRequests?.length) {
381
+ const resumeHookUrl = getClientResumeHookUrl();
382
+ const workflowUrl = meta && typeof meta.url === "string" && meta.url.trim()
383
+ ? String(meta.url)
384
+ : undefined;
385
+ for (const rr of opts.reviewRequests) {
386
+ const toolCallId = String(rr.toolCallId);
387
+ events.push({
388
+ workflowRunId: runId,
389
+ eventId: `context_review:${String(opts.executionId)}:${toolCallId}`,
390
+ eventKind: "context.review",
391
+ eventAt: new Date().toISOString(),
392
+ contextId,
393
+ executionId: String(opts.executionId),
394
+ toolCallId,
395
+ payload: {
396
+ status: "in_review",
397
+ toolName: rr.toolName ?? "",
398
+ hookToken: toolApprovalHookToken({
399
+ executionId: String(opts.executionId),
400
+ toolCallId,
401
+ }),
402
+ webhookToken: toolApprovalWebhookToken({
403
+ executionId: String(opts.executionId),
404
+ toolCallId,
405
+ }),
406
+ resumeHookUrl,
407
+ workflowUrl,
408
+ },
409
+ });
410
+ }
411
+ }
412
+ await maybeWriteTraceEvents(env, events);
413
+ }
414
+ return saved;
415
+ }
416
+ export async function updateItem(env, eventId, event, opts) {
417
+ "use step";
418
+ const { getContextRuntime } = await import("../runtime.js");
419
+ const runtime = await getContextRuntime(env);
420
+ const { store, db } = runtime;
421
+ const saved = await store.updateItem(eventId, event);
422
+ const { runId } = await resolveWorkflowRunId({
423
+ env,
424
+ db,
425
+ executionId: opts?.executionId,
426
+ });
427
+ if (runId) {
428
+ await maybeWriteTraceEvents(env, [
429
+ {
430
+ workflowRunId: runId,
431
+ eventId: `context_item:${String(saved.id)}`,
432
+ eventKind: "context.item",
433
+ eventAt: new Date().toISOString(),
434
+ contextId: opts?.contextId,
435
+ executionId: opts?.executionId,
436
+ contextEventId: String(saved.id),
437
+ payload: {
438
+ ...saved,
439
+ direction: inferDirection(saved),
440
+ },
441
+ },
442
+ ]);
443
+ }
444
+ return saved;
445
+ }
446
+ export async function createExecution(env, contextIdentifier, triggerEventId, reactionEventId) {
447
+ "use step";
448
+ const { getContextRuntime } = await import("../runtime.js");
449
+ const { store } = await getContextRuntime(env);
450
+ return await store.createExecution(contextIdentifier, triggerEventId, reactionEventId);
451
+ }
452
+ export async function createReactionItem(params) {
453
+ "use step";
454
+ const { getContextRuntime } = await import("../runtime.js");
455
+ const { store } = await getContextRuntime(params.env);
456
+ // Generate a new reaction event id inside the step boundary.
457
+ const uuid = globalThis.crypto?.randomUUID?.();
458
+ const reactionEventId = typeof uuid === "string"
459
+ ? uuid
460
+ : `${Date.now()}-${Math.random().toString(16).slice(2)}`;
461
+ const execution = await store.createExecution(params.contextIdentifier, params.triggerEventId, reactionEventId);
462
+ return { reactionEventId, executionId: execution.id };
463
+ }
464
+ export async function completeExecution(env, contextIdentifier, executionId, status) {
465
+ "use step";
466
+ const { getContextRuntime } = await import("../runtime.js");
467
+ const runtime = await getContextRuntime(env);
468
+ const { store, db } = runtime;
469
+ await store.completeExecution(contextIdentifier, executionId, status);
470
+ const contextId = typeof contextIdentifier?.id === "string"
471
+ ? String(contextIdentifier.id)
472
+ : undefined;
473
+ const { runId } = await resolveWorkflowRunId({
474
+ env,
475
+ db,
476
+ executionId,
477
+ });
478
+ if (runId) {
479
+ await maybeWriteTraceEvents(env, [
480
+ {
481
+ workflowRunId: runId,
482
+ eventId: `context_execution:${String(executionId)}:${status}`,
483
+ eventKind: "context.execution",
484
+ eventAt: new Date().toISOString(),
485
+ contextId,
486
+ executionId: String(executionId),
487
+ payload: {
488
+ status,
489
+ },
490
+ },
491
+ ]);
492
+ }
493
+ }
494
+ export async function updateExecutionWorkflowRun(params) {
495
+ "use step";
496
+ const { getContextRuntime } = await import("../runtime.js");
497
+ const runtime = await getContextRuntime(params.env);
498
+ const db = runtime?.db;
499
+ if (db) {
500
+ await db.transact([
501
+ db.tx.event_executions[params.executionId].update({
502
+ workflowRunId: params.workflowRunId,
503
+ updatedAt: new Date(),
504
+ }),
505
+ ]);
506
+ }
507
+ }
508
+ export async function createContextStep(params) {
509
+ "use step";
510
+ const { getContextRuntime } = await import("../runtime.js");
511
+ const { store } = await getContextRuntime(params.env);
512
+ const res = await store.createStep({
513
+ executionId: params.executionId,
514
+ iteration: params.iteration,
515
+ });
516
+ return { stepId: res.id };
517
+ }
518
+ export async function updateContextStep(params) {
519
+ "use step";
520
+ const { getContextRuntime } = await import("../runtime.js");
521
+ const runtime = await getContextRuntime(params.env);
522
+ const { store, db } = runtime;
523
+ await store.updateStep(params.stepId, {
524
+ ...params.patch,
525
+ updatedAt: new Date(),
526
+ });
527
+ const { runId } = await resolveWorkflowRunId({
528
+ env: params.env,
529
+ db,
530
+ executionId: params.executionId,
531
+ });
532
+ if (runId) {
533
+ await maybeWriteTraceEvents(params.env, [
534
+ {
535
+ workflowRunId: runId,
536
+ eventId: `context_step:${String(params.stepId)}`,
537
+ eventKind: "context.step",
538
+ eventAt: new Date().toISOString(),
539
+ contextId: params.contextId,
540
+ executionId: params.executionId,
541
+ stepId: String(params.stepId),
542
+ payload: {
543
+ status: params.patch.status,
544
+ kind: params.patch.kind,
545
+ actionName: params.patch.actionName,
546
+ actionInput: params.patch.actionInput,
547
+ actionOutput: params.patch.actionOutput,
548
+ actionError: params.patch.actionError,
549
+ iteration: params.iteration,
550
+ actionRequests: params.patch.actionRequests,
551
+ actionResults: params.patch.actionResults,
552
+ continueLoop: params.patch.continueLoop,
553
+ errorText: params.patch.errorText,
554
+ },
555
+ },
556
+ ]);
557
+ }
558
+ }
559
+ export async function linkItemToExecutionStep(params) {
560
+ "use step";
561
+ const { getContextRuntime } = await import("../runtime.js");
562
+ const { store } = await getContextRuntime(params.env);
563
+ await store.linkItemToExecution({ itemId: params.itemId, executionId: params.executionId });
564
+ }
565
+ export async function saveContextPartsStep(params) {
566
+ "use step";
567
+ const { getContextRuntime } = await import("../runtime.js");
568
+ const runtime = await getContextRuntime(params.env);
569
+ const { store, db } = runtime;
570
+ await store.saveStepParts({ stepId: params.stepId, parts: params.parts });
571
+ const { runId } = await resolveWorkflowRunId({
572
+ env: params.env,
573
+ db,
574
+ executionId: params.executionId,
575
+ });
576
+ if (runId && params.parts?.length) {
577
+ const events = [];
578
+ for (let idx = 0; idx < params.parts.length; idx += 1) {
579
+ const part = params.parts[idx];
580
+ events.push({
581
+ workflowRunId: runId,
582
+ eventId: `context_part:${String(params.stepId)}:${idx}`,
583
+ eventKind: "context.part",
584
+ eventAt: new Date().toISOString(),
585
+ contextId: params.contextId,
586
+ executionId: params.executionId,
587
+ stepId: String(params.stepId),
588
+ partKey: `${String(params.stepId)}:${idx}`,
589
+ partIdx: idx,
590
+ payload: part,
591
+ });
592
+ }
593
+ await maybeWriteTraceEvents(params.env, events);
594
+ }
595
+ }
@@ -0,0 +1,86 @@
1
+ import type { UIMessageChunk } from "ai";
2
+ import type { ContextEnvironment } from "../context.config.js";
3
+ import { type ContextStepStreamChunk } from "../context.step-stream.js";
4
+ import type { ContextStreamEvent } from "../context.stream.js";
5
+ export declare function writeContextEvents(params: {
6
+ events: ContextStreamEvent[];
7
+ writable?: WritableStream<UIMessageChunk>;
8
+ }): Promise<void>;
9
+ export declare function closeContextStream(params: {
10
+ preventClose?: boolean;
11
+ sendFinish?: boolean;
12
+ writable?: WritableStream<UIMessageChunk>;
13
+ }): Promise<void>;
14
+ export declare function createContextStepStreamClientId(stepId: string): string;
15
+ export type PersistedContextStepStreamSession = {
16
+ stream: WritableStream<string>;
17
+ streamId: string;
18
+ clientId: string;
19
+ executionId: string;
20
+ stepId: string;
21
+ };
22
+ export declare function createPersistedContextStepStream(params: {
23
+ env: ContextEnvironment;
24
+ executionId: string;
25
+ stepId: string;
26
+ clientId?: string;
27
+ }): Promise<PersistedContextStepStreamSession>;
28
+ export declare function closePersistedContextStepStream(params: {
29
+ env: ContextEnvironment;
30
+ session: PersistedContextStepStreamSession;
31
+ }): Promise<void>;
32
+ export declare function abortPersistedContextStepStream(params: {
33
+ env: ContextEnvironment;
34
+ session: PersistedContextStepStreamSession;
35
+ reason?: string | null;
36
+ }): Promise<void>;
37
+ export declare function readPersistedContextStepStream(params: {
38
+ db: any;
39
+ clientId?: string;
40
+ streamId?: string;
41
+ byteOffset?: number;
42
+ onChunk?: (chunk: ContextStepStreamChunk) => Promise<void> | void;
43
+ }): Promise<{
44
+ chunks: ContextStepStreamChunk[];
45
+ byteOffset: number;
46
+ }>;
47
+ export declare function resolveContextExecutionStreamPointer(params: {
48
+ db: any;
49
+ contextId: string;
50
+ }): Promise<{
51
+ executionId: string;
52
+ status: string | null;
53
+ source: "active";
54
+ clientId: string | null;
55
+ streamId: string | null;
56
+ } | {
57
+ executionId: string;
58
+ status: string | null;
59
+ source: "last";
60
+ clientId: string | null;
61
+ streamId: string | null;
62
+ } | {
63
+ executionId: string;
64
+ status: string | null;
65
+ source: "none";
66
+ clientId: null;
67
+ streamId: null;
68
+ } | null>;
69
+ export declare function waitForContextExecutionStreamPointer(params: {
70
+ db: any;
71
+ contextId: string;
72
+ timeoutMs?: number;
73
+ pollMs?: number;
74
+ }): Promise<{
75
+ executionId: string;
76
+ status: string | null;
77
+ source: "active";
78
+ clientId: string | null;
79
+ streamId: string | null;
80
+ } | {
81
+ executionId: string;
82
+ status: string | null;
83
+ source: "last";
84
+ clientId: string | null;
85
+ streamId: string | null;
86
+ } | null>;