mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-24 23:54:13 +08:00
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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user