mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-24 07:24:12 +08:00
Compare commits
9 Commits
ashwin/cla
...
eaptest
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4cdae8adfc | ||
|
|
e5d38c6b74 | ||
|
|
af398fcc95 | ||
|
|
aeda2d62c0 | ||
|
|
2ce0b1c9b2 | ||
|
|
fd041f9b80 | ||
|
|
544983d6bf | ||
|
|
4d3cbe2826 | ||
|
|
52c2f5881b |
129
action.yml
129
action.yml
@@ -118,10 +118,10 @@ inputs:
|
|||||||
outputs:
|
outputs:
|
||||||
execution_file:
|
execution_file:
|
||||||
description: "Path to the Claude Code execution output file"
|
description: "Path to the Claude Code execution output file"
|
||||||
value: ${{ steps.claude.outputs.execution_file }}
|
value: ${{ steps.claude-code.outputs.execution_file }}
|
||||||
branch_name:
|
branch_name:
|
||||||
description: "The branch created by Claude Code for this execution"
|
description: "The branch created by Claude Code for this execution"
|
||||||
value: ${{ steps.claude.outputs.CLAUDE_BRANCH }}
|
value: ${{ steps.prepare.outputs.CLAUDE_BRANCH }}
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
@@ -137,36 +137,20 @@ runs:
|
|||||||
cd ${GITHUB_ACTION_PATH}
|
cd ${GITHUB_ACTION_PATH}
|
||||||
bun install
|
bun install
|
||||||
|
|
||||||
- name: Run Claude
|
- name: Prepare action
|
||||||
id: claude
|
id: prepare
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
# Install base-action dependencies
|
bun run ${GITHUB_ACTION_PATH}/src/entrypoints/prepare.ts
|
||||||
echo "Installing base-action dependencies..."
|
|
||||||
cd ${GITHUB_ACTION_PATH}/base-action
|
|
||||||
bun install
|
|
||||||
echo "Base-action dependencies installed"
|
|
||||||
cd -
|
|
||||||
|
|
||||||
# Install Claude Code globally
|
|
||||||
bun install -g @anthropic-ai/claude-code@1.0.67
|
|
||||||
|
|
||||||
# Setup network restrictions if needed
|
|
||||||
if [[ "${{ inputs.experimental_allowed_domains }}" != "" ]]; then
|
|
||||||
chmod +x ${GITHUB_ACTION_PATH}/scripts/setup-network-restrictions.sh
|
|
||||||
${GITHUB_ACTION_PATH}/scripts/setup-network-restrictions.sh
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Run the unified entrypoint
|
|
||||||
bun run ${GITHUB_ACTION_PATH}/src/entrypoints/run.ts
|
|
||||||
env:
|
env:
|
||||||
# Mode and trigger configuration
|
|
||||||
MODE: ${{ inputs.mode }}
|
MODE: ${{ inputs.mode }}
|
||||||
TRIGGER_PHRASE: ${{ inputs.trigger_phrase }}
|
TRIGGER_PHRASE: ${{ inputs.trigger_phrase }}
|
||||||
ASSIGNEE_TRIGGER: ${{ inputs.assignee_trigger }}
|
ASSIGNEE_TRIGGER: ${{ inputs.assignee_trigger }}
|
||||||
LABEL_TRIGGER: ${{ inputs.label_trigger }}
|
LABEL_TRIGGER: ${{ inputs.label_trigger }}
|
||||||
BASE_BRANCH: ${{ inputs.base_branch }}
|
BASE_BRANCH: ${{ inputs.base_branch }}
|
||||||
BRANCH_PREFIX: ${{ inputs.branch_prefix }}
|
BRANCH_PREFIX: ${{ inputs.branch_prefix }}
|
||||||
|
ALLOWED_TOOLS: ${{ inputs.allowed_tools }}
|
||||||
|
DISALLOWED_TOOLS: ${{ inputs.disallowed_tools }}
|
||||||
CUSTOM_INSTRUCTIONS: ${{ inputs.custom_instructions }}
|
CUSTOM_INSTRUCTIONS: ${{ inputs.custom_instructions }}
|
||||||
DIRECT_PROMPT: ${{ inputs.direct_prompt }}
|
DIRECT_PROMPT: ${{ inputs.direct_prompt }}
|
||||||
OVERRIDE_PROMPT: ${{ inputs.override_prompt }}
|
OVERRIDE_PROMPT: ${{ inputs.override_prompt }}
|
||||||
@@ -177,25 +161,65 @@ runs:
|
|||||||
DEFAULT_WORKFLOW_TOKEN: ${{ github.token }}
|
DEFAULT_WORKFLOW_TOKEN: ${{ github.token }}
|
||||||
ADDITIONAL_PERMISSIONS: ${{ inputs.additional_permissions }}
|
ADDITIONAL_PERMISSIONS: ${{ inputs.additional_permissions }}
|
||||||
USE_COMMIT_SIGNING: ${{ inputs.use_commit_signing }}
|
USE_COMMIT_SIGNING: ${{ inputs.use_commit_signing }}
|
||||||
|
|
||||||
|
# Authentication for remote-agent mode
|
||||||
|
ANTHROPIC_API_KEY: ${{ inputs.anthropic_api_key }}
|
||||||
|
CLAUDE_CODE_OAUTH_TOKEN: ${{ inputs.claude_code_oauth_token }}
|
||||||
|
|
||||||
|
- name: Install Base Action Dependencies
|
||||||
|
if: steps.prepare.outputs.contains_trigger == 'true'
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
echo "Installing base-action dependencies..."
|
||||||
|
cd ${GITHUB_ACTION_PATH}/base-action
|
||||||
|
bun install
|
||||||
|
echo "Base-action dependencies installed"
|
||||||
|
cd -
|
||||||
|
# Install Claude Code globally
|
||||||
|
bun install -g @anthropic-ai/claude-code
|
||||||
|
|
||||||
|
- name: Setup Network Restrictions
|
||||||
|
if: steps.prepare.outputs.contains_trigger == 'true' && inputs.experimental_allowed_domains != ''
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
chmod +x ${GITHUB_ACTION_PATH}/scripts/setup-network-restrictions.sh
|
||||||
|
${GITHUB_ACTION_PATH}/scripts/setup-network-restrictions.sh
|
||||||
|
env:
|
||||||
EXPERIMENTAL_ALLOWED_DOMAINS: ${{ inputs.experimental_allowed_domains }}
|
EXPERIMENTAL_ALLOWED_DOMAINS: ${{ inputs.experimental_allowed_domains }}
|
||||||
|
|
||||||
# Claude configuration
|
- name: Run Claude Code
|
||||||
ALLOWED_TOOLS: ${{ inputs.allowed_tools }}
|
id: claude-code
|
||||||
DISALLOWED_TOOLS: ${{ inputs.disallowed_tools }}
|
if: steps.prepare.outputs.contains_trigger == 'true'
|
||||||
MAX_TURNS: ${{ inputs.max_turns }}
|
shell: bash
|
||||||
SETTINGS: ${{ inputs.settings }}
|
run: |
|
||||||
TIMEOUT_MINUTES: ${{ inputs.timeout_minutes }}
|
# Run the base-action
|
||||||
CLAUDE_ENV: ${{ inputs.claude_env }}
|
bun run ${GITHUB_ACTION_PATH}/base-action/src/index.ts
|
||||||
FALLBACK_MODEL: ${{ inputs.fallback_model }}
|
env:
|
||||||
|
# Base-action inputs
|
||||||
|
CLAUDE_CODE_ACTION: "1"
|
||||||
|
INPUT_PROMPT_FILE: ${{ runner.temp }}/claude-prompts/claude-prompt.txt
|
||||||
|
INPUT_ALLOWED_TOOLS: ${{ env.ALLOWED_TOOLS }}
|
||||||
|
INPUT_DISALLOWED_TOOLS: ${{ env.DISALLOWED_TOOLS }}
|
||||||
|
INPUT_MAX_TURNS: ${{ inputs.max_turns }}
|
||||||
|
INPUT_MCP_CONFIG: ${{ steps.prepare.outputs.mcp_config }}
|
||||||
|
INPUT_SETTINGS: ${{ inputs.settings }}
|
||||||
|
INPUT_SYSTEM_PROMPT: ""
|
||||||
|
INPUT_APPEND_SYSTEM_PROMPT: ${{ env.APPEND_SYSTEM_PROMPT }}
|
||||||
|
INPUT_TIMEOUT_MINUTES: ${{ inputs.timeout_minutes }}
|
||||||
|
INPUT_CLAUDE_ENV: ${{ inputs.claude_env }}
|
||||||
|
INPUT_FALLBACK_MODEL: ${{ inputs.fallback_model }}
|
||||||
INPUT_EXPERIMENTAL_SLASH_COMMANDS_DIR: ${{ github.action_path }}/slash-commands
|
INPUT_EXPERIMENTAL_SLASH_COMMANDS_DIR: ${{ github.action_path }}/slash-commands
|
||||||
|
INPUT_STREAM_CONFIG: ${{ steps.prepare.outputs.stream_config }}
|
||||||
|
|
||||||
# Model configuration
|
# Model configuration
|
||||||
MODEL: ${{ inputs.model }}
|
ANTHROPIC_MODEL: ${{ steps.prepare.outputs.anthropic_model || inputs.model || inputs.anthropic_model }}
|
||||||
ANTHROPIC_MODEL: ${{ inputs.model || inputs.anthropic_model }}
|
GITHUB_TOKEN: ${{ steps.prepare.outputs.GITHUB_TOKEN }}
|
||||||
|
NODE_VERSION: ${{ env.NODE_VERSION }}
|
||||||
|
DETAILED_PERMISSION_MESSAGES: "1"
|
||||||
|
|
||||||
# Provider configuration
|
# Provider configuration
|
||||||
ANTHROPIC_API_KEY: ${{ inputs.anthropic_api_key }}
|
ANTHROPIC_API_KEY: ${{ inputs.anthropic_api_key }}
|
||||||
CLAUDE_CODE_OAUTH_TOKEN: ${{ inputs.claude_code_oauth_token }}
|
CLAUDE_CODE_OAUTH_TOKEN: ${{ steps.prepare.outputs.claude_code_oauth_token || inputs.claude_code_oauth_token }}
|
||||||
ANTHROPIC_BASE_URL: ${{ env.ANTHROPIC_BASE_URL }}
|
ANTHROPIC_BASE_URL: ${{ env.ANTHROPIC_BASE_URL }}
|
||||||
CLAUDE_CODE_USE_BEDROCK: ${{ inputs.use_bedrock == 'true' && '1' || '' }}
|
CLAUDE_CODE_USE_BEDROCK: ${{ inputs.use_bedrock == 'true' && '1' || '' }}
|
||||||
CLAUDE_CODE_USE_VERTEX: ${{ inputs.use_vertex == 'true' && '1' || '' }}
|
CLAUDE_CODE_USE_VERTEX: ${{ inputs.use_vertex == 'true' && '1' || '' }}
|
||||||
@@ -218,36 +242,47 @@ runs:
|
|||||||
VERTEX_REGION_CLAUDE_3_5_SONNET: ${{ env.VERTEX_REGION_CLAUDE_3_5_SONNET }}
|
VERTEX_REGION_CLAUDE_3_5_SONNET: ${{ env.VERTEX_REGION_CLAUDE_3_5_SONNET }}
|
||||||
VERTEX_REGION_CLAUDE_3_7_SONNET: ${{ env.VERTEX_REGION_CLAUDE_3_7_SONNET }}
|
VERTEX_REGION_CLAUDE_3_7_SONNET: ${{ env.VERTEX_REGION_CLAUDE_3_7_SONNET }}
|
||||||
|
|
||||||
|
- name: Report Claude completion
|
||||||
|
if: steps.prepare.outputs.contains_trigger == 'true' && always()
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
bun run ${GITHUB_ACTION_PATH}/src/entrypoints/report-claude-complete.ts
|
||||||
|
env:
|
||||||
|
MODE: ${{ inputs.mode }}
|
||||||
|
STREAM_CONFIG: ${{ steps.prepare.outputs.stream_config }}
|
||||||
|
CLAUDE_CONCLUSION: ${{ steps.claude-code.outputs.conclusion }}
|
||||||
|
CLAUDE_START_TIME: ${{ steps.prepare.outputs.claude_start_time }}
|
||||||
|
|
||||||
- name: Update comment with job link
|
- name: Update comment with job link
|
||||||
if: steps.claude.outputs.contains_trigger == 'true' && steps.claude.outputs.claude_comment_id && always()
|
if: steps.prepare.outputs.contains_trigger == 'true' && steps.prepare.outputs.claude_comment_id && always()
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
bun run ${GITHUB_ACTION_PATH}/src/entrypoints/update-comment-link.ts
|
bun run ${GITHUB_ACTION_PATH}/src/entrypoints/update-comment-link.ts
|
||||||
env:
|
env:
|
||||||
REPOSITORY: ${{ github.repository }}
|
REPOSITORY: ${{ github.repository }}
|
||||||
PR_NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
|
PR_NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
|
||||||
CLAUDE_COMMENT_ID: ${{ steps.claude.outputs.claude_comment_id }}
|
CLAUDE_COMMENT_ID: ${{ steps.prepare.outputs.claude_comment_id }}
|
||||||
GITHUB_RUN_ID: ${{ github.run_id }}
|
GITHUB_RUN_ID: ${{ github.run_id }}
|
||||||
GITHUB_TOKEN: ${{ steps.claude.outputs.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ steps.prepare.outputs.GITHUB_TOKEN }}
|
||||||
GITHUB_EVENT_NAME: ${{ github.event_name }}
|
GITHUB_EVENT_NAME: ${{ github.event_name }}
|
||||||
TRIGGER_COMMENT_ID: ${{ github.event.comment.id }}
|
TRIGGER_COMMENT_ID: ${{ github.event.comment.id }}
|
||||||
CLAUDE_BRANCH: ${{ steps.claude.outputs.CLAUDE_BRANCH }}
|
CLAUDE_BRANCH: ${{ steps.prepare.outputs.CLAUDE_BRANCH }}
|
||||||
IS_PR: ${{ github.event.issue.pull_request != null || github.event_name == 'pull_request_review_comment' }}
|
IS_PR: ${{ github.event.issue.pull_request != null || github.event_name == 'pull_request_review_comment' }}
|
||||||
BASE_BRANCH: ${{ steps.claude.outputs.BASE_BRANCH }}
|
BASE_BRANCH: ${{ steps.prepare.outputs.BASE_BRANCH }}
|
||||||
CLAUDE_SUCCESS: ${{ steps.claude.outputs.conclusion == 'success' }}
|
CLAUDE_SUCCESS: ${{ steps.claude-code.outputs.conclusion == 'success' }}
|
||||||
OUTPUT_FILE: ${{ steps.claude.outputs.execution_file || '' }}
|
OUTPUT_FILE: ${{ steps.claude-code.outputs.execution_file || '' }}
|
||||||
TRIGGER_USERNAME: ${{ github.event.comment.user.login || github.event.issue.user.login || github.event.pull_request.user.login || github.event.sender.login || github.triggering_actor || github.actor || '' }}
|
TRIGGER_USERNAME: ${{ github.event.comment.user.login || github.event.issue.user.login || github.event.pull_request.user.login || github.event.sender.login || github.triggering_actor || github.actor || '' }}
|
||||||
PREPARE_SUCCESS: ${{ steps.claude.outcome == 'success' }}
|
PREPARE_SUCCESS: ${{ steps.prepare.outcome == 'success' }}
|
||||||
PREPARE_ERROR: ${{ steps.claude.outputs.prepare_error || '' }}
|
PREPARE_ERROR: ${{ steps.prepare.outputs.prepare_error || '' }}
|
||||||
USE_STICKY_COMMENT: ${{ inputs.use_sticky_comment }}
|
USE_STICKY_COMMENT: ${{ inputs.use_sticky_comment }}
|
||||||
USE_COMMIT_SIGNING: ${{ inputs.use_commit_signing }}
|
USE_COMMIT_SIGNING: ${{ inputs.use_commit_signing }}
|
||||||
|
|
||||||
- name: Display Claude Code Report
|
- name: Display Claude Code Report
|
||||||
if: steps.claude.outputs.contains_trigger == 'true' && steps.claude.outputs.execution_file != ''
|
if: steps.prepare.outputs.contains_trigger == 'true' && steps.claude-code.outputs.execution_file != ''
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
# Try to format the turns, but if it fails, dump the raw JSON
|
# Try to format the turns, but if it fails, dump the raw JSON
|
||||||
if bun run ${{ github.action_path }}/src/entrypoints/format-turns.ts "${{ steps.claude.outputs.execution_file }}" >> $GITHUB_STEP_SUMMARY 2>/dev/null; then
|
if bun run ${{ github.action_path }}/src/entrypoints/format-turns.ts "${{ steps.claude-code.outputs.execution_file }}" >> $GITHUB_STEP_SUMMARY 2>/dev/null; then
|
||||||
echo "Successfully formatted Claude Code report"
|
echo "Successfully formatted Claude Code report"
|
||||||
else
|
else
|
||||||
echo "## Claude Code Report (Raw Output)" >> $GITHUB_STEP_SUMMARY
|
echo "## Claude Code Report (Raw Output)" >> $GITHUB_STEP_SUMMARY
|
||||||
@@ -255,7 +290,7 @@ runs:
|
|||||||
echo "Failed to format output (please report). Here's the raw JSON:" >> $GITHUB_STEP_SUMMARY
|
echo "Failed to format output (please report). Here's the raw JSON:" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "" >> $GITHUB_STEP_SUMMARY
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
echo '```json' >> $GITHUB_STEP_SUMMARY
|
echo '```json' >> $GITHUB_STEP_SUMMARY
|
||||||
cat "${{ steps.claude.outputs.execution_file }}" >> $GITHUB_STEP_SUMMARY
|
cat "${{ steps.claude-code.outputs.execution_file }}" >> $GITHUB_STEP_SUMMARY
|
||||||
echo '```' >> $GITHUB_STEP_SUMMARY
|
echo '```' >> $GITHUB_STEP_SUMMARY
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -266,6 +301,6 @@ runs:
|
|||||||
curl -L \
|
curl -L \
|
||||||
-X DELETE \
|
-X DELETE \
|
||||||
-H "Accept: application/vnd.github+json" \
|
-H "Accept: application/vnd.github+json" \
|
||||||
-H "Authorization: Bearer ${{ steps.claude.outputs.GITHUB_TOKEN }}" \
|
-H "Authorization: Bearer ${{ steps.prepare.outputs.GITHUB_TOKEN }}" \
|
||||||
-H "X-GitHub-Api-Version: 2022-11-28" \
|
-H "X-GitHub-Api-Version: 2022-11-28" \
|
||||||
${GITHUB_API_URL:-https://api.github.com}/installation/token
|
${GITHUB_API_URL:-https://api.github.com}/installation/token
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ runs:
|
|||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # https://github.com/actions/setup-node/releases/tag/v4.4.0
|
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # https://github.com/actions/setup-node/releases/tag/v4.4.0
|
||||||
with:
|
with:
|
||||||
node-version: ${{ env.NODE_VERSION || '18.x' }}
|
node-version: ${{ env.NODE_VERSION || '22.x' }}
|
||||||
cache: ${{ inputs.use_node_cache == 'true' && 'npm' || '' }}
|
cache: ${{ inputs.use_node_cache == 'true' && 'npm' || '' }}
|
||||||
|
|
||||||
- name: Install Bun
|
- name: Install Bun
|
||||||
@@ -118,7 +118,9 @@ runs:
|
|||||||
|
|
||||||
- name: Install Claude Code
|
- name: Install Claude Code
|
||||||
shell: bash
|
shell: bash
|
||||||
run: bun install -g @anthropic-ai/claude-code@1.0.69
|
run: |
|
||||||
|
# Install Claude Code
|
||||||
|
bun install -g @anthropic-ai/claude-code
|
||||||
|
|
||||||
- name: Run Claude Code Action
|
- name: Run Claude Code Action
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
import { preparePrompt } from "./prepare-prompt";
|
import { preparePrompt } from "./prepare-prompt";
|
||||||
import { runClaudeCore } from "./run-claude-core";
|
import { runClaude } from "./run-claude";
|
||||||
import { setupClaudeCodeSettings } from "./setup-claude-code-settings";
|
import { setupClaudeCodeSettings } from "./setup-claude-code-settings";
|
||||||
import { validateEnvironmentVariables } from "./validate-env";
|
import { validateEnvironmentVariables } from "./validate-env";
|
||||||
|
|
||||||
@@ -21,9 +21,7 @@ async function run() {
|
|||||||
promptFile: process.env.INPUT_PROMPT_FILE || "",
|
promptFile: process.env.INPUT_PROMPT_FILE || "",
|
||||||
});
|
});
|
||||||
|
|
||||||
await runClaudeCore({
|
await runClaude(promptConfig.path, {
|
||||||
promptFile: promptConfig.path,
|
|
||||||
settings: process.env.INPUT_SETTINGS,
|
|
||||||
allowedTools: process.env.INPUT_ALLOWED_TOOLS,
|
allowedTools: process.env.INPUT_ALLOWED_TOOLS,
|
||||||
disallowedTools: process.env.INPUT_DISALLOWED_TOOLS,
|
disallowedTools: process.env.INPUT_DISALLOWED_TOOLS,
|
||||||
maxTurns: process.env.INPUT_MAX_TURNS,
|
maxTurns: process.env.INPUT_MAX_TURNS,
|
||||||
@@ -32,8 +30,7 @@ async function run() {
|
|||||||
appendSystemPrompt: process.env.INPUT_APPEND_SYSTEM_PROMPT,
|
appendSystemPrompt: process.env.INPUT_APPEND_SYSTEM_PROMPT,
|
||||||
claudeEnv: process.env.INPUT_CLAUDE_ENV,
|
claudeEnv: process.env.INPUT_CLAUDE_ENV,
|
||||||
fallbackModel: process.env.INPUT_FALLBACK_MODEL,
|
fallbackModel: process.env.INPUT_FALLBACK_MODEL,
|
||||||
model: process.env.ANTHROPIC_MODEL,
|
streamConfig: process.env.INPUT_STREAM_CONFIG,
|
||||||
timeoutMinutes: process.env.INPUT_TIMEOUT_MINUTES,
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(`Action failed with error: ${error}`);
|
core.setFailed(`Action failed with error: ${error}`);
|
||||||
|
|||||||
@@ -1,366 +0,0 @@
|
|||||||
#!/usr/bin/env bun
|
|
||||||
|
|
||||||
import * as core from "@actions/core";
|
|
||||||
import { exec } from "child_process";
|
|
||||||
import { promisify } from "util";
|
|
||||||
import { unlink, writeFile, stat } from "fs/promises";
|
|
||||||
import { createWriteStream } from "fs";
|
|
||||||
import { spawn } from "child_process";
|
|
||||||
|
|
||||||
const execAsync = promisify(exec);
|
|
||||||
|
|
||||||
const PIPE_PATH = `${process.env.RUNNER_TEMP}/claude_prompt_pipe`;
|
|
||||||
const EXECUTION_FILE = `${process.env.RUNNER_TEMP}/claude-execution-output.json`;
|
|
||||||
const BASE_ARGS = ["-p", "--verbose", "--output-format", "stream-json"];
|
|
||||||
|
|
||||||
export type ClaudeOptions = {
|
|
||||||
allowedTools?: string;
|
|
||||||
disallowedTools?: string;
|
|
||||||
maxTurns?: string;
|
|
||||||
mcpConfig?: string;
|
|
||||||
systemPrompt?: string;
|
|
||||||
appendSystemPrompt?: string;
|
|
||||||
claudeEnv?: string;
|
|
||||||
fallbackModel?: string;
|
|
||||||
model?: string;
|
|
||||||
timeoutMinutes?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type RunClaudeConfig = {
|
|
||||||
promptFile: string;
|
|
||||||
settings?: string;
|
|
||||||
allowedTools?: string;
|
|
||||||
disallowedTools?: string;
|
|
||||||
maxTurns?: string;
|
|
||||||
mcpConfig?: string;
|
|
||||||
systemPrompt?: string;
|
|
||||||
appendSystemPrompt?: string;
|
|
||||||
claudeEnv?: string;
|
|
||||||
fallbackModel?: string;
|
|
||||||
model?: string;
|
|
||||||
timeoutMinutes?: string;
|
|
||||||
env?: Record<string, string>;
|
|
||||||
};
|
|
||||||
|
|
||||||
function parseCustomEnvVars(claudeEnv?: string): Record<string, string> {
|
|
||||||
if (!claudeEnv || claudeEnv.trim() === "") {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
const customEnv: Record<string, string> = {};
|
|
||||||
|
|
||||||
// Split by lines and parse each line as KEY: VALUE
|
|
||||||
const lines = claudeEnv.split("\n");
|
|
||||||
|
|
||||||
for (const line of lines) {
|
|
||||||
const trimmedLine = line.trim();
|
|
||||||
if (trimmedLine === "" || trimmedLine.startsWith("#")) {
|
|
||||||
continue; // Skip empty lines and comments
|
|
||||||
}
|
|
||||||
|
|
||||||
const colonIndex = trimmedLine.indexOf(":");
|
|
||||||
if (colonIndex === -1) {
|
|
||||||
continue; // Skip lines without colons
|
|
||||||
}
|
|
||||||
|
|
||||||
const key = trimmedLine.substring(0, colonIndex).trim();
|
|
||||||
const value = trimmedLine.substring(colonIndex + 1).trim();
|
|
||||||
|
|
||||||
if (key) {
|
|
||||||
customEnv[key] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return customEnv;
|
|
||||||
}
|
|
||||||
|
|
||||||
function prepareClaudeArgs(config: RunClaudeConfig): string[] {
|
|
||||||
const claudeArgs = [...BASE_ARGS];
|
|
||||||
|
|
||||||
if (config.allowedTools) {
|
|
||||||
claudeArgs.push("--allowedTools", config.allowedTools);
|
|
||||||
}
|
|
||||||
if (config.disallowedTools) {
|
|
||||||
claudeArgs.push("--disallowedTools", config.disallowedTools);
|
|
||||||
}
|
|
||||||
if (config.maxTurns) {
|
|
||||||
const maxTurnsNum = parseInt(config.maxTurns, 10);
|
|
||||||
if (isNaN(maxTurnsNum) || maxTurnsNum <= 0) {
|
|
||||||
throw new Error(
|
|
||||||
`maxTurns must be a positive number, got: ${config.maxTurns}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
claudeArgs.push("--max-turns", config.maxTurns);
|
|
||||||
}
|
|
||||||
if (config.mcpConfig) {
|
|
||||||
claudeArgs.push("--mcp-config", config.mcpConfig);
|
|
||||||
}
|
|
||||||
if (config.systemPrompt) {
|
|
||||||
claudeArgs.push("--system-prompt", config.systemPrompt);
|
|
||||||
}
|
|
||||||
if (config.appendSystemPrompt) {
|
|
||||||
claudeArgs.push("--append-system-prompt", config.appendSystemPrompt);
|
|
||||||
}
|
|
||||||
if (config.fallbackModel) {
|
|
||||||
claudeArgs.push("--fallback-model", config.fallbackModel);
|
|
||||||
}
|
|
||||||
if (config.model) {
|
|
||||||
claudeArgs.push("--model", config.model);
|
|
||||||
}
|
|
||||||
if (config.timeoutMinutes) {
|
|
||||||
const timeoutMinutesNum = parseInt(config.timeoutMinutes, 10);
|
|
||||||
if (isNaN(timeoutMinutesNum) || timeoutMinutesNum <= 0) {
|
|
||||||
throw new Error(
|
|
||||||
`timeoutMinutes must be a positive number, got: ${config.timeoutMinutes}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return claudeArgs;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function prepareRunConfig(
|
|
||||||
promptPath: string,
|
|
||||||
options: ClaudeOptions,
|
|
||||||
): { claudeArgs: string[]; promptPath: string; env: Record<string, string> } {
|
|
||||||
const config: RunClaudeConfig = {
|
|
||||||
promptFile: promptPath,
|
|
||||||
...options,
|
|
||||||
};
|
|
||||||
|
|
||||||
const claudeArgs = prepareClaudeArgs(config);
|
|
||||||
const customEnv = parseCustomEnvVars(config.claudeEnv);
|
|
||||||
const mergedEnv = {
|
|
||||||
...customEnv,
|
|
||||||
...(config.env || {}),
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
claudeArgs,
|
|
||||||
promptPath,
|
|
||||||
env: mergedEnv,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function runClaudeCore(config: RunClaudeConfig) {
|
|
||||||
const claudeArgs = prepareClaudeArgs(config);
|
|
||||||
|
|
||||||
// Parse custom environment variables from claudeEnv
|
|
||||||
const customEnv = parseCustomEnvVars(config.claudeEnv);
|
|
||||||
|
|
||||||
// Merge with additional env vars passed in config
|
|
||||||
const mergedEnv = {
|
|
||||||
...customEnv,
|
|
||||||
...(config.env || {}),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create a named pipe
|
|
||||||
try {
|
|
||||||
await unlink(PIPE_PATH);
|
|
||||||
} catch (e) {
|
|
||||||
// Ignore if file doesn't exist
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the named pipe
|
|
||||||
await execAsync(`mkfifo "${PIPE_PATH}"`);
|
|
||||||
|
|
||||||
// Log prompt file size
|
|
||||||
let promptSize = "unknown";
|
|
||||||
try {
|
|
||||||
const stats = await stat(config.promptFile);
|
|
||||||
promptSize = stats.size.toString();
|
|
||||||
} catch (e) {
|
|
||||||
// Ignore error
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`Prompt file size: ${promptSize} bytes`);
|
|
||||||
|
|
||||||
// Log custom environment variables if any
|
|
||||||
const totalEnvVars = Object.keys(mergedEnv).length;
|
|
||||||
if (totalEnvVars > 0) {
|
|
||||||
const envKeys = Object.keys(mergedEnv).join(", ");
|
|
||||||
console.log(`Custom environment variables (${totalEnvVars}): ${envKeys}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output to console
|
|
||||||
console.log(`Running Claude with prompt from file: ${config.promptFile}`);
|
|
||||||
|
|
||||||
// Start sending prompt to pipe in background
|
|
||||||
const catProcess = spawn("cat", [config.promptFile], {
|
|
||||||
stdio: ["ignore", "pipe", "inherit"],
|
|
||||||
});
|
|
||||||
const pipeStream = createWriteStream(PIPE_PATH);
|
|
||||||
catProcess.stdout.pipe(pipeStream);
|
|
||||||
|
|
||||||
catProcess.on("error", (error) => {
|
|
||||||
console.error("Error reading prompt file:", error);
|
|
||||||
pipeStream.destroy();
|
|
||||||
});
|
|
||||||
|
|
||||||
const claudeProcess = spawn("claude", claudeArgs, {
|
|
||||||
stdio: ["pipe", "pipe", "inherit"],
|
|
||||||
env: {
|
|
||||||
...process.env,
|
|
||||||
...mergedEnv,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle Claude process errors
|
|
||||||
claudeProcess.on("error", (error) => {
|
|
||||||
console.error("Error spawning Claude process:", error);
|
|
||||||
pipeStream.destroy();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Capture output for parsing execution metrics
|
|
||||||
let output = "";
|
|
||||||
claudeProcess.stdout.on("data", (data) => {
|
|
||||||
const text = data.toString();
|
|
||||||
|
|
||||||
// Try to parse as JSON and pretty print if it's on a single line
|
|
||||||
const lines = text.split("\n");
|
|
||||||
lines.forEach((line: string, index: number) => {
|
|
||||||
if (line.trim() === "") return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Check if this line is a JSON object
|
|
||||||
const parsed = JSON.parse(line);
|
|
||||||
const prettyJson = JSON.stringify(parsed, null, 2);
|
|
||||||
process.stdout.write(prettyJson);
|
|
||||||
if (index < lines.length - 1 || text.endsWith("\n")) {
|
|
||||||
process.stdout.write("\n");
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
// Not a JSON object, print as is
|
|
||||||
process.stdout.write(line);
|
|
||||||
if (index < lines.length - 1 || text.endsWith("\n")) {
|
|
||||||
process.stdout.write("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
output += text;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle stdout errors
|
|
||||||
claudeProcess.stdout.on("error", (error) => {
|
|
||||||
console.error("Error reading Claude stdout:", error);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Pipe from named pipe to Claude
|
|
||||||
const pipeProcess = spawn("cat", [PIPE_PATH]);
|
|
||||||
pipeProcess.stdout.pipe(claudeProcess.stdin);
|
|
||||||
|
|
||||||
// Handle pipe process errors
|
|
||||||
pipeProcess.on("error", (error) => {
|
|
||||||
console.error("Error reading from named pipe:", error);
|
|
||||||
claudeProcess.kill("SIGTERM");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Wait for Claude to finish with timeout
|
|
||||||
let timeoutMs = 10 * 60 * 1000; // Default 10 minutes
|
|
||||||
if (config.timeoutMinutes) {
|
|
||||||
timeoutMs = parseInt(config.timeoutMinutes, 10) * 60 * 1000;
|
|
||||||
} else if (process.env.INPUT_TIMEOUT_MINUTES) {
|
|
||||||
const envTimeout = parseInt(process.env.INPUT_TIMEOUT_MINUTES, 10);
|
|
||||||
if (isNaN(envTimeout) || envTimeout <= 0) {
|
|
||||||
throw new Error(
|
|
||||||
`INPUT_TIMEOUT_MINUTES must be a positive number, got: ${process.env.INPUT_TIMEOUT_MINUTES}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
timeoutMs = envTimeout * 60 * 1000;
|
|
||||||
}
|
|
||||||
const exitCode = await new Promise<number>((resolve) => {
|
|
||||||
let resolved = false;
|
|
||||||
|
|
||||||
// Set a timeout for the process
|
|
||||||
const timeoutId = setTimeout(() => {
|
|
||||||
if (!resolved) {
|
|
||||||
console.error(
|
|
||||||
`Claude process timed out after ${timeoutMs / 1000} seconds`,
|
|
||||||
);
|
|
||||||
claudeProcess.kill("SIGTERM");
|
|
||||||
// Give it 5 seconds to terminate gracefully, then force kill
|
|
||||||
setTimeout(() => {
|
|
||||||
try {
|
|
||||||
claudeProcess.kill("SIGKILL");
|
|
||||||
} catch (e) {
|
|
||||||
// Process may already be dead
|
|
||||||
}
|
|
||||||
}, 5000);
|
|
||||||
resolved = true;
|
|
||||||
resolve(124); // Standard timeout exit code
|
|
||||||
}
|
|
||||||
}, timeoutMs);
|
|
||||||
|
|
||||||
claudeProcess.on("close", (code) => {
|
|
||||||
if (!resolved) {
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
resolved = true;
|
|
||||||
resolve(code || 0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
claudeProcess.on("error", (error) => {
|
|
||||||
if (!resolved) {
|
|
||||||
console.error("Claude process error:", error);
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
resolved = true;
|
|
||||||
resolve(1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Clean up processes
|
|
||||||
try {
|
|
||||||
catProcess.kill("SIGTERM");
|
|
||||||
} catch (e) {
|
|
||||||
// Process may already be dead
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
pipeProcess.kill("SIGTERM");
|
|
||||||
} catch (e) {
|
|
||||||
// Process may already be dead
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up pipe file
|
|
||||||
try {
|
|
||||||
await unlink(PIPE_PATH);
|
|
||||||
} catch (e) {
|
|
||||||
// Ignore errors during cleanup
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set conclusion based on exit code
|
|
||||||
if (exitCode === 0) {
|
|
||||||
// Try to process the output and save execution metrics
|
|
||||||
try {
|
|
||||||
await writeFile("output.txt", output);
|
|
||||||
|
|
||||||
// Process output.txt into JSON and save to execution file
|
|
||||||
const { stdout: jsonOutput } = await execAsync("jq -s '.' output.txt");
|
|
||||||
await writeFile(EXECUTION_FILE, jsonOutput);
|
|
||||||
|
|
||||||
console.log(`Log saved to ${EXECUTION_FILE}`);
|
|
||||||
} catch (e) {
|
|
||||||
core.warning(`Failed to process output for execution metrics: ${e}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
core.setOutput("conclusion", "success");
|
|
||||||
core.setOutput("execution_file", EXECUTION_FILE);
|
|
||||||
} else {
|
|
||||||
core.setOutput("conclusion", "failure");
|
|
||||||
|
|
||||||
// Still try to save execution file if we have output
|
|
||||||
if (output) {
|
|
||||||
try {
|
|
||||||
await writeFile("output.txt", output);
|
|
||||||
const { stdout: jsonOutput } = await execAsync("jq -s '.' output.txt");
|
|
||||||
await writeFile(EXECUTION_FILE, jsonOutput);
|
|
||||||
core.setOutput("execution_file", EXECUTION_FILE);
|
|
||||||
} catch (e) {
|
|
||||||
// Ignore errors when processing output during failure
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
process.exit(exitCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,44 +1,452 @@
|
|||||||
#!/usr/bin/env bun
|
|
||||||
|
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
import { preparePrompt } from "./prepare-prompt";
|
import { exec } from "child_process";
|
||||||
import { runClaudeCore } from "./run-claude-core";
|
import { promisify } from "util";
|
||||||
export { prepareRunConfig, type ClaudeOptions } from "./run-claude-core";
|
import { unlink, writeFile, stat } from "fs/promises";
|
||||||
import { setupClaudeCodeSettings } from "./setup-claude-code-settings";
|
import { createWriteStream } from "fs";
|
||||||
import { validateEnvironmentVariables } from "./validate-env";
|
import { spawn } from "child_process";
|
||||||
|
import { StreamHandler } from "./stream-handler";
|
||||||
|
|
||||||
async function run() {
|
const execAsync = promisify(exec);
|
||||||
|
|
||||||
|
const PIPE_PATH = `${process.env.RUNNER_TEMP}/claude_prompt_pipe`;
|
||||||
|
const EXECUTION_FILE = `${process.env.RUNNER_TEMP}/claude-execution-output.json`;
|
||||||
|
const BASE_ARGS = ["-p", "--verbose", "--output-format", "stream-json"];
|
||||||
|
|
||||||
|
export type ClaudeOptions = {
|
||||||
|
allowedTools?: string;
|
||||||
|
disallowedTools?: string;
|
||||||
|
maxTurns?: string;
|
||||||
|
mcpConfig?: string;
|
||||||
|
systemPrompt?: string;
|
||||||
|
appendSystemPrompt?: string;
|
||||||
|
claudeEnv?: string;
|
||||||
|
fallbackModel?: string;
|
||||||
|
timeoutMinutes?: string;
|
||||||
|
streamConfig?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type StreamConfig = {
|
||||||
|
progress_endpoint?: string;
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
resume_endpoint?: string;
|
||||||
|
session_id?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type PreparedConfig = {
|
||||||
|
claudeArgs: string[];
|
||||||
|
promptPath: string;
|
||||||
|
env: Record<string, string>;
|
||||||
|
};
|
||||||
|
|
||||||
|
function parseCustomEnvVars(claudeEnv?: string): Record<string, string> {
|
||||||
|
if (!claudeEnv || claudeEnv.trim() === "") {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const customEnv: Record<string, string> = {};
|
||||||
|
|
||||||
|
// Split by lines and parse each line as KEY: VALUE
|
||||||
|
const lines = claudeEnv.split("\n");
|
||||||
|
|
||||||
|
for (const line of lines) {
|
||||||
|
const trimmedLine = line.trim();
|
||||||
|
if (trimmedLine === "" || trimmedLine.startsWith("#")) {
|
||||||
|
continue; // Skip empty lines and comments
|
||||||
|
}
|
||||||
|
|
||||||
|
const colonIndex = trimmedLine.indexOf(":");
|
||||||
|
if (colonIndex === -1) {
|
||||||
|
continue; // Skip lines without colons
|
||||||
|
}
|
||||||
|
|
||||||
|
const key = trimmedLine.substring(0, colonIndex).trim();
|
||||||
|
const value = trimmedLine.substring(colonIndex + 1).trim();
|
||||||
|
|
||||||
|
if (key) {
|
||||||
|
customEnv[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return customEnv;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function prepareRunConfig(
|
||||||
|
promptPath: string,
|
||||||
|
options: ClaudeOptions,
|
||||||
|
): PreparedConfig {
|
||||||
|
const claudeArgs = [...BASE_ARGS];
|
||||||
|
|
||||||
|
if (options.allowedTools) {
|
||||||
|
claudeArgs.push("--allowedTools", options.allowedTools);
|
||||||
|
}
|
||||||
|
if (options.disallowedTools) {
|
||||||
|
claudeArgs.push("--disallowedTools", options.disallowedTools);
|
||||||
|
}
|
||||||
|
if (options.maxTurns) {
|
||||||
|
const maxTurnsNum = parseInt(options.maxTurns, 10);
|
||||||
|
if (isNaN(maxTurnsNum) || maxTurnsNum <= 0) {
|
||||||
|
throw new Error(
|
||||||
|
`maxTurns must be a positive number, got: ${options.maxTurns}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
claudeArgs.push("--max-turns", options.maxTurns);
|
||||||
|
}
|
||||||
|
if (options.mcpConfig) {
|
||||||
|
claudeArgs.push("--mcp-config", options.mcpConfig);
|
||||||
|
}
|
||||||
|
if (options.systemPrompt) {
|
||||||
|
claudeArgs.push("--system-prompt", options.systemPrompt);
|
||||||
|
}
|
||||||
|
if (options.appendSystemPrompt) {
|
||||||
|
claudeArgs.push("--append-system-prompt", options.appendSystemPrompt);
|
||||||
|
}
|
||||||
|
if (options.fallbackModel) {
|
||||||
|
claudeArgs.push("--fallback-model", options.fallbackModel);
|
||||||
|
}
|
||||||
|
if (options.timeoutMinutes) {
|
||||||
|
const timeoutMinutesNum = parseInt(options.timeoutMinutes, 10);
|
||||||
|
if (isNaN(timeoutMinutesNum) || timeoutMinutesNum <= 0) {
|
||||||
|
throw new Error(
|
||||||
|
`timeoutMinutes must be a positive number, got: ${options.timeoutMinutes}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Parse stream config for session_id and resume_endpoint
|
||||||
|
if (options.streamConfig) {
|
||||||
try {
|
try {
|
||||||
validateEnvironmentVariables();
|
const streamConfig: StreamConfig = JSON.parse(options.streamConfig);
|
||||||
|
// Add --session-id if session_id is provided
|
||||||
|
if (streamConfig.session_id) {
|
||||||
|
claudeArgs.push("--session-id", streamConfig.session_id);
|
||||||
|
}
|
||||||
|
// Only add --teleport if we have both session_id AND resume_endpoint
|
||||||
|
if (streamConfig.session_id && streamConfig.resume_endpoint) {
|
||||||
|
claudeArgs.push("--teleport", streamConfig.session_id);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Failed to parse stream_config JSON:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await setupClaudeCodeSettings(process.env.INPUT_SETTINGS);
|
// Parse custom environment variables
|
||||||
|
const customEnv = parseCustomEnvVars(options.claudeEnv);
|
||||||
|
|
||||||
const promptConfig = await preparePrompt({
|
return {
|
||||||
prompt: process.env.INPUT_PROMPT || "",
|
claudeArgs,
|
||||||
promptFile: process.env.INPUT_PROMPT_FILE || "",
|
promptPath,
|
||||||
|
env: customEnv,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runClaude(promptPath: string, options: ClaudeOptions) {
|
||||||
|
const config = prepareRunConfig(promptPath, options);
|
||||||
|
|
||||||
|
// Set up streaming if endpoint is provided in stream config
|
||||||
|
let streamHandler: StreamHandler | null = null;
|
||||||
|
let streamConfig: StreamConfig | null = null;
|
||||||
|
if (options.streamConfig) {
|
||||||
|
try {
|
||||||
|
streamConfig = JSON.parse(options.streamConfig);
|
||||||
|
if (streamConfig?.progress_endpoint) {
|
||||||
|
const customHeaders = streamConfig.headers || {};
|
||||||
|
console.log("parsed headers", customHeaders);
|
||||||
|
Object.keys(customHeaders).forEach((key) => {
|
||||||
|
console.log(`Custom header: ${key} = ${customHeaders[key]}`);
|
||||||
|
});
|
||||||
|
streamHandler = new StreamHandler(
|
||||||
|
streamConfig.progress_endpoint,
|
||||||
|
customHeaders,
|
||||||
|
);
|
||||||
|
console.log(`Streaming output to: ${streamConfig.progress_endpoint}`);
|
||||||
|
if (Object.keys(customHeaders).length > 0) {
|
||||||
|
console.log(
|
||||||
|
`Custom streaming headers: ${Object.keys(customHeaders).join(", ")}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Failed to parse stream_config JSON:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a named pipe
|
||||||
|
try {
|
||||||
|
await unlink(PIPE_PATH);
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore if file doesn't exist
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the named pipe
|
||||||
|
await execAsync(`mkfifo "${PIPE_PATH}"`);
|
||||||
|
|
||||||
|
// Log prompt file size
|
||||||
|
let promptSize = "unknown";
|
||||||
|
try {
|
||||||
|
const stats = await stat(config.promptPath);
|
||||||
|
promptSize = stats.size.toString();
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore error
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Prompt file size: ${promptSize} bytes`);
|
||||||
|
|
||||||
|
// Log custom environment variables if any
|
||||||
|
if (Object.keys(config.env).length > 0) {
|
||||||
|
const envKeys = Object.keys(config.env).join(", ");
|
||||||
|
console.log(`Custom environment variables: ${envKeys}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output to console
|
||||||
|
console.log(`Running Claude with prompt from file: ${config.promptPath}`);
|
||||||
|
|
||||||
|
// Start sending prompt to pipe in background
|
||||||
|
const catProcess = spawn("cat", [config.promptPath], {
|
||||||
|
stdio: ["ignore", "pipe", "inherit"],
|
||||||
|
});
|
||||||
|
const pipeStream = createWriteStream(PIPE_PATH);
|
||||||
|
catProcess.stdout.pipe(pipeStream);
|
||||||
|
|
||||||
|
catProcess.on("error", (error) => {
|
||||||
|
console.error("Error reading prompt file:", error);
|
||||||
|
pipeStream.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
await runClaudeCore({
|
// Prepare environment variables
|
||||||
promptFile: promptConfig.path,
|
const processEnv = {
|
||||||
settings: process.env.INPUT_SETTINGS,
|
...process.env,
|
||||||
allowedTools: process.env.INPUT_ALLOWED_TOOLS,
|
...config.env,
|
||||||
disallowedTools: process.env.INPUT_DISALLOWED_TOOLS,
|
};
|
||||||
maxTurns: process.env.INPUT_MAX_TURNS,
|
|
||||||
mcpConfig: process.env.INPUT_MCP_CONFIG,
|
// If both session_id and resume_endpoint are provided, set environment variables
|
||||||
systemPrompt: process.env.INPUT_SYSTEM_PROMPT,
|
if (streamConfig?.session_id && streamConfig?.resume_endpoint) {
|
||||||
appendSystemPrompt: process.env.INPUT_APPEND_SYSTEM_PROMPT,
|
processEnv.TELEPORT_RESUME_URL = streamConfig.resume_endpoint;
|
||||||
claudeEnv: process.env.INPUT_CLAUDE_ENV,
|
console.log(
|
||||||
fallbackModel: process.env.INPUT_FALLBACK_MODEL,
|
`Setting TELEPORT_RESUME_URL to: ${streamConfig.resume_endpoint}`,
|
||||||
model: process.env.ANTHROPIC_MODEL,
|
);
|
||||||
timeoutMinutes: process.env.INPUT_TIMEOUT_MINUTES,
|
|
||||||
|
if (streamConfig.headers && Object.keys(streamConfig.headers).length > 0) {
|
||||||
|
processEnv.TELEPORT_HEADERS = JSON.stringify(streamConfig.headers);
|
||||||
|
console.log(`Setting TELEPORT_HEADERS for resume endpoint`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log the full Claude command being executed
|
||||||
|
console.log(`Running Claude with args: ${config.claudeArgs.join(" ")}`);
|
||||||
|
|
||||||
|
const claudeProcess = spawn("claude", config.claudeArgs, {
|
||||||
|
stdio: ["pipe", "pipe", "inherit"],
|
||||||
|
env: processEnv,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Handle Claude process errors
|
||||||
|
claudeProcess.on("error", (error) => {
|
||||||
|
console.error("Error spawning Claude process:", error);
|
||||||
|
pipeStream.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Capture output for parsing execution metrics
|
||||||
|
let output = "";
|
||||||
|
let lineBuffer = ""; // Buffer for incomplete lines
|
||||||
|
|
||||||
|
claudeProcess.stdout.on("data", async (data) => {
|
||||||
|
const text = data.toString();
|
||||||
|
output += text;
|
||||||
|
|
||||||
|
// Add new data to line buffer
|
||||||
|
lineBuffer += text;
|
||||||
|
|
||||||
|
// Split into lines - the last element might be incomplete
|
||||||
|
const lines = lineBuffer.split("\n");
|
||||||
|
|
||||||
|
// The last element is either empty (if text ended with \n) or incomplete
|
||||||
|
lineBuffer = lines.pop() || "";
|
||||||
|
|
||||||
|
// Process complete lines
|
||||||
|
for (let index = 0; index < lines.length; index++) {
|
||||||
|
const line = lines[index];
|
||||||
|
if (!line || line.trim() === "") continue;
|
||||||
|
|
||||||
|
// Try to parse as JSON and pretty print if it's on a single line
|
||||||
|
try {
|
||||||
|
// Check if this line is a JSON object
|
||||||
|
const parsed = JSON.parse(line);
|
||||||
|
const prettyJson = JSON.stringify(parsed, null, 2);
|
||||||
|
process.stdout.write(prettyJson);
|
||||||
|
process.stdout.write("\n");
|
||||||
|
|
||||||
|
// Send valid JSON to stream handler if available
|
||||||
|
if (streamHandler) {
|
||||||
|
try {
|
||||||
|
// Send the original line (which is valid JSON) with newline for proper splitting
|
||||||
|
const dataToSend = line + "\n";
|
||||||
|
await streamHandler.addOutput(dataToSend);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(`Action failed with error: ${error}`);
|
core.warning(`Failed to stream output: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Not a JSON object, print as is
|
||||||
|
process.stdout.write(line);
|
||||||
|
process.stdout.write("\n");
|
||||||
|
// Don't send non-JSON lines to stream handler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle stdout errors
|
||||||
|
claudeProcess.stdout.on("error", (error) => {
|
||||||
|
console.error("Error reading Claude stdout:", error);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Pipe from named pipe to Claude
|
||||||
|
const pipeProcess = spawn("cat", [PIPE_PATH]);
|
||||||
|
pipeProcess.stdout.pipe(claudeProcess.stdin);
|
||||||
|
|
||||||
|
// Handle pipe process errors
|
||||||
|
pipeProcess.on("error", (error) => {
|
||||||
|
console.error("Error reading from named pipe:", error);
|
||||||
|
claudeProcess.kill("SIGTERM");
|
||||||
|
});
|
||||||
|
|
||||||
|
// Wait for Claude to finish with timeout
|
||||||
|
let timeoutMs = 10 * 60 * 1000; // Default 10 minutes
|
||||||
|
if (options.timeoutMinutes) {
|
||||||
|
timeoutMs = parseInt(options.timeoutMinutes, 10) * 60 * 1000;
|
||||||
|
} else if (process.env.INPUT_TIMEOUT_MINUTES) {
|
||||||
|
const envTimeout = parseInt(process.env.INPUT_TIMEOUT_MINUTES, 10);
|
||||||
|
if (isNaN(envTimeout) || envTimeout <= 0) {
|
||||||
|
throw new Error(
|
||||||
|
`INPUT_TIMEOUT_MINUTES must be a positive number, got: ${process.env.INPUT_TIMEOUT_MINUTES}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
timeoutMs = envTimeout * 60 * 1000;
|
||||||
|
}
|
||||||
|
const exitCode = await new Promise<number>((resolve) => {
|
||||||
|
let resolved = false;
|
||||||
|
|
||||||
|
// Set a timeout for the process
|
||||||
|
const timeoutId = setTimeout(() => {
|
||||||
|
if (!resolved) {
|
||||||
|
console.error(
|
||||||
|
`Claude process timed out after ${timeoutMs / 1000} seconds`,
|
||||||
|
);
|
||||||
|
claudeProcess.kill("SIGTERM");
|
||||||
|
// Give it 5 seconds to terminate gracefully, then force kill
|
||||||
|
setTimeout(() => {
|
||||||
|
try {
|
||||||
|
claudeProcess.kill("SIGKILL");
|
||||||
|
} catch (e) {
|
||||||
|
// Process may already be dead
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
|
resolved = true;
|
||||||
|
resolve(124); // Standard timeout exit code
|
||||||
|
}
|
||||||
|
}, timeoutMs);
|
||||||
|
|
||||||
|
claudeProcess.on("close", async (code) => {
|
||||||
|
if (!resolved) {
|
||||||
|
// Process any remaining data in the line buffer
|
||||||
|
if (lineBuffer.trim()) {
|
||||||
|
// Try to parse and print the remaining line
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(lineBuffer);
|
||||||
|
const prettyJson = JSON.stringify(parsed, null, 2);
|
||||||
|
process.stdout.write(prettyJson);
|
||||||
|
process.stdout.write("\n");
|
||||||
|
|
||||||
|
// Send valid JSON to stream handler if available
|
||||||
|
if (streamHandler) {
|
||||||
|
try {
|
||||||
|
const dataToSend = lineBuffer + "\n";
|
||||||
|
await streamHandler.addOutput(dataToSend);
|
||||||
|
} catch (error) {
|
||||||
|
core.warning(`Failed to stream final output: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
process.stdout.write(lineBuffer);
|
||||||
|
process.stdout.write("\n");
|
||||||
|
// Don't send non-JSON lines to stream handler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
resolved = true;
|
||||||
|
resolve(code || 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
claudeProcess.on("error", (error) => {
|
||||||
|
if (!resolved) {
|
||||||
|
console.error("Claude process error:", error);
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
resolved = true;
|
||||||
|
resolve(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Clean up streaming
|
||||||
|
if (streamHandler) {
|
||||||
|
try {
|
||||||
|
await streamHandler.close();
|
||||||
|
} catch (error) {
|
||||||
|
core.warning(`Failed to close stream handler: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up processes
|
||||||
|
try {
|
||||||
|
catProcess.kill("SIGTERM");
|
||||||
|
} catch (e) {
|
||||||
|
// Process may already be dead
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
pipeProcess.kill("SIGTERM");
|
||||||
|
} catch (e) {
|
||||||
|
// Process may already be dead
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up pipe file
|
||||||
|
try {
|
||||||
|
await unlink(PIPE_PATH);
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore errors during cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set conclusion based on exit code
|
||||||
|
if (exitCode === 0) {
|
||||||
|
// Try to process the output and save execution metrics
|
||||||
|
try {
|
||||||
|
await writeFile("output.txt", output);
|
||||||
|
|
||||||
|
// Process output.txt into JSON and save to execution file
|
||||||
|
const { stdout: jsonOutput } = await execAsync("jq -s '.' output.txt");
|
||||||
|
await writeFile(EXECUTION_FILE, jsonOutput);
|
||||||
|
|
||||||
|
console.log(`Log saved to ${EXECUTION_FILE}`);
|
||||||
|
} catch (e) {
|
||||||
|
core.warning(`Failed to process output for execution metrics: ${e}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
core.setOutput("conclusion", "success");
|
||||||
|
core.setOutput("execution_file", EXECUTION_FILE);
|
||||||
|
} else {
|
||||||
core.setOutput("conclusion", "failure");
|
core.setOutput("conclusion", "failure");
|
||||||
process.exit(1);
|
|
||||||
|
// Still try to save execution file if we have output
|
||||||
|
if (output) {
|
||||||
|
try {
|
||||||
|
await writeFile("output.txt", output);
|
||||||
|
const { stdout: jsonOutput } = await execAsync("jq -s '.' output.txt");
|
||||||
|
await writeFile(EXECUTION_FILE, jsonOutput);
|
||||||
|
core.setOutput("execution_file", EXECUTION_FILE);
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore errors when processing output during failure
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
process.exit(exitCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (import.meta.main) {
|
|
||||||
run();
|
|
||||||
}
|
|
||||||
|
|||||||
152
base-action/src/stream-handler.ts
Normal file
152
base-action/src/stream-handler.ts
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
import * as core from "@actions/core";
|
||||||
|
|
||||||
|
export function parseStreamHeaders(
|
||||||
|
headersInput?: string,
|
||||||
|
): Record<string, string> {
|
||||||
|
if (!headersInput || headersInput.trim() === "") {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(headersInput);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Failed to parse stream headers as JSON:", e);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TokenGetter = (audience: string) => Promise<string>;
|
||||||
|
|
||||||
|
export class StreamHandler {
|
||||||
|
private endpoint: string;
|
||||||
|
private customHeaders: Record<string, string>;
|
||||||
|
private tokenGetter: TokenGetter;
|
||||||
|
private token: string | null = null;
|
||||||
|
private tokenFetchTime: number = 0;
|
||||||
|
private buffer: string[] = [];
|
||||||
|
private flushTimer: NodeJS.Timeout | null = null;
|
||||||
|
private isClosed = false;
|
||||||
|
|
||||||
|
private readonly TOKEN_LIFETIME_MS = 4 * 60 * 1000; // 4 minutes
|
||||||
|
private readonly BATCH_SIZE = 10;
|
||||||
|
private readonly BATCH_TIMEOUT_MS = 1000;
|
||||||
|
private readonly REQUEST_TIMEOUT_MS = 5000;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
endpoint: string,
|
||||||
|
customHeaders: Record<string, string> = {},
|
||||||
|
tokenGetter?: TokenGetter,
|
||||||
|
) {
|
||||||
|
this.endpoint = endpoint;
|
||||||
|
this.customHeaders = customHeaders;
|
||||||
|
this.tokenGetter = tokenGetter || ((audience) => core.getIDToken(audience));
|
||||||
|
}
|
||||||
|
|
||||||
|
async addOutput(data: string): Promise<void> {
|
||||||
|
if (this.isClosed) return;
|
||||||
|
|
||||||
|
// Split by newlines and add to buffer
|
||||||
|
const lines = data.split("\n").filter((line) => line.length > 0);
|
||||||
|
this.buffer.push(...lines);
|
||||||
|
|
||||||
|
// Check if we should flush
|
||||||
|
if (this.buffer.length >= this.BATCH_SIZE) {
|
||||||
|
await this.flush();
|
||||||
|
} else {
|
||||||
|
// Set or reset the timer
|
||||||
|
this.resetFlushTimer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private resetFlushTimer(): void {
|
||||||
|
if (this.flushTimer) {
|
||||||
|
clearTimeout(this.flushTimer);
|
||||||
|
}
|
||||||
|
this.flushTimer = setTimeout(() => {
|
||||||
|
this.flush().catch((err) => {
|
||||||
|
core.warning(`Failed to flush stream buffer: ${err}`);
|
||||||
|
});
|
||||||
|
}, this.BATCH_TIMEOUT_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getToken(): Promise<string> {
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
// Check if we need a new token
|
||||||
|
if (!this.token || now - this.tokenFetchTime >= this.TOKEN_LIFETIME_MS) {
|
||||||
|
try {
|
||||||
|
this.token = await this.tokenGetter("claude-code-github-action");
|
||||||
|
this.tokenFetchTime = now;
|
||||||
|
core.debug("Fetched new OIDC token for streaming");
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to get OIDC token: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.token;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async flush(): Promise<void> {
|
||||||
|
if (this.buffer.length === 0) return;
|
||||||
|
|
||||||
|
// Clear the flush timer
|
||||||
|
if (this.flushTimer) {
|
||||||
|
clearTimeout(this.flushTimer);
|
||||||
|
this.flushTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the current buffer and clear it
|
||||||
|
const output = [...this.buffer];
|
||||||
|
this.buffer = [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const token = await this.getToken();
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
output: output,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create an AbortController for timeout
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => controller.abort(),
|
||||||
|
this.REQUEST_TIMEOUT_MS,
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fetch(this.endpoint, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
...this.customHeaders,
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
signal: controller.signal,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Log but don't throw - we don't want to interrupt Claude's execution
|
||||||
|
core.warning(`Failed to stream output: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async close(): Promise<void> {
|
||||||
|
// Clear any pending timer
|
||||||
|
if (this.flushTimer) {
|
||||||
|
clearTimeout(this.flushTimer);
|
||||||
|
this.flushTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flush any remaining output
|
||||||
|
if (this.buffer.length > 0) {
|
||||||
|
await this.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark as closed after flushing
|
||||||
|
this.isClosed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
97
base-action/test/resume-endpoint.test.ts
Normal file
97
base-action/test/resume-endpoint.test.ts
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
import { describe, it, expect } from "bun:test";
|
||||||
|
import { prepareRunConfig } from "../src/run-claude";
|
||||||
|
|
||||||
|
describe("resume endpoint functionality", () => {
|
||||||
|
it("should add --teleport flag when both session_id and resume_endpoint are provided", () => {
|
||||||
|
const streamConfig = JSON.stringify({
|
||||||
|
session_id: "12345",
|
||||||
|
resume_endpoint: "https://example.com/resume/12345",
|
||||||
|
});
|
||||||
|
const config = prepareRunConfig("/path/to/prompt", {
|
||||||
|
streamConfig,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(config.claudeArgs).toContain("--teleport");
|
||||||
|
expect(config.claudeArgs).toContain("12345");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not add --teleport flag when no streamConfig is provided", () => {
|
||||||
|
const config = prepareRunConfig("/path/to/prompt", {
|
||||||
|
allowedTools: "Edit",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(config.claudeArgs).not.toContain("--teleport");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not add --teleport flag when only session_id is provided without resume_endpoint", () => {
|
||||||
|
const streamConfig = JSON.stringify({
|
||||||
|
session_id: "12345",
|
||||||
|
// No resume_endpoint
|
||||||
|
});
|
||||||
|
const config = prepareRunConfig("/path/to/prompt", {
|
||||||
|
streamConfig,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(config.claudeArgs).not.toContain("--teleport");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not add --teleport flag when only resume_endpoint is provided without session_id", () => {
|
||||||
|
const streamConfig = JSON.stringify({
|
||||||
|
resume_endpoint: "https://example.com/resume/12345",
|
||||||
|
// No session_id
|
||||||
|
});
|
||||||
|
const config = prepareRunConfig("/path/to/prompt", {
|
||||||
|
streamConfig,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(config.claudeArgs).not.toContain("--teleport");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should maintain order of arguments with session_id", () => {
|
||||||
|
const streamConfig = JSON.stringify({
|
||||||
|
session_id: "12345",
|
||||||
|
resume_endpoint: "https://example.com/resume/12345",
|
||||||
|
});
|
||||||
|
const config = prepareRunConfig("/path/to/prompt", {
|
||||||
|
allowedTools: "Edit",
|
||||||
|
streamConfig,
|
||||||
|
maxTurns: "5",
|
||||||
|
});
|
||||||
|
|
||||||
|
const teleportIndex = config.claudeArgs.indexOf("--teleport");
|
||||||
|
const maxTurnsIndex = config.claudeArgs.indexOf("--max-turns");
|
||||||
|
|
||||||
|
expect(teleportIndex).toBeGreaterThan(-1);
|
||||||
|
expect(maxTurnsIndex).toBeGreaterThan(-1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle progress_endpoint and headers in streamConfig", () => {
|
||||||
|
const streamConfig = JSON.stringify({
|
||||||
|
progress_endpoint: "https://example.com/progress",
|
||||||
|
headers: { "X-Test": "value" },
|
||||||
|
});
|
||||||
|
const config = prepareRunConfig("/path/to/prompt", {
|
||||||
|
streamConfig,
|
||||||
|
});
|
||||||
|
|
||||||
|
// This test just verifies parsing doesn't fail - actual streaming logic
|
||||||
|
// is tested elsewhere as it requires environment setup
|
||||||
|
expect(config.claudeArgs).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle session_id with resume_endpoint and headers", () => {
|
||||||
|
const streamConfig = JSON.stringify({
|
||||||
|
session_id: "abc123",
|
||||||
|
resume_endpoint: "https://example.com/resume/abc123",
|
||||||
|
headers: { Authorization: "Bearer token" },
|
||||||
|
progress_endpoint: "https://example.com/progress",
|
||||||
|
});
|
||||||
|
const config = prepareRunConfig("/path/to/prompt", {
|
||||||
|
streamConfig,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(config.claudeArgs).toContain("--teleport");
|
||||||
|
expect(config.claudeArgs).toContain("abc123");
|
||||||
|
// Note: Environment variable setup (TELEPORT_RESUME_URL, TELEPORT_HEADERS) is tested in integration tests
|
||||||
|
});
|
||||||
|
});
|
||||||
364
base-action/test/stream-handler.test.ts
Normal file
364
base-action/test/stream-handler.test.ts
Normal file
@@ -0,0 +1,364 @@
|
|||||||
|
import { describe, it, expect, beforeEach, mock } from "bun:test";
|
||||||
|
import {
|
||||||
|
StreamHandler,
|
||||||
|
parseStreamHeaders,
|
||||||
|
type TokenGetter,
|
||||||
|
} from "../src/stream-handler";
|
||||||
|
|
||||||
|
describe("parseStreamHeaders", () => {
|
||||||
|
it("should return empty object for empty input", () => {
|
||||||
|
expect(parseStreamHeaders("")).toEqual({});
|
||||||
|
expect(parseStreamHeaders(undefined)).toEqual({});
|
||||||
|
expect(parseStreamHeaders(" ")).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse single header", () => {
|
||||||
|
const result = parseStreamHeaders('{"X-Correlation-Id": "12345"}');
|
||||||
|
expect(result).toEqual({ "X-Correlation-Id": "12345" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse multiple headers", () => {
|
||||||
|
const headers = JSON.stringify({
|
||||||
|
"X-Correlation-Id": "12345",
|
||||||
|
"X-Custom-Header": "custom-value",
|
||||||
|
Authorization: "Bearer token123",
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = parseStreamHeaders(headers);
|
||||||
|
expect(result).toEqual({
|
||||||
|
"X-Correlation-Id": "12345",
|
||||||
|
"X-Custom-Header": "custom-value",
|
||||||
|
Authorization: "Bearer token123",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle headers with spaces", () => {
|
||||||
|
const headers = JSON.stringify({
|
||||||
|
"X-Header-One": "value with spaces",
|
||||||
|
"X-Header-Two": "another value",
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = parseStreamHeaders(headers);
|
||||||
|
expect(result).toEqual({
|
||||||
|
"X-Header-One": "value with spaces",
|
||||||
|
"X-Header-Two": "another value",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should skip empty lines and comments", () => {
|
||||||
|
const headers = JSON.stringify({
|
||||||
|
"X-Header-One": "value1",
|
||||||
|
"X-Header-Two": "value2",
|
||||||
|
"X-Header-Three": "value3",
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = parseStreamHeaders(headers);
|
||||||
|
expect(result).toEqual({
|
||||||
|
"X-Header-One": "value1",
|
||||||
|
"X-Header-Two": "value2",
|
||||||
|
"X-Header-Three": "value3",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should skip lines without colons", () => {
|
||||||
|
const headers = JSON.stringify({
|
||||||
|
"X-Header-One": "value1",
|
||||||
|
"X-Header-Two": "value2",
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = parseStreamHeaders(headers);
|
||||||
|
expect(result).toEqual({
|
||||||
|
"X-Header-One": "value1",
|
||||||
|
"X-Header-Two": "value2",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle headers with colons in values", () => {
|
||||||
|
const headers = JSON.stringify({
|
||||||
|
"X-URL": "https://example.com:8080/path",
|
||||||
|
"X-Time": "10:30:45",
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = parseStreamHeaders(headers);
|
||||||
|
expect(result).toEqual({
|
||||||
|
"X-URL": "https://example.com:8080/path",
|
||||||
|
"X-Time": "10:30:45",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("StreamHandler", () => {
|
||||||
|
let handler: StreamHandler;
|
||||||
|
let mockFetch: ReturnType<typeof mock>;
|
||||||
|
let mockTokenGetter: TokenGetter;
|
||||||
|
const mockEndpoint = "https://test.example.com/stream";
|
||||||
|
const mockToken = "mock-oidc-token";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
// Mock fetch
|
||||||
|
mockFetch = mock(() => Promise.resolve({ ok: true }));
|
||||||
|
global.fetch = mockFetch as any;
|
||||||
|
|
||||||
|
// Mock token getter
|
||||||
|
mockTokenGetter = mock(() => Promise.resolve(mockToken));
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("basic functionality", () => {
|
||||||
|
it("should batch lines up to BATCH_SIZE", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
// Add 9 lines (less than batch size of 10)
|
||||||
|
for (let i = 1; i <= 9; i++) {
|
||||||
|
await handler.addOutput(`line ${i}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should not have sent anything yet
|
||||||
|
expect(mockFetch).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
// Add the 10th line to trigger flush
|
||||||
|
await handler.addOutput("line 10\n");
|
||||||
|
|
||||||
|
// Should have sent the batch
|
||||||
|
expect(mockFetch).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockFetch).toHaveBeenCalledWith(mockEndpoint, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${mockToken}`,
|
||||||
|
},
|
||||||
|
body: expect.stringContaining(
|
||||||
|
'"output":["line 1","line 2","line 3","line 4","line 5","line 6","line 7","line 8","line 9","line 10"]',
|
||||||
|
),
|
||||||
|
signal: expect.any(AbortSignal),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should flush on timeout", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
// Add a few lines
|
||||||
|
await handler.addOutput("line 1\n");
|
||||||
|
await handler.addOutput("line 2\n");
|
||||||
|
|
||||||
|
// Should not have sent anything yet
|
||||||
|
expect(mockFetch).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
// Wait for the timeout to trigger
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1100));
|
||||||
|
|
||||||
|
// Should have sent the batch
|
||||||
|
expect(mockFetch).toHaveBeenCalledTimes(1);
|
||||||
|
const call = mockFetch.mock.calls[0];
|
||||||
|
expect(call).toBeDefined();
|
||||||
|
const body = JSON.parse(call![1].body);
|
||||||
|
expect(body.output).toEqual(["line 1", "line 2"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should include custom headers", async () => {
|
||||||
|
const customHeaders = {
|
||||||
|
"X-Correlation-Id": "12345",
|
||||||
|
"X-Custom": "value",
|
||||||
|
};
|
||||||
|
handler = new StreamHandler(mockEndpoint, customHeaders, mockTokenGetter);
|
||||||
|
|
||||||
|
// Trigger a batch
|
||||||
|
for (let i = 1; i <= 10; i++) {
|
||||||
|
await handler.addOutput(`line ${i}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(mockFetch).toHaveBeenCalledWith(mockEndpoint, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${mockToken}`,
|
||||||
|
"X-Correlation-Id": "12345",
|
||||||
|
"X-Custom": "value",
|
||||||
|
},
|
||||||
|
body: expect.any(String),
|
||||||
|
signal: expect.any(AbortSignal),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should include timestamp in payload", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
const beforeTime = new Date().toISOString();
|
||||||
|
|
||||||
|
// Trigger a batch
|
||||||
|
for (let i = 1; i <= 10; i++) {
|
||||||
|
await handler.addOutput(`line ${i}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const afterTime = new Date().toISOString();
|
||||||
|
|
||||||
|
const call = mockFetch.mock.calls[0];
|
||||||
|
expect(call).toBeDefined();
|
||||||
|
const body = JSON.parse(call![1].body);
|
||||||
|
|
||||||
|
expect(body).toHaveProperty("timestamp");
|
||||||
|
expect(new Date(body.timestamp).toISOString()).toBe(body.timestamp);
|
||||||
|
expect(body.timestamp >= beforeTime).toBe(true);
|
||||||
|
expect(body.timestamp <= afterTime).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("token management", () => {
|
||||||
|
it("should fetch token on first request", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
// Trigger a flush
|
||||||
|
for (let i = 1; i <= 10; i++) {
|
||||||
|
await handler.addOutput(`line ${i}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(mockTokenGetter).toHaveBeenCalledWith("claude-code-github-action");
|
||||||
|
expect(mockTokenGetter).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should reuse token within 4 minutes", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
// First batch
|
||||||
|
for (let i = 1; i <= 10; i++) {
|
||||||
|
await handler.addOutput(`line ${i}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Second batch immediately (within 4 minutes)
|
||||||
|
for (let i = 11; i <= 20; i++) {
|
||||||
|
await handler.addOutput(`line ${i}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should have only fetched token once
|
||||||
|
expect(mockTokenGetter).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle token fetch errors", async () => {
|
||||||
|
const errorTokenGetter = mock(() =>
|
||||||
|
Promise.reject(new Error("Token fetch failed")),
|
||||||
|
);
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, errorTokenGetter);
|
||||||
|
|
||||||
|
// Try to send data
|
||||||
|
for (let i = 1; i <= 10; i++) {
|
||||||
|
await handler.addOutput(`line ${i}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should not have made fetch request
|
||||||
|
expect(mockFetch).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("error handling", () => {
|
||||||
|
it("should handle fetch errors gracefully", async () => {
|
||||||
|
mockFetch.mockImplementation(() =>
|
||||||
|
Promise.reject(new Error("Network error")),
|
||||||
|
);
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
// Send data - should not throw
|
||||||
|
for (let i = 1; i <= 10; i++) {
|
||||||
|
await handler.addOutput(`line ${i}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should have attempted to fetch
|
||||||
|
expect(mockFetch).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should continue processing after errors", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
// First batch - make it fail
|
||||||
|
let callCount = 0;
|
||||||
|
mockFetch.mockImplementation(() => {
|
||||||
|
callCount++;
|
||||||
|
if (callCount === 1) {
|
||||||
|
return Promise.reject(new Error("First batch failed"));
|
||||||
|
}
|
||||||
|
return Promise.resolve({ ok: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
for (let i = 1; i <= 10; i++) {
|
||||||
|
await handler.addOutput(`line ${i}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Second batch - should work
|
||||||
|
for (let i = 11; i <= 20; i++) {
|
||||||
|
await handler.addOutput(`line ${i}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should have attempted both batches
|
||||||
|
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("close functionality", () => {
|
||||||
|
it("should flush remaining data on close", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
// Add some data but not enough to trigger batch
|
||||||
|
await handler.addOutput("line 1\n");
|
||||||
|
await handler.addOutput("line 2\n");
|
||||||
|
|
||||||
|
expect(mockFetch).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
// Close should flush
|
||||||
|
await handler.close();
|
||||||
|
|
||||||
|
expect(mockFetch).toHaveBeenCalledTimes(1);
|
||||||
|
const call = mockFetch.mock.calls[0];
|
||||||
|
expect(call).toBeDefined();
|
||||||
|
const body = JSON.parse(call![1].body);
|
||||||
|
expect(body.output).toEqual(["line 1", "line 2"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not accept new data after close", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
await handler.close();
|
||||||
|
|
||||||
|
// Try to add data after close
|
||||||
|
await handler.addOutput("should not be sent\n");
|
||||||
|
|
||||||
|
// Should not have sent anything
|
||||||
|
expect(mockFetch).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("data handling", () => {
|
||||||
|
it("should filter out empty lines", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
await handler.addOutput("line 1\n\n\nline 2\n\n");
|
||||||
|
await handler.close();
|
||||||
|
|
||||||
|
const call = mockFetch.mock.calls[0];
|
||||||
|
expect(call).toBeDefined();
|
||||||
|
const body = JSON.parse(call![1].body);
|
||||||
|
expect(body.output).toEqual(["line 1", "line 2"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle data without newlines", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
await handler.addOutput("single line");
|
||||||
|
await handler.close();
|
||||||
|
|
||||||
|
const call = mockFetch.mock.calls[0];
|
||||||
|
expect(call).toBeDefined();
|
||||||
|
const body = JSON.parse(call![1].body);
|
||||||
|
expect(body.output).toEqual(["single line"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle multi-line input correctly", async () => {
|
||||||
|
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
|
||||||
|
|
||||||
|
await handler.addOutput("line 1\nline 2\nline 3");
|
||||||
|
await handler.close();
|
||||||
|
|
||||||
|
const call = mockFetch.mock.calls[0];
|
||||||
|
expect(call).toBeDefined();
|
||||||
|
const body = JSON.parse(call![1].body);
|
||||||
|
expect(body.output).toEqual(["line 1", "line 2", "line 3"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -6,8 +6,8 @@ echo "Installing git hooks..."
|
|||||||
# Make sure hooks directory exists
|
# Make sure hooks directory exists
|
||||||
mkdir -p .git/hooks
|
mkdir -p .git/hooks
|
||||||
|
|
||||||
# Install pre-commit hook
|
# Install pre-push hook
|
||||||
cp scripts/pre-commit .git/hooks/pre-commit
|
cp scripts/pre-push .git/hooks/pre-push
|
||||||
chmod +x .git/hooks/pre-commit
|
chmod +x .git/hooks/pre-push
|
||||||
|
|
||||||
echo "Git hooks installed successfully!"
|
echo "Git hooks installed successfully!"
|
||||||
@@ -60,6 +60,8 @@ export function buildAllowedToolsString(
|
|||||||
"Bash(git diff:*)",
|
"Bash(git diff:*)",
|
||||||
"Bash(git log:*)",
|
"Bash(git log:*)",
|
||||||
"Bash(git rm:*)",
|
"Bash(git rm:*)",
|
||||||
|
"Bash(git config user.name:*)",
|
||||||
|
"Bash(git config user.email:*)",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -811,18 +813,12 @@ f. If you are unable to complete certain steps, such as running a linter or test
|
|||||||
return promptContent;
|
return promptContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CreatePromptResult = {
|
|
||||||
promptFile: string;
|
|
||||||
allowedTools: string;
|
|
||||||
disallowedTools: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function createPrompt(
|
export async function createPrompt(
|
||||||
mode: Mode,
|
mode: Mode,
|
||||||
modeContext: ModeContext,
|
modeContext: ModeContext,
|
||||||
githubData: FetchDataResult,
|
githubData: FetchDataResult,
|
||||||
context: ParsedGitHubContext,
|
context: ParsedGitHubContext,
|
||||||
): Promise<CreatePromptResult> {
|
) {
|
||||||
try {
|
try {
|
||||||
// Prepare the context for prompt generation
|
// Prepare the context for prompt generation
|
||||||
let claudeCommentId: string = "";
|
let claudeCommentId: string = "";
|
||||||
@@ -894,17 +890,8 @@ export async function createPrompt(
|
|||||||
combinedAllowedTools,
|
combinedAllowedTools,
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Remove these environment variable exports once modes are updated to use return values
|
|
||||||
core.exportVariable("ALLOWED_TOOLS", allAllowedTools);
|
core.exportVariable("ALLOWED_TOOLS", allAllowedTools);
|
||||||
core.exportVariable("DISALLOWED_TOOLS", allDisallowedTools);
|
core.exportVariable("DISALLOWED_TOOLS", allDisallowedTools);
|
||||||
|
|
||||||
const promptFile = `${process.env.RUNNER_TEMP}/claude-prompts/claude-prompt.txt`;
|
|
||||||
|
|
||||||
return {
|
|
||||||
promptFile,
|
|
||||||
allowedTools: allAllowedTools,
|
|
||||||
disallowedTools: allDisallowedTools,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(`Create prompt failed with error: ${error}`);
|
core.setFailed(`Create prompt failed with error: ${error}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import { createOctokit } from "../github/api/client";
|
|||||||
import { parseGitHubContext, isEntityContext } from "../github/context";
|
import { parseGitHubContext, isEntityContext } from "../github/context";
|
||||||
import { getMode, isValidMode, DEFAULT_MODE } from "../modes/registry";
|
import { getMode, isValidMode, DEFAULT_MODE } from "../modes/registry";
|
||||||
import type { ModeName } from "../modes/types";
|
import type { ModeName } from "../modes/types";
|
||||||
import { prepare } from "../prepare";
|
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
try {
|
try {
|
||||||
@@ -60,7 +59,19 @@ async function run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Step 4: Get mode and check trigger conditions
|
// Step 4: Get mode and check trigger conditions
|
||||||
const mode = getMode(validatedMode, context);
|
let mode;
|
||||||
|
|
||||||
|
// TEMPORARY HACK: Always use remote-agent mode for repository_dispatch events
|
||||||
|
// This ensures backward compatibility while we transition
|
||||||
|
if (context.eventName === "repository_dispatch") {
|
||||||
|
console.log(
|
||||||
|
"🔧 TEMPORARY HACK: Forcing remote-agent mode for repository_dispatch event",
|
||||||
|
);
|
||||||
|
mode = getMode("remote-agent", context);
|
||||||
|
} else {
|
||||||
|
mode = getMode(context.inputs.mode, context);
|
||||||
|
}
|
||||||
|
|
||||||
const containsTrigger = mode.shouldTrigger(context);
|
const containsTrigger = mode.shouldTrigger(context);
|
||||||
|
|
||||||
// Set output for action.yml to check
|
// Set output for action.yml to check
|
||||||
@@ -72,10 +83,9 @@ async function run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Step 5: Use the new modular prepare function
|
// Step 5: Use the new modular prepare function
|
||||||
const result = await prepare({
|
const result = await mode.prepare({
|
||||||
context,
|
context,
|
||||||
octokit,
|
octokit,
|
||||||
mode,
|
|
||||||
githubToken,
|
githubToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
81
src/entrypoints/report-claude-complete.ts
Normal file
81
src/entrypoints/report-claude-complete.ts
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#!/usr/bin/env bun
|
||||||
|
|
||||||
|
import * as core from "@actions/core";
|
||||||
|
import { reportClaudeComplete } from "../modes/remote-agent/system-progress-handler";
|
||||||
|
import type { SystemProgressConfig } from "../modes/remote-agent/progress-types";
|
||||||
|
import type { StreamConfig } from "../types/stream-config";
|
||||||
|
|
||||||
|
async function run() {
|
||||||
|
try {
|
||||||
|
// Only run if we're in remote-agent mode
|
||||||
|
const mode = process.env.MODE;
|
||||||
|
if (mode !== "remote-agent") {
|
||||||
|
console.log(
|
||||||
|
"Not in remote-agent mode, skipping Claude completion reporting",
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we have stream config with system progress endpoint
|
||||||
|
const streamConfigStr = process.env.STREAM_CONFIG;
|
||||||
|
if (!streamConfigStr) {
|
||||||
|
console.log(
|
||||||
|
"No stream config available, skipping Claude completion reporting",
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let streamConfig: StreamConfig;
|
||||||
|
try {
|
||||||
|
streamConfig = JSON.parse(streamConfigStr);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Failed to parse stream config:", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!streamConfig.system_progress_endpoint) {
|
||||||
|
console.log(
|
||||||
|
"No system progress endpoint in stream config, skipping Claude completion reporting",
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the system progress config
|
||||||
|
const systemProgressConfig: SystemProgressConfig = {
|
||||||
|
endpoint: streamConfig.system_progress_endpoint,
|
||||||
|
headers: streamConfig.headers || {},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get the OIDC token from Authorization header
|
||||||
|
const authHeader = systemProgressConfig.headers?.["Authorization"];
|
||||||
|
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
||||||
|
console.error("No valid Authorization header in stream config");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const oidcToken = authHeader.substring(7); // Remove "Bearer " prefix
|
||||||
|
|
||||||
|
// Get Claude execution status
|
||||||
|
const claudeConclusion = process.env.CLAUDE_CONCLUSION || "failure";
|
||||||
|
const exitCode = claudeConclusion === "success" ? 0 : 1;
|
||||||
|
|
||||||
|
// Calculate duration if possible
|
||||||
|
const startTime = process.env.CLAUDE_START_TIME;
|
||||||
|
let durationMs = 0;
|
||||||
|
if (startTime) {
|
||||||
|
durationMs = Date.now() - parseInt(startTime, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report Claude completion
|
||||||
|
console.log(
|
||||||
|
`Reporting Claude completion: exitCode=${exitCode}, duration=${durationMs}ms`,
|
||||||
|
);
|
||||||
|
reportClaudeComplete(systemProgressConfig, oidcToken, exitCode, durationMs);
|
||||||
|
} catch (error) {
|
||||||
|
// Don't fail the action if reporting fails
|
||||||
|
core.warning(`Failed to report Claude completion: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (import.meta.main) {
|
||||||
|
run();
|
||||||
|
}
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
#!/usr/bin/env bun
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unified entrypoint that combines prepare and run-claude steps
|
|
||||||
*/
|
|
||||||
|
|
||||||
import * as core from "@actions/core";
|
|
||||||
import { setupGitHubToken } from "../github/token";
|
|
||||||
import { checkWritePermissions } from "../github/validation/permissions";
|
|
||||||
import { createOctokit } from "../github/api/client";
|
|
||||||
import { parseGitHubContext, isEntityContext } from "../github/context";
|
|
||||||
import { getMode, isValidMode, DEFAULT_MODE } from "../modes/registry";
|
|
||||||
import type { ModeName } from "../modes/types";
|
|
||||||
import { prepare } from "../prepare";
|
|
||||||
import { runClaudeCore } from "../../base-action/src/run-claude-core";
|
|
||||||
import { validateEnvironmentVariables } from "../../base-action/src/validate-env";
|
|
||||||
import { setupClaudeCodeSettings } from "../../base-action/src/setup-claude-code-settings";
|
|
||||||
|
|
||||||
async function run() {
|
|
||||||
try {
|
|
||||||
// Step 1: Get mode first to determine authentication method
|
|
||||||
const modeInput = process.env.MODE || DEFAULT_MODE;
|
|
||||||
|
|
||||||
// Validate mode input
|
|
||||||
if (!isValidMode(modeInput)) {
|
|
||||||
throw new Error(`Invalid mode: ${modeInput}`);
|
|
||||||
}
|
|
||||||
const validatedMode: ModeName = modeInput;
|
|
||||||
|
|
||||||
// Step 2: Setup GitHub token based on mode
|
|
||||||
let githubToken: string;
|
|
||||||
if (validatedMode === "experimental-review") {
|
|
||||||
// For experimental-review mode, use the default GitHub Action token
|
|
||||||
githubToken = process.env.DEFAULT_WORKFLOW_TOKEN || "";
|
|
||||||
if (!githubToken) {
|
|
||||||
throw new Error(
|
|
||||||
"DEFAULT_WORKFLOW_TOKEN not found for experimental-review mode",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
console.log("Using default GitHub Action token for review mode");
|
|
||||||
} else {
|
|
||||||
// For other modes, use the existing token exchange
|
|
||||||
githubToken = await setupGitHubToken();
|
|
||||||
}
|
|
||||||
const octokit = createOctokit(githubToken);
|
|
||||||
|
|
||||||
// Step 3: Parse GitHub context (once for all operations)
|
|
||||||
const context = parseGitHubContext();
|
|
||||||
|
|
||||||
// Step 4: Check write permissions (only for entity contexts)
|
|
||||||
if (isEntityContext(context)) {
|
|
||||||
const hasWritePermissions = await checkWritePermissions(
|
|
||||||
octokit.rest,
|
|
||||||
context,
|
|
||||||
);
|
|
||||||
if (!hasWritePermissions) {
|
|
||||||
throw new Error(
|
|
||||||
"Actor does not have write permissions to the repository",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 5: Get mode and check trigger conditions
|
|
||||||
const mode = getMode(validatedMode, context);
|
|
||||||
const containsTrigger = mode.shouldTrigger(context);
|
|
||||||
|
|
||||||
// Set output for action.yml to check (in case it's still needed)
|
|
||||||
core.setOutput("contains_trigger", containsTrigger.toString());
|
|
||||||
|
|
||||||
if (!containsTrigger) {
|
|
||||||
console.log("No trigger found, skipping remaining steps");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 6: Use the modular prepare function
|
|
||||||
const prepareResult = await prepare({
|
|
||||||
context,
|
|
||||||
octokit,
|
|
||||||
mode,
|
|
||||||
githubToken,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Set critical outputs immediately after prepare completes
|
|
||||||
// This ensures they're available for cleanup even if Claude fails
|
|
||||||
core.setOutput("GITHUB_TOKEN", githubToken);
|
|
||||||
core.setOutput("mcp_config", prepareResult.mcpConfig);
|
|
||||||
if (prepareResult.branchInfo.claudeBranch) {
|
|
||||||
core.setOutput("branch_name", prepareResult.branchInfo.claudeBranch);
|
|
||||||
core.setOutput("CLAUDE_BRANCH", prepareResult.branchInfo.claudeBranch);
|
|
||||||
}
|
|
||||||
core.setOutput("BASE_BRANCH", prepareResult.branchInfo.baseBranch);
|
|
||||||
if (prepareResult.commentId) {
|
|
||||||
core.setOutput("claude_comment_id", prepareResult.commentId.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 7: The mode.prepare() call already created the prompt and set up tools
|
|
||||||
// We need to get the allowed/disallowed tools from environment variables
|
|
||||||
// TODO: Update Mode interface to return tools from prepare() instead of relying on env vars
|
|
||||||
const allowedTools = process.env.ALLOWED_TOOLS || "";
|
|
||||||
const disallowedTools = process.env.DISALLOWED_TOOLS || "";
|
|
||||||
const promptFile = `${process.env.RUNNER_TEMP}/claude-prompts/claude-prompt.txt`;
|
|
||||||
|
|
||||||
// Step 8: Validate environment and setup Claude settings
|
|
||||||
validateEnvironmentVariables();
|
|
||||||
await setupClaudeCodeSettings(process.env.SETTINGS);
|
|
||||||
|
|
||||||
// Step 9: Run Claude Code
|
|
||||||
console.log("Running Claude Code...");
|
|
||||||
|
|
||||||
// Build environment object to pass to Claude
|
|
||||||
const claudeEnvObject: Record<string, string> = {
|
|
||||||
GITHUB_TOKEN: githubToken,
|
|
||||||
NODE_VERSION: process.env.NODE_VERSION || "18.x",
|
|
||||||
DETAILED_PERMISSION_MESSAGES: "1",
|
|
||||||
CLAUDE_CODE_ACTION: "1",
|
|
||||||
};
|
|
||||||
|
|
||||||
await runClaudeCore({
|
|
||||||
promptFile,
|
|
||||||
settings: process.env.SETTINGS,
|
|
||||||
allowedTools,
|
|
||||||
disallowedTools,
|
|
||||||
maxTurns: process.env.MAX_TURNS,
|
|
||||||
mcpConfig: prepareResult.mcpConfig,
|
|
||||||
systemPrompt: "",
|
|
||||||
appendSystemPrompt: "",
|
|
||||||
claudeEnv: process.env.CLAUDE_ENV,
|
|
||||||
fallbackModel: process.env.FALLBACK_MODEL,
|
|
||||||
model: process.env.ANTHROPIC_MODEL || process.env.MODEL,
|
|
||||||
timeoutMinutes: process.env.TIMEOUT_MINUTES || "30",
|
|
||||||
env: claudeEnvObject,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
||||||
core.setFailed(`Action failed with error: ${errorMessage}`);
|
|
||||||
// Also output the clean error message for the action to capture
|
|
||||||
core.setOutput("prepare_error", errorMessage);
|
|
||||||
core.setOutput("conclusion", "failure");
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (import.meta.main) {
|
|
||||||
run();
|
|
||||||
}
|
|
||||||
@@ -6,6 +6,7 @@ import type {
|
|||||||
PullRequestEvent,
|
PullRequestEvent,
|
||||||
PullRequestReviewEvent,
|
PullRequestReviewEvent,
|
||||||
PullRequestReviewCommentEvent,
|
PullRequestReviewCommentEvent,
|
||||||
|
RepositoryDispatchEvent,
|
||||||
} from "@octokit/webhooks-types";
|
} from "@octokit/webhooks-types";
|
||||||
// Custom types for GitHub Actions events that aren't webhooks
|
// Custom types for GitHub Actions events that aren't webhooks
|
||||||
export type WorkflowDispatchEvent = {
|
export type WorkflowDispatchEvent = {
|
||||||
@@ -46,7 +47,11 @@ const ENTITY_EVENT_NAMES = [
|
|||||||
"pull_request_review_comment",
|
"pull_request_review_comment",
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
const AUTOMATION_EVENT_NAMES = ["workflow_dispatch", "schedule"] as const;
|
const AUTOMATION_EVENT_NAMES = [
|
||||||
|
"workflow_dispatch",
|
||||||
|
"schedule",
|
||||||
|
"repository_dispatch",
|
||||||
|
] as const;
|
||||||
|
|
||||||
// Derive types from constants for better maintainability
|
// Derive types from constants for better maintainability
|
||||||
type EntityEventName = (typeof ENTITY_EVENT_NAMES)[number];
|
type EntityEventName = (typeof ENTITY_EVENT_NAMES)[number];
|
||||||
@@ -62,6 +67,17 @@ type BaseContext = {
|
|||||||
full_name: string;
|
full_name: string;
|
||||||
};
|
};
|
||||||
actor: string;
|
actor: string;
|
||||||
|
payload:
|
||||||
|
| IssuesEvent
|
||||||
|
| IssueCommentEvent
|
||||||
|
| PullRequestEvent
|
||||||
|
| PullRequestReviewEvent
|
||||||
|
| PullRequestReviewCommentEvent
|
||||||
|
| RepositoryDispatchEvent
|
||||||
|
| WorkflowDispatchEvent
|
||||||
|
| ScheduleEvent;
|
||||||
|
entityNumber?: number;
|
||||||
|
isPR?: boolean;
|
||||||
inputs: {
|
inputs: {
|
||||||
mode: ModeName;
|
mode: ModeName;
|
||||||
triggerPhrase: string;
|
triggerPhrase: string;
|
||||||
@@ -78,6 +94,14 @@ type BaseContext = {
|
|||||||
additionalPermissions: Map<string, string>;
|
additionalPermissions: Map<string, string>;
|
||||||
useCommitSigning: boolean;
|
useCommitSigning: boolean;
|
||||||
};
|
};
|
||||||
|
progressTracking?: {
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
resumeEndpoint?: string;
|
||||||
|
sessionId?: string;
|
||||||
|
progressEndpoint: string;
|
||||||
|
systemProgressEndpoint?: string;
|
||||||
|
oauthTokenEndpoint?: string;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// Context for entity-based events (issues, PRs, comments)
|
// Context for entity-based events (issues, PRs, comments)
|
||||||
@@ -96,7 +120,7 @@ export type ParsedGitHubContext = BaseContext & {
|
|||||||
// Context for automation events (workflow_dispatch, schedule)
|
// Context for automation events (workflow_dispatch, schedule)
|
||||||
export type AutomationContext = BaseContext & {
|
export type AutomationContext = BaseContext & {
|
||||||
eventName: AutomationEventName;
|
eventName: AutomationEventName;
|
||||||
payload: WorkflowDispatchEvent | ScheduleEvent;
|
payload: WorkflowDispatchEvent | ScheduleEvent | RepositoryDispatchEvent;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Union type for all contexts
|
// Union type for all contexts
|
||||||
@@ -190,6 +214,66 @@ export function parseGitHubContext(): GitHubContext {
|
|||||||
isPR: true,
|
isPR: true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
case "repository_dispatch": {
|
||||||
|
const payload = context.payload as RepositoryDispatchEvent;
|
||||||
|
// Extract task description from client_payload
|
||||||
|
const clientPayload = payload.client_payload as {
|
||||||
|
prompt?: string;
|
||||||
|
stream_endpoint?: string;
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
resume_endpoint?: string;
|
||||||
|
session_id?: string;
|
||||||
|
endpoints?: {
|
||||||
|
resume?: string;
|
||||||
|
progress?: string;
|
||||||
|
system_progress?: string;
|
||||||
|
oauth_endpoint?: string;
|
||||||
|
};
|
||||||
|
overrideInputs?: {
|
||||||
|
model?: string;
|
||||||
|
base_branch?: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Override directPrompt with the prompt
|
||||||
|
if (clientPayload.prompt) {
|
||||||
|
commonFields.inputs.directPrompt = clientPayload.prompt;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply input overrides
|
||||||
|
if (clientPayload.overrideInputs) {
|
||||||
|
if (clientPayload.overrideInputs.base_branch) {
|
||||||
|
commonFields.inputs.baseBranch =
|
||||||
|
clientPayload.overrideInputs.base_branch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up progress tracking - prioritize endpoints object if available, fallback to individual fields
|
||||||
|
let progressTracking: ParsedGitHubContext["progressTracking"] = undefined;
|
||||||
|
|
||||||
|
if (clientPayload.endpoints?.progress || clientPayload.stream_endpoint) {
|
||||||
|
progressTracking = {
|
||||||
|
progressEndpoint:
|
||||||
|
clientPayload.endpoints?.progress ||
|
||||||
|
clientPayload.stream_endpoint ||
|
||||||
|
"",
|
||||||
|
headers: clientPayload.headers,
|
||||||
|
resumeEndpoint:
|
||||||
|
// clientPayload.endpoints?.resume || clientPayload.resume_endpoint,
|
||||||
|
clientPayload.resume_endpoint,
|
||||||
|
sessionId: clientPayload.session_id,
|
||||||
|
systemProgressEndpoint: clientPayload.endpoints?.system_progress,
|
||||||
|
oauthTokenEndpoint: clientPayload.endpoints?.oauth_endpoint,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...commonFields,
|
||||||
|
eventName: "repository_dispatch",
|
||||||
|
payload: payload,
|
||||||
|
progressTracking,
|
||||||
|
};
|
||||||
|
}
|
||||||
case "workflow_dispatch": {
|
case "workflow_dispatch": {
|
||||||
return {
|
return {
|
||||||
...commonFields,
|
...commonFields,
|
||||||
@@ -287,3 +371,9 @@ export function isAutomationContext(
|
|||||||
context.eventName as AutomationEventName,
|
context.eventName as AutomationEventName,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isRepositoryDispatchEvent(
|
||||||
|
context: GitHubContext,
|
||||||
|
): context is GitHubContext & { payload: RepositoryDispatchEvent } {
|
||||||
|
return context.eventName === "repository_dispatch";
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import { $ } from "bun";
|
import { $ } from "bun";
|
||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
import type { ParsedGitHubContext } from "../context";
|
import type { GitHubContext } from "../context";
|
||||||
import type { GitHubPullRequest } from "../types";
|
import type { GitHubPullRequest } from "../types";
|
||||||
import type { Octokits } from "../api/client";
|
import type { Octokits } from "../api/client";
|
||||||
import type { FetchDataResult } from "../data/fetcher";
|
import type { FetchDataResult } from "../data/fetcher";
|
||||||
@@ -21,15 +21,15 @@ export type BranchInfo = {
|
|||||||
|
|
||||||
export async function setupBranch(
|
export async function setupBranch(
|
||||||
octokits: Octokits,
|
octokits: Octokits,
|
||||||
githubData: FetchDataResult,
|
githubData: FetchDataResult | null,
|
||||||
context: ParsedGitHubContext,
|
context: GitHubContext,
|
||||||
): Promise<BranchInfo> {
|
): Promise<BranchInfo> {
|
||||||
const { owner, repo } = context.repository;
|
const { owner, repo } = context.repository;
|
||||||
const entityNumber = context.entityNumber;
|
const entityNumber = context.entityNumber;
|
||||||
const { baseBranch, branchPrefix } = context.inputs;
|
const { baseBranch, branchPrefix } = context.inputs;
|
||||||
const isPR = context.isPR;
|
const isPR = context.isPR;
|
||||||
|
|
||||||
if (isPR) {
|
if (isPR && githubData) {
|
||||||
const prData = githubData.contextData as GitHubPullRequest;
|
const prData = githubData.contextData as GitHubPullRequest;
|
||||||
const prState = prData.state;
|
const prState = prData.state;
|
||||||
|
|
||||||
@@ -84,19 +84,27 @@ export async function setupBranch(
|
|||||||
sourceBranch = repoResponse.data.default_branch;
|
sourceBranch = repoResponse.data.default_branch;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate branch name for either an issue or closed/merged PR
|
// Generate branch name for either an issue, closed/merged PR, or repository_dispatch event
|
||||||
const entityType = isPR ? "pr" : "issue";
|
let branchName: string;
|
||||||
|
|
||||||
// Create Kubernetes-compatible timestamp: lowercase, hyphens only, shorter format
|
if (context.eventName === "repository_dispatch") {
|
||||||
|
// For repository_dispatch events, use run ID for uniqueness since there's no entity number
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const timestamp = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, "0")}${String(now.getDate()).padStart(2, "0")}-${String(now.getHours()).padStart(2, "0")}${String(now.getMinutes()).padStart(2, "0")}`;
|
const timestamp = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, "0")}${String(now.getDate()).padStart(2, "0")}-${String(now.getHours()).padStart(2, "0")}${String(now.getMinutes()).padStart(2, "0")}`;
|
||||||
|
branchName = `${branchPrefix}dispatch-${context.runId}-${timestamp}`;
|
||||||
|
} else {
|
||||||
|
// For issues and PRs, use the existing logic
|
||||||
|
const entityType = isPR ? "pr" : "issue";
|
||||||
|
const now = new Date();
|
||||||
|
const timestamp = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, "0")}${String(now.getDate()).padStart(2, "0")}-${String(now.getHours()).padStart(2, "0")}${String(now.getMinutes()).padStart(2, "0")}`;
|
||||||
|
branchName = `${branchPrefix}${entityType}-${entityNumber}-${timestamp}`;
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure branch name is Kubernetes-compatible:
|
// Ensure branch name is Kubernetes-compatible:
|
||||||
// - Lowercase only
|
// - Lowercase only
|
||||||
// - Alphanumeric with hyphens
|
// - Alphanumeric with hyphens
|
||||||
// - No underscores
|
// - No underscores
|
||||||
// - Max 50 chars (to allow for prefixes)
|
// - Max 50 chars (to allow for prefixes)
|
||||||
const branchName = `${branchPrefix}${entityType}-${entityNumber}-${timestamp}`;
|
|
||||||
const newBranch = branchName.toLowerCase().substring(0, 50);
|
const newBranch = branchName.toLowerCase().substring(0, 50);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -132,8 +140,18 @@ export async function setupBranch(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For non-signing case, create and checkout the branch locally only
|
// For non-signing case, create and checkout the branch locally only
|
||||||
|
const entityType =
|
||||||
|
context.eventName === "repository_dispatch"
|
||||||
|
? "dispatch"
|
||||||
|
: isPR
|
||||||
|
? "pr"
|
||||||
|
: "issue";
|
||||||
|
const entityId =
|
||||||
|
context.eventName === "repository_dispatch"
|
||||||
|
? context.runId
|
||||||
|
: entityNumber!.toString();
|
||||||
console.log(
|
console.log(
|
||||||
`Creating local branch ${newBranch} for ${entityType} #${entityNumber} from source branch: ${sourceBranch}...`,
|
`Creating local branch ${newBranch} for ${entityType} ${entityId} from source branch: ${sourceBranch}...`,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Fetch and checkout the source branch first to ensure we branch from the correct base
|
// Fetch and checkout the source branch first to ensure we branch from the correct base
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { $ } from "bun";
|
import { $ } from "bun";
|
||||||
import type { ParsedGitHubContext } from "../context";
|
import type { GitHubContext } from "../context";
|
||||||
import { GITHUB_SERVER_URL } from "../api/config";
|
import { GITHUB_SERVER_URL } from "../api/config";
|
||||||
|
|
||||||
type GitUser = {
|
type GitUser = {
|
||||||
@@ -16,7 +16,7 @@ type GitUser = {
|
|||||||
|
|
||||||
export async function configureGitAuth(
|
export async function configureGitAuth(
|
||||||
githubToken: string,
|
githubToken: string,
|
||||||
context: ParsedGitHubContext,
|
context: GitHubContext,
|
||||||
user: GitUser | null,
|
user: GitUser | null,
|
||||||
) {
|
) {
|
||||||
console.log("Configuring git authentication for non-signing mode");
|
console.log("Configuring git authentication for non-signing mode");
|
||||||
|
|||||||
@@ -3,17 +3,11 @@ import path from "path";
|
|||||||
import type { Octokits } from "../api/client";
|
import type { Octokits } from "../api/client";
|
||||||
import { GITHUB_SERVER_URL } from "../api/config";
|
import { GITHUB_SERVER_URL } from "../api/config";
|
||||||
|
|
||||||
const escapedUrl = GITHUB_SERVER_URL.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
||||||
const IMAGE_REGEX = new RegExp(
|
const IMAGE_REGEX = new RegExp(
|
||||||
`!\\[[^\\]]*\\]\\((${escapedUrl}\\/user-attachments\\/assets\\/[^)]+)\\)`,
|
`!\\[[^\\]]*\\]\\((${GITHUB_SERVER_URL.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\/user-attachments\\/assets\\/[^)]+)\\)`,
|
||||||
"g",
|
"g",
|
||||||
);
|
);
|
||||||
|
|
||||||
const HTML_IMG_REGEX = new RegExp(
|
|
||||||
`<img[^>]+src=["']([^"']*${escapedUrl}\\/user-attachments\\/assets\\/[^"']+)["'][^>]*>`,
|
|
||||||
"gi",
|
|
||||||
);
|
|
||||||
|
|
||||||
type IssueComment = {
|
type IssueComment = {
|
||||||
type: "issue_comment";
|
type: "issue_comment";
|
||||||
id: string;
|
id: string;
|
||||||
@@ -69,16 +63,8 @@ export async function downloadCommentImages(
|
|||||||
}> = [];
|
}> = [];
|
||||||
|
|
||||||
for (const comment of comments) {
|
for (const comment of comments) {
|
||||||
// Extract URLs from Markdown format
|
const imageMatches = [...comment.body.matchAll(IMAGE_REGEX)];
|
||||||
const markdownMatches = [...comment.body.matchAll(IMAGE_REGEX)];
|
const urls = imageMatches.map((match) => match[1] as string);
|
||||||
const markdownUrls = markdownMatches.map((match) => match[1] as string);
|
|
||||||
|
|
||||||
// Extract URLs from HTML format
|
|
||||||
const htmlMatches = [...comment.body.matchAll(HTML_IMG_REGEX)];
|
|
||||||
const htmlUrls = htmlMatches.map((match) => match[1] as string);
|
|
||||||
|
|
||||||
// Combine and deduplicate URLs
|
|
||||||
const urls = [...new Set([...markdownUrls, ...htmlUrls])];
|
|
||||||
|
|
||||||
if (urls.length > 0) {
|
if (urls.length > 0) {
|
||||||
commentsWithImages.push({ comment, urls });
|
commentsWithImages.push({ comment, urls });
|
||||||
|
|||||||
@@ -1,178 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
||||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { createOctokit } from "../github/api/client";
|
|
||||||
|
|
||||||
// Get repository and PR information from environment variables
|
|
||||||
const REPO_OWNER = process.env.REPO_OWNER;
|
|
||||||
const REPO_NAME = process.env.REPO_NAME;
|
|
||||||
const PR_NUMBER = process.env.PR_NUMBER;
|
|
||||||
|
|
||||||
if (!REPO_OWNER || !REPO_NAME || !PR_NUMBER) {
|
|
||||||
console.error(
|
|
||||||
"Error: REPO_OWNER, REPO_NAME, and PR_NUMBER environment variables are required",
|
|
||||||
);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// GitHub Inline Comment MCP Server - Provides inline PR comment functionality
|
|
||||||
// Provides an inline comment tool without exposing full PR review capabilities, so that
|
|
||||||
// Claude can't accidentally approve a PR
|
|
||||||
const server = new McpServer({
|
|
||||||
name: "GitHub Inline Comment Server",
|
|
||||||
version: "0.0.1",
|
|
||||||
});
|
|
||||||
|
|
||||||
server.tool(
|
|
||||||
"create_inline_comment",
|
|
||||||
"Create an inline comment on a specific line or lines in a PR file",
|
|
||||||
{
|
|
||||||
path: z
|
|
||||||
.string()
|
|
||||||
.describe("The file path to comment on (e.g., 'src/index.js')"),
|
|
||||||
body: z
|
|
||||||
.string()
|
|
||||||
.describe(
|
|
||||||
"The comment text (supports markdown and GitHub code suggestion blocks). " +
|
|
||||||
"For code suggestions, use: ```suggestion\\nreplacement code\\n```. " +
|
|
||||||
"IMPORTANT: The suggestion block will REPLACE the ENTIRE line range (single line or startLine to line). " +
|
|
||||||
"Ensure the replacement is syntactically complete and valid - it must work as a drop-in replacement for the selected lines.",
|
|
||||||
),
|
|
||||||
line: z
|
|
||||||
.number()
|
|
||||||
.optional()
|
|
||||||
.describe(
|
|
||||||
"Line number for single-line comments (required if startLine is not provided)",
|
|
||||||
),
|
|
||||||
startLine: z
|
|
||||||
.number()
|
|
||||||
.optional()
|
|
||||||
.describe(
|
|
||||||
"Start line for multi-line comments (use with line parameter for the end line)",
|
|
||||||
),
|
|
||||||
side: z
|
|
||||||
.enum(["LEFT", "RIGHT"])
|
|
||||||
.optional()
|
|
||||||
.default("RIGHT")
|
|
||||||
.describe(
|
|
||||||
"Side of the diff to comment on: LEFT (old code) or RIGHT (new code)",
|
|
||||||
),
|
|
||||||
commit_id: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.describe(
|
|
||||||
"Specific commit SHA to comment on (defaults to latest commit)",
|
|
||||||
),
|
|
||||||
},
|
|
||||||
async ({ path, body, line, startLine, side, commit_id }) => {
|
|
||||||
try {
|
|
||||||
const githubToken = process.env.GITHUB_TOKEN;
|
|
||||||
|
|
||||||
if (!githubToken) {
|
|
||||||
throw new Error("GITHUB_TOKEN environment variable is required");
|
|
||||||
}
|
|
||||||
|
|
||||||
const owner = REPO_OWNER;
|
|
||||||
const repo = REPO_NAME;
|
|
||||||
const pull_number = parseInt(PR_NUMBER, 10);
|
|
||||||
|
|
||||||
const octokit = createOctokit(githubToken).rest;
|
|
||||||
|
|
||||||
// Validate that either line or both startLine and line are provided
|
|
||||||
if (!line && !startLine) {
|
|
||||||
throw new Error(
|
|
||||||
"Either 'line' for single-line comments or both 'startLine' and 'line' for multi-line comments must be provided",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If only line is provided, it's a single-line comment
|
|
||||||
// If both startLine and line are provided, it's a multi-line comment
|
|
||||||
const isSingleLine = !startLine;
|
|
||||||
|
|
||||||
const pr = await octokit.pulls.get({
|
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
pull_number,
|
|
||||||
});
|
|
||||||
|
|
||||||
const params: Parameters<
|
|
||||||
typeof octokit.rest.pulls.createReviewComment
|
|
||||||
>[0] = {
|
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
pull_number,
|
|
||||||
body,
|
|
||||||
path,
|
|
||||||
side: side || "RIGHT",
|
|
||||||
commit_id: commit_id || pr.data.head.sha,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isSingleLine) {
|
|
||||||
// Single-line comment
|
|
||||||
params.line = line;
|
|
||||||
} else {
|
|
||||||
// Multi-line comment
|
|
||||||
params.start_line = startLine;
|
|
||||||
params.start_side = side || "RIGHT";
|
|
||||||
params.line = line;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await octokit.rest.pulls.createReviewComment(params);
|
|
||||||
|
|
||||||
return {
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
text: JSON.stringify(
|
|
||||||
{
|
|
||||||
success: true,
|
|
||||||
comment_id: result.data.id,
|
|
||||||
html_url: result.data.html_url,
|
|
||||||
path: result.data.path,
|
|
||||||
line: result.data.line || result.data.original_line,
|
|
||||||
message: `Inline comment created successfully on ${path}${isSingleLine ? ` at line ${line}` : ` from line ${startLine} to ${line}`}`,
|
|
||||||
},
|
|
||||||
null,
|
|
||||||
2,
|
|
||||||
),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
const errorMessage =
|
|
||||||
error instanceof Error ? error.message : String(error);
|
|
||||||
|
|
||||||
// Provide more helpful error messages for common issues
|
|
||||||
let helpMessage = "";
|
|
||||||
if (errorMessage.includes("Validation Failed")) {
|
|
||||||
helpMessage =
|
|
||||||
"\n\nThis usually means the line number doesn't exist in the diff or the file path is incorrect. Make sure you're commenting on lines that are part of the PR's changes.";
|
|
||||||
} else if (errorMessage.includes("Not Found")) {
|
|
||||||
helpMessage =
|
|
||||||
"\n\nThis usually means the PR number, repository, or file path is incorrect.";
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
text: `Error creating inline comment: ${errorMessage}${helpMessage}`,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
error: errorMessage,
|
|
||||||
isError: true,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
async function runServer() {
|
|
||||||
const transport = new StdioServerTransport();
|
|
||||||
await server.connect(transport);
|
|
||||||
process.on("exit", () => {
|
|
||||||
server.close();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
runServer().catch(console.error);
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
import { GITHUB_API_URL, GITHUB_SERVER_URL } from "../github/api/config";
|
import { GITHUB_API_URL, GITHUB_SERVER_URL } from "../github/api/config";
|
||||||
import type { ParsedGitHubContext } from "../github/context";
|
import type { GitHubContext } from "../github/context";
|
||||||
import { Octokit } from "@octokit/rest";
|
import { Octokit } from "@octokit/rest";
|
||||||
|
|
||||||
type PrepareConfigParams = {
|
type PrepareConfigParams = {
|
||||||
@@ -12,7 +12,7 @@ type PrepareConfigParams = {
|
|||||||
additionalMcpConfig?: string;
|
additionalMcpConfig?: string;
|
||||||
claudeCommentId?: string;
|
claudeCommentId?: string;
|
||||||
allowedTools: string[];
|
allowedTools: string[];
|
||||||
context: ParsedGitHubContext;
|
context: GitHubContext;
|
||||||
};
|
};
|
||||||
|
|
||||||
async function checkActionsReadPermission(
|
async function checkActionsReadPermission(
|
||||||
@@ -73,6 +73,7 @@ export async function prepareMcpConfig(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Always include comment server for updating Claude comments
|
// Always include comment server for updating Claude comments
|
||||||
|
if (context.inputs.mode === "tag") {
|
||||||
baseMcpConfig.mcpServers.github_comment = {
|
baseMcpConfig.mcpServers.github_comment = {
|
||||||
command: "bun",
|
command: "bun",
|
||||||
args: [
|
args: [
|
||||||
@@ -88,6 +89,7 @@ export async function prepareMcpConfig(
|
|||||||
GITHUB_API_URL: GITHUB_API_URL,
|
GITHUB_API_URL: GITHUB_API_URL,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Include file ops server when commit signing is enabled
|
// Include file ops server when commit signing is enabled
|
||||||
if (context.inputs.useCommitSigning) {
|
if (context.inputs.useCommitSigning) {
|
||||||
@@ -111,24 +113,6 @@ export async function prepareMcpConfig(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Include inline comment server for experimental review mode
|
|
||||||
if (context.inputs.mode === "experimental-review" && context.isPR) {
|
|
||||||
baseMcpConfig.mcpServers.github_inline_comment = {
|
|
||||||
command: "bun",
|
|
||||||
args: [
|
|
||||||
"run",
|
|
||||||
`${process.env.GITHUB_ACTION_PATH}/src/mcp/github-inline-comment-server.ts`,
|
|
||||||
],
|
|
||||||
env: {
|
|
||||||
GITHUB_TOKEN: githubToken,
|
|
||||||
REPO_OWNER: owner,
|
|
||||||
REPO_NAME: repo,
|
|
||||||
PR_NUMBER: context.entityNumber?.toString() || "",
|
|
||||||
GITHUB_API_URL: GITHUB_API_URL,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only add CI server if we have actions:read permission and we're in a PR context
|
// Only add CI server if we have actions:read permission and we're in a PR context
|
||||||
const hasActionsReadPermission =
|
const hasActionsReadPermission =
|
||||||
context.inputs.additionalPermissions.get("actions") === "read";
|
context.inputs.additionalPermissions.get("actions") === "read";
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
import { mkdir, writeFile } from "fs/promises";
|
|
||||||
import type { Mode, ModeOptions, ModeResult } from "../types";
|
import type { Mode, ModeOptions, ModeResult } from "../types";
|
||||||
import { isAutomationContext } from "../../github/context";
|
import { isAutomationContext } from "../../github/context";
|
||||||
import type { PreparedContext } from "../../create-prompt/types";
|
import type { PreparedContext } from "../../create-prompt/types";
|
||||||
@@ -43,23 +42,7 @@ export const agentMode: Mode = {
|
|||||||
async prepare({ context }: ModeOptions): Promise<ModeResult> {
|
async prepare({ context }: ModeOptions): Promise<ModeResult> {
|
||||||
// Agent mode handles automation events (workflow_dispatch, schedule) only
|
// Agent mode handles automation events (workflow_dispatch, schedule) only
|
||||||
|
|
||||||
// TODO: handle by createPrompt (similar to tag and review modes)
|
// Agent mode doesn't need to create prompt files here - handled by createPrompt
|
||||||
// Create prompt directory
|
|
||||||
await mkdir(`${process.env.RUNNER_TEMP}/claude-prompts`, {
|
|
||||||
recursive: true,
|
|
||||||
});
|
|
||||||
// Write the prompt file - the base action requires a prompt_file parameter,
|
|
||||||
// so we must create this file even though agent mode typically uses
|
|
||||||
// override_prompt or direct_prompt. If neither is provided, we write
|
|
||||||
// a minimal prompt with just the repository information.
|
|
||||||
const promptContent =
|
|
||||||
context.inputs.overridePrompt ||
|
|
||||||
context.inputs.directPrompt ||
|
|
||||||
`Repository: ${context.repository.owner}/${context.repository.repo}`;
|
|
||||||
await writeFile(
|
|
||||||
`${process.env.RUNNER_TEMP}/claude-prompts/claude-prompt.txt`,
|
|
||||||
promptContent,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Export tool environment variables for agent mode
|
// Export tool environment variables for agent mode
|
||||||
const baseTools = [
|
const baseTools = [
|
||||||
|
|||||||
@@ -16,9 +16,15 @@ import { agentMode } from "./agent";
|
|||||||
import { reviewMode } from "./review";
|
import { reviewMode } from "./review";
|
||||||
import type { GitHubContext } from "../github/context";
|
import type { GitHubContext } from "../github/context";
|
||||||
import { isAutomationContext } from "../github/context";
|
import { isAutomationContext } from "../github/context";
|
||||||
|
import { remoteAgentMode } from "./remote-agent";
|
||||||
|
|
||||||
export const DEFAULT_MODE = "tag" as const;
|
export const DEFAULT_MODE = "tag" as const;
|
||||||
export const VALID_MODES = ["tag", "agent", "experimental-review"] as const;
|
export const VALID_MODES = [
|
||||||
|
"tag",
|
||||||
|
"agent",
|
||||||
|
"remote-agent",
|
||||||
|
"experimental-review",
|
||||||
|
] as const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All available modes.
|
* All available modes.
|
||||||
@@ -28,6 +34,7 @@ const modes = {
|
|||||||
tag: tagMode,
|
tag: tagMode,
|
||||||
agent: agentMode,
|
agent: agentMode,
|
||||||
"experimental-review": reviewMode,
|
"experimental-review": reviewMode,
|
||||||
|
"remote-agent": remoteAgentMode,
|
||||||
} as const satisfies Record<ModeName, Mode>;
|
} as const satisfies Record<ModeName, Mode>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,7 +56,13 @@ export function getMode(name: ModeName, context: GitHubContext): Mode {
|
|||||||
// Validate mode can handle the event type
|
// Validate mode can handle the event type
|
||||||
if (name === "tag" && isAutomationContext(context)) {
|
if (name === "tag" && isAutomationContext(context)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Tag mode cannot handle ${context.eventName} events. Use 'agent' mode for automation events.`,
|
`Tag mode cannot handle ${context.eventName} events. Use 'agent' mode for automation events or 'remote-agent' mode for repository_dispatch events.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name === "remote-agent" && context.eventName !== "repository_dispatch") {
|
||||||
|
throw new Error(
|
||||||
|
`Remote agent mode can only handle repository_dispatch events. Use 'tag' mode for @claude mentions or 'agent' mode for other automation events.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
468
src/modes/remote-agent/index.ts
Normal file
468
src/modes/remote-agent/index.ts
Normal file
@@ -0,0 +1,468 @@
|
|||||||
|
import * as core from "@actions/core";
|
||||||
|
import { mkdir, writeFile } from "fs/promises";
|
||||||
|
import type { Mode, ModeOptions, ModeResult } from "../types";
|
||||||
|
import { isRepositoryDispatchEvent } from "../../github/context";
|
||||||
|
import type { GitHubContext } from "../../github/context";
|
||||||
|
import { setupBranch } from "../../github/operations/branch";
|
||||||
|
import { configureGitAuth } from "../../github/operations/git-config";
|
||||||
|
import { prepareMcpConfig } from "../../mcp/install-mcp-server";
|
||||||
|
import { GITHUB_SERVER_URL } from "../../github/api/config";
|
||||||
|
import {
|
||||||
|
buildAllowedToolsString,
|
||||||
|
buildDisallowedToolsString,
|
||||||
|
type PreparedContext,
|
||||||
|
} from "../../create-prompt";
|
||||||
|
import {
|
||||||
|
reportWorkflowInitialized,
|
||||||
|
reportClaudeStarting,
|
||||||
|
reportWorkflowFailed,
|
||||||
|
} from "./system-progress-handler";
|
||||||
|
import type { SystemProgressConfig } from "./progress-types";
|
||||||
|
import { fetchUserDisplayName } from "../../github/data/fetcher";
|
||||||
|
import { createOctokit } from "../../github/api/client";
|
||||||
|
import type { StreamConfig } from "../../types/stream-config";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches a Claude Code OAuth token from the specified endpoint using OIDC authentication
|
||||||
|
*/
|
||||||
|
async function fetchClaudeCodeOAuthToken(
|
||||||
|
oauthTokenEndpoint: string,
|
||||||
|
oidcToken?: string,
|
||||||
|
sessionId?: string,
|
||||||
|
): Promise<string> {
|
||||||
|
console.log(`Fetching Claude Code OAuth token from: ${oauthTokenEndpoint}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!oidcToken) {
|
||||||
|
throw new Error("OIDC token is required for OAuth authentication");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make request to OAuth token endpoint
|
||||||
|
const response = await fetch(oauthTokenEndpoint, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${oidcToken}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
...(sessionId && { session_id: sessionId }),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(
|
||||||
|
`OAuth token request failed: ${response.status} ${response.statusText}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = (await response.json()) as {
|
||||||
|
oauth_token?: string;
|
||||||
|
message?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!data.oauth_token) {
|
||||||
|
const message = data.message || "Unknown error";
|
||||||
|
throw new Error(`OAuth token request failed: ${message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Successfully fetched Claude Code OAuth token");
|
||||||
|
return data.oauth_token;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to fetch Claude Code OAuth token:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote Agent mode implementation.
|
||||||
|
*
|
||||||
|
* This mode is specifically designed for repository_dispatch events triggered by external APIs.
|
||||||
|
* It bypasses the standard trigger checking, comment tracking, and GitHub data fetching used by tag mode,
|
||||||
|
* making it ideal for automated tasks triggered via API calls with custom payloads.
|
||||||
|
*/
|
||||||
|
export const remoteAgentMode: Mode = {
|
||||||
|
name: "remote-agent",
|
||||||
|
description: "Remote automation mode for repository_dispatch events",
|
||||||
|
|
||||||
|
shouldTrigger(context) {
|
||||||
|
// Only trigger for repository_dispatch events
|
||||||
|
return isRepositoryDispatchEvent(context);
|
||||||
|
},
|
||||||
|
|
||||||
|
prepareContext(context, data) {
|
||||||
|
// Remote agent mode uses minimal context
|
||||||
|
return {
|
||||||
|
mode: "remote-agent",
|
||||||
|
githubContext: context,
|
||||||
|
baseBranch: data?.baseBranch,
|
||||||
|
claudeBranch: data?.claudeBranch,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
getAllowedTools() {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
|
||||||
|
getDisallowedTools() {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
|
||||||
|
shouldCreateTrackingComment() {
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
|
async prepare({
|
||||||
|
context,
|
||||||
|
octokit,
|
||||||
|
githubToken,
|
||||||
|
}: ModeOptions): Promise<ModeResult> {
|
||||||
|
// Remote agent mode handles repository_dispatch events only
|
||||||
|
|
||||||
|
if (!isRepositoryDispatchEvent(context)) {
|
||||||
|
throw new Error(
|
||||||
|
"Remote agent mode can only handle repository_dispatch events",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract task details from client_payload
|
||||||
|
const payload = context.payload;
|
||||||
|
const clientPayload = payload.client_payload as {
|
||||||
|
prompt?: string;
|
||||||
|
stream_endpoint?: string;
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
resume_endpoint?: string;
|
||||||
|
session_id?: string;
|
||||||
|
endpoints?: {
|
||||||
|
stream?: string;
|
||||||
|
progress?: string;
|
||||||
|
systemProgress?: string;
|
||||||
|
oauthToken?: string;
|
||||||
|
};
|
||||||
|
overrideInputs?: {
|
||||||
|
model?: string;
|
||||||
|
base_branch?: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get OIDC token for streaming and potential OAuth token fetching
|
||||||
|
let oidcToken: string;
|
||||||
|
try {
|
||||||
|
oidcToken = await core.getIDToken("claude-code-github-action");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to get OIDC token:", error);
|
||||||
|
throw new Error(
|
||||||
|
`OIDC token required for remote-agent mode. Please add 'id-token: write' to your workflow permissions. Error: ${error}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up system progress config if endpoint is provided
|
||||||
|
let systemProgressConfig: SystemProgressConfig | null = null;
|
||||||
|
if (context.progressTracking?.systemProgressEndpoint) {
|
||||||
|
systemProgressConfig = {
|
||||||
|
endpoint: context.progressTracking.systemProgressEndpoint,
|
||||||
|
headers: context.progressTracking.headers,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle authentication - fetch OAuth token if needed
|
||||||
|
const anthropicApiKey = process.env.ANTHROPIC_API_KEY;
|
||||||
|
const claudeCodeOAuthToken = process.env.CLAUDE_CODE_OAUTH_TOKEN;
|
||||||
|
|
||||||
|
if (!anthropicApiKey && !claudeCodeOAuthToken) {
|
||||||
|
const oauthTokenEndpoint = context.progressTracking?.oauthTokenEndpoint;
|
||||||
|
|
||||||
|
if (oauthTokenEndpoint) {
|
||||||
|
console.log(
|
||||||
|
"No API key or OAuth token found, fetching OAuth token from endpoint",
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
const fetchedToken = await fetchClaudeCodeOAuthToken(
|
||||||
|
oauthTokenEndpoint,
|
||||||
|
oidcToken,
|
||||||
|
context.progressTracking?.sessionId,
|
||||||
|
);
|
||||||
|
core.setOutput("claude_code_oauth_token", fetchedToken);
|
||||||
|
console.log(
|
||||||
|
"Successfully fetched and set OAuth token for Claude Code",
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to fetch OAuth token:", error);
|
||||||
|
throw new Error(
|
||||||
|
`Authentication failed: No API key or OAuth token available, and OAuth token fetching failed: ${error}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error(
|
||||||
|
"No authentication available: Missing ANTHROPIC_API_KEY, CLAUDE_CODE_OAUTH_TOKEN, and no OAuth token endpoint provided",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log("Using existing authentication (API key or OAuth token)");
|
||||||
|
}
|
||||||
|
|
||||||
|
const taskDescription =
|
||||||
|
clientPayload.prompt ||
|
||||||
|
context.inputs.directPrompt ||
|
||||||
|
"No task description provided";
|
||||||
|
|
||||||
|
// Setup branch for work isolation
|
||||||
|
let branchInfo;
|
||||||
|
try {
|
||||||
|
branchInfo = await setupBranch(octokit, null, context);
|
||||||
|
} catch (error) {
|
||||||
|
// Report failure if we have system progress config
|
||||||
|
if (systemProgressConfig) {
|
||||||
|
reportWorkflowFailed(
|
||||||
|
systemProgressConfig,
|
||||||
|
oidcToken,
|
||||||
|
"initialization",
|
||||||
|
error as Error,
|
||||||
|
"branch_setup_failed",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure git authentication if not using commit signing
|
||||||
|
if (!context.inputs.useCommitSigning) {
|
||||||
|
try {
|
||||||
|
// Force Claude bot as git user
|
||||||
|
await configureGitAuth(githubToken, context, {
|
||||||
|
login: "claude[bot]",
|
||||||
|
id: 209825114,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to configure git authentication:", error);
|
||||||
|
// Report failure if we have system progress config
|
||||||
|
if (systemProgressConfig) {
|
||||||
|
reportWorkflowFailed(
|
||||||
|
systemProgressConfig,
|
||||||
|
oidcToken,
|
||||||
|
"initialization",
|
||||||
|
error as Error,
|
||||||
|
"git_config_failed",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report workflow initialized
|
||||||
|
if (systemProgressConfig) {
|
||||||
|
reportWorkflowInitialized(
|
||||||
|
systemProgressConfig,
|
||||||
|
oidcToken,
|
||||||
|
branchInfo.claudeBranch || branchInfo.currentBranch,
|
||||||
|
branchInfo.baseBranch,
|
||||||
|
context.progressTracking?.sessionId,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create prompt directory
|
||||||
|
await mkdir(`${process.env.RUNNER_TEMP}/claude-prompts`, {
|
||||||
|
recursive: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch trigger user display name from context.actor
|
||||||
|
let triggerDisplayName: string | null | undefined;
|
||||||
|
if (context.actor) {
|
||||||
|
try {
|
||||||
|
const octokits = createOctokit(githubToken);
|
||||||
|
triggerDisplayName = await fetchUserDisplayName(
|
||||||
|
octokits,
|
||||||
|
context.actor,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(
|
||||||
|
`Failed to fetch user display name for ${context.actor}:`,
|
||||||
|
error,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate dispatch-specific prompt (just the task description)
|
||||||
|
const promptContent = generateDispatchPrompt(taskDescription);
|
||||||
|
|
||||||
|
console.log("Writing prompt file...");
|
||||||
|
console.log("Contents: ", promptContent);
|
||||||
|
// Write the prompt file
|
||||||
|
await writeFile(
|
||||||
|
`${process.env.RUNNER_TEMP}/claude-prompts/claude-prompt.txt`,
|
||||||
|
promptContent,
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
`Prompt file written successfully to ${process.env.RUNNER_TEMP}/claude-prompts/claude-prompt.txt`,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set stream configuration for repository_dispatch events
|
||||||
|
if (context.progressTracking) {
|
||||||
|
const streamConfig: StreamConfig = {};
|
||||||
|
|
||||||
|
if (context.progressTracking.resumeEndpoint) {
|
||||||
|
streamConfig.resume_endpoint = context.progressTracking.resumeEndpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context.progressTracking.sessionId) {
|
||||||
|
streamConfig.session_id = context.progressTracking.sessionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context.progressTracking.progressEndpoint) {
|
||||||
|
streamConfig.progress_endpoint =
|
||||||
|
context.progressTracking.progressEndpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context.progressTracking.systemProgressEndpoint) {
|
||||||
|
streamConfig.system_progress_endpoint =
|
||||||
|
context.progressTracking.systemProgressEndpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge provided headers with OIDC token
|
||||||
|
const headers: Record<string, string> = {
|
||||||
|
...(context.progressTracking.headers || {}),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Use existing OIDC token for streaming
|
||||||
|
headers["Authorization"] = `Bearer ${oidcToken}`;
|
||||||
|
|
||||||
|
if (Object.keys(headers).length > 0) {
|
||||||
|
streamConfig.headers = headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Setting stream config:", streamConfig);
|
||||||
|
core.setOutput("stream_config", JSON.stringify(streamConfig));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export tool environment variables for remote agent mode
|
||||||
|
// Check if we have actions:read permission for CI tools
|
||||||
|
const hasActionsReadPermission =
|
||||||
|
context.inputs.additionalPermissions.get("actions") === "read";
|
||||||
|
|
||||||
|
const allowedToolsString = buildAllowedToolsString(
|
||||||
|
context.inputs.allowedTools,
|
||||||
|
hasActionsReadPermission,
|
||||||
|
context.inputs.useCommitSigning,
|
||||||
|
);
|
||||||
|
const disallowedToolsString = buildDisallowedToolsString(
|
||||||
|
context.inputs.disallowedTools,
|
||||||
|
);
|
||||||
|
|
||||||
|
core.exportVariable("ALLOWED_TOOLS", allowedToolsString);
|
||||||
|
core.exportVariable("DISALLOWED_TOOLS", disallowedToolsString);
|
||||||
|
|
||||||
|
// Handle model override from repository_dispatch payload
|
||||||
|
if (clientPayload.overrideInputs?.model) {
|
||||||
|
core.setOutput("anthropic_model", clientPayload.overrideInputs.model);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get minimal MCP configuration for remote agent mode
|
||||||
|
const additionalMcpConfig = process.env.MCP_CONFIG || "";
|
||||||
|
const mcpConfig = await prepareMcpConfig({
|
||||||
|
githubToken,
|
||||||
|
owner: context.repository.owner,
|
||||||
|
repo: context.repository.repo,
|
||||||
|
branch: branchInfo.claudeBranch || branchInfo.currentBranch,
|
||||||
|
baseBranch: branchInfo.baseBranch,
|
||||||
|
additionalMcpConfig,
|
||||||
|
claudeCommentId: "", // No comment ID for remote agent mode
|
||||||
|
allowedTools: context.inputs.allowedTools,
|
||||||
|
context,
|
||||||
|
});
|
||||||
|
|
||||||
|
core.setOutput("mcp_config", mcpConfig);
|
||||||
|
|
||||||
|
// Report Claude is starting
|
||||||
|
if (systemProgressConfig) {
|
||||||
|
reportClaudeStarting(systemProgressConfig, oidcToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Track Claude start time for duration calculation
|
||||||
|
core.setOutput("claude_start_time", Date.now().toString());
|
||||||
|
|
||||||
|
// Export system prompt for remote agent mode
|
||||||
|
const systemPrompt = generateDispatchSystemPrompt(
|
||||||
|
context,
|
||||||
|
branchInfo.baseBranch,
|
||||||
|
branchInfo.claudeBranch,
|
||||||
|
context.actor,
|
||||||
|
triggerDisplayName,
|
||||||
|
);
|
||||||
|
core.exportVariable("APPEND_SYSTEM_PROMPT", systemPrompt);
|
||||||
|
|
||||||
|
return {
|
||||||
|
commentId: undefined, // No comment tracking for remote agent mode
|
||||||
|
branchInfo,
|
||||||
|
mcpConfig,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
generatePrompt(context: PreparedContext): string {
|
||||||
|
// TODO: update this to generate a more meaningful prompt
|
||||||
|
return `Repository: ${context.repository}`;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a task-focused prompt for repository_dispatch events
|
||||||
|
*/
|
||||||
|
function generateDispatchPrompt(taskDescription: string): string {
|
||||||
|
return taskDescription;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates the system prompt portion for repository_dispatch events
|
||||||
|
*/
|
||||||
|
function generateDispatchSystemPrompt(
|
||||||
|
context: GitHubContext,
|
||||||
|
baseBranch: string,
|
||||||
|
claudeBranch: string | undefined,
|
||||||
|
triggerUsername?: string,
|
||||||
|
triggerDisplayName?: string | null,
|
||||||
|
): string {
|
||||||
|
const { repository } = context;
|
||||||
|
|
||||||
|
const coAuthorLine =
|
||||||
|
triggerUsername && (triggerDisplayName || triggerUsername !== "Unknown")
|
||||||
|
? `Co-authored-by: ${triggerDisplayName ?? triggerUsername} <${triggerUsername}@users.noreply.github.com>`
|
||||||
|
: "";
|
||||||
|
|
||||||
|
let commitInstructions = "";
|
||||||
|
if (context.inputs.useCommitSigning) {
|
||||||
|
commitInstructions = `- Use mcp__github_file_ops__commit_files and mcp__github_file_ops__delete_files to commit and push changes`;
|
||||||
|
if (coAuthorLine) {
|
||||||
|
commitInstructions += `
|
||||||
|
- When pushing changes, include a Co-authored-by trailer in the commit message
|
||||||
|
- Use: "${coAuthorLine}"`;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
commitInstructions = `- Use git commands via the Bash tool to commit and push your changes:
|
||||||
|
- Stage files: Bash(git add <files>)
|
||||||
|
- Commit with a descriptive message: Bash(git commit -m "<message>")`;
|
||||||
|
if (coAuthorLine) {
|
||||||
|
commitInstructions += `
|
||||||
|
- When committing, include a Co-authored-by trailer:
|
||||||
|
Bash(git commit -m "<message>\\n\\n${coAuthorLine}")`;
|
||||||
|
}
|
||||||
|
commitInstructions += `
|
||||||
|
- Be sure to follow your commit message guidelines
|
||||||
|
- Push to the remote: Bash(git push origin HEAD)`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `You are Claude, an AI assistant designed to help with GitHub issues and pull requests. Think carefully as you analyze the context and respond appropriately. Here's the context for your current task:
|
||||||
|
|
||||||
|
Your task is to complete the request described in the task description.
|
||||||
|
|
||||||
|
Instructions:
|
||||||
|
1. For questions: Research the codebase and provide a detailed answer
|
||||||
|
2. For implementations: Make the requested changes, commit, and push
|
||||||
|
|
||||||
|
Key points:
|
||||||
|
- You're already on a new branch - NEVER create another branch (this is very important). ${claudeBranch} is the ONLY branch you should work on.
|
||||||
|
${commitInstructions}
|
||||||
|
${
|
||||||
|
claudeBranch
|
||||||
|
? `- After completing your work, provide a URL to create a PR in this format:
|
||||||
|
|
||||||
|
${GITHUB_SERVER_URL}/${repository.owner}/${repository.repo}/compare/${baseBranch}...${claudeBranch}?quick_pull=1`
|
||||||
|
: ""
|
||||||
|
}`;
|
||||||
|
}
|
||||||
78
src/modes/remote-agent/progress-types.ts
Normal file
78
src/modes/remote-agent/progress-types.ts
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
/**
|
||||||
|
* System progress tracking types for remote agent mode
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base event structure
|
||||||
|
*/
|
||||||
|
type BaseProgressEvent = {
|
||||||
|
timestamp: string; // ISO 8601
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Workflow initializing event
|
||||||
|
*/
|
||||||
|
export type WorkflowInitializingEvent = BaseProgressEvent & {
|
||||||
|
event_type: "workflow_initializing";
|
||||||
|
data: {
|
||||||
|
branch: string;
|
||||||
|
base_branch: string;
|
||||||
|
session_id?: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Claude starting event
|
||||||
|
*/
|
||||||
|
export type ClaudeStartingEvent = BaseProgressEvent & {
|
||||||
|
event_type: "claude_starting";
|
||||||
|
data: Record<string, never>; // No data needed
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Claude complete event
|
||||||
|
*/
|
||||||
|
export type ClaudeCompleteEvent = BaseProgressEvent & {
|
||||||
|
event_type: "claude_complete";
|
||||||
|
data: {
|
||||||
|
exit_code: number;
|
||||||
|
duration_ms: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Workflow failed event
|
||||||
|
*/
|
||||||
|
export type WorkflowFailedEvent = BaseProgressEvent & {
|
||||||
|
event_type: "workflow_failed";
|
||||||
|
data: {
|
||||||
|
error: {
|
||||||
|
phase: "initialization" | "claude_execution";
|
||||||
|
message: string;
|
||||||
|
code: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discriminated union of all progress events
|
||||||
|
*/
|
||||||
|
export type ProgressEvent =
|
||||||
|
| WorkflowInitializingEvent
|
||||||
|
| ClaudeStartingEvent
|
||||||
|
| ClaudeCompleteEvent
|
||||||
|
| WorkflowFailedEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Payload sent to the system progress endpoint
|
||||||
|
*/
|
||||||
|
export type SystemProgressPayload = ProgressEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration for system progress reporting
|
||||||
|
*/
|
||||||
|
export type SystemProgressConfig = {
|
||||||
|
endpoint: string;
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
timeout_ms?: number; // Default: 5000
|
||||||
|
};
|
||||||
149
src/modes/remote-agent/system-progress-handler.ts
Normal file
149
src/modes/remote-agent/system-progress-handler.ts
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
import * as core from "@actions/core";
|
||||||
|
import type {
|
||||||
|
ProgressEvent,
|
||||||
|
SystemProgressPayload,
|
||||||
|
SystemProgressConfig,
|
||||||
|
WorkflowInitializingEvent,
|
||||||
|
ClaudeStartingEvent,
|
||||||
|
ClaudeCompleteEvent,
|
||||||
|
WorkflowFailedEvent,
|
||||||
|
} from "./progress-types";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a progress event to the system progress endpoint (fire-and-forget)
|
||||||
|
*/
|
||||||
|
function sendProgressEvent(
|
||||||
|
event: ProgressEvent,
|
||||||
|
config: SystemProgressConfig,
|
||||||
|
oidcToken: string,
|
||||||
|
): void {
|
||||||
|
const payload: SystemProgressPayload = event;
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`Sending system progress event: ${event.event_type}`,
|
||||||
|
JSON.stringify(payload, null, 2),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Fire and forget - don't await
|
||||||
|
Promise.resolve().then(async () => {
|
||||||
|
try {
|
||||||
|
// Create an AbortController for timeout
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(
|
||||||
|
() => controller.abort(),
|
||||||
|
config.timeout_ms || 5000,
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(config.endpoint, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${oidcToken}`,
|
||||||
|
...config.headers,
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
signal: controller.signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error(
|
||||||
|
`System progress endpoint returned ${response.status}: ${response.statusText}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Log but don't throw - we don't want progress reporting to interrupt the workflow
|
||||||
|
core.warning(`Failed to send system progress event: ${error}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Report workflow initialization complete
|
||||||
|
*/
|
||||||
|
export function reportWorkflowInitialized(
|
||||||
|
config: SystemProgressConfig,
|
||||||
|
oidcToken: string,
|
||||||
|
branch: string,
|
||||||
|
baseBranch: string,
|
||||||
|
sessionId?: string,
|
||||||
|
): void {
|
||||||
|
const event: WorkflowInitializingEvent = {
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
event_type: "workflow_initializing",
|
||||||
|
data: {
|
||||||
|
branch,
|
||||||
|
base_branch: baseBranch,
|
||||||
|
...(sessionId && { session_id: sessionId }),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
sendProgressEvent(event, config, oidcToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Report Claude is starting
|
||||||
|
*/
|
||||||
|
export function reportClaudeStarting(
|
||||||
|
config: SystemProgressConfig,
|
||||||
|
oidcToken: string,
|
||||||
|
): void {
|
||||||
|
const event: ClaudeStartingEvent = {
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
event_type: "claude_starting",
|
||||||
|
data: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
sendProgressEvent(event, config, oidcToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Report Claude completed
|
||||||
|
*/
|
||||||
|
export function reportClaudeComplete(
|
||||||
|
config: SystemProgressConfig,
|
||||||
|
oidcToken: string,
|
||||||
|
exitCode: number,
|
||||||
|
durationMs: number,
|
||||||
|
): void {
|
||||||
|
const event: ClaudeCompleteEvent = {
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
event_type: "claude_complete",
|
||||||
|
data: {
|
||||||
|
exit_code: exitCode,
|
||||||
|
duration_ms: durationMs,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
sendProgressEvent(event, config, oidcToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Report workflow failed
|
||||||
|
*/
|
||||||
|
export function reportWorkflowFailed(
|
||||||
|
config: SystemProgressConfig,
|
||||||
|
oidcToken: string,
|
||||||
|
phase: "initialization" | "claude_execution",
|
||||||
|
error: Error | string,
|
||||||
|
code: string,
|
||||||
|
): void {
|
||||||
|
const errorMessage = error instanceof Error ? error.message : error;
|
||||||
|
|
||||||
|
const event: WorkflowFailedEvent = {
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
event_type: "workflow_failed",
|
||||||
|
data: {
|
||||||
|
error: {
|
||||||
|
phase,
|
||||||
|
message: errorMessage,
|
||||||
|
code,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
sendProgressEvent(event, config, oidcToken);
|
||||||
|
}
|
||||||
@@ -60,8 +60,20 @@ export const reviewMode: Mode = {
|
|||||||
|
|
||||||
getAllowedTools() {
|
getAllowedTools() {
|
||||||
return [
|
return [
|
||||||
"Bash(gh issue comment:*)",
|
// Context tools - to know who the current user is
|
||||||
"mcp__github_inline_comment__create_inline_comment",
|
"mcp__github__get_me",
|
||||||
|
// Core review tools
|
||||||
|
"mcp__github__create_pending_pull_request_review",
|
||||||
|
"mcp__github__add_comment_to_pending_review",
|
||||||
|
"mcp__github__submit_pending_pull_request_review",
|
||||||
|
"mcp__github__delete_pending_pull_request_review",
|
||||||
|
"mcp__github__create_and_submit_pull_request_review",
|
||||||
|
// Comment tools
|
||||||
|
"mcp__github__add_issue_comment",
|
||||||
|
// PR information tools
|
||||||
|
"mcp__github__get_pull_request",
|
||||||
|
"mcp__github__get_pull_request_reviews",
|
||||||
|
"mcp__github__get_pull_request_status",
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -151,13 +163,17 @@ REVIEW MODE WORKFLOW:
|
|||||||
|
|
||||||
1. First, understand the PR context:
|
1. First, understand the PR context:
|
||||||
- You are reviewing PR #${eventData.isPR && eventData.prNumber ? eventData.prNumber : "[PR number]"} in ${context.repository}
|
- You are reviewing PR #${eventData.isPR && eventData.prNumber ? eventData.prNumber : "[PR number]"} in ${context.repository}
|
||||||
|
- Use mcp__github__get_pull_request to get PR metadata
|
||||||
- Use the Read, Grep, and Glob tools to examine the modified files directly from disk
|
- Use the Read, Grep, and Glob tools to examine the modified files directly from disk
|
||||||
- This provides the full context and latest state of the code
|
- This provides the full context and latest state of the code
|
||||||
- Look at the changed_files section above to see which files were modified
|
- Look at the changed_files section above to see which files were modified
|
||||||
|
|
||||||
2. Add comments:
|
2. Create a pending review:
|
||||||
- use Bash(gh issue comment:*) to add top-level comments
|
- Use mcp__github__create_pending_pull_request_review to start your review
|
||||||
- Use mcp__github_inline_comment__create_inline_comment to add inline comments (prefer this where possible)
|
- This allows you to batch comments before submitting
|
||||||
|
|
||||||
|
3. Add inline comments:
|
||||||
|
- Use mcp__github__add_comment_to_pending_review for each issue or suggestion
|
||||||
- Parameters:
|
- Parameters:
|
||||||
* path: The file path (e.g., "src/index.js")
|
* path: The file path (e.g., "src/index.js")
|
||||||
* line: Line number for single-line comments
|
* line: Line number for single-line comments
|
||||||
@@ -166,6 +182,49 @@ REVIEW MODE WORKFLOW:
|
|||||||
* subjectType: "line" for line-level comments
|
* subjectType: "line" for line-level comments
|
||||||
* body: Your comment text
|
* body: Your comment text
|
||||||
|
|
||||||
|
- When to use multi-line comments:
|
||||||
|
* When replacing multiple consecutive lines
|
||||||
|
* When the fix requires changes across several lines
|
||||||
|
* Example: To replace lines 19-20, use startLine: 19, line: 20
|
||||||
|
|
||||||
|
- For code suggestions, use this EXACT format in the body:
|
||||||
|
\`\`\`suggestion
|
||||||
|
corrected code here
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
CRITICAL: GitHub suggestion blocks must ONLY contain the replacement for the specific line(s) being commented on:
|
||||||
|
- For single-line comments: Replace ONLY that line
|
||||||
|
- For multi-line comments: Replace ONLY the lines in the range
|
||||||
|
- Do NOT include surrounding context or function signatures
|
||||||
|
- Do NOT suggest changes that span beyond the commented lines
|
||||||
|
|
||||||
|
Example for line 19 \`var name = user.name;\`:
|
||||||
|
WRONG:
|
||||||
|
\\\`\\\`\\\`suggestion
|
||||||
|
function processUser(user) {
|
||||||
|
if (!user) throw new Error('Invalid user');
|
||||||
|
const name = user.name;
|
||||||
|
\\\`\\\`\\\`
|
||||||
|
|
||||||
|
CORRECT:
|
||||||
|
\\\`\\\`\\\`suggestion
|
||||||
|
const name = user.name;
|
||||||
|
\\\`\\\`\\\`
|
||||||
|
|
||||||
|
For validation suggestions, comment on the function declaration line or create separate comments for each concern.
|
||||||
|
|
||||||
|
4. Submit your review:
|
||||||
|
- Use mcp__github__submit_pending_pull_request_review
|
||||||
|
- Parameters:
|
||||||
|
* event: "COMMENT" (general feedback), "REQUEST_CHANGES" (issues found), or "APPROVE" (if appropriate)
|
||||||
|
* body: Write a comprehensive review summary that includes:
|
||||||
|
- Overview of what was reviewed (files, scope, focus areas)
|
||||||
|
- Summary of all issues found (with counts by severity if applicable)
|
||||||
|
- Key recommendations and action items
|
||||||
|
- Highlights of good practices observed
|
||||||
|
- Overall assessment and recommendation
|
||||||
|
- The body should be detailed and informative since it's the main review content
|
||||||
|
- Structure the body with clear sections using markdown headers
|
||||||
|
|
||||||
REVIEW GUIDELINES:
|
REVIEW GUIDELINES:
|
||||||
|
|
||||||
@@ -242,7 +301,6 @@ This ensures users get value from the review even before checking individual inl
|
|||||||
claudeBranch: branchInfo.claudeBranch,
|
claudeBranch: branchInfo.claudeBranch,
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Capture and return the allowed/disallowed tools from createPrompt
|
|
||||||
await createPrompt(reviewMode, modeContext, githubData, context);
|
await createPrompt(reviewMode, modeContext, githubData, context);
|
||||||
|
|
||||||
// Export tool environment variables for review mode
|
// Export tool environment variables for review mode
|
||||||
|
|||||||
@@ -98,7 +98,6 @@ export const tagMode: Mode = {
|
|||||||
claudeBranch: branchInfo.claudeBranch,
|
claudeBranch: branchInfo.claudeBranch,
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Capture and return the allowed/disallowed tools from createPrompt
|
|
||||||
await createPrompt(tagMode, modeContext, githubData, context);
|
await createPrompt(tagMode, modeContext, githubData, context);
|
||||||
|
|
||||||
// Get MCP configuration
|
// Get MCP configuration
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import type { PreparedContext } from "../create-prompt/types";
|
|||||||
import type { FetchDataResult } from "../github/data/fetcher";
|
import type { FetchDataResult } from "../github/data/fetcher";
|
||||||
import type { Octokits } from "../github/api/client";
|
import type { Octokits } from "../github/api/client";
|
||||||
|
|
||||||
export type ModeName = "tag" | "agent" | "experimental-review";
|
export type ModeName = "tag" | "agent" | "remote-agent" | "experimental-review";
|
||||||
|
|
||||||
export type ModeContext = {
|
export type ModeContext = {
|
||||||
mode: ModeName;
|
mode: ModeName;
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ export type PrepareResult = {
|
|||||||
currentBranch: string;
|
currentBranch: string;
|
||||||
};
|
};
|
||||||
mcpConfig: string;
|
mcpConfig: string;
|
||||||
// TODO: Add allowedTools and disallowedTools here once modes are updated
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PrepareOptions = {
|
export type PrepareOptions = {
|
||||||
|
|||||||
19
src/types/stream-config.ts
Normal file
19
src/types/stream-config.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
/**
|
||||||
|
* Configuration for streaming and progress tracking
|
||||||
|
*/
|
||||||
|
export type StreamConfig = {
|
||||||
|
/** Endpoint for streaming Claude execution progress */
|
||||||
|
progress_endpoint?: string;
|
||||||
|
|
||||||
|
/** Endpoint for system-level progress reporting (workflow lifecycle events) */
|
||||||
|
system_progress_endpoint?: string;
|
||||||
|
|
||||||
|
/** Resume endpoint for teleport functionality */
|
||||||
|
resume_endpoint?: string;
|
||||||
|
|
||||||
|
/** Session ID for tracking */
|
||||||
|
session_id?: string;
|
||||||
|
|
||||||
|
/** Headers to include with streaming requests (includes Authorization) */
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
};
|
||||||
@@ -1041,6 +1041,8 @@ describe("buildAllowedToolsString", () => {
|
|||||||
expect(result).toContain("Bash(git diff:*)");
|
expect(result).toContain("Bash(git diff:*)");
|
||||||
expect(result).toContain("Bash(git log:*)");
|
expect(result).toContain("Bash(git log:*)");
|
||||||
expect(result).toContain("Bash(git rm:*)");
|
expect(result).toContain("Bash(git rm:*)");
|
||||||
|
expect(result).toContain("Bash(git config user.name:*)");
|
||||||
|
expect(result).toContain("Bash(git config user.email:*)");
|
||||||
|
|
||||||
// Comment tool from minimal server should be included
|
// Comment tool from minimal server should be included
|
||||||
expect(result).toContain("mcp__github_comment__update_claude_comment");
|
expect(result).toContain("mcp__github_comment__update_claude_comment");
|
||||||
|
|||||||
@@ -662,255 +662,4 @@ describe("downloadCommentImages", () => {
|
|||||||
);
|
);
|
||||||
expect(result.get(imageUrl2)).toBeUndefined();
|
expect(result.get(imageUrl2)).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("should detect and download images from HTML img tags", async () => {
|
|
||||||
const mockOctokit = createMockOctokit();
|
|
||||||
const imageUrl =
|
|
||||||
"https://github.com/user-attachments/assets/html-image.png";
|
|
||||||
const signedUrl =
|
|
||||||
"https://private-user-images.githubusercontent.com/html.png?jwt=token";
|
|
||||||
|
|
||||||
// Mock octokit response
|
|
||||||
// @ts-expect-error Mock implementation doesn't match full type signature
|
|
||||||
mockOctokit.rest.issues.getComment = jest.fn().mockResolvedValue({
|
|
||||||
data: {
|
|
||||||
body_html: `<img src="${signedUrl}">`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Mock fetch for image download
|
|
||||||
const mockArrayBuffer = new ArrayBuffer(8);
|
|
||||||
fetchSpy = spyOn(global, "fetch").mockResolvedValue({
|
|
||||||
ok: true,
|
|
||||||
arrayBuffer: async () => mockArrayBuffer,
|
|
||||||
} as Response);
|
|
||||||
|
|
||||||
const comments: CommentWithImages[] = [
|
|
||||||
{
|
|
||||||
type: "issue_comment",
|
|
||||||
id: "777",
|
|
||||||
body: `Here's an HTML image: <img src="${imageUrl}" alt="test">`,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const result = await downloadCommentImages(
|
|
||||||
mockOctokit,
|
|
||||||
"owner",
|
|
||||||
"repo",
|
|
||||||
comments,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(mockOctokit.rest.issues.getComment).toHaveBeenCalledWith({
|
|
||||||
owner: "owner",
|
|
||||||
repo: "repo",
|
|
||||||
comment_id: 777,
|
|
||||||
mediaType: { format: "full+json" },
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(fetchSpy).toHaveBeenCalledWith(signedUrl);
|
|
||||||
expect(fsWriteFileSpy).toHaveBeenCalledWith(
|
|
||||||
"/tmp/github-images/image-1704067200000-0.png",
|
|
||||||
Buffer.from(mockArrayBuffer),
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(result.size).toBe(1);
|
|
||||||
expect(result.get(imageUrl)).toBe(
|
|
||||||
"/tmp/github-images/image-1704067200000-0.png",
|
|
||||||
);
|
|
||||||
expect(consoleLogSpy).toHaveBeenCalledWith(
|
|
||||||
"Found 1 image(s) in issue_comment 777",
|
|
||||||
);
|
|
||||||
expect(consoleLogSpy).toHaveBeenCalledWith(`Downloading ${imageUrl}...`);
|
|
||||||
expect(consoleLogSpy).toHaveBeenCalledWith(
|
|
||||||
"✓ Saved: /tmp/github-images/image-1704067200000-0.png",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("should handle HTML img tags with different quote styles", async () => {
|
|
||||||
const mockOctokit = createMockOctokit();
|
|
||||||
const imageUrl1 =
|
|
||||||
"https://github.com/user-attachments/assets/single-quote.jpg";
|
|
||||||
const imageUrl2 =
|
|
||||||
"https://github.com/user-attachments/assets/double-quote.png";
|
|
||||||
const signedUrl1 =
|
|
||||||
"https://private-user-images.githubusercontent.com/single.jpg?jwt=token1";
|
|
||||||
const signedUrl2 =
|
|
||||||
"https://private-user-images.githubusercontent.com/double.png?jwt=token2";
|
|
||||||
|
|
||||||
// @ts-expect-error Mock implementation doesn't match full type signature
|
|
||||||
mockOctokit.rest.issues.getComment = jest.fn().mockResolvedValue({
|
|
||||||
data: {
|
|
||||||
body_html: `<img src="${signedUrl1}"><img src="${signedUrl2}">`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
fetchSpy = spyOn(global, "fetch").mockResolvedValue({
|
|
||||||
ok: true,
|
|
||||||
arrayBuffer: async () => new ArrayBuffer(8),
|
|
||||||
} as Response);
|
|
||||||
|
|
||||||
const comments: CommentWithImages[] = [
|
|
||||||
{
|
|
||||||
type: "issue_comment",
|
|
||||||
id: "888",
|
|
||||||
body: `Single quote: <img src='${imageUrl1}' alt="test"> and double quote: <img src="${imageUrl2}" alt="test">`,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const result = await downloadCommentImages(
|
|
||||||
mockOctokit,
|
|
||||||
"owner",
|
|
||||||
"repo",
|
|
||||||
comments,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(fetchSpy).toHaveBeenCalledTimes(2);
|
|
||||||
expect(result.size).toBe(2);
|
|
||||||
expect(result.get(imageUrl1)).toBe(
|
|
||||||
"/tmp/github-images/image-1704067200000-0.jpg",
|
|
||||||
);
|
|
||||||
expect(result.get(imageUrl2)).toBe(
|
|
||||||
"/tmp/github-images/image-1704067200000-1.png",
|
|
||||||
);
|
|
||||||
expect(consoleLogSpy).toHaveBeenCalledWith(
|
|
||||||
"Found 2 image(s) in issue_comment 888",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("should handle mixed Markdown and HTML images", async () => {
|
|
||||||
const mockOctokit = createMockOctokit();
|
|
||||||
const markdownUrl =
|
|
||||||
"https://github.com/user-attachments/assets/markdown.png";
|
|
||||||
const htmlUrl = "https://github.com/user-attachments/assets/html.jpg";
|
|
||||||
const signedUrl1 =
|
|
||||||
"https://private-user-images.githubusercontent.com/md.png?jwt=token1";
|
|
||||||
const signedUrl2 =
|
|
||||||
"https://private-user-images.githubusercontent.com/html.jpg?jwt=token2";
|
|
||||||
|
|
||||||
// @ts-expect-error Mock implementation doesn't match full type signature
|
|
||||||
mockOctokit.rest.issues.getComment = jest.fn().mockResolvedValue({
|
|
||||||
data: {
|
|
||||||
body_html: `<img src="${signedUrl1}"><img src="${signedUrl2}">`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
fetchSpy = spyOn(global, "fetch").mockResolvedValue({
|
|
||||||
ok: true,
|
|
||||||
arrayBuffer: async () => new ArrayBuffer(8),
|
|
||||||
} as Response);
|
|
||||||
|
|
||||||
const comments: CommentWithImages[] = [
|
|
||||||
{
|
|
||||||
type: "issue_comment",
|
|
||||||
id: "999",
|
|
||||||
body: `Markdown:  and HTML: <img src="${htmlUrl}" alt="test">`,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const result = await downloadCommentImages(
|
|
||||||
mockOctokit,
|
|
||||||
"owner",
|
|
||||||
"repo",
|
|
||||||
comments,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(fetchSpy).toHaveBeenCalledTimes(2);
|
|
||||||
expect(result.size).toBe(2);
|
|
||||||
expect(result.get(markdownUrl)).toBe(
|
|
||||||
"/tmp/github-images/image-1704067200000-0.png",
|
|
||||||
);
|
|
||||||
expect(result.get(htmlUrl)).toBe(
|
|
||||||
"/tmp/github-images/image-1704067200000-1.jpg",
|
|
||||||
);
|
|
||||||
expect(consoleLogSpy).toHaveBeenCalledWith(
|
|
||||||
"Found 2 image(s) in issue_comment 999",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("should deduplicate identical URLs from Markdown and HTML", async () => {
|
|
||||||
const mockOctokit = createMockOctokit();
|
|
||||||
const imageUrl = "https://github.com/user-attachments/assets/duplicate.png";
|
|
||||||
const signedUrl =
|
|
||||||
"https://private-user-images.githubusercontent.com/dup.png?jwt=token";
|
|
||||||
|
|
||||||
// @ts-expect-error Mock implementation doesn't match full type signature
|
|
||||||
mockOctokit.rest.issues.getComment = jest.fn().mockResolvedValue({
|
|
||||||
data: {
|
|
||||||
body_html: `<img src="${signedUrl}">`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
fetchSpy = spyOn(global, "fetch").mockResolvedValue({
|
|
||||||
ok: true,
|
|
||||||
arrayBuffer: async () => new ArrayBuffer(8),
|
|
||||||
} as Response);
|
|
||||||
|
|
||||||
const comments: CommentWithImages[] = [
|
|
||||||
{
|
|
||||||
type: "issue_comment",
|
|
||||||
id: "1000",
|
|
||||||
body: `Same image twice:  and <img src="${imageUrl}" alt="test">`,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const result = await downloadCommentImages(
|
|
||||||
mockOctokit,
|
|
||||||
"owner",
|
|
||||||
"repo",
|
|
||||||
comments,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(fetchSpy).toHaveBeenCalledTimes(1); // Only downloaded once
|
|
||||||
expect(result.size).toBe(1);
|
|
||||||
expect(result.get(imageUrl)).toBe(
|
|
||||||
"/tmp/github-images/image-1704067200000-0.png",
|
|
||||||
);
|
|
||||||
expect(consoleLogSpy).toHaveBeenCalledWith(
|
|
||||||
"Found 1 image(s) in issue_comment 1000",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("should handle HTML img tags with additional attributes", async () => {
|
|
||||||
const mockOctokit = createMockOctokit();
|
|
||||||
const imageUrl =
|
|
||||||
"https://github.com/user-attachments/assets/complex-tag.webp";
|
|
||||||
const signedUrl =
|
|
||||||
"https://private-user-images.githubusercontent.com/complex.webp?jwt=token";
|
|
||||||
|
|
||||||
// @ts-expect-error Mock implementation doesn't match full type signature
|
|
||||||
mockOctokit.rest.issues.getComment = jest.fn().mockResolvedValue({
|
|
||||||
data: {
|
|
||||||
body_html: `<img src="${signedUrl}">`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
fetchSpy = spyOn(global, "fetch").mockResolvedValue({
|
|
||||||
ok: true,
|
|
||||||
arrayBuffer: async () => new ArrayBuffer(8),
|
|
||||||
} as Response);
|
|
||||||
|
|
||||||
const comments: CommentWithImages[] = [
|
|
||||||
{
|
|
||||||
type: "issue_comment",
|
|
||||||
id: "1001",
|
|
||||||
body: `Complex tag: <img class="image" src="${imageUrl}" alt="test image" width="100" height="200">`,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const result = await downloadCommentImages(
|
|
||||||
mockOctokit,
|
|
||||||
"owner",
|
|
||||||
"repo",
|
|
||||||
comments,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
||||||
expect(result.size).toBe(1);
|
|
||||||
expect(result.get(imageUrl)).toBe(
|
|
||||||
"/tmp/github-images/image-1704067200000-0.webp",
|
|
||||||
);
|
|
||||||
expect(consoleLogSpy).toHaveBeenCalledWith(
|
|
||||||
"Found 1 image(s) in issue_comment 1001",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -39,13 +39,13 @@ describe("Mode Registry", () => {
|
|||||||
|
|
||||||
test("getMode throws error for tag mode with workflow_dispatch event", () => {
|
test("getMode throws error for tag mode with workflow_dispatch event", () => {
|
||||||
expect(() => getMode("tag", mockWorkflowDispatchContext)).toThrow(
|
expect(() => getMode("tag", mockWorkflowDispatchContext)).toThrow(
|
||||||
"Tag mode cannot handle workflow_dispatch events. Use 'agent' mode for automation events.",
|
"Tag mode cannot handle workflow_dispatch events. Use 'agent' mode for automation events or 'remote-agent' mode for repository_dispatch events.",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("getMode throws error for tag mode with schedule event", () => {
|
test("getMode throws error for tag mode with schedule event", () => {
|
||||||
expect(() => getMode("tag", mockScheduleContext)).toThrow(
|
expect(() => getMode("tag", mockScheduleContext)).toThrow(
|
||||||
"Tag mode cannot handle schedule events. Use 'agent' mode for automation events.",
|
"Tag mode cannot handle schedule events. Use 'agent' mode for automation events or 'remote-agent' mode for repository_dispatch events.",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ describe("Mode Registry", () => {
|
|||||||
test("getMode throws error for invalid mode", () => {
|
test("getMode throws error for invalid mode", () => {
|
||||||
const invalidMode = "invalid" as unknown as ModeName;
|
const invalidMode = "invalid" as unknown as ModeName;
|
||||||
expect(() => getMode(invalidMode, mockContext)).toThrow(
|
expect(() => getMode(invalidMode, mockContext)).toThrow(
|
||||||
"Invalid mode 'invalid'. Valid modes are: 'tag', 'agent', 'experimental-review'. Please check your workflow configuration.",
|
"Invalid mode 'invalid'. Valid modes are: 'tag', 'agent', 'remote-agent', 'experimental-review'. Please check your workflow configuration.",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -72,6 +72,7 @@ describe("Mode Registry", () => {
|
|||||||
expect(isValidMode("tag")).toBe(true);
|
expect(isValidMode("tag")).toBe(true);
|
||||||
expect(isValidMode("agent")).toBe(true);
|
expect(isValidMode("agent")).toBe(true);
|
||||||
expect(isValidMode("experimental-review")).toBe(true);
|
expect(isValidMode("experimental-review")).toBe(true);
|
||||||
|
expect(isValidMode("remote-agent")).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("isValidMode returns false for invalid mode", () => {
|
test("isValidMode returns false for invalid mode", () => {
|
||||||
|
|||||||
28
test/report-claude-complete.test.ts
Normal file
28
test/report-claude-complete.test.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { describe, test, expect } from "bun:test";
|
||||||
|
import type { StreamConfig } from "../src/types/stream-config";
|
||||||
|
|
||||||
|
describe("report-claude-complete", () => {
|
||||||
|
test("StreamConfig type should include system_progress_endpoint", () => {
|
||||||
|
const config: StreamConfig = {
|
||||||
|
progress_endpoint: "https://example.com/progress",
|
||||||
|
system_progress_endpoint: "https://example.com/system-progress",
|
||||||
|
resume_endpoint: "https://example.com/resume",
|
||||||
|
session_id: "test-session",
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test-token",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(config.system_progress_endpoint).toBe(
|
||||||
|
"https://example.com/system-progress",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("StreamConfig type should allow optional fields", () => {
|
||||||
|
const config: StreamConfig = {};
|
||||||
|
|
||||||
|
expect(config.system_progress_endpoint).toBeUndefined();
|
||||||
|
expect(config.progress_endpoint).toBeUndefined();
|
||||||
|
expect(config.headers).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user