agent0-js 0.0.1
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/dist/index.d.ts +10 -0
- package/dist/index.js +73 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.js +2 -0
- package/package.json +26 -0
- package/src/index.ts +86 -0
- package/src/types.ts +20 -0
- package/tsconfig.json +13 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { TextStreamPart, ToolSet } from 'ai';
|
|
2
|
+
import type { Agent0Config, GenerateResponse, RunOptions } from './types';
|
|
3
|
+
export declare class Agent0 {
|
|
4
|
+
private apiKey;
|
|
5
|
+
private baseUrl;
|
|
6
|
+
constructor(config: Agent0Config);
|
|
7
|
+
private fetchApi;
|
|
8
|
+
generate(options: RunOptions): Promise<GenerateResponse>;
|
|
9
|
+
stream(options: RunOptions): AsyncGenerator<TextStreamPart<ToolSet>, void, unknown>;
|
|
10
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Agent0 = void 0;
|
|
4
|
+
class Agent0 {
|
|
5
|
+
constructor(config) {
|
|
6
|
+
this.apiKey = config.apiKey;
|
|
7
|
+
this.baseUrl = config.baseUrl || 'https://app.agent0.com'; // Default URL, can be overridden
|
|
8
|
+
}
|
|
9
|
+
async fetchApi(endpoint, body) {
|
|
10
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
11
|
+
const headers = {
|
|
12
|
+
'Content-Type': 'application/json',
|
|
13
|
+
'x-api-key': this.apiKey,
|
|
14
|
+
};
|
|
15
|
+
const response = await fetch(url, {
|
|
16
|
+
method: 'POST',
|
|
17
|
+
headers,
|
|
18
|
+
body: JSON.stringify(body),
|
|
19
|
+
});
|
|
20
|
+
if (!response.ok) {
|
|
21
|
+
const errorText = await response.text();
|
|
22
|
+
throw new Error(`API request failed: ${response.status} ${response.statusText} - ${errorText}`);
|
|
23
|
+
}
|
|
24
|
+
return response;
|
|
25
|
+
}
|
|
26
|
+
async generate(options) {
|
|
27
|
+
const response = await this.fetchApi('/api/v1/run', {
|
|
28
|
+
...options,
|
|
29
|
+
stream: false,
|
|
30
|
+
});
|
|
31
|
+
return await response.json();
|
|
32
|
+
}
|
|
33
|
+
async *stream(options) {
|
|
34
|
+
const response = await this.fetchApi('/api/v1/run', {
|
|
35
|
+
...options,
|
|
36
|
+
stream: true,
|
|
37
|
+
});
|
|
38
|
+
if (!response.body) {
|
|
39
|
+
throw new Error('Response body is empty');
|
|
40
|
+
}
|
|
41
|
+
const reader = response.body.getReader();
|
|
42
|
+
const decoder = new TextDecoder();
|
|
43
|
+
let buffer = '';
|
|
44
|
+
try {
|
|
45
|
+
while (true) {
|
|
46
|
+
const { done, value } = await reader.read();
|
|
47
|
+
if (done)
|
|
48
|
+
break;
|
|
49
|
+
buffer += decoder.decode(value, { stream: true });
|
|
50
|
+
const lines = buffer.split('\n');
|
|
51
|
+
// Keep the last incomplete line in the buffer
|
|
52
|
+
buffer = lines.pop() || '';
|
|
53
|
+
for (const line of lines) {
|
|
54
|
+
const trimmedLine = line.trim();
|
|
55
|
+
if (!trimmedLine || !trimmedLine.startsWith('data: '))
|
|
56
|
+
continue;
|
|
57
|
+
const data = trimmedLine.slice(6);
|
|
58
|
+
try {
|
|
59
|
+
const parsed = JSON.parse(data);
|
|
60
|
+
yield parsed;
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
console.warn('Failed to parse stream chunk:', data, e);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
reader.releaseLock();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.Agent0 = Agent0;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { StepResult, ToolSet } from "ai";
|
|
2
|
+
export interface Agent0Config {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface RunOptions {
|
|
7
|
+
agent_id: string;
|
|
8
|
+
variables?: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
export interface Message {
|
|
11
|
+
role: 'assistatnt';
|
|
12
|
+
content: StepResult<ToolSet>["content"];
|
|
13
|
+
}
|
|
14
|
+
export interface GenerateResponse {
|
|
15
|
+
messages: Message[];
|
|
16
|
+
}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "agent0-js",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "TypeScript SDK for Agent0",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"agent0",
|
|
9
|
+
"sdk",
|
|
10
|
+
"ai",
|
|
11
|
+
"agent"
|
|
12
|
+
],
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/node": "^20.0.0",
|
|
17
|
+
"typescript": "^5.0.0"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"ai": "^5.0.98"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"dev": "tsc -w"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { TextStreamPart, ToolSet } from 'ai';
|
|
2
|
+
import type { Agent0Config, GenerateResponse, RunOptions } from './types';
|
|
3
|
+
|
|
4
|
+
export class Agent0 {
|
|
5
|
+
private apiKey: string;
|
|
6
|
+
private baseUrl: string;
|
|
7
|
+
|
|
8
|
+
constructor(config: Agent0Config) {
|
|
9
|
+
this.apiKey = config.apiKey;
|
|
10
|
+
this.baseUrl = config.baseUrl || 'https://app.agent0.com'; // Default URL, can be overridden
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
private async fetchApi(endpoint: string, body: any): Promise<Response> {
|
|
14
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
15
|
+
|
|
16
|
+
const headers = {
|
|
17
|
+
'Content-Type': 'application/json',
|
|
18
|
+
'x-api-key': this.apiKey,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const response = await fetch(url, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers,
|
|
24
|
+
body: JSON.stringify(body),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
const errorText = await response.text();
|
|
29
|
+
throw new Error(`API request failed: ${response.status} ${response.statusText} - ${errorText}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return response;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async generate(options: RunOptions): Promise<GenerateResponse> {
|
|
36
|
+
const response = await this.fetchApi('/api/v1/run', {
|
|
37
|
+
...options,
|
|
38
|
+
stream: false,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return await response.json();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async *stream(options: RunOptions): AsyncGenerator<TextStreamPart<ToolSet>, void, unknown> {
|
|
45
|
+
const response = await this.fetchApi('/api/v1/run', {
|
|
46
|
+
...options,
|
|
47
|
+
stream: true,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (!response.body) {
|
|
51
|
+
throw new Error('Response body is empty');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const reader = response.body.getReader();
|
|
55
|
+
const decoder = new TextDecoder();
|
|
56
|
+
let buffer = '';
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
while (true) {
|
|
60
|
+
const { done, value } = await reader.read();
|
|
61
|
+
if (done) break;
|
|
62
|
+
|
|
63
|
+
buffer += decoder.decode(value, { stream: true });
|
|
64
|
+
const lines = buffer.split('\n');
|
|
65
|
+
|
|
66
|
+
// Keep the last incomplete line in the buffer
|
|
67
|
+
buffer = lines.pop() || '';
|
|
68
|
+
|
|
69
|
+
for (const line of lines) {
|
|
70
|
+
const trimmedLine = line.trim();
|
|
71
|
+
if (!trimmedLine || !trimmedLine.startsWith('data: ')) continue;
|
|
72
|
+
|
|
73
|
+
const data = trimmedLine.slice(6);
|
|
74
|
+
try {
|
|
75
|
+
const parsed = JSON.parse(data) as TextStreamPart<ToolSet>;
|
|
76
|
+
yield parsed;
|
|
77
|
+
} catch (e) {
|
|
78
|
+
console.warn('Failed to parse stream chunk:', data, e);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} finally {
|
|
83
|
+
reader.releaseLock();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { StepResult, ToolSet } from "ai";
|
|
2
|
+
|
|
3
|
+
export interface Agent0Config {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface RunOptions {
|
|
9
|
+
agent_id: string;
|
|
10
|
+
variables?: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Message {
|
|
14
|
+
role: 'assistatnt'
|
|
15
|
+
content: StepResult<ToolSet>["content"];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface GenerateResponse {
|
|
19
|
+
messages: Message[];
|
|
20
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["src/**/*"]
|
|
13
|
+
}
|