iconsulting/packages/services
hailin 6767215f83 refactor(agents): remove Structured Output (Layer 2) to enable true streaming
背景:
在 commit bb1a113 中引入了 4 层回复质量控制体系:
- Layer 1: System Prompt (1095行详细指导)
- Layer 2: Structured Output (Zod schema → output_config)
- Layer 3: LLM-as-Judge (Haiku 4.5 评分)
- Layer 4: Per-intent hard truncation (已在 db8617d 移除)

Layer 2 (Structured Output) 的问题:
1. 阻塞流式输出 — output_config 强制模型输出 JSON,JSON 片段无法展示给
   用户,导致整个响应缓冲后才一次性输出
2. Zod 验证频繁崩溃 — intent 枚举值不匹配时 SDK 抛错,已出现 4 次 hotfix
   (b55cd4b, db8617d, 7af8c4d, 及本次)
3. followUp 字段导致内容丢失 — 模型将回答内容分到 followUp 后被过滤
4. intent 分类仅用于日志,对用户体验无价值
5. z.string() 无 .max() 约束 — 实际不控制回答长度

移除后,回答质量由以下机制保证(全部保留):
- Layer 1: System Prompt — 意图分类表、回答风格、长度指导
- Layer 3: LLM-Judge — 相关性/简洁性/噪音评分,不合格则自动重试
- API max_tokens: 2048 — 硬限制输出上限

改动:
- coordinator-agent.service.ts: 移除 zodOutputFormat/CoordinatorResponseSchema
  import 和 outputConfig 参数
- agent-loop.ts: 移除 text_delta 中的 outputConfig 守卫(文本现在直接流式
  输出)、移除 output_config API 参数、移除两个 Structured Output 验证失败
  恢复 catch 块、移除 JSON 解析 + safety net 块
- agent.types.ts: 从 AgentLoopParams 接口移除 outputConfig 字段
- coordinator-response.schema.ts: 清空 Zod schema/工具函数,保留历史备注

效果:
- 用户现在能看到逐字流式输出(token-by-token streaming)
- 消除了 Structured Output 相关的所有崩溃风险
- 代码净减 ~130 行

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-07 11:15:48 -08:00
..
conversation-service refactor(agents): remove Structured Output (Layer 2) to enable true streaming 2026-02-07 11:15:48 -08:00
docling-service fix(docling): align volume mount with HF default cache path 2026-02-07 07:38:28 -08:00
evolution-service fix(analytics): handle statDate as string from database 2026-01-26 08:40:09 -08:00
file-service fix(files): replace MinIO presigned URLs with API proxy + base64 for Claude 2026-02-07 04:49:39 -08:00
knowledge-service feat(knowledge): add Docling document parsing microservice 2026-02-07 05:24:10 -08:00
payment-service fix(tenant): use TenantContextModule.forRoot() for global tenant context 2026-01-26 04:31:56 -08:00
user-service fix(nginx): fix admin location try_files path and add multi-tenancy migrations 2026-01-26 07:46:52 -08:00