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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AGI Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,55 @@
1
- # AGI Browser
1
+ <div align="center">
2
2
 
3
- AGI - Your intelligent browser worker
3
+ <img src="https://cdn.prod.website-files.com/67be56277f6d514dcad939e2/67e48dd1cdab04c17a311669_logo-agi-white.png" alt="AGI" width="120"/>
4
+
5
+ <h1>AGI Node.js SDK</h1>
6
+
7
+ <p>
8
+ <a href="https://www.npmjs.com/package/agi"><img src="https://img.shields.io/npm/v/agi?style=flat-square" alt="npm version"></a>
9
+ <a href="https://github.com/agi-inc/agi-node/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/agi-inc/agi-node/ci.yml?branch=main&style=flat-square" alt="Build Status"></a>
10
+ <a href="https://codecov.io/gh/agi-inc/agi-node"><img src="https://img.shields.io/codecov/c/github/agi-inc/agi-node?style=flat-square" alt="Coverage"></a>
11
+ <a href="https://github.com/agi-inc/agi-node/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/agi?style=flat-square" alt="License"></a>
12
+ <a href="https://www.npmjs.com/package/agi"><img src="https://img.shields.io/npm/dm/agi?style=flat-square" alt="Downloads"></a>
13
+ </p>
14
+
15
+ <p>
16
+ <a href="https://www.theagi.company/">Website</a> •
17
+ <a href="https://docs.agi.tech">Documentation</a> •
18
+ <a href="https://platform.agi.tech">Platform</a> •
19
+ <a href="https://theagi.company/blog">Blog</a>
20
+ </p>
21
+
22
+ ---
23
+
24
+ **Universal Computer-Use AI**
25
+
26
+ <br />
27
+
28
+ </div>
29
+
30
+ ```typescript
31
+ import { AGIClient } from 'agi';
32
+
33
+ const client = new AGIClient({ apiKey: process.env.AGI_API_KEY });
34
+
35
+ // Context manager with automatic cleanup
36
+ await using session = client.session('agi-0');
37
+
38
+ const result = await session.runTask(
39
+ 'Find three nonstop flights from SFO to JFK next month under $450. ' +
40
+ 'Return flight times, airlines, and booking links.'
41
+ );
42
+
43
+ console.log(result.data);
44
+ console.log(`Duration: ${result.metadata.duration}s, Steps: ${result.metadata.steps}`);
45
+ // Session automatically deleted
46
+ ```
47
+
48
+ <br />
49
+
50
+ > Powered by [AGI.tech](https://agi.tech) - the world's most capable computer agent. Trusted by [Visa](https://www.theagi.company/blog/agi-inc-visa) for agentic commerce. Evaluated with [REAL Bench](https://www.theagi.company/blog/introducing-real-bench).
51
+
52
+ <br />
4
53
 
5
54
  ## Installation
6
55
 
@@ -8,25 +57,498 @@ AGI - Your intelligent browser worker
8
57
  npm install agi
9
58
  ```
10
59
 
11
- **Note:** This package is currently only supported on macOS (darwin).
60
+ Get your API key at [platform.agi.tech](https://platform.agi.tech/api-keys)
12
61
 
13
- ## What it does
62
+ ```bash
63
+ export AGI_API_KEY="your-api-key"
64
+ ```
14
65
 
15
- The AGI app provides an intelligent browser automation assistant that can help you navigate and interact with web pages.
66
+ ## Quick Start
16
67
 
17
- ## Requirements
68
+ ### Simple Task (Recommended)
18
69
 
19
- - macOS (darwin)
20
- - Node.js >= 14.0.0
70
+ ```typescript
71
+ import { AGIClient } from 'agi';
21
72
 
22
- ## Post-install
73
+ const client = new AGIClient({ apiKey: process.env.AGI_API_KEY });
23
74
 
24
- Install the AGI app
75
+ // Context manager with automatic cleanup
76
+ await using session = client.session('agi-0');
25
77
 
26
- ## License
78
+ const result = await session.runTask('Find the cheapest iPhone 15 on Amazon');
79
+
80
+ console.log(result.data); // Task output
81
+ console.log(result.metadata.duration); // Execution time in seconds
82
+ console.log(result.metadata.steps); // Number of steps taken
83
+ // Session automatically deleted when scope exits
84
+ ```
85
+
86
+ ### Real-Time Event Streaming
87
+
88
+ ```typescript
89
+ await using session = client.session('agi-0');
90
+
91
+ await session.sendMessage('Research the top 5 AI companies in 2025');
92
+
93
+ for await (const event of session.streamEvents()) {
94
+ if (event.event === 'thought') {
95
+ console.log('💭 Agent:', event.data);
96
+ }
97
+ if (event.event === 'done') {
98
+ console.log('✅ Result:', event.data);
99
+ break;
100
+ }
101
+ }
102
+ // Session automatically deleted
103
+ ```
104
+
105
+ ### Polling Configuration
106
+
107
+ Configure timeouts and polling intervals for different task types:
108
+
109
+ ```typescript
110
+ await using session = client.session('agi-0');
111
+
112
+ // Quick task (1 min timeout, 2s polling)
113
+ const result1 = await session.runTask('What is 2+2?', {
114
+ timeout: 60000,
115
+ pollInterval: 2000,
116
+ });
117
+
118
+ // Complex task (15 min timeout, 5s polling)
119
+ const result2 = await session.runTask('Research AI companies...', {
120
+ timeout: 900000,
121
+ pollInterval: 5000,
122
+ });
123
+ ```
124
+
125
+ ### Manual Session Management
126
+
127
+ For advanced use cases requiring fine-grained control:
128
+
129
+ ```typescript
130
+ // Create session manually
131
+ const response = await client.sessions.create('agi-0', {
132
+ webhookUrl: 'https://yourapp.com/webhook',
133
+ maxSteps: 200,
134
+ });
135
+
136
+ // Send message
137
+ await client.sessions.sendMessage(response.sessionId, 'Find flights from SFO to JFK under $450');
138
+
139
+ // Check status
140
+ const status = await client.sessions.getStatus(response.sessionId);
141
+
142
+ // Delete when done
143
+ await client.sessions.delete(response.sessionId);
144
+ ```
145
+
146
+ ### Session Control
147
+
148
+ ```typescript
149
+ await using session = client.session('agi-0');
150
+
151
+ await session.sendMessage('Long research task...');
152
+
153
+ // Control execution
154
+ await session.pause(); // Pause the agent
155
+ await session.resume(); // Resume later
156
+ await session.cancel(); // Or cancel
157
+ ```
158
+
159
+ ---
160
+
161
+ ## Core Concepts
162
+
163
+ _Understanding the building blocks of agi_
164
+
165
+ ### Sessions: The Container for Tasks
166
+
167
+ Every task runs inside a **session** - an isolated browser environment:
168
+
169
+ ```typescript
170
+ // Recommended: Context manager (automatic cleanup)
171
+ await using session = client.session('agi-0');
172
+ await session.runTask('Find flights...');
173
+ // Session automatically deleted
174
+
175
+ // Alternative: Manual management
176
+ const response = await client.sessions.create('agi-0');
177
+ await client.sessions.delete(response.sessionId);
178
+ ```
179
+
180
+ ▸ Use context managers (`await using`) for automatic cleanup. Sessions consume resources and should be deleted when done.
181
+
182
+ ### Available Agents
183
+
184
+ - **agi-0** - General purpose agent (recommended)
185
+ - **agi-0-fast** - Faster agent for simple tasks
186
+ - **agi-1** - Advanced agent with enhanced capabilities
187
+
188
+ See [docs.agi.tech](https://docs.agi.tech) for the full list.
189
+
190
+ ---
191
+
192
+ ## Features
193
+
194
+ - **Natural Language** - Describe tasks in plain English, no selectors or scraping code
195
+ - **Real-Time Streaming** - Watch agent execution live with Server-Sent Events
196
+ - **Session Control** - Pause, resume, or cancel long-running tasks
197
+ - **Browser Control** - Navigate and screenshot for visual debugging
198
+ - **Type-Safe** - Full TypeScript support with comprehensive type definitions
199
+ - **Production-Ready** - Built-in retries, automatic cleanup, comprehensive error handling
200
+
201
+ ---
202
+
203
+ ## Common Use Cases
204
+
205
+ ### Price Monitoring
206
+
207
+ Monitor product prices and availability across retailers.
208
+
209
+ ```typescript
210
+ const session = await client.createSession('agi-0');
211
+
212
+ try {
213
+ const result = await session.runTask(
214
+ 'Go to amazon.com and search for "Sony WH-1000XM5". ' +
215
+ "Get the current price, check if it's in stock, and return the product rating. " +
216
+ 'Return as JSON with fields: price, in_stock, rating, url.'
217
+ );
218
+ console.log(result);
219
+ } finally {
220
+ await session.delete();
221
+ }
222
+ ```
223
+
224
+ ### Lead Generation
225
+
226
+ Extract structured data from public sources.
227
+
228
+ ```typescript
229
+ const session = await client.createSession('agi-0');
230
+
231
+ try {
232
+ const result = await session.runTask(
233
+ 'Go to ycombinator.com/companies, find companies in the "AI" category ' +
234
+ 'from the latest batch. For the first 10 companies, get their name, ' +
235
+ 'description, and website. Return as a JSON array.'
236
+ );
237
+ console.log(result);
238
+ } finally {
239
+ await session.delete();
240
+ }
241
+ ```
242
+
243
+ ### Flight Booking Research
244
+
245
+ ```typescript
246
+ const session = await client.createSession('agi-0');
247
+
248
+ try {
249
+ const result = await session.runTask(
250
+ 'Find three nonstop SFO→JFK flights next month under $450. ' +
251
+ 'Compare prices on Google Flights, Kayak, and Expedia. ' +
252
+ 'Return flight details and booking links.'
253
+ );
254
+ console.log(result);
255
+ } finally {
256
+ await session.delete();
257
+ }
258
+ ```
259
+
260
+ <details>
261
+ <summary><b>Browser Control</b> – Navigate and take screenshots for visual debugging</summary>
262
+
263
+ <br />
264
+
265
+ ```typescript
266
+ const session = await client.createSession('agi-0');
267
+
268
+ try {
269
+ // Navigate to specific URL
270
+ await session.navigate('https://amazon.com');
271
+
272
+ // Get screenshot for debugging
273
+ const screenshot = await session.screenshot();
274
+ console.log(screenshot.url); // Current page URL
275
+ console.log(screenshot.title); // Page title
276
+ // screenshot.screenshot contains base64 JPEG data
277
+ } finally {
278
+ await session.delete();
279
+ }
280
+ ```
27
281
 
28
- MIT
282
+ </details>
29
283
 
30
- ## Author
284
+ <details>
285
+ <summary><b>Session Snapshots</b> – Preserve authentication and browser state</summary>
286
+
287
+ <br />
288
+
289
+ ```typescript
290
+ // Create session and save environment
291
+ const session1 = await client.createSession('agi-0');
292
+ // ... do some authentication work ...
293
+ await session1.delete('filesystem');
294
+
295
+ // Later, restore from saved environment
296
+ const session2 = await client.createSession('agi-0', {
297
+ restoreFromEnvironmentId: session1.environmentId,
298
+ });
299
+ // Authentication state and cookies preserved!
300
+
301
+ await session2.delete();
302
+ ```
303
+
304
+ </details>
305
+
306
+ <details>
307
+ <summary><b>Advanced Session Management</b> – Full control over session lifecycle</summary>
308
+
309
+ <br />
310
+
311
+ ```typescript
312
+ // Create session with options
313
+ const session = await client.createSession('agi-0-fast', {
314
+ webhookUrl: 'https://yourapp.com/webhook',
315
+ maxSteps: 200,
316
+ });
317
+
318
+ // Send message
319
+ await session.sendMessage('Find flights from SFO to JFK under $450');
320
+
321
+ // Check status
322
+ const status = await session.getStatus();
323
+ console.log(status.status); // "running", "finished", etc.
324
+
325
+ // List all sessions
326
+ const sessions = await client.listSessions();
327
+
328
+ // Delete when done
329
+ await session.delete();
330
+ ```
331
+
332
+ </details>
333
+
334
+ <details>
335
+ <summary><b>Webhooks</b> – Get notified when tasks complete</summary>
336
+
337
+ <br />
338
+
339
+ ```typescript
340
+ const session = await client.createSession('agi-0', {
341
+ webhookUrl: 'https://yourapp.com/webhook',
342
+ });
343
+
344
+ // Your webhook will receive events:
345
+ // POST https://yourapp.com/webhook
346
+ // {
347
+ // "event": "done",
348
+ // "session_id": "sess_...",
349
+ // "data": {...}
350
+ // }
351
+ ```
352
+
353
+ </details>
354
+
355
+ <details>
356
+ <summary><b>Client-Driven Sessions</b> – Run agents on desktop, mobile, or custom environments</summary>
357
+
358
+ <br />
359
+
360
+ > **Note:** Desktop mode is currently feature-gated. For enterprise access, contact [`partner@theagi.company`](mailto:partner@theagi.company).
361
+
362
+ For scenarios where you control the execution environment (desktop automation, mobile apps, custom browsers), use client-driven sessions with `AgentLoop`:
363
+
364
+ ```typescript
365
+ import { AGIClient, AgentLoop } from 'agi';
366
+
367
+ const client = new AGIClient({ apiKey: process.env.AGI_API_KEY });
368
+
369
+ // Create a client-driven session
370
+ const session = await client.sessions.create('agi-2-claude', {
371
+ agentSessionType: 'desktop',
372
+ goal: 'Open calculator and compute 2+2',
373
+ });
374
+
375
+ // Create and run the loop
376
+ const loop = new AgentLoop({
377
+ client,
378
+ agentUrl: session.agentUrl!,
379
+ captureScreenshot: async () => {
380
+ // Return base64-encoded screenshot from your environment
381
+ return '...';
382
+ },
383
+ executeActions: async (actions) => {
384
+ for (const action of actions) {
385
+ console.log(`Executing: ${action.type}`);
386
+ // Execute action in your environment
387
+ }
388
+ },
389
+ onThinking: (t) => console.log(`💭 ${t}`),
390
+ });
391
+
392
+ const result = await loop.start();
393
+ console.log(`Finished: ${result.finished}`);
394
+ ```
395
+
396
+ **Loop Control:**
397
+
398
+ ```typescript
399
+ // Start without awaiting
400
+ const promise = loop.start();
401
+
402
+ // Pause/resume/stop
403
+ loop.pause(); // Pause after current step
404
+ loop.resume(); // Continue execution
405
+ loop.stop(); // Stop the loop
406
+
407
+ // Check state
408
+ loop.state; // 'running', 'paused', 'stopped', 'finished'
409
+ loop.currentStep; // Current step number
410
+ loop.lastResult; // Last StepDesktopResponse
411
+ ```
412
+
413
+ **Manual Step Control:**
414
+
415
+ ```typescript
416
+ // Or manage the loop yourself
417
+ let finished = false;
418
+ while (!finished) {
419
+ const screenshot = await captureScreenshot();
420
+ const result = await client.sessions.step(session.agentUrl!, screenshot);
421
+ await executeActions(result.actions);
422
+ finished = result.finished;
423
+ }
424
+ ```
425
+
426
+ </details>
427
+
428
+ ---
429
+
430
+ ## Error Handling
431
+
432
+ <details>
433
+ <summary><b>Robust error handling with detailed debugging</b></summary>
434
+
435
+ <br />
436
+
437
+ ```typescript
438
+ import {
439
+ AGIClient,
440
+ AuthenticationError,
441
+ NotFoundError,
442
+ RateLimitError,
443
+ AgentExecutionError,
444
+ AGIError,
445
+ } from 'agi';
446
+
447
+ const client = new AGIClient({ apiKey: process.env.AGI_API_KEY });
448
+
449
+ try {
450
+ const session = await client.createSession('agi-0');
451
+ try {
452
+ const result = await session.runTask('Find flights...');
453
+ console.log(result);
454
+ } finally {
455
+ await session.delete();
456
+ }
457
+ } catch (error) {
458
+ if (error instanceof AuthenticationError) {
459
+ console.error('Invalid API key');
460
+ } else if (error instanceof NotFoundError) {
461
+ console.error('Session not found');
462
+ } else if (error instanceof RateLimitError) {
463
+ console.error('Rate limit exceeded - please retry');
464
+ } else if (error instanceof AgentExecutionError) {
465
+ console.error('Task failed:', error.message);
466
+ // Debug at VNC URL if available
467
+ } else if (error instanceof AGIError) {
468
+ console.error('API error:', error.message);
469
+ }
470
+ }
471
+ ```
472
+
473
+ </details>
474
+
475
+ ---
476
+
477
+ ## Examples
478
+
479
+ **Real-world, production-ready examples** → See [examples/](./examples)
480
+
481
+ ### Quick Start
482
+
483
+ ```bash
484
+ # 5-line hello world
485
+ npx tsx examples/hello-world.ts
486
+
487
+ # Price tracking
488
+ npx tsx examples/ecommerce/price-tracker.ts \
489
+ --product "Sony WH-1000XM5" --max-price 350
490
+
491
+ # Flight search
492
+ npx tsx examples/travel/flight-finder.ts \
493
+ --from SFO --to JFK --date "2026-02-15" --max-price 450
494
+
495
+ # B2B lead generation
496
+ npx tsx examples/sales-marketing/linkedin-scraper.ts \
497
+ --industry "SaaS" --location "San Francisco" --count 10
498
+
499
+ # Competitor analysis
500
+ npx tsx examples/research/competitor-analysis.ts \
501
+ --competitors "stripe.com,square.com,paypal.com"
502
+ ```
503
+
504
+ ### Example Categories
505
+
506
+ - **[Basic](./examples#-basic-examples-basic)** (⭐ Beginner) - Quickstart, error handling, streaming
507
+ - **[E-Commerce](./examples#-e-commerce-ecommerce)** (⭐⭐ Intermediate) - Price tracking, product comparison
508
+ - **[Travel](./examples#️-travel-travel)** (⭐⭐ Intermediate) - Flight search, booking research
509
+ - **[Sales & Marketing](./examples#-sales--marketing-sales-marketing)** (⭐⭐ Intermediate) - Lead generation, LinkedIn scraping
510
+ - **[Research](./examples#-research-research)** (⭐⭐ Intermediate) - Competitive analysis, market research
511
+ - **[Production](./examples#-production-patterns-production)** (⭐⭐⭐ Advanced) - Batch processing, webhook servers
512
+
513
+ **Learning Path**: [hello-world.ts](./examples/hello-world.ts) → [basic/quickstart.ts](./examples/basic/quickstart.ts) → [Choose your domain](./examples#-examples-by-category) → [Production patterns](./examples#-production-patterns-production)
514
+
515
+ ---
516
+
517
+ ## Documentation & Resources
518
+
519
+ **Learn More**
520
+
521
+ - [API Reference](https://docs.agi.tech) – Complete API documentation
522
+ - [Code Examples](./examples) – 15+ production-ready examples
523
+ - [GitHub Issues](https://github.com/agi-inc/agi-node/issues) – Report bugs or request features
524
+
525
+ **Get Help**
526
+
527
+ - [Platform](https://platform.agi.tech) – Manage API keys and monitor usage
528
+ - [Documentation](https://docs.agi.tech) – Guides and tutorials
529
+
530
+ ---
531
+
532
+ ## TypeScript Support
533
+
534
+ This SDK is written in TypeScript and provides full type definitions:
535
+
536
+ ```typescript
537
+ import type { Session, SessionStatus, SSEEvent, NavigateResponse } from 'agi';
538
+
539
+ const session: Session = await client.createSession('agi-0');
540
+ const status: SessionStatus = (await session.getStatus()).status;
541
+ ```
542
+
543
+ ---
544
+
545
+ ## Requirements
546
+
547
+ - Node.js 20.4 or higher (required for `await using` syntax)
548
+ - TypeScript 5.0+ (for TypeScript users)
549
+
550
+ ---
551
+
552
+ ## License
31
553
 
32
- AGI, Inc.
554
+ MIT License - see [LICENSE](LICENSE) for details.