-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLLMS.txt
More file actions
638 lines (498 loc) · 18.8 KB
/
Copy pathLLMS.txt
File metadata and controls
638 lines (498 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# Harbor Web Agents API - Reference for AI Coding Assistants
> **WHAT THIS IS**: A compact, token-efficient reference for Claude, Cursor, Copilot, or any AI assistant building web apps with the Web Agents API. This doc matches `web-agents-api/src/injected.ts` exactly — if it's not here, it isn't shipped.
>
> **HUMAN DOCS**: For prose explanations, see `docs/WEB_AGENTS_API.md`, `docs/BUILDING_ON_HARBOR.md`, and `docs/AGENTIC_BROWSER_ROADMAP.md`.
---
## TL;DR - Copy This to Start
```javascript
if (!window.agent) throw new Error('Web Agents API not available');
// 1. Feature-detect (synchronous, no permission needed)
const caps = window.agent.capabilities();
if (!caps.features.textGeneration) throw new Error('Text generation disabled');
// 2. Request permissions (REQUIRED before any API use)
const { granted } = await window.agent.requestPermissions({
scopes: ['model:prompt', 'mcp:tools.list', 'mcp:tools.call'],
reason: 'Enable AI features'
});
if (!granted) throw new Error('Permissions denied');
// 3. Use text generation
const session = await window.ai.createTextSession({ systemPrompt: 'Be helpful.' });
const response = await session.prompt('Hello!');
session.destroy();
// 4. Or run an autonomous agent (requires toolCalling flag)
if (caps.features.toolCalling) {
for await (const ev of window.agent.run({ task: 'Search for AI news' })) {
if (ev.type === 'final') console.log(ev.output);
}
}
```
---
## Global Objects
| Object | Purpose |
|--------|---------|
| `window.ai` | Text generation (Chrome Prompt API compatible) |
| `window.agent` | Tools, browser control, sessions, multi-agent, MCP, chat |
| `navigator.modelContext` | WebMCP polyfill — page-defined tools |
`window.harbor.ai` and `window.harbor.agent` are unconditional aliases (used when Chrome's native AI is detected and `window.ai` is left untouched).
---
## Detection
```javascript
if (window.agent && window.ai) { /* ready */ }
// Or wait for the ready event
window.addEventListener('agent-ready', (e) => {
console.log('Version:', e.detail.version); // '1.4.0'
console.log('Features:', e.detail.features); // FeatureFlags
});
// Synchronous capability snapshot
const caps = window.agent.capabilities();
// → { apiVersion, features, surfaces, scopes }
```
---
## Permissions (MUST REQUEST FIRST)
All APIs require permission. There are two paths — pick one per session:
### Recommended — `agent.requestCapabilities()` (typed actions)
Mints a session bound to a capability token. Scoped to `plan` /
`execute` / `watch`. Plays nicely with budgets and the activity feed.
```javascript
const session = await window.agent.requestCapabilities({
name: 'Article assistant',
reason: 'Read the page and propose follow-ups.',
mode: 'plan',
require: [
{ action: 'model.prompt.local' },
{ action: 'browser.read.activeTab' },
{ action: 'tool.call', server: 'time-wasm', toolNames: ['time.now'] },
],
budget: { maxToolCalls: 30, ttlMinutes: 15 },
});
// Narrow into Watch when the user clicks "Run":
await window.agent.upgradeSession(session.id, { mode: 'watch' });
```
### Legacy — `agent.requestPermissions()` (origin-level scopes)
Still works; mapped to typed actions internally.
```javascript
const result = await window.agent.requestPermissions({
scopes: ['model:prompt', 'mcp:tools.call'],
reason: 'To help with your task',
tools: ['time-wasm/time.now'] // Optional allowlist for mcp:tools.call
});
if (result.granted) { /* all scopes granted */ }
else { /* check result.scopes['model:prompt'] === 'granted-always' */ }
```
### Permission Scopes
| Scope | Risk | Enables |
|-------|------|---------|
| `model:prompt` | Low | `ai.createTextSession`, `session.prompt` |
| `model:tools` | Medium | `agent.run` autonomous loop |
| `model:list` | Low | `ai.providers.list/getActive` |
| `mcp:tools.list` | Low | `agent.tools.list` |
| `mcp:tools.call` | Medium | `agent.tools.call` |
| `mcp:servers.register` | Medium | `agent.mcp.register` (BYOC) |
| `browser:activeTab.read` | Medium | `agent.browser.activeTab.readability/getElements` |
| `browser:activeTab.interact` | High | `click`, `fill`, `select`, `scroll` |
| `browser:activeTab.screenshot` | Medium | `agent.browser.activeTab.screenshot` |
| `browser:tabs.read` | Medium | `agent.browser.tabs.list`, `tab.*` reads |
| `browser:tabs.create` | High | `agent.browser.tabs.create/close`, drive spawned tabs |
| `browser:navigate` | High | `agent.browser.navigate` (active tab) |
| `web:fetch` | High | `agent.browser.fetch` (CORS-bypassing) |
| `chat:open` | Low | `agent.chat.open/close` |
| `agents:register` | Medium | `agent.agents.register` |
| `agents:discover` | Medium | `agent.agents.discover/list` |
| `agents:invoke` | Medium | `agent.agents.invoke` |
| `agents:message` | Medium | `agent.agents.send/broadcast` |
### Grant Types
| Grant | Meaning |
|-------|---------|
| `granted-always` | Persistent until revoked |
| `granted-once` | ~10 min or until tab closes |
| `denied` | User said no |
| `not-granted` | Never requested |
```javascript
const { scopes, allowedTools } = await window.agent.permissions.list();
```
---
## Capability Discovery
```javascript
const caps = window.agent.capabilities();
// {
// apiVersion: '1.4.0',
// features: { textGeneration, toolCalling, toolAccess,
// browserInteraction, browserControl, multiAgent },
// surfaces: {
// ai, tools, run,
// browser: { activeTab, tabs, navigate, fetch },
// mcp, chat, sessions, agents, modelContext
// },
// scopes: PermissionScope[]
// }
```
Use `caps.features.*` instead of try/catching `ERR_FEATURE_DISABLED`.
---
## window.ai - Text Generation
```javascript
const session = await window.ai.createTextSession({
systemPrompt: 'You are helpful.', // Optional
temperature: 0.7, // Optional
topK: 40, // Optional
});
// Plain prompt
const response = await session.prompt('What is 2+2?');
// Streaming — yields plain string chunks (Chrome Prompt API compatible)
for await (const chunk of session.promptStreaming('Tell me a story')) {
process.stdout.write(chunk);
}
session.destroy();
```
### Provider listing
```javascript
const providers = await window.ai.providers.list();
const { provider, model } = await window.ai.providers.getActive();
const configured = await window.ai.providers.listConfiguredModels();
```
### Capabilities probe
```javascript
const caps = await window.ai.languageModel.capabilities();
// { available: 'readily' | 'after-download' | 'no', defaultTemperature, ... }
```
> Note: `window.ai.runtime.*` (Firefox/Chrome native AI bridging) is **not** exposed by the Web Agents API extension today. Use `window.ai.providers.list()` and pick a provider explicitly.
---
## window.agent.tools - Direct Tool Access
```javascript
const tools = await window.agent.tools.list();
// [{ serverId: 'time-wasm', name: 'time-wasm/time.now', description, inputSchema }, …]
const result = await window.agent.tools.call({
tool: 'time-wasm/time.now',
args: { timezone: 'America/New_York' }
});
```
Page-defined tools (via `navigator.modelContext`) appear with `serverId: 'page'` and a `page/` prefix.
---
## window.agent.run - Autonomous Agent (toolCalling flag)
```javascript
for await (const ev of window.agent.run({
task: 'What time is it in Tokyo?',
maxToolCalls: 5, // default 5
systemPrompt: 'Be concise.',
})) {
switch (ev.type) {
case 'thinking': console.log('reasoning:', ev.content); break;
case 'tool_call': console.log('calling', ev.tool, ev.args); break;
case 'tool_result': console.log('got', ev.result); break;
case 'final': console.log('answer:', ev.output); break;
case 'error': throw new Error(ev.error);
}
}
```
Event union (exhaustive):
```ts
| { type: 'thinking'; content: string }
| { type: 'tool_call'; tool: string; args: Record<string, unknown> }
| { type: 'tool_result'; tool: string; result: unknown }
| { type: 'final'; output: string }
| { type: 'error'; error: string }
```
---
## Browser APIs
### Active tab — `browserInteraction` flag
```javascript
const { title, url, content, length } =
await window.agent.browser.activeTab.readability();
const { elements } = await window.agent.browser.activeTab.getElements({
selector: 'a, button, input',
});
await window.agent.browser.activeTab.click('ref-123');
await window.agent.browser.activeTab.fill('ref-456', 'Hello');
await window.agent.browser.activeTab.select('ref-789', 'option-value');
await window.agent.browser.activeTab.scroll('down', 500);
const { dataUrl } = await window.agent.browser.activeTab.screenshot();
```
### Active-tab navigation & fetch — `browserControl` flag
```javascript
// Navigate the page itself
await window.agent.browser.navigate('https://example.com');
// CORS-bypassing fetch (http/https only, blocks RFC1918/localhost)
const r = await window.agent.browser.fetch('https://api.example.com/data', {
method: 'GET',
headers: { 'X-Foo': 'bar' },
});
// → { ok, status, statusText, headers, body, url }
```
### Owned tabs — `browserControl` flag
```javascript
const tab = await window.agent.browser.tabs.create({
url: 'https://flights.example/search',
active: false,
});
await window.agent.browser.tab.waitForLoad(tab.id);
const { html } = await window.agent.browser.tab.getHtml(tab.id, 'main');
const { title, url, content } = await window.agent.browser.tab.readability(tab.id);
const tabs = await window.agent.browser.tabs.list();
await window.agent.browser.tabs.close(tab.id);
```
You can only drive tabs your origin spawned (same-origin or "owned" model). You cannot drive arbitrary user tabs.
---
## Sessions API (toolAccess flag)
Capability-bounded, named sessions with budgets.
```javascript
const session = await window.agent.sessions.create({
name: 'Research Assistant',
reason: 'multi-step research',
capabilities: {
llm: { provider: 'ollama', model: 'llama3.2' },
tools: ['web-search/search', 'memory/save'],
browser: ['read', 'screenshot'],
},
limits: { maxToolCalls: 20, ttlMinutes: 30 },
options: { systemPrompt: 'Cite sources.', temperature: 0.2 },
});
const response = await session.prompt('Find AI news');
const result = await session.callTool('web-search/search', { query: 'AI' });
const allowed = session.listAllowedTools();
await session.terminate();
const summaries = await window.agent.sessions.list();
```
---
## Multi-Agent API (multiAgent flag)
```javascript
const me = await window.agent.agents.register({
name: 'Research Agent',
description: 'Researches and summarizes topics',
capabilities: ['search', 'summarize'],
acceptsInvocations: true,
acceptsMessages: true,
});
window.agent.agents.onInvoke(async (req) => {
if (req.task === 'search') return { results: [/* … */] };
throw new Error('Unknown task');
});
window.agent.agents.onMessage((msg) => {
console.log('from', msg.from, msg.payload);
});
const { agents } = await window.agent.agents.discover({ capabilities: ['analyze'] });
const response = await window.agent.agents.invoke(agents[0].id, {
task: 'analyze', input: data, timeout: 30000,
});
await window.agent.agents.send(otherId, { hello: 'world' });
await window.agent.agents.broadcast('event-name', { foo: 1 });
```
### Orchestration
```javascript
await window.agent.agents.orchestrate.pipeline({
steps: [
{ agentId: 'researcher', task: 'research' },
{ agentId: 'writer', task: 'write' },
],
}, initialInput);
await window.agent.agents.orchestrate.parallel({
tasks: [{ agentId: 'a1', task: 'analyze', input: data }],
combineStrategy: 'merge', // 'array' | 'merge' | 'first'
});
await window.agent.agents.orchestrate.route({
routes: [{ condition: 'type:technical', agentId: 'tech-agent' }],
defaultAgentId: 'general-agent',
}, input, task);
```
---
## BYOC — `navigator.modelContext` + `agent.mcp` + `agent.chat`
### Page-declared tools (WebMCP polyfill)
```javascript
navigator.modelContext.addTool({
name: 'add_to_cart',
description: 'Add a product to the cart',
inputSchema: { type: 'object', properties: { productId: { type: 'string' } } },
handler: async ({ productId }) => { /* … */ },
});
```
These show up in `agent.tools.list()` as `page/add_to_cart`.
### Site-hosted MCP server registration
```html
<link rel="mcp-server" href="https://shop.example/mcp"
title="Shop" data-tools="search,add_to_cart">
```
```javascript
const servers = await window.agent.mcp.discover();
const reg = await window.agent.mcp.register({
url: 'https://shop.example/mcp',
name: 'Shop Assistant',
tools: ['search', 'add_to_cart'],
transport: 'sse', // 'sse' | 'stdio' | 'streamable-http'
});
await window.agent.mcp.unregister(reg.serverId);
```
### Open the user's chat with a site-specific config
```javascript
const chat = await window.agent.chat.open({
systemPrompt: 'Shopping assistant',
initialMessage: 'How can I help?',
tools: ['search', 'add_to_cart'],
style: { theme: 'light', accentColor: '#ff9900', position: 'right' },
});
await window.agent.chat.close(chat.chatId);
```
---
## Error Codes
| Code | Meaning |
|------|---------|
| `ERR_FEATURE_DISABLED` | Feature flag is off (check `agent.capabilities()`) |
| `ERR_NOT_INSTALLED` | Extension not installed |
| `ERR_PERMISSION_DENIED` | User denied or scope not granted |
| `ERR_SCOPE_REQUIRED` | Missing permission scope |
| `ERR_INVALID_REQUEST` | Bad parameters |
| `ERR_TOOL_NOT_ALLOWED` | Tool not in allowlist |
| `ERR_TOOL_NOT_FOUND` | Tool doesn't exist |
| `ERR_TOOL_FAILED` | Tool execution failed |
| `ERR_TOOL_TIMEOUT` | Tool call timed out |
| `ERR_MODEL_FAILED` | LLM error |
| `ERR_SESSION_NOT_FOUND` | Session terminated/destroyed |
| `ERR_TIMEOUT` | Request timed out |
| `ERR_ELEMENT_NOT_FOUND` | DOM ref not found |
| `ERR_AGENT_NOT_FOUND` | Agent not registered |
| `ERR_AGENT_NOT_ACCEPTING` | Agent doesn't accept invocations/messages |
| `ERR_AGENT_FAILED` | Agent invocation threw |
| `ERR_HANDLER_FAILED` | Page handler threw |
| `ERR_INTERNAL` | Internal error |
```javascript
try {
await window.agent.tools.list();
} catch (err) {
if (err.code === 'ERR_FEATURE_DISABLED') { /* tell user to enable in sidebar */ }
if (err.code === 'ERR_PERMISSION_DENIED') { /* fall back */ }
}
```
---
## Feature Flags
Users toggle these in the Web Agents API sidebar. Read them at runtime via `agent.capabilities().features`.
| Flag | Default | Surfaces |
|------|---------|----------|
| `textGeneration` | ON | `window.ai.*` |
| `toolAccess` | ON | `agent.tools.*`, `agent.sessions.*` |
| `toolCalling` | OFF | `agent.run()` |
| `browserInteraction` | OFF | `agent.browser.activeTab.*` |
| `browserControl` | OFF | `agent.browser.navigate/fetch/tabs/tab` |
| `multiAgent` | OFF | `agent.agents.*` |
---
## Debug logging
The injected script is silent by default. Opt in via any of:
```html
<meta name="web-agents-api-debug" content="1">
```
```javascript
localStorage.setItem('web-agents-api:debug', '1');
window.__webAgentsApiDebug = true; // before injection
```
---
## TypeScript Types (Quick Reference)
```typescript
type PermissionScope =
| 'model:prompt' | 'model:tools' | 'model:list'
| 'mcp:tools.list' | 'mcp:tools.call' | 'mcp:servers.register'
| 'browser:activeTab.read' | 'browser:activeTab.interact' | 'browser:activeTab.screenshot'
| 'browser:tabs.read' | 'browser:tabs.create' | 'browser:navigate'
| 'web:fetch' | 'chat:open'
| 'agents:register' | 'agents:discover' | 'agents:invoke' | 'agents:message';
type PermissionGrant = 'granted-once' | 'granted-always' | 'denied' | 'not-granted';
interface ToolDescriptor {
serverId: string;
name: string;
description?: string;
inputSchema?: unknown;
}
type RunEvent =
| { type: 'thinking'; content: string }
| { type: 'tool_call'; tool: string; args: Record<string, unknown> }
| { type: 'tool_result'; tool: string; result: unknown }
| { type: 'final'; output: string }
| { type: 'error'; error: string };
interface TextSession {
sessionId: string;
prompt(input: string): Promise<string>;
promptStreaming(input: string): AsyncIterable<string>; // plain strings
destroy(): void;
}
interface ActiveTabReadability {
title: string; url: string; content: string; length: number;
}
interface TabInfo { id: number; url: string; title: string; active: boolean; }
interface FetchResponse {
ok: boolean; status: number; statusText: string;
headers: Record<string, string>; body: string; url: string;
}
```
---
## Common Patterns
### Initialize app with feature detection + permissions
```javascript
async function init() {
if (!window.agent) { showError('Install Harbor extension'); return false; }
const caps = window.agent.capabilities();
if (!caps.features.textGeneration) { showError('Enable text generation'); return false; }
const { granted } = await window.agent.requestPermissions({
scopes: ['model:prompt', 'mcp:tools.list', 'mcp:tools.call'],
reason: 'Enable AI features',
});
return granted;
}
```
### Streaming chat
```javascript
let session;
async function sendMessageStreaming(text, onChunk) {
session ??= await window.ai.createTextSession({ systemPrompt: 'Be helpful.' });
for await (const chunk of session.promptStreaming(text)) onChunk(chunk);
}
```
### Page summarizer
```javascript
async function summarizePage() {
const { content, title } = await window.agent.browser.activeTab.readability();
const session = await window.ai.createTextSession({
systemPrompt: 'Summarize in 2-3 sentences.',
});
const summary = await session.prompt(content.slice(0, 10000));
session.destroy();
return { title, summary };
}
```
### Tool-calling agent
```javascript
async function runAgent(task) {
const events = [];
for await (const ev of window.agent.run({ task, maxToolCalls: 5 })) {
events.push(ev);
if (ev.type === 'final') return { output: ev.output, events };
if (ev.type === 'error') throw new Error(ev.error);
}
}
```
### Graceful degradation
```javascript
async function getAIResponse(prompt) {
if (window.agent?.capabilities().features.textGeneration) {
try {
const { granted } = await window.agent.requestPermissions({ scopes: ['model:prompt'] });
if (granted) {
const session = await window.ai.createTextSession();
const response = await session.prompt(prompt);
session.destroy();
return response;
}
} catch (e) { console.warn('Web Agents API failed:', e); }
}
return await fetch('/api/ai', { method: 'POST', body: JSON.stringify({ prompt }) })
.then(r => r.json());
}
```
---
## Key Files (for Harbor development)
```
web-agents-api/src/
├── injected.ts # window.ai/agent/navigator.modelContext (this doc's source of truth)
├── content-script.ts # Page ↔ background relay
├── background.ts # Cross-extension bridge to Harbor
└── handlers/ # Per-message handlers (browser, agents, sessions, mcp, chat, tools)
extension/src/ # Harbor core: LLM providers, MCP client, native bridge
bridge-rs/src/ # Native messaging host (Rust)
```
---
## Version
Web Agents API v1.4.0 / Harbor v1