@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/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# @ekairos/events
|
|
2
|
+
|
|
3
|
+
Context-first durable execution runtime for Ekairos.
|
|
4
|
+
|
|
5
|
+
## Surface
|
|
6
|
+
|
|
7
|
+
- `createContext`, `context`, `ContextEngine`
|
|
8
|
+
- `createAiSdkReactor`, `createScriptedReactor`
|
|
9
|
+
- `useContext`
|
|
10
|
+
- `eventsDomain`
|
|
11
|
+
- `getContextRuntime`, `getContextEnv`, `registerContextEnv`
|
|
12
|
+
|
|
13
|
+
## Runtime model
|
|
14
|
+
|
|
15
|
+
- `event_contexts`
|
|
16
|
+
- `event_items`
|
|
17
|
+
- `event_executions`
|
|
18
|
+
- `event_steps`
|
|
19
|
+
- `event_parts`
|
|
20
|
+
- `event_trace_events`
|
|
21
|
+
- `event_trace_runs`
|
|
22
|
+
- `event_trace_spans`
|
|
23
|
+
|
|
24
|
+
The aggregate is `context`. Executions, steps, parts, and items are scoped to a context.
|
|
25
|
+
|
|
26
|
+
## Canonical Parts
|
|
27
|
+
|
|
28
|
+
`event_parts` is the canonical content model for produced output.
|
|
29
|
+
|
|
30
|
+
Rules:
|
|
31
|
+
|
|
32
|
+
- `event_parts.part` is the source of truth for replay and inspection.
|
|
33
|
+
- `event_items.content.parts` on output items is maintained as a compatibility mirror and is deprecated as a replay source.
|
|
34
|
+
- Provider/model-specific values must live under `metadata`, never as first-class semantic fields.
|
|
35
|
+
|
|
36
|
+
Canonical part kinds:
|
|
37
|
+
|
|
38
|
+
- `content`
|
|
39
|
+
- `reasoning`
|
|
40
|
+
- `source`
|
|
41
|
+
- `tool-call`
|
|
42
|
+
- `tool-result`
|
|
43
|
+
|
|
44
|
+
Each canonical part stores a `content` array. The entries inside that array define the payload type:
|
|
45
|
+
|
|
46
|
+
- `text`
|
|
47
|
+
- `file`
|
|
48
|
+
- `json`
|
|
49
|
+
- `source-url`
|
|
50
|
+
- `source-document`
|
|
51
|
+
|
|
52
|
+
Example tool result:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
{
|
|
56
|
+
type: "tool-result",
|
|
57
|
+
toolCallId: "call_123",
|
|
58
|
+
toolName: "inspectCanvasRegion",
|
|
59
|
+
state: "output-available",
|
|
60
|
+
content: [
|
|
61
|
+
{
|
|
62
|
+
type: "text",
|
|
63
|
+
text: "Zoomed crop of the requested region.",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
type: "file",
|
|
67
|
+
mediaType: "image/png",
|
|
68
|
+
filename: "inspect-region.png",
|
|
69
|
+
data: "iVBORw0KGgoAAAANSUhEUgAA...",
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
metadata: {
|
|
73
|
+
provider: {
|
|
74
|
+
itemId: "fc_041cb...",
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The AI SDK bridge projects canonical parts to:
|
|
81
|
+
|
|
82
|
+
- assistant messages with text/file/reasoning/source/tool-call parts
|
|
83
|
+
- tool messages with `tool-result` or `tool-error`
|
|
84
|
+
|
|
85
|
+
That means multipart tool outputs are replayed from `event_parts` instead of relying on the deprecated output-item mirror.
|
|
86
|
+
|
|
87
|
+
## Install
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
pnpm add @ekairos/events
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Example
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { createContext, createAiSdkReactor } from "@ekairos/events";
|
|
97
|
+
|
|
98
|
+
type Env = { orgId: string };
|
|
99
|
+
|
|
100
|
+
export const supportContext = createContext<Env>("support.agent")
|
|
101
|
+
.context((stored, env) => ({
|
|
102
|
+
orgId: env.orgId,
|
|
103
|
+
...stored.content,
|
|
104
|
+
}))
|
|
105
|
+
.narrative(() => "You are a precise assistant.")
|
|
106
|
+
.actions(() => ({}))
|
|
107
|
+
.reactor(createAiSdkReactor())
|
|
108
|
+
.build();
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Notes
|
|
112
|
+
|
|
113
|
+
- Public continuity is context-based.
|
|
114
|
+
- Provider-specific IDs such as `providerContextId` may still exist when an upstream provider requires them.
|
|
115
|
+
- Runtime wiring for stores lives under `@ekairos/events/runtime`.
|
package/dist/codex.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ContextEnvironment } from "./context.config.js";
|
|
3
|
+
import type { ContextModelInit, ContextOptions, ContextReactParams, ContextShouldContinueArgs, ContextTool } from "./context.engine.js";
|
|
4
|
+
import type { ContextKey } from "./context.registry.js";
|
|
5
|
+
import type { StoredContext, ContextItem } from "./context.store.js";
|
|
6
|
+
import type { ContextInstance } from "./context.js";
|
|
7
|
+
export declare const DEFAULT_CODEX_TOOL_NAME = "codex";
|
|
8
|
+
export declare const DEFAULT_CODEX_MODEL = "openai/gpt-5.2";
|
|
9
|
+
export type CodexContextRuntimeMode = "local" | "remote" | "sandbox";
|
|
10
|
+
export type CodexContextReasoningLevel = "off" | "low" | "medium" | "high";
|
|
11
|
+
export type CodexContextRuntime = {
|
|
12
|
+
appServerUrl?: string;
|
|
13
|
+
repoPath?: string;
|
|
14
|
+
contextId?: string;
|
|
15
|
+
mode?: CodexContextRuntimeMode;
|
|
16
|
+
model?: string;
|
|
17
|
+
approvalPolicy?: string;
|
|
18
|
+
sandboxPolicy?: Record<string, unknown>;
|
|
19
|
+
};
|
|
20
|
+
export type CodexContextEnv = ContextEnvironment & {
|
|
21
|
+
sessionId: string;
|
|
22
|
+
runtime?: CodexContextRuntime;
|
|
23
|
+
reasoningLevel?: CodexContextReasoningLevel;
|
|
24
|
+
model?: string;
|
|
25
|
+
};
|
|
26
|
+
export type CodexToolInput = {
|
|
27
|
+
instruction: string;
|
|
28
|
+
files?: Array<{
|
|
29
|
+
url?: string;
|
|
30
|
+
path?: string;
|
|
31
|
+
mediaType?: string;
|
|
32
|
+
fileId?: string;
|
|
33
|
+
}>;
|
|
34
|
+
};
|
|
35
|
+
export type CodexToolOutput = {
|
|
36
|
+
contextId: string;
|
|
37
|
+
turnId: string;
|
|
38
|
+
assistantText: string;
|
|
39
|
+
reasoningText: string;
|
|
40
|
+
diff: string;
|
|
41
|
+
toolParts: any[];
|
|
42
|
+
};
|
|
43
|
+
export declare const codexToolInputSchema: z.ZodObject<{
|
|
44
|
+
instruction: z.ZodString;
|
|
45
|
+
files: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
46
|
+
url: z.ZodOptional<z.ZodString>;
|
|
47
|
+
path: z.ZodOptional<z.ZodString>;
|
|
48
|
+
mediaType: z.ZodOptional<z.ZodString>;
|
|
49
|
+
fileId: z.ZodOptional<z.ZodString>;
|
|
50
|
+
}, z.core.$strip>>>;
|
|
51
|
+
}, z.core.$strip>;
|
|
52
|
+
export type CodexExecuteArgs<Context, Env extends CodexContextEnv = CodexContextEnv> = {
|
|
53
|
+
context: StoredContext<Context>;
|
|
54
|
+
env: Env;
|
|
55
|
+
input: CodexToolInput;
|
|
56
|
+
toolName: string;
|
|
57
|
+
};
|
|
58
|
+
export type CodexContextBuilderConfig<Context, Env extends CodexContextEnv = CodexContextEnv> = {
|
|
59
|
+
key: ContextKey;
|
|
60
|
+
context: (context: StoredContext<Context>, env: Env) => Promise<Context> | Context;
|
|
61
|
+
executeCodex: (args: CodexExecuteArgs<Context, Env>) => Promise<CodexToolOutput>;
|
|
62
|
+
narrative?: (context: StoredContext<Context>, env: Env) => Promise<string> | string;
|
|
63
|
+
system?: (context: StoredContext<Context>, env: Env) => Promise<string> | string;
|
|
64
|
+
actions?: (context: StoredContext<Context>, env: Env) => Promise<Record<string, ContextTool>> | Record<string, ContextTool>;
|
|
65
|
+
model?: ContextModelInit | ((context: StoredContext<Context>, env: Env) => ContextModelInit);
|
|
66
|
+
shouldContinue?: (args: ContextShouldContinueArgs<Context, Env>) => Promise<boolean> | boolean;
|
|
67
|
+
toolName?: string;
|
|
68
|
+
toolDescription?: string;
|
|
69
|
+
opts?: ContextOptions<Context, Env>;
|
|
70
|
+
};
|
|
71
|
+
export type CodexContextBuilder<Context, Env extends CodexContextEnv = CodexContextEnv> = {
|
|
72
|
+
key: ContextKey;
|
|
73
|
+
react(triggerEvent: ContextItem, params: ContextReactParams<Env>): Promise<{
|
|
74
|
+
contextId: string;
|
|
75
|
+
context: StoredContext<Context>;
|
|
76
|
+
triggerEventId: string;
|
|
77
|
+
reactionEventId: string;
|
|
78
|
+
executionId: string;
|
|
79
|
+
}>;
|
|
80
|
+
stream(triggerEvent: ContextItem, params: ContextReactParams<Env>): Promise<{
|
|
81
|
+
contextId: string;
|
|
82
|
+
context: StoredContext<Context>;
|
|
83
|
+
triggerEventId: string;
|
|
84
|
+
reactionEventId: string;
|
|
85
|
+
executionId: string;
|
|
86
|
+
}>;
|
|
87
|
+
register(): void;
|
|
88
|
+
config(): unknown;
|
|
89
|
+
build(): ContextInstance<Context, Env>;
|
|
90
|
+
};
|
|
91
|
+
export declare function buildDefaultCodexNarrative(content: unknown): string;
|
|
92
|
+
export declare function didCodexToolExecute(event: Pick<{
|
|
93
|
+
content: any;
|
|
94
|
+
}, "content">, toolName?: string): boolean;
|
|
95
|
+
export declare function createCodexContextBuilder<Context, Env extends CodexContextEnv = CodexContextEnv>(config: CodexContextBuilderConfig<Context, Env>): CodexContextBuilder<Context, Env>;
|
package/dist/codex.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { tool } from "ai";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { createContext } from "./context.js";
|
|
4
|
+
import { didToolExecute } from "./context.toolcalls.js";
|
|
5
|
+
export const DEFAULT_CODEX_TOOL_NAME = "codex";
|
|
6
|
+
export const DEFAULT_CODEX_MODEL = "openai/gpt-5.2";
|
|
7
|
+
export const codexToolInputSchema = z.object({
|
|
8
|
+
instruction: z
|
|
9
|
+
.string()
|
|
10
|
+
.describe("The coding instruction to execute for this request."),
|
|
11
|
+
files: z
|
|
12
|
+
.array(z.object({
|
|
13
|
+
url: z.string().optional(),
|
|
14
|
+
path: z.string().optional(),
|
|
15
|
+
mediaType: z.string().optional(),
|
|
16
|
+
fileId: z.string().optional(),
|
|
17
|
+
}))
|
|
18
|
+
.optional(),
|
|
19
|
+
});
|
|
20
|
+
function toRecord(value) {
|
|
21
|
+
if (!value || typeof value !== "object")
|
|
22
|
+
return {};
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
function toString(value) {
|
|
26
|
+
if (typeof value !== "string")
|
|
27
|
+
return "";
|
|
28
|
+
return value.trim();
|
|
29
|
+
}
|
|
30
|
+
export function buildDefaultCodexNarrative(content) {
|
|
31
|
+
const record = toRecord(content);
|
|
32
|
+
const sessionId = toString(record.sessionId);
|
|
33
|
+
const repoUrl = toString(record.repoUrl);
|
|
34
|
+
const branchName = toString(record.branchName);
|
|
35
|
+
return [
|
|
36
|
+
"You are Codex running as an Ekairos Context.",
|
|
37
|
+
"",
|
|
38
|
+
"Execution style:",
|
|
39
|
+
"- Execute real coding work via the `codex` action.",
|
|
40
|
+
"- Keep answers concrete and implementation-oriented.",
|
|
41
|
+
"- Preserve repository and branch continuity across turns.",
|
|
42
|
+
"",
|
|
43
|
+
"Context:",
|
|
44
|
+
`- Session: ${sessionId || "unknown"}`,
|
|
45
|
+
`- Repository: ${repoUrl || "unknown"}`,
|
|
46
|
+
`- Branch: ${branchName || "unknown"}`,
|
|
47
|
+
].join("\n");
|
|
48
|
+
}
|
|
49
|
+
export function didCodexToolExecute(event, toolName = DEFAULT_CODEX_TOOL_NAME) {
|
|
50
|
+
return didToolExecute(event, toolName);
|
|
51
|
+
}
|
|
52
|
+
export function createCodexContextBuilder(config) {
|
|
53
|
+
const toolName = config.toolName ?? DEFAULT_CODEX_TOOL_NAME;
|
|
54
|
+
const toolDescription = config.toolDescription ??
|
|
55
|
+
"Run the coding request using a Codex app server and stream output.";
|
|
56
|
+
const narrative = config.narrative ??
|
|
57
|
+
config.system ??
|
|
58
|
+
((ctx) => buildDefaultCodexNarrative(ctx?.content));
|
|
59
|
+
const model = config.model ??
|
|
60
|
+
((_ctx, env) => (typeof env.model === "string" && env.model.trim()) || DEFAULT_CODEX_MODEL);
|
|
61
|
+
const shouldContinue = config.shouldContinue ??
|
|
62
|
+
((args) => !didToolExecute(args.reactionEvent, toolName));
|
|
63
|
+
let builder = createContext(config.key)
|
|
64
|
+
.context(config.context)
|
|
65
|
+
.narrative(narrative)
|
|
66
|
+
.actions(async (ctx, env) => {
|
|
67
|
+
const additional = config.actions ? await config.actions(ctx, env) : {};
|
|
68
|
+
if (Object.prototype.hasOwnProperty.call(additional, toolName)) {
|
|
69
|
+
throw new Error(`createCodexContextBuilder: action "${toolName}" is reserved for Codex.`);
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
...additional,
|
|
73
|
+
[toolName]: tool({
|
|
74
|
+
description: toolDescription,
|
|
75
|
+
inputSchema: codexToolInputSchema,
|
|
76
|
+
execute: async (input) => await config.executeCodex({
|
|
77
|
+
context: ctx,
|
|
78
|
+
env,
|
|
79
|
+
input,
|
|
80
|
+
toolName,
|
|
81
|
+
}),
|
|
82
|
+
}),
|
|
83
|
+
};
|
|
84
|
+
})
|
|
85
|
+
.model(model)
|
|
86
|
+
.shouldContinue(shouldContinue);
|
|
87
|
+
if (config.opts) {
|
|
88
|
+
builder = builder.opts(config.opts);
|
|
89
|
+
}
|
|
90
|
+
return builder;
|
|
91
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ContextEnvironment } from "./context.config.js";
|
|
2
|
+
import type { ContextSkillPackage } from "./context.skill.js";
|
|
3
|
+
import { ContextEngine, type ContextModelInit, type ContextOptions, type ContextTool, type ShouldContinue, type ContextShouldContinueArgs, type ContextReactParams } from "./context.engine.js";
|
|
4
|
+
import type { ContextReactor } from "./context.reactor.js";
|
|
5
|
+
import type { ContextItem, StoredContext } from "./context.store.js";
|
|
6
|
+
import { type ContextKey } from "./context.registry.js";
|
|
7
|
+
export interface ContextConfig<Context, Env extends ContextEnvironment = ContextEnvironment> {
|
|
8
|
+
context: (context: StoredContext<Context>, env: Env) => Promise<Context> | Context;
|
|
9
|
+
expandEvents?: (events: ContextItem[], context: StoredContext<Context>, env: Env) => Promise<ContextItem[]> | ContextItem[];
|
|
10
|
+
narrative: (context: StoredContext<Context>, env: Env) => Promise<string> | string;
|
|
11
|
+
skills?: (context: StoredContext<Context>, env: Env) => Promise<ContextSkillPackage[]> | ContextSkillPackage[];
|
|
12
|
+
actions: (context: StoredContext<Context>, env: Env) => Promise<Record<string, ContextTool>> | Record<string, ContextTool>;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Use `actions()` instead.
|
|
15
|
+
*/
|
|
16
|
+
tools?: (context: StoredContext<Context>, env: Env) => Promise<Record<string, ContextTool>> | Record<string, ContextTool>;
|
|
17
|
+
model?: ContextModelInit | ((context: StoredContext<Context>, env: Env) => ContextModelInit);
|
|
18
|
+
reactor?: ContextReactor<Context, Env>;
|
|
19
|
+
shouldContinue?: (args: ContextShouldContinueArgs<Context, Env>) => Promise<ShouldContinue> | ShouldContinue;
|
|
20
|
+
opts?: ContextOptions<Context, Env>;
|
|
21
|
+
}
|
|
22
|
+
export type ContextInstance<Context, Env extends ContextEnvironment = ContextEnvironment> = ContextEngine<Context, Env> & {
|
|
23
|
+
readonly __config: ContextConfig<Context, Env>;
|
|
24
|
+
readonly __contextKey?: ContextKey;
|
|
25
|
+
};
|
|
26
|
+
export declare function context<Context, Env extends ContextEnvironment = ContextEnvironment>(config: ContextConfig<Context, Env>): ContextInstance<Context, Env>;
|
|
27
|
+
type AnyContextInitializer<Env extends ContextEnvironment> = (context: StoredContext<any>, env: Env) => Promise<any> | any;
|
|
28
|
+
type InferContextFromInitializer<I extends AnyContextInitializer<any>> = Awaited<ReturnType<I>>;
|
|
29
|
+
type BuilderSystemPrompt<Context, Env extends ContextEnvironment> = (context: StoredContext<Context>, env: Env) => Promise<string> | string;
|
|
30
|
+
type BuilderSkills<Context, Env extends ContextEnvironment> = (context: StoredContext<Context>, env: Env) => Promise<ContextSkillPackage[]> | ContextSkillPackage[];
|
|
31
|
+
type BuilderTools<Context, Env extends ContextEnvironment> = (context: StoredContext<Context>, env: Env) => Promise<Record<string, ContextTool>> | Record<string, ContextTool>;
|
|
32
|
+
type BuilderExpandEvents<Context, Env extends ContextEnvironment> = (events: ContextItem[], context: StoredContext<Context>, env: Env) => Promise<ContextItem[]> | ContextItem[];
|
|
33
|
+
type BuilderShouldContinue<Context, Env extends ContextEnvironment> = (args: ContextShouldContinueArgs<Context, Env>) => Promise<ShouldContinue> | ShouldContinue;
|
|
34
|
+
type BuilderModel<Context, Env extends ContextEnvironment> = ContextModelInit | ((context: StoredContext<Context>, env: Env) => ContextModelInit);
|
|
35
|
+
export type RegistrableContextBuilder = {
|
|
36
|
+
key: ContextKey;
|
|
37
|
+
register: () => void;
|
|
38
|
+
};
|
|
39
|
+
type FluentContextBuilder<Context, Env extends ContextEnvironment> = {
|
|
40
|
+
key: ContextKey;
|
|
41
|
+
expandEvents(fn: BuilderExpandEvents<Context, Env>): FluentContextBuilder<Context, Env>;
|
|
42
|
+
narrative(fn: BuilderSystemPrompt<Context, Env>): FluentContextBuilder<Context, Env>;
|
|
43
|
+
system(fn: BuilderSystemPrompt<Context, Env>): FluentContextBuilder<Context, Env>;
|
|
44
|
+
skills(fn: BuilderSkills<Context, Env>): FluentContextBuilder<Context, Env>;
|
|
45
|
+
actions(fn: BuilderTools<Context, Env>): FluentContextBuilder<Context, Env>;
|
|
46
|
+
tools(fn: BuilderTools<Context, Env>): FluentContextBuilder<Context, Env>;
|
|
47
|
+
model(model: BuilderModel<Context, Env>): FluentContextBuilder<Context, Env>;
|
|
48
|
+
reactor(reactor: ContextReactor<Context, Env>): FluentContextBuilder<Context, Env>;
|
|
49
|
+
shouldContinue(fn: BuilderShouldContinue<Context, Env>): FluentContextBuilder<Context, Env>;
|
|
50
|
+
opts(opts: ContextOptions<Context, Env>): FluentContextBuilder<Context, Env>;
|
|
51
|
+
react(triggerEvent: ContextItem, params: ContextReactParams<Env>): ReturnType<ContextEngine<Context, Env>["react"]>;
|
|
52
|
+
stream(triggerEvent: ContextItem, params: ContextReactParams<Env>): ReturnType<ContextEngine<Context, Env>["react"]>;
|
|
53
|
+
register(): void;
|
|
54
|
+
config(): ContextConfig<Context, Env>;
|
|
55
|
+
build(): ContextInstance<Context, Env>;
|
|
56
|
+
};
|
|
57
|
+
type CreateContextEntry<Env extends ContextEnvironment> = {
|
|
58
|
+
context<Initializer extends AnyContextInitializer<Env>>(initializer: Initializer): FluentContextBuilder<InferContextFromInitializer<Initializer>, Env>;
|
|
59
|
+
initialize<Initializer extends AnyContextInitializer<Env>>(initializer: Initializer): FluentContextBuilder<InferContextFromInitializer<Initializer>, Env>;
|
|
60
|
+
};
|
|
61
|
+
export declare function createContext<Env extends ContextEnvironment = ContextEnvironment>(key: ContextKey): CreateContextEntry<Env>;
|
|
62
|
+
export {};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { ContextEngine, } from "./context.engine.js";
|
|
2
|
+
import { registerContext } from "./context.registry.js";
|
|
3
|
+
function isDynamicModelSelector(model) {
|
|
4
|
+
return typeof model === "function" && model.length >= 1;
|
|
5
|
+
}
|
|
6
|
+
export function context(config) {
|
|
7
|
+
class FunctionalContext extends ContextEngine {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(config.opts, config.reactor);
|
|
10
|
+
this.__config = config;
|
|
11
|
+
}
|
|
12
|
+
async initialize(contextValue, env) {
|
|
13
|
+
return config.context(contextValue, env);
|
|
14
|
+
}
|
|
15
|
+
async expandEvents(events, contextValue, env) {
|
|
16
|
+
if (config.expandEvents)
|
|
17
|
+
return config.expandEvents(events, contextValue, env);
|
|
18
|
+
return super.expandEvents(events, contextValue, env);
|
|
19
|
+
}
|
|
20
|
+
async buildSystemPrompt(contextValue, env) {
|
|
21
|
+
if (config.narrative)
|
|
22
|
+
return config.narrative(contextValue, env);
|
|
23
|
+
throw new Error("Context config is missing narrative()");
|
|
24
|
+
}
|
|
25
|
+
async buildSkills(contextValue, env) {
|
|
26
|
+
if (config.skills)
|
|
27
|
+
return config.skills(contextValue, env);
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
async buildTools(contextValue, env) {
|
|
31
|
+
if (config.actions)
|
|
32
|
+
return config.actions(contextValue, env);
|
|
33
|
+
if (config.tools)
|
|
34
|
+
return config.tools(contextValue, env);
|
|
35
|
+
throw new Error("Context config is missing actions()");
|
|
36
|
+
}
|
|
37
|
+
getModel(contextValue, env) {
|
|
38
|
+
if (isDynamicModelSelector(config.model))
|
|
39
|
+
return config.model(contextValue, env);
|
|
40
|
+
return config.model ?? super.getModel(contextValue, env);
|
|
41
|
+
}
|
|
42
|
+
async shouldContinue(args) {
|
|
43
|
+
if (config.shouldContinue)
|
|
44
|
+
return config.shouldContinue(args);
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const instance = new FunctionalContext();
|
|
49
|
+
return Object.assign(instance, { __config: config });
|
|
50
|
+
}
|
|
51
|
+
function assertConfigComplete(config) {
|
|
52
|
+
if (!config.context) {
|
|
53
|
+
throw new Error("createContext: you must define context() before building the Context.");
|
|
54
|
+
}
|
|
55
|
+
if (!config.narrative) {
|
|
56
|
+
throw new Error("createContext: you must define narrative() before building the Context.");
|
|
57
|
+
}
|
|
58
|
+
if (!config.actions && !config.tools) {
|
|
59
|
+
throw new Error("createContext: you must define actions() before building the Context.");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export function createContext(key) {
|
|
63
|
+
const initializeBuilder = (initializer) => {
|
|
64
|
+
const typedInitializer = (ctx, env) => initializer(ctx, env);
|
|
65
|
+
const fluentState = {
|
|
66
|
+
context: typedInitializer,
|
|
67
|
+
};
|
|
68
|
+
let cached = null;
|
|
69
|
+
const getOrBuild = () => {
|
|
70
|
+
assertConfigComplete(fluentState);
|
|
71
|
+
if (!cached) {
|
|
72
|
+
const config = fluentState;
|
|
73
|
+
cached = Object.assign(context(config), { __contextKey: key });
|
|
74
|
+
registerContext(key, () => cached);
|
|
75
|
+
}
|
|
76
|
+
return cached;
|
|
77
|
+
};
|
|
78
|
+
const builder = {
|
|
79
|
+
key,
|
|
80
|
+
expandEvents(fn) {
|
|
81
|
+
fluentState.expandEvents = fn;
|
|
82
|
+
return builder;
|
|
83
|
+
},
|
|
84
|
+
narrative(narrative) {
|
|
85
|
+
fluentState.narrative = narrative;
|
|
86
|
+
return builder;
|
|
87
|
+
},
|
|
88
|
+
system(system) {
|
|
89
|
+
fluentState.narrative = system;
|
|
90
|
+
return builder;
|
|
91
|
+
},
|
|
92
|
+
skills(skillsFactory) {
|
|
93
|
+
fluentState.skills = skillsFactory;
|
|
94
|
+
return builder;
|
|
95
|
+
},
|
|
96
|
+
actions(actionsFactory) {
|
|
97
|
+
fluentState.actions = actionsFactory;
|
|
98
|
+
return builder;
|
|
99
|
+
},
|
|
100
|
+
tools(toolsFactory) {
|
|
101
|
+
fluentState.actions = toolsFactory;
|
|
102
|
+
return builder;
|
|
103
|
+
},
|
|
104
|
+
model(model) {
|
|
105
|
+
fluentState.model = model;
|
|
106
|
+
return builder;
|
|
107
|
+
},
|
|
108
|
+
reactor(reactor) {
|
|
109
|
+
fluentState.reactor = reactor;
|
|
110
|
+
return builder;
|
|
111
|
+
},
|
|
112
|
+
shouldContinue(fn) {
|
|
113
|
+
fluentState.shouldContinue = fn;
|
|
114
|
+
return builder;
|
|
115
|
+
},
|
|
116
|
+
opts(options) {
|
|
117
|
+
fluentState.opts = options;
|
|
118
|
+
return builder;
|
|
119
|
+
},
|
|
120
|
+
react(triggerEvent, params) {
|
|
121
|
+
return getOrBuild().react(triggerEvent, params);
|
|
122
|
+
},
|
|
123
|
+
stream(triggerEvent, params) {
|
|
124
|
+
return builder.react(triggerEvent, params);
|
|
125
|
+
},
|
|
126
|
+
register() {
|
|
127
|
+
getOrBuild();
|
|
128
|
+
},
|
|
129
|
+
config() {
|
|
130
|
+
assertConfigComplete(fluentState);
|
|
131
|
+
return fluentState;
|
|
132
|
+
},
|
|
133
|
+
build() {
|
|
134
|
+
return getOrBuild();
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
return builder;
|
|
138
|
+
};
|
|
139
|
+
return {
|
|
140
|
+
context: initializeBuilder,
|
|
141
|
+
initialize: initializeBuilder,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ContextStore } from "./context.store.js";
|
|
2
|
+
import type { ConcreteDomain } from "@ekairos/domain";
|
|
3
|
+
export type ContextEnvironment = Record<string, unknown>;
|
|
4
|
+
export type ContextRuntime = {
|
|
5
|
+
store: ContextStore;
|
|
6
|
+
db: any;
|
|
7
|
+
domain?: ConcreteDomain<any, any>;
|
|
8
|
+
};
|
|
9
|
+
export declare function coerceContextRuntime(value: any): Promise<ContextRuntime>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const runtimeByDb = new WeakMap();
|
|
2
|
+
export async function coerceContextRuntime(value) {
|
|
3
|
+
if (!value) {
|
|
4
|
+
throw new Error("Context runtime resolver returned no value.");
|
|
5
|
+
}
|
|
6
|
+
if (typeof value === "object" && value.store) {
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
const dbCandidate = typeof value === "object" && value !== null && "db" in value
|
|
10
|
+
? value.db
|
|
11
|
+
: value;
|
|
12
|
+
if (!dbCandidate) {
|
|
13
|
+
throw new Error("Context runtime resolver did not provide a database or store.");
|
|
14
|
+
}
|
|
15
|
+
if (typeof dbCandidate === "object" && dbCandidate !== null) {
|
|
16
|
+
const cached = runtimeByDb.get(dbCandidate);
|
|
17
|
+
if (cached)
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
const { InstantStore } = await import("./stores/instant.store.js");
|
|
21
|
+
const runtime = {
|
|
22
|
+
store: new InstantStore(dbCandidate),
|
|
23
|
+
db: dbCandidate,
|
|
24
|
+
domain: typeof value === "object" ? value.domain : undefined,
|
|
25
|
+
};
|
|
26
|
+
if (typeof dbCandidate === "object" && dbCandidate !== null) {
|
|
27
|
+
runtimeByDb.set(dbCandidate, runtime);
|
|
28
|
+
}
|
|
29
|
+
return runtime;
|
|
30
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export type Transition<From extends string, To extends string> = {
|
|
2
|
+
from: From;
|
|
3
|
+
to: To;
|
|
4
|
+
};
|
|
5
|
+
export declare const CONTEXT_STATUSES: readonly ["open_idle", "open_streaming", "closed"];
|
|
6
|
+
export type ContextStatus = (typeof CONTEXT_STATUSES)[number];
|
|
7
|
+
export declare const EXECUTION_STATUSES: readonly ["executing", "completed", "failed"];
|
|
8
|
+
export type ExecutionStatus = (typeof EXECUTION_STATUSES)[number];
|
|
9
|
+
export declare const STEP_STATUSES: readonly ["running", "completed", "failed"];
|
|
10
|
+
export type StepStatus = (typeof STEP_STATUSES)[number];
|
|
11
|
+
export declare const STEP_KINDS: readonly ["message", "action_execute", "action_result"];
|
|
12
|
+
export type StepKind = (typeof STEP_KINDS)[number];
|
|
13
|
+
export declare const ITEM_STATUSES: readonly ["stored", "pending", "completed"];
|
|
14
|
+
export type ItemStatus = (typeof ITEM_STATUSES)[number];
|
|
15
|
+
export declare const ITEM_TYPES: readonly ["input", "output"];
|
|
16
|
+
export type ItemType = (typeof ITEM_TYPES)[number];
|
|
17
|
+
export declare const CHANNELS: readonly ["web", "whatsapp", "email"];
|
|
18
|
+
export type Channel = (typeof CHANNELS)[number];
|
|
19
|
+
export declare const TRACE_EVENT_KINDS: readonly ["workflow.run", "workflow.step", "context.run", "context.lifecycle", "context.execution", "context.item", "context.review", "context.step", "context.part", "context.llm"];
|
|
20
|
+
export type TraceEventKind = (typeof TRACE_EVENT_KINDS)[number];
|
|
21
|
+
export declare const STREAM_LIFECYCLE_CHUNK_TYPES: readonly ["chunk.start", "chunk.start_step", "chunk.finish_step", "chunk.finish"];
|
|
22
|
+
export declare const STREAM_TEXT_CHUNK_TYPES: readonly ["chunk.text_start", "chunk.text_delta", "chunk.text_end"];
|
|
23
|
+
export declare const STREAM_REASONING_CHUNK_TYPES: readonly ["chunk.reasoning_start", "chunk.reasoning_delta", "chunk.reasoning_end"];
|
|
24
|
+
export declare const STREAM_ACTION_CHUNK_TYPES: readonly ["chunk.action_input_start", "chunk.action_input_delta", "chunk.action_input_available", "chunk.action_output_available", "chunk.action_output_error"];
|
|
25
|
+
export declare const STREAM_SOURCE_CHUNK_TYPES: readonly ["chunk.source_url", "chunk.source_document", "chunk.file"];
|
|
26
|
+
export declare const STREAM_METADATA_CHUNK_TYPES: readonly ["chunk.message_metadata", "chunk.response_metadata"];
|
|
27
|
+
export declare const STREAM_ERROR_CHUNK_TYPES: readonly ["chunk.error", "chunk.unknown"];
|
|
28
|
+
export declare const CONTEXT_STREAM_CHUNK_TYPES: readonly ["chunk.start", "chunk.start_step", "chunk.finish_step", "chunk.finish", "chunk.text_start", "chunk.text_delta", "chunk.text_end", "chunk.reasoning_start", "chunk.reasoning_delta", "chunk.reasoning_end", "chunk.action_input_start", "chunk.action_input_delta", "chunk.action_input_available", "chunk.action_output_available", "chunk.action_output_error", "chunk.source_url", "chunk.source_document", "chunk.file", "chunk.message_metadata", "chunk.response_metadata", "chunk.error", "chunk.unknown"];
|
|
29
|
+
export type ContextStreamChunkType = (typeof CONTEXT_STREAM_CHUNK_TYPES)[number];
|
|
30
|
+
export declare function isContextStreamChunkType(value: string): value is ContextStreamChunkType;
|
|
31
|
+
export type ContextTransition = Transition<"open_idle" | "open_streaming" | "closed", "open_idle" | "open_streaming" | "closed">;
|
|
32
|
+
export type ExecutionTransition = Transition<"executing", "completed" | "failed">;
|
|
33
|
+
export type StepTransition = Transition<"running", "completed" | "failed">;
|
|
34
|
+
export type ItemTransition = Transition<"stored" | "pending", "pending" | "completed">;
|
|
35
|
+
export declare const CONTEXT_TRANSITIONS: readonly ContextTransition[];
|
|
36
|
+
export declare const EXECUTION_TRANSITIONS: readonly ExecutionTransition[];
|
|
37
|
+
export declare const STEP_TRANSITIONS: readonly StepTransition[];
|
|
38
|
+
export declare const ITEM_TRANSITIONS: readonly ItemTransition[];
|
|
39
|
+
export declare function canContextTransition(from: string, to: string): boolean;
|
|
40
|
+
export declare function canExecutionTransition(from: string, to: string): boolean;
|
|
41
|
+
export declare function canStepTransition(from: string, to: string): boolean;
|
|
42
|
+
export declare function canItemTransition(from: string, to: string): boolean;
|
|
43
|
+
export declare function assertContextTransition(from: string, to: string): void;
|
|
44
|
+
export declare function assertExecutionTransition(from: string, to: string): void;
|
|
45
|
+
export declare function assertStepTransition(from: string, to: string): void;
|
|
46
|
+
export declare function assertItemTransition(from: string, to: string): void;
|
|
47
|
+
export declare function assertContextPartKey(stepId: string, idx: number, key: string): void;
|