@ekairos/events 1.22.4-beta.development.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +115 -0
  2. package/dist/codex.d.ts +95 -0
  3. package/dist/codex.js +91 -0
  4. package/dist/context.builder.d.ts +62 -0
  5. package/dist/context.builder.js +143 -0
  6. package/dist/context.config.d.ts +9 -0
  7. package/dist/context.config.js +30 -0
  8. package/dist/context.contract.d.ts +47 -0
  9. package/dist/context.contract.js +132 -0
  10. package/dist/context.d.ts +4 -0
  11. package/dist/context.durable.d.ts +5 -0
  12. package/dist/context.durable.js +13 -0
  13. package/dist/context.engine.d.ts +216 -0
  14. package/dist/context.engine.js +1098 -0
  15. package/dist/context.events.d.ts +55 -0
  16. package/dist/context.events.js +431 -0
  17. package/dist/context.hooks.d.ts +21 -0
  18. package/dist/context.hooks.js +31 -0
  19. package/dist/context.js +3 -0
  20. package/dist/context.parts.d.ts +241 -0
  21. package/dist/context.parts.js +360 -0
  22. package/dist/context.reactor.d.ts +3 -0
  23. package/dist/context.reactor.js +2 -0
  24. package/dist/context.registry.d.ts +13 -0
  25. package/dist/context.registry.js +30 -0
  26. package/dist/context.skill.d.ts +9 -0
  27. package/dist/context.skill.js +1 -0
  28. package/dist/context.step-stream.d.ts +26 -0
  29. package/dist/context.step-stream.js +59 -0
  30. package/dist/context.store.d.ts +85 -0
  31. package/dist/context.store.js +1 -0
  32. package/dist/context.stream.d.ts +148 -0
  33. package/dist/context.stream.js +141 -0
  34. package/dist/context.toolcalls.d.ts +60 -0
  35. package/dist/context.toolcalls.js +117 -0
  36. package/dist/env.d.ts +3 -0
  37. package/dist/env.js +53 -0
  38. package/dist/index.d.ts +18 -0
  39. package/dist/index.js +11 -0
  40. package/dist/mcp.d.ts +1 -0
  41. package/dist/mcp.js +1 -0
  42. package/dist/mirror.d.ts +41 -0
  43. package/dist/mirror.js +1 -0
  44. package/dist/oidc.d.ts +7 -0
  45. package/dist/oidc.js +25 -0
  46. package/dist/polyfills/dom-events.d.ts +1 -0
  47. package/dist/polyfills/dom-events.js +89 -0
  48. package/dist/react.d.ts +42 -0
  49. package/dist/react.js +88 -0
  50. package/dist/reactors/ai-sdk.chunk-map.d.ts +12 -0
  51. package/dist/reactors/ai-sdk.chunk-map.js +143 -0
  52. package/dist/reactors/ai-sdk.reactor.d.ts +33 -0
  53. package/dist/reactors/ai-sdk.reactor.js +65 -0
  54. package/dist/reactors/ai-sdk.step.d.ts +48 -0
  55. package/dist/reactors/ai-sdk.step.js +343 -0
  56. package/dist/reactors/scripted.reactor.d.ts +17 -0
  57. package/dist/reactors/scripted.reactor.js +51 -0
  58. package/dist/reactors/types.d.ts +52 -0
  59. package/dist/reactors/types.js +1 -0
  60. package/dist/runtime.d.ts +19 -0
  61. package/dist/runtime.js +26 -0
  62. package/dist/runtime.step.d.ts +9 -0
  63. package/dist/runtime.step.js +7 -0
  64. package/dist/schema.d.ts +2 -0
  65. package/dist/schema.js +191 -0
  66. package/dist/steps/do-context-stream-step.d.ts +34 -0
  67. package/dist/steps/do-context-stream-step.js +96 -0
  68. package/dist/steps/mirror.steps.d.ts +6 -0
  69. package/dist/steps/mirror.steps.js +48 -0
  70. package/dist/steps/store.steps.d.ts +96 -0
  71. package/dist/steps/store.steps.js +595 -0
  72. package/dist/steps/stream.steps.d.ts +86 -0
  73. package/dist/steps/stream.steps.js +270 -0
  74. package/dist/steps/trace.steps.d.ts +38 -0
  75. package/dist/steps/trace.steps.js +270 -0
  76. package/dist/stores/instant.document-parser.d.ts +6 -0
  77. package/dist/stores/instant.document-parser.js +210 -0
  78. package/dist/stores/instant.documents.d.ts +16 -0
  79. package/dist/stores/instant.documents.js +152 -0
  80. package/dist/stores/instant.store.d.ts +66 -0
  81. package/dist/stores/instant.store.js +575 -0
  82. package/dist/tools-to-model-tools.d.ts +19 -0
  83. package/dist/tools-to-model-tools.js +21 -0
  84. package/package.json +142 -0
