cbrowser 2.2.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-2026 CBrowser Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,404 @@
1
+ # CBrowser
2
+
3
+ **The browser that thinks.** AI-powered browser automation with constitutional safety, persona-driven testing, session persistence, and autonomous journeys.
4
+
5
+ [![npm version](https://badge.fury.io/js/cbrowser.svg)](https://badge.fury.io/js/cbrowser)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## What Makes This Different
9
+
10
+ | Traditional Automation | CBrowser |
11
+ |------------------------|----------|
12
+ | Brittle CSS selectors | AI vision: "click the blue login button" |
13
+ | Breaks when DOM changes | Self-healing locators adapt automatically |
14
+ | Stateless between runs | Remembers sites, patterns, sessions |
15
+ | Blind execution | Constitutional verification before actions |
16
+ | Manual debugging | Auto-diagnostic with visual diffs |
17
+ | No user context | Personas with goals, behaviors, limitations |
18
+ | Manual login flows | Credential vault with session persistence |
19
+ | Scripted tests only | Autonomous goal-driven journeys |
20
+
21
+ ## Quick Start
22
+
23
+ ### Installation
24
+
25
+ ```bash
26
+ # Using npm
27
+ npm install cbrowser
28
+
29
+ # Using bun (recommended)
30
+ bun add cbrowser
31
+
32
+ # Using yarn
33
+ yarn add cbrowser
34
+ ```
35
+
36
+ ### Install Playwright Browsers
37
+
38
+ ```bash
39
+ npx playwright install chromium
40
+ ```
41
+
42
+ ### Basic Usage
43
+
44
+ ```bash
45
+ # Navigate to a URL
46
+ npx cbrowser navigate "https://example.com"
47
+
48
+ # Click an element using natural language
49
+ npx cbrowser click "the blue submit button"
50
+
51
+ # Fill a form field
52
+ npx cbrowser fill "email input" "user@example.com"
53
+
54
+ # Take a screenshot
55
+ npx cbrowser screenshot "./my-screenshot.png"
56
+ ```
57
+
58
+ ## Features
59
+
60
+ ### AI-Powered Element Selection
61
+
62
+ Forget brittle CSS selectors. Describe elements naturally:
63
+
64
+ ```bash
65
+ # Natural language
66
+ cbrowser click "the main navigation menu"
67
+ cbrowser fill "password field" "secret123"
68
+
69
+ # Accessibility-based
70
+ cbrowser click "aria:button/Submit"
71
+
72
+ # Visual description
73
+ cbrowser click "visual:red button in header"
74
+
75
+ # Semantic type
76
+ cbrowser fill "semantic:email" "user@example.com"
77
+
78
+ # Fallback to CSS when needed
79
+ cbrowser click "css:#login-btn"
80
+ ```
81
+
82
+ ### Session Persistence
83
+
84
+ Save and restore complete browser sessions:
85
+
86
+ ```bash
87
+ # Save current session (cookies, localStorage, sessionStorage)
88
+ cbrowser session save "github-logged-in" --url "https://github.com"
89
+
90
+ # Load a saved session
91
+ cbrowser session load "github-logged-in"
92
+
93
+ # List all sessions
94
+ cbrowser session list
95
+
96
+ # Delete a session
97
+ cbrowser session delete "github-logged-in"
98
+ ```
99
+
100
+ ### Persona-Driven Testing
101
+
102
+ Test your site from different user perspectives:
103
+
104
+ ```bash
105
+ # Run an autonomous journey as a specific persona
106
+ cbrowser journey "first-timer" \
107
+ --start "https://mysite.com" \
108
+ --goal "Complete signup and reach dashboard"
109
+
110
+ # List available personas
111
+ cbrowser persona list
112
+ ```
113
+
114
+ **Built-in Personas:**
115
+
116
+ | Persona | Description |
117
+ |---------|-------------|
118
+ | `power-user` | Tech-savvy expert who expects efficiency |
119
+ | `first-timer` | New user exploring for the first time |
120
+ | `mobile-user` | Smartphone user with touch interface |
121
+ | `screen-reader-user` | Blind user with screen reader |
122
+ | `elderly-user` | Older adult with vision/motor limitations |
123
+ | `impatient-user` | Quick to abandon slow experiences |
124
+
125
+ ### Credential Management
126
+
127
+ Securely store and use credentials:
128
+
129
+ ```bash
130
+ # Add credentials for a site
131
+ cbrowser creds add "github" \
132
+ --username "me@email.com" \
133
+ --password "my-secret-password"
134
+
135
+ # List stored credentials
136
+ cbrowser creds list
137
+
138
+ # Authenticate using stored credentials
139
+ cbrowser auth "github"
140
+ ```
141
+
142
+ ### Data Extraction
143
+
144
+ Extract structured data from pages:
145
+
146
+ ```bash
147
+ # Extract all links
148
+ cbrowser extract "links" --format json
149
+
150
+ # Extract headings
151
+ cbrowser extract "headings"
152
+
153
+ # Extract form data
154
+ cbrowser extract "forms"
155
+
156
+ # Extract product cards (AI-powered)
157
+ cbrowser extract "all product cards" --format json
158
+ ```
159
+
160
+ ### Storage Management
161
+
162
+ Keep your data directory clean:
163
+
164
+ ```bash
165
+ # Show storage usage
166
+ cbrowser storage
167
+
168
+ # Preview cleanup
169
+ cbrowser cleanup --dry-run
170
+
171
+ # Clean old files
172
+ cbrowser cleanup --older-than 7 --keep-screenshots 10
173
+ ```
174
+
175
+ ## Configuration
176
+
177
+ ### Data Directory
178
+
179
+ By default, CBrowser stores data in `~/.cbrowser/`. Override with:
180
+
181
+ ```bash
182
+ # Environment variable
183
+ export CBROWSER_DATA_DIR="/path/to/data"
184
+
185
+ # Or per-command
186
+ CBROWSER_DATA_DIR="./my-data" cbrowser navigate "https://example.com"
187
+ ```
188
+
189
+ ### Directory Structure
190
+
191
+ ```
192
+ ~/.cbrowser/
193
+ ├── sessions/ # Saved browser sessions
194
+ ├── screenshots/ # Captured screenshots
195
+ ├── personas/ # Custom persona definitions
196
+ ├── scenarios/ # Test scenarios
197
+ ├── helpers/ # Learned site patterns
198
+ ├── audit/ # Action audit logs
199
+ └── credentials.json # Encrypted credentials
200
+ ```
201
+
202
+ ## Constitutional Safety
203
+
204
+ CBrowser implements a safety framework with action zones:
205
+
206
+ | Zone | Actions | Behavior |
207
+ |------|---------|----------|
208
+ | **Green** | Navigate, read, screenshot, scroll | Auto-execute |
209
+ | **Yellow** | Click buttons, fill forms, select | Log and proceed |
210
+ | **Red** | Submit, delete, purchase, account changes | Requires `--force` |
211
+ | **Black** | Bypass auth, violate ToS, inject scripts | Never execute |
212
+
213
+ ### Safety Bypass
214
+
215
+ For automated testing, you can bypass yellow/red zone warnings:
216
+
217
+ ```bash
218
+ cbrowser click "Delete Account" --force
219
+ ```
220
+
221
+ ## API Usage
222
+
223
+ Use CBrowser programmatically:
224
+
225
+ ```typescript
226
+ import { CBrowser } from 'cbrowser';
227
+
228
+ const browser = new CBrowser({
229
+ dataDir: './my-data',
230
+ headless: true,
231
+ });
232
+
233
+ await browser.navigate('https://example.com');
234
+ await browser.click('Sign In');
235
+ await browser.fill('email', 'user@example.com');
236
+ await browser.fill('password', 'secret123');
237
+ await browser.click('Submit');
238
+
239
+ const screenshot = await browser.screenshot();
240
+ await browser.close();
241
+ ```
242
+
243
+ ### With Sessions
244
+
245
+ ```typescript
246
+ import { CBrowser } from 'cbrowser';
247
+
248
+ const browser = new CBrowser();
249
+
250
+ // Load existing session or create new
251
+ const hasSession = await browser.loadSession('my-app');
252
+ if (!hasSession) {
253
+ await browser.navigate('https://myapp.com/login');
254
+ await browser.fill('email', 'user@example.com');
255
+ await browser.fill('password', 'secret');
256
+ await browser.click('Login');
257
+ await browser.saveSession('my-app');
258
+ }
259
+
260
+ // Now authenticated, continue with tests
261
+ await browser.navigate('https://myapp.com/dashboard');
262
+ ```
263
+
264
+ ### Running Journeys
265
+
266
+ ```typescript
267
+ import { CBrowser } from 'cbrowser';
268
+
269
+ const browser = new CBrowser();
270
+ const result = await browser.journey({
271
+ persona: 'first-timer',
272
+ startUrl: 'https://example.com',
273
+ goal: 'Find pricing information',
274
+ maxSteps: 20,
275
+ });
276
+
277
+ console.log('Journey completed:', result.success);
278
+ console.log('Friction points:', result.frictionPoints);
279
+ console.log('Console logs:', result.consoleLogs);
280
+ ```
281
+
282
+ ## Examples
283
+
284
+ ### E2E Test with Session Reuse
285
+
286
+ ```typescript
287
+ // tests/checkout.test.ts
288
+ import { CBrowser } from 'cbrowser';
289
+
290
+ describe('Checkout Flow', () => {
291
+ let browser: CBrowser;
292
+
293
+ beforeAll(async () => {
294
+ browser = new CBrowser();
295
+ await browser.loadSession('logged-in-user');
296
+ });
297
+
298
+ afterAll(async () => {
299
+ await browser.close();
300
+ });
301
+
302
+ it('should add item to cart', async () => {
303
+ await browser.navigate('https://shop.example.com/products');
304
+ await browser.click('Add to Cart');
305
+ await browser.click('View Cart');
306
+
307
+ const cartItems = await browser.extract('cart items');
308
+ expect(cartItems.length).toBeGreaterThan(0);
309
+ });
310
+ });
311
+ ```
312
+
313
+ ### Multi-Persona Comparison
314
+
315
+ ```typescript
316
+ import { CBrowser } from 'cbrowser';
317
+
318
+ const personas = ['power-user', 'first-timer', 'mobile-user'];
319
+ const results = [];
320
+
321
+ for (const persona of personas) {
322
+ const browser = new CBrowser();
323
+ const result = await browser.journey({
324
+ persona,
325
+ startUrl: 'https://example.com',
326
+ goal: 'Complete checkout',
327
+ });
328
+
329
+ results.push({
330
+ persona,
331
+ success: result.success,
332
+ timeMs: result.totalTime,
333
+ frictionPoints: result.frictionPoints,
334
+ });
335
+
336
+ await browser.close();
337
+ }
338
+
339
+ console.table(results);
340
+ ```
341
+
342
+ ## Troubleshooting
343
+
344
+ ### Browser Not Starting
345
+
346
+ ```bash
347
+ # Ensure Playwright browsers are installed
348
+ npx playwright install chromium
349
+
350
+ # Check for display issues (Linux servers)
351
+ export DISPLAY=:0
352
+ # Or run headless
353
+ cbrowser navigate "https://example.com" --headless
354
+ ```
355
+
356
+ ### Session Not Persisting
357
+
358
+ Sessions are domain-specific. Make sure you're loading the session on the same domain:
359
+
360
+ ```bash
361
+ # Save from example.com
362
+ cbrowser session save "my-session" --url "https://example.com"
363
+
364
+ # Load must also be on example.com
365
+ cbrowser session load "my-session"
366
+ # This navigates to the saved URL automatically
367
+ ```
368
+
369
+ ### Credential Security
370
+
371
+ Credentials are stored in `~/.cbrowser/credentials.json`. For production:
372
+
373
+ 1. Set restrictive permissions: `chmod 600 ~/.cbrowser/credentials.json`
374
+ 2. Consider using environment variables instead
375
+ 3. Never commit the data directory to git
376
+
377
+ ## Contributing
378
+
379
+ Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.
380
+
381
+ ```bash
382
+ # Clone the repo
383
+ git clone https://github.com/yourusername/cbrowser.git
384
+ cd cbrowser
385
+
386
+ # Install dependencies
387
+ bun install
388
+
389
+ # Run in development
390
+ bun run dev
391
+
392
+ # Run tests
393
+ bun test
394
+ ```
395
+
396
+ ## License
397
+
398
+ MIT - see [LICENSE](LICENSE) for details.
399
+
400
+ ## Acknowledgments
401
+
402
+ - Built on [Playwright](https://playwright.dev/) for reliable browser automation
403
+ - Inspired by constitutional AI principles for safe automation
404
+ - Persona framework based on UX research methodologies
@@ -0,0 +1,98 @@
1
+ /**
2
+ * CBrowser - Main Browser Class
3
+ *
4
+ * AI-powered browser automation with constitutional safety.
5
+ */
6
+ import { type CBrowserConfig } from "./config.js";
7
+ import type { NavigationResult, ClickResult, ExtractResult, JourneyResult, CleanupOptions, CleanupResult, JourneyOptions } from "./types.js";
8
+ export declare class CBrowser {
9
+ private config;
10
+ private paths;
11
+ private browser;
12
+ private context;
13
+ private page;
14
+ private currentPersona;
15
+ constructor(userConfig?: Partial<CBrowserConfig>);
16
+ /**
17
+ * Launch the browser.
18
+ */
19
+ launch(): Promise<void>;
20
+ /**
21
+ * Close the browser.
22
+ */
23
+ close(): Promise<void>;
24
+ /**
25
+ * Get the current page, launching if needed.
26
+ */
27
+ private getPage;
28
+ /**
29
+ * Navigate to a URL.
30
+ */
31
+ navigate(url: string): Promise<NavigationResult>;
32
+ /**
33
+ * Click an element using AI-powered selector.
34
+ */
35
+ click(selector: string, options?: {
36
+ force?: boolean;
37
+ }): Promise<ClickResult>;
38
+ /**
39
+ * Fill a form field.
40
+ */
41
+ fill(selector: string, value: string): Promise<ClickResult>;
42
+ /**
43
+ * Find an element using multiple strategies.
44
+ */
45
+ private findElement;
46
+ /**
47
+ * Extract data from the page.
48
+ */
49
+ extract(what: string): Promise<ExtractResult>;
50
+ /**
51
+ * Take a screenshot.
52
+ */
53
+ screenshot(path?: string): Promise<string>;
54
+ /**
55
+ * Save the current session.
56
+ */
57
+ saveSession(name: string): Promise<void>;
58
+ /**
59
+ * Load a saved session.
60
+ */
61
+ loadSession(name: string): Promise<boolean>;
62
+ /**
63
+ * List all saved sessions.
64
+ */
65
+ listSessions(): string[];
66
+ /**
67
+ * Delete a saved session.
68
+ */
69
+ deleteSession(name: string): boolean;
70
+ /**
71
+ * Run an autonomous journey.
72
+ */
73
+ journey(options: JourneyOptions): Promise<JourneyResult>;
74
+ /**
75
+ * Get human-like delay based on persona.
76
+ */
77
+ private getHumanDelay;
78
+ /**
79
+ * Classify an action into a safety zone.
80
+ */
81
+ private classifyAction;
82
+ /**
83
+ * Log an action to the audit trail.
84
+ */
85
+ private audit;
86
+ /**
87
+ * Clean up old files.
88
+ */
89
+ cleanup(options?: CleanupOptions): CleanupResult;
90
+ /**
91
+ * Get storage statistics.
92
+ */
93
+ getStorageStats(): Record<string, {
94
+ count: number;
95
+ size: number;
96
+ }>;
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