Commit Graph

7 Commits

Author SHA1 Message Date
hailin a914a80fc3 docs(api-guide): 更新API文档至生产配置 + 重新生成PDF
主要变更:
- 模型名称: xiaoai-engine-v1 → xiaoai-chat-v1 (对话) / xiaoai-embed-v1 (向量化)
  与实际数据库配置的 modelAlias 保持一致
- model 参数说明: 明确区分对话接口和向量化接口使用不同模型名
- SDK 安装: 按接口类型(Messages / Chat Completions / Embeddings)分别说明
- FAQ: 补充两个模型名的对应关系
- 所有代码示例中的 model 值已同步更新
- 重新生成 PDF 文件

当前生产 API Key 配置:
  - Anthropic Messages → modelAlias: xiaoai-chat-v1
  - OpenAI Chat Completions → modelAlias: xiaoai-chat-v1
  - OpenAI Embeddings → modelAlias: xiaoai-embed-v1

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 02:42:33 -08:00
hailin df754ce8b8 feat(payment): P0 — 支付闭环,Agent 可创建真实订单并生成支付二维码
## 后端改动

### 新增 PaymentClientService
- 新建 `infrastructure/payment/payment-client.service.ts`
  - HTTP 客户端封装,调用 payment-service API(端口 3002)
  - 方法: createOrder, createPayment, checkPaymentStatus, getOrderStatus, getUserOrders
  - 基于 native fetch,模式与 KnowledgeClientService 一致
- 新建 `infrastructure/payment/payment.module.ts`
- AgentsModule 导入 PaymentModule

### 重写 generate_payment 工具
- 删除所有 MOCK 数据(fake orderId, placeholder QR URL)
- 实际调用 payment-service: createOrder → createPayment → 返回真实 QR URL
- 返回 orderId, paymentId, qrCodeUrl, paymentUrl, expiresAt

### 新增 check_payment_status 工具
- 查询订单支付状态(调用 payment-service GET /orders/:id/status)
- 返回 status, statusLabel(中文映射), paidAt
- 在 coordinator-tools.ts 和 concurrency map 中注册(只读 safe=true)

### 新增 query_order_history 工具
- 查询用户历史订单列表(调用 payment-service GET /orders)
- 返回 orders 数组含 orderId, serviceType, amount, status, createdAt
- 在 coordinator-tools.ts 和 concurrency map 中注册(只读 safe=true)

## 前端改动

### QR 码渲染
- 安装 qrcode.react 4.2.0
- ToolCallResult 组件使用 QRCodeSVG 渲染真实二维码
- 支持 qrCodeUrl(二维码)和 paymentUrl(跳转链接)两种支付方式
- 显示订单号、金额、过期时间

### 支付状态卡片
- check_payment_status 结果渲染为彩色状态卡片
- 已支付=绿色, 待支付=黄色, 已取消=红色, 已退款=橙色

### 订单历史列表
- query_order_history 结果渲染为订单列表卡片
- 每行显示: 类别、日期、金额、状态标签

### WebSocket 工具事件处理
- tool_result 事件收集到 pendingToolResults(chatStore 新增状态)
- stream_end 时将 toolResults 注入消息 metadata.toolCalls
- stream_start 时清空 pendingToolResults

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-07 01:17:14 -08:00
hailin 16cc0e4c08 feat(agents): implement multi-agent collaboration architecture
借鉴 Claude Code 的架构模式,将单一 Agent 重构为 Coordinator + 6 Specialist 多 Agent 协作系统。

## 新增文件 (36个)

### 架构设计文档 (docs/architecture/, 12个)
- 00-overview.md ~ 11-prompt-templates.md: 完整架构设计,覆盖所有 Agent 的详细设计、
  Prompt 模板、协作流程、工具并发系统、动态上下文注入

