{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://jambonz.org/schema/verbs/dialogflow",
  "minVersion": "0.9.6",
  "title": "Dialogflow",
  "description": "Connects the caller to a Google Dialogflow agent for a voice conversation. Supports Dialogflow ES, CX, and CES models. The caller speaks and Dialogflow handles intent detection and response generation.",
  "type": "object",
  "properties": {
    "verb": {
      "const": "dialogflow",
      "description": "The verb name."
    },
    "id": {
      "type": "string",
      "description": "An optional unique identifier for this verb instance."
    },
    "credentials": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": true
        },
        {
          "type": "string"
        }
      ],
      "description": "Google service account credentials as a JSON object or stringified JSON."
    },
    "project": {
      "type": "string",
      "description": "The Google Cloud project ID."
    },
    "agent": {
      "type": "string",
      "description": "The Dialogflow agent ID for CX, or the app ID for CES. Required for both."
    },
    "environment": {
      "type": "string",
      "description": "The Dialogflow environment to use (CX)."
    },
    "deployment": {
      "type": "string",
      "description": "The deployment to use (CES). Omit to use the app's default."
    },
    "region": {
      "type": "string",
      "description": "The Google Cloud region for the Dialogflow API endpoint."
    },
    "model": {
      "type": "string",
      "enum": [
        "es",
        "cx",
        "ces"
      ],
      "description": "The Dialogflow model type: 'es' for Dialogflow ES, 'cx' for Dialogflow CX, 'ces' for Dialogflow CES."
    },
    "lang": {
      "type": "string",
      "description": "The language code for the conversation (e.g. 'en-US')."
    },
    "sessionId": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]{1,200}$",
      "description": "Names the conversation on the Dialogflow side. Reuse the same value on a later dialogflow verb in the same call to return the caller to the same conversation, with the history the agent had built up - for example after handing the caller to a dial leg and back. Defaults to the call sid."
    },
    "actionHook": {
      "$ref": "../components/actionHook",
      "description": "A webhook invoked when the Dialogflow session ends."
    },
    "eventHook": {
      "$ref": "../components/actionHook",
      "description": "A webhook invoked for Dialogflow events during the conversation."
    },
    "toolHook": {
      "$ref": "../components/actionHook",
      "description": "A webhook invoked when the agent requests a client-side tool call (CX and CES). Respond with {outputParameters} (or {error}) to resume the conversation. The posted tool_call is each model's native shape: CX sends {tool, action, input_parameters}; CES sends {id, display_name, tool, args}. CES echoes its per-call id automatically, so you need not return it."
    },
    "events": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "List of event types to receive via the eventHook."
    },
    "welcomeEvent": {
      "type": "string",
      "description": "A Dialogflow event to trigger at the start of the conversation (e.g. 'welcome')."
    },
    "welcomeEventParams": {
      "type": "object",
      "description": "Parameters to pass with the welcome event.",
      "additionalProperties": true
    },
    "noInputTimeout": {
      "type": "number",
      "description": "Seconds to wait for caller input before triggering the no-input event."
    },
    "noInputEvent": {
      "type": "string",
      "description": "Dialogflow event to trigger when no input is received within the timeout."
    },
    "passDtmfAsTextInput": {
      "type": "boolean",
      "description": "If true, pass DTMF digits to Dialogflow as text input."
    },
    "thinkingMusic": {
      "type": "string",
      "description": "URL of an audio file to play while waiting for Dialogflow to respond."
    },
    "tts": {
      "$ref": "../components/synthesizer",
      "description": "TTS configuration for Dialogflow responses."
    },
    "bargein": {
      "type": "boolean",
      "description": "If true, allow the caller to interrupt Dialogflow responses with speech."
    },
    "queryInput": {
      "type": "object",
      "description": "Initial query input to send to Dialogflow.",
      "properties": {
        "text": {
          "type": "string",
          "description": "Text input."
        },
        "intent": {
          "type": "string",
          "description": "Intent to trigger."
        },
        "event": {
          "type": "string",
          "description": "Event to trigger."
        },
        "dtmf": {
          "type": "string",
          "description": "DTMF input."
        }
      },
      "additionalProperties": false
    }
  },
  "required": [
    "project",
    "credentials",
    "lang"
  ],
  "examples": [
    {
      "verb": "dialogflow",
      "project": "my-gcp-project",
      "credentials": "{\"type\": \"service_account\", \"project_id\": \"my-gcp-project\"}",
      "lang": "en-US",
      "model": "cx",
      "agent": "my-agent-id",
      "welcomeEvent": "welcome",
      "actionHook": "/dialogflow-action",
      "eventHook": "/dialogflow-event"
    }
  ]
}
