Overview

Resume a previously created session by calling init({ sessionId }). This allows you to add more steps and events to an existing workflow.

Syntax

await lai.init({ sessionId: string }): Promise<string>

Parameters

sessionId
string
required
The ID of the session to continue. This is returned when creating a session.

Returns

Returns a Promise that resolves to the real session ID (string).

Examples

Basic Usage

import * as lai from 'lucidicai';

// Continue a previous session
await lai.init({ sessionId: "session-uuid-123" });

// Add more steps to the existing session
await lai.createStep({
  state: "Resuming analysis",
  action: "Process additional data"
});

// Continue your workflow...

Workflow Continuation Pattern

// Initial run
const sessionId = await lai.init({ sessionName: "Long-running analysis" });

// Do some work...
await lai.createStep({ state: "Phase 1" });
// ... processing ...
await lai.endStep();

// Save session ID for later
saveToDatabase({ sessionId });

// End for now
await lai.endSession();

// === Later, in a different process ===

// Retrieve saved session ID
const { sessionId } = loadFromDatabase();

// Continue where we left off
await lai.init({ sessionId });

// Add more steps
await lai.createStep({ state: "Phase 2" });
// ... more processing ...
await lai.endStep();

await lai.endSession();

Error Recovery

try {
  await lai.init({ sessionId: "invalid-session-id" });
} catch (error) {
  console.error("Failed to continue session:", error);
  // Session not found, already finished, etc.
  
  // Create a new session instead
  await lai.init({ sessionName: "Recovery session" });
}

Use Cases

1. Long-Running Workflows

Perfect for workflows that span multiple executions:
// Job 1: Data collection
const sessionId = await lai.init({ sessionName: "ETL Pipeline" });
await collectData();
await lai.endSession();

// Job 2: Data processing (continues same session)
await lai.init({ sessionId });
await processData();
await lai.endSession();

// Job 3: Report generation (continues same session)
await lai.init({ sessionId });
await generateReport();
await lai.endSession({ isSuccessful: true, isSuccessfulReason: "Pipeline completed" });

2. Distributed Processing

When different services handle different parts of a workflow:
// Service A
const sessionId = await lai.init({ sessionName: "Multi-service workflow" });

// Publish session ID to message queue
await publishMessage({ sessionId, nextService: "B" });

// Service B (different process/container)
const { sessionId } = await consumeMessage();
await lai.init({ sessionId });
// Continue processing...

3. Retry Logic

async function processWithRetry(sessionId: string, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    try {
      await lai.init({ sessionId });
      
      await lai.createStep({
        state: `Attempt ${i + 1}`,
        action: "Process data"
      });
      
      await riskyOperation();
      
      await lai.endStep({ evalScore: 100 });
      return; // Success
      
    } catch (error) {
      await lai.endStep({
        evalScore: 0,
        evalDescription: `Failed: ${error.message}`
      });
      
      if (i === maxRetries - 1) throw error;
      await new Promise(r => setTimeout(r, 1000 * (i + 1))); // Backoff
    }
  }
}

Important Notes

  • The session must exist and not be finished
  • Only one session can be active at a time
  • Calling init({ sessionId }) replaces any previous active session in this process

Error Cases

ErrorCauseSolution
”SDK not initialized”init() not calledCall lai.init() first
”Session not found”Invalid session IDVerify session ID exists
”Session already finished”Session was endedCreate a new session

See Also