@aigne/core 1.0.9 → 1.0.11

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.
Files changed (116) hide show
  1. package/lib/cjs/agent.d.ts +70 -16
  2. package/lib/cjs/agent.js +68 -16
  3. package/lib/cjs/context.d.ts +1 -1
  4. package/lib/cjs/definitions/data-schema.d.ts +61 -0
  5. package/lib/cjs/definitions/memory.d.ts +18 -5
  6. package/lib/cjs/definitions/open-api.d.ts +3 -3
  7. package/lib/cjs/definitions/preload.d.ts +43 -0
  8. package/lib/cjs/definitions/preload.js +34 -0
  9. package/lib/cjs/function-agent.d.ts +15 -56
  10. package/lib/cjs/function-agent.js +9 -6
  11. package/lib/cjs/index.d.ts +1 -1
  12. package/lib/cjs/index.js +1 -1
  13. package/lib/cjs/llm-agent.d.ts +17 -60
  14. package/lib/cjs/llm-agent.js +8 -5
  15. package/lib/cjs/llm-decision-agent.d.ts +31 -51
  16. package/lib/cjs/llm-decision-agent.js +30 -13
  17. package/lib/cjs/llm-model.d.ts +4 -4
  18. package/lib/cjs/llm-models/openai-llm-model.d.ts +4 -1
  19. package/lib/cjs/llm-models/openai-llm-model.js +14 -14
  20. package/lib/cjs/memorable.d.ts +21 -13
  21. package/lib/cjs/memorable.js +2 -2
  22. package/lib/cjs/open-api-agent.d.ts +9 -38
  23. package/lib/cjs/open-api-agent.js +12 -6
  24. package/lib/cjs/pipeline-agent.d.ts +14 -37
  25. package/lib/cjs/pipeline-agent.js +10 -10
  26. package/lib/cjs/runnable.d.ts +7 -24
  27. package/lib/cjs/sandbox-function-agent.d.ts +10 -38
  28. package/lib/cjs/sandbox-function-agent.js +9 -5
  29. package/lib/cjs/sandbox-function-runner.d.ts +8 -17
  30. package/lib/cjs/sandbox-function-runner.js +1 -0
  31. package/lib/cjs/utils/index.d.ts +1 -0
  32. package/lib/cjs/utils/index.js +1 -0
  33. package/lib/cjs/utils/logger.d.ts +3 -0
  34. package/lib/cjs/utils/logger.js +17 -0
  35. package/lib/cjs/utils/message-utils.d.ts +1 -1
  36. package/lib/cjs/utils/message-utils.js +1 -4
  37. package/lib/cjs/utils/open-api-parameter.js +3 -6
  38. package/lib/cjs/utils/runnable-type.d.ts +5 -3
  39. package/lib/cjs/utils/structured-output-schema.d.ts +2 -2
  40. package/lib/cjs/utils/type-utils.d.ts +1 -0
  41. package/lib/cjs/{logger.js → utils/type-utils.js} +0 -2
  42. package/lib/dts/agent.d.ts +70 -16
  43. package/lib/dts/context.d.ts +1 -1
  44. package/lib/dts/definitions/data-schema.d.ts +61 -0
  45. package/lib/dts/definitions/memory.d.ts +18 -5
  46. package/lib/dts/definitions/open-api.d.ts +3 -3
  47. package/lib/dts/definitions/preload.d.ts +43 -0
  48. package/lib/dts/function-agent.d.ts +15 -56
  49. package/lib/dts/index.d.ts +1 -1
  50. package/lib/dts/llm-agent.d.ts +17 -60
  51. package/lib/dts/llm-decision-agent.d.ts +31 -51
  52. package/lib/dts/llm-model.d.ts +4 -4
  53. package/lib/dts/llm-models/openai-llm-model.d.ts +4 -1
  54. package/lib/dts/memorable.d.ts +21 -13
  55. package/lib/dts/open-api-agent.d.ts +9 -38
  56. package/lib/dts/pipeline-agent.d.ts +14 -37
  57. package/lib/dts/runnable.d.ts +7 -24
  58. package/lib/dts/sandbox-function-agent.d.ts +10 -38
  59. package/lib/dts/sandbox-function-runner.d.ts +8 -17
  60. package/lib/dts/utils/index.d.ts +1 -0
  61. package/lib/dts/utils/logger.d.ts +3 -0
  62. package/lib/dts/utils/message-utils.d.ts +1 -1
  63. package/lib/dts/utils/runnable-type.d.ts +5 -3
  64. package/lib/dts/utils/structured-output-schema.d.ts +2 -2
  65. package/lib/dts/utils/type-utils.d.ts +1 -0
  66. package/lib/esm/agent.d.ts +70 -16
  67. package/lib/esm/agent.js +64 -9
  68. package/lib/esm/context.d.ts +1 -1
  69. package/lib/esm/definitions/data-schema.d.ts +61 -0
  70. package/lib/esm/definitions/memory.d.ts +18 -5
  71. package/lib/esm/definitions/open-api.d.ts +3 -3
  72. package/lib/esm/definitions/preload.d.ts +43 -0
  73. package/lib/esm/definitions/preload.js +31 -0
  74. package/lib/esm/function-agent.d.ts +15 -56
  75. package/lib/esm/function-agent.js +9 -6
  76. package/lib/esm/index.d.ts +1 -1
  77. package/lib/esm/index.js +1 -1
  78. package/lib/esm/llm-agent.d.ts +17 -60
  79. package/lib/esm/llm-agent.js +8 -5
  80. package/lib/esm/llm-decision-agent.d.ts +31 -51
  81. package/lib/esm/llm-decision-agent.js +32 -15
  82. package/lib/esm/llm-model.d.ts +4 -4
  83. package/lib/esm/llm-models/openai-llm-model.d.ts +4 -1
  84. package/lib/esm/llm-models/openai-llm-model.js +14 -14
  85. package/lib/esm/memorable.d.ts +21 -13
  86. package/lib/esm/memorable.js +2 -2
  87. package/lib/esm/open-api-agent.d.ts +9 -38
  88. package/lib/esm/open-api-agent.js +11 -5
  89. package/lib/esm/pipeline-agent.d.ts +14 -37
  90. package/lib/esm/pipeline-agent.js +9 -6
  91. package/lib/esm/runnable.d.ts +7 -24
  92. package/lib/esm/sandbox-function-agent.d.ts +10 -38
  93. package/lib/esm/sandbox-function-agent.js +9 -5
  94. package/lib/esm/sandbox-function-runner.d.ts +8 -17
  95. package/lib/esm/sandbox-function-runner.js +2 -1
  96. package/lib/esm/utils/index.d.ts +1 -0
  97. package/lib/esm/utils/index.js +1 -0
  98. package/lib/esm/utils/logger.d.ts +3 -0
  99. package/lib/esm/utils/logger.js +14 -0
  100. package/lib/esm/utils/message-utils.d.ts +1 -1
  101. package/lib/esm/utils/message-utils.js +1 -4
  102. package/lib/esm/utils/open-api-parameter.js +1 -1
  103. package/lib/esm/utils/runnable-type.d.ts +5 -3
  104. package/lib/esm/utils/structured-output-schema.d.ts +2 -2
  105. package/lib/esm/utils/type-utils.d.ts +1 -0
  106. package/lib/esm/utils/type-utils.js +1 -0
  107. package/package.json +4 -4
  108. package/lib/cjs/definitions/data-type-schema.d.ts +0 -42
  109. package/lib/cjs/logger.d.ts +0 -2
  110. package/lib/dts/definitions/data-type-schema.d.ts +0 -42
  111. package/lib/dts/logger.d.ts +0 -2
  112. package/lib/esm/definitions/data-type-schema.d.ts +0 -42
  113. package/lib/esm/logger.d.ts +0 -2
  114. package/lib/esm/logger.js +0 -2
  115. /package/lib/cjs/definitions/{data-type-schema.js → data-schema.js} +0 -0
  116. /package/lib/esm/definitions/{data-type-schema.js → data-schema.js} +0 -0
