@ekairos/events 1.22.35-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 (73) hide show
  1. package/README.md +5 -3
  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 +60 -52
  16. package/dist/context.engine.js +506 -297
  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 +14 -4
  25. package/dist/context.runtime.js +21 -3
  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 +55 -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 -9
  48. package/dist/reactors/ai-sdk.reactor.js +6 -9
  49. package/dist/reactors/ai-sdk.step.d.ts +4 -5
  50. package/dist/reactors/ai-sdk.step.js +24 -17
  51. package/dist/reactors/scripted.reactor.d.ts +7 -4
  52. package/dist/reactors/types.d.ts +19 -10
  53. package/dist/runtime.d.ts +6 -0
  54. package/dist/runtime.js +9 -0
  55. package/dist/runtime.step.js +1 -1
  56. package/dist/schema.d.ts +268 -2
  57. package/dist/schema.js +4 -9
  58. package/dist/steps/do-context-stream-step.js +4 -4
  59. package/dist/steps/durable.steps.d.ts +28 -0
  60. package/dist/steps/durable.steps.js +34 -0
  61. package/dist/steps/store.steps.d.ts +64 -22
  62. package/dist/steps/store.steps.js +192 -35
  63. package/dist/steps/stream.steps.d.ts +32 -0
  64. package/dist/steps/stream.steps.js +124 -6
  65. package/dist/steps/trace.steps.d.ts +4 -4
  66. package/dist/steps/trace.steps.js +21 -6
  67. package/dist/stores/instant.store.d.ts +11 -11
  68. package/dist/stores/instant.store.js +136 -6
  69. package/dist/tools-to-model-tools.d.ts +4 -2
  70. package/dist/tools-to-model-tools.js +30 -11
  71. package/package.json +18 -7
  72. package/dist/context.toolcalls.d.ts +0 -60
  73. package/dist/context.toolcalls.js +0 -117
@@ -1,43 +1,95 @@
1
1
  import { ContextEngine, } from "./context.engine.js";
2
2
  import { registerContext } from "./context.registry.js";
3
+ import { eventsDomain } from "./schema.js";
3
4
  function isDynamicModelSelector(model) {
4
5
  return typeof model === "function" && model.length >= 1;
5
6
  }
