@ekairos/events 1.22.82-beta.development.0 → 1.22.84-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/dist/codex.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { createContext, defineAction } from "./context.js";
3
- import { didToolExecute } from "./context.toolcalls.js";
3
+ import { didActionExecute } from "./context.action-calls.js";
4
4
  export const DEFAULT_CODEX_TOOL_NAME = "codex";
5
5
  export const DEFAULT_CODEX_MODEL = "openai/gpt-5.2";
6
6
  export const codexToolInputSchema = z.object({
@@ -54,7 +54,7 @@ export function buildDefaultCodexNarrative(content) {
54
54
  ].join("\n");
55
55
  }
56
56
  export function didCodexToolExecute(event, toolName = DEFAULT_CODEX_TOOL_NAME) {
57
- return didToolExecute(event, toolName);
57
+ return didActionExecute(event, toolName);
58
58
  }
59
59
  export function createCodexContextBuilder(config) {
60
60
  const toolName = config.toolName ?? DEFAULT_CODEX_TOOL_NAME;
@@ -66,7 +66,7 @@ export function createCodexContextBuilder(config) {
66
66
  const model = config.model ??
67
67
  ((_ctx, env) => (typeof env.model === "string" && env.model.trim()) || DEFAULT_CODEX_MODEL);
68
68
  const shouldContinue = config.shouldContinue ??
69
- ((args) => !didToolExecute(args.reactionEvent, toolName));
69
+ ((args) => !didActionExecute(args.reactionEvent, toolName));
70
70
  let builder = createContext(config.key)
71
71
  .context(config.context)
72
72
  .narrative(narrative)
@@ -1,9 +1,9 @@
1
1
  /**
2
- * ## context.toolcalls.ts
2
+ * ## context.action-calls.ts
3
3
  *
4
4
  * This module isolates the **action-call plumbing** used by `context.engine.ts`.
5
5
  *
6
- * In our runtime, model tool calls are normalized into semantic `action` event parts.
6
+ * In our runtime, provider tool calls are normalized into semantic `action` event parts.
7
7
  * The engine needs to:
8
8
  * - extract a normalized list of action requests from `event.content.parts`, and
9
9
  * - merge action execution outcomes back into those parts.
@@ -12,9 +12,9 @@
12
12
  * these transformations testable and reusable.
13
13
  */
14
14
  import type { ContextItem } from "./context.store.js";
15
- export type ToolCall = {
16
- toolCallId: string;
17
- toolName: string;
15
+ export type ContextActionCall = {
16
+ actionCallId: string;
17
+ actionName: string;
18
18
  args: any;
19
19
  };
20
20
  /**
@@ -22,7 +22,7 @@ export type ToolCall = {
22
22
  *
23
23
  * Also accepts raw AI SDK tool UI parts before persistence normalization.
24
24
  */
25
- export declare function extractToolCallsFromParts(parts: any[] | undefined | null): ToolCall[];
25
+ export declare function extractActionCallsFromParts(parts: any[] | undefined | null): ContextActionCall[];
26
26
  /**
27
27
  * Applies an action execution outcome to the matching action part.
28
28
  *
@@ -30,19 +30,19 @@ export declare function extractToolCallsFromParts(parts: any[] | undefined | nul
30
30
  *
31
31
  * We match by action name and action call id.
32
32
  */
33
- export declare function applyToolExecutionResultToParts(parts: any[], toolCall: Pick<ToolCall, "toolCallId" | "toolName">, execution: {
33
+ export declare function applyActionExecutionResultToParts(parts: any[], actionCall: Pick<ContextActionCall, "actionCallId" | "actionName">, execution: {
34
34
  success: boolean;
35
35
  result: any;
36
36
  message?: string;
37
37
  }): any[];
38
38
  /**
39
- * Returns `true` when a given tool has a **settled** execution result in an event's parts.
39
+ * Returns `true` when a given action has a **settled** execution result in an event's parts.
40
40
  *
41
- * We treat a tool part as "executed" once it has either:
41
+ * We treat an action part as "executed" once it has either:
42
42
  * - `state: "output-available"` (success), or
43
43
  * - `state: "output-error"` (failure).
44
44
  *
45
45
  * This is useful for stop/continue logic in `context.shouldContinue(...)` where you want to
46
46
  * decide based on the persisted `reactionEvent` (not ephemeral in-memory arrays).
47
47
  */
48
- export declare function didToolExecute(event: Pick<ContextItem, "content">, toolName: string): boolean;
48
+ export declare function didActionExecute(event: Pick<ContextItem, "content">, actionName: string): boolean;
@@ -1,9 +1,9 @@
1
1
  /**
2
- * ## context.toolcalls.ts
2
+ * ## context.action-calls.ts
3
3
  *
4
4
  * This module isolates the **action-call plumbing** used by `context.engine.ts`.
5
5
  *
6
- * In our runtime, model tool calls are normalized into semantic `action` event parts.
6
+ * In our runtime, provider tool calls are normalized into semantic `action` event parts.
7
7
  * The engine needs to:
8
8
  * - extract a normalized list of action requests from `event.content.parts`, and
9
9
  * - merge action execution outcomes back into those parts.
@@ -17,20 +17,20 @@ import { isContextPartEnvelope, normalizePartsForPersistence, } from "./context.
17
17
  *
18
18
  * Also accepts raw AI SDK tool UI parts before persistence normalization.
19
19
  */
20
- export function extractToolCallsFromParts(parts) {
20
+ export function extractActionCallsFromParts(parts) {
21
21
  const safeParts = parts ?? [];
22
22
  return safeParts.reduce((acc, p) => {
23
23
  if (isContextPartEnvelope(p) && p.type === "action" && p.content.status === "started") {
24
24
  acc.push({
25
- toolCallId: p.content.actionCallId,
26
- toolName: p.content.actionName,
25
+ actionCallId: p.content.actionCallId,
26
+ actionName: p.content.actionName,
27
27
  args: p.content.input,
28
28
  });
29
29
  return acc;
30
30
  }
31
31
  if (typeof p?.type === "string" && p.type.startsWith("tool-")) {
32
- const toolName = p.type.split("-").slice(1).join("-");
33
- acc.push({ toolCallId: p.toolCallId, toolName, args: p.input });
32
+ const actionName = p.type.split("-").slice(1).join("-");
33
+ acc.push({ actionCallId: p.toolCallId, actionName, args: p.input });
34
34
  }
35
35
  return acc;
36
36
  }, []);
@@ -42,7 +42,7 @@ export function extractToolCallsFromParts(parts) {
42
42
  *
43
43
  * We match by action name and action call id.
44
44
  */
45
- export function applyToolExecutionResultToParts(parts, toolCall, execution) {
45
+ export function applyActionExecutionResultToParts(parts, actionCall, execution) {
46
46
  const normalized = normalizePartsForPersistence(parts);
47
47
  const next = [];
48
48
  let insertedResult = false;
@@ -51,8 +51,8 @@ export function applyToolExecutionResultToParts(parts, toolCall, execution) {
51
51
  if (part.type !== "action" || part.content.status !== "started") {
52
52
  continue;
53
53
  }
54
- if (part.content.actionCallId !== toolCall.toolCallId ||
55
- part.content.actionName !== toolCall.toolName) {
54
+ if (part.content.actionCallId !== actionCall.actionCallId ||
55
+ part.content.actionName !== actionCall.actionName) {
56
56
  continue;
57
57
  }
58
58
  next.push(execution.success
@@ -60,8 +60,8 @@ export function applyToolExecutionResultToParts(parts, toolCall, execution) {
60
60
  type: "action",
61
61
  content: {
62
62
  status: "completed",
63
- actionCallId: toolCall.toolCallId,
64
- actionName: toolCall.toolName,
63
+ actionCallId: actionCall.actionCallId,
64
+ actionName: actionCall.actionName,
65
65
  output: execution.result,
66
66
  },
67
67
  }
@@ -69,8 +69,8 @@ export function applyToolExecutionResultToParts(parts, toolCall, execution) {
69
69
  type: "action",
70
70
  content: {
71
71
  status: "failed",
72
- actionCallId: toolCall.toolCallId,
73
- actionName: toolCall.toolName,
72
+ actionCallId: actionCall.actionCallId,
73
+ actionName: actionCall.actionName,
74
74
  error: {
75
75
  message: String(execution.message || "Error"),
76
76
  },
@@ -84,8 +84,8 @@ export function applyToolExecutionResultToParts(parts, toolCall, execution) {
84
84
  type: "action",
85
85
  content: {
86
86
  status: "completed",
87
- actionCallId: toolCall.toolCallId,
88
- actionName: toolCall.toolName,
87
+ actionCallId: actionCall.actionCallId,
88
+ actionName: actionCall.actionName,
89
89
  output: execution.result,
90
90
  },
91
91
  }
@@ -93,8 +93,8 @@ export function applyToolExecutionResultToParts(parts, toolCall, execution) {
93
93
  type: "action",
94
94
  content: {
95
95
  status: "failed",
96
- actionCallId: toolCall.toolCallId,
97
- actionName: toolCall.toolName,
96
+ actionCallId: actionCall.actionCallId,
97
+ actionName: actionCall.actionName,
98
98
  error: {
99
99
  message: String(execution.message || "Error"),
100
100
  },
@@ -104,20 +104,20 @@ export function applyToolExecutionResultToParts(parts, toolCall, execution) {
104
104
  return next;
105
105
  }
106
106
  /**
107
- * Returns `true` when a given tool has a **settled** execution result in an event's parts.
107
+ * Returns `true` when a given action has a **settled** execution result in an event's parts.
108
108
  *
109
- * We treat a tool part as "executed" once it has either:
109
+ * We treat an action part as "executed" once it has either:
110
110
  * - `state: "output-available"` (success), or
111
111
  * - `state: "output-error"` (failure).
112
112
  *
113
113
  * This is useful for stop/continue logic in `context.shouldContinue(...)` where you want to
114
114
  * decide based on the persisted `reactionEvent` (not ephemeral in-memory arrays).
115
115
  */
116
- export function didToolExecute(event, toolName) {
116
+ export function didActionExecute(event, actionName) {
117
117
  const parts = (event.content.parts ?? []).flatMap((part) => isContextPartEnvelope(part) ? [part] : normalizePartsForPersistence([part]));
118
118
  return parts.some((p) => (p.type === "action" &&
119
- p.content.actionName === toolName &&
119
+ p.content.actionName === actionName &&
120
120
  (p.content.status === "completed" || p.content.status === "failed")) ||
121
- (p.type === `tool-${toolName}` &&
121
+ (p.type === `tool-${actionName}` &&
122
122
  (p.state === "output-available" || p.state === "output-error")));
123
123
  }
@@ -1,4 +1,4 @@
1
- import type { DomainSchemaResult } from "@ekairos/domain";
1
+ import type { DomainLike } from "@ekairos/domain";
2
2
  import type { z } from "zod";
3
3
  import type { ContextEnvironment } from "./context.config.js";
4
4
  import type { ContextToolExecuteContext } from "./context.engine.js";
@@ -19,24 +19,24 @@ export type ContextProviderDefinedAction = {
19
19
  args?: Record<string, unknown>;
20
20
  auto?: boolean;
21
21
  };
22
- export type ContextActionExecuteParams<TInput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ContextToolExecuteContext<Context, Env, RequiredDomain, Runtime> & {
22
+ export type ContextActionExecuteParams<TInput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ContextToolExecuteContext<Context, Env, RequiredDomain, Runtime> & {
23
23
  input: z.output<TInput>;
24
24
  };
25
- export type DefineContextActionExecute<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = (params: ContextActionExecuteParams<TInput, Context, Env, RequiredDomain, Runtime>) => MaybePromise<z.output<TOutput>>;
26
- export type ContextActionExecute<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = DefineContextActionExecute<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
27
- export type LegacyContextActionExecute<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = (input: z.output<TInput>, context: ContextToolExecuteContext<Context, Env, RequiredDomain, Runtime>) => MaybePromise<z.output<TOutput>>;
28
- export type DefineContextActionDefinition<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ContextActionBase & {
25
+ export type DefineContextActionExecute<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = (params: ContextActionExecuteParams<TInput, Context, Env, RequiredDomain, Runtime>) => MaybePromise<z.output<TOutput>>;
26
+ export type ContextActionExecute<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = DefineContextActionExecute<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
27
+ export type LegacyContextActionExecute<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = (input: z.output<TInput>, context: ContextToolExecuteContext<Context, Env, RequiredDomain, Runtime>) => MaybePromise<z.output<TOutput>>;
28
+ export type DefineContextActionDefinition<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ContextActionBase & {
29
29
  input: TInput;
30
30
  output: TOutput;
31
31
  execute: DefineContextActionExecute<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
32
32
  };
33
- export type LegacyContextActionDefinition<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ContextActionBase & {
33
+ export type LegacyContextActionDefinition<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ContextActionBase & {
34
34
  input: TInput;
35
35
  output: TOutput;
36
36
  execute: LegacyContextActionExecute<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
37
37
  };
38
- export type ContextActionDefinition<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = DefineContextActionDefinition<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
39
- export type ContextAction<TInput extends ContextActionSchema = ContextActionSchema, TOutput extends ContextActionSchema = ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ContextActionBase & {
38
+ export type ContextActionDefinition<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = DefineContextActionDefinition<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
39
+ export type ContextAction<TInput extends ContextActionSchema = ContextActionSchema, TOutput extends ContextActionSchema = ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ContextActionBase & {
40
40
  input: TInput;
41
41
  output: TOutput;
42
42
  inputSchema: TInput;
@@ -44,12 +44,12 @@ export type ContextAction<TInput extends ContextActionSchema = ContextActionSche
44
44
  execute: LegacyContextActionExecute<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
45
45
  };
46
46
  export type AnyContextAction = ContextAction<ContextActionSchema, ContextActionSchema, any, any, any, any>;
47
- export type ContextTool<Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ContextAction<ContextActionSchema, ContextActionSchema, Context, Env, RequiredDomain, Runtime> | ContextProviderDefinedAction;
47
+ export type ContextTool<Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>> = ContextAction<ContextActionSchema, ContextActionSchema, Context, Env, RequiredDomain, Runtime> | ContextProviderDefinedAction;
48
48
  export type ContextActionInput<TAction> = TAction extends ContextAction<infer TInput, any, any, any> ? z.output<TInput> : never;
49
49
  export type ContextActionOutput<TAction> = TAction extends ContextAction<any, infer TOutput, any, any> ? z.output<TOutput> : never;
50
- export declare function defineAction<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>>(definition: DefineContextActionDefinition<TInput, TOutput, Context, Env, RequiredDomain, Runtime>): ContextAction<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
50
+ export declare function defineAction<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>>(definition: DefineContextActionDefinition<TInput, TOutput, Context, Env, RequiredDomain, Runtime>): ContextAction<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
51
51
  /**
52
52
  * @deprecated Use defineAction().
53
53
  */
54
- export declare function action<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>>(definition: LegacyContextActionDefinition<TInput, TOutput, Context, Env, RequiredDomain, Runtime>): ContextAction<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
54
+ export declare function action<TInput extends ContextActionSchema, TOutput extends ContextActionSchema, Context = any, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain, Runtime extends ContextRuntime<Env> = ContextRuntime<Env>>(definition: LegacyContextActionDefinition<TInput, TOutput, Context, Env, RequiredDomain, Runtime>): ContextAction<TInput, TOutput, Context, Env, RequiredDomain, Runtime>;
55
55
  export {};
@@ -1,17 +1,26 @@
1
- import type { DomainSchemaResult } from "@ekairos/domain";
1
+ import type { DomainLike } from "@ekairos/domain";
2
2
  import type { ContextEnvironment } from "./context.config.js";
3
3
  import type { ContextSkillPackage } from "./context.skill.js";
4
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
5
  import type { ContextTool } from "./context.action.js";
6
6
  import type { ContextRuntime, ContextRuntimeHandleForDomain } from "./context.runtime.js";
7
7
  import type { ContextReactor } from "./context.reactor.js";
8
- import type { ContextItem, StoredContext } from "./context.store.js";
8
+ import type { ContextItem, ContextResource, StoredContext, StoredContextResource } from "./context.store.js";
9
9
  import { type ContextKey } from "./context.registry.js";
10
10
  import { eventsDomain } from "./schema.js";
11
- export interface ContextConfig<Context, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain> {
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> {
12
18
  context: (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<Context> | Context;
13
19
  expandEvents?: (events: ContextItem[], context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<ContextItem[]> | ContextItem[];
14
- narrative: (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<string> | string;
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[];
15
24
  skills?: (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<ContextSkillPackage[]> | ContextSkillPackage[];
16
25
  actions: (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<Record<string, ContextTool<Context, Env, RequiredDomain>>> | Record<string, ContextTool<Context, Env, RequiredDomain>>;
17
26
  /**
@@ -23,29 +32,37 @@ export interface ContextConfig<Context, Env extends ContextEnvironment = Context
23
32
  shouldContinue?: (args: ContextShouldContinueArgs<Context, Env, RequiredDomain>) => Promise<ShouldContinue> | ShouldContinue;
24
33
  opts?: ContextOptions<Context, Env, RequiredDomain>;
25
34
  }
26
- export type ContextInstance<Context, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain> = ContextEngine<Context, Env, RequiredDomain> & {
35
+ export type ContextInstance<Context, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = typeof eventsDomain> = ContextEngine<Context, Env, RequiredDomain> & {
27
36
  readonly __config: ContextConfig<Context, Env, RequiredDomain>;
28
37
  readonly __contextKey?: ContextKey;
29
38
  readonly __contextDomain?: RequiredDomain;
30
39
  };
31
- export declare function context<Context, Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = typeof eventsDomain>(config: ContextConfig<Context, Env, RequiredDomain>): ContextInstance<Context, Env, RequiredDomain>;
32
- type AnyContextInitializer<Env extends ContextEnvironment, RequiredDomain extends DomainSchemaResult> = (context: StoredContext<any>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<any> | any;
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;
33
42
  type InferContextFromInitializer<I extends AnyContextInitializer<any, any>> = Awaited<ReturnType<I>>;
34
- type BuilderSystemPrompt<Context, Env extends ContextEnvironment, RequiredDomain extends DomainSchemaResult> = (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<string> | string;
35
- type BuilderSkills<Context, Env extends ContextEnvironment, RequiredDomain extends DomainSchemaResult> = (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<ContextSkillPackage[]> | ContextSkillPackage[];
36
- type BuilderTools<Context, Env extends ContextEnvironment, RequiredDomain extends DomainSchemaResult> = (context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<Record<string, ContextTool<Context, Env, RequiredDomain>>> | Record<string, ContextTool<Context, Env, RequiredDomain>>;
37
- type BuilderExpandEvents<Context, Env extends ContextEnvironment, RequiredDomain extends DomainSchemaResult> = (events: ContextItem[], context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => Promise<ContextItem[]> | ContextItem[];
38
- type BuilderShouldContinue<Context, Env extends ContextEnvironment, RequiredDomain extends DomainSchemaResult> = (args: ContextShouldContinueArgs<Context, Env, RequiredDomain>) => Promise<ShouldContinue> | ShouldContinue;
39
- type BuilderModel<Context, Env extends ContextEnvironment, RequiredDomain extends DomainSchemaResult> = ContextModelInit | ((context: StoredContext<Context>, env: Env, runtime: ContextRuntimeHandleForDomain<Env, RequiredDomain>) => ContextModelInit);
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);
40
53
  export type RegistrableContextBuilder = {
41
54
  key: ContextKey;
42
55
  register: () => void;
43
56
  };
44
- type FluentContextBuilder<Context, Env extends ContextEnvironment, RequiredDomain extends DomainSchemaResult> = {
57
+ type FluentContextBuilder<Context, Env extends ContextEnvironment, RequiredDomain extends DomainLike> = {
45
58
  key: ContextKey;
46
59
  expandEvents(fn: BuilderExpandEvents<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
47
60
  narrative(fn: BuilderSystemPrompt<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
48
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>;
49
66
  skills(fn: BuilderSkills<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
50
67
  actions(fn: BuilderTools<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
51
68
  tools(fn: BuilderTools<Context, Env, RequiredDomain>): FluentContextBuilder<Context, Env, RequiredDomain>;
@@ -63,10 +80,11 @@ type FluentContextBuilder<Context, Env extends ContextEnvironment, RequiredDomai
63
80
  config(): ContextConfig<Context, Env, RequiredDomain>;
64
81
  build(): ContextInstance<Context, Env, RequiredDomain>;
65
82
  };
66
- type CreateContextEntry<Env extends ContextEnvironment, RequiredDomain extends DomainSchemaResult> = {
83
+ type CreateContextEntry<Env extends ContextEnvironment, RequiredDomain extends DomainLike> = {
84
+ content<Initializer extends AnyContextInitializer<Env, RequiredDomain>>(initializer: Initializer): FluentContextBuilder<InferContextFromInitializer<Initializer>, Env, RequiredDomain>;
67
85
  context<Initializer extends AnyContextInitializer<Env, RequiredDomain>>(initializer: Initializer): FluentContextBuilder<InferContextFromInitializer<Initializer>, Env, RequiredDomain>;
68
86
  initialize<Initializer extends AnyContextInitializer<Env, RequiredDomain>>(initializer: Initializer): FluentContextBuilder<InferContextFromInitializer<Initializer>, Env, RequiredDomain>;
69
87
  };
70
- export declare function createContext<Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainSchemaResult = DomainSchemaResult>(domain: RequiredDomain, key: ContextKey): CreateContextEntry<Env, RequiredDomain>;
88
+ export declare function createContext<Env extends ContextEnvironment = ContextEnvironment, RequiredDomain extends DomainLike = DomainLike>(domain: RequiredDomain, key: ContextKey): CreateContextEntry<Env, RequiredDomain>;
71
89
  export declare function createContext<Env extends ContextEnvironment = ContextEnvironment>(key: ContextKey): CreateContextEntry<Env, typeof eventsDomain>;
72
90
  export {};
@@ -4,6 +4,28 @@ import { eventsDomain } from "./schema.js";
4
4
  function isDynamicModelSelector(model) {
5
5
  return typeof model === "function" && model.length >= 1;
6
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
+ }
7
29
  export function context(config) {
8
30
  class FunctionalContext extends ContextEngine {
9
31
  constructor() {
@@ -21,7 +43,36 @@ export function context(config) {
21
43
  async buildSystemPrompt(contextValue, env, runtime) {
22
44
  if (config.narrative)
23
45
  return config.narrative(contextValue, env, runtime);
24
- throw new Error("Context config is missing narrative()");
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);
71
+ }
72
+ async defineResources(content, contextValue, env, runtime) {
73
+ if (!config.resources)
74
+ return [];
75
+ return config.resources({ content, context: contextValue, env, runtime });
25
76
  }
26
77
  async buildSkills(contextValue, env, runtime) {
27
78
  if (config.skills)
@@ -51,10 +102,10 @@ export function context(config) {
51
102
  }
52
103
  function assertConfigComplete(config) {
53
104
  if (!config.context) {
54
- throw new Error("createContext: you must define context() before building the Context.");
105
+ throw new Error("createContext: you must define content() before building the Context.");
55
106
  }
56
- if (!config.narrative) {
57
- throw new Error("createContext: you must define narrative() before building the Context.");
107
+ if (!config.narrative && (!config.description || !config.goal)) {
108
+ throw new Error("createContext: you must define description() and goal() before building the Context.");
58
109
  }
59
110
  if (!config.actions && !config.tools) {
60
111
  throw new Error("createContext: you must define actions() before building the Context.");
@@ -71,6 +122,20 @@ export function createContext(keyOrDomain, maybeKey) {
71
122
  const fluentState = {
72
123
  context: typedInitializer,
73
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
+ };
74
139
  let cached = null;
75
140
  const getOrBuild = () => {
76
141
  assertConfigComplete(fluentState);
@@ -98,6 +163,30 @@ export function createContext(keyOrDomain, maybeKey) {
98
163
  fluentState.narrative = system;
99
164
  return builder;
100
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
+ },
101
190
  skills(skillsFactory) {
102
191
  fluentState.skills = skillsFactory;
103
192
  return builder;
@@ -142,6 +231,7 @@ export function createContext(keyOrDomain, maybeKey) {
142
231
  return builder;
143
232
  };
144
233
  return {
234
+ content: initializeBuilder,
145
235
  context: initializeBuilder,
146
236
  initialize: initializeBuilder,
147
237
  };
package/dist/context.d.ts CHANGED
@@ -1,5 +1,5 @@
1
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 RegistrableContextBuilder, } from "./context.builder.js";
2
+ export { context, createContext, type ContextConfig, type ContextInstance, type ContextResourcesParams, type RegistrableContextBuilder, } from "./context.builder.js";
3
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";
4
4
  export { createAiSdkReactor, createScriptedReactor, type CreateAiSdkReactorOptions, type CreateScriptedReactorOptions, type ScriptedReactorStep, type ContextReactor, type ContextReactorParams, type ContextReactionResult, type ContextActionRequest, type ContextReactionLLM, } from "./context.reactor.js";
5
5
  export type { ContextSkillPackage, ContextSkillPackageFile, } from "./context.skill.js";