Update an existing event
updateEvent
await lai.updateEvent(params?: UpdateEventParams): Promise<void>
Show properties
// 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" });
// 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() });
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 }); }
// 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
const event = await lai.createEvent({ description: "Visual comparison" }); // Process and capture results const beforeImage = await captureScreenshot(); await performVisualChange(); const afterImage = await captureScreenshot(); // Upload images const beforeUrl = await lai.uploadImage(beforeImage); const afterUrl = await lai.uploadImage(afterImage); // Update event with visual evidence await lai.updateEvent({ eventId: event.eventId, result: "Visual changes applied successfully", screenshots: [beforeUrl, afterUrl] });
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) );
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` });
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; }
// ✅ 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); }
// ✅ 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" });
// Track all costs for accurate billing await lai.updateEvent({ eventId: event.eventId, costAdded: apiCost + computeCost + storageCost });