Calls Schema

Source of truth: callytics-common/src/db/schema/calls.ts (verified 2026-06-15) 表名: calls主键: telephony_session_id Coaching 字段:practicalCoachingScenario / practicalCoachingFeedback / reviewCategory / reviewReason 物理在 calls 表内,逻辑归 coaching-schema.md(本页标 ↗)


1. 表说明

Calls 表镜像 RingCentral Call Log API 响应(UserCallLogRecord)+ AI 分析结果:

  • RC 字段名在 TypeScript property 里原样保留;SQL 列用 snake_case,Drizzle 自动映射
  • RC 元数据由 transcribe-processor Lambda 写入(通话管道早期)
  • AI 分析字段由 ai-analysis-processor Lambda 在分析完成后 direct write
  • 所有枚举字段都是 text 列 + 注释里写约束,无 DB CHECK,无 const 数组 — 合法值靠写入方(AI pipeline)遵守

写入者列说明:

  • code = transcribe-processor / message-processor 等管道自动写入(RC 元数据 / 隔离字段 / pipeline 时间戳)
  • ai = ai-analysis-processor 写入(AI 分析字段 / cost)

2. Schema 全字段

90 列按存储顺序列出。Writer 列:code = transcribe-processor / message-processor 等管道;ai = ai-analysis-processor;ai (transcribe-processor) 等表示某 Lambda 专写。AI 枚举字段(callState / customerType 等)全在 §3 AI 枚举清单展开。

