Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sentienceapi",
"version": "0.91.0",
"version": "0.91.1",
"description": "TypeScript SDK for Sentience AI Agent Browser Automation",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
23 changes: 17 additions & 6 deletions src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,26 @@ export class SentienceAgent {

// Emit snapshot event
if (this.tracer) {
// Include ALL elements with full data for DOM tree display
// Use snap.elements (all elements) not filteredSnap.elements
const snapshotData: any = {
url: filteredSnap.url,
element_count: filteredSnap.elements.length,
timestamp: filteredSnap.timestamp,
elements: filteredSnap.elements.slice(0, 50).map(el => ({
url: snap.url,
element_count: snap.elements.length,
timestamp: snap.timestamp,
elements: snap.elements.map(el => ({
id: el.id,
bbox: el.bbox,
role: el.role,
text: el.text?.substring(0, 50),
text: el.text,
importance: el.importance,
bbox: el.bbox,
visual_cues: el.visual_cues,
in_viewport: el.in_viewport,
is_occluded: el.is_occluded,
z_index: el.z_index,
rerank_index: el.rerank_index,
heuristic_index: el.heuristic_index,
ml_probability: el.ml_probability,
ml_score: el.ml_score,
}))
};

Expand Down
32 changes: 28 additions & 4 deletions src/tracing/cloud-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,10 @@ export class CloudTraceSink extends TraceSink {
private generateIndex(): void {
try {
const { writeTraceIndex } = require('./indexer');
writeTraceIndex(this.tempFilePath);
// Use frontend format to ensure 'step' field is present (1-based)
// Frontend derives sequence from step.step - 1, so step must be valid
const indexPath = this.tempFilePath.replace('.jsonl', '.index.json');
writeTraceIndex(this.tempFilePath, indexPath, true);
} catch (error: any) {
// Non-fatal: log but don't crash
this.logger?.warn(`Failed to generate trace index: ${error.message}`);
Expand Down Expand Up @@ -609,9 +612,30 @@ export class CloudTraceSink extends TraceSink {
return;
}

// Read and compress index file
const indexData = await fsPromises.readFile(indexPath);
const compressedIndex = zlib.gzipSync(indexData);
// Read index file and update trace_file.path to cloud storage path
const indexContent = await fsPromises.readFile(indexPath, 'utf-8');
const indexJson = JSON.parse(indexContent);

// Extract cloud storage path from trace upload URL
// uploadUrl format: https://...digitaloceanspaces.com/traces/{run_id}.jsonl.gz
// Extract path: traces/{run_id}.jsonl.gz
try {
const parsedUrl = new URL(this.uploadUrl);
// Extract path after domain (e.g., /traces/run-123.jsonl.gz -> traces/run-123.jsonl.gz)
const cloudTracePath = parsedUrl.pathname.startsWith('/')
? parsedUrl.pathname.substring(1)
: parsedUrl.pathname;
// Update trace_file.path in index
if (indexJson.trace_file && typeof indexJson.trace_file === 'object') {
indexJson.trace_file.path = cloudTracePath;
}
} catch (error: any) {
this.logger?.warn(`Failed to extract cloud path from upload URL: ${error.message}`);
}

// Serialize updated index to JSON
const updatedIndexData = Buffer.from(JSON.stringify(indexJson, null, 2), 'utf-8');
const compressedIndex = zlib.gzipSync(updatedIndexData);
const indexSize = compressedIndex.length;
this.indexFileSizeBytes = indexSize; // Track index file size

Expand Down
27 changes: 19 additions & 8 deletions src/tracing/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,25 @@ function computeSnapshotDigest(snapshotData: any): string {
const elements = snapshotData.elements || [];

// Canonicalize elements
const canonicalElements = elements.map((elem: any) => ({
id: elem.id,
role: elem.role || '',
text_norm: normalizeText(elem.text),
bbox: roundBBox(elem.bbox || { x: 0, y: 0, width: 0, height: 0 }),
is_primary: elem.is_primary || false,
is_clickable: elem.is_clickable || false,
}));
const canonicalElements = elements.map((elem: any) => {
// Extract is_primary and is_clickable from visual_cues if present
const visualCues = elem.visual_cues || {};
const isPrimary = (typeof visualCues === 'object' && visualCues !== null)
? (visualCues.is_primary || false)
: (elem.is_primary || false);
const isClickable = (typeof visualCues === 'object' && visualCues !== null)
? (visualCues.is_clickable || false)
: (elem.is_clickable || false);

return {
id: elem.id,
role: elem.role || '',
text_norm: normalizeText(elem.text),
bbox: roundBBox(elem.bbox || { x: 0, y: 0, width: 0, height: 0 }),
is_primary: isPrimary,
is_clickable: isClickable,
};
});

// Sort by element id for determinism
canonicalElements.sort((a: { id?: number }, b: { id?: number }) => (a.id || 0) - (b.id || 0));
Expand Down
5 changes: 4 additions & 1 deletion src/tracing/jsonl-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ export class JsonlTraceSink extends TraceSink {
private generateIndex(): void {
try {
const { writeTraceIndex } = require('./indexer');
writeTraceIndex(this.path);
// Use frontend format to ensure 'step' field is present (1-based)
// Frontend derives sequence from step.step - 1, so step must be valid
const indexPath = this.path.replace(/\.jsonl$/, '.index.json');
writeTraceIndex(this.path, indexPath, true);
} catch (error: any) {
// Non-fatal: log but don't crash
console.log(`⚠️ Failed to generate trace index: ${error.message}`);
Expand Down