-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
66 lines (55 loc) · 2.21 KB
/
Copy pathexample.js
File metadata and controls
66 lines (55 loc) · 2.21 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
/**
* Koda - Intelligent Browser Automation Library
* This project uses Koda by Trent Pierce
* https://github.com/TrentPierce/Koda
* Licensed under the Koda Non-Commercial License
*
* Copyright (c) 2026 Trent Pierce. All rights reserved.
* See LICENSE file for full terms.
*/
const { createAgent } = require('./src/index.js');
async function main() {
console.log('🚀 Starting Koda...\n');
// Check for API key
if (!process.env.GEMINI_API_KEY && !process.env.OPENAI_API_KEY) {
console.error('❌ Error: No API key found!');
console.log('\nPlease set one of these environment variables:');
console.log(' - GEMINI_API_KEY (for Google Gemini)');
console.log(' - OPENAI_API_KEY (for OpenAI)');
console.log(' - ANTHROPIC_API_KEY (for Claude)');
console.log('\nOr create a .env file with your API key.');
process.exit(1);
}
try {
// Create agent
const agent = await createAgent({
provider: process.env.GEMINI_API_KEY ? 'gemini' : 'openai',
apiKey: process.env.GEMINI_API_KEY || process.env.OPENAI_API_KEY,
headless: false // Set to true to hide browser window
});
console.log('✅ Agent initialized successfully!\n');
// Navigate to a website
console.log('🌐 Navigating to example.com...');
await agent.goto('https://example.com');
console.log('✅ Navigation complete!\n');
// Extract information
console.log('📄 Extracting page information...');
const pageInfo = await agent.page();
console.log('Page Title:', pageInfo.title);
console.log('Page URL:', pageInfo.url);
console.log('');
// Take a screenshot
console.log('📸 Taking screenshot...');
await agent.core.page.screenshot({ path: 'example-screenshot.png' });
console.log('✅ Screenshot saved to example-screenshot.png\n');
// Clean up
console.log('🧹 Cleaning up...');
await agent.close();
console.log('✅ Done! Koda closed successfully.');
} catch (error) {
console.error('❌ Error:', error.message);
console.error(error.stack);
process.exit(1);
}
}
main();