@ekairos/thread 1.21.88-beta.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 (86) hide show
  1. package/README.md +363 -0
  2. package/dist/codex.d.ts +95 -0
  3. package/dist/codex.js +91 -0
  4. package/dist/env.d.ts +12 -0
  5. package/dist/env.js +62 -0
  6. package/dist/events.d.ts +35 -0
  7. package/dist/events.js +102 -0
  8. package/dist/index.d.ts +9 -0
  9. package/dist/index.js +9 -0
  10. package/dist/mcp.d.ts +1 -0
  11. package/dist/mcp.js +1 -0
  12. package/dist/mirror.d.ts +41 -0
  13. package/dist/mirror.js +1 -0
  14. package/dist/oidc.d.ts +7 -0
  15. package/dist/oidc.js +25 -0
  16. package/dist/polyfills/dom-events.d.ts +1 -0
  17. package/dist/polyfills/dom-events.js +89 -0
  18. package/dist/react.d.ts +62 -0
  19. package/dist/react.js +101 -0
  20. package/dist/runtime.d.ts +17 -0
  21. package/dist/runtime.js +23 -0
  22. package/dist/runtime.step.d.ts +9 -0
  23. package/dist/runtime.step.js +7 -0
  24. package/dist/schema.d.ts +2 -0
  25. package/dist/schema.js +200 -0
  26. package/dist/steps/do-story-stream-step.d.ts +29 -0
  27. package/dist/steps/do-story-stream-step.js +89 -0
  28. package/dist/steps/do-thread-stream-step.d.ts +29 -0
  29. package/dist/steps/do-thread-stream-step.js +90 -0
  30. package/dist/steps/mirror.steps.d.ts +6 -0
  31. package/dist/steps/mirror.steps.js +48 -0
  32. package/dist/steps/reaction.steps.d.ts +43 -0
  33. package/dist/steps/reaction.steps.js +354 -0
  34. package/dist/steps/store.steps.d.ts +98 -0
  35. package/dist/steps/store.steps.js +512 -0
  36. package/dist/steps/stream.steps.d.ts +41 -0
  37. package/dist/steps/stream.steps.js +99 -0
  38. package/dist/steps/trace.steps.d.ts +37 -0
  39. package/dist/steps/trace.steps.js +265 -0
  40. package/dist/stores/instant.document-parser.d.ts +6 -0
  41. package/dist/stores/instant.document-parser.js +210 -0
  42. package/dist/stores/instant.documents.d.ts +16 -0
  43. package/dist/stores/instant.documents.js +152 -0
  44. package/dist/stores/instant.store.d.ts +78 -0
  45. package/dist/stores/instant.store.js +530 -0
  46. package/dist/story.actions.d.ts +60 -0
  47. package/dist/story.actions.js +120 -0
  48. package/dist/story.builder.d.ts +115 -0
  49. package/dist/story.builder.js +130 -0
  50. package/dist/story.config.d.ts +54 -0
  51. package/dist/story.config.js +125 -0
  52. package/dist/story.d.ts +2 -0
  53. package/dist/story.engine.d.ts +224 -0
  54. package/dist/story.engine.js +464 -0
  55. package/dist/story.hooks.d.ts +21 -0
  56. package/dist/story.hooks.js +31 -0
  57. package/dist/story.js +6 -0
  58. package/dist/story.registry.d.ts +21 -0
  59. package/dist/story.registry.js +30 -0
  60. package/dist/story.store.d.ts +107 -0
  61. package/dist/story.store.js +1 -0
  62. package/dist/story.toolcalls.d.ts +60 -0
  63. package/dist/story.toolcalls.js +73 -0
  64. package/dist/thread.builder.d.ts +118 -0
  65. package/dist/thread.builder.js +134 -0
  66. package/dist/thread.config.d.ts +15 -0
  67. package/dist/thread.config.js +30 -0
  68. package/dist/thread.d.ts +3 -0
  69. package/dist/thread.engine.d.ts +229 -0
  70. package/dist/thread.engine.js +471 -0
  71. package/dist/thread.events.d.ts +35 -0
  72. package/dist/thread.events.js +105 -0
  73. package/dist/thread.hooks.d.ts +21 -0
  74. package/dist/thread.hooks.js +31 -0
  75. package/dist/thread.js +7 -0
  76. package/dist/thread.reactor.d.ts +82 -0
  77. package/dist/thread.reactor.js +65 -0
  78. package/dist/thread.registry.d.ts +21 -0
  79. package/dist/thread.registry.js +30 -0
  80. package/dist/thread.store.d.ts +121 -0
  81. package/dist/thread.store.js +1 -0
  82. package/dist/thread.toolcalls.d.ts +60 -0
  83. package/dist/thread.toolcalls.js +73 -0
  84. package/dist/tools-to-model-tools.d.ts +19 -0
  85. package/dist/tools-to-model-tools.js +21 -0
  86. package/package.json +133 -0
