fix txt imports

This commit is contained in:
dal 2025-10-02 16:07:09 -06:00
parent dec22b5bf8
commit 5641441c18
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
4 changed files with 70 additions and 84 deletions

View File

@ -1,4 +1,3 @@
import { $ } from 'bun';
import type { BashToolContext, BashToolInput, BashToolOutput } from './bash-tool'; import type { BashToolContext, BashToolInput, BashToolOutput } from './bash-tool';
const MAX_OUTPUT_LENGTH = 30_000; const MAX_OUTPUT_LENGTH = 30_000;
@ -6,22 +5,7 @@ const DEFAULT_TIMEOUT = 2 * 60 * 1000; // 2 minutes
const MAX_TIMEOUT = 10 * 60 * 1000; // 10 minutes const MAX_TIMEOUT = 10 * 60 * 1000; // 10 minutes
/** /**
* Creates a timeout promise that rejects after the specified time * Executes a bash command using Bun.spawn with timeout support
*/
function createTimeoutPromise(timeout: number, command: string): Promise<never> {
return new Promise((_, reject) => {
setTimeout(() => {
reject(
new Error(
`Command timed out after ${timeout}ms: ${command.length > 50 ? `${command.substring(0, 50)}...` : command}`
)
);
}, timeout);
});
}
/**
* Executes a bash command using Bun's $ shell with timeout support
* @param command - The bash command to execute * @param command - The bash command to execute
* @param timeout - Timeout in milliseconds * @param timeout - Timeout in milliseconds
* @param projectDirectory - The project directory to execute the command in * @param projectDirectory - The project directory to execute the command in
@ -33,57 +17,82 @@ async function executeCommand(
projectDirectory: string projectDirectory: string
): Promise<BashToolOutput> { ): Promise<BashToolOutput> {
try { try {
// Execute command with timeout using Promise.race // Create abort controller for timeout
const proc = await Promise.race([ const controller = new AbortController();
$`${command}`.cwd(projectDirectory).nothrow().quiet(), const timeoutId = setTimeout(() => controller.abort(), timeout);
createTimeoutPromise(timeout, command),
]);
let stdout = proc.stdout?.toString() || ''; // Execute command using Bun.spawn
let stderr = proc.stderr?.toString() || ''; const proc = Bun.spawn(['bash', '-c', command], {
cwd: projectDirectory,
stdout: 'pipe',
stderr: 'pipe',
stdin: 'ignore',
});
// Truncate output if it exceeds max length let stdout = '';
if (stdout.length > MAX_OUTPUT_LENGTH) { let stderr = '';
stdout = stdout.slice(0, MAX_OUTPUT_LENGTH);
stdout += '\n\n(Output was truncated due to length limit)';
}
if (stderr.length > MAX_OUTPUT_LENGTH) { try {
stderr = stderr.slice(0, MAX_OUTPUT_LENGTH); // Read stdout and stderr
stderr += '\n\n(Error output was truncated due to length limit)'; const [stdoutText, stderrText] = await Promise.all([
} new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
const exitCode = proc.exitCode ?? 0; stdout = stdoutText;
const success = exitCode === 0; stderr = stderrText;
const result: BashToolOutput = { // Wait for process to exit
command, const exitCode = await proc.exited;
stdout, clearTimeout(timeoutId);
stderr,
exitCode,
success,
};
// Only add error property if there is an error // Truncate output if it exceeds max length
if (!success) { if (stdout.length > MAX_OUTPUT_LENGTH) {
result.error = stderr || `Command failed with exit code ${exitCode}`; stdout = stdout.slice(0, MAX_OUTPUT_LENGTH);
} stdout += '\n\n(Output was truncated due to length limit)';
}
return result; if (stderr.length > MAX_OUTPUT_LENGTH) {
} catch (error) { stderr = stderr.slice(0, MAX_OUTPUT_LENGTH);
// Check if it was a timeout stderr += '\n\n(Error output was truncated due to length limit)';
if (error instanceof Error && error.message.includes('timed out')) { }
return {
const success = exitCode === 0;
const result: BashToolOutput = {
command, command,
stdout: '', stdout,
stderr: `Command timed out after ${timeout}ms`, stderr,
exitCode: 124, // Standard timeout exit code exitCode,
success: false, success,
error: `Command timed out after ${timeout}ms`,
}; };
}
// Handle other execution errors // Only add error property if there is an error
if (!success) {
result.error = stderr || `Command failed with exit code ${exitCode}`;
}
return result;
} catch (readError) {
clearTimeout(timeoutId);
proc.kill();
// Check if it was aborted (timeout)
if (controller.signal.aborted) {
return {
command,
stdout: '',
stderr: `Command timed out after ${timeout}ms`,
exitCode: 124, // Standard timeout exit code
success: false,
error: `Command timed out after ${timeout}ms`,
};
}
throw readError;
}
} catch (error) {
// Handle execution errors
const errorMessage = error instanceof Error ? error.message : 'Unknown error'; const errorMessage = error instanceof Error ? error.message : 'Unknown error';
return { return {

View File

@ -1,19 +1,7 @@
// The approaches in this edit tool are sourced from
// https://github.com/cline/cline/blob/main/evals/diff-edits/diff-apply/diff-06-23-25.ts
// https://github.com/google-gemini/gemini-cli/blob/main/packages/core/src/utils/editCorrector.ts
// https://github.com/cline/cline/blob/main/evals/diff-edits/diff-apply/diff-06-26-25.ts
import { tool } from 'ai'; import { tool } from 'ai';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { z } from 'zod'; import { z } from 'zod';
import { createEditFileToolExecute } from './edit-file-tool-execute'; import { createEditFileToolExecute } from './edit-file-tool-execute';
import DESCRIPTION from './edit.txt';
// Read description from file
const DESCRIPTION = readFileSync(
path.join(__dirname, 'edit.txt'),
'utf-8'
);
export const EditFileToolInputSchema = z.object({ export const EditFileToolInputSchema = z.object({
filePath: z.string().describe('The absolute path to the file to modify'), filePath: z.string().describe('The absolute path to the file to modify'),

View File

@ -1,11 +1,7 @@
import { tool } from 'ai'; import { tool } from 'ai';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { z } from 'zod'; import { z } from 'zod';
import { createLsToolExecute } from './ls-tool-execute'; import { createLsToolExecute } from './ls-tool-execute';
import DESCRIPTION from './ls.txt';
// Read description from file
const DESCRIPTION = readFileSync(path.join(__dirname, 'ls.txt'), 'utf-8');
export const LsToolInputSchema = z.object({ export const LsToolInputSchema = z.object({
path: z path: z

View File

@ -1,14 +1,7 @@
import { tool } from 'ai'; import { tool } from 'ai';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { z } from 'zod'; import { z } from 'zod';
import { createMultiEditFileToolExecute } from './multi-edit-file-tool-execute'; import { createMultiEditFileToolExecute } from './multi-edit-file-tool-execute';
import DESCRIPTION from './multiedit.txt';
// Read description from file
const DESCRIPTION = readFileSync(
path.join(__dirname, 'multiedit.txt'),
'utf-8'
);
const EditOperationSchema = z.object({ const EditOperationSchema = z.object({
oldString: z.string().describe('The text to replace'), oldString: z.string().describe('The text to replace'),