{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://github.com/andrewhuot/headless-chat-sdk/schemas/tools.schema.json",
  "title": "Tool definition",
  "description": "Mirrors packages/gecx-chat/src/tools/defineClientTool.ts and packages/gecx-chat/src/tools/defineServerTool.ts. JSON Schemas for input/output are validated at runtime. The `kind` discriminator selects whether the tool runs in the browser (`client`) or behind a customer backend (`server`).",
  "type": "object",
  "required": ["name", "description", "inputSchema"],
  "properties": {
    "kind": { "type": "string", "enum": ["client", "server"], "default": "client" },
    "name": { "type": "string", "pattern": "^[a-z][a-z0-9_-]+$" },
    "description": { "type": "string", "minLength": 1 },
    "inputSchema": { "type": "object", "description": "JSON Schema describing the tool's input arguments." },
    "outputSchema": { "type": "object", "description": "Optional JSON Schema describing the structured output." },
    "permissions": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "requiresUserApproval": { "type": "boolean" },
        "approvalPolicy": { "$ref": "#/definitions/ApprovalPolicy" },
        "allowedOrigins": { "type": "array", "items": { "type": "string" } }
      }
    },
    "approvalPolicy": { "$ref": "#/definitions/ApprovalPolicy" },
    "sideEffectLevel": { "$ref": "#/definitions/SideEffectLevel" },
    "idempotency": { "$ref": "#/definitions/IdempotencyPolicy" },
    "auth": { "$ref": "#/definitions/AuthHints" },
    "audit": { "$ref": "#/definitions/AuditPolicy" },
    "timeoutMs": { "type": "integer", "minimum": 100, "maximum": 600000 },
    "execute": { "description": "Function: (input, ctx) => Promise<output>. Not serialized. Required for kind=client; generated by the SDK for kind=server." },
    "http": {
      "type": "object",
      "description": "Server-tool HTTP transport options. Required when kind=server.",
      "required": ["endpoint"],
      "additionalProperties": false,
      "properties": {
        "endpoint": { "type": "string", "minLength": 1, "description": "URL of the customer-owned tool endpoint. Typically /chat/tool-call on the customer proxy." },
        "headers": { "type": "object", "additionalProperties": { "type": "string" } },
        "credentials": { "type": "string", "enum": ["omit", "same-origin", "include"] }
      }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "kind": { "const": "server" } }, "required": ["kind"] },
      "then": { "required": ["http"] }
    }
  ],
  "definitions": {
    "ApprovalPolicy": {
      "type": "string",
      "enum": ["auto", "user_confirm", "supervisor_approve", "denied", "async_pending"]
    },
    "SideEffectLevel": {
      "type": "string",
      "enum": ["none", "read_only", "state_changing", "external_side_effect"]
    },
    "IdempotencyPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode"],
      "properties": {
        "mode": { "type": "string", "enum": ["none", "optional", "required"] },
        "duplicateBehavior": { "type": "string", "enum": ["return_cached", "reject"] },
        "ttlMs": { "type": "integer", "minimum": 1 },
        "headerName": { "type": "string", "minLength": 1, "default": "Idempotency-Key" }
      }
    },
    "AuthHints": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "required": { "type": "boolean" },
        "scopes": { "type": "array", "items": { "type": "string" } },
        "permissions": { "type": "array", "items": { "type": "string" } },
        "resource": { "type": "string" }
      }
    },
    "AuditPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["classification"],
      "properties": {
        "classification": { "type": "string", "enum": ["none", "standard", "pii", "financial", "regulated"] },
        "redactInput": { "type": "array", "items": { "type": "string" } },
        "redactOutput": { "type": "array", "items": { "type": "string" } }
      }
    },
    "ServerToolManifest": {
      "type": "object",
      "required": ["kind", "name", "description", "inputSchema", "approvalPolicy", "idempotency", "auth", "audit", "timeoutMs", "sideEffectLevel"],
      "additionalProperties": false,
      "properties": {
        "kind": { "const": "server" },
        "name": { "type": "string" },
        "description": { "type": "string" },
        "inputSchema": { "type": "object" },
        "outputSchema": { "type": "object" },
        "approvalPolicy": { "$ref": "#/definitions/ApprovalPolicy" },
        "idempotency": { "$ref": "#/definitions/IdempotencyPolicy" },
        "auth": { "$ref": "#/definitions/AuthHints" },
        "audit": { "$ref": "#/definitions/AuditPolicy" },
        "timeoutMs": { "type": "integer" },
        "sideEffectLevel": { "$ref": "#/definitions/SideEffectLevel" }
      }
    },
    "ServerToolManifestDocument": {
      "type": "object",
      "required": ["version", "generatedAt", "tools"],
      "properties": {
        "version": { "const": 1 },
        "generatedAt": { "type": "string", "format": "date-time" },
        "tools": { "type": "array", "items": { "$ref": "#/definitions/ServerToolManifest" } }
      }
    },
    "ServerToolResultEnvelope": {
      "type": "object",
      "description": "Wire envelope a customer-owned /chat/tool-call route must return. Either output (on success/duplicate replay) or error must be present.",
      "additionalProperties": false,
      "properties": {
        "status": { "type": "string", "enum": ["completed", "denied", "pending", "failed", "duplicate"] },
        "output": { "type": "object", "description": "Structured tool output. Required on success." },
        "error": { "type": "string", "description": "Domain-level error message surfaced to the agent." },
        "errorCode": { "type": "string", "description": "Optional ChatSdkError code; the SDK maps SERVER_TOOL_* values directly." },
        "requestId": { "type": "string" },
        "idempotencyKey": { "type": "string" },
        "duplicateDisposition": { "type": "string", "enum": ["replayed", "rejected", "conflict"] },
        "approvalPolicy": { "$ref": "#/definitions/ApprovalPolicy" }
      }
    },
    "ServerToolInvocationRequest": {
      "type": "object",
      "description": "Wire request the SDK posts to a customer-owned tool endpoint.",
      "required": ["name", "input", "context"],
      "additionalProperties": false,
      "properties": {
        "name": { "type": "string" },
        "input": { "type": "object" },
        "idempotencyKey": { "type": "string" },
        "context": {
          "type": "object",
          "required": ["sessionId", "turnIndex", "toolCallId"],
          "additionalProperties": false,
          "properties": {
            "sessionId": { "type": "string" },
            "turnIndex": { "type": "integer", "minimum": 0 },
            "toolCallId": { "type": "string" },
            "idempotencyKey": { "type": "string" }
          }
        }
      }
    }
  }
}
