-- 8. ai:调用流水账(架构 §3.6) -- 来源:docs/architecture/schema-draft.sql(2026-11-03 验证过的草案) CREATE TABLE ai_call ( -- 不存原文;输入按 input_hash 去对象存储取 id uuid PRIMARY KEY, tenant_id uuid NOT NULL, job_type text NOT NULL CHECK (job_type IN ('parse_job', 'check_run')), job_id uuid NOT NULL, -- 不加外键:删除项目后成本数据保留 task text NOT NULL, -- 如 extract.qualification capability text NOT NULL CHECK (capability IN ('vision', 'fast', 'reasoning', 'embedding')), provider text NOT NULL, model text NOT NULL, prompt_id text NOT NULL, prompt_version text NOT NULL, prompt_hash bytea NOT NULL, input_hash bytea NOT NULL, output_hash bytea, tokens_in int, tokens_out int, tokens_cached int, latency_ms int, output_mode text CHECK (output_mode IN ('json_schema', 'json_mode', 'repaired')), error_class text CHECK (error_class IN ('timeout', 'rate_limited', 'server', 'network', 'bad_output')), cost_micro_cny bigint, -- 估算成本,百万分之一元 created_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX ON ai_call (tenant_id, job_id); CREATE INDEX ON ai_call (created_at); CREATE TRIGGER ai_call_append_only BEFORE UPDATE OR DELETE ON ai_call FOR EACH ROW EXECUTE FUNCTION forbid_change();