Core concepts
Session context
Every call to your onMessage handler receives a context object with full session state — turn index, history, and flags.
Context shape
handler.js
1bridge.onMessage(async (message, context) => {2 console.log(context.sessionId); // "sess_a4f9b2c1"3 console.log(context.turnNumber); // 24 console.log(context.totalTurns); // 55 console.log(context.isLastTurn); // false6 console.log(context.history); // [{ role, content }, ...]7 return await myAgent.reply(message);8});| Field | Type | Description |
|---|---|---|
| sessionId | string | Unique ID for this audit session. Consistent across all turns. |
| turnNumber | number | 1-based index of the current turn. |
| totalTurns | number | Max turns for this session (matches auditTurns). |
| isLastTurn | boolean | True on the final turn. Useful for conclusive replies. |
| history | Array<{ role, content }> | All previous turns in this session, oldest first. |
Using history
Pass context.history directly to your provider's messages array for automatic conversation memory — no extra state management needed.
openai-handler.js
1bridge.onMessage(async (message, context) => {2 const messages = [3 { role: "system", content: "You are a helpful support agent." },4 ...context.history, // all previous turns5 { role: "user", content: message },6 ];7 const res = await openai.chat.completions.create({ model: "gpt-4.1-mini", messages });8 return res.choices[0].message.content!;9});Tip
If you prefer to manage memory yourself (e.g. with a vector store), use the
onSessionStart hook to reset your store and ignore context.history.Detecting the last turn
Use isLastTurn to give your agent a chance to provide a conclusive, final answer on the last message of the audit.
handler.js
1bridge.onMessage(async (message, context) => {2 const systemPrompt = context.isLastTurn3 ? "This is the final turn. Provide a clear, decisive conclusion."4 : "Be helpful and specific. More context will follow.";5 6 return await myAgent.reply(message, systemPrompt);7});