@ekairos/events 1.22.34-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.
- package/README.md +58 -83
- package/dist/codex.d.ts +11 -2
- package/dist/codex.js +16 -8
- package/dist/context.action-calls.d.ts +48 -0
- package/dist/context.action-calls.js +123 -0
- package/dist/context.action.d.ts +55 -0
- package/dist/context.action.js +25 -0
- package/dist/context.builder.d.ts +71 -43
- package/dist/context.builder.js +123 -28
- package/dist/context.config.d.ts +2 -1
- package/dist/context.config.js +8 -3
- package/dist/context.contract.d.ts +2 -4
- package/dist/context.contract.js +3 -9
- package/dist/context.d.ts +3 -2
- package/dist/context.engine.d.ts +75 -46
- package/dist/context.engine.js +538 -302
- package/dist/context.events.js +28 -87
- package/dist/context.js +1 -0
- package/dist/context.part-identity.d.ts +40 -0
- package/dist/context.part-identity.js +270 -0
- package/dist/context.parts.d.ts +389 -164
- package/dist/context.parts.js +343 -218
- package/dist/context.registry.d.ts +1 -1
- package/dist/context.runtime.d.ts +21 -0
- package/dist/context.runtime.js +39 -0
- package/dist/context.step-stream.d.ts +16 -2
- package/dist/context.step-stream.js +58 -16
- package/dist/context.store.d.ts +63 -10
- package/dist/context.stream.d.ts +14 -4
- package/dist/context.stream.js +31 -3
- package/dist/domain.d.ts +1 -0
- package/dist/domain.js +1 -0
- package/dist/index.d.ts +13 -10
- package/dist/index.js +7 -6
- package/dist/react.context-event-parts.d.ts +18 -0
- package/dist/react.context-event-parts.js +509 -0
- package/dist/react.d.ts +7 -42
- package/dist/react.js +4 -87
- package/dist/react.step-stream.d.ts +39 -0
- package/dist/react.step-stream.js +625 -0
- package/dist/react.types.d.ts +121 -0
- package/dist/react.types.js +2 -0
- package/dist/react.use-context.d.ts +7 -0
- package/dist/react.use-context.js +867 -0
- package/dist/reactors/ai-sdk.chunk-map.d.ts +1 -0
- package/dist/reactors/ai-sdk.chunk-map.js +56 -5
- package/dist/reactors/ai-sdk.reactor.d.ts +8 -5
- package/dist/reactors/ai-sdk.reactor.js +10 -9
- package/dist/reactors/ai-sdk.step.d.ts +6 -6
- package/dist/reactors/ai-sdk.step.js +32 -24
- package/dist/reactors/scripted.reactor.d.ts +7 -4
- package/dist/reactors/types.d.ts +23 -8
- package/dist/runtime.d.ts +6 -0
- package/dist/runtime.js +9 -0
- package/dist/runtime.step.js +2 -2
- package/dist/schema.d.ts +268 -2
- package/dist/schema.js +5 -9
- package/dist/steps/do-context-stream-step.d.ts +2 -2
- package/dist/steps/do-context-stream-step.js +6 -8
- package/dist/steps/durable.steps.d.ts +28 -0
- package/dist/steps/durable.steps.js +34 -0
- package/dist/steps/store.steps.d.ts +121 -39
- package/dist/steps/store.steps.js +266 -111
- package/dist/steps/stream.steps.d.ts +36 -3
- package/dist/steps/stream.steps.js +137 -14
- package/dist/steps/trace.steps.d.ts +4 -2
- package/dist/steps/trace.steps.js +26 -8
- package/dist/stores/instant.store.d.ts +15 -11
- package/dist/stores/instant.store.js +155 -6
- package/dist/tools-to-model-tools.d.ts +39 -3
- package/dist/tools-to-model-tools.js +63 -6
- package/package.json +20 -6
- package/dist/context.toolcalls.d.ts +0 -60
- package/dist/context.toolcalls.js +0 -117
|
@@ -8,5 +8,6 @@ export type MapAiSdkChunkToContextEventParams = {
|
|
|
8
8
|
itemId?: string;
|
|
9
9
|
provider?: string;
|
|
10
10
|
sequence: number;
|
|
11
|
+
actionNameByRef?: Map<string, string>;
|
|
11
12
|
};
|
|
12
13
|
export declare function mapAiSdkChunkToContextEvent(params: MapAiSdkChunkToContextEventParams): ChunkEmittedEvent;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { resolveContextPartChunkIdentity } from "../context.part-identity.js";
|
|
1
2
|
const REDACT_KEY = /token|authorization|cookie|secret|api[_-]?key|password/i;
|
|
2
3
|
function asRecord(value) {
|
|
3
4
|
if (!value || typeof value !== "object")
|
|
@@ -64,7 +65,7 @@ function mapAiSdkChunkType(providerChunkType) {
|
|
|
64
65
|
return "chunk.reasoning_end";
|
|
65
66
|
case "tool-input-start":
|
|
66
67
|
case "tool-call-start":
|
|
67
|
-
return "chunk.
|
|
68
|
+
return "chunk.action_started";
|
|
68
69
|
case "tool-input-delta":
|
|
69
70
|
case "tool-call-delta":
|
|
70
71
|
return "chunk.action_input_delta";
|
|
@@ -72,11 +73,11 @@ function mapAiSdkChunkType(providerChunkType) {
|
|
|
72
73
|
case "tool-input-end":
|
|
73
74
|
case "tool-call":
|
|
74
75
|
case "tool-call-end":
|
|
75
|
-
return "chunk.
|
|
76
|
+
return "chunk.action_started";
|
|
76
77
|
case "tool-output-available":
|
|
77
|
-
return "chunk.
|
|
78
|
+
return "chunk.action_completed";
|
|
78
79
|
case "tool-output-error":
|
|
79
|
-
return "chunk.
|
|
80
|
+
return "chunk.action_failed";
|
|
80
81
|
case "source-url":
|
|
81
82
|
return "chunk.source_url";
|
|
82
83
|
case "source-document":
|
|
@@ -99,9 +100,15 @@ function buildNormalizedData(chunk) {
|
|
|
99
100
|
"id",
|
|
100
101
|
"text",
|
|
101
102
|
"delta",
|
|
103
|
+
"inputTextDelta",
|
|
104
|
+
"outputTextDelta",
|
|
102
105
|
"state",
|
|
106
|
+
"actionName",
|
|
103
107
|
"toolName",
|
|
104
108
|
"toolCallId",
|
|
109
|
+
"input",
|
|
110
|
+
"output",
|
|
111
|
+
"error",
|
|
105
112
|
"finishReason",
|
|
106
113
|
"url",
|
|
107
114
|
"title",
|
|
@@ -119,12 +126,52 @@ function buildNormalizedData(chunk) {
|
|
|
119
126
|
}
|
|
120
127
|
return toJsonSafe(normalized) ?? {};
|
|
121
128
|
}
|
|
129
|
+
function readProviderPartId(chunk, chunkType) {
|
|
130
|
+
if (chunkType.startsWith("chunk.action_")) {
|
|
131
|
+
return readString(chunk, "toolCallId") ?? readString(chunk, "id");
|
|
132
|
+
}
|
|
133
|
+
if (chunkType.startsWith("chunk.text_") || chunkType.startsWith("chunk.reasoning_")) {
|
|
134
|
+
return readString(chunk, "id");
|
|
135
|
+
}
|
|
136
|
+
if (chunkType.startsWith("chunk.source_")) {
|
|
137
|
+
return readString(chunk, "sourceId");
|
|
138
|
+
}
|
|
139
|
+
if (chunkType === "chunk.file") {
|
|
140
|
+
return readString(chunk, "id") ?? readString(chunk, "url");
|
|
141
|
+
}
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
122
144
|
export function mapAiSdkChunkToContextEvent(params) {
|
|
123
145
|
const chunk = asRecord(params.chunk);
|
|
124
146
|
const providerChunkType = readString(chunk, "type") ?? "unknown";
|
|
125
147
|
const chunkType = mapAiSdkChunkType(providerChunkType);
|
|
148
|
+
const providerPartId = readProviderPartId(chunk, chunkType);
|
|
149
|
+
const identity = resolveContextPartChunkIdentity({
|
|
150
|
+
stepId: params.stepId,
|
|
151
|
+
provider: params.provider ?? "ai-sdk",
|
|
152
|
+
providerPartId,
|
|
153
|
+
chunkType,
|
|
154
|
+
});
|
|
126
155
|
const actionRef = readString(chunk, "toolCallId") ??
|
|
127
156
|
readString(chunk, "id");
|
|
157
|
+
let data = buildNormalizedData(chunk);
|
|
158
|
+
if (chunkType.startsWith("chunk.action_") && actionRef) {
|
|
159
|
+
const observedActionName = readString(chunk, "actionName") ??
|
|
160
|
+
readString(chunk, "toolName") ??
|
|
161
|
+
readString(data, "actionName") ??
|
|
162
|
+
readString(data, "toolName");
|
|
163
|
+
if (observedActionName) {
|
|
164
|
+
params.actionNameByRef?.set(actionRef, observedActionName);
|
|
165
|
+
}
|
|
166
|
+
const knownActionName = observedActionName ?? params.actionNameByRef?.get(actionRef);
|
|
167
|
+
if (knownActionName) {
|
|
168
|
+
data = {
|
|
169
|
+
...data,
|
|
170
|
+
actionName: readString(data, "actionName") ?? knownActionName,
|
|
171
|
+
toolName: readString(data, "toolName") ?? knownActionName,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
128
175
|
return {
|
|
129
176
|
type: "chunk.emitted",
|
|
130
177
|
at: new Date().toISOString(),
|
|
@@ -133,11 +180,15 @@ export function mapAiSdkChunkToContextEvent(params) {
|
|
|
133
180
|
executionId: params.executionId,
|
|
134
181
|
stepId: params.stepId,
|
|
135
182
|
itemId: params.itemId,
|
|
183
|
+
partId: identity?.partId,
|
|
184
|
+
providerPartId: identity?.providerPartId,
|
|
185
|
+
partType: identity?.partType,
|
|
186
|
+
partSlot: identity?.partSlot,
|
|
136
187
|
actionRef: chunkType.startsWith("chunk.action_") ? actionRef : undefined,
|
|
137
188
|
provider: params.provider,
|
|
138
189
|
providerChunkType,
|
|
139
190
|
sequence: params.sequence,
|
|
140
|
-
data
|
|
191
|
+
data,
|
|
141
192
|
raw: sanitizeRaw(chunk),
|
|
142
193
|
};
|
|
143
194
|
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import type { ContextEnvironment } from "../context.config.js";
|
|
2
|
+
import type { DomainLike } from "@ekairos/domain";
|
|
3
|
+
import type { ContextRuntime, ContextRuntimeHandleForDomain } from "../context.runtime.js";
|
|
2
4
|
import type { ContextModelInit } from "../context.engine.js";
|
|
3
5
|
import type { ContextIdentifier, StoredContext, ContextItem } from "../context.store.js";
|
|
6
|
+
import { eventsDomain } from "../schema.js";
|
|
4
7
|
import type { ContextReactor } from "./types.js";
|
|
5
|
-
export type CreateAiSdkReactorOptions<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, Config = unknown> = {
|
|
8
|
+
export type CreateAiSdkReactorOptions<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>, Config = unknown> = {
|
|
6
9
|
resolveConfig?: (params: {
|
|
7
|
-
|
|
10
|
+
runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>;
|
|
8
11
|
context: StoredContext<Context>;
|
|
9
12
|
contextIdentifier: ContextIdentifier;
|
|
10
13
|
triggerEvent: ContextItem;
|
|
@@ -16,18 +19,18 @@ export type CreateAiSdkReactorOptions<Context = unknown, Env extends ContextEnvi
|
|
|
16
19
|
iteration: number;
|
|
17
20
|
}) => Promise<Config> | Config;
|
|
18
21
|
selectModel?: (params: {
|
|
19
|
-
|
|
22
|
+
runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>;
|
|
20
23
|
context: StoredContext<Context>;
|
|
21
24
|
triggerEvent: ContextItem;
|
|
22
25
|
baseModel: ContextModelInit;
|
|
23
26
|
config: Config;
|
|
24
27
|
}) => Promise<ContextModelInit> | ContextModelInit;
|
|
25
28
|
selectMaxModelSteps?: (params: {
|
|
26
|
-
|
|
29
|
+
runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>;
|
|
27
30
|
context: StoredContext<Context>;
|
|
28
31
|
triggerEvent: ContextItem;
|
|
29
32
|
baseMaxModelSteps: number;
|
|
30
33
|
config: Config;
|
|
31
34
|
}) => Promise<number> | number;
|
|
32
35
|
};
|
|
33
|
-
export declare function createAiSdkReactor<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, Config = unknown>(options?: CreateAiSdkReactorOptions<Context, Env, Config>): ContextReactor<Context, Env>;
|
|
36
|
+
export declare function createAiSdkReactor<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>, Config = unknown>(options?: CreateAiSdkReactorOptions<Context, Env, RequiredDomain, Runtime, Config>): ContextReactor<Context, Env, RequiredDomain, Runtime>;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { executeAiSdkReaction } from "./ai-sdk.step.js";
|
|
2
|
+
import { actionsToActionSpecs } from "../tools-to-model-tools.js";
|
|
2
3
|
export function createAiSdkReactor(options) {
|
|
3
4
|
return async (params) => {
|
|
4
5
|
let config;
|
|
5
6
|
if (options?.resolveConfig) {
|
|
6
7
|
config = await options.resolveConfig({
|
|
7
|
-
|
|
8
|
+
runtime: params.runtime,
|
|
8
9
|
context: params.context,
|
|
9
10
|
contextIdentifier: params.contextIdentifier,
|
|
10
11
|
triggerEvent: params.triggerEvent,
|
|
@@ -18,7 +19,7 @@ export function createAiSdkReactor(options) {
|
|
|
18
19
|
}
|
|
19
20
|
const model = options?.selectModel && config !== undefined
|
|
20
21
|
? await options.selectModel({
|
|
21
|
-
|
|
22
|
+
runtime: params.runtime,
|
|
22
23
|
context: params.context,
|
|
23
24
|
triggerEvent: params.triggerEvent,
|
|
24
25
|
baseModel: params.model,
|
|
@@ -27,7 +28,7 @@ export function createAiSdkReactor(options) {
|
|
|
27
28
|
: params.model;
|
|
28
29
|
const maxSteps = options?.selectMaxModelSteps && config !== undefined
|
|
29
30
|
? await options.selectMaxModelSteps({
|
|
30
|
-
|
|
31
|
+
runtime: params.runtime,
|
|
31
32
|
context: params.context,
|
|
32
33
|
triggerEvent: params.triggerEvent,
|
|
33
34
|
baseMaxModelSteps: params.maxModelSteps,
|
|
@@ -35,16 +36,16 @@ export function createAiSdkReactor(options) {
|
|
|
35
36
|
})
|
|
36
37
|
: params.maxModelSteps;
|
|
37
38
|
const result = await executeAiSdkReaction({
|
|
38
|
-
|
|
39
|
+
runtime: params.runtime,
|
|
39
40
|
contextIdentifier: params.contextIdentifier,
|
|
41
|
+
events: params.events,
|
|
40
42
|
model,
|
|
41
43
|
system: params.systemPrompt,
|
|
42
|
-
tools: params.
|
|
44
|
+
tools: actionsToActionSpecs(params.actions),
|
|
43
45
|
eventId: params.eventId,
|
|
44
46
|
iteration: params.iteration,
|
|
45
47
|
maxSteps,
|
|
46
48
|
sendStart: params.sendStart,
|
|
47
|
-
silent: params.silent,
|
|
48
49
|
contextStepStream: params.contextStepStream,
|
|
49
50
|
writable: params.writable,
|
|
50
51
|
executionId: params.executionId,
|
|
@@ -53,9 +54,9 @@ export function createAiSdkReactor(options) {
|
|
|
53
54
|
});
|
|
54
55
|
return {
|
|
55
56
|
assistantEvent: result.assistantEvent,
|
|
56
|
-
actionRequests: result.
|
|
57
|
-
actionRef: String(entry.
|
|
58
|
-
actionName: String(entry.
|
|
57
|
+
actionRequests: result.actionCalls.map((entry) => ({
|
|
58
|
+
actionRef: String(entry.actionCallId),
|
|
59
|
+
actionName: String(entry.actionName),
|
|
59
60
|
input: entry.args,
|
|
60
61
|
})),
|
|
61
62
|
messagesForModel: result.messagesForModel,
|
|
@@ -2,7 +2,7 @@ import type { ModelMessage, UIMessageChunk } from "ai";
|
|
|
2
2
|
import type { ContextEnvironment } from "../context.config.js";
|
|
3
3
|
import type { ContextModelInit } from "../context.engine.js";
|
|
4
4
|
import type { ContextItem, ContextIdentifier } from "../context.store.js";
|
|
5
|
-
import type
|
|
5
|
+
import { type SerializableActionSpec } from "../tools-to-model-tools.js";
|
|
6
6
|
/**
|
|
7
7
|
* AI SDK-backed reaction execution inside a single workflow step.
|
|
8
8
|
*
|
|
@@ -13,17 +13,17 @@ import type { SerializableToolForModel } from "../tools-to-model-tools.js";
|
|
|
13
13
|
* - map provider chunks to the Context stream contract
|
|
14
14
|
* - emit UI chunks and persist step stream chunks
|
|
15
15
|
*/
|
|
16
|
-
export declare function executeAiSdkReaction(params: {
|
|
17
|
-
|
|
16
|
+
export declare function executeAiSdkReaction<Env extends ContextEnvironment = ContextEnvironment>(params: {
|
|
17
|
+
runtime: import("../context.runtime.js").ContextRuntimeServiceHandle;
|
|
18
18
|
contextIdentifier: ContextIdentifier;
|
|
19
|
+
events?: ContextItem[];
|
|
19
20
|
model: ContextModelInit;
|
|
20
21
|
system: string;
|
|
21
|
-
tools: Record<string,
|
|
22
|
+
tools: Record<string, SerializableActionSpec>;
|
|
22
23
|
eventId: string;
|
|
23
24
|
iteration?: number;
|
|
24
25
|
maxSteps: number;
|
|
25
26
|
sendStart?: boolean;
|
|
26
|
-
silent?: boolean;
|
|
27
27
|
contextStepStream?: WritableStream<string>;
|
|
28
28
|
writable?: WritableStream<UIMessageChunk>;
|
|
29
29
|
executionId?: string;
|
|
@@ -31,7 +31,7 @@ export declare function executeAiSdkReaction(params: {
|
|
|
31
31
|
stepId?: string;
|
|
32
32
|
}): Promise<{
|
|
33
33
|
assistantEvent: ContextItem;
|
|
34
|
-
|
|
34
|
+
actionCalls: any[];
|
|
35
35
|
messagesForModel: ModelMessage[];
|
|
36
36
|
llm?: {
|
|
37
37
|
provider?: string;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { getContextRuntimeServices } from "../context.runtime.js";
|
|
1
2
|
import { OUTPUT_ITEM_TYPE } from "../context.events.js";
|
|
2
3
|
import { createContextStepStreamChunk, encodeContextStepStreamChunk, } from "../context.step-stream.js";
|
|
3
4
|
import { mapAiSdkChunkToContextEvent } from "./ai-sdk.chunk-map.js";
|
|
5
|
+
import { actionSpecToAiSdkTool, } from "../tools-to-model-tools.js";
|
|
4
6
|
import { writeContextTraceEvents } from "../steps/trace.steps.js";
|
|
5
7
|
async function readWorkflowMetadata() {
|
|
6
8
|
try {
|
|
@@ -11,7 +13,7 @@ async function readWorkflowMetadata() {
|
|
|
11
13
|
return null;
|
|
12
14
|
}
|
|
13
15
|
}
|
|
14
|
-
async function resolveWorkflowRunId(
|
|
16
|
+
async function resolveWorkflowRunId(runtime, executionId) {
|
|
15
17
|
let runId = "";
|
|
16
18
|
const meta = await readWorkflowMetadata();
|
|
17
19
|
if (meta && meta.workflowRunId !== undefined && meta.workflowRunId !== null) {
|
|
@@ -19,9 +21,7 @@ async function resolveWorkflowRunId(env, executionId) {
|
|
|
19
21
|
}
|
|
20
22
|
if (!runId && executionId) {
|
|
21
23
|
try {
|
|
22
|
-
const
|
|
23
|
-
const runtime = await getContextRuntime(env);
|
|
24
|
-
const db = runtime?.db;
|
|
24
|
+
const db = await runtime.db();
|
|
25
25
|
if (db) {
|
|
26
26
|
const q = await db.query({
|
|
27
27
|
event_executions: {
|
|
@@ -82,15 +82,16 @@ function safeErrorJson(error) {
|
|
|
82
82
|
*/
|
|
83
83
|
export async function executeAiSdkReaction(params) {
|
|
84
84
|
"use step";
|
|
85
|
-
const {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
85
|
+
const { store } = await getContextRuntimeServices(params.runtime);
|
|
86
|
+
let events = Array.isArray(params.events) ? params.events : [];
|
|
87
|
+
if (events.length === 0) {
|
|
88
|
+
try {
|
|
89
|
+
events = await store.getItems(params.contextIdentifier);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
console.error("[ekairos/story] ai-sdk.step store.getItems failed");
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
94
95
|
}
|
|
95
96
|
let messagesForModel;
|
|
96
97
|
try {
|
|
@@ -101,7 +102,7 @@ export async function executeAiSdkReaction(params) {
|
|
|
101
102
|
throw error;
|
|
102
103
|
}
|
|
103
104
|
const { jsonSchema, gateway, smoothStream, stepCountIs, streamText } = await import("ai");
|
|
104
|
-
const {
|
|
105
|
+
const { extractActionCallsFromParts } = await import("../context.action-calls.js");
|
|
105
106
|
const resolvedModel = typeof params.model === "string"
|
|
106
107
|
? gateway(params.model)
|
|
107
108
|
: typeof params.model === "function"
|
|
@@ -111,10 +112,7 @@ export async function executeAiSdkReaction(params) {
|
|
|
111
112
|
})();
|
|
112
113
|
const toolsForStreamText = {};
|
|
113
114
|
for (const [name, t] of Object.entries(params.tools)) {
|
|
114
|
-
toolsForStreamText[name] =
|
|
115
|
-
description: t?.description,
|
|
116
|
-
inputSchema: jsonSchema(t.inputSchema),
|
|
117
|
-
};
|
|
115
|
+
toolsForStreamText[name] = actionSpecToAiSdkTool(name, t, jsonSchema);
|
|
118
116
|
}
|
|
119
117
|
const startedAtMs = Date.now();
|
|
120
118
|
const result = streamText({
|
|
@@ -135,8 +133,9 @@ export async function executeAiSdkReaction(params) {
|
|
|
135
133
|
rejectFinish = reject;
|
|
136
134
|
});
|
|
137
135
|
const modelId = typeof params.model === "string" ? params.model : "";
|
|
138
|
-
const mappedProvider = modelId.includes("/") ? modelId.split("/")[0] :
|
|
136
|
+
const mappedProvider = modelId.includes("/") ? modelId.split("/")[0] || "ai-sdk" : "ai-sdk";
|
|
139
137
|
const contextStepStreamWriter = params.contextStepStream?.getWriter();
|
|
138
|
+
const actionNameByRef = new Map();
|
|
140
139
|
try {
|
|
141
140
|
const uiStream = result
|
|
142
141
|
.toUIMessageStream({
|
|
@@ -175,11 +174,17 @@ export async function executeAiSdkReaction(params) {
|
|
|
175
174
|
itemId: params.eventId,
|
|
176
175
|
provider: mappedProvider,
|
|
177
176
|
sequence: ++chunkSequence,
|
|
177
|
+
actionNameByRef,
|
|
178
178
|
});
|
|
179
179
|
const persistedChunk = createContextStepStreamChunk({
|
|
180
180
|
at: mapped.at,
|
|
181
181
|
sequence: mapped.sequence,
|
|
182
182
|
chunkType: mapped.chunkType,
|
|
183
|
+
stepId: params.stepId,
|
|
184
|
+
partId: mapped.partId,
|
|
185
|
+
providerPartId: mapped.providerPartId,
|
|
186
|
+
partType: mapped.partType,
|
|
187
|
+
partSlot: mapped.partSlot,
|
|
183
188
|
provider: mapped.provider,
|
|
184
189
|
providerChunkType: mapped.providerChunkType,
|
|
185
190
|
actionRef: mapped.actionRef,
|
|
@@ -221,11 +226,14 @@ export async function executeAiSdkReaction(params) {
|
|
|
221
226
|
}
|
|
222
227
|
}
|
|
223
228
|
finally {
|
|
224
|
-
contextStepStreamWriter
|
|
229
|
+
const streamWriter = contextStepStreamWriter;
|
|
230
|
+
if (typeof streamWriter?.releaseLock === "function") {
|
|
231
|
+
streamWriter.releaseLock();
|
|
232
|
+
}
|
|
225
233
|
}
|
|
226
234
|
const assistantEvent = await finishPromise;
|
|
227
235
|
const finishedAtMs = Date.now();
|
|
228
|
-
const
|
|
236
|
+
const actionCalls = extractActionCallsFromParts(assistantEvent?.content?.parts);
|
|
229
237
|
const latencyMs = Math.max(0, finishedAtMs - startedAtMs);
|
|
230
238
|
let usage = undefined;
|
|
231
239
|
let providerMetadata = undefined;
|
|
@@ -291,10 +299,10 @@ export async function executeAiSdkReaction(params) {
|
|
|
291
299
|
rawProviderMetadata: providerMetadataJson,
|
|
292
300
|
};
|
|
293
301
|
try {
|
|
294
|
-
const runId = await resolveWorkflowRunId(params.
|
|
302
|
+
const runId = await resolveWorkflowRunId(params.runtime, params.executionId);
|
|
295
303
|
if (runId && llm) {
|
|
296
304
|
await writeContextTraceEvents({
|
|
297
|
-
|
|
305
|
+
runtime: params.runtime,
|
|
298
306
|
events: [
|
|
299
307
|
{
|
|
300
308
|
workflowRunId: runId,
|
|
@@ -339,5 +347,5 @@ export async function executeAiSdkReaction(params) {
|
|
|
339
347
|
catch {
|
|
340
348
|
// tracing must not break reaction
|
|
341
349
|
}
|
|
342
|
-
return { assistantEvent,
|
|
350
|
+
return { assistantEvent, actionCalls, messagesForModel, llm };
|
|
343
351
|
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { ModelMessage } from "ai";
|
|
2
|
+
import type { DomainLike } from "@ekairos/domain";
|
|
2
3
|
import type { ContextEnvironment } from "../context.config.js";
|
|
4
|
+
import type { ContextRuntime } from "../context.runtime.js";
|
|
3
5
|
import type { ContextItem } from "../context.store.js";
|
|
6
|
+
import { eventsDomain } from "../schema.js";
|
|
4
7
|
import type { ContextReactionLLM, ContextActionRequest, ContextReactor, ContextReactorParams } from "./types.js";
|
|
5
8
|
type ScriptedReactionPayload = {
|
|
6
9
|
assistantEvent?: Partial<ContextItem>;
|
|
@@ -8,10 +11,10 @@ type ScriptedReactionPayload = {
|
|
|
8
11
|
messagesForModel?: ModelMessage[];
|
|
9
12
|
llm?: ContextReactionLLM;
|
|
10
13
|
};
|
|
11
|
-
export type ScriptedReactorStep<Context = unknown, Env extends ContextEnvironment = ContextEnvironment> = ScriptedReactionPayload | ((params: ContextReactorParams<Context, Env>) => Promise<ScriptedReactionPayload> | ScriptedReactionPayload);
|
|
12
|
-
export type CreateScriptedReactorOptions<Context = unknown, Env extends ContextEnvironment = ContextEnvironment> = {
|
|
13
|
-
steps: ScriptedReactorStep<Context, Env>[];
|
|
14
|
+
export type ScriptedReactorStep<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ScriptedReactionPayload | ((params: ContextReactorParams<Context, Env, RequiredDomain, Runtime>) => Promise<ScriptedReactionPayload> | ScriptedReactionPayload);
|
|
15
|
+
export type CreateScriptedReactorOptions<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = {
|
|
16
|
+
steps: ScriptedReactorStep<Context, Env, RequiredDomain, Runtime>[];
|
|
14
17
|
repeatLast?: boolean;
|
|
15
18
|
};
|
|
16
|
-
export declare function createScriptedReactor<Context = unknown, Env extends ContextEnvironment = ContextEnvironment>(options: CreateScriptedReactorOptions<Context, Env>): ContextReactor<Context, Env>;
|
|
19
|
+
export declare function createScriptedReactor<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>>(options: CreateScriptedReactorOptions<Context, Env, RequiredDomain, Runtime>): ContextReactor<Context, Env, RequiredDomain, Runtime>;
|
|
17
20
|
export {};
|
package/dist/reactors/types.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { ModelMessage, UIMessageChunk } from "ai";
|
|
2
|
+
import type { DomainLike } from "@ekairos/domain";
|
|
2
3
|
import type { ContextEnvironment } from "../context.config.js";
|
|
4
|
+
import type { ContextTool } from "../context.action.js";
|
|
5
|
+
import type { ContextRuntime, ContextRuntimeHandleForDomain } from "../context.runtime.js";
|
|
3
6
|
import type { ContextModelInit } from "../context.engine.js";
|
|
4
|
-
import type { ContextIdentifier, StoredContext, ContextItem } from "../context.store.js";
|
|
7
|
+
import type { ContextIdentifier, StoredContext, StoredContextResource, ContextItem } from "../context.store.js";
|
|
5
8
|
import type { ContextSkillPackage } from "../context.skill.js";
|
|
6
|
-
import
|
|
9
|
+
import { eventsDomain } from "../schema.js";
|
|
7
10
|
export type ContextActionRequest = {
|
|
8
11
|
actionRef: string;
|
|
9
12
|
actionName: string;
|
|
@@ -26,16 +29,29 @@ export type ContextReactionResult = {
|
|
|
26
29
|
actionRequests: ContextActionRequest[];
|
|
27
30
|
messagesForModel: ModelMessage[];
|
|
28
31
|
llm?: ContextReactionLLM;
|
|
32
|
+
reactor?: {
|
|
33
|
+
kind: string;
|
|
34
|
+
state?: Record<string, unknown> | null;
|
|
35
|
+
};
|
|
29
36
|
};
|
|
30
|
-
export type ContextReactorParams<Context = unknown, Env extends ContextEnvironment = ContextEnvironment> = {
|
|
31
|
-
|
|
37
|
+
export type ContextReactorParams<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = any> = {
|
|
38
|
+
runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>;
|
|
32
39
|
context: StoredContext<Context>;
|
|
33
40
|
contextIdentifier: ContextIdentifier;
|
|
41
|
+
resources: StoredContextResource[];
|
|
42
|
+
/**
|
|
43
|
+
* Context items after the engine-level expansion stage.
|
|
44
|
+
*
|
|
45
|
+
* Reactors should prefer these over fetching raw items when constructing
|
|
46
|
+
* model/runtime input. Expanders must return regular ContextItems whose
|
|
47
|
+
* parts follow the shared context part contract, so this is not tied to any
|
|
48
|
+
* specific model provider.
|
|
49
|
+
*/
|
|
50
|
+
events: ContextItem[];
|
|
34
51
|
triggerEvent: ContextItem;
|
|
35
52
|
model: ContextModelInit;
|
|
36
53
|
systemPrompt: string;
|
|
37
|
-
actions: Record<string,
|
|
38
|
-
toolsForModel: Record<string, SerializableToolForModel>;
|
|
54
|
+
actions: Record<string, ContextTool<Context, Env, RequiredDomain, Runtime>>;
|
|
39
55
|
skills: ContextSkillPackage[];
|
|
40
56
|
eventId: string;
|
|
41
57
|
executionId: string;
|
|
@@ -44,9 +60,8 @@ export type ContextReactorParams<Context = unknown, Env extends ContextEnvironme
|
|
|
44
60
|
iteration: number;
|
|
45
61
|
maxModelSteps: number;
|
|
46
62
|
sendStart: boolean;
|
|
47
|
-
silent: boolean;
|
|
48
63
|
contextStepStream?: WritableStream<string>;
|
|
49
64
|
writable?: WritableStream<UIMessageChunk>;
|
|
50
65
|
persistReactionParts?: (parts: any[]) => Promise<void>;
|
|
51
66
|
};
|
|
52
|
-
export type ContextReactor<Context = unknown, Env extends ContextEnvironment = ContextEnvironment> = (params: ContextReactorParams<Context, Env>) => Promise<ContextReactionResult>;
|
|
67
|
+
export type ContextReactor<Context = unknown, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = any> = (params: ContextReactorParams<Context, Env, RequiredDomain, Runtime>) => Promise<ContextReactionResult>;
|
package/dist/runtime.d.ts
CHANGED
|
@@ -8,6 +8,12 @@
|
|
|
8
8
|
* - Do NOT import this entrypoint from client/browser code.
|
|
9
9
|
* - Keep `@ekairos/events` main entrypoint safe to import from schema/domain modules.
|
|
10
10
|
*/
|
|
11
|
+
import "./reactors/ai-sdk.step.js";
|
|
12
|
+
import "./runtime.step.js";
|
|
13
|
+
import "./steps/durable.steps.js";
|
|
14
|
+
import "./steps/store.steps.js";
|
|
15
|
+
import "./steps/stream.steps.js";
|
|
16
|
+
import "./steps/trace.steps.js";
|
|
11
17
|
export { getContextRuntime, } from "./runtime.step.js";
|
|
12
18
|
export { createContextStepStreamClientId, createPersistedContextStepStream, readPersistedContextStepStream, resolveContextExecutionStreamPointer, waitForContextExecutionStreamPointer, } from "./steps/stream.steps.js";
|
|
13
19
|
export type { ContextEnvironment, ContextRuntime } from "./context.config.js";
|
package/dist/runtime.js
CHANGED
|
@@ -20,6 +20,15 @@ if (typeof globalThis.Event === "undefined") {
|
|
|
20
20
|
;
|
|
21
21
|
globalThis.Event = NodeEvent;
|
|
22
22
|
}
|
|
23
|
+
// Workflow bundles only register `use step` functions that are reachable from a
|
|
24
|
+
// workflow/runtime entrypoint. Keep the internal context steps reachable whenever
|
|
25
|
+
// apps import `@ekairos/events/runtime` to configure durable context execution.
|
|
26
|
+
import "./reactors/ai-sdk.step.js";
|
|
27
|
+
import "./runtime.step.js";
|
|
28
|
+
import "./steps/durable.steps.js";
|
|
29
|
+
import "./steps/store.steps.js";
|
|
30
|
+
import "./steps/stream.steps.js";
|
|
31
|
+
import "./steps/trace.steps.js";
|
|
23
32
|
export { getContextRuntime, } from "./runtime.step.js";
|
|
24
33
|
export { createContextStepStreamClientId, createPersistedContextStepStream, readPersistedContextStepStream, resolveContextExecutionStreamPointer, waitForContextExecutionStreamPointer, } from "./steps/stream.steps.js";
|
|
25
34
|
export { registerContextEnv, getContextEnv } from "./env.js";
|
package/dist/runtime.step.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { coerceContextRuntime } from "./context.config.js";
|
|
2
2
|
import { eventsDomain } from "./schema.js";
|
|
3
3
|
export async function getContextRuntime(env) {
|
|
4
|
-
const { resolveRuntime } = await import("@ekairos/domain/runtime
|
|
4
|
+
const { resolveRuntime } = await import("@ekairos/domain/runtime");
|
|
5
5
|
const resolved = await resolveRuntime(eventsDomain, env);
|
|
6
|
-
return await coerceContextRuntime(resolved);
|
|
6
|
+
return await coerceContextRuntime(resolved, env);
|
|
7
7
|
}
|