@aigne/core 1.9.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 +25 -0
- package/LICENSE +93 -0
- package/README.md +89 -0
- package/README.zh.md +89 -0
- package/lib/cjs/agents/agent.d.ts +30 -3
- package/lib/cjs/agents/agent.js +51 -22
- package/lib/cjs/agents/ai-agent.d.ts +14 -11
- package/lib/cjs/agents/ai-agent.js +36 -17
- package/lib/cjs/agents/user-agent.d.ts +3 -3
- package/lib/cjs/agents/user-agent.js +14 -8
- package/lib/cjs/execution-engine/context.d.ts +18 -7
- package/lib/cjs/execution-engine/context.js +76 -28
- 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/chat-model.d.ts +4 -0
- package/lib/cjs/models/chat-model.js +6 -0
- 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/open-router-chat-model.d.ts +1 -0
- package/lib/cjs/models/open-router-chat-model.js +1 -0
- package/lib/cjs/models/openai-chat-model.d.ts +12 -3
- package/lib/cjs/models/openai-chat-model.js +110 -46
- 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 +17 -0
- package/lib/cjs/utils/stream-utils.js +165 -0
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/type-utils.js +7 -0
- package/lib/dts/agents/agent.d.ts +30 -3
- package/lib/dts/agents/ai-agent.d.ts +14 -11
- package/lib/dts/agents/user-agent.d.ts +3 -3
- package/lib/dts/execution-engine/context.d.ts +18 -7
- 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/chat-model.d.ts +4 -0
- package/lib/dts/models/claude-chat-model.d.ts +3 -1
- package/lib/dts/models/open-router-chat-model.d.ts +1 -0
- package/lib/dts/models/openai-chat-model.d.ts +12 -3
- package/lib/dts/utils/camelize.d.ts +13 -0
- package/lib/dts/utils/stream-utils.d.ts +17 -0
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/esm/agents/agent.d.ts +30 -3
- package/lib/esm/agents/agent.js +51 -23
- package/lib/esm/agents/ai-agent.d.ts +14 -11
- package/lib/esm/agents/ai-agent.js +37 -18
- package/lib/esm/agents/user-agent.d.ts +3 -3
- package/lib/esm/agents/user-agent.js +15 -9
- package/lib/esm/execution-engine/context.d.ts +18 -7
- package/lib/esm/execution-engine/context.js +78 -30
- 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/chat-model.d.ts +4 -0
- package/lib/esm/models/chat-model.js +6 -0
- 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/open-router-chat-model.d.ts +1 -0
- package/lib/esm/models/open-router-chat-model.js +1 -0
- package/lib/esm/models/openai-chat-model.d.ts +12 -3
- package/lib/esm/models/openai-chat-model.js +110 -45
- 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 +17 -0
- package/lib/esm/utils/stream-utils.js +149 -0
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/type-utils.js +6 -0
- package/package.json +4 -2
package/CHANGELOG.md
CHANGED
|
@@ -22,6 +22,31 @@
|
|
|
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
|
+
|
|
38
|
+
## [1.10.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.9.0...core-v1.10.0) (2025-04-22)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### Features
|
|
42
|
+
|
|
43
|
+
* **stream:** add streaming output support for agent ([#73](https://github.com/AIGNE-io/aigne-framework/issues/73)) ([5f3ea4b](https://github.com/AIGNE-io/aigne-framework/commit/5f3ea4bccda7c8c457d6e9518b3d6a8b254ec041))
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
### Bug Fixes
|
|
47
|
+
|
|
48
|
+
* **core:** support dynamic model capability detection ([#72](https://github.com/AIGNE-io/aigne-framework/issues/72)) ([9d56d98](https://github.com/AIGNE-io/aigne-framework/commit/9d56d9885778962e5bef806445ad8c4d199f2c65))
|
|
49
|
+
|
|
25
50
|
## [1.9.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.8.0...core-v1.9.0) (2025-04-20)
|
|
26
51
|
|
|
27
52
|
|
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
|
|
@@ -19,6 +19,9 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
19
19
|
disableEvents?: boolean;
|
|
20
20
|
memory?: AgentMemory | AgentMemoryOptions | true;
|
|
21
21
|
}
|
|
22
|
+
export interface AgentCallOptions {
|
|
23
|
+
streaming?: boolean;
|
|
24
|
+
}
|
|
22
25
|
export declare abstract class Agent<I extends Message = Message, O extends Message = Message> {
|
|
23
26
|
constructor({ inputSchema, outputSchema, ...options }: AgentOptions<I, O>);
|
|
24
27
|
readonly memory?: AgentMemory;
|
|
@@ -50,14 +53,38 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
50
53
|
get isCallable(): boolean;
|
|
51
54
|
private checkContextStatus;
|
|
52
55
|
private newDefaultContext;
|
|
53
|
-
call(input: I | string, context
|
|
56
|
+
call(input: I | string, context: Context | undefined, options: AgentCallOptions & {
|
|
57
|
+
streaming: true;
|
|
58
|
+
}): Promise<AgentResponseStream<O>>;
|
|
59
|
+
call(input: I | string, context?: Context, options?: AgentCallOptions & {
|
|
60
|
+
streaming?: false;
|
|
61
|
+
}): Promise<O>;
|
|
62
|
+
call(input: I | string, context?: Context, options?: AgentCallOptions): Promise<AgentResponse<O>>;
|
|
63
|
+
private processAgentOutput;
|
|
64
|
+
private processAgentError;
|
|
54
65
|
protected checkUsageAgentCalls(context: Context): void;
|
|
55
66
|
protected preprocess(_: I, context: Context): void;
|
|
56
67
|
protected postprocess(input: I, output: O, context: Context): void;
|
|
57
|
-
abstract process(input: I, context: Context):
|
|
68
|
+
abstract process(input: I, context: Context, options?: AgentCallOptions): AgentProcessResult<O | TransferAgentOutput>;
|
|
58
69
|
shutdown(): Promise<void>;
|
|
59
70
|
[inspect.custom](): string;
|
|
60
71
|
}
|
|
72
|
+
export type AgentResponse<T> = T | AgentResponseStream<T>;
|
|
73
|
+
export type AgentResponseStream<T> = ReadableStream<AgentResponseChunk<T>>;
|
|
74
|
+
export type AgentResponseChunk<T> = AgentResponseDelta<T>;
|
|
75
|
+
export declare function isEmptyChunk<T>(chunk: AgentResponseChunk<T>): boolean;
|
|
76
|
+
export interface AgentResponseDelta<T> {
|
|
77
|
+
delta: {
|
|
78
|
+
text?: Partial<{
|
|
79
|
+
[key in keyof T as Extract<T[key], string> extends string ? key : never]: string;
|
|
80
|
+
}> | {
|
|
81
|
+
[key: string]: string;
|
|
82
|
+
};
|
|
83
|
+
json?: Partial<T>;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O> | undefined | void>;
|
|
87
|
+
export type AgentProcessResult<O extends Message> = Promise<AgentResponse<O>> | AgentProcessAsyncGenerator<O>;
|
|
61
88
|
export type AgentInputOutputSchema<I extends Message = Message> = ZodType<I> | ((agent: Agent) => ZodType<I>);
|
|
62
89
|
export interface FunctionAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
63
90
|
fn?: FunctionAgentFn<I, O>;
|
|
@@ -66,6 +93,6 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
|
|
|
66
93
|
static from<I extends Message, O extends Message>(options: FunctionAgentOptions<I, O> | FunctionAgentFn<I, O>): FunctionAgent<I, O>;
|
|
67
94
|
constructor(options: FunctionAgentOptions<I, O>);
|
|
68
95
|
fn: FunctionAgentFn<I, O>;
|
|
69
|
-
process(input: I, context: Context): Promise<
|
|
96
|
+
process(input: I, context: Context, options?: AgentCallOptions): Promise<AgentResponse<O | TransferAgentOutput>>;
|
|
70
97
|
}
|
|
71
98
|
export type FunctionAgentFn<I extends Message = Message, O extends Message = Message> = (input: I, context: Context) => O | Promise<O> | Agent | Promise<Agent>;
|
package/lib/cjs/agents/agent.js
CHANGED
|
@@ -34,10 +34,12 @@ 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");
|
|
40
41
|
const logger_js_1 = require("../utils/logger.js");
|
|
42
|
+
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
41
43
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
42
44
|
const memory_js_1 = require("./memory.js");
|
|
43
45
|
const types_js_1 = require("./types.js");
|
|
@@ -130,7 +132,7 @@ class Agent {
|
|
|
130
132
|
async newDefaultContext() {
|
|
131
133
|
return Promise.resolve().then(() => __importStar(require("../execution-engine/context.js"))).then((m) => new m.ExecutionContext());
|
|
132
134
|
}
|
|
133
|
-
async call(input, context) {
|
|
135
|
+
async call(input, context, options) {
|
|
134
136
|
const ctx = context ?? (await this.newDefaultContext());
|
|
135
137
|
const message = typeof input === "string" ? (0, prompt_builder_js_1.createMessage)(input) : input;
|
|
136
138
|
logger_js_1.logger.core("Call agent %s started with input: %O", this.name, input);
|
|
@@ -140,27 +142,51 @@ class Agent {
|
|
|
140
142
|
const parsedInput = (0, type_utils_js_1.checkArguments)(`Agent ${this.name} input`, this.inputSchema, message);
|
|
141
143
|
this.preprocess(parsedInput, ctx);
|
|
142
144
|
this.checkContextStatus(ctx);
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
return
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
145
|
+
const response = await this.process(parsedInput, ctx, options);
|
|
146
|
+
if (options?.streaming) {
|
|
147
|
+
const stream = response instanceof ReadableStream
|
|
148
|
+
? response
|
|
149
|
+
: (0, stream_utils_js_1.isAsyncGenerator)(response)
|
|
150
|
+
? (0, stream_utils_js_1.asyncGeneratorToReadableStream)(response)
|
|
151
|
+
: (0, stream_utils_js_1.objectToAgentResponseStream)(response);
|
|
152
|
+
return (0, stream_utils_js_1.onAgentResponseStreamEnd)(stream, async (result) => {
|
|
153
|
+
return await this.processAgentOutput(parsedInput, result, ctx);
|
|
154
|
+
}, {
|
|
155
|
+
errorCallback: (error) => {
|
|
156
|
+
try {
|
|
157
|
+
this.processAgentError(error, ctx);
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
return error;
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return await this.processAgentOutput(parsedInput, response instanceof ReadableStream
|
|
166
|
+
? await (0, stream_utils_js_1.agentResponseStreamToObject)(response)
|
|
167
|
+
: (0, stream_utils_js_1.isAsyncGenerator)(response)
|
|
168
|
+
? await (0, stream_utils_js_1.agentResponseStreamToObject)(response)
|
|
169
|
+
: response, ctx);
|
|
156
170
|
}
|
|
157
171
|
catch (error) {
|
|
158
|
-
|
|
159
|
-
if (!this.disableEvents)
|
|
160
|
-
ctx.emit("agentFailed", { agent: this, error });
|
|
161
|
-
throw error;
|
|
172
|
+
this.processAgentError(error, ctx);
|
|
162
173
|
}
|
|
163
174
|
}
|
|
175
|
+
async processAgentOutput(input, output, context) {
|
|
176
|
+
const parsedOutput = (0, type_utils_js_1.checkArguments)(`Agent ${this.name} output`, this.outputSchema, output);
|
|
177
|
+
const finalOutput = this.includeInputInOutput ? { ...input, ...parsedOutput } : parsedOutput;
|
|
178
|
+
this.postprocess(input, finalOutput, context);
|
|
179
|
+
logger_js_1.logger.core("Call agent %s succeed with output: %O", this.name, finalOutput);
|
|
180
|
+
if (!this.disableEvents)
|
|
181
|
+
context.emit("agentSucceed", { agent: this, output: finalOutput });
|
|
182
|
+
return finalOutput;
|
|
183
|
+
}
|
|
184
|
+
processAgentError(error, context) {
|
|
185
|
+
logger_js_1.logger.core("Call agent %s failed with error: %O", this.name, error);
|
|
186
|
+
if (!this.disableEvents)
|
|
187
|
+
context.emit("agentFailed", { agent: this, error });
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
164
190
|
checkUsageAgentCalls(context) {
|
|
165
191
|
const { limits, usage } = context;
|
|
166
192
|
if (limits?.maxAgentCalls && usage.agentCalls >= limits.maxAgentCalls) {
|
|
@@ -189,6 +215,9 @@ class Agent {
|
|
|
189
215
|
}
|
|
190
216
|
}
|
|
191
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
|
+
}
|
|
192
221
|
function checkAgentInputOutputSchema(schema) {
|
|
193
222
|
if (!(schema instanceof zod_1.ZodObject) && typeof schema !== "function") {
|
|
194
223
|
throw new Error("schema must be a zod object or function return a zod object ");
|
|
@@ -203,12 +232,12 @@ class FunctionAgent extends Agent {
|
|
|
203
232
|
this.fn = options.fn ?? (() => ({}));
|
|
204
233
|
}
|
|
205
234
|
fn;
|
|
206
|
-
async process(input, context) {
|
|
207
|
-
|
|
235
|
+
async process(input, context, options) {
|
|
236
|
+
let result = await this.fn(input, context);
|
|
208
237
|
if (result instanceof Agent) {
|
|
209
|
-
|
|
238
|
+
result = (0, types_js_1.transferToAgentOutput)(result);
|
|
210
239
|
}
|
|
211
|
-
return result;
|
|
240
|
+
return options?.streaming ? (0, stream_utils_js_1.objectToAgentResponseStream)(result) : result;
|
|
212
241
|
}
|
|
213
242
|
}
|
|
214
243
|
exports.FunctionAgent = FunctionAgent;
|
|
@@ -1,8 +1,10 @@
|
|
|
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
|
-
import { Agent, type AgentOptions, type Message } from "./agent.js";
|
|
6
|
+
import { Agent, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
7
|
+
import { type TransferAgentOutput } from "./types.js";
|
|
6
8
|
export interface AIAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
7
9
|
model?: ChatModel;
|
|
8
10
|
instructions?: string | PromptBuilder;
|
|
@@ -27,35 +29,35 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
|
|
|
27
29
|
disableLogging: z.ZodOptional<z.ZodBoolean>;
|
|
28
30
|
memory: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodAny, z.ZodAny]>>;
|
|
29
31
|
}, "strip", z.ZodTypeAny, {
|
|
30
|
-
description?: string | undefined;
|
|
31
|
-
memory?: any;
|
|
32
|
-
includeInputInOutput?: boolean | undefined;
|
|
33
|
-
subscribeTopic?: string | string[] | undefined;
|
|
34
|
-
publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
|
|
35
32
|
tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
|
|
36
33
|
toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
|
|
37
34
|
name?: string | undefined;
|
|
35
|
+
description?: string | undefined;
|
|
38
36
|
model?: ChatModel | undefined;
|
|
39
37
|
instructions?: string | PromptBuilder | undefined;
|
|
40
38
|
outputKey?: string | undefined;
|
|
41
39
|
enableHistory?: boolean | undefined;
|
|
42
40
|
maxHistoryMessages?: number | undefined;
|
|
43
|
-
disableLogging?: boolean | undefined;
|
|
44
|
-
}, {
|
|
45
|
-
description?: string | undefined;
|
|
46
|
-
memory?: any;
|
|
47
41
|
includeInputInOutput?: boolean | undefined;
|
|
48
42
|
subscribeTopic?: string | string[] | undefined;
|
|
49
43
|
publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
|
|
44
|
+
disableLogging?: boolean | undefined;
|
|
45
|
+
memory?: any;
|
|
46
|
+
}, {
|
|
50
47
|
tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
|
|
51
48
|
toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
|
|
52
49
|
name?: string | undefined;
|
|
50
|
+
description?: string | undefined;
|
|
53
51
|
model?: ChatModel | undefined;
|
|
54
52
|
instructions?: string | PromptBuilder | undefined;
|
|
55
53
|
outputKey?: string | undefined;
|
|
56
54
|
enableHistory?: boolean | undefined;
|
|
57
55
|
maxHistoryMessages?: number | undefined;
|
|
56
|
+
includeInputInOutput?: boolean | undefined;
|
|
57
|
+
subscribeTopic?: string | string[] | undefined;
|
|
58
|
+
publishTopic?: string | string[] | ((...args: unknown[]) => unknown) | undefined;
|
|
58
59
|
disableLogging?: boolean | undefined;
|
|
60
|
+
memory?: any;
|
|
59
61
|
}>;
|
|
60
62
|
export declare class AIAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
61
63
|
static from<I extends Message, O extends Message>(options: AIAgentOptions<I, O>): AIAgent<I, O>;
|
|
@@ -64,5 +66,6 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
64
66
|
instructions: PromptBuilder;
|
|
65
67
|
outputKey?: string;
|
|
66
68
|
toolChoice?: AIAgentToolChoice;
|
|
67
|
-
process(input: I, context: Context):
|
|
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>;
|
|
68
71
|
}
|
|
@@ -5,6 +5,8 @@ const zod_1 = require("zod");
|
|
|
5
5
|
const chat_model_js_1 = require("../models/chat-model.js");
|
|
6
6
|
const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
|
|
7
7
|
const template_js_1 = require("../prompt/template.js");
|
|
8
|
+
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
9
|
+
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
8
10
|
const agent_js_1 = require("./agent.js");
|
|
9
11
|
const types_js_1 = require("./types.js");
|
|
10
12
|
exports.aiAgentToolChoiceSchema = zod_1.z.union([
|
|
@@ -49,23 +51,35 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
49
51
|
instructions;
|
|
50
52
|
outputKey;
|
|
51
53
|
toolChoice;
|
|
52
|
-
async process(input, context) {
|
|
54
|
+
async *process(input, context) {
|
|
53
55
|
const model = context.model ?? this.model;
|
|
54
56
|
if (!model)
|
|
55
57
|
throw new Error("model is required to run AIAgent");
|
|
56
|
-
const { toolAgents,
|
|
58
|
+
const { toolAgents, ...modelInput } = await this.instructions.build({
|
|
57
59
|
agent: this,
|
|
58
60
|
input,
|
|
59
61
|
model,
|
|
60
62
|
context,
|
|
61
63
|
});
|
|
62
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
|
+
}
|
|
63
69
|
const toolCallMessages = [];
|
|
70
|
+
const outputKey = this.outputKey || prompt_builder_js_1.MESSAGE_KEY;
|
|
64
71
|
for (;;) {
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
72
|
+
const modelOutput = {};
|
|
73
|
+
const stream = await context.call(model, { ...modelInput, messages: modelInput.messages.concat(toolCallMessages) }, { streaming: true });
|
|
74
|
+
for await (const value of (0, stream_utils_js_1.readableStreamToAsyncIterator)(stream)) {
|
|
75
|
+
if (value.delta.text?.text) {
|
|
76
|
+
yield { delta: { text: { [outputKey]: value.delta.text.text } } };
|
|
77
|
+
}
|
|
78
|
+
if (value.delta.json) {
|
|
79
|
+
Object.assign(modelOutput, value.delta.json);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const { toolCalls, json, text } = modelOutput;
|
|
69
83
|
if (toolCalls?.length) {
|
|
70
84
|
const executedToolCalls = [];
|
|
71
85
|
// Execute tools
|
|
@@ -84,14 +98,6 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
84
98
|
// Continue LLM function calling loop if any tools were executed
|
|
85
99
|
if (executedToolCalls.length) {
|
|
86
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()));
|
|
87
|
-
// Return the output of the first tool if the toolChoice is "router"
|
|
88
|
-
if (this.toolChoice === "router") {
|
|
89
|
-
const output = executedToolCalls[0]?.output;
|
|
90
|
-
if (!output || executedToolCalls.length !== 1) {
|
|
91
|
-
throw new Error("Router toolChoice requires exactly one tool to be executed");
|
|
92
|
-
}
|
|
93
|
-
return output;
|
|
94
|
-
}
|
|
95
101
|
continue;
|
|
96
102
|
}
|
|
97
103
|
}
|
|
@@ -99,12 +105,25 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
99
105
|
if (modelInput.responseFormat?.type === "json_schema") {
|
|
100
106
|
Object.assign(result, json);
|
|
101
107
|
}
|
|
102
|
-
else {
|
|
103
|
-
const outputKey = this.outputKey || prompt_builder_js_1.MESSAGE_KEY;
|
|
108
|
+
else if (text) {
|
|
104
109
|
Object.assign(result, { [outputKey]: text });
|
|
105
110
|
}
|
|
106
|
-
|
|
111
|
+
if (!(0, type_utils_js_1.isEmpty)(result)) {
|
|
112
|
+
yield { delta: { json: result } };
|
|
113
|
+
}
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
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");
|
|
107
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);
|
|
108
127
|
}
|
|
109
128
|
}
|
|
110
129
|
exports.AIAgent = AIAgent;
|
|
@@ -2,7 +2,7 @@ import { ReadableStream } from "node:stream/web";
|
|
|
2
2
|
import { type Context, type Runnable } from "../execution-engine/context.js";
|
|
3
3
|
import type { MessagePayload } from "../execution-engine/message-queue.js";
|
|
4
4
|
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
5
|
-
import { Agent, type AgentOptions, type Message } from "./agent.js";
|
|
5
|
+
import { Agent, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
6
6
|
export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
7
7
|
context: Context;
|
|
8
8
|
process?: (input: I, context: Context) => PromiseOrValue<O>;
|
|
@@ -14,8 +14,8 @@ export declare class UserAgent<I extends Message = Message, O extends Message =
|
|
|
14
14
|
context: Context;
|
|
15
15
|
private _process?;
|
|
16
16
|
private activeAgent?;
|
|
17
|
-
call
|
|
18
|
-
process(input: I, context: Context):
|
|
17
|
+
call: Agent<I, O>["call"];
|
|
18
|
+
process(input: I, context: Context): AgentProcessAsyncGenerator<O>;
|
|
19
19
|
publish: Context["publish"];
|
|
20
20
|
subscribe: Context["subscribe"];
|
|
21
21
|
unsubscribe: Context["unsubscribe"];
|