#FieldTypeNullableEnum / ValuesWriter用途
1telephonySessionIdtext (PK)NOT NULLcodeRC telephony session ID(每通通话唯一,主键)
2idtextNULLcodeRC 内部 call log record ID
3sessionIdtextNULLcodeRC session ID
4startTimetimestamptzNOT NULLcode通话开始时间
5durationintegerNULLcode通话时长(秒)
6typetextNULLVoice | Faxcode通话类型
7transporttextNULLPSTN | VoIPcode传输方式
8directiontextNOT NULLInbound | Outboundcode通话方向
9resulttextNULLAccepted | Missed | No Answer | Busy | Voicemail | Hang Up | ...codeRC 通话结果
10actiontextNULLPhone Call | VoIP Call | Incoming Fax | ...codeRC 动作类型
11reasontextNULLAccepted | Connected | Busy | BlindTransfer | ...code挂断原因
12reasonDescriptiontextNULLcode原因的自由文本描述
13shortRecordingbooleanNULLtrue / falsecode录音过短(< 30s)无法存储时为 true
14deletedbooleanNULLtrue / falsecode软删除标记(ISync 用)
15lastModifiedTimetimestamptzNULLcodeRC 最后修改此记录的时间
16fromPhoneNumbertextNULLcode主叫号码(from RC from 对象拍平)
17fromNametextNULLcode主叫名
18fromExtensionIdtextNULLcode主叫分机 ID
19fromExtensionNumbertextNULLcode主叫分机号
20fromLocationtextNULLcode主叫位置
21fromDeviceIdtextNULLcode主叫设备 ID
22toPhoneNumbertextNULLcode被叫号码(from RC to 对象拍平)
23toNametextNULLcode被叫名
24toExtensionIdtextNULLcode被叫分机 ID
25toExtensionNumbertextNULLcode被叫分机号
26toLocationtextNULLcode被叫位置
27toDeviceIdtextNULLcode被叫设备 ID
28extensionIdtextNULLcodecall record 所属分机(owner)
29recordingIdtextNULLcode录音 ID(from RC recording 对象)
30recordingTypetextNULLcode录音类型
31recordingContentUritextNULLcode录音内容 URI
32billingCostIncludednumericNULLcode套餐内话费(from RC billing 对象)
33billingCostPurchasednumericNULLcode额外购买话费
34messageIdtextNULLcode关联的语音留言/传真消息 ID(from RC message 对象)
35messageTypetextNULLcode消息类型
36delegateIdtextNULLcode代理(秘书)ID(from RC delegate 对象)
37delegateNametextNULLcode代理名
38legsjsonbNULLcodeRC Detailed view 的完整 legs 数组
39rawCallLogjsonbNULLcode完整 Call Log API 响应(不截断)
40accountIdtextNOT NULLcodeRC account ID(多租户隔离审计字段)
41legacySiteIdtext (SQL site_id)NOT NULLcode@deprecated — 与 account_id 冗余,保留向后兼容。注意 TS 名 legacySiteId ↔ SQL 列 site_id
42franchiseIdtextNOT NULLcode品牌标识
43storeIdtextNULLcodeDynamoDB StoresV2 store UUID,终态隔离键。由 ai-analysis-processor + transcribe-processor 经 resolvePhoneIdentity() 写入。Phase 3 rollout 前为 NULL
44clientIdtextNULLcode我们的复合键 {franchise}-{siteId}
45durationMsintegerNULLcode时长(毫秒,高精度)
46internalTypetextNULLcode内部通话类型分类(TollFreeNumber / Local 等)
47partyExtensionIdtextNULLcodeWebhook party 分机 ID
48callStatetextNULL6 值,见 §3ai通话状态(驱动下游 gate 决策)
49staffNametextNULLai员工姓名(AI 从对话识别)
50staffConfidencenumericNULL0-1ai员工识别置信度
51originalStaffNametextNULLai首次修订前的原始员工名(审计)
52amendmentHistoryjsonbNULL[{from, to, amendedBy, amendedAt, reason}]ai修订历史
53customerTypetextNULL5 值,见 §3ai客户类型
54customerConfidencenumericNULL0-1ai客户类型置信度
55customerEvidencetextNULLai判断客户类型的证据
56primaryTopicTypetextNULLai主题类型分类
57primaryCategorytextNULL4 值,见 §3ai主题大类
58primarySubcategorytextNULLai主题子分类
59primaryOutcomeResulttextNULL6 值,见 §3ai主结果
60primaryOutcomeReasoningtextNULLai结果判断的推理过程
61creditCardCapturedtextNULLyes | no | naai是否采集到信用卡
62primaryEvidencetextNULLai支持主题判断的转录证据
63secondaryTopicsjsonb (string[])NULLai二级主题子分类列表
64revenuePrioritytextNULL4 值,见 §3ai收入优先级
65revenuePriorityEvidencetextNULLai收入优先级判断的证据
66executiveSummarytextNULLaiAI 生成的通话摘要
67followUpNeededtextNULLyes | noai是否需要跟进
68followUpReasonsjsonb (string[])NULLai需要跟进的原因列表
69practicalCoachingScenariotextNULLaiAI 识别的辅导场景 — 详见 coaching-schema.md
70practicalCoachingFeedbackjsonbNULL{timestamp, what_was_said, context, ...}aiAI 详细辅导反馈 — 详见 coaching-schema.md
71reviewCategorytextNULL6 值,见 coaching-schema.mdaiAI 通话质量评估 — 详见 coaching-schema.md
72reviewReasontextNULLaireviewCategory ≠ NOT_REQUIRED 时的说明(1-2 句话)— 详见 coaching-schema.md
73inputTokensintegerNULLai输入 token 数
74outputTokensintegerNULLai输出 token 数
75reasoningTokensintegerNULLai推理 token 数(部分模型支持)
76totalTokensintegerNULLai总 token 数
77estimatedCostUsdnumericNULLai预估成本(USD)
78aiModelUsedtextNULLai实际使用的 AI 模型 ID
79s3AnalysisPathtextNULLaiAI 分析结果在 S3 的完整路径
80callLogFetchedAttimestamptzNULLtranscribe-processorRC call log 已拉取存储(Disconnected 事件的首步)。Pipeline 进度时间戳:NULL=未到达,NOT NULL=到达时间
81recordingAvailableAttimestamptzNULLtranscribe-processor确认 RC 录音可下载
82transcribeStartedAttimestamptzNULLtranscribe-processor开始下载 + 转录
83transcribeCompletedAttimestamptzNULLtranscribe-processor转录完成
84aiAnalysisStartedAttimestamptzNULLai-analysis-processor开始 AI 分析
85aiAnalysisCompletedAttimestamptzNULLai-analysis-processorAI 分析完成
86contactsCompletedAttimestamptzNULLcontacts-analyzer / ai-analysis-processortask 已生成;或 AI 分析跳过(voicemail / no-answer)
87leadIdtextNULLcodeLead ID(用于关联 contacts 表)
88contactPhonetextNULLcode客户电话号码(用于关联 contacts 表)
89syncedAttimestamptzNOT NULLDEFAULT NOW()code首次同步此记录的时间
90updatedAttimestamptzNOT NULLDEFAULT NOW(), $onUpdatecode最后更新时间(Drizzle db.update() 自动刷新)