@@ -1,9 +1,22 @@
1
1
  import type { Memorable } from "../memorable";
2
- import type { RunnableInput, RunnableMemory } from "../runnable";
2
+ import type { RunnableInputType } from "../runnable";
3
3
  import { OrderedRecord } from "../utils";
4
- import type { DataTypeSchema } from "./data-type-schema";
4
+ import type { DataSchema } from "./data-schema";
5
+ export interface AgentMemory {
6
+ id: string;
7
+ name?: string;
8
+ memory?: Memorable<unknown>;
9
+ query?: {
10
+ from: "variable";
11
+ fromVariableId?: string;
12
+ fromVariablePropPath?: string[];
13
+ };
14
+ options?: {
15
+ k?: number;
16
+ };
17
+ }
5
18
  export interface CreateRunnableMemory<I extends {
6
- [key: string]: DataTypeSchema;
19
+ [key: string]: DataSchema;
7
20
  } = {}> {
8
21
  /**
9
22
  * Memory instance to query/store memory.
@@ -35,6 +48,6 @@ export interface CreateRunnableMemory<I extends {
35
48
  k?: number;
36
49
  };
37
50
  }
38
- export declare function toRunnableMemories<I extends {}>(agentName: string, inputs: OrderedRecord<RunnableInput>, memories: {
51
+ export declare function toRunnableMemories<I extends {}>(agentName: string, inputs: OrderedRecord<RunnableInputType>, memories: {
39
52
  [name: string]: CreateRunnableMemory<I>;
40
- }): OrderedRecord<RunnableMemory>;
53
+ }): OrderedRecord<AgentMemory>;
@@ -1,5 +1,5 @@
1
+ import type { DataSchema } from "./data-schema";
1
2
  import type { DataType } from "./data-type";
2
- import type { DataTypeSchema } from "./data-type-schema";
3
3
  export interface BaseAuthConfig {
4
4
  type: "bearer" | "basic";
5
5
  token: string;
@@ -22,7 +22,7 @@ export type ParameterLocation = "path" | "query" | "body" | "header" | "cookie";
22
22
  export type OpenAPIDataType = DataType & {
23
23
  in?: ParameterLocation;
24
24
  };
25
- export type OpenAPIDataTypeSchema = DataTypeSchema & {
25
+ export type OpenAPIDataTypeSchema = DataSchema & {
26
26
  in?: ParameterLocation;
27
27
  };
28
28
  export type FetchRequest = {
@@ -31,6 +31,6 @@ export type FetchRequest = {
31
31
  query?: Record<string, string | number | boolean>;
32
32
  headers?: Record<string, string>;
33
33
  cookies?: Record<string, string>;
34
- body?: Record<string, any>;
34
+ body?: Record<string, unknown>;
35
35
  };
36
36
  export {};
@@ -0,0 +1,43 @@
1
+ import type { CreateAgentInputSchema, CreateAgentPreloadsSchema } from "../agent";
2
+ import type { Runnable, RunnableInputType } from "../runnable";
3
+ import { OrderedRecord } from "../utils/ordered-map";
4
+ import type { ExtractRunnableInputType } from "../utils/runnable-type";
5
+ import type { UnionToIntersection } from "../utils/union";
6
+ import type { SchemaToType } from "./data-schema";
7
+ export declare function preloadCreatorsToPreloads<I extends CreateAgentInputSchema, C extends CreateAgentPreloadsSchema<I>>(inputs: OrderedRecord<RunnableInputType>, creators?: C): OrderedRecord<AgentPreload>;
8
+ export type PreloadCreator<I extends CreateAgentInputSchema> = (preload: <R extends Runnable, Input extends BindAgentInputs<SchemaToType<I>, R>>(runnable: R, input: Input, options?: {
9
+ description?: string;
10
+ }) => Readonly<BoundAgent<I, R, Input>>) => ReturnType<typeof preload>;
11
+ export type BindAgentInputs<Vars extends Record<string, unknown>, R extends Runnable> = {
12
+ [key in keyof ExtractRunnableInputType<R>]: BindAgentInput<Vars, ExtractRunnableInputType<R>[key]>;
13
+ };
14
+ export interface AgentPreload {
15
+ id: string;
16
+ name?: string;
17
+ runnable?: {
18
+ id: string;
19
+ };
20
+ input?: {
21
+ [inputId: string]: BindAgentInput;
22
+ };
23
+ }
24
+ export type BindAgentInput<Vars extends Record<string, unknown> = Record<string, unknown>, Input = unknown> = {
25
+ from: "ai";
26
+ } | {
27
+ from: "input";
28
+ fromInput: keyof {
29
+ [name in keyof Vars as Vars[name] extends Input ? name : never]: name;
30
+ };
31
+ };
32
+ export interface BoundAgent<Vars extends Record<string, unknown> = Record<string, unknown>, R extends Runnable = Runnable, I extends BindAgentInputs<Vars, R> = BindAgentInputs<Vars, R>> {
33
+ description?: string;
34
+ runnable: R;
35
+ input?: I;
36
+ }
37
+ type PickInputFrom<I, From extends BindAgentInput["from"]> = {
38
+ [key in keyof I as I[key] extends {
39
+ from: From;
40
+ } ? key : never]: I[key];
41
+ };
42
+ export type OmitBoundAgentInput<Case extends BoundAgent, From extends BindAgentInput["from"]> = Omit<UnionToIntersection<ExtractRunnableInputType<Case["runnable"]>, Record<string, never>>, keyof PickInputFrom<Required<UnionToIntersection<NonNullable<Case["input"]>, Record<string, never>>>, From>>;
43
+ export {};
@@ -0,0 +1,31 @@
1
+ import { isNonNullable } from "../utils/is-non-nullable";
2
+ import { OrderedRecord } from "../utils/ordered-map";
3
+ export function preloadCreatorsToPreloads(inputs, creators) {
4
+ return OrderedRecord.fromArray(Object.entries(creators ?? {}).map(([name, preload]) => {
5
+ const p = preload((runnable, input, options) => ({
6
+ ...options,
7
+ runnable,
8
+ input,
9
+ }));
10
+ const input = Object.fromEntries(OrderedRecord.map(p.runnable.definition.inputs, (i) => {
11
+ if (!i.name)
12
+ return null;
13
+ const bind = p.input?.[i.name];
14
+ if (!bind)
15
+ return null;
16
+ if (bind.from !== "input")
17
+ throw new Error(`Unsupported bind from ${bind.from} in preloads`);
18
+ const from = OrderedRecord.find(inputs, (i) => i.name === bind.fromInput);
19
+ if (!from)
20
+ throw new Error(`Input ${bind.fromInput} not found`);
21
+ return [i.id, { ...bind, fromInput: from.id }];
22
+ }).filter(isNonNullable));
23
+ return {
24
+ id: name,
25
+ name,
26
+ description: p.description,
27
+ runnable: { id: p.runnable.id },
28
+ input,
29
+ };
30
+ }));
31
+ }
@@ -1,64 +1,23 @@
1
- import { Agent, type AgentProcessOptions } from "./agent";
1
+ import { Agent, type AgentDefinition, type AgentMemories, type AgentPreloads, type AgentProcessInput, type AgentProcessOptions, type CreateAgentInputSchema, type CreateAgentMemoriesSchema, type CreateAgentMemoriesType, type CreateAgentOptions, type CreateAgentOutputSchema, type CreateAgentPreloadsSchema, type CreateAgentPreloadsType } from "./agent";
2
2
  import type { Context, ContextState } from "./context";
3
- import { type DataTypeSchema, type SchemaMapType } from "./definitions/data-type-schema";
4
- import { type CreateRunnableMemory } from "./definitions/memory";
5
- import type { MemorableSearchOutput, MemoryItemWithScore } from "./memorable";
6
- import type { RunnableDefinition, RunnableResponse, RunnableResponseChunk } from "./runnable";
7
- export declare class FunctionAgent<I extends {
8
- [name: string]: any;
9
- } = {}, O extends {
10
- [name: string]: any;
11
- } = {}, Memories extends {
12
- [name: string]: MemoryItemWithScore[];
13
- } = {}, State extends ContextState = ContextState> extends Agent<I, O, Memories, State> {
14
- definition: FunctionAgentDefinition<I, O, Memories, State>;
3
+ import { type SchemaToType } from "./definitions/data-schema";
4
+ import type { RunnableInput, RunnableOutput, RunnableResponse, RunnableResponseChunk } from "./runnable";
5
+ export declare class FunctionAgent<I extends RunnableInput = RunnableInput, O extends RunnableOutput = RunnableOutput, State extends ContextState = ContextState, Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> extends Agent<I, O, State, Preloads, Memories> {
6
+ definition: FunctionAgentDefinition<I, O, State, Preloads, Memories>;
15
7
  static create: typeof create;
16
- constructor(definition: FunctionAgentDefinition<I, O, Memories, State>, context?: Context<State>);
17
- process(input: I, options: AgentProcessOptions<Memories>): Promise<AsyncGenerator<RunnableResponseChunk<O>, void, any> | RunnableResponse<O>>;
8
+ constructor(definition: FunctionAgentDefinition<I, O, State, Preloads, Memories>, context?: Context<State>);
9
+ process(input: AgentProcessInput<I, Preloads, Memories>, options: AgentProcessOptions<Preloads, Memories>): Promise<AsyncGenerator<RunnableResponseChunk<O>, void, any> | RunnableResponse<O>>;
18
10
  }
19
- export interface FunctionAgentDefinition<I extends {
20
- [name: string]: any;
21
- }, O extends {
22
- [name: string]: any;
23
- }, Memories extends {
24
- [name: string]: MemoryItemWithScore[];
25
- }, State extends ContextState> extends RunnableDefinition {
11
+ export interface FunctionAgentDefinition<I extends RunnableInput, O extends RunnableOutput, State extends ContextState, Preloads extends AgentPreloads, Memories extends AgentMemories> extends AgentDefinition {
26
12
  type: "function_agent";
27
- function?: FunctionFuncType<I, O, Memories, State>;
13
+ function?: FunctionFuncType<I, O, State, Preloads, Memories>;
28
14
  }
29
- export type FunctionFuncType<I extends {
30
- [name: string]: any;
31
- }, O extends {
32
- [name: string]: any;
33
- }, Memories extends {
34
- [name: string]: MemoryItemWithScore[];
35
- }, State extends ContextState> = (input: I, options: {
36
- memories: Memories;
15
+ export type FunctionFuncType<I extends RunnableInput, O extends RunnableOutput, State extends ContextState, Preloads extends AgentPreloads, Memories extends AgentMemories> = (input: AgentProcessInput<I, Preloads, Memories>, options: {
37
16
  context: Context<State>;
17
+ preloads: Preloads;
18
+ memories: Memories;
38
19
  }) => Promise<RunnableResponse<O> | AsyncGenerator<RunnableResponseChunk<O>, void>> | AsyncGenerator<RunnableResponseChunk<O>, void>;
39
- export interface CreateFunctionAgentOptions<I extends {
40
- [name: string]: DataTypeSchema;
41
- }, O extends {
42
- [name: string]: DataTypeSchema;
43
- }, Memories extends {
44
- [name: string]: CreateRunnableMemory<I>;
45
- }, State extends ContextState> {
46
- context?: Context<State>;
47
- name?: string;
48
- inputs: I;
49
- outputs: O;
50
- memories?: Memories;
51
- function?: FunctionFuncType<SchemaMapType<I>, SchemaMapType<O>, {
52
- [key in keyof Memories]: MemorableSearchOutput<Memories[key]["memory"]>;
53
- }, State>;
54
- }
55
- declare function create<I extends {
56
- [name: string]: DataTypeSchema;
57
- }, O extends {
58
- [name: string]: DataTypeSchema;
59
- }, Memories extends {
60
- [name: string]: CreateRunnableMemory<I>;
61
- }, State extends ContextState>({ context, ...options }: CreateFunctionAgentOptions<I, O, Memories, State>): FunctionAgent<SchemaMapType<I>, SchemaMapType<O>, {
62
- [name in keyof Memories]: MemorableSearchOutput<Memories[name]["memory"]>;
63
- }, State>;
20
+ declare function create<I extends CreateAgentInputSchema, O extends CreateAgentOutputSchema, State extends ContextState, Preloads extends CreateAgentPreloadsSchema<I>, Memories extends CreateAgentMemoriesSchema<I>>(options: CreateAgentOptions<I, O, State, Preloads, Memories> & {
21
+ function?: FunctionFuncType<SchemaToType<I>, SchemaToType<O>, State, CreateAgentPreloadsType<I, Preloads>, CreateAgentMemoriesType<I, Memories>>;
22
+ }): FunctionAgent<SchemaToType<I>, SchemaToType<O>, State, CreateAgentPreloadsType<I, Preloads>, CreateAgentMemoriesType<I, Memories>>;
64
23
  export {};
@@ -12,10 +12,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
12
  };
13
13
  import { nanoid } from "nanoid";
14
14
  import { inject, injectable } from "tsyringe";
15
- import { Agent } from "./agent";
15
+ import { Agent, } from "./agent";
16
16
  import { TYPES } from "./constants";
17
- import { schemaToDataType, } from "./definitions/data-type-schema";
18
- import { toRunnableMemories, } from "./definitions/memory";
17
+ import { schemaToDataType } from "./definitions/data-schema";
18
+ import { toRunnableMemories } from "./definitions/memory";
19
+ import { preloadCreatorsToPreloads } from "./definitions/preload";
19
20
  let FunctionAgent = class FunctionAgent extends Agent {
20
21
  definition;
21
22
  static create = create;
@@ -29,7 +30,7 @@ let FunctionAgent = class FunctionAgent extends Agent {
29
30
  throw new Error("Function is required");
30
31
  if (!context)
31
32
  throw new Error("Context is required");
32
- return await func(input, { context, memories: options.memories });
33
+ return await func(input, { ...options, context });
33
34
  }
34
35
  };
35
36
  FunctionAgent = __decorate([
@@ -39,10 +40,11 @@ FunctionAgent = __decorate([
39
40
  __metadata("design:paramtypes", [Object, Object])
40
41
  ], FunctionAgent);
41
42
  export { FunctionAgent };
42
- function create({ context, ...options }) {
43
+ function create(options) {
43
44
  const agentId = options.name || nanoid();
44
45
  const inputs = schemaToDataType(options.inputs);
45
46
  const outputs = schemaToDataType(options.outputs);
47
+ const preloads = preloadCreatorsToPreloads(inputs, options.preloads);
46
48
  const memories = toRunnableMemories(agentId, inputs, options.memories || {});
47
49
  return new FunctionAgent({
48
50
  id: agentId,
@@ -50,7 +52,8 @@ function create({ context, ...options }) {
50
52
  type: "function_agent",
51
53
  inputs,
52
54
  outputs,
55
+ preloads,
53
56
  memories,
54
57
  function: options.function,
55
- }, context);
58
+ }, options.context);
56
59
  }
@@ -2,7 +2,7 @@ export * from "./agent";
2
2
  export * from "./constants";
3
3
  export * from "./context";
4
4
  export * from "./definitions/data-type";
5
- export * from "./definitions/data-type-schema";
5
+ export * from "./definitions/data-schema";
6
6
  export * from "./definitions/memory";
7
7
  export * from "./definitions/open-api";
8
8
  export * from "./function-agent";
package/lib/esm/index.js CHANGED
@@ -2,7 +2,7 @@ export * from "./agent";
2
2
  export * from "./constants";
3
3
  export * from "./context";
4
4
  export * from "./definitions/data-type";
5
- export * from "./definitions/data-type-schema";
5
+ export * from "./definitions/data-schema";
6
6
  export * from "./definitions/memory";
7
7
  export * from "./definitions/open-api";
8
8
  export * from "./function-agent";
@@ -1,23 +1,15 @@
1
- import { Agent, type AgentProcessOptions } from "./agent";
1
+ import { Agent, type AgentDefinition, type AgentMemories, type AgentPreloads, type AgentProcessInput, type AgentProcessOptions, type CreateAgentInputSchema, type CreateAgentMemoriesSchema, type CreateAgentMemoriesType, type CreateAgentOptions, type CreateAgentOutputSchema, type CreateAgentPreloadsSchema, type CreateAgentPreloadsType } from "./agent";
2
2
  import type { Context, ContextState } from "./context";
3
- import { type DataTypeSchema, type SchemaMapType } from "./definitions/data-type-schema";
4
- import { type CreateRunnableMemory } from "./definitions/memory";
3
+ import { type SchemaToType } from "./definitions/data-schema";
5
4
  import type { LLMModel, LLMModelInputMessage, LLMModelInputs } from "./llm-model";
6
- import type { MemorableSearchOutput, MemoryItemWithScore } from "./memorable";
7
- import type { RunnableDefinition } from "./runnable";
5
+ import type { RunnableInput, RunnableOutput } from "./runnable";
8
6
  import { OrderedRecord } from "./utils/ordered-map";
9
- export declare class LLMAgent<I extends {
10
- [name: string]: any;
11
- } = {}, O extends {
12
- [name: string]: any;
13
- } = {}, Memories extends {
14
- [name: string]: MemoryItemWithScore[];
15
- } = {}, State extends ContextState = ContextState> extends Agent<I, O, Memories, State> {
7
+ export declare class LLMAgent<I extends RunnableInput = RunnableInput, O extends RunnableOutput = RunnableOutput, State extends ContextState = ContextState, Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> extends Agent<I, O, State, Preloads, Memories> {
16
8
  definition: LLMAgentDefinition;
17
9
  model?: LLMModel | undefined;
18
10
  static create: typeof create;
19
11
  constructor(definition: LLMAgentDefinition, context?: Context<State>, model?: LLMModel | undefined);
20
- process(input: I, options: AgentProcessOptions<Memories>): AsyncGenerator<{
12
+ process(input: AgentProcessInput<I, Preloads, Memories>, options: AgentProcessOptions): AsyncGenerator<{
21
13
  $text: string | undefined;
22
14
  delta?: undefined;
23
15
  } | {
@@ -27,7 +19,7 @@ export declare class LLMAgent<I extends {
27
19
  private runWithStructuredOutput;
28
20
  private runWithTextOutput;
29
21
  }
30
- export interface LLMAgentDefinition extends RunnableDefinition {
22
+ export interface LLMAgentDefinition extends AgentDefinition {
31
23
  type: "llm_agent";
32
24
  primaryMemoryId?: string;
33
25
  messages?: OrderedRecord<LLMModelInputMessage & {
@@ -36,32 +28,19 @@ export interface LLMAgentDefinition extends RunnableDefinition {
36
28
  modelOptions?: LLMModelInputs["modelOptions"];
37
29
  }
38
30
  /**
39
- * Options to create LLMAgent.
31
+ * Create LLMAgent definition.
32
+ * @param options Options to create LLMAgent.
33
+ * @returns LLMAgent definition.
40
34
  */
41
- export interface CreateLLMAgentOptions<I extends {
42
- [name: string]: DataTypeSchema;
43
- }, O extends {
44
- [name: string]: DataTypeSchema;
45
- }, Memories extends {
46
- [name: string]: CreateRunnableMemory<I>;
47
- }, State extends ContextState> {
48
- context?: Context<State>;
49
- /**
50
- * Agent name, used to identify the agent.
51
- */
52
- name?: string;
53
- /**
54
- * Input variables for this agent.
55
- */
56
- inputs: I;
35
+ declare function create<I extends CreateAgentInputSchema, O extends CreateAgentOutputSchema, State extends ContextState, Preloads extends CreateAgentPreloadsSchema<I>, Memories extends CreateAgentMemoriesSchema<I, {
57
36
  /**
58
- * Output variables for this agent.
37
+ * Whether this memory is primary? Primary memory will be passed as messages to LLM chat model,
38
+ * otherwise, it will be placed in a system message.
39
+ *
40
+ * Only one primary memory is allowed.
59
41
  */
60
- outputs: O;
61
- /**
62
- * Memories to be used in this agent.
63
- */
64
- memories?: Memories;
42
+ primary?: boolean;
43
+ }>>(options: CreateAgentOptions<I, O, State, Preloads, Memories> & {
65
44
  /**
66
45
  * Options for LLM chat model.
67
46
  */
@@ -70,27 +49,5 @@ export interface CreateLLMAgentOptions<I extends {
70
49
  * Messages to be passed to LLM chat model.
71
50
  */
72
51
  messages?: LLMModelInputMessage[];
73
- }
74
- /**
75
- * Create LLMAgent definition.
76
- * @param options Options to create LLMAgent.
77
- * @returns LLMAgent definition.
78
- */
79
- declare function create<I extends {
80
- [name: string]: DataTypeSchema;
81
- }, O extends {
82
- [name: string]: DataTypeSchema;
83
- }, Memories extends {
84
- [name: string]: CreateRunnableMemory<I> & {
85
- /**
86
- * Whether this memory is primary? Primary memory will be passed as messages to LLM chat model,
87
- * otherwise, it will be placed in a system message.
88
- *
89
- * Only one primary memory is allowed.
90
- */
91
- primary?: boolean;
92
- };
93
- }, State extends ContextState>({ context, ...options }: CreateLLMAgentOptions<I, O, Memories, State>): LLMAgent<SchemaMapType<I>, SchemaMapType<O>, {
94
- [name in keyof Memories]: MemorableSearchOutput<Memories[name]["memory"]>;
95
- }, State>;
52
+ }): LLMAgent<SchemaToType<I>, SchemaToType<O>, State, CreateAgentPreloadsType<I, Preloads>, CreateAgentMemoriesType<I, Memories>>;
96
53
  export {};
@@ -12,10 +12,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
12
  };
13
13
  import { nanoid } from "nanoid";
14
14
  import { inject, injectable } from "tsyringe";
15
- import { Agent } from "./agent";
15
+ import { Agent, } from "./agent";
16
16
  import { StreamTextOutputName, TYPES } from "./constants";
17
- import { schemaToDataType, } from "./definitions/data-type-schema";
18
- import { toRunnableMemories, } from "./definitions/memory";
17
+ import { schemaToDataType } from "./definitions/data-schema";
18
+ import { toRunnableMemories } from "./definitions/memory";
19
+ import { preloadCreatorsToPreloads } from "./definitions/preload";
19
20
  import { prepareMessages } from "./utils/message-utils";
20
21
  import { renderMessage } from "./utils/mustache-utils";
21
22
  import { OrderedRecord } from "./utils/ordered-map";
@@ -101,10 +102,11 @@ export { LLMAgent };
101
102
  * @param options Options to create LLMAgent.
102
103
  * @returns LLMAgent definition.
103
104
  */
104
- function create({ context, ...options }) {
105
+ function create(options) {
105
106
  const agentId = options.name || nanoid();
106
107
  const inputs = schemaToDataType(options.inputs);
107
108
  const outputs = schemaToDataType(options.outputs);
109
+ const preloads = preloadCreatorsToPreloads(inputs, options.preloads);
108
110
  const memories = toRunnableMemories(agentId, inputs, options.memories ?? {});
109
111
  const primaryMemoryNames = Object.entries(options.memories ?? {})
110
112
  .filter(([, i]) => i.primary)
@@ -123,9 +125,10 @@ function create({ context, ...options }) {
123
125
  type: "llm_agent",
124
126
  inputs,
125
127
  outputs,
128
+ preloads,
126
129
  primaryMemoryId: primaryMemoryNames?.at(0),
127
130
  memories,
128
131
  modelOptions: options.modelOptions,
129
132
  messages,
130
- }, context);
133
+ }, options.context);
131
134
  }
