PlayClaw
PlayClaw

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); // 2
4 console.log(context.totalTurns); // 5
5 console.log(context.isLastTurn); // false
6 console.log(context.history); // [{ role, content }, ...]
7 return await myAgent.reply(message);
8});
FieldTypeDescription
sessionIdstringUnique ID for this audit session. Consistent across all turns.
turnNumbernumber1-based index of the current turn.
totalTurnsnumberMax turns for this session (matches auditTurns).
isLastTurnbooleanTrue on the final turn. Useful for conclusive replies.
historyArray<{ 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 turns
5 { 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.isLastTurn
3 ? "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});