@inferencesh/sdk 0.4.21 → 0.4.23
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 +49 -23
- package/dist/api/agents.d.ts +144 -0
- package/dist/api/agents.js +261 -0
- package/dist/api/apps.d.ts +72 -0
- package/dist/api/apps.js +94 -0
- package/dist/api/chats.d.ts +30 -0
- package/dist/api/chats.js +46 -0
- package/dist/api/engines.d.ts +37 -0
- package/dist/api/engines.js +52 -0
- package/dist/api/files.d.ts +37 -0
- package/dist/api/files.js +130 -0
- package/dist/api/flow-runs.d.ts +46 -0
- package/dist/api/flow-runs.js +70 -0
- package/dist/api/flows.d.ts +46 -0
- package/dist/api/flows.js +70 -0
- package/dist/api/index.d.ts +3 -0
- package/dist/api/index.js +13 -0
- package/dist/api/tasks.d.ts +68 -0
- package/dist/api/tasks.js +147 -0
- package/dist/client.test.js +143 -18
- package/dist/http/client.d.ts +56 -0
- package/dist/http/client.js +186 -0
- package/dist/{errors.d.ts → http/errors.d.ts} +1 -1
- package/dist/http/index.d.ts +3 -0
- package/dist/http/index.js +11 -0
- package/dist/index.cjs +165 -0
- package/dist/index.d.ts +94 -4
- package/dist/index.js +132 -13
- package/dist/index.mjs +141 -1
- package/dist/integration.test.js +3 -3
- package/dist/proxy/express.cjs +71 -0
- package/dist/proxy/express.d.ts +1 -1
- package/dist/proxy/express.mjs +2 -1
- package/dist/proxy/hono.cjs +55 -0
- package/dist/proxy/hono.mjs +2 -1
- package/dist/proxy/index.cjs +178 -0
- package/dist/proxy/index.mjs +2 -1
- package/dist/proxy/nextjs.cjs +123 -0
- package/dist/proxy/nextjs.mjs +2 -1
- package/dist/proxy/remix.cjs +51 -0
- package/dist/proxy/remix.mjs +2 -1
- package/dist/proxy/svelte.cjs +55 -0
- package/dist/proxy/svelte.mjs +2 -1
- package/dist/stream.test.js +1 -1
- package/dist/tool-builder.d.ts +3 -1
- package/dist/tool-builder.js +10 -5
- package/dist/types.d.ts +349 -0
- package/dist/types.js +53 -4
- package/package.json +2 -2
- package/dist/client.d.ts +0 -248
- package/dist/client.js +0 -618
- /package/dist/{errors.js → http/errors.js} +0 -0
- /package/dist/{stream.d.ts → http/stream.d.ts} +0 -0
- /package/dist/{stream.js → http/stream.js} +0 -0
package/README.md
CHANGED
|
@@ -24,19 +24,21 @@ Get your API key from the [inference.sh dashboard](https://app.inference.sh/sett
|
|
|
24
24
|
## Quick Start
|
|
25
25
|
|
|
26
26
|
```typescript
|
|
27
|
-
import { inference } from '@inferencesh/sdk';
|
|
27
|
+
import { inference, TaskStatusCompleted } from '@inferencesh/sdk';
|
|
28
28
|
|
|
29
29
|
const client = inference({ apiKey: 'your-api-key' });
|
|
30
30
|
|
|
31
31
|
// Run a task and wait for the result
|
|
32
|
-
const result = await client.run({
|
|
32
|
+
const result = await client.tasks.run({
|
|
33
33
|
app: 'your-app',
|
|
34
34
|
input: {
|
|
35
35
|
prompt: 'Hello, world!'
|
|
36
36
|
}
|
|
37
37
|
});
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
if (result.status === TaskStatusCompleted) {
|
|
40
|
+
console.log(result.output);
|
|
41
|
+
}
|
|
40
42
|
```
|
|
41
43
|
|
|
42
44
|
## Usage
|
|
@@ -44,17 +46,19 @@ console.log(result.output);
|
|
|
44
46
|
### Basic Usage
|
|
45
47
|
|
|
46
48
|
```typescript
|
|
47
|
-
import { inference } from '@inferencesh/sdk';
|
|
49
|
+
import { inference, TaskStatusCompleted } from '@inferencesh/sdk';
|
|
48
50
|
|
|
49
51
|
const client = inference({ apiKey: 'your-api-key' });
|
|
50
52
|
|
|
51
53
|
// Wait for result (default behavior)
|
|
52
|
-
const result = await client.run({
|
|
54
|
+
const result = await client.tasks.run({
|
|
53
55
|
app: 'my-app',
|
|
54
56
|
input: { prompt: 'Generate something amazing' }
|
|
55
57
|
});
|
|
56
58
|
|
|
57
|
-
|
|
59
|
+
if (result.status === TaskStatusCompleted) {
|
|
60
|
+
console.log('Output:', result.output);
|
|
61
|
+
}
|
|
58
62
|
```
|
|
59
63
|
|
|
60
64
|
### With Setup Parameters
|
|
@@ -62,7 +66,7 @@ console.log('Output:', result.output);
|
|
|
62
66
|
Setup parameters configure the app instance (e.g., model selection). Workers with matching setup are "warm" and skip setup:
|
|
63
67
|
|
|
64
68
|
```typescript
|
|
65
|
-
const result = await client.run({
|
|
69
|
+
const result = await client.tasks.run({
|
|
66
70
|
app: 'my-app',
|
|
67
71
|
setup: { model: 'schnell' }, // Setup parameters
|
|
68
72
|
input: { prompt: 'hello' }
|
|
@@ -73,7 +77,7 @@ const result = await client.run({
|
|
|
73
77
|
|
|
74
78
|
```typescript
|
|
75
79
|
// Get task info immediately without waiting
|
|
76
|
-
const task = await client.run(
|
|
80
|
+
const task = await client.tasks.run(
|
|
77
81
|
{ app: 'my-app', input: { prompt: 'hello' } },
|
|
78
82
|
{ wait: false }
|
|
79
83
|
);
|
|
@@ -85,7 +89,7 @@ console.log('Status:', task.status);
|
|
|
85
89
|
### Real-time Status Updates
|
|
86
90
|
|
|
87
91
|
```typescript
|
|
88
|
-
const result = await client.run(
|
|
92
|
+
const result = await client.tasks.run(
|
|
89
93
|
{ app: 'my-app', input: { prompt: 'hello' } },
|
|
90
94
|
{
|
|
91
95
|
onUpdate: (update) => {
|
|
@@ -101,18 +105,18 @@ const result = await client.run(
|
|
|
101
105
|
```typescript
|
|
102
106
|
async function processImages(images: string[]) {
|
|
103
107
|
const results = [];
|
|
104
|
-
|
|
108
|
+
|
|
105
109
|
for (const image of images) {
|
|
106
|
-
const result = await client.run({
|
|
110
|
+
const result = await client.tasks.run({
|
|
107
111
|
app: 'image-processor',
|
|
108
112
|
input: { image }
|
|
109
113
|
}, {
|
|
110
114
|
onUpdate: (update) => console.log(`Processing: ${update.status}`)
|
|
111
115
|
});
|
|
112
|
-
|
|
116
|
+
|
|
113
117
|
results.push(result);
|
|
114
118
|
}
|
|
115
|
-
|
|
119
|
+
|
|
116
120
|
return results;
|
|
117
121
|
}
|
|
118
122
|
```
|
|
@@ -121,13 +125,13 @@ async function processImages(images: string[]) {
|
|
|
121
125
|
|
|
122
126
|
```typescript
|
|
123
127
|
// Upload from base64
|
|
124
|
-
const file = await client.
|
|
128
|
+
const file = await client.files.upload('data:image/png;base64,...', {
|
|
125
129
|
filename: 'image.png',
|
|
126
130
|
contentType: 'image/png'
|
|
127
131
|
});
|
|
128
132
|
|
|
129
133
|
// Use the uploaded file in a task
|
|
130
|
-
const result = await client.run({
|
|
134
|
+
const result = await client.tasks.run({
|
|
131
135
|
app: 'image-app',
|
|
132
136
|
input: { image: file.uri }
|
|
133
137
|
});
|
|
@@ -136,18 +140,18 @@ const result = await client.run({
|
|
|
136
140
|
### Cancel a Task
|
|
137
141
|
|
|
138
142
|
```typescript
|
|
139
|
-
const task = await client.run(
|
|
143
|
+
const task = await client.tasks.run(
|
|
140
144
|
{ app: 'long-running-app', input: {} },
|
|
141
145
|
{ wait: false }
|
|
142
146
|
);
|
|
143
147
|
|
|
144
148
|
// Cancel if needed
|
|
145
|
-
await client.cancel(task.id);
|
|
149
|
+
await client.tasks.cancel(task.id);
|
|
146
150
|
```
|
|
147
151
|
|
|
148
152
|
## Agent Chat
|
|
149
153
|
|
|
150
|
-
Chat with AI agents using `client.
|
|
154
|
+
Chat with AI agents using `client.agents.create()`.
|
|
151
155
|
|
|
152
156
|
### Using a Template Agent
|
|
153
157
|
|
|
@@ -159,7 +163,7 @@ import { inference } from '@inferencesh/sdk';
|
|
|
159
163
|
const client = inference({ apiKey: 'your-api-key' });
|
|
160
164
|
|
|
161
165
|
// Create agent from template
|
|
162
|
-
const agent = client.
|
|
166
|
+
const agent = client.agents.create('my-org/assistant@abc123');
|
|
163
167
|
|
|
164
168
|
// Send a message with streaming
|
|
165
169
|
await agent.sendMessage('Hello!', {
|
|
@@ -188,7 +192,7 @@ import { inference, tool, string } from '@inferencesh/sdk';
|
|
|
188
192
|
const client = inference({ apiKey: 'your-api-key' });
|
|
189
193
|
|
|
190
194
|
// Create ad-hoc agent
|
|
191
|
-
const agent = client.
|
|
195
|
+
const agent = client.agents.create({
|
|
192
196
|
coreApp: 'infsh/claude-sonnet-4@abc123', // LLM to use
|
|
193
197
|
systemPrompt: 'You are a helpful assistant.',
|
|
194
198
|
tools: [
|
|
@@ -235,7 +239,7 @@ Creates a new inference client.
|
|
|
235
239
|
| `config.apiKey` | `string` | Yes | Your inference.sh API key |
|
|
236
240
|
| `config.baseUrl` | `string` | No | Custom API URL (default: `https://api.inference.sh`) |
|
|
237
241
|
|
|
238
|
-
### `client.run(params, options?)`
|
|
242
|
+
### `client.tasks.run(params, options?)`
|
|
239
243
|
|
|
240
244
|
Runs a task on inference.sh.
|
|
241
245
|
|
|
@@ -259,11 +263,15 @@ Runs a task on inference.sh.
|
|
|
259
263
|
| `maxReconnects` | `number` | `5` | Max reconnection attempts |
|
|
260
264
|
| `reconnectDelayMs` | `number` | `1000` | Delay between reconnects (ms) |
|
|
261
265
|
|
|
262
|
-
### `client.
|
|
266
|
+
### `client.tasks.get(taskId)`
|
|
267
|
+
|
|
268
|
+
Gets a task by ID.
|
|
269
|
+
|
|
270
|
+
### `client.tasks.cancel(taskId)`
|
|
263
271
|
|
|
264
272
|
Cancels a running task.
|
|
265
273
|
|
|
266
|
-
### `client.
|
|
274
|
+
### `client.files.upload(data, options?)`
|
|
267
275
|
|
|
268
276
|
Uploads a file to inference.sh.
|
|
269
277
|
|
|
@@ -276,6 +284,24 @@ Uploads a file to inference.sh.
|
|
|
276
284
|
| `options.contentType` | `string` | MIME type |
|
|
277
285
|
| `options.public` | `boolean` | Make file publicly accessible |
|
|
278
286
|
|
|
287
|
+
### `client.agents.create(templateOrConfig)`
|
|
288
|
+
|
|
289
|
+
Creates an agent instance from a template or ad-hoc configuration.
|
|
290
|
+
|
|
291
|
+
**Template mode:**
|
|
292
|
+
```typescript
|
|
293
|
+
const agent = client.agents.create('namespace/name@version');
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
**Ad-hoc mode:**
|
|
297
|
+
```typescript
|
|
298
|
+
const agent = client.agents.create({
|
|
299
|
+
coreApp: 'infsh/claude-sonnet-4@abc123',
|
|
300
|
+
systemPrompt: 'You are helpful.',
|
|
301
|
+
tools: [...]
|
|
302
|
+
});
|
|
303
|
+
```
|
|
304
|
+
|
|
279
305
|
## Task Status Constants
|
|
280
306
|
|
|
281
307
|
```typescript
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { HttpClient } from '../http/client';
|
|
2
|
+
import { FilesAPI } from './files';
|
|
3
|
+
import { ChatDTO, ChatMessageDTO, AgentConfig, AgentDTO, AgentVersionDTO, CreateAgentRequest, File, CursorListRequest, CursorListResponse } from '../types';
|
|
4
|
+
/** Internal tool definition returned by getInternalTools */
|
|
5
|
+
export interface InternalToolDefinition {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
description: string;
|
|
9
|
+
tools: string[];
|
|
10
|
+
scope: string;
|
|
11
|
+
}
|
|
12
|
+
/** Options for creating an agent */
|
|
13
|
+
export interface AgentOptions {
|
|
14
|
+
/** Optional name for the adhoc agent (used for deduplication and display) */
|
|
15
|
+
name?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface SendMessageOptions {
|
|
18
|
+
/** File attachments - Blob (will be uploaded) or FileDTO (already uploaded, has uri) */
|
|
19
|
+
files?: (Blob | File)[];
|
|
20
|
+
/** Callback for message updates */
|
|
21
|
+
onMessage?: (message: ChatMessageDTO) => void;
|
|
22
|
+
/** Callback for chat updates */
|
|
23
|
+
onChat?: (chat: ChatDTO) => void;
|
|
24
|
+
/** Callback when a client tool needs execution */
|
|
25
|
+
onToolCall?: (invocation: {
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
args: Record<string, unknown>;
|
|
29
|
+
}) => void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Agent for chat interactions
|
|
33
|
+
*
|
|
34
|
+
* Created via `client.agent()` - do not instantiate directly.
|
|
35
|
+
*/
|
|
36
|
+
export declare class Agent {
|
|
37
|
+
private readonly http;
|
|
38
|
+
private readonly files;
|
|
39
|
+
private readonly config;
|
|
40
|
+
private readonly agentName;
|
|
41
|
+
private chatId;
|
|
42
|
+
private stream;
|
|
43
|
+
private dispatchedToolCalls;
|
|
44
|
+
/** @internal */
|
|
45
|
+
constructor(http: HttpClient, files: FilesAPI, config: string | AgentConfig, options?: AgentOptions);
|
|
46
|
+
/** Get current chat ID */
|
|
47
|
+
get currentChatId(): string | null;
|
|
48
|
+
/** Send a message to the agent */
|
|
49
|
+
sendMessage(text: string, options?: SendMessageOptions): Promise<{
|
|
50
|
+
userMessage: ChatMessageDTO;
|
|
51
|
+
assistantMessage: ChatMessageDTO;
|
|
52
|
+
}>;
|
|
53
|
+
/** Get chat by ID */
|
|
54
|
+
getChat(chatId?: string): Promise<ChatDTO | null>;
|
|
55
|
+
/** Stop the current chat generation */
|
|
56
|
+
stopChat(): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Submit a tool result
|
|
59
|
+
*/
|
|
60
|
+
submitToolResult(toolInvocationId: string, resultOrAction: string | {
|
|
61
|
+
action: {
|
|
62
|
+
type: string;
|
|
63
|
+
payload?: Record<string, unknown>;
|
|
64
|
+
};
|
|
65
|
+
form_data?: Record<string, unknown>;
|
|
66
|
+
}): Promise<void>;
|
|
67
|
+
/** Stop streaming and cleanup */
|
|
68
|
+
disconnect(): void;
|
|
69
|
+
/** Reset the agent (start fresh chat) */
|
|
70
|
+
reset(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Start streaming for the current chat.
|
|
73
|
+
*/
|
|
74
|
+
startStreaming(options?: Omit<SendMessageOptions, 'files'>): void;
|
|
75
|
+
/** Stream events until chat becomes idle */
|
|
76
|
+
private streamUntilIdle;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Agents API
|
|
80
|
+
*/
|
|
81
|
+
export declare class AgentsAPI {
|
|
82
|
+
private readonly http;
|
|
83
|
+
private readonly files;
|
|
84
|
+
constructor(http: HttpClient, files: FilesAPI);
|
|
85
|
+
/**
|
|
86
|
+
* List agent templates with cursor-based pagination
|
|
87
|
+
*/
|
|
88
|
+
list(params?: Partial<CursorListRequest>): Promise<CursorListResponse<AgentDTO>>;
|
|
89
|
+
/**
|
|
90
|
+
* Get an agent template by ID
|
|
91
|
+
*/
|
|
92
|
+
get(agentId: string): Promise<AgentDTO>;
|
|
93
|
+
/**
|
|
94
|
+
* Create a new agent template or create a new version of an existing agent
|
|
95
|
+
*/
|
|
96
|
+
createAgent(data: CreateAgentRequest): Promise<AgentDTO>;
|
|
97
|
+
/**
|
|
98
|
+
* Update an agent template
|
|
99
|
+
*/
|
|
100
|
+
update(agentId: string, data: Partial<AgentDTO>): Promise<AgentDTO>;
|
|
101
|
+
/**
|
|
102
|
+
* Delete an agent template
|
|
103
|
+
*/
|
|
104
|
+
delete(agentId: string): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* Duplicate an agent template
|
|
107
|
+
*/
|
|
108
|
+
duplicate(agentId: string): Promise<AgentDTO>;
|
|
109
|
+
/**
|
|
110
|
+
* List agent template versions
|
|
111
|
+
*/
|
|
112
|
+
listVersions(agentId: string, params?: Partial<CursorListRequest>): Promise<CursorListResponse<AgentVersionDTO>>;
|
|
113
|
+
/**
|
|
114
|
+
* Transfer agent ownership to another team
|
|
115
|
+
*/
|
|
116
|
+
transferOwnership(agentId: string, newTeamId: string): Promise<AgentDTO>;
|
|
117
|
+
/**
|
|
118
|
+
* Update agent visibility
|
|
119
|
+
*/
|
|
120
|
+
updateVisibility(agentId: string, visibility: string): Promise<AgentDTO>;
|
|
121
|
+
/**
|
|
122
|
+
* Get a specific agent version
|
|
123
|
+
*/
|
|
124
|
+
getVersion(agentId: string, versionId: string): Promise<AgentVersionDTO>;
|
|
125
|
+
/**
|
|
126
|
+
* Get internal tools for the agent
|
|
127
|
+
*/
|
|
128
|
+
getInternalTools(): Promise<InternalToolDefinition[]>;
|
|
129
|
+
/**
|
|
130
|
+
* Create an agent instance for chat interactions
|
|
131
|
+
*/
|
|
132
|
+
create(config: string | AgentConfig, options?: AgentOptions): Agent;
|
|
133
|
+
/**
|
|
134
|
+
* Submit a tool result
|
|
135
|
+
*/
|
|
136
|
+
submitToolResult(toolInvocationId: string, resultOrAction: string | {
|
|
137
|
+
action: {
|
|
138
|
+
type: string;
|
|
139
|
+
payload?: Record<string, unknown>;
|
|
140
|
+
};
|
|
141
|
+
form_data?: Record<string, unknown>;
|
|
142
|
+
}): Promise<void>;
|
|
143
|
+
}
|
|
144
|
+
export declare function createAgentsAPI(http: HttpClient, files: FilesAPI): AgentsAPI;
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentsAPI = exports.Agent = void 0;
|
|
4
|
+
exports.createAgentsAPI = createAgentsAPI;
|
|
5
|
+
const stream_1 = require("../http/stream");
|
|
6
|
+
const types_1 = require("../types");
|
|
7
|
+
/**
|
|
8
|
+
* Agent for chat interactions
|
|
9
|
+
*
|
|
10
|
+
* Created via `client.agent()` - do not instantiate directly.
|
|
11
|
+
*/
|
|
12
|
+
class Agent {
|
|
13
|
+
/** @internal */
|
|
14
|
+
constructor(http, files, config, options) {
|
|
15
|
+
this.chatId = null;
|
|
16
|
+
this.stream = null;
|
|
17
|
+
this.dispatchedToolCalls = new Set();
|
|
18
|
+
this.http = http;
|
|
19
|
+
this.files = files;
|
|
20
|
+
this.config = config;
|
|
21
|
+
this.agentName = options?.name;
|
|
22
|
+
}
|
|
23
|
+
/** Get current chat ID */
|
|
24
|
+
get currentChatId() {
|
|
25
|
+
return this.chatId;
|
|
26
|
+
}
|
|
27
|
+
/** Send a message to the agent */
|
|
28
|
+
async sendMessage(text, options = {}) {
|
|
29
|
+
const isTemplate = typeof this.config === 'string';
|
|
30
|
+
const hasCallbacks = !!(options.onMessage || options.onChat || options.onToolCall);
|
|
31
|
+
// Process files - either already uploaded (FileDTO with uri) or needs upload (Blob)
|
|
32
|
+
let imageUris;
|
|
33
|
+
let fileUris;
|
|
34
|
+
if (options.files && options.files.length > 0) {
|
|
35
|
+
const toUpload = [];
|
|
36
|
+
const alreadyUploaded = [];
|
|
37
|
+
for (const file of options.files) {
|
|
38
|
+
if ('uri' in file && typeof file.uri === 'string') {
|
|
39
|
+
alreadyUploaded.push(file);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
toUpload.push(file);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const uploadedFiles = toUpload.length > 0 ? await Promise.all(toUpload.map((blob) => this.files.upload(blob))) : [];
|
|
46
|
+
const allFiles = [...alreadyUploaded, ...uploadedFiles];
|
|
47
|
+
const images = allFiles.filter((f) => f.content_type?.startsWith('image/'));
|
|
48
|
+
const others = allFiles.filter((f) => !f.content_type?.startsWith('image/'));
|
|
49
|
+
if (images.length > 0)
|
|
50
|
+
imageUris = images.map((f) => f.uri);
|
|
51
|
+
if (others.length > 0)
|
|
52
|
+
fileUris = others.map((f) => f.uri);
|
|
53
|
+
}
|
|
54
|
+
const body = isTemplate
|
|
55
|
+
? {
|
|
56
|
+
chat_id: this.chatId,
|
|
57
|
+
agent: this.config,
|
|
58
|
+
input: { text, images: imageUris, files: fileUris, role: 'user', context: [], system_prompt: '', context_size: 0 },
|
|
59
|
+
}
|
|
60
|
+
: {
|
|
61
|
+
chat_id: this.chatId,
|
|
62
|
+
agent_config: this.config,
|
|
63
|
+
agent_name: this.agentName,
|
|
64
|
+
input: { text, images: imageUris, files: fileUris, role: 'user', context: [], system_prompt: '', context_size: 0 },
|
|
65
|
+
};
|
|
66
|
+
// For existing chats with callbacks: Start streaming BEFORE POST so we don't miss updates
|
|
67
|
+
let streamPromise = null;
|
|
68
|
+
if (this.chatId && hasCallbacks) {
|
|
69
|
+
streamPromise = this.streamUntilIdle(options);
|
|
70
|
+
}
|
|
71
|
+
// Make the POST request
|
|
72
|
+
const response = await this.http.request('post', '/agents/run', { data: body });
|
|
73
|
+
// For new chats: Set chatId and start streaming immediately after POST
|
|
74
|
+
const isNewChat = !this.chatId && response.assistant_message.chat_id;
|
|
75
|
+
if (isNewChat) {
|
|
76
|
+
this.chatId = response.assistant_message.chat_id;
|
|
77
|
+
if (hasCallbacks) {
|
|
78
|
+
streamPromise = this.streamUntilIdle(options);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// Wait for streaming to complete
|
|
82
|
+
if (streamPromise) {
|
|
83
|
+
await streamPromise;
|
|
84
|
+
}
|
|
85
|
+
return { userMessage: response.user_message, assistantMessage: response.assistant_message };
|
|
86
|
+
}
|
|
87
|
+
/** Get chat by ID */
|
|
88
|
+
async getChat(chatId) {
|
|
89
|
+
const id = chatId || this.chatId;
|
|
90
|
+
if (!id)
|
|
91
|
+
return null;
|
|
92
|
+
return this.http.request('get', `/chats/${id}`);
|
|
93
|
+
}
|
|
94
|
+
/** Stop the current chat generation */
|
|
95
|
+
async stopChat() {
|
|
96
|
+
if (!this.chatId)
|
|
97
|
+
return;
|
|
98
|
+
await this.http.request('post', `/chats/${this.chatId}/stop`);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Submit a tool result
|
|
102
|
+
*/
|
|
103
|
+
async submitToolResult(toolInvocationId, resultOrAction) {
|
|
104
|
+
const result = typeof resultOrAction === 'string' ? resultOrAction : JSON.stringify(resultOrAction);
|
|
105
|
+
await this.http.request('post', `/tools/${toolInvocationId}`, { data: { result } });
|
|
106
|
+
}
|
|
107
|
+
/** Stop streaming and cleanup */
|
|
108
|
+
disconnect() {
|
|
109
|
+
this.stream?.stop();
|
|
110
|
+
this.stream = null;
|
|
111
|
+
}
|
|
112
|
+
/** Reset the agent (start fresh chat) */
|
|
113
|
+
reset() {
|
|
114
|
+
this.disconnect();
|
|
115
|
+
this.chatId = null;
|
|
116
|
+
this.dispatchedToolCalls.clear();
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Start streaming for the current chat.
|
|
120
|
+
*/
|
|
121
|
+
startStreaming(options = {}) {
|
|
122
|
+
if (!this.chatId)
|
|
123
|
+
return;
|
|
124
|
+
this.streamUntilIdle(options);
|
|
125
|
+
}
|
|
126
|
+
/** Stream events until chat becomes idle */
|
|
127
|
+
streamUntilIdle(options) {
|
|
128
|
+
if (!this.chatId)
|
|
129
|
+
return Promise.resolve();
|
|
130
|
+
return new Promise((resolve) => {
|
|
131
|
+
this.stream?.stop();
|
|
132
|
+
this.stream = new stream_1.StreamManager({
|
|
133
|
+
createEventSource: async () => this.http.createEventSource(`/chats/${this.chatId}/stream`),
|
|
134
|
+
autoReconnect: true,
|
|
135
|
+
});
|
|
136
|
+
this.stream.addEventListener('chats', (chat) => {
|
|
137
|
+
options.onChat?.(chat);
|
|
138
|
+
if (chat.status === 'idle') {
|
|
139
|
+
resolve();
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
this.stream.addEventListener('chat_messages', (message) => {
|
|
143
|
+
options.onMessage?.(message);
|
|
144
|
+
if (message.tool_invocations && options.onToolCall) {
|
|
145
|
+
for (const inv of message.tool_invocations) {
|
|
146
|
+
if (this.dispatchedToolCalls.has(inv.id))
|
|
147
|
+
continue;
|
|
148
|
+
if (inv.type === types_1.ToolTypeClient && inv.status === types_1.ToolInvocationStatusAwaitingInput) {
|
|
149
|
+
this.dispatchedToolCalls.add(inv.id);
|
|
150
|
+
options.onToolCall({
|
|
151
|
+
id: inv.id,
|
|
152
|
+
name: inv.function?.name || '',
|
|
153
|
+
args: inv.function?.arguments || {},
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
this.stream.connect();
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
exports.Agent = Agent;
|
|
164
|
+
/**
|
|
165
|
+
* Agents API
|
|
166
|
+
*/
|
|
167
|
+
class AgentsAPI {
|
|
168
|
+
constructor(http, files) {
|
|
169
|
+
this.http = http;
|
|
170
|
+
this.files = files;
|
|
171
|
+
}
|
|
172
|
+
// ==========================================================================
|
|
173
|
+
// Agent Template CRUD (stored agent configurations)
|
|
174
|
+
// ==========================================================================
|
|
175
|
+
/**
|
|
176
|
+
* List agent templates with cursor-based pagination
|
|
177
|
+
*/
|
|
178
|
+
async list(params) {
|
|
179
|
+
return this.http.request('post', '/agents/list', { data: params });
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Get an agent template by ID
|
|
183
|
+
*/
|
|
184
|
+
async get(agentId) {
|
|
185
|
+
return this.http.request('get', `/agents/${agentId}`);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Create a new agent template or create a new version of an existing agent
|
|
189
|
+
*/
|
|
190
|
+
async createAgent(data) {
|
|
191
|
+
return this.http.request('post', '/agents', { data });
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Update an agent template
|
|
195
|
+
*/
|
|
196
|
+
async update(agentId, data) {
|
|
197
|
+
return this.http.request('put', `/agents/${agentId}`, { data });
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Delete an agent template
|
|
201
|
+
*/
|
|
202
|
+
async delete(agentId) {
|
|
203
|
+
return this.http.request('delete', `/agents/${agentId}`);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Duplicate an agent template
|
|
207
|
+
*/
|
|
208
|
+
async duplicate(agentId) {
|
|
209
|
+
return this.http.request('post', `/agents/${agentId}/duplicate`);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* List agent template versions
|
|
213
|
+
*/
|
|
214
|
+
async listVersions(agentId, params) {
|
|
215
|
+
return this.http.request('post', `/agents/${agentId}/versions/list`, { data: params });
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Transfer agent ownership to another team
|
|
219
|
+
*/
|
|
220
|
+
async transferOwnership(agentId, newTeamId) {
|
|
221
|
+
return this.http.request('post', `/agents/${agentId}/transfer`, { data: { team_id: newTeamId } });
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Update agent visibility
|
|
225
|
+
*/
|
|
226
|
+
async updateVisibility(agentId, visibility) {
|
|
227
|
+
return this.http.request('put', `/agents/${agentId}/visibility`, { data: { visibility } });
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Get a specific agent version
|
|
231
|
+
*/
|
|
232
|
+
async getVersion(agentId, versionId) {
|
|
233
|
+
return this.http.request('get', `/agents/${agentId}/versions/${versionId}`);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Get internal tools for the agent
|
|
237
|
+
*/
|
|
238
|
+
async getInternalTools() {
|
|
239
|
+
return this.http.request('get', '/agents/internal-tools');
|
|
240
|
+
}
|
|
241
|
+
// ==========================================================================
|
|
242
|
+
// Agent Runtime (chat interactions)
|
|
243
|
+
// ==========================================================================
|
|
244
|
+
/**
|
|
245
|
+
* Create an agent instance for chat interactions
|
|
246
|
+
*/
|
|
247
|
+
create(config, options) {
|
|
248
|
+
return new Agent(this.http, this.files, config, options);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Submit a tool result
|
|
252
|
+
*/
|
|
253
|
+
async submitToolResult(toolInvocationId, resultOrAction) {
|
|
254
|
+
const result = typeof resultOrAction === 'string' ? resultOrAction : JSON.stringify(resultOrAction);
|
|
255
|
+
await this.http.request('post', `/tools/${toolInvocationId}`, { data: { result } });
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
exports.AgentsAPI = AgentsAPI;
|
|
259
|
+
function createAgentsAPI(http, files) {
|
|
260
|
+
return new AgentsAPI(http, files);
|
|
261
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { HttpClient } from '../http/client';
|
|
2
|
+
import { AppDTO as App, AppVersionDTO, CursorListRequest, CursorListResponse } from '../types';
|
|
3
|
+
interface LicenseRecord {
|
|
4
|
+
id: string;
|
|
5
|
+
created_at: string;
|
|
6
|
+
updated_at: string;
|
|
7
|
+
deleted_at?: string;
|
|
8
|
+
user_id: string;
|
|
9
|
+
app_id: string;
|
|
10
|
+
license: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Apps API
|
|
14
|
+
*/
|
|
15
|
+
export declare class AppsAPI {
|
|
16
|
+
private readonly http;
|
|
17
|
+
constructor(http: HttpClient);
|
|
18
|
+
/**
|
|
19
|
+
* List apps with cursor-based pagination
|
|
20
|
+
*/
|
|
21
|
+
list(params?: Partial<CursorListRequest>): Promise<CursorListResponse<App>>;
|
|
22
|
+
/**
|
|
23
|
+
* Get an app by ID
|
|
24
|
+
*/
|
|
25
|
+
get(appId: string): Promise<App>;
|
|
26
|
+
/**
|
|
27
|
+
* Get an app by version ID
|
|
28
|
+
*/
|
|
29
|
+
getByVersionId(appId: string, versionId: string): Promise<App>;
|
|
30
|
+
/**
|
|
31
|
+
* Create a new app
|
|
32
|
+
*/
|
|
33
|
+
create(data: Partial<App>): Promise<App>;
|
|
34
|
+
/**
|
|
35
|
+
* Update an app
|
|
36
|
+
*/
|
|
37
|
+
update(appId: string, data: Partial<App>): Promise<App>;
|
|
38
|
+
/**
|
|
39
|
+
* Delete an app
|
|
40
|
+
*/
|
|
41
|
+
delete(appId: string): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Duplicate an app
|
|
44
|
+
*/
|
|
45
|
+
duplicate(appId: string): Promise<App>;
|
|
46
|
+
/**
|
|
47
|
+
* List app versions
|
|
48
|
+
*/
|
|
49
|
+
listVersions(appId: string, params?: Partial<CursorListRequest>): Promise<CursorListResponse<AppVersionDTO>>;
|
|
50
|
+
/**
|
|
51
|
+
* Transfer app ownership to another team
|
|
52
|
+
*/
|
|
53
|
+
transferOwnership(appId: string, newTeamId: string): Promise<App>;
|
|
54
|
+
/**
|
|
55
|
+
* Update app visibility
|
|
56
|
+
*/
|
|
57
|
+
updateVisibility(appId: string, visibility: string): Promise<App>;
|
|
58
|
+
/**
|
|
59
|
+
* Get an app by namespace and name (e.g., "inference/claude-haiku")
|
|
60
|
+
*/
|
|
61
|
+
getByName(name: string): Promise<App>;
|
|
62
|
+
/**
|
|
63
|
+
* Get app license record
|
|
64
|
+
*/
|
|
65
|
+
getLicense(appId: string): Promise<LicenseRecord>;
|
|
66
|
+
/**
|
|
67
|
+
* Save app license
|
|
68
|
+
*/
|
|
69
|
+
saveLicense(appId: string, license: string): Promise<LicenseRecord>;
|
|
70
|
+
}
|
|
71
|
+
export declare function createAppsAPI(http: HttpClient): AppsAPI;
|
|
72
|
+
export {};
|