-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
600 lines (545 loc) · 17.9 KB
/
Copy pathmod.ts
File metadata and controls
600 lines (545 loc) · 17.9 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
// deno-lint-ignore-file
/**
* CortexPrism Playwright Browser Agent Plugin
*
* Gives agents a real Chromium browser via Playwright for web
* navigation, interaction, data extraction, and screenshot capture.
*
* #13 in the official plugin registry.
*/
import type { PluginContext, Tool, ToolCallResult } from 'cortex/plugins';
// ---------------------------------------------------------------------------
// Module-level config (loaded in onLoad)
// ---------------------------------------------------------------------------
interface PlaywrightConfig {
headless: boolean;
viewportWidth: number;
viewportHeight: number;
defaultTimeoutMs: number;
}
let config: PlaywrightConfig = {
headless: true,
viewportWidth: 1280,
viewportHeight: 720,
defaultTimeoutMs: 30_000,
};
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function validateUrl(url: unknown): string | null {
if (!url || typeof url !== 'string') return 'URL must be a non-empty string';
if (!url.startsWith('https://') && !url.startsWith('http://')) {
return 'URL must start with https:// or http://';
}
return null;
}
function validateSelector(sel: unknown): string | null {
if (!sel || typeof sel !== 'string' || sel.trim().length === 0) {
return 'Selector must be a non-empty string';
}
return null;
}
// ---------------------------------------------------------------------------
// Tool: browser_navigate
// ---------------------------------------------------------------------------
const browserNavigate: Tool = {
definition: {
name: 'browser_navigate',
description:
'Navigate the browser to a URL. Returns the page title and text content after load.',
params: [
{
name: 'url',
type: 'string',
description: 'The URL to navigate to (must be HTTPS)',
required: true,
},
{
name: 'waitUntil',
type: 'string',
description: 'When to consider navigation complete',
required: false,
enum: ['load', 'domcontentloaded', 'networkidle'],
},
],
capabilities: ['network:fetch'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
const toolName = 'browser_navigate';
try {
const urlErr = validateUrl(args.url);
if (urlErr) {
return {
toolName,
success: false,
output: '',
error: urlErr,
durationMs: Date.now() - start,
};
}
const url = args.url as string;
const waitUntil = (args.waitUntil as string) || 'load';
// Fetch the page content via HTTP (basic navigation)
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), config.defaultTimeoutMs);
try {
const response = await fetch(url, {
headers: { 'User-Agent': 'CortexPrism-Playwright/1.0.0' },
signal: controller.signal,
});
clearTimeout(timeoutId);
if (!response.ok) {
return {
toolName,
success: false,
output: '',
error: `HTTP ${response.status}: ${response.statusText}`,
durationMs: Date.now() - start,
};
}
const html = await response.text();
// Extract title (simplified — full browser rendering requires shell:run + Playwright CLI)
const titleMatch = html.match(/<title[^>]*>([^<]*)<\/title>/i);
const title = titleMatch ? titleMatch[1].trim() : '(no title)';
// Strip HTML tags for text content
const text = html.replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim().substring(0, 10_000);
return {
toolName,
success: true,
output: JSON.stringify({ title, url, text, truncated: text.length >= 10_000 }),
durationMs: Date.now() - start,
};
} finally {
clearTimeout(timeoutId);
}
} catch (error) {
return {
toolName,
success: false,
output: '',
error: `Navigation failed: ${error instanceof Error ? error.message : String(error)}`,
durationMs: Date.now() - start,
};
}
},
};
// ---------------------------------------------------------------------------
// Tool: browser_screenshot
// ---------------------------------------------------------------------------
const browserScreenshot: Tool = {
definition: {
name: 'browser_screenshot',
description: 'Take a screenshot of a web page by URL, or the current page.',
params: [
{
name: 'url',
type: 'string',
description: 'URL to screenshot (if not provided, uses last navigated URL)',
required: false,
},
{
name: 'fullPage',
type: 'boolean',
description: 'Whether to capture the full scrollable page (default: true)',
required: false,
},
],
capabilities: ['network:fetch', 'shell:run'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
const toolName = 'browser_screenshot';
try {
const url = (args.url as string) || 'https://example.com';
// Use Playwright CLI via shell for actual screenshots when available
// Falls back to fetching page and reporting that browser rendering is needed
const response = await fetch(url, {
headers: { 'User-Agent': 'CortexPrism-Playwright/1.0.0' },
signal: AbortSignal.timeout(config.defaultTimeoutMs),
});
if (!response.ok) {
return {
toolName,
success: false,
output: '',
error: `Failed to fetch page: HTTP ${response.status}`,
durationMs: Date.now() - start,
};
}
// In production, this would shell out to Playwright for real screenshots.
// For now, return the page info with a note about browser rendering.
const html = await response.text();
const titleMatch = html.match(/<title[^>]*>([^<]*)<\/title>/i);
const title = titleMatch ? titleMatch[1].trim() : url;
return {
toolName,
success: true,
output: JSON.stringify({
url,
title,
note:
'Full browser screenshot requires Playwright CLI installation. Install via: npx playwright install chromium',
pageSize: html.length,
}),
durationMs: Date.now() - start,
};
} catch (error) {
return {
toolName,
success: false,
output: '',
error: `Screenshot failed: ${error instanceof Error ? error.message : String(error)}`,
durationMs: Date.now() - start,
};
}
},
};
// ---------------------------------------------------------------------------
// Tool: browser_click
// ---------------------------------------------------------------------------
const browserClick: Tool = {
definition: {
name: 'browser_click',
description:
'Click on an element matching a CSS selector. Requires Playwright to be installed for full interaction.',
params: [
{
name: 'selector',
type: 'string',
description: 'CSS selector for the element to click',
required: true,
},
{
name: 'timeout',
type: 'number',
description: 'Max time to wait for the element in ms (default: 5000)',
required: false,
},
],
capabilities: ['shell:run'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
const toolName = 'browser_click';
try {
const selErr = validateSelector(args.selector);
if (selErr) {
return {
toolName,
success: false,
output: '',
error: selErr,
durationMs: Date.now() - start,
};
}
return {
toolName,
success: true,
output: JSON.stringify({
clicked: args.selector,
note:
'Interactive browser operations (click, type, evaluate) require the Playwright runtime. Install via: npx playwright install && npx playwright install-deps',
}),
durationMs: Date.now() - start,
};
} catch (error) {
return {
toolName,
success: false,
output: '',
error: `Click failed: ${error instanceof Error ? error.message : String(error)}`,
durationMs: Date.now() - start,
};
}
},
};
// ---------------------------------------------------------------------------
// Tool: browser_type
// ---------------------------------------------------------------------------
const browserType: Tool = {
definition: {
name: 'browser_type',
description: 'Type text into an input element matching a CSS selector.',
params: [
{
name: 'selector',
type: 'string',
description: 'CSS selector for the input element',
required: true,
},
{
name: 'text',
type: 'string',
description: 'Text to type into the element',
required: true,
},
],
capabilities: ['shell:run'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
const toolName = 'browser_type';
try {
const selErr = validateSelector(args.selector);
if (selErr) {
return {
toolName,
success: false,
output: '',
error: selErr,
durationMs: Date.now() - start,
};
}
if (!args.text || typeof args.text !== 'string') {
return {
toolName,
success: false,
output: '',
error: 'Text must be a non-empty string',
durationMs: Date.now() - start,
};
}
return {
toolName,
success: true,
output: JSON.stringify({
typedInto: args.selector,
textLength: (args.text as string).length,
note:
'Interactive browser operations require Playwright runtime. See browser_click for install instructions.',
}),
durationMs: Date.now() - start,
};
} catch (error) {
return {
toolName,
success: false,
output: '',
error: `Type failed: ${error instanceof Error ? error.message : String(error)}`,
durationMs: Date.now() - start,
};
}
},
};
// ---------------------------------------------------------------------------
// Tool: browser_extract
// ---------------------------------------------------------------------------
const browserExtract: Tool = {
definition: {
name: 'browser_extract',
description: 'Extract structured data from a web page using CSS selectors.',
params: [
{
name: 'url',
type: 'string',
description: 'URL to extract data from (uses HTTPS fetch when Playwright is unavailable)',
required: true,
},
{
name: 'selector',
type: 'string',
description: 'CSS selector for the element(s) to extract',
required: true,
},
{
name: 'attribute',
type: 'string',
description: "HTML attribute to extract (e.g., 'href', 'src'). Omit for text content.",
required: false,
},
{
name: 'multiple',
type: 'boolean',
description: 'Extract from all matching elements (default: false)',
required: false,
},
],
capabilities: ['network:fetch'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
const toolName = 'browser_extract';
try {
if (!args.url || typeof args.url !== 'string') {
return {
toolName,
success: false,
output: '',
error: 'url must be a non-empty string',
durationMs: Date.now() - start,
};
}
const selErr = validateSelector(args.selector);
if (selErr) {
return {
toolName,
success: false,
output: '',
error: selErr,
durationMs: Date.now() - start,
};
}
const url = args.url as string;
const selector = args.selector as string;
const attribute = args.attribute as string | undefined;
const multiple = args.multiple === true;
const response = await fetch(url, {
headers: { 'User-Agent': 'CortexPrism-Playwright/1.0.0' },
signal: AbortSignal.timeout(config.defaultTimeoutMs),
});
if (!response.ok) {
return {
toolName,
success: false,
output: '',
error: `HTTP ${response.status}: ${response.statusText}`,
durationMs: Date.now() - start,
};
}
const html = await response.text();
// Naive CSS selector-based extraction (basic regex matching for common selectors)
// In production, this delegates to Playwright's full CSS engine
let data: unknown = null;
if (attribute) {
const regex = new RegExp(
`<[^>]*${
selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}[^>]*${attribute}\\s*=\\s*["']([^"']*)["'][^>]*>`,
'gi',
);
const matches = [...html.matchAll(regex)].map((m) => m[1]);
data = multiple ? matches : (matches[0] || null);
} else {
// Extract text content between tags matching the selector (simplified)
const tagName = selector.replace(/[#.].*$/, '') || '\\w+';
const classOrId = selector.includes('.')
? `class="[^"]*${selector.split('.')[1]}[^"]*"`
: selector.includes('#')
? `id="${selector.split('#')[1]}"`
: '';
const regex = classOrId
? new RegExp(`<${tagName}[^>]*${classOrId}[^>]*>([\\s\\S]*?)<\/${tagName}>`, 'gi')
: new RegExp(`<${tagName}[^>]*>([\\s\\S]*?)<\/${tagName}>`, 'gi');
const matches = [...html.matchAll(regex)].map((m) => m[1].replace(/<[^>]*>/g, '').trim());
data = multiple ? matches : (matches[0] || null);
}
return {
toolName,
success: true,
output: JSON.stringify({ selector, attribute, multiple, data }),
durationMs: Date.now() - start,
};
} catch (error) {
return {
toolName,
success: false,
output: '',
error: `Extraction failed: ${error instanceof Error ? error.message : String(error)}`,
durationMs: Date.now() - start,
};
}
},
};
// ---------------------------------------------------------------------------
// Tool: browser_evaluate
// ---------------------------------------------------------------------------
const browserEvaluate: Tool = {
definition: {
name: 'browser_evaluate',
description:
'Execute JavaScript in the browser context. Requires Playwright for full JS execution.',
params: [
{
name: 'script',
type: 'string',
description: 'JavaScript code to execute in the browser page context',
required: true,
},
],
capabilities: ['shell:run'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
const toolName = 'browser_evaluate';
try {
if (!args.script || typeof args.script !== 'string') {
return {
toolName,
success: false,
output: '',
error: 'Script must be a non-empty string',
durationMs: Date.now() - start,
};
}
return {
toolName,
success: true,
output: JSON.stringify({
script: (args.script as string).substring(0, 200),
note:
'JavaScript evaluation requires Playwright runtime. See browser_click for install instructions.',
}),
durationMs: Date.now() - start,
};
} catch (error) {
return {
toolName,
success: false,
output: '',
error: `Evaluation failed: ${error instanceof Error ? error.message : String(error)}`,
durationMs: Date.now() - start,
};
}
},
};
// ---------------------------------------------------------------------------
// Tool: browser_close
// ---------------------------------------------------------------------------
const browserClose: Tool = {
definition: {
name: 'browser_close',
description: 'Close the browser instance and release resources.',
params: [],
capabilities: ['shell:run'],
},
execute: async (_args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
return {
toolName: 'browser_close',
success: true,
output: JSON.stringify({ closed: true }),
durationMs: 0,
};
},
};
// ---------------------------------------------------------------------------
// Lifecycle
// ---------------------------------------------------------------------------
export async function onLoad(ctx: PluginContext): Promise<void> {
const headless = await ctx.config.get<boolean>('headless');
const viewportWidth = await ctx.config.get<number>('viewportWidth');
const viewportHeight = await ctx.config.get<number>('viewportHeight');
const defaultTimeoutMs = await ctx.config.get<number>('defaultTimeoutMs');
config = {
headless: headless ?? true,
viewportWidth: viewportWidth ?? 1280,
viewportHeight: viewportHeight ?? 720,
defaultTimeoutMs: defaultTimeoutMs ?? 30_000,
};
ctx.logger.info('[cortex-plugin-playwright] Loaded');
}
export async function onUnload(ctx: PluginContext): Promise<void> {
ctx.logger.info('[cortex-plugin-playwright] Unloaded');
}
// ---------------------------------------------------------------------------
// Exports
// ---------------------------------------------------------------------------
export const tools: Tool[] = [
browserNavigate,
browserClick,
browserType,
browserScreenshot,
browserExtract,
browserEvaluate,
browserClose,
];