@@ -1,64 +1,41 @@
1
- import { Agent, type AgentProcessOptions } from "./agent";
1
+ import { Agent, type AgentDefinition, type AgentMemories, type AgentPreloads, type AgentProcessInput, type AgentProcessOptions, type CreateAgentMemoriesSchema, type CreateAgentMemoriesType, type CreateAgentOptions, type CreateAgentPreloadsSchema, type CreateAgentPreloadsType } from "./agent";
2
2
  import type { Context, ContextState } from "./context";
3
- import type { DataTypeSchema } from "./definitions/data-type-schema";
4
- import { type CreateRunnableMemory } from "./definitions/memory";
5
- import type { CreateLLMAgentOptions, LLMAgentDefinition } from "./llm-agent";
6
- import type { LLMModel } from "./llm-model";
7
- import type { MemorableSearchOutput, MemoryItemWithScore } from "./memorable";
8
- import type { Runnable, RunnableDefinition } from "./runnable";
3
+ import type { TypeToSchema } from "./definitions/data-schema";
4
+ import type { BindAgentInput, BoundAgent, OmitBoundAgentInput } from "./definitions/preload";
5
+ import type { LLMAgentDefinition } from "./llm-agent";
6
+ import type { LLMModel, LLMModelInputMessage, LLMModelInputs } from "./llm-model";
7
+ import type { RunnableInput, RunnableOutput } from "./runnable";
9
8
  import { OrderedRecord } from "./utils";
