@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.
- package/lib/cjs/agent.d.ts +63 -19
- package/lib/cjs/agent.js +57 -16
- package/lib/cjs/context.d.ts +1 -1
- package/lib/cjs/definitions/data-schema.d.ts +61 -0
- package/lib/cjs/definitions/memory.d.ts +18 -5
- package/lib/cjs/definitions/open-api.d.ts +3 -3
- package/lib/cjs/definitions/preload.d.ts +43 -0
- package/lib/cjs/definitions/preload.js +34 -0
- package/lib/cjs/function-agent.d.ts +15 -56
- package/lib/cjs/function-agent.js +9 -6
- package/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +1 -1
- package/lib/cjs/llm-agent.d.ts +17 -60
- package/lib/cjs/llm-agent.js +8 -5
- package/lib/cjs/llm-decision-agent.d.ts +28 -31
- package/lib/cjs/llm-decision-agent.js +2 -2
- package/lib/cjs/llm-model.d.ts +4 -4
- package/lib/cjs/memorable.d.ts +21 -13
- package/lib/cjs/memorable.js +2 -2
- package/lib/cjs/open-api-agent.d.ts +9 -38
- package/lib/cjs/open-api-agent.js +12 -6
- package/lib/cjs/pipeline-agent.d.ts +14 -37
- package/lib/cjs/pipeline-agent.js +10 -10
- package/lib/cjs/runnable.d.ts +7 -24
- package/lib/cjs/sandbox-function-agent.d.ts +10 -38
- package/lib/cjs/sandbox-function-agent.js +9 -5
- package/lib/cjs/sandbox-function-runner.d.ts +8 -17
- package/lib/cjs/sandbox-function-runner.js +1 -0
- package/lib/cjs/utils/index.d.ts +1 -0
- package/lib/cjs/utils/index.js +1 -0
- package/lib/cjs/utils/logger.d.ts +3 -0
- package/lib/cjs/utils/logger.js +17 -0
- package/lib/cjs/utils/message-utils.d.ts +1 -1
- package/lib/cjs/utils/message-utils.js +1 -4
- package/lib/cjs/utils/open-api-parameter.js +3 -6
- package/lib/cjs/utils/runnable-type.d.ts +4 -22
- package/lib/cjs/utils/structured-output-schema.d.ts +2 -2
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/{logger.js → utils/type-utils.js} +0 -2
- package/lib/dts/agent.d.ts +63 -19
- package/lib/dts/context.d.ts +1 -1
- package/lib/dts/definitions/data-schema.d.ts +61 -0
- package/lib/dts/definitions/memory.d.ts +18 -5
- package/lib/dts/definitions/open-api.d.ts +3 -3
- package/lib/dts/definitions/preload.d.ts +43 -0
- package/lib/dts/function-agent.d.ts +15 -56
- package/lib/dts/index.d.ts +1 -1
- package/lib/dts/llm-agent.d.ts +17 -60
- package/lib/dts/llm-decision-agent.d.ts +28 -31
- package/lib/dts/llm-model.d.ts +4 -4
- package/lib/dts/memorable.d.ts +21 -13
- package/lib/dts/open-api-agent.d.ts +9 -38
- package/lib/dts/pipeline-agent.d.ts +14 -37
- package/lib/dts/runnable.d.ts +7 -24
- package/lib/dts/sandbox-function-agent.d.ts +10 -38
- package/lib/dts/sandbox-function-runner.d.ts +8 -17
- package/lib/dts/utils/index.d.ts +1 -0
- package/lib/dts/utils/logger.d.ts +3 -0
- package/lib/dts/utils/message-utils.d.ts +1 -1
- package/lib/dts/utils/runnable-type.d.ts +4 -22
- package/lib/dts/utils/structured-output-schema.d.ts +2 -2
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/esm/agent.d.ts +63 -19
- package/lib/esm/agent.js +53 -9
- package/lib/esm/context.d.ts +1 -1
- package/lib/esm/definitions/data-schema.d.ts +61 -0
- package/lib/esm/definitions/memory.d.ts +18 -5
- package/lib/esm/definitions/open-api.d.ts +3 -3
- package/lib/esm/definitions/preload.d.ts +43 -0
- package/lib/esm/definitions/preload.js +31 -0
- package/lib/esm/function-agent.d.ts +15 -56
- package/lib/esm/function-agent.js +9 -6
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/llm-agent.d.ts +17 -60
- package/lib/esm/llm-agent.js +8 -5
- package/lib/esm/llm-decision-agent.d.ts +28 -31
- package/lib/esm/llm-decision-agent.js +4 -4
- package/lib/esm/llm-model.d.ts +4 -4
- package/lib/esm/memorable.d.ts +21 -13
- package/lib/esm/memorable.js +2 -2
- package/lib/esm/open-api-agent.d.ts +9 -38
- package/lib/esm/open-api-agent.js +11 -5
- package/lib/esm/pipeline-agent.d.ts +14 -37
- package/lib/esm/pipeline-agent.js +9 -6
- package/lib/esm/runnable.d.ts +7 -24
- package/lib/esm/sandbox-function-agent.d.ts +10 -38
- package/lib/esm/sandbox-function-agent.js +9 -5
- package/lib/esm/sandbox-function-runner.d.ts +8 -17
- package/lib/esm/sandbox-function-runner.js +2 -1
- package/lib/esm/utils/index.d.ts +1 -0
- package/lib/esm/utils/index.js +1 -0
- package/lib/esm/utils/logger.d.ts +3 -0
- package/lib/esm/utils/logger.js +14 -0
- package/lib/esm/utils/message-utils.d.ts +1 -1
- package/lib/esm/utils/message-utils.js +1 -4
- package/lib/esm/utils/open-api-parameter.js +1 -1
- package/lib/esm/utils/runnable-type.d.ts +4 -22
- package/lib/esm/utils/structured-output-schema.d.ts +2 -2
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/type-utils.js +1 -0
- package/package.json +1 -1
- package/lib/cjs/definitions/data-type-schema.d.ts +0 -42
- package/lib/cjs/logger.d.ts +0 -2
- package/lib/dts/definitions/data-type-schema.d.ts +0 -42
- package/lib/dts/logger.d.ts +0 -2
- package/lib/esm/definitions/data-type-schema.d.ts +0 -42
- package/lib/esm/logger.d.ts +0 -2
- package/lib/esm/logger.js +0 -2
- /package/lib/cjs/definitions/{data-type-schema.js → data-schema.js} +0 -0
- /package/lib/esm/definitions/{data-type-schema.js → data-schema.js} +0 -0
package/lib/cjs/agent.d.ts
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import type { Context, ContextState } from "./context";
|
|
2
|
-
import type {
|
|
3
|
-
import
|
|
4
|
-
import type { BindAgentInputs, BoundAgent } from "./
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
|
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
|
|
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
|
}
|
package/lib/cjs/agent.js
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
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.Agent = void 0;
|
|
7
|
-
const logger_1 = __importDefault(require("./logger"));
|
|
8
4
|
const runnable_1 = require("./runnable");
|
|
9
5
|
const utils_1 = require("./utils");
|
|
6
|
+
const logger_1 = require("./utils/logger");
|
|
10
7
|
class Agent extends runnable_1.Runnable {
|
|
8
|
+
definition;
|
|
9
|
+
context;
|
|
10
|
+
constructor(definition, context) {
|
|
11
|
+
super(definition, context);
|
|
12
|
+
this.definition = definition;
|
|
13
|
+
this.context = context;
|
|
14
|
+
}
|
|
11
15
|
async getMemoryQuery(input, query) {
|
|
12
16
|
if (query?.from === "variable") {
|
|
13
17
|
const i = utils_1.OrderedRecord.find(this.definition.inputs, (i) => i.id === query.fromVariableId);
|
|
14
18
|
if (!i)
|
|
15
19
|
throw new Error(`Input variable ${query.fromVariableId} not found`);
|
|
16
|
-
const value = input[i.name];
|
|
20
|
+
const value = i.name ? input[i.name] : undefined;
|
|
17
21
|
return (0, utils_1.renderMessage)("{{value}}", { value });
|
|
18
22
|
}
|
|
19
23
|
return Object.entries(input)
|
|
@@ -26,9 +30,13 @@ class Agent extends runnable_1.Runnable {
|
|
|
26
30
|
* @param context The AIGNE context.
|
|
27
31
|
* @returns A dictionary of memories, where the key is the memory id or name and the value is an array of memory items.
|
|
28
32
|
*/
|
|
29
|
-
async loadMemories(input
|
|
30
|
-
const { memories } = this
|
|
31
|
-
|
|
33
|
+
async loadMemories(input) {
|
|
34
|
+
const { definition: { memories }, context, } = this;
|
|
35
|
+
if (!memories?.$indexes.length)
|
|
36
|
+
return {};
|
|
37
|
+
if (!context)
|
|
38
|
+
throw new Error("Context is required");
|
|
39
|
+
const { userId, sessionId } = context.state;
|
|
32
40
|
return Object.fromEntries((await Promise.all(utils_1.OrderedRecord.map(memories, async ({ id, name, memory, query, options }) => {
|
|
33
41
|
if (!name || !memory)
|
|
34
42
|
return null;
|
|
@@ -51,22 +59,55 @@ class Agent extends runnable_1.Runnable {
|
|
|
51
59
|
* @param messages Messages to be added to memories.
|
|
52
60
|
*/
|
|
53
61
|
async updateMemories(messages) {
|
|
54
|
-
const { memories } = this
|
|
55
|
-
|
|
62
|
+
const { context, definition: { memories }, } = this;
|
|
63
|
+
if (!context)
|
|
64
|
+
throw new Error("Context is required");
|
|
65
|
+
const { userId, sessionId } = context.state ?? {};
|
|
56
66
|
await Promise.all(utils_1.OrderedRecord.map(memories, async ({ memory }) => {
|
|
57
67
|
if (!memory) {
|
|
58
|
-
logger_1.
|
|
68
|
+
logger_1.logger.warn(`Memory is not defined in agent ${this.name || this.id}`);
|
|
59
69
|
return;
|
|
60
70
|
}
|
|
61
71
|
await memory.add(messages, { userId, sessionId });
|
|
62
72
|
}));
|
|
63
73
|
}
|
|
74
|
+
async loadPreloads(input) {
|
|
75
|
+
const { context, definition: { preloads }, } = this;
|
|
76
|
+
if (!preloads?.$indexes.length)
|
|
77
|
+
return {};
|
|
78
|
+
if (!context)
|
|
79
|
+
throw new Error("Context is required");
|
|
80
|
+
return Object.fromEntries(await Promise.all(utils_1.OrderedRecord.map(preloads, async (preload) => {
|
|
81
|
+
if (!preload.runnable?.id)
|
|
82
|
+
throw new Error("Runnable id in preload is required");
|
|
83
|
+
const runnable = await context.resolve(preload.runnable.id);
|
|
84
|
+
const runnableInput = Object.fromEntries(utils_1.OrderedRecord.map(runnable.definition.inputs, (i) => {
|
|
85
|
+
if (!i.name)
|
|
86
|
+
return null;
|
|
87
|
+
const bind = preload.input?.[i.id];
|
|
88
|
+
if (!bind)
|
|
89
|
+
return null;
|
|
90
|
+
if (bind.from !== "input")
|
|
91
|
+
throw new Error(`Unsupported bind from ${bind.from}`);
|
|
92
|
+
const from = this.definition.inputs[bind.fromInput];
|
|
93
|
+
if (!from)
|
|
94
|
+
throw new Error(`Input ${bind.fromInput} not found`);
|
|
95
|
+
if (!from.name)
|
|
96
|
+
return null;
|
|
97
|
+
const v = input[from.name];
|
|
98
|
+
return [i.name, v];
|
|
99
|
+
}).filter(utils_1.isNonNullable));
|
|
100
|
+
const result = await runnable.run(runnableInput);
|
|
101
|
+
return [preload.id, result];
|
|
102
|
+
})));
|
|
103
|
+
}
|
|
64
104
|
async run(input, options) {
|
|
65
|
-
logger_1.
|
|
105
|
+
logger_1.logger.debug(`AIGNE core: run agent ${this.name || this.id} with`, {
|
|
66
106
|
input,
|
|
67
107
|
});
|
|
68
|
-
const
|
|
69
|
-
const
|
|
108
|
+
const preloads = await this.loadPreloads(input);
|
|
109
|
+
const memories = await this.loadMemories(input);
|
|
110
|
+
const processResult = await this.process({ ...input, ...preloads, ...memories }, { preloads, memories });
|
|
70
111
|
if (options?.stream) {
|
|
71
112
|
const stream = processResult instanceof ReadableStream ||
|
|
72
113
|
(0, utils_1.isAsyncGenerator)(processResult)
|
|
@@ -74,7 +115,7 @@ class Agent extends runnable_1.Runnable {
|
|
|
74
115
|
: (0, utils_1.objectToRunnableResponseStream)(processResult);
|
|
75
116
|
return (0, utils_1.extractOutputsFromRunnableOutput)(stream, async (result) => {
|
|
76
117
|
// TODO: validate result against outputs schema
|
|
77
|
-
logger_1.
|
|
118
|
+
logger_1.logger.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
78
119
|
result,
|
|
79
120
|
});
|
|
80
121
|
await this.onResult(result);
|
|
@@ -85,7 +126,7 @@ class Agent extends runnable_1.Runnable {
|
|
|
85
126
|
: Symbol.asyncIterator in processResult
|
|
86
127
|
? await (0, utils_1.runnableResponseStreamToObject)(processResult)
|
|
87
128
|
: processResult;
|
|
88
|
-
logger_1.
|
|
129
|
+
logger_1.logger.debug(`AIGNE core: run agent ${this.name || this.id} success`, {
|
|
89
130
|
result,
|
|
90
131
|
});
|
|
91
132
|
// TODO: validate result against outputs schema
|
package/lib/cjs/context.d.ts
CHANGED
|
@@ -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 {
|
|
2
|
+
import type { RunnableInputType } from "../runnable";
|
|
3
3
|
import { OrderedRecord } from "../utils";
|
|
4
|
-
import type {
|
|
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]:
|
|
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<
|
|
51
|
+
export declare function toRunnableMemories<I extends {}>(agentName: string, inputs: OrderedRecord<RunnableInputType>, memories: {
|
|
39
52
|
[name: string]: CreateRunnableMemory<I>;
|
|
40
|
-
}): OrderedRecord<
|
|
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 =
|
|
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,
|
|
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,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.preloadCreatorsToPreloads = preloadCreatorsToPreloads;
|
|
4
|
+
const is_non_nullable_1 = require("../utils/is-non-nullable");
|
|
5
|
+
const ordered_map_1 = require("../utils/ordered-map");
|
|
6
|
+
function preloadCreatorsToPreloads(inputs, creators) {
|
|
7
|
+
return ordered_map_1.OrderedRecord.fromArray(Object.entries(creators ?? {}).map(([name, preload]) => {
|
|
8
|
+
const p = preload((runnable, input, options) => ({
|
|
9
|
+
...options,
|
|
10
|
+
runnable,
|
|
11
|
+
input,
|
|
12
|
+
}));
|
|
13
|
+
const input = Object.fromEntries(ordered_map_1.OrderedRecord.map(p.runnable.definition.inputs, (i) => {
|
|
14
|
+
if (!i.name)
|
|
15
|
+
return null;
|
|
16
|
+
const bind = p.input?.[i.name];
|
|
17
|
+
if (!bind)
|
|
18
|
+
return null;
|
|
19
|
+
if (bind.from !== "input")
|
|
20
|
+
throw new Error(`Unsupported bind from ${bind.from} in preloads`);
|
|
21
|
+
const from = ordered_map_1.OrderedRecord.find(inputs, (i) => i.name === bind.fromInput);
|
|
22
|
+
if (!from)
|
|
23
|
+
throw new Error(`Input ${bind.fromInput} not found`);
|
|
24
|
+
return [i.id, { ...bind, fromInput: from.id }];
|
|
25
|
+
}).filter(is_non_nullable_1.isNonNullable));
|
|
26
|
+
return {
|
|
27
|
+
id: name,
|
|
28
|
+
name,
|
|
29
|
+
description: p.description,
|
|
30
|
+
runnable: { id: p.runnable.id },
|
|
31
|
+
input,
|
|
32
|
+
};
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
@@ -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
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
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,
|
|
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,
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
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 {};
|
|
@@ -17,8 +17,9 @@ const nanoid_1 = require("nanoid");
|
|
|
17
17
|
const tsyringe_1 = require("tsyringe");
|
|
18
18
|
const agent_1 = require("./agent");
|
|
19
19
|
const constants_1 = require("./constants");
|
|
20
|
-
const
|
|
20
|
+
const data_schema_1 = require("./definitions/data-schema");
|
|
21
21
|
const memory_1 = require("./definitions/memory");
|
|
22
|
+
const preload_1 = require("./definitions/preload");
|
|
22
23
|
let FunctionAgent = class FunctionAgent extends agent_1.Agent {
|
|
23
24
|
definition;
|
|
24
25
|
static create = create;
|
|
@@ -32,7 +33,7 @@ let FunctionAgent = class FunctionAgent extends agent_1.Agent {
|
|
|
32
33
|
throw new Error("Function is required");
|
|
33
34
|
if (!context)
|
|
34
35
|
throw new Error("Context is required");
|
|
35
|
-
return await func(input, {
|
|
36
|
+
return await func(input, { ...options, context });
|
|
36
37
|
}
|
|
37
38
|
};
|
|
38
39
|
exports.FunctionAgent = FunctionAgent;
|
|
@@ -42,10 +43,11 @@ exports.FunctionAgent = FunctionAgent = __decorate([
|
|
|
42
43
|
__param(1, (0, tsyringe_1.inject)(constants_1.TYPES.context)),
|
|
43
44
|
__metadata("design:paramtypes", [Object, Object])
|
|
44
45
|
], FunctionAgent);
|
|
45
|
-
function create(
|
|
46
|
+
function create(options) {
|
|
46
47
|
const agentId = options.name || (0, nanoid_1.nanoid)();
|
|
47
|
-
const inputs = (0,
|
|
48
|
-
const outputs = (0,
|
|
48
|
+
const inputs = (0, data_schema_1.schemaToDataType)(options.inputs);
|
|
49
|
+
const outputs = (0, data_schema_1.schemaToDataType)(options.outputs);
|
|
50
|
+
const preloads = (0, preload_1.preloadCreatorsToPreloads)(inputs, options.preloads);
|
|
49
51
|
const memories = (0, memory_1.toRunnableMemories)(agentId, inputs, options.memories || {});
|
|
50
52
|
return new FunctionAgent({
|
|
51
53
|
id: agentId,
|
|
@@ -53,7 +55,8 @@ function create({ context, ...options }) {
|
|
|
53
55
|
type: "function_agent",
|
|
54
56
|
inputs,
|
|
55
57
|
outputs,
|
|
58
|
+
preloads,
|
|
56
59
|
memories,
|
|
57
60
|
function: options.function,
|
|
58
|
-
}, context);
|
|
61
|
+
}, options.context);
|
|
59
62
|
}
|
package/lib/cjs/index.d.ts
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-
|
|
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/cjs/index.js
CHANGED
|
@@ -18,7 +18,7 @@ __exportStar(require("./agent"), exports);
|
|
|
18
18
|
__exportStar(require("./constants"), exports);
|
|
19
19
|
__exportStar(require("./context"), exports);
|
|
20
20
|
__exportStar(require("./definitions/data-type"), exports);
|
|
21
|
-
__exportStar(require("./definitions/data-
|
|
21
|
+
__exportStar(require("./definitions/data-schema"), exports);
|
|
22
22
|
__exportStar(require("./definitions/memory"), exports);
|
|
23
23
|
__exportStar(require("./definitions/open-api"), exports);
|
|
24
24
|
__exportStar(require("./function-agent"), exports);
|