Skip to content

Commit 4c053a1

Browse files
authored
streamline screenshot process, add pnpm claude to start (#3)
1 parent 914a4e4 commit 4c053a1

File tree

4 files changed

+2584
-44
lines changed

4 files changed

+2584
-44
lines changed

browser-utils.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Page } from '@browserbasehq/stagehand';
12
import { existsSync, cpSync, mkdirSync } from 'fs';
23
import { platform } from 'os';
34
import { join } from 'path';
@@ -101,3 +102,44 @@ export function prepareChromeProfile() {
101102
}
102103
}
103104
}
105+
106+
// Use CDP to take screenshot directly
107+
export async function takeScreenshot(page: Page) {
108+
const currentPath = process.cwd();
109+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
110+
const screenshotPath = `${currentPath}/agent/browser_screenshots/screenshot-${timestamp}.png`;
111+
112+
const context = page.context();
113+
const client = await context.newCDPSession(page);
114+
const screenshotResult = await client.send('Page.captureScreenshot', {
115+
format: 'png',
116+
quality: 100,
117+
fromSurface: false
118+
});
119+
120+
// Save the base64 screenshot data to file with resizing if needed
121+
const fs = await import('fs');
122+
const sharp = (await import('sharp')).default;
123+
const buffer = Buffer.from(screenshotResult.data, 'base64');
124+
125+
// Check image dimensions
126+
const image = sharp(buffer);
127+
const metadata = await image.metadata();
128+
const { width, height } = metadata;
129+
130+
let finalBuffer: Buffer = buffer;
131+
132+
// Only resize if image exceeds 2000x2000
133+
if (width && height && (width > 2000 || height > 2000)) {
134+
finalBuffer = await sharp(buffer)
135+
.resize(2000, 2000, {
136+
fit: 'inside',
137+
withoutEnlargement: true
138+
})
139+
.png()
140+
.toBuffer();
141+
}
142+
143+
fs.writeFileSync(screenshotPath, finalBuffer);
144+
return screenshotPath;
145+
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"scripts": {
3+
"claude": "tsx agent-browse.ts"
4+
},
25
"dependencies": {
36
"@anthropic-ai/claude-agent-sdk": "^0.1.14",
47
"@browserbasehq/stagehand": "^2.5.2",
@@ -10,5 +13,6 @@
1013
"@types/node": "^24.7.2",
1114
"tsx": "^4.20.6",
1215
"typescript": "^5.9.3"
13-
}
16+
},
17+
"packageManager": "pnpm@10.12.1+sha512.f0dda8580f0ee9481c5c79a1d927b9164f2c478e90992ad268bbb2465a736984391d6333d2c327913578b2804af33474ca554ba29c04a8b13060a717675ae3ac"
1418
}

0 commit comments

Comments
 (0)