Run npm run format

- Also updated README.md accordingly.

Part of https://b.corp.google.com/issues/411384603
This commit is contained in:
Taylor Mullen
2025-04-17 18:06:21 -04:00
committed by N. Taylor Mullen
parent 7928c1727f
commit cfc697a96d
45 changed files with 4373 additions and 3332 deletions

View File

@@ -1,19 +1,18 @@
import { promises as fs } from 'fs';
import { SchemaUnion, Type } from "@google/genai"; // Assuming these types exist
import { GeminiClient } from "../core/gemini-client.js"; // Assuming this path
import { SchemaUnion, Type } from '@google/genai'; // Assuming these types exist
import { GeminiClient } from '../core/gemini-client.js'; // Assuming this path
import { exec } from 'child_process'; // Needed for Windows process check
import { promisify } from 'util'; // To promisify exec
// Promisify child_process.exec for easier async/await usage
const execAsync = promisify(exec);
// Define the expected interface for the AI client dependency
export interface AiClient {
generateJson(
prompt: any[], // Keep flexible or define a stricter prompt structure type
schema: SchemaUnion
): Promise<any>; // Ideally, specify the expected JSON structure TAnalysisResult | TAnalysisFailure
generateJson(
prompt: any[], // Keep flexible or define a stricter prompt structure type
schema: SchemaUnion,
): Promise<any>; // Ideally, specify the expected JSON structure TAnalysisResult | TAnalysisFailure
}
// Identifier for the background process (e.g., PID)
@@ -22,232 +21,290 @@ export type ProcessHandle = number | string | unknown;
// Represents the structure expected from a successful LLM analysis call
export interface AnalysisResult {
summary: string;
inferredStatus: 'Running' | 'SuccessReported' | 'ErrorReported' | 'Unknown';
summary: string;
inferredStatus: 'Running' | 'SuccessReported' | 'ErrorReported' | 'Unknown';
}
// Represents the structure returned when the LLM analysis itself fails
export interface AnalysisFailure {
error: string;
inferredStatus: 'AnalysisFailed';
error: string;
inferredStatus: 'AnalysisFailed';
}
// Type guard to check if the result is a failure object
function isAnalysisFailure(result: AnalysisResult | AnalysisFailure): result is AnalysisFailure {
return (result as AnalysisFailure).inferredStatus === 'AnalysisFailed';
function isAnalysisFailure(
result: AnalysisResult | AnalysisFailure,
): result is AnalysisFailure {
return (result as AnalysisFailure).inferredStatus === 'AnalysisFailed';
}
// Represents the final outcome after polling is complete (or failed/timed out)
export interface FinalAnalysisOutcome {
status: string; // e.g., 'SuccessReported', 'ErrorReported', 'ProcessEnded_SuccessReported', 'TimedOut_Running', 'AnalysisFailed'
summary: string; // Final summary or error message
status: string; // e.g., 'SuccessReported', 'ErrorReported', 'ProcessEnded_SuccessReported', 'TimedOut_Running', 'AnalysisFailed'
summary: string; // Final summary or error message
}
export class BackgroundTerminalAnalyzer {
private ai: AiClient;
// Make polling parameters configurable via constructor
private pollIntervalMs: number;
private maxAttempts: number;
private initialDelayMs: number;
private ai: AiClient;
// Make polling parameters configurable via constructor
private pollIntervalMs: number;
private maxAttempts: number;
private initialDelayMs: number;
// --- Dependency Injection & Configuration ---
constructor(
aiClient?: AiClient, // Allow injecting AiClient, default to GeminiClient
options: {
pollIntervalMs?: number,
maxAttempts?: number,
initialDelayMs?: number
} = {} // Provide default options
) {
this.ai = aiClient || new GeminiClient(); // Use injected client or default
this.pollIntervalMs = options.pollIntervalMs ?? 5000; // Default 5 seconds
this.maxAttempts = options.maxAttempts ?? 6; // Default 6 attempts (approx 30s total)
this.initialDelayMs = options.initialDelayMs ?? 500; // Default 0.5s initial delay
}
// --- Dependency Injection & Configuration ---
constructor(
aiClient?: AiClient, // Allow injecting AiClient, default to GeminiClient
options: {
pollIntervalMs?: number;
maxAttempts?: number;
initialDelayMs?: number;
} = {}, // Provide default options
) {
this.ai = aiClient || new GeminiClient(); // Use injected client or default
this.pollIntervalMs = options.pollIntervalMs ?? 5000; // Default 5 seconds
this.maxAttempts = options.maxAttempts ?? 6; // Default 6 attempts (approx 30s total)
this.initialDelayMs = options.initialDelayMs ?? 500; // Default 0.5s initial delay
}
/**
* Polls the output of a background process using an LLM
* until a conclusive status is determined or timeout occurs.
* @param pid The handle/identifier of the background process (typically PID number).
* @param tempStdoutFilePath Path to the temporary file capturing stdout.
* @param tempStderrFilePath Path to the temporary file capturing stderr.
* @param command The command string that was executed (for context in prompts).
* @returns A promise resolving to the final analysis outcome.
*/
public async analyze(
pid: ProcessHandle,
tempStdoutFilePath: string,
tempStderrFilePath: string,
command: string
): Promise<FinalAnalysisOutcome> {
/**
* Polls the output of a background process using an LLM
* until a conclusive status is determined or timeout occurs.
* @param pid The handle/identifier of the background process (typically PID number).
* @param tempStdoutFilePath Path to the temporary file capturing stdout.
* @param tempStderrFilePath Path to the temporary file capturing stderr.
* @param command The command string that was executed (for context in prompts).
* @returns A promise resolving to the final analysis outcome.
*/
public async analyze(
pid: ProcessHandle,
tempStdoutFilePath: string,
tempStderrFilePath: string,
command: string,
): Promise<FinalAnalysisOutcome> {
// --- Initial Delay ---
// Wait briefly before the first check to allow the process to initialize
// and potentially write initial output.
await new Promise((resolve) => setTimeout(resolve, this.initialDelayMs));
// --- Initial Delay ---
// Wait briefly before the first check to allow the process to initialize
// and potentially write initial output.
await new Promise(resolve => setTimeout(resolve, this.initialDelayMs));
let attempts = 0;
let lastAnalysisResult: AnalysisResult | AnalysisFailure | null = null;
let attempts = 0;
let lastAnalysisResult: AnalysisResult | AnalysisFailure | null = null;
while (attempts < this.maxAttempts) {
attempts++;
let currentStdout: string = '';
let currentStderr: string = '';
while (attempts < this.maxAttempts) {
attempts++;
let currentStdout: string = '';
let currentStderr: string = '';
// --- Robust File Reading ---
try {
currentStdout = await fs.readFile(tempStdoutFilePath, 'utf-8');
} catch (error: any) {
// If file doesn't exist yet or isn't readable, treat as empty, but log warning
if (error.code !== 'ENOENT') {
console.warn(`Attempt ${attempts}: Failed to read stdout file ${tempStdoutFilePath}: ${error.message}`);
}
}
try {
currentStderr = await fs.readFile(tempStderrFilePath, 'utf-8');
} catch (error: any) {
if (error.code !== 'ENOENT') {
console.warn(`Attempt ${attempts}: Failed to read stderr file ${tempStderrFilePath}: ${error.message}`);
}
}
// --- Process Status Check ---
let isRunning = false;
try {
// Check if process is running *before* the final analysis if it seems to have ended
isRunning = await this.isProcessRunning(pid);
if (!isRunning) {
// Reread files one last time in case output was written just before exit
try { currentStdout = await fs.readFile(tempStdoutFilePath, 'utf-8'); } catch {}
try { currentStderr = await fs.readFile(tempStderrFilePath, 'utf-8'); } catch {}
lastAnalysisResult = await this.analyzeOutputWithLLM(currentStdout, currentStderr, command);
if (isAnalysisFailure(lastAnalysisResult)) {
return { status: 'ProcessEnded_AnalysisFailed', summary: `Process ended. Final analysis failed: ${lastAnalysisResult.error}` };
}
// Append ProcessEnded to the status determined by the final analysis
return { status: 'ProcessEnded_' + lastAnalysisResult.inferredStatus, summary: `Process ended. Final analysis summary: ${lastAnalysisResult.summary}` };
}
} catch (procCheckError: any) {
// Log the error but allow polling to continue, as log analysis might still be useful
console.warn(`Could not check process status for PID ${pid} on attempt ${attempts}: ${procCheckError.message}`);
// Decide if you want to bail out here or continue analysis based on logs only
// For now, we continue.
}
// --- LLM Analysis ---
lastAnalysisResult = await this.analyzeOutputWithLLM(currentStdout, currentStderr, command);
if (isAnalysisFailure(lastAnalysisResult)) {
console.error(`LLM Analysis failed for PID ${pid} on attempt ${attempts}:`, lastAnalysisResult.error);
// Stop polling on analysis failure, returning the specific failure status
return { status: lastAnalysisResult.inferredStatus, summary: lastAnalysisResult.error };
}
// --- Exit Conditions ---
if (lastAnalysisResult.inferredStatus === 'SuccessReported' || lastAnalysisResult.inferredStatus === 'ErrorReported') {
return { status: lastAnalysisResult.inferredStatus, summary: lastAnalysisResult.summary };
}
// Heuristic: If the process seems stable and 'Running' after several checks,
// return that status without waiting for the full timeout. Adjust threshold as needed.
const runningExitThreshold = Math.floor(this.maxAttempts / 3) + 1; // e.g., exit after attempt 4 if maxAttempts is 6
if (attempts >= runningExitThreshold && lastAnalysisResult.inferredStatus === 'Running') {
return { status: lastAnalysisResult.inferredStatus, summary: lastAnalysisResult.summary };
}
// --- Wait before next poll ---
if (attempts < this.maxAttempts) {
await new Promise(resolve => setTimeout(resolve, this.pollIntervalMs));
}
} // End while loop
// --- Timeout Condition ---
console.warn(`Polling timed out for PID ${pid} after ${this.maxAttempts} attempts.`);
// Determine final status based on the last successful analysis (if any)
const finalStatus = (lastAnalysisResult && !isAnalysisFailure(lastAnalysisResult))
? `TimedOut_${lastAnalysisResult.inferredStatus}` // e.g., TimedOut_Running
: 'TimedOut_AnalysisFailed'; // If last attempt failed or no analysis succeeded
const finalSummary = (lastAnalysisResult && !isAnalysisFailure(lastAnalysisResult))
? `Polling timed out after ${this.maxAttempts} attempts. Last known summary: ${lastAnalysisResult.summary}`
: (lastAnalysisResult && isAnalysisFailure(lastAnalysisResult))
? `Polling timed out; last analysis attempt failed: ${lastAnalysisResult}`
: `Polling timed out after ${this.maxAttempts} attempts without any successful analysis.`;
return { status: finalStatus, summary: finalSummary };
}
// --- Actual Implementation of isProcessRunning ---
/**
* Checks if the background process is still running using OS-specific methods.
* @param pid Process handle/identifier (expects a number for standard checks).
* @returns True if running, false otherwise.
* @throws Error if the check itself fails critically (e.g., command not found, permissions).
*/
private async isProcessRunning(pid: ProcessHandle): Promise<boolean> {
if (typeof pid !== 'number' || !Number.isInteger(pid) || pid <= 0) {
console.warn(`isProcessRunning: Invalid PID provided (${pid}). Assuming not running.`);
return false;
// --- Robust File Reading ---
try {
currentStdout = await fs.readFile(tempStdoutFilePath, 'utf-8');
} catch (error: any) {
// If file doesn't exist yet or isn't readable, treat as empty, but log warning
if (error.code !== 'ENOENT') {
console.warn(
`Attempt ${attempts}: Failed to read stdout file ${tempStdoutFilePath}: ${error.message}`,
);
}
try {
if (process.platform === 'win32') {
// Windows: Use tasklist command
const command = `tasklist /FI "PID eq ${pid}" /NH`; // /NH for no header
const { stdout } = await execAsync(command);
// Check if the output contains the process information (it will have the image name if found)
return stdout.toLowerCase().includes('.exe'); // A simple check, adjust if needed
} else {
// Linux/macOS/Unix-like: Use kill -0 signal
// process.kill sends signal 0 to check existence without killing
process.kill(pid, 0);
return true; // If no error is thrown, process exists
}
} catch (error: any) {
if (error.code === 'ESRCH') {
// ESRCH: Standard error code for "No such process" on Unix-like systems
return false;
} else if (process.platform === 'win32' && error.message.includes('No tasks are running')) {
// tasklist specific error when PID doesn't exist
return false;
} else {
// Other errors (e.g., EPERM - permission denied) mean we couldn't determine status.
// Re-throwing might be appropriate depending on desired behavior.
// Here, we log it and cautiously return true, assuming it *might* still be running.
console.warn(`isProcessRunning(${pid}) encountered error: ${error.message}. Assuming process might still exist.`);
// Or you could throw the error: throw new Error(`Failed to check process status for PID ${pid}: ${error.message}`);
return true; // Cautious assumption
}
}
try {
currentStderr = await fs.readFile(tempStderrFilePath, 'utf-8');
} catch (error: any) {
if (error.code !== 'ENOENT') {
console.warn(
`Attempt ${attempts}: Failed to read stderr file ${tempStderrFilePath}: ${error.message}`,
);
}
}
}
// --- LLM Analysis Method (largely unchanged but added validation robustness) ---
private async analyzeOutputWithLLM(
stdout: string,
stderr: string,
command: string
): Promise<AnalysisResult | AnalysisFailure> {
try {
const schema: SchemaUnion = { /* ... schema definition remains the same ... */
type: Type.OBJECT,
properties: {
summary: {
type: Type.STRING,
description: "A concise interpretation of significant events, progress, final results, or errors found in the process's stdout and stderr. Summarizes what the logs indicate happened. Should be formatted as markdown."
},
inferredStatus: {
type: Type.STRING,
description: "The inferred status based *only* on analyzing the provided log content. Possible values: 'Running' (logs show ongoing activity without completion/error), 'SuccessReported' (logs indicate successful completion or final positive result), 'ErrorReported' (logs indicate an error or failure), 'Unknown' (status cannot be clearly determined from the log content).",
enum: ['Running', 'SuccessReported', 'ErrorReported', 'Unknown']
}
},
required: ['summary', 'inferredStatus']
// --- Process Status Check ---
let isRunning = false;
try {
// Check if process is running *before* the final analysis if it seems to have ended
isRunning = await this.isProcessRunning(pid);
if (!isRunning) {
// Reread files one last time in case output was written just before exit
try {
currentStdout = await fs.readFile(tempStdoutFilePath, 'utf-8');
} catch {}
try {
currentStderr = await fs.readFile(tempStderrFilePath, 'utf-8');
} catch {}
lastAnalysisResult = await this.analyzeOutputWithLLM(
currentStdout,
currentStderr,
command,
);
if (isAnalysisFailure(lastAnalysisResult)) {
return {
status: 'ProcessEnded_AnalysisFailed',
summary: `Process ended. Final analysis failed: ${lastAnalysisResult.error}`,
};
}
// Append ProcessEnded to the status determined by the final analysis
return {
status: 'ProcessEnded_' + lastAnalysisResult.inferredStatus,
summary: `Process ended. Final analysis summary: ${lastAnalysisResult.summary}`,
};
}
} catch (procCheckError: any) {
// Log the error but allow polling to continue, as log analysis might still be useful
console.warn(
`Could not check process status for PID ${pid} on attempt ${attempts}: ${procCheckError.message}`,
);
// Decide if you want to bail out here or continue analysis based on logs only
// For now, we continue.
}
const prompt = `**Analyze Background Process Logs**
// --- LLM Analysis ---
lastAnalysisResult = await this.analyzeOutputWithLLM(
currentStdout,
currentStderr,
command,
);
if (isAnalysisFailure(lastAnalysisResult)) {
console.error(
`LLM Analysis failed for PID ${pid} on attempt ${attempts}:`,
lastAnalysisResult.error,
);
// Stop polling on analysis failure, returning the specific failure status
return {
status: lastAnalysisResult.inferredStatus,
summary: lastAnalysisResult.error,
};
}
// --- Exit Conditions ---
if (
lastAnalysisResult.inferredStatus === 'SuccessReported' ||
lastAnalysisResult.inferredStatus === 'ErrorReported'
) {
return {
status: lastAnalysisResult.inferredStatus,
summary: lastAnalysisResult.summary,
};
}
// Heuristic: If the process seems stable and 'Running' after several checks,
// return that status without waiting for the full timeout. Adjust threshold as needed.
const runningExitThreshold = Math.floor(this.maxAttempts / 3) + 1; // e.g., exit after attempt 4 if maxAttempts is 6
if (
attempts >= runningExitThreshold &&
lastAnalysisResult.inferredStatus === 'Running'
) {
return {
status: lastAnalysisResult.inferredStatus,
summary: lastAnalysisResult.summary,
};
}
// --- Wait before next poll ---
if (attempts < this.maxAttempts) {
await new Promise((resolve) =>
setTimeout(resolve, this.pollIntervalMs),
);
}
} // End while loop
// --- Timeout Condition ---
console.warn(
`Polling timed out for PID ${pid} after ${this.maxAttempts} attempts.`,
);
// Determine final status based on the last successful analysis (if any)
const finalStatus =
lastAnalysisResult && !isAnalysisFailure(lastAnalysisResult)
? `TimedOut_${lastAnalysisResult.inferredStatus}` // e.g., TimedOut_Running
: 'TimedOut_AnalysisFailed'; // If last attempt failed or no analysis succeeded
const finalSummary =
lastAnalysisResult && !isAnalysisFailure(lastAnalysisResult)
? `Polling timed out after ${this.maxAttempts} attempts. Last known summary: ${lastAnalysisResult.summary}`
: lastAnalysisResult && isAnalysisFailure(lastAnalysisResult)
? `Polling timed out; last analysis attempt failed: ${lastAnalysisResult}`
: `Polling timed out after ${this.maxAttempts} attempts without any successful analysis.`;
return { status: finalStatus, summary: finalSummary };
}
// --- Actual Implementation of isProcessRunning ---
/**
* Checks if the background process is still running using OS-specific methods.
* @param pid Process handle/identifier (expects a number for standard checks).
* @returns True if running, false otherwise.
* @throws Error if the check itself fails critically (e.g., command not found, permissions).
*/
private async isProcessRunning(pid: ProcessHandle): Promise<boolean> {
if (typeof pid !== 'number' || !Number.isInteger(pid) || pid <= 0) {
console.warn(
`isProcessRunning: Invalid PID provided (${pid}). Assuming not running.`,
);
return false;
}
try {
if (process.platform === 'win32') {
// Windows: Use tasklist command
const command = `tasklist /FI "PID eq ${pid}" /NH`; // /NH for no header
const { stdout } = await execAsync(command);
// Check if the output contains the process information (it will have the image name if found)
return stdout.toLowerCase().includes('.exe'); // A simple check, adjust if needed
} else {
// Linux/macOS/Unix-like: Use kill -0 signal
// process.kill sends signal 0 to check existence without killing
process.kill(pid, 0);
return true; // If no error is thrown, process exists
}
} catch (error: any) {
if (error.code === 'ESRCH') {
// ESRCH: Standard error code for "No such process" on Unix-like systems
return false;
} else if (
process.platform === 'win32' &&
error.message.includes('No tasks are running')
) {
// tasklist specific error when PID doesn't exist
return false;
} else {
// Other errors (e.g., EPERM - permission denied) mean we couldn't determine status.
// Re-throwing might be appropriate depending on desired behavior.
// Here, we log it and cautiously return true, assuming it *might* still be running.
console.warn(
`isProcessRunning(${pid}) encountered error: ${error.message}. Assuming process might still exist.`,
);
// Or you could throw the error: throw new Error(`Failed to check process status for PID ${pid}: ${error.message}`);
return true; // Cautious assumption
}
}
}
// --- LLM Analysis Method (largely unchanged but added validation robustness) ---
private async analyzeOutputWithLLM(
stdout: string,
stderr: string,
command: string,
): Promise<AnalysisResult | AnalysisFailure> {
try {
const schema: SchemaUnion = {
/* ... schema definition remains the same ... */ type: Type.OBJECT,
properties: {
summary: {
type: Type.STRING,
description:
"A concise interpretation of significant events, progress, final results, or errors found in the process's stdout and stderr. Summarizes what the logs indicate happened. Should be formatted as markdown.",
},
inferredStatus: {
type: Type.STRING,
description:
"The inferred status based *only* on analyzing the provided log content. Possible values: 'Running' (logs show ongoing activity without completion/error), 'SuccessReported' (logs indicate successful completion or final positive result), 'ErrorReported' (logs indicate an error or failure), 'Unknown' (status cannot be clearly determined from the log content).",
enum: ['Running', 'SuccessReported', 'ErrorReported', 'Unknown'],
},
},
required: ['summary', 'inferredStatus'],
};
const prompt = `**Analyze Background Process Logs**
**Context:** A command (\`${command}\`) was executed in the background. You are analyzing the standard output (stdout) and standard error (stderr) collected so far to understand its progress and outcome. This analysis will be used to inform a user about what the command did.
@@ -277,49 +334,85 @@ Based *only* on the provided stdout and stderr:
3. **Format Output:** Return the results as a JSON object adhering strictly to the following schema:
\`\`\`json
${JSON.stringify({ // Generate the schema JSON string for the prompt context
type: "object",
properties: {
summary: { type: "string", description: "Concise markdown summary of log interpretation." },
inferredStatus: { type: "string", enum: ["Running", "SuccessReported", "ErrorReported", "Unknown"], description: "Status inferred *only* from log content." }
},
required: ["summary", "inferredStatus"]
}, null, 2)}
${JSON.stringify(
{
// Generate the schema JSON string for the prompt context
type: 'object',
properties: {
summary: {
type: 'string',
description: 'Concise markdown summary of log interpretation.',
},
inferredStatus: {
type: 'string',
enum: ['Running', 'SuccessReported', 'ErrorReported', 'Unknown'],
description: 'Status inferred *only* from log content.',
},
},
required: ['summary', 'inferredStatus'],
},
null,
2,
)}
\`\`\`
**Instructions:**
* The \`summary\` must be an interpretation of the logs, focusing on key outcomes or activities. Prioritize recent events if logs are extensive.
* The \`inferredStatus\` should reflect the most likely state *deduced purely from the log text provided*. Ensure it is one of the specified enum values.`;
const response = await this.ai.generateJson([{ role: "user", parts: [{ text: prompt }] }], schema);
const response = await this.ai.generateJson(
[{ role: 'user', parts: [{ text: prompt }] }],
schema,
);
// --- Enhanced Validation ---
if (typeof response !== 'object' || response === null) {
throw new Error(`LLM returned non-object response: ${JSON.stringify(response)}`);
}
if (typeof response.summary !== 'string' || response.summary.trim() === '') {
// Ensure summary is a non-empty string
console.warn("LLM response validation warning: 'summary' field is missing, empty or not a string. Raw response:", response);
// Decide how to handle: throw error, or assign default? Let's throw for now.
throw new Error(`LLM response missing or invalid 'summary'. Got: ${JSON.stringify(response.summary)}`);
// --- Enhanced Validation ---
if (typeof response !== 'object' || response === null) {
throw new Error(
`LLM returned non-object response: ${JSON.stringify(response)}`,
);
}
if (
typeof response.summary !== 'string' ||
response.summary.trim() === ''
) {
// Ensure summary is a non-empty string
console.warn(
"LLM response validation warning: 'summary' field is missing, empty or not a string. Raw response:",
response,
);
// Decide how to handle: throw error, or assign default? Let's throw for now.
throw new Error(
`LLM response missing or invalid 'summary'. Got: ${JSON.stringify(response.summary)}`,
);
}
if (
!['Running', 'SuccessReported', 'ErrorReported', 'Unknown'].includes(
response.inferredStatus,
)
) {
console.warn(
`LLM response validation warning: 'inferredStatus' is invalid ('${response.inferredStatus}'). Raw response:`,
response,
);
// Decide how to handle: throw error, or default to 'Unknown'? Let's throw.
throw new Error(
`LLM returned invalid 'inferredStatus': ${JSON.stringify(response.inferredStatus)}`,
);
}
}
if (!['Running', 'SuccessReported', 'ErrorReported', 'Unknown'].includes(response.inferredStatus)) {
console.warn(`LLM response validation warning: 'inferredStatus' is invalid ('${response.inferredStatus}'). Raw response:`, response);
// Decide how to handle: throw error, or default to 'Unknown'? Let's throw.
throw new Error(`LLM returned invalid 'inferredStatus': ${JSON.stringify(response.inferredStatus)}`);
}
return response as AnalysisResult; // Cast after validation
} catch (error: any) {
console.error(`LLM analysis call failed for command "${command}":`, error);
// Ensure the error message passed back is helpful
const errorMessage = error instanceof Error ? error.message : String(error);
return {
error: `LLM analysis call encountered an error: ${errorMessage}`,
inferredStatus: 'AnalysisFailed'
};
}
return response as AnalysisResult; // Cast after validation
} catch (error: any) {
console.error(
`LLM analysis call failed for command "${command}":`,
error,
);
// Ensure the error message passed back is helpful
const errorMessage =
error instanceof Error ? error.message : String(error);
return {
error: `LLM analysis call encountered an error: ${errorMessage}`,
inferredStatus: 'AnalysisFailed',
};
}
}
}
}

View File

@@ -18,11 +18,12 @@ interface FolderStructureOptions {
}
// Define a type for the merged options where fileIncludePattern remains optional
type MergedFolderStructureOptions = Required<Omit<FolderStructureOptions, 'fileIncludePattern'>> & {
fileIncludePattern?: RegExp;
type MergedFolderStructureOptions = Required<
Omit<FolderStructureOptions, 'fileIncludePattern'>
> & {
fileIncludePattern?: RegExp;
};
/** Represents the full, unfiltered information about a folder and its contents. */
interface FullFolderInfo {
name: string;
@@ -55,7 +56,7 @@ interface ReducedFolderNode {
*/
async function readFullStructure(
folderPath: string,
options: MergedFolderStructureOptions
options: MergedFolderStructureOptions,
): Promise<FullFolderInfo | null> {
const name = path.basename(folderPath);
// Initialize with isIgnored: false
@@ -88,7 +89,7 @@ async function readFullStructure(
files: [],
subFolders: [],
totalChildren: 0, // No children explored
totalFiles: 0, // No files explored
totalFiles: 0, // No files explored
isIgnored: true, // Mark as ignored
};
folderInfo.subFolders.push(ignoredFolderInfo);
@@ -99,7 +100,12 @@ async function readFullStructure(
// If not ignored, recurse as before
const subFolderInfo = await readFullStructure(subFolderPath, options);
// Add non-empty folders OR explicitly ignored folders
if (subFolderInfo && (subFolderInfo.totalChildren > 0 || subFolderInfo.files.length > 0 || subFolderInfo.isIgnored)) {
if (
subFolderInfo &&
(subFolderInfo.totalChildren > 0 ||
subFolderInfo.files.length > 0 ||
subFolderInfo.isIgnored)
) {
folderInfo.subFolders.push(subFolderInfo);
}
}
@@ -107,34 +113,43 @@ async function readFullStructure(
// Then process files (only if the current folder itself isn't marked as ignored)
for (const entry of entries) {
if (entry.isFile()) {
const fileName = entry.name;
// Include if no pattern or if pattern matches
if (!options.fileIncludePattern || options.fileIncludePattern.test(fileName)) {
folderInfo.files.push(fileName);
}
}
if (entry.isFile()) {
const fileName = entry.name;
// Include if no pattern or if pattern matches
if (
!options.fileIncludePattern ||
options.fileIncludePattern.test(fileName)
) {
folderInfo.files.push(fileName);
}
}
}
// Calculate totals *after* processing children
// Ignored folders contribute 0 to counts here because we didn't look inside.
totalFileCount = folderInfo.files.length + folderInfo.subFolders.reduce((sum, sf) => sum + sf.totalFiles, 0);
totalFileCount =
folderInfo.files.length +
folderInfo.subFolders.reduce((sum, sf) => sum + sf.totalFiles, 0);
// Count the ignored folder itself as one child item in the parent's count.
totalChildrenCount = folderInfo.files.length + folderInfo.subFolders.length + folderInfo.subFolders.reduce((sum, sf) => sum + sf.totalChildren, 0);
totalChildrenCount =
folderInfo.files.length +
folderInfo.subFolders.length +
folderInfo.subFolders.reduce((sum, sf) => sum + sf.totalChildren, 0);
} catch (error: any) {
if (error.code === 'EACCES' || error.code === 'ENOENT') {
console.warn(`Warning: Could not read directory ${folderPath}: ${error.message}`);
console.warn(
`Warning: Could not read directory ${folderPath}: ${error.message}`,
);
return null;
}
throw error;
}
return {
...(folderInfo as FullFolderInfo), // Cast needed after conditional assignment check
totalChildren: totalChildrenCount,
totalFiles: totalFileCount,
};
return {
...(folderInfo as FullFolderInfo), // Cast needed after conditional assignment check
totalChildren: totalChildrenCount,
totalFiles: totalFileCount,
};
}
/**
@@ -146,12 +161,20 @@ async function readFullStructure(
* @returns The root node of the reduced structure.
*/
function reduceStructure(
fullInfo: FullFolderInfo,
maxItems: number,
ignoredFolders: Set<string> // Pass ignoredFolders for checking
fullInfo: FullFolderInfo,
maxItems: number,
ignoredFolders: Set<string>, // Pass ignoredFolders for checking
): ReducedFolderNode {
const rootReducedNode: ReducedFolderNode = { name: fullInfo.name, files: [], subFolders: [], isRoot: true };
const queue: Array<{ original: FullFolderInfo; reduced: ReducedFolderNode }> = [];
const rootReducedNode: ReducedFolderNode = {
name: fullInfo.name,
files: [],
subFolders: [],
isRoot: true,
};
const queue: Array<{
original: FullFolderInfo;
reduced: ReducedFolderNode;
}> = [];
// Don't count the root itself towards the limit initially
queue.push({ original: fullInfo, reduced: rootReducedNode });
@@ -160,20 +183,20 @@ function reduceStructure(
while (queue.length > 0) {
const { original: originalFolder, reduced: reducedFolder } = queue.shift()!;
// If the folder being processed was itself marked as ignored (shouldn't happen for root)
if (originalFolder.isIgnored) {
continue;
}
// If the folder being processed was itself marked as ignored (shouldn't happen for root)
if (originalFolder.isIgnored) {
continue;
}
// Process Files
let fileLimitReached = false;
for (const file of originalFolder.files) {
// Check limit *before* adding the file
// Check limit *before* adding the file
if (itemCount >= maxItems) {
if (!fileLimitReached) {
reducedFolder.files.push(TRUNCATION_INDICATOR);
reducedFolder.hasMoreFiles = true;
fileLimitReached = true;
reducedFolder.files.push(TRUNCATION_INDICATOR);
reducedFolder.hasMoreFiles = true;
fileLimitReached = true;
}
break;
}
@@ -184,41 +207,44 @@ function reduceStructure(
// Process Subfolders
let subfolderLimitReached = false;
for (const subFolder of originalFolder.subFolders) {
// Count the folder itself towards the limit
itemCount++;
if (itemCount > maxItems) {
if (!subfolderLimitReached) {
// Add a placeholder node ONLY if we haven't already added one
const truncatedSubfolderNode: ReducedFolderNode = {
name: subFolder.name,
files: [TRUNCATION_INDICATOR], // Generic truncation
subFolders: [],
hasMoreFiles: true,
};
reducedFolder.subFolders.push(truncatedSubfolderNode);
reducedFolder.hasMoreSubfolders = true;
subfolderLimitReached = true;
}
continue; // Stop processing further subfolders for this parent
}
// Handle explicitly ignored folders identified during the read phase
if (subFolder.isIgnored) {
const ignoredReducedNode: ReducedFolderNode = {
name: subFolder.name,
files: [TRUNCATION_INDICATOR], // Indicate contents ignored/truncated
subFolders: [],
hasMoreFiles: true, // Mark as truncated
// Count the folder itself towards the limit
itemCount++;
if (itemCount > maxItems) {
if (!subfolderLimitReached) {
// Add a placeholder node ONLY if we haven't already added one
const truncatedSubfolderNode: ReducedFolderNode = {
name: subFolder.name,
files: [TRUNCATION_INDICATOR], // Generic truncation
subFolders: [],
hasMoreFiles: true,
};
reducedFolder.subFolders.push(ignoredReducedNode);
// DO NOT add the ignored folder to the queue for further processing
}
else {
// If not ignored and within limit, create the reduced node and add to queue
const reducedSubFolder: ReducedFolderNode = { name: subFolder.name, files: [], subFolders: [] };
reducedFolder.subFolders.push(reducedSubFolder);
queue.push({ original: subFolder, reduced: reducedSubFolder });
}
reducedFolder.subFolders.push(truncatedSubfolderNode);
reducedFolder.hasMoreSubfolders = true;
subfolderLimitReached = true;
}
continue; // Stop processing further subfolders for this parent
}
// Handle explicitly ignored folders identified during the read phase
if (subFolder.isIgnored) {
const ignoredReducedNode: ReducedFolderNode = {
name: subFolder.name,
files: [TRUNCATION_INDICATOR], // Indicate contents ignored/truncated
subFolders: [],
hasMoreFiles: true, // Mark as truncated
};
reducedFolder.subFolders.push(ignoredReducedNode);
// DO NOT add the ignored folder to the queue for further processing
} else {
// If not ignored and within limit, create the reduced node and add to queue
const reducedSubFolder: ReducedFolderNode = {
name: subFolder.name,
files: [],
subFolders: [],
};
reducedFolder.subFolders.push(reducedSubFolder);
queue.push({ original: subFolder, reduced: reducedSubFolder });
}
}
}
@@ -227,25 +253,27 @@ function reduceStructure(
/** Calculates the total number of items present in the reduced structure. */
function countReducedItems(node: ReducedFolderNode): number {
let count = 0;
// Count files, treating '...' as one item if present
count += node.files.length;
let count = 0;
// Count files, treating '...' as one item if present
count += node.files.length;
// Count subfolders and recursively count their contents
count += node.subFolders.length;
for (const sub of node.subFolders) {
// Check if it's a placeholder ignored/truncated node
const isTruncatedPlaceholder = (sub.files.length === 1 && sub.files[0] === TRUNCATION_INDICATOR && sub.subFolders.length === 0);
// Count subfolders and recursively count their contents
count += node.subFolders.length;
for (const sub of node.subFolders) {
// Check if it's a placeholder ignored/truncated node
const isTruncatedPlaceholder =
sub.files.length === 1 &&
sub.files[0] === TRUNCATION_INDICATOR &&
sub.subFolders.length === 0;
if (!isTruncatedPlaceholder) {
count += countReducedItems(sub);
}
// Don't add count for items *inside* the placeholder node itself.
if (!isTruncatedPlaceholder) {
count += countReducedItems(sub);
}
return count;
// Don't add count for items *inside* the placeholder node itself.
}
return count;
}
/**
* Formats the reduced folder structure into a tree-like string.
* (No changes needed in this function)
@@ -258,23 +286,23 @@ function formatReducedStructure(
node: ReducedFolderNode,
indent: string,
isLast: boolean,
builder: string[]
builder: string[],
): void {
const connector = isLast ? "└───" : "├───";
const connector = isLast ? '└───' : '├───';
const linePrefix = indent + connector;
// Don't print the root node's name directly, only its contents
if (!node.isRoot) {
builder.push(`${linePrefix}${node.name}/`);
builder.push(`${linePrefix}${node.name}/`);
}
const childIndent = indent + (isLast || node.isRoot ? " " : ""); // Use " " if last, "│" otherwise
const childIndent = indent + (isLast || node.isRoot ? ' ' : ''); // Use " " if last, "│" otherwise
// Render files
const fileCount = node.files.length;
for (let i = 0; i < fileCount; i++) {
const isLastFile = i === fileCount - 1 && node.subFolders.length === 0;
const fileConnector = isLastFile ? "└───" : "├───";
const fileConnector = isLastFile ? '└───' : '├───';
builder.push(`${childIndent}${fileConnector}${node.files[i]}`);
}
@@ -299,7 +327,7 @@ function formatReducedStructure(
*/
export async function getFolderStructure(
directory: string,
options?: FolderStructureOptions
options?: FolderStructureOptions,
): Promise<string> {
const resolvedPath = path.resolve(directory);
const mergedOptions: MergedFolderStructureOptions = {
@@ -317,31 +345,38 @@ export async function getFolderStructure(
}
// 2. Reduce the structure (handles ignored folders specifically)
const reducedRoot = reduceStructure(fullInfo, mergedOptions.maxItems, mergedOptions.ignoredFolders);
const reducedRoot = reduceStructure(
fullInfo,
mergedOptions.maxItems,
mergedOptions.ignoredFolders,
);
// 3. Count items in the *reduced* structure for the summary
const rootNodeItselfCount = 0; // Don't count the root node in the items summary
const reducedItemCount = countReducedItems(reducedRoot) - rootNodeItselfCount;
const reducedItemCount =
countReducedItems(reducedRoot) - rootNodeItselfCount;
// 4. Format the reduced structure into a string
const structureLines: string[] = [];
formatReducedStructure(reducedRoot, "", true, structureLines);
formatReducedStructure(reducedRoot, '', true, structureLines);
// 5. Build the final output string
const displayPath = resolvedPath.replace(/\\/g, '/');
const totalOriginalChildren = fullInfo.totalChildren;
let disclaimer = "";
// Check if any truncation happened OR if ignored folders were present
if (reducedItemCount < totalOriginalChildren || fullInfo.subFolders.some(sf => sf.isIgnored)) {
disclaimer = `Folders or files indicated with ${TRUNCATION_INDICATOR} contain more items not shown or were ignored.`;
let disclaimer = '';
// Check if any truncation happened OR if ignored folders were present
if (
reducedItemCount < totalOriginalChildren ||
fullInfo.subFolders.some((sf) => sf.isIgnored)
) {
disclaimer = `Folders or files indicated with ${TRUNCATION_INDICATOR} contain more items not shown or were ignored.`;
}
const summary = `Showing ${reducedItemCount} of ${totalOriginalChildren} items (files + folders). ${disclaimer}`.trim();
const summary =
`Showing ${reducedItemCount} of ${totalOriginalChildren} items (files + folders). ${disclaimer}`.trim();
return `${summary}\n\n${displayPath}/\n${structureLines.join('\n')}`;
} catch (error: any) {
console.error(`Error getting folder structure for ${resolvedPath}:`, error);
return `Error processing directory "${resolvedPath}": ${error.message}`;

View File

@@ -5,7 +5,7 @@ import path from 'node:path'; // Import the 'path' module
* Returns the target directory, using the provided argument or the current working directory.
*/
export function getTargetDirectory(targetDirArg: string | undefined): string {
return targetDirArg || process.cwd();
return targetDirArg || process.cwd();
}
/**
@@ -13,73 +13,72 @@ export function getTargetDirectory(targetDirArg: string | undefined): string {
* Example: /path/to/a/very/long/file.txt -> /path/.../long/file.txt
*/
export function shortenPath(filePath: string, maxLen: number = 35): string {
if (filePath.length <= maxLen) {
return filePath;
if (filePath.length <= maxLen) {
return filePath;
}
const parsedPath = path.parse(filePath);
const root = parsedPath.root;
const separator = path.sep;
// Get segments of the path *after* the root
const relativePath = filePath.substring(root.length);
const segments = relativePath.split(separator).filter((s) => s !== ''); // Filter out empty segments
// Handle cases with no segments after root (e.g., "/", "C:\") or only one segment
if (segments.length <= 1) {
// Fallback to simple start/end truncation for very short paths or single segments
const keepLen = Math.floor((maxLen - 3) / 2);
// Ensure keepLen is not negative if maxLen is very small
if (keepLen <= 0) {
return filePath.substring(0, maxLen - 3) + '...';
}
const start = filePath.substring(0, keepLen);
const end = filePath.substring(filePath.length - keepLen);
return `${start}...${end}`;
}
const parsedPath = path.parse(filePath);
const root = parsedPath.root;
const separator = path.sep;
const firstDir = segments[0];
const startComponent = root + firstDir;
// Get segments of the path *after* the root
const relativePath = filePath.substring(root.length);
const segments = relativePath.split(separator).filter(s => s !== ''); // Filter out empty segments
const endPartSegments: string[] = [];
// Base length: startComponent + separator + "..."
let currentLength = startComponent.length + separator.length + 3;
// Handle cases with no segments after root (e.g., "/", "C:\") or only one segment
if (segments.length <= 1) {
// Fallback to simple start/end truncation for very short paths or single segments
const keepLen = Math.floor((maxLen - 3) / 2);
// Ensure keepLen is not negative if maxLen is very small
if (keepLen <= 0) {
return filePath.substring(0, maxLen - 3) + '...';
}
const start = filePath.substring(0, keepLen);
const end = filePath.substring(filePath.length - keepLen);
return `${start}...${end}`;
// Iterate backwards through segments (excluding the first one)
for (let i = segments.length - 1; i >= 1; i--) {
const segment = segments[i];
// Length needed if we add this segment: current + separator + segment
const lengthWithSegment = currentLength + separator.length + segment.length;
if (lengthWithSegment <= maxLen) {
endPartSegments.unshift(segment); // Add to the beginning of the end part
currentLength = lengthWithSegment;
} else {
// Adding this segment would exceed maxLen
break;
}
}
const firstDir = segments[0];
const startComponent = root + firstDir;
// Construct the final path
let result = startComponent + separator + '...';
if (endPartSegments.length > 0) {
result += separator + endPartSegments.join(separator);
}
const endPartSegments: string[] = [];
// Base length: startComponent + separator + "..."
let currentLength = startComponent.length + separator.length + 3;
// Iterate backwards through segments (excluding the first one)
for (let i = segments.length - 1; i >= 1; i--) {
const segment = segments[i];
// Length needed if we add this segment: current + separator + segment
const lengthWithSegment = currentLength + separator.length + segment.length;
if (lengthWithSegment <= maxLen) {
endPartSegments.unshift(segment); // Add to the beginning of the end part
currentLength = lengthWithSegment;
} else {
// Adding this segment would exceed maxLen
break;
}
// As a final check, if the result is somehow still too long (e.g., startComponent + ... is too long)
// fallback to simple truncation of the original path
if (result.length > maxLen) {
const keepLen = Math.floor((maxLen - 3) / 2);
if (keepLen <= 0) {
return filePath.substring(0, maxLen - 3) + '...';
}
const start = filePath.substring(0, keepLen);
const end = filePath.substring(filePath.length - keepLen);
return `${start}...${end}`;
}
// Construct the final path
let result = startComponent + separator + '...';
if (endPartSegments.length > 0) {
result += separator + endPartSegments.join(separator);
}
// As a final check, if the result is somehow still too long (e.g., startComponent + ... is too long)
// fallback to simple truncation of the original path
if (result.length > maxLen) {
const keepLen = Math.floor((maxLen - 3) / 2);
if (keepLen <= 0) {
return filePath.substring(0, maxLen - 3) + '...';
}
const start = filePath.substring(0, keepLen);
const end = filePath.substring(filePath.length - keepLen);
return `${start}...${end}`;
}
return result;
return result;
}
/**
@@ -91,12 +90,15 @@ export function shortenPath(filePath: string, maxLen: number = 35): string {
* @param rootDirectory The absolute path of the directory to make the target path relative to.
* @returns The relative path from rootDirectory to targetPath.
*/
export function makeRelative(targetPath: string, rootDirectory: string): string {
const resolvedTargetPath = path.resolve(targetPath);
const resolvedRootDirectory = path.resolve(rootDirectory);
export function makeRelative(
targetPath: string,
rootDirectory: string,
): string {
const resolvedTargetPath = path.resolve(targetPath);
const resolvedRootDirectory = path.resolve(rootDirectory);
const relativePath = path.relative(resolvedRootDirectory, resolvedTargetPath);
const relativePath = path.relative(resolvedRootDirectory, resolvedTargetPath);
// If the paths are the same, path.relative returns '', return '.' instead
return relativePath || '.';
// If the paths are the same, path.relative returns '', return '.' instead
return relativePath || '.';
}

View File

@@ -12,12 +12,12 @@ export class SchemaValidator {
static validate(schema: Record<string, unknown>, data: unknown): boolean {
// This is a simplified implementation
// In a real application, you would use a library like Ajv for proper validation
// Check for required fields
if (schema.required && Array.isArray(schema.required)) {
const required = schema.required as string[];
const dataObj = data as Record<string, unknown>;
for (const field of required) {
if (dataObj[field] === undefined) {
console.error(`Missing required field: ${field}`);
@@ -25,25 +25,29 @@ export class SchemaValidator {
}
}
}
// Check property types if properties are defined
if (schema.properties && typeof schema.properties === 'object') {
const properties = schema.properties as Record<string, { type?: string }>;
const dataObj = data as Record<string, unknown>;
for (const [key, prop] of Object.entries(properties)) {
if (dataObj[key] !== undefined && prop.type) {
const expectedType = prop.type;
const actualType = Array.isArray(dataObj[key]) ? 'array' : typeof dataObj[key];
const actualType = Array.isArray(dataObj[key])
? 'array'
: typeof dataObj[key];
if (expectedType !== actualType) {
console.error(`Type mismatch for property "${key}": expected ${expectedType}, got ${actualType}`);
console.error(
`Type mismatch for property "${key}": expected ${expectedType}, got ${actualType}`,
);
return false;
}
}
}
}
return true;
}
}
}