@datename/fastcode-termux 0.0.0-stage → 0.1.6-beta
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/LICENSE +21 -0
- package/README.md +418 -2
- package/bin/fastcode.mjs +59 -0
- package/dist/agent/approval.d.ts +50 -0
- package/dist/agent/approval.js +7 -0
- package/dist/agent/compaction.d.ts +39 -0
- package/dist/agent/compaction.js +227 -0
- package/dist/agent/context.d.ts +29 -0
- package/dist/agent/context.js +62 -0
- package/dist/agent/guards.d.ts +43 -0
- package/dist/agent/guards.js +269 -0
- package/dist/agent/ignore.d.ts +11 -0
- package/dist/agent/ignore.js +129 -0
- package/dist/agent/limits.d.ts +24 -0
- package/dist/agent/limits.js +71 -0
- package/dist/agent/process-tree.d.ts +2 -0
- package/dist/agent/process-tree.js +31 -0
- package/dist/agent/protocol-mode.d.ts +6 -0
- package/dist/agent/protocol-mode.js +51 -0
- package/dist/agent/protocol.d.ts +4 -0
- package/dist/agent/protocol.js +129 -0
- package/dist/agent/registry.d.ts +17 -0
- package/dist/agent/registry.js +90 -0
- package/dist/agent/runner.d.ts +106 -0
- package/dist/agent/runner.js +352 -0
- package/dist/agent/shell-session.d.ts +37 -0
- package/dist/agent/shell-session.js +400 -0
- package/dist/agent/stream.d.ts +29 -0
- package/dist/agent/stream.js +103 -0
- package/dist/agent/tool-schemas.d.ts +2 -0
- package/dist/agent/tool-schemas.js +63 -0
- package/dist/agent/tools/append_file.d.ts +2 -0
- package/dist/agent/tools/append_file.js +26 -0
- package/dist/agent/tools/apply_patch.d.ts +10 -0
- package/dist/agent/tools/apply_patch.js +52 -0
- package/dist/agent/tools/copy_file.d.ts +2 -0
- package/dist/agent/tools/copy_file.js +30 -0
- package/dist/agent/tools/delete_file.d.ts +2 -0
- package/dist/agent/tools/delete_file.js +24 -0
- package/dist/agent/tools/grep.d.ts +2 -0
- package/dist/agent/tools/grep.js +23 -0
- package/dist/agent/tools/index.d.ts +2 -0
- package/dist/agent/tools/index.js +32 -0
- package/dist/agent/tools/list_files.d.ts +2 -0
- package/dist/agent/tools/list_files.js +17 -0
- package/dist/agent/tools/make_dir.d.ts +2 -0
- package/dist/agent/tools/make_dir.js +24 -0
- package/dist/agent/tools/move_path.d.ts +2 -0
- package/dist/agent/tools/move_path.js +47 -0
- package/dist/agent/tools/paths.d.ts +7 -0
- package/dist/agent/tools/paths.js +84 -0
- package/dist/agent/tools/pwd.d.ts +2 -0
- package/dist/agent/tools/pwd.js +10 -0
- package/dist/agent/tools/read_file.d.ts +2 -0
- package/dist/agent/tools/read_file.js +21 -0
- package/dist/agent/tools/read_many.d.ts +2 -0
- package/dist/agent/tools/read_many.js +25 -0
- package/dist/agent/tools/readers.d.ts +6 -0
- package/dist/agent/tools/readers.js +216 -0
- package/dist/agent/tools/replace_in_file.d.ts +2 -0
- package/dist/agent/tools/replace_in_file.js +30 -0
- package/dist/agent/tools/shell.d.ts +2 -0
- package/dist/agent/tools/shell.js +48 -0
- package/dist/agent/tools/stat_path.d.ts +2 -0
- package/dist/agent/tools/stat_path.js +31 -0
- package/dist/agent/tools/types.d.ts +39 -0
- package/dist/agent/tools/types.js +1 -0
- package/dist/agent/tools/validate.d.ts +6 -0
- package/dist/agent/tools/validate.js +29 -0
- package/dist/agent/tools/write_file.d.ts +2 -0
- package/dist/agent/tools/write_file.js +26 -0
- package/dist/agent/tools.d.ts +3 -0
- package/dist/agent/tools.js +2 -0
- package/dist/app.d.ts +4 -0
- package/dist/app.js +708 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +569 -0
- package/dist/commands/about.d.ts +8 -0
- package/dist/commands/about.js +16 -0
- package/dist/commands/agent.d.ts +11 -0
- package/dist/commands/agent.js +18 -0
- package/dist/commands/attach.d.ts +9 -0
- package/dist/commands/attach.js +15 -0
- package/dist/commands/bg.d.ts +7 -0
- package/dist/commands/bg.js +9 -0
- package/dist/commands/cancel.d.ts +10 -0
- package/dist/commands/cancel.js +10 -0
- package/dist/commands/clear.d.ts +11 -0
- package/dist/commands/clear.js +9 -0
- package/dist/commands/compact.d.ts +10 -0
- package/dist/commands/compact.js +22 -0
- package/dist/commands/config.d.ts +7 -0
- package/dist/commands/config.js +24 -0
- package/dist/commands/context.d.ts +7 -0
- package/dist/commands/context.js +24 -0
- package/dist/commands/cost.d.ts +9 -0
- package/dist/commands/cost.js +13 -0
- package/dist/commands/cwd.d.ts +7 -0
- package/dist/commands/cwd.js +7 -0
- package/dist/commands/edit.d.ts +10 -0
- package/dist/commands/edit.js +36 -0
- package/dist/commands/effort.d.ts +25 -0
- package/dist/commands/effort.js +48 -0
- package/dist/commands/exit.d.ts +9 -0
- package/dist/commands/exit.js +8 -0
- package/dist/commands/expand.d.ts +10 -0
- package/dist/commands/expand.js +8 -0
- package/dist/commands/help.d.ts +6 -0
- package/dist/commands/help.js +18 -0
- package/dist/commands/index.d.ts +9 -0
- package/dist/commands/index.js +80 -0
- package/dist/commands/jobs.d.ts +7 -0
- package/dist/commands/jobs.js +16 -0
- package/dist/commands/kill.d.ts +7 -0
- package/dist/commands/kill.js +11 -0
- package/dist/commands/login.d.ts +18 -0
- package/dist/commands/login.js +63 -0
- package/dist/commands/logout.d.ts +9 -0
- package/dist/commands/logout.js +45 -0
- package/dist/commands/model.d.ts +24 -0
- package/dist/commands/model.js +50 -0
- package/dist/commands/plugin.d.ts +11 -0
- package/dist/commands/plugin.js +74 -0
- package/dist/commands/provider.d.ts +24 -0
- package/dist/commands/provider.js +39 -0
- package/dist/commands/reset.d.ts +9 -0
- package/dist/commands/reset.js +13 -0
- package/dist/commands/skills.d.ts +11 -0
- package/dist/commands/skills.js +106 -0
- package/dist/commands/steer.d.ts +11 -0
- package/dist/commands/steer.js +13 -0
- package/dist/commands/system.d.ts +11 -0
- package/dist/commands/system.js +20 -0
- package/dist/commands/theme.d.ts +11 -0
- package/dist/commands/theme.js +22 -0
- package/dist/commands/tools.d.ts +7 -0
- package/dist/commands/tools.js +29 -0
- package/dist/commands/whoami.d.ts +9 -0
- package/dist/commands/whoami.js +52 -0
- package/dist/config/paths.d.ts +4 -0
- package/dist/config/paths.js +59 -0
- package/dist/config/store.d.ts +89 -0
- package/dist/config/store.js +260 -0
- package/dist/config/themes.d.ts +73 -0
- package/dist/config/themes.js +77 -0
- package/dist/config/trust.d.ts +22 -0
- package/dist/config/trust.js +153 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +31 -0
- package/dist/lib/diagnostics.d.ts +66 -0
- package/dist/lib/diagnostics.js +195 -0
- package/dist/lib/http.d.ts +23 -0
- package/dist/lib/http.js +128 -0
- package/dist/lib/markdown.d.ts +1 -0
- package/dist/lib/markdown.js +32 -0
- package/dist/lib/model-meta.d.ts +19 -0
- package/dist/lib/model-meta.js +113 -0
- package/dist/lib/native-tools.d.ts +12 -0
- package/dist/lib/native-tools.js +21 -0
- package/dist/lib/net.d.ts +33 -0
- package/dist/lib/net.js +168 -0
- package/dist/lib/pkg.d.ts +5 -0
- package/dist/lib/pkg.js +27 -0
- package/dist/lib/platform.d.ts +7 -0
- package/dist/lib/platform.js +57 -0
- package/dist/lib/startup.d.ts +6 -0
- package/dist/lib/startup.js +25 -0
- package/dist/lib/uiMode.d.ts +2 -0
- package/dist/lib/uiMode.js +7 -0
- package/dist/patch/apply.d.ts +15 -0
- package/dist/patch/apply.js +172 -0
- package/dist/patch/parse.d.ts +29 -0
- package/dist/patch/parse.js +112 -0
- package/dist/patch/plan.d.ts +49 -0
- package/dist/patch/plan.js +273 -0
- package/dist/plugins/manager.d.ts +23 -0
- package/dist/plugins/manager.js +101 -0
- package/dist/providers/anthropic.d.ts +22 -0
- package/dist/providers/anthropic.js +119 -0
- package/dist/providers/custom.d.ts +33 -0
- package/dist/providers/custom.js +25 -0
- package/dist/providers/deepseek.d.ts +34 -0
- package/dist/providers/deepseek.js +16 -0
- package/dist/providers/google.d.ts +33 -0
- package/dist/providers/google.js +19 -0
- package/dist/providers/groq.d.ts +34 -0
- package/dist/providers/groq.js +21 -0
- package/dist/providers/index.d.ts +10 -0
- package/dist/providers/index.js +67 -0
- package/dist/providers/mistral.d.ts +34 -0
- package/dist/providers/mistral.js +20 -0
- package/dist/providers/moonshot.d.ts +34 -0
- package/dist/providers/moonshot.js +23 -0
- package/dist/providers/ollama.d.ts +33 -0
- package/dist/providers/ollama.js +22 -0
- package/dist/providers/openai.d.ts +34 -0
- package/dist/providers/openai.js +26 -0
- package/dist/providers/openaiCompatible.d.ts +26 -0
- package/dist/providers/openaiCompatible.js +158 -0
- package/dist/providers/openrouter.d.ts +34 -0
- package/dist/providers/openrouter.js +27 -0
- package/dist/providers/qwen.d.ts +34 -0
- package/dist/providers/qwen.js +24 -0
- package/dist/providers/xai.d.ts +34 -0
- package/dist/providers/xai.js +19 -0
- package/dist/providers/zhipu.d.ts +34 -0
- package/dist/providers/zhipu.js +24 -0
- package/dist/session/log.d.ts +52 -0
- package/dist/session/log.js +331 -0
- package/dist/session/projection.d.ts +43 -0
- package/dist/session/projection.js +257 -0
- package/dist/skills/manager.d.ts +18 -0
- package/dist/skills/manager.js +201 -0
- package/dist/skills/parser.d.ts +18 -0
- package/dist/skills/parser.js +65 -0
- package/dist/types.d.ts +205 -0
- package/dist/types.js +1 -0
- package/dist/ui/Banner.d.ts +5 -0
- package/dist/ui/Banner.js +65 -0
- package/dist/ui/Message.d.ts +1 -0
- package/dist/ui/Message.js +24 -0
- package/dist/ui/Picker.d.ts +8 -0
- package/dist/ui/Picker.js +24 -0
- package/dist/ui/Prompt.d.ts +9 -0
- package/dist/ui/Prompt.js +38 -0
- package/dist/ui/Spinner.d.ts +5 -0
- package/dist/ui/Spinner.js +39 -0
- package/dist/ui/StatusBar.d.ts +11 -0
- package/dist/ui/StatusBar.js +20 -0
- package/dist/ui/TrustGate.d.ts +5 -0
- package/dist/ui/TrustGate.js +26 -0
- package/package.json +96 -4
- package/src/agent/approval.ts +47 -0
- package/src/agent/compaction.ts +276 -0
- package/src/agent/context.ts +97 -0
- package/src/agent/guards.ts +270 -0
- package/src/agent/ignore.ts +147 -0
- package/src/agent/limits.ts +71 -0
- package/src/agent/process-tree.ts +32 -0
- package/src/agent/protocol-mode.ts +59 -0
- package/src/agent/protocol.ts +129 -0
- package/src/agent/registry.ts +98 -0
- package/src/agent/runner.ts +443 -0
- package/src/agent/shell-session.ts +449 -0
- package/src/agent/stream.ts +143 -0
- package/src/agent/tool-schemas.ts +67 -0
- package/src/agent/tools/append_file.ts +28 -0
- package/src/agent/tools/apply_patch.ts +54 -0
- package/src/agent/tools/copy_file.ts +32 -0
- package/src/agent/tools/delete_file.ts +26 -0
- package/src/agent/tools/grep.ts +25 -0
- package/src/agent/tools/index.ts +34 -0
- package/src/agent/tools/list_files.ts +19 -0
- package/src/agent/tools/make_dir.ts +26 -0
- package/src/agent/tools/move_path.ts +50 -0
- package/src/agent/tools/paths.ts +92 -0
- package/src/agent/tools/pwd.ts +12 -0
- package/src/agent/tools/read_file.ts +23 -0
- package/src/agent/tools/read_many.ts +27 -0
- package/src/agent/tools/readers.ts +228 -0
- package/src/agent/tools/replace_in_file.ts +32 -0
- package/src/agent/tools/shell.ts +53 -0
- package/src/agent/tools/stat_path.ts +33 -0
- package/src/agent/tools/types.ts +42 -0
- package/src/agent/tools/validate.ts +35 -0
- package/src/agent/tools/write_file.ts +28 -0
- package/src/agent/tools.ts +3 -0
- package/src/app.ts +708 -0
- package/src/cli.ts +335 -0
- package/src/commands/about.ts +16 -0
- package/src/commands/agent.ts +18 -0
- package/src/commands/attach.ts +16 -0
- package/src/commands/bg.ts +9 -0
- package/src/commands/cancel.ts +10 -0
- package/src/commands/clear.ts +9 -0
- package/src/commands/compact.ts +23 -0
- package/src/commands/config.ts +24 -0
- package/src/commands/context.ts +25 -0
- package/src/commands/cost.ts +13 -0
- package/src/commands/cwd.ts +7 -0
- package/src/commands/edit.ts +37 -0
- package/src/commands/effort.ts +49 -0
- package/src/commands/exit.ts +8 -0
- package/src/commands/expand.ts +8 -0
- package/src/commands/help.ts +18 -0
- package/src/commands/index.ts +80 -0
- package/src/commands/jobs.ts +17 -0
- package/src/commands/kill.ts +12 -0
- package/src/commands/login.ts +63 -0
- package/src/commands/logout.ts +45 -0
- package/src/commands/model.ts +50 -0
- package/src/commands/plugin.ts +74 -0
- package/src/commands/provider.ts +39 -0
- package/src/commands/reset.ts +13 -0
- package/src/commands/skills.ts +106 -0
- package/src/commands/steer.ts +13 -0
- package/src/commands/system.ts +20 -0
- package/src/commands/theme.ts +22 -0
- package/src/commands/tools.ts +29 -0
- package/src/commands/whoami.ts +52 -0
- package/src/config/paths.ts +59 -0
- package/src/config/store.ts +293 -0
- package/src/config/themes.ts +77 -0
- package/src/config/trust.ts +183 -0
- package/src/index.ts +31 -0
- package/src/lib/diagnostics.ts +195 -0
- package/src/lib/http.ts +132 -0
- package/src/lib/markdown.ts +32 -0
- package/src/lib/model-meta.ts +138 -0
- package/src/lib/native-tools.ts +32 -0
- package/src/lib/net.ts +204 -0
- package/src/lib/pkg.ts +35 -0
- package/src/lib/platform.ts +57 -0
- package/src/lib/startup.ts +25 -0
- package/src/lib/uiMode.ts +7 -0
- package/src/patch/apply.ts +194 -0
- package/src/patch/parse.ts +131 -0
- package/src/patch/plan.ts +317 -0
- package/src/plugins/manager.ts +101 -0
- package/src/providers/anthropic.ts +119 -0
- package/src/providers/custom.ts +25 -0
- package/src/providers/deepseek.ts +16 -0
- package/src/providers/google.ts +19 -0
- package/src/providers/groq.ts +21 -0
- package/src/providers/index.ts +68 -0
- package/src/providers/mistral.ts +20 -0
- package/src/providers/moonshot.ts +23 -0
- package/src/providers/ollama.ts +22 -0
- package/src/providers/openai.ts +26 -0
- package/src/providers/openaiCompatible.ts +158 -0
- package/src/providers/openrouter.ts +27 -0
- package/src/providers/qwen.ts +24 -0
- package/src/providers/xai.ts +19 -0
- package/src/providers/zhipu.ts +24 -0
- package/src/session/log.ts +374 -0
- package/src/session/projection.ts +308 -0
- package/src/skills/manager.ts +201 -0
- package/src/skills/parser.ts +65 -0
- package/src/types.ts +205 -0
- package/src/ui/Banner.ts +68 -0
- package/src/ui/Message.ts +24 -0
- package/src/ui/Picker.ts +24 -0
- package/src/ui/Prompt.ts +38 -0
- package/src/ui/Spinner.ts +39 -0
- package/src/ui/StatusBar.ts +20 -0
- package/src/ui/TrustGate.ts +26 -0
- package/tsconfig.json +22 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { resolvePath } from "./paths.js";
|
|
3
|
+
import type { Tool } from "./types.js";
|
|
4
|
+
import { invalid, preparePath, requiredString } from "./validate.js";
|
|
5
|
+
|
|
6
|
+
export const replaceInFileTool: Tool = {
|
|
7
|
+
name: "replace_in_file",
|
|
8
|
+
requiresTrust: true,
|
|
9
|
+
stateScope: "turn",
|
|
10
|
+
revalidate: ["trust", "sandboxRoots"],
|
|
11
|
+
prepare(call, ctx) {
|
|
12
|
+
try {
|
|
13
|
+
requiredString(call, "path");
|
|
14
|
+
requiredString(call, "find");
|
|
15
|
+
requiredString(call, "replace");
|
|
16
|
+
}
|
|
17
|
+
catch (err: any) {
|
|
18
|
+
return invalid(err.message);
|
|
19
|
+
}
|
|
20
|
+
return preparePath(ctx.cwd, requiredString(call, "path"));
|
|
21
|
+
},
|
|
22
|
+
async execute(call, ctx) {
|
|
23
|
+
const file = resolvePath(ctx.cwd, call);
|
|
24
|
+
const find = requiredString(call, "find");
|
|
25
|
+
const replacement = requiredString(call, "replace");
|
|
26
|
+
const source = await readFile(file, "utf8");
|
|
27
|
+
if (!source.includes(find))
|
|
28
|
+
return { ok: false, output: `text not found in ${file}` };
|
|
29
|
+
await writeFile(file, source.split(find).join(replacement), "utf8");
|
|
30
|
+
return { ok: true, output: `replaced text in ${file}`, sideEffects: { touched: [file] } };
|
|
31
|
+
},
|
|
32
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { ShellExecResult } from "../shell-session.js";
|
|
2
|
+
import { executeShellCommand, pollShellSession } from "../shell-session.js";
|
|
3
|
+
import type { Tool } from "./types.js";
|
|
4
|
+
import { invalid, numberOption } from "./validate.js";
|
|
5
|
+
|
|
6
|
+
export const shellTool: Tool = {
|
|
7
|
+
name: "shell",
|
|
8
|
+
requiresTrust: true,
|
|
9
|
+
stateScope: "turn",
|
|
10
|
+
revalidate: ["trust", "sandboxRoots"],
|
|
11
|
+
approvalHints: { mayRequireApproval: true },
|
|
12
|
+
prepare(call) {
|
|
13
|
+
if (typeof call.session_id === "string" && call.session_id.trim())
|
|
14
|
+
return { ok: true };
|
|
15
|
+
if (typeof call.command !== "string" || call.command.trim() === "")
|
|
16
|
+
return invalid("command must be a non-empty string");
|
|
17
|
+
return { ok: true };
|
|
18
|
+
},
|
|
19
|
+
async execute(call, ctx) {
|
|
20
|
+
if (typeof call.session_id === "string" && call.session_id.trim()) {
|
|
21
|
+
const polled = await pollShellSession(call.session_id, numberOption(call.waitMs, 0));
|
|
22
|
+
if (!polled)
|
|
23
|
+
return { ok: false, output: `unknown shell session: ${call.session_id}` };
|
|
24
|
+
return formatPoll(call.session_id, polled);
|
|
25
|
+
}
|
|
26
|
+
const result = await executeShellCommand(call.command, ctx.cwd, {
|
|
27
|
+
signal: ctx.signal,
|
|
28
|
+
turn: ctx.turn,
|
|
29
|
+
timeoutMs: numberOption(call.timeoutMs, 120000),
|
|
30
|
+
});
|
|
31
|
+
return formatExec(result);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function formatExec(result: ShellExecResult): { ok: boolean; output: string } {
|
|
36
|
+
if (result.status !== "running")
|
|
37
|
+
return { ok: result.ok, output: result.output || `[command ${result.status}]` };
|
|
38
|
+
const header = `[session ${result.sessionId}] running`;
|
|
39
|
+
const body = result.output ? "\n" + result.output : "";
|
|
40
|
+
return {
|
|
41
|
+
ok: true,
|
|
42
|
+
output: `${header}${body}\n\nPoll with: {"tool":"shell","session_id":"${result.sessionId}"}`,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function formatPoll(id: string, result: ShellExecResult): { ok: boolean; output: string } {
|
|
47
|
+
const status = result.status === "running"
|
|
48
|
+
? `running (next poll ~${Math.round((result.nextPollMs ?? 1000) / 1000)}s)`
|
|
49
|
+
: `${result.status}${result.exitCode !== undefined && result.exitCode !== null ? ` (exit ${result.exitCode})` : ""}`;
|
|
50
|
+
const header = `[session ${id}] ${status}`;
|
|
51
|
+
const body = result.output ? "\n" + result.output : result.status === "running" ? "\n(no new output)" : "";
|
|
52
|
+
return { ok: result.ok, output: header + body };
|
|
53
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises";
|
|
2
|
+
import { resolvePath } from "./paths.js";
|
|
3
|
+
import type { Tool } from "./types.js";
|
|
4
|
+
import { invalid, preparePath, requiredString } from "./validate.js";
|
|
5
|
+
|
|
6
|
+
export const statPathTool: Tool = {
|
|
7
|
+
name: "stat_path",
|
|
8
|
+
stateScope: "call",
|
|
9
|
+
revalidate: ["sandboxRoots"],
|
|
10
|
+
prepare(call, ctx) {
|
|
11
|
+
let path: string;
|
|
12
|
+
try {
|
|
13
|
+
path = requiredString(call, "path");
|
|
14
|
+
}
|
|
15
|
+
catch (err: any) {
|
|
16
|
+
return invalid(err.message);
|
|
17
|
+
}
|
|
18
|
+
return preparePath(ctx.cwd, path);
|
|
19
|
+
},
|
|
20
|
+
async execute(call, ctx) {
|
|
21
|
+
const file = resolvePath(ctx.cwd, call);
|
|
22
|
+
const info = await stat(file);
|
|
23
|
+
return {
|
|
24
|
+
ok: true,
|
|
25
|
+
output: JSON.stringify({
|
|
26
|
+
path: file,
|
|
27
|
+
type: info.isDirectory() ? "dir" : info.isFile() ? "file" : "other",
|
|
28
|
+
size: info.size,
|
|
29
|
+
modified: info.mtime.toISOString(),
|
|
30
|
+
}, null, 2),
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type StateScope = "call" | "turn" | "session";
|
|
2
|
+
|
|
3
|
+
export type RevalidateKey = "trust" | "sandboxRoots" | "schema" | "modelCaps";
|
|
4
|
+
|
|
5
|
+
export interface SideEffects {
|
|
6
|
+
touched?: string[];
|
|
7
|
+
created?: string[];
|
|
8
|
+
removed?: string[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ToolCall {
|
|
12
|
+
tool: string;
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ToolResult {
|
|
17
|
+
ok: boolean;
|
|
18
|
+
output: string;
|
|
19
|
+
sideEffects?: SideEffects;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ToolCtx {
|
|
23
|
+
cwd: string;
|
|
24
|
+
signal?: AbortSignal;
|
|
25
|
+
turn?: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type Prepared = { ok: true } | { ok: false; output: string };
|
|
29
|
+
|
|
30
|
+
export interface ApprovalHints {
|
|
31
|
+
mayRequireApproval: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface Tool {
|
|
35
|
+
name: string;
|
|
36
|
+
requiresTrust?: boolean;
|
|
37
|
+
stateScope: StateScope;
|
|
38
|
+
revalidate?: RevalidateKey[];
|
|
39
|
+
approvalHints?: ApprovalHints;
|
|
40
|
+
prepare(call: ToolCall, ctx: ToolCtx): Prepared;
|
|
41
|
+
execute(call: ToolCall, ctx: ToolCtx): Promise<ToolResult>;
|
|
42
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { sandboxPath } from "./paths.js";
|
|
2
|
+
import type { Prepared, ToolCall } from "./types.js";
|
|
3
|
+
|
|
4
|
+
export function invalid(output: string): Prepared {
|
|
5
|
+
return { ok: false, output };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function requiredString(call: ToolCall, key: string): string {
|
|
9
|
+
const value = call[key];
|
|
10
|
+
if (typeof value !== "string")
|
|
11
|
+
throw new Error(`${key} must be a string`);
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function stringArray(call: ToolCall, key: string): string[] {
|
|
16
|
+
const value = call[key];
|
|
17
|
+
if (!Array.isArray(value) || !value.every((item) => typeof item === "string")) {
|
|
18
|
+
throw new Error(`${key} must be an array of strings`);
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function numberOption(value: any, fallback: number): number {
|
|
24
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function preparePath(cwd: string, raw: string): Prepared {
|
|
28
|
+
try {
|
|
29
|
+
sandboxPath(cwd, raw);
|
|
30
|
+
return { ok: true };
|
|
31
|
+
}
|
|
32
|
+
catch (err: any) {
|
|
33
|
+
return invalid(err?.message ?? String(err));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { resolvePath } from "./paths.js";
|
|
4
|
+
import type { Tool } from "./types.js";
|
|
5
|
+
import { invalid, preparePath, requiredString } from "./validate.js";
|
|
6
|
+
|
|
7
|
+
export const writeFileTool: Tool = {
|
|
8
|
+
name: "write_file",
|
|
9
|
+
requiresTrust: true,
|
|
10
|
+
stateScope: "turn",
|
|
11
|
+
revalidate: ["trust", "sandboxRoots"],
|
|
12
|
+
prepare(call, ctx) {
|
|
13
|
+
try {
|
|
14
|
+
requiredString(call, "path");
|
|
15
|
+
requiredString(call, "content");
|
|
16
|
+
}
|
|
17
|
+
catch (err: any) {
|
|
18
|
+
return invalid(err.message);
|
|
19
|
+
}
|
|
20
|
+
return preparePath(ctx.cwd, requiredString(call, "path"));
|
|
21
|
+
},
|
|
22
|
+
async execute(call, ctx) {
|
|
23
|
+
const file = resolvePath(ctx.cwd, call);
|
|
24
|
+
await mkdir(dirname(file), { recursive: true });
|
|
25
|
+
await writeFile(file, requiredString(call, "content"), "utf8");
|
|
26
|
+
return { ok: true, output: `wrote ${file}`, sideEffects: { touched: [file] } };
|
|
27
|
+
},
|
|
28
|
+
};
|