@@ -0,0 +1,229 @@
1
+ import type { Tool, UIMessageChunk } from "ai";
2
+ import type { ThreadEnvironment } from "./thread.config.js";
3
+ import type { ThreadItem, ContextIdentifier, StoredContext } from "./thread.store.js";
4
+ import { type ThreadReactor } from "./thread.reactor.js";
5
+ import { getClientResumeHookUrl, toolApprovalHookToken, toolApprovalWebhookToken } from "./thread.hooks.js";
6
+ export interface ThreadOptions<Context = any, Env extends ThreadEnvironment = ThreadEnvironment> {
7
+ onContextCreated?: (args: {
8
+ env: Env;
9
+ context: StoredContext<Context>;
10
+ }) => void | Promise<void>;
11
+ onContextUpdated?: (args: {
12
+ env: Env;
13
+ context: StoredContext<Context>;
14
+ }) => void | Promise<void>;
15
+ onEventCreated?: (event: ThreadItem) => void | Promise<void>;
16
+ onToolCallExecuted?: (executionEvent: any) => void | Promise<void>;
17
+ onEnd?: (lastEvent: ThreadItem) => void | {
18
+ end?: boolean;
19
+ } | Promise<void | {
20
+ end?: boolean;
21
+ }>;
22
+ }
23
+ export interface ThreadStreamOptions {
24
+ /**
25
+ * Maximum loop iterations (LLM call → tool execution → repeat).
26
+ * Default: 20
27
+ */
28
+ maxIterations?: number;
29
+ /**
30
+ * Maximum model steps per LLM call.
31
+ * Default: 1 (or 5 if you override it in your implementation).
32
+ */
33
+ maxModelSteps?: number;
34
+ /**
35
+ * If true, we do not close the workflow writable stream.
36
+ * Default: false.
37
+ */
38
+ preventClose?: boolean;
39
+ /**
40
+ * If true, we write a `finish` chunk to the workflow stream.
41
+ * Default: true.
42
+ */
43
+ sendFinish?: boolean;
44
+ /**
45
+ * If true, the story loop runs silently (no UI streaming output).
46
+ *
47
+ * Persistence (contexts/events/executions) still happens normally.
48
+ *
49
+ * Default: false.
50
+ */
51
+ silent?: boolean;
52
+ /**
53
+ * Optional workflow writable stream to emit UIMessageChunks into.
54
+ *
55
+ * When omitted, the story will obtain a namespaced writable automatically:
56
+ * `getWritable({ namespace: "context:<contextId>" })`.
57
+ *
58
+ * This allows multiple stories / contexts to stream concurrently within the same workflow run.
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 ThreadModelInit = string | (() => Promise<any>) | any;
70
+ export type ThreadReactParams<Env extends ThreadEnvironment = ThreadEnvironment> = {
71
+ env: Env;
72
+ /**
73
+ * Context/thread selector (exclusive: `{ id }` OR `{ key }`).
74
+ * - `{ id }` resolves a concrete context id.
75
+ * - `{ key }` resolves by `thread.key`.
76
+ * If omitted/null, the story will create a new thread+context pair.
77
+ */
78
+ context?: ContextIdentifier | null;
79
+ options?: ThreadStreamOptions;
80
+ };
81
+ /**
82
+ * Payload expected to resume an auto=false tool execution.
83
+ *
84
+ * This must be serializable because it crosses the workflow hook boundary.
85
+ *
86
+ * See: https://useworkflow.dev/docs/foundations/hooks
87
+ */
88
+ export type ThreadToolApprovalPayload = {
89
+ approved: true;
90
+ comment?: string;
91
+ args?: Record<string, unknown>;
92
+ } | {
93
+ approved: false;
94
+ comment?: string;
95
+ };
96
+ export { toolApprovalHookToken, toolApprovalWebhookToken, getClientResumeHookUrl };
97
+ /**
98
+ * Thread-level tool type.
99
+ *
100
+ * Allows threads to attach metadata to actions/tools (e.g. `{ auto: false }`)
101
+ * while remaining compatible with the AI SDK `Tool` runtime shape.
102
+ *
103
+ * Default behavior when omitted: `auto === true`.
104
+ */
105
+ export type ThreadTool = Tool & {
106
+ /**
107
+ * If `false`, this action is not intended for automatic execution by the engine.
108
+ * (Validation/enforcement can be added by callers; default is `true`.)
109
+ */
110
+ auto?: boolean;
111
+ };
112
+ /**
113
+ * ## Thread loop continuation signal
114
+ *
115
+ * This hook result is intentionally a **boolean** so stories can be extremely declarative:
116
+ *
117
+ * - `return true` => **continue** the durable loop
118
+ * - `return false` => **finalize** the durable loop
119
+ *
120
+ * (No imports required in callers.)
121
+ */
122
+ export type ShouldContinue = boolean;
123
+ export type ThreadShouldContinueArgs<Context = any, Env extends ThreadEnvironment = ThreadEnvironment> = {
124
+ env: Env;
125
+ context: StoredContext<Context>;
126
+ /**
127
+ * The persisted reaction event **so far** for the current streaming run.
128
+ *
129
+ * This contains the assistant's streamed parts as well as merged tool execution
130
+ * outcomes (e.g. `state: "output-available"` / `"output-error"`).
131
+ *
132
+ * Stories can inspect `reactionEvent.content.parts` to determine stop conditions
133
+ * (for example: when `tool-end` has an `output-available` state).
134
+ */
135
+ reactionEvent: ThreadItem;
136
+ assistantEvent: ThreadItem;
137
+ toolCalls: any[];
138
+ toolExecutionResults: Array<{
139
+ tc: any;
140
+ success: boolean;
141
+ output: any;
142
+ errorText?: string;
143
+ }>;
144
+ };
145
+ export declare abstract class Thread<Context, Env extends ThreadEnvironment = ThreadEnvironment> {
146
+ private opts;
147
+ private readonly reactor;
148
+ constructor(opts?: ThreadOptions<Context, Env>, reactor?: ThreadReactor<Context, Env>);
149
+ protected abstract initialize(context: StoredContext<Context>, env: Env): Promise<Context> | Context;
150
+ protected abstract buildSystemPrompt(context: StoredContext<Context>, env: Env): Promise<string> | string;
151
+ protected abstract buildTools(context: StoredContext<Context>, env: Env): Promise<Record<string, ThreadTool>> | Record<string, ThreadTool>;
152
+ /**
153
+ * First-class event expansion stage (runs on every iteration of the durable loop).
154
+ *
155
+ * Use this to expand/normalize events before they are converted into model messages.
156
+ * Typical use-cases:
157
+ * - Expand file/document references into text (LlamaCloud/Reducto/…)
158
+ * - Token compaction / summarization of older parts
159
+ * - Attaching derived context snippets to the next model call
160
+ *
161
+ * IMPORTANT:
162
+ * - This stage is ALWAYS executed by the engine.
163
+ * - If you don't provide an implementation, the default behavior is an identity transform
164
+ * (events pass through unchanged).
165
+ * - If your implementation performs I/O, implement it as a `"use-step"` function (provided via
166
+ * the builder) so results are durable and replay-safe.
167
+ * - If it’s pure/deterministic, it can run in workflow context.
168
+ */
169
+ protected expandEvents(events: ThreadItem[], _context: StoredContext<Context>, _env: Env): Promise<ThreadItem[]>;
170
+ protected getModel(_context: StoredContext<Context>, _env: Env): ThreadModelInit;
171
+ protected getReactor(_context: StoredContext<Context>, _env: Env): ThreadReactor<Context, Env>;
172
+ /**
173
+ * Thread stop/continue hook.
174
+ *
175
+ * After the model streamed and tools executed, the story can decide whether the loop should
176
+ * continue.
177
+ *
178
+ * Default: `true` (continue).
179
+ */
180
+ protected shouldContinue(_args: ThreadShouldContinueArgs<Context, Env>): Promise<ShouldContinue>;
181
+ /**
182
+ * Workflow-first execution entrypoint.
183
+ *
184
+ * - Streaming is written to the workflow run's output stream.
185
+ * - All I/O is delegated to steps (store access, LLM streaming, stream writes).
186
+ * - This method returns metadata only.
187
+ */
188
+ /**
189
+ * React to an incoming event and advance the story.
190
+ *
191
+ * This is the primary workflow entrypoint.
192
+ */
193
+ react(triggerEvent: ThreadItem, params: ThreadReactParams<Env>): Promise<{
194
+ contextId: string;
195
+ context: StoredContext<Context>;
196
+ triggerEventId: string;
197
+ reactionEventId: string;
198
+ executionId: string;
199
+ }>;
200
+ /**
201
+ * @deprecated Back-compat: old object-style call signature.
202
+ */
203
+ react(params: {
204
+ env: Env;
205
+ /** @deprecated Use `triggerEvent` */
206
+ incomingEvent?: ThreadItem;
207
+ triggerEvent?: ThreadItem;
208
+ contextIdentifier: ContextIdentifier | null;
209
+ options?: ThreadStreamOptions;
210
+ }): Promise<{
211
+ contextId: string;
212
+ context: StoredContext<Context>;
213
+ triggerEventId: string;
214
+ reactionEventId: string;
215
+ executionId: string;
216
+ }>;
217
+ private static runLoop;
218
+ /**
219
+ * @deprecated Use `react()` instead. Kept for backwards compatibility.
220
+ */
221
+ stream(triggerEvent: ThreadItem, params: ThreadReactParams<Env>): Promise<{
222
+ contextId: string;
223
+ context: StoredContext<Context>;
224
+ triggerEventId: string;
225
+ reactionEventId: string;
226
+ executionId: string;
227
+ }>;
228
+ private callOnEnd;
229
+ }