10
- import type { ExtractRunnableInputType, ExtractRunnableOutputType } from "./utils/runnable-type";
11
- import type { UnionToIntersection } from "./utils/union";
12
- export declare class LLMDecisionAgent<I extends {
13
- [name: string]: any;
14
- } = {}, O extends {
15
- [name: string]: any;
16
- } = {}, Memories extends {
17
- [name: string]: MemoryItemWithScore[];
18
- } = {}, State extends ContextState = ContextState> extends Agent<I, O, Memories, State> {
9
+ import type { ExtractRunnableInputTypeIntersection, ExtractRunnableOutputType } from "./utils/runnable-type";
10
+ export declare class LLMDecisionAgent<I extends RunnableInput = RunnableInput, O extends RunnableOutput = RunnableOutput, State extends ContextState = ContextState, Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> extends Agent<I, O, State, Preloads, Memories> {
19
11
  definition: LLMDecisionAgentDefinition;
20
12
  model?: LLMModel | undefined;
21
13
  static create: typeof create;
22
14
  constructor(definition: LLMDecisionAgentDefinition, context?: Context<State>, model?: LLMModel | undefined);
23
- process(input: I, options: AgentProcessOptions<Memories>): Promise<import("./runnable").RunnableResponse<O>>;
15
+ process(input: AgentProcessInput<I, Preloads, Memories>, options: AgentProcessOptions<Preloads, Memories>): Promise<import("./runnable").RunnableResponse<O>>;
24
16
  }
25
- export interface DecisionAgentCaseParameter<R extends Runnable = Runnable> {
26
- description?: string;
27
- runnable: R;
28
- }
29
- /**
30
- * Options to create LLMDecisionAgent.
31
- */
32
- export interface CreateLLMDecisionAgentOptions<Case extends DecisionAgentCaseParameter, I extends UnionToIntersection<ExtractRunnableInputType<Case["runnable"]>, {
33
- [name: string]: DataTypeSchema;
34
- }>, O extends UnionToIntersection<ExtractRunnableOutputType<Case["runnable"]>, {
35
- [name: string]: DataTypeSchema;
36
- }>, Memories extends {
37
- [name: string]: CreateRunnableMemory<I>;
38
- }, State extends ContextState> extends Pick<CreateLLMAgentOptions<I, O, Memories, State>, "name" | "memories" | "messages" | "modelOptions"> {
39
- context: Context<State>;
17
+ declare function create<Case extends BoundAgent, InputType extends ExtractRunnableInputTypeIntersection<Case["runnable"]>, OutputType extends ExtractRunnableOutputType<Case["runnable"]>, State extends ContextState, Preloads extends CreateAgentPreloadsSchema<TypeToSchema<InputType>>, Memories extends CreateAgentMemoriesSchema<TypeToSchema<InputType>, {
18
+ /**
19
+ * Whether this memory is primary? Primary memory will be passed as messages to LLM chat model,
20
+ * otherwise, it will be placed in a system message.
21
+ *
22
+ * Only one primary memory is allowed.
23
+ */
24
+ primary?: boolean;
25
+ }>>(options: Omit<CreateAgentOptions<never, never, State, Preloads, Memories>, "inputs" | "outputs"> & {
40
26
  cases: {
41
27
  [name: string]: Case;
42
28
  };
43
- }
44
- declare function create<Case extends DecisionAgentCaseParameter, I extends UnionToIntersection<ExtractRunnableInputType<Case["runnable"]>, {
45
- [name: string]: DataTypeSchema;
46
- }>, O extends UnionToIntersection<ExtractRunnableOutputType<Case["runnable"]>, {
47
- [name: string]: DataTypeSchema;
48
- }>, Memories extends {
49
- [name: string]: CreateRunnableMemory<I> & {
50
- /**
51
- * Whether this memory is primary? Primary memory will be passed as messages to LLM chat model,
52
- * otherwise, it will be placed in a system message.
53
- *
54
- * Only one primary memory is allowed.
55
- */
56
- primary?: boolean;
57
- };
58
- }, State extends ContextState>({ context, ...options }: CreateLLMDecisionAgentOptions<Case, I, O, Memories, State>): LLMDecisionAgent<UnionToIntersection<ExtractRunnableInputType<Case["runnable"]>, {}>, ExtractRunnableOutputType<Case["runnable"]>, {
59
- [name in keyof Memories]: MemorableSearchOutput<Memories[name]["memory"]>;
60
- }, State>;
61
- export interface LLMDecisionAgentDefinition extends RunnableDefinition, Pick<LLMAgentDefinition, "modelOptions" | "messages" | "primaryMemoryId"> {
29
+ /**
30
+ * Options for LLM chat model.
31
+ */
32
+ modelOptions?: LLMModelInputs["modelOptions"];
33
+ /**
34
+ * Messages to be passed to LLM chat model.
35
+ */
36
+ messages?: LLMModelInputMessage[];
37
+ }): LLMDecisionAgent<OmitBoundAgentInput<Case, "ai">, OutputType, State, CreateAgentPreloadsType<TypeToSchema<InputType>, Preloads>, CreateAgentMemoriesType<TypeToSchema<InputType>, Memories>>;
38
+ export interface LLMDecisionAgentDefinition extends AgentDefinition, Pick<LLMAgentDefinition, "modelOptions" | "messages" | "primaryMemoryId"> {
62
39
  type: "llm_decision_agent";
63
40
  cases?: OrderedRecord<LLMDecisionCase>;
64
41
  }
@@ -69,5 +46,8 @@ export interface LLMDecisionCase {
69
46
  runnable?: {
70
47
  id?: string;
71
48
  };
49
+ input?: {
50
+ [inputId: string]: BindAgentInput;
51
+ };
72
52
  }
73
53
  export {};
@@ -12,11 +12,12 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
12
  };
13
13
  import { nanoid } from "nanoid";
14
14
  import { inject, injectable } from "tsyringe";
15
- import { Agent } from "./agent";
15
+ import { Agent, } from "./agent";
16
16
  import { TYPES } from "./constants";
17
- import { toRunnableMemories, } from "./definitions/memory";
17
+ import { toRunnableMemories } from "./definitions/memory";
18
18
  import { OrderedRecord, extractOutputsFromRunnableOutput, renderMessage, } from "./utils";
19
19
  import { prepareMessages } from "./utils/message-utils";
20
+ import { outputsToJsonSchema } from "./utils/structured-output-schema";
20
21
  let LLMDecisionAgent = class LLMDecisionAgent extends Agent {
21
22
  definition;
22
23
  model;
@@ -42,19 +43,23 @@ let LLMDecisionAgent = class LLMDecisionAgent extends Agent {
42
43
  const name = t.name || runnable.name;
43
44
  if (!name)
44
45
  throw new Error("Case name is required");
45
- return { name, description: t.description, runnable };
46
+ return { ...t, name, runnable };
46
47
  }));
