hataraku
Version:
An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.
119 lines • 5 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.insertContentTool = void 0;
const zod_1 = require("zod");
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
// Helper function to insert groups of lines at specified indices
function insertGroups(lines, groups) {
// Sort groups by index in descending order to avoid affecting subsequent insertions
const sortedGroups = [...groups].sort((a, b) => b.index - a.index);
// Create a copy of the lines array
let result = [...lines];
// Insert each group
for (const { index, elements } of sortedGroups) {
// Ensure index is within bounds
const insertAt = Math.max(0, Math.min(index, result.length));
result.splice(insertAt, 0, ...elements);
}
return result;
}
exports.insertContentTool = {
description: "Insert content at specific line numbers in a file. Multiple insertions can be performed in a single operation.",
parameters: zod_1.z.object({
path: zod_1.z.string().describe('The path of the file to modify'),
operations: zod_1.z.array(zod_1.z.object({
start_line: zod_1.z.number().min(1).describe('The line number where content should be inserted (1-based)'),
content: zod_1.z.string().describe('The content to insert at the specified line')
})).describe('Array of insert operations, each specifying where to insert content')
}),
execute: async ({ path: filePath, operations }) => {
try {
const absolutePath = path.resolve(process.cwd(), filePath);
// Check if file exists
try {
await fs.access(absolutePath);
}
catch {
return {
isError: true,
content: [{
type: "text",
text: `File not found at path: ${filePath}`
}]
};
}
// Read the file
const fileContent = await fs.readFile(absolutePath, 'utf8');
const lines = fileContent.split('\n');
// Prepare operations for insertion
const insertOperations = operations.map((op) => ({
index: op.start_line - 1, // Convert to 0-based index
elements: op.content.split('\n')
}));
// Perform insertions
const updatedLines = insertGroups(lines, insertOperations);
const updatedContent = updatedLines.join('\n');
// Check if any changes were made
if (updatedContent === fileContent) {
return {
content: [{
type: "text",
text: `No changes needed for '${filePath}'`
}]
};
}
// Write the modified content back to the file
await fs.writeFile(absolutePath, updatedContent, 'utf-8');
return {
content: [{
type: "text",
text: `Successfully inserted content at ${operations.length} location(s) in ${filePath}`
}]
};
}
catch (error) {
return {
isError: true,
content: [{
type: "text",
text: `Error inserting content: ${error instanceof Error ? error.message : String(error)}`
}]
};
}
}
};
//# sourceMappingURL=insert-content.js.map