@cuylabs/agent-code 0.1.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/README.md +250 -0
- package/dist/chunk-IOXLKDEB.js +815 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +45 -0
- package/dist/tools/index.d.ts +360 -0
- package/dist/tools/index.js +42 -0
- package/package.json +75 -0
package/README.md
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
# @cuylabs/agent-code
|
|
2
|
+
|
|
3
|
+
An embeddable AI coding agent library built on top of the [Vercel AI SDK](https://sdk.vercel.ai/).
|
|
4
|
+
|
|
5
|
+
Designed to be embedded into larger applications, using modern Vercel AI SDK patterns for streaming, tool calling, and multi-provider support.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- š **Embeddable** - Drop into any Node.js/TypeScript application
|
|
10
|
+
- š¤ **Multi-Provider** - Works with OpenAI, Anthropic, Google, and more via Vercel AI SDK
|
|
11
|
+
- š ļø **Coding Tools** - Built-in tools for file operations, search, and shell commands
|
|
12
|
+
- š” **Streaming** - Real-time streaming of responses and tool execution
|
|
13
|
+
- š§ **Extensible** - Easy to add custom tools
|
|
14
|
+
- š¾ **Session Management** - Built-in conversation history tracking
|
|
15
|
+
- š§ **Reasoning Support** - Works with reasoning models (o3-mini, etc.)
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @cuylabs/agent-code ai @ai-sdk/openai
|
|
21
|
+
# or
|
|
22
|
+
pnpm add @cuylabs/agent-code ai @ai-sdk/openai
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { createAgent, defaultCodingTools } from "@cuylabs/agent-code";
|
|
29
|
+
import { openai } from "@ai-sdk/openai";
|
|
30
|
+
|
|
31
|
+
// Create an agent with GPT-4o
|
|
32
|
+
const agent = createAgent({
|
|
33
|
+
model: openai("gpt-4o"),
|
|
34
|
+
cwd: process.cwd(),
|
|
35
|
+
tools: defaultCodingTools,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Non-streaming usage
|
|
39
|
+
const { response, toolCalls } = await agent.send(
|
|
40
|
+
"session-1",
|
|
41
|
+
"List all TypeScript files in the src directory"
|
|
42
|
+
);
|
|
43
|
+
console.log(response);
|
|
44
|
+
|
|
45
|
+
// Streaming usage
|
|
46
|
+
for await (const event of agent.chat("session-1", "Fix the bug in utils.ts")) {
|
|
47
|
+
switch (event.type) {
|
|
48
|
+
case "text-delta":
|
|
49
|
+
process.stdout.write(event.text);
|
|
50
|
+
break;
|
|
51
|
+
case "tool-start":
|
|
52
|
+
console.log(`\nš§ ${event.toolName}...`);
|
|
53
|
+
break;
|
|
54
|
+
case "tool-result":
|
|
55
|
+
console.log(`ā
${event.toolName} complete`);
|
|
56
|
+
break;
|
|
57
|
+
case "error":
|
|
58
|
+
console.error(`ā Error:`, event.error);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Built-in Tools
|
|
65
|
+
|
|
66
|
+
| Tool | Description |
|
|
67
|
+
|------|-------------|
|
|
68
|
+
| `bash` | Execute shell commands |
|
|
69
|
+
| `read` | Read file contents with line numbers |
|
|
70
|
+
| `edit` | Edit files by replacing text |
|
|
71
|
+
| `write` | Create or overwrite files |
|
|
72
|
+
| `grep` | Search file contents with regex |
|
|
73
|
+
| `glob` | Find files by glob pattern |
|
|
74
|
+
|
|
75
|
+
## Custom Tools
|
|
76
|
+
|
|
77
|
+
Add your own tools using the `Tool.define` helper:
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { createAgent, Tool } from "@cuylabs/agent-code";
|
|
81
|
+
import { openai } from "@ai-sdk/openai";
|
|
82
|
+
import { z } from "zod";
|
|
83
|
+
|
|
84
|
+
const myTool = Tool.define("my_custom_tool", {
|
|
85
|
+
description: "Does something custom",
|
|
86
|
+
parameters: z.object({
|
|
87
|
+
input: z.string().describe("The input to process"),
|
|
88
|
+
}),
|
|
89
|
+
execute: async (params, ctx) => ({
|
|
90
|
+
title: "Custom Tool",
|
|
91
|
+
output: `Processed: ${params.input}`,
|
|
92
|
+
metadata: {},
|
|
93
|
+
}),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const agent = createAgent({
|
|
97
|
+
model: openai("gpt-4o"),
|
|
98
|
+
cwd: process.cwd(),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
agent.addTool(myTool);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Using Different Providers
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
// OpenAI (recommended)
|
|
108
|
+
import { openai } from "@ai-sdk/openai";
|
|
109
|
+
const agent = createAgent({
|
|
110
|
+
model: openai("gpt-4o"),
|
|
111
|
+
cwd: process.cwd(),
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// OpenAI with reasoning
|
|
115
|
+
const reasoningAgent = createAgent({
|
|
116
|
+
model: openai("o3-mini"),
|
|
117
|
+
cwd: process.cwd(),
|
|
118
|
+
reasoningLevel: "high",
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Anthropic
|
|
122
|
+
import { anthropic } from "@ai-sdk/anthropic";
|
|
123
|
+
const agent = createAgent({
|
|
124
|
+
model: anthropic("claude-sonnet-4-20250514"),
|
|
125
|
+
cwd: process.cwd(),
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Google
|
|
129
|
+
import { google } from "@ai-sdk/google";
|
|
130
|
+
const agent = createAgent({
|
|
131
|
+
model: google("gemini-2.0-flash"),
|
|
132
|
+
cwd: process.cwd(),
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Configuration
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { createAgent, defaultCodingTools, readTool, grepTool, globTool } from "@cuylabs/agent-code";
|
|
140
|
+
import { openai } from "@ai-sdk/openai";
|
|
141
|
+
|
|
142
|
+
const agent = createAgent({
|
|
143
|
+
// Required: Vercel AI SDK model instance
|
|
144
|
+
model: openai("gpt-4o"),
|
|
145
|
+
|
|
146
|
+
// Working directory for file operations (default: process.cwd())
|
|
147
|
+
cwd: "/path/to/project",
|
|
148
|
+
|
|
149
|
+
// System prompt (has sensible defaults for coding)
|
|
150
|
+
systemPrompt: "You are a helpful coding assistant...",
|
|
151
|
+
|
|
152
|
+
// Temperature (0-1, default varies by provider)
|
|
153
|
+
temperature: 0.7,
|
|
154
|
+
|
|
155
|
+
// Max output tokens (default: 32000)
|
|
156
|
+
maxOutputTokens: 16000,
|
|
157
|
+
|
|
158
|
+
// Max steps for tool calling loops (default: 50)
|
|
159
|
+
maxSteps: 25,
|
|
160
|
+
|
|
161
|
+
// Reasoning level for reasoning models (default: "off")
|
|
162
|
+
reasoningLevel: "high", // "off", "low", "medium", "high"
|
|
163
|
+
|
|
164
|
+
// Tools to enable (defaults to empty, use defaultCodingTools for all)
|
|
165
|
+
tools: defaultCodingTools,
|
|
166
|
+
|
|
167
|
+
// Or use a subset for read-only operations
|
|
168
|
+
// tools: [readTool, grepTool, globTool],
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Session Management
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
// Sessions are managed automatically
|
|
176
|
+
// Just use a unique session ID for each conversation
|
|
177
|
+
|
|
178
|
+
// List all sessions
|
|
179
|
+
const sessions = await agent.listSessions();
|
|
180
|
+
|
|
181
|
+
// Delete a session
|
|
182
|
+
await agent.deleteSession("session-id");
|
|
183
|
+
|
|
184
|
+
// Get current session's messages
|
|
185
|
+
const messages = agent.getMessages();
|
|
186
|
+
|
|
187
|
+
// Branch from current point (for undo/redo)
|
|
188
|
+
const branchId = await agent.branch("checkpoint before refactor");
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Events
|
|
192
|
+
|
|
193
|
+
The streaming `chat()` method yields these events:
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
type AgentEvent =
|
|
197
|
+
// Text streaming
|
|
198
|
+
| { type: "text-start" }
|
|
199
|
+
| { type: "text-delta"; text: string }
|
|
200
|
+
| { type: "text-end" }
|
|
201
|
+
|
|
202
|
+
// Reasoning (for o3-mini, etc.)
|
|
203
|
+
| { type: "reasoning-start"; id: string }
|
|
204
|
+
| { type: "reasoning-delta"; id: string; text: string }
|
|
205
|
+
| { type: "reasoning-end"; id: string }
|
|
206
|
+
|
|
207
|
+
// Tool execution
|
|
208
|
+
| { type: "tool-start"; toolName: string; toolCallId: string; input: unknown }
|
|
209
|
+
| { type: "tool-result"; toolName: string; toolCallId: string; result: unknown }
|
|
210
|
+
| { type: "tool-error"; toolName: string; toolCallId: string; error: string }
|
|
211
|
+
|
|
212
|
+
// Progress
|
|
213
|
+
| { type: "step-start"; step: number; maxSteps: number }
|
|
214
|
+
| { type: "step-finish"; step: number; usage?: TokenUsage; finishReason?: string }
|
|
215
|
+
|
|
216
|
+
// Completion
|
|
217
|
+
| { type: "error"; error: Error }
|
|
218
|
+
| { type: "complete"; usage?: TokenUsage };
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## Examples
|
|
222
|
+
|
|
223
|
+
See the [examples/](./examples/) folder for working examples:
|
|
224
|
+
|
|
225
|
+
| # | File | What it shows |
|
|
226
|
+
|---|------|---------------|
|
|
227
|
+
| 01 | `01-basic.ts` | Basic streaming chat with coding tools |
|
|
228
|
+
| 02 | `02-reasoning.ts` | Using reasoning models (o4-mini) |
|
|
229
|
+
| 03 | `03-read-only.ts` | Read-only agent (no bash/edit/write) |
|
|
230
|
+
| 04 | `04-custom-tools.ts` | Adding your own tools |
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
cd packages/agent-code/examples
|
|
234
|
+
cp .env.example .env
|
|
235
|
+
# Add your OPENAI_API_KEY
|
|
236
|
+
|
|
237
|
+
npx tsx examples/01-basic.ts
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Architecture
|
|
241
|
+
|
|
242
|
+
This library is built on:
|
|
243
|
+
|
|
244
|
+
- **@cuylabs/agent-core** - Core agent infrastructure (sessions, streaming, tool execution)
|
|
245
|
+
- **Vercel AI SDK** - LLM interaction and multi-provider support
|
|
246
|
+
- **Zod** - Parameter validation for tools
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
Apache-2.0
|