# Implementation Plan: Coze AI Integration ## Overview 本实现计划将重构AiChatServiceImpl,添加通用的Coze工作流调用方法,并优化EpicScriptServiceImpl#createScript接口以调用Coze AI生成剧本内容。实现采用增量方式,每个任务都建立在前一个任务的基础上。 ## Tasks - [x] 1. 扩展AiConfigService接口和实现 - [x] 1.1 在AiConfigService接口中添加getByConfigKey方法 - 添加方法签名:`AiConfig getByConfigKey(String configKey)` - 添加方法级注释说明功能和参数 - _Requirements: 5.1_ - [x] 1.2 在AiConfigServiceImpl中实现getByConfigKey方法 - 使用LambdaQueryWrapper查询config_key匹配且is_enabled=1的配置 - 返回查询结果,不存在则返回null - _Requirements: 1.2, 5.1, 5.2_ - [x] 2. 扩展AiChatService接口 - [x] 2.1 在AiChatService接口中添加callWorkflowByConfigKey方法 - 添加方法签名:`String callWorkflowByConfigKey(String configKey, String input, String userId)` - 添加重载方法:`String callWorkflowByConfigKey(String configKey, Map parameters, String userId)` - 添加方法级注释说明功能、参数和返回值 - _Requirements: 1.1, 1.4_ - [x] 3. 实现通用工作流调用方法 - [x] 3.1 在AiChatServiceImpl中实现callWorkflowByConfigKey方法 - 调用aiConfigService.getByConfigKey获取配置 - 验证配置存在且启用,否则抛出异常 - 构建工作流请求(workflow_id, user_id, stream=true, parameters.input) - 设置请求头(Authorization, Content-Type) - _Requirements: 1.2, 1.3, 1.6, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6_ - [x] 3.2 实现流式响应处理方法 - 解析SSE格式响应(event:和data:行) - 提取event为Message且node_type为End的content - 从content的JSON中提取output字段 - 处理Done事件完成流处理 - _Requirements: 3.1, 3.2, 3.3, 3.4_ - [x] 3.3 实现错误处理逻辑 - 处理配置不存在/禁用的情况 - 处理API调用失败(非200状态码) - 处理流式解析失败 - 确保错误消息不包含敏感信息 - _Requirements: 1.6, 3.5, 6.1, 6.2, 6.4, 6.5_ - [x] 3.4 编写Property 1属性测试:请求格式正确性 - **Property 1: Request Format Correctness** - **Validates: Requirements 2.1, 2.2, 2.3, 2.4, 2.5, 2.6** - [x] 3.5 编写Property 2属性测试:流式响应解析 - **Property 2: Stream Response Parsing** - **Validates: Requirements 3.1, 3.2, 3.3** - [x] 4. Checkpoint - 验证通用AI调用服务 - 确保所有测试通过,如有问题请询问用户 - [x] 5. 实现爽文剧本AI生成 - [x] 5.1 在EpicScriptServiceImpl中添加输入组装方法 - 创建assembleScriptInput方法 - 将EpicScriptCreateRequest的字段组装为格式化字符串 - 包含title, theme, style, length, plotIntro, plotTurning, plotClimax, plotEnding - _Requirements: 4.2_ - [x] 5.2 修改createScript方法调用AI生成 - 调用assembleScriptInput组装输入 - 调用aiChatService.callWorkflowByConfigKey("coze.course.life.generate", input, userId) - 解析AI返回的内容并存储到EpicScript实体 - 处理AI调用失败的情况 - _Requirements: 4.1, 4.3, 4.4, 4.5_ - [x] 5.3 编写Property 3属性测试:输入组装完整性 - **Property 3: Input Assembly Completeness** - **Validates: Requirements 4.2** - [x] 6. 实现配置参数合并 - [x] 6.1 实现custom_params合并逻辑 - 解析AiConfig.customParams JSON字符串 - 将custom_params与运行时参数合并 - 运行时参数优先级高于custom_params - _Requirements: 5.3_ - [x] 6.2 实现超时和重试配置 - 应用AiConfig.timeoutMs设置 - 实现基于retry_count和retry_delay_ms的重试逻辑 - _Requirements: 5.4, 5.5, 6.3_ - [x] 6.3 编写Property 4属性测试:配置应用正确性 - **Property 4: Configuration Application** - **Validates: Requirements 1.3, 5.2, 5.3** - [x] 7. 完善错误处理和日志 - [x] 7.1 完善错误消息格式 - 确保错误消息包含config_key和状态码 - 确保不暴露API token等敏感信息 - 添加详细的日志记录 - _Requirements: 6.1, 6.2, 6.4, 6.5_ - [x] 7.2 编写Property 5属性测试:错误消息质量 - **Property 5: Error Message Quality** - **Validates: Requirements 6.4, 6.5** - [x] 8. Final Checkpoint - 确保所有测试通过 - 运行所有单元测试和属性测试 - 确保所有测试通过,如有问题请询问用户 ## Notes - All tasks are required for comprehensive implementation - Each task references specific requirements for traceability - Checkpoints ensure incremental validation - Property tests validate universal correctness properties - Unit tests validate specific examples and edge cases - 实现语言:Java (Spring Boot) - 测试框架:JUnit 5