@@ -0,0 +1,132 @@
1
+ export const CONTEXT_STATUSES = ["open_idle", "open_streaming", "closed"];
2
+ export const EXECUTION_STATUSES = ["executing", "completed", "failed"];
3
+ export const STEP_STATUSES = ["running", "completed", "failed"];
4
+ export const STEP_KINDS = [
5
+ "message",
6
+ "action_execute",
7
+ "action_result",
8
+ ];
9
+ export const ITEM_STATUSES = ["stored", "pending", "completed"];
10
+ export const ITEM_TYPES = [
11
+ "input",
12
+ "output",
13
+ ];
14
+ export const CHANNELS = ["web", "whatsapp", "email"];
15
+ export const TRACE_EVENT_KINDS = [
16
+ "workflow.run",
17
+ "workflow.step",
18
+ "context.run",
19
+ "context.lifecycle",
20
+ "context.execution",
21
+ "context.item",
22
+ "context.review",
23
+ "context.step",
24
+ "context.part",
25
+ "context.llm",
26
+ ];
27
+ export const STREAM_LIFECYCLE_CHUNK_TYPES = [
28
+ "chunk.start",
29
+ "chunk.start_step",
30
+ "chunk.finish_step",
31
+ "chunk.finish",
32
+ ];
33
+ export const STREAM_TEXT_CHUNK_TYPES = [
34
+ "chunk.text_start",
35
+ "chunk.text_delta",
36
+ "chunk.text_end",
37
+ ];
38
+ export const STREAM_REASONING_CHUNK_TYPES = [
39
+ "chunk.reasoning_start",
40
+ "chunk.reasoning_delta",
41
+ "chunk.reasoning_end",
42
+ ];
43
+ export const STREAM_ACTION_CHUNK_TYPES = [
44
+ "chunk.action_input_start",
45
+ "chunk.action_input_delta",
46
+ "chunk.action_input_available",
47
+ "chunk.action_output_available",
48
+ "chunk.action_output_error",
49
+ ];
50
+ export const STREAM_SOURCE_CHUNK_TYPES = [
51
+ "chunk.source_url",
52
+ "chunk.source_document",
53
+ "chunk.file",
54
+ ];
55
+ export const STREAM_METADATA_CHUNK_TYPES = [
56
+ "chunk.message_metadata",
57
+ "chunk.response_metadata",
58
+ ];
59
+ export const STREAM_ERROR_CHUNK_TYPES = [
60
+ "chunk.error",
61
+ "chunk.unknown",
62
+ ];
63
+ export const CONTEXT_STREAM_CHUNK_TYPES = [
64
+ ...STREAM_LIFECYCLE_CHUNK_TYPES,
65
+ ...STREAM_TEXT_CHUNK_TYPES,
66
+ ...STREAM_REASONING_CHUNK_TYPES,
67
+ ...STREAM_ACTION_CHUNK_TYPES,
68
+ ...STREAM_SOURCE_CHUNK_TYPES,
69
+ ...STREAM_METADATA_CHUNK_TYPES,
70
+ ...STREAM_ERROR_CHUNK_TYPES,
71
+ ];
72
+ export function isContextStreamChunkType(value) {
73
+ return CONTEXT_STREAM_CHUNK_TYPES.includes(value);
74
+ }
75
+ export const CONTEXT_TRANSITIONS = [
76
+ { from: "open_idle", to: "open_streaming" },
77
+ { from: "open_streaming", to: "open_idle" },
78
+ { from: "open_idle", to: "closed" },
79
+ { from: "open_streaming", to: "closed" },
80
+ { from: "closed", to: "open_idle" },
81
+ ];
82
+ export const EXECUTION_TRANSITIONS = [
83
+ { from: "executing", to: "completed" },
84
+ { from: "executing", to: "failed" },
85
+ ];
86
+ export const STEP_TRANSITIONS = [
87
+ { from: "running", to: "completed" },
88
+ { from: "running", to: "failed" },
89
+ ];
90
+ export const ITEM_TRANSITIONS = [
91
+ { from: "stored", to: "pending" },
92
+ { from: "stored", to: "completed" },
93
+ { from: "pending", to: "completed" },
94
+ ];
95
+ function canTransition(transitions, from, to) {
96
+ return transitions.some((transition) => transition.from === from && transition.to === to);
97
+ }
98
+ function assertTransition(transitions, from, to, entity) {
99
+ if (!canTransition(transitions, from, to)) {
100
+ throw new Error(`Invalid ${entity} transition: ${from} -> ${to}`);
101
+ }
102
+ }
103
+ export function canContextTransition(from, to) {
104
+ return canTransition(CONTEXT_TRANSITIONS, from, to);
105
+ }
106
+ export function canExecutionTransition(from, to) {
107
+ return canTransition(EXECUTION_TRANSITIONS, from, to);
108
+ }
109
+ export function canStepTransition(from, to) {
110
+ return canTransition(STEP_TRANSITIONS, from, to);
111
+ }
112
+ export function canItemTransition(from, to) {
113
+ return canTransition(ITEM_TRANSITIONS, from, to);
114
+ }
115
+ export function assertContextTransition(from, to) {
116
+ assertTransition(CONTEXT_TRANSITIONS, from, to, "context.status");
117
+ }
118
+ export function assertExecutionTransition(from, to) {
119
+ assertTransition(EXECUTION_TRANSITIONS, from, to, "execution.status");
120
+ }
121
+ export function assertStepTransition(from, to) {
122
+ assertTransition(STEP_TRANSITIONS, from, to, "step.status");
123
+ }
124
+ export function assertItemTransition(from, to) {
125
+ assertTransition(ITEM_TRANSITIONS, from, to, "item.status");
126
+ }
127
+ export function assertContextPartKey(stepId, idx, key) {
128
+ const expected = `${stepId}:${idx}`;
129
+ if (key !== expected) {
130
+ throw new Error(`Invalid context_parts.key: expected "${expected}" got "${key}"`);
131
+ }
132
+ }
@@ -0,0 +1,4 @@
1
+ export { ContextEngine, type ContextOptions, type ContextStreamOptions, type ShouldContinue, type ContextShouldContinueArgs, type ContextReactParams, type ContextReactResult, type ContextDurableWorkflowPayload, type ContextDurableWorkflowFunction, type ContextModelInit, type ContextTool, runContextReactionDirect, } from "./context.engine.js";
2
+ export { context, createContext, type ContextConfig, type ContextInstance, type RegistrableContextBuilder, } from "./context.builder.js";
3
+ export { createAiSdkReactor, createScriptedReactor, type CreateAiSdkReactorOptions, type CreateScriptedReactorOptions, type ScriptedReactorStep, type ContextReactor, type ContextReactorParams, type ContextReactionResult, type ContextActionRequest, type ContextReactionLLM, } from "./context.reactor.js";
4
+ export type { ContextSkillPackage, ContextSkillPackageFile, } from "./context.skill.js";
@@ -0,0 +1,5 @@
1
+ import type { ContextDurableWorkflowFunction, ContextReactResult, ContextDurableWorkflowPayload } from "./context.engine.js";
2
+ import type { ContextEnvironment } from "./context.config.js";
3
+ export declare function configureContextDurableWorkflow<Context = any, Env extends ContextEnvironment = ContextEnvironment>(workflow: ContextDurableWorkflowFunction<Context, Env> | null): void;
4
+ export declare function getContextDurableWorkflow(): ContextDurableWorkflowFunction<any, any> | undefined;
5
+ export type { ContextDurableWorkflowFunction, ContextDurableWorkflowPayload, ContextReactResult, };
@@ -0,0 +1,13 @@
1
+ const durableWorkflowSymbol = Symbol.for("ekairos.events.contextDurableWorkflow");
2
+ export function configureContextDurableWorkflow(workflow) {
3
+ if (typeof globalThis === "undefined")
4
+ return;
5
+ const store = globalThis;
6
+ store[durableWorkflowSymbol] = workflow ?? undefined;
7
+ }
8
+ export function getContextDurableWorkflow() {
9
+ if (typeof globalThis === "undefined")
10
+ return undefined;
11
+ const store = globalThis;
12
+ return store[durableWorkflowSymbol];
13
+ }
@@ -0,0 +1,216 @@
1
+ import type { Tool, UIMessageChunk } from "ai";
2
+ import type { ContextEnvironment } from "./context.config.js";
3
+ import type { ContextExecution, ContextItem, ContextIdentifier, StoredContext } from "./context.store.js";
4
+ import type { ContextSkillPackage } from "./context.skill.js";
5
+ import { type ContextReactor } from "./context.reactor.js";
6
+ import { getClientResumeHookUrl, toolApprovalHookToken, toolApprovalWebhookToken } from "./context.hooks.js";
7
+ export interface ContextOptions<Context = any, Env extends ContextEnvironment = ContextEnvironment> {
8
+ onContextCreated?: (args: {
9
+ env: Env;
10
+ context: StoredContext<Context>;
11
+ }) => void | Promise<void>;
12
+ onContextUpdated?: (args: {
13
+ env: Env;
14
+ context: StoredContext<Context>;
15
+ }) => void | Promise<void>;
16
+ onEventCreated?: (event: ContextItem) => void | Promise<void>;
17
+ onActionExecuted?: (executionEvent: any) => void | Promise<void>;
18
+ onEnd?: (lastEvent: ContextItem) => void | boolean | Promise<void | boolean>;
19
+ }
20
+ type ContextBenchmarkRecorder = {
21
+ measure<T>(name: string, run: () => Promise<T> | T): Promise<T>;
22
+ add?(name: string, value: number): void;
23
+ getCurrentStage?(): string | undefined;
24
+ };
25
+ export declare function runContextReactionDirect<Context, Env extends ContextEnvironment = ContextEnvironment>(context: ContextEngine<Context, Env>, triggerEvent: ContextItem, params: ContextReactParams<Env>): Promise<ContextReactResult<Context>>;
26
+ export interface ContextStreamOptions {
27
+ /**
28
+ * Maximum loop iterations (LLM call → tool execution → repeat).
29
+ * Default: 20
30
+ */
31
+ maxIterations?: number;
32
+ /**
33
+ * Maximum model steps per LLM call.
34
+ * Default: 1 (or 5 if you override it in your implementation).
35
+ */
36
+ maxModelSteps?: number;
37
+ /**
38
+ * If true, we do not close the workflow writable stream.
39
+ * Default: false.
40
+ */
41
+ preventClose?: boolean;
42
+ /**
43
+ * If true, we write a `finish` chunk to the workflow stream.
44
+ * Default: true.
45
+ */
46
+ sendFinish?: boolean;
47
+ /**
48
+ * If true, the story loop runs silently (no UI streaming output).
49
+ *
50
+ * Persistence (contexts/events/executions) still happens normally.
51
+ *
52
+ * Default: false.
53
+ */
54
+ silent?: boolean;
55
+ /**
56
+ * Optional writable stream used by direct/non-durable execution.
57
+ *
58
+ * Durable execution owns its workflow stream and will reject custom writables.
59
+ */
60
+ writable?: WritableStream<UIMessageChunk>;
61
+ }
62
+ /**
63
+ * Model initializer (DurableAgent-style).
64
+ *
65
+ * - `string`: Vercel AI Gateway model id (e.g. `"openai/gpt-5"`), resolved inside the LLM step.
66
+ * - `function`: a function that returns a model instance. For Workflow compatibility, this should
67
+ * be a `"use-step"` function (so it can be serialized by reference).
68
+ */
69
+ export type ContextModelInit = string | (() => Promise<any>);
70
+ export type ContextReactParams<Env extends ContextEnvironment = ContextEnvironment> = {
71
+ env: Env;
72
+ /**
73
+ * Context selector (exclusive: `{ id }` OR `{ key }`).
74
+ * - `{ id }` resolves a concrete context id.
75
+ * - `{ key }` resolves by `context.key`.
76
+ * If omitted/null, the story will create a new context.
77
+ */
78
+ context?: ContextIdentifier | null;
79
+ durable?: boolean;
80
+ options?: ContextStreamOptions;
81
+ /**
82
+ * Internal bootstrap used by the workflow-owned continuation path.
83
+ * Not part of the public API surface.
84
+ */
85
+ __bootstrap?: {
86
+ contextId: string;
87
+ trigger: ContextItem;
88
+ reaction: ContextItem;
89
+ execution: ContextExecution;
90
+ };
91
+ __benchmark?: ContextBenchmarkRecorder;
92
+ };
93
+ export type ContextReactResult<Context = any> = {
94
+ context: StoredContext<Context>;
95
+ trigger: ContextItem;
96
+ reaction: ContextItem;
97
+ execution: ContextExecution;
98
+ };
99
+ export type ContextDurableWorkflowPayload<Env extends ContextEnvironment = ContextEnvironment> = {
100
+ contextKey: string;
101
+ env: Env;
102
+ context?: ContextIdentifier | null;
103
+ triggerEvent: ContextItem;
104
+ options?: Omit<ContextStreamOptions, "writable">;
105
+ bootstrap: NonNullable<ContextReactParams<Env>["__bootstrap"]>;
106
+ };
107
+ export type ContextDurableWorkflowFunction<Context = any, Env extends ContextEnvironment = ContextEnvironment> = (payload: ContextDurableWorkflowPayload<Env>) => Promise<ContextReactResult<Context>>;
108
+ export { toolApprovalHookToken, toolApprovalWebhookToken, getClientResumeHookUrl };
109
+ /**
110
+ * Context-level tool type.
111
+ *
112
+ * Allows contexts to attach metadata to actions/tools (e.g. `{ auto: false }`)
113
+ * while remaining compatible with the AI SDK `Tool` runtime shape.
114
+ *
115
+ * Default behavior when omitted: `auto === true`.
116
+ */
117
+ export type ContextTool = Tool & {
118
+ /**
119
+ * If `false`, this action is not intended for automatic execution by the engine.
120
+ * (Validation/enforcement can be added by callers; default is `true`.)
121
+ */
122
+ auto?: boolean;
123
+ };
124
+ /**
125
+ * ## Context loop continuation signal
126
+ *
127
+ * This hook result is intentionally a **boolean** so stories can be extremely declarative:
128
+ *
129
+ * - `return true` => **continue** the durable loop
130
+ * - `return false` => **finalize** the durable loop
131
+ *
132
+ * (No imports required in callers.)
133
+ */
134
+ export type ShouldContinue = boolean;
135
+ export type ContextShouldContinueArgs<Context = any, Env extends ContextEnvironment = ContextEnvironment> = {
136
+ env: Env;
137
+ context: StoredContext<Context>;
138
+ /**
139
+ * The persisted reaction event **so far** for the current streaming run.
140
+ *
141
+ * This contains the assistant's streamed parts as well as merged tool execution
142
+ * outcomes (e.g. `state: "output-available"` / `"output-error"`).
143
+ *
144
+ * Stories can inspect `reactionEvent.content.parts` to determine stop conditions
145
+ * (for example: when `tool-end` has an `output-available` state).
146
+ */
147
+ reactionEvent: ContextItem;
148
+ assistantEvent: ContextItem;
149
+ actionRequests: Array<{
150
+ actionRef: string;
151
+ actionName: string;
152
+ input: unknown;
153
+ }>;
154
+ actionResults: Array<{
155
+ actionRequest: {
156
+ actionRef: string;
157
+ actionName: string;
158
+ input: unknown;
159
+ };
160
+ success: boolean;
161
+ output: any;
162
+ errorText?: string;
163
+ }>;
164
+ };
165
+ export declare abstract class ContextEngine<Context, Env extends ContextEnvironment = ContextEnvironment> {
166
+ protected readonly opts: ContextOptions<Context, Env>;
167
+ private readonly reactor;
168
+ constructor(opts?: ContextOptions<Context, Env>, reactor?: ContextReactor<Context, Env>);
169
+ protected abstract initialize(context: StoredContext<Context>, env: Env): Promise<Context> | Context;
170
+ protected abstract buildSystemPrompt(context: StoredContext<Context>, env: Env): Promise<string> | string;
171
+ protected abstract buildTools(context: StoredContext<Context>, env: Env): Promise<Record<string, ContextTool>> | Record<string, ContextTool>;
172
+ protected buildSkills(_context: StoredContext<Context>, _env: Env): Promise<ContextSkillPackage[]>;
173
+ /**
174
+ * First-class event expansion stage (runs on every iteration of the durable loop).
175
+ *
176
+ * Use this to expand/normalize events before they are converted into model messages.
177
+ * Typical use-cases:
178
+ * - Expand file/document references into text (LlamaCloud/Reducto/…)
179
+ * - Token compaction / summarization of older parts
180
+ * - Attaching derived context snippets to the next model call
181
+ *
182
+ * IMPORTANT:
183
+ * - This stage is ALWAYS executed by the engine.
184
+ * - If you don't provide an implementation, the default behavior is an identity transform
185
+ * (events pass through unchanged).
186
+ * - If your implementation performs I/O, implement it as a `"use-step"` function (provided via
187
+ * the builder) so results are durable and replay-safe.
188
+ * - If it’s pure/deterministic, it can run in workflow context.
189
+ */
190
+ protected expandEvents(events: ContextItem[], _context: StoredContext<Context>, _env: Env): Promise<ContextItem[]>;
191
+ protected getModel(_context: StoredContext<Context>, _env: Env): ContextModelInit;
192
+ protected getReactor(_context: StoredContext<Context>, _env: Env): ContextReactor<Context, Env>;
193
+ /**
194
+ * Context stop/continue hook.
195
+ *
196
+ * After the model streamed and tools executed, the story can decide whether the loop should
197
+ * continue.
198
+ *
199
+ * Default: `true` (continue).
200
+ */
201
+ protected shouldContinue(_args: ContextShouldContinueArgs<Context, Env>): Promise<ShouldContinue>;
202
+ react(triggerEvent: ContextItem, params: ContextReactParams<Env>): Promise<ContextReactResult<Context>>;
203
+ private static prepareExecutionShell;
204
+ private static startDurable;
205
+ static runDirect<Context, Env extends ContextEnvironment>(story: ContextEngine<Context, Env>, triggerEvent: ContextItem, params: ContextReactParams<Env>): Promise<{
206
+ context: StoredContext<Context>;
207
+ trigger: ContextItem;
208
+ reaction: ContextItem;
209
+ execution: ContextExecution;
210
+ }>;
211
+ /**
212
+ * @deprecated Use `react()` instead. Kept for backwards compatibility.
213
+ */
214
+ stream(triggerEvent: ContextItem, params: ContextReactParams<Env>): Promise<ContextReactResult<Context>>;
215
+ private callOnEnd;
216
+ }