Skip to content

Commit 6869144

Browse files
committed
add trigger comment into prompt
1 parent 38913cc commit 6869144

File tree

4 files changed

+47
-83
lines changed

4 files changed

+47
-83
lines changed

internal/code/claude_docker.go

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os/exec"
88
"path/filepath"
99
"strings"
10-
"time"
1110

1211
"github.com/qiniu/codeagent/internal/config"
1312
"github.com/qiniu/codeagent/pkg/models"
@@ -125,17 +124,6 @@ func NewClaudeDocker(workspace *models.Workspace, cfg *config.Config) (Code, err
125124
args = append(args, "-e", fmt.Sprintf("GH_TOKEN=%s", cfg.GitHub.GHToken))
126125
}
127126

128-
// 设置 Claude CLI 配置目录,避免权限问题
129-
args = append(args, "-e", "HOME=/home/codeagent")
130-
args = append(args, "-e", "CLAUDE_CONFIG_DIR=/home/codeagent/.claude")
131-
args = append(args, "-e", "XDG_CONFIG_HOME=/home/codeagent/.config")
132-
133-
// 不在容器启动时强制用户,而是在执行命令时设置
134-
// 这样可以避免容器镜像中没有 codeagent 用户的问题
135-
136-
// 创建必要的目录
137-
args = append(args, "--init")
138-
139127
// 添加容器镜像
140128
args = append(args, cfg.Claude.ContainerImage)
141129

