@aigne/core 1.18.6 → 1.20.1
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/CHANGELOG.md +28 -0
- package/lib/cjs/agents/agent.d.ts +31 -8
- package/lib/cjs/agents/agent.js +31 -22
- package/lib/cjs/agents/ai-agent.d.ts +18 -6
- package/lib/cjs/agents/ai-agent.js +23 -12
- package/lib/cjs/agents/guide-rail-agent.d.ts +1 -1
- package/lib/cjs/agents/mcp-agent.d.ts +1 -1
- package/lib/cjs/agents/team-agent.js +1 -1
- package/lib/cjs/aigne/aigne.d.ts +10 -10
- package/lib/cjs/aigne/context.d.ts +11 -6
- package/lib/cjs/aigne/context.js +73 -48
- package/lib/cjs/aigne/message-queue.d.ts +1 -1
- package/lib/cjs/aigne/message-queue.js +2 -3
- package/lib/cjs/aigne/usage.d.ts +1 -0
- package/lib/cjs/aigne/usage.js +1 -0
- package/lib/cjs/loader/agent-yaml.d.ts +2 -1
- package/lib/cjs/loader/agent-yaml.js +4 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +2 -16
- package/lib/cjs/prompt/prompt-builder.js +12 -25
- package/lib/cjs/utils/event-stream.d.ts +5 -1
- package/lib/cjs/utils/event-stream.js +88 -23
- package/lib/cjs/utils/stream-utils.d.ts +9 -7
- package/lib/cjs/utils/stream-utils.js +48 -15
- package/lib/cjs/utils/type-utils.d.ts +2 -0
- package/lib/cjs/utils/type-utils.js +18 -0
- package/lib/dts/agents/agent.d.ts +31 -8
- package/lib/dts/agents/ai-agent.d.ts +18 -6
- package/lib/dts/agents/guide-rail-agent.d.ts +1 -1
- package/lib/dts/agents/mcp-agent.d.ts +1 -1
- package/lib/dts/aigne/aigne.d.ts +10 -10
- package/lib/dts/aigne/context.d.ts +11 -6
- package/lib/dts/aigne/message-queue.d.ts +1 -1
- package/lib/dts/aigne/usage.d.ts +1 -0
- package/lib/dts/loader/agent-yaml.d.ts +2 -1
- package/lib/dts/prompt/prompt-builder.d.ts +2 -16
- package/lib/dts/utils/event-stream.d.ts +5 -1
- package/lib/dts/utils/stream-utils.d.ts +9 -7
- package/lib/dts/utils/type-utils.d.ts +2 -0
- package/lib/esm/agents/agent.d.ts +31 -8
- package/lib/esm/agents/agent.js +29 -22
- package/lib/esm/agents/ai-agent.d.ts +18 -6
- package/lib/esm/agents/ai-agent.js +25 -14
- package/lib/esm/agents/guide-rail-agent.d.ts +1 -1
- package/lib/esm/agents/mcp-agent.d.ts +1 -1
- package/lib/esm/agents/team-agent.js +2 -2
- package/lib/esm/aigne/aigne.d.ts +10 -10
- package/lib/esm/aigne/context.d.ts +11 -6
- package/lib/esm/aigne/context.js +76 -51
- package/lib/esm/aigne/message-queue.d.ts +1 -1
- package/lib/esm/aigne/message-queue.js +2 -3
- package/lib/esm/aigne/usage.d.ts +1 -0
- package/lib/esm/aigne/usage.js +1 -0
- package/lib/esm/loader/agent-yaml.d.ts +2 -1
- package/lib/esm/loader/agent-yaml.js +4 -0
- package/lib/esm/prompt/prompt-builder.d.ts +2 -16
- package/lib/esm/prompt/prompt-builder.js +12 -23
- package/lib/esm/utils/event-stream.d.ts +5 -1
- package/lib/esm/utils/event-stream.js +86 -22
- package/lib/esm/utils/stream-utils.d.ts +9 -7
- package/lib/esm/utils/stream-utils.js +48 -16
- package/lib/esm/utils/type-utils.d.ts +2 -0
- package/lib/esm/utils/type-utils.js +16 -0
- package/package.json +2 -2
|
@@ -15,6 +15,7 @@ exports.readableStreamToArray = readableStreamToArray;
|
|
|
15
15
|
exports.stringToAgentResponseStream = stringToAgentResponseStream;
|
|
16
16
|
exports.toReadableStream = toReadableStream;
|
|
17
17
|
exports.readAllString = readAllString;
|
|
18
|
+
exports.mergeReadableStreams = mergeReadableStreams;
|
|
18
19
|
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
19
20
|
const agent_js_1 = require("../agents/agent.js");
|
|
20
21
|
const type_utils_js_1 = require("./type-utils.js");
|
|
@@ -28,16 +29,18 @@ function objectToAgentResponseStream(json) {
|
|
|
28
29
|
});
|
|
29
30
|
}
|
|
30
31
|
function mergeAgentResponseChunk(output, chunk) {
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
if ((0, agent_js_1.isAgentResponseDelta)(chunk)) {
|
|
33
|
+
if (chunk.delta.text) {
|
|
34
|
+
for (const [key, text] of Object.entries(chunk.delta.text)) {
|
|
35
|
+
const original = output[key];
|
|
36
|
+
const t = (original || "") + (text || "");
|
|
37
|
+
if (t)
|
|
38
|
+
Object.assign(output, { [key]: t });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (chunk.delta.json) {
|
|
42
|
+
Object.assign(output, (0, type_utils_js_1.omitBy)(chunk.delta.json, (v) => v === undefined));
|
|
37
43
|
}
|
|
38
|
-
}
|
|
39
|
-
if (chunk.delta.json) {
|
|
40
|
-
Object.assign(output, (0, type_utils_js_1.omitBy)(chunk.delta.json, (v) => v === undefined));
|
|
41
44
|
}
|
|
42
45
|
return output;
|
|
43
46
|
}
|
|
@@ -88,7 +91,7 @@ function asyncGeneratorToReadableStream(generator) {
|
|
|
88
91
|
},
|
|
89
92
|
});
|
|
90
93
|
}
|
|
91
|
-
function onAgentResponseStreamEnd(stream,
|
|
94
|
+
function onAgentResponseStreamEnd(stream, options) {
|
|
92
95
|
const json = {};
|
|
93
96
|
const reader = stream.getReader();
|
|
94
97
|
return new ReadableStream({
|
|
@@ -97,18 +100,17 @@ function onAgentResponseStreamEnd(stream, callback, options) {
|
|
|
97
100
|
while (true) {
|
|
98
101
|
const { value, done } = await reader.read();
|
|
99
102
|
if (done) {
|
|
100
|
-
const result = await
|
|
103
|
+
const result = (await options?.onResult?.(json)) ?? json;
|
|
101
104
|
if (result && !(0, fast_deep_equal_1.default)(result, json)) {
|
|
102
105
|
let chunk = { delta: { json: result } };
|
|
103
|
-
|
|
104
|
-
chunk = options.processChunk(chunk);
|
|
106
|
+
chunk = (await options?.onChunk?.(chunk)) ?? chunk;
|
|
105
107
|
controller.enqueue(chunk);
|
|
106
108
|
}
|
|
107
109
|
controller.close();
|
|
108
110
|
return;
|
|
109
111
|
}
|
|
110
112
|
mergeAgentResponseChunk(json, value);
|
|
111
|
-
const chunk = options?.
|
|
113
|
+
const chunk = (await options?.onChunk?.(value)) ?? value;
|
|
112
114
|
if (!(0, agent_js_1.isEmptyChunk)(chunk)) {
|
|
113
115
|
controller.enqueue(chunk);
|
|
114
116
|
break;
|
|
@@ -116,7 +118,7 @@ function onAgentResponseStreamEnd(stream, callback, options) {
|
|
|
116
118
|
}
|
|
117
119
|
}
|
|
118
120
|
catch (error) {
|
|
119
|
-
controller.error((await options?.
|
|
121
|
+
controller.error((await options?.onError?.(error)) ?? error);
|
|
120
122
|
}
|
|
121
123
|
},
|
|
122
124
|
});
|
|
@@ -197,3 +199,34 @@ function toReadableStream(stream) {
|
|
|
197
199
|
async function readAllString(stream) {
|
|
198
200
|
return (await readableStreamToArray((stream instanceof ReadableStream ? stream : toReadableStream(stream)).pipeThrough(new TextDecoderStream()))).join("");
|
|
199
201
|
}
|
|
202
|
+
function mergeReadableStreams(...streams) {
|
|
203
|
+
let readers;
|
|
204
|
+
return new ReadableStream({
|
|
205
|
+
async pull(controller) {
|
|
206
|
+
try {
|
|
207
|
+
readers ??= streams.map((s) => ({ reader: s.getReader(), data: [] }));
|
|
208
|
+
while (readers.length) {
|
|
209
|
+
const chunk = await Promise.race(readers.map((i) => {
|
|
210
|
+
i.reading ??= i.reader.read().then((result) => ({ result, item: i }));
|
|
211
|
+
return i.reading;
|
|
212
|
+
}));
|
|
213
|
+
if (chunk.result.value) {
|
|
214
|
+
controller.enqueue(chunk.result.value);
|
|
215
|
+
chunk.item.reading = undefined;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (chunk.result.done) {
|
|
219
|
+
readers = readers.filter((i) => i !== chunk.item);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
controller.close();
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
controller.error(error);
|
|
226
|
+
if (readers)
|
|
227
|
+
for (const item of readers)
|
|
228
|
+
item.reader.releaseLock();
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
}
|
|
@@ -15,6 +15,8 @@ export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
|
|
|
15
15
|
export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
|
|
16
16
|
export declare function remove<T>(arr: T[], remove: T[] | ((item: T) => boolean)): T[];
|
|
17
17
|
export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
|
|
18
|
+
export declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
|
|
19
|
+
export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
|
|
18
20
|
export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
|
|
19
21
|
export declare function orArrayToArray<T>(value?: T | T[]): T[];
|
|
20
22
|
export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
|
|
@@ -8,6 +8,8 @@ exports.isNotEmpty = isNotEmpty;
|
|
|
8
8
|
exports.duplicates = duplicates;
|
|
9
9
|
exports.remove = remove;
|
|
10
10
|
exports.unique = unique;
|
|
11
|
+
exports.omit = omit;
|
|
12
|
+
exports.omitDeep = omitDeep;
|
|
11
13
|
exports.omitBy = omitBy;
|
|
12
14
|
exports.orArrayToArray = orArrayToArray;
|
|
13
15
|
exports.createAccessorArray = createAccessorArray;
|
|
@@ -73,6 +75,22 @@ function unique(arr, key = (item) => item) {
|
|
|
73
75
|
return true;
|
|
74
76
|
});
|
|
75
77
|
}
|
|
78
|
+
function omit(obj, ...keys) {
|
|
79
|
+
const flattenedKeys = new Set(keys.flat());
|
|
80
|
+
return Object.fromEntries(Object.entries(obj).filter(([key]) => !flattenedKeys.has(key)));
|
|
81
|
+
}
|
|
82
|
+
function omitDeep(obj, ...keys) {
|
|
83
|
+
if (Array.isArray(obj)) {
|
|
84
|
+
return obj.map((item) => omitDeep(item, ...keys));
|
|
85
|
+
}
|
|
86
|
+
if (isRecord(obj)) {
|
|
87
|
+
const flattenedKeys = new Set(keys.flat());
|
|
88
|
+
return Object.fromEntries(Object.entries(obj)
|
|
89
|
+
.filter(([key]) => !flattenedKeys.has(key))
|
|
90
|
+
.map(([key, value]) => [key, omitDeep(value, ...keys)]));
|
|
91
|
+
}
|
|
92
|
+
return obj;
|
|
93
|
+
}
|
|
76
94
|
function omitBy(obj, predicate) {
|
|
77
95
|
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => {
|
|
78
96
|
const k = key;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
|
|
2
2
|
import { ZodObject, type ZodType } from "zod";
|
|
3
|
-
import type { Context, UserContext } from "../aigne/context.js";
|
|
3
|
+
import type { AgentEvent, Context, UserContext } from "../aigne/context.js";
|
|
4
4
|
import type { MessagePayload } from "../aigne/message-queue.js";
|
|
5
|
+
import type { ContextUsage } from "../aigne/usage.js";
|
|
5
6
|
import type { Memory, MemoryAgent } from "../memory/memory.js";
|
|
6
7
|
import type { MemoryRecorderInput } from "../memory/recorder.js";
|
|
7
8
|
import type { MemoryRetrieverInput } from "../memory/retriever.js";
|
|
@@ -12,7 +13,11 @@ export * from "./types.js";
|
|
|
12
13
|
/**
|
|
13
14
|
* Basic message type that can contain any key-value pairs
|
|
14
15
|
*/
|
|
15
|
-
export
|
|
16
|
+
export interface Message extends Record<string, unknown> {
|
|
17
|
+
$meta?: {
|
|
18
|
+
usage: ContextUsage;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
16
21
|
/**
|
|
17
22
|
* Topics the agent subscribes to, can be a single topic string or an array of topic strings
|
|
18
23
|
*/
|
|
@@ -328,7 +333,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
328
333
|
* Regular mode waits for the agent to complete processing and return the final result,
|
|
329
334
|
* suitable for scenarios where a complete result is needed at once.
|
|
330
335
|
*
|
|
331
|
-
* @param input Input message to the agent
|
|
336
|
+
* @param input Input message to the agent
|
|
332
337
|
* @param options Invocation options, must set streaming to false or leave unset
|
|
333
338
|
* @returns Final JSON response
|
|
334
339
|
*
|
|
@@ -336,7 +341,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
336
341
|
* Here's an example of invoking an agent with regular mode:
|
|
337
342
|
* {@includeCode ../../test/agents/agent.test.ts#example-invoke}
|
|
338
343
|
*/
|
|
339
|
-
invoke(input: I
|
|
344
|
+
invoke(input: I, options?: Partial<AgentInvokeOptions> & {
|
|
340
345
|
streaming?: false;
|
|
341
346
|
}): Promise<O>;
|
|
342
347
|
/**
|
|
@@ -346,7 +351,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
346
351
|
* suitable for scenarios requiring real-time progress updates, such as
|
|
347
352
|
* chat bot typing effects.
|
|
348
353
|
*
|
|
349
|
-
* @param input Input message to the agent
|
|
354
|
+
* @param input Input message to the agent
|
|
350
355
|
* @param options Invocation options, must set streaming to true for this overload
|
|
351
356
|
* @returns Streaming response object
|
|
352
357
|
*
|
|
@@ -354,7 +359,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
354
359
|
* Here's an example of invoking an agent with streaming response:
|
|
355
360
|
* {@includeCode ../../test/agents/agent.test.ts#example-invoke-streaming}
|
|
356
361
|
*/
|
|
357
|
-
invoke(input: I
|
|
362
|
+
invoke(input: I, options: Partial<AgentInvokeOptions> & {
|
|
358
363
|
streaming: true;
|
|
359
364
|
}): Promise<AgentResponseStream<O>>;
|
|
360
365
|
/**
|
|
@@ -366,7 +371,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
366
371
|
* @param options Invocation options
|
|
367
372
|
* @returns Agent response (streaming or regular)
|
|
368
373
|
*/
|
|
369
|
-
invoke(input: I
|
|
374
|
+
invoke(input: I, options?: Partial<AgentInvokeOptions>): Promise<AgentResponse<O>>;
|
|
370
375
|
protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I, options: AgentInvokeOptions): Promise<O>;
|
|
371
376
|
/**
|
|
372
377
|
* Process agent output
|
|
@@ -594,7 +599,7 @@ export type AgentResponseStream<T> = ReadableStream<AgentResponseChunk<T>>;
|
|
|
594
599
|
*
|
|
595
600
|
* @template T Response data type
|
|
596
601
|
*/
|
|
597
|
-
export type AgentResponseChunk<T> = AgentResponseDelta<T
|
|
602
|
+
export type AgentResponseChunk<T> = AgentResponseDelta<T> | AgentResponseProgress;
|
|
598
603
|
/**
|
|
599
604
|
* Check if a response chunk is empty
|
|
600
605
|
*
|
|
@@ -622,6 +627,24 @@ export interface AgentResponseDelta<T> {
|
|
|
622
627
|
json?: Partial<T> | TransferAgentOutput;
|
|
623
628
|
};
|
|
624
629
|
}
|
|
630
|
+
export declare function isAgentResponseDelta<T>(chunk: AgentResponseChunk<T>): chunk is AgentResponseDelta<T>;
|
|
631
|
+
export interface AgentResponseProgress {
|
|
632
|
+
progress: ({
|
|
633
|
+
event: "agentStarted";
|
|
634
|
+
input: Message;
|
|
635
|
+
} | {
|
|
636
|
+
event: "agentSucceed";
|
|
637
|
+
output: Message;
|
|
638
|
+
} | {
|
|
639
|
+
event: "agentFailed";
|
|
640
|
+
error: Error;
|
|
641
|
+
}) & Omit<AgentEvent, "agent"> & {
|
|
642
|
+
agent: {
|
|
643
|
+
name: string;
|
|
644
|
+
};
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
export declare function isAgentResponseProgress<T>(chunk: AgentResponseChunk<T>): chunk is AgentResponseProgress;
|
|
625
648
|
/**
|
|
626
649
|
* Creates a text delta for streaming responses
|
|
627
650
|
*
|
|
@@ -12,7 +12,7 @@ import type { GuideRailAgentOutput } from "./guide-rail-agent.js";
|
|
|
12
12
|
* @template I The input message type the agent accepts
|
|
13
13
|
* @template O The output message type the agent returns
|
|
14
14
|
*/
|
|
15
|
-
export interface AIAgentOptions<I extends Message = Message
|
|
15
|
+
export interface AIAgentOptions<InputKey extends string = string, I extends Message & InputMessage<InputKey> = Message & InputMessage<InputKey>, O extends Message = Message> extends AgentOptions<Omit<I, InputKey> & Partial<InputMessage<InputKey>>, O> {
|
|
16
16
|
/**
|
|
17
17
|
* The language model to use for this agent
|
|
18
18
|
*
|
|
@@ -26,10 +26,14 @@ export interface AIAgentOptions<I extends Message = Message, O extends Message =
|
|
|
26
26
|
* more complex prompt templates
|
|
27
27
|
*/
|
|
28
28
|
instructions?: string | PromptBuilder;
|
|
29
|
+
/**
|
|
30
|
+
* Pick a message from input to use as the user's message
|
|
31
|
+
*/
|
|
32
|
+
inputKey?: InputKey;
|
|
29
33
|
/**
|
|
30
34
|
* Custom key to use for text output in the response
|
|
31
35
|
*
|
|
32
|
-
* Defaults to
|
|
36
|
+
* Defaults to `message` if not specified
|
|
33
37
|
*/
|
|
34
38
|
outputKey?: string;
|
|
35
39
|
/**
|
|
@@ -106,6 +110,9 @@ export declare const aiAgentToolChoiceSchema: z.ZodUnion<[z.ZodNativeEnum<typeof
|
|
|
106
110
|
export declare const aiAgentOptionsSchema: ZodObject<{
|
|
107
111
|
[key in keyof AIAgentOptions]: ZodType<AIAgentOptions[key]>;
|
|
108
112
|
}>;
|
|
113
|
+
type InputMessage<K> = K extends string ? {
|
|
114
|
+
[key in K]: string;
|
|
115
|
+
} : Message;
|
|
109
116
|
/**
|
|
110
117
|
* AI-powered agent that leverages language models
|
|
111
118
|
*
|
|
@@ -126,7 +133,7 @@ export declare const aiAgentOptionsSchema: ZodObject<{
|
|
|
126
133
|
* Basic AIAgent creation:
|
|
127
134
|
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-basic}
|
|
128
135
|
*/
|
|
129
|
-
export declare class AIAgent<I extends Message = Message
|
|
136
|
+
export declare class AIAgent<InputKey extends string = string, I extends Message & InputMessage<InputKey> = Message & InputMessage<InputKey>, O extends Message = Message> extends Agent<I, O> {
|
|
130
137
|
/**
|
|
131
138
|
* Create an AIAgent with the specified options
|
|
132
139
|
*
|
|
@@ -139,13 +146,13 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
139
146
|
* AI agent with custom instructions:
|
|
140
147
|
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-instructions}
|
|
141
148
|
*/
|
|
142
|
-
static from<I extends Message
|
|
149
|
+
static from<InputKey extends string, I extends Message & InputMessage<InputKey>, O extends Message>(options: AIAgentOptions<InputKey, I, O>): AIAgent<InputKey, I, O>;
|
|
143
150
|
/**
|
|
144
151
|
* Create an AIAgent instance
|
|
145
152
|
*
|
|
146
153
|
* @param options Configuration options for the AI agent
|
|
147
154
|
*/
|
|
148
|
-
constructor(options: AIAgentOptions<I, O>);
|
|
155
|
+
constructor(options: AIAgentOptions<InputKey, I, O>);
|
|
149
156
|
/**
|
|
150
157
|
* The language model used by this agent
|
|
151
158
|
*
|
|
@@ -163,6 +170,10 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
163
170
|
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-prompt-builder}
|
|
164
171
|
*/
|
|
165
172
|
instructions: PromptBuilder;
|
|
173
|
+
/**
|
|
174
|
+
* Pick a message from input to use as the user's message
|
|
175
|
+
*/
|
|
176
|
+
inputKey?: InputKey;
|
|
166
177
|
/**
|
|
167
178
|
* Custom key to use for text output in the response
|
|
168
179
|
*
|
|
@@ -170,7 +181,7 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
170
181
|
* Setting a custom output key:
|
|
171
182
|
* {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-custom-output-key}
|
|
172
183
|
*/
|
|
173
|
-
outputKey
|
|
184
|
+
outputKey: string;
|
|
174
185
|
/**
|
|
175
186
|
* Controls how the agent uses tools during execution
|
|
176
187
|
*
|
|
@@ -224,3 +235,4 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
224
235
|
*/
|
|
225
236
|
_processRouter(input: I, model: ChatModel, modelInput: ChatModelInput, options: AgentInvokeOptions, toolsMap: Map<string, Agent>): AgentProcessAsyncGenerator<O>;
|
|
226
237
|
}
|
|
238
|
+
export {};
|
|
@@ -59,4 +59,4 @@ export interface GuideRailAgentOutput extends Message {
|
|
|
59
59
|
* - Monitor and audit agent behavior
|
|
60
60
|
*/
|
|
61
61
|
export type GuideRailAgent = Agent<GuideRailAgentInput, GuideRailAgentOutput>;
|
|
62
|
-
export declare const guideRailAgentOptions: AgentOptions<
|
|
62
|
+
export declare const guideRailAgentOptions: AgentOptions<any, GuideRailAgentOutput>;
|
|
@@ -192,7 +192,7 @@ export declare abstract class MCPBase<I extends Message, O extends Message> exte
|
|
|
192
192
|
export declare class MCPTool extends MCPBase<Message, CallToolResult> {
|
|
193
193
|
process(input: Message): Promise<CallToolResult>;
|
|
194
194
|
}
|
|
195
|
-
export interface MCPPromptInput extends
|
|
195
|
+
export interface MCPPromptInput extends Record<string, unknown> {
|
|
196
196
|
[key: string]: string;
|
|
197
197
|
}
|
|
198
198
|
export declare class MCPPrompt extends MCPBase<MCPPromptInput, GetPromptResult> {
|
package/lib/dts/aigne/aigne.d.ts
CHANGED
|
@@ -129,12 +129,12 @@ export declare class AIGNE<U extends UserContext = UserContext> {
|
|
|
129
129
|
* This overload is useful when you need to track which agent was ultimately responsible for generating the response.
|
|
130
130
|
*
|
|
131
131
|
* @param agent - Target agent to invoke
|
|
132
|
-
* @param message - Input message to send to the agent
|
|
132
|
+
* @param message - Input message to send to the agent
|
|
133
133
|
* @param options.returnActiveAgent - Must be true to return the final active agent
|
|
134
134
|
* @param options.streaming - Must be false to return a response stream
|
|
135
135
|
* @returns A promise resolving to a tuple containing the agent's response and the final active agent
|
|
136
136
|
*/
|
|
137
|
-
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I
|
|
137
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions<U> & {
|
|
138
138
|
returnActiveAgent: true;
|
|
139
139
|
streaming?: false;
|
|
140
140
|
}): Promise<[O, Agent]>;
|
|
@@ -143,12 +143,12 @@ export declare class AIGNE<U extends UserContext = UserContext> {
|
|
|
143
143
|
* This overload is useful when you need streaming responses while also tracking which agent provided them.
|
|
144
144
|
*
|
|
145
145
|
* @param agent - Target agent to invoke
|
|
146
|
-
* @param message - Input message to send to the agent
|
|
146
|
+
* @param message - Input message to send to the agent
|
|
147
147
|
* @param options.returnActiveAgent - Must be true to return the final active agent
|
|
148
148
|
* @param options.streaming - Must be true to return a response stream
|
|
149
149
|
* @returns A promise resolving to a tuple containing the agent's response stream and a promise for the final agent
|
|
150
150
|
*/
|
|
151
|
-
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I
|
|
151
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions<U> & {
|
|
152
152
|
returnActiveAgent: true;
|
|
153
153
|
streaming: true;
|
|
154
154
|
}): Promise<[AgentResponseStream<O>, Promise<Agent>]>;
|
|
@@ -157,7 +157,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
|
|
|
157
157
|
* This is the standard way to invoke an agent when you only need the response.
|
|
158
158
|
*
|
|
159
159
|
* @param agent - Target agent to invoke
|
|
160
|
-
* @param message - Input message to send to the agent
|
|
160
|
+
* @param message - Input message to send to the agent
|
|
161
161
|
* @param options - Optional configuration parameters for the invocation
|
|
162
162
|
* @returns A promise resolving to the agent's complete response
|
|
163
163
|
*
|
|
@@ -165,7 +165,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
|
|
|
165
165
|
* Here's a simple example of how to invoke an agent:
|
|
166
166
|
* {@includeCode ../../test/aigne/aigne.test.ts#example-simple}
|
|
167
167
|
*/
|
|
168
|
-
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I
|
|
168
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options?: InvokeOptions<U> & {
|
|
169
169
|
returnActiveAgent?: false;
|
|
170
170
|
streaming?: false;
|
|
171
171
|
}): Promise<O>;
|
|
@@ -174,7 +174,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
|
|
|
174
174
|
* This allows processing the response incrementally as it's being generated.
|
|
175
175
|
*
|
|
176
176
|
* @param agent - Target agent to invoke
|
|
177
|
-
* @param message - Input message to send to the agent
|
|
177
|
+
* @param message - Input message to send to the agent
|
|
178
178
|
* @param options - Configuration with streaming enabled to receive incremental response chunks
|
|
179
179
|
* @returns A promise resolving to a stream of the agent's response that can be consumed incrementally
|
|
180
180
|
*
|
|
@@ -182,7 +182,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
|
|
|
182
182
|
* Here's an example of how to invoke an agent with streaming response:
|
|
183
183
|
* {@includeCode ../../test/aigne/aigne.test.ts#example-streaming}
|
|
184
184
|
*/
|
|
185
|
-
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I
|
|
185
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions<U> & {
|
|
186
186
|
returnActiveAgent?: false;
|
|
187
187
|
streaming: true;
|
|
188
188
|
}): Promise<AgentResponseStream<O>>;
|
|
@@ -196,7 +196,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
|
|
|
196
196
|
* @returns Either a UserAgent (when no message provided) or a promise resolving to the agent's response
|
|
197
197
|
* with optional active agent information based on the provided options
|
|
198
198
|
*/
|
|
199
|
-
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I
|
|
199
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I, options?: InvokeOptions<U>): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
|
|
200
200
|
/**
|
|
201
201
|
* Publishes a message to the message queue for inter-agent communication.
|
|
202
202
|
* This method broadcasts a message to all subscribers of the specified topic(s).
|
|
@@ -210,7 +210,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
|
|
|
210
210
|
* Here's an example of how to publish a message:
|
|
211
211
|
* {@includeCode ../../test/aigne/aigne.test.ts#example-publish-message}
|
|
212
212
|
*/
|
|
213
|
-
publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message
|
|
213
|
+
publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message, options?: InvokeOptions<U>): void;
|
|
214
214
|
/**
|
|
215
215
|
* Subscribes to receive the next message on a specific topic.
|
|
216
216
|
* This overload returns a Promise that resolves with the next message published to the topic.
|
|
@@ -41,6 +41,8 @@ export type ContextEmitEventMap = {
|
|
|
41
41
|
*/
|
|
42
42
|
export interface InvokeOptions<U extends UserContext = UserContext> extends Partial<Omit<AgentInvokeOptions<U>, "context">> {
|
|
43
43
|
returnActiveAgent?: boolean;
|
|
44
|
+
returnProgressChunks?: boolean;
|
|
45
|
+
returnMetadata?: boolean;
|
|
44
46
|
disableTransfer?: boolean;
|
|
45
47
|
sourceAgent?: Agent;
|
|
46
48
|
}
|
|
@@ -53,6 +55,8 @@ export interface UserContext extends Record<string, unknown> {
|
|
|
53
55
|
* @hidden
|
|
54
56
|
*/
|
|
55
57
|
export interface Context<U extends UserContext = UserContext> extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
|
|
58
|
+
id: string;
|
|
59
|
+
parentId?: string;
|
|
56
60
|
model?: ChatModel;
|
|
57
61
|
skills?: Agent[];
|
|
58
62
|
usage: ContextUsage;
|
|
@@ -74,11 +78,11 @@ export interface Context<U extends UserContext = UserContext> extends TypedEvent
|
|
|
74
78
|
* @param options.streaming return a stream of the output
|
|
75
79
|
* @returns the output of the agent and the final active agent
|
|
76
80
|
*/
|
|
77
|
-
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I
|
|
81
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions & {
|
|
78
82
|
returnActiveAgent: true;
|
|
79
83
|
streaming?: false;
|
|
80
84
|
}): Promise<[O, Agent]>;
|
|
81
|
-
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I
|
|
85
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions & {
|
|
82
86
|
returnActiveAgent: true;
|
|
83
87
|
streaming: true;
|
|
84
88
|
}): Promise<[AgentResponseStream<O>, Promise<Agent>]>;
|
|
@@ -88,19 +92,19 @@ export interface Context<U extends UserContext = UserContext> extends TypedEvent
|
|
|
88
92
|
* @param message Message to pass to the agent
|
|
89
93
|
* @returns the output of the agent
|
|
90
94
|
*/
|
|
91
|
-
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I
|
|
95
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options?: InvokeOptions & {
|
|
92
96
|
streaming?: false;
|
|
93
97
|
}): Promise<O>;
|
|
94
|
-
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I
|
|
98
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I, options: InvokeOptions & {
|
|
95
99
|
streaming: true;
|
|
96
100
|
}): Promise<AgentResponseStream<O>>;
|
|
97
|
-
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I
|
|
101
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I, options?: InvokeOptions): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
|
|
98
102
|
/**
|
|
99
103
|
* Publish a message to a topic, the aigne will invoke the listeners of the topic
|
|
100
104
|
* @param topic topic name, or an array of topic names
|
|
101
105
|
* @param payload message to publish
|
|
102
106
|
*/
|
|
103
|
-
publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message
|
|
107
|
+
publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message, options?: InvokeOptions): void;
|
|
104
108
|
subscribe(topic: string | string[], listener?: undefined): Promise<MessagePayload>;
|
|
105
109
|
subscribe(topic: string | string[], listener: MessageQueueListener): Unsubscribe;
|
|
106
110
|
subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
@@ -139,6 +143,7 @@ export declare class AIGNEContext implements Context {
|
|
|
139
143
|
reset?: boolean;
|
|
140
144
|
}): AIGNEContext;
|
|
141
145
|
invoke: Context["invoke"];
|
|
146
|
+
private onInvocationResult;
|
|
142
147
|
publish: Context["publish"];
|
|
143
148
|
subscribe: Context["subscribe"];
|
|
144
149
|
unsubscribe: Context["unsubscribe"];
|
|
@@ -21,7 +21,7 @@ export interface MessagePayload {
|
|
|
21
21
|
/**
|
|
22
22
|
* @hidden
|
|
23
23
|
*/
|
|
24
|
-
export declare function toMessagePayload(payload: Omit<MessagePayload, "context"> |
|
|
24
|
+
export declare function toMessagePayload(payload: Omit<MessagePayload, "context"> | Message, options?: Partial<Pick<MessagePayload, "role" | "source">>): Omit<MessagePayload, "context">;
|
|
25
25
|
/**
|
|
26
26
|
* @hidden
|
|
27
27
|
*/
|
package/lib/dts/aigne/usage.d.ts
CHANGED
|
@@ -5,11 +5,11 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
|
5
5
|
name: string;
|
|
6
6
|
description?: string | undefined;
|
|
7
7
|
skills?: string[] | undefined;
|
|
8
|
-
instructions?: string | undefined;
|
|
9
8
|
memory?: true | {
|
|
10
9
|
provider: string;
|
|
11
10
|
subscribeTopic?: string[] | undefined;
|
|
12
11
|
} | undefined;
|
|
12
|
+
instructions?: string | undefined;
|
|
13
13
|
inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
14
14
|
[x: string]: any;
|
|
15
15
|
}, {
|
|
@@ -20,6 +20,7 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
|
20
20
|
}, {
|
|
21
21
|
[x: string]: any;
|
|
22
22
|
}> | undefined;
|
|
23
|
+
inputKey?: string | undefined;
|
|
23
24
|
outputKey?: string | undefined;
|
|
24
25
|
toolChoice?: AIAgentToolChoice | undefined;
|
|
25
26
|
} | {
|
|
@@ -3,33 +3,19 @@ import { Agent, type AgentInvokeOptions, type Message } from "../agents/agent.js
|
|
|
3
3
|
import type { AIAgent } from "../agents/ai-agent.js";
|
|
4
4
|
import type { ChatModel, ChatModelInput } from "../agents/chat-model.js";
|
|
5
5
|
import { ChatMessagesTemplate } from "./template.js";
|
|
6
|
-
export declare const MESSAGE_KEY = "$message";
|
|
7
|
-
export declare function createMessage<V extends Message>(message: string, variables?: V): {
|
|
8
|
-
[MESSAGE_KEY]: string;
|
|
9
|
-
} & typeof variables;
|
|
10
|
-
export declare function createMessage<I extends Message, V extends Message>(message: I, variables?: V): I & typeof variables;
|
|
11
|
-
export declare function createMessage<I extends Message, V extends Message>(message: string | I, variables?: V): ({
|
|
12
|
-
[MESSAGE_KEY]: string;
|
|
13
|
-
} | I) & typeof variables;
|
|
14
|
-
export declare function getMessage(input: Message): string | undefined;
|
|
15
6
|
export interface PromptBuilderOptions {
|
|
16
7
|
instructions?: string | ChatMessagesTemplate;
|
|
17
8
|
}
|
|
18
9
|
export interface PromptBuildOptions extends Pick<AgentInvokeOptions, "context"> {
|
|
19
|
-
agent?: AIAgent
|
|
10
|
+
agent?: AIAgent<any, any, any>;
|
|
20
11
|
input?: Message;
|
|
21
12
|
model?: ChatModel;
|
|
22
13
|
outputSchema?: Agent["outputSchema"];
|
|
23
14
|
}
|
|
24
15
|
export declare class PromptBuilder {
|
|
25
|
-
static from(instructions: string): PromptBuilder;
|
|
26
|
-
static from(instructions: GetPromptResult): PromptBuilder;
|
|
27
|
-
static from(instructions: {
|
|
28
|
-
path: string;
|
|
29
|
-
}): Promise<PromptBuilder>;
|
|
30
16
|
static from(instructions: string | {
|
|
31
17
|
path: string;
|
|
32
|
-
} | GetPromptResult): PromptBuilder
|
|
18
|
+
} | GetPromptResult): PromptBuilder;
|
|
33
19
|
private static fromFile;
|
|
34
20
|
private static fromMCPPromptResult;
|
|
35
21
|
constructor(options?: PromptBuilderOptions);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AgentResponseChunk, type AgentResponseProgress, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
2
|
+
import type { Context } from "../aigne/context.js";
|
|
2
3
|
export declare class EventStreamParser<T> extends TransformStream<string, T | Error> {
|
|
3
4
|
constructor();
|
|
4
5
|
}
|
|
@@ -9,3 +10,6 @@ export declare class AgentResponseStreamParser<O extends Message> extends Transf
|
|
|
9
10
|
export declare class AgentResponseStreamSSE<O extends Message> extends ReadableStream<string> {
|
|
10
11
|
constructor(stream: AgentResponseStream<O>);
|
|
11
12
|
}
|
|
13
|
+
export declare class AgentResponseProgressStream extends ReadableStream<AgentResponseProgress> {
|
|
14
|
+
constructor(context: Context);
|
|
15
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { type AgentProcessAsyncGenerator, type AgentResponseChunk, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
2
|
-
import type { MESSAGE_KEY } from "../prompt/prompt-builder.js";
|
|
3
2
|
import { type PromiseOrValue } from "./type-utils.js";
|
|
4
3
|
import "./stream-polyfill.js";
|
|
5
4
|
export declare function objectToAgentResponseStream<T extends Message>(json: T): AgentResponseStream<T>;
|
|
6
5
|
export declare function mergeAgentResponseChunk<T extends Message>(output: T, chunk: AgentResponseChunk<T>): T;
|
|
7
6
|
export declare function agentResponseStreamToObject<T extends Message>(stream: AgentResponseStream<T> | AgentProcessAsyncGenerator<T>): Promise<T>;
|
|
8
7
|
export declare function asyncGeneratorToReadableStream<T extends Message>(generator: AgentProcessAsyncGenerator<T>): AgentResponseStream<T>;
|
|
9
|
-
export declare function onAgentResponseStreamEnd<T extends Message>(stream: AgentResponseStream<T>,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
export declare function onAgentResponseStreamEnd<T extends Message>(stream: AgentResponseStream<T>, options?: {
|
|
9
|
+
onChunk?: (chunk: AgentResponseChunk<T>) => PromiseOrValue<AgentResponseChunk<T> | undefined | void>;
|
|
10
|
+
onResult?: (result: T) => PromiseOrValue<Partial<T> | undefined | void>;
|
|
11
|
+
onError?: (error: Error) => PromiseOrValue<Error>;
|
|
12
|
+
}): AgentResponseStream<T>;
|
|
13
13
|
export declare function isAsyncGenerator<T extends AsyncGenerator>(value: AsyncGenerator | unknown): value is T;
|
|
14
14
|
export declare function arrayToAgentProcessAsyncGenerator<T extends Message>(chunks: (AgentResponseChunk<T> | Error)[], result?: Partial<T>): AgentProcessAsyncGenerator<T>;
|
|
15
15
|
export declare function arrayToReadableStream<T>(chunks: (T | Error)[]): ReadableStream<T>;
|
|
@@ -17,8 +17,10 @@ export declare function readableStreamToArray<T>(stream: ReadableStream<T>, opti
|
|
|
17
17
|
catchError: true;
|
|
18
18
|
}): Promise<(T | Error)[]>;
|
|
19
19
|
export declare function readableStreamToArray<T>(stream: ReadableStream<T>, options?: {
|
|
20
|
-
catchError?:
|
|
20
|
+
catchError?: boolean;
|
|
21
21
|
}): Promise<T[]>;
|
|
22
|
-
export declare function stringToAgentResponseStream(str: string, key?: "text" |
|
|
22
|
+
export declare function stringToAgentResponseStream(str: string, key?: "text" | string): AgentResponseStream<Message>;
|
|
23
23
|
export declare function toReadableStream(stream: NodeJS.ReadStream): ReadableStream<Uint8Array<ArrayBufferLike>>;
|
|
24
24
|
export declare function readAllString(stream: NodeJS.ReadStream | ReadableStream): Promise<string>;
|
|
25
|
+
export declare function mergeReadableStreams<T1, T2>(s1: ReadableStream<T1>, s2: ReadableStream<T2>): ReadableStream<T1 | T2>;
|
|
26
|
+
export declare function mergeReadableStreams(...streams: ReadableStream<any>[]): ReadableStream<any>;
|
|
@@ -15,6 +15,8 @@ export declare function isNotEmpty<T>(arr: T[]): arr is [T, ...T[]];
|
|
|
15
15
|
export declare function duplicates<T>(arr: T[], key?: (item: T) => unknown): T[];
|
|
16
16
|
export declare function remove<T>(arr: T[], remove: T[] | ((item: T) => boolean)): T[];
|
|
17
17
|
export declare function unique<T>(arr: T[], key?: (item: T) => unknown): T[];
|
|
18
|
+
export declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
|
|
19
|
+
export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
|
|
18
20
|
export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
|
|
19
21
|
export declare function orArrayToArray<T>(value?: T | T[]): T[];
|
|
20
22
|
export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
|