sentienceapi 0.90.9__py3-none-any.whl

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.

Potentially problematic release.


This version of sentienceapi might be problematic. Click here for more details.

Files changed (46) hide show
  1. sentience/__init__.py +153 -0
  2. sentience/actions.py +439 -0
  3. sentience/agent.py +687 -0
  4. sentience/agent_config.py +43 -0
  5. sentience/base_agent.py +101 -0
  6. sentience/browser.py +409 -0
  7. sentience/cli.py +130 -0
  8. sentience/cloud_tracing.py +292 -0
  9. sentience/conversational_agent.py +509 -0
  10. sentience/expect.py +92 -0
  11. sentience/extension/background.js +233 -0
  12. sentience/extension/content.js +298 -0
  13. sentience/extension/injected_api.js +1473 -0
  14. sentience/extension/manifest.json +36 -0
  15. sentience/extension/pkg/sentience_core.d.ts +51 -0
  16. sentience/extension/pkg/sentience_core.js +529 -0
  17. sentience/extension/pkg/sentience_core_bg.wasm +0 -0
  18. sentience/extension/pkg/sentience_core_bg.wasm.d.ts +10 -0
  19. sentience/extension/release.json +115 -0
  20. sentience/extension/test-content.js +4 -0
  21. sentience/formatting.py +59 -0
  22. sentience/generator.py +202 -0
  23. sentience/inspector.py +185 -0
  24. sentience/llm_provider.py +431 -0
  25. sentience/models.py +406 -0
  26. sentience/overlay.py +115 -0
  27. sentience/query.py +303 -0
  28. sentience/read.py +96 -0
  29. sentience/recorder.py +369 -0
  30. sentience/schemas/trace_v1.json +216 -0
  31. sentience/screenshot.py +54 -0
  32. sentience/snapshot.py +282 -0
  33. sentience/text_search.py +107 -0
  34. sentience/trace_indexing/__init__.py +27 -0
  35. sentience/trace_indexing/index_schema.py +111 -0
  36. sentience/trace_indexing/indexer.py +363 -0
  37. sentience/tracer_factory.py +211 -0
  38. sentience/tracing.py +285 -0
  39. sentience/utils.py +296 -0
  40. sentience/wait.py +73 -0
  41. sentienceapi-0.90.9.dist-info/METADATA +878 -0
  42. sentienceapi-0.90.9.dist-info/RECORD +46 -0
  43. sentienceapi-0.90.9.dist-info/WHEEL +5 -0
  44. sentienceapi-0.90.9.dist-info/entry_points.txt +2 -0
  45. sentienceapi-0.90.9.dist-info/licenses/LICENSE.md +43 -0
  46. sentienceapi-0.90.9.dist-info/top_level.txt +1 -0
