@ekairos/events 1.22.34-beta.development.0 → 1.22.35

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 (74) hide show
  1. package/README.md +58 -83
  2. package/dist/codex.d.ts +11 -2
  3. package/dist/codex.js +16 -8
  4. package/dist/context.action-calls.d.ts +48 -0
  5. package/dist/context.action-calls.js +123 -0
  6. package/dist/context.action.d.ts +55 -0
  7. package/dist/context.action.js +25 -0
  8. package/dist/context.builder.d.ts +71 -43
  9. package/dist/context.builder.js +123 -28
  10. package/dist/context.config.d.ts +2 -1
  11. package/dist/context.config.js +8 -3
  12. package/dist/context.contract.d.ts +2 -4
  13. package/dist/context.contract.js +3 -9
  14. package/dist/context.d.ts +3 -2
  15. package/dist/context.engine.d.ts +75 -46
  16. package/dist/context.engine.js +538 -302
  17. package/dist/context.events.js +28 -87
  18. package/dist/context.js +1 -0
  19. package/dist/context.part-identity.d.ts +40 -0
  20. package/dist/context.part-identity.js +270 -0
  21. package/dist/context.parts.d.ts +389 -164
  22. package/dist/context.parts.js +343 -218
  23. package/dist/context.registry.d.ts +1 -1
  24. package/dist/context.runtime.d.ts +21 -0
  25. package/dist/context.runtime.js +39 -0
  26. package/dist/context.step-stream.d.ts +16 -2
  27. package/dist/context.step-stream.js +58 -16
  28. package/dist/context.store.d.ts +63 -10
  29. package/dist/context.stream.d.ts +14 -4
  30. package/dist/context.stream.js +31 -3
  31. package/dist/domain.d.ts +1 -0
  32. package/dist/domain.js +1 -0
  33. package/dist/index.d.ts +13 -10
  34. package/dist/index.js +7 -6
  35. package/dist/react.context-event-parts.d.ts +18 -0
  36. package/dist/react.context-event-parts.js +509 -0
  37. package/dist/react.d.ts +7 -42
  38. package/dist/react.js +4 -87
  39. package/dist/react.step-stream.d.ts +39 -0
  40. package/dist/react.step-stream.js +625 -0
  41. package/dist/react.types.d.ts +121 -0
  42. package/dist/react.types.js +2 -0
  43. package/dist/react.use-context.d.ts +7 -0
  44. package/dist/react.use-context.js +867 -0
  45. package/dist/reactors/ai-sdk.chunk-map.d.ts +1 -0
  46. package/dist/reactors/ai-sdk.chunk-map.js +56 -5
  47. package/dist/reactors/ai-sdk.reactor.d.ts +8 -5
  48. package/dist/reactors/ai-sdk.reactor.js +10 -9
  49. package/dist/reactors/ai-sdk.step.d.ts +6 -6
  50. package/dist/reactors/ai-sdk.step.js +32 -24
  51. package/dist/reactors/scripted.reactor.d.ts +7 -4
  52. package/dist/reactors/types.d.ts +23 -8
  53. package/dist/runtime.d.ts +6 -0
  54. package/dist/runtime.js +9 -0
  55. package/dist/runtime.step.js +2 -2
  56. package/dist/schema.d.ts +268 -2
  57. package/dist/schema.js +5 -9
  58. package/dist/steps/do-context-stream-step.d.ts +2 -2
  59. package/dist/steps/do-context-stream-step.js +6 -8
  60. package/dist/steps/durable.steps.d.ts +28 -0
  61. package/dist/steps/durable.steps.js +34 -0
  62. package/dist/steps/store.steps.d.ts +121 -39
  63. package/dist/steps/store.steps.js +266 -111
  64. package/dist/steps/stream.steps.d.ts +36 -3
  65. package/dist/steps/stream.steps.js +137 -14
  66. package/dist/steps/trace.steps.d.ts +4 -2
  67. package/dist/steps/trace.steps.js +26 -8
  68. package/dist/stores/instant.store.d.ts +15 -11
  69. package/dist/stores/instant.store.js +155 -6
  70. package/dist/tools-to-model-tools.d.ts +39 -3
  71. package/dist/tools-to-model-tools.js +63 -6
  72. package/package.json +20 -6
  73. package/dist/context.toolcalls.d.ts +0 -60
  74. package/dist/context.toolcalls.js +0 -117
