{
  "openapi": "3.1.0",
  "info": {
    "title": "Trumis API",
    "version": "1.0.0",
    "description": "Complaint-intake conversation API. The client sends the visible transcript each turn; Trumis encrypts and retains the complete request/response intake record for up to 24 months. Human-readable contract: https://trumis.com.au/developers/ — changelog: https://trumis.com.au/developers/changelog/",
    "contact": { "name": "Trumis", "email": "info@trumis.com.au", "url": "https://trumis.com.au/developers/" },
    "termsOfService": "https://trumis.com.au/terms/"
  },
  "servers": [{ "url": "https://trumis.com.au" }],
  "security": [{}, { "partnerKey": [] }],
  "paths": {
    "/api/v1/companion": {
      "post": {
        "operationId": "companionTurn",
        "summary": "Run one conversation turn (also served at /api/companion)",
        "description": "Send the visible transcript and the previous turn's facts; receive the agent's next move. When the person confirms their claims, the response carries the finished complaint. Rate limits: 30 req/min per address unauthenticated, 120 req/min per licensed key.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": { "schema": { "$ref": "#/components/schemas/CompanionRequest" } }
          }
        },
        "responses": {
          "200": {
            "description": "The next turn.",
            "headers": {
              "X-Trumis-Request-Id": { "$ref": "#/components/headers/RequestId" },
              "X-RateLimit-Limit": { "$ref": "#/components/headers/RateLimit" },
              "X-RateLimit-Remaining": { "$ref": "#/components/headers/RateRemaining" }
            },
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/CompanionTurn" } }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidJson" },
          "401": { "$ref": "#/components/responses/InvalidPartnerKey" },
          "405": { "$ref": "#/components/responses/MethodNotAllowed" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "502": { "$ref": "#/components/responses/CompanionFailed" }
        }
      }
    },
    "/api/v1/register": {
      "get": {
        "operationId": "executiveRegister",
        "summary": "Read the licensed partner executive register",
        "description": "Returns the authenticated partner's category-only complaint register. The store retains day granularity; no complaint narrative or person identifiers are returned.",
        "security": [{ "partnerKey": [] }],
        "parameters": [
          {
            "name": "window_days",
            "in": "query",
            "schema": { "type": "integer", "minimum": 1, "maximum": 90, "default": 90 }
          }
        ],
        "responses": {
          "200": {
            "description": "Partner-scoped executive register.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Register" } } }
          },
          "401": { "$ref": "#/components/responses/InvalidPartnerKey" },
          "405": { "$ref": "#/components/responses/MethodNotAllowed" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/RegisterUnavailable" }
        }
      }
    },
    "/api/v1/register-event": {
      "post": {
        "operationId": "registerLifecycleEvent",
        "summary": "Confirm lodgement or update a complaint lifecycle event",
        "description": "Consumes the short-lived, content-free token returned in partner mode. The first `lodged` event writes the anonymous signal and its initial sent event; subsequent events are day-granular and idempotent.",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/RegisterEventRequest" } } }
        },
        "responses": {
          "202": { "description": "Event accepted." },
          "400": { "$ref": "#/components/responses/InvalidJson" },
          "401": { "$ref": "#/components/responses/InvalidLodgementToken" },
          "405": { "$ref": "#/components/responses/MethodNotAllowed" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/RegisterUnavailable" }
        }
      }
    },
    "/api/v1/evidence": {
      "post": {
        "operationId": "reconstructEvidence",
        "summary": "Reconstruct correspondence from on-device OCR lines (also /api/evidence)",
        "description": "OCR runs on the person's device; only text lines with normalised geometry are sent — images never leave the device. Returns the reconstructed transcript and proposed facts for the person to review. Limits: 25 screenshots, 500 lines each, 400 chars per line.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": { "schema": { "$ref": "#/components/schemas/EvidenceRequest" } }
          }
        },
        "responses": {
          "200": {
            "description": "Reconstructed correspondence.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/EvidenceResult" } }
            }
          },
          "400": { "$ref": "#/components/responses/NoScreenshots" },
          "405": { "$ref": "#/components/responses/MethodNotAllowed" },
          "422": { "$ref": "#/components/responses/NothingRecognised" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/api/v1/health": {
      "get": {
        "operationId": "health",
        "summary": "Liveness (also /api/health)",
        "responses": {
          "200": {
            "description": "Service is up.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Health" } }
            }
          },
          "503": {
            "description": "Service degraded — knowledge base unavailable.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Health" } }
            }
          }
        }
      }
    },
    "/api/embed-config": {
      "get": {
        "operationId": "embedConfig",
        "summary": "Licensed embed configuration by public partner id",
        "parameters": [
          {
            "name": "pid",
            "in": "query",
            "required": true,
            "schema": { "type": "string" },
            "description": "The partner id from your licence (the embed's data-partner-id)."
          }
        ],
        "responses": {
          "200": {
            "description": "Publishable partner configuration — never includes keys.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/EmbedConfig" } }
            }
          },
          "404": { "$ref": "#/components/responses/UnknownPartner" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/api/checkout": {
      "post": {
        "operationId": "createCheckout",
        "summary": "Create a Stripe Checkout session for the formal letter",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": { "matter": { "type": "string", "maxLength": 64 } }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Hosted payment page URL, or {free:true} when no fee is configured.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url": { "type": "string", "format": "uri" },
                    "free": { "type": "boolean" }
                  }
                }
              }
            }
          },
          "502": {
            "description": "Stripe session creation failed.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "partnerKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Trumis-Key",
        "description": "Licensed partner key (server-side use only). Resolves the organisation identity from the registry and raises rate limits."
      }
    },
    "headers": {
      "RequestId": {
        "description": "Correlation id — quote it in support requests.",
        "schema": { "type": "string", "format": "uuid" }
      },
      "RateLimit": { "description": "Requests allowed per rolling minute.", "schema": { "type": "integer" } },
      "RateRemaining": { "description": "Requests remaining in the window.", "schema": { "type": "integer" } }
    },
    "responses": {
      "InvalidJson": {
        "description": "Body is not valid JSON.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "InvalidPartnerKey": {
        "description": "X-Trumis-Key does not match a configured partner.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "MethodNotAllowed": {
        "description": "Wrong HTTP method — see the Allow header.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "RateLimited": {
        "description": "Fair-use budget spent — honour Retry-After.",
        "headers": { "Retry-After": { "schema": { "type": "integer" }, "description": "Seconds until capacity returns." } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "CompanionFailed": {
        "description": "Upstream hiccup — safe to retry; body carries request_id.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "RegisterUnavailable": {
        "description": "The executive register or signal store is temporarily unavailable.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "InvalidLodgementToken": {
        "description": "The signed lodgement token is invalid or expired.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "NoScreenshots": {
        "description": "No OCR lines supplied.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "NothingRecognised": {
        "description": "The OCR lines yielded no reconstructable correspondence.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "UnknownPartner": {
        "description": "No partner registered under that id.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "string",
            "enum": ["invalid_json", "invalid_partner_key", "method_not_allowed", "rate_limited", "companion_failed", "no_screenshots", "nothing_recognised", "unknown_partner", "checkout_failed", "register_unavailable", "invalid_event", "invalid_lodgement_token"]
          },
          "message": { "type": "string" },
          "request_id": { "type": "string", "format": "uuid" }
        }
      },
      "Message": {
        "type": "object",
        "required": ["role", "content"],
        "properties": {
          "role": { "type": "string", "enum": ["user", "assistant"] },
          "content": { "type": "string", "maxLength": 8000 }
        }
      },
      "CompanionRequest": {
        "type": "object",
        "required": ["history"],
        "properties": {
          "history": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Message" },
            "maxItems": 60,
            "description": "The whole visible transcript, oldest first."
          },
          "known_facts": {
            "type": "object",
            "additionalProperties": true,
            "description": "The previous response's `facts`, echoed back verbatim. Set a fact to false to retract it."
          },
          "client_done": { "type": "boolean", "description": "The person has nothing more to add." },
          "partner": {
            "type": "object",
            "required": ["name"],
            "properties": { "name": { "type": "string", "maxLength": 80 } },
            "description": "Organisation intake (pilot tier without a key; licensed keys override the name from the registry)."
          },
          "evidence": { "$ref": "#/components/schemas/EvidenceTranscript" },
          "lang": { "type": "string", "enum": ["zh", "vi", "ar", "el", "en"], "description": "Opening-language hint; the person's own words always win." },
          "formal_requested": { "type": "boolean" },
          "reference_id": { "type": "string", "pattern": "^TRM-[A-Z0-9]{4}-[A-Z0-9]{4}$", "description": "Optional client-held reference for partner-mode lodgement matching." },
          "payment_ref": { "type": "string", "pattern": "^cs_[a-zA-Z0-9_]+$", "description": "Stripe Checkout session id, verified with Stripe when a fee is configured." }
        }
      },
      "CompanionTurn": {
        "type": "object",
        "required": ["reply", "stage", "method", "facts", "grounds", "urgency", "confirm", "complaint", "formal_offer", "disclaimer"],
        "properties": {
          "reply": { "type": "string", "description": "The agent's next message, in the person's language." },
          "stage": {
            "type": "string",
            "enum": ["listening", "probing", "confirming", "grounds_ready", "account_ready", "no_grounds_close"]
          },
          "method": { "type": "string", "enum": ["rules", "rules+llm"] },
          "facts": { "type": "object", "additionalProperties": true, "description": "Echo back next turn as known_facts." },
          "grounds": {
            "type": "object",
            "properties": {
              "sufficient": { "type": "boolean" },
              "passed_arguments": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": { "id": { "type": "string" }, "summary": { "type": "string" } }
                }
              }
            }
          },
          "urgency": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "level": { "type": "string", "enum": ["critical", "high", "medium"] },
                "message": { "type": "string" },
                "action": { "type": "string" }
              }
            }
          },
          "confirm": {
            "type": ["object", "null"],
            "description": "Present at stage `confirming` — render claims as pre-ticked options.",
            "properties": {
              "intro": { "type": "string" },
              "claims": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "key": { "type": "string" },
                    "label": { "type": "string" },
                    "quote": { "type": "string" },
                    "theirs": { "type": "boolean" }
                  }
                }
              },
              "about": { "type": "string" },
              "subject": { "type": "string" },
              "question": { "type": "string" },
              "ui": {
                "type": "object",
                "properties": {
                  "tick_prompt": { "type": "string" },
                  "unsure_note": { "type": "string" },
                  "confirm_yes": { "type": "string" },
                  "you_said": { "type": "string" },
                  "their_words": { "type": "string" }
                }
              }
            }
          },
          "complaint": {
            "type": ["object", "null"],
            "description": "The finished complaint, once confirmed.",
            "properties": {
              "document": { "type": "object", "description": "Structured document: title, sections, outcomes sought, evidence list, disclaimer." },
              "text": { "type": "string", "description": "The complete complaint as ready-to-send text." },
              "routing": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "order": { "type": "integer" },
                    "recipient": { "type": "string" },
                    "channel": { "type": "string", "enum": ["email", "portal", "phone", "post"] },
                    "subject": { "type": "string" },
                    "url": { "type": "string" },
                    "to_email": { "type": "string" },
                    "note": { "type": "string" },
                    "response_days": { "type": "integer" },
                    "when": { "type": "string" }
                  }
                }
              },
              "receiver": {
                "type": ["object", "null"],
                "properties": { "id": { "type": "string" }, "name": { "type": "string" } }
              }
            }
          },
          "formal_offer": {
            "type": ["object", "null"],
            "properties": { "text": { "type": "string" }, "fee": { "type": "string" } }
          },
          "disclaimer": { "type": "string" },
          "partner_tier": { "type": "string", "enum": ["licensed", "pilot"], "description": "Partner mode only." }
          ,"lodgement_token": { "type": "string", "description": "Partner mode only; signed, content-free, expires after 90 days." }
          ,"register_reference": { "type": "string", "pattern": "^TRM-[A-Z0-9]{4}-[A-Z0-9]{4}$", "description": "Stable reference stamped into the partner-facing complaint." }
        }
      },
      "RegisterEventRequest": {
        "type": "object",
        "required": ["token", "event"],
        "properties": {
          "token": { "type": "string" },
          "event": { "type": "string", "enum": ["lodged", "sent", "response_received", "deadline_passed", "escalated", "resolved", "withdrawn"] }
        }
      },
      "Register": {
        "type": "object",
        "required": ["partner", "as_at_utc", "last_refresh_utc", "window_days", "summary", "entries"],
        "properties": {
          "partner": { "type": "object", "properties": { "id": { "type": "string" }, "name": { "type": "string" } } },
          "as_at_utc": { "type": "string", "format": "date-time" },
          "last_refresh_utc": { "type": "string", "format": "date-time" },
          "window_days": { "type": "integer" },
          "summary": { "$ref": "#/components/schemas/RegisterSummary" },
          "entries": { "type": "array", "items": { "$ref": "#/components/schemas/RegisterEntry" } }
        }
      },
      "RegisterEntry": {
        "type": "object",
        "properties": {
          "reference_id": { "type": "string", "pattern": "^TRM-[A-Z0-9]{4}-[A-Z0-9]{4}$" },
          "received_on": { "type": "string", "format": "date" },
          "kind": { "type": "string", "enum": ["account", "formal"] },
          "dispute_type": { "type": "string" },
          "industry_code": { "type": ["string", "null"] },
          "org_id": { "type": ["string", "null"] },
          "grounds_sufficient": { "type": "boolean" },
          "grounds": { "type": "array", "items": { "type": "string" } },
          "urgency": { "type": "string", "enum": ["none", "medium", "high", "critical"] },
          "evidence_direct": { "type": "integer" },
          "evidence_documentable": { "type": "integer" },
          "evidence_asserted": { "type": "integer" },
          "amount_band": { "type": "string" },
          "language": { "type": "string" },
          "risk_tier": { "type": "string", "enum": ["critical", "elevated", "watch"] },
          "risk_factors": { "type": "array", "items": { "type": "string" } },
          "tier_rule_version": { "type": "string" },
          "pattern_id": { "type": ["string", "null"] },
          "pattern_count_30d": { "type": "integer" },
          "pattern_direction": { "type": ["string", "null"], "enum": ["rising", "stable", "falling", "new", null] },
          "modelled_external_exposure_aud": { "type": ["number", "null"] },
          "exposure_basis": { "type": "string" },
          "exposure_assumption_version": { "type": ["string", "null"] },
          "edr_scheme": { "type": ["string", "null"] },
          "case_system_ref": { "type": ["string", "null"] },
          "case_deep_link": { "type": ["string", "null"], "format": "uri-reference" },
          "case_status_snapshot": { "type": "string" },
          "case_status_as_at": { "type": ["string", "null"], "format": "date" },
          "events": { "type": "object", "additionalProperties": { "type": "string", "format": "date" } }
        }
      },
      "RegisterSummary": {
        "type": "object",
        "properties": {
          "total": { "type": "integer" },
          "total_received": { "type": "integer" },
          "open_count": { "type": "integer" },
          "tiers": { "type": "object", "additionalProperties": { "type": "integer" } },
          "open_tiers": { "type": "object", "additionalProperties": { "type": "integer" } },
          "critical_open_count": { "type": "integer" },
          "elevated_open_count": { "type": "integer" },
          "watch_open_count": { "type": "integer" },
          "lifecycle": { "type": "object", "additionalProperties": { "type": "integer" } },
          "modelled_open_exposure_aud": { "type": ["number", "null"] },
          "critical_exposure_aud": { "type": ["number", "null"] },
          "unknown_status_count": { "type": "integer" },
          "tier_increase_7d": { "type": "integer" },
          "repeat_pattern_count": { "type": "integer" },
          "top_pattern_codes": { "type": "array", "items": { "type": "string" } },
          "oldest_open_received_on": { "type": ["string", "null"], "format": "date" },
          "headline_basis": { "type": "string", "enum": ["case_system_status", "partner_snapshot", "unconfirmed"] }
        }
      },
      "EvidenceTranscript": {
        "type": "object",
        "properties": {
          "source": { "type": "string" },
          "transcript": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["speaker", "text"],
              "properties": {
                "speaker": { "type": "string", "enum": ["person", "other", "system"] },
                "name": { "type": "string" },
                "time": { "type": "string" },
                "text": { "type": "string" }
              }
            }
          }
        }
      },
      "EvidenceRequest": {
        "type": "object",
        "required": ["screenshots"],
        "properties": {
          "source": { "type": "string" },
          "screenshots": {
            "type": "array",
            "maxItems": 25,
            "items": {
              "type": "object",
              "properties": {
                "lines": {
                  "type": "array",
                  "maxItems": 500,
                  "items": {
                    "type": "object",
                    "required": ["text", "x", "y", "w", "h"],
                    "properties": {
                      "text": { "type": "string", "maxLength": 400 },
                      "x": { "type": "number" },
                      "y": { "type": "number" },
                      "w": { "type": "number" },
                      "h": { "type": "number" }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "EvidenceResult": {
        "type": "object",
        "properties": {
          "transcript": { "$ref": "#/components/schemas/EvidenceTranscript/properties/transcript" },
          "facts": { "type": "object", "additionalProperties": true },
          "evidence": { "type": "object", "additionalProperties": true },
          "summary": {
            "type": "object",
            "properties": {
              "messages": { "type": "integer" },
              "from": { "type": ["string", "null"] },
              "to": { "type": ["string", "null"] },
              "participants": { "type": "array", "items": { "type": "string" } }
            }
          }
        }
      },
      "Health": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "service": { "type": "string" },
          "time": { "type": "string", "format": "date-time" },
          "region": { "type": ["string", "null"] }
        }
      },
      "EmbedConfig": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "partner": {
            "type": "object",
            "properties": {
              "id": { "type": "string" },
              "name": { "type": "string" },
              "to": { "type": "string" },
              "origins": { "type": "array", "items": { "type": "string" } },
              "branded": { "type": "boolean" }
            }
          }
        }
      }
    }
  }
}
