feat: implement Claude Code GitHub Action v1.0 with auto-detection and slash commands

Major features:
- Mode auto-detection based on GitHub event type
- Unified prompt field replacing override_prompt and direct_prompt
- Slash command system with pre-built commands
- Full backward compatibility with v0.x

Key changes:
- Add mode detector for automatic mode selection
- Implement slash command loader with YAML frontmatter support
- Update action.yml with new prompt input
- Create pre-built slash commands for common tasks
- Update all tests for v1.0 compatibility

Breaking changes (with compatibility):
- Mode input now optional (auto-detected)
- override_prompt deprecated (use prompt)
- direct_prompt deprecated (use prompt)
This commit is contained in:
km-anthropic
2025-08-05 21:21:41 -07:00
parent 188d526721
commit 9a665625f7
18 changed files with 506 additions and 185 deletions

77
src/modes/detector.ts Normal file
View File

@@ -0,0 +1,77 @@
import type { GitHubContext } from "../github/context";
import {
isEntityContext,
isAutomationContext,
isPullRequestEvent,
isIssueCommentEvent,
isPullRequestReviewCommentEvent,
} from "../github/context";
import { checkContainsTrigger } from "../github/validation/trigger";
export type AutoDetectedMode = "review" | "tag" | "agent";
export function detectMode(context: GitHubContext): AutoDetectedMode {
if (isPullRequestEvent(context)) {
const allowedActions = ["opened", "synchronize", "reopened"];
const action = context.payload.action;
if (allowedActions.includes(action)) {
return "review";
}
}
if (isEntityContext(context)) {
if (
isIssueCommentEvent(context) ||
isPullRequestReviewCommentEvent(context)
) {
if (checkContainsTrigger(context)) {
return "tag";
}
}
if (context.eventName === "issues") {
if (checkContainsTrigger(context)) {
return "tag";
}
}
}
if (isAutomationContext(context)) {
return "agent";
}
return "agent";
}
export function getModeDescription(mode: AutoDetectedMode): string {
switch (mode) {
case "review":
return "Automated code review mode for pull requests";
case "tag":
return "Interactive mode triggered by @claude mentions";
case "agent":
return "Automation mode for scheduled tasks and workflows";
default:
return "Unknown mode";
}
}
export function shouldUseTrackingComment(mode: AutoDetectedMode): boolean {
return mode === "tag";
}
export function getDefaultPromptForMode(
mode: AutoDetectedMode,
context: GitHubContext,
): string | undefined {
switch (mode) {
case "review":
return "/review";
case "tag":
return undefined;
case "agent":
return context.inputs?.directPrompt || context.inputs?.overridePrompt;
default:
return undefined;
}
}

View File

@@ -1,13 +1,8 @@
/**
* Mode Registry for claude-code-action
* Mode Registry for claude-code-action v1.0
*
* This module provides access to all available execution modes.
*
* To add a new mode:
* 1. Add the mode name to VALID_MODES below
* 2. Create the mode implementation in a new directory (e.g., src/modes/new-mode/)
* 3. Import and add it to the modes object below
* 4. Update action.yml description to mention the new mode
* This module provides access to all available execution modes and handles
* automatic mode detection based on GitHub event types.
*/
import type { Mode, ModeName } from "./types";
@@ -15,41 +10,44 @@ import { tagMode } from "./tag";
import { agentMode } from "./agent";
import { reviewMode } from "./review";
import type { GitHubContext } from "../github/context";
import { isAutomationContext } from "../github/context";
import { detectMode, type AutoDetectedMode } from "./detector";
export const DEFAULT_MODE = "tag" as const;
export const VALID_MODES = ["tag", "agent", "experimental-review"] as const;
export const VALID_MODES = ["tag", "agent", "review"] as const;
/**
* All available modes.
* Add new modes here as they are created.
* All available modes in v1.0
*/
const modes = {
tag: tagMode,
agent: agentMode,
"experimental-review": reviewMode,
} as const satisfies Record<ModeName, Mode>;
review: reviewMode,
} as const satisfies Record<AutoDetectedMode, Mode>;
/**
* Retrieves a mode by name and validates it can handle the event type.
* @param name The mode name to retrieve
* @param context The GitHub context to validate against
* @returns The requested mode
* @throws Error if the mode is not found or cannot handle the event
* Automatically detects and retrieves the appropriate mode based on the GitHub context.
* In v1.0, modes are auto-selected based on event type.
* @param context The GitHub context
* @param explicitMode Optional explicit mode override (for backward compatibility)
* @returns The appropriate mode for the context
*/
export function getMode(name: ModeName, context: GitHubContext): Mode {
const mode = modes[name];
if (!mode) {
const validModes = VALID_MODES.join("', '");
throw new Error(
`Invalid mode '${name}'. Valid modes are: '${validModes}'. Please check your workflow configuration.`,
);
export function getMode(
context: GitHubContext,
explicitMode?: string,
): Mode {
let modeName: AutoDetectedMode;
if (explicitMode && isValidModeV1(explicitMode)) {
console.log(`Using explicit mode: ${explicitMode}`);
modeName = mapLegacyMode(explicitMode);
} else {
modeName = detectMode(context);
console.log(`Auto-detected mode: ${modeName} for event: ${context.eventName}`);
}
// Validate mode can handle the event type
if (name === "tag" && isAutomationContext(context)) {
const mode = modes[modeName];
if (!mode) {
throw new Error(
`Tag mode cannot handle ${context.eventName} events. Use 'agent' mode for automation events.`,
`Mode '${modeName}' not found. This should not happen. Please report this issue.`,
);
}
@@ -57,10 +55,29 @@ export function getMode(name: ModeName, context: GitHubContext): Mode {
}
/**
* Type guard to check if a string is a valid mode name.
* Maps legacy mode names to v1.0 mode names
*/
function mapLegacyMode(name: string): AutoDetectedMode {
if (name === "experimental-review") {
return "review";
}
return name as AutoDetectedMode;
}
/**
* Type guard to check if a string is a valid v1.0 mode name.
* @param name The string to check
* @returns True if the name is a valid mode name
*/
export function isValidMode(name: string): name is ModeName {
return VALID_MODES.includes(name as ModeName);
export function isValidModeV1(name: string): boolean {
const v1Modes = ["tag", "agent", "review", "experimental-review"];
return v1Modes.includes(name);
}
/**
* Legacy type guard for backward compatibility
* @deprecated Use auto-detection instead
*/
export function isValidMode(name: string): name is ModeName {
return isValidModeV1(name);
}

View File

@@ -89,10 +89,14 @@ export const reviewMode: Mode = {
context: PreparedContext,
githubData: FetchDataResult,
): string {
// Support overridePrompt
if (context.overridePrompt) {
return context.overridePrompt;
// Support v1.0 unified prompt or legacy overridePrompt
const userPrompt = context.prompt || context.overridePrompt;
if (userPrompt) {
return userPrompt;
}
// Default to /review slash command content
// This will be expanded by the slash command system
const {
contextData,

View File

@@ -3,7 +3,7 @@ import type { PreparedContext } from "../create-prompt/types";
import type { FetchDataResult } from "../github/data/fetcher";
import type { Octokits } from "../github/api/client";
export type ModeName = "tag" | "agent" | "experimental-review";
export type ModeName = "tag" | "agent" | "experimental-review" | "review";
export type ModeContext = {
mode: ModeName;