Pipeline 进度时间戳(#80-86)Reconciliation 用 WHERE X_at IS NOT NULL AND Y_at IS NULL AND age > threshold 找卡住的通话。COALESCE 规则:_started_at / _available_at 保留首个值;_completed_at 取最新值。


3. AI 枚举清单

所有 AI 枚举字段都是 text 列 + 注释约束,无 DB CHECK。合法值靠写入方(prompt + Lambda 代码)遵守。Reviewer 验 prompt 输出时按本表对照。

枚举字段含义
callStatehuman_conversation | voicemail | no_answer | busy_signal | system_error | No Transcript通话状态(驱动下游 gate)
customerTypeprospective_client | existing_member | former_member | returning_visitor | other客户类型
primaryCategoryrevenue_impacting | service | scheduling | other主题大类
primaryOutcomeResultsuccess | attempted | na | cancelled | retained | pending_follow_up主结果
revenuePriorityhigh | medium | low | none收入优先级
followUpNeededyes | no是否需要跟进
creditCardCapturedyes | no | na是否采集到信用卡

reviewCategory 枚举(NOT_REQUIRED ~90-95% | EXEMPLARY | FAILURE | CHURN_RISK | MISSED_OPPORTUNITY | ESCALATION)归 coaching-schema.md


4. Prompt Output ↔ Schema 字段对照(给 reviewer 用)

Reviewer 用本表验证 prompt 输出 JSON 字段是否落在 calls 表内。

Prompt写入字段备注
01 TriagecallState / staffName / staffConfidence(+ worth_analyzing 不入库,仅 gate Classify/Coaching)Triage 输出在 pipeline.ts:192-193 上调,最终随 Classify 一起 persist 到 calls(见 lambda/ai-analysis-processor/src/core/stages/triage.ts:5)
02 ClassifycustomerType / customerConfidence / customerEvidence / primaryCategory / primarySubcategory / primaryTopicType / primaryOutcomeResult / primaryOutcomeReasoning / primaryEvidence / creditCardCaptured / revenuePriority / revenuePriorityEvidence / executiveSummary / followUpNeeded / followUpReasons / secondaryTopics主要写入 prompt — reviewer 重点验证输出字段 ∈ §2 行 48-72(去掉 Triage 已写的 callState/staffName/staffConfidence)
03 Verify(conditional verification fields,不直接写 calls 表)n/a
04 CoachingpracticalCoachingScenario / practicalCoachingFeedback / reviewCategory / reviewReasoncoaching-schema.md
05 Contact Profile(写 contacts 表,见 contacts-schema.md)n/a
06 Task Decision(写 tasks + task_progress_events,见 tasks-schema.md)n/a
07 Task Playbook(read-only)n/a

5. Appendix A — Re-verification commands

2026-06-01 verified。文档 stale(>30 天)时重跑下方命令并比对。

# 1. calls 表所有 column
grep -E "^\s*[a-zA-Z]+: (text|integer|timestamp|boolean|numeric|jsonb)\(" \
  ../../../../../callytics-common/src/db/schema/calls.ts

# 2. 枚举值(注释里的)
grep -E "/\*\*.*\|" ../../../../../callytics-common/src/db/schema/calls.ts

# 3. Task pipeline design 是否动 calls
grep -i "calls\." ../../tasks-feature/design/task-pipeline-deliverable-codex.md
# 期望: 只 reference 不修改 schema

# 4. Unified pipeline 是否动 calls
grep -i "calls\." ../../unified-pipeline/unified-pipeline-final.md
# 期望: 只 reference 不修改 schema

6. Cross-References