@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.
- package/README.md +115 -0
- package/dist/codex.d.ts +95 -0
- package/dist/codex.js +91 -0
- package/dist/context.builder.d.ts +62 -0
- package/dist/context.builder.js +143 -0
- package/dist/context.config.d.ts +9 -0
- package/dist/context.config.js +30 -0
- package/dist/context.contract.d.ts +47 -0
- package/dist/context.contract.js +132 -0
- package/dist/context.d.ts +4 -0
- package/dist/context.durable.d.ts +5 -0
- package/dist/context.durable.js +13 -0
- package/dist/context.engine.d.ts +216 -0
- package/dist/context.engine.js +1098 -0
- package/dist/context.events.d.ts +55 -0
- package/dist/context.events.js +431 -0
- package/dist/context.hooks.d.ts +21 -0
- package/dist/context.hooks.js +31 -0
- package/dist/context.js +3 -0
- package/dist/context.parts.d.ts +241 -0
- package/dist/context.parts.js +360 -0
- package/dist/context.reactor.d.ts +3 -0
- package/dist/context.reactor.js +2 -0
- package/dist/context.registry.d.ts +13 -0
- package/dist/context.registry.js +30 -0
- package/dist/context.skill.d.ts +9 -0
- package/dist/context.skill.js +1 -0
- package/dist/context.step-stream.d.ts +26 -0
- package/dist/context.step-stream.js +59 -0
- package/dist/context.store.d.ts +85 -0
- package/dist/context.store.js +1 -0
- package/dist/context.stream.d.ts +148 -0
- package/dist/context.stream.js +141 -0
- package/dist/context.toolcalls.d.ts +60 -0
- package/dist/context.toolcalls.js +117 -0
- package/dist/env.d.ts +3 -0
- package/dist/env.js +53 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +11 -0
- package/dist/mcp.d.ts +1 -0
- package/dist/mcp.js +1 -0
- package/dist/mirror.d.ts +41 -0
- package/dist/mirror.js +1 -0
- package/dist/oidc.d.ts +7 -0
- package/dist/oidc.js +25 -0
- package/dist/polyfills/dom-events.d.ts +1 -0
- package/dist/polyfills/dom-events.js +89 -0
- package/dist/react.d.ts +42 -0
- package/dist/react.js +88 -0
- package/dist/reactors/ai-sdk.chunk-map.d.ts +12 -0
- package/dist/reactors/ai-sdk.chunk-map.js +143 -0
- package/dist/reactors/ai-sdk.reactor.d.ts +33 -0
- package/dist/reactors/ai-sdk.reactor.js +65 -0
- package/dist/reactors/ai-sdk.step.d.ts +48 -0
- package/dist/reactors/ai-sdk.step.js +343 -0
- package/dist/reactors/scripted.reactor.d.ts +17 -0
- package/dist/reactors/scripted.reactor.js +51 -0
- package/dist/reactors/types.d.ts +52 -0
- package/dist/reactors/types.js +1 -0
- package/dist/runtime.d.ts +19 -0
- package/dist/runtime.js +26 -0
- package/dist/runtime.step.d.ts +9 -0
- package/dist/runtime.step.js +7 -0
- package/dist/schema.d.ts +2 -0
- package/dist/schema.js +191 -0
- package/dist/steps/do-context-stream-step.d.ts +34 -0
- package/dist/steps/do-context-stream-step.js +96 -0
- package/dist/steps/mirror.steps.d.ts +6 -0
- package/dist/steps/mirror.steps.js +48 -0
- package/dist/steps/store.steps.d.ts +96 -0
- package/dist/steps/store.steps.js +595 -0
- package/dist/steps/stream.steps.d.ts +86 -0
- package/dist/steps/stream.steps.js +270 -0
- package/dist/steps/trace.steps.d.ts +38 -0
- package/dist/steps/trace.steps.js +270 -0
- package/dist/stores/instant.document-parser.d.ts +6 -0
- package/dist/stores/instant.document-parser.js +210 -0
- package/dist/stores/instant.documents.d.ts +16 -0
- package/dist/stores/instant.documents.js +152 -0
- package/dist/stores/instant.store.d.ts +66 -0
- package/dist/stores/instant.store.js +575 -0
- package/dist/tools-to-model-tools.d.ts +19 -0
- package/dist/tools-to-model-tools.js +21 -0
- package/package.json +142 -0
package/dist/schema.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { i } from "@instantdb/core";
|
|
2
|
+
import { domain } from "@ekairos/domain";
|
|
3
|
+
export const eventsDomain = domain("events")
|
|
4
|
+
.schema({
|
|
5
|
+
entities: {
|
|
6
|
+
event_contexts: i.entity({
|
|
7
|
+
createdAt: i.date(),
|
|
8
|
+
updatedAt: i.date().optional(),
|
|
9
|
+
key: i.string().optional().indexed().unique(),
|
|
10
|
+
name: i.string().optional(),
|
|
11
|
+
status: i.string().optional().indexed(), // open_idle | open_streaming | closed
|
|
12
|
+
content: i.any().optional(),
|
|
13
|
+
}),
|
|
14
|
+
event_items: i.entity({
|
|
15
|
+
channel: i.string().indexed(),
|
|
16
|
+
createdAt: i.date().indexed(),
|
|
17
|
+
type: i.string().optional().indexed(),
|
|
18
|
+
content: i.any().optional(),
|
|
19
|
+
status: i.string().optional().indexed(),
|
|
20
|
+
}),
|
|
21
|
+
event_executions: i.entity({
|
|
22
|
+
createdAt: i.date(),
|
|
23
|
+
updatedAt: i.date().optional(),
|
|
24
|
+
status: i.string().optional().indexed(), // executing | completed | failed
|
|
25
|
+
workflowRunId: i.string().optional().indexed(),
|
|
26
|
+
activeStreamId: i.string().optional().indexed(),
|
|
27
|
+
activeStreamClientId: i.string().optional().indexed(),
|
|
28
|
+
lastStreamId: i.string().optional().indexed(),
|
|
29
|
+
lastStreamClientId: i.string().optional().indexed(),
|
|
30
|
+
}),
|
|
31
|
+
event_steps: i.entity({
|
|
32
|
+
createdAt: i.date().indexed(),
|
|
33
|
+
updatedAt: i.date().optional(),
|
|
34
|
+
status: i.string().optional().indexed(), // running | completed | failed
|
|
35
|
+
iteration: i.number().indexed(),
|
|
36
|
+
kind: i.string().optional().indexed(), // message | action_execute | action_result
|
|
37
|
+
actionName: i.string().optional().indexed(),
|
|
38
|
+
actionInput: i.any().optional(),
|
|
39
|
+
actionOutput: i.any().optional(),
|
|
40
|
+
actionError: i.string().optional(),
|
|
41
|
+
actionRequests: i.any().optional(),
|
|
42
|
+
actionResults: i.any().optional(),
|
|
43
|
+
continueLoop: i.boolean().optional(),
|
|
44
|
+
errorText: i.string().optional(),
|
|
45
|
+
streamId: i.string().optional().indexed(),
|
|
46
|
+
streamClientId: i.string().optional().indexed(),
|
|
47
|
+
streamStartedAt: i.date().optional().indexed(),
|
|
48
|
+
streamFinishedAt: i.date().optional().indexed(),
|
|
49
|
+
streamAbortReason: i.string().optional(),
|
|
50
|
+
}),
|
|
51
|
+
event_parts: i.entity({
|
|
52
|
+
key: i.string().unique().indexed(), // `${stepId}:${idx}`
|
|
53
|
+
stepId: i.string().indexed(),
|
|
54
|
+
idx: i.number().indexed(),
|
|
55
|
+
type: i.string().optional().indexed(), // canonical part.type
|
|
56
|
+
part: i.json().optional(),
|
|
57
|
+
metadata: i.json().optional(), // provider/model/runtime metadata only
|
|
58
|
+
updatedAt: i.date().optional(),
|
|
59
|
+
}),
|
|
60
|
+
event_trace_events: i.entity({
|
|
61
|
+
key: i.string().unique().indexed(), // `${workflowRunId}:${eventId}`
|
|
62
|
+
workflowRunId: i.string().indexed(),
|
|
63
|
+
seq: i.number().indexed(),
|
|
64
|
+
eventId: i.string().indexed(),
|
|
65
|
+
eventKind: i.string().indexed(),
|
|
66
|
+
eventAt: i.date().optional(),
|
|
67
|
+
ingestedAt: i.date().optional(),
|
|
68
|
+
orgId: i.string().optional().indexed(),
|
|
69
|
+
projectId: i.string().optional().indexed(),
|
|
70
|
+
contextKey: i.string().optional().indexed(),
|
|
71
|
+
contextId: i.string().optional().indexed(),
|
|
72
|
+
executionId: i.string().optional().indexed(),
|
|
73
|
+
stepId: i.string().optional().indexed(),
|
|
74
|
+
contextEventId: i.string().optional().indexed(),
|
|
75
|
+
toolCallId: i.string().optional().indexed(),
|
|
76
|
+
partKey: i.string().optional().indexed(),
|
|
77
|
+
partIdx: i.number().optional().indexed(),
|
|
78
|
+
spanId: i.string().optional().indexed(),
|
|
79
|
+
parentSpanId: i.string().optional().indexed(),
|
|
80
|
+
isDeleted: i.boolean().optional(),
|
|
81
|
+
aiProvider: i.string().optional().indexed(),
|
|
82
|
+
aiModel: i.string().optional().indexed(),
|
|
83
|
+
promptTokens: i.number().optional(),
|
|
84
|
+
promptTokensCached: i.number().optional(),
|
|
85
|
+
promptTokensUncached: i.number().optional(),
|
|
86
|
+
completionTokens: i.number().optional(),
|
|
87
|
+
totalTokens: i.number().optional(),
|
|
88
|
+
latencyMs: i.number().optional(),
|
|
89
|
+
cacheCostUsd: i.number().optional(),
|
|
90
|
+
computeCostUsd: i.number().optional(),
|
|
91
|
+
costUsd: i.number().optional(),
|
|
92
|
+
payload: i.any().optional(),
|
|
93
|
+
}),
|
|
94
|
+
event_trace_runs: i.entity({
|
|
95
|
+
workflowRunId: i.string().unique().indexed(),
|
|
96
|
+
orgId: i.string().optional().indexed(),
|
|
97
|
+
projectId: i.string().optional().indexed(),
|
|
98
|
+
firstEventAt: i.date().optional().indexed(),
|
|
99
|
+
lastEventAt: i.date().optional().indexed(),
|
|
100
|
+
lastIngestedAt: i.date().optional().indexed(),
|
|
101
|
+
eventsCount: i.number().optional(),
|
|
102
|
+
status: i.string().optional().indexed(),
|
|
103
|
+
payload: i.any().optional(),
|
|
104
|
+
}),
|
|
105
|
+
event_trace_spans: i.entity({
|
|
106
|
+
spanId: i.string().unique().indexed(),
|
|
107
|
+
parentSpanId: i.string().optional().indexed(),
|
|
108
|
+
workflowRunId: i.string().indexed(),
|
|
109
|
+
executionId: i.string().optional().indexed(),
|
|
110
|
+
stepId: i.string().optional().indexed(),
|
|
111
|
+
kind: i.string().optional().indexed(),
|
|
112
|
+
name: i.string().optional().indexed(),
|
|
113
|
+
status: i.string().optional().indexed(),
|
|
114
|
+
startedAt: i.date().optional().indexed(),
|
|
115
|
+
endedAt: i.date().optional().indexed(),
|
|
116
|
+
durationMs: i.number().optional(),
|
|
117
|
+
payload: i.any().optional(),
|
|
118
|
+
}),
|
|
119
|
+
document_documents: i.entity({
|
|
120
|
+
name: i.string().optional().indexed(),
|
|
121
|
+
mimeType: i.string().optional(),
|
|
122
|
+
size: i.number().optional(),
|
|
123
|
+
ownerId: i.string().optional().indexed(),
|
|
124
|
+
orgId: i.string().optional().indexed(),
|
|
125
|
+
createdAt: i.date().optional().indexed(),
|
|
126
|
+
processedAt: i.date().optional().indexed(),
|
|
127
|
+
updatedAt: i.date().optional(),
|
|
128
|
+
lastJobId: i.string().optional(),
|
|
129
|
+
content: i.json().optional(),
|
|
130
|
+
}),
|
|
131
|
+
},
|
|
132
|
+
links: {
|
|
133
|
+
contextItemsContext: {
|
|
134
|
+
forward: { on: "event_items", has: "one", label: "context" },
|
|
135
|
+
reverse: { on: "event_contexts", has: "many", label: "items" },
|
|
136
|
+
},
|
|
137
|
+
contextExecutionsContext: {
|
|
138
|
+
forward: { on: "event_executions", has: "one", label: "context" },
|
|
139
|
+
reverse: { on: "event_contexts", has: "many", label: "executions" },
|
|
140
|
+
},
|
|
141
|
+
contextCurrentExecution: {
|
|
142
|
+
forward: { on: "event_contexts", has: "one", label: "currentExecution" },
|
|
143
|
+
reverse: { on: "event_executions", has: "one", label: "currentOf" },
|
|
144
|
+
},
|
|
145
|
+
contextExecutionsTrigger: {
|
|
146
|
+
forward: { on: "event_executions", has: "one", label: "trigger" },
|
|
147
|
+
reverse: { on: "event_items", has: "many", label: "executionsAsTrigger" },
|
|
148
|
+
},
|
|
149
|
+
contextExecutionsReaction: {
|
|
150
|
+
forward: { on: "event_executions", has: "one", label: "reaction" },
|
|
151
|
+
reverse: { on: "event_items", has: "many", label: "executionsAsReaction" },
|
|
152
|
+
},
|
|
153
|
+
contextStepsExecution: {
|
|
154
|
+
forward: { on: "event_steps", has: "one", label: "execution" },
|
|
155
|
+
reverse: { on: "event_executions", has: "many", label: "steps" },
|
|
156
|
+
},
|
|
157
|
+
contextExecutionItems: {
|
|
158
|
+
forward: { on: "event_items", has: "one", label: "execution" },
|
|
159
|
+
reverse: { on: "event_executions", has: "many", label: "items" },
|
|
160
|
+
},
|
|
161
|
+
contextPartsStep: {
|
|
162
|
+
forward: { on: "event_parts", has: "one", label: "step" },
|
|
163
|
+
reverse: { on: "event_steps", has: "many", label: "parts" },
|
|
164
|
+
},
|
|
165
|
+
contextStepStream: {
|
|
166
|
+
forward: { on: "event_steps", has: "one", label: "stream" },
|
|
167
|
+
reverse: { on: "$streams", has: "many", label: "step" },
|
|
168
|
+
},
|
|
169
|
+
contextExecutionActiveStream: {
|
|
170
|
+
forward: { on: "event_executions", has: "one", label: "activeStream" },
|
|
171
|
+
reverse: { on: "$streams", has: "many", label: "activeOf" },
|
|
172
|
+
},
|
|
173
|
+
contextExecutionLastStream: {
|
|
174
|
+
forward: { on: "event_executions", has: "one", label: "lastStream" },
|
|
175
|
+
reverse: { on: "$streams", has: "many", label: "lastOf" },
|
|
176
|
+
},
|
|
177
|
+
documentFile: {
|
|
178
|
+
forward: {
|
|
179
|
+
on: "document_documents",
|
|
180
|
+
has: "one",
|
|
181
|
+
label: "file",
|
|
182
|
+
},
|
|
183
|
+
reverse: {
|
|
184
|
+
on: "$files",
|
|
185
|
+
has: "one",
|
|
186
|
+
label: "document",
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
rooms: {},
|
|
191
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ModelMessage } from "ai";
|
|
2
|
+
import type { ContextItem } from "../context.store.js";
|
|
3
|
+
import type { SerializableToolForModel } from "../tools-to-model-tools.js";
|
|
4
|
+
import type { ContextModelInit } from "../context.engine.js";
|
|
5
|
+
/**
|
|
6
|
+
* Runs a single LLM streaming step as a Workflow step.
|
|
7
|
+
*
|
|
8
|
+
* - Performs the provider/network call in a step (Node/runtime allowed).
|
|
9
|
+
* - Pipes AI SDK UI message chunks into the workflow-owned writable stream.
|
|
10
|
+
* - Returns the assistant event + extracted action requests for the workflow loop.
|
|
11
|
+
*/
|
|
12
|
+
export declare function doContextStreamStep(params: {
|
|
13
|
+
model: ContextModelInit;
|
|
14
|
+
system: string;
|
|
15
|
+
messages: ModelMessage[];
|
|
16
|
+
tools: Record<string, SerializableToolForModel>;
|
|
17
|
+
eventId: string;
|
|
18
|
+
maxSteps: number;
|
|
19
|
+
/**
|
|
20
|
+
* Whether to emit a `start` chunk for this streamed assistant message.
|
|
21
|
+
*
|
|
22
|
+
* IMPORTANT:
|
|
23
|
+
* Our context loop may call the model multiple times within a single "turn" while continuing
|
|
24
|
+
* to append to the same `eventId`. In that case, `start` must only be sent once.
|
|
25
|
+
*/
|
|
26
|
+
sendStart?: boolean;
|
|
27
|
+
}): Promise<{
|
|
28
|
+
assistantEvent: ContextItem;
|
|
29
|
+
actionRequests: {
|
|
30
|
+
actionRef: string;
|
|
31
|
+
actionName: string;
|
|
32
|
+
input: any;
|
|
33
|
+
}[];
|
|
34
|
+
}>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { OUTPUT_ITEM_TYPE } from "../context.events.js";
|
|
2
|
+
/**
|
|
3
|
+
* Runs a single LLM streaming step as a Workflow step.
|
|
4
|
+
*
|
|
5
|
+
* - Performs the provider/network call in a step (Node/runtime allowed).
|
|
6
|
+
* - Pipes AI SDK UI message chunks into the workflow-owned writable stream.
|
|
7
|
+
* - Returns the assistant event + extracted action requests for the workflow loop.
|
|
8
|
+
*/
|
|
9
|
+
export async function doContextStreamStep(params) {
|
|
10
|
+
"use step";
|
|
11
|
+
const { getWritable } = await import("workflow");
|
|
12
|
+
const writable = getWritable();
|
|
13
|
+
const { jsonSchema, gateway, smoothStream, stepCountIs, streamText } = await import("ai");
|
|
14
|
+
const { extractToolCallsFromParts } = await import("../context.toolcalls.js");
|
|
15
|
+
// Match DurableAgent's model init behavior:
|
|
16
|
+
// - string => AI Gateway model id, resolved via `gateway(...)` in the step runtime
|
|
17
|
+
// - function => model factory (should be a `"use step"` function for workflow serialization)
|
|
18
|
+
const resolvedModel = typeof params.model === "string"
|
|
19
|
+
? gateway(params.model)
|
|
20
|
+
: typeof params.model === "function"
|
|
21
|
+
? await params.model()
|
|
22
|
+
: (() => {
|
|
23
|
+
throw new Error("Invalid model init passed to doContextStreamStep. Expected a model id string or an async model factory.");
|
|
24
|
+
})();
|
|
25
|
+
// IMPORTANT:
|
|
26
|
+
// `streamText` expects tools in the AI SDK ToolSet shape, where `inputSchema` is a Schema-like value.
|
|
27
|
+
// We pass plain JSON schema objects across the step boundary (serializable), then wrap them here with
|
|
28
|
+
// `jsonSchema(...)` so the AI SDK does not attempt Zod conversion at runtime.
|
|
29
|
+
const toolsForStreamText = {};
|
|
30
|
+
for (const [name, t] of Object.entries(params.tools)) {
|
|
31
|
+
toolsForStreamText[name] = {
|
|
32
|
+
description: t?.description,
|
|
33
|
+
inputSchema: jsonSchema(t.inputSchema),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const result = streamText({
|
|
37
|
+
model: resolvedModel,
|
|
38
|
+
system: params.system,
|
|
39
|
+
messages: params.messages,
|
|
40
|
+
tools: toolsForStreamText,
|
|
41
|
+
toolChoice: "required",
|
|
42
|
+
stopWhen: stepCountIs(params.maxSteps),
|
|
43
|
+
experimental_transform: smoothStream({ delayInMs: 30, chunking: "word" }),
|
|
44
|
+
});
|
|
45
|
+
// Ensure the underlying stream is consumed (AI SDK requirement)
|
|
46
|
+
result.consumeStream();
|
|
47
|
+
let resolveFinish;
|
|
48
|
+
let rejectFinish;
|
|
49
|
+
const finishPromise = new Promise((resolve, reject) => {
|
|
50
|
+
resolveFinish = resolve;
|
|
51
|
+
rejectFinish = reject;
|
|
52
|
+
});
|
|
53
|
+
const uiStream = result
|
|
54
|
+
.toUIMessageStream({
|
|
55
|
+
// Emit `start` only when the engine says so (typically once per turn).
|
|
56
|
+
sendStart: Boolean(params.sendStart),
|
|
57
|
+
generateMessageId: () => params.eventId,
|
|
58
|
+
messageMetadata() {
|
|
59
|
+
return { eventId: params.eventId };
|
|
60
|
+
},
|
|
61
|
+
onFinish: ({ messages }) => {
|
|
62
|
+
const lastMessage = messages[messages.length - 1];
|
|
63
|
+
const event = {
|
|
64
|
+
id: params.eventId,
|
|
65
|
+
type: OUTPUT_ITEM_TYPE,
|
|
66
|
+
channel: "web",
|
|
67
|
+
createdAt: new Date().toISOString(),
|
|
68
|
+
content: { parts: lastMessage?.parts ?? [] },
|
|
69
|
+
};
|
|
70
|
+
resolveFinish(event);
|
|
71
|
+
},
|
|
72
|
+
onError: (e) => {
|
|
73
|
+
rejectFinish(e);
|
|
74
|
+
return e instanceof Error ? e.message : String(e);
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
// Filter out per-step finish boundary. Workflow will emit a single finish.
|
|
78
|
+
.pipeThrough(new TransformStream({
|
|
79
|
+
transform(chunk, controller) {
|
|
80
|
+
if (chunk.type === "finish")
|
|
81
|
+
return;
|
|
82
|
+
controller.enqueue(chunk);
|
|
83
|
+
},
|
|
84
|
+
}));
|
|
85
|
+
await uiStream.pipeTo(writable, { preventClose: true });
|
|
86
|
+
const assistantEvent = await finishPromise;
|
|
87
|
+
const actionRequests = extractToolCallsFromParts(assistantEvent?.content?.parts).map((entry) => ({
|
|
88
|
+
actionRef: String(entry.toolCallId),
|
|
89
|
+
actionName: String(entry.toolName),
|
|
90
|
+
input: entry.args,
|
|
91
|
+
}));
|
|
92
|
+
return {
|
|
93
|
+
assistantEvent,
|
|
94
|
+
actionRequests,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
function requireOrgId(env) {
|
|
2
|
+
const orgId = env?.orgId;
|
|
3
|
+
if (typeof orgId !== "string" || !orgId) {
|
|
4
|
+
throw new Error("[context/mirror] Missing env.orgId");
|
|
5
|
+
}
|
|
6
|
+
return orgId;
|
|
7
|
+
}
|
|
8
|
+
function requireBaseUrl() {
|
|
9
|
+
const baseUrl = process.env.EKAIROS_CORE_BASE_URL ||
|
|
10
|
+
process.env.EKAIROS_MIRROR_BASE_URL ||
|
|
11
|
+
process.env.EKAIROS_BASE_URL;
|
|
12
|
+
if (!baseUrl) {
|
|
13
|
+
throw new Error("[context/mirror] Missing EKAIROS_CORE_BASE_URL (or EKAIROS_MIRROR_BASE_URL)");
|
|
14
|
+
}
|
|
15
|
+
return baseUrl.replace(/\/$/, "");
|
|
16
|
+
}
|
|
17
|
+
function requireToken() {
|
|
18
|
+
// Preferred: Clerk org API key (opaque token) for ekairos-core.
|
|
19
|
+
const apiKey = process.env.EKAIROS_CLERK_API_KEY;
|
|
20
|
+
if (apiKey)
|
|
21
|
+
return apiKey;
|
|
22
|
+
// Fallback: shared secret token.
|
|
23
|
+
const token = process.env.EKAIROS_CONTEXT_MIRROR_TOKEN;
|
|
24
|
+
if (token)
|
|
25
|
+
return token;
|
|
26
|
+
throw new Error("[context/mirror] Missing EKAIROS_CLERK_API_KEY (or EKAIROS_CONTEXT_MIRROR_TOKEN fallback)");
|
|
27
|
+
}
|
|
28
|
+
export async function mirrorContextWrites(params) {
|
|
29
|
+
"use step";
|
|
30
|
+
if (!params.writes?.length)
|
|
31
|
+
return;
|
|
32
|
+
const orgId = requireOrgId(params.env);
|
|
33
|
+
const baseUrl = requireBaseUrl();
|
|
34
|
+
const token = requireToken();
|
|
35
|
+
const body = { orgId, writes: params.writes };
|
|
36
|
+
const res = await fetch(`${baseUrl}/api/context`, {
|
|
37
|
+
method: "POST",
|
|
38
|
+
headers: {
|
|
39
|
+
"content-type": "application/json",
|
|
40
|
+
authorization: `Bearer ${token}`,
|
|
41
|
+
},
|
|
42
|
+
body: JSON.stringify(body),
|
|
43
|
+
});
|
|
44
|
+
if (!res.ok) {
|
|
45
|
+
const text = await res.text().catch(() => "");
|
|
46
|
+
throw new Error(`[context/mirror] ekairos-core write failed (${res.status}): ${text}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { UIMessageChunk } from "ai";
|
|
2
|
+
import type { ContextEnvironment } from "../context.config.js";
|
|
3
|
+
import type { ContextExecution, ContextItem, ContextIdentifier, StoredContext, ContextStatus } from "../context.store.js";
|
|
4
|
+
export type ContextReviewRequest = {
|
|
5
|
+
toolCallId: string;
|
|
6
|
+
toolName?: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Initializes/ensures the story context exists.
|
|
10
|
+
*
|
|
11
|
+
* This is the "context init" boundary for the story engine.
|
|
12
|
+
*/
|
|
13
|
+
export declare function initializeContext<C>(env: ContextEnvironment, contextIdentifier: ContextIdentifier | null, opts?: {
|
|
14
|
+
silent?: boolean;
|
|
15
|
+
writable?: WritableStream<UIMessageChunk>;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
context: StoredContext<C>;
|
|
18
|
+
isNew: boolean;
|
|
19
|
+
}>;
|
|
20
|
+
export declare function updateContextContent<C>(env: ContextEnvironment, contextIdentifier: ContextIdentifier, content: C): Promise<StoredContext<C>>;
|
|
21
|
+
export declare function updateContextStatus(env: ContextEnvironment, contextIdentifier: ContextIdentifier, status: ContextStatus): Promise<void>;
|
|
22
|
+
export declare function saveTriggerItem(env: ContextEnvironment, contextIdentifier: ContextIdentifier, event: ContextItem): Promise<ContextItem>;
|
|
23
|
+
export declare function saveTriggerAndCreateExecution(params: {
|
|
24
|
+
env: ContextEnvironment;
|
|
25
|
+
contextIdentifier: ContextIdentifier;
|
|
26
|
+
triggerEvent: ContextItem;
|
|
27
|
+
}): Promise<{
|
|
28
|
+
triggerEvent: ContextItem;
|
|
29
|
+
reactionEvent: ContextItem;
|
|
30
|
+
execution: ContextExecution;
|
|
31
|
+
}>;
|
|
32
|
+
export declare function saveReactionItem(env: ContextEnvironment, contextIdentifier: ContextIdentifier, event: ContextItem, opts?: {
|
|
33
|
+
executionId?: string;
|
|
34
|
+
contextId?: string;
|
|
35
|
+
reviewRequests?: ContextReviewRequest[];
|
|
36
|
+
}): Promise<ContextItem>;
|
|
37
|
+
export declare function updateItem(env: ContextEnvironment, eventId: string, event: ContextItem, opts?: {
|
|
38
|
+
executionId?: string;
|
|
39
|
+
contextId?: string;
|
|
40
|
+
}): Promise<ContextItem>;
|
|
41
|
+
export declare function createExecution(env: ContextEnvironment, contextIdentifier: ContextIdentifier, triggerEventId: string, reactionEventId: string): Promise<{
|
|
42
|
+
id: string;
|
|
43
|
+
}>;
|
|
44
|
+
export declare function createReactionItem(params: {
|
|
45
|
+
env: ContextEnvironment;
|
|
46
|
+
contextIdentifier: ContextIdentifier;
|
|
47
|
+
triggerEventId: string;
|
|
48
|
+
}): Promise<{
|
|
49
|
+
reactionEventId: string;
|
|
50
|
+
executionId: string;
|
|
51
|
+
}>;
|
|
52
|
+
export declare function completeExecution(env: ContextEnvironment, contextIdentifier: ContextIdentifier, executionId: string, status: "completed" | "failed"): Promise<void>;
|
|
53
|
+
export declare function updateExecutionWorkflowRun(params: {
|
|
54
|
+
env: ContextEnvironment;
|
|
55
|
+
executionId: string;
|
|
56
|
+
workflowRunId: string;
|
|
57
|
+
}): Promise<void>;
|
|
58
|
+
export declare function createContextStep(params: {
|
|
59
|
+
env: ContextEnvironment;
|
|
60
|
+
executionId: string;
|
|
61
|
+
iteration: number;
|
|
62
|
+
}): Promise<{
|
|
63
|
+
stepId: string;
|
|
64
|
+
}>;
|
|
65
|
+
export declare function updateContextStep(params: {
|
|
66
|
+
env: ContextEnvironment;
|
|
67
|
+
stepId: string;
|
|
68
|
+
executionId?: string;
|
|
69
|
+
contextId?: string;
|
|
70
|
+
iteration?: number;
|
|
71
|
+
patch: {
|
|
72
|
+
status?: "running" | "completed" | "failed";
|
|
73
|
+
kind?: "message" | "action_execute" | "action_result";
|
|
74
|
+
actionName?: string;
|
|
75
|
+
actionInput?: unknown;
|
|
76
|
+
actionOutput?: unknown;
|
|
77
|
+
actionError?: string;
|
|
78
|
+
actionRequests?: any;
|
|
79
|
+
actionResults?: any;
|
|
80
|
+
continueLoop?: boolean;
|
|
81
|
+
errorText?: string;
|
|
82
|
+
};
|
|
83
|
+
}): Promise<void>;
|
|
84
|
+
export declare function linkItemToExecutionStep(params: {
|
|
85
|
+
env: ContextEnvironment;
|
|
86
|
+
itemId: string;
|
|
87
|
+
executionId: string;
|
|
88
|
+
}): Promise<void>;
|
|
89
|
+
export declare function saveContextPartsStep(params: {
|
|
90
|
+
env: ContextEnvironment;
|
|
91
|
+
stepId: string;
|
|
92
|
+
executionId?: string;
|
|
93
|
+
contextId?: string;
|
|
94
|
+
iteration?: number;
|
|
95
|
+
parts: any[];
|
|
96
|
+
}): Promise<void>;
|