@ekairos/events 1.22.39-beta.development.0 → 1.22.40-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 (45) hide show
  1. package/README.md +5 -3
  2. package/dist/codex.d.ts +11 -2
  3. package/dist/codex.js +13 -5
  4. package/dist/context.action.d.ts +55 -0
  5. package/dist/context.action.js +25 -0
  6. package/dist/context.builder.d.ts +52 -42
  7. package/dist/context.builder.js +29 -24
  8. package/dist/context.d.ts +2 -1
  9. package/dist/context.engine.d.ts +50 -47
  10. package/dist/context.engine.js +222 -173
  11. package/dist/context.events.js +28 -87
  12. package/dist/context.js +1 -0
  13. package/dist/context.part-identity.d.ts +40 -0
  14. package/dist/context.part-identity.js +268 -0
  15. package/dist/context.parts.d.ts +389 -164
  16. package/dist/context.parts.js +235 -224
  17. package/dist/context.registry.d.ts +1 -1
  18. package/dist/context.runtime.d.ts +10 -4
  19. package/dist/context.runtime.js +7 -1
  20. package/dist/context.step-stream.d.ts +16 -2
  21. package/dist/context.step-stream.js +58 -16
  22. package/dist/context.stream.d.ts +4 -0
  23. package/dist/context.stream.js +23 -1
  24. package/dist/context.toolcalls.d.ts +8 -20
  25. package/dist/context.toolcalls.js +61 -55
  26. package/dist/index.d.ts +8 -4
  27. package/dist/index.js +5 -3
  28. package/dist/reactors/ai-sdk.chunk-map.js +27 -0
  29. package/dist/reactors/ai-sdk.reactor.d.ts +8 -9
  30. package/dist/reactors/ai-sdk.reactor.js +2 -5
  31. package/dist/reactors/ai-sdk.step.d.ts +2 -3
  32. package/dist/reactors/ai-sdk.step.js +10 -7
  33. package/dist/reactors/scripted.reactor.d.ts +7 -4
  34. package/dist/reactors/types.d.ts +8 -8
  35. package/dist/schema.d.ts +273 -2
  36. package/dist/schema.js +1 -1
  37. package/dist/steps/store.steps.d.ts +51 -12
  38. package/dist/steps/store.steps.js +137 -0
  39. package/dist/steps/stream.steps.d.ts +15 -0
  40. package/dist/steps/stream.steps.js +16 -5
  41. package/dist/steps/trace.steps.d.ts +4 -4
  42. package/dist/steps/trace.steps.js +21 -6
  43. package/dist/tools-to-model-tools.d.ts +4 -2
  44. package/dist/tools-to-model-tools.js +30 -11
  45. package/package.json +7 -6
@@ -1,6 +1,9 @@
1
1
  import type { ModelMessage } from "ai";
2
+ import type { DomainSchemaResult } from "@ekairos/domain";
2
3
  import type { ContextEnvironment } from "../context.config.js";
4
+ import type { ContextRuntime } from "../context.runtime.js";
3
5
  import type { ContextItem } from "../context.store.js";
6
+ import { eventsDomain } from "../schema.js";
4
7
  import type { ContextReactionLLM, ContextActionRequest, ContextReactor, ContextReactorParams } from "./types.js";
