import { CodeTabs } from '../views/docs/CodeTabs';
import { Callout, NextLink, PropTable } from '../views/docs/prose';
export const ON_AGENT_SPEECH_SIG_PY = `@agent.on_agent_speech
def on_agent_speech(call: guava.Call, event: AgentSpeechEvent) -> None:
    ...`;

export const ON_AGENT_SPEECH_SIG_TS = `agent.onAgentSpeech((call: guava.Call, event: AgentSpeechEvent) => void);`;

export const ON_AGENT_SPEECH_EX_PY = `import logging
from guava.events import AgentSpeechEvent

logger = logging.getLogger(__name__)


@agent.on_agent_speech
def on_agent_speech(call: guava.Call, event: AgentSpeechEvent):
    logger.info("agent speech event: %s", event)

# Output:
# [INFO  15:02:29] agent speech event: sequence=None event_type='agent-speech'
#   utterance='Hi, thank you for calling Thai Palace. My name is Grace.
#   I can help you with the waitlist. ' interrupted=False`;

export const ON_AGENT_SPEECH_EX_TS = `import * as guava from "@guava-ai/guava-sdk";
import { AgentSpeechEvent } from "@guava-ai/guava-sdk/events";

agent.onAgentSpeech((_call: guava.Call, event: AgentSpeechEvent) => {
  console.log("agent speech event:", JSON.stringify(event));
});`;

## on\_agent\_speech()

Register a handler to receive a callback whenever the agent speaks. The event contains what the agent said and whether it was interrupted by the caller.

The `AgentSpeechEvent` is a pydantic model imported from `guava.events`:

```python
from guava.events import AgentSpeechEvent

class AgentSpeechEvent(BaseEvent):
    event_type: Literal["agent-speech"] = "agent-speech"
    utterance: str
    interrupted: bool = False
```

### Signature

<CodeTabs
  python={{ code: ON_AGENT_SPEECH_SIG_PY, filename: "signature" }}
  typescript={{ code: ON_AGENT_SPEECH_SIG_TS, filename: "signature" }}
/>

<PropTable rows={[
  {
    name: "call",
    type: "Call",
    desc: "The active call object.",
  },
  {
    name: "event",
    type: "AgentSpeechEvent",
    desc: "Contains `utterance` (string) and `interrupted` (boolean) fields.",
  },
]} />

**Return value:** `None`

### Example

<CodeTabs
  python={{ code: ON_AGENT_SPEECH_EX_PY, filename: "controller.py" }}
  typescript={{ code: ON_AGENT_SPEECH_EX_TS, filename: "controller.ts" }}
/>

<NextLink section="on-caller-speech" label="on_caller_speech()" />