@@ -165,13 +153,6 @@ func NewClaudeDocker(workspace *models.Workspace, cfg *config.Config) (Code, err
165153

166154
log.Infof("docker container started successfully")
167155

168-
// 确保容器内的目录结构正确,避免 Claude CLI 权限问题
169-
setupCmd := exec.Command("docker", "exec", containerName,
170-
"sh", "-c", "mkdir -p /home/codeagent/.config /home/codeagent/.claude && chmod -R 755 /home/codeagent")
171-
if err := setupCmd.Run(); err != nil {
172-
log.Warnf("Failed to setup container directories: %v", err)
173-
}
174-
175156
return &claudeCode{
176157
containerName: containerName,
177158
configGen: configGen,
@@ -224,52 +205,19 @@ func (c *claudeCode) executeClaudeWithMCP(message string) (*Response, error) {
224205
// 修复容器内文件权限,确保所有用户都可以读取
225206
log.Infof("Step 2.1: Fixing file permissions in container")
226207

227-
// 首先尝试设置文件为全局可读 (更宽松的权限)
228-
chmodCmd := exec.Command("docker", "exec", c.containerName, "chmod", "644", "/tmp/mcp-config.json")
208+
// 首先尝试设置文件为全局可读
209+
chmodCmd := exec.Command("docker", "exec", c.containerName, "chmod", "755", "/tmp/mcp-config.json")
229210
if err := chmodCmd.Run(); err != nil {
230211
log.Warnf("Failed to chmod MCP config file: %v", err)
231-
232-
// 如果 chmod 失败,尝试更宽松的权限
233-
chmodAllCmd := exec.Command("docker", "exec", c.containerName, "chmod", "666", "/tmp/mcp-config.json")
234-
if err := chmodAllCmd.Run(); err != nil {
235-
log.Warnf("Failed to chmod 666 MCP config file: %v", err)
236-
237-
// 最后的尝试:检查文件是否已经可读
238-
testReadCmd := exec.Command("docker", "exec", c.containerName, "test", "-r", "/tmp/mcp-config.json")
239-
if err := testReadCmd.Run(); err != nil {
240-
return nil, fmt.Errorf("MCP config file is not readable: %w", err)
241-
}
242-
log.Infof("MCP config file is already readable, continuing...")
243-
} else {
244-
log.Infof("Fixed MCP config file permissions with chmod 666")
245-
}
246-
} else {
247-
log.Infof("Fixed MCP config file permissions with chmod 644")
248-
}
249-
250-
// 3. 确保MCP服务器二进制文件权限正确并启动MCP服务器(后台运行)
251-
log.Infof("Step 3: Starting MCP server in container")
252-
253-
log.Infof("Step 3.1: Starting MCP server daemon")
254-
mcpServerCmd := []string{
255-
"exec", "-d", c.containerName,
256-
"/usr/local/bin/mcp-server",
257212
}
258-
259-
startServerCmd := exec.Command("docker", mcpServerCmd...)
260-
log.Infof("Running command: docker %s", strings.Join(mcpServerCmd, " "))
261-
if err := startServerCmd.Run(); err != nil {
262-
return nil, fmt.Errorf("failed to start MCP server in container: %w", err)
213+
// 检查文件是否已经可读
214+
testReadCmd := exec.Command("docker", "exec", c.containerName, "test", "-r", "/tmp/mcp-config.json")
215+
if err := testReadCmd.Run(); err != nil {
216+
return nil, fmt.Errorf("MCP config file is not readable: %w", err)
263217
}
264218

265-
log.Infof("MCP server started in container")
266-
267-
// 4. 等待MCP服务器启动
268-
log.Infof("Step 4: Waiting for MCP server to be ready")
269-
time.Sleep(2 * time.Second)
270-
271-
// 5. 执行Claude CLI with MCP
272-
log.Infof("Step 5: Executing Claude CLI with MCP configuration")
219+
// 3. 执行Claude CLI with MCP
220+
log.Infof("Step 3: Executing Claude CLI with MCP configuration")
273221
args := []string{
274222
"exec",
275223
"-e", "HOME=/home/codeagent", // 设置环境变量

internal/context/generator.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ func (g *DefaultPromptGenerator) GenerateToolsList(ctx *EnhancedContext, mode st
216216

217217
// MCP工具(始终可用)
218218
mcpTools := []string{
219-
"github-files_read_file",
220-
"github-files_write_file",
221-
"github-files_search_files",
222-
"github-comments_update_comment",
223-
"github-comments_create_comment",
219+
"mcp__github_files__read_repository_file",
220+
"mcp__github_files__write_repository_file",
221+
"mcp__github_files__search_files",
222+
"mcp__github_comments__update_comment",
223+
"mcp__github_comments__create_comment",
224224
}
225225
tools = append(tools, mcpTools...)
226226

@@ -229,9 +229,9 @@ func (g *DefaultPromptGenerator) GenerateToolsList(ctx *EnhancedContext, mode st
229229
case "Continue", "Fix", "Code":
230230
// 开发模式需要完整的工具集
231231
additionalTools := []string{
232-
"github-files_commit_files",
233-
"github-files_create_branch",
234-
"github-files_list_files",
232+
"mcp__github_files__commit_files",
233+
"mcp__github_files__create_branch",
234+
"mcp__github_files__get_file_tree",
235235
}
236236
tools = append(tools, additionalTools...)
237237

internal/context/template_generator.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ func (g *TemplatePromptGenerator) buildVariables(ctx *EnhancedContext, mode stri
163163
vars["TRIGGER_DISPLAY_NAME"] = fmt.Sprintf("%v", triggerDisplayName)
164164
}
165165

166+
if triggerComment, ok := ctx.Metadata["trigger_comment"]; ok {
167+
vars["TRIGGER_COMMENT"] = fmt.Sprintf("%v", triggerComment)
168+
}
169+
166170
return vars
167171
}
168172

@@ -389,6 +393,9 @@ Images have been downloaded from GitHub comments and saved to disk. Their file p
389393
<claude_comment_id>$CLAUDE_COMMENT_ID</claude_comment_id>
390394
<trigger_username>$TRIGGER_USERNAME</trigger_username>
391395
<trigger_display_name>$TRIGGER_DISPLAY_NAME</trigger_display_name>
396+
<trigger_comment>
397+
$TRIGGER_COMMENT
398+
</trigger_comment>
392399
393400
<direct_prompt>
394401
IMPORTANT: The following are direct instructions from the user that MUST take precedence over all other instructions and context. These instructions should guide your behavior and actions above any other considerations:
@@ -442,7 +449,7 @@ Follow these steps:
442449
- Mark this todo as complete in the comment by checking the box: - [x].
443450
444451
3. Understand the Request:
445-
- Extract the actual question or request from the <trigger_comment> tag above.
452+
- Extract the actual question or request from the <direct_prompt> tag above.
446453
- CRITICAL: If other users requested changes in other comments, DO NOT implement those changes unless the trigger comment explicitly asks you to implement them.
447454
- Only follow the instructions in the trigger comment - all other comments are just for context.
448455
- IMPORTANT: Always check for and follow the repository's CLAUDE.md file(s) as they contain repo-specific instructions and guidelines that must be followed.
@@ -464,6 +471,7 @@ Follow these steps:
464471
- Reference specific code with inline formatting or code blocks.
465472
- Include relevant file paths and line numbers when applicable.
466473
- IMPORTANT: Submit your review feedback by updating the Claude comment using mcp__codeagent__github-comments__update_comment. This will be displayed as your PR review.
474+
- IMPORTANT: Before submission, be sure to add a prompt to manually re-execute the code review at the end, and use friendly guiding language to remind users to enter /review in the comment section
467475
468476
B. For Straightforward Changes:
469477
- Use file system tools to make the change locally.

internal/modes/review_handler.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (rh *ReviewHandler) handlePREvent(ctx context.Context, event *models.PullRe
133133
xl.Infof("Auto-reviewing PR #%d", event.PullRequest.GetNumber())
134134

135135
// 执行自动代码审查
136-
return rh.processCodeReview(ctx, event, client)
136+
return rh.processCodeReview(ctx, event, client, nil)
137137

138138
case "closed":
139139
return rh.handlePRClosed(ctx, event, client)
@@ -207,7 +207,7 @@ func (rh *ReviewHandler) handlePRClosed(ctx context.Context, event *models.PullR
207207
}
208208

209209
// processCodeReview PR自动代码审查方法
210-
func (rh *ReviewHandler) processCodeReview(ctx context.Context, prEvent *models.PullRequestContext, client *ghclient.Client) error {
210+
func (rh *ReviewHandler) processCodeReview(ctx context.Context, prEvent *models.PullRequestContext, client *ghclient.Client, triggerComment *string) error {
211211
xl := xlog.NewWith(ctx)
212212
xl.Infof("Starting automatic code review for PR")
213213

@@ -258,7 +258,7 @@ func (rh *ReviewHandler) processCodeReview(ctx context.Context, prEvent *models.
258258

259259
// 5. 构建审查上下文和提示词
260260
xl.Infof("Building review context and prompt")
261-
prompt, err := rh.buildReviewPrompt(ctx, prEvent, commentID)
261+
prompt, err := rh.buildReviewPrompt(ctx, prEvent, commentID, triggerComment)
262262
if err != nil {
263263
xl.Errorf("Failed to build enhanced prompt : %v", err)
264264
}
@@ -283,7 +283,7 @@ func (rh *ReviewHandler) processCodeReview(ctx context.Context, prEvent *models.
283283
}
284284

285285
// buildReviewPrompt 构建代码审查提示词
286-
func (rh *ReviewHandler) buildReviewPrompt(ctx context.Context, prEvent *models.PullRequestContext, commentID int64) (string, error) {
286+
func (rh *ReviewHandler) buildReviewPrompt(ctx context.Context, prEvent *models.PullRequestContext, commentID int64, triggerComment *string) (string, error) {
287287
xl := xlog.NewWith(ctx)
288288

289289
if prEvent == nil {
@@ -309,15 +309,22 @@ func (rh *ReviewHandler) buildReviewPrompt(ctx context.Context, prEvent *models.
309309
Timestamp: time.Now(),
310310
Subject: prEvent,
311311
Code: codeCtx, // 确保代码上下文被设置
312-
Metadata: map[string]interface{}{
313-
"pr_number": prEvent.PullRequest.GetNumber(),
314-
"pr_title": prEvent.PullRequest.GetTitle(),
315-
"pr_body": prEvent.PullRequest.GetBody(),
316-
"repository": prEvent.PullRequest.GetBase().GetRepo().GetFullName(),
317-
"trigger_username": "system", // 自动审查
318-
"trigger_display_name": "CodeAgent Auto Review",
319-
"claude_comment_id": commentID,
320-
},
312+
Metadata: func() map[string]interface{} {
313+
metadata := map[string]interface{}{
314+
"pr_number": prEvent.PullRequest.GetNumber(),
315+
"pr_title": prEvent.PullRequest.GetTitle(),
316+
"pr_body": prEvent.PullRequest.GetBody(),
317+
"repository": prEvent.PullRequest.GetBase().GetRepo().GetFullName(),
318+
"trigger_username": prEvent.Sender.GetLogin(),
319+
"trigger_display_name": prEvent.Sender.GetLogin(),
320+
"claude_comment_id": commentID,
321+
}
322+
323+
if triggerComment != nil && *triggerComment != "" {
324+
metadata["trigger_comment"] = *triggerComment
325+
}
326+
return metadata
327+
}(),
321328
}
322329

323330
// 使用模板生成器的Review模式生成提示词
@@ -387,6 +394,7 @@ func (rh *ReviewHandler) ProcessManualCodeReview(ctx context.Context, event *mod
387394
return fmt.Errorf("failed to get PR information: %w", err)
388395
}
389396

397+
triggerComment := event.Comment.GetBody()
390398
// 3. 构造 PullRequestContext
391399
prEvent := &models.PullRequestContext{
392400
BaseContext: models.BaseContext{
@@ -402,5 +410,5 @@ func (rh *ReviewHandler) ProcessManualCodeReview(ctx context.Context, event *mod
402410
}
403411

404412
// 4. 调用统一的代码审查逻辑
405-
return rh.processCodeReview(ctx, prEvent, client)
413+
return rh.processCodeReview(ctx, prEvent, client, &triggerComment)
406414
}

0 commit comments

Comments
 (0)