{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://jambonz.org/schema/callbacks/agent-turn",
  "title": "Agent EventHook Events",
  "description": "Events sent to the agent verb's eventHook during a conversation. These are sent as 'agent:event' messages over the WebSocket connection.",
  "type": "object",
  "oneOf": [
    {
      "properties": {
        "type": {
          "const": "turn_end",
          "description": "Sent at the end of each conversational turn."
        },
        "transcript": {
          "type": "string",
          "description": "The user's final speech-to-text transcript for this turn."
        },
        "response": {
          "type": "string",
          "description": "The assistant's text response for this turn. May be trimmed to what was actually spoken if the turn was interrupted and alignment tracking is enabled."
        },
        "interrupted": {
          "type": "boolean",
          "description": "True if the user interrupted the assistant before it finished speaking.",
          "default": false
        },
        "latency": {
          "type": "object",
          "description": "Latency metrics for this turn, all in milliseconds. Fields are absent when not applicable.",
          "properties": {
            "transcriber_latency": {
              "type": "integer",
              "description": "STT processing latency: time from user stops talking until final transcript received, in milliseconds."
            },
            "turn_detection_latency": {
              "type": "integer",
              "description": "Additional wait after final transcript for end-of-turn detection, in milliseconds. Absent when EOT fires before or with the transcript."
            },
            "model_latency": {
              "type": "integer",
              "description": "LLM latency: time spent waiting for the first LLM token after the system is ready to prompt, in milliseconds. Absent on a preflight hit."
            },
            "voice_latency": {
              "type": "integer",
              "description": "TTS engine latency: time from first text sent to the TTS engine until first audio received back, in milliseconds."
            },
            "preflight": {
              "type": "object",
              "description": "Early generation (preflight) metrics. Only present when earlyGeneration is enabled.",
              "properties": {
                "result": {
                  "type": "string",
                  "enum": ["hit", "miss", "pending"],
                  "description": "Whether the speculative preflight transcript matched the final transcript ('hit'), did not match ('miss'), or was still in progress ('pending')."
                },
                "tokens": {
                  "type": "integer",
                  "description": "Number of preflight tokens that were buffered. Only present on a 'hit'."
                }
              },
              "additionalProperties": false
            },
            "first_speech_token_ms": {
              "type": "integer",
              "description": "With the emotion option: time from prompting the LLM to its first spoken (non-directive) token, in milliseconds."
            }
          },
          "additionalProperties": false
        },
        "user_emotion": {
          "type": "object",
          "description": "The caller's tone from the STT vendor, with the agent verb's emotion option.",
          "properties": {
            "label": {
              "type": "string",
              "enum": ["neutral", "happy", "sad", "angry", "fearful", "surprised", "disgusted", "calm", "other"]
            },
            "detail": {
              "type": "string",
              "description": "The STT vendor's own word when it is more specific than label, such as frustrated."
            },
            "confidence": {
              "type": ["number", "null"]
            },
            "source": {
              "type": "string",
              "enum": ["acoustic", "text", "llm"]
            }
          },
          "required": ["label", "source"],
          "additionalProperties": false
        },
        "agent_styles": {
          "type": "array",
          "description": "Voice directives used in this turn's reply, with the agent verb's emotion option.",
          "items": {
            "type": "object",
            "additionalProperties": true
          }
        }
      },
      "required": ["type", "transcript", "response", "interrupted", "latency"],
      "additionalProperties": false
    },
    {
      "properties": {
        "type": {
          "const": "user_transcript",
          "description": "Sent when the user's final transcript is available and the system is proceeding to prompt the LLM. This indicates the end of the user's speech input for the current turn."
        },
        "transcript": {
          "type": "string",
          "description": "The user's final speech-to-text transcript."
        },
        "emotion": {
          "type": "object",
          "description": "The caller's tone from the STT vendor, with the agent verb's emotion option.",
          "properties": {
            "label": {
              "type": "string",
              "enum": ["neutral", "happy", "sad", "angry", "fearful", "surprised", "disgusted", "calm", "other"]
            },
            "detail": {
              "type": "string",
              "description": "The STT vendor's own word when it is more specific than label, such as frustrated."
            },
            "confidence": {
              "type": ["number", "null"]
            },
            "source": {
              "type": "string",
              "enum": ["acoustic", "text", "llm"]
            }
          },
          "required": ["label", "source"],
          "additionalProperties": false
        }
      },
      "required": ["type", "transcript"],
      "additionalProperties": false
    },
    {
      "properties": {
        "type": {
          "const": "llm_response",
          "description": "Sent when the LLM has finished generating its response for the current turn. Contains the complete response text."
        },
        "response": {
          "type": "string",
          "description": "The assistant's complete text response."
        },
        "styles": {
          "type": "array",
          "description": "Voice directives the LLM wrote, with the agent verb's emotion option: the offset in the response text where each style starts, and its attributes.",
          "items": {
            "type": "object",
            "properties": {
              "offset": {
                "type": "integer"
              }
            },
            "additionalProperties": true
          }
        }
      },
      "required": ["type", "response"],
      "additionalProperties": false
    },
    {
      "properties": {
        "type": {
          "const": "user_interruption",
          "description": "Sent when the user barges in (interrupts) while the assistant is speaking. The agent verb's pipeline adds what confirmed the barge-in and what the caller heard; speech-to-speech engines send no additional data."
        },
        "cause": {
          "type": "string",
          "enum": ["vad", "transcript", "interrupt_prediction"],
          "description": "What confirmed the barge-in: speech still active after minSpeechDuration ('vad'), words from the speech recognizer ('transcript'), or the turn-taking model's interrupt prediction ('interrupt_prediction')."
        },
        "probability": {
          "type": "number",
          "description": "The interrupt prediction's probability. Only with cause 'interrupt_prediction'."
        },
        "spoken_text": {
          "type": "string",
          "description": "The part of the assistant's reply the caller heard before the interruption. Absent when it is not known."
        },
        "spoken_estimated": {
          "type": "boolean",
          "description": "True when spoken_text is estimated from playout time because the TTS vendor reports no word alignment."
        }
      },
      "required": ["type"],
      "additionalProperties": false
    },
    {
      "properties": {
        "type": {
          "const": "overlapping_speech",
          "description": "Sent when the caller speaks during the assistant's reply and it is not treated as an interruption (barge-in off, or the interrupt prediction model judged it not a floor-taking attempt)."
        },
        "outcome": {
          "type": "string",
          "enum": ["ignored", "held", "answered"],
          "description": "'ignored': too short to be a turn (a backchannel such as 'okay'), dropped. 'held': kept and added to the caller's next turn. 'answered': the reply ended and the held speech became the caller's turn."
        },
        "transcript": {
          "type": "string",
          "description": "What the caller said. With outcome 'answered', all the held speech."
        },
        "word_count": {
          "type": "integer",
          "description": "Number of words in transcript."
        }
      },
      "required": ["type", "outcome", "transcript", "word_count"],
      "additionalProperties": false
    },
    {
      "properties": {
        "type": {
          "const": "interrupt_prediction_ignored",
          "description": "Sent when the interrupt prediction model fires before the assistant has started speaking (while it is thinking or waiting on the turn gate). It is usually the tail of the utterance being answered, so it is not treated as an interruption."
        },
        "probability": {
          "type": "number",
          "description": "The interrupt prediction's probability."
        },
        "state": {
          "type": "string",
          "enum": ["thinking", "awaiting_turn_gate"],
          "description": "The agent's state when the prediction arrived."
        }
      },
      "required": ["type", "probability", "state"],
      "additionalProperties": false
    },
    {
      "properties": {
        "type": {
          "const": "no_response_timeout",
          "description": "Sent when the caller has said nothing for noResponseTimeout and the agent prompts them to check they are still there."
        },
        "count": {
          "type": "integer",
          "description": "How many times in a row the agent has prompted the silent caller, this one included."
        },
        "timeout_ms": {
          "type": "integer",
          "description": "The silence that triggered the prompt, in milliseconds."
        }
      },
      "required": ["type", "count", "timeout_ms"],
      "additionalProperties": false
    },
    {
      "properties": {
        "type": {
          "const": "llm_error",
          "description": "Sent when a request to the LLM fails. Unless retrying is true, the agent verb then ends with completion reason 'llm_failure'."
        },
        "stage": {
          "type": "string",
          "enum": ["prompt", "retry_without_tools", "tool_result"],
          "description": "'prompt': the request for a reply failed. 'retry_without_tools': the retry of that request without tools failed. 'tool_result': the request that follows a tool result failed."
        },
        "vendor": {
          "type": "string",
          "description": "The LLM vendor."
        },
        "model": {
          "type": "string",
          "description": "The LLM model."
        },
        "message": {
          "type": "string",
          "description": "The vendor's error message, at most 200 characters, with anything that looks like a key or token masked."
        },
        "retrying": {
          "type": "boolean",
          "description": "True when the agent retries the request without tools."
        }
      },
      "required": ["type", "stage", "message", "retrying"],
      "additionalProperties": false
    }
  ]
}
