agent0-js 0.0.1 → 0.0.3
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 +342 -0
- package/dist/index.js +4 -2
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
- package/src/index.ts +6 -3
- package/src/types.ts +5 -9
package/README.md
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
# Agent0 JavaScript SDK
|
|
2
|
+
|
|
3
|
+
The official JavaScript/TypeScript SDK for Agent0 - a powerful platform for building and deploying AI agents.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the SDK using npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install agent0-js
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or using yarn:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add agent0-js
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or using pnpm:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm add agent0-js
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Agent0 } from 'agent0-js';
|
|
29
|
+
|
|
30
|
+
// Initialize the client
|
|
31
|
+
const client = new Agent0({
|
|
32
|
+
apiKey: 'your-api-key-here',
|
|
33
|
+
baseUrl: 'https://app.agent0.com' // Optional, defaults to this value
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Run an agent
|
|
37
|
+
const response = await client.generate({
|
|
38
|
+
agentId: 'your-agent-id',
|
|
39
|
+
variables: {
|
|
40
|
+
name: 'John',
|
|
41
|
+
topic: 'AI agents'
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
console.log(response.messages);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Getting Your API Key
|
|
49
|
+
|
|
50
|
+
1. Log in to your [Agent0 dashboard](https://app.agent0.com)
|
|
51
|
+
2. Navigate to **API Keys**
|
|
52
|
+
3. Click **+ Create**
|
|
53
|
+
4. Copy the generated key and store it securely
|
|
54
|
+
|
|
55
|
+
> ⚠️ **Important**: Keep your API key secure and never commit it to version control. Use environment variables instead.
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
### Initialize the Client
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { Agent0 } from 'agent0-js';
|
|
63
|
+
|
|
64
|
+
const client = new Agent0({
|
|
65
|
+
apiKey: process.env.AGENT0_API_KEY!,
|
|
66
|
+
baseUrl: 'https://app.agent0.com' // Optional
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Configuration Options
|
|
71
|
+
|
|
72
|
+
| Option | Type | Required | Default | Description |
|
|
73
|
+
|--------|------|----------|---------|-------------|
|
|
74
|
+
| `apiKey` | `string` | Yes | - | Your Agent0 API key |
|
|
75
|
+
| `baseUrl` | `string` | No | `https://app.agent0.com` | The base URL for the Agent0 API |
|
|
76
|
+
|
|
77
|
+
## Methods
|
|
78
|
+
|
|
79
|
+
### `generate(options: RunOptions): Promise<GenerateResponse>`
|
|
80
|
+
|
|
81
|
+
Execute an agent and get the complete response.
|
|
82
|
+
|
|
83
|
+
**Parameters:**
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
interface RunOptions {
|
|
87
|
+
agentId: string; // The ID of the agent to run
|
|
88
|
+
variables?: Record<string, string>; // Variables to pass to the agent
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Returns:**
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
interface GenerateResponse {
|
|
96
|
+
messages: Message[];
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**Example:**
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const response = await client.generate({
|
|
104
|
+
agentId: 'agent_123',
|
|
105
|
+
variables: {
|
|
106
|
+
userInput: 'Tell me about AI',
|
|
107
|
+
context: 'technical'
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
console.log(response.messages);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### `stream(options: RunOptions): AsyncGenerator<TextStreamPart<ToolSet>>`
|
|
115
|
+
|
|
116
|
+
Execute an agent and stream the response in real-time.
|
|
117
|
+
|
|
118
|
+
**Parameters:**
|
|
119
|
+
|
|
120
|
+
Same as `generate()` method.
|
|
121
|
+
|
|
122
|
+
**Returns:**
|
|
123
|
+
|
|
124
|
+
An async generator that yields stream chunks as they arrive.
|
|
125
|
+
|
|
126
|
+
**Example:**
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
const stream = client.stream({
|
|
130
|
+
agentId: 'agent_123',
|
|
131
|
+
variables: {
|
|
132
|
+
query: 'What is the weather today?'
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
for await (const chunk of stream) {
|
|
137
|
+
if (chunk.type === 'text-delta') {
|
|
138
|
+
process.stdout.write(chunk.textDelta);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Examples
|
|
144
|
+
|
|
145
|
+
### Basic Usage (Node.js)
|
|
146
|
+
|
|
147
|
+
```javascript
|
|
148
|
+
const { Agent0 } = require('agent0-js');
|
|
149
|
+
|
|
150
|
+
const client = new Agent0({
|
|
151
|
+
apiKey: process.env.AGENT0_API_KEY
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
async function main() {
|
|
155
|
+
try {
|
|
156
|
+
const result = await client.generate({
|
|
157
|
+
agentId: 'agent_123',
|
|
158
|
+
variables: {
|
|
159
|
+
name: 'Alice',
|
|
160
|
+
task: 'summarize'
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
console.log('Agent response:', result.messages);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.error('Error:', error.message);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
main();
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Streaming with TypeScript
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
import { Agent0 } from 'agent0-js';
|
|
177
|
+
|
|
178
|
+
const client = new Agent0({
|
|
179
|
+
apiKey: process.env.AGENT0_API_KEY!
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
async function streamExample() {
|
|
183
|
+
console.log('Agent response: ');
|
|
184
|
+
|
|
185
|
+
const stream = client.stream({
|
|
186
|
+
agentId: 'agent_123',
|
|
187
|
+
variables: {
|
|
188
|
+
prompt: 'Write a short story about robots'
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
for await (const chunk of stream) {
|
|
193
|
+
// Handle different chunk types
|
|
194
|
+
switch (chunk.type) {
|
|
195
|
+
case 'text-delta':
|
|
196
|
+
process.stdout.write(chunk.textDelta);
|
|
197
|
+
break;
|
|
198
|
+
case 'tool-call':
|
|
199
|
+
console.log('\nTool called:', chunk.toolName);
|
|
200
|
+
break;
|
|
201
|
+
case 'tool-result':
|
|
202
|
+
console.log('\nTool result:', chunk.result);
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
console.log('\n\nStream complete!');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
streamExample();
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Using Variables
|
|
214
|
+
|
|
215
|
+
Variables allow you to pass dynamic data to your agents. Any variables defined in your agent's prompts will be replaced with the values you provide.
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
// If your agent prompt contains: "Hello {{name}}, let's talk about {{topic}}"
|
|
219
|
+
const response = await client.generate({
|
|
220
|
+
agentId: 'agent_123',
|
|
221
|
+
variables: {
|
|
222
|
+
name: 'Sarah',
|
|
223
|
+
topic: 'machine learning'
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
// Prompt becomes: "Hello Sarah, let's talk about machine learning"
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Error Handling
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
import { Agent0 } from 'agent0-js';
|
|
233
|
+
|
|
234
|
+
const client = new Agent0({
|
|
235
|
+
apiKey: process.env.AGENT0_API_KEY!
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
async function runAgentWithErrorHandling() {
|
|
239
|
+
try {
|
|
240
|
+
const response = await client.generate({
|
|
241
|
+
agentId: 'agent_123',
|
|
242
|
+
variables: { input: 'test' }
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
return response.messages;
|
|
246
|
+
} catch (error) {
|
|
247
|
+
if (error instanceof Error) {
|
|
248
|
+
console.error('Agent execution failed:', error.message);
|
|
249
|
+
|
|
250
|
+
// Handle specific error cases
|
|
251
|
+
if (error.message.includes('401')) {
|
|
252
|
+
console.error('Invalid API key');
|
|
253
|
+
} else if (error.message.includes('404')) {
|
|
254
|
+
console.error('Agent not found');
|
|
255
|
+
} else if (error.message.includes('429')) {
|
|
256
|
+
console.error('Rate limit exceeded');
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
throw error;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### Using with Environment Variables
|
|
266
|
+
|
|
267
|
+
Create a `.env` file:
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
AGENT0_API_KEY=your_api_key_here
|
|
271
|
+
AGENT0_BASE_URL=https://app.agent0.com
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Then use it in your application:
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
import { Agent0 } from 'agent0-js';
|
|
278
|
+
import * as dotenv from 'dotenv';
|
|
279
|
+
|
|
280
|
+
dotenv.config();
|
|
281
|
+
|
|
282
|
+
const client = new Agent0({
|
|
283
|
+
apiKey: process.env.AGENT0_API_KEY!,
|
|
284
|
+
baseUrl: process.env.AGENT0_BASE_URL
|
|
285
|
+
});
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## TypeScript Support
|
|
289
|
+
|
|
290
|
+
This SDK is written in TypeScript and includes full type definitions. You get autocomplete and type checking out of the box:
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
import { Agent0, type RunOptions, type GenerateResponse } from 'agent0-js';
|
|
294
|
+
|
|
295
|
+
const client = new Agent0({
|
|
296
|
+
apiKey: process.env.AGENT0_API_KEY!
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// TypeScript will enforce correct types
|
|
300
|
+
const options: RunOptions = {
|
|
301
|
+
agentId: 'agent_123',
|
|
302
|
+
variables: {
|
|
303
|
+
key: 'value' // Must be Record<string, string>
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const response: GenerateResponse = await client.generate(options);
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## Best Practices
|
|
311
|
+
|
|
312
|
+
1. **Secure Your API Key**: Never hardcode API keys. Use environment variables or secret management services.
|
|
313
|
+
|
|
314
|
+
2. **Use Streaming for Long Responses**: For agents that generate lengthy content, use the `stream()` method for a better user experience.
|
|
315
|
+
|
|
316
|
+
3. **Handle Errors Gracefully**: Always wrap API calls in try-catch blocks and handle errors appropriately.
|
|
317
|
+
|
|
318
|
+
4. **Type Safety**: Use TypeScript for better development experience and fewer runtime errors.
|
|
319
|
+
|
|
320
|
+
5. **Set Timeouts**: For production applications, consider implementing timeout logic for long-running agent executions.
|
|
321
|
+
|
|
322
|
+
## Platform Compatibility
|
|
323
|
+
|
|
324
|
+
- **Node.js**: 18.x or higher
|
|
325
|
+
- **Browsers**: Modern browsers with fetch API support
|
|
326
|
+
- **Edge Runtimes**: Vercel Edge, Cloudflare Workers, etc.
|
|
327
|
+
- **React Native**: Supported (with appropriate polyfills)
|
|
328
|
+
|
|
329
|
+
## Support & Resources
|
|
330
|
+
|
|
331
|
+
- **Documentation**: [https://docs.agent0.com](https://docs.agent0.com)
|
|
332
|
+
- **Dashboard**: [https://app.agent0.com](https://app.agent0.com)
|
|
333
|
+
- **Issues**: Report bugs or request features on GitHub
|
|
334
|
+
- **Community**: Join our Discord community for support
|
|
335
|
+
|
|
336
|
+
## License
|
|
337
|
+
|
|
338
|
+
ISC
|
|
339
|
+
|
|
340
|
+
## Contributing
|
|
341
|
+
|
|
342
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
package/dist/index.js
CHANGED
|
@@ -25,14 +25,16 @@ class Agent0 {
|
|
|
25
25
|
}
|
|
26
26
|
async generate(options) {
|
|
27
27
|
const response = await this.fetchApi('/api/v1/run', {
|
|
28
|
-
|
|
28
|
+
agent_id: options.agentId,
|
|
29
|
+
variables: options.variables,
|
|
29
30
|
stream: false,
|
|
30
31
|
});
|
|
31
32
|
return await response.json();
|
|
32
33
|
}
|
|
33
34
|
async *stream(options) {
|
|
34
35
|
const response = await this.fetchApi('/api/v1/run', {
|
|
35
|
-
|
|
36
|
+
agent_id: options.agentId,
|
|
37
|
+
variables: options.variables,
|
|
36
38
|
stream: true,
|
|
37
39
|
});
|
|
38
40
|
if (!response.body) {
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ export class Agent0 {
|
|
|
10
10
|
this.baseUrl = config.baseUrl || 'https://app.agent0.com'; // Default URL, can be overridden
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
private async fetchApi(endpoint: string, body:
|
|
13
|
+
private async fetchApi(endpoint: string, body: unknown): Promise<Response> {
|
|
14
14
|
const url = `${this.baseUrl}${endpoint}`;
|
|
15
15
|
|
|
16
16
|
const headers = {
|
|
@@ -34,7 +34,8 @@ export class Agent0 {
|
|
|
34
34
|
|
|
35
35
|
async generate(options: RunOptions): Promise<GenerateResponse> {
|
|
36
36
|
const response = await this.fetchApi('/api/v1/run', {
|
|
37
|
-
|
|
37
|
+
agent_id: options.agentId,
|
|
38
|
+
variables: options.variables,
|
|
38
39
|
stream: false,
|
|
39
40
|
});
|
|
40
41
|
|
|
@@ -43,7 +44,8 @@ export class Agent0 {
|
|
|
43
44
|
|
|
44
45
|
async *stream(options: RunOptions): AsyncGenerator<TextStreamPart<ToolSet>, void, unknown> {
|
|
45
46
|
const response = await this.fetchApi('/api/v1/run', {
|
|
46
|
-
|
|
47
|
+
agent_id: options.agentId,
|
|
48
|
+
variables: options.variables,
|
|
47
49
|
stream: true,
|
|
48
50
|
});
|
|
49
51
|
|
|
@@ -53,6 +55,7 @@ export class Agent0 {
|
|
|
53
55
|
|
|
54
56
|
const reader = response.body.getReader();
|
|
55
57
|
const decoder = new TextDecoder();
|
|
58
|
+
|
|
56
59
|
let buffer = '';
|
|
57
60
|
|
|
58
61
|
try {
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ModelMessage } from "ai";
|
|
2
2
|
|
|
3
3
|
export interface Agent0Config {
|
|
4
4
|
apiKey: string;
|
|
@@ -6,15 +6,11 @@ export interface Agent0Config {
|
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
export interface RunOptions {
|
|
9
|
-
|
|
9
|
+
agentId: string;
|
|
10
10
|
variables?: Record<string, string>;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export interface Message {
|
|
14
|
-
role: 'assistatnt'
|
|
15
|
-
content: StepResult<ToolSet>["content"];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
13
|
export interface GenerateResponse {
|
|
19
|
-
messages:
|
|
20
|
-
|
|
14
|
+
messages: ModelMessage[];
|
|
15
|
+
text: string;
|
|
16
|
+
}
|