-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: send user request as separate content block for slash command support #785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** | ||
| * Extracts the user's request from a trigger comment. | ||
| * | ||
| * Given a comment like "@claude /review-pr please check the auth module", | ||
| * this extracts "/review-pr please check the auth module". | ||
| * | ||
| * @param commentBody - The full comment body containing the trigger phrase | ||
| * @param triggerPhrase - The trigger phrase (e.g., "@claude") | ||
| * @returns The user's request (text after the trigger phrase), or null if not found | ||
| */ | ||
| export function extractUserRequest( | ||
| commentBody: string | undefined, | ||
| triggerPhrase: string, | ||
| ): string | null { | ||
| if (!commentBody) { | ||
| return null; | ||
| } | ||
|
|
||
| // Use string operations instead of regex for better performance and security | ||
| // (avoids potential ReDoS with large comment bodies) | ||
| const triggerIndex = commentBody | ||
| .toLowerCase() | ||
| .indexOf(triggerPhrase.toLowerCase()); | ||
| if (triggerIndex === -1) { | ||
| return null; | ||
| } | ||
|
|
||
| const afterTrigger = commentBody | ||
| .substring(triggerIndex + triggerPhrase.length) | ||
| .trim(); | ||
| return afterTrigger || null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { describe, test, expect } from "bun:test"; | ||
ashwin-ant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { extractUserRequest } from "../src/utils/extract-user-request"; | ||
|
|
||
| describe("extractUserRequest", () => { | ||
| test("extracts text after @claude trigger", () => { | ||
| expect(extractUserRequest("@claude /review-pr", "@claude")).toBe( | ||
| "/review-pr", | ||
| ); | ||
| }); | ||
|
|
||
| test("extracts slash command with arguments", () => { | ||
| expect( | ||
| extractUserRequest( | ||
| "@claude /review-pr please check the auth module", | ||
| "@claude", | ||
| ), | ||
| ).toBe("/review-pr please check the auth module"); | ||
| }); | ||
|
|
||
| test("handles trigger phrase with extra whitespace", () => { | ||
| expect(extractUserRequest("@claude /review-pr", "@claude")).toBe( | ||
| "/review-pr", | ||
| ); | ||
| }); | ||
|
|
||
| test("handles trigger phrase at start of multiline comment", () => { | ||
| const comment = `@claude /review-pr | ||
| Please review this PR carefully. | ||
| Focus on security issues.`; | ||
| expect(extractUserRequest(comment, "@claude")).toBe( | ||
| `/review-pr | ||
| Please review this PR carefully. | ||
| Focus on security issues.`, | ||
| ); | ||
| }); | ||
|
|
||
| test("handles trigger phrase in middle of text", () => { | ||
| expect( | ||
| extractUserRequest("Hey team, @claude can you review this?", "@claude"), | ||
| ).toBe("can you review this?"); | ||
| }); | ||
|
|
||
| test("returns null for empty comment body", () => { | ||
| expect(extractUserRequest("", "@claude")).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null for undefined comment body", () => { | ||
| expect(extractUserRequest(undefined, "@claude")).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null when trigger phrase not found", () => { | ||
| expect(extractUserRequest("Please review this PR", "@claude")).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null when only trigger phrase with no request", () => { | ||
| expect(extractUserRequest("@claude", "@claude")).toBeNull(); | ||
| }); | ||
|
|
||
| test("handles custom trigger phrase", () => { | ||
| expect(extractUserRequest("/claude help me", "/claude")).toBe("help me"); | ||
| }); | ||
|
|
||
| test("handles trigger phrase with special regex characters", () => { | ||
| expect( | ||
| extractUserRequest("@claude[bot] do something", "@claude[bot]"), | ||
| ).toBe("do something"); | ||
| }); | ||
|
|
||
| test("is case insensitive", () => { | ||
| expect(extractUserRequest("@CLAUDE /review-pr", "@claude")).toBe( | ||
| "/review-pr", | ||
| ); | ||
| expect(extractUserRequest("@Claude /review-pr", "@claude")).toBe( | ||
| "/review-pr", | ||
| ); | ||
| }); | ||
ashwin-ant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.