@ekairos/events 1.22.4-beta.development.0 → 1.22.5-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.
@@ -1,6 +1,10 @@
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 { ContextExecution, ContextItem, ContextIdentifier, StoredContext, ContextStatus } from "../context.store.js";
5
+ type RuntimeParams<Env extends ContextEnvironment = ContextEnvironment> = {
6
+ runtime: ContextRuntime<Env>;
7
+ };
4
8
  export type ContextReviewRequest = {
5
9
  toolCallId: string;
6
10
  toolName?: string;
@@ -10,18 +14,30 @@ export type ContextReviewRequest = {
10
14
  *
11
15
  * This is the "context init" boundary for the story engine.
12
16
  */
13
- export declare function initializeContext<C>(env: ContextEnvironment, contextIdentifier: ContextIdentifier | null, opts?: {
14
- silent?: boolean;
15
- writable?: WritableStream<UIMessageChunk>;
17
+ export declare function initializeContext<C>(params: RuntimeParams & {
18
+ contextIdentifier: ContextIdentifier | null;
19
+ opts?: {
20
+ silent?: boolean;
21
+ writable?: WritableStream<UIMessageChunk>;
22
+ };
16
23
  }): Promise<{
17
24
  context: StoredContext<C>;
18
25
  isNew: boolean;
19
26
  }>;
20
- export declare function updateContextContent<C>(env: ContextEnvironment, contextIdentifier: ContextIdentifier, content: C): Promise<StoredContext<C>>;
21
- export declare function updateContextStatus(env: ContextEnvironment, contextIdentifier: ContextIdentifier, status: ContextStatus): Promise<void>;
22
- export declare function saveTriggerItem(env: ContextEnvironment, contextIdentifier: ContextIdentifier, event: ContextItem): Promise<ContextItem>;
27
+ export declare function updateContextContent<C>(params: RuntimeParams & {
28
+ contextIdentifier: ContextIdentifier;
29
+ content: C;
30
+ }): Promise<StoredContext<C>>;
31
+ export declare function updateContextStatus(params: RuntimeParams & {
32
+ contextIdentifier: ContextIdentifier;
33
+ status: ContextStatus;
34
+ }): Promise<void>;
35
+ export declare function saveTriggerItem(params: RuntimeParams & {
36
+ contextIdentifier: ContextIdentifier;
37
+ event: ContextItem;
38
+ }): Promise<ContextItem>;
23
39
  export declare function saveTriggerAndCreateExecution(params: {
24
- env: ContextEnvironment;
40
+ runtime: ContextRuntime<ContextEnvironment>;
25
41
  contextIdentifier: ContextIdentifier;
26
42
  triggerEvent: ContextItem;
27
43
  }): Promise<{
@@ -29,41 +45,57 @@ export declare function saveTriggerAndCreateExecution(params: {
29
45
  reactionEvent: ContextItem;
30
46
  execution: ContextExecution;
31
47
  }>;
32
- export declare function saveReactionItem(env: ContextEnvironment, contextIdentifier: ContextIdentifier, event: ContextItem, opts?: {
33
- executionId?: string;
34
- contextId?: string;
35
- reviewRequests?: ContextReviewRequest[];
48
+ export declare function saveReactionItem(params: RuntimeParams & {
49
+ contextIdentifier: ContextIdentifier;
50
+ event: ContextItem;
51
+ opts?: {
52
+ executionId?: string;
53
+ contextId?: string;
54
+ reviewRequests?: ContextReviewRequest[];
55
+ };
36
56
  }): Promise<ContextItem>;
37
- export declare function updateItem(env: ContextEnvironment, eventId: string, event: ContextItem, opts?: {
38
- executionId?: string;
39
- contextId?: string;
57
+ export declare function updateItem(params: RuntimeParams & {
58
+ eventId: string;
59
+ event: ContextItem;
60
+ opts?: {
61
+ executionId?: string;
62
+ contextId?: string;
63
+ };
40
64
  }): Promise<ContextItem>;
41
- export declare function createExecution(env: ContextEnvironment, contextIdentifier: ContextIdentifier, triggerEventId: string, reactionEventId: string): Promise<{
65
+ export declare function createExecution(params: RuntimeParams & {
66
+ contextIdentifier: ContextIdentifier;
67
+ triggerEventId: string;
68
+ reactionEventId: string;
69
+ }): Promise<{
42
70
  id: string;
43
71
  }>;
44
72
  export declare function createReactionItem(params: {
45
- env: ContextEnvironment;
73
+ runtime: ContextRuntime<ContextEnvironment>;
46
74
  contextIdentifier: ContextIdentifier;
47
75
  triggerEventId: string;
48
76
  }): Promise<{
49
77
  reactionEventId: string;
50
78
  executionId: string;
51
79
  }>;
52
- export declare function completeExecution(env: ContextEnvironment, contextIdentifier: ContextIdentifier, executionId: string, status: "completed" | "failed"): Promise<void>;
80
+ export declare function completeExecution(params: RuntimeParams & {
81
+ contextIdentifier: ContextIdentifier;
82
+ executionId: string;
83
+ status: "completed" | "failed";
84
+ }): Promise<void>;
53
85
  export declare function updateExecutionWorkflowRun(params: {
54
- env: ContextEnvironment;
86
+ runtime: ContextRuntime<ContextEnvironment>;
55
87
  executionId: string;
56
88
  workflowRunId: string;
57
89
  }): Promise<void>;
58
90
  export declare function createContextStep(params: {
59
- env: ContextEnvironment;
91
+ runtime: ContextRuntime<ContextEnvironment>;
60
92
  executionId: string;
61
93
  iteration: number;
62
94
  }): Promise<{
63
95
  stepId: string;
64
96
  }>;
65
97
  export declare function updateContextStep(params: {
66
- env: ContextEnvironment;
98
+ runtime: ContextRuntime<ContextEnvironment>;
67
99
  stepId: string;
68
100
  executionId?: string;
69
101
  contextId?: string;
@@ -82,15 +114,16 @@ export declare function updateContextStep(params: {
82
114
  };
83
115
  }): Promise<void>;
84
116
  export declare function linkItemToExecutionStep(params: {
85
- env: ContextEnvironment;
117
+ runtime: ContextRuntime<ContextEnvironment>;
86
118
  itemId: string;
87
119
  executionId: string;
88
120
  }): Promise<void>;
89
121
  export declare function saveContextPartsStep(params: {
90
- env: ContextEnvironment;
122
+ runtime: ContextRuntime<ContextEnvironment>;
91
123
  stepId: string;
92
124
  executionId?: string;
93
125
  contextId?: string;
94
126
  iteration?: number;
95
127
  parts: any[];
96
128
  }): Promise<void>;
129
+ export {};
@@ -1,6 +1,12 @@
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
+ async function getRuntimeAndEnv(params) {
6
+ const env = params.runtime.env;
7
+ const runtime = await getContextRuntimeServices(params.runtime);
8
+ return { runtime, env };
9
+ }
4
10
  async function maybeWriteTraceEvents(env, traceEvents) {
5
11
  if (!traceEvents?.length)
6
12
  return;
@@ -92,24 +98,23 @@ function logStepDebug(message, payload) {
92
98
  *
93
99
  * This is the "context init" boundary for the story engine.
94
100
  */
95
- export async function initializeContext(env, contextIdentifier, opts) {
101
+ export async function initializeContext(params) {
96
102
  "use step";
97
- const { getContextRuntime } = await import("../runtime.js");
98
- const runtime = await getContextRuntime(env);
103
+ const { runtime, env } = await getRuntimeAndEnv(params);
99
104
  const { store, db } = runtime;
100
105
  // Detect creation explicitly so the engine can run onContextCreated hooks.
101
106
  let result;
102
- if (!contextIdentifier) {
107
+ if (!params.contextIdentifier) {
103
108
  const context = await store.getOrCreateContext(null);
104
109
  result = { context, isNew: true };
105
110
  }
106
111
  else {
107
- const existing = await store.getContext(contextIdentifier);
112
+ const existing = await store.getContext(params.contextIdentifier);
108
113
  if (existing) {
109
114
  result = { context: existing, isNew: false };
110
115
  }
111
116
  else {
112
- const created = await store.getOrCreateContext(contextIdentifier);
117
+ const created = await store.getOrCreateContext(params.contextIdentifier);
113
118
  result = { context: created, isNew: true };
114
119
  }
115
120
  }
@@ -132,29 +137,24 @@ export async function initializeContext(env, contextIdentifier, opts) {
132
137
  }
133
138
  return result;
134
139
  }
135
- export async function updateContextContent(env, contextIdentifier, content) {
140
+ export async function updateContextContent(params) {
136
141
  "use step";
137
- const { getContextRuntime } = await import("../runtime.js");
138
- const { store } = await getContextRuntime(env);
139
- return await store.updateContextContent(contextIdentifier, content);
142
+ const { runtime } = await getRuntimeAndEnv(params);
143
+ return await runtime.store.updateContextContent(params.contextIdentifier, params.content);
140
144
  }
141
- export async function updateContextStatus(env, contextIdentifier, status) {
145
+ export async function updateContextStatus(params) {
142
146
  "use step";
143
- const { getContextRuntime } = await import("../runtime.js");
144
- const { store } = await getContextRuntime(env);
145
- await store.updateContextStatus(contextIdentifier, status);
147
+ const { runtime } = await getRuntimeAndEnv(params);
148
+ await runtime.store.updateContextStatus(params.contextIdentifier, params.status);
146
149
  }
147
- export async function saveTriggerItem(env, contextIdentifier, event) {
150
+ export async function saveTriggerItem(params) {
148
151
  "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;
152
+ const { runtime } = await getRuntimeAndEnv(params);
153
+ return await runtime.store.saveItem(params.contextIdentifier, params.event);
153
154
  }
154
155
  export async function saveTriggerAndCreateExecution(params) {
155
156
  "use step";
156
- const { getContextRuntime } = await import("../runtime.js");
157
- const runtime = await getContextRuntime(params.env);
157
+ const { runtime, env } = await getRuntimeAndEnv(params);
158
158
  const { store, db } = runtime;
159
159
  logStepDebug("saveTriggerAndCreateExecution:start", {
160
160
  contextIdentifier: summarizeContextIdentifierForLog(params.contextIdentifier),
@@ -241,7 +241,7 @@ export async function saveTriggerAndCreateExecution(params) {
241
241
  throw error;
242
242
  }
243
243
  const { runId, meta } = await resolveWorkflowRunId({
244
- env: params.env,
244
+ env,
245
245
  db,
246
246
  executionId: execution.id,
247
247
  });
@@ -329,7 +329,7 @@ export async function saveTriggerAndCreateExecution(params) {
329
329
  },
330
330
  },
331
331
  ];
332
- await maybeWriteTraceEvents(params.env, events);
332
+ await maybeWriteTraceEvents(env, events);
333
333
  }
334
334
  return {
335
335
  triggerEvent: saved,
@@ -340,26 +340,25 @@ export async function saveTriggerAndCreateExecution(params) {
340
340
  },
341
341
  };
342
342
  }
343
- export async function saveReactionItem(env, contextIdentifier, event, opts) {
343
+ export async function saveReactionItem(params) {
344
344
  "use step";
345
- const { getContextRuntime } = await import("../runtime.js");
346
- const runtime = await getContextRuntime(env);
345
+ const { runtime, env } = await getRuntimeAndEnv(params);
347
346
  const { store, db } = runtime;
348
- const saved = await store.saveItem(contextIdentifier, event);
349
- if (opts?.executionId) {
347
+ const saved = await store.saveItem(params.contextIdentifier, params.event);
348
+ if (params.opts?.executionId) {
350
349
  await store.linkItemToExecution({
351
350
  itemId: saved.id,
352
- executionId: opts.executionId,
351
+ executionId: params.opts.executionId,
353
352
  });
354
353
  }
355
- const contextId = opts?.contextId ??
356
- (typeof contextIdentifier?.id === "string"
357
- ? String(contextIdentifier.id)
354
+ const contextId = params.opts?.contextId ??
355
+ (typeof params.contextIdentifier?.id === "string"
356
+ ? String(params.contextIdentifier.id)
358
357
  : undefined);
359
358
  const { runId, meta } = await resolveWorkflowRunId({
360
359
  env,
361
360
  db,
362
- executionId: opts?.executionId,
361
+ executionId: params.opts?.executionId,
363
362
  });
364
363
  if (runId) {
365
364
  const events = [
@@ -369,7 +368,7 @@ export async function saveReactionItem(env, contextIdentifier, event, opts) {
369
368
  eventKind: "context.item",
370
369
  eventAt: new Date().toISOString(),
371
370
  contextId,
372
- executionId: opts?.executionId,
371
+ executionId: params.opts?.executionId,
373
372
  contextEventId: String(saved.id),
374
373
  payload: {
375
374
  ...saved,
@@ -377,30 +376,30 @@ export async function saveReactionItem(env, contextIdentifier, event, opts) {
377
376
  },
378
377
  },
379
378
  ];
380
- if (opts?.executionId && opts.reviewRequests?.length) {
379
+ if (params.opts?.executionId && params.opts.reviewRequests?.length) {
381
380
  const resumeHookUrl = getClientResumeHookUrl();
382
381
  const workflowUrl = meta && typeof meta.url === "string" && meta.url.trim()
383
382
  ? String(meta.url)
384
383
  : undefined;
385
- for (const rr of opts.reviewRequests) {
384
+ for (const rr of params.opts.reviewRequests) {
386
385
  const toolCallId = String(rr.toolCallId);
387
386
  events.push({
388
387
  workflowRunId: runId,
389
- eventId: `context_review:${String(opts.executionId)}:${toolCallId}`,
388
+ eventId: `context_review:${String(params.opts.executionId)}:${toolCallId}`,
390
389
  eventKind: "context.review",
391
390
  eventAt: new Date().toISOString(),
392
391
  contextId,
393
- executionId: String(opts.executionId),
392
+ executionId: String(params.opts.executionId),
394
393
  toolCallId,
395
394
  payload: {
396
395
  status: "in_review",
397
396
  toolName: rr.toolName ?? "",
398
397
  hookToken: toolApprovalHookToken({
399
- executionId: String(opts.executionId),
398
+ executionId: String(params.opts.executionId),
400
399
  toolCallId,
401
400
  }),
402
401
  webhookToken: toolApprovalWebhookToken({
403
- executionId: String(opts.executionId),
402
+ executionId: String(params.opts.executionId),
404
403
  toolCallId,
405
404
  }),
406
405
  resumeHookUrl,
@@ -413,16 +412,15 @@ export async function saveReactionItem(env, contextIdentifier, event, opts) {
413
412
  }
414
413
  return saved;
415
414
  }
416
- export async function updateItem(env, eventId, event, opts) {
415
+ export async function updateItem(params) {
417
416
  "use step";
418
- const { getContextRuntime } = await import("../runtime.js");
419
- const runtime = await getContextRuntime(env);
417
+ const { runtime, env } = await getRuntimeAndEnv(params);
420
418
  const { store, db } = runtime;
421
- const saved = await store.updateItem(eventId, event);
419
+ const saved = await store.updateItem(params.eventId, params.event);
422
420
  const { runId } = await resolveWorkflowRunId({
423
421
  env,
424
422
  db,
425
- executionId: opts?.executionId,
423
+ executionId: params.opts?.executionId,
426
424
  });
427
425
  if (runId) {
428
426
  await maybeWriteTraceEvents(env, [
@@ -431,8 +429,8 @@ export async function updateItem(env, eventId, event, opts) {
431
429
  eventId: `context_item:${String(saved.id)}`,
432
430
  eventKind: "context.item",
433
431
  eventAt: new Date().toISOString(),
434
- contextId: opts?.contextId,
435
- executionId: opts?.executionId,
432
+ contextId: params.opts?.contextId,
433
+ executionId: params.opts?.executionId,
436
434
  contextEventId: String(saved.id),
437
435
  payload: {
438
436
  ...saved,
@@ -443,16 +441,15 @@ export async function updateItem(env, eventId, event, opts) {
443
441
  }
444
442
  return saved;
445
443
  }
446
- export async function createExecution(env, contextIdentifier, triggerEventId, reactionEventId) {
444
+ export async function createExecution(params) {
447
445
  "use step";
448
- const { getContextRuntime } = await import("../runtime.js");
449
- const { store } = await getContextRuntime(env);
450
- return await store.createExecution(contextIdentifier, triggerEventId, reactionEventId);
446
+ const { runtime } = await getRuntimeAndEnv(params);
447
+ return await runtime.store.createExecution(params.contextIdentifier, params.triggerEventId, params.reactionEventId);
451
448
  }
452
449
  export async function createReactionItem(params) {
453
450
  "use step";
454
- const { getContextRuntime } = await import("../runtime.js");
455
- const { store } = await getContextRuntime(params.env);
451
+ const { runtime } = await getRuntimeAndEnv(params);
452
+ const { store } = runtime;
456
453
  // Generate a new reaction event id inside the step boundary.
457
454
  const uuid = globalThis.crypto?.randomUUID?.();
458
455
  const reactionEventId = typeof uuid === "string"
@@ -461,31 +458,30 @@ export async function createReactionItem(params) {
461
458
  const execution = await store.createExecution(params.contextIdentifier, params.triggerEventId, reactionEventId);
462
459
  return { reactionEventId, executionId: execution.id };
463
460
  }
464
- export async function completeExecution(env, contextIdentifier, executionId, status) {
461
+ export async function completeExecution(params) {
465
462
  "use step";
466
- const { getContextRuntime } = await import("../runtime.js");
467
- const runtime = await getContextRuntime(env);
463
+ const { runtime, env } = await getRuntimeAndEnv(params);
468
464
  const { store, db } = runtime;
469
- await store.completeExecution(contextIdentifier, executionId, status);
470
- const contextId = typeof contextIdentifier?.id === "string"
471
- ? String(contextIdentifier.id)
465
+ await store.completeExecution(params.contextIdentifier, params.executionId, params.status);
466
+ const contextId = typeof params.contextIdentifier?.id === "string"
467
+ ? String(params.contextIdentifier.id)
472
468
  : undefined;
473
469
  const { runId } = await resolveWorkflowRunId({
474
470
  env,
475
471
  db,
476
- executionId,
472
+ executionId: params.executionId,
477
473
  });
478
474
  if (runId) {
479
475
  await maybeWriteTraceEvents(env, [
480
476
  {
481
477
  workflowRunId: runId,
482
- eventId: `context_execution:${String(executionId)}:${status}`,
478
+ eventId: `context_execution:${String(params.executionId)}:${params.status}`,
483
479
  eventKind: "context.execution",
484
480
  eventAt: new Date().toISOString(),
485
481
  contextId,
486
- executionId: String(executionId),
482
+ executionId: String(params.executionId),
487
483
  payload: {
488
- status,
484
+ status: params.status,
489
485
  },
490
486
  },
491
487
  ]);
@@ -493,9 +489,8 @@ export async function completeExecution(env, contextIdentifier, executionId, sta
493
489
  }
494
490
  export async function updateExecutionWorkflowRun(params) {
495
491
  "use step";
496
- const { getContextRuntime } = await import("../runtime.js");
497
- const runtime = await getContextRuntime(params.env);
498
- const db = runtime?.db;
492
+ const { runtime } = await getRuntimeAndEnv(params);
493
+ const db = runtime.db;
499
494
  if (db) {
500
495
  await db.transact([
501
496
  db.tx.event_executions[params.executionId].update({
@@ -507,8 +502,8 @@ export async function updateExecutionWorkflowRun(params) {
507
502
  }
508
503
  export async function createContextStep(params) {
509
504
  "use step";
510
- const { getContextRuntime } = await import("../runtime.js");
511
- const { store } = await getContextRuntime(params.env);
505
+ const { runtime } = await getRuntimeAndEnv(params);
506
+ const { store } = runtime;
512
507
  const res = await store.createStep({
513
508
  executionId: params.executionId,
514
509
  iteration: params.iteration,
@@ -517,20 +512,19 @@ export async function createContextStep(params) {
517
512
  }
518
513
  export async function updateContextStep(params) {
519
514
  "use step";
520
- const { getContextRuntime } = await import("../runtime.js");
521
- const runtime = await getContextRuntime(params.env);
515
+ const { runtime, env } = await getRuntimeAndEnv(params);
522
516
  const { store, db } = runtime;
523
517
  await store.updateStep(params.stepId, {
524
518
  ...params.patch,
525
519
  updatedAt: new Date(),
526
520
  });
527
521
  const { runId } = await resolveWorkflowRunId({
528
- env: params.env,
522
+ env,
529
523
  db,
530
524
  executionId: params.executionId,
531
525
  });
532
526
  if (runId) {
533
- await maybeWriteTraceEvents(params.env, [
527
+ await maybeWriteTraceEvents(env, [
534
528
  {
535
529
  workflowRunId: runId,
536
530
  eventId: `context_step:${String(params.stepId)}`,
@@ -558,18 +552,17 @@ export async function updateContextStep(params) {
558
552
  }
559
553
  export async function linkItemToExecutionStep(params) {
560
554
  "use step";
561
- const { getContextRuntime } = await import("../runtime.js");
562
- const { store } = await getContextRuntime(params.env);
555
+ const { runtime } = await getRuntimeAndEnv(params);
556
+ const { store } = runtime;
563
557
  await store.linkItemToExecution({ itemId: params.itemId, executionId: params.executionId });
564
558
  }
565
559
  export async function saveContextPartsStep(params) {
566
560
  "use step";
567
- const { getContextRuntime } = await import("../runtime.js");
568
- const runtime = await getContextRuntime(params.env);
561
+ const { runtime, env } = await getRuntimeAndEnv(params);
569
562
  const { store, db } = runtime;
570
563
  await store.saveStepParts({ stepId: params.stepId, parts: params.parts });
571
564
  const { runId } = await resolveWorkflowRunId({
572
- env: params.env,
565
+ env,
573
566
  db,
574
567
  executionId: params.executionId,
575
568
  });
@@ -590,6 +583,6 @@ export async function saveContextPartsStep(params) {
590
583
  payload: part,
591
584
  });
592
585
  }
593
- await maybeWriteTraceEvents(params.env, events);
586
+ await maybeWriteTraceEvents(env, events);
594
587
  }
595
588
  }
@@ -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: {
@@ -20,17 +21,17 @@ export type PersistedContextStepStreamSession = {
20
21
  stepId: string;
21
22
  };
22
23
  export declare function createPersistedContextStepStream(params: {
23
- env: ContextEnvironment;
24
+ runtime: ContextRuntime<ContextEnvironment>;
24
25
  executionId: string;
25
26
  stepId: string;
26
27
  clientId?: string;
27
28
  }): Promise<PersistedContextStepStreamSession>;
28
29
  export declare function closePersistedContextStepStream(params: {
29
- env: ContextEnvironment;
30
+ runtime: ContextRuntime<ContextEnvironment>;
30
31
  session: PersistedContextStepStreamSession;
31
32
  }): Promise<void>;
32
33
  export declare function abortPersistedContextStepStream(params: {
33
- env: ContextEnvironment;
34
+ runtime: ContextRuntime<ContextEnvironment>;
34
35
  session: PersistedContextStepStreamSession;
35
36
  reason?: string | null;
36
37
  }): Promise<void>;
@@ -1,3 +1,4 @@
1
+ import { getContextRuntimeServices } from "../context.runtime.js";
1
2
  import { contextStreamByteLength, parseContextStepStreamChunk, } from "../context.step-stream.js";
2
3
  export async function writeContextEvents(params) {
3
4
  "use step";
@@ -14,7 +15,9 @@ export async function writeContextEvents(params) {
14
15
  }
15
16
  }
16
17
  finally {
17
- writer.releaseLock();
18
+ if (typeof writer?.releaseLock === "function") {
19
+ writer.releaseLock();
20
+ }
18
21
  }
19
22
  }
20
23
  export async function closeContextStream(params) {
@@ -30,7 +33,9 @@ export async function closeContextStream(params) {
30
33
  await writer.write({ type: "finish" });
31
34
  }
32
35
  finally {
33
- writer.releaseLock();
36
+ if (typeof writer?.releaseLock === "function") {
37
+ writer.releaseLock();
38
+ }
34
39
  }
35
40
  }
36
41
  if (!preventClose) {
@@ -60,8 +65,7 @@ export function createContextStepStreamClientId(stepId) {
60
65
  }
61
66
  export async function createPersistedContextStepStream(params) {
62
67
  "use step";
63
- const { getContextRuntime } = await import("../runtime.js");
64
- const runtime = await getContextRuntime(params.env);
68
+ const runtime = await getContextRuntimeServices(params.runtime);
65
69
  const db = runtime?.db;
66
70
  if (!db?.streams?.createWriteStream) {
67
71
  throw new Error("InstantDB streams are not available on the configured runtime. Upgrade @instantdb/admin to a streams-capable version.");
@@ -103,8 +107,7 @@ export async function createPersistedContextStepStream(params) {
103
107
  }
104
108
  async function finalizePersistedContextStepStream(params) {
105
109
  "use step";
106
- const { getContextRuntime } = await import("../runtime.js");
107
- const runtime = await getContextRuntime(params.env);
110
+ const runtime = await getContextRuntimeServices(params.runtime);
108
111
  const db = runtime?.db;
109
112
  const writer = params.session.stream.getWriter();
110
113
  try {
@@ -116,7 +119,9 @@ async function finalizePersistedContextStepStream(params) {
116
119
  }
117
120
  }
118
121
  finally {
119
- writer.releaseLock();
122
+ if (typeof writer?.releaseLock === "function") {
123
+ writer.releaseLock();
124
+ }
120
125
  }
121
126
  const now = new Date();
122
127
  const txs = [
@@ -140,14 +145,14 @@ async function finalizePersistedContextStepStream(params) {
140
145
  }
141
146
  export async function closePersistedContextStepStream(params) {
142
147
  return await finalizePersistedContextStepStream({
143
- env: params.env,
148
+ runtime: params.runtime,
144
149
  session: params.session,
145
150
  mode: "close",
146
151
  });
147
152
  }
148
153
  export async function abortPersistedContextStepStream(params) {
149
154
  return await finalizePersistedContextStepStream({
150
- env: params.env,
155
+ runtime: params.runtime,
151
156
  session: params.session,
152
157
  mode: "abort",
153
158
  abortReason: params.reason,
@@ -1,5 +1,6 @@
1
1
  import "../polyfills/dom-events.js";
2
2
  import type { ContextEnvironment } from "../context.config.js";
3
+ import type { ContextRuntime } from "../context.runtime.js";
3
4
  import type { TraceEventKind } from "../context.contract.js";
4
5
  export type ContextTraceEventWrite = {
5
6
  workflowRunId: string;
@@ -33,6 +34,7 @@ export type ContextTraceEventWrite = {
33
34
  testId?: string;
34
35
  };
35
36
  export declare function writeContextTraceEvents(params: {
37
+ runtime?: ContextRuntime<ContextEnvironment>;
36
38
  env: ContextEnvironment;
37
39
  events: ContextTraceEventWrite[];
38
40
  }): Promise<void>;
@@ -1,4 +1,5 @@
1
1
  import "../polyfills/dom-events.js";
2
+ import { getContextRuntimeServices } from "../context.runtime.js";
2
3
  import { lookup } from "@instantdb/admin";
3
4
  function requireBaseUrl() {
4
5
  const baseUrl = process.env.EKAIROS_CORE_BASE_URL ||
@@ -84,8 +85,10 @@ export async function writeContextTraceEvents(params) {
84
85
  const strict = envTrace?.strict === true || process.env.EKAIROS_TRACES_STRICT === "1";
85
86
  // 1) Local trace persistence (InstantDB source of truth).
86
87
  try {
87
- const { getContextRuntime } = await import("../runtime.js");
88
- const runtime = await getContextRuntime(params.env);
88
+ if (!params.runtime) {
89
+ throw new Error("[context/trace] runtime is required");
90
+ }
91
+ const runtime = await getContextRuntimeServices(params.runtime);
89
92
  const db = runtime?.db;
90
93
  if (db) {
91
94
  const now = new Date();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ekairos/events",
3
- "version": "1.22.4-beta.development.0",
3
+ "version": "1.22.5-beta.development.0",
4
4
  "description": "Ekairos Events - Context-first workflow runtime",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -108,6 +108,7 @@
108
108
  "clean": "node -e \"require('fs').rmSync('dist', {recursive:true, force:true})\"",
109
109
  "typecheck": "tsc --noEmit",
110
110
  "test": "vitest run -c vitest.config.mts",
111
+ "test:workflow": "vitest run -c vitest.workflow.config.mts",
111
112
  "pretest:ai-sdk-reactor": "pnpm --filter @ekairos/domain build",
112
113
  "test:ai-sdk-reactor": "vitest run -c vitest.config.mts src/tests/context.ai-sdk-reactor.instant.test.ts",
113
114
  "test:ai-sdk-reactor:real": "vitest run -c vitest.config.mts src/tests/context.ai-sdk-reactor.real.instant.test.ts",
@@ -116,7 +117,7 @@
116
117
  },
117
118
  "dependencies": {
118
119
  "@ai-sdk/openai": "^2.0.52",
119
- "@ekairos/domain": "^1.22.4-beta.development.0",
120
+ "@ekairos/domain": "^1.22.5-beta.development.0",
120
121
  "@instantdb/admin": "0.22.158",
121
122
  "@instantdb/core": "0.22.142",
122
123
  "@vercel/mcp-adapter": "^1.0.0",
@@ -133,6 +134,8 @@
133
134
  "devDependencies": {
134
135
  "@ekairos/testing": "workspace:*",
135
136
  "@ekairos/tsconfig": "workspace:*",
137
+ "@workflow/serde": "^4.1.0",
138
+ "@workflow/vitest": "^4.0.1",
136
139
  "@types/node": "^24.5.0",
137
140
  "@types/react": "^19.2.2",
138
141
  "dotenv": "^17.2.3",