@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
|
@@ -1,62 +1,90 @@
|
|
|
1
|
+
import type { DomainLike } from "@ekairos/domain";
|
|
1
2
|
import type { ContextEnvironment } from "./context.config.js";
|
|
2
3
|
import type { ContextSkillPackage } from "./context.skill.js";
|
|
3
|
-
import { ContextEngine, type ContextModelInit, type ContextOptions, type
|
|
4
|
+
import { ContextEngine, type ContextModelInit, type ContextOptions, type ShouldContinue, type ContextShouldContinueArgs, type ContextReactParams, type ContextDirectReactParams, type ContextDurableReactParams, type ContextReactResult, type ContextDirectRun, type ContextWorkflowRun } from "./context.engine.js";
|
|
5
|
+
import type { ContextTool } from "./context.action.js";
|
|
6
|
+
import type { ContextRuntime, ContextRuntimeHandleForDomain } from "./context.runtime.js";
|
|
4
7
|
import type { ContextReactor } from "./context.reactor.js";
|
|
5
|
-
import type { ContextItem, StoredContext } from "./context.store.js";
|
|
8
|
+
import type { ContextItem, ContextResource, StoredContext, StoredContextResource } from "./context.store.js";
|
|
6
9
|
import { type ContextKey } from "./context.registry.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
import { eventsDomain } from "./schema.js";
|
|
11
|
+
export type ContextResourcesParams<Context, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain> = {
|
|
12
|
+
content: Context;
|
|
13
|
+
context: StoredContext<Context>;
|
|
14
|
+
env: Env;
|
|
15
|
+
runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>;
|
|
16
|
+
};
|
|
17
|
+
export interface ContextConfig<Context, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain> {
|
|
18
|
+
context: (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<Context> | Context;
|
|
19
|
+
expandEvents?: (events: ContextItem[], context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<ContextItem[]> | ContextItem[];
|
|
20
|
+
narrative?: (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<string> | string;
|
|
21
|
+
description?: (content: Context, context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<string> | string;
|
|
22
|
+
goal?: (content: Context, context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<string> | string;
|
|
23
|
+
resources?: (params: ContextResourcesParams<Context, Env, RequiredDomain>) => Promise<ContextResource[] | StoredContextResource[]> | ContextResource[] | StoredContextResource[];
|
|
24
|
+
skills?: (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<ContextSkillPackage[]> | ContextSkillPackage[];
|
|
25
|
+
actions: (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<Record<string, ContextTool<Context, Env, RequiredDomain>>> | Record<string, ContextTool<Context, Env, RequiredDomain>>;
|
|
13
26
|
/**
|
|
14
27
|
* @deprecated Use `actions()` instead.
|
|
15
28
|
*/
|
|
16
|
-
tools?: (context: StoredContext<Context>, env: Env) => Promise<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>;
|
|
29
|
+
tools?: (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<Record<string, ContextTool<Context, Env, RequiredDomain>>> | Record<string, ContextTool<Context, Env, RequiredDomain>>;
|
|
30
|
+
model?: ContextModelInit | ((context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => ContextModelInit);
|
|
31
|
+
reactor?: ContextReactor<Context, Env, RequiredDomain>;
|
|
32
|
+
shouldContinue?: (args: ContextShouldContinueArgs<Context, Env, RequiredDomain>) => Promise<ShouldContinue> | ShouldContinue;
|
|
33
|
+
opts?: ContextOptions<Context, Env, RequiredDomain>;
|
|
21
34
|
}
|
|
22
|
-
export type ContextInstance<Context, Env extends ContextEnvironment = ContextEnvironment> = ContextEngine<Context, Env> & {
|
|
23
|
-
readonly __config: ContextConfig<Context, Env>;
|
|
35
|
+
export type ContextInstance<Context, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain> = ContextEngine<Context, Env, RequiredDomain> & {
|
|
36
|
+
readonly __config: ContextConfig<Context, Env, RequiredDomain>;
|
|
24
37
|
readonly __contextKey?: ContextKey;
|
|
38
|
+
readonly __contextDomain?: RequiredDomain;
|
|
25
39
|
};
|
|
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
|
|
31
|
-
type
|
|
32
|
-
type
|
|
33
|
-
type
|
|
34
|
-
type
|
|
40
|
+
export declare function context<Context, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain>(config: ContextConfig<Context, Env, RequiredDomain>): ContextInstance<Context, Env, RequiredDomain>;
|
|
41
|
+
type AnyContextInitializer<Env extends ContextEnvironment, RequiredDomain extends DomainLike> = (context: StoredContext<any>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<any> | any;
|
|
42
|
+
type InferContextFromInitializer<I extends AnyContextInitializer<any, any>> = Awaited<ReturnType<I>>;
|
|
43
|
+
type BuilderSystemPrompt<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<string> | string;
|
|
44
|
+
type BuilderDescription<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = (content: Context, context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<string> | string;
|
|
45
|
+
type BuilderGoal<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = BuilderDescription<Context, Env, RequiredDomain>;
|
|
46
|
+
type BuilderResources<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = (params: ContextResourcesParams<Context, Env, RequiredDomain>) => Promise<ContextResource[] | StoredContextResource[] | null> | ContextResource[] | StoredContextResource[] | null;
|
|
47
|
+
type BuilderResource<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = ContextResource | StoredContextResource | ((params: ContextResourcesParams<Context, Env, RequiredDomain>) => Promise<ContextResource | StoredContextResource | null | undefined> | ContextResource | StoredContextResource | null | undefined);
|
|
48
|
+
type BuilderSkills<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<ContextSkillPackage[]> | ContextSkillPackage[];
|
|
49
|
+
type BuilderTools<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<Record<string, ContextTool<Context, Env, RequiredDomain>>> | Record<string, ContextTool<Context, Env, RequiredDomain>>;
|
|
50
|
+
type BuilderExpandEvents<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = (events: ContextItem[], context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<ContextItem[]> | ContextItem[];
|
|
51
|
+
type BuilderShouldContinue<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = (args: ContextShouldContinueArgs<Context, Env, RequiredDomain>) => Promise<ShouldContinue> | ShouldContinue;
|
|
52
|
+
type BuilderModel<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = ContextModelInit | ((context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => ContextModelInit);
|
|
35
53
|
export type RegistrableContextBuilder = {
|
|
36
54
|
key: ContextKey;
|
|
37
55
|
register: () => void;
|
|
38
56
|
};
|
|
39
|
-
type FluentContextBuilder<Context, Env extends ContextEnvironment> = {
|
|
57
|
+
type FluentContextBuilder<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = {
|
|
40
58
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
59
|
+
expandEvents(fn: BuilderExpandEvents<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
60
|
+
narrative(fn: BuilderSystemPrompt<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
61
|
+
system(fn: BuilderSystemPrompt<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
62
|
+
description(fn: BuilderDescription<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
63
|
+
goal(fn: BuilderGoal<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
64
|
+
resources(fn: BuilderResources<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
65
|
+
resource(resource: BuilderResource<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
66
|
+
skills(fn: BuilderSkills<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
67
|
+
actions(fn: BuilderTools<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
68
|
+
tools(fn: BuilderTools<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
69
|
+
model(model: BuilderModel<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
70
|
+
reactor(reactor: ContextReactor<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
71
|
+
shouldContinue(fn: BuilderShouldContinue<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
72
|
+
opts(opts: ContextOptions<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
|
|
73
|
+
react<Runtime extends ContextRuntime<Env>>(triggerEvent: ContextItem, params: ContextDurableReactParams<Env, RequiredDomain, Runtime>): Promise<ContextReactResult<Context, ContextWorkflowRun<Context>>>;
|
|
74
|
+
react<Runtime extends ContextRuntime<Env>>(triggerEvent: ContextItem, params: ContextDirectReactParams<Env, RequiredDomain, Runtime>): Promise<ContextReactResult<Context, ContextDirectRun<Context>>>;
|
|
75
|
+
react<Runtime extends ContextRuntime<Env>>(triggerEvent: ContextItem, params: ContextReactParams<Env, RequiredDomain, Runtime>): ReturnType<ContextEngine<Context, Env, RequiredDomain>["react"]>;
|
|
76
|
+
stream<Runtime extends ContextRuntime<Env>>(triggerEvent: ContextItem, params: ContextDurableReactParams<Env, RequiredDomain, Runtime>): Promise<ContextReactResult<Context, ContextWorkflowRun<Context>>>;
|
|
77
|
+
stream<Runtime extends ContextRuntime<Env>>(triggerEvent: ContextItem, params: ContextDirectReactParams<Env, RequiredDomain, Runtime>): Promise<ContextReactResult<Context, ContextDirectRun<Context>>>;
|
|
78
|
+
stream<Runtime extends ContextRuntime<Env>>(triggerEvent: ContextItem, params: ContextReactParams<Env, RequiredDomain, Runtime>): ReturnType<ContextEngine<Context, Env, RequiredDomain>["react"]>;
|
|
53
79
|
register(): void;
|
|
54
|
-
config(): ContextConfig<Context, Env>;
|
|
55
|
-
build(): ContextInstance<Context, Env>;
|
|
80
|
+
config(): ContextConfig<Context, Env, RequiredDomain>;
|
|
81
|
+
build(): ContextInstance<Context, Env, RequiredDomain>;
|
|
56
82
|
};
|
|
57
|
-
type CreateContextEntry<Env extends ContextEnvironment> = {
|
|
58
|
-
|
|
59
|
-
|
|
83
|
+
type CreateContextEntry<Env extends ContextEnvironment, RequiredDomain extends DomainLike> = {
|
|
84
|
+
content<Initializer extends AnyContextInitializer<Env, RequiredDomain>>(initializer: Initializer): FluentContextBuilder<InferContextFromInitializer<Initializer>, Env, RequiredDomain>;
|
|
85
|
+
context<Initializer extends AnyContextInitializer<Env, RequiredDomain>>(initializer: Initializer): FluentContextBuilder<InferContextFromInitializer<Initializer>, Env, RequiredDomain>;
|
|
86
|
+
initialize<Initializer extends AnyContextInitializer<Env, RequiredDomain>>(initializer: Initializer): FluentContextBuilder<InferContextFromInitializer<Initializer>, Env, RequiredDomain>;
|
|
60
87
|
};
|
|
61
|
-
export declare function createContext<Env extends ContextEnvironment = ContextEnvironment>(key: ContextKey): CreateContextEntry<Env>;
|
|
88
|
+
export declare function createContext<Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = DomainLike>(domain: RequiredDomain, key: ContextKey): CreateContextEntry<Env, RequiredDomain>;
|
|
89
|
+
export declare function createContext<Env extends ContextEnvironment = ContextEnvironment>(key: ContextKey): CreateContextEntry<Env, typeof eventsDomain>;
|
|
62
90
|
export {};
|
package/dist/context.builder.js
CHANGED
|
@@ -1,43 +1,95 @@
|
|
|
1
1
|
import { ContextEngine, } from "./context.engine.js";
|
|
2
2
|
import { registerContext } from "./context.registry.js";
|
|
3
|
+
import { eventsDomain } from "./schema.js";
|
|
3
4
|
function isDynamicModelSelector(model) {
|
|
4
5
|
return typeof model === "function" && model.length >= 1;
|
|
5
6
|
}
|
|
7
|
+
function stringifyContextContent(content) {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.stringify(content ?? null, null, 2);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return String(content);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function buildDefaultContextPrompt(params) {
|
|
16
|
+
const sections = [];
|
|
17
|
+
sections.push(`Content:\n${stringifyContextContent(params.content)}`);
|
|
18
|
+
if (params.resources?.length) {
|
|
19
|
+
sections.push(`Resources:\n${stringifyContextContent(params.resources)}`);
|
|
20
|
+
}
|
|
21
|
+
if (params.description) {
|
|
22
|
+
sections.push(`Description:\n${params.description}`);
|
|
23
|
+
}
|
|
24
|
+
if (params.goal) {
|
|
25
|
+
sections.push(`Goal:\n${params.goal}`);
|
|
26
|
+
}
|
|
27
|
+
return sections.join("\n\n");
|
|
28
|
+
}
|
|
6
29
|
export function context(config) {
|
|
7
30
|
class FunctionalContext extends ContextEngine {
|
|
8
31
|
constructor() {
|
|
9
32
|
super(config.opts, config.reactor);
|
|
10
33
|
this.__config = config;
|
|
11
34
|
}
|
|
12
|
-
async initialize(contextValue, env) {
|
|
13
|
-
return config.context(contextValue, env);
|
|
35
|
+
async initialize(contextValue, env, runtime) {
|
|
36
|
+
return config.context(contextValue, env, runtime);
|
|
14
37
|
}
|
|
15
|
-
async expandEvents(events, contextValue, env) {
|
|
38
|
+
async expandEvents(events, contextValue, env, runtime) {
|
|
16
39
|
if (config.expandEvents)
|
|
17
|
-
return config.expandEvents(events, contextValue, env);
|
|
18
|
-
return super.expandEvents(events, contextValue, env);
|
|
40
|
+
return config.expandEvents(events, contextValue, env, runtime);
|
|
41
|
+
return super.expandEvents(events, contextValue, env, runtime);
|
|
19
42
|
}
|
|
20
|
-
async buildSystemPrompt(contextValue, env) {
|
|
43
|
+
async buildSystemPrompt(contextValue, env, runtime) {
|
|
21
44
|
if (config.narrative)
|
|
22
|
-
return config.narrative(contextValue, env);
|
|
23
|
-
|
|
45
|
+
return config.narrative(contextValue, env, runtime);
|
|
46
|
+
const content = contextValue.content;
|
|
47
|
+
const description = contextValue.description ??
|
|
48
|
+
(config.description
|
|
49
|
+
? await config.description(content, contextValue, env, runtime)
|
|
50
|
+
: null);
|
|
51
|
+
const goal = contextValue.goal ??
|
|
52
|
+
(config.goal
|
|
53
|
+
? await config.goal(content, contextValue, env, runtime)
|
|
54
|
+
: null);
|
|
55
|
+
return buildDefaultContextPrompt({
|
|
56
|
+
content,
|
|
57
|
+
resources: contextValue.resources ?? [],
|
|
58
|
+
description,
|
|
59
|
+
goal,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
async describeContext(content, contextValue, env, runtime) {
|
|
63
|
+
if (!config.description)
|
|
64
|
+
return null;
|
|
65
|
+
return config.description(content, contextValue, env, runtime);
|
|
66
|
+
}
|
|
67
|
+
async defineGoal(content, contextValue, env, runtime) {
|
|
68
|
+
if (!config.goal)
|
|
69
|
+
return null;
|
|
70
|
+
return config.goal(content, contextValue, env, runtime);
|
|
24
71
|
}
|
|
25
|
-
async
|
|
72
|
+
async defineResources(content, contextValue, env, runtime) {
|
|
73
|
+
if (!config.resources)
|
|
74
|
+
return [];
|
|
75
|
+
return config.resources({ content, context: contextValue, env, runtime });
|
|
76
|
+
}
|
|
77
|
+
async buildSkills(contextValue, env, runtime) {
|
|
26
78
|
if (config.skills)
|
|
27
|
-
return config.skills(contextValue, env);
|
|
79
|
+
return config.skills(contextValue, env, runtime);
|
|
28
80
|
return [];
|
|
29
81
|
}
|
|
30
|
-
async buildTools(contextValue, env) {
|
|
82
|
+
async buildTools(contextValue, env, runtime) {
|
|
31
83
|
if (config.actions)
|
|
32
|
-
return config.actions(contextValue, env);
|
|
84
|
+
return config.actions(contextValue, env, runtime);
|
|
33
85
|
if (config.tools)
|
|
34
|
-
return config.tools(contextValue, env);
|
|
86
|
+
return config.tools(contextValue, env, runtime);
|
|
35
87
|
throw new Error("Context config is missing actions()");
|
|
36
88
|
}
|
|
37
|
-
getModel(contextValue, env) {
|
|
89
|
+
getModel(contextValue, env, runtime) {
|
|
38
90
|
if (isDynamicModelSelector(config.model))
|
|
39
|
-
return config.model(contextValue, env);
|
|
40
|
-
return config.model ?? super.getModel(contextValue, env);
|
|
91
|
+
return config.model(contextValue, env, runtime);
|
|
92
|
+
return config.model ?? super.getModel(contextValue, env, runtime);
|
|
41
93
|
}
|
|
42
94
|
async shouldContinue(args) {
|
|
43
95
|
if (config.shouldContinue)
|
|
@@ -50,27 +102,49 @@ export function context(config) {
|
|
|
50
102
|
}
|
|
51
103
|
function assertConfigComplete(config) {
|
|
52
104
|
if (!config.context) {
|
|
53
|
-
throw new Error("createContext: you must define
|
|
105
|
+
throw new Error("createContext: you must define content() before building the Context.");
|
|
54
106
|
}
|
|
55
|
-
if (!config.narrative) {
|
|
56
|
-
throw new Error("createContext: you must define
|
|
107
|
+
if (!config.narrative && (!config.description || !config.goal)) {
|
|
108
|
+
throw new Error("createContext: you must define description() and goal() before building the Context.");
|
|
57
109
|
}
|
|
58
110
|
if (!config.actions && !config.tools) {
|
|
59
111
|
throw new Error("createContext: you must define actions() before building the Context.");
|
|
60
112
|
}
|
|
61
113
|
}
|
|
62
|
-
export function createContext(
|
|
114
|
+
export function createContext(keyOrDomain, maybeKey) {
|
|
115
|
+
const requiredDomain = typeof keyOrDomain === "string" ? eventsDomain : keyOrDomain;
|
|
116
|
+
const key = typeof keyOrDomain === "string" ? keyOrDomain : maybeKey;
|
|
117
|
+
if (!key) {
|
|
118
|
+
throw new Error("createContext: key is required.");
|
|
119
|
+
}
|
|
63
120
|
const initializeBuilder = (initializer) => {
|
|
64
|
-
const typedInitializer = (ctx, env) => initializer(ctx, env);
|
|
121
|
+
const typedInitializer = (ctx, env, runtime) => initializer(ctx, env, runtime);
|
|
65
122
|
const fluentState = {
|
|
66
123
|
context: typedInitializer,
|
|
67
124
|
};
|
|
125
|
+
const resourceFactories = [];
|
|
126
|
+
const refreshResourcesConfig = () => {
|
|
127
|
+
fluentState.resources = async (params) => {
|
|
128
|
+
const resources = [];
|
|
129
|
+
for (const factory of resourceFactories) {
|
|
130
|
+
const result = await factory(params);
|
|
131
|
+
if (!result)
|
|
132
|
+
continue;
|
|
133
|
+
const list = Array.isArray(result) ? result : [result];
|
|
134
|
+
resources.push(...list);
|
|
135
|
+
}
|
|
136
|
+
return resources;
|
|
137
|
+
};
|
|
138
|
+
};
|
|
68
139
|
let cached = null;
|
|
69
140
|
const getOrBuild = () => {
|
|
70
141
|
assertConfigComplete(fluentState);
|
|
71
142
|
if (!cached) {
|
|
72
143
|
const config = fluentState;
|
|
73
|
-
cached = Object.assign(context(config), {
|
|
144
|
+
cached = Object.assign(context(config), {
|
|
145
|
+
__contextKey: key,
|
|
146
|
+
__contextDomain: requiredDomain,
|
|
147
|
+
});
|
|
74
148
|
registerContext(key, () => cached);
|
|
75
149
|
}
|
|
76
150
|
return cached;
|
|
@@ -89,6 +163,30 @@ export function createContext(key) {
|
|
|
89
163
|
fluentState.narrative = system;
|
|
90
164
|
return builder;
|
|
91
165
|
},
|
|
166
|
+
description(description) {
|
|
167
|
+
fluentState.description = description;
|
|
168
|
+
return builder;
|
|
169
|
+
},
|
|
170
|
+
goal(goal) {
|
|
171
|
+
fluentState.goal = goal;
|
|
172
|
+
return builder;
|
|
173
|
+
},
|
|
174
|
+
resources(resources) {
|
|
175
|
+
resourceFactories.push(resources);
|
|
176
|
+
refreshResourcesConfig();
|
|
177
|
+
return builder;
|
|
178
|
+
},
|
|
179
|
+
resource(resource) {
|
|
180
|
+
resourceFactories.push(async (params) => {
|
|
181
|
+
if (typeof resource === "function") {
|
|
182
|
+
const resolved = await resource(params);
|
|
183
|
+
return resolved ? [resolved] : [];
|
|
184
|
+
}
|
|
185
|
+
return [resource];
|
|
186
|
+
});
|
|
187
|
+
refreshResourcesConfig();
|
|
188
|
+
return builder;
|
|
189
|
+
},
|
|
92
190
|
skills(skillsFactory) {
|
|
93
191
|
fluentState.skills = skillsFactory;
|
|
94
192
|
return builder;
|
|
@@ -117,12 +215,8 @@ export function createContext(key) {
|
|
|
117
215
|
fluentState.opts = options;
|
|
118
216
|
return builder;
|
|
119
217
|
},
|
|
120
|
-
react(triggerEvent, params)
|
|
121
|
-
|
|
122
|
-
},
|
|
123
|
-
stream(triggerEvent, params) {
|
|
124
|
-
return builder.react(triggerEvent, params);
|
|
125
|
-
},
|
|
218
|
+
react: ((triggerEvent, params) => getOrBuild().react(triggerEvent, params)),
|
|
219
|
+
stream: ((triggerEvent, params) => builder.react(triggerEvent, params)),
|
|
126
220
|
register() {
|
|
127
221
|
getOrBuild();
|
|
128
222
|
},
|
|
@@ -137,6 +231,7 @@ export function createContext(key) {
|
|
|
137
231
|
return builder;
|
|
138
232
|
};
|
|
139
233
|
return {
|
|
234
|
+
content: initializeBuilder,
|
|
140
235
|
context: initializeBuilder,
|
|
141
236
|
initialize: initializeBuilder,
|
|
142
237
|
};
|
package/dist/context.config.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export type ContextEnvironment = Record<string, unknown>;
|
|
|
4
4
|
export type ContextRuntime = {
|
|
5
5
|
store: ContextStore;
|
|
6
6
|
db: any;
|
|
7
|
+
env?: ContextEnvironment;
|
|
7
8
|
domain?: ConcreteDomain<any, any>;
|
|
8
9
|
};
|
|
9
|
-
export declare function coerceContextRuntime(value: any): Promise<ContextRuntime>;
|
|
10
|
+
export declare function coerceContextRuntime(value: any, env?: ContextEnvironment): Promise<ContextRuntime>;
|
package/dist/context.config.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
const runtimeByDb = new WeakMap();
|
|
2
|
-
export async function coerceContextRuntime(value) {
|
|
2
|
+
export async function coerceContextRuntime(value, env) {
|
|
3
3
|
if (!value) {
|
|
4
4
|
throw new Error("Context runtime resolver returned no value.");
|
|
5
5
|
}
|
|
6
|
+
const resolvedEnv = env ??
|
|
7
|
+
(typeof value === "object" && value !== null
|
|
8
|
+
? value.env
|
|
9
|
+
: undefined);
|
|
6
10
|
if (typeof value === "object" && value.store) {
|
|
7
|
-
return value;
|
|
11
|
+
return resolvedEnv ? { ...value, env: resolvedEnv } : value;
|
|
8
12
|
}
|
|
9
13
|
const dbCandidate = typeof value === "object" && value !== null && "db" in value
|
|
10
14
|
? value.db
|
|
@@ -15,12 +19,13 @@ export async function coerceContextRuntime(value) {
|
|
|
15
19
|
if (typeof dbCandidate === "object" && dbCandidate !== null) {
|
|
16
20
|
const cached = runtimeByDb.get(dbCandidate);
|
|
17
21
|
if (cached)
|
|
18
|
-
return cached;
|
|
22
|
+
return resolvedEnv ? { ...cached, env: resolvedEnv } : cached;
|
|
19
23
|
}
|
|
20
24
|
const { InstantStore } = await import("./stores/instant.store.js");
|
|
21
25
|
const runtime = {
|
|
22
26
|
store: new InstantStore(dbCandidate),
|
|
23
27
|
db: dbCandidate,
|
|
28
|
+
env: resolvedEnv,
|
|
24
29
|
domain: typeof value === "object" ? value.domain : undefined,
|
|
25
30
|
};
|
|
26
31
|
if (typeof dbCandidate === "object" && dbCandidate !== null) {
|
|
@@ -8,8 +8,6 @@ export declare const EXECUTION_STATUSES: readonly ["executing", "completed", "fa
|
|
|
8
8
|
export type ExecutionStatus = (typeof EXECUTION_STATUSES)[number];
|
|
9
9
|
export declare const STEP_STATUSES: readonly ["running", "completed", "failed"];
|
|
10
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
11
|
export declare const ITEM_STATUSES: readonly ["stored", "pending", "completed"];
|
|
14
12
|
export type ItemStatus = (typeof ITEM_STATUSES)[number];
|
|
15
13
|
export declare const ITEM_TYPES: readonly ["input", "output"];
|
|
@@ -21,11 +19,11 @@ export type TraceEventKind = (typeof TRACE_EVENT_KINDS)[number];
|
|
|
21
19
|
export declare const STREAM_LIFECYCLE_CHUNK_TYPES: readonly ["chunk.start", "chunk.start_step", "chunk.finish_step", "chunk.finish"];
|
|
22
20
|
export declare const STREAM_TEXT_CHUNK_TYPES: readonly ["chunk.text_start", "chunk.text_delta", "chunk.text_end"];
|
|
23
21
|
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.
|
|
22
|
+
export declare const STREAM_ACTION_CHUNK_TYPES: readonly ["chunk.action_started", "chunk.action_input_delta", "chunk.action_completed", "chunk.action_failed"];
|
|
25
23
|
export declare const STREAM_SOURCE_CHUNK_TYPES: readonly ["chunk.source_url", "chunk.source_document", "chunk.file"];
|
|
26
24
|
export declare const STREAM_METADATA_CHUNK_TYPES: readonly ["chunk.message_metadata", "chunk.response_metadata"];
|
|
27
25
|
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.
|
|
26
|
+
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_started", "chunk.action_input_delta", "chunk.action_completed", "chunk.action_failed", "chunk.source_url", "chunk.source_document", "chunk.file", "chunk.message_metadata", "chunk.response_metadata", "chunk.error", "chunk.unknown"];
|
|
29
27
|
export type ContextStreamChunkType = (typeof CONTEXT_STREAM_CHUNK_TYPES)[number];
|
|
30
28
|
export declare function isContextStreamChunkType(value: string): value is ContextStreamChunkType;
|
|
31
29
|
export type ContextTransition = Transition<"open_idle" | "open_streaming" | "closed", "open_idle" | "open_streaming" | "closed">;
|
package/dist/context.contract.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
export const CONTEXT_STATUSES = ["open_idle", "open_streaming", "closed"];
|
|
2
2
|
export const EXECUTION_STATUSES = ["executing", "completed", "failed"];
|
|
3
3
|
export const STEP_STATUSES = ["running", "completed", "failed"];
|
|
4
|
-
export const STEP_KINDS = [
|
|
5
|
-
"message",
|
|
6
|
-
"action_execute",
|
|
7
|
-
"action_result",
|
|
8
|
-
];
|
|
9
4
|
export const ITEM_STATUSES = ["stored", "pending", "completed"];
|
|
10
5
|
export const ITEM_TYPES = [
|
|
11
6
|
"input",
|
|
@@ -41,11 +36,10 @@ export const STREAM_REASONING_CHUNK_TYPES = [
|
|
|
41
36
|
"chunk.reasoning_end",
|
|
42
37
|
];
|
|
43
38
|
export const STREAM_ACTION_CHUNK_TYPES = [
|
|
44
|
-
"chunk.
|
|
39
|
+
"chunk.action_started",
|
|
45
40
|
"chunk.action_input_delta",
|
|
46
|
-
"chunk.
|
|
47
|
-
"chunk.
|
|
48
|
-
"chunk.action_output_error",
|
|
41
|
+
"chunk.action_completed",
|
|
42
|
+
"chunk.action_failed",
|
|
49
43
|
];
|
|
50
44
|
export const STREAM_SOURCE_CHUNK_TYPES = [
|
|
51
45
|
"chunk.source_url",
|
package/dist/context.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { ContextEngine, type ContextOptions, type ContextStreamOptions, type ShouldContinue, type ContextShouldContinueArgs, type ContextReactParams, type ContextReactResult, type ContextDurableWorkflowPayload, type ContextDurableWorkflowFunction, type ContextModelInit, type
|
|
2
|
-
export { context, createContext, type ContextConfig, type ContextInstance, type RegistrableContextBuilder, } from "./context.builder.js";
|
|
1
|
+
export { ContextEngine, type ContextOptions, type ContextStreamOptions, type ShouldContinue, type ContextShouldContinueArgs, type ContextReactParams, type ContextDirectReactParams, type ContextDurableReactParams, type ContextReactResult, type ContextReactBase, type ContextReactFinalResult, type ContextDirectRun, type ContextReactRun, type ContextWorkflowRun, type ContextDurableWorkflowPayload, type ContextDurableWorkflowFunction, type ContextModelInit, type ContextToolExecuteContext, runContextReactionDirect, } from "./context.engine.js";
|
|
2
|
+
export { context, createContext, type ContextConfig, type ContextInstance, type ContextResourcesParams, type RegistrableContextBuilder, } from "./context.builder.js";
|
|
3
|
+
export { defineAction, action, type ContextAction, type ContextActionBase, type ContextActionExecuteParams, type AnyContextAction, type ContextActionDefinition, type ContextActionExecute, type DefineContextActionDefinition, type DefineContextActionExecute, type LegacyContextActionDefinition, type LegacyContextActionExecute, type ContextActionInput, type ContextActionOutput, type ContextProviderDefinedAction, type ContextActionSchema, type ContextTool, } from "./context.action.js";
|
|
3
4
|
export { createAiSdkReactor, createScriptedReactor, type CreateAiSdkReactorOptions, type CreateScriptedReactorOptions, type ScriptedReactorStep, type ContextReactor, type ContextReactorParams, type ContextReactionResult, type ContextActionRequest, type ContextReactionLLM, } from "./context.reactor.js";
|
|
4
5
|
export type { ContextSkillPackage, ContextSkillPackageFile, } from "./context.skill.js";
|