@aigne/core 1.0.10 → 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 (111) hide show
  1. package/lib/cjs/agent.d.ts +63 -19
  2. package/lib/cjs/agent.js +57 -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 +28 -31
  16. package/lib/cjs/llm-decision-agent.js +2 -2
  17. package/lib/cjs/llm-model.d.ts +4 -4
  18. package/lib/cjs/memorable.d.ts +21 -13
  19. package/lib/cjs/memorable.js +2 -2
  20. package/lib/cjs/open-api-agent.d.ts +9 -38
  21. package/lib/cjs/open-api-agent.js +12 -6
  22. package/lib/cjs/pipeline-agent.d.ts +14 -37
  23. package/lib/cjs/pipeline-agent.js +10 -10
  24. package/lib/cjs/runnable.d.ts +7 -24
  25. package/lib/cjs/sandbox-function-agent.d.ts +10 -38
  26. package/lib/cjs/sandbox-function-agent.js +9 -5
  27. package/lib/cjs/sandbox-function-runner.d.ts +8 -17
  28. package/lib/cjs/sandbox-function-runner.js +1 -0
  29. package/lib/cjs/utils/index.d.ts +1 -0
  30. package/lib/cjs/utils/index.js +1 -0
  31. package/lib/cjs/utils/logger.d.ts +3 -0
  32. package/lib/cjs/utils/logger.js +17 -0
  33. package/lib/cjs/utils/message-utils.d.ts +1 -1
  34. package/lib/cjs/utils/message-utils.js +1 -4
  35. package/lib/cjs/utils/open-api-parameter.js +3 -6
  36. package/lib/cjs/utils/runnable-type.d.ts +4 -22
  37. package/lib/cjs/utils/structured-output-schema.d.ts +2 -2
  38. package/lib/cjs/utils/type-utils.d.ts +1 -0
  39. package/lib/cjs/{logger.js → utils/type-utils.js} +0 -2
  40. package/lib/dts/agent.d.ts +63 -19
  41. package/lib/dts/context.d.ts +1 -1
  42. package/lib/dts/definitions/data-schema.d.ts +61 -0
  43. package/lib/dts/definitions/memory.d.ts +18 -5
  44. package/lib/dts/definitions/open-api.d.ts +3 -3
  45. package/lib/dts/definitions/preload.d.ts +43 -0
  46. package/lib/dts/function-agent.d.ts +15 -56
  47. package/lib/dts/index.d.ts +1 -1
  48. package/lib/dts/llm-agent.d.ts +17 -60
  49. package/lib/dts/llm-decision-agent.d.ts +28 -31
  50. package/lib/dts/llm-model.d.ts +4 -4
  51. package/lib/dts/memorable.d.ts +21 -13
  52. package/lib/dts/open-api-agent.d.ts +9 -38
  53. package/lib/dts/pipeline-agent.d.ts +14 -37
  54. package/lib/dts/runnable.d.ts +7 -24
  55. package/lib/dts/sandbox-function-agent.d.ts +10 -38
  56. package/lib/dts/sandbox-function-runner.d.ts +8 -17
  57. package/lib/dts/utils/index.d.ts +1 -0
  58. package/lib/dts/utils/logger.d.ts +3 -0
  59. package/lib/dts/utils/message-utils.d.ts +1 -1
  60. package/lib/dts/utils/runnable-type.d.ts +4 -22
  61. package/lib/dts/utils/structured-output-schema.d.ts +2 -2
  62. package/lib/dts/utils/type-utils.d.ts +1 -0
  63. package/lib/esm/agent.d.ts +63 -19
  64. package/lib/esm/agent.js +53 -9
  65. package/lib/esm/context.d.ts +1 -1
  66. package/lib/esm/definitions/data-schema.d.ts +61 -0
  67. package/lib/esm/definitions/memory.d.ts +18 -5
  68. package/lib/esm/definitions/open-api.d.ts +3 -3
  69. package/lib/esm/definitions/preload.d.ts +43 -0
  70. package/lib/esm/definitions/preload.js +31 -0
  71. package/lib/esm/function-agent.d.ts +15 -56
  72. package/lib/esm/function-agent.js +9 -6
  73. package/lib/esm/index.d.ts +1 -1
  74. package/lib/esm/index.js +1 -1
  75. package/lib/esm/llm-agent.d.ts +17 -60
  76. package/lib/esm/llm-agent.js +8 -5
  77. package/lib/esm/llm-decision-agent.d.ts +28 -31
  78. package/lib/esm/llm-decision-agent.js +4 -4
  79. package/lib/esm/llm-model.d.ts +4 -4
  80. package/lib/esm/memorable.d.ts +21 -13
  81. package/lib/esm/memorable.js +2 -2
  82. package/lib/esm/open-api-agent.d.ts +9 -38
  83. package/lib/esm/open-api-agent.js +11 -5
  84. package/lib/esm/pipeline-agent.d.ts +14 -37
  85. package/lib/esm/pipeline-agent.js +9 -6
  86. package/lib/esm/runnable.d.ts +7 -24
  87. package/lib/esm/sandbox-function-agent.d.ts +10 -38
  88. package/lib/esm/sandbox-function-agent.js +9 -5
  89. package/lib/esm/sandbox-function-runner.d.ts +8 -17
  90. package/lib/esm/sandbox-function-runner.js +2 -1
  91. package/lib/esm/utils/index.d.ts +1 -0
  92. package/lib/esm/utils/index.js +1 -0
  93. package/lib/esm/utils/logger.d.ts +3 -0
  94. package/lib/esm/utils/logger.js +14 -0
  95. package/lib/esm/utils/message-utils.d.ts +1 -1
  96. package/lib/esm/utils/message-utils.js +1 -4
  97. package/lib/esm/utils/open-api-parameter.js +1 -1
  98. package/lib/esm/utils/runnable-type.d.ts +4 -22
  99. package/lib/esm/utils/structured-output-schema.d.ts +2 -2
  100. package/lib/esm/utils/type-utils.d.ts +1 -0
  101. package/lib/esm/utils/type-utils.js +1 -0
  102. package/package.json +1 -1
  103. package/lib/cjs/definitions/data-type-schema.d.ts +0 -42
  104. package/lib/cjs/logger.d.ts +0 -2
  105. package/lib/dts/definitions/data-type-schema.d.ts +0 -42
  106. package/lib/dts/logger.d.ts +0 -2
  107. package/lib/esm/definitions/data-type-schema.d.ts +0 -42
  108. package/lib/esm/logger.d.ts +0 -2
  109. package/lib/esm/logger.js +0 -2
  110. /package/lib/cjs/definitions/{data-type-schema.js → data-schema.js} +0 -0
  111. /package/lib/esm/definitions/{data-type-schema.js → data-schema.js} +0 -0
