Overview
TheupdateEvent
function updates properties of an existing event, such as adding results, costs, or changing the description.
Syntax
Copy
Ask AI
await lai.updateEvent(params: {
eventId: string;
description?: string;
result?: string;
model?: string;
costAdded?: number;
duration?: number;
functionName?: string;
arguments?: any; // serializable JSON
}): Promise<void>
Parameters
Returns
Returns a Promise that resolves to void.Examples
Basic Update
Copy
Ask AI
// Create an event
const event = await lai.createEvent({
description: "Processing data"
});
// Update with results
await lai.updateEvent({
eventId: event.eventId,
result: "Processed 1000 records successfully"
});
Progressive Updates
Copy
Ask AI
// Create event for long operation
const event = await lai.createEvent({
description: "Batch processing"
});
// Update as processing progresses
for (let i = 0; i < batches.length; i++) {
await processBatch(batches[i]);
await lai.updateEvent({
eventId: event.eventId,
result: `Processed ${i + 1}/${batches.length} batches`
});
}
// Final update
await lai.updateEvent({
eventId: event.eventId,
result: `Completed all ${batches.length} batches`,
costAdded: calculateTotalCost()
});
Error Handling
Copy
Ask AI
const event = await lai.createEvent({
description: "External API call"
});
try {
const response = await callExternalAPI();
await lai.updateEvent({
eventId: event.eventId,
result: `Success: ${response.status}`,
costAdded: 0.01
});
} catch (error) {
await lai.updateEvent({
eventId: event.eventId,
result: `Failed: ${error.message}`,
costAdded: 0 // No cost for failed calls
});
}
Adding Costs
Copy
Ask AI
// Track incremental costs
const event = await lai.createEvent({
description: "Multi-part operation"
});
// Part 1
await operation1();
await lai.updateEvent({
eventId: event.eventId,
costAdded: 0.05
});
// Part 2
await operation2();
await lai.updateEvent({
eventId: event.eventId,
costAdded: 0.03 // Adds to existing cost
});
// Total cost is now 0.08
With Screenshots
Copy
Ask AI
const event = await lai.createEvent({
description: "Visual comparison"
});
// Process and capture results
const beforeImage = await captureScreenshot();
await performVisualChange();
const afterImage = await captureScreenshot();
// Update event with visual evidence
await lai.updateEvent({
eventId: event.eventId,
result: "Visual changes applied successfully",
// Note: screenshots are attached during createEvent in the TS SDK
});
Common Patterns
1. Try-Catch Pattern
Copy
Ask AI
async function trackedOperation(description: string, operation: () => Promise<any>) {
const event = await lai.createEvent({ description });
try {
const result = await operation();
await lai.updateEvent({
eventId: event.eventId,
result: `Success: ${JSON.stringify(result)}`
});
return result;
} catch (error) {
await lai.updateEvent({
eventId: event.eventId,
result: `Error: ${error.message}`
});
throw error;
}
}
// Usage
const data = await trackedOperation(
"Fetch user data",
() => fetchUserFromAPI(userId)
);
2. Streaming Updates
Copy
Ask AI
const event = await lai.createEvent({
description: "Stream processing"
});
let processedCount = 0;
const updateInterval = setInterval(async () => {
await lai.updateEvent({
eventId: event.eventId,
result: `Processed ${processedCount} items...`
});
}, 1000);
// Process stream
await processStream((item) => {
processedCount++;
});
clearInterval(updateInterval);
// Final update
await lai.updateEvent({
eventId: event.eventId,
result: `Completed: ${processedCount} items processed`
});
3. Cost Tracking
Copy
Ask AI
async function trackAPIUsage(apiName: string, apiCall: () => Promise<any>) {
const event = await lai.createEvent({
description: `${apiName} API call`
});
const startTime = Date.now();
const result = await apiCall();
const duration = Date.now() - startTime;
// Calculate cost based on duration or response
const cost = calculateAPIcost(apiName, duration, result);
await lai.updateEvent({
eventId: event.eventId,
result: `Completed in ${duration}ms`,
costAdded: cost
});
return result;
}
Best Practices
1. Update vs Create New
Copy
Ask AI
// ✅ Good - update existing event for related info
const event = await lai.createEvent({ description: "Process batch" });
for (const item of batch) {
await processItem(item);
await lai.updateEvent({
eventId: event.eventId,
result: `Processed ${item.id}`
});
}
// ❌ Avoid - creating many events for one operation
for (const item of batch) {
await lai.createEvent({ description: `Process ${item.id}` });
await processItem(item);
}
2. Meaningful Results
Copy
Ask AI
// ✅ Good - specific and useful
await lai.updateEvent({
eventId: event.eventId,
result: "Generated 5-page PDF report, 2.3MB, saved to /reports/2024-01.pdf"
});
// ❌ Less helpful
await lai.updateEvent({
eventId: event.eventId,
result: "Done"
});
3. Cost Accuracy
Copy
Ask AI
// Track all costs for accurate billing
await lai.updateEvent({
eventId: event.eventId,
costAdded: apiCost + computeCost + storageCost
});
Notes
- Event ID is required for updates
- Costs are additive - multiple updates add to the total
- Cannot update events in ended steps
- Updates don’t mark events as finished (events auto-end with steps)
See Also
- createEvent - Create new events
- Examples - More usage patterns