# Game R&D & System Deconstruction Knowledge Base — Agent API Guide

Base URL: `https://knowledge.traindata.ai`

## 1. Authentication
All `/api/v1/*` endpoints require an API key.
- Local machine key path (if running on the same host): `~/.config/game-knowledge/api-key` (format: `KNOWLEDGE_API_KEY=gk_live_...`)
- Pass the key in HTTP request header:
  ```http
  Authorization: Bearer <KNOWLEDGE_API_KEY>
  ```
  (Alternatively: `X-API-Key: <KNOWLEDGE_API_KEY>`)

---

## 2. Recommended Agent Workflow (When to Call Which Endpoint)

1. **Step 1 — Discover available entities (`GET /api/v1/catalog`)**:
   Always call this first if you are unsure of exact product names, genre names, general design disciplines, or Unity engineering topics in the database.
2. **Step 2A — Exact target lookup (`GET /api/v1/knowledge`) [0 LLM cost, <100ms]**:
   When you need the complete deconstruction of a **specific game** (e.g., `Castle Busters`, `英勇之地`, `Royal Match`, `Clash Royale`), **vertical genre** (e.g., `二合`, `三消`, `混合休闲`), **general design discipline**, or **Unity tech topic**:
   - Returns **pre-distilled 4–5 layer system mind-map** + **structured GDD-grade multimodal dossier (with tables, formulas, lossless C#/HLSL code blocks, and R2 screenshots)**.
   - Prefer `format=markdown` for clean LLM context consumption.
3. **Step 2B — Cross-database thematic retrieval / comparison (`POST /api/v1/search`)**:
   When answering open-ended cross-game / cross-discipline questions (e.g., *"Compare 1v1 PvP onboarding protection and pinch points across Castle Busters and Clash Royale"*):
   - Use `"mode": "chunks"` (fast, ~300ms, 0 synthesis cost) if **you** (the calling Agent) want raw high-density Markdown evidence sections to analyze yourself.
   - Use `"mode": "synthesize"` (~5-10s) if you want the knowledge base's internal Gemini 4 advisor to return a deduplicated, cited (`[1][2]`) report.

---

## 3. Endpoint Reference

### 3.1 `GET /api/v1/catalog` — List Full Knowledge Base Catalog
**Example:**
```bash
KEY=$(grep '^KNOWLEDGE_API_KEY=' ~/.config/game-knowledge/api-key | cut -d= -f2-)
curl -s -H "Authorization: Bearer $KEY" "https://knowledge.traindata.ai/api/v1/catalog"
```
**Response JSON:**
```json
{
  "ok": true,
  "catalog": {
    "products": [{"name": "Castle Busters", "node_id": "product:Castle Busters", "maturity_tier": "blueprint", "node_count": 150, "pillars": ["core_gameplay", "monetization", "..."]}],
    "genres": [{"name": "二合", "node_id": "genre:二合", "branch_count": 16, "section_count": 10}],
    "general_design": [{"name": "商业化与定价心理学", "node_id": "design_system:商业化与定价心理学", "section_count": 8}],
    "unity_tech": [{"name": "合批与渲染优化(SRP Batcher/DrawCall)", "node_id": "tech:合批与渲染优化(SRP Batcher/DrawCall)", "section_count": 8}]
  }
}
```

---

### 3.2 `GET /api/v1/knowledge` — Fetch Distilled Mind-Map & Dossier
**Query Parameters:**
- `type` (required): `product` | `genre` | `design_system` | `tech`
- `name` (required): Exact entity name from catalog (URL-encoded, e.g., `Castle%20Busters`, `%E8%8B%B1%E5%8B%87%E4%B9%8B%E5%9C%B0`)
- `section` (optional): Filter by subsystem keyword or pillar (e.g., `商业化`, `monetization`, `核心玩法`)
- `format` (optional): `markdown` (recommended for LLM agents) | `json` (default)

**Example:**
```bash
KEY=$(grep '^KNOWLEDGE_API_KEY=' ~/.config/game-knowledge/api-key | cut -d= -f2-)
curl -s -H "Authorization: Bearer $KEY" \
  "https://knowledge.traindata.ai/api/v1/knowledge?type=product&name=Castle%20Busters&format=markdown"
```

---

### 3.3 `POST /api/v1/search` — Hybrid Graph-RAG Search (768d Vector + FTS5 BM25 + RRF)
**JSON Body:**
- `query` (string, required): Natural language or keyword query (supports Chinese & English cross-lingual retrieval).
- `mode` (string, optional): `"chunks"` (recommended for autonomous AI agents — returns top-K clean Markdown sections directly) | `"synthesize"` (default — returns Gemini-synthesized report + `citations`).
- `domain` (string, optional): `""` (all domains) | `"product"` | `"genre"` | `"design_system"` | `"tech"`
- `top_k` (integer, optional): `3` to `14` (default `8`).

**Example:**
```bash
KEY=$(grep '^KNOWLEDGE_API_KEY=' ~/.config/game-knowledge/api-key | cut -d= -f2-)
curl -s -X POST "https://knowledge.traindata.ai/api/v1/search" \
  -H "Authorization: Bearer $KEY" \
  -H "Content-Type: application/json" \
  -d '{"query": "1v1 PvP 新手匹配保护与付费挤压点设计", "mode": "chunks", "top_k": 8}'
```

---

## 4. MCP Server Integration (Optional)
If the calling agent supports Model Context Protocol (stdio):
```json
{
  "mcpServers": {
    "game-knowledge-db": {
      "command": "python3",
      "args": ["/home/sam_lee/game-knowledge-database/tools/mcp_server.py"],
      "env": {
        "KNOWLEDGE_API_KEY": "<YOUR_KEY_IF_NOT_IN_HOME_CONFIG>"
      }
    }
  }
}
```