@@ -1,6 +1,13 @@
1
+ import { getContextRuntimeServices } from "../context.runtime.js";
1
2
  import { OUTPUT_ITEM_TYPE, WEB_CHANNEL } from "../context.events.js";
2
3
  import { writeContextTraceEvents } from "./trace.steps.js";
3
4
  import { getClientResumeHookUrl, toolApprovalHookToken, toolApprovalWebhookToken, } from "../context.hooks.js";
5
+ import { createPersistedContextStepStreamForRuntime, finalizePersistedContextStepStreamForRuntime, writeActionResultPartChunksToSession, } from "./stream.steps.js";
6
+ async function getRuntimeAndEnv(params) {
7
+ const env = params.runtime.env;
8
+ const runtime = await getContextRuntimeServices(params.runtime);
9
+ return { runtime, env };
10
+ }
4
11
  async function maybeWriteTraceEvents(env, traceEvents) {
5
12
  if (!traceEvents?.length)
6
13
  return;
@@ -92,24 +99,23 @@ function logStepDebug(message, payload) {
92
99
  *
93
100
  * This is the "context init" boundary for the story engine.
94
101
  */
95
- export async function initializeContext(env, contextIdentifier, opts) {
102
+ export async function initializeContext(params) {
96
103
  "use step";
97
- const { getContextRuntime } = await import("../runtime.js");
98
- const runtime = await getContextRuntime(env);
104
+ const { runtime, env } = await getRuntimeAndEnv(params);
99
105
  const { store, db } = runtime;
100
106
  // Detect creation explicitly so the engine can run onContextCreated hooks.
101
107
  let result;
102
- if (!contextIdentifier) {
108
+ if (!params.contextIdentifier) {
103
109
  const context = await store.getOrCreateContext(null);
104
110
  result = { context, isNew: true };
105
111
  }
106
112
  else {
107
- const existing = await store.getContext(contextIdentifier);
113
+ const existing = await store.getContext(params.contextIdentifier);
108
114
  if (existing) {
109
115
  result = { context: existing, isNew: false };
110
116
  }
111
117
  else {
112
- const created = await store.getOrCreateContext(contextIdentifier);
118
+ const created = await store.getOrCreateContext(params.contextIdentifier);
113
119
  result = { context: created, isNew: true };
114
120
  }
115
121
  }
@@ -132,31 +138,41 @@ export async function initializeContext(env, contextIdentifier, opts) {
132
138
  }
133
139
  return result;
134
140
  }
135
- export async function updateContextContent(env, contextIdentifier, content) {
141
+ export async function updateContextContent(params) {
136
142
  "use step";
137
- const { getContextRuntime } = await import("../runtime.js");
138
- const { store } = await getContextRuntime(env);
139
- return await store.updateContextContent(contextIdentifier, content);
143
+ const { runtime } = await getRuntimeAndEnv(params);
144
+ return await runtime.store.updateContextContent(params.contextIdentifier, params.content);
140
145
  }
141
- export async function updateContextStatus(env, contextIdentifier, status) {
146
+ export async function updateContextDefinition(params) {
142
147
  "use step";
143
- const { getContextRuntime } = await import("../runtime.js");
144
- const { store } = await getContextRuntime(env);
145
- await store.updateContextStatus(contextIdentifier, status);
148
+ const { runtime } = await getRuntimeAndEnv(params);
149
+ return await runtime.store.updateContextDefinition(params.contextIdentifier, params.definition);
146
150
  }
147
- export async function saveTriggerItem(env, contextIdentifier, event) {
151
+ export async function upsertContextResources(params) {
148
152
  "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
+ const { runtime } = await getRuntimeAndEnv(params);
154
+ return await runtime.store.upsertContextResources(params.contextIdentifier, params.resources);
155
+ }
156
+ export async function updateContextReactor(params) {
157
+ "use step";
158
+ const { runtime } = await getRuntimeAndEnv(params);
159
+ return await runtime.store.updateContextReactor(params.contextIdentifier, params.reactor);
160
+ }
161
+ export async function updateContextStatus(params) {
162
+ "use step";
163
+ const { runtime } = await getRuntimeAndEnv(params);
164
+ await runtime.store.updateContextStatus(params.contextIdentifier, params.status);
165
+ }
166
+ export async function saveTriggerItem(params) {
167
+ "use step";
168
+ const { runtime } = await getRuntimeAndEnv(params);
169
+ return await runtime.store.saveItem(params.contextIdentifier, params.event);
153
170
  }
154
- export async function saveTriggerAndCreateExecution(params) {
171
+ export async function openExecution(params) {
155
172
  "use step";
156
- const { getContextRuntime } = await import("../runtime.js");
157
- const runtime = await getContextRuntime(params.env);
173
+ const { runtime, env } = await getRuntimeAndEnv(params);
158
174
  const { store, db } = runtime;
159
- logStepDebug("saveTriggerAndCreateExecution:start", {
175
+ logStepDebug("openExecution:start", {
160
176
  contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
161
177
  triggerEventId: params.triggerEvent?.id,
162
178
  triggerEventType: params.triggerEvent?.type,
@@ -168,7 +184,7 @@ export async function saveTriggerAndCreateExecution(params) {
168
184
  saved = await store.saveItem(params.contextIdentifier, params.triggerEvent);
169
185
  }
170
186
  catch (error) {
171
- logStepDebug("saveTriggerAndCreateExecution:saveItem:error", {
187
+ logStepDebug("openExecution:saveItem:error", {
172
188
  contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
173
189
  triggerEventId: params.triggerEvent?.id,
174
190
  error: summarizeStepError(error),
@@ -200,7 +216,7 @@ export async function saveTriggerAndCreateExecution(params) {
200
216
  });
201
217
  }
202
218
  catch (error) {
203
- logStepDebug("saveTriggerAndCreateExecution:saveReaction:error", {
219
+ logStepDebug("openExecution:saveReaction:error", {
204
220
  contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
205
221
  reactionEventId,
206
222
  error: summarizeStepError(error),
@@ -212,7 +228,7 @@ export async function saveTriggerAndCreateExecution(params) {
212
228
  execution = await store.createExecution(params.contextIdentifier, saved.id, savedReaction.id);
213
229
  }
214
230
  catch (error) {
215
- logStepDebug("saveTriggerAndCreateExecution:createExecution:error", {
231
+ logStepDebug("openExecution:createExecution:error", {
216
232
  contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
217
233
  triggerEventId: saved.id,
218
234
  reactionEventId: savedReaction.id,
@@ -231,7 +247,7 @@ export async function saveTriggerAndCreateExecution(params) {
231
247
  });
232
248
  }
233
249
  catch (error) {
234
- logStepDebug("saveTriggerAndCreateExecution:linkItemsToExecution:error", {
250
+ logStepDebug("openExecution:linkItemsToExecution:error", {
235
251
  contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
236
252
  triggerEventId: saved.id,
237
253
  reactionEventId: savedReaction.id,
@@ -241,7 +257,7 @@ export async function saveTriggerAndCreateExecution(params) {
241
257
  throw error;
242
258
  }
243
259
  const { runId, meta } = await resolveWorkflowRunId({
244
- env: params.env,
260
+ env,
245
261
  db,
246
262
  executionId: execution.id,
247
263
  });
@@ -329,7 +345,7 @@ export async function saveTriggerAndCreateExecution(params) {
329
345
  },
330
346
  },
331
347
  ];
332
- await maybeWriteTraceEvents(params.env, events);
348
+ await maybeWriteTraceEvents(env, events);
333
349
  }
334
350
  return {
335
351
  triggerEvent: saved,
@@ -340,26 +356,25 @@ export async function saveTriggerAndCreateExecution(params) {
340
356
  },
341
357
  };
342
358
  }
343
- export async function saveReactionItem(env, contextIdentifier, event, opts) {
359
+ export async function saveReactionItem(params) {
344
360
  "use step";
345
- const { getContextRuntime } = await import("../runtime.js");
346
- const runtime = await getContextRuntime(env);
361
+ const { runtime, env } = await getRuntimeAndEnv(params);
347
362
  const { store, db } = runtime;
348
- const saved = await store.saveItem(contextIdentifier, event);
349
- if (opts?.executionId) {
363
+ const saved = await store.saveItem(params.contextIdentifier, params.event);
364
+ if (params.opts?.executionId) {
350
365
  await store.linkItemToExecution({
351
366
  itemId: saved.id,
352
- executionId: opts.executionId,
367
+ executionId: params.opts.executionId,
353
368
  });
354
369
  }
355
- const contextId = opts?.contextId ??
356
- (typeof contextIdentifier?.id === "string"
357
- ? String(contextIdentifier.id)
370
+ const contextId = params.opts?.contextId ??
371
+ (typeof params.contextIdentifier?.id === "string"
372
+ ? String(params.contextIdentifier.id)
358
373
  : undefined);
359
374
  const { runId, meta } = await resolveWorkflowRunId({
360
375
  env,
361
376
  db,
362
- executionId: opts?.executionId,
377
+ executionId: params.opts?.executionId,
363
378
  });
364
379
  if (runId) {
365
380
  const events = [
@@ -369,7 +384,7 @@ export async function saveReactionItem(env, contextIdentifier, event, opts) {
369
384
  eventKind: "context.item",
370
385
  eventAt: new Date().toISOString(),
371
386
  contextId,
372
- executionId: opts?.executionId,
387
+ executionId: params.opts?.executionId,
373
388
  contextEventId: String(saved.id),
374
389
  payload: {
375
390
  ...saved,
@@ -377,30 +392,30 @@ export async function saveReactionItem(env, contextIdentifier, event, opts) {
377
392
  },
378
393
  },
379
394
  ];
380
- if (opts?.executionId && opts.reviewRequests?.length) {
395
+ if (params.opts?.executionId && params.opts.reviewRequests?.length) {
381
396
  const resumeHookUrl = getClientResumeHookUrl();
382
397
  const workflowUrl = meta && typeof meta.url === "string" && meta.url.trim()
383
398
  ? String(meta.url)
384
399
  : undefined;
385
- for (const rr of opts.reviewRequests) {
400
+ for (const rr of params.opts.reviewRequests) {
386
401
  const toolCallId = String(rr.toolCallId);
387
402
  events.push({
388
403
  workflowRunId: runId,
389
- eventId: `context_review:${String(opts.executionId)}:${toolCallId}`,
404
+ eventId: `context_review:${String(params.opts.executionId)}:${toolCallId}`,
390
405
  eventKind: "context.review",
391
406
  eventAt: new Date().toISOString(),
392
407
  contextId,
393
- executionId: String(opts.executionId),
408
+ executionId: String(params.opts.executionId),
394
409
  toolCallId,
395
410
  payload: {
396
411
  status: "in_review",
397
412
  toolName: rr.toolName ?? "",
398
413
  hookToken: toolApprovalHookToken({
399
- executionId: String(opts.executionId),
414
+ executionId: String(params.opts.executionId),
400
415
  toolCallId,
401
416
  }),
402
417
  webhookToken: toolApprovalWebhookToken({
403
- executionId: String(opts.executionId),
418
+ executionId: String(params.opts.executionId),
404
419
  toolCallId,
405
420
  }),
406
421
  resumeHookUrl,
@@ -413,16 +428,20 @@ export async function saveReactionItem(env, contextIdentifier, event, opts) {
413
428
  }
414
429
  return saved;
415
430
  }
416
- export async function updateItem(env, eventId, event, opts) {
431
+ export async function getContextItems(params) {
432
+ "use step";
433
+ const { runtime } = await getRuntimeAndEnv(params);
434
+ return await runtime.store.getItems(params.contextIdentifier);
435
+ }
436
+ export async function updateItem(params) {
417
437
  "use step";
418
- const { getContextRuntime } = await import("../runtime.js");
419
- const runtime = await getContextRuntime(env);
438
+ const { runtime, env } = await getRuntimeAndEnv(params);
420
439
  const { store, db } = runtime;
421
- const saved = await store.updateItem(eventId, event);
440
+ const saved = await store.updateItem(params.eventId, params.event);
422
441
  const { runId } = await resolveWorkflowRunId({
423
442
  env,
424
443
  db,
425
- executionId: opts?.executionId,
444
+ executionId: params.opts?.executionId,
426
445
  });
427
446
  if (runId) {
428
447
  await maybeWriteTraceEvents(env, [
@@ -431,8 +450,8 @@ export async function updateItem(env, eventId, event, opts) {
431
450
  eventId: `context_item:${String(saved.id)}`,
432
451
  eventKind: "context.item",
433
452
  eventAt: new Date().toISOString(),
434
- contextId: opts?.contextId,
435
- executionId: opts?.executionId,
453
+ contextId: params.opts?.contextId,
454
+ executionId: params.opts?.executionId,
436
455
  contextEventId: String(saved.id),
437
456
  payload: {
438
457
  ...saved,
@@ -443,16 +462,15 @@ export async function updateItem(env, eventId, event, opts) {
443
462
  }
444
463
  return saved;
445
464
  }
446
- export async function createExecution(env, contextIdentifier, triggerEventId, reactionEventId) {
465
+ export async function createExecution(params) {
447
466
  "use step";
448
- const { getContextRuntime } = await import("../runtime.js");
449
- const { store } = await getContextRuntime(env);
450
- return await store.createExecution(contextIdentifier, triggerEventId, reactionEventId);
467
+ const { runtime } = await getRuntimeAndEnv(params);
468
+ return await runtime.store.createExecution(params.contextIdentifier, params.triggerEventId, params.reactionEventId);
451
469
  }
452
470
  export async function createReactionItem(params) {
453
471
  "use step";
454
- const { getContextRuntime } = await import("../runtime.js");
455
- const { store } = await getContextRuntime(params.env);
472
+ const { runtime } = await getRuntimeAndEnv(params);
473
+ const { store } = runtime;
456
474
  // Generate a new reaction event id inside the step boundary.
457
475
  const uuid = globalThis.crypto?.randomUUID?.();
458
476
  const reactionEventId = typeof uuid === "string"
@@ -461,41 +479,62 @@ export async function createReactionItem(params) {
461
479
  const execution = await store.createExecution(params.contextIdentifier, params.triggerEventId, reactionEventId);
462
480
  return { reactionEventId, executionId: execution.id };
463
481
  }
464
- export async function completeExecution(env, contextIdentifier, executionId, status) {
482
+ export async function completeExecution(params) {
465
483
  "use step";
466
- const { getContextRuntime } = await import("../runtime.js");
467
- const runtime = await getContextRuntime(env);
484
+ const { runtime, env } = await getRuntimeAndEnv(params);
468
485
  const { store, db } = runtime;
469
- await store.completeExecution(contextIdentifier, executionId, status);
470
- const contextId = typeof contextIdentifier?.id === "string"
471
- ? String(contextIdentifier.id)
472
- : undefined;
486
+ await store.completeExecution(params.contextIdentifier, params.executionId, params.status);
487
+ let savedReaction;
488
+ if (params.reactionEventId && params.reactionEvent) {
489
+ savedReaction = await store.updateItem(params.reactionEventId, params.reactionEvent);
490
+ }
491
+ const contextId = typeof params.contextId === "string"
492
+ ? params.contextId
493
+ : typeof params.contextIdentifier?.id === "string"
494
+ ? String(params.contextIdentifier.id)
495
+ : undefined;
473
496
  const { runId } = await resolveWorkflowRunId({
474
497
  env,
475
498
  db,
476
- executionId,
499
+ executionId: params.executionId,
477
500
  });
478
501
  if (runId) {
479
- await maybeWriteTraceEvents(env, [
502
+ const events = [
480
503
  {
481
504
  workflowRunId: runId,
482
- eventId: `context_execution:${String(executionId)}:${status}`,
505
+ eventId: `context_execution:${String(params.executionId)}:${params.status}`,
483
506
  eventKind: "context.execution",
484
507
  eventAt: new Date().toISOString(),
485
508
  contextId,
486
- executionId: String(executionId),
509
+ executionId: String(params.executionId),
487
510
  payload: {
488
- status,
511
+ status: params.status,
489
512
  },
490
513
  },
491
- ]);
514
+ ];
515
+ if (savedReaction) {
516
+ events.push({
517
+ workflowRunId: runId,
518
+ eventId: `context_item:${String(savedReaction.id)}`,
519
+ eventKind: "context.item",
520
+ eventAt: new Date().toISOString(),
521
+ contextId,
522
+ executionId: String(params.executionId),
523
+ contextEventId: String(savedReaction.id),
524
+ payload: {
525
+ ...savedReaction,
526
+ direction: inferDirection(savedReaction),
527
+ },
528
+ });
529
+ }
530
+ await maybeWriteTraceEvents(env, events);
492
531
  }
532
+ return { reactionEvent: savedReaction };
493
533
  }
494
534
  export async function updateExecutionWorkflowRun(params) {
495
535
  "use step";
496
- const { getContextRuntime } = await import("../runtime.js");
497
- const runtime = await getContextRuntime(params.env);
498
- const db = runtime?.db;
536
+ const { runtime } = await getRuntimeAndEnv(params);
537
+ const db = runtime.db;
499
538
  if (db) {
500
539
  await db.transact([
501
540
  db.tx.event_executions[params.executionId].update({
@@ -505,10 +544,31 @@ export async function updateExecutionWorkflowRun(params) {
505
544
  ]);
506
545
  }
507
546
  }
547
+ export async function openExecutionStep(params) {
548
+ "use step";
549
+ const { runtime } = await getRuntimeAndEnv(params);
550
+ const { store } = runtime;
551
+ const step = await store.createStep({
552
+ executionId: params.executionId,
553
+ iteration: params.iteration,
554
+ });
555
+ const stream = await createPersistedContextStepStreamForRuntime(runtime, {
556
+ executionId: params.executionId,
557
+ stepId: step.id,
558
+ });
559
+ const context = await store.updateContextContent(params.contextIdentifier, params.content);
560
+ const events = await store.getItems(params.contextIdentifier);
561
+ return {
562
+ stepId: step.id,
563
+ stream,
564
+ context,
565
+ events,
566
+ };
567
+ }
508
568
  export async function createContextStep(params) {
509
569
  "use step";
510
- const { getContextRuntime } = await import("../runtime.js");
511
- const { store } = await getContextRuntime(params.env);
570
+ const { runtime } = await getRuntimeAndEnv(params);
571
+ const { store } = runtime;
512
572
  const res = await store.createStep({
513
573
  executionId: params.executionId,
514
574
  iteration: params.iteration,
@@ -517,20 +577,19 @@ export async function createContextStep(params) {
517
577
  }
518
578
  export async function updateContextStep(params) {
519
579
  "use step";
520
- const { getContextRuntime } = await import("../runtime.js");
521
- const runtime = await getContextRuntime(params.env);
580
+ const { runtime, env } = await getRuntimeAndEnv(params);
522
581
  const { store, db } = runtime;
523
582
  await store.updateStep(params.stepId, {
524
583
  ...params.patch,
525
584
  updatedAt: new Date(),
526
585
  });
527
586
  const { runId } = await resolveWorkflowRunId({
528
- env: params.env,
587
+ env,
529
588
  db,
530
589
  executionId: params.executionId,
531
590
  });
532
591
  if (runId) {
533
- await maybeWriteTraceEvents(params.env, [
592
+ await maybeWriteTraceEvents(env, [
534
593
  {
535
594
  workflowRunId: runId,
536
595
  eventId: `context_step:${String(params.stepId)}`,
@@ -541,55 +600,151 @@ export async function updateContextStep(params) {
541
600
  stepId: String(params.stepId),
542
601
  payload: {
543
602
  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
603
  iteration: params.iteration,
550
- actionRequests: params.patch.actionRequests,
551
- actionResults: params.patch.actionResults,
552
- continueLoop: params.patch.continueLoop,
553
604
  errorText: params.patch.errorText,
554
605
  },
555
606
  },
556
607
  ]);
557
608
  }
558
609
  }
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) {
610
+ export async function completeExecutionStep(params) {
566
611
  "use step";
567
- const { getContextRuntime } = await import("../runtime.js");
568
- const runtime = await getContextRuntime(params.env);
612
+ const { runtime, env } = await getRuntimeAndEnv(params);
569
613
  const { store, db } = runtime;
570
- await store.saveStepParts({ stepId: params.stepId, parts: params.parts });
614
+ const actionResultChunkEvents = await writeActionResultPartChunksToSession({
615
+ session: params.session,
616
+ contextId: String(params.contextId ?? ""),
617
+ executionId: String(params.executionId ?? ""),
618
+ itemId: String(params.reactionEventId ?? ""),
619
+ actionResults: params.actionResults ?? [],
620
+ });
621
+ if (params.parts) {
622
+ await store.saveStepParts({ stepId: params.stepId, parts: params.parts });
623
+ }
624
+ if (params.session) {
625
+ await finalizePersistedContextStepStreamForRuntime({
626
+ runtime,
627
+ session: params.session,
628
+ mode: "close",
629
+ });
630
+ }
631
+ await store.updateStep(params.stepId, {
632
+ status: params.stepStatus ?? "completed",
633
+ ...(params.errorText !== undefined ? { errorText: params.errorText } : {}),
634
+ updatedAt: new Date(),
635
+ });
636
+ let savedReaction;
637
+ if (params.reactionEventId && params.reactionEvent) {
638
+ savedReaction = await store.updateItem(params.reactionEventId, params.reactionEvent);
639
+ }
571
640
  const { runId } = await resolveWorkflowRunId({
572
- env: params.env,
641
+ env,
573
642
  db,
574
643
  executionId: params.executionId,
575
644
  });
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({
645
+ if (runId) {
646
+ const events = [
647
+ {
581
648
  workflowRunId: runId,
582
- eventId: `context_part:${String(params.stepId)}:${idx}`,
583
- eventKind: "context.part",
649
+ eventId: `context_step:${String(params.stepId)}`,
650
+ eventKind: "context.step",
584
651
  eventAt: new Date().toISOString(),
585
652
  contextId: params.contextId,
586
653
  executionId: params.executionId,
587
654
  stepId: String(params.stepId),
588
- partKey: `${String(params.stepId)}:${idx}`,
589
- partIdx: idx,
590
- payload: part,
655
+ payload: {
656
+ status: params.stepStatus ?? "completed",
657
+ iteration: params.iteration,
658
+ errorText: params.errorText,
659
+ },
660
+ },
661
+ ];
662
+ if (params.parts?.length) {
663
+ for (let idx = 0; idx < params.parts.length; idx += 1) {
664
+ const part = params.parts[idx];
665
+ events.push({
666
+ workflowRunId: runId,
667
+ eventId: `context_part:${String(params.stepId)}:${idx}`,
668
+ eventKind: "context.part",
669
+ eventAt: new Date().toISOString(),
670
+ contextId: params.contextId,
671
+ executionId: params.executionId,
672
+ stepId: String(params.stepId),
673
+ partKey: `${String(params.stepId)}:${idx}`,
674
+ partIdx: idx,
675
+ payload: part,
676
+ });
677
+ }
678
+ }
679
+ if (savedReaction) {
680
+ events.push({
681
+ workflowRunId: runId,
682
+ eventId: `context_item:${String(savedReaction.id)}`,
683
+ eventKind: "context.item",
684
+ eventAt: new Date().toISOString(),
685
+ contextId: params.contextId,
686
+ executionId: params.executionId,
687
+ contextEventId: String(savedReaction.id),
688
+ payload: {
689
+ ...savedReaction,
690
+ direction: inferDirection(savedReaction),
691
+ },
591
692
  });
592
693
  }
593
- await maybeWriteTraceEvents(params.env, events);
694
+ await maybeWriteTraceEvents(env, events);
695
+ }
696
+ return { reactionEvent: savedReaction, actionResultChunkEvents };
697
+ }
698
+ export async function linkItemToExecutionStep(params) {
699
+ "use step";
700
+ const { runtime } = await getRuntimeAndEnv(params);
701
+ const { store } = runtime;
702
+ await store.linkItemToExecution({ itemId: params.itemId, executionId: params.executionId });
703
+ }
704
+ export async function saveExecutionStepOutput(params) {
705
+ "use step";
706
+ const { runtime, env } = await getRuntimeAndEnv(params);
707
+ const { store, db } = runtime;
708
+ await store.saveStepParts({ stepId: params.stepId, parts: params.parts });
709
+ const savedReaction = await store.updateItem(params.reactionEventId, params.reactionEvent);
710
+ const { runId } = await resolveWorkflowRunId({
711
+ env,
712
+ db,
713
+ executionId: params.executionId,
714
+ });
715
+ if (runId) {
716
+ const events = [];
717
+ if (params.parts?.length) {
718
+ for (let idx = 0; idx < params.parts.length; idx += 1) {
719
+ const part = params.parts[idx];
720
+ events.push({
721
+ workflowRunId: runId,
722
+ eventId: `context_part:${String(params.stepId)}:${idx}`,
723
+ eventKind: "context.part",
724
+ eventAt: new Date().toISOString(),
725
+ contextId: params.contextId,
726
+ executionId: params.executionId,
727
+ stepId: String(params.stepId),
728
+ partKey: `${String(params.stepId)}:${idx}`,
729
+ partIdx: idx,
730
+ payload: part,
731
+ });
732
+ }
733
+ }
734
+ events.push({
735
+ workflowRunId: runId,
736
+ eventId: `context_item:${String(savedReaction.id)}`,
737
+ eventKind: "context.item",
738
+ eventAt: new Date().toISOString(),
739
+ contextId: params.contextId,
740
+ executionId: params.executionId,
741
+ contextEventId: String(savedReaction.id),
742
+ payload: {
743
+ ...savedReaction,
744
+ direction: inferDirection(savedReaction),
745
+ },
746
+ });
747
+ await maybeWriteTraceEvents(env, events);
594
748
  }
749
+ return { reactionEvent: savedReaction };
595
750
  }
@@ -1,5 +1,6 @@
1
1
  import type { UIMessageChunk } from "ai";
2
2
  import type { ContextEnvironment } from "../context.config.js";
3
+ import type { ContextRuntime } from "../context.runtime.js";
3
4
  import { type ContextStepStreamChunk } from "../context.step-stream.js";
4
5
  import type { ContextStreamEvent } from "../context.stream.js";
5
6
  export declare function writeContextEvents(params: {
@@ -19,21 +20,53 @@ export type PersistedContextStepStreamSession = {
19
20
  executionId: string;
20
21
  stepId: string;
21
22
  };
23
+ export declare function createPersistedContextStepStreamForRuntime(runtime: {
24
+ db?: any;
25
+ }, params: {
26
+ executionId: string;
27
+ stepId: string;
28
+ clientId?: string;
29
+ }): Promise<PersistedContextStepStreamSession>;
22
30
  export declare function createPersistedContextStepStream(params: {
23
- env: ContextEnvironment;
31
+ runtime: ContextRuntime<ContextEnvironment>;
24
32
  executionId: string;
25
33
  stepId: string;
26
34
  clientId?: string;
27
35
  }): Promise<PersistedContextStepStreamSession>;
36
+ export declare function finalizePersistedContextStepStreamForRuntime(params: {
37
+ runtime: {
38
+ db?: any;
39
+ };
40
+ session: PersistedContextStepStreamSession;
41
+ mode: "close" | "abort";
42
+ abortReason?: string | null;
43
+ }): Promise<void>;
28
44
  export declare function closePersistedContextStepStream(params: {
29
- env: ContextEnvironment;
45
+ runtime: ContextRuntime<ContextEnvironment>;
30
46
  session: PersistedContextStepStreamSession;
31
47
  }): Promise<void>;
32
48
  export declare function abortPersistedContextStepStream(params: {
33
- env: ContextEnvironment;
49
+ runtime: ContextRuntime<ContextEnvironment>;
34
50
  session: PersistedContextStepStreamSession;
35
51
  reason?: string | null;
36
52
  }): Promise<void>;
53
+ export type ContextActionResultForStream = {
54
+ actionRequest: {
55
+ actionRef: string;
56
+ actionName: string;
57
+ input: unknown;
58
+ };
59
+ success: boolean;
60
+ output: unknown;
61
+ errorText?: string;
62
+ };
63
+ export declare function writeActionResultPartChunksToSession(params: {
64
+ session?: PersistedContextStepStreamSession | null;
65
+ contextId: string;
66
+ executionId: string;
67
+ itemId: string;
68
+ actionResults: ContextActionResultForStream[];
69
+ }): Promise<ContextStreamEvent[]>;
37
70
  export declare function readPersistedContextStepStream(params: {
38
71
  db: any;
39
72
  clientId?: string;