@aigne/core 1.10.0 → 1.12.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 +28 -0
- package/LICENSE +93 -0
- package/README.md +90 -0
- package/README.zh.md +90 -0
- package/lib/cjs/agents/agent.d.ts +21 -20
- package/lib/cjs/agents/agent.js +29 -26
- package/lib/cjs/agents/ai-agent.d.ts +9 -8
- package/lib/cjs/agents/ai-agent.js +20 -14
- package/lib/cjs/agents/mcp-agent.d.ts +10 -4
- package/lib/cjs/agents/mcp-agent.js +12 -6
- package/lib/cjs/agents/memory.d.ts +1 -1
- package/lib/cjs/agents/team-agent.d.ts +28 -0
- package/lib/cjs/agents/team-agent.js +93 -0
- package/lib/cjs/agents/user-agent.d.ts +9 -10
- package/lib/cjs/agents/user-agent.js +10 -13
- package/lib/{esm/execution-engine/execution-engine.d.ts → cjs/aigne/aigne.d.ts} +9 -12
- package/lib/cjs/{execution-engine/execution-engine.js → aigne/aigne.js} +19 -19
- package/lib/cjs/{execution-engine → aigne}/context.d.ts +31 -32
- package/lib/cjs/{execution-engine → aigne}/context.js +30 -40
- package/lib/cjs/aigne/index.d.ts +4 -0
- package/lib/cjs/{execution-engine → aigne}/index.js +2 -2
- package/lib/cjs/{execution-engine → aigne}/usage.d.ts +1 -1
- package/lib/cjs/client/client.d.ts +19 -0
- package/lib/cjs/client/client.js +49 -0
- package/lib/cjs/index.d.ts +2 -1
- package/lib/cjs/index.js +2 -1
- package/lib/cjs/loader/agent-js.d.ts +2 -2
- package/lib/cjs/loader/agent-js.js +4 -5
- package/lib/cjs/loader/agent-yaml.d.ts +8 -5
- package/lib/cjs/loader/agent-yaml.js +21 -2
- package/lib/cjs/loader/index.d.ts +5 -5
- package/lib/cjs/loader/index.js +8 -19
- package/lib/cjs/models/chat-model.d.ts +1 -1
- package/lib/cjs/models/claude-chat-model.d.ts +3 -1
- package/lib/cjs/models/claude-chat-model.js +75 -60
- package/lib/cjs/models/openai-chat-model.d.ts +3 -3
- package/lib/cjs/models/openai-chat-model.js +1 -3
- package/lib/cjs/prompt/prompt-builder.d.ts +1 -1
- package/lib/cjs/prompt/prompt-builder.js +3 -3
- package/lib/cjs/server/error.d.ts +4 -0
- package/lib/cjs/server/error.js +11 -0
- package/lib/cjs/server/server.d.ts +54 -0
- package/lib/cjs/server/server.js +130 -0
- package/lib/cjs/utils/camelize.d.ts +13 -0
- package/lib/cjs/utils/camelize.js +16 -0
- package/lib/cjs/utils/event-stream.d.ts +11 -0
- package/lib/cjs/utils/event-stream.js +91 -0
- package/lib/cjs/utils/mcp-utils.js +4 -1
- package/lib/cjs/utils/stream-utils.d.ts +10 -3
- package/lib/cjs/utils/stream-utils.js +51 -36
- package/lib/cjs/utils/type-utils.d.ts +4 -2
- package/lib/cjs/utils/type-utils.js +10 -2
- package/lib/dts/agents/agent.d.ts +21 -20
- package/lib/dts/agents/ai-agent.d.ts +9 -8
- package/lib/dts/agents/mcp-agent.d.ts +10 -4
- package/lib/dts/agents/memory.d.ts +1 -1
- package/lib/dts/agents/team-agent.d.ts +28 -0
- package/lib/dts/agents/user-agent.d.ts +9 -10
- package/lib/dts/{execution-engine/execution-engine.d.ts → aigne/aigne.d.ts} +9 -12
- package/lib/dts/{execution-engine → aigne}/context.d.ts +31 -32
- package/lib/dts/aigne/index.d.ts +4 -0
- package/lib/dts/{execution-engine → aigne}/usage.d.ts +1 -1
- package/lib/dts/client/client.d.ts +19 -0
- package/lib/dts/index.d.ts +2 -1
- package/lib/dts/loader/agent-js.d.ts +2 -2
- package/lib/dts/loader/agent-yaml.d.ts +8 -5
- package/lib/dts/loader/index.d.ts +5 -5
- package/lib/dts/models/chat-model.d.ts +1 -1
- package/lib/dts/models/claude-chat-model.d.ts +3 -1
- package/lib/dts/models/openai-chat-model.d.ts +3 -3
- package/lib/dts/prompt/prompt-builder.d.ts +1 -1
- package/lib/dts/server/error.d.ts +4 -0
- package/lib/dts/server/server.d.ts +54 -0
- package/lib/dts/utils/camelize.d.ts +13 -0
- package/lib/dts/utils/event-stream.d.ts +11 -0
- package/lib/dts/utils/stream-utils.d.ts +10 -3
- package/lib/dts/utils/type-utils.d.ts +4 -2
- package/lib/esm/agents/agent.d.ts +21 -20
- package/lib/esm/agents/agent.js +29 -27
- package/lib/esm/agents/ai-agent.d.ts +9 -8
- package/lib/esm/agents/ai-agent.js +20 -14
- package/lib/esm/agents/mcp-agent.d.ts +10 -4
- package/lib/esm/agents/mcp-agent.js +12 -6
- package/lib/esm/agents/memory.d.ts +1 -1
- package/lib/esm/agents/team-agent.d.ts +28 -0
- package/lib/esm/agents/team-agent.js +89 -0
- package/lib/esm/agents/user-agent.d.ts +9 -10
- package/lib/esm/agents/user-agent.js +11 -14
- package/lib/{cjs/execution-engine/execution-engine.d.ts → esm/aigne/aigne.d.ts} +9 -12
- package/lib/esm/{execution-engine/execution-engine.js → aigne/aigne.js} +18 -18
- package/lib/esm/{execution-engine → aigne}/context.d.ts +31 -32
- package/lib/esm/{execution-engine → aigne}/context.js +28 -38
- package/lib/esm/aigne/index.d.ts +4 -0
- package/lib/esm/aigne/index.js +4 -0
- package/lib/esm/{execution-engine → aigne}/usage.d.ts +1 -1
- package/lib/esm/client/client.d.ts +19 -0
- package/lib/esm/client/client.js +45 -0
- package/lib/esm/index.d.ts +2 -1
- package/lib/esm/index.js +2 -1
- package/lib/esm/loader/agent-js.d.ts +2 -2
- package/lib/esm/loader/agent-js.js +4 -5
- package/lib/esm/loader/agent-yaml.d.ts +8 -5
- package/lib/esm/loader/agent-yaml.js +21 -2
- package/lib/esm/loader/index.d.ts +5 -5
- package/lib/esm/loader/index.js +8 -19
- package/lib/esm/models/chat-model.d.ts +1 -1
- package/lib/esm/models/claude-chat-model.d.ts +3 -1
- package/lib/esm/models/claude-chat-model.js +75 -60
- package/lib/esm/models/openai-chat-model.d.ts +3 -3
- package/lib/esm/models/openai-chat-model.js +1 -3
- package/lib/esm/prompt/prompt-builder.d.ts +1 -1
- package/lib/esm/prompt/prompt-builder.js +3 -3
- package/lib/esm/server/error.d.ts +4 -0
- package/lib/esm/server/error.js +7 -0
- package/lib/esm/server/server.d.ts +54 -0
- package/lib/esm/server/server.js +123 -0
- package/lib/esm/utils/camelize.d.ts +13 -0
- package/lib/esm/utils/camelize.js +10 -0
- package/lib/esm/utils/event-stream.d.ts +11 -0
- package/lib/esm/utils/event-stream.js +85 -0
- package/lib/esm/utils/mcp-utils.js +4 -1
- package/lib/esm/utils/stream-utils.d.ts +10 -3
- package/lib/esm/utils/stream-utils.js +49 -35
- package/lib/esm/utils/type-utils.d.ts +4 -2
- package/lib/esm/utils/type-utils.js +9 -2
- package/package.json +15 -5
- package/lib/cjs/execution-engine/index.d.ts +0 -4
- package/lib/cjs/execution-engine/utils.d.ts +0 -4
- package/lib/cjs/execution-engine/utils.js +0 -34
- package/lib/dts/execution-engine/index.d.ts +0 -4
- package/lib/dts/execution-engine/utils.d.ts +0 -4
- package/lib/esm/execution-engine/index.d.ts +0 -4
- package/lib/esm/execution-engine/index.js +0 -4
- package/lib/esm/execution-engine/utils.d.ts +0 -4
- package/lib/esm/execution-engine/utils.js +0 -30
- /package/lib/cjs/{execution-engine → aigne}/message-queue.d.ts +0 -0
- /package/lib/cjs/{execution-engine → aigne}/message-queue.js +0 -0
- /package/lib/cjs/{execution-engine → aigne}/usage.js +0 -0
- /package/lib/dts/{execution-engine → aigne}/message-queue.d.ts +0 -0
- /package/lib/esm/{execution-engine → aigne}/message-queue.d.ts +0 -0
- /package/lib/esm/{execution-engine → aigne}/message-queue.js +0 -0
- /package/lib/esm/{execution-engine → aigne}/usage.js +0 -0
|
@@ -3,29 +3,29 @@ import { Agent } from "../agents/agent.js";
|
|
|
3
3
|
import { load } from "../loader/index.js";
|
|
4
4
|
import { ChatModel } from "../models/chat-model.js";
|
|
5
5
|
import { checkArguments, createAccessorArray } from "../utils/type-utils.js";
|
|
6
|
-
import {
|
|
6
|
+
import { AIGNEContext } from "./context.js";
|
|
7
7
|
import { MessageQueue } from "./message-queue.js";
|
|
8
|
-
export class
|
|
8
|
+
export class AIGNE {
|
|
9
9
|
static async load({ path, ...options }) {
|
|
10
|
-
const { model, agents,
|
|
11
|
-
return new
|
|
10
|
+
const { model, agents, skills, ...aigne } = await load({ path });
|
|
11
|
+
return new AIGNE({
|
|
12
12
|
...options,
|
|
13
13
|
model: options.model || model,
|
|
14
14
|
name: options.name || aigne.name || undefined,
|
|
15
15
|
description: options.description || aigne.description || undefined,
|
|
16
16
|
agents: agents.concat(options.agents ?? []),
|
|
17
|
-
|
|
17
|
+
skills: skills.concat(options.skills ?? []),
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
constructor(options) {
|
|
21
21
|
if (options)
|
|
22
|
-
checkArguments("
|
|
22
|
+
checkArguments("AIGNE", aigneOptionsSchema, options);
|
|
23
23
|
this.name = options?.name;
|
|
24
24
|
this.description = options?.description;
|
|
25
25
|
this.model = options?.model;
|
|
26
26
|
this.limits = options?.limits;
|
|
27
|
-
if (options?.
|
|
28
|
-
this.
|
|
27
|
+
if (options?.skills?.length)
|
|
28
|
+
this.skills.push(...options.skills);
|
|
29
29
|
if (options?.agents?.length)
|
|
30
30
|
this.addAgent(...options.agents);
|
|
31
31
|
this.initProcessExitHandler();
|
|
@@ -34,24 +34,24 @@ export class ExecutionEngine {
|
|
|
34
34
|
description;
|
|
35
35
|
messageQueue = new MessageQueue();
|
|
36
36
|
model;
|
|
37
|
-
|
|
37
|
+
skills = createAccessorArray([], (arr, name) => arr.find((i) => i.name === name));
|
|
38
38
|
agents = createAccessorArray([], (arr, name) => arr.find((i) => i.name === name));
|
|
39
39
|
limits;
|
|
40
40
|
addAgent(...agents) {
|
|
41
|
-
checkArguments("
|
|
41
|
+
checkArguments("AIGNE.addAgent", aigneAddAgentArgsSchema, agents);
|
|
42
42
|
for (const agent of agents) {
|
|
43
43
|
this.agents.push(agent);
|
|
44
44
|
agent.attach(this);
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
newContext() {
|
|
48
|
-
return new
|
|
48
|
+
return new AIGNEContext(this);
|
|
49
49
|
}
|
|
50
50
|
publish = ((...args) => {
|
|
51
|
-
return new
|
|
51
|
+
return new AIGNEContext(this).publish(...args);
|
|
52
52
|
});
|
|
53
|
-
|
|
54
|
-
return new
|
|
53
|
+
invoke = ((...args) => {
|
|
54
|
+
return new AIGNEContext(this).invoke(...args);
|
|
55
55
|
});
|
|
56
56
|
subscribe = ((...args) => {
|
|
57
57
|
return this.messageQueue.subscribe(...args);
|
|
@@ -60,7 +60,7 @@ export class ExecutionEngine {
|
|
|
60
60
|
this.messageQueue.unsubscribe(...args);
|
|
61
61
|
});
|
|
62
62
|
async shutdown() {
|
|
63
|
-
for (const tool of this.
|
|
63
|
+
for (const tool of this.skills) {
|
|
64
64
|
await tool.shutdown();
|
|
65
65
|
}
|
|
66
66
|
for (const agent of this.agents) {
|
|
@@ -73,9 +73,9 @@ export class ExecutionEngine {
|
|
|
73
73
|
process.on("exit", shutdownAndExit);
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
|
-
const
|
|
76
|
+
const aigneOptionsSchema = z.object({
|
|
77
77
|
model: z.instanceof(ChatModel).optional(),
|
|
78
|
-
|
|
78
|
+
skills: z.array(z.instanceof(Agent)).optional(),
|
|
79
79
|
agents: z.array(z.instanceof(Agent)).optional(),
|
|
80
80
|
});
|
|
81
|
-
const
|
|
81
|
+
const aigneAddAgentArgsSchema = z.array(z.instanceof(Agent));
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import EventEmitter from "node:events";
|
|
2
|
-
import { Agent, type
|
|
2
|
+
import { Agent, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
3
3
|
import { UserAgent } from "../agents/user-agent.js";
|
|
4
4
|
import type { ChatModel } from "../models/chat-model.js";
|
|
5
5
|
import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
|
|
6
6
|
import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
|
|
7
7
|
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
8
8
|
import { type ContextLimits, type ContextUsage } from "./usage.js";
|
|
9
|
-
export type Runnable<I extends Message = Message, O extends Message = Message> = Agent<I, O> | FunctionAgentFn;
|
|
10
9
|
export interface AgentEvent {
|
|
11
10
|
parentContextId?: string;
|
|
12
11
|
contextId: string;
|
|
@@ -27,53 +26,53 @@ export interface ContextEventMap {
|
|
|
27
26
|
export type ContextEmitEventMap = {
|
|
28
27
|
[K in keyof ContextEventMap]: OmitPropertiesFromArrayFirstElement<ContextEventMap[K], "contextId" | "parentContextId" | "timestamp">;
|
|
29
28
|
};
|
|
30
|
-
export interface
|
|
29
|
+
export interface InvokeOptions extends AgentInvokeOptions {
|
|
31
30
|
returnActiveAgent?: boolean;
|
|
32
31
|
disableTransfer?: boolean;
|
|
33
32
|
}
|
|
34
33
|
export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
|
|
35
34
|
model?: ChatModel;
|
|
36
|
-
|
|
35
|
+
skills?: Agent[];
|
|
37
36
|
usage: ContextUsage;
|
|
38
37
|
limits?: ContextLimits;
|
|
39
38
|
status?: "normal" | "timeout";
|
|
40
39
|
/**
|
|
41
|
-
* Create a user agent to consistently
|
|
42
|
-
* @param agent Agent to
|
|
40
|
+
* Create a user agent to consistently invoke an agent
|
|
41
|
+
* @param agent Agent to invoke
|
|
43
42
|
* @returns User agent
|
|
44
43
|
*/
|
|
45
|
-
|
|
44
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>): UserAgent<I, O>;
|
|
46
45
|
/**
|
|
47
|
-
*
|
|
48
|
-
* @param agent Agent to
|
|
46
|
+
* Invoke an agent with a message and return the output and the active agent
|
|
47
|
+
* @param agent Agent to invoke
|
|
49
48
|
* @param message Message to pass to the agent
|
|
50
49
|
* @param options.returnActiveAgent return the active agent
|
|
51
50
|
* @param options.streaming return a stream of the output
|
|
52
51
|
* @returns the output of the agent and the final active agent
|
|
53
52
|
*/
|
|
54
|
-
|
|
53
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
|
|
55
54
|
returnActiveAgent: true;
|
|
56
55
|
streaming?: false;
|
|
57
|
-
}): Promise<[O,
|
|
58
|
-
|
|
56
|
+
}): Promise<[O, Agent]>;
|
|
57
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
|
|
59
58
|
returnActiveAgent: true;
|
|
60
59
|
streaming: true;
|
|
61
|
-
}): Promise<[AgentResponseStream<O>, Promise<
|
|
60
|
+
}): Promise<[AgentResponseStream<O>, Promise<Agent>]>;
|
|
62
61
|
/**
|
|
63
|
-
*
|
|
64
|
-
* @param agent Agent to
|
|
62
|
+
* Invoke an agent with a message
|
|
63
|
+
* @param agent Agent to invoke
|
|
65
64
|
* @param message Message to pass to the agent
|
|
66
65
|
* @returns the output of the agent
|
|
67
66
|
*/
|
|
68
|
-
|
|
67
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options?: InvokeOptions & {
|
|
69
68
|
streaming?: false;
|
|
70
69
|
}): Promise<O>;
|
|
71
|
-
|
|
70
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
|
|
72
71
|
streaming: true;
|
|
73
72
|
}): Promise<AgentResponseStream<O>>;
|
|
74
|
-
|
|
73
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I | string, options?: InvokeOptions): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
|
|
75
74
|
/**
|
|
76
|
-
* Publish a message to a topic, the
|
|
75
|
+
* Publish a message to a topic, the aigne will invoke the listeners of the topic
|
|
77
76
|
* @param topic topic name, or an array of topic names
|
|
78
77
|
* @param payload message to publish
|
|
79
78
|
*/
|
|
@@ -96,21 +95,21 @@ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitE
|
|
|
96
95
|
}): Context;
|
|
97
96
|
}
|
|
98
97
|
export declare function createPublishMessage(message: string | Message, from?: Agent): Omit<MessagePayload, "context">;
|
|
99
|
-
export declare class
|
|
100
|
-
constructor(parent?: ConstructorParameters<typeof
|
|
98
|
+
export declare class AIGNEContext implements Context {
|
|
99
|
+
constructor(parent?: ConstructorParameters<typeof AIGNEContextInternal>[0]);
|
|
101
100
|
parentId?: string;
|
|
102
101
|
id: string;
|
|
103
|
-
readonly internal:
|
|
102
|
+
readonly internal: AIGNEContextInternal;
|
|
104
103
|
get model(): ChatModel | undefined;
|
|
105
|
-
get
|
|
104
|
+
get skills(): Agent<Message, Message>[] | undefined;
|
|
106
105
|
get limits(): ContextLimits | undefined;
|
|
107
106
|
get status(): "normal" | "timeout";
|
|
108
107
|
get usage(): ContextUsage;
|
|
109
108
|
newContext({ reset }?: {
|
|
110
109
|
reset?: boolean;
|
|
111
|
-
}):
|
|
112
|
-
|
|
113
|
-
private
|
|
110
|
+
}): AIGNEContext;
|
|
111
|
+
invoke: Context["invoke"];
|
|
112
|
+
private onInvokeSuccess;
|
|
114
113
|
publish: Context["publish"];
|
|
115
114
|
subscribe: Context["subscribe"];
|
|
116
115
|
unsubscribe: Context["unsubscribe"];
|
|
@@ -119,24 +118,24 @@ export declare class ExecutionContext implements Context {
|
|
|
119
118
|
once<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
120
119
|
off<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
121
120
|
}
|
|
122
|
-
declare class
|
|
121
|
+
declare class AIGNEContextInternal {
|
|
123
122
|
private readonly parent?;
|
|
124
|
-
constructor(parent?: (Pick<Context, "model" | "
|
|
123
|
+
constructor(parent?: (Pick<Context, "model" | "skills" | "limits"> & {
|
|
125
124
|
messageQueue?: MessageQueue;
|
|
126
125
|
}) | undefined);
|
|
127
126
|
readonly messageQueue: MessageQueue;
|
|
128
127
|
readonly events: EventEmitter<ContextEventMap>;
|
|
129
128
|
get model(): ChatModel | undefined;
|
|
130
|
-
get
|
|
129
|
+
get skills(): Agent<Message, Message>[] | undefined;
|
|
131
130
|
get limits(): ContextLimits | undefined;
|
|
132
131
|
usage: ContextUsage;
|
|
133
132
|
private abortController;
|
|
134
133
|
private timer?;
|
|
135
134
|
private initTimeout;
|
|
136
135
|
get status(): "normal" | "timeout";
|
|
137
|
-
|
|
138
|
-
__activeAgent__:
|
|
136
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, input: I, context: Context, options?: InvokeOptions): AgentProcessAsyncGenerator<O & {
|
|
137
|
+
__activeAgent__: Agent;
|
|
139
138
|
}>;
|
|
140
|
-
private
|
|
139
|
+
private invokeAgent;
|
|
141
140
|
}
|
|
142
141
|
export {};
|
|
@@ -16,14 +16,14 @@ export function createPublishMessage(message, from) {
|
|
|
16
16
|
message: createMessage(message),
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
|
-
export class
|
|
19
|
+
export class AIGNEContext {
|
|
20
20
|
constructor(parent) {
|
|
21
|
-
if (parent instanceof
|
|
21
|
+
if (parent instanceof AIGNEContext) {
|
|
22
22
|
this.parentId = parent.id;
|
|
23
23
|
this.internal = parent.internal;
|
|
24
24
|
}
|
|
25
25
|
else {
|
|
26
|
-
this.internal = new
|
|
26
|
+
this.internal = new AIGNEContextInternal(parent);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
parentId;
|
|
@@ -32,8 +32,8 @@ export class ExecutionContext {
|
|
|
32
32
|
get model() {
|
|
33
33
|
return this.internal.model;
|
|
34
34
|
}
|
|
35
|
-
get
|
|
36
|
-
return this.internal.
|
|
35
|
+
get skills() {
|
|
36
|
+
return this.internal.skills;
|
|
37
37
|
}
|
|
38
38
|
get limits() {
|
|
39
39
|
return this.internal.limits;
|
|
@@ -46,11 +46,11 @@ export class ExecutionContext {
|
|
|
46
46
|
}
|
|
47
47
|
newContext({ reset } = {}) {
|
|
48
48
|
if (reset)
|
|
49
|
-
return new
|
|
50
|
-
return new
|
|
49
|
+
return new AIGNEContext(this.internal);
|
|
50
|
+
return new AIGNEContext(this);
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
checkArguments("
|
|
52
|
+
invoke = ((agent, message, options) => {
|
|
53
|
+
checkArguments("AIGNEContext.invoke", aigneContextInvokeArgsSchema, {
|
|
54
54
|
agent,
|
|
55
55
|
message,
|
|
56
56
|
options,
|
|
@@ -63,10 +63,10 @@ export class ExecutionContext {
|
|
|
63
63
|
}
|
|
64
64
|
const newContext = this.newContext();
|
|
65
65
|
const msg = createMessage(message);
|
|
66
|
-
return Promise.resolve(newContext.internal.
|
|
66
|
+
return Promise.resolve(newContext.internal.invoke(agent, msg, newContext, options)).then(async (response) => {
|
|
67
67
|
if (!options?.streaming) {
|
|
68
68
|
const { __activeAgent__: activeAgent, ...output } = await agentResponseStreamToObject(response);
|
|
69
|
-
this.
|
|
69
|
+
this.onInvokeSuccess(activeAgent, output, newContext);
|
|
70
70
|
if (options?.returnActiveAgent) {
|
|
71
71
|
return [output, activeAgent];
|
|
72
72
|
}
|
|
@@ -74,7 +74,7 @@ export class ExecutionContext {
|
|
|
74
74
|
}
|
|
75
75
|
const activeAgentPromise = Promise.withResolvers();
|
|
76
76
|
const stream = onAgentResponseStreamEnd(asyncGeneratorToReadableStream(response), async ({ __activeAgent__: activeAgent, ...output }) => {
|
|
77
|
-
this.
|
|
77
|
+
this.onInvokeSuccess(activeAgent, output, newContext);
|
|
78
78
|
activeAgentPromise.resolve(activeAgent);
|
|
79
79
|
}, {
|
|
80
80
|
processChunk(chunk) {
|
|
@@ -96,7 +96,7 @@ export class ExecutionContext {
|
|
|
96
96
|
return stream;
|
|
97
97
|
});
|
|
98
98
|
});
|
|
99
|
-
async
|
|
99
|
+
async onInvokeSuccess(activeAgent, output, context) {
|
|
100
100
|
if (activeAgent instanceof Agent) {
|
|
101
101
|
const publishTopics = typeof activeAgent.publishTopic === "function"
|
|
102
102
|
? await activeAgent.publishTopic(output)
|
|
@@ -138,7 +138,7 @@ export class ExecutionContext {
|
|
|
138
138
|
return this;
|
|
139
139
|
}
|
|
140
140
|
}
|
|
141
|
-
class
|
|
141
|
+
class AIGNEContextInternal {
|
|
142
142
|
parent;
|
|
143
143
|
constructor(parent) {
|
|
144
144
|
this.parent = parent;
|
|
@@ -149,8 +149,8 @@ class ExecutionContextInternal {
|
|
|
149
149
|
get model() {
|
|
150
150
|
return this.parent?.model;
|
|
151
151
|
}
|
|
152
|
-
get
|
|
153
|
-
return this.parent?.
|
|
152
|
+
get skills() {
|
|
153
|
+
return this.parent?.skills;
|
|
154
154
|
}
|
|
155
155
|
get limits() {
|
|
156
156
|
return this.parent?.limits;
|
|
@@ -171,33 +171,23 @@ class ExecutionContextInternal {
|
|
|
171
171
|
get status() {
|
|
172
172
|
return this.abortController.signal.aborted ? "timeout" : "normal";
|
|
173
173
|
}
|
|
174
|
-
|
|
174
|
+
invoke(agent, input, context, options) {
|
|
175
175
|
this.initTimeout();
|
|
176
|
-
return withAbortSignal(this.abortController.signal, new Error("
|
|
176
|
+
return withAbortSignal(this.abortController.signal, new Error("AIGNEContext is timeout"), () => this.invokeAgent(agent, input, context, options));
|
|
177
177
|
}
|
|
178
|
-
async *
|
|
178
|
+
async *invokeAgent(agent, input, context, options) {
|
|
179
179
|
let activeAgent = agent;
|
|
180
180
|
let output;
|
|
181
181
|
for (;;) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (value.delta.text) {
|
|
191
|
-
yield { delta: { text: value.delta.text } };
|
|
192
|
-
}
|
|
193
|
-
if (value.delta.json) {
|
|
194
|
-
Object.assign(result, value.delta.json);
|
|
195
|
-
}
|
|
182
|
+
const result = {};
|
|
183
|
+
const stream = await activeAgent.invoke(input, context, { streaming: true });
|
|
184
|
+
for await (const value of readableStreamToAsyncIterator(stream)) {
|
|
185
|
+
if (value.delta.text) {
|
|
186
|
+
yield { delta: { text: value.delta.text } };
|
|
187
|
+
}
|
|
188
|
+
if (value.delta.json) {
|
|
189
|
+
Object.assign(result, value.delta.json);
|
|
196
190
|
}
|
|
197
|
-
}
|
|
198
|
-
if (result instanceof Agent) {
|
|
199
|
-
activeAgent = result;
|
|
200
|
-
continue;
|
|
201
191
|
}
|
|
202
192
|
if (!options?.disableTransfer) {
|
|
203
193
|
const transferToAgent = isTransferAgentOutput(result)
|
|
@@ -242,7 +232,7 @@ async function* withAbortSignal(signal, error, fn) {
|
|
|
242
232
|
signal.removeEventListener("abort", listener);
|
|
243
233
|
}
|
|
244
234
|
}
|
|
245
|
-
const
|
|
235
|
+
const aigneContextInvokeArgsSchema = z.object({
|
|
246
236
|
agent: z.union([z.function(), z.instanceof(Agent)]),
|
|
247
237
|
message: z.union([z.record(z.unknown()), z.string()]).optional(),
|
|
248
238
|
options: z.object({ returnActiveAgent: z.boolean().optional() }).optional(),
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AgentInvokeOptions, AgentResponse, AgentResponseStream, Message } from "../agents/agent.js";
|
|
2
|
+
export interface AIGNEClientOptions {
|
|
3
|
+
url: string;
|
|
4
|
+
}
|
|
5
|
+
export interface AIGNEClientInvokeOptions extends AgentInvokeOptions {
|
|
6
|
+
fetchOptions?: Partial<RequestInit>;
|
|
7
|
+
}
|
|
8
|
+
export declare class AIGNEClient {
|
|
9
|
+
options: AIGNEClientOptions;
|
|
10
|
+
constructor(options: AIGNEClientOptions);
|
|
11
|
+
invoke<I extends Message, O extends Message>(agent: string, input: I, options: AIGNEClientInvokeOptions & {
|
|
12
|
+
streaming: true;
|
|
13
|
+
}): Promise<AgentResponseStream<O>>;
|
|
14
|
+
invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions & {
|
|
15
|
+
streaming?: false;
|
|
16
|
+
}): Promise<O>;
|
|
17
|
+
invoke<I extends Message, O extends Message>(agent: string, input: I, options?: AIGNEClientInvokeOptions): Promise<AgentResponse<O>>;
|
|
18
|
+
fetch(...args: Parameters<typeof globalThis.fetch>): Promise<Response>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { AgentResponseStreamParser, EventStreamParser } from "../utils/event-stream.js";
|
|
2
|
+
import { tryOrThrow } from "../utils/type-utils.js";
|
|
3
|
+
export class AIGNEClient {
|
|
4
|
+
options;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.options = options;
|
|
7
|
+
}
|
|
8
|
+
async invoke(agent, input, options) {
|
|
9
|
+
const response = await this.fetch(this.options.url, {
|
|
10
|
+
...options?.fetchOptions,
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: {
|
|
13
|
+
"Content-Type": "application/json",
|
|
14
|
+
...options?.fetchOptions?.headers,
|
|
15
|
+
},
|
|
16
|
+
body: JSON.stringify({ agent, input, options }),
|
|
17
|
+
});
|
|
18
|
+
if (!options?.streaming) {
|
|
19
|
+
return await response.json();
|
|
20
|
+
}
|
|
21
|
+
const stream = response.body;
|
|
22
|
+
if (!stream)
|
|
23
|
+
throw new Error("Response body is not a stream");
|
|
24
|
+
return stream
|
|
25
|
+
.pipeThrough(new TextDecoderStream())
|
|
26
|
+
.pipeThrough(new EventStreamParser())
|
|
27
|
+
.pipeThrough(new AgentResponseStreamParser());
|
|
28
|
+
}
|
|
29
|
+
async fetch(...args) {
|
|
30
|
+
const result = await globalThis.fetch(...args);
|
|
31
|
+
if (!result.ok) {
|
|
32
|
+
let message;
|
|
33
|
+
try {
|
|
34
|
+
const text = await result.text();
|
|
35
|
+
const json = tryOrThrow(() => JSON.parse(text));
|
|
36
|
+
message = json?.error?.message || text;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// ignore
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`Failed to fetch url ${args[0]} with status ${result.status}: ${message}`);
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
}
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -2,9 +2,10 @@ export * from "./agents/agent.js";
|
|
|
2
2
|
export * from "./agents/ai-agent.js";
|
|
3
3
|
export * from "./agents/mcp-agent.js";
|
|
4
4
|
export * from "./agents/memory.js";
|
|
5
|
+
export * from "./agents/team-agent.js";
|
|
5
6
|
export * from "./agents/types.js";
|
|
6
7
|
export * from "./agents/user-agent.js";
|
|
7
|
-
export * from "./
|
|
8
|
+
export * from "./aigne/index.js";
|
|
8
9
|
export * from "./models/chat-model.js";
|
|
9
10
|
export * from "./prompt/prompt-builder.js";
|
|
10
11
|
export * from "./prompt/template.js";
|
package/lib/esm/index.js
CHANGED
|
@@ -2,9 +2,10 @@ export * from "./agents/agent.js";
|
|
|
2
2
|
export * from "./agents/ai-agent.js";
|
|
3
3
|
export * from "./agents/mcp-agent.js";
|
|
4
4
|
export * from "./agents/memory.js";
|
|
5
|
+
export * from "./agents/team-agent.js";
|
|
5
6
|
export * from "./agents/types.js";
|
|
6
7
|
export * from "./agents/user-agent.js";
|
|
7
|
-
export * from "./
|
|
8
|
+
export * from "./aigne/index.js";
|
|
8
9
|
export * from "./models/chat-model.js";
|
|
9
10
|
export * from "./prompt/prompt-builder.js";
|
|
10
11
|
export * from "./prompt/template.js";
|
|
@@ -4,12 +4,12 @@ export declare function loadAgentFromJsFile(path: string): Promise<{
|
|
|
4
4
|
name: string;
|
|
5
5
|
fn: (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;
|
|
9
9
|
}, {
|
|
10
10
|
[x: string]: any;
|
|
11
11
|
}> | undefined;
|
|
12
|
-
|
|
12
|
+
outputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
13
13
|
[x: string]: any;
|
|
14
14
|
}, {
|
|
15
15
|
[x: string]: any;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { jsonSchemaToZod } from "@aigne/json-schema-to-zod";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
import { customCamelize } from "../utils/camelize.js";
|
|
3
4
|
import { tryOrThrow } from "../utils/type-utils.js";
|
|
4
5
|
import { inputOutputSchema } from "./schema.js";
|
|
5
6
|
const agentJsFileSchema = z.object({
|
|
@@ -21,11 +22,9 @@ export async function loadAgentFromJsFile(path) {
|
|
|
21
22
|
if (typeof agent !== "function") {
|
|
22
23
|
throw new Error(`Agent file ${path} must export a default function, but got ${typeof agent}`);
|
|
23
24
|
}
|
|
24
|
-
return tryOrThrow(() => agentJsFileSchema.parse({
|
|
25
|
+
return tryOrThrow(() => customCamelize(agentJsFileSchema.parse({
|
|
26
|
+
...agent,
|
|
25
27
|
name: agent.agent_name || agent.name,
|
|
26
|
-
description: agent.description,
|
|
27
|
-
input_schema: agent.input_schema,
|
|
28
|
-
output_schema: agent.output_schema,
|
|
29
28
|
fn: agent,
|
|
30
|
-
}), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
|
|
29
|
+
}), { shallowKeys: ["input_schema", "output_schema"] }), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
|
|
31
30
|
}
|
|
@@ -3,20 +3,23 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
|
3
3
|
type: "ai";
|
|
4
4
|
name: string;
|
|
5
5
|
description?: string | undefined;
|
|
6
|
-
|
|
6
|
+
memory?: true | {
|
|
7
|
+
subscribeTopic?: string[] | undefined;
|
|
8
|
+
} | undefined;
|
|
9
|
+
skills?: string[] | undefined;
|
|
7
10
|
instructions?: string | undefined;
|
|
8
|
-
|
|
11
|
+
inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
9
12
|
[x: string]: any;
|
|
10
13
|
}, {
|
|
11
14
|
[x: string]: any;
|
|
12
15
|
}> | undefined;
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
toolChoice?: "auto" | "none" | "required" | "router" | undefined;
|
|
17
|
+
outputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
15
18
|
[x: string]: any;
|
|
16
19
|
}, {
|
|
17
20
|
[x: string]: any;
|
|
18
21
|
}> | undefined;
|
|
19
|
-
|
|
22
|
+
outputKey?: string | undefined;
|
|
20
23
|
} | {
|
|
21
24
|
type: "mcp";
|
|
22
25
|
url?: string | undefined;
|
|
@@ -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 { customCamelize } from "../utils/camelize.js";
|
|
5
6
|
import { tryOrThrow } from "../utils/type-utils.js";
|
|
6
7
|
import { inputOutputSchema } from "./schema.js";
|
|
7
8
|
const agentFileSchema = z.discriminatedUnion("type", [
|
|
@@ -26,7 +27,7 @@ const agentFileSchema = z.discriminatedUnion("type", [
|
|
|
26
27
|
.string()
|
|
27
28
|
.nullish()
|
|
28
29
|
.transform((v) => v ?? undefined),
|
|
29
|
-
|
|
30
|
+
skills: z
|
|
30
31
|
.array(z.string())
|
|
31
32
|
.nullish()
|
|
32
33
|
.transform((v) => v ?? undefined),
|
|
@@ -34,6 +35,18 @@ const agentFileSchema = z.discriminatedUnion("type", [
|
|
|
34
35
|
.union([z.literal("auto"), z.literal("none"), z.literal("required"), z.literal("router")])
|
|
35
36
|
.nullish()
|
|
36
37
|
.transform((v) => v ?? undefined),
|
|
38
|
+
memory: z
|
|
39
|
+
.union([
|
|
40
|
+
z.boolean(),
|
|
41
|
+
z.object({
|
|
42
|
+
subscribe_topic: z
|
|
43
|
+
.array(z.string())
|
|
44
|
+
.nullish()
|
|
45
|
+
.transform((v) => v ?? undefined),
|
|
46
|
+
}),
|
|
47
|
+
])
|
|
48
|
+
.nullish()
|
|
49
|
+
.transform((v) => v || undefined),
|
|
37
50
|
}),
|
|
38
51
|
z.object({
|
|
39
52
|
type: z.literal("mcp"),
|
|
@@ -54,6 +67,12 @@ const agentFileSchema = z.discriminatedUnion("type", [
|
|
|
54
67
|
export async function loadAgentFromYamlFile(path) {
|
|
55
68
|
const raw = await tryOrThrow(() => readFile(path, "utf8"), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
|
|
56
69
|
const json = await tryOrThrow(() => parse(raw), (error) => new Error(`Failed to parse agent definition from ${path}: ${error.message}`));
|
|
57
|
-
const agent = tryOrThrow(() => agentFileSchema.parse({
|
|
70
|
+
const agent = tryOrThrow(() => customCamelize(agentFileSchema.parse({
|
|
71
|
+
...json,
|
|
72
|
+
type: json.type ?? "ai",
|
|
73
|
+
skills: json.skills ?? json.tools,
|
|
74
|
+
}), {
|
|
75
|
+
shallowKeys: ["input_schema", "output_schema"],
|
|
76
|
+
}), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
|
|
58
77
|
return agent;
|
|
59
78
|
}
|