codecane 1.0.149 → 1.0.150
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/README.md +1 -1
- package/dist/__tests__/browser-runner.test.d.ts +1 -0
- package/dist/__tests__/browser-runner.test.js +15 -0
- package/dist/__tests__/browser-runner.test.js.map +1 -0
- package/dist/browser-runner.d.ts +33 -0
- package/dist/browser-runner.js +599 -0
- package/dist/browser-runner.js.map +1 -0
- package/dist/chat-storage.d.ts +2 -4
- package/dist/chat-storage.js +74 -44
- package/dist/chat-storage.js.map +1 -1
- package/dist/cli.d.ts +2 -1
- package/dist/cli.js +44 -24
- package/dist/cli.js.map +1 -1
- package/dist/client.d.ts +7 -3
- package/dist/client.js +55 -37
- package/dist/client.js.map +1 -1
- package/dist/code-map/languages.js +7 -17
- package/dist/code-map/languages.js.map +1 -1
- package/dist/code-map/parse.js +7 -17
- package/dist/code-map/parse.js.map +1 -1
- package/dist/code-map/tsconfig.tsbuildinfo +1 -1
- package/dist/common/actions.d.ts +100 -81
- package/dist/common/actions.js +4 -0
- package/dist/common/actions.js.map +1 -1
- package/dist/common/advanced-analyzer.d.ts +19 -0
- package/dist/common/advanced-analyzer.js +140 -0
- package/dist/common/advanced-analyzer.js.map +1 -0
- package/dist/common/browser-actions.d.ts +4335 -0
- package/dist/common/browser-actions.js +330 -0
- package/dist/common/browser-actions.js.map +1 -0
- package/dist/common/constants.d.ts +3 -1
- package/dist/common/constants.js +4 -3
- package/dist/common/constants.js.map +1 -1
- package/dist/common/util/credentials.d.ts +2 -2
- package/dist/common/util/file.d.ts +3 -0
- package/dist/common/util/file.js +1 -0
- package/dist/common/util/file.js.map +1 -1
- package/dist/common/websockets/websocket-schema.d.ts +216 -158
- package/dist/index.js +17 -22
- package/dist/index.js.map +1 -1
- package/dist/menu.d.ts +2 -0
- package/dist/menu.js +29 -18
- package/dist/menu.js.map +1 -1
- package/dist/project-files.d.ts +6 -0
- package/dist/project-files.js +64 -22
- package/dist/project-files.js.map +1 -1
- package/dist/tool-handlers.d.ts +1 -0
- package/dist/tool-handlers.js +67 -6
- package/dist/tool-handlers.js.map +1 -1
- package/dist/utils/terminal.js +9 -23
- package/dist/utils/terminal.js.map +1 -1
- package/package.json +4 -3
- package/dist/common/util/helpers.d.ts +0 -1
- package/dist/common/util/helpers.js +0 -6
- package/dist/common/util/helpers.js.map +0 -1
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ You can also control the cost/quality tradeoff with these flags:
|
|
|
32
32
|
|
|
33
33
|
```bash
|
|
34
34
|
codebuff --lite # Budget mode for more econonmical responses
|
|
35
|
-
codebuff --
|
|
35
|
+
codebuff --max # Premium mode for higher quality responses (--o1 also works)
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
After running `codebuff`, simply chat with it to say what coding task you want done.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const browser_runner_1 = require("../browser-runner");
|
|
4
|
+
// Add your tests here
|
|
5
|
+
describe('BrowserRunner', () => {
|
|
6
|
+
let runner;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
runner = new browser_runner_1.BrowserRunner();
|
|
9
|
+
});
|
|
10
|
+
afterEach(async () => {
|
|
11
|
+
await runner.shutdown();
|
|
12
|
+
});
|
|
13
|
+
// Add test cases...
|
|
14
|
+
});
|
|
15
|
+
//# sourceMappingURL=browser-runner.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-runner.test.js","sourceRoot":"","sources":["../../src/__tests__/browser-runner.test.ts"],"names":[],"mappings":";;AAAA,sDAAiD;AAGjD,sBAAsB;AACtB,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,IAAI,MAAqB,CAAA;IAEzB,UAAU,CAAC,GAAG,EAAE;QACd,MAAM,GAAG,IAAI,8BAAa,EAAE,CAAA;IAC9B,CAAC,CAAC,CAAA;IAEF,SAAS,CAAC,KAAK,IAAI,EAAE;QACnB,MAAM,MAAM,CAAC,QAAQ,EAAE,CAAA;IACzB,CAAC,CAAC,CAAA;IAEF,oBAAoB;AACtB,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BrowserAction, BrowserResponse } from 'common/src/browser-actions';
|
|
2
|
+
export declare class BrowserRunner {
|
|
3
|
+
getLogs(): BrowserResponse['logs'];
|
|
4
|
+
getNetworkEvents(): BrowserResponse['networkEvents'];
|
|
5
|
+
private browser;
|
|
6
|
+
private page;
|
|
7
|
+
private logs;
|
|
8
|
+
private jsErrorCount;
|
|
9
|
+
private retryCount;
|
|
10
|
+
private startTime;
|
|
11
|
+
private consecutiveErrors;
|
|
12
|
+
private totalErrors;
|
|
13
|
+
constructor();
|
|
14
|
+
private maxConsecutiveErrors;
|
|
15
|
+
private totalErrorThreshold;
|
|
16
|
+
private performanceMetrics;
|
|
17
|
+
private networkEvents;
|
|
18
|
+
private executeWithRetry;
|
|
19
|
+
private executeAction;
|
|
20
|
+
private logErrorForAnalysis;
|
|
21
|
+
private startBrowser;
|
|
22
|
+
private navigate;
|
|
23
|
+
private typeText;
|
|
24
|
+
private scroll;
|
|
25
|
+
private takeScreenshot;
|
|
26
|
+
private attachPageListeners;
|
|
27
|
+
private collectPerformanceMetrics;
|
|
28
|
+
private collectMetrics;
|
|
29
|
+
private filterLogs;
|
|
30
|
+
execute(action: BrowserAction): Promise<BrowserResponse>;
|
|
31
|
+
shutdown(): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export declare const handleBrowserInstruction: (action: BrowserAction) => Promise<BrowserResponse>;
|
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.handleBrowserInstruction = exports.BrowserRunner = void 0;
|
|
30
|
+
const puppeteer_1 = __importDefault(require("puppeteer"));
|
|
31
|
+
const child_process_1 = require("child_process");
|
|
32
|
+
const browser_actions_1 = require("common/src/browser-actions");
|
|
33
|
+
const fs = __importStar(require("fs"));
|
|
34
|
+
const path = __importStar(require("path"));
|
|
35
|
+
const project_files_1 = require("./project-files");
|
|
36
|
+
const file_1 = require("./common/util/file");
|
|
37
|
+
// Single browser instance for the application
|
|
38
|
+
let activeBrowserRunner = null;
|
|
39
|
+
class BrowserRunner {
|
|
40
|
+
// Add getter methods for diagnostic loop
|
|
41
|
+
getLogs() {
|
|
42
|
+
return this.logs;
|
|
43
|
+
}
|
|
44
|
+
getNetworkEvents() {
|
|
45
|
+
return this.networkEvents;
|
|
46
|
+
}
|
|
47
|
+
browser = null;
|
|
48
|
+
page = null;
|
|
49
|
+
logs = [];
|
|
50
|
+
jsErrorCount = 0;
|
|
51
|
+
retryCount = 0;
|
|
52
|
+
startTime = 0;
|
|
53
|
+
// Error tracking
|
|
54
|
+
consecutiveErrors = 0;
|
|
55
|
+
totalErrors = 0;
|
|
56
|
+
constructor() { }
|
|
57
|
+
// Error tracking configuration
|
|
58
|
+
maxConsecutiveErrors = 3;
|
|
59
|
+
totalErrorThreshold = 10;
|
|
60
|
+
performanceMetrics = {};
|
|
61
|
+
networkEvents = [];
|
|
62
|
+
async executeWithRetry(action) {
|
|
63
|
+
const retryOptions = action.retryOptions ?? browser_actions_1.BROWSER_DEFAULTS.retryOptions;
|
|
64
|
+
let lastError = null;
|
|
65
|
+
for (let attempt = 0; attempt <= (retryOptions.maxRetries ?? 3); attempt++) {
|
|
66
|
+
try {
|
|
67
|
+
const result = await this.executeAction(action);
|
|
68
|
+
// Reset consecutive errors on success
|
|
69
|
+
this.consecutiveErrors = 0;
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
// Track errors
|
|
74
|
+
this.consecutiveErrors++;
|
|
75
|
+
this.totalErrors++;
|
|
76
|
+
// Log error analysis
|
|
77
|
+
this.logErrorForAnalysis(error);
|
|
78
|
+
// Check error thresholds
|
|
79
|
+
if (this.consecutiveErrors >= this.maxConsecutiveErrors) {
|
|
80
|
+
const msg = `Max consecutive errors reached (${this.maxConsecutiveErrors}).`;
|
|
81
|
+
this.logs.push({
|
|
82
|
+
type: 'error',
|
|
83
|
+
message: msg,
|
|
84
|
+
timestamp: Date.now(),
|
|
85
|
+
source: 'tool',
|
|
86
|
+
});
|
|
87
|
+
await this.shutdown();
|
|
88
|
+
return {
|
|
89
|
+
success: false,
|
|
90
|
+
error: msg,
|
|
91
|
+
logs: this.logs,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (this.totalErrors >= this.totalErrorThreshold) {
|
|
95
|
+
const msg = `Total error threshold reached (${this.totalErrorThreshold}).`;
|
|
96
|
+
this.logs.push({
|
|
97
|
+
type: 'error',
|
|
98
|
+
message: msg,
|
|
99
|
+
timestamp: Date.now(),
|
|
100
|
+
source: 'tool',
|
|
101
|
+
});
|
|
102
|
+
await this.shutdown();
|
|
103
|
+
return {
|
|
104
|
+
success: false,
|
|
105
|
+
error: msg,
|
|
106
|
+
logs: this.logs,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
lastError = error;
|
|
110
|
+
const shouldRetry = retryOptions.retryOnErrors?.includes(error.name);
|
|
111
|
+
if (!shouldRetry || attempt === retryOptions.maxRetries) {
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
await new Promise((resolve) => setTimeout(resolve, retryOptions.retryDelay ?? 1000));
|
|
115
|
+
this.logs.push({
|
|
116
|
+
type: 'info',
|
|
117
|
+
message: `Retrying action (attempt ${attempt + 1}/${retryOptions.maxRetries})`,
|
|
118
|
+
timestamp: Date.now(),
|
|
119
|
+
category: 'retry',
|
|
120
|
+
source: 'tool',
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
throw lastError;
|
|
125
|
+
}
|
|
126
|
+
async executeAction(action) {
|
|
127
|
+
try {
|
|
128
|
+
switch (action.type) {
|
|
129
|
+
case 'start':
|
|
130
|
+
await this.startBrowser(action);
|
|
131
|
+
break;
|
|
132
|
+
case 'navigate':
|
|
133
|
+
await this.navigate(action);
|
|
134
|
+
break;
|
|
135
|
+
case 'click':
|
|
136
|
+
console.log('Clicking has not been implemented yet');
|
|
137
|
+
break;
|
|
138
|
+
case 'type':
|
|
139
|
+
await this.typeText(action);
|
|
140
|
+
break;
|
|
141
|
+
case 'scroll':
|
|
142
|
+
await this.scroll(action);
|
|
143
|
+
break;
|
|
144
|
+
case 'screenshot':
|
|
145
|
+
return await this.takeScreenshot(action);
|
|
146
|
+
case 'stop':
|
|
147
|
+
await this.shutdown();
|
|
148
|
+
return {
|
|
149
|
+
success: true,
|
|
150
|
+
logs: this.logs,
|
|
151
|
+
metrics: await this.collectMetrics(),
|
|
152
|
+
};
|
|
153
|
+
default:
|
|
154
|
+
throw new Error(`Unknown action type: ${action.type}`);
|
|
155
|
+
}
|
|
156
|
+
const metrics = await this.collectMetrics();
|
|
157
|
+
const response = {
|
|
158
|
+
success: true,
|
|
159
|
+
logs: this.logs,
|
|
160
|
+
metrics,
|
|
161
|
+
};
|
|
162
|
+
return response;
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
await this.shutdown();
|
|
166
|
+
return {
|
|
167
|
+
success: false,
|
|
168
|
+
error: err?.message ?? String(err),
|
|
169
|
+
logs: this.logs,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
logErrorForAnalysis(error) {
|
|
174
|
+
// Add helpful hints based on error patterns
|
|
175
|
+
const errorPatterns = {
|
|
176
|
+
'not defined': 'Check for missing script dependencies or undefined variables',
|
|
177
|
+
'Failed to fetch': 'Verify endpoint URLs and network connectivity',
|
|
178
|
+
'404': 'Resource not found - verify URLs and paths',
|
|
179
|
+
SSL: 'SSL certificate error - check HTTPS configuration',
|
|
180
|
+
ERR_NAME_NOT_RESOLVED: 'DNS resolution failed - check domain name',
|
|
181
|
+
ERR_CONNECTION_TIMED_OUT: 'Connection timeout - check network or firewall',
|
|
182
|
+
ERR_NETWORK_CHANGED: 'Network changed during request - retry operation',
|
|
183
|
+
ERR_INTERNET_DISCONNECTED: 'No internet connection',
|
|
184
|
+
'Navigation timeout': 'Page took too long to load - check performance or timeouts',
|
|
185
|
+
WebSocket: 'WebSocket connection issue - check server status',
|
|
186
|
+
ERR_TUNNEL_CONNECTION_FAILED: 'Proxy or VPN connection issue',
|
|
187
|
+
ERR_CERT_: 'SSL/TLS certificate validation error',
|
|
188
|
+
ERR_BLOCKED_BY_CLIENT: 'Request blocked by browser extension or policy',
|
|
189
|
+
ERR_TOO_MANY_REDIRECTS: 'Redirect loop detected',
|
|
190
|
+
'Frame detached': 'Target frame or element no longer exists',
|
|
191
|
+
'Node is detached': 'Element was removed from DOM',
|
|
192
|
+
ERR_ABORTED: 'Request was aborted - possible navigation or reload',
|
|
193
|
+
ERR_CONTENT_LENGTH_MISMATCH: 'Incomplete response - check server stability',
|
|
194
|
+
ERR_RESPONSE_HEADERS_TRUNCATED: 'Response headers too large or malformed',
|
|
195
|
+
};
|
|
196
|
+
for (const [pattern, hint] of Object.entries(errorPatterns)) {
|
|
197
|
+
if (error.message.includes(pattern)) {
|
|
198
|
+
this.logs.push({
|
|
199
|
+
type: 'info',
|
|
200
|
+
message: `Hint: ${hint}`,
|
|
201
|
+
timestamp: Date.now(),
|
|
202
|
+
category: 'hint',
|
|
203
|
+
source: 'tool',
|
|
204
|
+
});
|
|
205
|
+
break; // Stop after first matching pattern
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
this.logs.push({
|
|
209
|
+
type: 'error',
|
|
210
|
+
message: `Action error: ${error.message}`,
|
|
211
|
+
timestamp: Date.now(),
|
|
212
|
+
stack: error.stack,
|
|
213
|
+
source: 'tool',
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
async startBrowser(action) {
|
|
217
|
+
if (this.browser) {
|
|
218
|
+
await this.shutdown();
|
|
219
|
+
}
|
|
220
|
+
console.log('Starting browser...');
|
|
221
|
+
// Set start time for session tracking
|
|
222
|
+
this.startTime = Date.now();
|
|
223
|
+
// Update session configuration
|
|
224
|
+
this.maxConsecutiveErrors =
|
|
225
|
+
action.maxConsecutiveErrors ?? browser_actions_1.BROWSER_DEFAULTS.maxConsecutiveErrors;
|
|
226
|
+
this.totalErrorThreshold =
|
|
227
|
+
action.totalErrorThreshold ?? browser_actions_1.BROWSER_DEFAULTS.totalErrorThreshold;
|
|
228
|
+
// Reset error counters
|
|
229
|
+
this.consecutiveErrors = 0;
|
|
230
|
+
this.totalErrors = 0;
|
|
231
|
+
// Set up user data directory for profile persistence
|
|
232
|
+
const userDataDir = (0, project_files_1.getDebugDir)('browser-profile');
|
|
233
|
+
(0, file_1.ensureDirectoryExists)(userDataDir);
|
|
234
|
+
try {
|
|
235
|
+
this.browser = await puppeteer_1.default.launch({
|
|
236
|
+
defaultViewport: { width: 960, height: 720 },
|
|
237
|
+
headless: action.headless ?? browser_actions_1.BROWSER_DEFAULTS.headless,
|
|
238
|
+
userDataDir,
|
|
239
|
+
args: ['--no-sandbox', '--restore-last-session=false']
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
// If launch fails, try installing/updating Chrome and retry
|
|
244
|
+
console.log('Browser launch failed, attempting to install/update Chrome...');
|
|
245
|
+
try {
|
|
246
|
+
(0, child_process_1.execSync)('npx puppeteer browsers install chrome', { stdio: 'inherit' });
|
|
247
|
+
this.browser = await puppeteer_1.default.launch({
|
|
248
|
+
defaultViewport: { width: 960, height: 720 },
|
|
249
|
+
headless: action.headless ?? browser_actions_1.BROWSER_DEFAULTS.headless,
|
|
250
|
+
userDataDir,
|
|
251
|
+
args: ['--no-sandbox', '--restore-last-session=false']
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
catch (retryError) {
|
|
255
|
+
throw new Error(`Failed to launch browser after install attempt: ${retryError?.message || String(retryError)}`);
|
|
256
|
+
}
|
|
257
|
+
console.log('Installing Chrome for Puppeteer...');
|
|
258
|
+
(0, child_process_1.execSync)('npx puppeteer browsers install chrome', { stdio: 'inherit' });
|
|
259
|
+
// Try launching again
|
|
260
|
+
this.browser = await puppeteer_1.default.launch({
|
|
261
|
+
defaultViewport: { width: 1080, height: 720 },
|
|
262
|
+
headless: action.headless ?? browser_actions_1.BROWSER_DEFAULTS.headless,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
console.log('Browser started');
|
|
266
|
+
const pages = await this.browser.pages();
|
|
267
|
+
this.page = pages.length > 0 ? pages[0] : await this.browser.newPage();
|
|
268
|
+
this.attachPageListeners();
|
|
269
|
+
await this.page.goto(action.url, {
|
|
270
|
+
waitUntil: 'networkidle0',
|
|
271
|
+
timeout: action.timeout ?? 15000,
|
|
272
|
+
});
|
|
273
|
+
return {
|
|
274
|
+
success: true,
|
|
275
|
+
logs: [
|
|
276
|
+
{
|
|
277
|
+
type: 'info',
|
|
278
|
+
message: `Opened ${action.url} in browser`,
|
|
279
|
+
timestamp: Date.now(),
|
|
280
|
+
source: 'tool',
|
|
281
|
+
},
|
|
282
|
+
],
|
|
283
|
+
networkEvents: [],
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
async navigate(action) {
|
|
287
|
+
if (!this.browser) {
|
|
288
|
+
await this.startBrowser({
|
|
289
|
+
...action,
|
|
290
|
+
type: 'start',
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
if (!this.page)
|
|
294
|
+
throw new Error('No browser page found; call start first.');
|
|
295
|
+
await this.page.goto(action.url, {
|
|
296
|
+
waitUntil: action.waitUntil ?? browser_actions_1.BROWSER_DEFAULTS.waitUntil,
|
|
297
|
+
timeout: action.timeout ?? browser_actions_1.BROWSER_DEFAULTS.timeout,
|
|
298
|
+
});
|
|
299
|
+
const screenshotResp = await this.takeScreenshot({
|
|
300
|
+
...action,
|
|
301
|
+
type: 'screenshot',
|
|
302
|
+
});
|
|
303
|
+
return {
|
|
304
|
+
success: true,
|
|
305
|
+
logs: [
|
|
306
|
+
{
|
|
307
|
+
type: 'info',
|
|
308
|
+
message: `Navigated to ${action.url}`,
|
|
309
|
+
timestamp: Date.now(),
|
|
310
|
+
source: 'tool',
|
|
311
|
+
},
|
|
312
|
+
...screenshotResp.logs,
|
|
313
|
+
],
|
|
314
|
+
networkEvents: [],
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
async typeText(action) {
|
|
318
|
+
if (!this.page)
|
|
319
|
+
throw new Error('No browser page found; call start first.');
|
|
320
|
+
await this.page.type(action.selector, action.text, {
|
|
321
|
+
delay: action.delay ?? browser_actions_1.BROWSER_DEFAULTS.delay,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
async scroll(action) {
|
|
325
|
+
if (!this.page)
|
|
326
|
+
throw new Error('No browser page found; call start first.');
|
|
327
|
+
// Get viewport height
|
|
328
|
+
const viewport = this.page.viewport();
|
|
329
|
+
if (!viewport)
|
|
330
|
+
throw new Error('No viewport found');
|
|
331
|
+
// Default to scrolling down if no direction specified
|
|
332
|
+
const direction = action.direction ?? 'down';
|
|
333
|
+
const scrollAmount = direction === 'up' ? -viewport.height : viewport.height;
|
|
334
|
+
await this.page.evaluate((amount) => {
|
|
335
|
+
window.scrollBy(0, amount);
|
|
336
|
+
}, scrollAmount);
|
|
337
|
+
const screenshotResp = await this.takeScreenshot({
|
|
338
|
+
...action,
|
|
339
|
+
type: 'screenshot',
|
|
340
|
+
});
|
|
341
|
+
return {
|
|
342
|
+
success: true,
|
|
343
|
+
logs: [
|
|
344
|
+
{
|
|
345
|
+
type: 'info',
|
|
346
|
+
message: `Scrolled ${direction}`,
|
|
347
|
+
timestamp: Date.now(),
|
|
348
|
+
source: 'tool',
|
|
349
|
+
},
|
|
350
|
+
...screenshotResp.logs,
|
|
351
|
+
],
|
|
352
|
+
networkEvents: [],
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
async takeScreenshot(action) {
|
|
356
|
+
if (!this.page)
|
|
357
|
+
throw new Error('No browser page found; call start first.');
|
|
358
|
+
// Take a simple screenshot with fixed settings
|
|
359
|
+
const screenshot = await this.page.screenshot({
|
|
360
|
+
fullPage: action.fullPage ?? browser_actions_1.BROWSER_DEFAULTS.fullPage,
|
|
361
|
+
type: 'jpeg',
|
|
362
|
+
quality: action.screenshotCompressionQuality ??
|
|
363
|
+
browser_actions_1.BROWSER_DEFAULTS.screenshotCompressionQuality,
|
|
364
|
+
encoding: 'base64',
|
|
365
|
+
});
|
|
366
|
+
// If debug mode is enabled, save the screenshot
|
|
367
|
+
if (action.debug) {
|
|
368
|
+
this.logs.push({
|
|
369
|
+
type: 'debug',
|
|
370
|
+
message: `Saving screenshot to disk...`,
|
|
371
|
+
timestamp: Date.now(),
|
|
372
|
+
category: 'debug',
|
|
373
|
+
source: 'tool',
|
|
374
|
+
});
|
|
375
|
+
try {
|
|
376
|
+
const screenshotsDir = (0, project_files_1.getCurrentChatDir)();
|
|
377
|
+
(0, file_1.ensureDirectoryExists)(screenshotsDir);
|
|
378
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
379
|
+
const filename = `screenshot-${timestamp}.jpg`;
|
|
380
|
+
const filepath = path.join(screenshotsDir, filename);
|
|
381
|
+
fs.writeFileSync(filepath, Buffer.from(screenshot, 'base64'));
|
|
382
|
+
this.logs.push({
|
|
383
|
+
type: 'debug',
|
|
384
|
+
message: `Saved screenshot to ${filepath}`,
|
|
385
|
+
timestamp: Date.now(),
|
|
386
|
+
category: 'debug',
|
|
387
|
+
source: 'tool',
|
|
388
|
+
});
|
|
389
|
+
// Save metadata
|
|
390
|
+
const metadataPath = path.join(screenshotsDir, `${timestamp}-metadata.json`);
|
|
391
|
+
const metadata = {
|
|
392
|
+
timestamp,
|
|
393
|
+
format: 'jpeg',
|
|
394
|
+
quality: 25,
|
|
395
|
+
fullPage: action.fullPage ?? browser_actions_1.BROWSER_DEFAULTS.fullPage,
|
|
396
|
+
metrics: await this.collectMetrics(),
|
|
397
|
+
};
|
|
398
|
+
fs.writeFileSync(metadataPath, JSON.stringify(metadata, null, 2));
|
|
399
|
+
}
|
|
400
|
+
catch (error) {
|
|
401
|
+
this.logs.push({
|
|
402
|
+
type: 'error',
|
|
403
|
+
message: `Failed to save screenshot: ${error.message}`,
|
|
404
|
+
timestamp: Date.now(),
|
|
405
|
+
category: 'debug',
|
|
406
|
+
source: 'tool',
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
const metrics = await this.collectMetrics();
|
|
411
|
+
return {
|
|
412
|
+
success: true,
|
|
413
|
+
logs: this.logs,
|
|
414
|
+
screenshot,
|
|
415
|
+
metrics,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
attachPageListeners() {
|
|
419
|
+
if (!this.page)
|
|
420
|
+
return;
|
|
421
|
+
// Console messages
|
|
422
|
+
this.page.on('console', (msg) => {
|
|
423
|
+
const type = msg.type() === 'error' ? 'error' : msg.type();
|
|
424
|
+
this.logs.push({
|
|
425
|
+
type,
|
|
426
|
+
message: msg.text(),
|
|
427
|
+
timestamp: Date.now(),
|
|
428
|
+
source: 'browser',
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
// Page errors
|
|
432
|
+
this.page.on('pageerror', (err) => {
|
|
433
|
+
this.logs.push({
|
|
434
|
+
type: 'error',
|
|
435
|
+
message: err.message,
|
|
436
|
+
timestamp: Date.now(),
|
|
437
|
+
stack: err.stack,
|
|
438
|
+
source: 'browser',
|
|
439
|
+
});
|
|
440
|
+
this.jsErrorCount++;
|
|
441
|
+
});
|
|
442
|
+
// Network requests
|
|
443
|
+
this.page.on('request', (request) => {
|
|
444
|
+
const method = request.method();
|
|
445
|
+
if (method) {
|
|
446
|
+
this.networkEvents.push({
|
|
447
|
+
url: request.url(),
|
|
448
|
+
method,
|
|
449
|
+
timestamp: Date.now(),
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
// Network responses
|
|
454
|
+
this.page.on('response', async (response) => {
|
|
455
|
+
const req = response.request();
|
|
456
|
+
const index = this.networkEvents.findIndex((evt) => evt.url === req.url() && evt.method === req.method());
|
|
457
|
+
const status = response.status();
|
|
458
|
+
const errorText = status >= 400 ? await response.text().catch(() => '') : undefined;
|
|
459
|
+
if (index !== -1) {
|
|
460
|
+
this.networkEvents[index].status = status;
|
|
461
|
+
this.networkEvents[index].errorText = errorText;
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
const method = req.method();
|
|
465
|
+
if (method) {
|
|
466
|
+
this.networkEvents.push({
|
|
467
|
+
url: req.url(),
|
|
468
|
+
method,
|
|
469
|
+
status,
|
|
470
|
+
errorText,
|
|
471
|
+
timestamp: Date.now(),
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
// Log network errors
|
|
476
|
+
if (status >= 400) {
|
|
477
|
+
this.logs.push({
|
|
478
|
+
type: 'error',
|
|
479
|
+
message: `Network error ${status} for ${req.url()}`,
|
|
480
|
+
timestamp: Date.now(),
|
|
481
|
+
source: 'tool',
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
async collectPerformanceMetrics() {
|
|
487
|
+
if (!this.page)
|
|
488
|
+
return;
|
|
489
|
+
// Collect Web Vitals and other performance metrics
|
|
490
|
+
const metrics = await this.page.evaluate(() => {
|
|
491
|
+
const lcpEntry = performance.getEntriesByType('largest-contentful-paint')[0];
|
|
492
|
+
const navEntry = performance.getEntriesByType('navigation')[0];
|
|
493
|
+
const fcpEntry = performance
|
|
494
|
+
.getEntriesByType('paint')
|
|
495
|
+
.find((entry) => entry.name === 'first-contentful-paint');
|
|
496
|
+
return {
|
|
497
|
+
ttfb: navEntry?.responseStart - navEntry?.requestStart,
|
|
498
|
+
lcp: lcpEntry?.startTime,
|
|
499
|
+
fcp: fcpEntry?.startTime,
|
|
500
|
+
domContentLoaded: navEntry?.domContentLoadedEventEnd - navEntry?.startTime,
|
|
501
|
+
};
|
|
502
|
+
});
|
|
503
|
+
this.performanceMetrics = metrics;
|
|
504
|
+
}
|
|
505
|
+
async collectMetrics() {
|
|
506
|
+
if (!this.page)
|
|
507
|
+
return undefined;
|
|
508
|
+
const perfEntries = JSON.parse(await this.page.evaluate(() => JSON.stringify(performance.getEntriesByType('navigation'))));
|
|
509
|
+
let loadTime = 0;
|
|
510
|
+
if (perfEntries && perfEntries.length > 0) {
|
|
511
|
+
const navTiming = perfEntries[0];
|
|
512
|
+
loadTime = navTiming.loadEventEnd - navTiming.startTime;
|
|
513
|
+
}
|
|
514
|
+
const memoryUsed = await this.page
|
|
515
|
+
.metrics()
|
|
516
|
+
.then((m) => m.JSHeapUsedSize || 0);
|
|
517
|
+
await this.collectPerformanceMetrics();
|
|
518
|
+
return {
|
|
519
|
+
loadTime,
|
|
520
|
+
memoryUsage: memoryUsed,
|
|
521
|
+
jsErrors: this.jsErrorCount,
|
|
522
|
+
networkErrors: this.networkEvents.filter((e) => e.status && e.status >= 400).length,
|
|
523
|
+
ttfb: this.performanceMetrics.ttfb,
|
|
524
|
+
lcp: this.performanceMetrics.lcp,
|
|
525
|
+
fcp: this.performanceMetrics.fcp,
|
|
526
|
+
domContentLoaded: this.performanceMetrics.domContentLoaded,
|
|
527
|
+
sessionDuration: Date.now() - this.startTime,
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
filterLogs(logs, filter) {
|
|
531
|
+
if (!filter)
|
|
532
|
+
return logs;
|
|
533
|
+
return logs.filter((log) => {
|
|
534
|
+
if (filter.types && !filter.types.includes(log.type))
|
|
535
|
+
return false;
|
|
536
|
+
if (filter.minLevel && log.level && log.level < filter.minLevel)
|
|
537
|
+
return false;
|
|
538
|
+
if (filter.categories &&
|
|
539
|
+
log.category &&
|
|
540
|
+
!filter.categories.includes(log.category))
|
|
541
|
+
return false;
|
|
542
|
+
return true;
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
async execute(action) {
|
|
546
|
+
try {
|
|
547
|
+
const response = await this.executeWithRetry(action);
|
|
548
|
+
response.logs = this.filterLogs(response.logs, action.logFilter ?? undefined);
|
|
549
|
+
return response;
|
|
550
|
+
}
|
|
551
|
+
catch (error) {
|
|
552
|
+
if (error.name === 'TargetClosedError') {
|
|
553
|
+
this.logs.push({
|
|
554
|
+
type: 'error',
|
|
555
|
+
message: 'Browser crashed or was closed unexpectedly. Attempting recovery...',
|
|
556
|
+
timestamp: Date.now(),
|
|
557
|
+
category: 'browser',
|
|
558
|
+
source: 'tool',
|
|
559
|
+
});
|
|
560
|
+
// Try to recover by restarting browser
|
|
561
|
+
await this.shutdown();
|
|
562
|
+
if (action.type !== 'stop') {
|
|
563
|
+
await this.startBrowser({
|
|
564
|
+
type: 'start',
|
|
565
|
+
url: 'about:blank',
|
|
566
|
+
headless: true,
|
|
567
|
+
timeout: 15000,
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
throw error;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
async shutdown() {
|
|
575
|
+
const browser = this.browser;
|
|
576
|
+
if (browser) {
|
|
577
|
+
// Clear references first to prevent double shutdown
|
|
578
|
+
this.browser = null;
|
|
579
|
+
this.page = null;
|
|
580
|
+
try {
|
|
581
|
+
await browser.close();
|
|
582
|
+
}
|
|
583
|
+
catch (err) {
|
|
584
|
+
console.error('Error closing browser:', err);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
exports.BrowserRunner = BrowserRunner;
|
|
590
|
+
const handleBrowserInstruction = async (action) => {
|
|
591
|
+
// Create new browser if none exists
|
|
592
|
+
if (!activeBrowserRunner) {
|
|
593
|
+
activeBrowserRunner = new BrowserRunner();
|
|
594
|
+
}
|
|
595
|
+
const response = await activeBrowserRunner.execute(action);
|
|
596
|
+
return response;
|
|
597
|
+
};
|
|
598
|
+
exports.handleBrowserInstruction = handleBrowserInstruction;
|
|
599
|
+
//# sourceMappingURL=browser-runner.js.map
|