Skip to content

Conversation

qiniu-ci
Copy link
Collaborator

@qiniu-ci qiniu-ci commented Jul 17, 2025

改动摘要

根据Issue要求,在PR review comment场景中,现在会获取所有历史comments(包括PR body、一般评论、代码行评论、Review评论)作为上下文信息提供给AI,以提高代码质量。修改保持了当前Comment作为主要指令的核心地位,历史信息仅作为补充上下文。

具体改动

1. 添加新的数据模型

  • 文件: /workspace/pkg/models/workspace.go
  • 变动: 添加了 PRAllComments 结构体,用于存储PR的所有评论信息:
    • PRBody: PR描述
    • IssueComments: 一般PR评论
    • ReviewComments: 代码行评论
    • Reviews: Review评论

2. 扩展GitHub客户端功能

  • 文件: /workspace/internal/github/client.go
  • 变动: 添加了 GetAllPRComments 方法,用于获取PR的所有评论历史:
    • 获取Issue Comments(一般PR评论)
    • 获取Review Comments(代码行评论)
    • 获取Reviews(Review评论)
    • 返回包含PR body在内的完整评论信息

3. 增强AI Agent处理逻辑

  • 文件: /workspace/internal/agent/agent.go
  • 变动:
    • 添加了 formatHistoricalComments 方法,用于格式化历史评论作为上下文
    • 修改了 ContinuePRWithArgs 方法,增加历史上下文获取和智能prompt构建
    • 修改了 FixPRWithArgs 方法,增加历史上下文获取和智能prompt构建
    • 在prompt中明确区分当前指令(主要任务)和历史信息(上下文参考)

4. 智能Prompt构建策略

  • 特点:
    • 优先执行当前Comment指令,历史信息仅作为上下文
    • 当有历史上下文时,构建包含PR描述、历史讨论的完整prompt
    • 当无历史上下文时,降级为简单prompt模式
    • 明确告知AI如何处理历史信息和当前指令的优先级关系

5. 错误处理和兼容性

  • 策略:
    • 获取历史评论失败时不中断流程,降级为简单模式
    • 处理Comment为nil的边界情况
    • 保持与现有API的完全兼容

这些改动确保了PR review comment场景下AI能够获得完整的背景信息,从而提供更高质量的代码建议和修改,同时保持了当前指令的核心地位。


AI 完整输出

Perfect! The implementation is complete and the code compiles successfully. Let me provide a summary of the changes:

改动摘要

根据Issue要求,在PR review comment场景中,现在会获取所有历史comments(包括PR body、一般评论、代码行评论、Review评论)作为上下文信息提供给AI,以提高代码质量。修改保持了当前Comment作为主要指令的核心地位,历史信息仅作为补充上下文。

具体改动

1. 添加新的数据模型

  • 文件: /workspace/pkg/models/workspace.go
  • 变动: 添加了 PRAllComments 结构体,用于存储PR的所有评论信息:
    • PRBody: PR描述
    • IssueComments: 一般PR评论
    • ReviewComments: 代码行评论
    • Reviews: Review评论

2. 扩展GitHub客户端功能

  • 文件: /workspace/internal/github/client.go
  • 变动: 添加了 GetAllPRComments 方法,用于获取PR的所有评论历史:
    • 获取Issue Comments(一般PR评论)
    • 获取Review Comments(代码行评论)
    • 获取Reviews(Review评论)
    • 返回包含PR body在内的完整评论信息

3. 增强AI Agent处理逻辑

  • 文件: /workspace/internal/agent/agent.go
  • 变动:
    • 添加了 formatHistoricalComments 方法,用于格式化历史评论作为上下文
    • 修改了 ContinuePRWithArgs 方法,增加历史上下文获取和智能prompt构建
    • 修改了 FixPRWithArgs 方法,增加历史上下文获取和智能prompt构建
    • 在prompt中明确区分当前指令(主要任务)和历史信息(上下文参考)

4. 智能Prompt构建策略

  • 特点:
    • 优先执行当前Comment指令,历史信息仅作为上下文
    • 当有历史上下文时,构建包含PR描述、历史讨论的完整prompt
    • 当无历史上下文时,降级为简单prompt模式
    • 明确告知AI如何处理历史信息和当前指令的优先级关系

5. 错误处理和兼容性

  • 策略:
    • 获取历史评论失败时不中断流程,降级为简单模式
    • 处理Comment为nil的边界情况
    • 保持与现有API的完全兼容

