From 1328894afd83f72a9bc47e97370aa578ea4d50f4 Mon Sep 17 00:00:00 2001 From: David Blass Date: Thu, 23 Oct 2025 17:10:28 -0400 Subject: [PATCH] update action --- agents/claude.ts | 28 ++++++++++++++++++++-------- mcp/config.ts | 2 +- mcp/shared.ts | 2 -- package.json | 2 +- utils/logging.ts | 30 ++++++++++++++++++++++++++++++ utils/setup.ts | 4 ++-- 6 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 utils/logging.ts diff --git a/agents/claude.ts b/agents/claude.ts index 2432eb0..5e00268 100644 --- a/agents/claude.ts +++ b/agents/claude.ts @@ -1,6 +1,7 @@ import { access, constants } from "node:fs/promises"; import * as core from "@actions/core"; import { createMcpConfig } from "../mcp/config.ts"; +import { debugLog, isDebug } from "../utils/logging.ts"; import { spawn } from "../utils/subprocess.ts"; import { boxString, tableString } from "../utils/table.ts"; import { instructions } from "./shared.ts"; @@ -77,23 +78,27 @@ export class ClaudeAgent implements Agent { const env = { ANTHROPIC_API_KEY: this.apiKey, + ...(isDebug() && { LOG_LEVEL: "debug" }), }; console.log(boxString(prompt, { title: "Prompt" })); const mcpConfig = createMcpConfig(this.githubInstallationToken); - console.log("📋 MCP Config:", mcpConfig); + + if (isDebug()) { + debugLog(`📋 MCP Config: ${mcpConfig}`); + } const args = [ "--print", "--output-format", "stream-json", "--verbose", - "--debug", "--permission-mode", "bypassPermissions", "--mcp-config", mcpConfig, + ...(isDebug() ? ["--debug"] : []), ]; core.startGroup("🔄 Run details"); @@ -114,7 +119,9 @@ export class ClaudeAgent implements Agent { input: `${instructions} ${prompt}`, timeout: 10 * 60 * 1000, // 10 minutes onStdout: (_chunk) => { - processJSONChunk(_chunk, this); + if (_chunk.trim()) { + processJSONChunk(_chunk, this); + } }, onStderr: (_chunk) => { if (_chunk.trim()) { @@ -165,14 +172,19 @@ export class ClaudeAgent implements Agent { */ function processJSONChunk(chunk: string, agent?: ClaudeAgent): void { try { - // Skip debug lines that start with [DEBUG] or [debug] const trimmedChunk = chunk.trim(); if (trimmedChunk.startsWith("[DEBUG]") || trimmedChunk.startsWith("[debug]")) { console.log(chunk); return; } - console.log(chunk); + if (trimmedChunk.startsWith("[ERROR]") || trimmedChunk.startsWith("[error]")) { + console.error(chunk); + return; + } + + debugLog(trimmedChunk); + const parsedChunk = JSON.parse(trimmedChunk); switch (parsedChunk.type) { @@ -309,11 +321,11 @@ function processJSONChunk(chunk: string, agent?: ClaudeAgent): void { break; default: - core.debug(`📦 Unknown chunk type: ${parsedChunk.type}`); + debugLog(`📦 Unknown chunk type: ${parsedChunk.type}`); break; } } catch (error) { - core.debug(`Failed to parse chunk: ${error}`); - core.debug(`Raw chunk: ${chunk.substring(0, 200)}...`); + debugLog(`Failed to parse chunk: ${error}`); + debugLog(`Raw chunk: ${chunk.substring(0, 200)}...`); } } diff --git a/mcp/config.ts b/mcp/config.ts index 1c6c1c4..680d351 100644 --- a/mcp/config.ts +++ b/mcp/config.ts @@ -21,7 +21,7 @@ export function createMcpConfig(githubInstallationToken: string) { env: { GITHUB_INSTALLATION_TOKEN: githubInstallationToken, GITHUB_REPOSITORY: githubRepository, - LOG_LEVEL: "debug", + LOG_LEVEL: process.env.LOG_LEVEL, }, }, }, diff --git a/mcp/shared.ts b/mcp/shared.ts index e454ed2..522ee3c 100644 --- a/mcp/shared.ts +++ b/mcp/shared.ts @@ -9,7 +9,6 @@ export interface ToolResult { type: "text"; text: string; }[]; - error?: string; isError?: boolean; } @@ -72,7 +71,6 @@ const handleToolError = (error: unknown): ToolResult => { text: `Error: ${errorMessage}`, }, ], - error: errorMessage, isError: true, }; }; diff --git a/package.json b/package.json index 35fb9ce..d746bbc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@pullfrog/action", - "version": "0.0.58", + "version": "0.0.60", "type": "module", "files": [ "index.js", diff --git a/utils/logging.ts b/utils/logging.ts new file mode 100644 index 0000000..8c979cf --- /dev/null +++ b/utils/logging.ts @@ -0,0 +1,30 @@ +/** + * Centralized debug logging utility + * Controls debug behavior based on LOG_LEVEL environment variable + */ + +import * as core from "@actions/core"; + +const isDebugEnabled = process.env.LOG_LEVEL === "debug"; +const isGitHubActions = !!process.env.GITHUB_ACTIONS; + +/** + * Check if debug logging is enabled + */ +export function isDebug(): boolean { + return isDebugEnabled; +} + +/** + * Log debug message if debug is enabled + * Uses core.debug() in GitHub Actions, console.log() locally + */ +export function debugLog(message: string): void { + if (isDebugEnabled) { + if (isGitHubActions) { + core.debug(message); + } else { + console.log(`[DEBUG] ${message}`); + } + } +} \ No newline at end of file diff --git a/utils/setup.ts b/utils/setup.ts index f7d161b..7a3710b 100644 --- a/utils/setup.ts +++ b/utils/setup.ts @@ -43,8 +43,8 @@ export function setupTestRepo(options: SetupOptions): void { */ export function setupGitConfig(): void { console.log("🔧 Setting up git configuration..."); - execSync('git config --global user.email "action@pullfrog.ai"', { stdio: "inherit" }); - execSync('git config --global user.name "Pullfrog Action"', { stdio: "inherit" }); + execSync('git config user.email "action@pullfrog.ai"', { stdio: "inherit" }); + execSync('git config user.name "Pullfrog Action"', { stdio: "inherit" }); } /**