### 多 Agent 系统 (infrastructure/agents/, 23个)
- coordinator/coordinator-agent.service.ts: 主协调器,替代 ClaudeAgentServiceV2
- coordinator/agent-loop.ts: 核心递归 async generator 循环(参考 Claude Code aM())
- coordinator/context-injector.service.ts: 动态上下文注入(8种上下文类型按优先级注入)
- specialists/base-specialist.service.ts: Agent 基类(封装 Claude API 调用 + prompt 缓存)
- specialists/policy-expert.service.ts: 政策专家 (Sonnet 4, temp=0)
- specialists/assessment-expert.service.ts: 评估专家 (Sonnet 4, temp=0)
- specialists/strategist.service.ts: 策略顾问 (Sonnet 4, temp=0.3)
- specialists/objection-handler.service.ts: 异议处理 (Sonnet 4, temp=0.2)
- specialists/case-analyst.service.ts: 案例分析 (Haiku, temp=0)
- specialists/memory-manager.service.ts: 记忆管理 (Haiku, temp=0)
- prompts/coordinator-system-prompt.ts: 协调器 Prompt(1095行,13章)
- prompts/{policy,assessment,strategist,objection,case,memory}-*-prompt.ts: 各专家 Prompt
- tools/coordinator-tools.ts: 16个工具定义(6 Agent 调用 + 10 直接工具)
- tools/tool-execution-queue.ts: 并发执行队列(isConcurrencySafe 控制并行/串行)
- types/agent.types.ts: Agent 配置、输入/输出类型定义
- types/stream.types.ts: 流式事件类型(含 agent_start/complete/coordinator_thinking)
- types/context.types.ts: 上下文注入类型
- agents.module.ts: NestJS 模块注册

### 前端 Agent 状态展示 (1个)
- AgentStatusIndicator.tsx: 多 Agent 工作状态组件(含动画)

## 修改文件 (15个)

### 后端集成
- conversation.service.ts: 切换到 CoordinatorAgentService
- conversation.gateway.ts: 新增 agent_start/agent_complete/coordinator_thinking 事件
- claude.module.ts: 引入 AgentsModule
- agents.module.ts: 注册 ImmigrationToolsService(复用旧版生产测试的工具实现)
- knowledge-client.service.ts: 新增 search()/getUserContext() 便捷方法

### 旧代码标记 @deprecated
- claude-agent.service.ts, claude-agent-v2.service.ts
- strategy-engine.service.ts, intent-classifier.ts, response-gate.ts

### 前端适配
- chatStore.ts: 新增 ActiveAgent/CompletedAgent/CoordinatorPhase 状态
- useChat.ts: 新增 WebSocket 事件处理
- ChatWindow.tsx: 集成 AgentStatusIndicator
- globals.css: 新增 agentPulse/agentSlideIn 动画

### 共享类型
- conversation.types.ts: 新增 AGENT_START/AGENT_COMPLETE/COORDINATOR_THINKING 事件

## 核心设计决策

1. **新旧结合**: Coordinator 的 10 个直接工具委托给旧版 ImmigrationToolsService
   (经过生产测试的 Google Search、汇率 API、新闻 API 等),6 个 Agent 调用工具
   走新的 Specialist Agent 系统
2. **递归 async generator**: agent-loop 支持流式输出 + 工具递归 + 成本/轮次控制
3. **并行 Agent 执行**: ToolExecutionQueue 根据 isConcurrencySafe 自动并行/串行
4. **Prompt 缓存**: 所有 Agent 的 system prompt 使用 cache_control: ephemeral
5. **速率限制重试**: 429/529 指数退避,最多 2 次
6. **向后兼容**: LegacyConversationContext 类型别名,StreamChunk 扩展不破坏现有结构

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-06 04:26:39 -08:00
hailin d9b4c72894 feat(agent): implement 3-layer architecture for better response quality
Implement a three-layer architecture to improve AI response quality:

Layer 1 - Intent Classifier (intent-classifier.ts):
- Classifies user intent into 6 types: SIMPLE_QUERY, DEEP_CONSULTATION,
  ACTION_NEEDED, CHAT, CLARIFICATION, CONFIRMATION
