Overview

The updateSession function updates properties of the active session, such as task description, tags, or completion status.

Syntax

await lai.updateSession(
  task?: string,
  tags?: Record<string, any>,
  isFinished?: boolean,
  isSuccessful?: boolean,
  isSuccessfulReason?: string
): Promise<void>

Parameters

task
string
Updated task description for the session.
tags
Record<string, any>
Key-value pairs to tag and categorize the session.
isFinished
boolean
Whether the session is finished. Usually handled by endSession().
isSuccessful
boolean
Whether the session completed successfully.
isSuccessfulReason
string
Explanation of success or failure reason.

Returns

Returns a Promise that resolves to void.

Examples

Update Task Description

// Initial session
await lai.init({
  sessionName: "Data processor",
  task: "Process unknown dataset"
});

// After understanding the data
await lai.updateSession(
  "Process customer transaction data for Q4 2023"
);

Add Tags

// Add metadata tags
await lai.updateSession(
  undefined, // Don't change task
  {
    environment: "production",
    version: "2.1.0",
    customer: "enterprise-client-123",
    dataSize: "10GB",
    priority: "high"
  }
);

Progressive Updates

// Start with basic info
await lai.init({ sessionName: "Analysis workflow" });

// Add context as you learn more
await lai.updateSession(
  "Analyze user behavior patterns",
  { phase: "discovery" }
);

// Update after initial findings
await lai.updateSession(
  "Analyze shopping cart abandonment patterns",
  { 
    phase: "analysis",
    findingType: "cart-abandonment",
    timeRange: "30-days"
  }
);

Error Tracking

try {
  await performCriticalOperation();
} catch (error) {
  // Mark session state before ending
  await lai.updateSession(
    undefined,
    {
      error: error.message,
      errorType: error.constructor.name,
      failurePoint: "critical-operation"
    }
  );
  
  // Then end session
  await lai.endSession(false, error.message);
}

Workflow Phases

async function multiPhaseWorkflow() {
  await lai.init({ sessionName: "ETL Pipeline" });

  // Phase 1
  await lai.updateSession("Extract data from sources", { phase: "extract" });
  await extractData();

  // Phase 2  
  await lai.updateSession("Transform and validate data", { phase: "transform" });
  await transformData();

  // Phase 3
  await lai.updateSession("Load data to warehouse", { phase: "load" });
  await loadData();

  // Final update
  await lai.updateSession(
    "ETL pipeline completed",
    { 
      phase: "complete",
      recordsProcessed: 50000,
      duration: "45min"
    }
  );
}

Common Use Cases

1. A/B Testing

const variant = Math.random() > 0.5 ? 'A' : 'B';

await lai.updateSession(
  undefined,
  {
    experiment: "prompt-optimization",
    variant: variant,
    testGroup: "treatment"
  }
);

2. Performance Tracking

const startTime = Date.now();
const result = await complexOperation();
const duration = Date.now() - startTime;

await lai.updateSession(
  undefined,
  {
    performanceMs: duration,
    resultSize: result.length,
    cacheHit: result.fromCache
  }
);

3. User Context

await lai.updateSession(
  undefined,
  {
    userId: user.id,
    userTier: user.subscription,
    userRegion: user.location,
    sessionSource: "web-app"
  }
);

Best Practices

1. Use Tags for Filtering

// Good - enables dashboard filtering
await lai.updateSession(undefined, {
  env: "prod",
  feature: "chat",
  model: "gpt-4",
  customer: customerId
});

2. Update Task as Context Emerges

// Start generic
await lai.init({ task: "Process user request" });

// Update with specifics
await lai.updateSession(
  `Generate ${reportType} report for ${dateRange}`
);

3. Don’t Use for Step Data

// ❌ Wrong - use step functions instead
await lai.updateSession("Working on step 2");

// ✅ Correct - session-level info only
await lai.updateSession(
  "Multi-step analysis workflow",
  { totalSteps: 5 }
);

Notes

  • Only updates the active session
  • All parameters are optional
  • isFinished is typically handled by endSession()
  • Tags are merged with existing tags, not replaced
  • Cannot update a finished session

See Also