hataraku
Version:
An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.
142 lines • 5.92 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeFileTool = void 0;
const zod_1 = require("zod");
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
// Helper to check if file exists
async function fileExistsAtPath(filePath) {
try {
await fs.access(filePath);
return true;
}
catch {
return false;
}
}
// Helper to preprocess content
function preprocessContent(content) {
let processedContent = content;
// Remove markdown code block markers if present
if (processedContent.startsWith("```")) {
processedContent = processedContent.split("\n").slice(1).join("\n").trim();
}
if (processedContent.endsWith("```")) {
processedContent = processedContent.split("\n").slice(0, -1).join("\n").trim();
}
// Fix common HTML entities
processedContent = processedContent
.replace(/>/g, ">")
.replace(/</g, "<")
.replace(/"/g, '"');
return processedContent;
}
// Helper to detect code omissions
function detectCodeOmission(content, predictedLineCount) {
const actualLineCount = content.split("\n").length;
if (predictedLineCount !== 0 && actualLineCount !== predictedLineCount) {
return true;
}
// Check for common code omission indicators
const omissionIndicators = [
'// rest of code unchanged',
'/* previous code */',
'// ... rest of the code ...',
'// ... existing code ...',
'/* ... */',
];
return omissionIndicators.some(indicator => content.includes(indicator));
}
exports.writeFileTool = {
description: "Write content to a file at the specified path. Creates directories if they don't exist. If the file exists, it will be overwritten.",
parameters: zod_1.z.object({
path: zod_1.z.string().describe('The path of the file to write to (relative to the current working directory)'),
content: zod_1.z.string().describe('The content to write to the file. ALWAYS provide the COMPLETE intended content of the file.'),
line_count: zod_1.z.number().int().min(0).describe('The number of lines in the file')
}),
execute: async ({ path: filePath, content, line_count }) => {
try {
const absolutePath = path.resolve(process.cwd(), filePath);
// Check if file exists
const fileExists = await fileExistsAtPath(absolutePath);
// Preprocess content
const processedContent = preprocessContent(content);
// Validate content and check for omissions
if (detectCodeOmission(processedContent, line_count)) {
return {
isError: true,
content: [{
type: "text",
text: `Content appears to be truncated or contains omission indicators. File has ${processedContent.split("\n").length} lines but was predicted to have ${line_count} lines. Please provide complete file content without omissions.`
}]
};
}
// Validate line count matches content
const actualLineCount = processedContent.split("\n").length;
if (line_count !== 0 && actualLineCount !== line_count) {
return {
isError: true,
content: [{
type: "text",
text: `Line count mismatch: expected ${line_count} but content has ${actualLineCount} lines`
}]
};
}
// Create directories if they don't exist
await fs.mkdir(path.dirname(absolutePath), { recursive: true });
// Write the file
await fs.writeFile(absolutePath, processedContent, 'utf-8');
const action = fileExists ? 'updated' : 'created';
return {
content: [{
type: "text",
text: `File successfully ${action} at ${filePath}`
}]
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
isError: true,
content: [{
type: "text",
text: `Error writing file: ${errorMessage}`
}]
};
}
}
};
//# sourceMappingURL=write-file.js.map