-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
288 lines (267 loc) · 9.56 KB
/
Copy pathindex.ts
File metadata and controls
288 lines (267 loc) · 9.56 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
import type { PluginAPI, PluginToolResult } from '@ampcode/plugin'
import { captureScreenshot } from './src/chrome.ts'
import { extractLinks, extractTitle, htmlToMarkdown } from './src/html.ts'
import { fetchPage } from './src/http.ts'
import { searchWeb } from './src/search.ts'
import { normalizeUrl, urlsInText } from './src/url.ts'
export const description =
'Web browser tools for Amp: fetch pages as readable text, search the web, list page links, take real screenshots via headless Chrome, and open URLs.'
const MAX_TEXT_CHARS = 24_000
function asString(value: unknown): string {
return typeof value === 'string' ? value : ''
}
function asNumber(value: unknown, fallback: number): number {
return typeof value === 'number' && Number.isFinite(value) ? value : fallback
}
function truncate(text: string, limit: number): string {
if (text.length <= limit) return text
return `${text.slice(0, limit)}\n\n[truncated at ${limit} chars — fetch again with a different selector or maxChars]`
}
function requireUrl(input: Record<string, unknown>): string {
const raw = asString(input.url)
if (!raw.trim()) {
throw new Error('Missing required "url" parameter.')
}
return normalizeUrl(raw).toString()
}
export default function (amp: PluginAPI) {
amp.logger.log('[browser] plugin initialized')
amp.registerTool({
name: 'browser_fetch',
title: 'Fetch web page',
transcriptGroup: { active: 'Fetching web page', complete: 'Fetched web page' },
description:
'Fetch a web page over HTTP and return its readable text as markdown. Use for reading documentation, articles, and pages the user links. For JavaScript-heavy pages that render empty, use browser_screenshot instead.',
inputSchema: {
type: 'object',
properties: {
url: { type: 'string', description: 'URL to fetch (https:// is added if omitted).' },
maxChars: {
type: 'number',
description: `Maximum characters of page text to return (default ${MAX_TEXT_CHARS}).`,
},
format: {
type: 'string',
enum: ['text', 'html'],
description: '"text" (default) returns readable markdown; "html" returns the raw document.',
},
},
required: ['url'],
},
async execute(input) {
const url = requireUrl(input)
const maxChars = asNumber(input.maxChars, MAX_TEXT_CHARS)
const format = asString(input.format) || 'text'
const page = await fetchPage(url)
const isHtml = page.contentType.includes('html') || /<html[\s>]/i.test(page.body.slice(0, 2000))
if (format === 'html' || !isHtml) {
const label = page.truncated ? ' (download truncated)' : ''
return `HTTP ${page.status} ${page.url}${label}\n\n${truncate(page.body, maxChars)}`
}
const title = extractTitle(page.body)
const text = htmlToMarkdown(page.body, page.url)
const header = [`HTTP ${page.status} ${page.url}`]
if (title) header.push(`Title: ${title}`)
if (page.truncated) header.push('(download truncated)')
if (!text.trim()) {
header.push(
'(no readable text extracted — the page may require JavaScript; try browser_screenshot)',
)
}
return `${header.join('\n')}\n\n${truncate(text, maxChars)}`
},
})
amp.registerTool({
name: 'browser_search',
title: 'Search the web',
transcriptGroup: { active: 'Searching the web', complete: 'Searched the web' },
description:
'Search the web with DuckDuckGo and return titles, URLs, and snippets. No API key required. Follow up with browser_fetch to read a result.',
inputSchema: {
type: 'object',
properties: {
query: { type: 'string', description: 'Search query.' },
maxResults: { type: 'number', description: 'Number of results, 1-20 (default 5).' },
},
required: ['query'],
},
async execute(input) {
const query = asString(input.query).trim()
if (!query) throw new Error('Missing required "query" parameter.')
const maxResults = asNumber(input.maxResults, 5)
const results = await searchWeb(query, { maxResults })
if (results.length === 0) {
return `No results for "${query}".`
}
return results
.map((r, i) => `${i + 1}. ${r.title}\n ${r.url}\n ${r.snippet}`)
.join('\n\n')
},
})
amp.registerTool({
name: 'browser_links',
title: 'List page links',
transcriptGroup: { active: 'Listing page links', complete: 'Listed page links' },
description:
'Fetch a page and list its outbound links with anchor text, resolved to absolute URLs. Useful for navigating docs sites or finding downloads.',
inputSchema: {
type: 'object',
properties: {
url: { type: 'string', description: 'Page URL to extract links from.' },
filter: {
type: 'string',
description: 'Optional substring filter applied to link text and URL (case-insensitive).',
},
maxLinks: { type: 'number', description: 'Maximum links to return (default 50).' },
},
required: ['url'],
},
async execute(input) {
const url = requireUrl(input)
const filter = asString(input.filter).toLowerCase()
const maxLinks = asNumber(input.maxLinks, 50)
const page = await fetchPage(url)
let links = extractLinks(page.body, page.url)
if (filter) {
links = links.filter(
(l) => l.text.toLowerCase().includes(filter) || l.href.toLowerCase().includes(filter),
)
}
const shown = links.slice(0, Math.max(1, maxLinks))
const suffix = links.length > shown.length ? `\n\n(${links.length - shown.length} more links omitted)` : ''
return (
`${shown.length} link(s) on ${page.url}:\n\n` +
shown.map((l) => `- [${l.text}](${l.href})`).join('\n') +
suffix
)
},
})
amp.registerTool({
name: 'browser_screenshot',
title: 'Screenshot web page',
transcriptGroup: { active: 'Screenshotting web page', complete: 'Screenshot web page' },
description:
'Render a URL in headless Chrome and return a PNG screenshot. Use for JavaScript-rendered pages, visual verification of web UI, or when browser_fetch returns empty content.',
inputSchema: {
type: 'object',
properties: {
url: { type: 'string', description: 'URL to render.' },
width: { type: 'number', description: 'Viewport width in px (default 1280).' },
height: { type: 'number', description: 'Viewport height in px (default 800).' },
},
required: ['url'],
},
async execute(input): Promise<PluginToolResult> {
const url = requireUrl(input)
const png = await captureScreenshot(url, {
width: asNumber(input.width, 1280),
height: asNumber(input.height, 800),
})
try {
const { url: imageUrl } = await amp.attachments.upload({ data: png, mimeType: 'image/png' })
return [
{ type: 'text', text: `Screenshot of ${url}` },
{ type: 'image', mimeType: 'image/png', url: imageUrl },
]
} catch {
return [
{ type: 'text', text: `Screenshot of ${url}` },
{ type: 'image', mimeType: 'image/png', data: Buffer.from(png).toString('base64') },
]
}
},
})
amp.registerTool({
name: 'browser_open',
title: 'Open URL in browser',
transcriptGroup: { active: 'Opening URL in browser', complete: 'Opened URL in browser' },
description:
"Open a URL in the user's default web browser. Use when the user asks to open a page, or when they should look at something themselves.",
inputSchema: {
type: 'object',
properties: {
url: { type: 'string', description: 'URL to open in the system browser.' },
},
required: ['url'],
},
async execute(input) {
const url = requireUrl(input)
await amp.system.open(url)
return `Opened ${url} in the browser.`
},
})
amp.registerCommand(
'browse-url',
{
title: 'Browse URL',
category: 'browser',
description: 'Fetch a URL and append its readable text to the current thread.',
},
async (ctx) => {
const input = await ctx.ui.input({
title: 'Browse URL',
helpText: 'Enter a URL to fetch and append to this thread.',
submitButtonText: 'Fetch',
})
if (!input) return
let page
try {
page = await fetchPage(input)
} catch (error) {
await ctx.ui.notify(`Fetch failed: ${error instanceof Error ? error.message : String(error)}`)
return
}
const text = htmlToMarkdown(page.body, page.url)
const title = extractTitle(page.body)
await ctx.ui.notify(`Fetched ${page.url}${title ? ` — ${title}` : ''} (${text.length} chars).`)
if (!ctx.thread) {
await ctx.ui.notify('No active thread. Send any message to create one, then re-run this command.')
return
}
await ctx.thread.append([
{
type: 'user-message',
content: `Content of ${page.url}${title ? ` (${title})` : ''}:\n\n${truncate(text, MAX_TEXT_CHARS)}`,
},
])
},
)
amp.registerCommand(
'open-url',
{
title: 'Open URL in browser',
category: 'browser',
description: 'Open a URL in your default web browser.',
},
async (ctx) => {
const input = await ctx.ui.input({
title: 'Open URL',
helpText: 'Enter a URL to open in your default browser.',
submitButtonText: 'Open',
})
if (!input) return
try {
await ctx.system.open(normalizeUrl(input))
} catch (error) {
await ctx.ui.notify(`Could not open URL: ${error instanceof Error ? error.message : String(error)}`)
}
},
)
// When a turn starts with URLs in the prompt, give the agent a hidden hint
// that the browser tools exist for inspecting them.
amp.on('agent.start', (event) => {
const urls = urlsInText(event.message)
if (urls.length === 0) return {}
return {
message: {
content:
`This prompt mentions ${urls.length} URL(s): ${urls.join(', ')}. ` +
'Use browser_fetch to read page text, browser_screenshot to render it visually, or browser_links to enumerate its links.',
display: false,
},
}
})
amp.onDispose(() => {
amp.logger.log('[browser] plugin disposing')
})
}