这些改动确保了PR review comment场景下AI能够获得完整的背景信息,从而提供更高质量的代码建议和修改,同时保持了当前指令的核心地位。

原始 Prompt

根据Issue修改代码:

标题:在PR review comment 里执行Comment,要把所有的历史comments都给到AI
描述:PR review场景一般有两种交互姿势,一种是基于代码行精准评价,而第二种就是reviewer会直接在PR的Comment里反馈,且这两种场景对应的github event类型也是不同的。

在第二种场景,目前codeagent仅仅从事件的body里拿到内容,实际上给到AI的上下文信息不全,导致代码质量不高。因为一般reviewer在PR 里comments时,默认是知道所有PR内容以及相关讨论的,有相关的背景。

所以在这个场景,我们应该把所有的有效内容都给到AI,包括PR的body,其他comments,以及当前事件的comment。当然,这里要注意,其他的comments仅仅是作为上下文信息补充,核心的要面对的指令,仍然是当前的Comment。所以这里的prompt构建要有一定的针对性。

输出格式:

改动摘要

简要说明改动内容

具体改动

  • 列出修改的文件和具体变动

qiniu-ci added 2 commits July 17, 2025 19:52
Include all PR comments, reviews, and descriptions as context when processing PR review comments to improve AI code quality. The current comment remains the primary instruction while historical information serves as supplementary context.

- Add PRAllComments model to store complete PR comment history
- Add GetAllPRComments method to GitHub client for fetching all comment types
- Enhance agent prompt building with formatHistoricalComments function
- Update ContinuePRWithArgs and FixPRWithArgs to use historical context
- Implement graceful fallback when historical context retrieval fails

Closes #143
@CarlJi
Copy link
Contributor

CarlJi commented Jul 17, 2025

/continue FixPRWithArgs 和 ContinuePRWithArgs 感觉相同的逻辑有点多,帮我重构下。另外执行完代码,你得执行下go fmt才行

Extract common functionality from ContinuePRWithArgs and FixPRWithArgs into a shared processPRWithArgs function with mode-specific behavior. This eliminates approximately 200 lines of duplicate code while maintaining identical external interfaces and functionality.

Key improvements:
- Create processPRWithArgs function to handle common PR workflow
- Add buildPrompt helper for mode-specific prompt generation
- Preserve all existing validation and error handling logic
- Maintain behavioral differences between Continue and Fix modes

Closes #143
@qiniu-ci
Copy link
Collaborator Author

完成!我已经成功重构了 FixPRWithArgsContinuePRWithArgs 函数,消除了重复代码。下面是重构的总结:

重构总结

1. 提取公共逻辑

  • 创建了通用函数 processPRWithArgs(ctx, event, args, mode) 来处理所有公共逻辑
  • 创建了辅助函数 buildPrompt(mode, args, historicalContext) 来构建不同模式的提示

2. 重构后的架构

  • processPRWithArgs: 统一处理仓库信息提取、PR获取、工作空间管理、代码拉取、AI处理、提交和评论
  • buildPrompt: 根据不同模式(Continue/Fix)和参数构建合适的提示
  • ContinuePRWithArgs: 简化为调用 processPRWithArgs(ctx, event, args, "Continue")
  • FixPRWithArgs: 简化为调用 processPRWithArgs(ctx, event, args, "Fix")

3. 保持差异化功能

  • 验证逻辑: 只有 Continue 模式需要验证是否为PR评论
  • 错误处理: Fix 模式在提交失败时会返回错误,Continue 模式则继续执行
  • 提示内容: 根据不同模式生成针对性的提示文本

4. 代码质量改进

  • 减少了约 200行 重复代码
  • 提高了代码的可维护性和可读性
  • 运行了 go fmt 格式化代码
  • 通过了编译测试

5. 功能保持不变

  • 两个函数的对外接口和行为完全保持不变
  • 所有原有的功能都被保留在通用函数中
  • 错误处理逻辑与原来完全一致

这次重构大大提高了代码的可维护性,如果将来需要添加新的PR处理模式,只需要添加新的模式到 buildPrompt 函数中,并创建一个简单的包装函数即可。

@CarlJi CarlJi merged commit 6b712f4 into main Jul 17, 2025
1 check passed
@qiniu-ci qiniu-ci deleted the codeagent/issue-143-1752753172 branch July 22, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants