@datename/fastcode-termux 0.1.6 → 0.1.8-beta.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/LICENSE +4 -0
- package/README.md +67 -16
- package/dist/agent/approval-json.d.ts +43 -0
- package/dist/agent/approval-json.js +69 -0
- package/dist/agent/approval-native.d.ts +3 -0
- package/dist/agent/approval-native.js +165 -0
- package/dist/agent/approval-store.d.ts +16 -0
- package/dist/agent/approval-store.js +21 -0
- package/dist/agent/approval.d.ts +83 -0
- package/dist/agent/approval.js +42 -0
- package/dist/agent/compaction.d.ts +41 -0
- package/dist/agent/compaction.js +270 -0
- package/dist/agent/context.d.ts +29 -0
- package/dist/agent/context.js +62 -0
- package/dist/agent/guards.d.ts +24 -16
- package/dist/agent/guards.js +1167 -18
- package/dist/agent/ignore.d.ts +11 -0
- package/dist/agent/ignore.js +129 -0
- package/dist/agent/policy/defaults.d.ts +16 -0
- package/dist/agent/policy/defaults.js +17 -0
- package/dist/agent/policy/egress.d.ts +32 -0
- package/dist/agent/policy/egress.js +280 -0
- package/dist/agent/process-tree.d.ts +22 -0
- package/dist/agent/process-tree.js +102 -0
- package/dist/agent/protocol-mode.d.ts +6 -0
- package/dist/agent/protocol-mode.js +51 -0
- package/dist/agent/protocol.d.ts +20 -1
- package/dist/agent/protocol.js +169 -39
- package/dist/agent/registry.d.ts +19 -0
- package/dist/agent/registry.js +247 -0
- package/dist/agent/runner.d.ts +127 -0
- package/dist/agent/runner.js +391 -0
- package/dist/agent/shell-ast.d.ts +11 -0
- package/dist/agent/shell-ast.js +204 -0
- package/dist/agent/shell-session.d.ts +51 -0
- package/dist/agent/shell-session.js +611 -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 +82 -0
- package/dist/agent/tools/append_file.d.ts +2 -0
- package/dist/agent/tools/append_file.js +28 -0
- package/dist/agent/tools/apply_patch.d.ts +10 -0
- package/dist/agent/tools/apply_patch.js +92 -0
- package/dist/agent/tools/copy_file.d.ts +2 -0
- package/dist/agent/tools/copy_file.js +53 -0
- package/dist/agent/tools/delete_file.d.ts +2 -0
- package/dist/agent/tools/delete_file.js +43 -0
- package/dist/agent/tools/grep.d.ts +2 -0
- package/dist/agent/tools/grep.js +54 -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 +26 -0
- package/dist/agent/tools/move_path.d.ts +9 -0
- package/dist/agent/tools/move_path.js +73 -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 +81 -0
- package/dist/agent/tools/read_many.d.ts +2 -0
- package/dist/agent/tools/read_many.js +43 -0
- package/dist/agent/tools/readers.d.ts +58 -0
- package/dist/agent/tools/readers.js +426 -0
- package/dist/agent/tools/replace_in_file.d.ts +2 -0
- package/dist/agent/tools/replace_in_file.js +63 -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 +65 -0
- package/dist/agent/tools/types.js +1 -0
- package/dist/agent/tools/validate.d.ts +19 -0
- package/dist/agent/tools/validate.js +92 -0
- package/dist/agent/tools/write_file.d.ts +2 -0
- package/dist/agent/tools/write_file.js +28 -0
- package/dist/agent/tools.d.ts +3 -1
- package/dist/agent/tools.js +2 -377
- package/dist/app.d.ts +2 -1
- package/dist/app.js +846 -173
- package/dist/cli-main.js +363 -0
- package/dist/cli.js +444 -333
- package/dist/commands/about.js +1 -1
- 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 +1 -0
- package/dist/commands/clear.js +1 -1
- package/dist/commands/compact.d.ts +22 -0
- package/dist/commands/compact.js +34 -0
- package/dist/commands/context.d.ts +7 -0
- package/dist/commands/context.js +25 -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/expand.d.ts +10 -0
- package/dist/commands/expand.js +8 -0
- package/dist/commands/index.js +24 -0
- package/dist/commands/jobs.d.ts +7 -0
- package/dist/commands/jobs.js +17 -0
- package/dist/commands/kill.d.ts +7 -0
- package/dist/commands/kill.js +13 -0
- package/dist/commands/login.d.ts +6 -4
- package/dist/commands/login.js +24 -22
- package/dist/commands/logout.js +8 -16
- package/dist/commands/model.d.ts +1 -0
- package/dist/commands/model.js +10 -22
- package/dist/commands/provider.d.ts +1 -2
- package/dist/commands/provider.js +4 -11
- package/dist/commands/skills.js +34 -4
- package/dist/commands/steer.d.ts +11 -0
- package/dist/commands/steer.js +13 -0
- package/dist/commands/threads.d.ts +9 -0
- package/dist/commands/threads.js +26 -0
- package/dist/commands/whoami.js +6 -5
- package/dist/config/store.d.ts +53 -12
- package/dist/config/store.js +163 -73
- package/dist/config/trust.d.ts +30 -8
- package/dist/config/trust.js +139 -14
- package/dist/headless.d.ts +7 -0
- package/dist/headless.js +213 -0
- package/dist/index.js +19 -3
- package/dist/lib/debug.d.ts +2 -0
- package/dist/lib/debug.js +22 -0
- package/dist/lib/diagnostics.d.ts +48 -4
- package/dist/lib/diagnostics.js +90 -8
- package/dist/lib/highlight-subset.d.ts +21 -0
- package/dist/lib/highlight-subset.js +88 -0
- package/dist/lib/http.d.ts +11 -1
- package/dist/lib/http.js +62 -4
- package/dist/lib/lifecycle.d.ts +5 -0
- package/dist/lib/lifecycle.js +62 -0
- package/dist/lib/markdown.js +22 -4
- 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/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/providers/anthropic.d.ts +1 -0
- package/dist/providers/anthropic.js +12 -0
- package/dist/providers/custom.d.ts +9 -0
- package/dist/providers/deepseek.d.ts +10 -0
- package/dist/providers/deepseek.js +3 -2
- package/dist/providers/google.d.ts +9 -0
- package/dist/providers/groq.d.ts +10 -0
- package/dist/providers/groq.js +2 -1
- package/dist/providers/index.d.ts +10 -201
- package/dist/providers/index.js +30 -0
- package/dist/providers/mistral.d.ts +10 -0
- package/dist/providers/mistral.js +2 -1
- package/dist/providers/moonshot.d.ts +10 -0
- package/dist/providers/moonshot.js +2 -1
- package/dist/providers/ollama.d.ts +9 -0
- package/dist/providers/openai.d.ts +10 -0
- package/dist/providers/openai.js +2 -1
- package/dist/providers/openaiCompatible.d.ts +33 -2
- package/dist/providers/openaiCompatible.js +78 -16
- package/dist/providers/openrouter.d.ts +10 -0
- package/dist/providers/openrouter.js +2 -1
- package/dist/providers/qwen.d.ts +10 -0
- package/dist/providers/qwen.js +2 -1
- package/dist/providers/spec.d.ts +12 -0
- package/dist/providers/spec.js +29 -0
- package/dist/providers/xai.d.ts +10 -0
- package/dist/providers/xai.js +2 -1
- package/dist/providers/zhipu.d.ts +10 -0
- package/dist/providers/zhipu.js +2 -1
- package/dist/session/log.d.ts +49 -14
- package/dist/session/log.js +236 -11
- package/dist/session/projection.d.ts +76 -0
- package/dist/session/projection.js +526 -0
- package/dist/skills/manager.d.ts +15 -0
- package/dist/skills/manager.js +135 -18
- package/dist/types.d.ts +75 -1
- package/dist/ui/Message.d.ts +1 -7
- package/dist/ui/Message.js +13 -2
- package/dist/ui/Picker.d.ts +8 -0
- package/dist/ui/Picker.js +24 -0
- package/dist/ui/Prompt.d.ts +1 -2
- package/dist/ui/Prompt.js +2 -2
- package/dist/ui/Spinner.js +11 -5
- package/dist/ui/StatusBar.d.ts +2 -1
- package/dist/ui/StatusBar.js +6 -5
- package/package.json +3 -2
- package/src/agent/approval-json.ts +103 -0
- package/src/agent/approval-native.ts +168 -0
- package/src/agent/approval-store.ts +38 -0
- package/src/agent/approval.ts +94 -0
- package/src/agent/compaction.ts +321 -0
- package/src/agent/context.ts +97 -0
- package/src/agent/guards.ts +1239 -19
- package/src/agent/ignore.ts +147 -0
- package/src/agent/policy/defaults.ts +34 -0
- package/src/agent/policy/egress.ts +335 -0
- package/src/agent/process-tree.ts +114 -0
- package/src/agent/protocol-mode.ts +59 -0
- package/src/agent/protocol.ts +191 -40
- package/src/agent/registry.ts +270 -0
- package/src/agent/runner.ts +486 -0
- package/src/agent/shell-ast.ts +227 -0
- package/src/agent/shell-session.ts +673 -0
- package/src/agent/stream.ts +143 -0
- package/src/agent/tool-schemas.ts +86 -0
- package/src/agent/tools/append_file.ts +30 -0
- package/src/agent/tools/apply_patch.ts +95 -0
- package/src/agent/tools/copy_file.ts +56 -0
- package/src/agent/tools/delete_file.ts +45 -0
- package/src/agent/tools/grep.ts +57 -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 +28 -0
- package/src/agent/tools/move_path.ts +83 -0
- package/src/agent/tools/paths.ts +92 -0
- package/src/agent/tools/pwd.ts +12 -0
- package/src/agent/tools/read_file.ts +84 -0
- package/src/agent/tools/read_many.ts +45 -0
- package/src/agent/tools/readers.ts +491 -0
- package/src/agent/tools/replace_in_file.ts +66 -0
- package/src/agent/tools/shell.ts +53 -0
- package/src/agent/tools/stat_path.ts +33 -0
- package/src/agent/tools/types.ts +84 -0
- package/src/agent/tools/validate.ts +104 -0
- package/src/agent/tools/write_file.ts +30 -0
- package/src/agent/tools.ts +3 -377
- package/src/app.ts +853 -170
- package/src/{cli.ts → cli-main.ts} +64 -9
- package/src/commands/about.ts +1 -1
- 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 +1 -1
- package/src/commands/compact.ts +35 -0
- package/src/commands/context.ts +26 -0
- package/src/commands/edit.ts +37 -0
- package/src/commands/effort.ts +49 -0
- package/src/commands/expand.ts +8 -0
- package/src/commands/index.ts +24 -0
- package/src/commands/jobs.ts +18 -0
- package/src/commands/kill.ts +14 -0
- package/src/commands/login.ts +24 -22
- package/src/commands/logout.ts +8 -16
- package/src/commands/model.ts +10 -22
- package/src/commands/provider.ts +4 -11
- package/src/commands/skills.ts +34 -4
- package/src/commands/steer.ts +13 -0
- package/src/commands/threads.ts +28 -0
- package/src/commands/whoami.ts +6 -5
- package/src/config/store.ts +168 -77
- package/src/config/trust.ts +181 -22
- package/src/headless.ts +225 -0
- package/src/index.ts +19 -3
- package/src/lib/debug.ts +24 -0
- package/src/lib/diagnostics.ts +90 -8
- package/src/lib/highlight-subset.ts +93 -0
- package/src/lib/http.ts +66 -4
- package/src/lib/lifecycle.ts +69 -0
- package/src/lib/markdown.ts +22 -4
- 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/patch/apply.ts +194 -0
- package/src/patch/parse.ts +131 -0
- package/src/patch/plan.ts +317 -0
- package/src/providers/anthropic.ts +13 -1
- package/src/providers/deepseek.ts +3 -2
- package/src/providers/groq.ts +2 -1
- package/src/providers/index.ts +32 -1
- package/src/providers/mistral.ts +2 -1
- package/src/providers/moonshot.ts +2 -1
- package/src/providers/openai.ts +2 -1
- package/src/providers/openaiCompatible.ts +104 -19
- package/src/providers/openrouter.ts +2 -1
- package/src/providers/qwen.ts +2 -1
- package/src/providers/spec.ts +42 -0
- package/src/providers/xai.ts +2 -1
- package/src/providers/zhipu.ts +2 -1
- package/src/session/log.ts +311 -29
- package/src/session/projection.ts +621 -0
- package/src/skills/manager.ts +140 -18
- package/src/types/highlight-shims.d.ts +14 -0
- package/src/types.ts +79 -2
- package/src/ui/Message.ts +13 -2
- package/src/ui/Picker.ts +24 -0
- package/src/ui/Prompt.ts +2 -2
- package/src/ui/Spinner.ts +11 -5
- package/src/ui/StatusBar.ts +6 -5
- package/tsconfig.json +1 -0
- /package/dist/{cli.d.ts → cli-main.d.ts} +0 -0
package/dist/agent/protocol.d.ts
CHANGED
|
@@ -1,4 +1,23 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface ToolCallRecord {
|
|
2
|
+
tool: string;
|
|
3
|
+
[key: string]: any;
|
|
4
|
+
}
|
|
5
|
+
export interface ToolCallParseResult {
|
|
6
|
+
calls: ToolCallRecord[];
|
|
7
|
+
/** End offset of the last complete construct that was consumed. */
|
|
8
|
+
consumedEnd: number;
|
|
9
|
+
/** True parse failures. Salvaged calls do not add an error. */
|
|
10
|
+
errors: string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Parse all complete tool calls, then salvage only the *remaining* text.
|
|
14
|
+
*
|
|
15
|
+
* Invariants (regression-tested):
|
|
16
|
+
* - a complete construct is consumed exactly once and never rescanned;
|
|
17
|
+
* - the salvage pass receives `raw.slice(consumedEnd)`, never the full text;
|
|
18
|
+
* - two adjacent normal calls plus a stray close tag stays at two calls.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseToolCalls(text: any): ToolCallParseResult;
|
|
2
21
|
export declare function stripToolCalls(text: any): any;
|
|
3
22
|
export declare function formatToolResults(results: any): string;
|
|
4
23
|
export declare const AGENT_SYSTEM_PROMPT: string;
|
package/dist/agent/protocol.js
CHANGED
|
@@ -1,18 +1,68 @@
|
|
|
1
|
-
const TOOL_RE = /<fastcode_tool>([\s\S]*?)<\/fastcode_tool>/g;
|
|
2
1
|
// Some models (notably DeepSeek) emit their native "DSML" tool-call markup
|
|
3
2
|
// instead of <fastcode_tool>. The markers look like `<|DSML|invoke ...>`
|
|
4
3
|
// (U+FF5C fullwidth bar, sometimes doubled or spaced). We canonicalize those
|
|
5
4
|
// markers back to plain angle-bracket tags and then parse the
|
|
6
5
|
// calls -> invoke -> parameter structure as a second dialect.
|
|
6
|
+
const FASTCODE_TOOL_OPEN = "<fastcode_tool>";
|
|
7
|
+
const FASTCODE_TOOL_CLOSE_RE = /<\/\s*fastcode_tool\s*>/gi;
|
|
8
|
+
const RESIDUAL_CLOSE_RE = /<\/\s*(?:fastcode_tool|parameter|invoke|calls)\s*>/i;
|
|
7
9
|
const DSML_OPEN_RE = /<\s*(?:[||]\s*)+DSML\s*(?:[||]\s*)+/gi;
|
|
8
10
|
const DSML_CLOSE_RE = /<\/\s*(?:[||]\s*)+DSML\s*(?:[||]\s*)+/gi;
|
|
9
|
-
const DSML_BLOCK_RE = /<([a-z_]*calls?)>([\s\S]*?)<\/\1
|
|
10
|
-
const DSML_INVOKE_RE = /<invoke\s+name="([^"]+)"\s*>([\s\S]*?)
|
|
11
|
-
const DSML_PARAM_RE = /<parameter\s+name="([^"]+)"([^>]*)>([\s\S]*?)
|
|
11
|
+
const DSML_BLOCK_RE = /<([a-z_]*calls?)>([\s\S]*?)<\/\s*\1\s*>/gi;
|
|
12
|
+
const DSML_INVOKE_RE = /<invoke\s+name="([^"]+)"\s*>([\s\S]*?)<\/\s*invoke\s*>/gi;
|
|
13
|
+
const DSML_PARAM_RE = /<parameter\s+name="([^"]+)"([^>]*)>([\s\S]*?)<\/\s*parameter\s*>/gi;
|
|
14
|
+
const INVOKE_OPEN_RE = /<invoke\s+name="([^"]+)"\s*>/gi;
|
|
12
15
|
/** Turn the DSML glyph markers into ordinary `<tag>` / `</tag>` tags. */
|
|
13
16
|
function canonicalizeDsml(text) {
|
|
14
17
|
return text.replace(DSML_CLOSE_RE, "</").replace(DSML_OPEN_RE, "<");
|
|
15
18
|
}
|
|
19
|
+
function invokeToCall(invoke) {
|
|
20
|
+
const call = { tool: invoke[1] };
|
|
21
|
+
for (const param of (invoke[2] ?? "").matchAll(DSML_PARAM_RE))
|
|
22
|
+
call[param[1]] = coerceDsmlValue(param[2] ?? "", param[3] ?? "");
|
|
23
|
+
return call;
|
|
24
|
+
}
|
|
25
|
+
function insideSpan(start, spans) {
|
|
26
|
+
return spans.some((span) => start >= span.start && start < span.end);
|
|
27
|
+
}
|
|
28
|
+
function matchFrom(text, from, re) {
|
|
29
|
+
re.lastIndex = from;
|
|
30
|
+
const match = re.exec(text);
|
|
31
|
+
if (!match || match.index === undefined)
|
|
32
|
+
return undefined;
|
|
33
|
+
return { start: match.index, end: match.index + match[0].length };
|
|
34
|
+
}
|
|
35
|
+
function parseJsonCall(body) {
|
|
36
|
+
try {
|
|
37
|
+
const parsed = JSON.parse(body.trim());
|
|
38
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed) && typeof parsed.tool === "string")
|
|
39
|
+
return parsed;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// Malformed JSON is reported by the caller.
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Recover a JSON object that starts at the beginning of `text` but may be
|
|
48
|
+
* followed by more markup (for example a drifted close tag). Returns the
|
|
49
|
+
* consumed prefix length so the caller can continue after it.
|
|
50
|
+
*/
|
|
51
|
+
function salvageJsonPrefix(text) {
|
|
52
|
+
let searchFrom = 0;
|
|
53
|
+
while (searchFrom < text.length) {
|
|
54
|
+
const end = text.indexOf("}", searchFrom);
|
|
55
|
+
if (end < 0)
|
|
56
|
+
return undefined;
|
|
57
|
+
const call = parseJsonCall(text.slice(0, end + 1));
|
|
58
|
+
if (call) {
|
|
59
|
+
const close = matchFrom(text, end + 1, FASTCODE_TOOL_CLOSE_RE);
|
|
60
|
+
return { call, consumed: close ? close.end : end + 1 };
|
|
61
|
+
}
|
|
62
|
+
searchFrom = end + 1;
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
16
66
|
function coerceDsmlValue(attrs, raw) {
|
|
17
67
|
const value = raw.replace(/^\n/, "").replace(/\n$/, "");
|
|
18
68
|
if (/\bstring\s*=\s*"true"/i.test(attrs))
|
|
@@ -32,44 +82,119 @@ function coerceDsmlValue(attrs, raw) {
|
|
|
32
82
|
return Number.isNaN(n) ? value : n;
|
|
33
83
|
}
|
|
34
84
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Parse all complete tool calls, then salvage only the *remaining* text.
|
|
87
|
+
*
|
|
88
|
+
* Invariants (regression-tested):
|
|
89
|
+
* - a complete construct is consumed exactly once and never rescanned;
|
|
90
|
+
* - the salvage pass receives `raw.slice(consumedEnd)`, never the full text;
|
|
91
|
+
* - two adjacent normal calls plus a stray close tag stays at two calls.
|
|
92
|
+
*/
|
|
93
|
+
export function parseToolCalls(text) {
|
|
94
|
+
const raw = canonicalizeDsml(String(text ?? ""));
|
|
42
95
|
const calls = [];
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
96
|
+
const errors = [];
|
|
97
|
+
const spans = [];
|
|
98
|
+
let cursor = 0;
|
|
99
|
+
while (cursor < raw.length) {
|
|
100
|
+
const open = raw.indexOf(FASTCODE_TOOL_OPEN, cursor);
|
|
101
|
+
if (open < 0)
|
|
102
|
+
break;
|
|
103
|
+
const innerStart = open + FASTCODE_TOOL_OPEN.length;
|
|
104
|
+
const close = matchFrom(raw, innerStart, FASTCODE_TOOL_CLOSE_RE);
|
|
105
|
+
if (!close) {
|
|
106
|
+
const salvaged = salvageJsonPrefix(raw.slice(innerStart));
|
|
107
|
+
if (salvaged) {
|
|
108
|
+
calls.push(salvaged.call);
|
|
109
|
+
cursor = innerStart + salvaged.consumed;
|
|
110
|
+
spans.push({ start: open, end: cursor });
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
errors.push("unclosed <fastcode_tool> block; re-emit a complete tool call");
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
const body = raw.slice(innerStart, close.start);
|
|
117
|
+
const full = parseJsonCall(body);
|
|
118
|
+
if (full) {
|
|
119
|
+
calls.push(full);
|
|
120
|
+
spans.push({ start: open, end: close.end });
|
|
121
|
+
cursor = close.end;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
// Closing drift: the JSON may be valid even when extra markup (or a
|
|
125
|
+
// second opener) sits before the matched close tag.
|
|
126
|
+
const prefix = salvageJsonPrefix(body);
|
|
127
|
+
if (prefix) {
|
|
128
|
+
calls.push(prefix.call);
|
|
129
|
+
cursor = innerStart + prefix.consumed;
|
|
130
|
+
spans.push({ start: open, end: cursor });
|
|
131
|
+
continue;
|
|
49
132
|
}
|
|
133
|
+
errors.push("invalid JSON inside <fastcode_tool>; re-emit a complete tool call");
|
|
134
|
+
spans.push({ start: open, end: close.end });
|
|
135
|
+
cursor = close.end;
|
|
50
136
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
137
|
+
// DSML dialect: parse each complete block once, then complete invokes that
|
|
138
|
+
// did not belong to a block. Spans prevent a second parse of the same text.
|
|
139
|
+
for (const block of raw.matchAll(DSML_BLOCK_RE)) {
|
|
140
|
+
const start = block.index ?? 0;
|
|
141
|
+
const end = start + block[0].length;
|
|
142
|
+
if (insideSpan(start, spans))
|
|
143
|
+
continue;
|
|
144
|
+
for (const invoke of (block[2] ?? "").matchAll(DSML_INVOKE_RE))
|
|
145
|
+
calls.push(invokeToCall(invoke));
|
|
146
|
+
spans.push({ start, end });
|
|
147
|
+
}
|
|
148
|
+
for (const invoke of raw.matchAll(DSML_INVOKE_RE)) {
|
|
149
|
+
const start = invoke.index ?? 0;
|
|
150
|
+
if (insideSpan(start, spans))
|
|
58
151
|
continue;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
152
|
+
calls.push(invokeToCall(invoke));
|
|
153
|
+
spans.push({ start, end: start + invoke[0].length });
|
|
154
|
+
}
|
|
155
|
+
const consumedEnd = spans.reduce((max, span) => Math.max(max, span.end), 0);
|
|
156
|
+
salvageResidual(raw.slice(consumedEnd), calls, errors);
|
|
157
|
+
return { calls, consumedEnd, errors };
|
|
158
|
+
}
|
|
159
|
+
function salvageResidual(tail, calls, errors) {
|
|
160
|
+
if (!tail.trim())
|
|
161
|
+
return;
|
|
162
|
+
let salvaged = false;
|
|
163
|
+
const toolOpen = tail.indexOf(FASTCODE_TOOL_OPEN);
|
|
164
|
+
if (toolOpen >= 0) {
|
|
165
|
+
const recovered = salvageJsonPrefix(tail.slice(toolOpen + FASTCODE_TOOL_OPEN.length));
|
|
166
|
+
if (recovered) {
|
|
167
|
+
calls.push(recovered.call);
|
|
168
|
+
salvaged = true;
|
|
63
169
|
}
|
|
64
|
-
|
|
65
|
-
|
|
170
|
+
else {
|
|
171
|
+
errors.push("unclosed <fastcode_tool> block; re-emit a complete tool call");
|
|
66
172
|
}
|
|
67
173
|
}
|
|
68
|
-
|
|
69
|
-
|
|
174
|
+
const openers = Array.from(tail.matchAll(INVOKE_OPEN_RE));
|
|
175
|
+
openers.forEach((opener, index) => {
|
|
176
|
+
const start = opener.index ?? 0;
|
|
177
|
+
const bodyStart = start + opener[0].length;
|
|
178
|
+
const nextStart = openers[index + 1]?.index ?? tail.length;
|
|
179
|
+
const call = invokeToCall({ 1: opener[1], 2: tail.slice(bodyStart, nextStart) });
|
|
180
|
+
if (Object.keys(call).length > 1) {
|
|
181
|
+
calls.push(call);
|
|
182
|
+
salvaged = true;
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
errors.push(`incomplete invoke for "${opener[1]}"; re-emit a complete tool call`);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
if (!salvaged && openers.length === 0 && toolOpen < 0 && RESIDUAL_CLOSE_RE.test(tail))
|
|
189
|
+
errors.push("stray closing tag without an opening tool call; re-emit a complete tool call");
|
|
70
190
|
}
|
|
71
191
|
export function stripToolCalls(text) {
|
|
72
|
-
return canonicalizeDsml(text
|
|
192
|
+
return canonicalizeDsml(String(text ?? ""))
|
|
193
|
+
.replace(/<fastcode_tool>[\s\S]*?<\/\s*fastcode_tool\s*>/gi, "")
|
|
194
|
+
.replace(DSML_BLOCK_RE, "")
|
|
195
|
+
.replace(/<fastcode_tool>[\s\S]*$/i, "")
|
|
196
|
+
.replace(/<\/\s*(?:fastcode_tool|parameter|invoke|calls)\s*>/gi, "")
|
|
197
|
+
.trim();
|
|
73
198
|
}
|
|
74
199
|
export function formatToolResults(results) {
|
|
75
200
|
const blocks = results.map((r, index) => {
|
|
@@ -97,20 +222,25 @@ You can inspect and change the user's machine by emitting exactly one or more to
|
|
|
97
222
|
Available tools:
|
|
98
223
|
- shell: {"tool":"shell","command":"command to run"}
|
|
99
224
|
- shell with timeout: {"tool":"shell","command":"npm test","timeoutMs":120000}
|
|
225
|
+
- shell poll (long commands return a session id): {"tool":"shell","session_id":"sh_..."}
|
|
226
|
+
Polling returns only new output; use waitMs to wait briefly for more.
|
|
100
227
|
- pwd: {"tool":"pwd"}
|
|
101
228
|
- read_file: {"tool":"read_file","path":"path/to/file"}
|
|
102
|
-
-
|
|
229
|
+
- read_file range: {"tool":"read_file","path":"large.ts","offset":800,"limit":50}
|
|
230
|
+
offset is 1-based; use it instead of reading a large file from the start.
|
|
231
|
+
- read_many: {"tool":"read_many","paths":["file1","file2"],"maxBytes":300000}
|
|
103
232
|
- write_file: {"tool":"write_file","path":"path/to/file","content":"full file contents"}
|
|
104
233
|
- append_file: {"tool":"append_file","path":"path/to/file","content":"text to append"}
|
|
105
234
|
- apply_patch: {"tool":"apply_patch","patch":"*** Begin Patch\\n*** Update File: path\\n@@\\n-old\\n+new\\n*** End Patch\\n"}
|
|
106
|
-
- list_files: {"tool":"list_files","path":"optional/path","maxDepth":2}
|
|
107
|
-
-
|
|
108
|
-
-
|
|
235
|
+
- list_files: {"tool":"list_files","path":"optional/path","maxDepth":2,"detail":false}
|
|
236
|
+
- read_file is capped (default 200KB); grep skips files over 1MB. .fastcodeignore is honored.
|
|
237
|
+
- grep: {"tool":"grep","pattern":"text or regex","path":"optional/path","caseSensitive":false,"glob":"*.ts","context":2,"maxResults":200}
|
|
238
|
+
- replace_in_file: {"tool":"replace_in_file","path":"path/to/file","find":"exact text","replace":"replacement","expectedMatches":1}
|
|
109
239
|
- stat_path: {"tool":"stat_path","path":"path/to/file"}
|
|
110
240
|
- make_dir: {"tool":"make_dir","path":"path/to/dir"}
|
|
111
|
-
- copy_file: {"tool":"copy_file","from":"src","to":"dest"}
|
|
241
|
+
- copy_file: {"tool":"copy_file","from":"src","to":"dest","overwrite":false}
|
|
112
242
|
- move_path: {"tool":"move_path","from":"src","to":"dest"}
|
|
113
|
-
- delete_file: {"tool":"delete_file","path":"path/to/file"}
|
|
243
|
+
- delete_file: {"tool":"delete_file","path":"path/to/file","recursive":false}
|
|
114
244
|
|
|
115
245
|
Rules:
|
|
116
246
|
- Prefer inspecting files before editing.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type ApprovalDecision } from "./approval.js";
|
|
2
|
+
import type { Tool, ToolCall, ToolCtx, ToolResult } from "./tools/types.js";
|
|
3
|
+
export declare function assertToolContract(tools?: Tool[]): void;
|
|
4
|
+
export declare function toolCatalogHash(tools?: Tool[]): string;
|
|
5
|
+
export declare class ToolRegistry {
|
|
6
|
+
private readonly tools;
|
|
7
|
+
private readonly catalogHash;
|
|
8
|
+
constructor(tools?: Tool[]);
|
|
9
|
+
get(name: string): Tool | undefined;
|
|
10
|
+
list(): Tool[];
|
|
11
|
+
approve(call: ToolCall, ctx: ToolCtx): ApprovalDecision;
|
|
12
|
+
execute(call: ToolCall, ctx: ToolCtx): Promise<ToolResult>;
|
|
13
|
+
}
|
|
14
|
+
export declare const registry: ToolRegistry;
|
|
15
|
+
export declare function runAgentTool(call: ToolCall, cwd?: string, options?: {
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
turn?: number;
|
|
18
|
+
requestApproval?: import("./approval.js").ApprovalResolver;
|
|
19
|
+
}): Promise<ToolResult>;
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isTrusted } from "../config/trust.js";
|
|
3
|
+
import { DEFAULT_ALLOW_OPTIONS, approvalRequestId, } from "./approval.js";
|
|
4
|
+
import { approvalKey, isPreApproved, rememberApproval } from "./approval-store.js";
|
|
5
|
+
import { evaluateShellCommand } from "./guards.js";
|
|
6
|
+
import { TOOLS } from "./tools/index.js";
|
|
7
|
+
import { loadConfig } from "../config/store.js";
|
|
8
|
+
import { egressAudit, evaluateEgressCall, evaluateEgressOutput, formatEgressDenial, normalizeEgressConfig, } from "./policy/egress.js";
|
|
9
|
+
const STATE_SCOPES = new Set(["call", "turn", "session"]);
|
|
10
|
+
const REVALIDATE_KEYS = new Set(["trust", "sandboxRoots", "schema", "modelCaps"]);
|
|
11
|
+
const TOOL_ERROR_CODES = new Set([
|
|
12
|
+
"ENOENT", "EACCES", "EEXIST", "ENOTDIR", "EISDIR", "EXDEV", "INVALID_ARGS",
|
|
13
|
+
"SANDBOX_DENY", "APPROVAL_DENY", "EGRESS_DENY", "NOT_FOUND", "ABORTED", "UNKNOWN",
|
|
14
|
+
]);
|
|
15
|
+
function errorCode(error) {
|
|
16
|
+
const code = String(error?.code ?? "");
|
|
17
|
+
if (TOOL_ERROR_CODES.has(code))
|
|
18
|
+
return code;
|
|
19
|
+
const message = String(error?.message ?? error ?? "");
|
|
20
|
+
if (/outside sandbox/i.test(message))
|
|
21
|
+
return "SANDBOX_DENY";
|
|
22
|
+
if (/not trusted/i.test(message))
|
|
23
|
+
return "APPROVAL_DENY";
|
|
24
|
+
if (/aborted/i.test(message))
|
|
25
|
+
return "ABORTED";
|
|
26
|
+
return "UNKNOWN";
|
|
27
|
+
}
|
|
28
|
+
function unique(values) {
|
|
29
|
+
return Array.from(new Set((values ?? []).filter(Boolean)));
|
|
30
|
+
}
|
|
31
|
+
function normalizeSideEffects(sideEffects) {
|
|
32
|
+
if (!sideEffects)
|
|
33
|
+
return undefined;
|
|
34
|
+
const touched = unique(sideEffects.touched);
|
|
35
|
+
const created = unique(sideEffects.created);
|
|
36
|
+
const removed = unique(sideEffects.removed);
|
|
37
|
+
if (touched.length === 0 && created.length === 0 && removed.length === 0)
|
|
38
|
+
return undefined;
|
|
39
|
+
return {
|
|
40
|
+
...(touched.length > 0 ? { touched } : {}),
|
|
41
|
+
...(created.length > 0 ? { created } : {}),
|
|
42
|
+
...(removed.length > 0 ? { removed } : {}),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export function assertToolContract(tools = TOOLS) {
|
|
46
|
+
const names = new Set();
|
|
47
|
+
for (const tool of tools) {
|
|
48
|
+
if (!tool.name || typeof tool.name !== "string")
|
|
49
|
+
throw new Error("tool contract: every tool needs a name");
|
|
50
|
+
if (names.has(tool.name))
|
|
51
|
+
throw new Error(`tool contract: duplicate tool name ${tool.name}`);
|
|
52
|
+
names.add(tool.name);
|
|
53
|
+
if (!STATE_SCOPES.has(tool.stateScope))
|
|
54
|
+
throw new Error(`tool contract: ${tool.name} has an invalid stateScope`);
|
|
55
|
+
for (const key of tool.revalidate ?? []) {
|
|
56
|
+
if (!REVALIDATE_KEYS.has(key))
|
|
57
|
+
throw new Error(`tool contract: ${tool.name} has an invalid revalidate key ${String(key)}`);
|
|
58
|
+
}
|
|
59
|
+
if (tool.requiresTrust && !(tool.revalidate ?? []).includes("trust")) {
|
|
60
|
+
throw new Error(`tool contract: ${tool.name} requiresTrust but does not revalidate "trust"`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export function toolCatalogHash(tools = TOOLS) {
|
|
65
|
+
const catalog = tools
|
|
66
|
+
.map((tool) => ({
|
|
67
|
+
name: tool.name,
|
|
68
|
+
trust: Boolean(tool.requiresTrust),
|
|
69
|
+
scope: tool.stateScope,
|
|
70
|
+
revalidate: [...(tool.revalidate ?? [])].sort(),
|
|
71
|
+
}))
|
|
72
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
73
|
+
return createHash("sha1").update(JSON.stringify(catalog)).digest("hex").slice(0, 8);
|
|
74
|
+
}
|
|
75
|
+
export class ToolRegistry {
|
|
76
|
+
tools = new Map();
|
|
77
|
+
catalogHash;
|
|
78
|
+
constructor(tools = TOOLS) {
|
|
79
|
+
assertToolContract(tools);
|
|
80
|
+
for (const tool of tools)
|
|
81
|
+
this.tools.set(tool.name, tool);
|
|
82
|
+
this.catalogHash = toolCatalogHash(tools);
|
|
83
|
+
}
|
|
84
|
+
get(name) {
|
|
85
|
+
return this.tools.get(name);
|
|
86
|
+
}
|
|
87
|
+
list() {
|
|
88
|
+
return Array.from(this.tools.values());
|
|
89
|
+
}
|
|
90
|
+
approve(call, ctx) {
|
|
91
|
+
const tool = this.get(call.tool);
|
|
92
|
+
if (!tool)
|
|
93
|
+
return { kind: "deny", reason: `unknown tool: ${String(call.tool)}` };
|
|
94
|
+
if (tool.name === "shell") {
|
|
95
|
+
const verdict = evaluateShellCommand(call.command);
|
|
96
|
+
if (verdict.action === "hard_block")
|
|
97
|
+
return { kind: "deny", reason: verdict.output, code: verdict.code, target: verdict.target };
|
|
98
|
+
if (verdict.action === "soft_gate") {
|
|
99
|
+
const key = approvalKey(tool.name, call, ctx.cwd);
|
|
100
|
+
if (isPreApproved(key, this.catalogHash))
|
|
101
|
+
return { kind: "allow", scope: "once" };
|
|
102
|
+
return { kind: "pending", request: shellApprovalRequest(call, verdict) };
|
|
103
|
+
}
|
|
104
|
+
if (!verdict.allow)
|
|
105
|
+
return { kind: "deny", reason: verdict.output };
|
|
106
|
+
}
|
|
107
|
+
if ((tool.revalidate ?? []).includes("trust") && !isTrusted(ctx.cwd)) {
|
|
108
|
+
return {
|
|
109
|
+
kind: "deny",
|
|
110
|
+
reason: `project is not trusted: ${ctx.cwd}. Run \`fastcode trust ${ctx.cwd}\` or accept the trust prompt.`,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return { kind: "allow", scope: "once" };
|
|
114
|
+
}
|
|
115
|
+
async execute(call, ctx) {
|
|
116
|
+
const tool = this.get(call.tool);
|
|
117
|
+
if (!tool)
|
|
118
|
+
return { ok: false, output: `unknown tool: ${String(call.tool)}`, code: "INVALID_ARGS" };
|
|
119
|
+
const toolCtx = { ...ctx, catalogHash: this.catalogHash };
|
|
120
|
+
const prepared = tool.prepare(call, toolCtx);
|
|
121
|
+
if (prepared.ok === false)
|
|
122
|
+
return { ok: false, output: prepared.output, code: prepared.code ?? "INVALID_ARGS", hint: prepared.hint };
|
|
123
|
+
const egressConfig = normalizeEgressConfig(loadConfig().egress);
|
|
124
|
+
const preEgress = evaluateEgressCall(call, ctx.cwd, egressConfig);
|
|
125
|
+
if (preEgress.action === "deny") {
|
|
126
|
+
const egressDecision = preEgress.decision;
|
|
127
|
+
return {
|
|
128
|
+
ok: false,
|
|
129
|
+
output: formatEgressDenial(egressDecision),
|
|
130
|
+
code: "EGRESS_DENY",
|
|
131
|
+
hint: "Egress policy blocked this call before execution; no approval request was created.",
|
|
132
|
+
egress: egressAudit(egressDecision),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const preAudit = preEgress.decision ? egressAudit(preEgress.decision) : undefined;
|
|
136
|
+
const decision = this.approve(call, toolCtx);
|
|
137
|
+
if (decision.kind === "deny") {
|
|
138
|
+
const output = decision.reason ?? "denied by approval policy";
|
|
139
|
+
if (tool.name === "shell" && (decision.code === "sensitive_file_egress" || decision.code === "credential_read")) {
|
|
140
|
+
return {
|
|
141
|
+
ok: false,
|
|
142
|
+
output,
|
|
143
|
+
code: "EGRESS_DENY",
|
|
144
|
+
egress: {
|
|
145
|
+
outlet: "shell.command",
|
|
146
|
+
...(decision.target ? { target: decision.target } : {}),
|
|
147
|
+
rule: decision.code,
|
|
148
|
+
mode: "block",
|
|
149
|
+
wouldBlock: true,
|
|
150
|
+
code: "EGRESS_DENY",
|
|
151
|
+
reason: decision.code,
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return { ok: false, output, code: "APPROVAL_DENY" };
|
|
156
|
+
}
|
|
157
|
+
if (decision.kind === "pending") {
|
|
158
|
+
if (!ctx.requestApproval)
|
|
159
|
+
return { ok: false, output: "approval required, but no approval resolver is registered (fail closed)", code: "APPROVAL_DENY" };
|
|
160
|
+
let resolved;
|
|
161
|
+
try {
|
|
162
|
+
resolved = await ctx.requestApproval(decision.request);
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
return { ok: false, output: `approval failed (fail closed): ${err?.message ?? String(err)}`, code: "APPROVAL_DENY" };
|
|
166
|
+
}
|
|
167
|
+
if (!resolved || resolved.kind === "pending")
|
|
168
|
+
return { ok: false, output: "approval unresolved; nothing was executed (fail closed)", code: "APPROVAL_DENY" };
|
|
169
|
+
if (resolved.kind !== "allow") {
|
|
170
|
+
const reason = resolved.kind === "deny" ? resolved.reason : resolved.kind === "feedback" ? resolved.text : `decision ${resolved.kind} requires a later batch`;
|
|
171
|
+
return { ok: false, output: `approval denied: ${reason ?? "no reason"}`, code: "APPROVAL_DENY" };
|
|
172
|
+
}
|
|
173
|
+
rememberApproval(approvalKey(tool.name, call, ctx.cwd), this.catalogHash, resolved.scope);
|
|
174
|
+
}
|
|
175
|
+
try {
|
|
176
|
+
let result = await tool.execute(call, toolCtx);
|
|
177
|
+
result = {
|
|
178
|
+
...result,
|
|
179
|
+
sideEffects: normalizeSideEffects(result.sideEffects),
|
|
180
|
+
...(preAudit && !result.egress ? { egress: preAudit } : {}),
|
|
181
|
+
};
|
|
182
|
+
const postEgress = evaluateEgressOutput(call.tool, result.output, egressConfig);
|
|
183
|
+
if (postEgress.action === "deny") {
|
|
184
|
+
const egressDecision = postEgress.decision;
|
|
185
|
+
return {
|
|
186
|
+
ok: false,
|
|
187
|
+
output: formatEgressDenial(egressDecision),
|
|
188
|
+
code: "EGRESS_DENY",
|
|
189
|
+
hint: "Egress policy blocked this tool output before it reached the model.",
|
|
190
|
+
sideEffects: result.sideEffects,
|
|
191
|
+
egress: egressAudit(egressDecision),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
if (postEgress.decision)
|
|
195
|
+
result = { ...result, egress: egressAudit(postEgress.decision) };
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
const output = err?.message ?? String(err);
|
|
200
|
+
const postEgress = evaluateEgressOutput(call.tool, output, egressConfig);
|
|
201
|
+
if (postEgress.action === "deny") {
|
|
202
|
+
const egressDecision = postEgress.decision;
|
|
203
|
+
return {
|
|
204
|
+
ok: false,
|
|
205
|
+
output: formatEgressDenial(egressDecision),
|
|
206
|
+
code: "EGRESS_DENY",
|
|
207
|
+
hint: "Egress policy blocked this error output before it reached the model.",
|
|
208
|
+
egress: egressAudit(egressDecision),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
ok: false,
|
|
213
|
+
output,
|
|
214
|
+
code: errorCode(err),
|
|
215
|
+
...(preAudit ? { egress: preAudit } : {}),
|
|
216
|
+
...(postEgress.decision ? { egress: egressAudit(postEgress.decision) } : {}),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function shellApprovalRequest(call, verdict) {
|
|
222
|
+
const options = DEFAULT_ALLOW_OPTIONS.map((option) => ({ ...option }));
|
|
223
|
+
const denyIndex = options.findIndex((option) => option.kind === "deny");
|
|
224
|
+
const target = verdict.target ?? "";
|
|
225
|
+
const prompt = `Shell command needs approval: ${verdict.code}${target ? ` (${target})` : ""}`;
|
|
226
|
+
return {
|
|
227
|
+
id: approvalRequestId(),
|
|
228
|
+
tool: "shell",
|
|
229
|
+
args: call,
|
|
230
|
+
reason: "destructive",
|
|
231
|
+
options,
|
|
232
|
+
tier: 2,
|
|
233
|
+
prompt,
|
|
234
|
+
source: "system",
|
|
235
|
+
defaultIndex: denyIndex >= 0 ? denyIndex : 0,
|
|
236
|
+
timeoutMs: 60_000,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
export const registry = new ToolRegistry();
|
|
240
|
+
export async function runAgentTool(call, cwd = process.cwd(), options = {}) {
|
|
241
|
+
return registry.execute(call, {
|
|
242
|
+
cwd,
|
|
243
|
+
signal: options.signal,
|
|
244
|
+
turn: options.turn,
|
|
245
|
+
requestApproval: options.requestApproval,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { ChatChunk, ChatMessage, NativeToolCall, Provider, ReasoningEffort, ToolSchema } from "../types.js";
|
|
2
|
+
import type { ActiveFile } from "../session/log.js";
|
|
3
|
+
import type { SideEffects } from "./tools/types.js";
|
|
4
|
+
import { type CompactionConfig } from "./compaction.js";
|
|
5
|
+
import { type ToolProtocol } from "./protocol-mode.js";
|
|
6
|
+
import type { ApprovalResolver } from "./approval.js";
|
|
7
|
+
export interface ToolCall {
|
|
8
|
+
tool: string;
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
export interface LoopDetectionOptions {
|
|
12
|
+
readOnlyThreshold?: number;
|
|
13
|
+
sideEffectThreshold?: number;
|
|
14
|
+
window?: number;
|
|
15
|
+
exemptReadOnly?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface RunOptions {
|
|
18
|
+
provider: Provider;
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
model: string;
|
|
21
|
+
messages?: ChatMessage[];
|
|
22
|
+
prompt: string;
|
|
23
|
+
system?: string;
|
|
24
|
+
maxTurns?: number;
|
|
25
|
+
allowTools?: boolean;
|
|
26
|
+
tokenBudget?: number;
|
|
27
|
+
costBudgetUsd?: number;
|
|
28
|
+
signal?: AbortSignal;
|
|
29
|
+
cwd?: string;
|
|
30
|
+
idleTimeoutMs?: number;
|
|
31
|
+
contextBudgetChars?: number;
|
|
32
|
+
compaction?: CompactionConfig;
|
|
33
|
+
contextWindow?: number;
|
|
34
|
+
serverOpaqueState?: string;
|
|
35
|
+
loopDetection?: LoopDetectionOptions;
|
|
36
|
+
reasoningEffort?: ReasoningEffort;
|
|
37
|
+
protocol?: ToolProtocol;
|
|
38
|
+
tools?: ToolSchema[];
|
|
39
|
+
onProtocolFallback?: (message: string) => void;
|
|
40
|
+
onNativeOk?: () => void;
|
|
41
|
+
takeSteering?: () => string[];
|
|
42
|
+
onDelta?: (delta: string, text: string) => void;
|
|
43
|
+
requestApproval?: ApprovalResolver;
|
|
44
|
+
}
|
|
45
|
+
export type AgentStopReason = "complete" | "max_turns" | "token_budget" | "cost_budget" | "loop_detected" | "aborted" | "incomplete" | "stream_error";
|
|
46
|
+
export type AgentErrorKind = "token_budget" | "cost_budget" | "loop_detected" | "aborted" | "stream_error";
|
|
47
|
+
export type AgentEvent = {
|
|
48
|
+
type: "compaction";
|
|
49
|
+
beforeChars: number;
|
|
50
|
+
afterChars: number;
|
|
51
|
+
beforeTokens: number;
|
|
52
|
+
afterTokens: number;
|
|
53
|
+
droppedMessages: number;
|
|
54
|
+
keptMessages: number;
|
|
55
|
+
reason: "threshold" | "heap";
|
|
56
|
+
source: "local" | "server";
|
|
57
|
+
summaryFile?: string;
|
|
58
|
+
} | {
|
|
59
|
+
type: "assistant_raw";
|
|
60
|
+
text: string;
|
|
61
|
+
toolCalls?: NativeToolCall[];
|
|
62
|
+
} | {
|
|
63
|
+
type: "assistant_text";
|
|
64
|
+
text: string;
|
|
65
|
+
} | {
|
|
66
|
+
type: "tool_call";
|
|
67
|
+
id: string;
|
|
68
|
+
call: ToolCall;
|
|
69
|
+
native?: boolean;
|
|
70
|
+
} | {
|
|
71
|
+
type: "tool_result";
|
|
72
|
+
id: string;
|
|
73
|
+
call: ToolCall;
|
|
74
|
+
ok: boolean;
|
|
75
|
+
output: string;
|
|
76
|
+
sideEffects?: SideEffects;
|
|
77
|
+
code?: string;
|
|
78
|
+
hint?: string;
|
|
79
|
+
truncated?: boolean;
|
|
80
|
+
totalLines?: number;
|
|
81
|
+
returnedLines?: number;
|
|
82
|
+
totalBytes?: number;
|
|
83
|
+
returnedBytes?: number;
|
|
84
|
+
filesRead?: number;
|
|
85
|
+
filesSkipped?: number;
|
|
86
|
+
matches?: number;
|
|
87
|
+
} | {
|
|
88
|
+
type: "egress_denied";
|
|
89
|
+
outlet: string;
|
|
90
|
+
target?: string;
|
|
91
|
+
rule: string;
|
|
92
|
+
mode: "audit" | "block";
|
|
93
|
+
wouldBlock: boolean;
|
|
94
|
+
code: "EGRESS_DENY";
|
|
95
|
+
reason: string;
|
|
96
|
+
} | {
|
|
97
|
+
type: "usage";
|
|
98
|
+
usage: NonNullable<ChatChunk["usage"]>;
|
|
99
|
+
} | {
|
|
100
|
+
type: "loop_detected";
|
|
101
|
+
fingerprint: string;
|
|
102
|
+
occurrences: number;
|
|
103
|
+
warned: boolean;
|
|
104
|
+
} | {
|
|
105
|
+
type: "notice";
|
|
106
|
+
text: string;
|
|
107
|
+
} | {
|
|
108
|
+
type: "error";
|
|
109
|
+
kind: AgentErrorKind;
|
|
110
|
+
message: string;
|
|
111
|
+
text?: string;
|
|
112
|
+
used?: number;
|
|
113
|
+
} | {
|
|
114
|
+
type: "checkpoint";
|
|
115
|
+
turn: number;
|
|
116
|
+
cwd: string;
|
|
117
|
+
providerId: string;
|
|
118
|
+
model: string;
|
|
119
|
+
activeFiles: ActiveFile[];
|
|
120
|
+
} | {
|
|
121
|
+
type: "done";
|
|
122
|
+
history: ChatMessage[];
|
|
123
|
+
finalText: string;
|
|
124
|
+
stopReason: AgentStopReason;
|
|
125
|
+
};
|
|
126
|
+
export declare const LOOP_WARNING: string;
|
|
127
|
+
export declare function runAgent(options: RunOptions): AsyncGenerator<AgentEvent, void, void>;
|