- Determines suggested response length based on intent type
- Detects follow-up questions and extracts entities (visa types, etc.)
- Uses keyword matching for fast classification (no API calls)

Layer 2 - ReAct Agent (system-prompt.ts):
- Adds ReAct thinking framework to system prompt
- 4-step process: Understand -> Evaluate -> Act -> Generate
- Emphasizes concise responses, avoids redundant phrases
- Injects intent classification results to guide response strategy

Layer 3 - Response Gate (response-gate.ts):
- Quality checks: length, relevance, redundancy, completeness, tone
- Logs gate results for analysis and future optimization
- Can trim responses and remove redundant expressions

Integration (claude-agent.service.ts):
- Integrates all 3 layers in sendMessage flow
- Dynamically adjusts max_tokens based on intent type
- Collects full response for gate analysis

Documentation:
- Added AGENT_THREE_LAYER_ARCHITECTURE.md with detailed design docs

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 07:51:19 -08:00
hailin 36ce824db8 docs: add comprehensive agent tools documentation
Document all 10 tools available to the immigration consulting agent:
- 6 knowledge & memory tools (search, off-topic check, assessment, payment, memory, context)
- 4 real-time tools (datetime, web search, exchange rate, news)

Includes input parameters, return formats, implementation details, and configuration.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 00:49:10 -08:00
hailin 911132ab3e feat(agent): upgrade to Level 3 with real RAG, Memory and Evolution integration
## Summary
Upgrade iConsulting from Level 2 (48 points) to Level 3 (68 points) by
implementing real service-to-service integration between conversation-service
and knowledge-service.

## New Files
- knowledge-client.service.ts: HTTP client for knowledge-service APIs
- knowledge.module.ts: NestJS module for KnowledgeClientService
- AGENT_EVALUATION_REPORT.md: Agent capability evaluation report
- LEVEL3_UPGRADE_PLAN.md: Upgrade plan and completion report

## Changes

### RAG Integration
- search_knowledge tool now calls /api/v1/knowledge/retrieve
- check_off_topic tool calls /api/v1/knowledge/check-off-topic
- Results include real vector similarity search from knowledge base

### Memory Integration
- save_user_memory writes to PostgreSQL + Neo4j via knowledge-service
- collect_assessment_info saves user data to long-term memory
- generate_payment records payment intent to user memory
- New get_user_context tool retrieves user's historical memories

### Evolution Integration
- getAccumulatedExperience() fetches approved system experiences
- sendMessage() dynamically injects experiences into system prompt
- System learns from approved experiences across all conversations

## Expected Score Improvement
| Dimension  | Before | After | Delta |
|------------|--------|-------|-------|
| Tool Use   | 14/20  | 18/20 | +4    |
| Memory     | 12/20  | 16/20 | +4    |
| RAG        | 10/20  | 16/20 | +6    |
| Evolution  | 8/20   | 14/20 | +6    |
| Total      | 48     | 68    | +20   |

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-22 23:45:58 -08:00
hailin 4dbd6f075f docs: reorganize documentation and add architecture document
Changes:
- Create docs/ directory for centralized documentation
- Move DEVELOPMENT_GUIDE.md from root to docs/
- Add comprehensive ARCHITECTURE.md with:
  - System overview and architecture diagrams (ASCII)
  - Microservices responsibilities (6 services)
  - Technology stack details
  - Data flow diagrams (user conversation, admin evolution)
  - Production deployment architecture
  - Kong API routing table
  - Port mapping reference
  - Database schema overview (PostgreSQL, Neo4j, MinIO)
  - Security design notes
- Update README.md with documentation index table

Documentation structure:
  docs/
  ├── ARCHITECTURE.md      - System architecture reference
  └── DEVELOPMENT_GUIDE.md - Development guide and code examples

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-22 23:05:41 -08:00