{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/adeelahmad/lense/blob/main/docs/schemas/chat-export.schema.json",
  "title": "Lens conversation export",
  "description": "GET /api/v1/chats/{id}/export.json (docs/assistant.md, Copy and export): a conversation as a tree of OpenAI chat messages with Dublin Core terms, or with format=messages one version as {\"messages\": [...]}.",
  "oneOf": [{ "$ref": "#/$defs/document" }, { "$ref": "#/$defs/messages" }],
  "$defs": {
    "document": {
      "type": "object",
      "required": ["@context", "format", "dc:title", "dc:identifier", "dc:format", "id", "title", "current_node", "mapping"],
      "properties": {
        "@context": {
          "type": "object",
          "required": ["dc"],
          "properties": { "dc": { "const": "http://purl.org/dc/terms/" } }
        },
        "format": { "const": "lens.chat-export/1" },
        "dc:title": { "type": "string" },
        "dc:created": { "type": ["string", "null"], "description": "when the conversation started (ISO 8601, UTC)" },
        "dc:modified": { "type": ["string", "null"], "description": "when it was last added to" },
        "dc:creator": { "type": "string", "description": "the person whose conversation it is" },
        "dc:identifier": { "type": "string", "description": "its address in Lens" },
        "dc:format": { "const": "application/json" },
        "dc:publisher": { "type": "string", "description": "the Lens and version that exported it" },
        "id": { "type": "string" },
        "title": { "type": "string" },
        "scope": { "type": "object", "description": "what the conversation draws on (namespaces, recordings, collections, speakers, from, to)" },
        "model": { "type": ["string", "null"], "description": "the model picked for it; null: the configured one" },
        "exported_at": { "type": "string" },
        "current_node": { "type": ["string", "null"], "description": "the last message of the version showing" },
        "mapping": {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/node" }
        }
      }
    },
    "node": {
      "type": "object",
      "required": ["id", "parent", "children", "message", "lens"],
      "properties": {
        "id": { "type": "string" },
        "parent": { "type": ["string", "null"], "description": "the message it follows, among those exported" },
        "children": { "type": "array", "items": { "type": "string" }, "description": "the messages that follow it: more than one where there are versions" },
        "created_at": { "type": ["string", "null"] },
        "message": { "$ref": "#/$defs/chatMessage" },
        "tool_messages": {
          "type": "array",
          "description": "an answer's tool calls, before it: an assistant message calling them, then a tool message for each carrying its summary (raw results aren't kept)",
          "items": { "$ref": "#/$defs/openaiMessage" }
        },
        "lens": { "$ref": "#/$defs/lens" }
      }
    },
    "chatMessage": {
      "type": "object",
      "required": ["role", "content"],
      "properties": {
        "role": { "enum": ["user", "assistant"] },
        "content": { "type": "string", "description": "the text; an answer cites with [n], the n of lens.citations" }
      }
    },
    "openaiMessage": {
      "type": "object",
      "required": ["role"],
      "properties": {
        "role": { "enum": ["system", "user", "assistant", "tool"] },
        "content": { "type": ["string", "null"] },
        "tool_call_id": { "type": "string" },
        "tool_calls": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["id", "type", "function"],
            "properties": {
              "id": { "type": "string" },
              "type": { "const": "function" },
              "function": {
                "type": "object",
                "required": ["name", "arguments"],
                "properties": {
                  "name": { "type": "string" },
                  "arguments": { "type": "string", "description": "the arguments, as JSON text" }
                }
              }
            }
          }
        }
      },
      "allOf": [
        { "if": { "properties": { "role": { "const": "tool" } } }, "then": { "required": ["tool_call_id", "content"] } }
      ]
    },
    "citation": {
      "type": "object",
      "required": ["n", "source", "text"],
      "properties": {
        "n": { "type": "integer" },
        "source": { "enum": ["said", "page", "screen", "memory", "web", "docs", "server"], "description": "where it is: said in a recording, on a document's page, shown on screen, from memory, a web page, Lens's docs, or this Lens server's facts" },
        "url": { "type": ["string", "null"], "description": "the web page, the docs section, this server, or the asset's page in Lens at that moment" },
        "recording_id": { "type": "integer" },
        "title": { "type": ["string", "null"] },
        "namespace": { "type": ["string", "null"] },
        "recorded_at": { "type": ["string", "null"] },
        "time": { "type": ["string", "null"] },
        "page": { "type": ["integer", "null"] },
        "speaker": { "type": ["string", "null"] },
        "site": { "type": ["string", "null"] },
        "fetched_at": { "type": ["string", "null"] },
        "text": { "type": "string" },
        "used": { "type": ["boolean", "null"], "description": "cited in the answer" }
      }
    },
    "lens": {
      "type": "object",
      "description": "what only Lens knows of a message",
      "properties": {
        "model": { "type": "string" },
        "stopped": { "type": "boolean" },
        "notice": { "type": "string" },
        "error": { "type": "string" },
        "check": { "type": "object" },
        "context": { "type": "object", "description": "a question asked from a page: the page and any highlighted text" },
        "versions": {
          "type": "object",
          "required": ["index", "count", "ids"],
          "properties": { "index": { "type": "integer" }, "count": { "type": "integer" }, "ids": { "type": "array", "items": { "type": "integer" } } }
        },
        "attachments": {
          "type": "array",
          "items": { "type": "object", "properties": { "filename": { "type": "string" }, "size": { "type": "integer" }, "recording": { "type": "integer" } } }
        },
        "citations": { "type": "array", "items": { "$ref": "#/$defs/citation" } },
        "meta": { "type": "object", "description": "what produced it (GET /chats/{cid}/messages/{mid} meta): model, provider, tools, scope, limits, use, outcome, tokens, cost, time" }
      }
    },
    "messages": {
      "type": "object",
      "required": ["messages"],
      "additionalProperties": false,
      "properties": {
        "messages": { "type": "array", "items": { "$ref": "#/$defs/openaiMessage" } }
      }
    }
  }
}
