| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | package loop |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 8 | "sketch.dev/claudetool" |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 9 | "sketch.dev/llm" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | // makeDoneTool creates a tool that provides a checklist to the agent. There |
| 13 | // are some duplicative instructions here and in the system prompt, and it's |
| 14 | // not as reliable as it could be. Historically, we've found that Claude ignores |
| 15 | // the tool results here, so we don't tell the tool to say "hey, really check this" |
| 16 | // at the moment, though we've tried. |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 17 | func makeDoneTool(codereview *claudetool.CodeReviewer, gitUsername, gitEmail string) *llm.Tool { |
| 18 | return &llm.Tool{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 19 | Name: "done", |
| 20 | Description: `Use this tool when you have achieved the user's goal. The parameters form a checklist which you should evaluate.`, |
| 21 | InputSchema: json.RawMessage(doneChecklistJSONSchema(gitUsername, gitEmail)), |
| 22 | Run: func(ctx context.Context, input json.RawMessage) (string, error) { |
| 23 | // Cannot be done with a messy git. |
| 24 | if err := codereview.RequireNormalGitState(ctx); err != nil { |
| 25 | return "", err |
| 26 | } |
| 27 | if err := codereview.RequireNoUncommittedChanges(ctx); err != nil { |
| 28 | return "", err |
| 29 | } |
| 30 | // Ensure that the current commit has been reviewed. |
| 31 | head, err := codereview.CurrentCommit(ctx) |
| 32 | if err == nil { |
| 33 | needsReview := !codereview.IsInitialCommit(head) && !codereview.HasReviewed(head) |
| 34 | if needsReview { |
| 35 | return "", fmt.Errorf("codereview tool has not been run for commit %v", head) |
| 36 | } |
| 37 | } |
| 38 | return `Please ask the user to review your work. Be concise - users are more likely to read shorter comments.`, nil |
| 39 | }, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func doneChecklistJSONSchema(gitUsername, gitEmail string) string { |
| Josh Bleecher Snyder | 75ec6bb | 2025-04-24 10:49:16 -0700 | [diff] [blame] | 44 | gitCommitDescription := fmt.Sprintf(`Create git commits for any code changes you made. Match the style of recent commit messages. Include 'Co-Authored-By: sketch <hello@sketch.dev>' and the original user prompt. Use GIT_AUTHOR_NAME="%s" GIT_AUTHOR_EMAIL="%s" (not git config).`, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 45 | gitUsername, gitEmail) |
| 46 | desc, err := json.Marshal(gitCommitDescription) |
| 47 | if err != nil { |
| 48 | panic(err) |
| 49 | } |
| 50 | return doneChecklistJSONSchemaPrefix + string(desc) + doneChecklistJSONSchemaSuffix |
| 51 | } |
| 52 | |
| 53 | // TODO: this is ugly, maybe JSON-encode a deeply nested map[string]any instead? also ugly. |
| 54 | const ( |
| 55 | doneChecklistJSONSchemaPrefix = `{ |
| 56 | "$schema": "http://json-schema.org/draft-07/schema#", |
| 57 | "title": "Checklist", |
| 58 | "description": "A schema for tracking checklist items with status and comments", |
| 59 | "type": "object", |
| 60 | "required": ["checklist_items"], |
| 61 | "properties": { |
| 62 | "checklist_items": { |
| 63 | "type": "object", |
| 64 | "description": "Collection of checklist items", |
| 65 | "properties": { |
| 66 | "wrote_tests": { |
| 67 | "$ref": "#/definitions/checklistItem", |
| 68 | "description": "If code was changed, tests were written or updated." |
| 69 | }, |
| 70 | "passes_tests": { |
| 71 | "$ref": "#/definitions/checklistItem", |
| 72 | "description": "If any commits were made, tests pass." |
| 73 | }, |
| 74 | "code_reviewed": { |
| 75 | "$ref": "#/definitions/checklistItem", |
| 76 | "description": "If any commits were made, the codereview tool was run and its output was addressed." |
| 77 | }, |
| 78 | "git_commit": { |
| 79 | "$ref": "#/definitions/checklistItem", |
| 80 | "description": ` |
| 81 | |
| 82 | doneChecklistJSONSchemaSuffix = ` |
| 83 | } |
| 84 | }, |
| 85 | "additionalProperties": { |
| 86 | "$ref": "#/definitions/checklistItem" |
| 87 | } |
| 88 | } |
| 89 | }, |
| 90 | "definitions": { |
| 91 | "checklistItem": { |
| 92 | "type": "object", |
| 93 | "required": ["status"], |
| 94 | "properties": { |
| 95 | "status": { |
| 96 | "type": "string", |
| 97 | "description": "Current status of the checklist item", |
| 98 | "enum": ["yes", "no", "not applicable", "other"] |
| 99 | }, |
| 100 | "description": { |
| 101 | "type": "string", |
| 102 | "description": "Description of what this checklist item verifies" |
| 103 | }, |
| 104 | "comments": { |
| 105 | "type": "string", |
| 106 | "description": "Additional comments or context for this checklist item" |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | }` |
| 112 | ) |