5
8
  type ScriptedReactionPayload = {
6
9
  assistantEvent?: Partial<ContextItem>;
@@ -8,10 +11,10 @@ type ScriptedReactionPayload = {
8
11
  messagesForModel?: ModelMessage[];
9
12
  llm?: ContextReactionLLM;
10
13
  };
11
- export type ScriptedReactorStep<Context = unknown, Env extends ContextEnvironment = ContextEnvironment> = ScriptedReactionPayload | ((params: ContextReactorParams<Context, Env>) => Promise<ScriptedReactionPayload> | ScriptedReactionPayload);
12
- export type CreateScriptedReactorOptions<Context = unknown, Env extends ContextEnvironment = ContextEnvironment> = {
13
- steps: ScriptedReactorStep<Context, Env>[];
14
+ export type ScriptedReactorStep<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ScriptedReactionPayload | ((params: ContextReactorParams<Context, Env, RequiredDomain, Runtime>) => Promise<ScriptedReactionPayload> | ScriptedReactionPayload);
15
+ export type CreateScriptedReactorOptions<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = {
16
+ steps: ScriptedReactorStep<Context, Env, RequiredDomain, Runtime>[];
14
17
  repeatLast?: boolean;
15
18
  };
16
- export declare function createScriptedReactor<Context = unknown, Env extends ContextEnvironment = ContextEnvironment>(options: CreateScriptedReactorOptions<Context, Env>): ContextReactor<Context, Env>;
19
+ export declare function createScriptedReactor<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>>(options: CreateScriptedReactorOptions<Context, Env, RequiredDomain, Runtime>): ContextReactor<Context, Env, RequiredDomain, Runtime>;
17
20
  export {};
@@ -1,10 +1,12 @@
1
1
  import type { ModelMessage, UIMessageChunk } from "ai";
2
+ import type { DomainSchemaResult } from "@ekairos/domain";
2
3
  import type { ContextEnvironment } from "../context.config.js";
3
- import type { ContextRuntime } from "../context.runtime.js";
4
+ import type { ContextTool } from "../context.action.js";
5
+ import type { ContextRuntime, ContextRuntimeHandleForDomain } from "../context.runtime.js";
4
6
  import type { ContextModelInit } from "../context.engine.js";
5
7
  import type { ContextIdentifier, StoredContext, ContextItem } from "../context.store.js";
6
8
  import type { ContextSkillPackage } from "../context.skill.js";
7
- import type { SerializableActionSpec } from "../tools-to-model-tools.js";
9
+ import { eventsDomain } from "../schema.js";
8
10
  export type ContextActionRequest = {
9
11
  actionRef: string;
10
12
  actionName: string;
@@ -32,9 +34,8 @@ export type ContextReactionResult = {
32
34
  state?: Record<string, unknown> | null;
33
35
  };
34
36
  };
35
- export type ContextReactorParams<Context = unknown, Env extends ContextEnvironment = ContextEnvironment> = {
36
- runtime: ContextRuntime<Env>;
37
- env: Env;
37
+ export type ContextReactorParams<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = any> = {
38
+ runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>;
38
39
  context: StoredContext<Context>;
39
40
  contextIdentifier: ContextIdentifier;
40
41
  /**
@@ -49,8 +50,7 @@ export type ContextReactorParams<Context = unknown, Env extends ContextEnvironme
49
50
  triggerEvent: ContextItem;
50
51
  model: ContextModelInit;
51
52
  systemPrompt: string;
52
- actions: Record<string, unknown>;
53
- actionSpecs: Record<string, SerializableActionSpec>;
53
+ actions: Record<string, ContextTool<Context, Env, RequiredDomain, Runtime>>;
54
54
  skills: ContextSkillPackage[];
55
55
  eventId: string;
56
56
  executionId: string;
@@ -64,4 +64,4 @@ export type ContextReactorParams<Context = unknown, Env extends ContextEnvironme
64
64
  writable?: WritableStream<UIMessageChunk>;
65
65
  persistReactionParts?: (parts: any[]) => Promise<void>;
66
66
  };
67
- export type ContextReactor<Context = unknown, Env extends ContextEnvironment = ContextEnvironment> = (params: ContextReactorParams<Context, Env>) => Promise<ContextReactionResult>;
67
+ export type ContextReactor<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = any> = (params: ContextReactorParams<Context, Env, RequiredDomain, Runtime>) => Promise<ContextReactionResult>;
package/dist/schema.d.ts CHANGED
@@ -1,2 +1,273 @@
1
- import { type DomainSchemaResult } from "@ekairos/domain";
2
- export declare const eventsDomain: DomainSchemaResult;
1
+ export declare const eventsDomain: import("@ekairos/domain").DomainSchemaResult<{
2
+ event_contexts: import("@instantdb/core").EntityDef<{
3
+ createdAt: import("@instantdb/core").DataAttrDef<Date, true, false, false>;
4
+ updatedAt: import("@instantdb/core").DataAttrDef<Date, false, false, false>;
5
+ key: import("@instantdb/core").DataAttrDef<string, false, true, true>;
6
+ name: import("@instantdb/core").DataAttrDef<string, false, false, false>;
7
+ status: import("@instantdb/core").DataAttrDef<string, false, true, false>;
8
+ content: import("@instantdb/core").DataAttrDef<any, false, false, false>;
9
+ reactor: import("@instantdb/core").DataAttrDef<any, false, false, false>;
10
+ }, {}, unknown>;
11
+ event_items: import("@instantdb/core").EntityDef<{
12
+ channel: import("@instantdb/core").DataAttrDef<string, true, true, false>;
13
+ createdAt: import("@instantdb/core").DataAttrDef<Date, true, true, false>;
14
+ type: import("@instantdb/core").DataAttrDef<string, false, true, false>;
15
+ content: import("@instantdb/core").DataAttrDef<any, false, false, false>;
16
+ status: import("@instantdb/core").DataAttrDef<string, false, true, false>;
17
+ }, {}, unknown>;
18
+ event_executions: import("@instantdb/core").EntityDef<{
19
+ createdAt: import("@instantdb/core").DataAttrDef<Date, true, false, false>;
20
+ updatedAt: import("@instantdb/core").DataAttrDef<Date, false, false, false>;
21
+ status: import("@instantdb/core").DataAttrDef<string, false, true, false>;
22
+ workflowRunId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
23
+ activeStreamId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
24
+ activeStreamClientId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
25
+ lastStreamId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
26
+ lastStreamClientId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
27
+ }, {}, unknown>;
28
+ event_steps: import("@instantdb/core").EntityDef<{
29
+ createdAt: import("@instantdb/core").DataAttrDef<Date, true, true, false>;
30
+ updatedAt: import("@instantdb/core").DataAttrDef<Date, false, false, false>;
31
+ status: import("@instantdb/core").DataAttrDef<string, false, true, false>;
32
+ iteration: import("@instantdb/core").DataAttrDef<number, true, true, false>;
33
+ kind: import("@instantdb/core").DataAttrDef<string, false, true, false>;
34
+ actionName: import("@instantdb/core").DataAttrDef<string, false, true, false>;
35
+ actionInput: import("@instantdb/core").DataAttrDef<any, false, false, false>;
36
+ actionOutput: import("@instantdb/core").DataAttrDef<any, false, false, false>;
37
+ actionError: import("@instantdb/core").DataAttrDef<string, false, false, false>;
38
+ actionRequests: import("@instantdb/core").DataAttrDef<any, false, false, false>;
39
+ actionResults: import("@instantdb/core").DataAttrDef<any, false, false, false>;
40
+ continueLoop: import("@instantdb/core").DataAttrDef<boolean, false, false, false>;
41
+ errorText: import("@instantdb/core").DataAttrDef<string, false, false, false>;
42
+ streamId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
43
+ streamClientId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
44
+ streamStartedAt: import("@instantdb/core").DataAttrDef<Date, false, true, false>;
45
+ streamFinishedAt: import("@instantdb/core").DataAttrDef<Date, false, true, false>;
46
+ streamAbortReason: import("@instantdb/core").DataAttrDef<string, false, false, false>;
47
+ }, {}, unknown>;
48
+ event_parts: import("@instantdb/core").EntityDef<{
49
+ key: import("@instantdb/core").DataAttrDef<string, true, true, true>;
50
+ stepId: import("@instantdb/core").DataAttrDef<string, true, true, false>;
51
+ idx: import("@instantdb/core").DataAttrDef<number, true, true, false>;
52
+ type: import("@instantdb/core").DataAttrDef<string, false, true, false>;
53
+ part: import("@instantdb/core").DataAttrDef<any, false, false, false>;
54
+ metadata: import("@instantdb/core").DataAttrDef<any, false, false, false>;
55
+ updatedAt: import("@instantdb/core").DataAttrDef<Date, false, false, false>;
56
+ }, {}, unknown>;
57
+ event_trace_events: import("@instantdb/core").EntityDef<{
58
+ key: import("@instantdb/core").DataAttrDef<string, true, true, true>;
59
+ workflowRunId: import("@instantdb/core").DataAttrDef<string, true, true, false>;
60
+ seq: import("@instantdb/core").DataAttrDef<number, true, true, false>;
61
+ eventId: import("@instantdb/core").DataAttrDef<string, true, true, false>;
62
+ eventKind: import("@instantdb/core").DataAttrDef<string, true, true, false>;
63
+ eventAt: import("@instantdb/core").DataAttrDef<Date, false, false, false>;
64
+ ingestedAt: import("@instantdb/core").DataAttrDef<Date, false, false, false>;
65
+ orgId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
66
+ projectId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
67
+ contextKey: import("@instantdb/core").DataAttrDef<string, false, true, false>;
68
+ contextId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
69
+ executionId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
70
+ stepId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
71
+ contextEventId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
72
+ toolCallId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
73
+ partKey: import("@instantdb/core").DataAttrDef<string, false, true, false>;
74
+ partIdx: import("@instantdb/core").DataAttrDef<number, false, true, false>;
75
+ spanId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
76
+ parentSpanId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
77
+ isDeleted: import("@instantdb/core").DataAttrDef<boolean, false, false, false>;
78
+ aiProvider: import("@instantdb/core").DataAttrDef<string, false, true, false>;
79
+ aiModel: import("@instantdb/core").DataAttrDef<string, false, true, false>;
80
+ promptTokens: import("@instantdb/core").DataAttrDef<number, false, false, false>;
81
+ promptTokensCached: import("@instantdb/core").DataAttrDef<number, false, false, false>;
82
+ promptTokensUncached: import("@instantdb/core").DataAttrDef<number, false, false, false>;
83
+ completionTokens: import("@instantdb/core").DataAttrDef<number, false, false, false>;
84
+ totalTokens: import("@instantdb/core").DataAttrDef<number, false, false, false>;
85
+ latencyMs: import("@instantdb/core").DataAttrDef<number, false, false, false>;
86
+ cacheCostUsd: import("@instantdb/core").DataAttrDef<number, false, false, false>;
87
+ computeCostUsd: import("@instantdb/core").DataAttrDef<number, false, false, false>;
88
+ costUsd: import("@instantdb/core").DataAttrDef<number, false, false, false>;
89
+ payload: import("@instantdb/core").DataAttrDef<any, false, false, false>;
90
+ }, {}, unknown>;
91
+ event_trace_runs: import("@instantdb/core").EntityDef<{
92
+ workflowRunId: import("@instantdb/core").DataAttrDef<string, true, true, true>;
93
+ orgId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
94
+ projectId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
95
+ firstEventAt: import("@instantdb/core").DataAttrDef<Date, false, true, false>;
96
+ lastEventAt: import("@instantdb/core").DataAttrDef<Date, false, true, false>;
97
+ lastIngestedAt: import("@instantdb/core").DataAttrDef<Date, false, true, false>;
98
+ eventsCount: import("@instantdb/core").DataAttrDef<number, false, false, false>;
99
+ status: import("@instantdb/core").DataAttrDef<string, false, true, false>;
100
+ payload: import("@instantdb/core").DataAttrDef<any, false, false, false>;
101
+ }, {}, unknown>;
102
+ event_trace_spans: import("@instantdb/core").EntityDef<{
103
+ spanId: import("@instantdb/core").DataAttrDef<string, true, true, true>;
104
+ parentSpanId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
105
+ workflowRunId: import("@instantdb/core").DataAttrDef<string, true, true, false>;
106
+ executionId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
107
+ stepId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
108
+ kind: import("@instantdb/core").DataAttrDef<string, false, true, false>;
109
+ name: import("@instantdb/core").DataAttrDef<string, false, true, false>;
110
+ status: import("@instantdb/core").DataAttrDef<string, false, true, false>;
111
+ startedAt: import("@instantdb/core").DataAttrDef<Date, false, true, false>;
112
+ endedAt: import("@instantdb/core").DataAttrDef<Date, false, true, false>;
113
+ durationMs: import("@instantdb/core").DataAttrDef<number, false, false, false>;
114
+ payload: import("@instantdb/core").DataAttrDef<any, false, false, false>;
115
+ }, {}, unknown>;
116
+ document_documents: import("@instantdb/core").EntityDef<{
117
+ name: import("@instantdb/core").DataAttrDef<string, false, true, false>;
118
+ mimeType: import("@instantdb/core").DataAttrDef<string, false, false, false>;
119
+ size: import("@instantdb/core").DataAttrDef<number, false, false, false>;
120
+ ownerId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
121
+ orgId: import("@instantdb/core").DataAttrDef<string, false, true, false>;
122
+ createdAt: import("@instantdb/core").DataAttrDef<Date, false, true, false>;
123
+ processedAt: import("@instantdb/core").DataAttrDef<Date, false, true, false>;
124
+ updatedAt: import("@instantdb/core").DataAttrDef<Date, false, false, false>;
125
+ lastJobId: import("@instantdb/core").DataAttrDef<string, false, false, false>;
126
+ content: import("@instantdb/core").DataAttrDef<any, false, false, false>;
127
+ }, {}, unknown>;
128
+ }, {
129
+ contextItemsContext: {
130
+ readonly forward: {
131
+ readonly on: "event_items";
132
+ readonly has: "one";
133
+ readonly label: "context";
134
+ };
135
+ readonly reverse: {
136
+ readonly on: "event_contexts";
137
+ readonly has: "many";
138
+ readonly label: "items";
139
+ };
140
+ };
141
+ contextExecutionsContext: {
142
+ readonly forward: {
143
+ readonly on: "event_executions";
144
+ readonly has: "one";
145
+ readonly label: "context";
146
+ };
147
+ readonly reverse: {
148
+ readonly on: "event_contexts";
149
+ readonly has: "many";
150
+ readonly label: "executions";
151
+ };
152
+ };
153
+ contextCurrentExecution: {
154
+ readonly forward: {
155
+ readonly on: "event_contexts";
156
+ readonly has: "one";
157
+ readonly label: "currentExecution";
158
+ };
159
+ readonly reverse: {
160
+ readonly on: "event_executions";
161
+ readonly has: "one";
162
+ readonly label: "currentOf";
163
+ };
164
+ };
165
+ contextExecutionsTrigger: {
166
+ readonly forward: {
167
+ readonly on: "event_executions";
168
+ readonly has: "one";
169
+ readonly label: "trigger";
170
+ };
171
+ readonly reverse: {
172
+ readonly on: "event_items";
173
+ readonly has: "many";
174
+ readonly label: "executionsAsTrigger";
175
+ };
176
+ };
177
+ contextExecutionsReaction: {
178
+ readonly forward: {
179
+ readonly on: "event_executions";
180
+ readonly has: "one";
181
+ readonly label: "reaction";
182
+ };
183
+ readonly reverse: {
184
+ readonly on: "event_items";
185
+ readonly has: "many";
186
+ readonly label: "executionsAsReaction";
187
+ };
188
+ };
189
+ contextStepsExecution: {
190
+ readonly forward: {
191
+ readonly on: "event_steps";
192
+ readonly has: "one";
193
+ readonly label: "execution";
194
+ };
195
+ readonly reverse: {
196
+ readonly on: "event_executions";
197
+ readonly has: "many";
198
+ readonly label: "steps";
199
+ };
200
+ };
201
+ contextExecutionItems: {
202
+ readonly forward: {
203
+ readonly on: "event_items";
204
+ readonly has: "one";
205
+ readonly label: "execution";
206
+ };
207
+ readonly reverse: {
208
+ readonly on: "event_executions";
209
+ readonly has: "many";
210
+ readonly label: "items";
211
+ };
212
+ };
213
+ contextPartsStep: {
214
+ readonly forward: {
215
+ readonly on: "event_parts";
216
+ readonly has: "one";
217
+ readonly label: "step";
218
+ };
219
+ readonly reverse: {
220
+ readonly on: "event_steps";
221
+ readonly has: "many";
222
+ readonly label: "parts";
223
+ };
224
+ };
225
+ contextStepStream: {
226
+ readonly forward: {
227
+ readonly on: "event_steps";
228
+ readonly has: "one";
229
+ readonly label: "stream";
230
+ };
231
+ readonly reverse: {
232
+ readonly on: any;
233
+ readonly has: "many";
234
+ readonly label: "step";
235
+ };
236
+ };
237
+ contextExecutionActiveStream: {
238
+ readonly forward: {
239
+ readonly on: "event_executions";
240
+ readonly has: "one";
241
+ readonly label: "activeStream";
242
+ };
243
+ readonly reverse: {
244
+ readonly on: any;
245
+ readonly has: "many";
246
+ readonly label: "activeOf";
247
+ };
248
+ };
249
+ contextExecutionLastStream: {
250
+ readonly forward: {
251
+ readonly on: "event_executions";
252
+ readonly has: "one";
253
+ readonly label: "lastStream";
254
+ };
255
+ readonly reverse: {
256
+ readonly on: any;
257
+ readonly has: "many";
258
+ readonly label: "lastOf";
259
+ };
260
+ };
261
+ documentFile: {
262
+ readonly forward: {
263
+ readonly on: "document_documents";
264
+ readonly has: "one";
265
+ readonly label: "file";
266
+ };
267
+ readonly reverse: {
268
+ readonly on: "$files";
269
+ readonly has: "one";
270
+ readonly label: "document";
271
+ };
272
+ };
273
+ }, import("@instantdb/core").RoomsDef, {}, "events", "events">;
package/dist/schema.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { i } from "@instantdb/core";
2
2
  import { domain } from "@ekairos/domain";
3
3
  export const eventsDomain = domain("events")
4
- .schema({
4
+ .withSchema({
5
5
  entities: {
6
6
  event_contexts: i.entity({
7
7
  createdAt: i.date(),
@@ -2,6 +2,7 @@ import type { UIMessageChunk } from "ai";
2
2
  import type { ContextEnvironment } from "../context.config.js";
3
3
  import type { ContextRuntime } from "../context.runtime.js";
4
4
  import type { ContextExecution, ContextItem, ContextIdentifier, StoredContext, ContextStatus } from "../context.store.js";
5
+ import { type PersistedContextStepStreamSession } from "./stream.steps.js";
5
6
  type RuntimeParams<Env extends ContextEnvironment = ContextEnvironment> = {
6
7
  runtime: ContextRuntime<Env>;
7
8
  };
@@ -9,6 +10,18 @@ export type ContextReviewRequest = {
9
10
  toolCallId: string;
10
11
  toolName?: string;
11
12
  };
13
+ type ContextStepPatch = {
14
+ status?: "running" | "completed" | "failed";
15
+ kind?: "message" | "action_execute" | "action_result";
16
+ actionName?: string;
17
+ actionInput?: unknown;
18
+ actionOutput?: unknown;
19
+ actionError?: string;
20
+ actionRequests?: any;
21
+ actionResults?: any;
22
+ continueLoop?: boolean;
23
+ errorText?: string;
24
+ };
12
25
  /**
13
26
  * Initializes/ensures the story context exists.
14
27
  *
@@ -97,6 +110,18 @@ export declare function updateExecutionWorkflowRun(params: {
97
110
  executionId: string;
98
111
  workflowRunId: string;
99
112
  }): Promise<void>;
113
+ export declare function openReactionStep<C>(params: {
114
+ runtime: ContextRuntime<ContextEnvironment>;
115
+ contextIdentifier: ContextIdentifier;
116
+ content: C;
117
+ executionId: string;
118
+ iteration: number;
119
+ }): Promise<{
120
+ stepId: string;
121
+ stream: PersistedContextStepStreamSession;
122
+ context: StoredContext<C>;
123
+ events: ContextItem[];
124
+ }>;
100
125
  export declare function createContextStep(params: {
101
126
  runtime: ContextRuntime<ContextEnvironment>;
102
127
  executionId: string;
@@ -110,19 +135,21 @@ export declare function updateContextStep(params: {
110
135
  executionId?: string;
111
136
  contextId?: string;
112
137
  iteration?: number;
113
- patch: {
114
- status?: "running" | "completed" | "failed";
115
- kind?: "message" | "action_execute" | "action_result";
116
- actionName?: string;
117
- actionInput?: unknown;
118
- actionOutput?: unknown;
119
- actionError?: string;
120
- actionRequests?: any;
121
- actionResults?: any;
122
- continueLoop?: boolean;
123
- errorText?: string;
124
- };
138
+ patch: ContextStepPatch;
125
139
  }): Promise<void>;
140
+ export declare function finalizeReactionStep(params: {
141
+ runtime: ContextRuntime<ContextEnvironment>;
142
+ session?: PersistedContextStepStreamSession | null;
143
+ stepId: string;
144
+ executionId?: string;
145
+ contextId?: string;
146
+ iteration?: number;
147
+ patch: ContextStepPatch;
148
+ reactionEventId?: string;
149
+ reactionEvent?: ContextItem;
150
+ }): Promise<{
151
+ reactionEvent?: ContextItem;
152
+ }>;
126
153
  export declare function linkItemToExecutionStep(params: {
127
154
  runtime: ContextRuntime<ContextEnvironment>;
128
155
  itemId: string;
@@ -136,4 +163,16 @@ export declare function saveContextPartsStep(params: {
136
163
  iteration?: number;
137
164
  parts: any[];
138
165
  }): Promise<void>;
166
+ export declare function saveContextPartsAndUpdateReaction(params: {
167
+ runtime: ContextRuntime<ContextEnvironment>;
168
+ stepId: string;
169
+ executionId?: string;
170
+ contextId?: string;
171
+ iteration?: number;
172
+ parts: any[];
173
+ reactionEventId: string;
174
+ reactionEvent: ContextItem;
175
+ }): Promise<{
176
+ reactionEvent: ContextItem;
177
+ }>;
139
178
  export {};
@@ -2,6 +2,7 @@ import { getContextRuntimeServices } from "../context.runtime.js";
2
2
  import { OUTPUT_ITEM_TYPE, WEB_CHANNEL } from "../context.events.js";
3
3
  import { writeContextTraceEvents } from "./trace.steps.js";
4
4
  import { getClientResumeHookUrl, toolApprovalHookToken, toolApprovalWebhookToken, } from "../context.hooks.js";
5
+ import { createPersistedContextStepStreamForRuntime, finalizePersistedContextStepStreamForRuntime, } from "./stream.steps.js";
5
6
  async function getRuntimeAndEnv(params) {
6
7
  const env = params.runtime.env;
7
8
  const runtime = await getContextRuntimeServices(params.runtime);
@@ -510,6 +511,27 @@ export async function updateExecutionWorkflowRun(params) {
510
511
  ]);
511
512
  }
512
513
  }
514
+ export async function openReactionStep(params) {
515
+ "use step";
516
+ const { runtime } = await getRuntimeAndEnv(params);
517
+ const { store } = runtime;
518
+ const step = await store.createStep({
519
+ executionId: params.executionId,
520
+ iteration: params.iteration,
521
+ });
522
+ const stream = await createPersistedContextStepStreamForRuntime(runtime, {
523
+ executionId: params.executionId,
524
+ stepId: step.id,
525
+ });
526
+ const context = await store.updateContextContent(params.contextIdentifier, params.content);
527
+ const events = await store.getItems(params.contextIdentifier);
528
+ return {
529
+ stepId: step.id,
530
+ stream,
531
+ context,
532
+ events,
533
+ };
534
+ }
513
535
  export async function createContextStep(params) {
514
536
  "use step";
515
537
  const { runtime } = await getRuntimeAndEnv(params);
@@ -560,6 +582,74 @@ export async function updateContextStep(params) {
560
582
  ]);
561
583
  }
562
584
  }
585
+ export async function finalizeReactionStep(params) {
586
+ "use step";
587
+ const { runtime, env } = await getRuntimeAndEnv(params);
588
+ const { store, db } = runtime;
589
+ if (params.session) {
590
+ await finalizePersistedContextStepStreamForRuntime({
591
+ runtime,
592
+ session: params.session,
593
+ mode: "close",
594
+ });
595
+ }
596
+ await store.updateStep(params.stepId, {
597
+ ...params.patch,
598
+ updatedAt: new Date(),
599
+ });
600
+ let savedReaction;
601
+ if (params.reactionEventId && params.reactionEvent) {
602
+ savedReaction = await store.updateItem(params.reactionEventId, params.reactionEvent);
603
+ }
604
+ const { runId } = await resolveWorkflowRunId({
605
+ env,
606
+ db,
607
+ executionId: params.executionId,
608
+ });
609
+ if (runId) {
610
+ const events = [
611
+ {
612
+ workflowRunId: runId,
613
+ eventId: `context_step:${String(params.stepId)}`,
614
+ eventKind: "context.step",
615
+ eventAt: new Date().toISOString(),
616
+ contextId: params.contextId,
617
+ executionId: params.executionId,
618
+ stepId: String(params.stepId),
619
+ payload: {
620
+ status: params.patch.status,
621
+ kind: params.patch.kind,
622
+ actionName: params.patch.actionName,
623
+ actionInput: params.patch.actionInput,
624
+ actionOutput: params.patch.actionOutput,
625
+ actionError: params.patch.actionError,
626
+ iteration: params.iteration,
627
+ actionRequests: params.patch.actionRequests,
628
+ actionResults: params.patch.actionResults,
629
+ continueLoop: params.patch.continueLoop,
630
+ errorText: params.patch.errorText,
631
+ },
632
+ },
633
+ ];
634
+ if (savedReaction) {
635
+ events.push({
636
+ workflowRunId: runId,
637
+ eventId: `context_item:${String(savedReaction.id)}`,
638
+ eventKind: "context.item",
639
+ eventAt: new Date().toISOString(),
640
+ contextId: params.contextId,
641
+ executionId: params.executionId,
642
+ contextEventId: String(savedReaction.id),
643
+ payload: {
644
+ ...savedReaction,
645
+ direction: inferDirection(savedReaction),
646
+ },
647
+ });
648
+ }
649
+ await maybeWriteTraceEvents(env, events);
650
+ }
651
+ return { reactionEvent: savedReaction };
652
+ }
563
653
  export async function linkItemToExecutionStep(params) {
564
654
  "use step";
565
655
  const { runtime } = await getRuntimeAndEnv(params);
@@ -596,3 +686,50 @@ export async function saveContextPartsStep(params) {
596
686
  await maybeWriteTraceEvents(env, events);
597
687
  }
598
688
  }
689
+ export async function saveContextPartsAndUpdateReaction(params) {
690
+ "use step";
691
+ const { runtime, env } = await getRuntimeAndEnv(params);
692
+ const { store, db } = runtime;
693
+ await store.saveStepParts({ stepId: params.stepId, parts: params.parts });
694
+ const savedReaction = await store.updateItem(params.reactionEventId, params.reactionEvent);
695
+ const { runId } = await resolveWorkflowRunId({
696
+ env,
697
+ db,
698
+ executionId: params.executionId,
699
+ });
700
+ if (runId) {
701
+ const events = [];
702
+ if (params.parts?.length) {
703
+ for (let idx = 0; idx < params.parts.length; idx += 1) {
704
+ const part = params.parts[idx];
705
+ events.push({
706
+ workflowRunId: runId,
707
+ eventId: `context_part:${String(params.stepId)}:${idx}`,
708
+ eventKind: "context.part",
709
+ eventAt: new Date().toISOString(),
710
+ contextId: params.contextId,
711
+ executionId: params.executionId,
712
+ stepId: String(params.stepId),
713
+ partKey: `${String(params.stepId)}:${idx}`,
714
+ partIdx: idx,
715
+ payload: part,
716
+ });
717
+ }
718
+ }
719
+ events.push({
720
+ workflowRunId: runId,
721
+ eventId: `context_item:${String(savedReaction.id)}`,
722
+ eventKind: "context.item",
723
+ eventAt: new Date().toISOString(),
724
+ contextId: params.contextId,
725
+ executionId: params.executionId,
726
+ contextEventId: String(savedReaction.id),
727
+ payload: {
728
+ ...savedReaction,
729
+ direction: inferDirection(savedReaction),
730
+ },
731
+ });
732
+ await maybeWriteTraceEvents(env, events);
733
+ }
734
+ return { reactionEvent: savedReaction };
735
+ }
@@ -20,12 +20,27 @@ export type PersistedContextStepStreamSession = {
20
20
  executionId: string;
21
21
  stepId: string;
22
22
  };
23
+ export declare function createPersistedContextStepStreamForRuntime(runtime: {
24
+ db?: any;
25
+ }, params: {
26
+ executionId: string;
27
+ stepId: string;
28
+ clientId?: string;
29
+ }): Promise<PersistedContextStepStreamSession>;
23
30
  export declare function createPersistedContextStepStream(params: {
24
31
  runtime: ContextRuntime<ContextEnvironment>;
25
32
  executionId: string;
26
33
  stepId: string;
27
34
  clientId?: string;
28
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>;
29
44
  export declare function closePersistedContextStepStream(params: {
30
45
  runtime: ContextRuntime<ContextEnvironment>;
31
46
  session: PersistedContextStepStreamSession;