Go to file
hailin bb1a1139a3 feat(agents): add 4-layer response quality control — structured outputs, LLM judge, smart truncation
AI回复质量硬约束系统,解决核心问题:AI无法用最少的语言精准回答用户问题。

## 四层防线架构

### Layer 1 — Prompt 优化 (软约束)
- coordinator-system-prompt.ts: 新增"最高优先级原则:精准回答"章节
  - 意图分类表(7种)+ 每种对应长度和回答策略
  - 错误示范 vs 正确示范对比
  - "宁可太短,不可太长"原则
  - 最终提醒三条:精准回答 > 准确性 > 简洁就是专业
- policy-expert-prompt.ts: 精简输出格式
- objection-handler-prompt.ts: 微调

### Layer 2 — Structured Outputs (格式约束)
- 新文件 coordinator-response.schema.ts: Zod schema 定义
  - intent: 7种意图分类 (factual/yes_no/comparison/assessment/objection/detailed/casual)
  - answer: 回复文本
  - followUp: 可选跟进问题
- agent-loop.ts: 通过 output_config 传入 Claude API,强制 JSON 输出
  - 流式模式下抑制 text delta(JSON 片段不展示给用户)
  - 流结束后解析 JSON,提取 answer 字段 yield 给前端
  - JSON 解析失败时回退到原始文本(安全降级)
- coordinator-agent.service.ts: 传入 zodOutputFormat(CoordinatorResponseSchema)
- agent.types.ts: AgentLoopParams 新增 outputConfig 字段

### Layer 3 — LLM-as-Judge (语义质检)
- evaluation-rule.entity.ts: 新增 LLM_JUDGE 规则类型(第9种)
- evaluation-gate.service.ts:
  - 注入 ConfigService + 初始化 Anthropic client (Haiku 4.5)
  - evaluateRule 改为 async(支持异步 LLM 调用)
  - 新增 checkLlmJudge():评估 relevance/conciseness/noise 三维度
  - 可配置阈值:minRelevance(7), minConciseness(6), maxNoise(3)
  - 5s 超时 + 异常默认通过(非阻塞)
  - EvaluationContext 新增 userMessage 字段
- coordinator-agent.service.ts: 传入 userMessage 到评估门控

### Layer 4 — 程序级硬截断 (物理约束)
- coordinator-response.schema.ts:
  - INTENT_MAX_ANSWER_LENGTH: 按意图限制字符数
    factual=200, yes_no=120, comparison=250, assessment=400,
    objection=200, detailed=500, casual=80
  - MAX_FOLLOWUP_LENGTH: 80 字符
  - smartTruncate(): 在句子边界处智能截断(中英文标点)
- agent-loop.ts: JSON 解析后按 intent 强制截断 answer 和 followUp
- max_tokens 从 4096 降至 2048

## Bug 修复
- agent-loop.ts: currentTextContent 在 content_block_stop 时被重置为空字符串,
  导致评估门控收到空文本。改为从 finalMessage.content 提取 responseText。

## 依赖升级
- @anthropic-ai/sdk: 0.52.0 → 0.73.0 (支持 output_config)
- 新增 zod@4.3.6 (Structured Output schema 定义)

## 文件清单 (1 new + 10 modified)
- NEW: agents/schemas/coordinator-response.schema.ts
- MOD: agents/coordinator/agent-loop.ts (核心改造)
- MOD: agents/coordinator/coordinator-agent.service.ts
- MOD: agents/coordinator/evaluation-gate.service.ts
- MOD: agents/types/agent.types.ts
- MOD: agents/prompts/coordinator-system-prompt.ts
- MOD: agents/prompts/policy-expert-prompt.ts
- MOD: agents/prompts/objection-handler-prompt.ts
- MOD: domain/entities/evaluation-rule.entity.ts
- MOD: package.json + pnpm-lock.yaml

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-07 01:01:05 -08:00
.claude fix(evolution): handle super admin login without tenant context 2026-01-26 08:20:26 -08:00
database/migrations fix(database): add DELETED to conversations status CHECK constraint 2026-02-06 22:25:31 -08:00
docs feat(agents): implement multi-agent collaboration architecture 2026-02-06 04:26:39 -08:00
infrastructure fix(database): sync init.sql schema with all migrations for 100% consistency 2026-02-06 22:33:41 -08:00
kong feat(kong): add analytics and audit routes for evolution-service 2026-01-25 08:22:30 -08:00
nginx fix(nginx): fix admin SPA routing fallback to correct index.html 2026-02-06 23:24:22 -08:00
packages feat(agents): add 4-layer response quality control — structured outputs, LLM judge, smart truncation 2026-02-07 01:01:05 -08:00
scripts fix(db): add multi-tenancy migration for conversation-service tables 2026-02-06 08:39:10 -08:00
.builder.Dockerfile fix(tenant): use TenantContextModule.forRoot() for global tenant context 2026-01-26 04:31:56 -08:00
.dockerignore Initial commit: iConsulting 香港移民咨询智能客服系统 2026-01-09 00:01:12 -08:00
.env.example feat(embedding): add OpenAI proxy support for IP-based URLs 2026-01-23 05:42:22 -08:00
.gitignore Initial commit: iConsulting 香港移民咨询智能客服系统 2026-01-09 00:01:12 -08:00
README.md docs: add link to Agent Evaluation Report in README 2026-01-23 04:56:27 -08:00
deploy.sh feat(multi-tenant): add super admin module and user profile pages 2026-01-26 06:41:11 -08:00
docker-compose.yml feat(multi-tenant): add super admin module and user profile pages 2026-01-26 06:41:11 -08:00
iconsulting部署架构.jpg Initial commit: iConsulting 香港移民咨询智能客服系统 2026-01-09 00:01:12 -08:00
package.json feat(multi-tenant): add super admin module and user profile pages 2026-01-26 06:41:11 -08:00
pnpm-lock.yaml feat(agents): add 4-layer response quality control — structured outputs, LLM judge, smart truncation 2026-02-07 01:01:05 -08:00
pnpm-workspace.yaml Initial commit: iConsulting 香港移民咨询智能客服系统 2026-01-09 00:01:12 -08:00
tsconfig.base.json Initial commit: iConsulting 香港移民咨询智能客服系统 2026-01-09 00:01:12 -08:00
turbo.json refactor(services): implement 4-layer Clean Architecture for all backend services 2026-01-24 22:18:22 -08:00
优才.jpg docs: add QMAS (优才计划) reference image 2026-01-22 23:47:03 -08:00
香港移民类别.jpg Initial commit: iConsulting 香港移民咨询智能客服系统 2026-01-09 00:01:12 -08:00