@@ -1,16 +1,13 @@
1
1
  import type { Context, ContextState } from "./context";
2
2
  import type { DataType } from "./definitions/data-type";
3
- import type { Memorable } from "./memorable";
4
3
  import { OrderedRecord } from "./utils/ordered-map";
5
4
  export interface RunOptions {
6
5
  stream?: boolean;
7
6
  }
8
7
  export type RunnableResponse<T> = T | RunnableResponseStream<T>;
9
- export declare abstract class Runnable<I extends {
10
- [name: string]: any;
11
- } = {}, O extends {
12
- [name: string]: any;
13
- } = {}, State extends ContextState = ContextState> {
8
+ export type RunnableInput = Record<string, any>;
9
+ export type RunnableOutput = Record<string, any>;
10
+ export declare abstract class Runnable<I extends RunnableInput = RunnableInput, O extends RunnableOutput = RunnableOutput, State extends ContextState = ContextState> {
14
11
  definition: RunnableDefinition;
15
12
  context?: Context<State> | undefined;
16
13
  constructor(definition: RunnableDefinition, context?: Context<State> | undefined);
@@ -35,25 +32,11 @@ export interface RunnableDefinition {
35
32
  type: string;
36
33
  name?: string;
37
34
  description?: string;
38
- memories?: OrderedRecord<RunnableMemory>;
39
- inputs: OrderedRecord<RunnableInput>;
40
- outputs: OrderedRecord<RunnableOutput>;
35
+ inputs: OrderedRecord<RunnableInputType>;
36
+ outputs: OrderedRecord<RunnableOutputType>;
41
37
  }
42
- export interface RunnableMemory {
43
- id: string;
44
- name?: string;
45
- memory?: Memorable<any>;
46
- query?: {
47
- from: "variable";
48
- fromVariableId?: string;
49
- fromVariablePropPath?: string[];
50
- };
51
- options?: {
52
- k?: number;
53
- };
54
- }
55
- export type RunnableInput = DataType;
56
- export type RunnableOutput = DataType;
38
+ export type RunnableInputType = DataType;
39
+ export type RunnableOutputType = DataType;
57
40
  export interface RunnableResponseDelta<T> {
58
41
  $text?: string;
59
42
  delta?: Partial<T>;
@@ -1,48 +1,20 @@
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 } from "./runnable";
3
+ import { type SchemaToType } from "./definitions/data-schema";
4
+ import type { RunnableInput, RunnableOutput } from "./runnable";
7
5
  import type { SandboxFunctionRunner } from "./sandbox-function-runner";
8
- export declare class SandboxFunctionAgent<I extends {
9
- [name: string]: any;
10
- } = {}, O extends {
11
- [name: string]: any;
12
- } = {}, Memories extends {
13
- [name: string]: MemoryItemWithScore[];
14
- } = {}, State extends ContextState = ContextState> extends Agent<I, O, Memories, State> {
6
+ export declare class SandboxFunctionAgent<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> {
15
7
  definition: SandboxFunctionAgentDefinition;
16
- runner?: SandboxFunctionRunner<I, O, Memories, State> | undefined;
8
+ runner?: SandboxFunctionRunner<I, O, State, Preloads, Memories> | undefined;
17
9
  static create: typeof create;
18
- constructor(definition: SandboxFunctionAgentDefinition, context?: Context<State>, runner?: SandboxFunctionRunner<I, O, Memories, State> | undefined);
19
- process(input: I, options: AgentProcessOptions<Memories>): Promise<import("./runnable").RunnableResponseStream<O>>;
10
+ constructor(definition: SandboxFunctionAgentDefinition, context?: Context<State>, runner?: SandboxFunctionRunner<I, O, State, Preloads, Memories> | undefined);
11
+ process(input: AgentProcessInput<I, Preloads, Memories>, options: AgentProcessOptions<Preloads, Memories>): Promise<import("./runnable").RunnableResponseStream<O>>;
20
12
  }
21
- export interface CreateSandboxFunctionAgentOptions<I extends {
22
- [name: string]: DataTypeSchema;
23
- }, O extends {
24
- [name: string]: DataTypeSchema;
25
- }, Memories extends {
26
- [name: string]: CreateRunnableMemory<I>;
27
- }, State extends ContextState> {
28
- context?: Context<State>;
29
- name?: string;
30
- inputs: I;
31
- outputs: O;
32
- memories?: Memories;
13
+ export 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> & {
33
14
  language?: string;
34
15
  code: string;
35
- }
36
- export declare function create<I extends {
37
- [name: string]: DataTypeSchema;
38
- }, O extends {
39
- [name: string]: DataTypeSchema;
40
- }, Memories extends {
41
- [name: string]: CreateRunnableMemory<I>;
42
- }, State extends ContextState>({ context, ...options }: CreateSandboxFunctionAgentOptions<I, O, Memories, State>): SandboxFunctionAgent<SchemaMapType<I>, SchemaMapType<O>, {
43
- [name in keyof Memories]: MemorableSearchOutput<Memories[name]["memory"]>;
44
- }, State>;
45
- export interface SandboxFunctionAgentDefinition extends RunnableDefinition {
16
+ }): SandboxFunctionAgent<SchemaToType<I>, SchemaToType<O>, State, CreateAgentPreloadsType<I, Preloads>, CreateAgentMemoriesType<I, Memories>>;
17
+ export interface SandboxFunctionAgentDefinition extends AgentDefinition {
46
18
  type: "sandbox_function_agent";
47
19
  language?: string;
48
20
  code?: string;
@@ -18,8 +18,9 @@ const nanoid_1 = require("nanoid");
18
18
  const tsyringe_1 = require("tsyringe");
19
19
  const agent_1 = require("./agent");
20
20
  const constants_1 = require("./constants");
21
- const data_type_schema_1 = require("./definitions/data-type-schema");
21
+ const data_schema_1 = require("./definitions/data-schema");
22
22
  const memory_1 = require("./definitions/memory");
23
+ const preload_1 = require("./definitions/preload");
23
24
  let SandboxFunctionAgent = class SandboxFunctionAgent extends agent_1.Agent {
24
25
  definition;
25
26
  runner;
@@ -43,6 +44,7 @@ let SandboxFunctionAgent = class SandboxFunctionAgent extends agent_1.Agent {
43
44
  language,
44
45
  code,
45
46
  input,
47
+ preloads: options.preloads,
46
48
  memories: options.memories,
47
49
  context: { state: context.state },
48
50
  }, { stream: true });
@@ -56,10 +58,11 @@ exports.SandboxFunctionAgent = SandboxFunctionAgent = __decorate([
56
58
  __param(2, (0, tsyringe_1.inject)(constants_1.TYPES.sandboxFunctionRunner)),
57
59
  __metadata("design:paramtypes", [Object, Object, Function])
58
60
  ], SandboxFunctionAgent);
59
- function create({ context, ...options }) {
61
+ function create(options) {
60
62
  const agentId = options.name || (0, nanoid_1.nanoid)();
61
- const inputs = (0, data_type_schema_1.schemaToDataType)(options.inputs);
62
- const outputs = (0, data_type_schema_1.schemaToDataType)(options.outputs);
63
+ const inputs = (0, data_schema_1.schemaToDataType)(options.inputs);
64
+ const outputs = (0, data_schema_1.schemaToDataType)(options.outputs);
65
+ const preloads = (0, preload_1.preloadCreatorsToPreloads)(inputs, options.preloads);
63
66
  const memories = (0, memory_1.toRunnableMemories)(agentId, inputs, options.memories ?? {});
64
67
  return new SandboxFunctionAgent({
65
68
  id: agentId,
@@ -67,8 +70,9 @@ function create({ context, ...options }) {
67
70
  type: "sandbox_function_agent",
68
71
  inputs,
69
72
  outputs,
73
+ preloads,
70
74
  memories,
71
75
  language: options.language,
72
76
  code: options.code,
73
- }, context);
77
+ }, options.context);
74
78
  }
@@ -1,25 +1,16 @@
1
- import { Agent } from "./agent";
1
+ import { Agent, type AgentMemories, type AgentPreloads } from "./agent";
2
2
  import type { Context, ContextState } from "./context";
3
- import type { MemoryItemWithScore } from "./memorable";
4
- export interface SandboxFunctionRunnerInput<I extends {
5
- [name: string]: any;
6
- } = {}, Memories extends {
7
- [name: string]: MemoryItemWithScore[];
8
- } = {}, State extends ContextState = ContextState> {
3
+ import type { RunnableInput, RunnableOutput } from "./runnable";
4
+ export type SandboxFunctionRunnerInput<Input extends RunnableInput = RunnableInput, State extends ContextState = ContextState, Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> = {
9
5
  name: string;
10
6
  language?: string;
11
7
  code: string;
12
- input: I;
8
+ input: Input;
9
+ preloads: Preloads;
13
10
  memories: Memories;
14
11
  context: Pick<Context<State>, "state">;
15
- }
16
- export type SandboxFunctionRunnerOutput<O> = O;
17
- export declare abstract class SandboxFunctionRunner<I extends {
18
- [name: string]: any;
19
- } = {}, O extends {
20
- [name: string]: any;
21
- } = {}, Memories extends {
22
- [name: string]: MemoryItemWithScore[];
23
- } = {}, State extends ContextState = ContextState> extends Agent<SandboxFunctionRunnerInput<I, Memories, State>, SandboxFunctionRunnerOutput<O>> {
12
+ };
13
+ export type SandboxFunctionRunnerOutput<Output extends RunnableOutput> = Output;
14
+ export declare abstract class SandboxFunctionRunner<I extends RunnableInput = RunnableInput, O extends RunnableOutput = RunnableOutput, State extends ContextState = ContextState, Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> extends Agent<SandboxFunctionRunnerInput<I, State, Preloads, Memories>, SandboxFunctionRunnerOutput<O>> {
24
15
  constructor(context?: Context);
25
16
  }
@@ -15,6 +15,7 @@ class SandboxFunctionRunner extends agent_1.Agent {
15
15
  { id: "language", name: "language", type: "string" },
16
16
  { id: "code", name: "code", type: "string", required: true },
17
17
  { id: "input", name: "input", type: "object", required: true },
18
+ { id: "preloads", name: "preloads", type: "object", required: true },
18
19
  { id: "memories", name: "memories", type: "object", required: true },
19
20
  { id: "context", name: "context", type: "object", required: true },
20
21
  ]),
@@ -1,6 +1,7 @@
1
1
  export * from "./fetch";
2
2
  export * from "./fetch-open-api";
3
3
  export * from "./is-non-nullable";
4
+ export * from "./logger";
4
5
  export * from "./mustache-utils";
5
6
  export * from "./nullable";
6
7
  export * from "./omit";
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./fetch"), exports);
18
18
  __exportStar(require("./fetch-open-api"), exports);
19
19
  __exportStar(require("./is-non-nullable"), exports);
20
+ __exportStar(require("./logger"), exports);
20
21
  __exportStar(require("./mustache-utils"), exports);
21
22
  __exportStar(require("./nullable"), exports);
22
23
  __exportStar(require("./omit"), exports);
@@ -0,0 +1,3 @@
1
+ type Logger = Pick<typeof console, "debug" | "info" | "warn" | "error">;
2
+ export declare const logger: Logger;
3
+ export {};
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.logger = void 0;
4
+ function logLevel() {
5
+ return process.env.LOG_LEVEL || "info";
6
+ }
7
+ function createLogger(logger, level = logLevel()) {
8
+ return {
9
+ debug: level === "debug" ? logger.debug : () => { },
10
+ info: level === "debug" || level === "info" ? logger.info : () => { },
11
+ warn: level === "debug" || level === "info" || level === "warn"
12
+ ? logger.warn
13
+ : () => { },
14
+ error: logger.error,
15
+ };
16
+ }
17
+ exports.logger = createLogger(console);
@@ -11,7 +11,7 @@ export declare function memoriesToMessages(memories: {
11
11
  memory: string;
12
12
  };
13
13
  export declare function prepareMessages(definition: Pick<LLMAgentDefinition, "messages" | "memories" | "primaryMemoryId">, input: {
14
- [name: string]: any;
14
+ [name: string]: unknown;
15
15
  }, memories: {
16
16
  [name: string]: MemoryItemWithScore[];
17
17
  }): {
@@ -49,12 +49,9 @@ function memoriesToMessages(memories, { primaryMemoryName } = {}) {
49
49
  };
50
50
  }
51
51
  function prepareMessages(definition, input, memories) {
52
- const variables = { ...input, ...memories };
53
52
  const originalMessages = ordered_map_1.OrderedRecord.toArray(definition.messages).map(({ role, content }) => ({
54
53
  role,
55
- content: typeof content === "string"
56
- ? (0, mustache_utils_1.renderMessage)(content, variables)
57
- : content,
54
+ content: typeof content === "string" ? (0, mustache_utils_1.renderMessage)(content, input) : content,
58
55
  }));
59
56
  if (!originalMessages.length)
60
57
  throw new Error("Messages are required");
@@ -1,17 +1,14 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.formatOpenAPIRequest = formatOpenAPIRequest;
7
4
  const lodash_1 = require("lodash");
8
- const logger_1 = __importDefault(require("../logger"));
5
+ const logger_1 = require("./logger");
9
6
  const ordered_map_1 = require("./ordered-map");
10
7
  async function formatOpenAPIRequest(api, inputs, input) {
11
8
  const { url, method, ...inputParams } = processParameters(api, inputs, input);
12
- logger_1.default.debug("inputParams", inputParams);
9
+ logger_1.logger.debug("inputParams", inputParams);
13
10
  const authParams = await getAuthParams(api.auth);
14
- logger_1.default.debug("authParams", authParams);
11
+ logger_1.logger.debug("authParams", authParams);
15
12
  return {
16
13
  url,
17
14
  method,
@@ -1,23 +1,5 @@
1
- import type { Runnable, RunnableResponseStream } from "../runnable";
1
+ import type { Runnable, RunnableInput, RunnableOutput, RunnableResponseStream } from "../runnable";
2
2
  import type { UnionToIntersection } from "./union";
3
- export type ExtractRunnableInputType<T> = T extends Runnable<infer I, any> ? I : never;
4
- export type ExtractRunnableOutputType<T> = T extends Runnable<any, infer O> ? Exclude<O, RunnableResponseStream<any>> : never;
5
- export type ExtractRunnableInputTypeIntersection<T> = UnionToIntersection<ExtractRunnableInputType<T>, {}>;
6
- export type ExtractRunnableOutputTypeIntersection<T> = UnionToIntersection<ExtractRunnableOutputType<T>, {}>;
7
- export type BindAgentInput = {
8
- from: "ai";
9
- };
10
- export type PickInputFrom<I, From extends BindAgentInput["from"]> = {
11
- [key in keyof I as I[key] extends {
12
- from: From;
13
- } ? key : never]: I[key];
14
- };
15
- export type OmitBoundAgentInput<Case extends BoundAgent, From extends BindAgentInput["from"]> = Omit<UnionToIntersection<ExtractRunnableInputType<Case["runnable"]>, {}>, keyof PickInputFrom<Required<UnionToIntersection<NonNullable<Case["input"]>, {}>>, From>>;
16
- export type BindAgentInputs<R extends Runnable> = {
17
- [key in keyof ExtractRunnableInputType<R>]?: BindAgentInput;
18
- };
19
- export interface BoundAgent<R extends Runnable = Runnable, I extends BindAgentInputs<R> = BindAgentInputs<R>> {
20
- description?: string;
21
- runnable: R;
22
- input?: I;
23
- }
3
+ export type ExtractRunnableInputType<T> = T extends Runnable<infer I, RunnableOutput> ? I : never;
4
+ export type ExtractRunnableOutputType<T> = T extends Runnable<RunnableInput, infer O> ? Exclude<O, RunnableResponseStream<unknown>> : never;
5
+ export type ExtractRunnableInputTypeIntersection<T> = UnionToIntersection<ExtractRunnableInputType<T>, Record<string, unknown>>;
@@ -1,3 +1,3 @@
1
- import type { RunnableOutput } from "../runnable";
1
+ import type { RunnableOutputType } from "../runnable";
2
2
  import { OrderedRecord } from "./ordered-map";
3
- export declare function outputsToJsonSchema(outputs: OrderedRecord<RunnableOutput>): object;
3
+ export declare function outputsToJsonSchema(outputs: OrderedRecord<RunnableOutputType>): object;
@@ -0,0 +1 @@
1
+ export type ArrayElement<T> = T extends (infer U)[] ? U : never;
@@ -1,4 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const logger = console;
4
- exports.default = logger;
@@ -1,19 +1,17 @@
1
1
  import type { Context, ContextState } from "./context";
2
- import type { MemoryItemWithScore, MemoryMessage } from "./memorable";
3
- import { type RunOptions, Runnable, type RunnableResponse, type RunnableResponseChunk, type RunnableResponseStream } from "./runnable";
4
- import type { BindAgentInputs, BoundAgent } from "./utils/runnable-type";
5
- export interface AgentProcessOptions<Memories extends {
6
- [name: string]: MemoryItemWithScore[];
7
- }> {
8
- memories: Memories;
9
- }
10
- export declare abstract class Agent<I extends {
11
- [key: string]: any;
12
- } = {}, O extends {
13
- [key: string]: any;
14
- } = {}, Memories extends {
15
- [name: string]: MemoryItemWithScore[];
16
- } = {}, State extends ContextState = ContextState> extends Runnable<I, O, State> {
2
+ import type { DataSchema } from "./definitions/data-schema";
3
+ import type { AgentMemory, CreateRunnableMemory } from "./definitions/memory";
4
+ import type { AgentPreload, BindAgentInputs, BoundAgent, PreloadCreator } from "./definitions/preload";
5
+ import type { MemorableSearchOutput, MemoryItemWithScore, MemoryMessage } from "./memorable";
6
+ import { type RunOptions, Runnable, type RunnableDefinition, type RunnableInput, type RunnableOutput, type RunnableResponse, type RunnableResponseChunk, type RunnableResponseStream } from "./runnable";
7
+ import { OrderedRecord } from "./utils";
8
+ import type { ExtractRunnableOutputType } from "./utils/runnable-type";
9
+ export type AgentPreloads = Record<string, unknown>;
10
+ export type AgentMemories = Record<string, MemoryItemWithScore[]>;
11
+ export declare abstract class Agent<I extends RunnableInput = RunnableInput, O extends RunnableOutput = RunnableOutput, State extends ContextState = ContextState, Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> extends Runnable<I, O, State> {
12
+ definition: AgentDefinition;
13
+ context?: Context<State> | undefined;
14
+ constructor(definition: AgentDefinition, context?: Context<State> | undefined);
17
15
  private getMemoryQuery;
18
16
  /**
19
17
  * Load memories that are defined in the agent definition.
@@ -21,12 +19,13 @@ export declare abstract class Agent<I extends {
21
19
  * @param context The AIGNE context.
22
20
  * @returns A dictionary of memories, where the key is the memory id or name and the value is an array of memory items.
23
21
  */
24
- protected loadMemories(input: I, context?: Context): Promise<Memories>;
22
+ protected loadMemories(input: I): Promise<Memories>;
25
23
  /**
26
24
  * Update memories by user messages and assistant responses.
27
25
  * @param messages Messages to be added to memories.
28
26
  */
29
27
  protected updateMemories(messages: MemoryMessage[]): Promise<void>;
28
+ protected loadPreloads(input: I): Promise<Preloads>;
30
29
  run(input: I, options: RunOptions & {
31
30
  stream: true;
32
31
  }): Promise<RunnableResponseStream<O>>;
@@ -38,14 +37,59 @@ export declare abstract class Agent<I extends {
38
37
  * @param _result The agent result.
39
38
  */
40
39
  protected onResult(_result: O): Promise<void>;
41
- abstract process(input: I, options: AgentProcessOptions<Memories>): Promise<RunnableResponse<O> | AsyncGenerator<RunnableResponseChunk<O>, void>> | AsyncGenerator<RunnableResponseChunk<O>, void>;
40
+ abstract process(input: AgentProcessInput<I, Preloads, Memories>, options: AgentProcessOptions): Promise<RunnableResponse<O> | AsyncGenerator<RunnableResponseChunk<O>, void>> | AsyncGenerator<RunnableResponseChunk<O>, void>;
42
41
  /**
43
42
  * Bind some inputs to the agent, used for process of `PipelineAgent` or case of `LLMDecisionAgent`.
44
43
  * @param options The bind options.
45
44
  * @returns The bound agent.
46
45
  */
47
- bind<Input extends BindAgentInputs<typeof this>>(options: {
46
+ bind<Input extends BindAgentInputs<Record<string, never>, typeof this>>(options: {
48
47
  description?: string;
49
48
  input?: Input;
50
- }): BoundAgent<typeof this, Readonly<Input>>;
49
+ }): BoundAgent<Record<string, never>, typeof this, Readonly<Input>>;
50
+ }
51
+ export type AgentProcessOptions<Preloads extends AgentPreloads = AgentPreloads, Memories extends AgentMemories = AgentMemories> = {
52
+ preloads: Preloads;
53
+ memories: Memories;
54
+ };
55
+ export type AgentProcessInput<I extends RunnableInput, Preloads extends AgentPreloads, Memories extends AgentMemories> = I & Preloads & Memories;
56
+ export interface AgentDefinition extends RunnableDefinition {
57
+ preloads?: OrderedRecord<AgentPreload>;
58
+ memories?: OrderedRecord<AgentMemory>;
59
+ }
60
+ export type CreateAgentInputSchema = Record<string, DataSchema>;
61
+ export type CreateAgentOutputSchema<Extra extends Record<string, unknown> = Record<string, unknown>> = Record<string, DataSchema & Extra>;
62
+ export type CreateAgentPreloadsSchema<I extends CreateAgentInputSchema> = Record<string, PreloadCreator<I>>;
63
+ export type CreateAgentPreloadsType<I extends CreateAgentInputSchema, Preloads extends CreateAgentPreloadsSchema<I>> = {
64
+ [name in keyof Preloads]: ExtractRunnableOutputType<ReturnType<Preloads[name]>["runnable"]>;
65
+ };
66
+ export type CreateAgentMemoriesSchema<I extends CreateAgentInputSchema, Extras extends Record<string, unknown> = Record<string, unknown>> = Record<string, CreateRunnableMemory<I> & Extras>;
67
+ export type CreateAgentMemoriesType<I extends CreateAgentInputSchema, Memories extends CreateAgentMemoriesSchema<I, Record<string, unknown>>> = {
68
+ [key in keyof Memories]: MemorableSearchOutput<Memories[key]["memory"]>;
69
+ };
70
+ /**
71
+ * Common options to create Agent.
72
+ */
73
+ export interface CreateAgentOptions<I extends CreateAgentInputSchema, O extends CreateAgentOutputSchema, State extends ContextState, Preloads extends CreateAgentPreloadsSchema<I>, Memories extends CreateAgentMemoriesSchema<I, Record<string, unknown>>> {
74
+ context?: Context<State>;
75
+ /**
76
+ * Agent name, used to identify the agent.
77
+ */
78
+ name?: string;
79
+ /**
80
+ * Input variables for this agent.
81
+ */
82
+ inputs: I;
83
+ /**
84
+ * Output variables for this agent.
85
+ */
86
+ outputs: O;
87
+ /**
88
+ * Preload runnables before running this agent. the preloaded data are available in the agent as input variables.
89
+ */
90
+ preloads?: Preloads;
91
+ /**
92
+ * Memories to be used in this agent.
93
+ */
94
+ memories?: Memories;
51
95
  }
@@ -3,7 +3,7 @@ import type { Runnable, RunnableDefinition } from "./runnable";
3
3
  export interface ContextState {
4
4
  userId?: string;
5
5
  sessionId?: string;
6
- [key: string]: any;
6
+ [key: string]: unknown;
7
7
  }
8
8
  export interface ContextConfig {
9
9
  llmModel?: LLMModelConfiguration;
@@ -0,0 +1,61 @@
1
+ import type { MakeNullablePropertyOptional } from "../utils/nullable";
2
+ import { OrderedRecord } from "../utils/ordered-map";
3
+ import type { ArrayElement } from "../utils/type-utils";
4
+ import type { DataType } from "./data-type";
5
+ export declare function schemaToDataType(dataType: {
6
+ [name: string]: DataSchema;
7
+ }): OrderedRecord<DataType>;
8
+ export type DataSchema = DataSchemaString | DataSchemaNumber | DataSchemaBoolean | DataSchemaObject | DataSchemaArray;
9
+ export interface DataSchemaBase {
10
+ description?: string;
11
+ required?: boolean;
12
+ }
13
+ export interface DataSchemaString extends DataSchemaBase {
14
+ type: "string";
15
+ }
16
+ export interface DataSchemaNumber extends DataSchemaBase {
17
+ type: "number";
18
+ }
19
+ export interface DataSchemaBoolean extends DataSchemaBase {
20
+ type: "boolean";
21
+ }
22
+ export interface DataSchemaObject extends DataSchemaBase {
23
+ type: "object";
24
+ properties?: {
25
+ [key: string]: DataSchema;
26
+ };
27
+ }
28
+ export interface DataSchemaArray extends DataSchemaBase {
29
+ type: "array";
30
+ items?: DataSchema;
31
+ }
32
+ type SchemaTypeInner<T extends DataSchema> = T extends DataSchemaString ? string : T extends DataSchemaNumber ? number : T extends DataSchemaBoolean ? boolean : T extends DataSchemaObject ? MakeNullablePropertyOptional<{
33
+ [K in keyof T["properties"]]: SchemaType<NonNullable<T["properties"]>[K]>;
34
+ }> : T extends DataSchemaArray ? Extract<T["items"], null | undefined> extends null | undefined ? SchemaType<{
35
+ type: "object";
36
+ }>[] : SchemaType<NonNullable<T["items"]>>[] : never;
37
+ type SchemaType<T extends DataSchema> = T["required"] extends true ? SchemaTypeInner<T> : SchemaTypeInner<T> | undefined | null;
38
+ export type SchemaToType<T extends Record<string, DataSchema>> = SchemaType<{
39
+ type: "object";
40
+ required: true;
41
+ properties: T;
42
+ }>;
43
+ type TypeToSchemaInner<T> = Extract<T, string | undefined> extends string | undefined ? DataSchemaString & {
44
+ required: Extract<T, undefined> extends undefined ? false : true;
45
+ } : Extract<T, number | undefined> extends number | undefined ? DataSchemaNumber & {
46
+ required: Extract<T, undefined> extends undefined ? false : true;
47
+ } : Extract<T, boolean | undefined> extends boolean | undefined ? DataSchemaBoolean & {
48
+ required: Extract<T, undefined> extends undefined ? false : true;
49
+ } : Extract<T, object | undefined> extends object | undefined ? DataSchemaObject & {
50
+ properties: {
51
+ [K in keyof T]: TypeToSchemaInner<NonNullable<T[K]>>;
52
+ };
53
+ required: Extract<T, undefined> extends undefined ? false : true;
54
+ } : Extract<T, [] | undefined> extends [] | undefined ? DataSchemaArray & {
55
+ items: Extract<T, undefined> extends undefined ? undefined : TypeToSchemaInner<NonNullable<ArrayElement<T>>>;
56
+ required: Extract<T, undefined> extends undefined ? false : true;
57
+ } : never;
58
+ export type TypeToSchema<T extends Record<string, unknown>> = {
59
+ [K in keyof T]: TypeToSchemaInner<T[K]>;
60
+ };
61
+ export {};
@@ -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 {};