@thejeetsingh/kalcode 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -4
- package/dist/bin/kalcode.d.ts +2 -0
- package/dist/bin/kalcode.js +12 -0
- package/dist/bin/kalcode.js.map +1 -0
- package/dist/src/agent/context.d.ts +6 -0
- package/dist/src/agent/context.js +60 -0
- package/dist/src/agent/context.js.map +1 -0
- package/dist/src/agent/history.d.ts +8 -0
- package/dist/src/agent/history.js +59 -0
- package/dist/src/agent/history.js.map +1 -0
- package/dist/src/agent/loop.d.ts +6 -0
- package/dist/src/agent/loop.js +235 -0
- package/dist/src/agent/loop.js.map +1 -0
- package/dist/src/agent/memory.d.ts +2 -0
- package/dist/src/agent/memory.js +27 -0
- package/dist/src/agent/memory.js.map +1 -0
- package/dist/src/agent/permissions.d.ts +5 -0
- package/dist/src/agent/permissions.js +66 -0
- package/dist/src/agent/permissions.js.map +1 -0
- package/dist/src/agent/text-tool-parser.d.ts +2 -0
- package/dist/src/agent/text-tool-parser.js +68 -0
- package/dist/src/agent/text-tool-parser.js.map +1 -0
- package/dist/src/api/client.d.ts +2 -0
- package/dist/src/api/client.js +86 -0
- package/dist/src/api/client.js.map +1 -0
- package/dist/src/api/stream-parser.d.ts +2 -0
- package/dist/src/api/stream-parser.js +97 -0
- package/dist/src/api/stream-parser.js.map +1 -0
- package/dist/src/config.d.ts +7 -0
- package/dist/src/config.js +52 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/constants.d.ts +25 -0
- package/{src/constants.ts → dist/src/constants.js} +17 -19
- package/dist/src/constants.js.map +1 -0
- package/dist/src/git/git.d.ts +15 -0
- package/dist/src/git/git.js +73 -0
- package/dist/src/git/git.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +415 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/proxy/server.d.ts +1 -0
- package/dist/src/proxy/server.js +92 -0
- package/dist/src/proxy/server.js.map +1 -0
- package/dist/src/tools/edit-file.d.ts +2 -0
- package/dist/src/tools/edit-file.js +88 -0
- package/dist/src/tools/edit-file.js.map +1 -0
- package/dist/src/tools/glob-tool.d.ts +2 -0
- package/dist/src/tools/glob-tool.js +52 -0
- package/dist/src/tools/glob-tool.js.map +1 -0
- package/dist/src/tools/grep.d.ts +2 -0
- package/dist/src/tools/grep.js +93 -0
- package/dist/src/tools/grep.js.map +1 -0
- package/dist/src/tools/list-directory.d.ts +2 -0
- package/dist/src/tools/list-directory.js +90 -0
- package/dist/src/tools/list-directory.js.map +1 -0
- package/dist/src/tools/read-file.d.ts +2 -0
- package/dist/src/tools/read-file.js +64 -0
- package/dist/src/tools/read-file.js.map +1 -0
- package/dist/src/tools/registry.d.ts +4 -0
- package/dist/src/tools/registry.js +32 -0
- package/dist/src/tools/registry.js.map +1 -0
- package/dist/src/tools/run-command.d.ts +2 -0
- package/dist/src/tools/run-command.js +98 -0
- package/dist/src/tools/run-command.js.map +1 -0
- package/dist/src/tools/write-file.d.ts +2 -0
- package/dist/src/tools/write-file.js +39 -0
- package/dist/src/tools/write-file.js.map +1 -0
- package/dist/src/types.d.ts +61 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/ui/input.d.ts +5 -0
- package/dist/src/ui/input.js +52 -0
- package/dist/src/ui/input.js.map +1 -0
- package/dist/src/ui/model-picker.d.ts +7 -0
- package/dist/src/ui/model-picker.js +70 -0
- package/dist/src/ui/model-picker.js.map +1 -0
- package/dist/src/ui/skills-picker.d.ts +2 -0
- package/dist/src/ui/skills-picker.js +95 -0
- package/dist/src/ui/skills-picker.js.map +1 -0
- package/dist/src/ui/skills.d.ts +14 -0
- package/dist/src/ui/skills.js +137 -0
- package/dist/src/ui/skills.js.map +1 -0
- package/dist/src/ui/spinner.d.ts +5 -0
- package/dist/src/ui/spinner.js +49 -0
- package/dist/src/ui/spinner.js.map +1 -0
- package/dist/src/ui/stream-renderer.d.ts +2 -0
- package/dist/src/ui/stream-renderer.js +66 -0
- package/dist/src/ui/stream-renderer.js.map +1 -0
- package/dist/src/ui/terminal.d.ts +24 -0
- package/dist/src/ui/terminal.js +272 -0
- package/dist/src/ui/terminal.js.map +1 -0
- package/package.json +16 -16
- package/api/health.ts +0 -10
- package/api/v1/chat/completions.ts +0 -59
- package/bin/kalcode.ts +0 -14
- package/src/agent/context.ts +0 -62
- package/src/agent/history.ts +0 -70
- package/src/agent/loop.ts +0 -282
- package/src/agent/memory.ts +0 -26
- package/src/agent/permissions.ts +0 -84
- package/src/agent/text-tool-parser.ts +0 -71
- package/src/api/client.ts +0 -110
- package/src/api/stream-parser.ts +0 -109
- package/src/config.ts +0 -61
- package/src/git/git.ts +0 -86
- package/src/index.ts +0 -403
- package/src/proxy/server.ts +0 -128
- package/src/tools/edit-file.ts +0 -97
- package/src/tools/glob-tool.ts +0 -59
- package/src/tools/grep.ts +0 -96
- package/src/tools/list-directory.ts +0 -101
- package/src/tools/read-file.ts +0 -71
- package/src/tools/registry.ts +0 -41
- package/src/tools/run-command.ts +0 -99
- package/src/tools/write-file.ts +0 -42
- package/src/types.ts +0 -68
- package/src/ui/input.ts +0 -60
- package/src/ui/model-picker.ts +0 -92
- package/src/ui/skills-picker.ts +0 -113
- package/src/ui/skills.ts +0 -152
- package/src/ui/spinner.ts +0 -56
- package/src/ui/stream-renderer.ts +0 -69
- package/src/ui/terminal.ts +0 -337
- package/tsconfig.json +0 -15
- package/vercel.json +0 -12
package/src/tools/edit-file.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync, existsSync } from "fs";
|
|
2
|
-
import { resolve } from "path";
|
|
3
|
-
import type { ToolHandler } from "../types.js";
|
|
4
|
-
|
|
5
|
-
export const editFileTool: ToolHandler = {
|
|
6
|
-
definition: {
|
|
7
|
-
type: "function",
|
|
8
|
-
function: {
|
|
9
|
-
name: "editFile",
|
|
10
|
-
description:
|
|
11
|
-
"Make surgical edits to a file using search/replace pairs. Each edit finds an exact text match and replaces it. Read the file first to get the exact text to search for.",
|
|
12
|
-
parameters: {
|
|
13
|
-
type: "object",
|
|
14
|
-
properties: {
|
|
15
|
-
filePath: {
|
|
16
|
-
type: "string",
|
|
17
|
-
description: "Path to the file to edit",
|
|
18
|
-
},
|
|
19
|
-
edits: {
|
|
20
|
-
type: "array",
|
|
21
|
-
description: "Array of search/replace edit operations",
|
|
22
|
-
items: {
|
|
23
|
-
type: "object",
|
|
24
|
-
properties: {
|
|
25
|
-
search: {
|
|
26
|
-
type: "string",
|
|
27
|
-
description: "Exact text to search for in the file",
|
|
28
|
-
},
|
|
29
|
-
replace: {
|
|
30
|
-
type: "string",
|
|
31
|
-
description: "Text to replace the search text with",
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
required: ["search", "replace"],
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
required: ["filePath", "edits"],
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
async execute(args) {
|
|
44
|
-
const filePath = resolve(String(args.filePath));
|
|
45
|
-
const edits = args.edits as Array<{ search: string; replace: string }>;
|
|
46
|
-
|
|
47
|
-
if (!existsSync(filePath)) {
|
|
48
|
-
return `Error: File not found: ${filePath}`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (!Array.isArray(edits) || edits.length === 0) {
|
|
52
|
-
return "Error: edits must be a non-empty array of {search, replace} pairs";
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
let content = readFileSync(filePath, "utf-8");
|
|
57
|
-
const results: string[] = [];
|
|
58
|
-
|
|
59
|
-
for (let i = 0; i < edits.length; i++) {
|
|
60
|
-
const edit = edits[i]!;
|
|
61
|
-
const search = String(edit.search);
|
|
62
|
-
const replace = String(edit.replace);
|
|
63
|
-
|
|
64
|
-
let idx = content.indexOf(search);
|
|
65
|
-
// Try trimming trailing whitespace per line if exact match fails
|
|
66
|
-
if (idx === -1) {
|
|
67
|
-
const searchNorm = search.split("\n").map(l => l.trimEnd()).join("\n");
|
|
68
|
-
const contentNorm = content.split("\n").map(l => l.trimEnd()).join("\n");
|
|
69
|
-
idx = contentNorm.indexOf(searchNorm);
|
|
70
|
-
if (idx !== -1) {
|
|
71
|
-
// Find the real position accounting for trailing whitespace diffs
|
|
72
|
-
const before = contentNorm.slice(0, idx);
|
|
73
|
-
const linesBefore = before.split("\n").length - 1;
|
|
74
|
-
const linesInSearch = searchNorm.split("\n").length;
|
|
75
|
-
const contentLines = content.split("\n");
|
|
76
|
-
const realBefore = contentLines.slice(0, linesBefore).join("\n") + (linesBefore > 0 ? "\n" : "");
|
|
77
|
-
const realMatch = contentLines.slice(linesBefore, linesBefore + linesInSearch).join("\n");
|
|
78
|
-
content = realBefore + replace + content.slice(realBefore.length + realMatch.length);
|
|
79
|
-
results.push(`Edit ${i + 1}: OK (fuzzy whitespace match)`);
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
results.push(`Edit ${i + 1}: FAILED - search text not found`);
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Only replace first occurrence
|
|
87
|
-
content = content.slice(0, idx) + replace + content.slice(idx + search.length);
|
|
88
|
-
results.push(`Edit ${i + 1}: OK`);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
writeFileSync(filePath, content);
|
|
92
|
-
return `Edited ${filePath}:\n${results.join("\n")}`;
|
|
93
|
-
} catch (err: unknown) {
|
|
94
|
-
return `Error editing file: ${err instanceof Error ? err.message : String(err)}`;
|
|
95
|
-
}
|
|
96
|
-
},
|
|
97
|
-
};
|
package/src/tools/glob-tool.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { resolve } from "path";
|
|
2
|
-
import type { ToolHandler } from "../types.js";
|
|
3
|
-
import { MAX_GLOB_RESULTS } from "../constants.js";
|
|
4
|
-
|
|
5
|
-
export const globTool: ToolHandler = {
|
|
6
|
-
definition: {
|
|
7
|
-
type: "function",
|
|
8
|
-
function: {
|
|
9
|
-
name: "glob",
|
|
10
|
-
description:
|
|
11
|
-
"Find files matching a glob pattern. Returns matching file paths sorted by modification time.",
|
|
12
|
-
parameters: {
|
|
13
|
-
type: "object",
|
|
14
|
-
properties: {
|
|
15
|
-
pattern: {
|
|
16
|
-
type: "string",
|
|
17
|
-
description: "Glob pattern (e.g., '**/*.ts', 'src/**/*.json')",
|
|
18
|
-
},
|
|
19
|
-
path: {
|
|
20
|
-
type: "string",
|
|
21
|
-
description: "Base directory to search from. Default: current directory",
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
required: ["pattern"],
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
async execute(args) {
|
|
30
|
-
const pattern = String(args.pattern);
|
|
31
|
-
const basePath = resolve(String(args.path || "."));
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
const glob = new Bun.Glob(pattern);
|
|
35
|
-
const matches: string[] = [];
|
|
36
|
-
|
|
37
|
-
for await (const entry of glob.scan({ cwd: basePath, dot: false })) {
|
|
38
|
-
// Skip common noise directories
|
|
39
|
-
if (entry.startsWith("node_modules/") || entry.startsWith(".git/")) continue;
|
|
40
|
-
matches.push(entry);
|
|
41
|
-
if (matches.length >= MAX_GLOB_RESULTS) break;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (matches.length === 0) {
|
|
45
|
-
return `No files matching pattern: ${pattern} in ${basePath}`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
matches.sort();
|
|
49
|
-
let result = `Found ${matches.length} file(s) matching "${pattern}":\n`;
|
|
50
|
-
result += matches.join("\n");
|
|
51
|
-
if (matches.length >= MAX_GLOB_RESULTS) {
|
|
52
|
-
result += `\n... (results limited to ${MAX_GLOB_RESULTS})`;
|
|
53
|
-
}
|
|
54
|
-
return result;
|
|
55
|
-
} catch (err: unknown) {
|
|
56
|
-
return `Error searching for files: ${err instanceof Error ? err.message : String(err)}`;
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
};
|
package/src/tools/grep.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type { ToolHandler } from "../types.js";
|
|
2
|
-
import { MAX_GREP_RESULTS } from "../constants.js";
|
|
3
|
-
|
|
4
|
-
export const grepTool: ToolHandler = {
|
|
5
|
-
definition: {
|
|
6
|
-
type: "function",
|
|
7
|
-
function: {
|
|
8
|
-
name: "grep",
|
|
9
|
-
description:
|
|
10
|
-
"Search file contents for a pattern. Uses ripgrep if available, otherwise falls back to grep. Returns matching lines with file paths and line numbers.",
|
|
11
|
-
parameters: {
|
|
12
|
-
type: "object",
|
|
13
|
-
properties: {
|
|
14
|
-
pattern: {
|
|
15
|
-
type: "string",
|
|
16
|
-
description: "Search pattern (regex supported)",
|
|
17
|
-
},
|
|
18
|
-
path: {
|
|
19
|
-
type: "string",
|
|
20
|
-
description: "Directory or file to search in. Default: current directory",
|
|
21
|
-
},
|
|
22
|
-
include: {
|
|
23
|
-
type: "string",
|
|
24
|
-
description: "File glob pattern to include (e.g., '*.ts')",
|
|
25
|
-
},
|
|
26
|
-
maxResults: {
|
|
27
|
-
type: "number",
|
|
28
|
-
description: `Maximum number of results. Default: ${MAX_GREP_RESULTS}`,
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
required: ["pattern"],
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
|
|
36
|
-
async execute(args) {
|
|
37
|
-
const pattern = String(args.pattern);
|
|
38
|
-
const searchPath = String(args.path || ".");
|
|
39
|
-
const include = args.include ? String(args.include) : null;
|
|
40
|
-
const maxResults = Number(args.maxResults) || MAX_GREP_RESULTS;
|
|
41
|
-
|
|
42
|
-
// Try ripgrep first, fall back to grep
|
|
43
|
-
const hasRg = await checkCommand("rg");
|
|
44
|
-
|
|
45
|
-
let cmd: string;
|
|
46
|
-
if (hasRg) {
|
|
47
|
-
cmd = `rg --line-number --max-count ${maxResults} --no-heading --glob '!node_modules' --glob '!.git'`;
|
|
48
|
-
if (include) cmd += ` --glob '${include}'`;
|
|
49
|
-
cmd += ` -- ${escapeShellArg(pattern)} ${escapeShellArg(searchPath)}`;
|
|
50
|
-
} else {
|
|
51
|
-
cmd = `grep -rn --max-count=${maxResults} --exclude-dir=node_modules --exclude-dir=.git`;
|
|
52
|
-
if (include) cmd += ` --include='${include}'`;
|
|
53
|
-
cmd += ` -- ${escapeShellArg(pattern)} ${escapeShellArg(searchPath)}`;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
try {
|
|
57
|
-
const proc = Bun.spawn(["sh", "-c", cmd], {
|
|
58
|
-
stdout: "pipe",
|
|
59
|
-
stderr: "pipe",
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
const stdout = await new Response(proc.stdout).text();
|
|
63
|
-
const stderr = await new Response(proc.stderr).text();
|
|
64
|
-
await proc.exited;
|
|
65
|
-
|
|
66
|
-
if (!stdout.trim()) {
|
|
67
|
-
return `No matches found for pattern: ${pattern}`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Limit results
|
|
71
|
-
const lines = stdout.trim().split("\n");
|
|
72
|
-
const limited = lines.slice(0, maxResults);
|
|
73
|
-
let result = limited.join("\n");
|
|
74
|
-
if (lines.length > maxResults) {
|
|
75
|
-
result += `\n... (${lines.length - maxResults} more matches)`;
|
|
76
|
-
}
|
|
77
|
-
return result;
|
|
78
|
-
} catch (err: unknown) {
|
|
79
|
-
return `Error searching: ${err instanceof Error ? err.message : String(err)}`;
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
async function checkCommand(cmd: string): Promise<boolean> {
|
|
85
|
-
try {
|
|
86
|
-
const proc = Bun.spawn(["which", cmd], { stdout: "pipe", stderr: "pipe" });
|
|
87
|
-
await proc.exited;
|
|
88
|
-
return proc.exitCode === 0;
|
|
89
|
-
} catch {
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function escapeShellArg(arg: string): string {
|
|
95
|
-
return `'${arg.replace(/'/g, "'\\''")}'`;
|
|
96
|
-
}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { readdirSync, statSync, existsSync } from "fs";
|
|
2
|
-
import { resolve, join } from "path";
|
|
3
|
-
import type { ToolHandler } from "../types.js";
|
|
4
|
-
import { MAX_DIR_ENTRIES, MAX_DIR_DEPTH } from "../constants.js";
|
|
5
|
-
|
|
6
|
-
export const listDirectoryTool: ToolHandler = {
|
|
7
|
-
definition: {
|
|
8
|
-
type: "function",
|
|
9
|
-
function: {
|
|
10
|
-
name: "listDirectory",
|
|
11
|
-
description:
|
|
12
|
-
"List the contents of a directory. Shows file names, types, and sizes.",
|
|
13
|
-
parameters: {
|
|
14
|
-
type: "object",
|
|
15
|
-
properties: {
|
|
16
|
-
dirPath: {
|
|
17
|
-
type: "string",
|
|
18
|
-
description: "Path to the directory to list. Default: current directory",
|
|
19
|
-
},
|
|
20
|
-
recursive: {
|
|
21
|
-
type: "boolean",
|
|
22
|
-
description: "List recursively (max depth 3). Default: false",
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
async execute(args) {
|
|
30
|
-
const dirPath = resolve(String(args.dirPath || "."));
|
|
31
|
-
const recursive = Boolean(args.recursive);
|
|
32
|
-
|
|
33
|
-
if (!existsSync(dirPath)) {
|
|
34
|
-
return `Error: Directory not found: ${dirPath}`;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
const entries: string[] = [];
|
|
39
|
-
listDir(dirPath, "", recursive ? MAX_DIR_DEPTH : 0, entries);
|
|
40
|
-
|
|
41
|
-
if (entries.length === 0) {
|
|
42
|
-
return `Directory is empty: ${dirPath}`;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
let result = `Contents of ${dirPath} (${entries.length} entries):\n`;
|
|
46
|
-
result += entries.join("\n");
|
|
47
|
-
if (entries.length >= MAX_DIR_ENTRIES) {
|
|
48
|
-
result += `\n... (limited to ${MAX_DIR_ENTRIES} entries)`;
|
|
49
|
-
}
|
|
50
|
-
return result;
|
|
51
|
-
} catch (err: unknown) {
|
|
52
|
-
return `Error listing directory: ${err instanceof Error ? err.message : String(err)}`;
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
function listDir(
|
|
58
|
-
basePath: string,
|
|
59
|
-
prefix: string,
|
|
60
|
-
depth: number,
|
|
61
|
-
entries: string[]
|
|
62
|
-
): void {
|
|
63
|
-
if (entries.length >= MAX_DIR_ENTRIES) return;
|
|
64
|
-
|
|
65
|
-
let items: string[];
|
|
66
|
-
try {
|
|
67
|
-
items = readdirSync(join(basePath, prefix));
|
|
68
|
-
} catch {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
items.sort();
|
|
73
|
-
|
|
74
|
-
for (const item of items) {
|
|
75
|
-
if (entries.length >= MAX_DIR_ENTRIES) return;
|
|
76
|
-
|
|
77
|
-
const relPath = prefix ? `${prefix}/${item}` : item;
|
|
78
|
-
const fullPath = join(basePath, relPath);
|
|
79
|
-
|
|
80
|
-
try {
|
|
81
|
-
const stat = statSync(fullPath);
|
|
82
|
-
if (stat.isDirectory()) {
|
|
83
|
-
entries.push(`${relPath}/`);
|
|
84
|
-
if (depth > 0) {
|
|
85
|
-
listDir(basePath, relPath, depth - 1, entries);
|
|
86
|
-
}
|
|
87
|
-
} else {
|
|
88
|
-
const size = formatSize(stat.size);
|
|
89
|
-
entries.push(`${relPath} (${size})`);
|
|
90
|
-
}
|
|
91
|
-
} catch {
|
|
92
|
-
entries.push(`${relPath} (unreadable)`);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function formatSize(bytes: number): string {
|
|
98
|
-
if (bytes < 1024) return `${bytes}B`;
|
|
99
|
-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`;
|
|
100
|
-
return `${(bytes / 1024 / 1024).toFixed(1)}MB`;
|
|
101
|
-
}
|
package/src/tools/read-file.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { readFileSync, statSync, existsSync } from "fs";
|
|
2
|
-
import { resolve } from "path";
|
|
3
|
-
import type { ToolHandler } from "../types.js";
|
|
4
|
-
import { MAX_FILE_SIZE, DEFAULT_LINE_LIMIT } from "../constants.js";
|
|
5
|
-
|
|
6
|
-
export const readFileTool: ToolHandler = {
|
|
7
|
-
definition: {
|
|
8
|
-
type: "function",
|
|
9
|
-
function: {
|
|
10
|
-
name: "readFile",
|
|
11
|
-
description:
|
|
12
|
-
"Read the contents of a file with line numbers. Returns line-numbered output. Use offset and limit to read specific portions of large files.",
|
|
13
|
-
parameters: {
|
|
14
|
-
type: "object",
|
|
15
|
-
properties: {
|
|
16
|
-
filePath: {
|
|
17
|
-
type: "string",
|
|
18
|
-
description: "Path to the file to read (absolute or relative to cwd)",
|
|
19
|
-
},
|
|
20
|
-
offset: {
|
|
21
|
-
type: "number",
|
|
22
|
-
description: "Line number to start reading from (1-based). Default: 1",
|
|
23
|
-
},
|
|
24
|
-
limit: {
|
|
25
|
-
type: "number",
|
|
26
|
-
description: `Maximum number of lines to read. Default: ${DEFAULT_LINE_LIMIT}`,
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
required: ["filePath"],
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
async execute(args) {
|
|
35
|
-
const filePath = resolve(String(args.filePath));
|
|
36
|
-
const offset = Math.max(1, Number(args.offset) || 1);
|
|
37
|
-
const limit = Number(args.limit) || DEFAULT_LINE_LIMIT;
|
|
38
|
-
|
|
39
|
-
if (!existsSync(filePath)) {
|
|
40
|
-
return `Error: File not found: ${filePath}`;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
try {
|
|
44
|
-
const stat = statSync(filePath);
|
|
45
|
-
if (stat.isDirectory()) {
|
|
46
|
-
return `Error: Path is a directory, not a file: ${filePath}`;
|
|
47
|
-
}
|
|
48
|
-
if (stat.size > MAX_FILE_SIZE) {
|
|
49
|
-
return `Error: File too large (${(stat.size / 1024 / 1024).toFixed(1)}MB). Use offset/limit to read portions.`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const content = readFileSync(filePath, "utf-8");
|
|
53
|
-
const lines = content.split("\n");
|
|
54
|
-
const startIdx = offset - 1;
|
|
55
|
-
const endIdx = Math.min(startIdx + limit, lines.length);
|
|
56
|
-
const slice = lines.slice(startIdx, endIdx);
|
|
57
|
-
|
|
58
|
-
const numbered = slice
|
|
59
|
-
.map((line, i) => {
|
|
60
|
-
const lineNum = String(startIdx + i + 1).padStart(4, " ");
|
|
61
|
-
return `${lineNum} | ${line}`;
|
|
62
|
-
})
|
|
63
|
-
.join("\n");
|
|
64
|
-
|
|
65
|
-
const header = `File: ${filePath} (${lines.length} lines total, showing ${offset}-${endIdx})`;
|
|
66
|
-
return `${header}\n${numbered}`;
|
|
67
|
-
} catch (err: unknown) {
|
|
68
|
-
return `Error reading file: ${err instanceof Error ? err.message : String(err)}`;
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
};
|
package/src/tools/registry.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { ToolDefinition, ToolHandler } from "../types.js";
|
|
2
|
-
import { readFileTool } from "./read-file.js";
|
|
3
|
-
import { writeFileTool } from "./write-file.js";
|
|
4
|
-
import { editFileTool } from "./edit-file.js";
|
|
5
|
-
import { runCommandTool } from "./run-command.js";
|
|
6
|
-
import { grepTool } from "./grep.js";
|
|
7
|
-
import { globTool } from "./glob-tool.js";
|
|
8
|
-
import { listDirectoryTool } from "./list-directory.js";
|
|
9
|
-
|
|
10
|
-
const tools: Map<string, ToolHandler> = new Map();
|
|
11
|
-
|
|
12
|
-
function register(handler: ToolHandler): void {
|
|
13
|
-
tools.set(handler.definition.function.name, handler);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
register(readFileTool);
|
|
17
|
-
register(writeFileTool);
|
|
18
|
-
register(editFileTool);
|
|
19
|
-
register(runCommandTool);
|
|
20
|
-
register(grepTool);
|
|
21
|
-
register(globTool);
|
|
22
|
-
register(listDirectoryTool);
|
|
23
|
-
|
|
24
|
-
export function getToolHandler(name: string): ToolHandler | undefined {
|
|
25
|
-
return tools.get(name);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function getAllDefinitions(): ToolDefinition[] {
|
|
29
|
-
return Array.from(tools.values()).map((t) => t.definition);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function executeToolCall(
|
|
33
|
-
name: string,
|
|
34
|
-
args: Record<string, unknown>
|
|
35
|
-
): Promise<string> {
|
|
36
|
-
const handler = tools.get(name);
|
|
37
|
-
if (!handler) {
|
|
38
|
-
return Promise.resolve(`Error: Unknown tool "${name}"`);
|
|
39
|
-
}
|
|
40
|
-
return handler.execute(args);
|
|
41
|
-
}
|
package/src/tools/run-command.ts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { resolve } from "path";
|
|
2
|
-
import type { ToolHandler } from "../types.js";
|
|
3
|
-
import { DEFAULT_COMMAND_TIMEOUT, MAX_COMMAND_TIMEOUT, MAX_OUTPUT_BYTES } from "../constants.js";
|
|
4
|
-
|
|
5
|
-
export const runCommandTool: ToolHandler = {
|
|
6
|
-
definition: {
|
|
7
|
-
type: "function",
|
|
8
|
-
function: {
|
|
9
|
-
name: "runCommand",
|
|
10
|
-
description:
|
|
11
|
-
"Execute a shell command and return its output. Default timeout is 30s and hard-capped at 45s. Output is truncated at 50KB.",
|
|
12
|
-
parameters: {
|
|
13
|
-
type: "object",
|
|
14
|
-
properties: {
|
|
15
|
-
command: {
|
|
16
|
-
type: "string",
|
|
17
|
-
description: "The shell command to execute",
|
|
18
|
-
},
|
|
19
|
-
cwd: {
|
|
20
|
-
type: "string",
|
|
21
|
-
description: "Working directory for the command. Default: current directory",
|
|
22
|
-
},
|
|
23
|
-
timeout: {
|
|
24
|
-
type: "number",
|
|
25
|
-
description: "Timeout in milliseconds. Default: 30000, max: 45000",
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
required: ["command"],
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
async execute(args) {
|
|
34
|
-
const command = String(args.command);
|
|
35
|
-
const cwd = args.cwd ? resolve(String(args.cwd)) : process.cwd();
|
|
36
|
-
const requestedTimeout = Number(args.timeout) || DEFAULT_COMMAND_TIMEOUT;
|
|
37
|
-
const timeout = Math.min(Math.max(1_000, requestedTimeout), MAX_COMMAND_TIMEOUT);
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
const proc = Bun.spawn(["sh", "-c", command], {
|
|
41
|
-
stdout: "pipe",
|
|
42
|
-
stderr: "pipe",
|
|
43
|
-
cwd,
|
|
44
|
-
env: { ...process.env },
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
let timedOut = false;
|
|
48
|
-
const timer = setTimeout(() => {
|
|
49
|
-
timedOut = true;
|
|
50
|
-
try {
|
|
51
|
-
proc.kill(9);
|
|
52
|
-
} catch {
|
|
53
|
-
// Process may already have exited.
|
|
54
|
-
}
|
|
55
|
-
}, timeout);
|
|
56
|
-
|
|
57
|
-
const exitCode = await Promise.race([
|
|
58
|
-
proc.exited,
|
|
59
|
-
new Promise<number>((resolveTimeout) => {
|
|
60
|
-
setTimeout(() => resolveTimeout(-1), timeout + 250);
|
|
61
|
-
}),
|
|
62
|
-
]);
|
|
63
|
-
|
|
64
|
-
clearTimeout(timer);
|
|
65
|
-
|
|
66
|
-
if (timedOut || exitCode === -1) {
|
|
67
|
-
return [
|
|
68
|
-
`Exit code: 124`,
|
|
69
|
-
`STDERR:`,
|
|
70
|
-
`Command timed out after ${Math.round(timeout / 1000)}s.`,
|
|
71
|
-
`Tip: use a shorter command, reduce scope, or run background/dev servers manually outside kalcode.`,
|
|
72
|
-
].join("\n\n");
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const [stdoutBuf, stderrBuf] = await Promise.all([
|
|
76
|
-
new Response(proc.stdout).arrayBuffer(),
|
|
77
|
-
new Response(proc.stderr).arrayBuffer(),
|
|
78
|
-
]);
|
|
79
|
-
|
|
80
|
-
let stdout = new TextDecoder().decode(stdoutBuf);
|
|
81
|
-
let stderr = new TextDecoder().decode(stderrBuf);
|
|
82
|
-
|
|
83
|
-
if (stdout.length > MAX_OUTPUT_BYTES) {
|
|
84
|
-
stdout = stdout.slice(0, MAX_OUTPUT_BYTES) + "\n... (output truncated)";
|
|
85
|
-
}
|
|
86
|
-
if (stderr.length > MAX_OUTPUT_BYTES) {
|
|
87
|
-
stderr = stderr.slice(0, MAX_OUTPUT_BYTES) + "\n... (output truncated)";
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
let result = `Exit code: ${exitCode}`;
|
|
91
|
-
if (stdout.trim()) result += `\n\nSTDOUT:\n${stdout.trim()}`;
|
|
92
|
-
if (stderr.trim()) result += `\n\nSTDERR:\n${stderr.trim()}`;
|
|
93
|
-
|
|
94
|
-
return result;
|
|
95
|
-
} catch (err: unknown) {
|
|
96
|
-
return `Error running command: ${err instanceof Error ? err.message : String(err)}`;
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
};
|
package/src/tools/write-file.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { writeFileSync, mkdirSync } from "fs";
|
|
2
|
-
import { resolve, dirname } from "path";
|
|
3
|
-
import type { ToolHandler } from "../types.js";
|
|
4
|
-
|
|
5
|
-
export const writeFileTool: ToolHandler = {
|
|
6
|
-
definition: {
|
|
7
|
-
type: "function",
|
|
8
|
-
function: {
|
|
9
|
-
name: "writeFile",
|
|
10
|
-
description:
|
|
11
|
-
"Write content to a file. Creates the file if it doesn't exist. Automatically creates parent directories as needed.",
|
|
12
|
-
parameters: {
|
|
13
|
-
type: "object",
|
|
14
|
-
properties: {
|
|
15
|
-
filePath: {
|
|
16
|
-
type: "string",
|
|
17
|
-
description: "Path to the file to write (absolute or relative to cwd)",
|
|
18
|
-
},
|
|
19
|
-
content: {
|
|
20
|
-
type: "string",
|
|
21
|
-
description: "Content to write to the file",
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
required: ["filePath", "content"],
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
async execute(args) {
|
|
30
|
-
const filePath = resolve(String(args.filePath));
|
|
31
|
-
const content = String(args.content);
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
mkdirSync(dirname(filePath), { recursive: true });
|
|
35
|
-
writeFileSync(filePath, content);
|
|
36
|
-
const lineCount = content.split("\n").length;
|
|
37
|
-
return `Successfully wrote ${lineCount} lines to ${filePath}`;
|
|
38
|
-
} catch (err: unknown) {
|
|
39
|
-
return `Error writing file: ${err instanceof Error ? err.message : String(err)}`;
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
};
|
package/src/types.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
export interface Config {
|
|
2
|
-
apiKey: string;
|
|
3
|
-
model: string;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export interface Message {
|
|
7
|
-
role: "system" | "user" | "assistant" | "tool";
|
|
8
|
-
content: string | null;
|
|
9
|
-
tool_calls?: ToolCall[];
|
|
10
|
-
tool_call_id?: string;
|
|
11
|
-
name?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface ToolCall {
|
|
15
|
-
id: string;
|
|
16
|
-
type: "function";
|
|
17
|
-
function: {
|
|
18
|
-
name: string;
|
|
19
|
-
arguments: string;
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface ToolDefinition {
|
|
24
|
-
type: "function";
|
|
25
|
-
function: {
|
|
26
|
-
name: string;
|
|
27
|
-
description: string;
|
|
28
|
-
parameters: {
|
|
29
|
-
type: "object";
|
|
30
|
-
properties: Record<string, unknown>;
|
|
31
|
-
required?: string[];
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface StreamDelta {
|
|
37
|
-
type: "content" | "tool_call" | "done" | "error";
|
|
38
|
-
content?: string;
|
|
39
|
-
toolCall?: {
|
|
40
|
-
index: number;
|
|
41
|
-
id?: string;
|
|
42
|
-
function?: {
|
|
43
|
-
name?: string;
|
|
44
|
-
arguments?: string;
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
error?: string;
|
|
48
|
-
usage?: TokenUsage;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface ToolHandler {
|
|
52
|
-
definition: ToolDefinition;
|
|
53
|
-
execute: (args: Record<string, unknown>) => Promise<string>;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export interface ToolCallAccumulator {
|
|
57
|
-
id: string;
|
|
58
|
-
function: {
|
|
59
|
-
name: string;
|
|
60
|
-
arguments: string;
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export interface TokenUsage {
|
|
65
|
-
prompt_tokens: number;
|
|
66
|
-
completion_tokens: number;
|
|
67
|
-
total_tokens: number;
|
|
68
|
-
}
|