@@ -0,0 +1,233 @@
1
+ // background.js - Service Worker with WASM (CSP-Immune!)
2
+ // This runs in an isolated environment, completely immune to page CSP policies
3
+
4
+ // ✅ STATIC IMPORTS at top level - Required for Service Workers!
5
+ // Dynamic import() is FORBIDDEN in ServiceWorkerGlobalScope
6
+ import init, { analyze_page, analyze_page_with_options, prune_for_api } from './pkg/sentience_core.js';
7
+
8
+ console.log('[Sentience Background] Initializing...');
9
+
10
+ // Global WASM initialization state
11
+ let wasmReady = false;
12
+ let wasmInitPromise = null;
13
+
14
+ /**
15
+ * Initialize WASM module - called once on service worker startup
16
+ * Uses static imports (not dynamic import()) which is required for Service Workers
17
+ */
18
+ async function initWASM() {
19
+ if (wasmReady) return;
20
+ if (wasmInitPromise) return wasmInitPromise;
21
+
22
+ wasmInitPromise = (async () => {
23
+ try {
24
+ console.log('[Sentience Background] Loading WASM module...');
25
+
26
+ // Define the js_click_element function that WASM expects
27
+ // In Service Workers, use 'globalThis' instead of 'window'
28
+ // In background context, we can't actually click, so we log a warning
29
+ globalThis.js_click_element = (_id) => {
30
+ console.warn('[Sentience Background] js_click_element called in background (ignored)');
31
+ };
32
+
33
+ // Initialize WASM - this calls the init() function from the static import
34
+ // The init() function handles fetching and instantiating the .wasm file
35
+ await init();
36
+
37
+ wasmReady = true;
38
+ console.log('[Sentience Background] ✓ WASM ready!');
39
+ console.log('[Sentience Background] Available functions: analyze_page, analyze_page_with_options, prune_for_api');
40
+ } catch (error) {
41
+ console.error('[Sentience Background] WASM initialization failed:', error);
42
+ throw error;
43
+ }
44
+ })();
45
+
46
+ return wasmInitPromise;
47
+ }
48
+
49
+ // Initialize WASM on service worker startup
50
+ initWASM().catch(err => {
51
+ console.error('[Sentience Background] Failed to initialize WASM:', err);
52
+ });
53
+
54
+ /**
55
+ * Message handler for all extension communication
56
+ * Includes global error handling to prevent extension crashes
57
+ */
58
+ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
59
+ // Global error handler to prevent extension crashes
60
+ try {
61
+ // Handle screenshot requests (existing functionality)
62
+ if (request.action === 'captureScreenshot') {
63
+ handleScreenshotCapture(sender.tab.id, request.options)
64
+ .then(screenshot => {
65
+ sendResponse({ success: true, screenshot });
66
+ })
67
+ .catch(error => {
68
+ console.error('[Sentience Background] Screenshot capture failed:', error);
69
+ sendResponse({
70
+ success: false,
71
+ error: error.message || 'Screenshot capture failed'
72
+ });
73
+ });
74
+ return true; // Async response
75
+ }
76
+
77
+ // Handle WASM processing requests (NEW!)
78
+ if (request.action === 'processSnapshot') {
79
+ handleSnapshotProcessing(request.rawData, request.options)
80
+ .then(result => {
81
+ sendResponse({ success: true, result });
82
+ })
83
+ .catch(error => {
84
+ console.error('[Sentience Background] Snapshot processing failed:', error);
85
+ sendResponse({
86
+ success: false,
87
+ error: error.message || 'Snapshot processing failed'
88
+ });
89
+ });
90
+ return true; // Async response
91
+ }
92
+
93
+ // Unknown action
94
+ console.warn('[Sentience Background] Unknown action:', request.action);
95
+ sendResponse({ success: false, error: 'Unknown action' });
96
+ return false;
97
+ } catch (error) {
98
+ // Catch any synchronous errors that might crash the extension
99
+ console.error('[Sentience Background] Fatal error in message handler:', error);
100
+ try {
101
+ sendResponse({
102
+ success: false,
103
+ error: `Fatal error: ${error.message || 'Unknown error'}`
104
+ });
105
+ } catch (e) {
106
+ // If sendResponse already called, ignore
107
+ }
108
+ return false;
109
+ }
110
+ });
111
+
112
+ /**
113
+ * Handle screenshot capture (existing functionality)
114
+ */
115
+ async function handleScreenshotCapture(_tabId, options = {}) {
116
+ try {
117
+ const {
118
+ format = 'png',
119
+ quality = 90
120
+ } = options;
121
+
122
+ const dataUrl = await chrome.tabs.captureVisibleTab(null, {
123
+ format: format,
124
+ quality: quality
125
+ });
126
+
127
+ console.log(`[Sentience Background] Screenshot captured: ${format}, size: ${dataUrl.length} bytes`);
128
+ return dataUrl;
129
+ } catch (error) {
130
+ console.error('[Sentience Background] Screenshot error:', error);
131
+ throw new Error(`Failed to capture screenshot: ${error.message}`);
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Handle snapshot processing with WASM (NEW!)
137
+ * This is where the magic happens - completely CSP-immune!
138
+ * Includes safeguards to prevent crashes and hangs.
139
+ *
140
+ * @param {Array} rawData - Raw element data from injected_api.js
141
+ * @param {Object} options - Snapshot options (limit, filter, etc.)
142
+ * @returns {Promise<Object>} Processed snapshot result
143
+ */
144
+ async function handleSnapshotProcessing(rawData, options = {}) {
145
+ const MAX_ELEMENTS = 10000; // Safety limit to prevent hangs
146
+ const startTime = performance.now();
147
+
148
+ try {
149
+ // Safety check: limit element count to prevent hangs
150
+ if (!Array.isArray(rawData)) {
151
+ throw new Error('rawData must be an array');
152
+ }
153
+
154
+ if (rawData.length > MAX_ELEMENTS) {
155
+ console.warn(`[Sentience Background] ⚠️ Large dataset: ${rawData.length} elements. Limiting to ${MAX_ELEMENTS} to prevent hangs.`);
156
+ rawData = rawData.slice(0, MAX_ELEMENTS);
157
+ }
158
+
159
+ // Ensure WASM is initialized
160
+ await initWASM();
161
+ if (!wasmReady) {
162
+ throw new Error('WASM module not initialized');
163
+ }
164
+
165
+ console.log(`[Sentience Background] Processing ${rawData.length} elements with options:`, options);
166
+
167
+ // Run WASM processing using the imported functions directly
168
+ // Wrap in try-catch with timeout protection
169
+ let analyzedElements;
170
+ try {
171
+ // Use a timeout wrapper to prevent infinite hangs
172
+ const wasmPromise = new Promise((resolve, reject) => {
173
+ try {
174
+ let result;
175
+ if (options.limit || options.filter) {
176
+ result = analyze_page_with_options(rawData, options);
177
+ } else {
178
+ result = analyze_page(rawData);
179
+ }
180
+ resolve(result);
181
+ } catch (e) {
182
+ reject(e);
183
+ }
184
+ });
185
+
186
+ // Add timeout protection (18 seconds - less than content.js timeout)
187
+ analyzedElements = await Promise.race([
188
+ wasmPromise,
189
+ new Promise((_, reject) =>
190
+ setTimeout(() => reject(new Error('WASM processing timeout (>18s)')), 18000)
191
+ )
192
+ ]);
193
+ } catch (e) {
194
+ const errorMsg = e.message || 'Unknown WASM error';
195
+ console.error(`[Sentience Background] WASM analyze_page failed: ${errorMsg}`, e);
196
+ throw new Error(`WASM analyze_page failed: ${errorMsg}`);
197
+ }
198
+
199
+ // Prune elements for API (prevents 413 errors on large sites)
200
+ let prunedRawData;
201
+ try {
202
+ prunedRawData = prune_for_api(rawData);
203
+ } catch (e) {
204
+ console.warn('[Sentience Background] prune_for_api failed, using original data:', e);
205
+ prunedRawData = rawData;
206
+ }
207
+
208
+ const duration = performance.now() - startTime;
209
+ console.log(`[Sentience Background] ✓ Processed: ${analyzedElements.length} analyzed, ${prunedRawData.length} pruned (${duration.toFixed(1)}ms)`);
210
+
211
+ return {
212
+ elements: analyzedElements,
213
+ raw_elements: prunedRawData
214
+ };
215
+ } catch (error) {
216
+ const duration = performance.now() - startTime;
217
+ console.error(`[Sentience Background] Processing error after ${duration.toFixed(1)}ms:`, error);
218
+ throw error;
219
+ }
220
+ }
221
+
222
+ console.log('[Sentience Background] Service worker ready');
223
+
224
+ // Global error handlers to prevent extension crashes
225
+ self.addEventListener('error', (event) => {
226
+ console.error('[Sentience Background] Global error caught:', event.error);
227
+ event.preventDefault(); // Prevent extension crash
228
+ });
229
+
230
+ self.addEventListener('unhandledrejection', (event) => {
231
+ console.error('[Sentience Background] Unhandled promise rejection:', event.reason);
232
+ event.preventDefault(); // Prevent extension crash
233
+ });
@@ -0,0 +1,298 @@
1
+ // content.js - ISOLATED WORLD (Bridge between Main World and Background)
2
+ console.log('[Sentience Bridge] Loaded.');
3
+
4
+ // Detect if we're in a child frame (for iframe support)
5
+ const isChildFrame = window !== window.top;
6
+ if (isChildFrame) {
7
+ console.log('[Sentience Bridge] Running in child frame:', window.location.href);
8
+ }
9
+
10
+ // 1. Pass Extension ID to Main World (So API knows where to find resources)
11
+ document.documentElement.dataset.sentienceExtensionId = chrome.runtime.id;
12
+
13
+ // 2. Message Router - Handles all communication between page and background
14
+ window.addEventListener('message', (event) => {
15
+ // Security check: only accept messages from same window
16
+ if (event.source !== window) return;
17
+
18
+ // Route different message types
19
+ switch (event.data.type) {
20
+ case 'SENTIENCE_SCREENSHOT_REQUEST':
21
+ handleScreenshotRequest(event.data);
22
+ break;
23
+
24
+ case 'SENTIENCE_SNAPSHOT_REQUEST':
25
+ handleSnapshotRequest(event.data);
26
+ break;
27
+
28
+ case 'SENTIENCE_SHOW_OVERLAY':
29
+ handleShowOverlay(event.data);
30
+ break;
31
+
32
+ case 'SENTIENCE_CLEAR_OVERLAY':
33
+ handleClearOverlay();
34
+ break;
35
+
36
+ default:
37
+ // Ignore unknown message types
38
+ break;
39
+ }
40
+ });
41
+
42
+ /**
43
+ * Handle screenshot requests (existing functionality)
44
+ */
45
+ function handleScreenshotRequest(data) {
46
+ chrome.runtime.sendMessage(
47
+ { action: 'captureScreenshot', options: data.options },
48
+ (response) => {
49
+ window.postMessage({
50
+ type: 'SENTIENCE_SCREENSHOT_RESULT',
51
+ requestId: data.requestId,
52
+ screenshot: response?.success ? response.screenshot : null,
53
+ error: response?.error
54
+ }, '*');
55
+ }
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Handle snapshot processing requests (NEW!)
61
+ * Sends raw DOM data to background worker for WASM processing
62
+ * Includes timeout protection to prevent extension crashes
63
+ */
64
+ function handleSnapshotRequest(data) {
65
+ const startTime = performance.now();
66
+ const TIMEOUT_MS = 20000; // 20 seconds (longer than injected_api timeout)
67
+ let responded = false;
68
+
69
+ // Timeout protection: if background doesn't respond, send error
70
+ const timeoutId = setTimeout(() => {
71
+ if (!responded) {
72
+ responded = true;
73
+ const duration = performance.now() - startTime;
74
+ console.error(`[Sentience Bridge] ⚠️ WASM processing timeout after ${duration.toFixed(1)}ms`);
75
+ window.postMessage({
76
+ type: 'SENTIENCE_SNAPSHOT_RESULT',
77
+ requestId: data.requestId,
78
+ error: 'WASM processing timeout - background script may be unresponsive',
79
+ duration: duration
80
+ }, '*');
81
+ }
82
+ }, TIMEOUT_MS);
83
+
84
+ try {
85
+ chrome.runtime.sendMessage(
86
+ {
87
+ action: 'processSnapshot',
88
+ rawData: data.rawData,
89
+ options: data.options
90
+ },
91
+ (response) => {
92
+ if (responded) return; // Already responded via timeout
93
+ responded = true;
94
+ clearTimeout(timeoutId);
95
+
96
+ const duration = performance.now() - startTime;
97
+
98
+ // Handle Chrome extension errors (e.g., background script crashed)
99
+ if (chrome.runtime.lastError) {
100
+ console.error('[Sentience Bridge] Chrome runtime error:', chrome.runtime.lastError.message);
101
+ window.postMessage({
102
+ type: 'SENTIENCE_SNAPSHOT_RESULT',
103
+ requestId: data.requestId,
104
+ error: `Chrome runtime error: ${chrome.runtime.lastError.message}`,
105
+ duration: duration
106
+ }, '*');
107
+ return;
108
+ }
109
+
110
+ if (response?.success) {
111
+ console.log(`[Sentience Bridge] ✓ WASM processing complete in ${duration.toFixed(1)}ms`);
112
+ window.postMessage({
113
+ type: 'SENTIENCE_SNAPSHOT_RESULT',
114
+ requestId: data.requestId,
115
+ elements: response.result.elements,
116
+ raw_elements: response.result.raw_elements,
117
+ duration: duration
118
+ }, '*');
119
+ } else {
120
+ console.error('[Sentience Bridge] WASM processing failed:', response?.error);
121
+ window.postMessage({
122
+ type: 'SENTIENCE_SNAPSHOT_RESULT',
123
+ requestId: data.requestId,
124
+ error: response?.error || 'Processing failed',
125
+ duration: duration
126
+ }, '*');
127
+ }
128
+ }
129
+ );
130
+ } catch (error) {
131
+ if (!responded) {
132
+ responded = true;
133
+ clearTimeout(timeoutId);
134
+ const duration = performance.now() - startTime;
135
+ console.error('[Sentience Bridge] Exception sending message:', error);
136
+ window.postMessage({
137
+ type: 'SENTIENCE_SNAPSHOT_RESULT',
138
+ requestId: data.requestId,
139
+ error: `Failed to send message: ${error.message}`,
140
+ duration: duration
141
+ }, '*');
142
+ }
143
+ }
144
+ }
145
+
146
+ // ============================================================================
147
+ // Visual Overlay - Shadow DOM Implementation
148
+ // ============================================================================
149
+
150
+ const OVERLAY_HOST_ID = 'sentience-overlay-host';
151
+ let overlayTimeout = null;
152
+
153
+ /**
154
+ * Show visual overlay highlighting elements using Shadow DOM
155
+ * @param {Object} data - Message data with elements and targetElementId
156
+ */
157
+ function handleShowOverlay(data) {
158
+ const { elements, targetElementId } = data;
159
+
160
+ if (!elements || !Array.isArray(elements)) {
161
+ console.warn('[Sentience Bridge] showOverlay: elements must be an array');
162
+ return;
163
+ }
164
+
165
+ removeOverlay();
166
+
167
+ // Create host with Shadow DOM for CSS isolation
168
+ const host = document.createElement('div');
169
+ host.id = OVERLAY_HOST_ID;
170
+ host.style.cssText = `
171
+ position: fixed !important;
172
+ top: 0 !important;
173
+ left: 0 !important;
174
+ width: 100vw !important;
175
+ height: 100vh !important;
176
+ pointer-events: none !important;
177
+ z-index: 2147483647 !important;
178
+ margin: 0 !important;
179
+ padding: 0 !important;
180
+ `;
181
+ document.body.appendChild(host);
182
+
183
+ // Attach shadow root (closed mode for security and CSS isolation)
184
+ const shadow = host.attachShadow({ mode: 'closed' });
185
+
186
+ // Calculate max importance for scaling
187
+ const maxImportance = Math.max(...elements.map(e => e.importance || 0), 1);
188
+
189
+ elements.forEach((element) => {
190
+ const bbox = element.bbox;
191
+ if (!bbox) return;
192
+
193
+ const isTarget = element.id === targetElementId;
194
+ const isPrimary = element.visual_cues?.is_primary || false;
195
+ const importance = element.importance || 0;
196
+
197
+ // Color: Red (target), Blue (primary), Green (regular)
198
+ let color;
199
+ if (isTarget) color = '#FF0000';
200
+ else if (isPrimary) color = '#0066FF';
201
+ else color = '#00FF00';
202
+
203
+ // Scale opacity and border width based on importance
204
+ const importanceRatio = maxImportance > 0 ? importance / maxImportance : 0.5;
205
+ const borderOpacity = isTarget ? 1.0 : (isPrimary ? 0.9 : Math.max(0.4, 0.5 + importanceRatio * 0.5));
206
+ const fillOpacity = borderOpacity * 0.2;
207
+ const borderWidth = isTarget ? 2 : (isPrimary ? 1.5 : Math.max(0.5, Math.round(importanceRatio * 2)));
208
+
209
+ // Convert fill opacity to hex for background-color
210
+ const hexOpacity = Math.round(fillOpacity * 255).toString(16).padStart(2, '0');
211
+
212
+ // Create box with semi-transparent fill
213
+ const box = document.createElement('div');
214
+ box.style.cssText = `
215
+ position: absolute;
216
+ left: ${bbox.x}px;
217
+ top: ${bbox.y}px;
218
+ width: ${bbox.width}px;
219
+ height: ${bbox.height}px;
220
+ border: ${borderWidth}px solid ${color};
221
+ background-color: ${color}${hexOpacity};
222
+ box-sizing: border-box;
223
+ opacity: ${borderOpacity};
224
+ pointer-events: none;
225
+ `;
226
+
227
+ // Add badge showing importance score
228
+ if (importance > 0 || isPrimary) {
229
+ const badge = document.createElement('span');
230
+ badge.textContent = isPrimary ? `⭐${importance}` : `${importance}`;
231
+ badge.style.cssText = `
232
+ position: absolute;
233
+ top: -18px;
234
+ left: 0;
235
+ background: ${color};
236
+ color: white;
237
+ font-size: 11px;
238
+ font-weight: bold;
239
+ padding: 2px 6px;
240
+ font-family: Arial, sans-serif;
241
+ border-radius: 3px;
242
+ opacity: 0.95;
243
+ white-space: nowrap;
244
+ pointer-events: none;
245
+ `;
246
+ box.appendChild(badge);
247
+ }
248
+
249
+ // Add target emoji for target element
250
+ if (isTarget) {
251
+ const targetIndicator = document.createElement('span');
252
+ targetIndicator.textContent = '🎯';
253
+ targetIndicator.style.cssText = `
254
+ position: absolute;
255
+ top: -18px;
256
+ right: 0;
257
+ font-size: 16px;
258
+ pointer-events: none;
259
+ `;
260
+ box.appendChild(targetIndicator);
261
+ }
262
+
263
+ shadow.appendChild(box);
264
+ });
265
+
266
+ console.log(`[Sentience Bridge] Overlay shown for ${elements.length} elements`);
267
+
268
+ // Auto-remove after 5 seconds
269
+ overlayTimeout = setTimeout(() => {
270
+ removeOverlay();
271
+ console.log('[Sentience Bridge] Overlay auto-cleared after 5 seconds');
272
+ }, 5000);
273
+ }
274
+
275
+ /**
276
+ * Clear overlay manually
277
+ */
278
+ function handleClearOverlay() {
279
+ removeOverlay();
280
+ console.log('[Sentience Bridge] Overlay cleared manually');
281
+ }
282
+
283
+ /**
284
+ * Remove overlay from DOM
285
+ */
286
+ function removeOverlay() {
287
+ const existing = document.getElementById(OVERLAY_HOST_ID);
288
+ if (existing) {
289
+ existing.remove();
290
+ }
291
+
292
+ if (overlayTimeout) {
293
+ clearTimeout(overlayTimeout);
294
+ overlayTimeout = null;
295
+ }
296
+ }
297
+
298
+ // console.log('[Sentience Bridge] Ready - Extension ID:', chrome.runtime.id);