47
48
  const llmInputs = {
48
49
  messages: messagesWithMemory,
49
50
  modelOptions: definition.modelOptions,
50
51
  tools: cases.map((t) => {
51
- // TODO: auto generate parameters by llm model if needed
52
+ // Filter inputs that are bound from AI
53
+ const inputsFromAI = OrderedRecord.fromArray(OrderedRecord.filter(t.runnable.definition.inputs, (i) => t.input?.[i.id]?.from === "ai"));
54
+ const parameters = inputsFromAI.$indexes.length > 0
55
+ ? outputsToJsonSchema(inputsFromAI)
56
+ : {};
52
57
  return {
53
58
  type: "function",
54
59
  function: {
55
60
  name: t.name,
56
61
  description: t.description,
57
- parameters: {},
62
+ parameters,
58
63
  },
59
64
  };
60
65
  }),
@@ -62,14 +67,16 @@ let LLMDecisionAgent = class LLMDecisionAgent extends Agent {
62
67
  };
63
68
  const { toolCalls } = await model.run(llmInputs);
64
69
  // TODO: support run multiple calls
65
- const functionNameToCall = toolCalls?.[0]?.function?.name;
70
+ const { name: functionNameToCall, arguments: args } = toolCalls?.[0]?.function ?? {};
66
71
  if (!functionNameToCall)
67
72
  throw new Error("No any runnable called");
68
73
  const caseToCall = cases.find((i) => i.name === functionNameToCall);
69
74
  if (!caseToCall)
70
75
  throw new Error("Case not found");
76
+ // Prepare arguments generated by LLM model
77
+ const llmArgs = args ? JSON.parse(args) : {};
71
78
  // TODO: check result structure and omit undefined values
72
- const output = await caseToCall.runnable.run(input, { stream: true });
79
+ const output = await caseToCall.runnable.run({ ...input, ...llmArgs }, { stream: true });
73
80
  return extractOutputsFromRunnableOutput(output, ({ $text, ...json }) => this.updateMemories([
74
81
  ...originalMessages,
75
82
  {
@@ -87,14 +94,24 @@ LLMDecisionAgent = __decorate([
87
94
  __metadata("design:paramtypes", [Object, Object, Function])
88
95
  ], LLMDecisionAgent);
89
96
  export { LLMDecisionAgent };
90
- function create({ context, ...options }) {
97
+ function create(options) {
91
98
  const agentId = options.name || nanoid();
92
- const cases = OrderedRecord.fromArray(Object.entries(options.cases).map(([name, c]) => ({
93
- id: nanoid(),
94
- name: name || c.runnable.name,
95
- description: c.description,
96
- runnable: { id: c.runnable.id },
97
- })));
99
+ const cases = OrderedRecord.fromArray(Object.entries(options.cases).map(([name, c]) => {
100
+ const bindInputs = Object.entries(c.input ?? {});
101
+ return {
102
+ id: nanoid(),
103
+ name: name || c.runnable.name,
104
+ description: c.description,
105
+ runnable: { id: c.runnable.id },
106
+ input: Object.fromEntries(bindInputs.map(([inputName, v]) => {
107
+ const input = c.runnable.definition.inputs[inputName] ||
108
+ OrderedRecord.find(c.runnable.definition.inputs, (i) => i.name === inputName);
109
+ if (!input)
110
+ throw new Error(`Input ${inputName} not found`);
111
+ return [input.id, v];
112
+ })),
113
+ };
114
+ }));
98
115
  const inputs = OrderedRecord.merge(...Object.values(options.cases).map((i) => i.runnable.definition.inputs));
99
116
  const outputs = OrderedRecord.fromArray(OrderedRecord.map(OrderedRecord.merge(...Object.values(options.cases).map((i) => i.runnable.definition.outputs)), (o) => ({ ...o, required: false })));
100
117
  const memories = toRunnableMemories(agentId, inputs, options.memories ?? {});
@@ -120,5 +137,5 @@ function create({ context, ...options }) {
120
137
  memories,
121
138
  modelOptions: options.modelOptions,
122
139
  cases,
123
- }, context);
140
+ }, options.context);
124
141
  }