ctx.message
ctx.message represents the assistant message produced in the current turn.
When It Exists
ctx.message represents the assistant message generated during the current turn.
This value is only available in post-turn hooks that run after the model finishes generating a response.
During prompt construction (onContextBuild), ctx.message is always null.
It becomes populated in post-turn hooks where the host provides the assistant message for the current turn, such as onTurnEnd.
Shape
interface TurnMessage {
id: string
role: 'assistant'
content: string | null
finishReason?: 'stop' | 'length' | 'tool_calls' | 'error'
toolCalls?: ToolCall[]
}Fields
id– unique identifier of the assistant messagerole– always"assistant"content– textual content produced by the modelfinishReason– reason the model stopped generatingtoolCalls– reserved for tool-capable flows
content may be null in cases such as:
- generation failed
- the turn was aborted
Practical Use
Strategies typically use ctx.message in post-turn hooks to:
- skip post-processing after aborted or failed turns
- extract structured memory from generated text
Example:
export async function onTurnEnd(ctx) {
if (!ctx.message) return
if (ctx.message.finishReason !== 'stop') {
return
}
const text = ctx.message.content
if (!text) return
// perform memory extraction or post-processing
}In v0.1, strategies should primarily rely on content and finishReason.
Notes
ctx.message represents only the assistant message generated in the current turn.
It does not expose earlier messages.
To access conversation history, use:
ctx.history