{
 "$schema": "https://json-schema.org/draft/2020-12/schema",
 "$id": "https://maiagent.ai/schema/xams-summary-v3.schema.json",
 "title": "xAMS 兒科抗生素照會摘要 v3（Agent 判斷版）：API 回應格式",
 "description": "摘要服務 v3 回應 xAMS 的 JSON 格式定義。xAMS 依這份格式自行繪製畫面。每一筆摘要項目都附類型（type）、句子來源類型（text_origin）與來源病歷（sources，含資料時間）；外部知識另放 knowledge[]，資料缺漏另放 missing[]。本版本為示範版（schema 欄位值 xams-summary-v3.4-demo），正式版欄位如有增減會更改 schema 欄位值。日期時間一律為 ISO 8601、含時區（例如 2026-10-25T10:00:00+08:00）。 v3 在 v2 上新增：agent_review（Agent 從照會說明萃取的重點與查詢判斷）、knowledge[].requested_by（程式必查或 Agent 判斷查詢）、validation.agent_review（程式對 Agent 查詢的把關結果）。Sanford Guide 只依 Agent 的判斷查詢；Medi-Span 由程式必查，Agent 可追加。",
 "type": "object",
 "additionalProperties": false,
 "required": [
  "schema",
  "summary_id",
  "generated_at",
  "patient_ref",
  "consult_ref",
  "time_basis",
  "data_time",
  "input_summary",
  "highlights",
  "rules_version",
  "sections",
  "knowledge",
  "missing",
  "validation",
  "agent_review",
  "timing",
  "agent",
  "self_check"
 ],
 "properties": {
  "schema": {
   "description": "格式版本代號。本版固定為 xams-summary-v3.0-demo；格式有不相容的變更時會改這個值。",
   "const": "xams-summary-v3.0-demo"
  },
  "summary_id": {
   "description": "這份摘要的編號，每次產生都不同（格式：sum-<病人代號>-<產生時間毫秒>）。xAMS 保存摘要時可用來辨識版本。",
   "type": "string",
   "pattern": "^sum-.+-\\d+$"
  },
  "generated_at": {
   "description": "摘要產生時間（摘要服務的時間）。",
   "$ref": "#/$defs/dateTime"
  },
  "patient_ref": {
   "description": "病人的 FHIR 資源參照（例如 Patient/a4），與 xAMS 送來的病人資料相同。",
   "$ref": "#/$defs/fhirRef"
  },
  "consult_ref": {
   "description": "照會單的 FHIR 資源參照（例如 ServiceRequest/a4-sr1）。",
   "$ref": "#/$defs/fhirRef"
  },
  "time_basis": {
   "description": "時間基準。照會時間只當計算基準（日齡、已使用天數、資料時效等），不用來排除資料；xAMS 送進來的資料全部處理。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "consult_time"
   ],
   "properties": {
    "consult_time": {
     "description": "照會時間（時間基準）。",
     "$ref": "#/$defs/dateTime"
    }
   }
  },
  "data_time": {
   "description": "摘要所依據的資料截止時間（送入資料中最新一筆的時間）。沒有任何資料時為 null。",
   "oneOf": [
    {
     "$ref": "#/$defs/dateTime"
    },
    {
     "type": "null"
    }
   ]
  },
  "input_summary": {
   "description": "本次 xAMS 送入的資料：依種類列出筆數與查詢狀態。思邁不做時間範圍篩選，送多少處理多少。",
   "type": "array",
   "items": {
    "type": "object",
    "additionalProperties": false,
    "required": [
     "category",
     "label",
     "unit",
     "count",
     "status",
     "queried_at"
    ],
    "properties": {
     "category": {
      "description": "資料種類代號（FHIR 資源類型，Observation 依用途細分）。",
      "type": "string"
     },
     "label": {
      "description": "中文名稱。",
      "type": "string"
     },
     "unit": {
      "description": "計數單位（筆或份）。",
      "enum": [
       "筆",
       "份"
      ]
     },
     "count": {
      "description": "送入的筆數。",
      "type": "integer",
      "minimum": 0
     },
     "status": {
      "description": "xAMS 附的查詢狀態。ok＝成功；empty＝查無資料；error＝查詢失敗；not_sent＝本次未附狀態。",
      "enum": [
       "ok",
       "empty",
       "error",
       "not_sent"
      ]
     },
     "queried_at": {
      "description": "查詢時間；未附時為 null。",
      "anyOf": [
       {
        "$ref": "#/$defs/dateTime"
       },
       {
        "type": "null"
       }
      ]
     }
    }
   }
  },
  "highlights": {
   "description": "需留意：程式依規則從病歷事實挑出、影響選藥、劑量與用藥安全的風險因子（依優先順序排列），每筆寫成事實＋數值＋來源，不寫結論。列入的類型為示範設定，實際依長庚臨床確認：免疫低下（ANC < 500 為示範門檻、血小板低）、腎功能（肌酸酐、eGFR）、肝功能（ALT、AST、膽紅素）、血中濃度（以上數值類依檢驗室 H／L 旗標）、過敏紀錄、管路、抗藥菌帶菌史、早產（出生週數 < 37 週）、資料矛盾（例如同一時點兩個不同體重）。發炎指標（WBC、CRP、PCT、尿液白血球）不列，已在感染證據區。照會前超過 3 天的值，文字加註「距照會 N 天」。",
   "type": "array",
   "items": {
    "type": "object",
    "additionalProperties": false,
    "required": [
     "item",
     "text",
     "value",
     "sources",
     "rule_basis",
     "related_section"
    ],
    "properties": {
     "item": {
      "description": "項目名稱，例如 ANC、過敏、管路。",
      "type": "string"
     },
     "text": {
      "description": "顯示文字（事實＋數值＋時間）。",
      "type": "string"
     },
     "value": {
      "description": "數值或內容（照錄）。"
     },
     "sources": {
      "description": "來源。",
      "type": "array",
      "items": {
       "$ref": "#/$defs/source"
      }
     },
     "rule_basis": {
      "description": "挑選依據。general_definition＝依檢驗室旗標或通用定義（例如出生週數 < 37 週為早產）；demo_setting＝示範門檻（例如 ANC < 500），僅供參考。",
      "enum": [
       "general_definition",
       "demo_setting"
      ]
     },
     "related_section": {
      "$ref": "#/$defs/sectionKey",
      "description": "對應的摘要區塊（畫面點擊可跳到此區）。"
     },
     "flag": {
      "description": "檢驗室旗標（數值類才有）：H 或 L。",
      "type": "string"
     }
    }
   }
  },
  "rules_version": {
   "description": "院方規則（劑量用體重、資料時效、計算規則等）的版本。示範版為示範設定，待感染科確認。",
   "type": "string",
   "minLength": 1
  },
  "sections": {
   "description": "摘要本體：固定 9 個區塊，順序固定（依醫師審核順序）。",
   "type": "array",
   "prefixItems": [
    {
     "$ref": "#/$defs/section",
     "properties": {
      "section": {
       "const": "consult_overview"
      }
     }
    },
    {
     "$ref": "#/$defs/section",
     "properties": {
      "section": {
       "const": "infection_evidence"
      }
     }
    },
    {
     "$ref": "#/$defs/section",
     "properties": {
      "section": {
       "const": "microbiology"
      }
     }
    },
    {
     "$ref": "#/$defs/section",
     "properties": {
      "section": {
       "const": "medications"
      }
     }
    },
    {
     "$ref": "#/$defs/section",
     "properties": {
      "section": {
       "const": "susceptibility"
      }
     }
    },
    {
     "$ref": "#/$defs/section",
     "properties": {
      "section": {
       "const": "dose_related"
      }
     }
    },
    {
     "$ref": "#/$defs/section",
     "properties": {
      "section": {
       "const": "allergy"
      }
     }
    },
    {
     "$ref": "#/$defs/section",
     "properties": {
      "section": {
       "const": "course_route"
      }
     }
    },
    {
     "$ref": "#/$defs/section",
     "properties": {
      "section": {
       "const": "clinical_context"
      }
     }
    }
   ],
   "items": false,
   "minItems": 9
  },
  "knowledge": {
   "description": "相關知識：Medi-Span 由程式直接查詢（requested_by＝pipeline，不經 AI）；Sanford Guide 與 Agent 追加的 Medi-Span 查詢由 Agent 判斷後經 MCP 查詢（requested_by＝agent），內容一律採用程式用同樣參數重查、核對一致的結果。每筆以 related_section 指出要顯示在哪個區塊下方。依區塊順序排列。",
   "type": "array",
   "items": {
    "$ref": "#/$defs/knowledge"
   }
  },
  "missing": {
   "description": "資料缺漏清單：依 xAMS 附的每類查詢狀態、資料時效規則與知識庫查詢結果，由程式產生。沒有缺漏時為空陣列。",
   "type": "array",
   "items": {
    "$ref": "#/$defs/missingEntry"
   }
  },
  "validation": {
   "description": "程式對 AI 摘錄的檢查紀錄（逐字比對、禁用詞組）。被擋下的內容只出現在這裡，不會出現在 sections。 v3 另含 agent_review：程式對 Agent 判斷的把關（萃取逐字、參數出處、重查核對、理由禁用詞）。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "excerpts",
    "agent_text",
    "verbatim_not_checked",
    "agent_review"
   ],
   "properties": {
    "excerpts": {
     "description": "AI 摘錄的逐字比對結果。",
     "type": "object",
     "additionalProperties": false,
     "required": [
      "received",
      "accepted",
      "rejected",
      "simulated_error_count"
     ],
     "properties": {
      "received": {
       "description": "AI 回傳的摘錄句數（不含測試用錯誤）。",
       "type": "integer",
       "minimum": 0
      },
      "accepted": {
       "description": "通過逐字比對、放進臨床脈絡區塊的句數。",
       "type": "integer",
       "minimum": 0
      },
      "rejected": {
       "description": "被擋下的摘錄（含測試用錯誤，以 simulated_error 標示）。",
       "type": "array",
       "items": {
        "$ref": "#/$defs/rejectedExcerpt"
       }
      },
      "simulated_error_count": {
       "description": "測試用錯誤內容的句數（示範用、預先寫好的錯誤內容，不是 AI 產生的）。正式環境固定為 0。",
       "type": "integer",
       "minimum": 0
      }
     }
    },
    "agent_text": {
     "description": "Agent 撰寫文字（摘錄的短標題）的禁用詞組檢查。病歷原文與指引原文不檢查。",
     "type": "object",
     "additionalProperties": false,
     "required": [
      "checked",
      "blocked"
     ],
     "properties": {
      "checked": {
       "description": "檢查的 Agent 撰寫句數。",
       "type": "integer",
       "minimum": 0
      },
      "blocked": {
       "description": "含禁用詞組而被拿掉的標題。",
       "type": "array",
       "items": {
        "$ref": "#/$defs/blockedLabel"
       }
      }
     }
    },
    "verbatim_not_checked": {
     "description": "病歷原文與指引原文中含禁用詞組的句子：照錄、不做禁用詞檢查（禁用詞只查 Agent 撰寫的句子），列出供確認。",
     "type": "array",
     "items": {
      "type": "object",
      "additionalProperties": false,
      "required": [
       "origin",
       "ref",
       "section",
       "text",
       "phrases"
      ],
      "properties": {
       "origin": {
        "description": "句子來源類型。",
        "enum": [
         "ehr_verbatim",
         "guideline_verbatim"
        ]
       },
       "ref": {
        "description": "來源（病歷 FHIR 參照或指引出處）；沒有時為 null。",
        "type": [
         "string",
         "null"
        ]
       },
       "section": {
        "$ref": "#/$defs/sectionKey",
        "description": "所在區塊。"
       },
       "text": {
        "description": "句子全文。",
        "type": "string"
       },
       "phrases": {
        "description": "句子中出現的禁用詞組。",
        "type": "array",
        "items": {
         "type": "string"
        }
       }
      }
     }
    },
    "agent_review": {
     "description": "程式對 Agent 判斷的把關紀錄。",
     "type": "object",
     "additionalProperties": false,
     "required": [
      "status",
      "extractions",
      "queries",
      "reasons",
      "simulated_error_count"
     ],
     "properties": {
      "status": {
       "description": "ok＝已檢查；agent_failed＝Agent 判斷失敗，沒有可檢查的內容；not_called＝未呼叫。",
       "enum": [
        "ok",
        "agent_failed",
        "not_called"
       ]
      },
      "extractions": {
       "description": "萃取的逐字核對：原文片段必須逐字出現在照會目的或送出的結構化資料。",
       "type": "object",
       "additionalProperties": false,
       "required": [
        "received",
        "accepted",
        "rejected"
       ],
       "properties": {
        "received": {
         "type": "integer",
         "minimum": 0,
         "description": "收到的萃取項數。"
        },
        "accepted": {
         "type": "integer",
         "minimum": 0,
         "description": "通過的項數。"
        },
        "rejected": {
         "type": "array",
         "items": {
          "$ref": "#/$defs/rejectedExtraction"
         },
         "description": "被擋下的萃取。"
        }
       }
      },
      "queries": {
       "description": "Agent 每次實際查詢的核對結果。",
       "type": "array",
       "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
         "decision",
         "tool",
         "args",
         "args_ok",
         "requery",
         "match",
         "adopted",
         "problems"
        ],
        "properties": {
         "decision": {
          "type": "string",
          "description": "判斷編號。"
         },
         "tool": {
          "type": "string",
          "description": "MCP 工具。"
         },
         "args": {
          "type": "object",
          "description": "查詢參數。"
         },
         "args_ok": {
          "type": "boolean",
          "description": "參數是否都出現在病歷事實或照會原文。"
         },
         "requery": {
          "description": "程式重查：done＝已重查；timeout＝逾時；failed＝失敗；not_done＝參數不符，不重查。",
          "enum": [
           "done",
           "timeout",
           "failed",
           "not_done"
          ]
         },
         "match": {
          "description": "Agent 看到的結果與程式重查是否一致；沒有可比對的工具結果或未重查時為 null。",
          "type": [
           "boolean",
           "null"
          ]
         },
         "adopted": {
          "type": "boolean",
          "description": "是否採用（放進 knowledge[]）。"
         },
         "problems": {
          "type": "array",
          "items": {
           "type": "string"
          },
          "description": "問題說明。"
         },
         "simulated_error": {
          "const": true,
          "description": "測試用錯誤內容的示範標記。"
         }
        }
       }
      },
      "reasons": {
       "description": "判斷理由的禁用詞組檢查（理由是 Agent 撰寫）。",
       "type": "object",
       "additionalProperties": false,
       "required": [
        "checked",
        "blocked"
       ],
       "properties": {
        "checked": {
         "type": "integer",
         "minimum": 0,
         "description": "檢查的理由數。"
        },
        "blocked": {
         "type": "array",
         "description": "被擋下的理由。",
         "items": {
          "type": "object",
          "additionalProperties": false,
          "required": [
           "decision",
           "text",
           "phrases"
          ],
          "properties": {
           "decision": {
            "type": "string",
            "description": "判斷編號。"
           },
           "text": {
            "type": "string",
            "description": "被擋下的理由原文。"
           },
           "phrases": {
            "type": "array",
            "items": {
             "type": "string"
            },
            "description": "命中的禁用詞組。"
           },
           "simulated_error": {
            "const": true,
            "description": "測試用錯誤內容的示範標記（正式版不含）。"
           }
          }
         }
        }
       }
      },
      "simulated_error_count": {
       "type": "integer",
       "minimum": 0,
       "description": "測試用錯誤內容的項數（示範用；正式環境固定為 0）。"
      }
     }
    }
   }
  },
  "timing": {
   "description": "分段計時（毫秒）。AI 摘錄、Agent 判斷與知識庫查詢同時進行，總時間不是各段相加。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "agent_ms",
    "agent_mode",
    "kb_ms",
    "kb_calls",
    "service_ms",
    "total_ms",
    "note",
    "agent_calls",
    "steps",
    "judge_ms",
    "judge_verify_ms",
    "judge_mode"
   ],
   "properties": {
    "agent_ms": {
     "description": "AI 摘錄耗時（毫秒）；沒有病程紀錄、未呼叫 AI 時為 null。",
     "type": [
      "integer",
      "null"
     ],
     "minimum": 0
    },
    "agent_mode": {
     "description": "AI 摘錄的來源。maiagent＝即時呼叫 MaiAgent；recorded＝回放事先錄製的 AI 摘錄（示範用）；offline_mock＝離線假回應（測試用）；simulated＝測試用輸出，不呼叫 AI（示範用，E1／E2）；not_called＝沒有病程紀錄，未呼叫 AI；null＝AI 呼叫失敗。",
     "enum": [
      "maiagent",
      "recorded",
      "offline_mock",
      "simulated",
      "not_called",
      null
     ]
    },
    "kb_ms": {
     "description": "知識庫查詢耗時（毫秒，所有查詢同時進行的總耗時）。",
     "type": "integer",
     "minimum": 0
    },
    "kb_calls": {
     "description": "實際送出的知識庫查詢筆數。",
     "type": "integer",
     "minimum": 0
    },
    "service_ms": {
     "description": "摘要服務本身（整理、計算、驗證）的耗時（毫秒）。",
     "type": "integer",
     "minimum": 0
    },
    "total_ms": {
     "description": "總耗時（毫秒）。",
     "type": "integer",
     "minimum": 0
    },
    "note": {
     "description": "計時說明文字。",
     "type": "string"
    },
    "agent_calls": {
     "description": "AI 摘錄每份文件一次呼叫的紀錄（同時送出）。",
     "type": "array",
     "items": {
      "type": "object",
      "additionalProperties": false,
      "required": [
       "doc_ref",
       "doc_type",
       "ms",
       "ok",
       "excerpts",
       "recorded_ms",
       "error"
      ],
      "properties": {
       "doc_ref": {
        "$ref": "#/$defs/fhirRef",
        "description": "送出的文件。"
       },
       "doc_type": {
        "description": "文件類型。",
        "type": "string"
       },
       "ms": {
        "description": "這次呼叫耗時（毫秒）。",
        "type": "integer",
        "minimum": 0
       },
       "ok": {
        "description": "是否成功取得摘錄。",
        "type": "boolean"
       },
       "excerpts": {
        "description": "這份文件回傳的摘錄句數。",
        "type": "integer",
        "minimum": 0
       },
       "recorded_ms": {
        "description": "回放錄製結果時，錄製當時實測的耗時（毫秒）；即時呼叫為 null。",
        "type": [
         "integer",
         "null"
        ],
        "minimum": 0
       },
       "error": {
        "description": "這份文件 AI 摘錄失敗的原因（例如逾時）；成功時為 null。",
        "type": [
         "string",
         "null"
        ]
       }
      }
     }
    },
    "steps": {
     "description": "摘要服務內部各步驟耗時（毫秒，小數一位）。",
     "type": "object",
     "additionalProperties": false,
     "required": [
      "collect_ms",
      "compute_ms",
      "verify_ms",
      "assemble_ms"
     ],
     "properties": {
      "collect_ms": {
       "description": "收到資料、依照會時間整理",
       "type": "number",
       "minimum": 0
      },
      "compute_ms": {
       "description": "整理事實與計算值",
       "type": "number",
       "minimum": 0
      },
      "verify_ms": {
       "description": "逐字核對與禁用詞檢查",
       "type": "number",
       "minimum": 0
      },
      "assemble_ms": {
       "description": "組成回傳 JSON 與自我檢查",
       "type": "number",
       "minimum": 0
      }
     }
    },
    "judge_ms": {
     "description": "Agent 判斷耗時（毫秒，含 Agent 自己的 MCP 查詢）；未呼叫時為 null。",
     "type": [
      "integer",
      "null"
     ],
     "minimum": 0
    },
    "judge_verify_ms": {
     "description": "程式核對 Agent 查詢（參數出處、重查）的耗時（毫秒）；Agent 失敗時為 null。",
     "type": [
      "integer",
      "null"
     ],
     "minimum": 0
    },
    "judge_mode": {
     "description": "Agent 判斷的來源：maiagent、recorded、simulated（測試用輸出，不呼叫 AI）、not_called；失敗時為 null。",
     "enum": [
      "maiagent",
      "recorded",
      "simulated",
      "not_called",
      null
     ]
    }
   }
  },
  "agent": {
   "description": "AI 摘錄的呼叫資訊（模式、對話編號、錄製資訊）；沒有呼叫 AI 或呼叫失敗時為 null。",
   "oneOf": [
    {
     "type": "null"
    },
    {
     "type": "object",
     "additionalProperties": false,
     "required": [
      "mode",
      "conversation_id",
      "recorded_ms",
      "recorded_at"
     ],
     "properties": {
      "mode": {
       "description": "同 timing.agent_mode（不含 not_called）。",
       "enum": [
        "maiagent",
        "recorded",
        "offline_mock",
        "simulated"
       ]
      },
      "conversation_id": {
       "description": "MaiAgent 對話編號（即時呼叫時才有），供追查用。",
       "type": [
        "string",
        "null"
       ]
      },
      "recorded_ms": {
       "description": "回放模式下，錄製當時實測的 AI 摘錄耗時（毫秒）。",
       "type": [
        "integer",
        "null"
       ],
       "minimum": 0
      },
      "recorded_at": {
       "description": "回放模式下，錄製時間。",
       "oneOf": [
        {
         "$ref": "#/$defs/dateTime"
        },
        {
         "type": "null"
        }
       ]
      }
     }
    }
   ]
  },
  "self_check": {
   "description": "輸出自我檢查：每一項都有來源、計算值都附公式與輸入、知識都附出處與版本、Agent 撰寫句沒有禁用詞組。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "ok",
    "problems"
   ],
   "properties": {
    "ok": {
     "description": "true＝全部通過。",
     "type": "boolean"
    },
    "problems": {
     "description": "沒通過的項目說明；全部通過時為空陣列。",
     "type": "array",
     "items": {
      "type": "string"
     }
    }
   }
  },
  "agent_review": {
   "$ref": "#/$defs/agentReview"
  }
 },
 "$defs": {
  "dateTime": {
   "description": "ISO 8601 日期時間，含時區。",
   "type": "string",
   "format": "date-time"
  },
  "fhirRef": {
   "description": "FHIR 資源參照，格式為「資源類型/編號」。",
   "type": "string",
   "pattern": "^[A-Za-z]+/[^/\\s]+$"
  },
  "sectionKey": {
   "description": "區塊代號。consult_overview＝照會與病人概況；infection_evidence＝感染證據；microbiology＝微生物；medications＝擬開與用藥歷程；susceptibility＝藥敏對照；dose_related＝劑量相關數值；allergy＝過敏；course_route＝療程與途徑；clinical_context＝臨床脈絡。",
   "enum": [
    "consult_overview",
    "infection_evidence",
    "microbiology",
    "medications",
    "susceptibility",
    "dose_related",
    "allergy",
    "course_route",
    "clinical_context"
   ]
  },
  "section": {
   "description": "一個摘要區塊。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "section",
    "title",
    "question",
    "items",
    "status",
    "status_reason"
   ],
   "properties": {
    "section": {
     "$ref": "#/$defs/sectionKey"
    },
    "title": {
     "description": "區塊中文標題（照會與病人概況、感染證據、微生物、擬開與用藥歷程、藥敏對照、劑量相關數值、過敏、療程與途徑、臨床脈絡）。",
     "enum": [
      "照會與病人概況",
      "感染證據",
      "微生物",
      "擬開與用藥歷程",
      "藥敏對照",
      "劑量相關數值",
      "過敏",
      "療程與途徑",
      "臨床脈絡"
     ]
    },
    "question": {
     "description": "這個區塊回答的問題（給畫面當副標）。",
     "type": "string"
    },
    "status": {
     "description": "區塊狀態。ok＝照常產出；partial＝部分內容無法產出（例如某幾份文件的 AI 摘錄失敗）；failed＝這一區無法產出（例如 AI 摘錄全部失敗）；no_data＝送入資料中沒有這一區的內容。",
     "enum": [
      "ok",
      "partial",
      "failed",
      "no_data"
     ]
    },
    "status_reason": {
     "description": "狀態說明（status 不是 ok 時才有文字，否則為 null），例如失敗原因。",
     "type": [
      "string",
      "null"
     ]
    },
    "items": {
     "description": "區塊內的摘要項目，依產生順序排列。",
     "type": "array",
     "items": {
      "$ref": "#/$defs/item"
     }
    }
   }
  },
  "source": {
   "description": "來源：這一項資料出自哪一筆病歷，以及那筆資料的時間。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "resource",
    "date"
   ],
   "properties": {
    "resource": {
     "description": "來源病歷的 FHIR 資源參照（例如 Observation/a4-l003）。查無資料時，改填查詢本身的說明（例如「AllergyIntolerance 查詢（0 筆）」、「MedicationRequest 查詢」）。",
     "type": "string",
     "minLength": 1
    },
    "date": {
     "description": "資料時間（該筆病歷的時間）；病人基本資料等沒有時間的來源為 null。",
     "oneOf": [
      {
       "$ref": "#/$defs/dateTime"
      },
      {
       "type": "null"
      }
     ]
    }
   }
  },
  "input": {
   "description": "計算值的一個輸入。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "label",
    "value",
    "source"
   ],
   "properties": {
    "label": {
     "description": "輸入名稱（例如「擬開劑量」、「體重（出生體重）」）。",
     "type": "string"
    },
    "value": {
     "description": "輸入值（含單位的文字）。",
     "type": "string"
    },
    "source": {
     "description": "這個輸入的來源病歷；輸入不是病歷（例如照會時間、上一項計算值）時為 null，並以 note 說明。",
     "oneOf": [
      {
       "$ref": "#/$defs/source"
      },
      {
       "type": "null"
      }
     ]
    },
    "note": {
     "description": "source 為 null 時的說明。",
     "enum": [
      "時間基準",
      "上一項計算值"
     ]
    }
   }
  },
  "item": {
   "description": "一個摘要項目。type 決定性質：fact＝病歷事實（照錄病歷）；derived＝計算值（程式計算，附公式與輸入）；missing＝資料缺漏（該項查不到或不能算）。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "id",
    "type",
    "label",
    "text",
    "text_origin",
    "sources"
   ],
   "properties": {
    "id": {
     "description": "項目編號，同一份摘要內唯一（i1、i2…）。",
     "type": "string",
     "pattern": "^i\\d+$"
    },
    "type": {
     "description": "項目類型。fact＝病歷事實；derived＝計算值；missing＝資料缺漏。",
     "enum": [
      "fact",
      "derived",
      "missing"
     ]
    },
    "label": {
     "description": "項目名稱（例如「最近體重」、「ampicillin 換算」）。臨床脈絡區塊為「文件類型（時間）」。",
     "type": "string",
     "minLength": 1
    },
    "text": {
     "description": "顯示文字。fact 為病歷內容；derived 為計算結果；missing 為缺漏說明。臨床脈絡區塊為 AI 摘錄、經逐字比對通過的病歷原文。",
     "type": "string",
     "minLength": 1
    },
    "text_origin": {
     "description": "句子來源類型。ehr_verbatim＝病歷原文；system_generated＝系統產生（計算結果、缺漏說明）。摘要項目不會出現 agent_written（Agent 撰寫的只有 agent_label）。",
     "enum": [
      "ehr_verbatim",
      "system_generated"
     ]
    },
    "sources": {
     "description": "來源清單（含資料時間）。fact 與 derived 至少一筆；missing 為空陣列。",
     "type": "array",
     "items": {
      "$ref": "#/$defs/source"
     }
    },
    "emphasis": {
     "description": "true＝畫面上建議加粗強調（照會目的原文、擬開醫囑）。",
     "const": true
    },
    "conflict": {
     "description": "true＝同一時點有互相矛盾的資料（例如兩個不同體重），並列呈現、不擇一。",
     "const": true
    },
    "rule": {
     "description": "這一項套用的院方規則或計算規則說明（例如劑量用體重規則）。 一律是示範設定，僅供參考，實際規則依長庚臨床確認。",
     "type": "string"
    },
    "compact": {
     "description": "true＝次要的計算值，畫面可縮小或併入上一項顯示。",
     "const": true
    },
    "flag": {
     "description": "檢驗異常旗標，照錄檢驗報告（例如 H＝偏高、L＝偏低）；沒有旗標時為 null。",
     "type": [
      "string",
      "null"
     ]
    },
    "pending": {
     "description": "培養報告是否還不是最終報告。true＝初步報告或待報告；false＝最終報告。",
     "type": "boolean"
    },
    "value": {
     "description": "計算值的數值（derived 才有）。依計算種類為數字（天數、ANC、eGFR、小時數）、文字（升降方向：上升／下降／持平）或物件（年齡、矯正週數、mg/kg 換算）。",
     "oneOf": [
      {
       "type": "number"
      },
      {
       "enum": [
        "上升",
        "下降",
        "持平"
       ]
      },
      {
       "description": "年齡：歲、月、天與總天數。",
       "type": "object",
       "additionalProperties": false,
       "required": [
        "years",
        "months",
        "days",
        "totalDays"
       ],
       "properties": {
        "years": {
         "description": "歲。",
         "type": "integer",
         "minimum": 0
        },
        "months": {
         "description": "個月。",
         "type": "integer",
         "minimum": 0
        },
        "days": {
         "description": "天。",
         "type": "integer",
         "minimum": 0
        },
        "totalDays": {
         "description": "出生至照會的總天數。",
         "type": "integer",
         "minimum": 0
        }
       }
      },
      {
       "description": "矯正週數（PMA）：週與天。",
       "type": "object",
       "additionalProperties": false,
       "required": [
        "weeks",
        "days"
       ],
       "properties": {
        "weeks": {
         "description": "週。",
         "type": "integer",
         "minimum": 0
        },
        "days": {
         "description": "天（0–6）。",
         "type": "integer",
         "minimum": 0
        }
       }
      },
      {
       "description": "mg/kg 換算：每次（mg/kg/dose）與每日（mg/kg/day）；頻次無法換算成每日次數時 perDay 為 null。",
       "type": "object",
       "additionalProperties": false,
       "required": [
        "perDose",
        "perDay"
       ],
       "properties": {
        "perDose": {
         "description": "每次劑量 mg/kg/dose。",
         "type": "number"
        },
        "perDay": {
         "description": "每日劑量 mg/kg/day；無法換算時為 null。",
         "type": [
          "number",
          "null"
         ]
        }
       }
      }
     ]
    },
    "formula": {
     "description": "計算公式（含代入的數值），derived 才有。",
     "type": "string",
     "minLength": 1
    },
    "inputs": {
     "description": "計算的每個輸入值與來源，derived 才有。",
     "type": "array",
     "minItems": 1,
     "items": {
      "$ref": "#/$defs/input"
     }
    },
    "table": {
     "description": "表格資料（目前用於生命徵象每日最低–最高）。",
     "type": "object",
     "additionalProperties": false,
     "required": [
      "columns",
      "rows"
     ],
     "properties": {
      "columns": {
       "description": "欄位名稱。",
       "type": "array",
       "items": {
        "type": "string"
       }
      },
      "rows": {
       "description": "每一列的儲存格文字，欄數與 columns 相同；「—」表示當天沒有紀錄。",
       "type": "array",
       "items": {
        "type": "array",
        "items": {
         "type": "string"
        }
       }
      }
     }
    },
    "matrix": {
     "description": "藥敏對照的一格（藥敏對照區塊才有）：一個菌株對一個藥物的結果。",
     "type": "object",
     "additionalProperties": false,
     "required": [
      "organism",
      "drug",
      "role",
      "result",
      "mic"
     ],
     "properties": {
      "organism": {
       "description": "菌種名稱。",
       "type": "string"
      },
      "drug": {
       "description": "藥物名稱。",
       "type": "string"
      },
      "role": {
       "description": "藥物角色。擬開＝這次擬開的藥；現用＝目前正在使用的藥。",
       "enum": [
        "擬開",
        "現用"
       ]
      },
      "result": {
       "description": "藥敏結果，照錄報告。S＝敏感；I＝中間；R＝抗藥；未測＝報告沒有列出這個藥物。",
       "enum": [
        "S",
        "I",
        "R",
        "未測"
       ]
      },
      "mic": {
       "description": "最小抑菌濃度（照錄報告）；報告沒有時為 null。",
       "type": [
        "string",
        "null"
       ]
      }
     }
    },
    "allergy_state": {
     "description": "過敏狀態（過敏區塊才有）。has_allergy＝有過敏紀錄；nka＝記載無已知過敏；no_record＝FHIR 無過敏紀錄。過敏紀錄查詢失敗時，改以 type＝missing 的項目表示。",
     "enum": [
      "has_allergy",
      "nka",
      "no_record"
     ]
    },
    "topic": {
     "description": "AI 摘錄的主題（臨床脈絡區塊才有）。",
     "enum": [
      "感染",
      "用藥"
     ]
    },
    "agent_label": {
     "description": "AI 為摘錄寫的短標題（臨床脈絡區塊才有）。這是唯一由 Agent 撰寫的文字，已經過禁用詞組檢查；含禁用詞時為 null。",
     "type": [
      "string",
      "null"
     ]
    },
    "agent_label_origin": {
     "description": "agent_label 的來源類型，固定為 agent_written（Agent 撰寫）；沒有標題時為 null。",
     "enum": [
      "agent_written",
      null
     ]
    },
    "doc_type": {
     "description": "摘錄出處的文件類型（臨床脈絡區塊才有）。",
     "enum": [
      "病程紀錄",
      "入院紀錄"
     ]
    },
    "rule_basis": {
     "description": "計算值（type: derived）的公式類別。general_definition＝依業界通用定義計算（年齡、日齡、矯正週數、每公斤劑量、ANC、eGFR、採血距上一劑時間）；demo_setting＝需要院方決定的規則，目前為示範設定（已使用天數、升降方向、生命徵象每日最高最低的時間範圍）。兩類都僅供參考，實際依長庚臨床確認。另外，項目的 rule 欄位（例如劑量用體重、近 30 天）一律是示範設定。",
     "enum": [
      "general_definition",
      "demo_setting"
     ]
    }
   },
   "allOf": [
    {
     "if": {
      "properties": {
       "type": {
        "const": "derived"
       }
      }
     },
     "then": {
      "required": [
       "value",
       "formula",
       "inputs"
      ],
      "properties": {
       "text_origin": {
        "const": "system_generated"
       },
       "sources": {
        "minItems": 1
       }
      }
     }
    },
    {
     "if": {
      "properties": {
       "type": {
        "const": "fact"
       }
      }
     },
     "then": {
      "properties": {
       "text_origin": {
        "const": "ehr_verbatim"
       },
       "sources": {
        "minItems": 1
       }
      }
     }
    },
    {
     "if": {
      "properties": {
       "type": {
        "const": "missing"
       }
      }
     },
     "then": {
      "properties": {
       "text_origin": {
        "const": "system_generated"
       },
       "sources": {
        "maxItems": 0
       }
      }
     }
    }
   ]
  },
  "knowledge": {
   "description": "一筆相關知識（知識庫查詢結果，內容不是 AI 寫的；Agent 判斷查詢的筆數附判斷理由）。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "id",
    "type",
    "provider",
    "check",
    "related_section",
    "query",
    "status",
    "text",
    "text_origin",
    "citation",
    "version",
    "retrieved_at",
    "requested_by"
   ],
   "properties": {
    "id": {
     "description": "知識編號，同一份摘要內唯一（k1、k2…）。",
     "type": "string",
     "pattern": "^k\\d+$"
    },
    "type": {
     "description": "固定為 knowledge。",
     "const": "knowledge"
    },
    "provider": {
     "description": "知識庫提供者。示範版為「Medi-Span（示範）」或「Sanford Guide（示範）」。",
     "type": "string",
     "pattern": "^(Medi-Span|Sanford Guide)"
    },
    "check": {
     "description": "查詢項目。Medi-Span：劑量範圍、交互作用、重複用藥、過敏與交叉過敏、藥碼對照（院內藥碼對不到）。Sanford Guide：指引章節（對不到或查不到章節）、指引列出的用藥、指引列出的療程、菌種用藥、兒科劑量、新生兒劑量。 v3 Agent 判斷查詢：指引查詢（Agent 判斷不查或查詢未採用）、指引建議用藥（Sanford Guide 查詢結果）、交互作用（Agent 追加）、劑量調整（Agent 追加）、Agent 判斷（Agent 判斷步驟失敗）。",
     "enum": [
      "劑量範圍",
      "交互作用",
      "重複用藥",
      "過敏與交叉過敏",
      "藥碼對照",
      "指引章節",
      "指引列出的用藥",
      "指引列出的療程",
      "菌種用藥",
      "兒科劑量",
      "新生兒劑量",
      "指引查詢",
      "指引建議用藥",
      "交互作用（Agent 追加）",
      "劑量調整（Agent 追加）",
      "Agent 判斷"
     ]
    },
    "related_section": {
     "description": "這筆知識要顯示在哪個區塊下方。",
     "$ref": "#/$defs/sectionKey"
    },
    "drugs": {
     "description": "相關藥物名稱。",
     "type": "array",
     "items": {
      "type": "string"
     }
    },
    "organism": {
     "description": "相關菌種（菌種用藥才有）。",
     "type": "string"
    },
    "query": {
     "description": "查詢條件（給人看的中文鍵值，例如 藥品、劑量、年齡層、體重、藥物清單、照會診斷、菌種、過敏原、院內藥碼）。",
     "type": "object",
     "additionalProperties": {
      "type": "string"
     }
    },
    "status": {
     "description": "查詢結果狀態。ok＝有結果；none_found＝查詢成功、未偵測到（例如無交互作用）；unavailable＝知識庫暫無法查詢（逾時或錯誤，摘要照常產出）；unmapped＝院內藥碼無法比對，未納入安全檢核；no_pediatric＝無兒科／新生兒資料；no_entry＝無對應指引條目；no_data＝知識庫沒有這個藥的資料；not_checked＝頻次或體重無法換算，未檢核。pending_culture＝培養尚未報告，依菌種的查詢待培養結果、本次未查詢。 v3：not_called＝Agent 判斷不需查詢（理由見 agent_reason 與 text）；rejected＝Agent 的查詢未通過程式把關、不採用（原因見 rejection 與 validation.agent_review）；agent_failed＝Agent 判斷步驟失敗（逾時或錯誤），未查詢。",
     "enum": [
      "ok",
      "none_found",
      "unavailable",
      "unmapped",
      "no_pediatric",
      "no_entry",
      "no_data",
      "not_checked",
      "pending_culture",
      "not_called",
      "rejected",
      "agent_failed"
     ]
    },
    "result": {
     "description": "劑量範圍檢核結果（劑量範圍才有）；沒有檢核結果時為 null。",
     "enum": [
      "在範圍內",
      "高於範圍上限",
      "低於範圍下限",
      null
     ]
    },
    "severity": {
     "description": "嚴重度的中文（與 severity_level 對應：嚴重、中度、輕微）；沒有等級時為 null。",
     "type": [
      "string",
      "null"
     ]
    },
    "title": {
     "description": "指引條目標題（Sanford Guide 才有）。",
     "type": [
      "string",
      "null"
     ]
    },
    "text": {
     "description": "顯示文字。Medi-Span 為固定句型（「Medi-Span 偵測：…〔出處，版本〕」）；Sanford Guide 為「Sanford Guide 指引內容：」加指引原文；查不到時為狀態說明。",
     "type": "string",
     "minLength": 1
    },
    "quote": {
     "description": "指引原文（Sanford Guide 有結果時才有），與 text 中引號後的內容相同。",
     "type": [
      "string",
      "null"
     ]
    },
    "text_origin": {
     "description": "句子來源類型。system_generated＝系統偵測（固定句型）；guideline_verbatim＝指引原文。 agent_written＝Agent 撰寫（只用在「Agent 判斷不需查詢：理由」）。",
     "enum": [
      "system_generated",
      "guideline_verbatim",
      "agent_written"
     ]
    },
    "citation": {
     "description": "出處（條目編號或對照表名稱）；status 為 ok 時必有。",
     "type": [
      "string",
      "null"
     ]
    },
    "version": {
     "description": "知識庫或對照表版本；status 為 ok 時必有，知識庫無法查詢時為 null。",
     "type": [
      "string",
      "null"
     ]
    },
    "retrieved_at": {
     "description": "查詢時間。",
     "$ref": "#/$defs/dateTime"
    },
    "ms": {
     "description": "這次知識庫查詢耗時（毫秒）；沒有實際呼叫知識庫（例如藥碼對不到、診斷對不到章節）時沒有這個欄位。",
     "type": "integer",
     "minimum": 0
    },
    "error": {
     "description": "知識庫無法查詢的原因（status 為 unavailable 才有）。timeout＝查詢逾時；其他文字為錯誤訊息（例如轉接服務無法連線）。",
     "type": "string"
    },
    "query_basis": {
     "description": "查詢依據。diagnosis＝依診斷或感染部位；organism＝依菌種；diagnosis_and_organism＝菌種與診斷一起；drug＝依藥品；Agent 判斷不查時為 null。",
     "enum": [
      "diagnosis",
      "organism",
      "drug",
      "diagnosis_and_organism",
      null
     ]
    },
    "error_code": {
     "description": "知識庫無法查詢的原因代碼（status 為 unavailable 才有）。timeout＝逾時；connection_failed＝轉接服務無法連線；other＝其他錯誤。",
     "enum": [
      "timeout",
      "connection_failed",
      "other"
     ]
    },
    "severity_level": {
     "description": "嚴重度等級代碼（給程式判斷），依 Medi-Span 分類：major＝嚴重；moderate＝中度；minor＝輕微。畫面一律顯示中文標籤（紅色「嚴重」、橘色「中度」、灰色「輕微」）。Medi-Span 的警示（交互作用、過敏與交叉過敏、重複用藥、劑量超出範圍）才有，其他為 null。",
     "enum": [
      "major",
      "moderate",
      "minor",
      null
     ]
    },
    "requested_by": {
     "description": "誰要求這筆查詢。pipeline＝程式必查（Medi-Span 用藥安全底線）；agent＝Agent 判斷查詢（Sanford Guide，或 Agent 追加的 Medi-Span）。",
     "enum": [
      "pipeline",
      "agent"
     ]
    },
    "decision_id": {
     "description": "對應 agent_review.decisions[] 的編號（d1、d2…）；Agent 判斷失敗時為 null。requested_by 為 agent 才有。",
     "type": [
      "string",
      "null"
     ]
    },
    "agent_reason": {
     "description": "Agent 撰寫的判斷理由（已通過禁用詞檢查；被擋下時為 null）。requested_by 為 agent 才有。",
     "type": [
      "string",
      "null"
     ]
    },
    "agent_reason_origin": {
     "description": "判斷理由的來源類型，固定為 agent_written（Agent 撰寫）；沒有理由時為 null。",
     "enum": [
      "agent_written",
      null
     ]
    },
    "rejection": {
     "description": "status 為 rejected 時的原因代碼。args_not_in_chart＝查詢參數不在病歷事實或照會原文；content_mismatch＝Agent 看到的結果與程式重查不一致；not_executed＝Agent 判斷要查但工具紀錄沒有這次呼叫。",
     "enum": [
      "args_not_in_chart",
      "content_mismatch",
      "not_executed"
     ]
    },
    "kb_ref": {
     "description": "知識庫條目編號（MCP 查詢結果的 kbRef）。",
     "type": "string"
    }
   },
   "allOf": [
    {
     "if": {
      "properties": {
       "status": {
        "const": "ok"
       }
      }
     },
     "then": {
      "properties": {
       "citation": {
        "type": "string",
        "minLength": 1
       },
       "version": {
        "type": "string",
        "minLength": 1
       }
      }
     }
    },
    {
     "if": {
      "properties": {
       "status": {
        "const": "unavailable"
       }
      }
     },
     "then": {
      "required": [
       "error"
      ],
      "properties": {
       "text": {
        "const": "知識庫暫無法查詢"
       }
      }
     }
    }
   ]
  },
  "missingEntry": {
   "description": "一筆資料缺漏。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "item",
    "category",
    "reason",
    "detail",
    "last_checked",
    "last_updated",
    "reason_code"
   ],
   "properties": {
    "item": {
     "description": "缺漏的項目（中文，例如「給藥紀錄」、「體重」、「Sanford Guide（示範） 兒科劑量」）。",
     "type": "string",
     "minLength": 1
    },
    "category": {
     "description": "缺漏類別。FHIR 查詢類別（Patient、ServiceRequest、Condition、Observation-vital、Observation-weight、Observation-lab、DiagnosticReport-micro、MedicationRequest、MedicationAdministration、AllergyIntolerance、DocumentReference、Device、ImagingStudy、NutritionOrder）；時效規則代號（weight＝體重、vital＝生命徵象、cr＝肌酸酐、crp＝CRP）；knowledge＝知識庫；agent＝AI 摘錄或 Agent 判斷。",
     "enum": [
      "Patient",
      "ServiceRequest",
      "Condition",
      "Observation-vital",
      "Observation-weight",
      "Observation-lab",
      "DiagnosticReport-micro",
      "MedicationRequest",
      "MedicationAdministration",
      "AllergyIntolerance",
      "DocumentReference",
      "Device",
      "ImagingStudy",
      "NutritionOrder",
      "weight",
      "vital",
      "cr",
      "crp",
      "knowledge",
      "agent"
     ]
    },
    "reason": {
     "description": "缺漏原因（中文，給人看；程式判斷請用 reason_code）。查詢失敗＝xAMS 回報查詢失敗（資料可能存在但沒拿到）；查無資料＝FHIR 確定沒有這類資料；已過期＝最後一筆超過院方時效；本次未收到＝必要項目 xAMS 沒有送；知識庫暫無法查詢＝知識庫逾時或無法連線；AI 摘錄失敗＝病程摘錄助理呼叫失敗。 Agent 判斷失敗＝照會判讀助理呼叫失敗（Sanford Guide 未查詢）。",
     "enum": [
      "AI 摘錄失敗",
      "已過期",
      "本次未收到",
      "查無資料",
      "查詢失敗",
      "知識庫暫無法查詢",
      "Agent 判斷失敗"
     ]
    },
    "reason_code": {
     "description": "缺漏原因代碼（供 xAMS 決定呈現方式）。query_failed＝查詢失敗：資料可能存在但這次沒拿到，請醫師到 HIS 確認；not_found＝查無資料：FHIR 確定沒有這類資料；stale＝已過期；not_received＝本次未收到必要項目；kb_unavailable＝知識庫暫無法查詢；agent_failed＝AI 摘錄失敗。 agent_judge_failed＝Agent 判斷失敗。",
     "enum": [
      "query_failed",
      "not_found",
      "stale",
      "not_received",
      "kb_unavailable",
      "agent_failed",
      "agent_judge_failed"
     ]
    },
    "detail": {
     "description": "缺漏說明。",
     "type": "string"
    },
    "last_checked": {
     "description": "查詢時間（xAMS 附的查詢時間，或摘要服務的檢查時間）；不明時為 null。",
     "oneOf": [
      {
       "$ref": "#/$defs/dateTime"
      },
      {
       "type": "null"
      }
     ]
    },
    "last_updated": {
     "description": "最後一筆資料的時間（已過期才有值）；其餘為 null。",
     "oneOf": [
      {
       "$ref": "#/$defs/dateTime"
      },
      {
       "type": "null"
      }
     ]
    },
    "source": {
     "description": "已過期的那筆資料的 FHIR 資源參照（已過期才有）。",
     "$ref": "#/$defs/fhirRef"
    }
   }
  },
  "rejectedExcerpt": {
   "description": "一句被擋下的摘錄。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "doc_ref",
    "reason"
   ],
   "properties": {
    "doc_ref": {
     "description": "摘錄宣稱引用的文件（FHIR 資源參照）；可能是不存在的文件。",
     "type": [
      "string",
      "null"
     ]
    },
    "quote": {
     "description": "被擋下的摘錄文字。",
     "type": [
      "string",
      "null"
     ]
    },
    "label": {
     "description": "摘錄附帶的 Agent 標題。",
     "type": [
      "string",
      "null"
     ]
    },
    "reason": {
     "description": "擋下原因。",
     "enum": [
      "引用的文件不在這位病人的病程紀錄／入院紀錄中",
      "摘錄是空的",
      "摘錄超過 200 字",
      "原文中找不到這段文字（不是逐字摘錄）",
      "重複的摘錄"
     ]
    },
    "simulated_error": {
     "description": "測試用錯誤內容的示範標記；正式版不含此欄位。",
     "const": true
    }
   }
  },
  "blockedLabel": {
   "description": "一個因禁用詞組被拿掉的 Agent 標題（摘錄原文保留）。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "doc_ref",
    "text",
    "phrases"
   ],
   "properties": {
    "doc_ref": {
     "description": "摘錄引用的文件（FHIR 資源參照）。",
     "$ref": "#/$defs/fhirRef"
    },
    "text": {
     "description": "被拿掉的標題原文。",
     "type": "string"
    },
    "phrases": {
     "description": "命中的禁用詞組。",
     "type": "array",
     "minItems": 1,
     "items": {
      "type": "string"
     }
    },
    "simulated_error": {
     "description": "測試用錯誤內容的示範標記；正式版不含此欄位。",
     "const": true
    }
   }
  },
  "extraction": {
   "type": "object",
   "additionalProperties": false,
   "required": [
    "text",
    "quote",
    "from",
    "text_origin",
    "quote_origin"
   ],
   "properties": {
    "text": {
     "description": "Agent 整理的名稱（Agent 撰寫）。",
     "type": "string"
    },
    "quote": {
     "description": "原文片段：逐字出現在照會目的或送給 Agent 的結構化資料（程式已核對）。",
     "type": "string"
    },
    "from": {
     "description": "原文片段的出處。",
     "enum": [
      "照會目的",
      "結構化資料"
     ]
    },
    "text_origin": {
     "description": "名稱的來源類型，固定為 agent_written。",
     "const": "agent_written"
    },
    "quote_origin": {
     "description": "原文片段的來源類型。ehr_verbatim＝照會目的原文；ehr_structured＝xAMS 送入的結構化資料。",
     "enum": [
      "ehr_verbatim",
      "ehr_structured"
     ]
    }
   }
  },
  "rejectedExtraction": {
   "type": "object",
   "additionalProperties": false,
   "required": [
    "kind",
    "text",
    "quote",
    "from",
    "text_origin",
    "quote_origin",
    "reason"
   ],
   "properties": {
    "text": {
     "description": "Agent 整理的名稱（Agent 撰寫）。",
     "type": "string"
    },
    "quote": {
     "description": "原文片段：逐字出現在照會目的或送給 Agent 的結構化資料（程式已核對）。",
     "type": "string"
    },
    "from": {
     "description": "原文片段的出處。",
     "enum": [
      "照會目的",
      "結構化資料"
     ]
    },
    "text_origin": {
     "description": "名稱的來源類型，固定為 agent_written。",
     "const": "agent_written"
    },
    "quote_origin": {
     "description": "原文片段的來源類型。ehr_verbatim＝照會目的原文；ehr_structured＝xAMS 送入的結構化資料。",
     "enum": [
      "ehr_verbatim",
      "ehr_structured"
     ]
    },
    "kind": {
     "description": "萃取類別。",
     "enum": [
      "consult_question",
      "sites",
      "organisms",
      "drugs"
     ]
    },
    "reason": {
     "description": "被擋下的原因。",
     "type": "string"
    },
    "simulated_error": {
     "description": "測試用錯誤內容的示範標記（正式版不含）。",
     "const": true
    }
   }
  },
  "decision": {
   "type": "object",
   "additionalProperties": false,
   "required": [
    "id",
    "tool",
    "provider",
    "action",
    "basis",
    "args",
    "reason",
    "reason_origin",
    "reason_blocked",
    "declared",
    "executed",
    "result_status",
    "problems",
    "knowledge_ids"
   ],
   "properties": {
    "id": {
     "description": "判斷編號（d1、d2…）。",
     "type": "string",
     "pattern": "^d\\d+$"
    },
    "tool": {
     "description": "MCP 工具。sanford_therapy＝Sanford Guide 建議用藥；medispan_interactions、medispan_dose_adjustment＝Agent 追加的 Medi-Span 查詢。",
     "enum": [
      "sanford_therapy",
      "medispan_interactions",
      "medispan_dose_adjustment"
     ]
    },
    "provider": {
     "description": "知識庫提供者。",
     "enum": [
      "Sanford Guide（示範）",
      "Medi-Span（示範）"
     ]
    },
    "action": {
     "description": "call＝查詢；skip＝判斷不需查詢。",
     "enum": [
      "call",
      "skip"
     ]
    },
    "basis": {
     "description": "查詢依據：diagnosis＝依診斷／感染部位；organism＝依菌種；diagnosis_and_organism＝兩者；drug＝依藥品；不查時為 null。",
     "enum": [
      "diagnosis",
      "organism",
      "diagnosis_and_organism",
      "drug",
      null
     ]
    },
    "args": {
     "description": "實際送出的查詢參數（以 MaiAgent 工具紀錄為準）；不查時為 {}。",
     "type": "object"
    },
    "reason": {
     "description": "Agent 撰寫的判斷理由（已通過禁用詞檢查；被擋下或沒寫時為 null）。",
     "type": [
      "string",
      "null"
     ]
    },
    "reason_origin": {
     "description": "固定為 agent_written。",
     "const": "agent_written"
    },
    "reason_blocked": {
     "description": "理由被擋下時命中的禁用詞組；沒被擋為 null。",
     "type": [
      "array",
      "null"
     ],
     "items": {
      "type": "string"
     }
    },
    "declared": {
     "description": "Agent 是否在回覆的 decisions 中寫了這筆判斷（false＝工具紀錄有呼叫但 Agent 沒說明）。",
     "type": "boolean"
    },
    "executed": {
     "description": "工具紀錄中是否真的有這次呼叫。",
     "type": "boolean"
    },
    "result_status": {
     "description": "結果。ok＝重查一致、採用；no_entry／none_found＝重查一致、知識庫沒有對應條目；not_called＝判斷不查；not_decided＝Agent 沒寫 Sanford 判斷；rejected_args＝參數不在病歷事實或照會原文，不採用；rejected_mismatch＝Agent 看到的結果與重查不一致，不採用；unverified＝重查逾時或失敗，無法核對，不採用；not_executed＝判斷要查但沒有實際呼叫。",
     "enum": [
      "ok",
      "no_entry",
      "none_found",
      "not_called",
      "not_decided",
      "rejected_args",
      "rejected_mismatch",
      "unverified",
      "not_executed"
     ]
    },
    "problems": {
     "description": "程式把關發現的問題（中文）。",
     "type": "array",
     "items": {
      "type": "string"
     }
    },
    "knowledge_ids": {
     "description": "這筆判斷產生的 knowledge[] 編號。",
     "type": "array",
     "items": {
      "type": "string",
      "pattern": "^k\\d+$"
     }
    }
   }
  },
  "agentReview": {
   "description": "Agent 判斷（照會判讀助理）：從照會目的萃取的重點與要不要查知識庫的判斷。Agent 撰寫的部分（名稱、理由）都標 agent_written；原文片段已逐字核對。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "status",
    "mode",
    "assistant",
    "input",
    "extractions",
    "decisions",
    "tool_calls_source",
    "error",
    "conversation_id",
    "recorded_ms",
    "recorded_at"
   ],
   "properties": {
    "status": {
     "description": "ok＝完成；failed＝Agent 判斷失敗（逾時、錯誤或回覆無法解析），Sanford Guide 未查詢；not_called＝未呼叫。",
     "enum": [
      "ok",
      "failed",
      "not_called"
     ]
    },
    "mode": {
     "description": "maiagent＝即時呼叫；recorded＝回放錄製；simulated＝測試用輸出，不呼叫 AI（示範用）；失敗時可能為 null。",
     "enum": [
      "maiagent",
      "recorded",
      "simulated",
      null
     ]
    },
    "assistant": {
     "description": "MaiAgent 助理名稱。",
     "type": "string"
    },
    "input": {
     "description": "送給 Agent 的內容：照會目的原文與最少的結構化資料（不送完整病歷）。",
     "type": "object",
     "additionalProperties": false,
     "required": [
      "purpose",
      "context_lines"
     ],
     "properties": {
      "purpose": {
       "type": "string",
       "description": "照會目的原文。"
      },
      "context_lines": {
       "type": "array",
       "items": {
        "type": "string"
       },
       "description": "結構化資料（每行一項：年齡層、照會診斷、診斷、擬開／現用抗微生物藥物、培養）。"
      }
     }
    },
    "extractions": {
     "description": "通過逐字核對的萃取結果；失敗時為 null。",
     "type": [
      "object",
      "null"
     ],
     "additionalProperties": false,
     "required": [
      "consult_question",
      "sites",
      "organisms",
      "drugs"
     ],
     "properties": {
      "consult_question": {
       "description": "照會問題。",
       "anyOf": [
        {
         "$ref": "#/$defs/extraction"
        },
        {
         "type": "null"
        }
       ]
      },
      "sites": {
       "description": "懷疑部位或診斷。",
       "type": "array",
       "items": {
        "$ref": "#/$defs/extraction"
       }
      },
      "organisms": {
       "description": "菌種。",
       "type": "array",
       "items": {
        "$ref": "#/$defs/extraction"
       }
      },
      "drugs": {
       "description": "藥物。",
       "type": "array",
       "items": {
        "$ref": "#/$defs/extraction"
       }
      }
     }
    },
    "decisions": {
     "description": "查詢判斷（Sanford 一定有一筆：查或不查）。",
     "type": "array",
     "items": {
      "$ref": "#/$defs/decision"
     }
    },
    "tool_calls_source": {
     "description": "Agent 實際呼叫了哪些工具的依據。maiagent_payload＝MaiAgent 訊息的工具紀錄（tool_use／tool_result，平台記錄，不是 Agent 自述）。",
     "enum": [
      "maiagent_payload",
      "agent_reported",
      null
     ]
    },
    "error": {
     "description": "失敗原因；成功時為 null。",
     "type": [
      "string",
      "null"
     ]
    },
    "conversation_id": {
     "description": "MaiAgent 對話編號（即時呼叫才有）。",
     "type": [
      "string",
      "null"
     ]
    },
    "recorded_ms": {
     "description": "回放錄製時，錄製當時實測耗時（毫秒）。",
     "type": [
      "integer",
      "null"
     ]
    },
    "recorded_at": {
     "description": "錄製時間。",
     "type": [
      "string",
      "null"
     ]
    }
   }
  }
 }
}