README.md

iConsulting - 香港移民在线咨询系统

基于 Claude Agent SDK 的智能在线客服系统,专注于提供香港移民咨询服务。

功能特性

  • 智能咨询: 基于 Claude Agent SDK 的自然语言对话
  • 付费评估: 移民资格评估服务,支持支付宝/微信/信用卡
  • 知识增强: RAG + Neo4j 知识图谱
  • 自我进化: 从对话中学习,根据管理员指令调整
  • 长期记忆: 基于时间线的知识图谱记录
  • 多端支持: PC Web / H5 响应式设计

支持的移民类别

  1. 优才计划 (QMAS) - 行业翘楚、精英人士
  2. 专才计划 (GEP) - 专业人才
  3. 留学IANG - 非本地毕业生
  4. 高才通 (TTPS) - 高端人才
  5. 投资移民 (CIES) - 投资者
  6. 科技人才 (TechTAS) - 科技领域人才

技术架构

├── 前端 (Clean Architecture)
│   ├── React 18 + TypeScript
│   ├── TailwindCSS + Radix UI
│   └── Zustand + React Query
│
├── 后端 (DDD + Hexagonal + 微服务)
│   ├── NestJS
│   ├── Claude Agent SDK
│   └── TypeORM
│
└── 基础设施
    ├── PostgreSQL + pgvector (RAG)
    ├── Neo4j (知识图谱)
    ├── Redis (缓存)
    └── Kafka (消息队列)

项目结构

iconsulting/
├── packages/
│   ├── shared/                 # 共享类型、常量、工具
│   ├── web-client/             # 用户端 Web 应用
│   ├── admin-client/           # 管理端 Web 应用
│   └── services/               # 后端微服务
│       ├── api-gateway/        # API 网关
│       ├── user-service/       # 用户服务
│       ├── conversation-service/ # 对话服务 (核心)
│       ├── knowledge-service/  # 知识服务
│       ├── payment-service/    # 支付服务
│       ├── admin-service/      # 管理服务
│       └── evolution-service/  # 进化服务
│
├── infrastructure/
│   └── docker/                 # Docker 配置
│
├── DEVELOPMENT_GUIDE.md        # 详细开发指导
└── README.md

快速开始

1. 安装依赖

# 安装 pnpm (如果没有)
npm install -g pnpm

# 安装项目依赖
pnpm install

2. 启动基础设施

# 启动 Docker 容器 (PostgreSQL, Neo4j, Redis, Kafka)
pnpm docker:dev

3. 配置环境变量

# 复制环境变量示例文件
cp .env.example .env

# 编辑 .env 文件,填入必要的配置
# 特别是 ANTHROPIC_API_KEY

4. 运行数据库迁移

pnpm db:migrate

5. 启动开发服务器

# 启动所有服务
pnpm dev

访问:

环境变量

关键配置项:

# Claude API
ANTHROPIC_API_KEY=sk-ant-xxx

# 数据库
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=iconsulting
POSTGRES_PASSWORD=your_password
POSTGRES_DB=iconsulting

# Neo4j
NEO4J_URI=bolt://localhost:7687
NEO4J_USER=neo4j
NEO4J_PASSWORD=your_password

# 支付 (支付宝/微信)
ALIPAY_APP_ID=xxx
WECHAT_APP_ID=xxx

完整配置请参考 .env.example

文档

文档 说明
架构文档 系统整体架构、服务职责、数据流、部署配置
Agent 评估报告 Agent 能力评估、成熟度分析、升级路线图
开发指南 详细开发指导、代码示例、API 设计
MinIO 配置 对象存储配置说明

开发进度

已完成

  • 项目架构设计
  • 开发指导文档
  • Monorepo 配置
  • 共享类型定义
  • Docker 基础设施配置
  • 数据库 Schema
  • 对话服务 (Claude Agent SDK 集成)
  • 用户端前端基础框架

进行中

  • 用户服务
  • 知识服务 (RAG + Neo4j)
  • 支付服务
  • 管理服务 (自我进化)
  • 管理后台前端

贡献指南

  1. Fork 本仓库
  2. 创建特性分支 (git checkout -b feature/amazing-feature)
  3. 提交更改 (git commit -m 'Add some amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 创建 Pull Request

许可证

私有项目,保留所有权利。