formagent-sdk 0.1.1 → 0.1.2
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/package.json +1 -4
- package/docs/README.md +0 -122
- package/docs/agent-core-summary.md +0 -516
- package/docs/api-reference.md +0 -564
- package/docs/getting-started.md +0 -234
- package/docs/mcp-servers.md +0 -465
- package/docs/tools.md +0 -501
- package/examples/01-basic-session.ts +0 -129
- package/examples/02-prompt-function.ts +0 -78
- package/examples/03-streaming.ts +0 -137
- package/examples/04-tools.ts +0 -200
- package/examples/05-mcp-server.ts +0 -216
- package/examples/06-system-prompts.ts +0 -171
- package/examples/07-cost-tracking.ts +0 -215
- package/examples/08-providers.ts +0 -230
- package/examples/09-skills.ts +0 -245
- package/examples/10-full-agent.ts +0 -202
- package/examples/11-user-skills.ts +0 -231
- package/examples/12-builtin-tools.ts +0 -214
- package/examples/13-cli-agent.ts +0 -482
- package/examples/14-structured-output.ts +0 -227
- package/examples/15-hooks.ts +0 -295
- package/examples/README.md +0 -366
- package/examples/_utils.ts +0 -273
- package/src/api.ts +0 -438
- package/src/cli/cli.ts +0 -555
- package/src/cli/index.ts +0 -19
- package/src/cost/index.ts +0 -6
- package/src/cost/tracker.ts +0 -359
- package/src/hooks/index.ts +0 -32
- package/src/hooks/manager.ts +0 -419
- package/src/index.ts +0 -160
- package/src/llm/anthropic.ts +0 -395
- package/src/llm/index.ts +0 -19
- package/src/llm/openai.ts +0 -538
- package/src/llm/resolver.ts +0 -260
- package/src/loop/agent.ts +0 -428
- package/src/prompt/builder.ts +0 -241
- package/src/prompt/claude-md.ts +0 -260
- package/src/prompt/index.ts +0 -43
- package/src/prompt/presets.ts +0 -335
- package/src/session/index.ts +0 -14
- package/src/session/manager.ts +0 -212
- package/src/session/session.ts +0 -871
- package/src/session/storage.ts +0 -162
- package/src/skills/loader.ts +0 -432
- package/src/stream/processor.ts +0 -106
- package/src/tools/builtin/bash.ts +0 -160
- package/src/tools/builtin/edit.ts +0 -119
- package/src/tools/builtin/glob.ts +0 -165
- package/src/tools/builtin/grep.ts +0 -279
- package/src/tools/builtin/index.ts +0 -138
- package/src/tools/builtin/path-guard.ts +0 -68
- package/src/tools/builtin/read.ts +0 -136
- package/src/tools/builtin/todo.ts +0 -137
- package/src/tools/builtin/types.ts +0 -155
- package/src/tools/builtin/webfetch.ts +0 -294
- package/src/tools/builtin/write.ts +0 -72
- package/src/tools/index.ts +0 -86
- package/src/tools/manager.ts +0 -373
- package/src/tools/mcp.ts +0 -345
- package/src/tools/registry.ts +0 -50
- package/src/tools/skill.ts +0 -272
- package/src/tools/tool.ts +0 -308
- package/src/types/core.ts +0 -285
- package/src/types/cost.ts +0 -235
- package/src/types/hooks.ts +0 -275
- package/src/types/index.ts +0 -34
- package/src/types/legacy.ts +0 -334
- package/src/types/prompt.ts +0 -178
- package/src/types/provider.ts +0 -296
- package/src/types/query.ts +0 -169
- package/src/types/session.ts +0 -302
- package/src/types/skill.ts +0 -183
- package/src/types/tool.ts +0 -215
- package/src/types.ts +0 -57
- package/src/utils/env.ts +0 -74
- package/src/utils/events.ts +0 -250
- package/src/utils/frontmatter.ts +0 -298
- package/src/utils/id.ts +0 -138
- package/src/utils/index.ts +0 -39
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "formagent-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "FormAgent SDK - AI Agent framework with streaming support, tool execution, skill management, and hooks system",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -26,9 +26,6 @@
|
|
|
26
26
|
},
|
|
27
27
|
"files": [
|
|
28
28
|
"dist",
|
|
29
|
-
"src",
|
|
30
|
-
"examples",
|
|
31
|
-
"docs/*.md",
|
|
32
29
|
"README.md",
|
|
33
30
|
"LICENSE"
|
|
34
31
|
],
|
package/docs/README.md
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
# formagent-sdk Documentation
|
|
2
|
-
|
|
3
|
-
Welcome to the documentation for `formagent-sdk`, a Claude Agent SDK compatible framework for building AI agents with streaming support, tool execution, and skill management.
|
|
4
|
-
|
|
5
|
-
## Table of Contents
|
|
6
|
-
|
|
7
|
-
- [Getting Started](./getting-started.md) - Quick start guide
|
|
8
|
-
- [API Reference](./api-reference.md) - Complete API documentation
|
|
9
|
-
- [Built-in Tools](./tools.md) - File operations, bash, and more
|
|
10
|
-
- [MCP Servers](./mcp-servers.md) - Model Context Protocol integration
|
|
11
|
-
- [Examples](./examples.md) - Code examples and tutorials
|
|
12
|
-
|
|
13
|
-
## Quick Links
|
|
14
|
-
|
|
15
|
-
### Installation
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
npm install formagent-sdk
|
|
19
|
-
# or
|
|
20
|
-
bun add formagent-sdk
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
### Environment Setup
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
export ANTHROPIC_API_KEY=your-api-key
|
|
27
|
-
# Optional: Custom endpoint
|
|
28
|
-
export ANTHROPIC_BASE_URL=https://your-proxy.com
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### Minimal Example
|
|
32
|
-
|
|
33
|
-
```typescript
|
|
34
|
-
import { createSession, builtinTools } from "formagent-sdk"
|
|
35
|
-
|
|
36
|
-
const session = await createSession({
|
|
37
|
-
model: "claude-sonnet-4-20250514",
|
|
38
|
-
tools: builtinTools,
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
await session.send("List files in the current directory")
|
|
42
|
-
|
|
43
|
-
for await (const event of session.receive()) {
|
|
44
|
-
if (event.type === "text") {
|
|
45
|
-
process.stdout.write(event.text)
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
await session.close()
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## Core Concepts
|
|
53
|
-
|
|
54
|
-
### Sessions
|
|
55
|
-
|
|
56
|
-
Sessions manage conversations with Claude. They handle message history, tool execution, and streaming responses.
|
|
57
|
-
|
|
58
|
-
```typescript
|
|
59
|
-
const session = await createSession({ model: "claude-sonnet-4-20250514" })
|
|
60
|
-
await session.send("Hello!")
|
|
61
|
-
for await (const event of session.receive()) { /* ... */ }
|
|
62
|
-
await session.close()
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### Tools
|
|
66
|
-
|
|
67
|
-
Tools extend Claude's capabilities. The SDK provides built-in tools and supports custom tool definitions.
|
|
68
|
-
|
|
69
|
-
```typescript
|
|
70
|
-
import { builtinTools, tool } from "formagent-sdk"
|
|
71
|
-
import { z } from "zod"
|
|
72
|
-
|
|
73
|
-
// Use built-in tools
|
|
74
|
-
const session = await createSession({ tools: builtinTools })
|
|
75
|
-
|
|
76
|
-
// Or create custom tools
|
|
77
|
-
const myTool = tool({
|
|
78
|
-
name: "my_tool",
|
|
79
|
-
description: "Does something useful",
|
|
80
|
-
schema: z.object({ input: z.string() }),
|
|
81
|
-
execute: async ({ input }) => `Result: ${input}`,
|
|
82
|
-
})
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
### MCP Servers
|
|
86
|
-
|
|
87
|
-
MCP (Model Context Protocol) servers provide a standardized way to expose tools.
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
import { createSdkMcpServer, tool } from "formagent-sdk"
|
|
91
|
-
|
|
92
|
-
const server = createSdkMcpServer({
|
|
93
|
-
name: "my-server",
|
|
94
|
-
version: "1.0.0",
|
|
95
|
-
tools: [myTool],
|
|
96
|
-
})
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
## Architecture
|
|
100
|
-
|
|
101
|
-
```
|
|
102
|
-
formagent-sdk
|
|
103
|
-
├── Session API # Conversation management
|
|
104
|
-
│ ├── createSession() # Create new sessions
|
|
105
|
-
│ ├── send() # Send messages
|
|
106
|
-
│ └── receive() # Stream responses
|
|
107
|
-
├── Tool System # Tool execution
|
|
108
|
-
│ ├── builtinTools # Built-in tools (Bash, Read, Write, etc.)
|
|
109
|
-
│ ├── tool() # Tool definition helper
|
|
110
|
-
│ └── ToolManager # Tool registration and execution
|
|
111
|
-
├── MCP Integration # Model Context Protocol
|
|
112
|
-
│ ├── createSdkMcpServer()
|
|
113
|
-
│ └── MCPServerManager
|
|
114
|
-
└── Providers # LLM providers
|
|
115
|
-
├── AnthropicProvider
|
|
116
|
-
└── OpenAIProvider
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
## Support
|
|
120
|
-
|
|
121
|
-
- [GitHub Issues](https://github.com/anthropics/claude-code/issues)
|
|
122
|
-
- [Examples](../examples/)
|
|
@@ -1,516 +0,0 @@
|
|
|
1
|
-
# AgentCore 项目总结
|
|
2
|
-
|
|
3
|
-
## 项目概述
|
|
4
|
-
|
|
5
|
-
`@opencode/agent-core` 是一个独立的 AI Agent 框架,提供完整的 agent 执行能力,包括消息循环、LLM 调用、工具系统、技能管理和流式响应。
|
|
6
|
-
|
|
7
|
-
## 项目结构
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
./
|
|
11
|
-
├── src/
|
|
12
|
-
│ ├── index.ts # 主入口,导出所有公共 API
|
|
13
|
-
│ ├── types.ts # 核心类型定义
|
|
14
|
-
│ ├── tools/
|
|
15
|
-
│ │ └── registry.ts # 工具注册表
|
|
16
|
-
│ ├── skills/
|
|
17
|
-
│ │ └── loader.ts # 技能加载器
|
|
18
|
-
│ ├── llm/
|
|
19
|
-
│ │ └── openai.ts # OpenAI LLM Provider 实现
|
|
20
|
-
│ ├── loop/
|
|
21
|
-
│ │ └── agent.ts # Agent 主类(消息循环)
|
|
22
|
-
│ └── stream/
|
|
23
|
-
│ └── processor.ts # 流式响应处理器
|
|
24
|
-
├── test/
|
|
25
|
-
│ └── agent-core.test.ts # 单元测试
|
|
26
|
-
├── example.ts # 完整使用示例
|
|
27
|
-
├── package.json # 包配置
|
|
28
|
-
├── tsconfig.json # TypeScript 配置
|
|
29
|
-
└── README.md # 项目文档
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## 核心功能
|
|
33
|
-
|
|
34
|
-
### 1. Agent 类 (`src/loop/agent.ts`)
|
|
35
|
-
|
|
36
|
-
Agent 类是整个框架的核心,负责:
|
|
37
|
-
|
|
38
|
-
**主要职责:**
|
|
39
|
-
- 管理消息历史
|
|
40
|
-
- 执行主循环(自动处理工具调用、生成响应等)
|
|
41
|
-
- 协调 LLM、工具、技能系统
|
|
42
|
-
- 事件发射(流式通知)
|
|
43
|
-
- Doom Loop 检测
|
|
44
|
-
- 上下文压缩
|
|
45
|
-
|
|
46
|
-
**核心方法:**
|
|
47
|
-
```typescript
|
|
48
|
-
class Agent {
|
|
49
|
-
// 启动 agent 执行
|
|
50
|
-
async run(input: AgentInput, options?: AgentOptions): Promise<AgentResult>
|
|
51
|
-
|
|
52
|
-
// 注册事件监听器
|
|
53
|
-
on(event: string, listener: (...args: any[]) => void): this
|
|
54
|
-
|
|
55
|
-
// 访问内部组件
|
|
56
|
-
getMessages(): Message[]
|
|
57
|
-
getToolRegistry(): ToolRegistry
|
|
58
|
-
getSkillLoader(): SkillLoader
|
|
59
|
-
|
|
60
|
-
// 重置状态
|
|
61
|
-
reset(): void
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### 2. ToolRegistry (`src/tools/registry.ts`)
|
|
66
|
-
|
|
67
|
-
工具注册表,管理所有可用工具。
|
|
68
|
-
|
|
69
|
-
**主要职责:**
|
|
70
|
-
- 注册和注销工具
|
|
71
|
-
- 查找和获取工具
|
|
72
|
-
- 执行工具(带权限检查和错误处理)
|
|
73
|
-
- 工具调用事件通知
|
|
74
|
-
|
|
75
|
-
**核心方法:**
|
|
76
|
-
```typescript
|
|
77
|
-
class ToolRegistry {
|
|
78
|
-
register(tool: ToolDefinition): void
|
|
79
|
-
unregister(toolId: string): void
|
|
80
|
-
get(toolId: string): ToolDefinition | undefined
|
|
81
|
-
getAll(): ToolDefinition[]
|
|
82
|
-
clear(): void
|
|
83
|
-
async execute(toolId, input, context): Promise<ToolResult>
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
### 3. SkillLoader (`src/skills/loader.ts`)
|
|
88
|
-
|
|
89
|
-
技能加载器,管理动态技能。
|
|
90
|
-
|
|
91
|
-
**主要职责:**
|
|
92
|
-
- 注册和注销技能
|
|
93
|
-
- 查找和获取技能
|
|
94
|
-
- 搜索技能(按名称或描述)
|
|
95
|
-
- 加载技能内容
|
|
96
|
-
|
|
97
|
-
**核心方法:**
|
|
98
|
-
```typescript
|
|
99
|
-
class SkillLoader {
|
|
100
|
-
register(skill: SkillDefinition): void
|
|
101
|
-
unregister(skillId: string): void
|
|
102
|
-
async load(skillId: string): Promise<SkillDefinition | undefined>
|
|
103
|
-
async loadAll(): Promise<SkillDefinition[]>
|
|
104
|
-
async search(query?: string): Promise<SkillDefinition[]>
|
|
105
|
-
clear(): void
|
|
106
|
-
}
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### 4. OpenAIProvider (`src/llm/openai.ts`)
|
|
110
|
-
|
|
111
|
-
OpenAI LLM Provider 实现。
|
|
112
|
-
|
|
113
|
-
**主要职责:**
|
|
114
|
-
- 创建流式 LLM 调用
|
|
115
|
-
- 处理 SSE (Server-Sent Events) 响应
|
|
116
|
-
- 转换消息格式
|
|
117
|
-
- 支持工具调用
|
|
118
|
-
|
|
119
|
-
**核心方法:**
|
|
120
|
-
```typescript
|
|
121
|
-
class OpenAIProvider implements LLMProvider {
|
|
122
|
-
async stream(config: {
|
|
123
|
-
messages: LLMMessage[]
|
|
124
|
-
tools: ToolDefinition[]
|
|
125
|
-
llmConfig: LLMConfig
|
|
126
|
-
abortSignal: AbortSignal
|
|
127
|
-
onChunk?: (chunk: LLMResponseChunk) => void
|
|
128
|
-
}): Promise<LLMStream>
|
|
129
|
-
}
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
### 5. StreamProcessor (`src/stream/processor.ts`)
|
|
133
|
-
|
|
134
|
-
流式响应处理器,处理 LLM 响应 chunk 并发射事件。
|
|
135
|
-
|
|
136
|
-
**主要职责:**
|
|
137
|
-
- 处理各种 chunk 类型(文本、工具调用、完成等)
|
|
138
|
-
- 累积文本和推理内容
|
|
139
|
-
- 发射适当的事件
|
|
140
|
-
|
|
141
|
-
**核心方法:**
|
|
142
|
-
```typescript
|
|
143
|
-
class StreamProcessor {
|
|
144
|
-
async processChunk(chunk: LLMResponseChunk): Promise<void>
|
|
145
|
-
reset(): void
|
|
146
|
-
getText(): string
|
|
147
|
-
getReasoning(): string
|
|
148
|
-
}
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
### 6. AgentEventEmitter (`src/types.ts`)
|
|
152
|
-
|
|
153
|
-
基于 Node.js EventEmitter 的事件发射器,提供类型安全的事件系统。
|
|
154
|
-
|
|
155
|
-
**支持的事件:**
|
|
156
|
-
- `chunk`: 响应 chunk
|
|
157
|
-
- `tool_call`: 工具调用(开始、结果、错误)
|
|
158
|
-
- `message_complete`: 消息完成
|
|
159
|
-
- `error`: 发生错误
|
|
160
|
-
- `complete`: 整体完成
|
|
161
|
-
|
|
162
|
-
## 使用方式
|
|
163
|
-
|
|
164
|
-
### 基础用法
|
|
165
|
-
|
|
166
|
-
```typescript
|
|
167
|
-
import { createAgent, AgentConfig, UserMessage } from "@opencode/agent-core"
|
|
168
|
-
|
|
169
|
-
// 1. 创建 Agent
|
|
170
|
-
const config: AgentConfig = {
|
|
171
|
-
id: "my-agent",
|
|
172
|
-
name: "My Agent",
|
|
173
|
-
llmConfig: {
|
|
174
|
-
providerId: "openai",
|
|
175
|
-
modelId: "gpt-4",
|
|
176
|
-
apiKey: "your-api-key",
|
|
177
|
-
},
|
|
178
|
-
tools: [
|
|
179
|
-
// 定义工具
|
|
180
|
-
{
|
|
181
|
-
id: "read_file",
|
|
182
|
-
name: "read_file",
|
|
183
|
-
description: "Reads a file",
|
|
184
|
-
parameters: { /* schema */ },
|
|
185
|
-
async execute(input, context) { /* 实现 */ },
|
|
186
|
-
},
|
|
187
|
-
],
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const agent = createAgent(config)
|
|
191
|
-
|
|
192
|
-
// 2. 运行 Agent
|
|
193
|
-
const result = await agent.run(
|
|
194
|
-
{
|
|
195
|
-
sessionId: "session-123",
|
|
196
|
-
userMessage: {
|
|
197
|
-
id: "user-1",
|
|
198
|
-
role: "user",
|
|
199
|
-
timestamp: Date.now(),
|
|
200
|
-
content: [
|
|
201
|
-
{ type: "text", text: "Hello!" },
|
|
202
|
-
],
|
|
203
|
-
},
|
|
204
|
-
abortSignal: new AbortController().signal,
|
|
205
|
-
},
|
|
206
|
-
{
|
|
207
|
-
// 实时事件处理
|
|
208
|
-
onChunk: async (chunk) => {
|
|
209
|
-
console.log("Received chunk:", chunk)
|
|
210
|
-
|
|
211
|
-
if (chunk.type === "text") {
|
|
212
|
-
// 实时显示文本
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (chunk.type === "tool_call") {
|
|
216
|
-
// 显示工具调用进度
|
|
217
|
-
}
|
|
218
|
-
},
|
|
219
|
-
onToolCall: async (event) => {
|
|
220
|
-
console.log("Tool event:", event)
|
|
221
|
-
},
|
|
222
|
-
onMessageComplete: async (message) => {
|
|
223
|
-
console.log("Message complete:", message)
|
|
224
|
-
},
|
|
225
|
-
onComplete: async (result) => {
|
|
226
|
-
console.log("Agent completed:", result)
|
|
227
|
-
},
|
|
228
|
-
onError: async (error) => {
|
|
229
|
-
console.error("Agent error:", error)
|
|
230
|
-
},
|
|
231
|
-
},
|
|
232
|
-
)
|
|
233
|
-
|
|
234
|
-
console.log("Final result:", result)
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### 自定义工具
|
|
238
|
-
|
|
239
|
-
```typescript
|
|
240
|
-
import type { ToolDefinition, ToolContext } from "@opencode/agent-core"
|
|
241
|
-
|
|
242
|
-
const customTool: ToolDefinition = {
|
|
243
|
-
id: "search_api",
|
|
244
|
-
name: "search_api",
|
|
245
|
-
description: "Searches external API",
|
|
246
|
-
parameters: {
|
|
247
|
-
type: "object",
|
|
248
|
-
properties: {
|
|
249
|
-
query: { type: "string", description: "Search query" },
|
|
250
|
-
},
|
|
251
|
-
required: ["query"],
|
|
252
|
-
},
|
|
253
|
-
async execute(input, context) {
|
|
254
|
-
const response = await fetch(`https://api.example.com/search?q=${input.query}`)
|
|
255
|
-
const data = await response.json()
|
|
256
|
-
|
|
257
|
-
return {
|
|
258
|
-
output: JSON.stringify(data.results),
|
|
259
|
-
metadata: { query: input.query },
|
|
260
|
-
}
|
|
261
|
-
},
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
const config: AgentConfig = {
|
|
265
|
-
// ...其他配置
|
|
266
|
-
tools: [customTool],
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
const agent = createAgent(config)
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
### 自定义 LLM Provider
|
|
273
|
-
|
|
274
|
-
```typescript
|
|
275
|
-
import type { LLMProvider, LLMConfig, LLMMessage, ToolDefinition } from "@opencode/agent-core"
|
|
276
|
-
|
|
277
|
-
class CustomProvider implements LLMProvider {
|
|
278
|
-
id = "custom"
|
|
279
|
-
name = "Custom Provider"
|
|
280
|
-
|
|
281
|
-
async stream(config: {
|
|
282
|
-
messages: LLMMessage[]
|
|
283
|
-
tools: ToolDefinition[]
|
|
284
|
-
llmConfig: LLMConfig
|
|
285
|
-
abortSignal: AbortSignal
|
|
286
|
-
}) {
|
|
287
|
-
// 实现自定义的 LLM 调用逻辑
|
|
288
|
-
// 返回异步生成器
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
### 事件监听
|
|
294
|
-
|
|
295
|
-
```typescript
|
|
296
|
-
import { createAgent, AgentConfig, UserMessage } from "@opencode/agent-core"
|
|
297
|
-
|
|
298
|
-
const config: AgentConfig = { /* ... */ }
|
|
299
|
-
const agent = createAgent(config)
|
|
300
|
-
|
|
301
|
-
// 监听所有事件
|
|
302
|
-
agent.on("chunk", (chunk) => {
|
|
303
|
-
if (chunk.type === "text") {
|
|
304
|
-
// 实时更新 UI
|
|
305
|
-
}
|
|
306
|
-
})
|
|
307
|
-
|
|
308
|
-
agent.on("tool_call", (event) => {
|
|
309
|
-
if (event.type === "start") {
|
|
310
|
-
// 显示工具调用开始
|
|
311
|
-
}
|
|
312
|
-
})
|
|
313
|
-
|
|
314
|
-
agent.on("message_complete", (message) => {
|
|
315
|
-
// 更新消息历史 UI
|
|
316
|
-
})
|
|
317
|
-
|
|
318
|
-
agent.on("error", (error) => {
|
|
319
|
-
// 显示错误提示
|
|
320
|
-
})
|
|
321
|
-
|
|
322
|
-
agent.on("complete", (result) => {
|
|
323
|
-
// 更新最终状态
|
|
324
|
-
})
|
|
325
|
-
```
|
|
326
|
-
|
|
327
|
-
## 高级特性
|
|
328
|
-
|
|
329
|
-
### Doom Loop 检测
|
|
330
|
-
|
|
331
|
-
自动检测并阻止无限循环的工具调用:
|
|
332
|
-
|
|
333
|
-
```typescript
|
|
334
|
-
const config: AgentConfig = {
|
|
335
|
-
enableDoomLoopDetection: true,
|
|
336
|
-
doomLoopThreshold: 3, // 同一工具调用3次后触发
|
|
337
|
-
}
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
### 上下文压缩
|
|
341
|
-
|
|
342
|
-
自动压缩对话历史以控制 token 使用:
|
|
343
|
-
|
|
344
|
-
```typescript
|
|
345
|
-
const config: AgentConfig = {
|
|
346
|
-
enableCompaction: true,
|
|
347
|
-
compactionThreshold: 100000, // 100k tokens 后压缩
|
|
348
|
-
}
|
|
349
|
-
```
|
|
350
|
-
|
|
351
|
-
### 最大步数限制
|
|
352
|
-
|
|
353
|
-
防止 agent 运行过多步数:
|
|
354
|
-
|
|
355
|
-
```typescript
|
|
356
|
-
const config: AgentConfig = {
|
|
357
|
-
maxSteps: 50, // 最多50步后停止
|
|
358
|
-
}
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
## 类型安全
|
|
362
|
-
|
|
363
|
-
所有核心类型都在 `src/types.ts` 中定义,包括:
|
|
364
|
-
|
|
365
|
-
- `Message`, `UserMessage`, `AssistantMessage`, `SystemMessage`
|
|
366
|
-
- `ToolDefinition`, `ToolContext`, `ToolResult`
|
|
367
|
-
- `SkillDefinition`
|
|
368
|
-
- `AgentConfig`, `AgentInput`, `AgentOptions`, `AgentResult`
|
|
369
|
-
- `LLMConfig`, `LLMMessage`, `LLMResponseChunk`, `LLMStream`
|
|
370
|
-
- `ToolEvent`, `AgentChunk`, `AgentEventEmitter`
|
|
371
|
-
|
|
372
|
-
## 测试
|
|
373
|
-
|
|
374
|
-
```bash
|
|
375
|
-
# 运行测试
|
|
376
|
-
bun test
|
|
377
|
-
|
|
378
|
-
# 运行示例
|
|
379
|
-
bun run example.ts
|
|
380
|
-
```
|
|
381
|
-
|
|
382
|
-
## 构建和发布
|
|
383
|
-
|
|
384
|
-
```bash
|
|
385
|
-
# 构建
|
|
386
|
-
bun run build
|
|
387
|
-
|
|
388
|
-
# 本地测试
|
|
389
|
-
bun run dev
|
|
390
|
-
|
|
391
|
-
# 发布到 npm
|
|
392
|
-
npm publish
|
|
393
|
-
```
|
|
394
|
-
|
|
395
|
-
## 设计原则
|
|
396
|
-
|
|
397
|
-
1. **最小依赖**: 仅依赖 TypeScript,无其他运行时依赖
|
|
398
|
-
2. **接口优先**: 所有核心组件都通过接口定义,易于扩展
|
|
399
|
-
3. **事件驱动**: 使用 EventEmitter 实现流式通知
|
|
400
|
-
4. **类型安全**: 完整的 TypeScript 支持
|
|
401
|
-
5. **零外部耦合**: 不依赖特定的 LLM 提供商或工具实现
|
|
402
|
-
6. **易于测试**: 简单的单元测试和集成测试
|
|
403
|
-
|
|
404
|
-
## 与 OpenCode 的区别
|
|
405
|
-
|
|
406
|
-
### OpenCode 实现:
|
|
407
|
-
- 深度集成到项目基础设施(文件系统、权限等)
|
|
408
|
-
- 复杂的会话持久化
|
|
409
|
-
- 与 CLI 和 TUI 深度耦合
|
|
410
|
-
- MCP 协议实现
|
|
411
|
-
- ACP (Agent Client Protocol) 实现
|
|
412
|
-
|
|
413
|
-
### AgentCore 实现:
|
|
414
|
-
- 纯粹的 agent 逻辑
|
|
415
|
-
- 可在任何项目中独立使用
|
|
416
|
-
- 只提供核心抽象
|
|
417
|
-
- 需要外部实现 LLM provider 和工具
|
|
418
|
-
- 简单的内存会话管理
|
|
419
|
-
|
|
420
|
-
## 扩展性
|
|
421
|
-
|
|
422
|
-
### 添加自定义工具:
|
|
423
|
-
|
|
424
|
-
```typescript
|
|
425
|
-
// 1. 实现工具接口
|
|
426
|
-
const myTool: ToolDefinition = {
|
|
427
|
-
id: "my_tool",
|
|
428
|
-
name: "My Tool",
|
|
429
|
-
description: "Does something useful",
|
|
430
|
-
parameters: { /* schema */ },
|
|
431
|
-
async execute(input, context) { /* 实现 */ },
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
// 2. 注册到 agent
|
|
435
|
-
const config: AgentConfig = {
|
|
436
|
-
tools: [myTool, /* ...其他工具 */],
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
const agent = createAgent(config)
|
|
440
|
-
```
|
|
441
|
-
|
|
442
|
-
### 添加自定义 LLM Provider:
|
|
443
|
-
|
|
444
|
-
```typescript
|
|
445
|
-
// 1. 实现 LLMProvider 接口
|
|
446
|
-
class MyProvider implements LLMProvider {
|
|
447
|
-
async stream(config: { /* ... */ }) { /* 实现 */ }
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
// 2. 创建 agent 时使用
|
|
451
|
-
// 注意:Agent 类默认使用 OpenAIProvider
|
|
452
|
-
// 你需要修改 Agent 类以支持自定义 provider
|
|
453
|
-
```
|
|
454
|
-
|
|
455
|
-
### 添加技能:
|
|
456
|
-
|
|
457
|
-
```typescript
|
|
458
|
-
// 1. 定义技能
|
|
459
|
-
const mySkill: SkillDefinition = {
|
|
460
|
-
id: "my_skill",
|
|
461
|
-
name: "My Skill",
|
|
462
|
-
description: "Specialized skill for something",
|
|
463
|
-
content: /* 技能内容 */,
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
// 2. 注册到 skill loader
|
|
467
|
-
const loader = agent.getSkillLoader()
|
|
468
|
-
loader.register(mySkill)
|
|
469
|
-
|
|
470
|
-
// 3. 在工具中加载技能
|
|
471
|
-
const skillTool: ToolDefinition = {
|
|
472
|
-
id: "load_skill",
|
|
473
|
-
name: "Load Skill",
|
|
474
|
-
description: "Load a skill",
|
|
475
|
-
async execute(input, context) {
|
|
476
|
-
const skill = await agent.getSkillLoader().load(input.skillId)
|
|
477
|
-
return {
|
|
478
|
-
output: skill.content,
|
|
479
|
-
metadata: { skillId: input.skillId },
|
|
480
|
-
}
|
|
481
|
-
},
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
const config: AgentConfig = {
|
|
485
|
-
tools: [skillTool, /* ...其他工具 */],
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
const agent = createAgent(config)
|
|
489
|
-
```
|
|
490
|
-
|
|
491
|
-
## 未来计划
|
|
492
|
-
|
|
493
|
-
- [ ] 添加更多 LLM Provider(Anthropic、Google 等)
|
|
494
|
-
- [ ] 实现更复杂的工具系统(插件架构)
|
|
495
|
-
- [ ] 添加持久化支持(存储会话到数据库/文件)
|
|
496
|
-
- [ ] 实现对话上下文窗口管理
|
|
497
|
-
- [ ] 添加 rate limiting(速率限制)
|
|
498
|
-
- [ ] 添加更详细的性能指标
|
|
499
|
-
|
|
500
|
-
## 许可证
|
|
501
|
-
|
|
502
|
-
MIT License
|
|
503
|
-
|
|
504
|
-
## 贡献
|
|
505
|
-
|
|
506
|
-
欢迎贡献!请遵循以下步骤:
|
|
507
|
-
|
|
508
|
-
1. Fork 本仓库
|
|
509
|
-
2. 创建特性分支
|
|
510
|
-
3. 提交更改
|
|
511
|
-
4. 推送到分支
|
|
512
|
-
5. 创建 Pull Request
|
|
513
|
-
|
|
514
|
-
## 联系
|
|
515
|
-
|
|
516
|
-
如有问题或建议,请提交 issue 到 OpenCode 仓库。
|