Skip to content

Commit ee41118

Browse files
author
Sentience Dev
committed
Merge pull request #88 from SentienceAPI/missing_fields2
trace should include snapshot elements; fixes to seq_num, etc
2 parents 505cced + 6e1188f commit ee41118

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sentienceapi",
3-
"version": "0.91.0",
3+
"version": "0.91.1",
44
"description": "TypeScript SDK for Sentience AI Agent Browser Automation",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/agent.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,26 @@ export class SentienceAgent {
214214

215215
// Emit snapshot event
216216
if (this.tracer) {
217+
// Include ALL elements with full data for DOM tree display
218+
// Use snap.elements (all elements) not filteredSnap.elements
217219
const snapshotData: any = {
218-
url: filteredSnap.url,
219-
element_count: filteredSnap.elements.length,
220-
timestamp: filteredSnap.timestamp,
221-
elements: filteredSnap.elements.slice(0, 50).map(el => ({
220+
url: snap.url,
221+
element_count: snap.elements.length,
222+
timestamp: snap.timestamp,
223+
elements: snap.elements.map(el => ({
222224
id: el.id,
223-
bbox: el.bbox,
224225
role: el.role,
225-
text: el.text?.substring(0, 50),
226+
text: el.text,
227+
importance: el.importance,
228+
bbox: el.bbox,
229+
visual_cues: el.visual_cues,
230+
in_viewport: el.in_viewport,
231+
is_occluded: el.is_occluded,
232+
z_index: el.z_index,
233+
rerank_index: el.rerank_index,
234+
heuristic_index: el.heuristic_index,
235+
ml_probability: el.ml_probability,
236+
ml_score: el.ml_score,
226237
}))
227238
};
228239

src/tracing/cloud-sink.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,10 @@ export class CloudTraceSink extends TraceSink {
572572
private generateIndex(): void {
573573
try {
574574
const { writeTraceIndex } = require('./indexer');
575-
writeTraceIndex(this.tempFilePath);
575+
// Use frontend format to ensure 'step' field is present (1-based)
576+
// Frontend derives sequence from step.step - 1, so step must be valid
577+
const indexPath = this.tempFilePath.replace('.jsonl', '.index.json');
578+
writeTraceIndex(this.tempFilePath, indexPath, true);
576579
} catch (error: any) {
577580
// Non-fatal: log but don't crash
578581
this.logger?.warn(`Failed to generate trace index: ${error.message}`);
@@ -609,9 +612,30 @@ export class CloudTraceSink extends TraceSink {
609612
return;
610613
}
611614

612-
// Read and compress index file
613-
const indexData = await fsPromises.readFile(indexPath);
614-
const compressedIndex = zlib.gzipSync(indexData);
615+
// Read index file and update trace_file.path to cloud storage path
616+
const indexContent = await fsPromises.readFile(indexPath, 'utf-8');
617+
const indexJson = JSON.parse(indexContent);
618+
619+
// Extract cloud storage path from trace upload URL
620+
// uploadUrl format: https://...digitaloceanspaces.com/traces/{run_id}.jsonl.gz
621+
// Extract path: traces/{run_id}.jsonl.gz
622+
try {
623+
const parsedUrl = new URL(this.uploadUrl);
624+
// Extract path after domain (e.g., /traces/run-123.jsonl.gz -> traces/run-123.jsonl.gz)
625+
const cloudTracePath = parsedUrl.pathname.startsWith('/')
626+
? parsedUrl.pathname.substring(1)
627+
: parsedUrl.pathname;
628+
// Update trace_file.path in index
629+
if (indexJson.trace_file && typeof indexJson.trace_file === 'object') {
630+
indexJson.trace_file.path = cloudTracePath;
631+
}
632+
} catch (error: any) {
633+
this.logger?.warn(`Failed to extract cloud path from upload URL: ${error.message}`);
634+
}
635+
636+
// Serialize updated index to JSON
637+
const updatedIndexData = Buffer.from(JSON.stringify(indexJson, null, 2), 'utf-8');
638+
const compressedIndex = zlib.gzipSync(updatedIndexData);
615639
const indexSize = compressedIndex.length;
616640
this.indexFileSizeBytes = indexSize; // Track index file size
617641

src/tracing/indexer.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,25 @@ function computeSnapshotDigest(snapshotData: any): string {
5757
const elements = snapshotData.elements || [];
5858

5959
// Canonicalize elements
60-
const canonicalElements = elements.map((elem: any) => ({
61-
id: elem.id,
62-
role: elem.role || '',
63-
text_norm: normalizeText(elem.text),
64-
bbox: roundBBox(elem.bbox || { x: 0, y: 0, width: 0, height: 0 }),
65-
is_primary: elem.is_primary || false,
66-
is_clickable: elem.is_clickable || false,
67-
}));
60+
const canonicalElements = elements.map((elem: any) => {
61+
// Extract is_primary and is_clickable from visual_cues if present
62+
const visualCues = elem.visual_cues || {};
63+
const isPrimary = (typeof visualCues === 'object' && visualCues !== null)
64+
? (visualCues.is_primary || false)
65+
: (elem.is_primary || false);
66+
const isClickable = (typeof visualCues === 'object' && visualCues !== null)
67+
? (visualCues.is_clickable || false)
68+
: (elem.is_clickable || false);
69+
70+
return {
71+
id: elem.id,
72+
role: elem.role || '',
73+
text_norm: normalizeText(elem.text),
74+
bbox: roundBBox(elem.bbox || { x: 0, y: 0, width: 0, height: 0 }),
75+
is_primary: isPrimary,
76+
is_clickable: isClickable,
77+
};
78+
});
6879

6980
// Sort by element id for determinism
7081
canonicalElements.sort((a: { id?: number }, b: { id?: number }) => (a.id || 0) - (b.id || 0));

src/tracing/jsonl-sink.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ export class JsonlTraceSink extends TraceSink {
162162
private generateIndex(): void {
163163
try {
164164
const { writeTraceIndex } = require('./indexer');
165-
writeTraceIndex(this.path);
165+
// Use frontend format to ensure 'step' field is present (1-based)
166+
// Frontend derives sequence from step.step - 1, so step must be valid
167+
const indexPath = this.path.replace(/\.jsonl$/, '.index.json');
168+
writeTraceIndex(this.path, indexPath, true);
166169
} catch (error: any) {
167170
// Non-fatal: log but don't crash
168171
console.log(`⚠️ Failed to generate trace index: ${error.message}`);

0 commit comments

Comments
 (0)