{
 "$schema": "https://json-schema.org/draft/2020-12/schema",
 "$id": "https://maiagent.ai/schema/xams-summary-v2.schema.json",
 "title": "xAMS 兒科抗生素照會摘要 v2：API 回應格式",
 "description": "摘要服務 v2 回應 xAMS 的 JSON 格式定義。xAMS 依這份格式自行繪製畫面。每一筆摘要項目都附類型（type）、句子來源類型（text_origin）與來源病歷（sources，含資料時間）；外部知識另放 knowledge[]，資料缺漏另放 missing[]。本版本為示範版（schema 欄位值 xams-summary-v2.3-demo），正式版欄位如有增減會更改 schema 欄位值。日期時間一律為 ISO 8601、含時區（例如 2026-10-25T10:00:00+08:00）。",
 "type": "object",
 "additionalProperties": false,
 "required": [
  "schema",
  "summary_id",
  "generated_at",
  "patient_ref",
  "consult_ref",
  "time_basis",
  "data_time",
  "input_summary",
  "rules_version",
  "sections",
  "knowledge",
  "missing",
  "validation",
  "timing",
  "agent",
  "self_check"
 ],
 "properties": {
  "schema": {
   "description": "格式版本代號。本版固定為 xams-summary-v2.3-demo；格式有不相容的變更時會改這個值。",
   "const": "xams-summary-v2.3-demo"
  },
  "summary_id": {
   "description": "這份摘要的編號，每次產生都不同（格式：sum-<病人代號>-<產生時間毫秒>）。xAMS 保存摘要時可用來辨識版本。",
   "type": "string",
   "pattern": "^sum-.+-\\d+$"
  },
  "generated_at": {
   "description": "摘要產生時間（摘要服務的時間）。",
   "$ref": "#/$defs/dateTime"
  },
  "patient_ref": {
   "description": "病人的 FHIR 資源參照（例如 Patient/c001），與 xAMS 送來的病人資料相同。",
   "$ref": "#/$defs/fhirRef"
  },
  "consult_ref": {
   "description": "照會單的 FHIR 資源參照（例如 ServiceRequest/c001-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"
       }
      ]
     }
    }
   }
  },
  "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／Sanford Guide 知識庫的結果（不經 AI）。每筆以 related_section 指出要顯示在哪個區塊下方。依區塊順序排列。",
   "type": "array",
   "items": {
    "$ref": "#/$defs/knowledge"
   }
  },
  "missing": {
   "description": "資料缺漏清單：依 xAMS 附的每類查詢狀態、資料時效規則與知識庫查詢結果，由程式產生。沒有缺漏時為空陣列。",
   "type": "array",
   "items": {
    "$ref": "#/$defs/missingEntry"
   }
  },
  "validation": {
   "description": "程式對 AI 摘錄的檢查紀錄（逐字比對、禁用詞組）。被擋下的內容只出現在這裡，不會出現在 sections。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "excerpts",
    "agent_text",
    "verbatim_not_checked"
   ],
   "properties": {
    "excerpts": {
     "description": "AI 摘錄的逐字比對結果。",
     "type": "object",
     "additionalProperties": false,
     "required": [
      "received",
      "accepted",
      "rejected",
      "injected_for_demo"
     ],
     "properties": {
      "received": {
       "description": "AI 回傳的摘錄句數（不含示範注入的測試資料）。",
       "type": "integer",
       "minimum": 0
      },
      "accepted": {
       "description": "通過逐字比對、放進臨床脈絡區塊的句數。",
       "type": "integer",
       "minimum": 0
      },
      "rejected": {
       "description": "被擋下的摘錄（含示範注入的測試資料，以 injected 標示）。",
       "type": "array",
       "items": {
        "$ref": "#/$defs/rejectedExcerpt"
       }
      },
      "injected_for_demo": {
       "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"
        }
       }
      }
     }
    }
   }
  },
  "timing": {
   "description": "分段計時（毫秒）。AI 摘錄與知識庫查詢同時進行，總時間不是各段相加。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "agent_ms",
    "agent_mode",
    "kb_ms",
    "kb_calls",
    "service_ms",
    "total_ms",
    "note",
    "agent_calls",
    "steps"
   ],
   "properties": {
    "agent_ms": {
     "description": "AI 摘錄耗時（毫秒）；沒有病程紀錄、未呼叫 AI 時為 null。",
     "type": [
      "integer",
      "null"
     ],
     "minimum": 0
    },
    "agent_mode": {
     "description": "AI 摘錄的來源。maiagent＝即時呼叫 MaiAgent；recorded＝回放事先錄製的 AI 摘錄（示範用）；offline_mock＝離線假回應（測試用）；not_called＝沒有病程紀錄，未呼叫 AI；null＝AI 呼叫失敗。",
     "enum": [
      "maiagent",
      "recorded",
      "offline_mock",
      "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
      }
     }
    }
   }
  },
  "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"
       ]
      },
      "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"
     }
    }
   }
  }
 },
 "$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/c001-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 寫的）。",
   "type": "object",
   "additionalProperties": false,
   "required": [
    "id",
    "type",
    "provider",
    "check",
    "related_section",
    "query",
    "status",
    "text",
    "text_origin",
    "citation",
    "version",
    "retrieved_at"
   ],
   "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：指引章節（對不到或查不到章節）、指引列出的用藥、指引列出的療程、菌種用藥、兒科劑量、新生兒劑量。",
     "enum": [
      "劑量範圍",
      "交互作用",
      "重複用藥",
      "過敏與交叉過敏",
      "藥碼對照",
      "指引章節",
      "指引列出的用藥",
      "指引列出的療程",
      "菌種用藥",
      "兒科劑量",
      "新生兒劑量"
     ]
    },
    "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＝培養尚未報告，依菌種的查詢待培養結果、本次未查詢。",
     "enum": [
      "ok",
      "none_found",
      "unavailable",
      "unmapped",
      "no_pediatric",
      "no_entry",
      "no_data",
      "not_checked",
      "pending_culture"
     ]
    },
    "result": {
     "description": "劑量範圍檢核結果（劑量範圍才有）；沒有檢核結果時為 null。",
     "enum": [
      "在範圍內",
      "高於範圍上限",
      "低於範圍下限",
      null
     ]
    },
    "severity": {
     "description": "嚴重度，照錄 Medi-Span（交互作用、過敏與交叉過敏才有）。示範資料出現的值：重大、中度、輕微、低。",
     "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＝指引原文。",
     "enum": [
      "system_generated",
      "guideline_verbatim"
     ]
    },
    "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": "Sanford 查詢依據。diagnosis＝依照會診斷（診斷對照表）；organism＝依培養菌種；drug＝依擬開藥物；Medi-Span 不帶此欄。",
     "enum": [
      "diagnosis",
      "organism",
      "drug"
     ]
    },
    "error_code": {
     "description": "知識庫無法查詢的原因代碼（status 為 unavailable 才有）。timeout＝逾時；connection_failed＝轉接服務無法連線；other＝其他錯誤。",
     "enum": [
      "timeout",
      "connection_failed",
      "other"
     ]
    }
   },
   "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 摘錄。",
     "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 摘錄失敗＝病程摘錄助理呼叫失敗。",
     "enum": [
      "AI 摘錄失敗",
      "已過期",
      "本次未收到",
      "查無資料",
      "查詢失敗",
      "知識庫暫無法查詢"
     ]
    },
    "reason_code": {
     "description": "缺漏原因代碼（供 xAMS 決定呈現方式）。query_failed＝查詢失敗：資料可能存在但這次沒拿到，請醫師到 HIS 確認；not_found＝查無資料：FHIR 確定沒有這類資料；stale＝已過期；not_received＝本次未收到必要項目；kb_unavailable＝知識庫暫無法查詢；agent_failed＝AI 摘錄失敗。",
     "enum": [
      "query_failed",
      "not_found",
      "stale",
      "not_received",
      "kb_unavailable",
      "agent_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 字",
      "原文中找不到這段文字（不是逐字摘錄）",
      "重複的摘錄"
     ]
    },
    "injected": {
     "description": "示範標記，用來標出刻意注入的測試句（不是 AI 產生的）；正式版不含此欄位。",
     "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"
     }
    },
    "injected": {
     "description": "示範標記，用來標出刻意注入的測試句（不是 AI 產生的）；正式版不含此欄位。",
     "const": true
    }
   }
  }
 }
}