7
+ function stringifyContextContent(content) {
8
+ try {
9
+ return JSON.stringify(content ?? null, null, 2);
10
+ }
11
+ catch {
12
+ return String(content);
13
+ }
14
+ }
15
+ function buildDefaultContextPrompt(params) {
16
+ const sections = [];
17
+ sections.push(`Content:\n${stringifyContextContent(params.content)}`);
18
+ if (params.resources?.length) {
19
+ sections.push(`Resources:\n${stringifyContextContent(params.resources)}`);
20
+ }
21
+ if (params.description) {
22
+ sections.push(`Description:\n${params.description}`);
23
+ }
24
+ if (params.goal) {
25
+ sections.push(`Goal:\n${params.goal}`);
26
+ }
27
+ return sections.join("\n\n");
28
+ }
6
29
  export function context(config) {
7
30
  class FunctionalContext extends ContextEngine {
8
31
  constructor() {
9
32
  super(config.opts, config.reactor);
10
33
  this.__config = config;
11
34
  }
12
- async initialize(contextValue, env) {
13
- return config.context(contextValue, env);
35
+ async initialize(contextValue, env, runtime) {
36
+ return config.context(contextValue, env, runtime);
14
37
  }
15
- async expandEvents(events, contextValue, env) {
38
+ async expandEvents(events, contextValue, env, runtime) {
16
39
  if (config.expandEvents)
17
- return config.expandEvents(events, contextValue, env);
18
- return super.expandEvents(events, contextValue, env);
40
+ return config.expandEvents(events, contextValue, env, runtime);
41
+ return super.expandEvents(events, contextValue, env, runtime);
19
42
  }
20
- async buildSystemPrompt(contextValue, env) {
43
+ async buildSystemPrompt(contextValue, env, runtime) {
21
44
  if (config.narrative)
22
- return config.narrative(contextValue, env);
23
- throw new Error("Context config is missing narrative()");
45
+ return config.narrative(contextValue, env, runtime);
46
+ const content = contextValue.content;
47
+ const description = contextValue.description ??
48
+ (config.description
49
+ ? await config.description(content, contextValue, env, runtime)
50
+ : null);
51
+ const goal = contextValue.goal ??
52
+ (config.goal
53
+ ? await config.goal(content, contextValue, env, runtime)
54
+ : null);
55
+ return buildDefaultContextPrompt({
56
+ content,
57
+ resources: contextValue.resources ?? [],
58
+ description,
59
+ goal,
60
+ });
61
+ }
62
+ async describeContext(content, contextValue, env, runtime) {
63
+ if (!config.description)
64
+ return null;
65
+ return config.description(content, contextValue, env, runtime);
66
+ }
67
+ async defineGoal(content, contextValue, env, runtime) {
68
+ if (!config.goal)
69
+ return null;
70
+ return config.goal(content, contextValue, env, runtime);
24
71
  }
25
- async buildSkills(contextValue, env) {
72
+ async defineResources(content, contextValue, env, runtime) {
73
+ if (!config.resources)
74
+ return [];
75
+ return config.resources({ content, context: contextValue, env, runtime });
76
+ }
77
+ async buildSkills(contextValue, env, runtime) {
26
78
  if (config.skills)
27
- return config.skills(contextValue, env);
79
+ return config.skills(contextValue, env, runtime);
28
80
  return [];
29
81
  }
30
- async buildTools(contextValue, env) {
82
+ async buildTools(contextValue, env, runtime) {
31
83
  if (config.actions)
32
- return config.actions(contextValue, env);
84
+ return config.actions(contextValue, env, runtime);
33
85
  if (config.tools)
34
- return config.tools(contextValue, env);
86
+ return config.tools(contextValue, env, runtime);
35
87
  throw new Error("Context config is missing actions()");
36
88
  }
37
- getModel(contextValue, env) {
89
+ getModel(contextValue, env, runtime) {
38
90
  if (isDynamicModelSelector(config.model))
39
- return config.model(contextValue, env);
40
- return config.model ?? super.getModel(contextValue, env);
91
+ return config.model(contextValue, env, runtime);
92
+ return config.model ?? super.getModel(contextValue, env, runtime);
41
93
  }
42
94
  async shouldContinue(args) {
43
95
  if (config.shouldContinue)
@@ -50,27 +102,49 @@ export function context(config) {
50
102
  }
51
103
  function assertConfigComplete(config) {
52
104
  if (!config.context) {
53
- throw new Error("createContext: you must define context() before building the Context.");
105
+ throw new Error("createContext: you must define content() before building the Context.");
54
106
  }
55
- if (!config.narrative) {
56
- throw new Error("createContext: you must define narrative() before building the Context.");
107
+ if (!config.narrative && (!config.description || !config.goal)) {
108
+ throw new Error("createContext: you must define description() and goal() before building the Context.");
57
109
  }
58
110
  if (!config.actions && !config.tools) {
59
111
  throw new Error("createContext: you must define actions() before building the Context.");
60
112
  }
61
113
  }
62
- export function createContext(key) {
114
+ export function createContext(keyOrDomain, maybeKey) {
115
+ const requiredDomain = typeof keyOrDomain === "string" ? eventsDomain : keyOrDomain;
116
+ const key = typeof keyOrDomain === "string" ? keyOrDomain : maybeKey;
117
+ if (!key) {
118
+ throw new Error("createContext: key is required.");
119
+ }
63
120
  const initializeBuilder = (initializer) => {
64
- const typedInitializer = (ctx, env) => initializer(ctx, env);
121
+ const typedInitializer = (ctx, env, runtime) => initializer(ctx, env, runtime);
65
122
  const fluentState = {
66
123
  context: typedInitializer,
67
124
  };
125
+ const resourceFactories = [];
126
+ const refreshResourcesConfig = () => {
127
+ fluentState.resources = async (params) => {
128
+ const resources = [];
129
+ for (const factory of resourceFactories) {
130
+ const result = await factory(params);
131
+ if (!result)
132
+ continue;
133
+ const list = Array.isArray(result) ? result : [result];
134
+ resources.push(...list);
135
+ }
136
+ return resources;
137
+ };
138
+ };
68
139
  let cached = null;
69
140
  const getOrBuild = () => {
70
141
  assertConfigComplete(fluentState);
71
142
  if (!cached) {
72
143
  const config = fluentState;
73
- cached = Object.assign(context(config), { __contextKey: key });
144
+ cached = Object.assign(context(config), {
145
+ __contextKey: key,
146
+ __contextDomain: requiredDomain,
147
+ });
74
148
  registerContext(key, () => cached);
75
149
  }
76
150
  return cached;
@@ -89,6 +163,30 @@ export function createContext(key) {
89
163
  fluentState.narrative = system;
90
164
  return builder;
91
165
  },
166
+ description(description) {
167
+ fluentState.description = description;
168
+ return builder;
169
+ },
170
+ goal(goal) {
171
+ fluentState.goal = goal;
172
+ return builder;
173
+ },
174
+ resources(resources) {
175
+ resourceFactories.push(resources);
176
+ refreshResourcesConfig();
177
+ return builder;
178
+ },
179
+ resource(resource) {
180
+ resourceFactories.push(async (params) => {
181
+ if (typeof resource === "function") {
182
+ const resolved = await resource(params);
183
+ return resolved ? [resolved] : [];
184
+ }
185
+ return [resource];
186
+ });
187
+ refreshResourcesConfig();
188
+ return builder;
189
+ },
92
190
  skills(skillsFactory) {
93
191
  fluentState.skills = skillsFactory;
94
192
  return builder;
@@ -117,12 +215,8 @@ export function createContext(key) {
117
215
  fluentState.opts = options;
118
216
  return builder;
119
217
  },
120
- react(triggerEvent, params) {
121
- return getOrBuild().react(triggerEvent, params);
122
- },
123
- stream(triggerEvent, params) {
124
- return builder.react(triggerEvent, params);
125
- },
218
+ react: ((triggerEvent, params) => getOrBuild().react(triggerEvent, params)),
219
+ stream: ((triggerEvent, params) => builder.react(triggerEvent, params)),
126
220
  register() {
127
221
  getOrBuild();
128
222
  },
@@ -137,6 +231,7 @@ export function createContext(key) {
137
231
  return builder;
138
232
  };
139
233
  return {
234
+ content: initializeBuilder,
140
235
  context: initializeBuilder,
141
236
  initialize: initializeBuilder,
142
237
  };
@@ -4,6 +4,7 @@ export type ContextEnvironment = Record<string, unknown>;
4
4
  export type ContextRuntime = {
5
5
  store: ContextStore;
6
6
  db: any;
7
+ env?: ContextEnvironment;
7
8
  domain?: ConcreteDomain<any, any>;
8
9
  };
9
- export declare function coerceContextRuntime(value: any): Promise<ContextRuntime>;
10
+ export declare function coerceContextRuntime(value: any, env?: ContextEnvironment): Promise<ContextRuntime>;
@@ -1,10 +1,14 @@
1
1
  const runtimeByDb = new WeakMap();
2
- export async function coerceContextRuntime(value) {
2
+ export async function coerceContextRuntime(value, env) {
3
3
  if (!value) {
4
4
  throw new Error("Context runtime resolver returned no value.");
5
5
  }
6
+ const resolvedEnv = env ??
7
+ (typeof value === "object" && value !== null
8
+ ? value.env
9
+ : undefined);
6
10
  if (typeof value === "object" && value.store) {
7
- return value;
11
+ return resolvedEnv ? { ...value, env: resolvedEnv } : value;
8
12
  }
9
13
  const dbCandidate = typeof value === "object" && value !== null && "db" in value
10
14
  ? value.db
@@ -15,12 +19,13 @@ export async function coerceContextRuntime(value) {
15
19
  if (typeof dbCandidate === "object" && dbCandidate !== null) {
16
20
  const cached = runtimeByDb.get(dbCandidate);
17
21
  if (cached)
18
- return cached;
22
+ return resolvedEnv ? { ...cached, env: resolvedEnv } : cached;
19
23
  }
20
24
  const { InstantStore } = await import("./stores/instant.store.js");
21
25
  const runtime = {
22
26
  store: new InstantStore(dbCandidate),
23
27
  db: dbCandidate,
28
+ env: resolvedEnv,
24
29
  domain: typeof value === "object" ? value.domain : undefined,
25
30
  };
26
31
  if (typeof dbCandidate === "object" && dbCandidate !== null) {
@@ -8,8 +8,6 @@ export declare const EXECUTION_STATUSES: readonly ["executing", "completed", "fa
8
8
  export type ExecutionStatus = (typeof EXECUTION_STATUSES)[number];
9
9
  export declare const STEP_STATUSES: readonly ["running", "completed", "failed"];
10
10
  export type StepStatus = (typeof STEP_STATUSES)[number];
11
- export declare const STEP_KINDS: readonly ["message", "action_execute", "action_result"];
12
- export type StepKind = (typeof STEP_KINDS)[number];
13
11
  export declare const ITEM_STATUSES: readonly ["stored", "pending", "completed"];
14
12
  export type ItemStatus = (typeof ITEM_STATUSES)[number];
15
13
  export declare const ITEM_TYPES: readonly ["input", "output"];
@@ -21,11 +19,11 @@ export type TraceEventKind = (typeof TRACE_EVENT_KINDS)[number];
21
19
  export declare const STREAM_LIFECYCLE_CHUNK_TYPES: readonly ["chunk.start", "chunk.start_step", "chunk.finish_step", "chunk.finish"];
22
20
  export declare const STREAM_TEXT_CHUNK_TYPES: readonly ["chunk.text_start", "chunk.text_delta", "chunk.text_end"];
23
21
  export declare const STREAM_REASONING_CHUNK_TYPES: readonly ["chunk.reasoning_start", "chunk.reasoning_delta", "chunk.reasoning_end"];
24
- export declare const STREAM_ACTION_CHUNK_TYPES: readonly ["chunk.action_input_start", "chunk.action_input_delta", "chunk.action_input_available", "chunk.action_output_available", "chunk.action_output_error"];
22
+ export declare const STREAM_ACTION_CHUNK_TYPES: readonly ["chunk.action_started", "chunk.action_input_delta", "chunk.action_completed", "chunk.action_failed"];
25
23
  export declare const STREAM_SOURCE_CHUNK_TYPES: readonly ["chunk.source_url", "chunk.source_document", "chunk.file"];
26
24
  export declare const STREAM_METADATA_CHUNK_TYPES: readonly ["chunk.message_metadata", "chunk.response_metadata"];
27
25
  export declare const STREAM_ERROR_CHUNK_TYPES: readonly ["chunk.error", "chunk.unknown"];
28
- export declare const CONTEXT_STREAM_CHUNK_TYPES: readonly ["chunk.start", "chunk.start_step", "chunk.finish_step", "chunk.finish", "chunk.text_start", "chunk.text_delta", "chunk.text_end", "chunk.reasoning_start", "chunk.reasoning_delta", "chunk.reasoning_end", "chunk.action_input_start", "chunk.action_input_delta", "chunk.action_input_available", "chunk.action_output_available", "chunk.action_output_error", "chunk.source_url", "chunk.source_document", "chunk.file", "chunk.message_metadata", "chunk.response_metadata", "chunk.error", "chunk.unknown"];
26
+ export declare const CONTEXT_STREAM_CHUNK_TYPES: readonly ["chunk.start", "chunk.start_step", "chunk.finish_step", "chunk.finish", "chunk.text_start", "chunk.text_delta", "chunk.text_end", "chunk.reasoning_start", "chunk.reasoning_delta", "chunk.reasoning_end", "chunk.action_started", "chunk.action_input_delta", "chunk.action_completed", "chunk.action_failed", "chunk.source_url", "chunk.source_document", "chunk.file", "chunk.message_metadata", "chunk.response_metadata", "chunk.error", "chunk.unknown"];
29
27
  export type ContextStreamChunkType = (typeof CONTEXT_STREAM_CHUNK_TYPES)[number];
30
28
  export declare function isContextStreamChunkType(value: string): value is ContextStreamChunkType;
31
29
  export type ContextTransition = Transition<"open_idle" | "open_streaming" | "closed", "open_idle" | "open_streaming" | "closed">;
@@ -1,11 +1,6 @@
1
1
  export const CONTEXT_STATUSES = ["open_idle", "open_streaming", "closed"];
2
2
  export const EXECUTION_STATUSES = ["executing", "completed", "failed"];
3
3
  export const STEP_STATUSES = ["running", "completed", "failed"];
4
- export const STEP_KINDS = [
5
- "message",
6
- "action_execute",
7
- "action_result",
8
- ];
9
4
  export const ITEM_STATUSES = ["stored", "pending", "completed"];
10
5
  export const ITEM_TYPES = [
11
6
  "input",
@@ -41,11 +36,10 @@ export const STREAM_REASONING_CHUNK_TYPES = [
41
36
  "chunk.reasoning_end",
42
37
  ];
43
38
  export const STREAM_ACTION_CHUNK_TYPES = [
44
- "chunk.action_input_start",
39
+ "chunk.action_started",
45
40
  "chunk.action_input_delta",
46
- "chunk.action_input_available",
47
- "chunk.action_output_available",
48
- "chunk.action_output_error",
41
+ "chunk.action_completed",
42
+ "chunk.action_failed",
49
43
  ];
50
44
  export const STREAM_SOURCE_CHUNK_TYPES = [
51
45
  "chunk.source_url",
package/dist/context.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { ContextEngine, type ContextOptions, type ContextStreamOptions, type ShouldContinue, type ContextShouldContinueArgs, type ContextReactParams, type ContextReactResult, type ContextWorkflowRun, type ContextDurableWorkflowPayload, type ContextDurableWorkflowFunction, type ContextModelInit, type ContextTool, type ContextToolExecuteContext, runContextReactionDirect, } from "./context.engine.js";
2
- export { context, createContext, type ContextConfig, type ContextInstance, type RegistrableContextBuilder, } from "./context.builder.js";
1
+ export { ContextEngine, type ContextOptions, type ContextStreamOptions, type ShouldContinue, type ContextShouldContinueArgs, type ContextReactParams, type ContextDirectReactParams, type ContextDurableReactParams, type ContextReactResult, type ContextReactBase, type ContextReactFinalResult, type ContextDirectRun, type ContextReactRun, type ContextWorkflowRun, type ContextDurableWorkflowPayload, type ContextDurableWorkflowFunction, type ContextModelInit, type ContextToolExecuteContext, runContextReactionDirect, } from "./context.engine.js";
2
+ export { context, createContext, type ContextConfig, type ContextInstance, type ContextResourcesParams, type RegistrableContextBuilder, } from "./context.builder.js";
3
+ export { defineAction, action, type ContextAction, type ContextActionBase, type ContextActionExecuteParams, type AnyContextAction, type ContextActionDefinition, type ContextActionExecute, type DefineContextActionDefinition, type DefineContextActionExecute, type LegacyContextActionDefinition, type LegacyContextActionExecute, type ContextActionInput, type ContextActionOutput, type ContextProviderDefinedAction, type ContextActionSchema, type ContextTool, } from "./context.action.js";
3
4
  export { createAiSdkReactor, createScriptedReactor, type CreateAiSdkReactorOptions, type CreateScriptedReactorOptions, type ScriptedReactorStep, type ContextReactor, type ContextReactorParams, type ContextReactionResult, type ContextActionRequest, type ContextReactionLLM, } from "./context.reactor.js";
4
5
  export type { ContextSkillPackage, ContextSkillPackageFile, } from "./context.skill.js";
@@ -1,17 +1,22 @@
1
- import type { ModelMessage, Tool, UIMessageChunk } from "ai";
1
+ import type { ModelMessage, UIMessageChunk } from "ai";
2
+ import type { DomainLike } from "@ekairos/domain";
2
3
  import type { ContextEnvironment } from "./context.config.js";
3
- import type { ContextRuntime } from "./context.runtime.js";
4
- import type { ContextExecution, ContextItem, ContextIdentifier, StoredContext } from "./context.store.js";
4
+ import type { ContextTool } from "./context.action.js";
5
+ import type { ContextRuntime, ContextRuntimeHandleForDomain, ContextRuntimeForDomain } from "./context.runtime.js";
6
+ import { eventsDomain } from "./schema.js";
7
+ import type { ContextExecution, ContextItem, ContextIdentifier, ContextResource, StoredContext } from "./context.store.js";
5
8
  import type { ContextSkillPackage } from "./context.skill.js";
6
9
  import { type ContextReactor } from "./context.reactor.js";
7
10
  import { getClientResumeHookUrl, toolApprovalHookToken, toolApprovalWebhookToken } from "./context.hooks.js";
8
- export interface ContextOptions<Context = any, Env extends ContextEnvironment = ContextEnvironment> {
11
+ export interface ContextOptions<Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain> {
9
12
  onContextCreated?: (args: {
10
13
  env: Env;
14
+ runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>;
11
15
  context: StoredContext<Context>;
12
16
  }) => void | Promise<void>;
13
17
  onContextUpdated?: (args: {
14
18
  env: Env;
19
+ runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>;
15
20
  context: StoredContext<Context>;
16
21
  }) => void | Promise<void>;
17
22
  onEventCreated?: (event: ContextItem) => void | Promise<void>;
@@ -23,7 +28,7 @@ type ContextBenchmarkRecorder = {
23
28
  add?(name: string, value: number): void;
24
29
  getCurrentStage?(): string | undefined;
25
30
  };
26
- export declare function runContextReactionDirect<Context, Env extends ContextEnvironment = ContextEnvironment>(context: ContextEngine<Context, Env>, triggerEvent: ContextItem, params: ContextReactParams<Env>): Promise<ContextReactResult<Context>>;
31
+ export declare function runContextReactionDirect<Context, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>>(context: ContextEngine<Context, Env, RequiredDomain>, triggerEvent: ContextItem, params: ContextReactParams<Env, RequiredDomain, Runtime>): Promise<ContextReactResult<Context>>;
27
32
  export interface ContextStreamOptions {
28
33
  /**
29
34
  * Maximum loop iterations (LLM call → tool execution → repeat).
@@ -45,14 +50,6 @@ export interface ContextStreamOptions {
45
50
  * Default: true.
46
51
  */
47
52
  sendFinish?: boolean;
48
- /**
49
- * If true, the story loop runs silently (no UI streaming output).
50
- *
51
- * Persistence (contexts/events/executions) still happens normally.
52
- *
53
- * Default: false.
54
- */
55
- silent?: boolean;
56
53
  /**
57
54
  * Optional writable stream used by direct/non-durable execution.
58
55
  *
@@ -68,8 +65,8 @@ export interface ContextStreamOptions {
68
65
  * be a `"use-step"` function (so it can be serialized by reference).
69
66
  */
70
67
  export type ContextModelInit = string | (() => Promise<any>);
71
- export type ContextReactParams<Env extends ContextEnvironment = ContextEnvironment> = {
72
- runtime: ContextRuntime<Env>;
68
+ export type ContextReactParams<Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = {
69
+ runtime: ContextRuntimeForDomain<Runtime, RequiredDomain>;
73
70
  /**
74
71
  * Context selector (exclusive: `{ id }` OR `{ key }`).
75
72
  * - `{ id }` resolves a concrete context id.
@@ -88,33 +85,49 @@ export type ContextReactParams<Env extends ContextEnvironment = ContextEnvironme
88
85
  trigger: ContextItem;
89
86
  reaction: ContextItem;
90
87
  execution: ContextExecution;
88
+ returnValueHookToken?: string | null;
91
89
  };
90
+ __initialContent?: unknown;
92
91
  __benchmark?: ContextBenchmarkRecorder;
93
92
  };
94
- export type ContextReactResult<Context = any> = {
93
+ export type ContextDurableReactParams<Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = Omit<ContextReactParams<Env, RequiredDomain, Runtime>, "durable"> & {
94
+ durable?: true | undefined;
95
+ };
96
+ export type ContextDirectReactParams<Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = Omit<ContextReactParams<Env, RequiredDomain, Runtime>, "durable"> & {
97
+ durable: false;
98
+ };
99
+ export type ContextReactBase<Context = any> = {
95
100
  context: StoredContext<Context>;
96
101
  trigger: ContextItem;
97
102
  reaction: ContextItem;
98
103
  execution: ContextExecution;
99
- run?: ContextWorkflowRun<Context>;
100
104
  };
105
+ export type ContextReactFinalResult<Context = any> = ContextReactBase<Context>;
106
+ export type ContextDirectRun<Context = any> = Promise<ContextReactFinalResult<Context>>;
101
107
  export type ContextWorkflowRun<Context = any> = {
102
108
  runId: string;
103
109
  status: Promise<"pending" | "running" | "completed" | "failed" | "cancelled">;
104
- returnValue: Promise<ContextReactResult<Context>>;
110
+ returnValue: Promise<ContextReactFinalResult<Context>>;
111
+ returnValueHook?: {
112
+ token: string;
113
+ parentWorkflowRunId: string;
114
+ } | null;
105
115
  };
106
- export type ContextDurableWorkflowPayload<Env extends ContextEnvironment = ContextEnvironment> = {
116
+ export type ContextReactRun<Context = any> = ContextWorkflowRun<Context> | ContextDirectRun<Context>;
117
+ export type ContextReactResult<Context = any, Run extends ContextReactRun<Context> = ContextReactRun<Context>> = ContextReactBase<Context> & {
118
+ run?: Run;
119
+ };
120
+ export type ContextDurableWorkflowPayload<Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = {
107
121
  contextKey: string;
108
- runtime: ContextRuntime<Env>;
122
+ runtime: ContextRuntimeForDomain<Runtime, RequiredDomain>;
109
123
  context?: ContextIdentifier | null;
110
124
  triggerEvent: ContextItem;
111
125
  options?: Omit<ContextStreamOptions, "writable">;
112
- bootstrap: NonNullable<ContextReactParams<Env>["__bootstrap"]>;
126
+ bootstrap: NonNullable<ContextReactParams<Env, RequiredDomain, Runtime>["__bootstrap"]>;
113
127
  };
114
- export type ContextDurableWorkflowFunction<Context = any, Env extends ContextEnvironment = ContextEnvironment> = (payload: ContextDurableWorkflowPayload<Env>) => Promise<ContextReactResult<Context>>;
115
- export type ContextToolExecuteContext<Context = any, Env extends ContextEnvironment = ContextEnvironment> = {
116
- runtime: ContextRuntime<Env>;
117
- env: Env;
128
+ export type ContextDurableWorkflowFunction<Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = (payload: ContextDurableWorkflowPayload<Env, RequiredDomain, Runtime>) => Promise<ContextReactFinalResult<Context>>;
129
+ export type ContextToolExecuteContext<Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = {
130
+ runtime: ContextRuntimeForDomain<Runtime, RequiredDomain>;
118
131
  context: StoredContext<Context>;
119
132
  contextIdentifier: ContextIdentifier;
120
133
  toolCallId: string;
@@ -125,6 +138,7 @@ export type ContextToolExecuteContext<Context = any, Env extends ContextEnvironm
125
138
  contextId: string;
126
139
  stepId: string;
127
140
  iteration: number;
141
+ contextStepStream?: WritableStream<string>;
128
142
  };
129
143
  export { toolApprovalHookToken, toolApprovalWebhookToken, getClientResumeHookUrl };
130
144
  /**
@@ -135,13 +149,6 @@ export { toolApprovalHookToken, toolApprovalWebhookToken, getClientResumeHookUrl
135
149
  *
136
150
  * Default behavior when omitted: `auto === true`.
137
151
  */
138
- export type ContextTool = Tool & {
139
- /**
140
- * If `false`, this action is not intended for automatic execution by the engine.
141
- * (Validation/enforcement can be added by callers; default is `true`.)
142
- */
143
- auto?: boolean;
144
- };
145
152
  /**
146
153
  * ## Context loop continuation signal
147
154
  *
@@ -153,8 +160,9 @@ export type ContextTool = Tool & {
153
160
  * (No imports required in callers.)
154
161
  */
155
162
  export type ShouldContinue = boolean;
156
- export type ContextShouldContinueArgs<Context = any, Env extends ContextEnvironment = ContextEnvironment> = {
163
+ export type ContextShouldContinueArgs<Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain> = {
157
164
  env: Env;
165
+ runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>;
158
166
  context: StoredContext<Context>;
159
167
  /**
160
168
  * The persisted reaction event **so far** for the current streaming run.
@@ -183,14 +191,17 @@ export type ContextShouldContinueArgs<Context = any, Env extends ContextEnvironm
183
191
  errorText?: string;
184
192
  }>;
185
193
  };
186
- export declare abstract class ContextEngine<Context, Env extends ContextEnvironment = ContextEnvironment> {
187
- protected readonly opts: ContextOptions<Context, Env>;
194
+ export declare abstract class ContextEngine<Context, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain> {
195
+ protected readonly opts: ContextOptions<Context, Env, RequiredDomain>;
188
196
  private readonly reactor;
189
- constructor(opts?: ContextOptions<Context, Env>, reactor?: ContextReactor<Context, Env>);
190
- protected abstract initialize(context: StoredContext<Context>, env: Env): Promise<Context> | Context;
191
- protected abstract buildSystemPrompt(context: StoredContext<Context>, env: Env): Promise<string> | string;
192
- protected abstract buildTools(context: StoredContext<Context>, env: Env): Promise<Record<string, ContextTool>> | Record<string, ContextTool>;
193
- protected buildSkills(_context: StoredContext<Context>, _env: Env): Promise<ContextSkillPackage[]>;
197
+ constructor(opts?: ContextOptions<Context, Env, RequiredDomain>, reactor?: ContextReactor<Context, Env, RequiredDomain>);
198
+ protected abstract initialize(context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>): Promise<Context> | Context;
199
+ protected abstract buildSystemPrompt(context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>): Promise<string> | string;
200
+ protected describeContext(_content: Context, _context: StoredContext<Context>, _env: Env, _runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>): Promise<string | null>;
201
+ protected defineGoal(_content: Context, _context: StoredContext<Context>, _env: Env, _runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>): Promise<string | null>;
202
+ protected defineResources(_content: Context, _context: StoredContext<Context>, _env: Env, _runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>): Promise<ContextResource[]>;
203
+ protected abstract buildTools(context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>): Promise<Record<string, ContextTool<Context, Env, RequiredDomain>>> | Record<string, ContextTool<Context, Env, RequiredDomain>>;
204
+ protected buildSkills(_context: StoredContext<Context>, _env: Env, _runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>): Promise<ContextSkillPackage[]>;
194
205
  /**
195
206
  * First-class event expansion stage (runs on every iteration of the durable loop).
196
207
  *
@@ -208,9 +219,9 @@ export declare abstract class ContextEngine<Context, Env extends ContextEnvironm
208
219
  * the builder) so results are durable and replay-safe.
209
220
  * - If it’s pure/deterministic, it can run in workflow context.
210
221
  */
211
- protected expandEvents(events: ContextItem[], _context: StoredContext<Context>, _env: Env): Promise<ContextItem[]>;
212
- protected getModel(_context: StoredContext<Context>, _env: Env): ContextModelInit;
213
- protected getReactor(_context: StoredContext<Context>, _env: Env): ContextReactor<Context, Env>;
222
+ protected expandEvents(events: ContextItem[], _context: StoredContext<Context>, _env: Env, _runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>): Promise<ContextItem[]>;
223
+ protected getModel(_context: StoredContext<Context>, _env: Env, _runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>): ContextModelInit;
224
+ protected getReactor(_context: StoredContext<Context>, _env: Env, _runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>): ContextReactor<Context, Env, RequiredDomain>;
214
225
  /**
215
226
  * Context stop/continue hook.
216
227
  *
@@ -219,19 +230,16 @@ export declare abstract class ContextEngine<Context, Env extends ContextEnvironm
219
230
  *
220
231
  * Default: `true` (continue).
221
232
  */
222
- protected shouldContinue(_args: ContextShouldContinueArgs<Context, Env>): Promise<ShouldContinue>;
223
- react(triggerEvent: ContextItem, params: ContextReactParams<Env>): Promise<ContextReactResult<Context>>;
233
+ protected shouldContinue(_args: ContextShouldContinueArgs<Context, Env, RequiredDomain>): Promise<ShouldContinue>;
234
+ react<Runtime extends ContextRuntime<Env>>(triggerEvent: ContextItem, params: ContextDurableReactParams<Env, RequiredDomain, Runtime>): Promise<ContextReactResult<Context, ContextWorkflowRun<Context>>>;
235
+ react<Runtime extends ContextRuntime<Env>>(triggerEvent: ContextItem, params: ContextDirectReactParams<Env, RequiredDomain, Runtime>): Promise<ContextReactResult<Context, ContextDirectRun<Context>>>;
236
+ react<Runtime extends ContextRuntime<Env>>(triggerEvent: ContextItem, params: ContextReactParams<Env, RequiredDomain, Runtime>): Promise<ContextReactResult<Context>>;
224
237
  private static prepareExecutionShell;
225
238
  private static startDurable;
226
- static runDirect<Context, Env extends ContextEnvironment>(story: ContextEngine<Context, Env>, triggerEvent: ContextItem, params: ContextReactParams<Env>): Promise<{
227
- context: StoredContext<Context>;
228
- trigger: ContextItem;
229
- reaction: ContextItem;
230
- execution: ContextExecution;
231
- }>;
239
+ static runDirect<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike, Runtime extends ContextRuntime<Env>>(story: ContextEngine<Context, Env, RequiredDomain>, triggerEvent: ContextItem, params: ContextReactParams<Env, RequiredDomain, Runtime>): Promise<ContextReactResult<Context, ContextDirectRun<Context>>>;
232
240
  /**
233
241
  * @deprecated Use `react()` instead. Kept for backwards compatibility.
234
242
  */
235
- stream(triggerEvent: ContextItem, params: ContextReactParams<Env>): Promise<ContextReactResult<Context>>;
243
+ stream<Runtime extends ContextRuntime<Env>>(triggerEvent: ContextItem, params: ContextReactParams<Env, RequiredDomain, Runtime>): Promise<ContextReactResult<Context, ContextWorkflowRun<Context>>>;
236
244
  private callOnEnd;
237
245
  }