* Load and parse agent output from the GH_AW_AGENT_OUTPUT file * * This utility handles the common pattern of: * 1. Reading the GH_AW_AGENT_OUTPUT environment variable * 2. Loading the file content * 3. Validating the JSON structure * 4. Returning parsed items array * * @returns {{ * succ
()
| 42 | * }} Result object with success flag and items array (if successful) or error message |
| 43 | */ |
| 44 | function loadAgentOutput() { |
| 45 | const agentOutputFile = process.env.GH_AW_AGENT_OUTPUT; |
| 46 | |
| 47 | // No agent output file specified |
| 48 | if (!agentOutputFile) { |
| 49 | core.info("No GH_AW_AGENT_OUTPUT environment variable found"); |
| 50 | return { success: false }; |
| 51 | } |
| 52 | |
| 53 | // Read agent output from file |
| 54 | let outputContent; |
| 55 | try { |
| 56 | outputContent = fs.readFileSync(agentOutputFile, "utf8"); |
| 57 | } catch (error) { |
| 58 | const errorMessage = `Error reading agent output file: ${getErrorMessage(error)}`; |
| 59 | // Use info instead of error for missing files - this is a normal scenario |
| 60 | // when the agent fails before producing any safe-outputs |
| 61 | core.info(errorMessage); |
| 62 | return { success: false, error: errorMessage }; |
| 63 | } |
| 64 | |
| 65 | // Check for empty content |
| 66 | if (outputContent.trim() === "") { |
| 67 | core.info("Agent output content is empty"); |
| 68 | return { success: false }; |
| 69 | } |
| 70 | |
| 71 | core.info(`Agent output content length: ${outputContent.length}`); |
| 72 | |
| 73 | // Parse the validated output JSON |
| 74 | let validatedOutput; |
| 75 | try { |
| 76 | validatedOutput = JSON.parse(outputContent); |
| 77 | } catch (error) { |
| 78 | const errorMessage = `Error parsing agent output JSON: ${getErrorMessage(error)}`; |
| 79 | core.error(errorMessage); |
| 80 | core.info(`Failed to parse content:\n${truncateForLogging(outputContent)}`); |
| 81 | return { success: false, error: errorMessage }; |
| 82 | } |
| 83 | |
| 84 | // Validate items array exists |
| 85 | if (!validatedOutput.items || !Array.isArray(validatedOutput.items)) { |
| 86 | core.info("No valid items found in agent output"); |
| 87 | core.info(`Parsed content: ${truncateForLogging(JSON.stringify(validatedOutput))}`); |
| 88 | return { success: false }; |
| 89 | } |
| 90 | |
| 91 | return { success: true, items: validatedOutput.items }; |
| 92 | } |
| 93 | |
| 94 | module.exports = { loadAgentOutput, truncateForLogging, MAX_LOG_CONTENT_LENGTH }; |
no test coverage detected