{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/roark/json-schema/roark-chat-create-schema.json",
  "title": "ChatCreate",
  "description": "Chat creation payload. Supports singular (agent/customer) or plural (agents/customers) formats.",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/roark-chat-api-openapi.yml#/components/schemas/ChatCreate",
  "type": "object",
  "properties": {
    "startTimestamp": {
      "type": "string",
      "description": "When the chat started (ISO 8601 format)"
    },
    "endTimestamp": {
      "type": "string",
      "description": "When the chat ended (ISO 8601 format)"
    },
    "messages": {
      "type": "array",
      "items": {
        "anyOf": [
          {
            "$ref": "#/$defs/ChatMessageAgent"
          },
          {
            "$ref": "#/$defs/ChatMessageCustomer"
          }
        ]
      },
      "description": "Ordered list of messages exchanged during the chat"
    },
    "toolInvocations": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "description": "Name of the tool that was invoked"
          },
          "description": {
            "type": "string",
            "description": "Description of when the tool should be invoked"
          },
          "parameters": {
            "type": "object",
            "additionalProperties": {
              "anyOf": [
                {
                  "type": "object",
                  "properties": {
                    "description": {
                      "type": "string"
                    },
                    "type": {
                      "type": "string",
                      "enum": [
                        "string",
                        "number",
                        "boolean"
                      ]
                    },
                    "value": {}
                  }
                },
                {}
              ]
            },
            "description": "Parameters provided to the tool during invocation"
          },
          "result": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "object",
                "additionalProperties": {}
              }
            ],
            "description": "Result returned by the tool after execution. Can be a string or a JSON object"
          },
          "startTimestamp": {
            "type": "string",
            "description": "When the tool was invoked (ISO 8601). Must fall within the chat time range."
          },
          "endTimestamp": {
            "type": "string",
            "description": "When the tool execution completed (ISO 8601). Used to calculate duration of the tool execution."
          },
          "agent": {
            "type": "object",
            "properties": {
              "roarkId": {
                "type": "string",
                "format": "uuid",
                "description": "The Roark ID of the agent"
              },
              "customId": {
                "type": "string",
                "description": "The custom ID set on the agent"
              }
            },
            "description": "Metadata about the agent that invoked this tool - used to match which agent from the agents array this tool invocation belongs to"
          }
        },
        "required": [
          "name",
          "parameters",
          "result",
          "startTimestamp"
        ]
      },
      "description": "List of tool invocations made during the chat"
    },
    "properties": {
      "type": "object",
      "additionalProperties": {},
      "description": "Custom properties to include with the chat. These can be used for filtering and will show in the chat details page"
    },
    "traceId": {
      "type": [
        "string",
        "null"
      ],
      "description": "Optional OpenTelemetry trace ID for the chat. Surfaces the chat in the tracing tab."
    },
    "externalId": {
      "type": "string",
      "maxLength": 255,
      "description": "A stable identifier from your own system (e.g. session ID, conversation ID) used to correlate this chat with OpenTelemetry traces. Set the same value as a `roark.external_id` span or resource attribute on your traces and the matching trace will be linked automatically. Must be unique within a project."
    },
    "agent": {
      "allOf": [
        {
          "anyOf": [
            {
              "$ref": "#/$defs/ChatAgentIdentificationByRoarkId"
            },
            {
              "$ref": "#/$defs/ChatAgentIdentificationByName"
            },
            {
              "$ref": "#/$defs/ChatAgentIdentificationByCustomId"
            }
          ]
        },
        {
          "type": "object",
          "properties": {
            "prompt": {
              "type": "object",
              "properties": {
                "resolvedPrompt": {
                  "type": "string",
                  "description": "The agent's system prompt used during this chat"
                }
              },
              "required": [
                "resolvedPrompt"
              ],
              "description": "Agent's prompt configuration (optional)"
            }
          }
        }
      ],
      "description": "Single agent participating in the chat. Use this for the simpler API when you have only one agent."
    },
    "agents": {
      "type": "array",
      "items": {
        "allOf": [
          {
            "anyOf": [
              {
                "$ref": "#/$defs/ChatAgentIdentificationByRoarkId"
              },
              {
                "$ref": "#/$defs/ChatAgentIdentificationByName"
              },
              {
                "$ref": "#/$defs/ChatAgentIdentificationByCustomId"
              }
            ]
          },
          {
            "type": "object",
            "properties": {
              "prompt": {
                "type": "object",
                "properties": {
                  "resolvedPrompt": {
                    "type": "string",
                    "description": "The agent's system prompt used during this chat"
                  }
                },
                "required": [
                  "resolvedPrompt"
                ],
                "description": "Agent's prompt configuration (optional)"
              }
            }
          }
        ],
        "description": "Agent participating in the chat with their identification and prompt"
      },
      "description": "Agents participating in the chat. Each agent requires identification and prompt information."
    },
    "customer": {
      "type": "object",
      "properties": {
        "label": {
          "type": [
            "string",
            "null"
          ],
          "description": "Label to identify this customer in the chat (e.g., \"user-01\", \"anonymous-visitor\")"
        },
        "phoneNumberE164": {
          "type": [
            "string",
            "null"
          ],
          "description": "Customer phone number in E.164 format (e.g., +14155551234), optional for chat customers"
        }
      },
      "required": [
        "phoneNumberE164"
      ],
      "description": "Single customer participating in the chat. Use this for the simpler API when you have only one customer."
    },
    "customers": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "label": {
            "type": [
              "string",
              "null"
            ],
            "description": "Label to identify this customer in the chat (e.g., \"user-01\", \"anonymous-visitor\")"
          },
          "phoneNumberE164": {
            "type": [
              "string",
              "null"
            ],
            "description": "Customer phone number in E.164 format (e.g., +14155551234), optional for chat customers"
          }
        },
        "required": [
          "phoneNumberE164"
        ],
        "description": "Customer participating in the chat"
      },
      "description": "Customers participating in the chat."
    }
  },
  "required": [
    "startTimestamp",
    "endTimestamp",
    "messages"
  ],
  "$defs": {
    "ChatAgentIdentificationByCustomId": {
      "type": "object",
      "properties": {
        "customId": {
          "type": "string",
          "description": "Existing custom ID for a Roark agent"
        }
      },
      "required": [
        "customId"
      ]
    },
    "ChatAgentIdentificationByName": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "description": "Agent name"
        },
        "description": {
          "type": "string",
          "description": "Agent description"
        },
        "customId": {
          "type": "string",
          "description": "Agent custom ID"
        }
      },
      "required": [
        "name"
      ],
      "description": "Find existing by customId if provided, otherwise reuse exact project name match before creating"
    },
    "ChatAgentIdentificationByRoarkId": {
      "type": "object",
      "properties": {
        "roarkId": {
          "type": "string",
          "format": "uuid",
          "description": "Existing Roark agent ID"
        }
      },
      "required": [
        "roarkId"
      ]
    },
    "ChatMessageAgent": {
      "type": "object",
      "properties": {
        "role": {
          "type": "string",
          "const": "AGENT",
          "description": "The agent sent this message"
        },
        "text": {
          "type": "string",
          "minLength": 1,
          "description": "Message content"
        },
        "languageCode": {
          "type": "string",
          "description": "ISO language code (e.g., \"en\")"
        },
        "startTimestamp": {
          "type": "string",
          "description": "When the message was sent (ISO 8601). Must fall within the chat time range."
        },
        "endTimestamp": {
          "type": "string",
          "description": "When the message finished sending (ISO 8601). For instant messages, can equal startTimestamp."
        },
        "agent": {
          "type": "object",
          "properties": {
            "roarkId": {
              "type": "string",
              "format": "uuid",
              "description": "The Roark ID of the agent"
            },
            "customId": {
              "type": "string",
              "description": "The custom ID set on the agent"
            }
          },
          "description": "Metadata about the agent that sent this message - used to match which agent from the agents array this message belongs to"
        }
      },
      "required": [
        "role",
        "text",
        "startTimestamp"
      ]
    },
    "ChatMessageCustomer": {
      "type": "object",
      "properties": {
        "role": {
          "type": "string",
          "const": "CUSTOMER",
          "description": "The customer sent this message"
        },
        "text": {
          "type": "string",
          "minLength": 1,
          "description": "Message content"
        },
        "languageCode": {
          "type": "string",
          "description": "ISO language code (e.g., \"en\")"
        },
        "startTimestamp": {
          "type": "string",
          "description": "When the message was sent (ISO 8601). Must fall within the chat time range."
        },
        "endTimestamp": {
          "type": "string",
          "description": "When the message finished sending (ISO 8601). For instant messages, can equal startTimestamp."
        },
        "customer": {
          "type": "object",
          "properties": {
            "phoneNumberE164": {
              "type": "string",
              "description": "Customer phone number in E.164 format"
            },
            "label": {
              "type": "string",
              "description": "Customer label"
            }
          },
          "description": "Metadata about the customer that sent this message - used to match which customer from the customers array this message belongs to"
        }
      },
      "required": [
        "role",
        "text",
        "startTimestamp"
      ]
    }
  }
}