Overview

The updateEvent function updates properties of an existing event, such as adding results, costs, or changing the description.

Syntax

await lai.updateEvent(params?: UpdateEventParams): Promise<void>

Parameters

params
UpdateEventParams
required
Object with event update properties:

Returns

Returns a Promise that resolves to void.

Examples

Basic Update

// 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

// 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

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

// 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

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]
});

Common Patterns

1. Try-Catch Pattern

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

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

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

// ✅ 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

// ✅ 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

// 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