主要改动: fix(docker/dify-deploy): 修复脚本核心功能 - heredoc 单引号 bug: 'ENVEOF' 改为 ENVEOF,变量正确展开 - 端口默认值 8083/8084/8085 对齐 .env.prod 生产配置 - 加 dc_cmd() 兼容 docker-compose v1/v2 plugin - openssl rand 生成强随机密码与 SECRET_KEY(42 字符) - install 跳过已存在 .env,保护用户配置(管理员密码/SECRET_KEY) - read -p < /dev/tty 兼容非 tty 环境(CI/CD) - show-config 改用 DIFY_NGINX_PORT(nginx 入口)而非 APP_WEB_PORT docs(mvp-design): 修正 §3.2 workflow inputs 描述 - 实际只有 query,删除错误的 user_id input 声明 - 节点序列图同步更新 feat(aiChatService): 新增 Dify 客户端与适配器 - service/dify_client.go: Dify Workflow 调用 + SSE 解析 - service/dify_adapter.go: 与现有 chat_service 桥接 - provider/ai_chat_provider.go: Dubbo 入口简化 - main.go: 装配 ConversationRepository + DifyClient feat(migrations): 新增 AI 搭子会话表 ai_chat.sql - ai_conversations / ai_messages 表 + 索引 docs: 新增 Dify 集成设计文档 - 2026-06-29-ai-chat-dify-mvp-design.md (MVP 实施级) - 2026-06-29-ai-chat-dify-integration-v2-design.md (V2 演进路线图) - docs/dify/角角.yml (Workflow DSL 导出) config: 更新 env 模板与 docker 配置 - backend/.env.example: DIFY_* 环境变量声明 - docker/.env.prod: DIFY_API_BASE 对齐 8083 - docker/build.sh: 微调 - CLAUDE.md: 项目规范补充 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/topfans/backend/services/aiChatService/model"
|
||
)
|
||
|
||
// AIProvider 流式对话接口(实现类:LLMService、DifyAdapter)
|
||
type AIProvider interface {
|
||
StreamChat(ctx context.Context, messages []model.Message) (StreamReader, error)
|
||
}
|
||
|
||
// ChatService 对话服务
|
||
type ChatService struct {
|
||
aiProvider AIProvider
|
||
personaService *PersonaService
|
||
memoryService *MemoryService
|
||
auditService *AuditService
|
||
contextTTL int // 上下文过期时间(秒)
|
||
triggerTurns int // 触发记忆提取的轮数
|
||
}
|
||
|
||
// NewChatService 创建对话服务
|
||
func NewChatService(
|
||
aiProvider AIProvider,
|
||
personaService *PersonaService,
|
||
memoryService *MemoryService,
|
||
auditService *AuditService,
|
||
contextTTL int,
|
||
triggerTurns int,
|
||
) *ChatService {
|
||
return &ChatService{
|
||
aiProvider: aiProvider,
|
||
personaService: personaService,
|
||
memoryService: memoryService,
|
||
auditService: auditService,
|
||
contextTTL: contextTTL,
|
||
triggerTurns: triggerTurns,
|
||
}
|
||
}
|
||
|
||
// GetHistory 获取对话历史
|
||
func (s *ChatService) GetHistory(ctx context.Context, sessionID string) ([]model.Message, error) {
|
||
return s.memoryService.GetContext(ctx, sessionID)
|
||
}
|
||
|
||
// SaveContext 保存对话上下文
|
||
func (s *ChatService) SaveContext(ctx context.Context, sessionID string, messages []model.Message, personaID string) error {
|
||
return s.memoryService.SaveContext(ctx, sessionID, messages, personaID)
|
||
}
|
||
|
||
// ExtractMemory 提取记忆
|
||
func (s *ChatService) ExtractMemory(ctx context.Context, userID int64, recentMessages []model.Message) error {
|
||
if ShouldExtractMemory(recentMessages, s.triggerTurns) {
|
||
return s.memoryService.ExtractMemory(ctx, userID, recentMessages)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// GetWelcomeMessage 获取欢迎消息
|
||
func (s *ChatService) GetWelcomeMessage(sessionID string, userID int64, starID int64) string {
|
||
// 默认欢迎消息
|
||
return "亲爱的你来辣 ~~"
|
||
}
|
||
|
||
// StreamChat 流式对话(委托给 aiProvider)
|
||
func (s *ChatService) StreamChat(ctx context.Context, messages []model.Message) (StreamReader, error) {
|
||
return s.aiProvider.StreamChat(ctx, messages)
|
||
} |