@ekairos/story 1.21.41-beta.0 → 1.21.52-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.
- package/dist/ekairos.config.js +1 -16
- package/dist/index.d.ts +6 -6
- package/dist/index.js +5 -5
- package/dist/runtime.d.ts +1 -2
- package/dist/runtime.js +1 -2
- package/dist/steps/reaction.steps.d.ts +26 -0
- package/dist/steps/reaction.steps.js +137 -0
- package/dist/steps/store.steps.d.ts +15 -28
- package/dist/steps/store.steps.js +35 -67
- package/dist/steps/stream.steps.d.ts +7 -0
- package/dist/steps/stream.steps.js +15 -0
- package/dist/stores/instant.document-parser.d.ts +1 -1
- package/dist/stores/instant.document-parser.js +175 -39
- package/dist/stores/instant.documents.js +82 -6
- package/dist/stores/instant.store.d.ts +2 -0
- package/dist/stores/instant.store.js +16 -1
- package/dist/story.builder.d.ts +4 -4
- package/dist/story.builder.js +2 -2
- package/dist/story.config.d.ts +1 -2
- package/dist/story.config.js +31 -39
- package/dist/story.d.ts +2 -2
- package/dist/story.engine.d.ts +10 -2
- package/dist/story.engine.js +99 -29
- package/dist/story.js +2 -2
- package/package.json +16 -2
package/dist/ekairos.config.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
import { configureStoryRuntime, configureStoryRuntimeBootstrap, isStoryRuntimeConfigured, } from "./story.config";
|
|
2
|
-
const GLOBAL_EKAIROS_CONFIG = Symbol.for("@ekairos/story.ekairosConfig");
|
|
3
|
-
function setGlobalEkairosConfig(config) {
|
|
4
|
-
try {
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
-
;
|
|
7
|
-
globalThis[GLOBAL_EKAIROS_CONFIG] = config;
|
|
8
|
-
}
|
|
9
|
-
catch {
|
|
10
|
-
// ignore
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
2
|
/**
|
|
14
3
|
* Creates a small "framework-style" config object that can be executed in the step runtime.
|
|
15
4
|
*
|
|
@@ -18,7 +7,7 @@ function setGlobalEkairosConfig(config) {
|
|
|
18
7
|
*/
|
|
19
8
|
export function createEkairosConfig(params) {
|
|
20
9
|
const stories = params.stories ?? [];
|
|
21
|
-
|
|
10
|
+
return {
|
|
22
11
|
stories,
|
|
23
12
|
runtime: params.runtime,
|
|
24
13
|
setup() {
|
|
@@ -34,8 +23,4 @@ export function createEkairosConfig(params) {
|
|
|
34
23
|
s.register();
|
|
35
24
|
},
|
|
36
25
|
};
|
|
37
|
-
// Register globally (process-level) so libraries/steps can access it transparently.
|
|
38
|
-
// Note: This does NOT call `setup()` (no runtime side-effects).
|
|
39
|
-
setGlobalEkairosConfig(config);
|
|
40
|
-
return config;
|
|
41
26
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { story, createStory, type StoryConfig, type StoryInstance, type StoryOptions, type StoryStreamOptions, Story, type RegistrableStoryBuilder, } from "./story";
|
|
2
|
-
export type { StoryStore, ContextIdentifier, StoredContext, ContextEvent, } from "./story.store";
|
|
3
|
-
export { registerStory, getStory, getStoryFactory, hasStory, listStories, type StoryKey, } from "./story.registry";
|
|
4
|
-
export { storyDomain } from "./schema";
|
|
5
|
-
export { didToolExecute } from "./story.toolcalls";
|
|
6
|
-
export { USER_MESSAGE_TYPE, ASSISTANT_MESSAGE_TYPE, SYSTEM_MESSAGE_TYPE, WEB_CHANNEL, AGENT_CHANNEL, EMAIL_CHANNEL, createUserEventFromUIMessages, createAssistantEventFromUIMessages, convertToUIMessage, convertEventToModelMessages, convertEventsToModelMessages, convertModelMessageToEvent, type ResponseMessage, } from "./events";
|
|
1
|
+
export { story, createStory, type StoryConfig, type StoryInstance, type StoryOptions, type StoryStreamOptions, Story, type RegistrableStoryBuilder, } from "./story.js";
|
|
2
|
+
export type { StoryStore, ContextIdentifier, StoredContext, ContextEvent, } from "./story.store.js";
|
|
3
|
+
export { registerStory, getStory, getStoryFactory, hasStory, listStories, type StoryKey, } from "./story.registry.js";
|
|
4
|
+
export { storyDomain } from "./schema.js";
|
|
5
|
+
export { didToolExecute } from "./story.toolcalls.js";
|
|
6
|
+
export { USER_MESSAGE_TYPE, ASSISTANT_MESSAGE_TYPE, SYSTEM_MESSAGE_TYPE, WEB_CHANNEL, AGENT_CHANNEL, EMAIL_CHANNEL, createUserEventFromUIMessages, createAssistantEventFromUIMessages, convertToUIMessage, convertEventToModelMessages, convertEventsToModelMessages, convertModelMessageToEvent, type ResponseMessage, } from "./events.js";
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export {
|
|
2
2
|
// Story API
|
|
3
|
-
story, createStory, Story, } from "./story";
|
|
4
|
-
export { registerStory, getStory, getStoryFactory, hasStory, listStories, } from "./story.registry";
|
|
5
|
-
export { storyDomain } from "./schema";
|
|
6
|
-
export { didToolExecute } from "./story.toolcalls";
|
|
7
|
-
export { USER_MESSAGE_TYPE, ASSISTANT_MESSAGE_TYPE, SYSTEM_MESSAGE_TYPE, WEB_CHANNEL, AGENT_CHANNEL, EMAIL_CHANNEL, createUserEventFromUIMessages, createAssistantEventFromUIMessages, convertToUIMessage, convertEventToModelMessages, convertEventsToModelMessages, convertModelMessageToEvent, } from "./events";
|
|
3
|
+
story, createStory, Story, } from "./story.js";
|
|
4
|
+
export { registerStory, getStory, getStoryFactory, hasStory, listStories, } from "./story.registry.js";
|
|
5
|
+
export { storyDomain } from "./schema.js";
|
|
6
|
+
export { didToolExecute } from "./story.toolcalls.js";
|
|
7
|
+
export { USER_MESSAGE_TYPE, ASSISTANT_MESSAGE_TYPE, SYSTEM_MESSAGE_TYPE, WEB_CHANNEL, AGENT_CHANNEL, EMAIL_CHANNEL, createUserEventFromUIMessages, createAssistantEventFromUIMessages, convertToUIMessage, convertEventToModelMessages, convertEventsToModelMessages, convertModelMessageToEvent, } from "./events.js";
|
package/dist/runtime.d.ts
CHANGED
|
@@ -8,6 +8,5 @@
|
|
|
8
8
|
* - Do NOT import this entrypoint from client/browser code.
|
|
9
9
|
* - Keep `@ekairos/story` main entrypoint safe to import from schema/domain modules.
|
|
10
10
|
*/
|
|
11
|
-
export { configureStoryRuntime, configureStoryRuntimeBootstrap,
|
|
11
|
+
export { configureStoryRuntime, configureStoryRuntimeBootstrap, isStoryRuntimeConfigured, resolveStoryRuntime, type StoryEnvironment, type StoryRuntime, type StoryRuntimeResolver, } from "./story.config";
|
|
12
12
|
export { createEkairosConfig, type EkairosConfig, type RegistrableStory, } from "./ekairos.config";
|
|
13
|
-
export { withEkairosRuntime } from "./next";
|
package/dist/runtime.js
CHANGED
|
@@ -8,6 +8,5 @@
|
|
|
8
8
|
* - Do NOT import this entrypoint from client/browser code.
|
|
9
9
|
* - Keep `@ekairos/story` main entrypoint safe to import from schema/domain modules.
|
|
10
10
|
*/
|
|
11
|
-
export { configureStoryRuntime, configureStoryRuntimeBootstrap,
|
|
11
|
+
export { configureStoryRuntime, configureStoryRuntimeBootstrap, isStoryRuntimeConfigured, resolveStoryRuntime, } from "./story.config";
|
|
12
12
|
export { createEkairosConfig, } from "./ekairos.config";
|
|
13
|
-
export { withEkairosRuntime } from "./next";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type ModelMessage } from "ai";
|
|
2
|
+
import type { StoryEnvironment } from "../story.config";
|
|
3
|
+
import type { ContextEvent, ContextIdentifier } from "../story.store";
|
|
4
|
+
import type { SerializableToolForModel } from "../tools-to-model-tools";
|
|
5
|
+
/**
|
|
6
|
+
* Executes a full "reaction" inside a single workflow step:
|
|
7
|
+
* - load events from store
|
|
8
|
+
* - convert events to model messages
|
|
9
|
+
* - run the streaming model call and emit chunks
|
|
10
|
+
* - extract tool calls from the resulting assistant event
|
|
11
|
+
*/
|
|
12
|
+
export declare function executeReaction(params: {
|
|
13
|
+
env: StoryEnvironment;
|
|
14
|
+
contextIdentifier: ContextIdentifier;
|
|
15
|
+
model: any;
|
|
16
|
+
system: string;
|
|
17
|
+
tools: Record<string, SerializableToolForModel>;
|
|
18
|
+
eventId: string;
|
|
19
|
+
maxSteps: number;
|
|
20
|
+
sendStart?: boolean;
|
|
21
|
+
silent?: boolean;
|
|
22
|
+
}): Promise<{
|
|
23
|
+
assistantEvent: ContextEvent;
|
|
24
|
+
toolCalls: any[];
|
|
25
|
+
messagesForModel: ModelMessage[];
|
|
26
|
+
}>;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { jsonSchema, gateway, smoothStream, stepCountIs, streamText, } from "ai";
|
|
2
|
+
import { getWritable } from "workflow";
|
|
3
|
+
import { resolveStoryRuntime } from "../story.config";
|
|
4
|
+
import { extractToolCallsFromParts } from "../story.toolcalls";
|
|
5
|
+
function safeErrorJson(error) {
|
|
6
|
+
const seen = new WeakSet();
|
|
7
|
+
const redactKey = (k) => /token|authorization|cookie|secret|api[_-]?key|password/i.test(k);
|
|
8
|
+
const err = error;
|
|
9
|
+
const payload = {
|
|
10
|
+
name: err?.name,
|
|
11
|
+
message: err?.message,
|
|
12
|
+
status: err?.status,
|
|
13
|
+
body: err?.body,
|
|
14
|
+
data: err?.data,
|
|
15
|
+
stack: err?.stack,
|
|
16
|
+
};
|
|
17
|
+
try {
|
|
18
|
+
return JSON.stringify(payload, (k, v) => {
|
|
19
|
+
if (redactKey(k))
|
|
20
|
+
return "[redacted]";
|
|
21
|
+
if (typeof v === "string" && v.length > 5000)
|
|
22
|
+
return "[truncated-string]";
|
|
23
|
+
if (typeof v === "object" && v !== null) {
|
|
24
|
+
if (seen.has(v))
|
|
25
|
+
return "[circular]";
|
|
26
|
+
seen.add(v);
|
|
27
|
+
}
|
|
28
|
+
return v;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return JSON.stringify({ message: String(err?.message ?? "error") });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Executes a full "reaction" inside a single workflow step:
|
|
37
|
+
* - load events from store
|
|
38
|
+
* - convert events to model messages
|
|
39
|
+
* - run the streaming model call and emit chunks
|
|
40
|
+
* - extract tool calls from the resulting assistant event
|
|
41
|
+
*/
|
|
42
|
+
export async function executeReaction(params) {
|
|
43
|
+
"use step";
|
|
44
|
+
const { store } = await resolveStoryRuntime(params.env);
|
|
45
|
+
console.log("[ekairos/story] reaction.step executeReaction begin");
|
|
46
|
+
let events;
|
|
47
|
+
try {
|
|
48
|
+
console.log("[ekairos/story] reaction.step store.getEvents begin");
|
|
49
|
+
events = await store.getEvents(params.contextIdentifier);
|
|
50
|
+
console.log("[ekairos/story] reaction.step store.getEvents ok");
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
console.error("[ekairos/story] reaction.step store.getEvents failed");
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
let messagesForModel;
|
|
57
|
+
try {
|
|
58
|
+
console.log("[ekairos/story] reaction.step store.eventsToModelMessages begin");
|
|
59
|
+
messagesForModel = (await store.eventsToModelMessages(events));
|
|
60
|
+
console.log("[ekairos/story] reaction.step store.eventsToModelMessages ok");
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
console.error("[ekairos/story] reaction.step store.eventsToModelMessages failed", safeErrorJson(error));
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
const writable = params.silent
|
|
67
|
+
? new WritableStream({ write() { } })
|
|
68
|
+
: getWritable();
|
|
69
|
+
// Match DurableAgent-style model init behavior:
|
|
70
|
+
const resolvedModel = typeof params.model === "string"
|
|
71
|
+
? gateway(params.model)
|
|
72
|
+
: typeof params.model === "function"
|
|
73
|
+
? await params.model()
|
|
74
|
+
: params.model;
|
|
75
|
+
// Wrap plain JSON Schema objects so the AI SDK doesn't attempt Zod conversion at runtime.
|
|
76
|
+
const toolsForStreamText = {};
|
|
77
|
+
for (const [name, t] of Object.entries(params.tools)) {
|
|
78
|
+
toolsForStreamText[name] = {
|
|
79
|
+
description: t?.description,
|
|
80
|
+
inputSchema: jsonSchema(t.inputSchema),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
console.log("[ekairos/story] reaction.step streamText begin");
|
|
84
|
+
const result = streamText({
|
|
85
|
+
model: resolvedModel,
|
|
86
|
+
system: params.system,
|
|
87
|
+
messages: messagesForModel,
|
|
88
|
+
tools: toolsForStreamText,
|
|
89
|
+
toolChoice: "required",
|
|
90
|
+
stopWhen: stepCountIs(params.maxSteps),
|
|
91
|
+
experimental_transform: smoothStream({ delayInMs: 30, chunking: "word" }),
|
|
92
|
+
});
|
|
93
|
+
console.log("[ekairos/story] reaction.step streamText ok");
|
|
94
|
+
// Ensure the underlying stream is consumed (AI SDK requirement)
|
|
95
|
+
result.consumeStream();
|
|
96
|
+
let resolveFinish;
|
|
97
|
+
let rejectFinish;
|
|
98
|
+
const finishPromise = new Promise((resolve, reject) => {
|
|
99
|
+
resolveFinish = resolve;
|
|
100
|
+
rejectFinish = reject;
|
|
101
|
+
});
|
|
102
|
+
const uiStream = result
|
|
103
|
+
.toUIMessageStream({
|
|
104
|
+
sendStart: Boolean(params.sendStart),
|
|
105
|
+
generateMessageId: () => params.eventId,
|
|
106
|
+
messageMetadata() {
|
|
107
|
+
return { eventId: params.eventId };
|
|
108
|
+
},
|
|
109
|
+
onFinish: ({ messages }) => {
|
|
110
|
+
const lastMessage = messages[messages.length - 1];
|
|
111
|
+
const event = {
|
|
112
|
+
id: params.eventId,
|
|
113
|
+
type: "assistant.message",
|
|
114
|
+
channel: "web",
|
|
115
|
+
createdAt: new Date().toISOString(),
|
|
116
|
+
content: { parts: lastMessage?.parts ?? [] },
|
|
117
|
+
};
|
|
118
|
+
resolveFinish(event);
|
|
119
|
+
},
|
|
120
|
+
onError: (e) => {
|
|
121
|
+
rejectFinish(e);
|
|
122
|
+
return e instanceof Error ? e.message : String(e);
|
|
123
|
+
},
|
|
124
|
+
})
|
|
125
|
+
// Filter out per-step finish boundary. Workflow will emit a single finish.
|
|
126
|
+
.pipeThrough(new TransformStream({
|
|
127
|
+
transform(chunk, controller) {
|
|
128
|
+
if (chunk.type === "finish")
|
|
129
|
+
return;
|
|
130
|
+
controller.enqueue(chunk);
|
|
131
|
+
},
|
|
132
|
+
}));
|
|
133
|
+
await uiStream.pipeTo(writable, { preventClose: true });
|
|
134
|
+
const assistantEvent = await finishPromise;
|
|
135
|
+
const toolCalls = extractToolCallsFromParts(assistantEvent?.content?.parts);
|
|
136
|
+
return { assistantEvent, toolCalls, messagesForModel };
|
|
137
|
+
}
|
|
@@ -1,43 +1,30 @@
|
|
|
1
|
-
import type { ModelMessage } from "ai";
|
|
2
1
|
import { type StoryEnvironment } from "../story.config";
|
|
3
2
|
import type { ContextEvent, ContextIdentifier, StoredContext } from "../story.store";
|
|
4
|
-
export declare function generateId(): Promise<string>;
|
|
5
|
-
export declare function getOrCreateContext<C>(env: StoryEnvironment, contextIdentifier: ContextIdentifier | null): Promise<{
|
|
6
|
-
context: StoredContext<C>;
|
|
7
|
-
isNew: boolean;
|
|
8
|
-
}>;
|
|
9
3
|
/**
|
|
10
|
-
*
|
|
4
|
+
* Initializes/ensures the story context exists and emits a single `data-context-id` chunk.
|
|
11
5
|
*
|
|
12
|
-
*
|
|
13
|
-
* - `getOrCreateContext(...)` and `writeContextIdChunk(...)` are semantically coupled.
|
|
14
|
-
* - Keeping them in a single step reduces step invocations per run (cheaper) without changing behavior.
|
|
6
|
+
* This is the "context init" boundary for the story engine.
|
|
15
7
|
*/
|
|
16
|
-
export declare function
|
|
8
|
+
export declare function initializeContext<C>(env: StoryEnvironment, contextIdentifier: ContextIdentifier | null, opts?: {
|
|
9
|
+
silent?: boolean;
|
|
10
|
+
}): Promise<{
|
|
17
11
|
context: StoredContext<C>;
|
|
18
12
|
isNew: boolean;
|
|
19
13
|
}>;
|
|
20
|
-
export declare function getContext<C>(env: StoryEnvironment, contextIdentifier: ContextIdentifier): Promise<StoredContext<C> | null>;
|
|
21
|
-
/**
|
|
22
|
-
* Loads the state needed for a single Story loop iteration.
|
|
23
|
-
*
|
|
24
|
-
* This is a "read aggregation" step: it groups read-only store calls into a single workflow step
|
|
25
|
-
* invocation to reduce step overhead (cheaper) without changing behavior.
|
|
26
|
-
*/
|
|
27
|
-
export declare function loadTurnState<C>(params: {
|
|
28
|
-
env: StoryEnvironment;
|
|
29
|
-
contextIdentifier: ContextIdentifier;
|
|
30
|
-
}): Promise<{
|
|
31
|
-
context: StoredContext<C> | null;
|
|
32
|
-
events: ContextEvent[];
|
|
33
|
-
}>;
|
|
34
14
|
export declare function updateContextContent<C>(env: StoryEnvironment, contextIdentifier: ContextIdentifier, content: C): Promise<StoredContext<C>>;
|
|
35
15
|
export declare function updateContextStatus(env: StoryEnvironment, contextIdentifier: ContextIdentifier, status: "open" | "streaming" | "closed"): Promise<void>;
|
|
36
|
-
export declare function
|
|
16
|
+
export declare function saveTriggerEvent(env: StoryEnvironment, contextIdentifier: ContextIdentifier, event: ContextEvent): Promise<ContextEvent>;
|
|
17
|
+
export declare function saveReactionEvent(env: StoryEnvironment, contextIdentifier: ContextIdentifier, event: ContextEvent): Promise<ContextEvent>;
|
|
37
18
|
export declare function updateEvent(env: StoryEnvironment, eventId: string, event: ContextEvent): Promise<ContextEvent>;
|
|
38
|
-
export declare function getEvents(env: StoryEnvironment, contextIdentifier: ContextIdentifier): Promise<ContextEvent[]>;
|
|
39
19
|
export declare function createExecution(env: StoryEnvironment, contextIdentifier: ContextIdentifier, triggerEventId: string, reactionEventId: string): Promise<{
|
|
40
20
|
id: string;
|
|
41
21
|
}>;
|
|
22
|
+
export declare function createReactionEvent(params: {
|
|
23
|
+
env: StoryEnvironment;
|
|
24
|
+
contextIdentifier: ContextIdentifier;
|
|
25
|
+
triggerEventId: string;
|
|
26
|
+
}): Promise<{
|
|
27
|
+
reactionEventId: string;
|
|
28
|
+
executionId: string;
|
|
29
|
+
}>;
|
|
42
30
|
export declare function completeExecution(env: StoryEnvironment, contextIdentifier: ContextIdentifier, executionId: string, status: "completed" | "failed"): Promise<void>;
|
|
43
|
-
export declare function eventsToModelMessages(env: StoryEnvironment, events: ContextEvent[]): Promise<ModelMessage[]>;
|
|
@@ -1,36 +1,11 @@
|
|
|
1
1
|
import { getWritable } from "workflow";
|
|
2
2
|
import { resolveStoryRuntime } from "../story.config";
|
|
3
|
-
export async function generateId() {
|
|
4
|
-
"use step";
|
|
5
|
-
// Use crypto.randomUUID when available (Node 18+)
|
|
6
|
-
const uuid = globalThis.crypto?.randomUUID?.();
|
|
7
|
-
if (uuid)
|
|
8
|
-
return uuid;
|
|
9
|
-
// Fallback
|
|
10
|
-
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
11
|
-
}
|
|
12
|
-
export async function getOrCreateContext(env, contextIdentifier) {
|
|
13
|
-
"use step";
|
|
14
|
-
const { store } = await resolveStoryRuntime(env);
|
|
15
|
-
// Detect creation explicitly so the engine can run onContextCreated hooks.
|
|
16
|
-
if (!contextIdentifier) {
|
|
17
|
-
const context = await store.getOrCreateContext(null);
|
|
18
|
-
return { context, isNew: true };
|
|
19
|
-
}
|
|
20
|
-
const existing = await store.getContext(contextIdentifier);
|
|
21
|
-
if (existing)
|
|
22
|
-
return { context: existing, isNew: false };
|
|
23
|
-
const created = await store.getOrCreateContext(contextIdentifier);
|
|
24
|
-
return { context: created, isNew: true };
|
|
25
|
-
}
|
|
26
3
|
/**
|
|
27
|
-
*
|
|
4
|
+
* Initializes/ensures the story context exists and emits a single `data-context-id` chunk.
|
|
28
5
|
*
|
|
29
|
-
*
|
|
30
|
-
* - `getOrCreateContext(...)` and `writeContextIdChunk(...)` are semantically coupled.
|
|
31
|
-
* - Keeping them in a single step reduces step invocations per run (cheaper) without changing behavior.
|
|
6
|
+
* This is the "context init" boundary for the story engine.
|
|
32
7
|
*/
|
|
33
|
-
export async function
|
|
8
|
+
export async function initializeContext(env, contextIdentifier, opts) {
|
|
34
9
|
"use step";
|
|
35
10
|
const { store } = await resolveStoryRuntime(env);
|
|
36
11
|
// Detect creation explicitly so the engine can run onContextCreated hooks.
|
|
@@ -49,38 +24,24 @@ export async function ensureContextAndEmitContextId(env, contextIdentifier) {
|
|
|
49
24
|
result = { context: created, isNew: true };
|
|
50
25
|
}
|
|
51
26
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
27
|
+
// If we're running in a non-streaming context (e.g. tests or headless usage),
|
|
28
|
+
// we skip writing stream chunks entirely.
|
|
29
|
+
if (!opts?.silent) {
|
|
30
|
+
const writable = getWritable();
|
|
31
|
+
const writer = writable.getWriter();
|
|
32
|
+
try {
|
|
33
|
+
await writer.write({
|
|
34
|
+
type: "data-context-id",
|
|
35
|
+
id: String(result.context.id),
|
|
36
|
+
data: { contextId: String(result.context.id) },
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
writer.releaseLock();
|
|
41
|
+
}
|
|
63
42
|
}
|
|
64
43
|
return result;
|
|
65
44
|
}
|
|
66
|
-
export async function getContext(env, contextIdentifier) {
|
|
67
|
-
"use step";
|
|
68
|
-
const { store } = await resolveStoryRuntime(env);
|
|
69
|
-
return await store.getContext(contextIdentifier);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Loads the state needed for a single Story loop iteration.
|
|
73
|
-
*
|
|
74
|
-
* This is a "read aggregation" step: it groups read-only store calls into a single workflow step
|
|
75
|
-
* invocation to reduce step overhead (cheaper) without changing behavior.
|
|
76
|
-
*/
|
|
77
|
-
export async function loadTurnState(params) {
|
|
78
|
-
"use step";
|
|
79
|
-
const { store } = await resolveStoryRuntime(params.env);
|
|
80
|
-
const context = await store.getContext(params.contextIdentifier);
|
|
81
|
-
const events = await store.getEvents(params.contextIdentifier);
|
|
82
|
-
return { context, events };
|
|
83
|
-
}
|
|
84
45
|
export async function updateContextContent(env, contextIdentifier, content) {
|
|
85
46
|
"use step";
|
|
86
47
|
const { store } = await resolveStoryRuntime(env);
|
|
@@ -91,33 +52,40 @@ export async function updateContextStatus(env, contextIdentifier, status) {
|
|
|
91
52
|
const { store } = await resolveStoryRuntime(env);
|
|
92
53
|
await store.updateContextStatus(contextIdentifier, status);
|
|
93
54
|
}
|
|
94
|
-
export async function
|
|
55
|
+
export async function saveTriggerEvent(env, contextIdentifier, event) {
|
|
95
56
|
"use step";
|
|
96
57
|
const { store } = await resolveStoryRuntime(env);
|
|
97
58
|
return await store.saveEvent(contextIdentifier, event);
|
|
98
59
|
}
|
|
99
|
-
export async function
|
|
60
|
+
export async function saveReactionEvent(env, contextIdentifier, event) {
|
|
100
61
|
"use step";
|
|
101
62
|
const { store } = await resolveStoryRuntime(env);
|
|
102
|
-
return await store.
|
|
63
|
+
return await store.saveEvent(contextIdentifier, event);
|
|
103
64
|
}
|
|
104
|
-
export async function
|
|
65
|
+
export async function updateEvent(env, eventId, event) {
|
|
105
66
|
"use step";
|
|
106
67
|
const { store } = await resolveStoryRuntime(env);
|
|
107
|
-
return await store.
|
|
68
|
+
return await store.updateEvent(eventId, event);
|
|
108
69
|
}
|
|
109
70
|
export async function createExecution(env, contextIdentifier, triggerEventId, reactionEventId) {
|
|
110
71
|
"use step";
|
|
111
72
|
const { store } = await resolveStoryRuntime(env);
|
|
112
73
|
return await store.createExecution(contextIdentifier, triggerEventId, reactionEventId);
|
|
113
74
|
}
|
|
114
|
-
export async function
|
|
75
|
+
export async function createReactionEvent(params) {
|
|
115
76
|
"use step";
|
|
116
|
-
const { store } = await resolveStoryRuntime(env);
|
|
117
|
-
|
|
77
|
+
const { store } = await resolveStoryRuntime(params.env);
|
|
78
|
+
// Generate a new reaction event id inside the step boundary.
|
|
79
|
+
const uuid = globalThis.crypto?.randomUUID?.();
|
|
80
|
+
const reactionEventId = typeof uuid === "string"
|
|
81
|
+
? uuid
|
|
82
|
+
: `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
83
|
+
await store.updateContextStatus(params.contextIdentifier, "streaming");
|
|
84
|
+
const execution = await store.createExecution(params.contextIdentifier, params.triggerEventId, reactionEventId);
|
|
85
|
+
return { reactionEventId, executionId: execution.id };
|
|
118
86
|
}
|
|
119
|
-
export async function
|
|
87
|
+
export async function completeExecution(env, contextIdentifier, executionId, status) {
|
|
120
88
|
"use step";
|
|
121
89
|
const { store } = await resolveStoryRuntime(env);
|
|
122
|
-
|
|
90
|
+
await store.completeExecution(contextIdentifier, executionId, status);
|
|
123
91
|
}
|
|
@@ -11,6 +11,13 @@ export declare function writeContextSubstate(params: {
|
|
|
11
11
|
export declare function writeContextIdChunk(params: {
|
|
12
12
|
contextId: string;
|
|
13
13
|
}): Promise<void>;
|
|
14
|
+
export declare function writeStoryPing(params: {
|
|
15
|
+
/**
|
|
16
|
+
* Simple ping event to validate that the workflow stream is alive.
|
|
17
|
+
* This is intentionally generic so clients can ignore it safely.
|
|
18
|
+
*/
|
|
19
|
+
label?: string;
|
|
20
|
+
}): Promise<void>;
|
|
14
21
|
export declare function writeToolOutputs(params: {
|
|
15
22
|
results: Array<{
|
|
16
23
|
toolCallId: string;
|
|
@@ -29,6 +29,21 @@ export async function writeContextIdChunk(params) {
|
|
|
29
29
|
writer.releaseLock();
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
+
export async function writeStoryPing(params) {
|
|
33
|
+
"use step";
|
|
34
|
+
const writable = getWritable();
|
|
35
|
+
const writer = writable.getWriter();
|
|
36
|
+
try {
|
|
37
|
+
await writer.write({
|
|
38
|
+
type: "data-story-ping",
|
|
39
|
+
data: { label: params.label ?? "story-ping" },
|
|
40
|
+
transient: true,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
writer.releaseLock();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
32
47
|
export async function writeToolOutputs(params) {
|
|
33
48
|
"use step";
|
|
34
49
|
const writable = getWritable();
|
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
* Parses a document with LlamaParse and stores it in InstantDB (document_documents + link to file).
|
|
3
3
|
* Returns the created documentId.
|
|
4
4
|
*/
|
|
5
|
-
export declare function parseAndStoreDocument(db: any, buffer: Buffer, fileName: string,
|
|
5
|
+
export declare function parseAndStoreDocument(db: any, buffer: Buffer, fileName: string, fileId: string): Promise<string>;
|