{
  "openapi": "3.0.3",
  "info": {
    "title": "Knowledge Sea Public API",
    "description": "Public, read-only, provider-independent API for retrieving official EB2026 Sri Lankan Efficiency Bar examination source material from Knowledge Sea. No registration, no API key, no model inference required.",
    "version": "1.0.0",
    "contact": {
      "name": "Telegram365 Books",
      "url": "https://tech.telegram365books.stream/"
    }
  },
  "servers": [
    {
      "url": "https://api.telegram365books.stream",
      "description": "Public Knowledge Sea API (via Cloudflare Tunnel)"
    },
    {
      "url": "http://127.0.0.1:8093",
      "description": "Local development (loopback only)"
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "summary": "Service health",
        "description": "Returns service status, version, and corpus availability. No authentication required.",
        "operationId": "getHealth",
        "tags": ["system"],
        "responses": {
          "200": {
            "description": "Service status",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {"type": "boolean"},
                    "service": {"type": "string"},
                    "version": {"type": "string"},
                    "corpus_available": {"type": "boolean"},
                    "corpus_error": {"type": "string"},
                    "uptime_seconds": {"type": "number"}
                  }
                }
              }
            }
          },
          "503": {
            "description": "Corpus unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          }
        }
      }
    },
    "/openapi.json": {
      "get": {
        "summary": "OpenAPI specification",
        "description": "Returns this OpenAPI 3.0 document.",
        "operationId": "getOpenAPI",
        "tags": ["system"],
        "responses": {
          "200": {
            "description": "OpenAPI specification",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/v1/topics": {
      "get": {
        "summary": "Discover available topics",
        "description": "Returns the topic router: exam topics with weights, keywords, and clause counts. Use this to locate the subject area before searching for clauses.",
        "operationId": "listTopics",
        "tags": ["retrieval"],
        "responses": {
          "200": {
            "description": "List of topics",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "topics": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {"type": "string"},
                          "label": {"type": "string"},
                          "exam_weight": {"type": "integer"},
                          "keywords": {"type": "array", "items": {"type": "string"}},
                          "clause_count": {"type": "integer"}
                        },
                        "required": ["id"]
                      }
                    }
                  },
                  "required": ["topics"]
                }
              }
            }
          },
          "429": {
            "description": "Rate limited",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "502": {
            "description": "Retrieval source unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          }
        }
      }
    },
    "/v1/topics/{id}": {
      "get": {
        "summary": "Retrieve a topic's guidance",
        "description": "Returns one routing topic's clause IDs by topic ID. Use the topic ID from /v1/topics.",
        "operationId": "getTopic",
        "tags": ["retrieval"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {"type": "string"},
            "description": "Topic ID (e.g. 'procurement')"
          }
        ],
        "responses": {
          "200": {
            "description": "Topic detail",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {"type": "string"},
                    "label": {"type": "string"},
                    "keywords": {"type": "array", "items": {"type": "string"}},
                    "clause_ids": {"type": "array", "items": {"type": "string"}}
                  },
                  "required": ["id"]
                }
              }
            }
          },
          "404": {
            "description": "Topic not found",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "429": {
            "description": "Rate limited",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "502": {
            "description": "Retrieval source unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          }
        }
      }
    },
    "/v1/search": {
      "get": {
        "summary": "Full-text search",
        "description": "Searches the full English text of the EB2026 books (all clauses) by keywords and returns the closest clauses with snippets. Deterministic keyword ranking, no embeddings. Search the act, the duty, the sanction and the procedure as separate queries.",
        "operationId": "searchText",
        "tags": ["retrieval"],
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "required": true,
            "schema": {"type": "string", "minLength": 1},
            "description": "Search query (keywords)"
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {"type": "integer", "minimum": 1, "maximum": 15, "default": 8},
            "description": "Maximum number of clauses to return (1-15)"
          }
        ],
        "responses": {
          "200": {
            "description": "Search results",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "query": {"type": "string"},
                    "hits": {"type": "integer"},
                    "clauses": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "clause_id": {"type": "string"},
                          "book": {"type": "string"},
                          "part_title": {"type": "string"},
                          "heading": {"type": "string"},
                          "snippet": {"type": "string"}
                        },
                        "required": ["clause_id"]
                      }
                    }
                  },
                  "required": ["query", "hits", "clauses"]
                }
              }
            }
          },
          "400": {
            "description": "Missing or invalid query",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "429": {
            "description": "Search rate limit exceeded",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "502": {
            "description": "Retrieval source unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "503": {
            "description": "Corpus unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          }
        }
      }
    },
    "/v1/clause/{id}": {
      "get": {
        "summary": "Retrieve an exact clause",
        "description": "Fetches one clause in full: content, part title, source URL, canonical language, and cited/cited-by relationships.",
        "operationId": "getClause",
        "tags": ["retrieval"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {"type": "string", "pattern": "^[a-z0-9\\-]{2,60}$"},
            "description": "Clause ID (e.g. 'fr-0003', 'ec1-c15', 'pog-gp-0169')"
          }
        ],
        "responses": {
          "200": {
            "description": "Clause content",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "clause_id": {"type": "string"},
                    "book": {"type": "string"},
                    "canonical_lang": {"type": "string"},
                    "authoritative": {"type": "boolean"},
                    "part_title": {"type": "string"},
                    "heading": {"type": "string"},
                    "content": {"type": "string"},
                    "source_url": {"type": "string"},
                    "cites": {"type": "array", "items": {"type": "string"}},
                    "cited_by": {"type": "array", "items": {"type": "string"}}
                  },
                  "required": ["clause_id"]
                }
              }
            }
          },
          "400": {
            "description": "Invalid clause ID",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "404": {
            "description": "Clause not found",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "429": {
            "description": "Rate limited",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "502": {
            "description": "Retrieval source unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          }
        }
      }
    },
    "/v1/quote-check": {
      "post": {
        "summary": "Verify a quotation",
        "description": "Verifies that a quotation is verbatim inside a clause. Call this before publishing any quotation from the corpus. Returns verified true/false; a caveat means the clause is authoritative in another language and the English text is a translation.",
        "operationId": "quoteCheck",
        "tags": ["retrieval"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "clause_id": {"type": "string"},
                  "quote": {"type": "string", "minLength": 1}
                },
                "required": ["clause_id", "quote"]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Verification result",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "clause_id": {"type": "string"},
                    "verified": {"type": "boolean"},
                    "language": {"type": "string"},
                    "canonical_lang": {"type": "string"},
                    "caveat": {"type": "boolean"},
                    "label": {"type": "string"},
                    "note": {"type": "string"},
                    "error": {"type": "string"}
                  },
                  "required": ["clause_id", "verified"]
                }
              }
            }
          },
          "400": {
            "description": "Missing or invalid parameters",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "404": {
            "description": "Clause not found",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "429": {
            "description": "Rate limited",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "502": {
            "description": "Retrieval source unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          }
        }
      }
    },
    "/v1/related/{id}": {
      "get": {
        "summary": "List related clauses",
        "description": "Lists the clause IDs a clause cites and the IDs that cite it. Useful for following cross-references between provisions.",
        "operationId": "getRelatedClauses",
        "tags": ["retrieval"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {"type": "string"},
            "description": "Clause ID (e.g. 'fr-156')"
          }
        ],
        "responses": {
          "200": {
            "description": "Related clause IDs",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "clause_id": {"type": "string"},
                    "cites": {"type": "array", "items": {"type": "string"}},
                    "cited_by": {"type": "array", "items": {"type": "string"}}
                  },
                  "required": ["clause_id"]
                }
              }
            }
          },
          "404": {
            "description": "Clause not found",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "429": {
            "description": "Rate limited",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "502": {
            "description": "Retrieval source unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          }
        }
      }
    },
    "/v1/tools/{name}": {
      "post": {
        "summary": "Execute a retrieval tool",
        "description": "Generic tool execution endpoint. Accepts one of the eight known tool names with validated arguments. Returns the tool's JSON result directly.",
        "operationId": "executeTool",
        "tags": ["retrieval"],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "enum": ["topic_index", "topic", "find_in_map", "read_map", "get_clause", "quote_check", "related_clauses", "search_text"]
            },
            "description": "Tool name"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "arguments": {
                    "type": "object",
                    "description": "Tool-specific arguments (see OpenAPI schemas for individual tools)"
                  }
                },
                "required": ["arguments"]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Tool result",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "400": {
            "description": "Invalid tool name or arguments",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "404": {
            "description": "Tool not found or result not found",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "429": {
            "description": "Rate limited",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          },
          "502": {
            "description": "Retrieval source unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {"type": "string"},
                    "detail": {"type": "string"}
                  },
                  "required": ["error"]
                }
              }
            }
          }
        }
      }
    }
  },
  "tags": [
    {"name": "system", "description": "Service health and specification"},
    {"name": "retrieval", "description": "Knowledge base retrieval operations"}
  ],
  "components": {
    "securitySchemes": {}
  },
  "security": []
}
