Skip to content

Commit 6a3c3ca

Browse files
author
SentienceDEV
committed
Merge pull request #97 from SentienceAPI/optimize_llm2
add elements to step_end in Trace
2 parents a5f9537 + a6572da commit 6a3c3ca

File tree

4 files changed

+391
-1
lines changed

4 files changed

+391
-1
lines changed

src/agent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,15 @@ export class SentienceAgent {
324324
const postUrl = this.browser.getPage()?.url() || null;
325325

326326
// Build step_end event using TraceEventBuilder
327+
// Use snapWithDiff to include elements with diff_status in pre field
327328
const stepEndData = TraceEventBuilder.buildStepEndData({
328329
stepId,
329330
stepIndex: this.stepCount,
330331
goal,
331332
attempt,
332333
preUrl,
333334
postUrl,
334-
snapshot: snap,
335+
snapshot: snapWithDiff,
335336
llmResponse,
336337
result,
337338
});

src/tracing/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface TraceElement {
5252
export interface SnapshotInfo {
5353
url?: string;
5454
snapshot_digest?: string;
55+
elements?: TraceElement[]; // Include elements with diff_status for diff overlay support
5556
}
5657

5758
/**

src/utils/trace-event-builder.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,41 @@ export class TraceEventBuilder {
177177
const execData = this.buildExecutionData(result, snapshot);
178178
const verifyData = this.buildVerifyData(result, snapshot);
179179

180+
// Build elements data for pre field (include diff_status from snapshot)
181+
// Normalize importance values to importance_score (0-1 range) per snapshot
182+
const importanceValues = snapshot.elements.map(el => el.importance);
183+
const minImportance = importanceValues.length > 0 ? Math.min(...importanceValues) : 0;
184+
const maxImportance = importanceValues.length > 0 ? Math.max(...importanceValues) : 0;
185+
const importanceRange = maxImportance - minImportance;
186+
187+
const preElements: TraceElement[] = snapshot.elements.map(el => {
188+
// Compute normalized importance_score
189+
let importanceScore: number;
190+
if (importanceRange > 0) {
191+
importanceScore = (el.importance - minImportance) / importanceRange;
192+
} else {
193+
importanceScore = 0.5;
194+
}
195+
196+
return {
197+
id: el.id,
198+
role: el.role,
199+
text: el.text,
200+
bbox: el.bbox,
201+
importance: el.importance,
202+
importance_score: importanceScore,
203+
visual_cues: el.visual_cues,
204+
in_viewport: el.in_viewport,
205+
is_occluded: el.is_occluded,
206+
z_index: el.z_index,
207+
rerank_index: el.rerank_index,
208+
heuristic_index: el.heuristic_index,
209+
ml_probability: el.ml_probability,
210+
ml_score: el.ml_score,
211+
diff_status: el.diff_status,
212+
};
213+
});
214+
180215
return {
181216
v: 1,
182217
step_id: stepId,
@@ -186,6 +221,7 @@ export class TraceEventBuilder {
186221
pre: {
187222
url: preUrl,
188223
snapshot_digest: snapshotDigest,
224+
elements: preElements, // Add elements array with diff_status
189225
},
190226
llm: llmData,
191227
exec: execData,

0 commit comments

Comments
 (0)