UNPKG

scai

Version:

> **AI-powered CLI for local code analysis, commit message suggestions, and natural-language queries.** > **100% local • No token cost • Private by design • GDPR-friendly** — made in Denmark/EU with ❤️.

116 lines (106 loc) 4.41 kB
import { logInputOutput } from "../../utils/promptLogHelper.js"; import { generate } from "../../lib/generate.js"; import chalk from "chalk"; export const finalAnswerModule = { name: "finalAnswer", description: "Produces the final, post-execution explanation for the user.", groups: ["finalize"], run: async (input) => { const query = input.query; const context = input.context; if (!context) { throw new Error("[finalAnswerModule] No context provided"); } // --------------------------------------------------------------------- // Detect execution state // --------------------------------------------------------------------- const transformedFiles = context.execution?.codeTransformArtifacts?.files ?? []; const hasTransforms = transformedFiles.length > 0; // --------------------------------------------------------------------- // Select authoritative files // --------------------------------------------------------------------- const filesToConsider = hasTransforms ? transformedFiles.map(f => ({ path: f.filePath, notes: f.notes, codeSnippet: f.content.slice(0, 500), })) : (context.workingFiles ?? []).map(f => ({ path: f.path, summary: f.summary ?? "", codeSnippet: f.code ? f.code.slice(0, 500) : undefined, })); // --------------------------------------------------------------------- // Analysis framing (before / after) // --------------------------------------------------------------------- const analysisPayload = { intent: context.analysis?.intent, // BEFORE: what problem was being solved understanding: context.analysis?.understanding, // AFTER: what is now true across the system combinedAnalysis: context.analysis?.combinedAnalysis, // Supporting detail (optional but useful) fileAnalysis: context.analysis?.fileAnalysis, }; // --------------------------------------------------------------------- // Phase-aware prompt construction // --------------------------------------------------------------------- const phasePreamble = hasTransforms ? ` SYSTEM CONTEXT: This is the FINAL STEP in a long, multi-stage execution pipeline. All planned code transformations have already been executed. The files shown represent the FINAL and AUTHORITATIVE state of the codebase. Your role is to explain what was done and why — not to suggest changes. ` : ` SYSTEM CONTEXT: This request does not include executed code changes. Provide a direct explanation based on the available context. `; const instructions = hasTransforms ? ` INSTRUCTIONS (POST-EXECUTION EXPLANATION MODE): - Explain the completed changes using past tense. - Anchor the explanation in the original problem (understanding). - Describe system-wide effects using combinedAnalysis. - Refer to transformed files as evidence, not proposals. - Mention relevant risks or trade-offs, if any. - DO NOT explain how to implement the change. - DO NOT suggest additional work. - DO NOT ask questions. ` : ` INSTRUCTIONS: - Answer the developer’s question clearly and concisely. - Base your response strictly on the provided context. - Do not invent changes or assume missing information. `; const promptText = ` ${phasePreamble} You are producing the FINAL ANSWER for this run. User query: ${query} Analysis: ${JSON.stringify(analysisPayload, null, 2)} Relevant files (final state): ${JSON.stringify(filesToConsider, null, 2)} ${instructions} `.trim(); // --------------------------------------------------------------------- // Generate final answer // --------------------------------------------------------------------- const aiResponse = await generate({ query, content: promptText, }); const output = { query, content: "", data: aiResponse.data, }; logInputOutput("finalAnswer", "output", output.data); console.log("\n\n\n\n--> Answer:\n", chalk.blue(output.data)); return output; }, };