agi 0.2.2 → 0.4.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/LICENSE +21 -0
- package/README.md +536 -14
- package/dist/index.d.mts +1427 -0
- package/dist/index.d.ts +1427 -0
- package/dist/index.js +1742 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1723 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +76 -32
- package/LICENSE.pdf +0 -0
- package/scripts/install-dmg.js +0 -74
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1427 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Result types for task execution
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Task execution metadata and performance metrics
|
|
8
|
+
*/
|
|
9
|
+
interface TaskMetadata {
|
|
10
|
+
/** Unique task identifier */
|
|
11
|
+
taskId: string | number;
|
|
12
|
+
/** Session ID */
|
|
13
|
+
sessionId: string;
|
|
14
|
+
/** Execution time in seconds */
|
|
15
|
+
duration: number;
|
|
16
|
+
/** Task cost in USD (not yet provided by API) */
|
|
17
|
+
cost: number;
|
|
18
|
+
/** Task completion timestamp */
|
|
19
|
+
timestamp: Date;
|
|
20
|
+
/** Number of steps executed */
|
|
21
|
+
steps: number;
|
|
22
|
+
/** Whether task succeeded */
|
|
23
|
+
success: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Result of task execution
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const result = await session.runTask("Find flights...");
|
|
31
|
+
* console.log(result.data);
|
|
32
|
+
* console.log(`Duration: ${result.metadata.duration}s`);
|
|
33
|
+
* console.log(`Steps: ${result.metadata.steps}`);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
interface TaskResult<T = unknown> {
|
|
37
|
+
/** Task output data */
|
|
38
|
+
data: T;
|
|
39
|
+
/** Execution metadata */
|
|
40
|
+
metadata: TaskMetadata;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Screenshot handling with image data and metadata
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* Browser screenshot with decoded image data and metadata
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const screenshot = await session.screenshot();
|
|
52
|
+
* await screenshot.save("page.png");
|
|
53
|
+
* console.log(`Size: ${screenshot.width}x${screenshot.height}`);
|
|
54
|
+
* console.log(`URL: ${screenshot.url}`);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare class Screenshot {
|
|
58
|
+
readonly data: Buffer;
|
|
59
|
+
readonly format: 'png' | 'jpg';
|
|
60
|
+
readonly timestamp: Date;
|
|
61
|
+
readonly width: number;
|
|
62
|
+
readonly height: number;
|
|
63
|
+
readonly url: string;
|
|
64
|
+
readonly title: string;
|
|
65
|
+
constructor(data: Buffer, format: 'png' | 'jpg', timestamp: Date, width: number, height: number, url: string, title: string);
|
|
66
|
+
/**
|
|
67
|
+
* Save screenshot to file
|
|
68
|
+
*
|
|
69
|
+
* @param path - File path to save to (e.g., "screenshot.png")
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const screenshot = await session.screenshot();
|
|
74
|
+
* await screenshot.save("amazon.png");
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
save(path: string): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Create Screenshot from base64 data URL
|
|
80
|
+
*
|
|
81
|
+
* @param base64Data - Base64-encoded data URL (e.g., "data:image/jpeg;base64,...")
|
|
82
|
+
* @param url - Current page URL
|
|
83
|
+
* @param title - Current page title
|
|
84
|
+
* @param timestamp - Screenshot timestamp (defaults to now)
|
|
85
|
+
* @returns Screenshot instance with decoded image data
|
|
86
|
+
*/
|
|
87
|
+
static fromBase64(base64Data: string, url: string, title: string, timestamp?: Date): Screenshot;
|
|
88
|
+
/**
|
|
89
|
+
* Extract width and height from image data
|
|
90
|
+
*
|
|
91
|
+
* @param data - Raw image bytes
|
|
92
|
+
* @param format - Image format (png, jpg)
|
|
93
|
+
* @returns Tuple of (width, height)
|
|
94
|
+
*/
|
|
95
|
+
private static getImageDimensions;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Type definitions for AGI SDK
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
type SnapshotMode = 'none' | 'memory' | 'filesystem';
|
|
103
|
+
type AgentSessionType = 'managed-cdp' | 'external-cdp' | 'desktop';
|
|
104
|
+
type SessionStatus = 'ready' | 'running' | 'waiting_for_input' | 'paused' | 'finished' | 'error';
|
|
105
|
+
type EventType$1 = 'step' | 'thought' | 'question' | 'done' | 'error' | 'log' | 'paused' | 'resumed' | 'heartbeat' | 'user';
|
|
106
|
+
type MessageType = 'THOUGHT' | 'QUESTION' | 'USER' | 'DONE' | 'ERROR' | 'LOG';
|
|
107
|
+
interface AGIClientOptions {
|
|
108
|
+
/** API key for authentication */
|
|
109
|
+
apiKey: string;
|
|
110
|
+
/** Base URL for API (default: https://api.agi.tech) */
|
|
111
|
+
baseUrl?: string;
|
|
112
|
+
/** Request timeout in milliseconds (default: 60000) */
|
|
113
|
+
timeout?: number;
|
|
114
|
+
/** Maximum number of retries (default: 3) */
|
|
115
|
+
maxRetries?: number;
|
|
116
|
+
}
|
|
117
|
+
interface CreateSessionOptions {
|
|
118
|
+
/** Webhook URL for session event notifications */
|
|
119
|
+
webhookUrl?: string;
|
|
120
|
+
/** Task goal (optional, can be set later via sendMessage) */
|
|
121
|
+
goal?: string;
|
|
122
|
+
/** Maximum number of agent steps (default: 100) */
|
|
123
|
+
maxSteps?: number;
|
|
124
|
+
/** Environment UUID to restore from */
|
|
125
|
+
restoreFromEnvironmentId?: string;
|
|
126
|
+
/** Agent session type */
|
|
127
|
+
agentSessionType?: AgentSessionType;
|
|
128
|
+
/** External CDP WebSocket URL (required for external-cdp session type) */
|
|
129
|
+
cdpUrl?: string;
|
|
130
|
+
}
|
|
131
|
+
interface SessionResponse {
|
|
132
|
+
/** Session UUID */
|
|
133
|
+
sessionId: string;
|
|
134
|
+
/** VNC URL for browser access */
|
|
135
|
+
vncUrl?: string;
|
|
136
|
+
/** Agent service URL (desktop mode) */
|
|
137
|
+
agentUrl?: string;
|
|
138
|
+
/** Agent name */
|
|
139
|
+
agentName: string;
|
|
140
|
+
/** Session status */
|
|
141
|
+
status: SessionStatus;
|
|
142
|
+
/** Session creation timestamp */
|
|
143
|
+
createdAt: string;
|
|
144
|
+
/** Environment UUID for restore */
|
|
145
|
+
environmentId?: string;
|
|
146
|
+
/** Task goal */
|
|
147
|
+
goal?: string;
|
|
148
|
+
/** Agent session type */
|
|
149
|
+
agentSessionType?: string;
|
|
150
|
+
}
|
|
151
|
+
interface ExecuteStatusResponse {
|
|
152
|
+
/** Current execution status */
|
|
153
|
+
status: SessionStatus;
|
|
154
|
+
}
|
|
155
|
+
interface MessageResponse {
|
|
156
|
+
/** Message ID */
|
|
157
|
+
id: number;
|
|
158
|
+
/** Message type */
|
|
159
|
+
type: MessageType;
|
|
160
|
+
/** Message content */
|
|
161
|
+
content: string | Record<string, unknown> | Array<Record<string, unknown>>;
|
|
162
|
+
/** ISO timestamp */
|
|
163
|
+
timestamp: string;
|
|
164
|
+
/** Additional metadata */
|
|
165
|
+
metadata?: Record<string, unknown>;
|
|
166
|
+
}
|
|
167
|
+
interface MessagesResponse {
|
|
168
|
+
/** List of messages */
|
|
169
|
+
messages: MessageResponse[];
|
|
170
|
+
/** Current execution status */
|
|
171
|
+
status: string;
|
|
172
|
+
/** Whether agent is connected */
|
|
173
|
+
hasAgent: boolean;
|
|
174
|
+
}
|
|
175
|
+
interface SSEEvent {
|
|
176
|
+
/** Event ID */
|
|
177
|
+
id?: string;
|
|
178
|
+
/** Event type */
|
|
179
|
+
event: EventType$1;
|
|
180
|
+
/** Event data */
|
|
181
|
+
data: Record<string, unknown>;
|
|
182
|
+
}
|
|
183
|
+
interface NavigateResponse {
|
|
184
|
+
/** Current URL after navigation */
|
|
185
|
+
currentUrl: string;
|
|
186
|
+
}
|
|
187
|
+
interface ScreenshotResponse {
|
|
188
|
+
/** Base64-encoded JPEG data URL */
|
|
189
|
+
screenshot: string;
|
|
190
|
+
/** Current page URL */
|
|
191
|
+
url: string;
|
|
192
|
+
/** Current page title */
|
|
193
|
+
title: string;
|
|
194
|
+
}
|
|
195
|
+
interface SuccessResponse {
|
|
196
|
+
/** Operation success */
|
|
197
|
+
success: boolean;
|
|
198
|
+
/** Success message */
|
|
199
|
+
message: string;
|
|
200
|
+
}
|
|
201
|
+
interface DeleteResponse {
|
|
202
|
+
/** Operation success */
|
|
203
|
+
success: boolean;
|
|
204
|
+
/** Whether resource was deleted */
|
|
205
|
+
deleted: boolean;
|
|
206
|
+
/** Response message */
|
|
207
|
+
message: string;
|
|
208
|
+
}
|
|
209
|
+
interface MessageOptions {
|
|
210
|
+
/** Optional starting URL */
|
|
211
|
+
startUrl?: string;
|
|
212
|
+
/** Optional configuration updates */
|
|
213
|
+
configUpdates?: Record<string, unknown>;
|
|
214
|
+
}
|
|
215
|
+
interface RunTaskOptions {
|
|
216
|
+
/** Optional starting URL */
|
|
217
|
+
startUrl?: string;
|
|
218
|
+
/** Optional configuration updates */
|
|
219
|
+
configUpdates?: Record<string, unknown>;
|
|
220
|
+
/** Maximum time to wait for task completion in milliseconds (default: 600000 = 10 min) */
|
|
221
|
+
timeout?: number;
|
|
222
|
+
/** Polling interval in milliseconds (default: 3000 = 3s) */
|
|
223
|
+
pollInterval?: number;
|
|
224
|
+
}
|
|
225
|
+
interface StreamOptions {
|
|
226
|
+
/** Event types to filter */
|
|
227
|
+
eventTypes?: EventType$1[];
|
|
228
|
+
/** Sanitize event data */
|
|
229
|
+
sanitize?: boolean;
|
|
230
|
+
/** Include historical events */
|
|
231
|
+
includeHistory?: boolean;
|
|
232
|
+
}
|
|
233
|
+
type DesktopActionType = 'click' | 'double_click' | 'triple_click' | 'right_click' | 'hover' | 'type' | 'key' | 'scroll' | 'drag' | 'wait' | 'finish' | 'fail' | 'confirm' | 'ask_question';
|
|
234
|
+
interface DesktopAction {
|
|
235
|
+
/** Action type */
|
|
236
|
+
type: DesktopActionType | string;
|
|
237
|
+
/** X coordinate (for click, hover, scroll) */
|
|
238
|
+
x?: number;
|
|
239
|
+
/** Y coordinate (for click, hover, scroll) */
|
|
240
|
+
y?: number;
|
|
241
|
+
/** Text to type (for type action) */
|
|
242
|
+
text?: string;
|
|
243
|
+
/** Content to type (alias for text) */
|
|
244
|
+
content?: string;
|
|
245
|
+
/** Key or key combination (for key action) */
|
|
246
|
+
key?: string;
|
|
247
|
+
/** Scroll direction (for scroll action) */
|
|
248
|
+
direction?: 'up' | 'down' | 'left' | 'right';
|
|
249
|
+
/** Scroll amount in lines (for scroll action) */
|
|
250
|
+
amount?: number;
|
|
251
|
+
/** Wait duration in seconds (for wait action) */
|
|
252
|
+
duration?: number;
|
|
253
|
+
/** Start X coordinate (for drag action) */
|
|
254
|
+
start_x?: number;
|
|
255
|
+
/** Start Y coordinate (for drag action) */
|
|
256
|
+
start_y?: number;
|
|
257
|
+
/** End X coordinate (for drag action) */
|
|
258
|
+
end_x?: number;
|
|
259
|
+
/** End Y coordinate (for drag action) */
|
|
260
|
+
end_y?: number;
|
|
261
|
+
/** Summary message (for finish action) */
|
|
262
|
+
summary?: string;
|
|
263
|
+
/** Reason message (for fail action) */
|
|
264
|
+
reason?: string;
|
|
265
|
+
/** Question to ask (for ask_question action) */
|
|
266
|
+
question?: string;
|
|
267
|
+
/** Additional action properties */
|
|
268
|
+
[key: string]: unknown;
|
|
269
|
+
}
|
|
270
|
+
interface StepDesktopRequest {
|
|
271
|
+
/** Base64-encoded screenshot (full resolution). Viewport dimensions are extracted from the image. */
|
|
272
|
+
screenshot: string;
|
|
273
|
+
/** Optional user message (e.g., initial goal or follow-up instruction) */
|
|
274
|
+
message?: string;
|
|
275
|
+
}
|
|
276
|
+
interface StepDesktopResponse {
|
|
277
|
+
/** Actions to execute (flat format) */
|
|
278
|
+
actions: DesktopAction[];
|
|
279
|
+
/** Model reasoning */
|
|
280
|
+
thinking?: string;
|
|
281
|
+
/** Whether the task is complete */
|
|
282
|
+
finished: boolean;
|
|
283
|
+
/** Question for user, if any */
|
|
284
|
+
askUser?: string;
|
|
285
|
+
/** Current step number */
|
|
286
|
+
step: number;
|
|
287
|
+
}
|
|
288
|
+
interface ModelsResponse {
|
|
289
|
+
/** Available agent models */
|
|
290
|
+
models: string[];
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Session context manager for high-level API
|
|
295
|
+
*/
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* High-level session context manager with automatic cleanup
|
|
299
|
+
*
|
|
300
|
+
* Use with `await using` for automatic session deletion:
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```typescript
|
|
304
|
+
* await using session = client.session('agi-0');
|
|
305
|
+
* const result = await session.runTask('Find flights...');
|
|
306
|
+
* // Session automatically deleted
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
declare class SessionContext {
|
|
310
|
+
private readonly client;
|
|
311
|
+
private readonly agentName;
|
|
312
|
+
private readonly createOptions?;
|
|
313
|
+
sessionId?: string;
|
|
314
|
+
vncUrl?: string;
|
|
315
|
+
agentUrl?: string;
|
|
316
|
+
constructor(client: AGIClient, agentName?: string, createOptions?: {
|
|
317
|
+
webhookUrl?: string;
|
|
318
|
+
goal?: string;
|
|
319
|
+
maxSteps?: number;
|
|
320
|
+
restoreFromEnvironmentId?: string;
|
|
321
|
+
} | undefined);
|
|
322
|
+
/**
|
|
323
|
+
* Automatic cleanup via explicit resource management
|
|
324
|
+
*/
|
|
325
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
326
|
+
/**
|
|
327
|
+
* Ensure session is created
|
|
328
|
+
*/
|
|
329
|
+
private ensureSession;
|
|
330
|
+
/**
|
|
331
|
+
* Run a task and wait for completion using HTTP polling
|
|
332
|
+
*
|
|
333
|
+
* This method uses HTTP polling instead of SSE streaming for better reliability
|
|
334
|
+
* with long-running tasks and network instability.
|
|
335
|
+
*
|
|
336
|
+
* @param task - Natural language task description
|
|
337
|
+
* @param options - Task execution options
|
|
338
|
+
* @returns TaskResult with data and execution metadata
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```typescript
|
|
342
|
+
* const result = await session.runTask(
|
|
343
|
+
* 'Find cheapest iPhone 15 Pro',
|
|
344
|
+
* { timeout: 300000, pollInterval: 2000 } // 5 min timeout, 2s polling
|
|
345
|
+
* );
|
|
346
|
+
* console.log(result.data);
|
|
347
|
+
* console.log(`Took ${result.metadata.duration}s, ${result.metadata.steps} steps`);
|
|
348
|
+
* ```
|
|
349
|
+
*/
|
|
350
|
+
runTask(task: string, options?: {
|
|
351
|
+
startUrl?: string;
|
|
352
|
+
timeout?: number;
|
|
353
|
+
pollInterval?: number;
|
|
354
|
+
}): Promise<TaskResult>;
|
|
355
|
+
/**
|
|
356
|
+
* Send a message to the agent
|
|
357
|
+
*
|
|
358
|
+
* @param message - Message content
|
|
359
|
+
* @param options - Message options
|
|
360
|
+
* @returns SuccessResponse confirming message was sent
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```typescript
|
|
364
|
+
* await session.sendMessage('Find flights from SFO to JFK under $450');
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
sendMessage(message: string, options?: {
|
|
368
|
+
startUrl?: string;
|
|
369
|
+
configUpdates?: Record<string, unknown>;
|
|
370
|
+
}): Promise<SuccessResponse>;
|
|
371
|
+
/**
|
|
372
|
+
* Get current execution status
|
|
373
|
+
*
|
|
374
|
+
* @returns ExecuteStatusResponse with status
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* ```typescript
|
|
378
|
+
* const status = await session.getStatus();
|
|
379
|
+
* console.log(status.status);
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
getStatus(): Promise<ExecuteStatusResponse>;
|
|
383
|
+
/**
|
|
384
|
+
* Get messages from the session
|
|
385
|
+
*
|
|
386
|
+
* @param afterId - Return messages with ID > afterId (for polling)
|
|
387
|
+
* @param sanitize - Filter out system messages, prompts, and images
|
|
388
|
+
* @returns MessagesResponse with messages list and status
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* ```typescript
|
|
392
|
+
* const messages = await session.getMessages(0);
|
|
393
|
+
* for (const msg of messages.messages) {
|
|
394
|
+
* console.log(`[${msg.type}] ${msg.content}`);
|
|
395
|
+
* }
|
|
396
|
+
* ```
|
|
397
|
+
*/
|
|
398
|
+
getMessages(afterId?: number, sanitize?: boolean): Promise<MessagesResponse>;
|
|
399
|
+
/**
|
|
400
|
+
* Stream real-time events from the session via Server-Sent Events
|
|
401
|
+
*
|
|
402
|
+
* @param options - Stream options
|
|
403
|
+
* @yields SSEEvent objects
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* ```typescript
|
|
407
|
+
* for await (const event of session.streamEvents()) {
|
|
408
|
+
* if (event.event === 'thought') {
|
|
409
|
+
* console.log('Agent:', event.data);
|
|
410
|
+
* }
|
|
411
|
+
* if (event.event === 'done') {
|
|
412
|
+
* console.log('Result:', event.data);
|
|
413
|
+
* break;
|
|
414
|
+
* }
|
|
415
|
+
* }
|
|
416
|
+
* ```
|
|
417
|
+
*/
|
|
418
|
+
streamEvents(options?: {
|
|
419
|
+
eventTypes?: string[];
|
|
420
|
+
sanitize?: boolean;
|
|
421
|
+
includeHistory?: boolean;
|
|
422
|
+
}): AsyncGenerator<SSEEvent>;
|
|
423
|
+
/**
|
|
424
|
+
* Pause task execution
|
|
425
|
+
*
|
|
426
|
+
* @returns SuccessResponse confirming pause
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```typescript
|
|
430
|
+
* await session.pause();
|
|
431
|
+
* ```
|
|
432
|
+
*/
|
|
433
|
+
pause(): Promise<SuccessResponse>;
|
|
434
|
+
/**
|
|
435
|
+
* Resume paused task
|
|
436
|
+
*
|
|
437
|
+
* @returns SuccessResponse confirming resume
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```typescript
|
|
441
|
+
* await session.resume();
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
444
|
+
resume(): Promise<SuccessResponse>;
|
|
445
|
+
/**
|
|
446
|
+
* Cancel task execution
|
|
447
|
+
*
|
|
448
|
+
* @returns SuccessResponse confirming cancellation
|
|
449
|
+
*
|
|
450
|
+
* @example
|
|
451
|
+
* ```typescript
|
|
452
|
+
* await session.cancel();
|
|
453
|
+
* ```
|
|
454
|
+
*/
|
|
455
|
+
cancel(): Promise<SuccessResponse>;
|
|
456
|
+
/**
|
|
457
|
+
* Navigate browser to URL
|
|
458
|
+
*
|
|
459
|
+
* @param url - URL to navigate to
|
|
460
|
+
* @returns NavigateResponse with current URL
|
|
461
|
+
*
|
|
462
|
+
* @example
|
|
463
|
+
* ```typescript
|
|
464
|
+
* await session.navigate('https://amazon.com');
|
|
465
|
+
* ```
|
|
466
|
+
*/
|
|
467
|
+
navigate(url: string): Promise<NavigateResponse>;
|
|
468
|
+
/**
|
|
469
|
+
* Get browser screenshot
|
|
470
|
+
*
|
|
471
|
+
* @returns Screenshot with decoded image data and save() method
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* ```typescript
|
|
475
|
+
* const screenshot = await session.screenshot();
|
|
476
|
+
* await screenshot.save('page.png');
|
|
477
|
+
* console.log(`Size: ${screenshot.width}x${screenshot.height}`);
|
|
478
|
+
* ```
|
|
479
|
+
*/
|
|
480
|
+
screenshot(): Promise<Screenshot>;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* HTTP client for AGI API with fetch and SSE support
|
|
485
|
+
*/
|
|
486
|
+
|
|
487
|
+
interface RequestOptions {
|
|
488
|
+
json?: unknown;
|
|
489
|
+
query?: Record<string, string>;
|
|
490
|
+
headers?: Record<string, string>;
|
|
491
|
+
}
|
|
492
|
+
declare class HTTPClient {
|
|
493
|
+
private readonly apiKey;
|
|
494
|
+
private readonly baseUrl;
|
|
495
|
+
private readonly timeout;
|
|
496
|
+
private readonly maxRetries;
|
|
497
|
+
constructor(options: AGIClientOptions);
|
|
498
|
+
/**
|
|
499
|
+
* Make an HTTP request with retries and error handling
|
|
500
|
+
*/
|
|
501
|
+
request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
|
|
502
|
+
/**
|
|
503
|
+
* Make an HTTP request to an absolute URL with retries and error handling
|
|
504
|
+
*/
|
|
505
|
+
requestUrl<T>(method: string, url: string, options?: RequestOptions): Promise<T>;
|
|
506
|
+
/**
|
|
507
|
+
* Stream Server-Sent Events from an endpoint
|
|
508
|
+
*/
|
|
509
|
+
streamEvents(path: string, query?: Record<string, string>): AsyncGenerator<SSEEvent>;
|
|
510
|
+
private pendingEvents;
|
|
511
|
+
private buildUrl;
|
|
512
|
+
private buildHeaders;
|
|
513
|
+
private handleErrorResponse;
|
|
514
|
+
private sleep;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Sessions API resource
|
|
519
|
+
*/
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Sessions API resource providing all session-related operations
|
|
523
|
+
*/
|
|
524
|
+
declare class SessionsResource {
|
|
525
|
+
private readonly http;
|
|
526
|
+
constructor(http: HTTPClient);
|
|
527
|
+
/**
|
|
528
|
+
* Create a new agent session
|
|
529
|
+
*
|
|
530
|
+
* @param agentName - Agent model to use (e.g., "agi-0", "agi-2-claude")
|
|
531
|
+
* @param options - Session creation options
|
|
532
|
+
* @returns SessionResponse with sessionId, vncUrl, agentUrl, status, etc.
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* ```typescript
|
|
536
|
+
* // Standard browser session
|
|
537
|
+
* const session = await client.sessions.create('agi-0', {
|
|
538
|
+
* webhookUrl: 'https://yourapp.com/webhook',
|
|
539
|
+
* maxSteps: 200
|
|
540
|
+
* });
|
|
541
|
+
*
|
|
542
|
+
* // Desktop session (client-managed)
|
|
543
|
+
* const session = await client.sessions.create('agi-2-claude', {
|
|
544
|
+
* agentSessionType: 'desktop',
|
|
545
|
+
* goal: 'Open calculator and compute 2+2'
|
|
546
|
+
* });
|
|
547
|
+
* console.log(session.agentUrl); // Use with client.desktop.step()
|
|
548
|
+
* ```
|
|
549
|
+
*/
|
|
550
|
+
create(agentName?: string, options?: {
|
|
551
|
+
webhookUrl?: string;
|
|
552
|
+
goal?: string;
|
|
553
|
+
maxSteps?: number;
|
|
554
|
+
restoreFromEnvironmentId?: string;
|
|
555
|
+
agentSessionType?: string;
|
|
556
|
+
cdpUrl?: string;
|
|
557
|
+
}): Promise<SessionResponse>;
|
|
558
|
+
/**
|
|
559
|
+
* List all sessions for the authenticated user
|
|
560
|
+
*
|
|
561
|
+
* @returns Array of SessionResponse objects
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* ```typescript
|
|
565
|
+
* const sessions = await client.sessions.list();
|
|
566
|
+
* for (const session of sessions) {
|
|
567
|
+
* console.log(`${session.sessionId}: ${session.status}`);
|
|
568
|
+
* }
|
|
569
|
+
* ```
|
|
570
|
+
*/
|
|
571
|
+
list(): Promise<SessionResponse[]>;
|
|
572
|
+
/**
|
|
573
|
+
* Get details for a specific session
|
|
574
|
+
*
|
|
575
|
+
* @param sessionId - Session UUID
|
|
576
|
+
* @returns SessionResponse with session details
|
|
577
|
+
*
|
|
578
|
+
* @example
|
|
579
|
+
* ```typescript
|
|
580
|
+
* const session = await client.sessions.get('session-uuid');
|
|
581
|
+
* console.log(session.status);
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
get(sessionId: string): Promise<SessionResponse>;
|
|
585
|
+
/**
|
|
586
|
+
* Delete a session and cleanup its resources
|
|
587
|
+
*
|
|
588
|
+
* @param sessionId - Session UUID
|
|
589
|
+
* @param saveSnapshotMode - Snapshot mode: "none", "memory", or "filesystem"
|
|
590
|
+
* @returns DeleteResponse confirming deletion
|
|
591
|
+
*
|
|
592
|
+
* @example
|
|
593
|
+
* ```typescript
|
|
594
|
+
* await client.sessions.delete('session-uuid', 'filesystem');
|
|
595
|
+
* ```
|
|
596
|
+
*/
|
|
597
|
+
delete(sessionId: string, saveSnapshotMode?: SnapshotMode): Promise<DeleteResponse>;
|
|
598
|
+
/**
|
|
599
|
+
* Delete all sessions for the authenticated user
|
|
600
|
+
*
|
|
601
|
+
* @returns DeleteResponse with count of deleted sessions
|
|
602
|
+
*
|
|
603
|
+
* @example
|
|
604
|
+
* ```typescript
|
|
605
|
+
* const result = await client.sessions.deleteAll();
|
|
606
|
+
* console.log(result.message);
|
|
607
|
+
* ```
|
|
608
|
+
*/
|
|
609
|
+
deleteAll(): Promise<DeleteResponse>;
|
|
610
|
+
/**
|
|
611
|
+
* Send a message to the agent to start a task or respond to questions
|
|
612
|
+
*
|
|
613
|
+
* @param sessionId - Session UUID
|
|
614
|
+
* @param message - Message content (task instruction or response)
|
|
615
|
+
* @param options - Message options
|
|
616
|
+
* @returns SuccessResponse confirming message was sent
|
|
617
|
+
*
|
|
618
|
+
* @example
|
|
619
|
+
* ```typescript
|
|
620
|
+
* await client.sessions.sendMessage(
|
|
621
|
+
* 'session-uuid',
|
|
622
|
+
* 'Find flights from SFO to JFK under $450'
|
|
623
|
+
* );
|
|
624
|
+
* ```
|
|
625
|
+
*/
|
|
626
|
+
sendMessage(sessionId: string, message: string, options?: {
|
|
627
|
+
startUrl?: string;
|
|
628
|
+
configUpdates?: Record<string, unknown>;
|
|
629
|
+
}): Promise<SuccessResponse>;
|
|
630
|
+
/**
|
|
631
|
+
* Get the current execution status of a session
|
|
632
|
+
*
|
|
633
|
+
* @param sessionId - Session UUID
|
|
634
|
+
* @returns ExecuteStatusResponse with status
|
|
635
|
+
*
|
|
636
|
+
* @example
|
|
637
|
+
* ```typescript
|
|
638
|
+
* const status = await client.sessions.getStatus('session-uuid');
|
|
639
|
+
* if (status.status === 'finished') {
|
|
640
|
+
* console.log('Task completed!');
|
|
641
|
+
* }
|
|
642
|
+
* ```
|
|
643
|
+
*/
|
|
644
|
+
getStatus(sessionId: string): Promise<ExecuteStatusResponse>;
|
|
645
|
+
/**
|
|
646
|
+
* Poll for messages and updates from the agent
|
|
647
|
+
*
|
|
648
|
+
* @param sessionId - Session UUID
|
|
649
|
+
* @param afterId - Return messages with ID > afterId (for polling)
|
|
650
|
+
* @param sanitize - Filter out system messages, prompts, and images
|
|
651
|
+
* @returns MessagesResponse with messages list and status
|
|
652
|
+
*
|
|
653
|
+
* @example
|
|
654
|
+
* ```typescript
|
|
655
|
+
* const messages = await client.sessions.getMessages('session-uuid', 0);
|
|
656
|
+
* for (const msg of messages.messages) {
|
|
657
|
+
* console.log(`[${msg.type}] ${msg.content}`);
|
|
658
|
+
* }
|
|
659
|
+
* ```
|
|
660
|
+
*/
|
|
661
|
+
getMessages(sessionId: string, afterId?: number, sanitize?: boolean): Promise<MessagesResponse>;
|
|
662
|
+
/**
|
|
663
|
+
* Stream real-time events from the session via Server-Sent Events
|
|
664
|
+
*
|
|
665
|
+
* @param sessionId - Session UUID
|
|
666
|
+
* @param options - Stream options
|
|
667
|
+
* @yields SSEEvent objects
|
|
668
|
+
*
|
|
669
|
+
* @example
|
|
670
|
+
* ```typescript
|
|
671
|
+
* for await (const event of client.sessions.streamEvents('session-uuid')) {
|
|
672
|
+
* if (event.event === 'thought') {
|
|
673
|
+
* console.log('Agent:', event.data);
|
|
674
|
+
* }
|
|
675
|
+
* if (event.event === 'done') {
|
|
676
|
+
* console.log('Result:', event.data);
|
|
677
|
+
* break;
|
|
678
|
+
* }
|
|
679
|
+
* }
|
|
680
|
+
* ```
|
|
681
|
+
*/
|
|
682
|
+
streamEvents(sessionId: string, options?: {
|
|
683
|
+
eventTypes?: string[];
|
|
684
|
+
sanitize?: boolean;
|
|
685
|
+
includeHistory?: boolean;
|
|
686
|
+
}): AsyncGenerator<SSEEvent>;
|
|
687
|
+
/**
|
|
688
|
+
* Temporarily pause task execution
|
|
689
|
+
*
|
|
690
|
+
* @param sessionId - Session UUID
|
|
691
|
+
* @returns SuccessResponse confirming pause
|
|
692
|
+
*
|
|
693
|
+
* @example
|
|
694
|
+
* ```typescript
|
|
695
|
+
* await client.sessions.pause('session-uuid');
|
|
696
|
+
* ```
|
|
697
|
+
*/
|
|
698
|
+
pause(sessionId: string): Promise<SuccessResponse>;
|
|
699
|
+
/**
|
|
700
|
+
* Resume a paused task
|
|
701
|
+
*
|
|
702
|
+
* @param sessionId - Session UUID
|
|
703
|
+
* @returns SuccessResponse confirming resume
|
|
704
|
+
*
|
|
705
|
+
* @example
|
|
706
|
+
* ```typescript
|
|
707
|
+
* await client.sessions.resume('session-uuid');
|
|
708
|
+
* ```
|
|
709
|
+
*/
|
|
710
|
+
resume(sessionId: string): Promise<SuccessResponse>;
|
|
711
|
+
/**
|
|
712
|
+
* Cancel task execution
|
|
713
|
+
*
|
|
714
|
+
* @param sessionId - Session UUID
|
|
715
|
+
* @returns SuccessResponse confirming cancellation
|
|
716
|
+
*
|
|
717
|
+
* @example
|
|
718
|
+
* ```typescript
|
|
719
|
+
* await client.sessions.cancel('session-uuid');
|
|
720
|
+
* ```
|
|
721
|
+
*/
|
|
722
|
+
cancel(sessionId: string): Promise<SuccessResponse>;
|
|
723
|
+
/**
|
|
724
|
+
* Navigate the browser to a specific URL
|
|
725
|
+
*
|
|
726
|
+
* @param sessionId - Session UUID
|
|
727
|
+
* @param url - URL to navigate to
|
|
728
|
+
* @returns NavigateResponse with current URL
|
|
729
|
+
*
|
|
730
|
+
* @example
|
|
731
|
+
* ```typescript
|
|
732
|
+
* await client.sessions.navigate('session-uuid', 'https://amazon.com');
|
|
733
|
+
* ```
|
|
734
|
+
*/
|
|
735
|
+
navigate(sessionId: string, url: string): Promise<NavigateResponse>;
|
|
736
|
+
/**
|
|
737
|
+
* Get a screenshot of the browser
|
|
738
|
+
*
|
|
739
|
+
* @param sessionId - Session UUID
|
|
740
|
+
* @returns ScreenshotResponse with base64-encoded image, URL, and title
|
|
741
|
+
*
|
|
742
|
+
* @example
|
|
743
|
+
* ```typescript
|
|
744
|
+
* const screenshot = await client.sessions.screenshot('session-uuid');
|
|
745
|
+
* console.log(screenshot.url);
|
|
746
|
+
* ```
|
|
747
|
+
*/
|
|
748
|
+
screenshot(sessionId: string): Promise<ScreenshotResponse>;
|
|
749
|
+
/**
|
|
750
|
+
* Execute a single step for client-driven sessions (desktop mode).
|
|
751
|
+
*
|
|
752
|
+
* In desktop mode (agentSessionType="desktop"), the client manages the
|
|
753
|
+
* execution loop. This method sends a screenshot to the agent and receives
|
|
754
|
+
* actions to execute locally.
|
|
755
|
+
*
|
|
756
|
+
* @param agentUrl - Agent service URL from session.agentUrl
|
|
757
|
+
* @param sessionId - Session ID (required for routing in shared sandbox)
|
|
758
|
+
* @param screenshot - Base64-encoded screenshot (full resolution, JPEG or PNG)
|
|
759
|
+
* @param message - Optional user message (goal on first call, or follow-up instruction)
|
|
760
|
+
* @returns StepDesktopResponse with actions, thinking, finished, askUser, and step
|
|
761
|
+
*
|
|
762
|
+
* @example
|
|
763
|
+
* ```typescript
|
|
764
|
+
* // Create a desktop session
|
|
765
|
+
* const session = await client.sessions.create('agi-2-claude', {
|
|
766
|
+
* agentSessionType: 'desktop',
|
|
767
|
+
* goal: 'Open calculator and compute 2+2'
|
|
768
|
+
* });
|
|
769
|
+
*
|
|
770
|
+
* // Client-managed loop
|
|
771
|
+
* let finished = false;
|
|
772
|
+
* while (!finished) {
|
|
773
|
+
* const screenshot = captureScreenshot(); // Client captures
|
|
774
|
+
* const result = await client.sessions.step(
|
|
775
|
+
* session.agentUrl!,
|
|
776
|
+
* session.sessionId,
|
|
777
|
+
* screenshot
|
|
778
|
+
* );
|
|
779
|
+
* executeActions(result.actions); // Client executes
|
|
780
|
+
* finished = result.finished;
|
|
781
|
+
* if (result.askUser) {
|
|
782
|
+
* const answer = await promptUser(result.askUser);
|
|
783
|
+
* // Send answer in next step
|
|
784
|
+
* }
|
|
785
|
+
* }
|
|
786
|
+
* ```
|
|
787
|
+
*/
|
|
788
|
+
step(agentUrl: string, sessionId: string, screenshot: string, message?: string): Promise<StepDesktopResponse>;
|
|
789
|
+
/**
|
|
790
|
+
* List available agent models.
|
|
791
|
+
*
|
|
792
|
+
* @param filter - Optional filter: "cdp" for browser agents, "desktop" for
|
|
793
|
+
* desktop agents
|
|
794
|
+
* @returns ModelsResponse with list of available model names
|
|
795
|
+
*
|
|
796
|
+
* @example
|
|
797
|
+
* ```typescript
|
|
798
|
+
* // List all models
|
|
799
|
+
* const models = await client.sessions.listModels();
|
|
800
|
+
* console.log(models.models);
|
|
801
|
+
*
|
|
802
|
+
* // List only desktop-compatible models
|
|
803
|
+
* const desktopModels = await client.sessions.listModels('desktop');
|
|
804
|
+
* console.log(desktopModels.models);
|
|
805
|
+
* // ['agi-2-claude', 'agi-2-qwen']
|
|
806
|
+
* ```
|
|
807
|
+
*/
|
|
808
|
+
listModels(filter?: string): Promise<ModelsResponse>;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* Main AGI API client
|
|
813
|
+
*/
|
|
814
|
+
|
|
815
|
+
/**
|
|
816
|
+
* Official TypeScript/JavaScript client for the AGI.tech API
|
|
817
|
+
*
|
|
818
|
+
* The AGIClient provides access to the AGI API for creating and managing
|
|
819
|
+
* AI agent sessions that can perform complex web tasks.
|
|
820
|
+
*
|
|
821
|
+
* @example
|
|
822
|
+
* Simple usage with context manager (recommended):
|
|
823
|
+
* ```typescript
|
|
824
|
+
* import { AGIClient } from 'agi-sdk';
|
|
825
|
+
*
|
|
826
|
+
* const client = new AGIClient({ apiKey: 'your_api_key' });
|
|
827
|
+
*
|
|
828
|
+
* await using session = client.session('agi-0');
|
|
829
|
+
* const result = await session.runTask('Find cheapest iPhone 15 on Amazon');
|
|
830
|
+
* console.log(result.data);
|
|
831
|
+
* console.log(`Duration: ${result.metadata.duration}s`);
|
|
832
|
+
* ```
|
|
833
|
+
*
|
|
834
|
+
* @example
|
|
835
|
+
* Advanced usage with low-level API:
|
|
836
|
+
* ```typescript
|
|
837
|
+
* const session = await client.sessions.create('agi-0', {
|
|
838
|
+
* webhookUrl: 'https://yourapp.com/webhook'
|
|
839
|
+
* });
|
|
840
|
+
*
|
|
841
|
+
* await client.sessions.sendMessage(session.sessionId, 'Find flights...');
|
|
842
|
+
*
|
|
843
|
+
* for await (const event of client.sessions.streamEvents(session.sessionId)) {
|
|
844
|
+
* if (event.event === 'thought') {
|
|
845
|
+
* console.log('Agent:', event.data);
|
|
846
|
+
* }
|
|
847
|
+
* if (event.event === 'done') {
|
|
848
|
+
* break;
|
|
849
|
+
* }
|
|
850
|
+
* }
|
|
851
|
+
*
|
|
852
|
+
* await client.sessions.delete(session.sessionId);
|
|
853
|
+
* ```
|
|
854
|
+
*/
|
|
855
|
+
declare class AGIClient {
|
|
856
|
+
private readonly http;
|
|
857
|
+
/** Sessions resource for low-level API access */
|
|
858
|
+
readonly sessions: SessionsResource;
|
|
859
|
+
/**
|
|
860
|
+
* Create a new AGI client
|
|
861
|
+
*
|
|
862
|
+
* @param options - Client configuration options
|
|
863
|
+
*
|
|
864
|
+
* @example
|
|
865
|
+
* ```typescript
|
|
866
|
+
* // With explicit API key
|
|
867
|
+
* const client = new AGIClient({ apiKey: 'your_api_key' });
|
|
868
|
+
*
|
|
869
|
+
* // With custom base URL
|
|
870
|
+
* const client = new AGIClient({
|
|
871
|
+
* apiKey: 'your_api_key',
|
|
872
|
+
* baseUrl: 'https://custom-api.example.com',
|
|
873
|
+
* timeout: 120000,
|
|
874
|
+
* });
|
|
875
|
+
* ```
|
|
876
|
+
*/
|
|
877
|
+
constructor(options: AGIClientOptions);
|
|
878
|
+
/**
|
|
879
|
+
* Create a session context manager for easy session lifecycle management (recommended)
|
|
880
|
+
*
|
|
881
|
+
* This is the recommended way to use the SDK. The context manager automatically
|
|
882
|
+
* creates and deletes the session using `await using` syntax.
|
|
883
|
+
*
|
|
884
|
+
* @param agentName - Agent model to use (e.g., "agi-0", "agi-0-fast", "agi-1")
|
|
885
|
+
* @param options - Session creation options
|
|
886
|
+
* @returns SessionContext manager
|
|
887
|
+
*
|
|
888
|
+
* @example
|
|
889
|
+
* ```typescript
|
|
890
|
+
* // Simple usage
|
|
891
|
+
* await using session = client.session('agi-0');
|
|
892
|
+
* const result = await session.runTask('Find flights SFO→JFK under $450');
|
|
893
|
+
* console.log(result.data);
|
|
894
|
+
* // Session automatically deleted
|
|
895
|
+
*
|
|
896
|
+
* // With options
|
|
897
|
+
* await using session = client.session('agi-0', {
|
|
898
|
+
* webhookUrl: 'https://yourapp.com/webhook',
|
|
899
|
+
* maxSteps: 200
|
|
900
|
+
* });
|
|
901
|
+
* const result = await session.runTask('Research company XYZ');
|
|
902
|
+
* ```
|
|
903
|
+
*/
|
|
904
|
+
session(agentName?: string, options?: {
|
|
905
|
+
webhookUrl?: string;
|
|
906
|
+
goal?: string;
|
|
907
|
+
maxSteps?: number;
|
|
908
|
+
restoreFromEnvironmentId?: string;
|
|
909
|
+
}): SessionContext;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* Async event loop manager for client-driven sessions.
|
|
914
|
+
*/
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
* State of the agent execution loop.
|
|
918
|
+
*/
|
|
919
|
+
type LoopState = 'idle' | 'running' | 'paused' | 'stopped' | 'finished';
|
|
920
|
+
/**
|
|
921
|
+
* Callback type for capturing screenshots.
|
|
922
|
+
* Should return a base64-encoded screenshot.
|
|
923
|
+
*/
|
|
924
|
+
type CaptureScreenshotFn = () => Promise<string>;
|
|
925
|
+
/**
|
|
926
|
+
* Callback type for executing actions.
|
|
927
|
+
*/
|
|
928
|
+
type ExecuteActionsFn = (actions: DesktopAction[]) => Promise<void>;
|
|
929
|
+
/**
|
|
930
|
+
* Callback type for handling thinking output.
|
|
931
|
+
*/
|
|
932
|
+
type OnThinkingFn = (thinking: string) => void;
|
|
933
|
+
/**
|
|
934
|
+
* Callback type for handling user questions.
|
|
935
|
+
* Should return the user's answer.
|
|
936
|
+
*/
|
|
937
|
+
type OnAskUserFn = (question: string) => Promise<string>;
|
|
938
|
+
/**
|
|
939
|
+
* Callback type for step completion.
|
|
940
|
+
*/
|
|
941
|
+
type OnStepFn = (step: number, response: StepDesktopResponse) => void;
|
|
942
|
+
/**
|
|
943
|
+
* Options for creating an AgentLoop.
|
|
944
|
+
*/
|
|
945
|
+
interface AgentLoopOptions {
|
|
946
|
+
/** AGIClient instance */
|
|
947
|
+
client: AGIClient;
|
|
948
|
+
/** Agent service URL from session.agentUrl */
|
|
949
|
+
agentUrl: string;
|
|
950
|
+
/** Session ID (required for routing in shared sandbox) */
|
|
951
|
+
sessionId: string;
|
|
952
|
+
/** Async callback that returns base64-encoded screenshot */
|
|
953
|
+
captureScreenshot: CaptureScreenshotFn;
|
|
954
|
+
/** Async callback that executes a list of actions */
|
|
955
|
+
executeActions: ExecuteActionsFn;
|
|
956
|
+
/** Optional callback for agent thinking/reasoning output */
|
|
957
|
+
onThinking?: OnThinkingFn;
|
|
958
|
+
/** Optional async callback to handle user questions. If not provided and agent asks a question, the loop will stop. */
|
|
959
|
+
onAskUser?: OnAskUserFn;
|
|
960
|
+
/** Optional callback called after each step with step number and full response */
|
|
961
|
+
onStep?: OnStepFn;
|
|
962
|
+
/** Optional delay in milliseconds between steps (default: 0) */
|
|
963
|
+
stepDelay?: number;
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* Async event loop manager for client-driven sessions.
|
|
967
|
+
*
|
|
968
|
+
* This class manages the execution loop for agent sessions where the client
|
|
969
|
+
* is responsible for capturing screenshots and executing actions (e.g., desktop,
|
|
970
|
+
* mobile, or any client-driven session type). It provides start/pause/resume/stop
|
|
971
|
+
* control over the loop.
|
|
972
|
+
*
|
|
973
|
+
* The loop:
|
|
974
|
+
* 1. Captures a screenshot using the provided callback
|
|
975
|
+
* 2. Sends it to the agent via step()
|
|
976
|
+
* 3. Executes returned actions using the provided callback
|
|
977
|
+
* 4. Repeats until finished or stopped
|
|
978
|
+
*
|
|
979
|
+
* @example
|
|
980
|
+
* ```typescript
|
|
981
|
+
* import { AGIClient, AgentLoop } from 'agi-sdk';
|
|
982
|
+
*
|
|
983
|
+
* const client = new AGIClient({ apiKey: '...' });
|
|
984
|
+
*
|
|
985
|
+
* // Create client-driven session
|
|
986
|
+
* const session = await client.sessions.create('agi-2-claude', {
|
|
987
|
+
* agentSessionType: 'desktop',
|
|
988
|
+
* goal: 'Open calculator and compute 2+2'
|
|
989
|
+
* });
|
|
990
|
+
*
|
|
991
|
+
* // Create and run loop
|
|
992
|
+
* const loop = new AgentLoop({
|
|
993
|
+
* client,
|
|
994
|
+
* agentUrl: session.agentUrl!,
|
|
995
|
+
* captureScreenshot: async () => {
|
|
996
|
+
* // Return base64-encoded screenshot
|
|
997
|
+
* return '...';
|
|
998
|
+
* },
|
|
999
|
+
* executeActions: async (actions) => {
|
|
1000
|
+
* for (const action of actions) {
|
|
1001
|
+
* console.log('Executing:', action);
|
|
1002
|
+
* }
|
|
1003
|
+
* },
|
|
1004
|
+
* onThinking: (t) => console.log('Thinking:', t),
|
|
1005
|
+
* });
|
|
1006
|
+
*
|
|
1007
|
+
* const result = await loop.start();
|
|
1008
|
+
* console.log('Finished:', result.finished);
|
|
1009
|
+
* ```
|
|
1010
|
+
*
|
|
1011
|
+
* @example Pause/resume control
|
|
1012
|
+
* ```typescript
|
|
1013
|
+
* // Start loop without awaiting
|
|
1014
|
+
* const promise = loop.start();
|
|
1015
|
+
*
|
|
1016
|
+
* // Pause after 5 seconds
|
|
1017
|
+
* setTimeout(() => loop.pause(), 5000);
|
|
1018
|
+
*
|
|
1019
|
+
* // Resume after user input
|
|
1020
|
+
* process.stdin.once('data', () => loop.resume());
|
|
1021
|
+
*
|
|
1022
|
+
* // Wait for completion
|
|
1023
|
+
* const result = await promise;
|
|
1024
|
+
* ```
|
|
1025
|
+
*/
|
|
1026
|
+
declare class AgentLoop {
|
|
1027
|
+
private readonly client;
|
|
1028
|
+
private readonly agentUrl;
|
|
1029
|
+
private readonly sessionId;
|
|
1030
|
+
private readonly captureScreenshot;
|
|
1031
|
+
private readonly executeActions;
|
|
1032
|
+
private readonly onThinking?;
|
|
1033
|
+
private readonly onAskUser?;
|
|
1034
|
+
private readonly onStep?;
|
|
1035
|
+
private readonly stepDelay;
|
|
1036
|
+
private _state;
|
|
1037
|
+
private _lastResult;
|
|
1038
|
+
private _currentStep;
|
|
1039
|
+
private pauseResolve;
|
|
1040
|
+
private pausePromise;
|
|
1041
|
+
constructor(options: AgentLoopOptions);
|
|
1042
|
+
/** Current state of the loop. */
|
|
1043
|
+
get state(): LoopState;
|
|
1044
|
+
/** Current step number. */
|
|
1045
|
+
get currentStep(): number;
|
|
1046
|
+
/** Last step result, if any. */
|
|
1047
|
+
get lastResult(): StepDesktopResponse | null;
|
|
1048
|
+
/**
|
|
1049
|
+
* Start the execution loop.
|
|
1050
|
+
*
|
|
1051
|
+
* Runs the loop until the task is finished, stopped, or an unhandled
|
|
1052
|
+
* ask_user question is encountered.
|
|
1053
|
+
*
|
|
1054
|
+
* @param message - Optional initial message (goal or instruction).
|
|
1055
|
+
* Usually not needed if goal was set during session creation.
|
|
1056
|
+
* @returns The final StepDesktopResponse
|
|
1057
|
+
* @throws Error if loop is already running
|
|
1058
|
+
*/
|
|
1059
|
+
start(message?: string): Promise<StepDesktopResponse>;
|
|
1060
|
+
/**
|
|
1061
|
+
* Pause the execution loop.
|
|
1062
|
+
*
|
|
1063
|
+
* The loop will complete the current step before pausing.
|
|
1064
|
+
* Call resume() to continue.
|
|
1065
|
+
*/
|
|
1066
|
+
pause(): void;
|
|
1067
|
+
/**
|
|
1068
|
+
* Resume a paused loop.
|
|
1069
|
+
*/
|
|
1070
|
+
resume(): void;
|
|
1071
|
+
/**
|
|
1072
|
+
* Stop the execution loop.
|
|
1073
|
+
*
|
|
1074
|
+
* The loop will complete the current step before stopping.
|
|
1075
|
+
*/
|
|
1076
|
+
stop(): void;
|
|
1077
|
+
/** Check if the loop is currently running. */
|
|
1078
|
+
isRunning(): boolean;
|
|
1079
|
+
/** Check if the loop is currently paused. */
|
|
1080
|
+
isPaused(): boolean;
|
|
1081
|
+
/** Check if the loop has finished successfully. */
|
|
1082
|
+
isFinished(): boolean;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
/**
|
|
1086
|
+
* Error classes for AGI SDK
|
|
1087
|
+
*/
|
|
1088
|
+
/**
|
|
1089
|
+
* Base error class for all AGI API errors
|
|
1090
|
+
*/
|
|
1091
|
+
declare class AGIError extends Error {
|
|
1092
|
+
readonly statusCode?: number | undefined;
|
|
1093
|
+
readonly response?: unknown | undefined;
|
|
1094
|
+
constructor(message: string, statusCode?: number | undefined, response?: unknown | undefined);
|
|
1095
|
+
}
|
|
1096
|
+
/**
|
|
1097
|
+
* Authentication error (401)
|
|
1098
|
+
*/
|
|
1099
|
+
declare class AuthenticationError extends AGIError {
|
|
1100
|
+
constructor(message: string, response?: unknown);
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* Not found error (404)
|
|
1104
|
+
*/
|
|
1105
|
+
declare class NotFoundError extends AGIError {
|
|
1106
|
+
constructor(message: string, response?: unknown);
|
|
1107
|
+
}
|
|
1108
|
+
/**
|
|
1109
|
+
* Rate limit error (429)
|
|
1110
|
+
*/
|
|
1111
|
+
declare class RateLimitError extends AGIError {
|
|
1112
|
+
constructor(message: string, response?: unknown);
|
|
1113
|
+
}
|
|
1114
|
+
/**
|
|
1115
|
+
* Agent execution error (task failed)
|
|
1116
|
+
*/
|
|
1117
|
+
declare class AgentExecutionError extends AGIError {
|
|
1118
|
+
constructor(message: string, response?: unknown);
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Validation error (422)
|
|
1122
|
+
*/
|
|
1123
|
+
declare class ValidationError extends AGIError {
|
|
1124
|
+
constructor(message: string, response?: unknown);
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* Permission error (403)
|
|
1128
|
+
*/
|
|
1129
|
+
declare class PermissionError extends AGIError {
|
|
1130
|
+
constructor(message: string, response?: unknown);
|
|
1131
|
+
}
|
|
1132
|
+
/**
|
|
1133
|
+
* API error (5xx server errors)
|
|
1134
|
+
*/
|
|
1135
|
+
declare class APIError extends AGIError {
|
|
1136
|
+
constructor(message: string, statusCode?: number, response?: unknown);
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/**
|
|
1140
|
+
* Protocol types for driver communication.
|
|
1141
|
+
*
|
|
1142
|
+
* The driver communicates via JSON lines:
|
|
1143
|
+
* - Events are emitted on stdout (driver -> SDK)
|
|
1144
|
+
* - Commands are sent on stdin (SDK -> driver)
|
|
1145
|
+
*/
|
|
1146
|
+
type EventType = 'ready' | 'state_change' | 'thinking' | 'action' | 'confirm' | 'ask_question' | 'finished' | 'error' | 'screenshot_captured';
|
|
1147
|
+
type CommandType = 'start' | 'screenshot' | 'pause' | 'resume' | 'stop' | 'confirm' | 'answer';
|
|
1148
|
+
type DriverState = 'idle' | 'running' | 'paused' | 'waiting_confirmation' | 'waiting_answer' | 'finished' | 'stopped' | 'error';
|
|
1149
|
+
interface BaseEvent {
|
|
1150
|
+
event: EventType;
|
|
1151
|
+
step: number;
|
|
1152
|
+
}
|
|
1153
|
+
interface ReadyEvent extends BaseEvent {
|
|
1154
|
+
event: 'ready';
|
|
1155
|
+
version: string;
|
|
1156
|
+
protocol: string;
|
|
1157
|
+
}
|
|
1158
|
+
interface StateChangeEvent extends BaseEvent {
|
|
1159
|
+
event: 'state_change';
|
|
1160
|
+
state: DriverState;
|
|
1161
|
+
}
|
|
1162
|
+
interface ThinkingEvent extends BaseEvent {
|
|
1163
|
+
event: 'thinking';
|
|
1164
|
+
text: string;
|
|
1165
|
+
}
|
|
1166
|
+
interface ActionEvent extends BaseEvent {
|
|
1167
|
+
event: 'action';
|
|
1168
|
+
action: DriverAction;
|
|
1169
|
+
}
|
|
1170
|
+
interface ConfirmEvent extends BaseEvent {
|
|
1171
|
+
event: 'confirm';
|
|
1172
|
+
action: DriverAction;
|
|
1173
|
+
reason: string;
|
|
1174
|
+
}
|
|
1175
|
+
interface AskQuestionEvent extends BaseEvent {
|
|
1176
|
+
event: 'ask_question';
|
|
1177
|
+
question: string;
|
|
1178
|
+
question_id: string;
|
|
1179
|
+
}
|
|
1180
|
+
interface FinishedEvent extends BaseEvent {
|
|
1181
|
+
event: 'finished';
|
|
1182
|
+
reason: string;
|
|
1183
|
+
summary: string;
|
|
1184
|
+
success: boolean;
|
|
1185
|
+
}
|
|
1186
|
+
interface ErrorEvent extends BaseEvent {
|
|
1187
|
+
event: 'error';
|
|
1188
|
+
message: string;
|
|
1189
|
+
code: string;
|
|
1190
|
+
recoverable: boolean;
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* Emitted in local mode when the driver captures a screenshot.
|
|
1194
|
+
* Lightweight notification (no image data).
|
|
1195
|
+
*/
|
|
1196
|
+
interface ScreenshotCapturedEvent extends BaseEvent {
|
|
1197
|
+
event: 'screenshot_captured';
|
|
1198
|
+
width: number;
|
|
1199
|
+
height: number;
|
|
1200
|
+
}
|
|
1201
|
+
type DriverEvent = ReadyEvent | StateChangeEvent | ThinkingEvent | ActionEvent | ConfirmEvent | AskQuestionEvent | FinishedEvent | ErrorEvent | ScreenshotCapturedEvent;
|
|
1202
|
+
interface DriverAction {
|
|
1203
|
+
type: string;
|
|
1204
|
+
x?: number;
|
|
1205
|
+
y?: number;
|
|
1206
|
+
[key: string]: unknown;
|
|
1207
|
+
}
|
|
1208
|
+
interface BaseCommand {
|
|
1209
|
+
command: CommandType;
|
|
1210
|
+
}
|
|
1211
|
+
interface StartCommand extends BaseCommand {
|
|
1212
|
+
command: 'start';
|
|
1213
|
+
session_id: string;
|
|
1214
|
+
goal: string;
|
|
1215
|
+
screenshot: string;
|
|
1216
|
+
screen_width: number;
|
|
1217
|
+
screen_height: number;
|
|
1218
|
+
platform: 'desktop' | 'android';
|
|
1219
|
+
model: string;
|
|
1220
|
+
/** "local" for autonomous mode, "" for legacy SDK-driven mode */
|
|
1221
|
+
mode?: string;
|
|
1222
|
+
}
|
|
1223
|
+
interface ScreenshotCommand extends BaseCommand {
|
|
1224
|
+
command: 'screenshot';
|
|
1225
|
+
data: string;
|
|
1226
|
+
screen_width: number;
|
|
1227
|
+
screen_height: number;
|
|
1228
|
+
}
|
|
1229
|
+
interface PauseCommand extends BaseCommand {
|
|
1230
|
+
command: 'pause';
|
|
1231
|
+
}
|
|
1232
|
+
interface ResumeCommand extends BaseCommand {
|
|
1233
|
+
command: 'resume';
|
|
1234
|
+
}
|
|
1235
|
+
interface StopCommand extends BaseCommand {
|
|
1236
|
+
command: 'stop';
|
|
1237
|
+
reason?: string;
|
|
1238
|
+
}
|
|
1239
|
+
interface ConfirmResponseCommand extends BaseCommand {
|
|
1240
|
+
command: 'confirm';
|
|
1241
|
+
approved: boolean;
|
|
1242
|
+
message?: string;
|
|
1243
|
+
}
|
|
1244
|
+
interface AnswerCommand extends BaseCommand {
|
|
1245
|
+
command: 'answer';
|
|
1246
|
+
text: string;
|
|
1247
|
+
question_id?: string;
|
|
1248
|
+
}
|
|
1249
|
+
type DriverCommand = StartCommand | ScreenshotCommand | PauseCommand | ResumeCommand | StopCommand | ConfirmResponseCommand | AnswerCommand;
|
|
1250
|
+
|
|
1251
|
+
/**
|
|
1252
|
+
* AgentDriver - Spawns and manages the agi-driver binary.
|
|
1253
|
+
*
|
|
1254
|
+
* The driver communicates via JSON lines over stdin/stdout and provides
|
|
1255
|
+
* an event-based interface for agent control.
|
|
1256
|
+
*/
|
|
1257
|
+
|
|
1258
|
+
/**
|
|
1259
|
+
* Options for creating an AgentDriver.
|
|
1260
|
+
*/
|
|
1261
|
+
interface DriverOptions {
|
|
1262
|
+
/** Path to the agi-driver binary. If not provided, will be auto-detected. */
|
|
1263
|
+
binaryPath?: string;
|
|
1264
|
+
/** Model to use (default: 'claude-sonnet') */
|
|
1265
|
+
model?: string;
|
|
1266
|
+
/** Platform type (default: 'desktop') */
|
|
1267
|
+
platform?: 'desktop' | 'android';
|
|
1268
|
+
/** "local" for autonomous mode, "" for legacy SDK-driven mode */
|
|
1269
|
+
mode?: string;
|
|
1270
|
+
/** Environment variables to pass to the driver process */
|
|
1271
|
+
env?: Record<string, string>;
|
|
1272
|
+
}
|
|
1273
|
+
/**
|
|
1274
|
+
* Result from running the driver.
|
|
1275
|
+
*/
|
|
1276
|
+
interface DriverResult {
|
|
1277
|
+
/** Whether the task completed successfully */
|
|
1278
|
+
success: boolean;
|
|
1279
|
+
/** Reason for completion */
|
|
1280
|
+
reason: string;
|
|
1281
|
+
/** Summary of what was accomplished */
|
|
1282
|
+
summary: string;
|
|
1283
|
+
/** Final step number */
|
|
1284
|
+
step: number;
|
|
1285
|
+
}
|
|
1286
|
+
/**
|
|
1287
|
+
* AgentDriver manages the lifecycle of the agi-driver binary.
|
|
1288
|
+
*
|
|
1289
|
+
* @example
|
|
1290
|
+
* ```typescript
|
|
1291
|
+
* const driver = new AgentDriver();
|
|
1292
|
+
*
|
|
1293
|
+
* driver.on('action', async (action) => {
|
|
1294
|
+
* // Execute the action on the local machine
|
|
1295
|
+
* await executeAction(action);
|
|
1296
|
+
* // Send next screenshot
|
|
1297
|
+
* driver.sendScreenshot(captureScreenshot());
|
|
1298
|
+
* });
|
|
1299
|
+
*
|
|
1300
|
+
* driver.on('thinking', (text) => {
|
|
1301
|
+
* console.log('Agent thinking:', text);
|
|
1302
|
+
* });
|
|
1303
|
+
*
|
|
1304
|
+
* driver.on('confirm', async (reason) => {
|
|
1305
|
+
* // Ask user for confirmation
|
|
1306
|
+
* return await askUser(reason);
|
|
1307
|
+
* });
|
|
1308
|
+
*
|
|
1309
|
+
* const result = await driver.start('Open calculator and compute 2+2');
|
|
1310
|
+
* console.log('Done:', result.summary);
|
|
1311
|
+
* ```
|
|
1312
|
+
*/
|
|
1313
|
+
declare class AgentDriver extends EventEmitter {
|
|
1314
|
+
private readonly binaryPath;
|
|
1315
|
+
private readonly pythonFallback;
|
|
1316
|
+
private readonly model;
|
|
1317
|
+
private readonly platform;
|
|
1318
|
+
private readonly mode;
|
|
1319
|
+
private readonly env;
|
|
1320
|
+
private process;
|
|
1321
|
+
private readline;
|
|
1322
|
+
private state;
|
|
1323
|
+
private step;
|
|
1324
|
+
private sessionId;
|
|
1325
|
+
private screenWidth;
|
|
1326
|
+
private screenHeight;
|
|
1327
|
+
private resolveStart;
|
|
1328
|
+
private rejectStart;
|
|
1329
|
+
private pendingConfirm;
|
|
1330
|
+
private pendingAnswer;
|
|
1331
|
+
constructor(options?: DriverOptions);
|
|
1332
|
+
/**
|
|
1333
|
+
* Get the current state of the driver.
|
|
1334
|
+
*/
|
|
1335
|
+
get currentState(): DriverState;
|
|
1336
|
+
/**
|
|
1337
|
+
* Get the current step number.
|
|
1338
|
+
*/
|
|
1339
|
+
get currentStep(): number;
|
|
1340
|
+
/**
|
|
1341
|
+
* Check if the driver is running.
|
|
1342
|
+
*/
|
|
1343
|
+
get isRunning(): boolean;
|
|
1344
|
+
/**
|
|
1345
|
+
* Check if the driver is waiting for user input.
|
|
1346
|
+
*/
|
|
1347
|
+
get isWaiting(): boolean;
|
|
1348
|
+
/**
|
|
1349
|
+
* Start the agent with a goal.
|
|
1350
|
+
*
|
|
1351
|
+
* @param goal - The task for the agent to accomplish
|
|
1352
|
+
* @param screenshot - Initial screenshot (base64-encoded). Not needed in local mode.
|
|
1353
|
+
* @param screenWidth - Screen width in pixels. Not needed in local mode.
|
|
1354
|
+
* @param screenHeight - Screen height in pixels. Not needed in local mode.
|
|
1355
|
+
* @param mode - Override the mode set in DriverOptions.
|
|
1356
|
+
* @returns Promise that resolves when the agent finishes
|
|
1357
|
+
*/
|
|
1358
|
+
start(goal: string, screenshot?: string, screenWidth?: number, screenHeight?: number, mode?: string): Promise<DriverResult>;
|
|
1359
|
+
/**
|
|
1360
|
+
* Send a new screenshot to the driver.
|
|
1361
|
+
*
|
|
1362
|
+
* @param screenshot - Base64-encoded screenshot
|
|
1363
|
+
* @param screenWidth - Screen width in pixels
|
|
1364
|
+
* @param screenHeight - Screen height in pixels
|
|
1365
|
+
*/
|
|
1366
|
+
sendScreenshot(screenshot: string, screenWidth?: number, screenHeight?: number): void;
|
|
1367
|
+
/**
|
|
1368
|
+
* Pause the driver.
|
|
1369
|
+
*/
|
|
1370
|
+
pause(): void;
|
|
1371
|
+
/**
|
|
1372
|
+
* Resume the driver.
|
|
1373
|
+
*/
|
|
1374
|
+
resume(): void;
|
|
1375
|
+
/**
|
|
1376
|
+
* Stop the driver.
|
|
1377
|
+
*
|
|
1378
|
+
* @param reason - Reason for stopping
|
|
1379
|
+
*/
|
|
1380
|
+
stop(reason?: string): Promise<void>;
|
|
1381
|
+
/**
|
|
1382
|
+
* Respond to a confirmation request.
|
|
1383
|
+
*
|
|
1384
|
+
* @param approved - Whether the action is approved
|
|
1385
|
+
* @param message - Optional message to send with the response
|
|
1386
|
+
*/
|
|
1387
|
+
respondConfirm(approved: boolean, message?: string): void;
|
|
1388
|
+
/**
|
|
1389
|
+
* Respond to a question.
|
|
1390
|
+
*
|
|
1391
|
+
* @param text - The answer text
|
|
1392
|
+
* @param questionId - Optional question ID
|
|
1393
|
+
*/
|
|
1394
|
+
respondAnswer(text: string, questionId?: string): void;
|
|
1395
|
+
private sendCommand;
|
|
1396
|
+
private handleLine;
|
|
1397
|
+
private handleFinished;
|
|
1398
|
+
private handleError;
|
|
1399
|
+
private cleanup;
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1402
|
+
/**
|
|
1403
|
+
* Binary locator for the agi-driver.
|
|
1404
|
+
*
|
|
1405
|
+
* Finds the platform-specific binary bundled with the package
|
|
1406
|
+
* or falls back to a global installation.
|
|
1407
|
+
*/
|
|
1408
|
+
/**
|
|
1409
|
+
* Platform identifier for binary selection.
|
|
1410
|
+
*/
|
|
1411
|
+
type PlatformId = 'darwin-arm64' | 'darwin-x64' | 'linux-x64' | 'win32-x64';
|
|
1412
|
+
/**
|
|
1413
|
+
* Get the current platform identifier.
|
|
1414
|
+
*/
|
|
1415
|
+
declare function getPlatformId(): PlatformId;
|
|
1416
|
+
/**
|
|
1417
|
+
* Find the agi-driver binary path.
|
|
1418
|
+
*
|
|
1419
|
+
* @throws Error if the binary cannot be found
|
|
1420
|
+
*/
|
|
1421
|
+
declare function findBinaryPath(): string;
|
|
1422
|
+
/**
|
|
1423
|
+
* Check if the binary is available.
|
|
1424
|
+
*/
|
|
1425
|
+
declare function isBinaryAvailable(): boolean;
|
|
1426
|
+
|
|
1427
|
+
export { AGIClient, type AGIClientOptions, AGIError, APIError, AgentDriver, AgentExecutionError, AgentLoop, type AgentLoopOptions, type AgentSessionType, AuthenticationError, type CaptureScreenshotFn, type CreateSessionOptions, type DeleteResponse, type DesktopAction, type DesktopActionType, type DriverAction, type DriverCommand, type DriverEvent, type DriverOptions, type DriverResult, type DriverState, type EventType$1 as EventType, type ExecuteActionsFn, type ExecuteStatusResponse, type LoopState, type MessageOptions, type MessageResponse, type MessageType, type MessagesResponse, type ModelsResponse, type NavigateResponse, NotFoundError, type OnAskUserFn, type OnStepFn, type OnThinkingFn, PermissionError, type PlatformId, RateLimitError, type RunTaskOptions, type SSEEvent, Screenshot, type ScreenshotResponse, SessionContext, type SessionResponse, type SessionStatus, SessionsResource, type SnapshotMode, type StepDesktopRequest, type StepDesktopResponse, type StreamOptions, type SuccessResponse, type TaskMetadata, type TaskResult, ValidationError, findBinaryPath, getPlatformId, isBinaryAvailable };
|