createSession(machine: MachineDefinition, metaJson?: Record<string, unknown>): Promise<Session>
Creates a new session instance from a machine definition. Generates a UUID, sets currentState to machine.initialState, and stores the session.
CLI Usage
npx dialai machine.json
This implicitly creates a session from your machine definition. For programmatic control:
import { createSession } from "dialai";
const machine = {
machineName: "document-review",
initialState: "pending",
goalState: "approved",
states: {
pending: {
prompt: "Review the document. Approve or request changes?",
transitions: { approve: "approved", reject: "rejected" }
},
approved: {},
rejected: {}
}
};
const session = await createSession(machine);
console.log(session);
Expected Output
Session created:
sessionId: a1b2c3d4-5678-90ab-cdef-1234567890ab
machineName: document-review
currentState: pending
currentRoundId: f1e2d3c4-...
history: []
createdAt: 2024-01-15T10:30:00.000Z
What Happened
- A new UUID was generated for the session
- The session was initialized in the machine's
initialState("pending") - A new round ID was generated for the first decision cycle
- The session was stored in memory
- The session object was returned
Parameters
| Param | Type | Required | Description |
|---|---|---|---|
machine | MachineDefinition | Yes | The machine definition to instantiate |
metaJson | Record<string, unknown> | No | Arbitrary session-level metadata (e.g., puzzle configuration) |
See MachineDefinition for the complete type definition.
Return Value
Returns a Session object containing:
sessionId: Unique identifier for this sessionmachineName: Name of the machine being runcurrentState: Current state (initiallymachine.initialState)currentRoundId: ID of the current decision roundmachine: The full machine definitionhistory: Empty array (no transitions yet)createdAt: Timestamp of creationmetaJson: Session-level metadata (if provided)
See Session for the complete type definition.
Error Cases
| Error | Cause |
|---|---|
Invalid machine definition | Missing required fields (machineName, initialState, goalState, states) |
Unknown initial state | initialState does not exist in states |