@aigne/core 1.10.0 → 1.11.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 +13 -0
- package/LICENSE +93 -0
- package/README.md +89 -0
- package/README.zh.md +89 -0
- package/lib/cjs/agents/agent.d.ts +1 -0
- package/lib/cjs/agents/agent.js +4 -0
- package/lib/cjs/agents/ai-agent.d.ts +11 -9
- package/lib/cjs/agents/ai-agent.js +17 -11
- 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 +13 -1
- package/lib/cjs/loader/index.d.ts +4 -4
- package/lib/cjs/loader/index.js +4 -15
- 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.js +1 -3
- package/lib/cjs/utils/camelize.d.ts +13 -0
- package/lib/cjs/utils/camelize.js +16 -0
- package/lib/cjs/utils/stream-utils.d.ts +3 -1
- package/lib/cjs/utils/stream-utils.js +10 -4
- package/lib/dts/agents/agent.d.ts +1 -0
- package/lib/dts/agents/ai-agent.d.ts +11 -9
- 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 +4 -4
- package/lib/dts/models/claude-chat-model.d.ts +3 -1
- package/lib/dts/utils/camelize.d.ts +13 -0
- package/lib/dts/utils/stream-utils.d.ts +3 -1
- package/lib/esm/agents/agent.d.ts +1 -0
- package/lib/esm/agents/agent.js +4 -1
- package/lib/esm/agents/ai-agent.d.ts +11 -9
- package/lib/esm/agents/ai-agent.js +17 -11
- 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 +13 -1
- package/lib/esm/loader/index.d.ts +4 -4
- package/lib/esm/loader/index.js +4 -15
- 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.js +1 -3
- package/lib/esm/utils/camelize.d.ts +13 -0
- package/lib/esm/utils/camelize.js +10 -0
- package/lib/esm/utils/stream-utils.d.ts +3 -1
- package/lib/esm/utils/stream-utils.js +9 -4
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -22,6 +22,19 @@
|
|
|
22
22
|
* rename @aigne/core-next to @aigne/core ([3a81009](https://github.com/AIGNE-io/aigne-framework/commit/3a8100962c81813217b687ae28e8de604419c622))
|
|
23
23
|
* use text resource from MCP correctly ([8b9eba8](https://github.com/AIGNE-io/aigne-framework/commit/8b9eba83352ec096a2a5d4f410d4c4bde7420bce))
|
|
24
24
|
|
|
25
|
+
## [1.11.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.10.0...core-v1.11.0) (2025-04-23)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Features
|
|
29
|
+
|
|
30
|
+
* **core:** enhance ClaudeChatModel to support streaming responses ([#85](https://github.com/AIGNE-io/aigne-framework/issues/85)) ([5433240](https://github.com/AIGNE-io/aigne-framework/commit/5433240e7b663ec9e9f4a79dffa05038088d54fc))
|
|
31
|
+
* support set memory in agent yaml ([#90](https://github.com/AIGNE-io/aigne-framework/issues/90)) ([215118f](https://github.com/AIGNE-io/aigne-framework/commit/215118f1dc55f02322d59a3f18395a459198e031))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
### Bug Fixes
|
|
35
|
+
|
|
36
|
+
* **core:** router model should support streaming response ([#88](https://github.com/AIGNE-io/aigne-framework/issues/88)) ([4fb4d92](https://github.com/AIGNE-io/aigne-framework/commit/4fb4d92f8b36011437efba3265591b2477f2d680))
|
|
37
|
+
|
|
25
38
|
## [1.10.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.9.0...core-v1.10.0) (2025-04-22)
|
|
26
39
|
|
|
27
40
|
|
package/LICENSE
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Elastic License 2.0
|
|
2
|
+
|
|
3
|
+
URL: https://www.elastic.co/licensing/elastic-license
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
12
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
13
|
+
available, and prepare derivative works of the software, in each case subject to
|
|
14
|
+
the limitations and conditions below.
|
|
15
|
+
|
|
16
|
+
## Limitations
|
|
17
|
+
|
|
18
|
+
You may not provide the software to third parties as a hosted or managed
|
|
19
|
+
service, where the service provides users with access to any substantial set of
|
|
20
|
+
the features or functionality of the software.
|
|
21
|
+
|
|
22
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
23
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
24
|
+
software that is protected by the license key.
|
|
25
|
+
|
|
26
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices
|
|
27
|
+
of the licensor in the software. Any use of the licensor’s trademarks is subject
|
|
28
|
+
to applicable law.
|
|
29
|
+
|
|
30
|
+
## Patents
|
|
31
|
+
|
|
32
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
33
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
34
|
+
sale, import and have imported the software, in each case subject to the
|
|
35
|
+
limitations and conditions in this license. This license does not cover any
|
|
36
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
37
|
+
the software. If you or your company make any written claim that the software
|
|
38
|
+
infringes or contributes to infringement of any patent, your patent license for
|
|
39
|
+
the software granted under these terms ends immediately. If your company makes
|
|
40
|
+
such a claim, your patent license ends immediately for work on behalf of your
|
|
41
|
+
company.
|
|
42
|
+
|
|
43
|
+
## Notices
|
|
44
|
+
|
|
45
|
+
You must ensure that anyone who gets a copy of any part of the software from you
|
|
46
|
+
also gets a copy of these terms.
|
|
47
|
+
|
|
48
|
+
If you modify the software, you must include in any modified copies of the
|
|
49
|
+
software prominent notices stating that you have modified the software.
|
|
50
|
+
|
|
51
|
+
## No Other Rights
|
|
52
|
+
|
|
53
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
54
|
+
these terms.
|
|
55
|
+
|
|
56
|
+
## Termination
|
|
57
|
+
|
|
58
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
59
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
60
|
+
with a notice of your violation, and you cease all violation of this license no
|
|
61
|
+
later than 30 days after you receive that notice, your licenses will be
|
|
62
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
63
|
+
reinstatement, any additional violation of these terms will cause your licenses
|
|
64
|
+
to terminate automatically and permanently.
|
|
65
|
+
|
|
66
|
+
## No Liability
|
|
67
|
+
|
|
68
|
+
*As far as the law allows, the software comes as is, without any warranty or
|
|
69
|
+
condition, and the licensor will not be liable to you for any damages arising
|
|
70
|
+
out of these terms or the use or nature of the software, under any kind of
|
|
71
|
+
legal claim.*
|
|
72
|
+
|
|
73
|
+
## Definitions
|
|
74
|
+
|
|
75
|
+
The **licensor** is the entity offering these terms, and the **software** is the
|
|
76
|
+
software the licensor makes available under these terms, including any portion
|
|
77
|
+
of it.
|
|
78
|
+
|
|
79
|
+
**you** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**your company** is any legal entity, sole proprietorship, or other kind of
|
|
82
|
+
organization that you work for, plus all organizations that have control over,
|
|
83
|
+
are under the control of, or are under common control with that
|
|
84
|
+
organization. **control** means ownership of substantially all the assets of an
|
|
85
|
+
entity, or the power to direct its management and policies by vote, contract, or
|
|
86
|
+
otherwise. Control can be direct or indirect.
|
|
87
|
+
|
|
88
|
+
**your licenses** are all the licenses granted to you for the software under
|
|
89
|
+
these terms.
|
|
90
|
+
|
|
91
|
+
**use** means anything you do with the software requiring one of your licenses.
|
|
92
|
+
|
|
93
|
+
**trademark** means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @aigne/core
|
|
2
|
+
|
|
3
|
+
[](https://star-history.com/#AIGNE-io/aigne-framework)
|
|
4
|
+
[](https://github.com/AIGNE-io/aigne-framework/issues)
|
|
5
|
+
[](https://codecov.io/gh/AIGNE-io/aigne-framework)
|
|
6
|
+
[](https://www.npmjs.com/package/@aigne/core)
|
|
7
|
+
[](https://github.com/AIGNE-io/aigne-framework/blob/main/LICENSE)
|
|
8
|
+
|
|
9
|
+
**English** | [中文](README.zh.md)
|
|
10
|
+
|
|
11
|
+
Core library of [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework) for building AI-powered applications.
|
|
12
|
+
|
|
13
|
+
## Introduction
|
|
14
|
+
|
|
15
|
+
`@aigne/core` is the foundation component of [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework), providing the essential modules and tools needed to build AI-driven applications. This package implements the core functionalities of the framework, including agent systems, execution engines, model integrations, and workflow pattern support.
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- **Multiple AI Model Support**: Built-in support for OpenAI, Gemini, Claude, and other mainstream AI models, easily extensible to support additional models
|
|
20
|
+
- **Agent System**: Powerful agent abstractions supporting AI agents, function agents, MCP agents, and more
|
|
21
|
+
- **Execution Engine**: Flexible execution engine handling communication between agents and workflow execution
|
|
22
|
+
- **Workflow Patterns**: Support for sequential, concurrent, routing, handoff, and other workflow patterns
|
|
23
|
+
- **MCP Protocol Integration**: Seamless integration with external systems through the Model Context Protocol
|
|
24
|
+
- **TypeScript Support**: Comprehensive type definitions providing an excellent development experience
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
# Using npm
|
|
30
|
+
npm install @aigne/core
|
|
31
|
+
|
|
32
|
+
# Using yarn
|
|
33
|
+
yarn add @aigne/core
|
|
34
|
+
|
|
35
|
+
# Using pnpm
|
|
36
|
+
pnpm add @aigne/core
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Basic Usage
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { AIAgent, ExecutionEngine } from "@aigne/core";
|
|
43
|
+
import { OpenAIChatModel } from "@aigne/core/models/openai-chat-model.js";
|
|
44
|
+
|
|
45
|
+
// Create AI model instance
|
|
46
|
+
const model = new OpenAIChatModel({
|
|
47
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
48
|
+
model: process.env.DEFAULT_CHAT_MODEL || "gpt-4-turbo",
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Create AI agent
|
|
52
|
+
const agent = AIAgent.from({
|
|
53
|
+
name: "Assistant",
|
|
54
|
+
instructions: "You are a helpful assistant.",
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Create execution engine
|
|
58
|
+
const engine = new ExecutionEngine({ model });
|
|
59
|
+
|
|
60
|
+
// Use the execution engine to call the agent
|
|
61
|
+
const userAgent = await engine.call(agent);
|
|
62
|
+
|
|
63
|
+
// Send a message to the agent
|
|
64
|
+
const response = await userAgent.call("Hello, can you help me write a short article?");
|
|
65
|
+
console.log(response);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Module Structure
|
|
69
|
+
|
|
70
|
+
- `agents/`: Agent implementations, including AI agents, function agents, MCP agents, etc.
|
|
71
|
+
- `execution-engine/`: Execution engine implementation
|
|
72
|
+
- `loader/`: Loader-related functionality
|
|
73
|
+
- `models/`: Integration with various AI models
|
|
74
|
+
- `prompt/`: Prompt handling functionality
|
|
75
|
+
- `utils/`: Utility functions and helper methods
|
|
76
|
+
|
|
77
|
+
## Documentation
|
|
78
|
+
|
|
79
|
+
For more detailed API documentation, please refer to:
|
|
80
|
+
|
|
81
|
+
- [Agent API](../../docs/apis/agent-api.md)
|
|
82
|
+
- [AI Agent API](../../docs/apis/ai-agent-api.md)
|
|
83
|
+
- [Function Agent API](../../docs/apis/function-agent-api.md)
|
|
84
|
+
- [MCP Agent API](../../docs/apis/mcp-agent-api.md)
|
|
85
|
+
- [Execution Engine API](../../docs/apis/execution-engine-api.md)
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
Elastic-2.0
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @aigne/core
|
|
2
|
+
|
|
3
|
+
[](https://star-history.com/#AIGNE-io/aigne-framework)
|
|
4
|
+
[](https://github.com/AIGNE-io/aigne-framework/issues)
|
|
5
|
+
[](https://codecov.io/gh/AIGNE-io/aigne-framework)
|
|
6
|
+
[](https://www.npmjs.com/package/@aigne/core)
|
|
7
|
+
[](https://github.com/AIGNE-io/aigne-framework/blob/main/LICENSE)
|
|
8
|
+
|
|
9
|
+
[English](README.md) | **中文**
|
|
10
|
+
|
|
11
|
+
[AIGNE 框架](https://github.com/AIGNE-io/aigne-framework)的核心库,用于构建 AI 驱动的应用程序。
|
|
12
|
+
|
|
13
|
+
## 简介
|
|
14
|
+
|
|
15
|
+
`@aigne/core` 是 [AIGNE 框架](https://github.com/AIGNE-io/aigne-framework)的基础核心组件,提供了构建 AI 驱动应用程序所需的基本模块和工具。该包实现了框架的核心功能,包括代理系统、执行引擎、模型集成以及工作流模式支持等。
|
|
16
|
+
|
|
17
|
+
## 特性
|
|
18
|
+
|
|
19
|
+
- **多种 AI 模型支持**:内置支持 OpenAI、Gemini、Claude 等主流 AI 模型,可轻松扩展支持其他模型
|
|
20
|
+
- **代理系统**:强大的代理抽象,支持 AI 代理、函数代理、MCP 代理等多种类型
|
|
21
|
+
- **执行引擎**:灵活的执行引擎,处理代理之间的通信和工作流执行
|
|
22
|
+
- **工作流模式**:支持顺序、并发、路由、交接等多种工作流模式
|
|
23
|
+
- **MCP 协议集成**:通过模型上下文协议(Model Context Protocol)实现与外部系统的无缝集成
|
|
24
|
+
- **TypeScript 支持**:完善的类型定义,提供良好的开发体验
|
|
25
|
+
|
|
26
|
+
## 安装
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
# 使用 npm
|
|
30
|
+
npm install @aigne/core
|
|
31
|
+
|
|
32
|
+
# 使用 yarn
|
|
33
|
+
yarn add @aigne/core
|
|
34
|
+
|
|
35
|
+
# 使用 pnpm
|
|
36
|
+
pnpm add @aigne/core
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## 基本用法
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { AIAgent, ExecutionEngine } from "@aigne/core";
|
|
43
|
+
import { OpenAIChatModel } from "@aigne/core/models/openai-chat-model.js";
|
|
44
|
+
|
|
45
|
+
// 创建 AI 模型实例
|
|
46
|
+
const model = new OpenAIChatModel({
|
|
47
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
48
|
+
model: process.env.DEFAULT_CHAT_MODEL || "gpt-4-turbo",
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// 创建 AI 代理
|
|
52
|
+
const agent = AIAgent.from({
|
|
53
|
+
name: "助手",
|
|
54
|
+
instructions: "你是一个有帮助的助手。",
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// 创建执行引擎
|
|
58
|
+
const engine = new ExecutionEngine({ model });
|
|
59
|
+
|
|
60
|
+
// 使用执行引擎调用代理
|
|
61
|
+
const userAgent = await engine.call(agent);
|
|
62
|
+
|
|
63
|
+
// 向代理发送消息
|
|
64
|
+
const response = await userAgent.call("你好,能帮我写一篇短文吗?");
|
|
65
|
+
console.log(response);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 模块结构
|
|
69
|
+
|
|
70
|
+
- `agents/`: 代理相关的实现,包括 AI 代理、函数代理、MCP 代理等
|
|
71
|
+
- `execution-engine/`: 执行引擎的实现
|
|
72
|
+
- `loader/`: 加载器相关功能
|
|
73
|
+
- `models/`: 各种 AI 模型的集成
|
|
74
|
+
- `prompt/`: 提示词处理相关功能
|
|
75
|
+
- `utils/`: 工具函数和辅助方法
|
|
76
|
+
|
|
77
|
+
## 文档
|
|
78
|
+
|
|
79
|
+
更多详细的 API 文档,请参考:
|
|
80
|
+
|
|
81
|
+
- [Agent API](../../docs/apis/agent-api.zh.md)
|
|
82
|
+
- [AI Agent API](../../docs/apis/ai-agent-api.zh.md)
|
|
83
|
+
- [Function Agent API](../../docs/apis/function-agent-api.zh.md)
|
|
84
|
+
- [MCP Agent API](../../docs/apis/mcp-agent-api.zh.md)
|
|
85
|
+
- [Execution Engine API](../../docs/apis/execution-engine-api.zh.md)
|
|
86
|
+
|
|
87
|
+
## 协议
|
|
88
|
+
|
|
89
|
+
Elastic-2.0
|
|
@@ -72,6 +72,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
72
72
|
export type AgentResponse<T> = T | AgentResponseStream<T>;
|
|
73
73
|
export type AgentResponseStream<T> = ReadableStream<AgentResponseChunk<T>>;
|
|
74
74
|
export type AgentResponseChunk<T> = AgentResponseDelta<T>;
|
|
75
|
+
export declare function isEmptyChunk<T>(chunk: AgentResponseChunk<T>): boolean;
|
|
75
76
|
export interface AgentResponseDelta<T> {
|
|
76
77
|
delta: {
|
|
77
78
|
text?: Partial<{
|
package/lib/cjs/agents/agent.js
CHANGED
|
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.FunctionAgent = exports.Agent = void 0;
|
|
37
|
+
exports.isEmptyChunk = isEmptyChunk;
|
|
37
38
|
const node_util_1 = require("node:util");
|
|
38
39
|
const zod_1 = require("zod");
|
|
39
40
|
const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
|
|
@@ -214,6 +215,9 @@ class Agent {
|
|
|
214
215
|
}
|
|
215
216
|
}
|
|
216
217
|
exports.Agent = Agent;
|
|
218
|
+
function isEmptyChunk(chunk) {
|
|
219
|
+
return (0, type_utils_js_1.isEmpty)(chunk.delta.json) && (0, type_utils_js_1.isEmpty)(chunk.delta.text);
|
|
220
|
+
}
|
|
217
221
|
function checkAgentInputOutputSchema(schema) {
|
|
218
222
|
if (!(schema instanceof zod_1.ZodObject) && typeof schema !== "function") {
|
|
219
223
|
throw new Error("schema must be a zod object or function return a zod object ");
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import type { Context } from "../execution-engine/context.js";
|
|
3
3
|
import { ChatModel } from "../models/chat-model.js";
|
|
4
|
+
import type { ChatModelInput } from "../models/chat-model.js";
|
|
4
5
|
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
5
6
|
import { Agent, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
6
7
|
import { type TransferAgentOutput } from "./types.js";
|
|
@@ -28,35 +29,35 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
|
|
|
28
29
|
disableLogging: z.ZodOptional<z.ZodBoolean>;
|
|
29
30
|
memory: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodAny, z.ZodAny]>>;
|
|
30
31
|
}, "strip", z.ZodTypeAny, {
|
|
31
|
-
description?: string | undefined;
|
|
32
|
-
memory?: any;
|
|
33
|
-
includeInputInOutput?: boolean | undefined;
|
|
34
|
-
subscribeTopic?: string | string[] | undefined;
|
|
35
|
-
publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
|
|
36
32
|
tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
|
|
37
33
|
toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
|
|
38
34
|
name?: string | undefined;
|
|
35
|
+
description?: string | undefined;
|
|
39
36
|
model?: ChatModel | undefined;
|
|
40
37
|
instructions?: string | PromptBuilder | undefined;
|
|
41
38
|
outputKey?: string | undefined;
|
|
42
39
|
enableHistory?: boolean | undefined;
|
|
43
40
|
maxHistoryMessages?: number | undefined;
|
|
44
|
-
disableLogging?: boolean | undefined;
|
|
45
|
-
}, {
|
|
46
|
-
description?: string | undefined;
|
|
47
|
-
memory?: any;
|
|
48
41
|
includeInputInOutput?: boolean | undefined;
|
|
49
42
|
subscribeTopic?: string | string[] | undefined;
|
|
50
43
|
publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
|
|
44
|
+
disableLogging?: boolean | undefined;
|
|
45
|
+
memory?: any;
|
|
46
|
+
}, {
|
|
51
47
|
tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
|
|
52
48
|
toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
|
|
53
49
|
name?: string | undefined;
|
|
50
|
+
description?: string | undefined;
|
|
54
51
|
model?: ChatModel | undefined;
|
|
55
52
|
instructions?: string | PromptBuilder | undefined;
|
|
56
53
|
outputKey?: string | undefined;
|
|
57
54
|
enableHistory?: boolean | undefined;
|
|
58
55
|
maxHistoryMessages?: number | undefined;
|
|
56
|
+
includeInputInOutput?: boolean | undefined;
|
|
57
|
+
subscribeTopic?: string | string[] | undefined;
|
|
58
|
+
publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
|
|
59
59
|
disableLogging?: boolean | undefined;
|
|
60
|
+
memory?: any;
|
|
60
61
|
}>;
|
|
61
62
|
export declare class AIAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
62
63
|
static from<I extends Message, O extends Message>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
|
|
@@ -66,4 +67,5 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
66
67
|
outputKey?: string;
|
|
67
68
|
toolChoice?: AIAgentToolChoice;
|
|
68
69
|
process(input: I, context: Context): AgentProcessAsyncGenerator<O | TransferAgentOutput>;
|
|
70
|
+
processRouter(input: I, model: ChatModel, modelInput: ChatModelInput, context: Context, toolsMap: Map<string, Agent>): AgentProcessAsyncGenerator<O | TransferAgentOutput>;
|
|
69
71
|
}
|
|
@@ -55,18 +55,22 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
55
55
|
const model = context.model ?? this.model;
|
|
56
56
|
if (!model)
|
|
57
57
|
throw new Error("model is required to run AIAgent");
|
|
58
|
-
const { toolAgents,
|
|
58
|
+
const { toolAgents, ...modelInput } = await this.instructions.build({
|
|
59
59
|
agent: this,
|
|
60
60
|
input,
|
|
61
61
|
model,
|
|
62
62
|
context,
|
|
63
63
|
});
|
|
64
64
|
const toolsMap = new Map(toolAgents?.map((i) => [i.name, i]));
|
|
65
|
+
if (this.toolChoice === "router") {
|
|
66
|
+
yield* this.processRouter(input, model, modelInput, context, toolsMap);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
65
69
|
const toolCallMessages = [];
|
|
66
70
|
const outputKey = this.outputKey || prompt_builder_js_1.MESSAGE_KEY;
|
|
67
71
|
for (;;) {
|
|
68
72
|
const modelOutput = {};
|
|
69
|
-
const stream = await context.call(model, { ...modelInput, messages: messages.concat(toolCallMessages) }, { streaming: true });
|
|
73
|
+
const stream = await context.call(model, { ...modelInput, messages: modelInput.messages.concat(toolCallMessages) }, { streaming: true });
|
|
70
74
|
for await (const value of (0, stream_utils_js_1.readableStreamToAsyncIterator)(stream)) {
|
|
71
75
|
if (value.delta.text?.text) {
|
|
72
76
|
yield { delta: { text: { [outputKey]: value.delta.text.text } } };
|
|
@@ -94,15 +98,6 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
94
98
|
// Continue LLM function calling loop if any tools were executed
|
|
95
99
|
if (executedToolCalls.length) {
|
|
96
100
|
toolCallMessages.push(template_js_1.AgentMessageTemplate.from(undefined, executedToolCalls.map(({ call }) => call)).format(), ...executedToolCalls.map(({ call, output }) => template_js_1.ToolMessageTemplate.from(output, call.id).format()));
|
|
97
|
-
// Return the output of the first tool if the toolChoice is "router"
|
|
98
|
-
if (this.toolChoice === "router") {
|
|
99
|
-
const output = executedToolCalls[0]?.output;
|
|
100
|
-
const { supportsParallelToolCalls } = model.getModelCapabilities();
|
|
101
|
-
if (!output || (supportsParallelToolCalls && executedToolCalls.length !== 1)) {
|
|
102
|
-
throw new Error("Router toolChoice requires exactly one tool to be executed");
|
|
103
|
-
}
|
|
104
|
-
return output;
|
|
105
|
-
}
|
|
106
101
|
continue;
|
|
107
102
|
}
|
|
108
103
|
}
|
|
@@ -119,5 +114,16 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
119
114
|
return;
|
|
120
115
|
}
|
|
121
116
|
}
|
|
117
|
+
async *processRouter(input, model, modelInput, context, toolsMap) {
|
|
118
|
+
const { toolCalls: [call] = [], } = await context.call(model, modelInput);
|
|
119
|
+
if (!call) {
|
|
120
|
+
throw new Error("Router toolChoice requires exactly one tool to be executed");
|
|
121
|
+
}
|
|
122
|
+
const tool = toolsMap.get(call.function.name);
|
|
123
|
+
if (!tool)
|
|
124
|
+
throw new Error(`Tool not found: ${call.function.name}`);
|
|
125
|
+
const stream = await context.call(tool, { ...call.function.arguments, ...input }, { streaming: true });
|
|
126
|
+
yield* (0, stream_utils_js_1.readableStreamToAsyncIterator)(stream);
|
|
127
|
+
}
|
|
122
128
|
}
|
|
123
129
|
exports.AIAgent = AIAgent;
|
|
@@ -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;
|
|
@@ -36,6 +36,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
36
36
|
exports.loadAgentFromJsFile = loadAgentFromJsFile;
|
|
37
37
|
const json_schema_to_zod_1 = require("@aigne/json-schema-to-zod");
|
|
38
38
|
const zod_1 = require("zod");
|
|
39
|
+
const camelize_js_1 = require("../utils/camelize.js");
|
|
39
40
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
40
41
|
const schema_js_1 = require("./schema.js");
|
|
41
42
|
const agentJsFileSchema = zod_1.z.object({
|
|
@@ -57,11 +58,9 @@ async function loadAgentFromJsFile(path) {
|
|
|
57
58
|
if (typeof agent !== "function") {
|
|
58
59
|
throw new Error(`Agent file ${path} must export a default function, but got ${typeof agent}`);
|
|
59
60
|
}
|
|
60
|
-
return (0, type_utils_js_1.tryOrThrow)(() => agentJsFileSchema.parse({
|
|
61
|
+
return (0, type_utils_js_1.tryOrThrow)(() => (0, camelize_js_1.customCamelize)(agentJsFileSchema.parse({
|
|
62
|
+
...agent,
|
|
61
63
|
name: agent.agent_name || agent.name,
|
|
62
|
-
description: agent.description,
|
|
63
|
-
input_schema: agent.input_schema,
|
|
64
|
-
output_schema: agent.output_schema,
|
|
65
64
|
fn: agent,
|
|
66
|
-
}), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
|
|
65
|
+
}), { shallowKeys: ["input_schema", "output_schema"] }), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
|
|
67
66
|
}
|
|
@@ -2,21 +2,24 @@ import { type ZodObject, type ZodType, z } from "zod";
|
|
|
2
2
|
export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
3
3
|
type: "ai";
|
|
4
4
|
name: string;
|
|
5
|
-
description?: string | undefined;
|
|
6
5
|
tools?: string[] | undefined;
|
|
6
|
+
description?: string | undefined;
|
|
7
7
|
instructions?: string | undefined;
|
|
8
|
-
|
|
8
|
+
memory?: true | {
|
|
9
|
+
subscribeTopic: string[];
|
|
10
|
+
} | undefined;
|
|
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;
|
|
@@ -5,6 +5,7 @@ const promises_1 = require("node:fs/promises");
|
|
|
5
5
|
const json_schema_to_zod_1 = require("@aigne/json-schema-to-zod");
|
|
6
6
|
const yaml_1 = require("yaml");
|
|
7
7
|
const zod_1 = require("zod");
|
|
8
|
+
const camelize_js_1 = require("../utils/camelize.js");
|
|
8
9
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
9
10
|
const schema_js_1 = require("./schema.js");
|
|
10
11
|
const agentFileSchema = zod_1.z.discriminatedUnion("type", [
|
|
@@ -37,6 +38,15 @@ const agentFileSchema = zod_1.z.discriminatedUnion("type", [
|
|
|
37
38
|
.union([zod_1.z.literal("auto"), zod_1.z.literal("none"), zod_1.z.literal("required"), zod_1.z.literal("router")])
|
|
38
39
|
.nullish()
|
|
39
40
|
.transform((v) => v ?? undefined),
|
|
41
|
+
memory: zod_1.z
|
|
42
|
+
.union([
|
|
43
|
+
zod_1.z.boolean(),
|
|
44
|
+
zod_1.z.object({
|
|
45
|
+
subscribe_topic: zod_1.z.array(zod_1.z.string()),
|
|
46
|
+
}),
|
|
47
|
+
])
|
|
48
|
+
.nullish()
|
|
49
|
+
.transform((v) => v || undefined),
|
|
40
50
|
}),
|
|
41
51
|
zod_1.z.object({
|
|
42
52
|
type: zod_1.z.literal("mcp"),
|
|
@@ -57,6 +67,8 @@ const agentFileSchema = zod_1.z.discriminatedUnion("type", [
|
|
|
57
67
|
async function loadAgentFromYamlFile(path) {
|
|
58
68
|
const raw = await (0, type_utils_js_1.tryOrThrow)(() => (0, promises_1.readFile)(path, "utf8"), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
|
|
59
69
|
const json = await (0, type_utils_js_1.tryOrThrow)(() => (0, yaml_1.parse)(raw), (error) => new Error(`Failed to parse agent definition from ${path}: ${error.message}`));
|
|
60
|
-
const agent = (0, type_utils_js_1.tryOrThrow)(() => agentFileSchema.parse({ ...json, type: json.type ?? "ai" }),
|
|
70
|
+
const agent = (0, type_utils_js_1.tryOrThrow)(() => (0, camelize_js_1.customCamelize)(agentFileSchema.parse({ ...json, type: json.type ?? "ai" }), {
|
|
71
|
+
shallowKeys: ["input_schema", "output_schema"],
|
|
72
|
+
}), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
|
|
61
73
|
return agent;
|
|
62
74
|
}
|
|
@@ -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
|
tools: 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,9 +63,9 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
63
63
|
agents: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
|
|
64
64
|
tools: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString, "many">>>;
|
|
65
65
|
}, "strip", z.ZodTypeAny, {
|
|
66
|
-
description?: string | null | undefined;
|
|
67
66
|
tools?: string[] | null | undefined;
|
|
68
67
|
name?: string | null | undefined;
|
|
68
|
+
description?: string | null | undefined;
|
|
69
69
|
chat_model?: {
|
|
70
70
|
name?: string | null | undefined;
|
|
71
71
|
temperature?: number | null | undefined;
|
|
@@ -76,9 +76,9 @@ declare const aigneFileSchema: z.ZodObject<{
|
|
|
76
76
|
} | null | undefined;
|
|
77
77
|
agents?: string[] | null | undefined;
|
|
78
78
|
}, {
|
|
79
|
-
description?: string | null | undefined;
|
|
80
79
|
tools?: string[] | null | undefined;
|
|
81
80
|
name?: string | null | undefined;
|
|
81
|
+
description?: string | null | undefined;
|
|
82
82
|
chat_model?: string | {
|
|
83
83
|
name?: string | null | undefined;
|
|
84
84
|
temperature?: number | null | undefined;
|
|
@@ -90,9 +90,9 @@ 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
|
tools?: string[] | null | undefined;
|
|
95
94
|
name?: string | null | undefined;
|
|
95
|
+
description?: string | null | undefined;
|
|
96
96
|
chat_model?: {
|
|
97
97
|
name?: string | null | undefined;
|
|
98
98
|
temperature?: number | null | undefined;
|
package/lib/cjs/loader/index.js
CHANGED
|
@@ -39,26 +39,15 @@ async function load(options) {
|
|
|
39
39
|
async function loadAgent(path) {
|
|
40
40
|
if ((0, node_path_1.extname)(path) === ".js") {
|
|
41
41
|
const agent = await (0, agent_js_js_1.loadAgentFromJsFile)(path);
|
|
42
|
-
return agent_js_1.FunctionAgent.from(
|
|
43
|
-
name: agent.name,
|
|
44
|
-
description: agent.description,
|
|
45
|
-
inputSchema: agent.input_schema,
|
|
46
|
-
outputSchema: agent.output_schema,
|
|
47
|
-
fn: agent.fn,
|
|
48
|
-
});
|
|
42
|
+
return agent_js_1.FunctionAgent.from(agent);
|
|
49
43
|
}
|
|
50
44
|
if ((0, node_path_1.extname)(path) === ".yaml" || (0, node_path_1.extname)(path) === ".yml") {
|
|
51
45
|
const agent = await (0, agent_yaml_js_1.loadAgentFromYamlFile)(path);
|
|
52
46
|
if (agent.type === "ai") {
|
|
53
47
|
return ai_agent_js_1.AIAgent.from({
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
inputSchema: agent.input_schema,
|
|
58
|
-
outputSchema: agent.output_schema,
|
|
59
|
-
outputKey: agent.output_key,
|
|
60
|
-
tools: await Promise.all((agent.tools ?? []).map((filename) => loadAgent((0, node_path_1.join)((0, node_path_1.dirname)(path), filename)))),
|
|
61
|
-
toolChoice: agent.tool_choice,
|
|
48
|
+
...agent,
|
|
49
|
+
tools: agent.tools &&
|
|
50
|
+
(await Promise.all(agent.tools.map((filename) => loadAgent((0, node_path_1.join)((0, node_path_1.dirname)(path), filename))))),
|
|
62
51
|
});
|
|
63
52
|
}
|
|
64
53
|
if (agent.type === "mcp") {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import Anthropic from "@anthropic-ai/sdk";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
import type { AgentCallOptions, AgentResponse } from "../agents/agent.js";
|
|
4
|
+
import type { Context } from "../execution-engine/context.js";
|
|
3
5
|
import { ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "./chat-model.js";
|
|
4
6
|
export interface ClaudeChatModelOptions {
|
|
5
7
|
apiKey?: string;
|
|
@@ -60,7 +62,7 @@ export declare class ClaudeChatModel extends ChatModel {
|
|
|
60
62
|
protected _client?: Anthropic;
|
|
61
63
|
get client(): Anthropic;
|
|
62
64
|
get modelOptions(): ChatModelOptions | undefined;
|
|
63
|
-
process(input: ChatModelInput): Promise<ChatModelOutput
|
|
65
|
+
process(input: ChatModelInput, _context: Context, options?: AgentCallOptions): Promise<AgentResponse<ChatModelOutput>>;
|
|
64
66
|
private extractResultFromClaudeStream;
|
|
65
67
|
private requestStructuredOutput;
|
|
66
68
|
}
|