@aigne/core 1.13.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +13 -26
- package/README.zh.md +24 -37
- package/lib/cjs/agents/agent.d.ts +522 -15
- package/lib/cjs/agents/agent.js +357 -36
- package/lib/cjs/agents/ai-agent.d.ts +210 -52
- package/lib/cjs/agents/ai-agent.js +182 -24
- package/lib/cjs/agents/mcp-agent.d.ts +112 -0
- package/lib/cjs/agents/mcp-agent.js +79 -1
- package/lib/cjs/agents/team-agent.d.ts +99 -0
- package/lib/cjs/agents/team-agent.js +94 -0
- package/lib/cjs/agents/user-agent.d.ts +6 -4
- package/lib/cjs/agents/user-agent.js +16 -5
- package/lib/cjs/aigne/aigne.d.ts +263 -16
- package/lib/cjs/aigne/aigne.js +130 -20
- package/lib/cjs/aigne/context.d.ts +24 -8
- package/lib/cjs/aigne/context.js +8 -22
- package/lib/cjs/aigne/message-queue.d.ts +26 -4
- package/lib/cjs/aigne/message-queue.js +42 -7
- package/lib/cjs/aigne/usage.d.ts +9 -0
- package/lib/cjs/aigne/usage.js +3 -0
- package/lib/cjs/client/client.d.ts +81 -3
- package/lib/cjs/client/client.js +38 -0
- package/lib/cjs/client/index.d.ts +1 -0
- package/lib/cjs/client/index.js +17 -0
- package/lib/cjs/index.d.ts +0 -1
- package/lib/cjs/index.js +0 -1
- package/lib/cjs/loader/agent-js.d.ts +1 -1
- package/lib/cjs/loader/agent-js.js +2 -2
- package/lib/cjs/loader/agent-yaml.d.ts +3 -2
- package/lib/cjs/loader/agent-yaml.js +2 -1
- package/lib/cjs/loader/index.d.ts +4 -4
- package/lib/cjs/memory/default-memory.d.ts +16 -0
- package/lib/cjs/memory/default-memory.js +70 -0
- package/lib/cjs/memory/index.d.ts +3 -0
- package/lib/cjs/memory/index.js +19 -0
- package/lib/cjs/memory/memory.d.ts +89 -0
- package/lib/cjs/memory/memory.js +132 -0
- package/lib/cjs/memory/recorder.d.ts +86 -0
- package/lib/cjs/memory/recorder.js +50 -0
- package/lib/cjs/memory/retriever.d.ts +99 -0
- package/lib/cjs/memory/retriever.js +51 -0
- package/lib/cjs/models/bedrock-chat-model.d.ts +12 -3
- package/lib/cjs/models/bedrock-chat-model.js +54 -24
- package/lib/cjs/models/chat-model.d.ts +278 -1
- package/lib/cjs/models/chat-model.js +54 -0
- package/lib/cjs/models/claude-chat-model.d.ts +49 -3
- package/lib/cjs/models/claude-chat-model.js +34 -2
- package/lib/cjs/models/deepseek-chat-model.d.ts +16 -0
- package/lib/cjs/models/deepseek-chat-model.js +16 -0
- package/lib/cjs/models/gemini-chat-model.d.ts +14 -0
- package/lib/cjs/models/gemini-chat-model.js +14 -0
- package/lib/cjs/models/ollama-chat-model.d.ts +16 -0
- package/lib/cjs/models/ollama-chat-model.js +16 -0
- package/lib/cjs/models/open-router-chat-model.d.ts +16 -0
- package/lib/cjs/models/open-router-chat-model.js +16 -0
- package/lib/cjs/models/openai-chat-model.d.ts +67 -3
- package/lib/cjs/models/openai-chat-model.js +47 -2
- package/lib/cjs/models/xai-chat-model.d.ts +16 -0
- package/lib/cjs/models/xai-chat-model.js +16 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +4 -4
- package/lib/cjs/prompt/prompt-builder.js +19 -18
- package/lib/cjs/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/cjs/prompt/prompts/memory-message-template.js +10 -0
- package/lib/cjs/prompt/template.js +5 -1
- package/lib/cjs/server/error.d.ts +11 -0
- package/lib/cjs/server/error.js +11 -0
- package/lib/cjs/server/index.d.ts +2 -0
- package/lib/cjs/server/index.js +18 -0
- package/lib/cjs/server/server.d.ts +89 -8
- package/lib/cjs/server/server.js +58 -0
- package/lib/cjs/utils/fs.d.ts +2 -0
- package/lib/cjs/utils/fs.js +25 -0
- package/lib/cjs/utils/prompts.d.ts +1 -0
- package/lib/cjs/utils/prompts.js +11 -2
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/type-utils.js +14 -0
- package/lib/dts/agents/agent.d.ts +522 -15
- package/lib/dts/agents/ai-agent.d.ts +210 -52
- package/lib/dts/agents/mcp-agent.d.ts +112 -0
- package/lib/dts/agents/team-agent.d.ts +99 -0
- package/lib/dts/agents/user-agent.d.ts +6 -4
- package/lib/dts/aigne/aigne.d.ts +263 -16
- package/lib/dts/aigne/context.d.ts +24 -8
- package/lib/dts/aigne/message-queue.d.ts +26 -4
- package/lib/dts/aigne/usage.d.ts +9 -0
- package/lib/dts/client/client.d.ts +81 -3
- package/lib/dts/client/index.d.ts +1 -0
- package/lib/dts/index.d.ts +0 -1
- package/lib/dts/loader/agent-js.d.ts +1 -1
- package/lib/dts/loader/agent-yaml.d.ts +3 -2
- package/lib/dts/loader/index.d.ts +4 -4
- package/lib/dts/memory/default-memory.d.ts +16 -0
- package/lib/dts/memory/index.d.ts +3 -0
- package/lib/dts/memory/memory.d.ts +89 -0
- package/lib/dts/memory/recorder.d.ts +86 -0
- package/lib/dts/memory/retriever.d.ts +99 -0
- package/lib/dts/models/bedrock-chat-model.d.ts +12 -3
- package/lib/dts/models/chat-model.d.ts +278 -1
- package/lib/dts/models/claude-chat-model.d.ts +49 -3
- package/lib/dts/models/deepseek-chat-model.d.ts +16 -0
- package/lib/dts/models/gemini-chat-model.d.ts +14 -0
- package/lib/dts/models/ollama-chat-model.d.ts +16 -0
- package/lib/dts/models/open-router-chat-model.d.ts +16 -0
- package/lib/dts/models/openai-chat-model.d.ts +67 -3
- package/lib/dts/models/xai-chat-model.d.ts +16 -0
- package/lib/dts/prompt/prompt-builder.d.ts +4 -4
- package/lib/dts/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/dts/server/error.d.ts +11 -0
- package/lib/dts/server/index.d.ts +2 -0
- package/lib/dts/server/server.d.ts +89 -8
- package/lib/dts/utils/fs.d.ts +2 -0
- package/lib/dts/utils/prompts.d.ts +1 -0
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/esm/agents/agent.d.ts +522 -15
- package/lib/esm/agents/agent.js +351 -35
- package/lib/esm/agents/ai-agent.d.ts +210 -52
- package/lib/esm/agents/ai-agent.js +183 -25
- package/lib/esm/agents/mcp-agent.d.ts +112 -0
- package/lib/esm/agents/mcp-agent.js +79 -1
- package/lib/esm/agents/team-agent.d.ts +99 -0
- package/lib/esm/agents/team-agent.js +94 -0
- package/lib/esm/agents/user-agent.d.ts +6 -4
- package/lib/esm/agents/user-agent.js +17 -6
- package/lib/esm/aigne/aigne.d.ts +263 -16
- package/lib/esm/aigne/aigne.js +132 -22
- package/lib/esm/aigne/context.d.ts +24 -8
- package/lib/esm/aigne/context.js +9 -22
- package/lib/esm/aigne/message-queue.d.ts +26 -4
- package/lib/esm/aigne/message-queue.js +42 -8
- package/lib/esm/aigne/usage.d.ts +9 -0
- package/lib/esm/aigne/usage.js +3 -0
- package/lib/esm/client/client.d.ts +81 -3
- package/lib/esm/client/client.js +38 -0
- package/lib/esm/client/index.d.ts +1 -0
- package/lib/esm/client/index.js +1 -0
- package/lib/esm/index.d.ts +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/loader/agent-js.d.ts +1 -1
- package/lib/esm/loader/agent-js.js +2 -2
- package/lib/esm/loader/agent-yaml.d.ts +3 -2
- package/lib/esm/loader/agent-yaml.js +2 -1
- package/lib/esm/loader/index.d.ts +4 -4
- package/lib/esm/memory/default-memory.d.ts +16 -0
- package/lib/esm/memory/default-memory.js +63 -0
- package/lib/esm/memory/index.d.ts +3 -0
- package/lib/esm/memory/index.js +3 -0
- package/lib/esm/memory/memory.d.ts +89 -0
- package/lib/esm/memory/memory.js +127 -0
- package/lib/esm/memory/recorder.d.ts +86 -0
- package/lib/esm/memory/recorder.js +46 -0
- package/lib/esm/memory/retriever.d.ts +99 -0
- package/lib/esm/memory/retriever.js +47 -0
- package/lib/esm/models/bedrock-chat-model.d.ts +12 -3
- package/lib/esm/models/bedrock-chat-model.js +56 -26
- package/lib/esm/models/chat-model.d.ts +278 -1
- package/lib/esm/models/chat-model.js +54 -0
- package/lib/esm/models/claude-chat-model.d.ts +49 -3
- package/lib/esm/models/claude-chat-model.js +35 -3
- package/lib/esm/models/deepseek-chat-model.d.ts +16 -0
- package/lib/esm/models/deepseek-chat-model.js +16 -0
- package/lib/esm/models/gemini-chat-model.d.ts +14 -0
- package/lib/esm/models/gemini-chat-model.js +14 -0
- package/lib/esm/models/ollama-chat-model.d.ts +16 -0
- package/lib/esm/models/ollama-chat-model.js +16 -0
- package/lib/esm/models/open-router-chat-model.d.ts +16 -0
- package/lib/esm/models/open-router-chat-model.js +16 -0
- package/lib/esm/models/openai-chat-model.d.ts +67 -3
- package/lib/esm/models/openai-chat-model.js +47 -2
- package/lib/esm/models/xai-chat-model.d.ts +16 -0
- package/lib/esm/models/xai-chat-model.js +16 -0
- package/lib/esm/prompt/prompt-builder.d.ts +4 -4
- package/lib/esm/prompt/prompt-builder.js +20 -19
- package/lib/esm/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/esm/prompt/prompts/memory-message-template.js +7 -0
- package/lib/esm/prompt/template.js +5 -1
- package/lib/esm/server/error.d.ts +11 -0
- package/lib/esm/server/error.js +11 -0
- package/lib/esm/server/index.d.ts +2 -0
- package/lib/esm/server/index.js +2 -0
- package/lib/esm/server/server.d.ts +89 -8
- package/lib/esm/server/server.js +58 -0
- package/lib/esm/utils/fs.d.ts +2 -0
- package/lib/esm/utils/fs.js +21 -0
- package/lib/esm/utils/prompts.d.ts +1 -0
- package/lib/esm/utils/prompts.js +10 -2
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/type-utils.js +13 -0
- package/package.json +14 -11
- package/lib/cjs/agents/memory.d.ts +0 -26
- package/lib/cjs/agents/memory.js +0 -45
- package/lib/dts/agents/memory.d.ts +0 -26
- package/lib/esm/agents/memory.d.ts +0 -26
- package/lib/esm/agents/memory.js +0 -41
- /package/{LICENSE → LICENSE.md} +0 -0
|
@@ -1,22 +1,44 @@
|
|
|
1
1
|
import { EventEmitter } from "node:events";
|
|
2
2
|
import type { Message } from "../agents/agent.js";
|
|
3
3
|
import type { Context } from "./context.js";
|
|
4
|
+
/**
|
|
5
|
+
* @hidden
|
|
6
|
+
*/
|
|
4
7
|
export declare const UserInputTopic = "UserInputTopic";
|
|
8
|
+
/**
|
|
9
|
+
* @hidden
|
|
10
|
+
*/
|
|
5
11
|
export declare const UserOutputTopic = "UserOutputTopic";
|
|
12
|
+
/**
|
|
13
|
+
* @hidden
|
|
14
|
+
*/
|
|
6
15
|
export interface MessagePayload {
|
|
7
16
|
role: "user" | "agent";
|
|
8
17
|
source?: string;
|
|
9
18
|
message: Message;
|
|
10
19
|
context: Context;
|
|
11
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* @hidden
|
|
23
|
+
*/
|
|
24
|
+
export declare function toMessagePayload(payload: Omit<MessagePayload, "context"> | string | Message, options?: Partial<Pick<MessagePayload, "role" | "source">>): Omit<MessagePayload, "context">;
|
|
25
|
+
/**
|
|
26
|
+
* @hidden
|
|
27
|
+
*/
|
|
12
28
|
export type MessageQueueListener = (message: MessagePayload) => void;
|
|
29
|
+
/**
|
|
30
|
+
* @hidden
|
|
31
|
+
*/
|
|
13
32
|
export type Unsubscribe = () => void;
|
|
33
|
+
/**
|
|
34
|
+
* @hidden
|
|
35
|
+
*/
|
|
14
36
|
export declare class MessageQueue {
|
|
15
37
|
events: EventEmitter<[never]>;
|
|
16
38
|
publish(topic: string | string[], payload: MessagePayload): void;
|
|
17
39
|
error(error: Error): void;
|
|
18
|
-
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
19
|
-
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
20
|
-
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
21
|
-
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
40
|
+
subscribe(topic: string | string[], listener?: undefined): Promise<MessagePayload>;
|
|
41
|
+
subscribe(topic: string | string[], listener: MessageQueueListener): Unsubscribe;
|
|
42
|
+
subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
43
|
+
unsubscribe(topic: string | string[], listener: MessageQueueListener): void;
|
|
22
44
|
}
|
|
@@ -1,8 +1,40 @@
|
|
|
1
1
|
import { EventEmitter } from "node:events";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import {
|
|
3
|
+
import { createMessage } from "../prompt/prompt-builder.js";
|
|
4
|
+
import { checkArguments, isNil, orArrayToArray } from "../utils/type-utils.js";
|
|
5
|
+
/**
|
|
6
|
+
* @hidden
|
|
7
|
+
*/
|
|
4
8
|
export const UserInputTopic = "UserInputTopic";
|
|
9
|
+
/**
|
|
10
|
+
* @hidden
|
|
11
|
+
*/
|
|
5
12
|
export const UserOutputTopic = "UserOutputTopic";
|
|
13
|
+
function isMessagePayload(payload) {
|
|
14
|
+
return (!isNil(payload) &&
|
|
15
|
+
typeof payload === "object" &&
|
|
16
|
+
"role" in payload &&
|
|
17
|
+
typeof payload.role === "string" &&
|
|
18
|
+
["user", "agent"].includes(payload.role) &&
|
|
19
|
+
"message" in payload &&
|
|
20
|
+
!isNil(payload.message));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* @hidden
|
|
24
|
+
*/
|
|
25
|
+
export function toMessagePayload(payload, options) {
|
|
26
|
+
if (isMessagePayload(payload)) {
|
|
27
|
+
return { ...payload, message: createMessage(payload.message), ...options };
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
role: options?.role || "user",
|
|
31
|
+
source: options?.source,
|
|
32
|
+
message: createMessage(payload),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @hidden
|
|
37
|
+
*/
|
|
6
38
|
export class MessageQueue {
|
|
7
39
|
events = new EventEmitter();
|
|
8
40
|
publish(topic, payload) {
|
|
@@ -41,23 +73,25 @@ export class MessageQueue {
|
|
|
41
73
|
topic,
|
|
42
74
|
listener,
|
|
43
75
|
});
|
|
44
|
-
|
|
76
|
+
for (const t of orArrayToArray(topic)) {
|
|
77
|
+
this.events.off(t, listener);
|
|
78
|
+
}
|
|
45
79
|
}
|
|
46
80
|
}
|
|
47
81
|
function on(events, event, listener) {
|
|
48
|
-
events.on(
|
|
49
|
-
return () => events.off(
|
|
82
|
+
orArrayToArray(event).forEach((e) => events.on(e, listener));
|
|
83
|
+
return () => orArrayToArray(event).forEach((e) => events.off(e, listener));
|
|
50
84
|
}
|
|
51
85
|
function once(events, event, listener) {
|
|
52
|
-
events.once(
|
|
53
|
-
return () => events.off(
|
|
86
|
+
orArrayToArray(event).forEach((e) => events.once(e, listener));
|
|
87
|
+
return () => orArrayToArray(event).forEach((e) => events.off(e, listener));
|
|
54
88
|
}
|
|
55
89
|
const subscribeArgsSchema = z.object({
|
|
56
|
-
topic: z.string(),
|
|
90
|
+
topic: z.union([z.string(), z.array(z.string())]),
|
|
57
91
|
listener: z.function(z.tuple([z.any()]), z.any()).optional(),
|
|
58
92
|
});
|
|
59
93
|
const unsubscribeArgsSchema = z.object({
|
|
60
|
-
topic: z.string(),
|
|
94
|
+
topic: z.union([z.string(), z.array(z.string())]),
|
|
61
95
|
listener: z.function(z.tuple([z.any()]), z.any()),
|
|
62
96
|
});
|
|
63
97
|
const publishArgsSchema = z.object({
|
package/lib/esm/aigne/usage.d.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hidden
|
|
3
|
+
*/
|
|
1
4
|
export interface ContextUsage {
|
|
2
5
|
inputTokens: number;
|
|
3
6
|
outputTokens: number;
|
|
4
7
|
agentCalls: number;
|
|
5
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* @hidden
|
|
11
|
+
*/
|
|
6
12
|
export declare function newEmptyContextUsage(): ContextUsage;
|
|
13
|
+
/**
|
|
14
|
+
* @hidden
|
|
15
|
+
*/
|
|
7
16
|
export interface ContextLimits {
|
|
8
17
|
maxTokens?: number;
|
|
9
18
|
maxAgentInvokes?: number;
|
package/lib/esm/aigne/usage.js
CHANGED
|
@@ -1,19 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client module used to interact with the AIGNE framework.
|
|
3
|
+
*/
|
|
1
4
|
import type { AgentInvokeOptions, AgentResponse, AgentResponseStream, Message } from "../agents/agent.js";
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for the AIGNEClient.
|
|
7
|
+
*/
|
|
2
8
|
export interface AIGNEClientOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The URL of the AIGNE server to connect to.
|
|
11
|
+
* This should point to the base endpoint where the AIGNEServer is hosted.
|
|
12
|
+
*/
|
|
3
13
|
url: string;
|
|
4
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Options for invoking an agent through the AIGNEClient.
|
|
17
|
+
* Extends the standard AgentInvokeOptions with client-specific options.
|
|
18
|
+
*/
|
|
5
19
|
export interface AIGNEClientInvokeOptions extends AgentInvokeOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Additional fetch API options to customize the HTTP request.
|
|
22
|
+
* These options will be merged with the default options used by the client.
|
|
23
|
+
*/
|
|
6
24
|
fetchOptions?: Partial<RequestInit>;
|
|
7
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Client for interacting with a remote AIGNE server.
|
|
28
|
+
* AIGNEClient provides a client-side interface that matches the AIGNE API,
|
|
29
|
+
* allowing applications to invoke agents and receive responses from a remote AIGNE instance.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* Here's a simple example of how to use AIGNEClient:
|
|
33
|
+
* {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* Here's an example of how to use AIGNEClient with streaming response:
|
|
37
|
+
* {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
|
|
38
|
+
*/
|
|
8
39
|
export declare class AIGNEClient {
|
|
9
40
|
options: AIGNEClientOptions;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a new AIGNEClient instance.
|
|
43
|
+
*
|
|
44
|
+
* @param options - Configuration options for connecting to the AIGNE server
|
|
45
|
+
*/
|
|
10
46
|
constructor(options: AIGNEClientOptions);
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Invokes an agent in non-streaming mode and returns the complete response.
|
|
49
|
+
*
|
|
50
|
+
* @param agent - Name of the agent to invoke
|
|
51
|
+
* @param input - Input message for the agent
|
|
52
|
+
* @param options - Options with streaming mode explicitly set to false or omitted
|
|
53
|
+
* @returns The complete agent response
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* Here's a simple example of how to use AIGNEClient:
|
|
57
|
+
* {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
|
|
58
|
+
*/
|
|
14
59
|
invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions & {
|
|
15
60
|
streaming?: false;
|
|
16
61
|
}): Promise<O>;
|
|
62
|
+
/**
|
|
63
|
+
* Invokes an agent with streaming mode enabled and returns a stream of response chunks.
|
|
64
|
+
*
|
|
65
|
+
* @param agent - Name of the agent to invoke
|
|
66
|
+
* @param input - Input message for the agent
|
|
67
|
+
* @param options - Options with streaming mode explicitly set to true
|
|
68
|
+
* @returns A stream of agent response chunks
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* Here's an example of how to use AIGNEClient with streaming response:
|
|
72
|
+
* {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
|
|
73
|
+
*/
|
|
74
|
+
invoke<I extends Message, O extends Message>(agent: string, input: I, options: AIGNEClientInvokeOptions & {
|
|
75
|
+
streaming: true;
|
|
76
|
+
}): Promise<AgentResponseStream<O>>;
|
|
77
|
+
/**
|
|
78
|
+
* Invokes an agent with the given input and options.
|
|
79
|
+
*
|
|
80
|
+
* @param agent - Name of the agent to invoke
|
|
81
|
+
* @param input - Input message for the agent
|
|
82
|
+
* @param options - Options for the invocation
|
|
83
|
+
* @returns Either a complete response or a response stream depending on the streaming option
|
|
84
|
+
*/
|
|
17
85
|
invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions): Promise<AgentResponse<O>>;
|
|
86
|
+
/**
|
|
87
|
+
* Enhanced fetch method that handles error responses from the AIGNE server.
|
|
88
|
+
* This method wraps the standard fetch API to provide better error handling and reporting.
|
|
89
|
+
*
|
|
90
|
+
* @param args - Standard fetch API arguments (url and options)
|
|
91
|
+
* @returns A Response object if the request was successful
|
|
92
|
+
* @throws Error with detailed information if the request failed
|
|
93
|
+
*
|
|
94
|
+
* @private
|
|
95
|
+
*/
|
|
18
96
|
fetch(...args: Parameters<typeof globalThis.fetch>): Promise<Response>;
|
|
19
97
|
}
|
package/lib/esm/client/client.js
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client module used to interact with the AIGNE framework.
|
|
3
|
+
*/
|
|
1
4
|
import { AgentResponseStreamParser, EventStreamParser } from "../utils/event-stream.js";
|
|
2
5
|
import { tryOrThrow } from "../utils/type-utils.js";
|
|
6
|
+
/**
|
|
7
|
+
* Client for interacting with a remote AIGNE server.
|
|
8
|
+
* AIGNEClient provides a client-side interface that matches the AIGNE API,
|
|
9
|
+
* allowing applications to invoke agents and receive responses from a remote AIGNE instance.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* Here's a simple example of how to use AIGNEClient:
|
|
13
|
+
* {@includeCode ../../test/client/client.test.ts#example-aigne-client-simple}
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* Here's an example of how to use AIGNEClient with streaming response:
|
|
17
|
+
* {@includeCode ../../test/client/client.test.ts#example-aigne-client-streaming}
|
|
18
|
+
*/
|
|
3
19
|
export class AIGNEClient {
|
|
4
20
|
options;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new AIGNEClient instance.
|
|
23
|
+
*
|
|
24
|
+
* @param options - Configuration options for connecting to the AIGNE server
|
|
25
|
+
*/
|
|
5
26
|
constructor(options) {
|
|
6
27
|
this.options = options;
|
|
7
28
|
}
|
|
8
29
|
async invoke(agent, input, options) {
|
|
30
|
+
// Send the agent invocation request to the AIGNE server
|
|
9
31
|
const response = await this.fetch(this.options.url, {
|
|
10
32
|
...options?.fetchOptions,
|
|
11
33
|
method: "POST",
|
|
@@ -15,17 +37,33 @@ export class AIGNEClient {
|
|
|
15
37
|
},
|
|
16
38
|
body: JSON.stringify({ agent, input, options }),
|
|
17
39
|
});
|
|
40
|
+
// For non-streaming responses, simply parse the JSON response and return it
|
|
18
41
|
if (!options?.streaming) {
|
|
19
42
|
return await response.json();
|
|
20
43
|
}
|
|
44
|
+
// For streaming responses, set up the streaming pipeline
|
|
21
45
|
const stream = response.body;
|
|
22
46
|
if (!stream)
|
|
23
47
|
throw new Error("Response body is not a stream");
|
|
48
|
+
// Process the stream through a series of transforms:
|
|
49
|
+
// 1. Convert bytes to text
|
|
50
|
+
// 2. Parse SSE format into structured events
|
|
51
|
+
// 3. Convert events into a standardized agent response stream
|
|
24
52
|
return stream
|
|
25
53
|
.pipeThrough(new TextDecoderStream())
|
|
26
54
|
.pipeThrough(new EventStreamParser())
|
|
27
55
|
.pipeThrough(new AgentResponseStreamParser());
|
|
28
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Enhanced fetch method that handles error responses from the AIGNE server.
|
|
59
|
+
* This method wraps the standard fetch API to provide better error handling and reporting.
|
|
60
|
+
*
|
|
61
|
+
* @param args - Standard fetch API arguments (url and options)
|
|
62
|
+
* @returns A Response object if the request was successful
|
|
63
|
+
* @throws Error with detailed information if the request failed
|
|
64
|
+
*
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
29
67
|
async fetch(...args) {
|
|
30
68
|
const result = await globalThis.fetch(...args);
|
|
31
69
|
if (!result.ok) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./client.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./client.js";
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export * from "./agents/agent.js";
|
|
2
2
|
export * from "./agents/ai-agent.js";
|
|
3
3
|
export * from "./agents/mcp-agent.js";
|
|
4
|
-
export * from "./agents/memory.js";
|
|
5
4
|
export * from "./agents/team-agent.js";
|
|
6
5
|
export * from "./agents/types.js";
|
|
7
6
|
export * from "./agents/user-agent.js";
|
package/lib/esm/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export * from "./agents/agent.js";
|
|
2
2
|
export * from "./agents/ai-agent.js";
|
|
3
3
|
export * from "./agents/mcp-agent.js";
|
|
4
|
-
export * from "./agents/memory.js";
|
|
5
4
|
export * from "./agents/team-agent.js";
|
|
6
5
|
export * from "./agents/types.js";
|
|
7
6
|
export * from "./agents/user-agent.js";
|
|
@@ -2,7 +2,7 @@ import { type ZodObject, type ZodType, z } from "zod";
|
|
|
2
2
|
import type { Message } from "../agents/agent.js";
|
|
3
3
|
export declare function loadAgentFromJsFile(path: string): Promise<{
|
|
4
4
|
name: string;
|
|
5
|
-
|
|
5
|
+
process: (args_0: Message) => Message;
|
|
6
6
|
description?: string | undefined;
|
|
7
7
|
inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
8
8
|
[x: string]: any;
|
|
@@ -15,7 +15,7 @@ const agentJsFileSchema = z.object({
|
|
|
15
15
|
output_schema: inputOutputSchema
|
|
16
16
|
.nullish()
|
|
17
17
|
.transform((v) => (v ? jsonSchemaToZod(v) : undefined)),
|
|
18
|
-
|
|
18
|
+
process: z.function(),
|
|
19
19
|
});
|
|
20
20
|
export async function loadAgentFromJsFile(path) {
|
|
21
21
|
const { default: agent } = await tryOrThrow(() => import(path), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
|
|
@@ -25,6 +25,6 @@ export async function loadAgentFromJsFile(path) {
|
|
|
25
25
|
return tryOrThrow(() => customCamelize(agentJsFileSchema.parse({
|
|
26
26
|
...agent,
|
|
27
27
|
name: agent.agent_name || agent.name,
|
|
28
|
-
|
|
28
|
+
process: agent,
|
|
29
29
|
}), { shallowKeys: ["input_schema", "output_schema"] }), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
|
|
30
30
|
}
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { type ZodObject, type ZodType, z } from "zod";
|
|
2
|
+
import { AIAgentToolChoice } from "../agents/ai-agent.js";
|
|
2
3
|
export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
3
4
|
type: "ai";
|
|
4
5
|
name: string;
|
|
5
6
|
description?: string | undefined;
|
|
7
|
+
skills?: string[] | undefined;
|
|
6
8
|
memory?: true | {
|
|
7
9
|
subscribeTopic?: string[] | undefined;
|
|
8
10
|
} | undefined;
|
|
9
|
-
skills?: string[] | undefined;
|
|
10
11
|
instructions?: string | undefined;
|
|
11
12
|
inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
12
13
|
[x: string]: any;
|
|
13
14
|
}, {
|
|
14
15
|
[x: string]: any;
|
|
15
16
|
}> | undefined;
|
|
16
|
-
toolChoice?:
|
|
17
|
+
toolChoice?: AIAgentToolChoice | undefined;
|
|
17
18
|
outputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
18
19
|
[x: string]: any;
|
|
19
20
|
}, {
|
|
@@ -2,6 +2,7 @@ import { readFile } from "node:fs/promises";
|
|
|
2
2
|
import { jsonSchemaToZod } from "@aigne/json-schema-to-zod";
|
|
3
3
|
import { parse } from "yaml";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
+
import { AIAgentToolChoice } from "../agents/ai-agent.js";
|
|
5
6
|
import { customCamelize } from "../utils/camelize.js";
|
|
6
7
|
import { tryOrThrow } from "../utils/type-utils.js";
|
|
7
8
|
import { inputOutputSchema } from "./schema.js";
|
|
@@ -32,7 +33,7 @@ const agentFileSchema = z.discriminatedUnion("type", [
|
|
|
32
33
|
.nullish()
|
|
33
34
|
.transform((v) => v ?? undefined),
|
|
34
35
|
tool_choice: z
|
|
35
|
-
.
|
|
36
|
+
.nativeEnum(AIAgentToolChoice)
|
|
36
37
|
.nullish()
|
|
37
38
|
.transform((v) => v ?? undefined),
|
|
38
39
|
memory: z
|
|
@@ -8,8 +8,8 @@ export declare function load(options: LoadOptions): Promise<{
|
|
|
8
8
|
model: ChatModel | undefined;
|
|
9
9
|
agents: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
|
|
10
10
|
skills: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
|
|
11
|
-
description?: string | null | undefined;
|
|
12
11
|
name?: string | null | undefined;
|
|
12
|
+
description?: string | null | undefined;
|
|
13
13
|
chat_model?: {
|
|
14
14
|
name?: string | null | undefined;
|
|
15
15
|
temperature?: number | null | undefined;
|
|
@@ -63,8 +63,8 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
63
63
|
agents: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
|
|
64
64
|
skills: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
|
|
65
65
|
}, "strip", z.ZodTypeAny, {
|
|
66
|
-
description?: string | null | undefined;
|
|
67
66
|
name?: string | null | undefined;
|
|
67
|
+
description?: string | null | undefined;
|
|
68
68
|
skills?: string[] | null | undefined;
|
|
69
69
|
chat_model?: {
|
|
70
70
|
name?: string | null | undefined;
|
|
@@ -76,8 +76,8 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
76
76
|
} | null | undefined;
|
|
77
77
|
agents?: string[] | null | undefined;
|
|
78
78
|
}, {
|
|
79
|
-
description?: string | null | undefined;
|
|
80
79
|
name?: string | null | undefined;
|
|
80
|
+
description?: string | null | undefined;
|
|
81
81
|
skills?: string[] | null | undefined;
|
|
82
82
|
chat_model?: string | {
|
|
83
83
|
name?: string | null | undefined;
|
|
@@ -90,8 +90,8 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
90
90
|
agents?: string[] | null | undefined;
|
|
91
91
|
}>;
|
|
92
92
|
export declare function loadAIGNEFile(path: string): Promise<{
|
|
93
|
-
description?: string | null | undefined;
|
|
94
93
|
name?: string | null | undefined;
|
|
94
|
+
description?: string | null | undefined;
|
|
95
95
|
skills?: string[] | null | undefined;
|
|
96
96
|
chat_model?: {
|
|
97
97
|
name?: string | null | undefined;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Memory, MemoryAgent, type MemoryAgentOptions } from "./memory.js";
|
|
2
|
+
export declare const DEFAULT_MAX_MEMORY_COUNT = 10;
|
|
3
|
+
export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions> {
|
|
4
|
+
}
|
|
5
|
+
export declare class DefaultMemory extends MemoryAgent {
|
|
6
|
+
constructor(options?: DefaultMemoryOptions);
|
|
7
|
+
storage: Memory[];
|
|
8
|
+
search(options?: {
|
|
9
|
+
limit?: number;
|
|
10
|
+
}): Promise<{
|
|
11
|
+
result: Memory[];
|
|
12
|
+
}>;
|
|
13
|
+
create(memory: Pick<Memory, "content">): Promise<{
|
|
14
|
+
result: Memory;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import equal from "fast-deep-equal";
|
|
2
|
+
import { MemoryAgent, newMemoryId } from "./memory.js";
|
|
3
|
+
import { MemoryRecorder } from "./recorder.js";
|
|
4
|
+
import { MemoryRetriever, } from "./retriever.js";
|
|
5
|
+
export const DEFAULT_MAX_MEMORY_COUNT = 10;
|
|
6
|
+
export class DefaultMemory extends MemoryAgent {
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
super({
|
|
9
|
+
...options,
|
|
10
|
+
autoUpdate: options.autoUpdate ?? true,
|
|
11
|
+
});
|
|
12
|
+
if (!this.recorder)
|
|
13
|
+
this.recorder = new DefaultMemoryRecorder(this);
|
|
14
|
+
if (!this.retriever)
|
|
15
|
+
this.retriever = new DefaultMemoryRetriever(this);
|
|
16
|
+
}
|
|
17
|
+
storage = [];
|
|
18
|
+
async search(options = {}) {
|
|
19
|
+
const { limit = DEFAULT_MAX_MEMORY_COUNT } = options;
|
|
20
|
+
const result = limit < 0 ? this.storage.slice(limit) : this.storage.slice(0, limit);
|
|
21
|
+
return { result };
|
|
22
|
+
}
|
|
23
|
+
async create(memory) {
|
|
24
|
+
const m = {
|
|
25
|
+
...memory,
|
|
26
|
+
id: newMemoryId(),
|
|
27
|
+
createdAt: new Date().toISOString(),
|
|
28
|
+
};
|
|
29
|
+
this.storage.push(m);
|
|
30
|
+
return { result: m };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
class DefaultMemoryRetriever extends MemoryRetriever {
|
|
34
|
+
memory;
|
|
35
|
+
constructor(memory) {
|
|
36
|
+
super({});
|
|
37
|
+
this.memory = memory;
|
|
38
|
+
}
|
|
39
|
+
async process(input) {
|
|
40
|
+
const { result } = await this.memory.search(input);
|
|
41
|
+
return { memories: result };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
class DefaultMemoryRecorder extends MemoryRecorder {
|
|
45
|
+
memory;
|
|
46
|
+
constructor(memory) {
|
|
47
|
+
super({});
|
|
48
|
+
this.memory = memory;
|
|
49
|
+
}
|
|
50
|
+
async process(input) {
|
|
51
|
+
const newMemories = [];
|
|
52
|
+
for (const content of input.content) {
|
|
53
|
+
const { result: [last], } = await this.memory.search({ limit: -1 });
|
|
54
|
+
if (!equal(last?.content, content)) {
|
|
55
|
+
const { result } = await this.memory.create({ content });
|
|
56
|
+
newMemories.push(result);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
memories: newMemories,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Agent, type AgentOptions, type Message } from "../agents/agent.js";
|
|
2
|
+
import type { Context } from "../aigne/context.js";
|
|
3
|
+
import type { MessagePayload } from "../aigne/message-queue.js";
|
|
4
|
+
import type { MemoryRecorder, MemoryRecorderInput, MemoryRecorderOutput } from "./recorder.js";
|
|
5
|
+
import type { MemoryRetriever, MemoryRetrieverInput, MemoryRetrieverOutput } from "./retriever.js";
|
|
6
|
+
export interface Memory {
|
|
7
|
+
id: string;
|
|
8
|
+
content: unknown;
|
|
9
|
+
createdAt: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const newMemoryId: () => string;
|
|
12
|
+
export interface MemoryAgentOptions extends Partial<Pick<MemoryAgent, "recorder" | "retriever" | "autoUpdate">>, Pick<AgentOptions, "subscribeTopic" | "skills"> {
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A specialized agent responsible for managing, storing, and retrieving memories within the agent system.
|
|
16
|
+
*
|
|
17
|
+
* MemoryAgent serves as a bridge between application logic and memory storage/retrieval mechanisms.
|
|
18
|
+
* It delegates the actual memory operations to specialized recorder and retriever agents that
|
|
19
|
+
* are attached as skills. This agent doesn't directly process messages like other agents but
|
|
20
|
+
* instead provides memory management capabilities to the system.
|
|
21
|
+
*/
|
|
22
|
+
export declare class MemoryAgent extends Agent {
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new MemoryAgent instance.
|
|
25
|
+
*/
|
|
26
|
+
constructor(options: MemoryAgentOptions);
|
|
27
|
+
private _retriever?;
|
|
28
|
+
/**
|
|
29
|
+
* Agent used for retrieving memories from storage.
|
|
30
|
+
*
|
|
31
|
+
* This retriever is automatically added to the agent's skills when set.
|
|
32
|
+
* Setting a new retriever will remove any previously set retriever from skills.
|
|
33
|
+
*/
|
|
34
|
+
get retriever(): MemoryRetriever | undefined;
|
|
35
|
+
set retriever(value: MemoryRetriever | undefined);
|
|
36
|
+
private _recorder?;
|
|
37
|
+
/**
|
|
38
|
+
* Agent used for recording and storing new memories.
|
|
39
|
+
*
|
|
40
|
+
* This recorder is automatically added to the agent's skills when set.
|
|
41
|
+
* Setting a new recorder will remove any previously set recorder from skills.
|
|
42
|
+
*/
|
|
43
|
+
get recorder(): MemoryRecorder | undefined;
|
|
44
|
+
set recorder(value: MemoryRecorder | undefined);
|
|
45
|
+
/**
|
|
46
|
+
* Controls whether to automatically update the memory when agent call completes.
|
|
47
|
+
*
|
|
48
|
+
* When true, the agent will automatically record any relevant information
|
|
49
|
+
* after completing operations, creating a history of interactions.
|
|
50
|
+
*/
|
|
51
|
+
autoUpdate?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Indicates whether this agent can be directly called.
|
|
54
|
+
*
|
|
55
|
+
* MemoryAgent is designed to be used as a supporting component rather than
|
|
56
|
+
* being directly invoked for processing, so this returns false.
|
|
57
|
+
*/
|
|
58
|
+
get isCallable(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* The standard message processing method required by the Agent interface.
|
|
61
|
+
*
|
|
62
|
+
* MemoryAgent doesn't directly process messages like other agents, so this method
|
|
63
|
+
* throws an error when called. Use the specialized retrieve() and record() methods instead.
|
|
64
|
+
*/
|
|
65
|
+
process(_input: Message, _context: Context): Promise<Message>;
|
|
66
|
+
/**
|
|
67
|
+
* Retrieves memories based on the provided input criteria.
|
|
68
|
+
*
|
|
69
|
+
* Delegates the actual retrieval operation to the configured retriever agent.
|
|
70
|
+
*
|
|
71
|
+
* @param input - The retrieval parameters (can include search terms, limits, etc.)
|
|
72
|
+
* @param context - The execution context
|
|
73
|
+
* @returns A promise resolving to the retrieved memories
|
|
74
|
+
* @throws Error - If no retriever has been initialized
|
|
75
|
+
*/
|
|
76
|
+
retrieve(input: MemoryRetrieverInput, context: Context): Promise<MemoryRetrieverOutput>;
|
|
77
|
+
/**
|
|
78
|
+
* Records new memories based on the provided input content.
|
|
79
|
+
*
|
|
80
|
+
* Delegates the actual recording operation to the configured recorder agent.
|
|
81
|
+
*
|
|
82
|
+
* @param input - The content to be recorded as memories
|
|
83
|
+
* @param context - The execution context
|
|
84
|
+
* @returns A promise resolving to the recorded memories
|
|
85
|
+
* @throws Error - If no recorder has been initialized
|
|
86
|
+
*/
|
|
87
|
+
record(input: MemoryRecorderInput, context: Context): Promise<MemoryRecorderOutput>;
|
|
88
|
+
onMessage({ role, source, message, context }: MessagePayload): Promise<void>;
|
|
89
|
+
}
|