@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
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { formatToolResults } from "../agent/protocol.js";
|
|
4
|
+
import { toolCatalogHash } from "../agent/registry.js";
|
|
5
|
+
import { projectionPath, readSession } from "./log.js";
|
|
6
|
+
const READ_ONLY_TOOLS = new Set(["read_file", "read_many", "grep", "list_files", "stat_path", "pwd"]);
|
|
7
|
+
const IDEMPOTENT_TOOLS = new Set(["write_file"]);
|
|
8
|
+
const MAX_MESSAGE_CHARS = 8_000;
|
|
9
|
+
const MAX_PROJECTION_BYTES = 200_000;
|
|
10
|
+
const TOOL_FOLD_CHARS = 600;
|
|
11
|
+
export function unpairedToolNotice(tool) {
|
|
12
|
+
if (READ_ONLY_TOOLS.has(tool))
|
|
13
|
+
return "执行中断(只读工具,可安全重试)。";
|
|
14
|
+
if (IDEMPOTENT_TOOLS.has(tool))
|
|
15
|
+
return "执行中断,可安全重试(write_file 为全量写)。";
|
|
16
|
+
return "执行状态未知:进程中断时命令可能已完成。请先验证再操作(stat / git status / git diff),不要直接重做。";
|
|
17
|
+
}
|
|
18
|
+
export function cwdDigest(cwd) {
|
|
19
|
+
return createHash("sha1").update(cwd).digest("hex").slice(0, 8);
|
|
20
|
+
}
|
|
21
|
+
export function projectionFingerprint(meta, compactionSource = "none") {
|
|
22
|
+
return {
|
|
23
|
+
toolCatalogHash: toolCatalogHash(),
|
|
24
|
+
modelId: `${meta.providerId}::${meta.model}`,
|
|
25
|
+
cwdDigest: cwdDigest(meta.cwd),
|
|
26
|
+
compactionSource,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function sameFingerprint(a, b) {
|
|
30
|
+
return Boolean(a) && a.toolCatalogHash === b.toolCatalogHash && a.modelId === b.modelId &&
|
|
31
|
+
a.cwdDigest === b.cwdDigest && a.compactionSource === b.compactionSource;
|
|
32
|
+
}
|
|
33
|
+
export function sessionMetaFromFacts(facts) {
|
|
34
|
+
let cwd = "";
|
|
35
|
+
let providerId = "";
|
|
36
|
+
let model = "";
|
|
37
|
+
let sawStart = false;
|
|
38
|
+
for (const fact of facts) {
|
|
39
|
+
// Only the first session_start defines the session. A resume appends
|
|
40
|
+
// another session_start with the current cwd/model, which must not
|
|
41
|
+
// change the fingerprint of the stored projection.
|
|
42
|
+
if (fact?.type === "session_start" && !sawStart) {
|
|
43
|
+
sawStart = true;
|
|
44
|
+
if (typeof fact.cwd === "string" && fact.cwd)
|
|
45
|
+
cwd = fact.cwd;
|
|
46
|
+
if (typeof fact.providerId === "string" && fact.providerId)
|
|
47
|
+
providerId = fact.providerId;
|
|
48
|
+
if (typeof fact.model === "string" && fact.model)
|
|
49
|
+
model = fact.model;
|
|
50
|
+
}
|
|
51
|
+
else if (fact?.type === "checkpoint") {
|
|
52
|
+
if (typeof fact.cwd === "string" && fact.cwd)
|
|
53
|
+
cwd = fact.cwd;
|
|
54
|
+
if (typeof fact.providerId === "string" && fact.providerId)
|
|
55
|
+
providerId = fact.providerId;
|
|
56
|
+
if (typeof fact.model === "string" && fact.model)
|
|
57
|
+
model = fact.model;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return { cwd, providerId, model };
|
|
61
|
+
}
|
|
62
|
+
function factsAfterClear(facts) {
|
|
63
|
+
for (let index = facts.length - 1; index >= 0; index -= 1) {
|
|
64
|
+
if (facts[index]?.type === "cleared")
|
|
65
|
+
return facts.slice(index + 1);
|
|
66
|
+
}
|
|
67
|
+
return facts;
|
|
68
|
+
}
|
|
69
|
+
export function sessionMetaFromFile(file) {
|
|
70
|
+
return sessionMetaFromFacts(readSession(file));
|
|
71
|
+
}
|
|
72
|
+
function legacyMessages(facts) {
|
|
73
|
+
return facts
|
|
74
|
+
.filter((fact) => fact?.type === "message" && typeof fact.content === "string")
|
|
75
|
+
.map((fact) => ({ role: fact.role, content: fact.content }));
|
|
76
|
+
}
|
|
77
|
+
export function factsToMessages(facts) {
|
|
78
|
+
const calls = new Map();
|
|
79
|
+
const pending = [];
|
|
80
|
+
const messages = [];
|
|
81
|
+
let nativeTurn = false;
|
|
82
|
+
const flush = () => {
|
|
83
|
+
for (const [id, call] of calls) {
|
|
84
|
+
const output = unpairedToolNotice(String(call?.tool ?? "unknown"));
|
|
85
|
+
if (nativeTurn)
|
|
86
|
+
messages.push({ role: "tool", toolCallId: id, content: output });
|
|
87
|
+
else
|
|
88
|
+
pending.push({ call, ok: false, output });
|
|
89
|
+
}
|
|
90
|
+
calls.clear();
|
|
91
|
+
if (pending.length > 0)
|
|
92
|
+
messages.push({ role: "user", content: formatToolResults(pending.splice(0)) });
|
|
93
|
+
};
|
|
94
|
+
for (const fact of facts) {
|
|
95
|
+
if (!fact)
|
|
96
|
+
continue;
|
|
97
|
+
if (fact.type === "user") {
|
|
98
|
+
flush();
|
|
99
|
+
nativeTurn = false;
|
|
100
|
+
if (typeof fact.content === "string" && fact.content)
|
|
101
|
+
messages.push({ role: "user", content: fact.content });
|
|
102
|
+
}
|
|
103
|
+
else if (fact.type === "assistant_raw") {
|
|
104
|
+
flush();
|
|
105
|
+
const nativeCalls = Array.isArray(fact.toolCalls) ? fact.toolCalls : undefined;
|
|
106
|
+
nativeTurn = Boolean(nativeCalls && nativeCalls.length > 0);
|
|
107
|
+
if (typeof fact.content === "string")
|
|
108
|
+
messages.push({ role: "assistant", content: fact.content, ...(nativeTurn ? { toolCalls: nativeCalls } : {}) });
|
|
109
|
+
}
|
|
110
|
+
else if (fact.type === "tool_call") {
|
|
111
|
+
calls.set(String(fact.id ?? ""), fact.args ?? { tool: fact.tool });
|
|
112
|
+
}
|
|
113
|
+
else if (fact.type === "notice") {
|
|
114
|
+
if (typeof fact.content === "string" && fact.content)
|
|
115
|
+
messages.push({ role: "system", content: fact.content });
|
|
116
|
+
}
|
|
117
|
+
else if (fact.type === "tool_result") {
|
|
118
|
+
const id = String(fact.id ?? "");
|
|
119
|
+
const call = calls.get(id) ?? { tool: fact.tool ?? "unknown" };
|
|
120
|
+
calls.delete(id);
|
|
121
|
+
if (nativeTurn) {
|
|
122
|
+
messages.push({ role: "tool", toolCallId: id, content: String(fact.output ?? "") });
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
pending.push({
|
|
126
|
+
call,
|
|
127
|
+
ok: Boolean(fact.ok),
|
|
128
|
+
output: String(fact.output ?? ""),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
else if (fact.type === "egress_denied") {
|
|
133
|
+
const target = fact.target ? ` target=${JSON.stringify(fact.target)}` : "";
|
|
134
|
+
const content = `[egress] ${fact.mode ?? "audit"} rule=${fact.rule ?? "unknown"}${target} would_block=${Boolean(fact.wouldBlock)}\n${fact.reason ?? ""}`;
|
|
135
|
+
messages.push({ role: "system", content });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
flush();
|
|
139
|
+
if (messages.length === 0)
|
|
140
|
+
return legacyMessages(facts);
|
|
141
|
+
return messages;
|
|
142
|
+
}
|
|
143
|
+
export function compactionSourceFromFacts(facts) {
|
|
144
|
+
let source = "none";
|
|
145
|
+
for (const fact of facts) {
|
|
146
|
+
if (fact?.type === "compacted" && (fact.source === "local" || fact.source === "server"))
|
|
147
|
+
source = fact.source;
|
|
148
|
+
}
|
|
149
|
+
return source;
|
|
150
|
+
}
|
|
151
|
+
function latestCompaction(facts) {
|
|
152
|
+
for (let i = facts.length - 1; i >= 0; i -= 1) {
|
|
153
|
+
if (facts[i]?.type === "compacted")
|
|
154
|
+
return facts[i];
|
|
155
|
+
}
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
function applySummary(messages, facts) {
|
|
159
|
+
const latest = latestCompaction(facts);
|
|
160
|
+
if (!latest || latest.source !== "local" || typeof latest.summaryRef !== "string")
|
|
161
|
+
return messages;
|
|
162
|
+
try {
|
|
163
|
+
const summary = readFileSync(latest.summaryRef, "utf8");
|
|
164
|
+
const keep = Number(latest.keepMessages);
|
|
165
|
+
const tail = Number.isFinite(keep) && keep > 0 ? messages.slice(-keep) : messages;
|
|
166
|
+
const first = messages[0]?.role === "user" ? messages[0] : undefined;
|
|
167
|
+
const marker = { role: "system", content: `[context summary]\n${summary}` };
|
|
168
|
+
if (first && tail[0] !== first)
|
|
169
|
+
return [first, marker, ...tail];
|
|
170
|
+
return [marker, ...tail];
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
return [
|
|
174
|
+
...messages,
|
|
175
|
+
{ role: "system", content: "[projection] Summary file could not be read; resumed without the summary marker." },
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
export function projectionContentHash(projection) {
|
|
180
|
+
const core = {
|
|
181
|
+
version: projection.version,
|
|
182
|
+
kind: projection.kind,
|
|
183
|
+
cwd: projection.cwd,
|
|
184
|
+
providerId: projection.providerId,
|
|
185
|
+
model: projection.model,
|
|
186
|
+
fingerprint: projection.fingerprint,
|
|
187
|
+
messages: projection.messages,
|
|
188
|
+
activeFiles: projection.activeFiles,
|
|
189
|
+
lastFactSeq: projection.lastFactSeq,
|
|
190
|
+
};
|
|
191
|
+
return createHash("sha256").update(JSON.stringify(core)).digest("hex");
|
|
192
|
+
}
|
|
193
|
+
export function factsContentHash(facts) {
|
|
194
|
+
return createHash("sha256").update(JSON.stringify(facts)).digest("hex");
|
|
195
|
+
}
|
|
196
|
+
function lastFactSeqOf(facts) {
|
|
197
|
+
let seq = 0;
|
|
198
|
+
for (const fact of facts) {
|
|
199
|
+
if (typeof fact?.seq === "number" && fact.seq > seq)
|
|
200
|
+
seq = fact.seq;
|
|
201
|
+
}
|
|
202
|
+
return seq;
|
|
203
|
+
}
|
|
204
|
+
function withManifest(projection, facts) {
|
|
205
|
+
return {
|
|
206
|
+
...projection,
|
|
207
|
+
manifest: {
|
|
208
|
+
version: 1,
|
|
209
|
+
algorithm: "sha256",
|
|
210
|
+
savedAt: new Date().toISOString(),
|
|
211
|
+
contentHash: projectionContentHash(projection),
|
|
212
|
+
...(facts ? { sessionHash: factsContentHash(facts), lastFactSeq: lastFactSeqOf(facts) } : {}),
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function emptyProjection(meta) {
|
|
217
|
+
return buildProjection([], meta);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Keep native tool_call/tool_result pairs complete. Orphan tool messages left
|
|
221
|
+
* behind by projection truncation are dropped; missing results are synthesized
|
|
222
|
+
* with the existing "execution unknown" notice so a resumed model never waits
|
|
223
|
+
* for a result that cannot arrive.
|
|
224
|
+
*/
|
|
225
|
+
export function pairToolMessages(messages) {
|
|
226
|
+
const out = [];
|
|
227
|
+
const expected = new Map();
|
|
228
|
+
let repaired = false;
|
|
229
|
+
const synthesize = () => {
|
|
230
|
+
for (const [id, tool] of expected)
|
|
231
|
+
out.push({ role: "tool", toolCallId: id, content: unpairedToolNotice(tool) });
|
|
232
|
+
if (expected.size > 0)
|
|
233
|
+
repaired = true;
|
|
234
|
+
expected.clear();
|
|
235
|
+
};
|
|
236
|
+
for (const message of messages) {
|
|
237
|
+
if (message.role === "tool") {
|
|
238
|
+
if (!message.toolCallId || !expected.has(message.toolCallId)) {
|
|
239
|
+
repaired = true;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
expected.delete(message.toolCallId);
|
|
243
|
+
out.push(message);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
synthesize();
|
|
247
|
+
out.push(message);
|
|
248
|
+
if (message.role === "assistant" && Array.isArray(message.toolCalls)) {
|
|
249
|
+
for (const call of message.toolCalls) {
|
|
250
|
+
if (call?.id)
|
|
251
|
+
expected.set(String(call.id), String(call.name ?? "unknown"));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
synthesize();
|
|
256
|
+
return { messages: out, repaired };
|
|
257
|
+
}
|
|
258
|
+
export function buildProjection(facts, metaOverride) {
|
|
259
|
+
const detected = sessionMetaFromFacts(facts);
|
|
260
|
+
const meta = {
|
|
261
|
+
cwd: metaOverride?.cwd || detected.cwd || process.cwd(),
|
|
262
|
+
providerId: metaOverride?.providerId || detected.providerId,
|
|
263
|
+
model: metaOverride?.model || detected.model,
|
|
264
|
+
};
|
|
265
|
+
const threadFacts = factsAfterClear(facts);
|
|
266
|
+
const folded = foldToolMessages(applySummary(factsToMessages(threadFacts), threadFacts));
|
|
267
|
+
const messages = pairToolMessages(clampMessages(folded)).messages;
|
|
268
|
+
const source = compactionSourceFromFacts(threadFacts);
|
|
269
|
+
let activeFiles = [];
|
|
270
|
+
let lastFactSeq = 0;
|
|
271
|
+
for (const fact of facts) {
|
|
272
|
+
if (typeof fact?.seq === "number" && fact.seq > lastFactSeq)
|
|
273
|
+
lastFactSeq = fact.seq;
|
|
274
|
+
}
|
|
275
|
+
for (const fact of threadFacts) {
|
|
276
|
+
if (fact?.type === "checkpoint" && Array.isArray(fact.activeFiles))
|
|
277
|
+
activeFiles = fact.activeFiles;
|
|
278
|
+
}
|
|
279
|
+
const projection = {
|
|
280
|
+
version: 1,
|
|
281
|
+
kind: "projection",
|
|
282
|
+
cwd: meta.cwd,
|
|
283
|
+
providerId: meta.providerId,
|
|
284
|
+
model: meta.model,
|
|
285
|
+
fingerprint: projectionFingerprint(meta, source),
|
|
286
|
+
messages,
|
|
287
|
+
activeFiles: activeFiles.slice(0, 200),
|
|
288
|
+
lastFactSeq,
|
|
289
|
+
};
|
|
290
|
+
return projection;
|
|
291
|
+
}
|
|
292
|
+
function clipProjection(text, max) {
|
|
293
|
+
const oneLine = String(text).replace(/\s+/g, " ").trim();
|
|
294
|
+
return oneLine.length > max ? oneLine.slice(0, Math.max(1, max - 1)) + "…" : oneLine;
|
|
295
|
+
}
|
|
296
|
+
function toolTarget(args) {
|
|
297
|
+
if (!args || typeof args !== "object")
|
|
298
|
+
return "";
|
|
299
|
+
const value = args.path ?? args.from ?? args.to ?? args.command ?? args.pattern;
|
|
300
|
+
return typeof value === "string" ? clipProjection(value, 160) : "";
|
|
301
|
+
}
|
|
302
|
+
function firstImportantLine(text) {
|
|
303
|
+
const lines = String(text)
|
|
304
|
+
.split(/\r?\n/)
|
|
305
|
+
.map((line) => line.trim())
|
|
306
|
+
.filter((line) => line && !/^```/.test(line) && line !== "output:");
|
|
307
|
+
const important = lines.find((line) => /error|failed|not found|denied|timed out|exit/i.test(line)) ?? lines[0] ?? "";
|
|
308
|
+
return clipProjection(important, 200);
|
|
309
|
+
}
|
|
310
|
+
/** Keep the edit fact (tool + target + outcome), not the full tool payload. */
|
|
311
|
+
function foldToolMessages(messages) {
|
|
312
|
+
const calls = new Map();
|
|
313
|
+
const out = [];
|
|
314
|
+
for (const message of messages) {
|
|
315
|
+
if (message.role === "assistant" && Array.isArray(message.toolCalls)) {
|
|
316
|
+
for (const call of message.toolCalls) {
|
|
317
|
+
let args = {};
|
|
318
|
+
try {
|
|
319
|
+
args = JSON.parse(call.arguments || "{}");
|
|
320
|
+
}
|
|
321
|
+
catch {
|
|
322
|
+
args = {};
|
|
323
|
+
}
|
|
324
|
+
calls.set(String(call.id), { name: String(call.name ?? "tool"), args });
|
|
325
|
+
}
|
|
326
|
+
out.push(message);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
if (message.role === "tool") {
|
|
330
|
+
const call = message.toolCallId ? calls.get(String(message.toolCallId)) : undefined;
|
|
331
|
+
const target = toolTarget(call?.args);
|
|
332
|
+
const status = /error|failed|not found|denied|timed out/i.test(message.content) ? "error" : "ok";
|
|
333
|
+
const detail = firstImportantLine(message.content);
|
|
334
|
+
out.push({
|
|
335
|
+
...message,
|
|
336
|
+
content: clipProjection(`${call?.name ?? "tool"}${target ? ` ${target}` : ""} → ${status}${detail ? `: ${detail}` : ""}`, TOOL_FOLD_CHARS),
|
|
337
|
+
});
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (message.role === "user" && message.content.startsWith("FastCode executed the requested local tools.")) {
|
|
341
|
+
out.push({ ...message, content: foldLegacyToolResults(message.content) });
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
out.push(message);
|
|
345
|
+
}
|
|
346
|
+
return out;
|
|
347
|
+
}
|
|
348
|
+
function foldLegacyToolResults(content) {
|
|
349
|
+
const header = "FastCode executed the requested local tools.";
|
|
350
|
+
const body = content.slice(content.indexOf(header) + header.length).trim();
|
|
351
|
+
const blocks = body.split(/\n\n(?=Tool result \d+ \()/);
|
|
352
|
+
const folded = [header];
|
|
353
|
+
for (const block of blocks) {
|
|
354
|
+
const statusMatch = block.match(/^Tool result \d+ \((ok|error)\)/);
|
|
355
|
+
if (!statusMatch)
|
|
356
|
+
continue;
|
|
357
|
+
const callMatch = block.match(/^call:\s*(\{.*\})$/m);
|
|
358
|
+
let call = {};
|
|
359
|
+
try {
|
|
360
|
+
call = callMatch ? JSON.parse(callMatch[1]) : {};
|
|
361
|
+
}
|
|
362
|
+
catch {
|
|
363
|
+
call = {};
|
|
364
|
+
}
|
|
365
|
+
const output = block.includes("output:") ? block.slice(block.indexOf("output:") + "output:".length) : block;
|
|
366
|
+
const target = toolTarget(call);
|
|
367
|
+
const detail = firstImportantLine(output);
|
|
368
|
+
folded.push(`- ${String(call.tool ?? "tool")}${target ? ` ${target}` : ""} → ${statusMatch[1]}${detail ? `: ${detail}` : ""}`);
|
|
369
|
+
}
|
|
370
|
+
return folded.length > 1 ? folded.join("\n") : clipProjection(content, TOOL_FOLD_CHARS);
|
|
371
|
+
}
|
|
372
|
+
function truncateMessageContent(content) {
|
|
373
|
+
if (content.length <= MAX_MESSAGE_CHARS)
|
|
374
|
+
return content;
|
|
375
|
+
const head = Math.floor(MAX_MESSAGE_CHARS * 0.6);
|
|
376
|
+
const tail = Math.max(0, MAX_MESSAGE_CHARS - head);
|
|
377
|
+
return `${content.slice(0, head)}\n[...truncated in projection...]\n${content.slice(-tail)}`;
|
|
378
|
+
}
|
|
379
|
+
function protectedMessage(message) {
|
|
380
|
+
return message.role === "system" && /^\[(context summary|context compacted|projection)/.test(message.content);
|
|
381
|
+
}
|
|
382
|
+
function clampMessages(messages) {
|
|
383
|
+
const list = messages.map((message) => ({
|
|
384
|
+
role: message.role,
|
|
385
|
+
content: truncateMessageContent(message.content),
|
|
386
|
+
...(message.toolCalls ? { toolCalls: message.toolCalls } : {}),
|
|
387
|
+
...(message.toolCallId ? { toolCallId: message.toolCallId } : {}),
|
|
388
|
+
}));
|
|
389
|
+
let dropped = 0;
|
|
390
|
+
const overBudget = () => list.length > 1 && Buffer.byteLength(JSON.stringify(list), "utf8") > MAX_PROJECTION_BYTES;
|
|
391
|
+
const dropOldest = (marker) => {
|
|
392
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
393
|
+
const message = list[index];
|
|
394
|
+
if (index === 0 && message.role === "user")
|
|
395
|
+
continue;
|
|
396
|
+
if (message === marker || protectedMessage(message))
|
|
397
|
+
continue;
|
|
398
|
+
list.splice(index, 1);
|
|
399
|
+
dropped += 1;
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
return false;
|
|
403
|
+
};
|
|
404
|
+
while (overBudget() && dropOldest())
|
|
405
|
+
;
|
|
406
|
+
if (dropped > 0) {
|
|
407
|
+
const marker = {
|
|
408
|
+
role: "system",
|
|
409
|
+
content: `[projection] ${dropped} earlier message(s) dropped to fit ${MAX_PROJECTION_BYTES} bytes.`,
|
|
410
|
+
};
|
|
411
|
+
const insertAt = list[0]?.role === "user" ? 1 : 0;
|
|
412
|
+
list.splice(insertAt, 0, marker);
|
|
413
|
+
while (overBudget() && dropOldest(marker)) {
|
|
414
|
+
marker.content = `[projection] ${dropped} earlier message(s) dropped to fit ${MAX_PROJECTION_BYTES} bytes.`;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return list;
|
|
418
|
+
}
|
|
419
|
+
export function readProjectionResult(file) {
|
|
420
|
+
let raw;
|
|
421
|
+
try {
|
|
422
|
+
raw = JSON.parse(readFileSync(projectionPath(file), "utf8"));
|
|
423
|
+
}
|
|
424
|
+
catch (err) {
|
|
425
|
+
if (err?.code === "ENOENT")
|
|
426
|
+
return { status: "missing" };
|
|
427
|
+
return { status: "corrupt" };
|
|
428
|
+
}
|
|
429
|
+
if (!raw || raw.version !== 1 || !Array.isArray(raw.messages) || !raw.fingerprint)
|
|
430
|
+
return { status: "corrupt" };
|
|
431
|
+
const projection = raw;
|
|
432
|
+
if (!raw.manifest || typeof raw.manifest.contentHash !== "string")
|
|
433
|
+
return { status: "legacy", projection };
|
|
434
|
+
const actualHash = projectionContentHash(projection);
|
|
435
|
+
if (actualHash !== raw.manifest.contentHash)
|
|
436
|
+
return { status: "hash_mismatch", projection, expectedHash: raw.manifest.contentHash, actualHash };
|
|
437
|
+
return { status: "ok", projection, expectedHash: raw.manifest.contentHash, actualHash };
|
|
438
|
+
}
|
|
439
|
+
export function readProjection(file) {
|
|
440
|
+
const result = readProjectionResult(file);
|
|
441
|
+
return result.status === "ok" || result.status === "legacy" ? result.projection : undefined;
|
|
442
|
+
}
|
|
443
|
+
export function writeProjection(file, projection, facts) {
|
|
444
|
+
const target = projectionPath(file);
|
|
445
|
+
const tmp = target + "." + process.pid + ".tmp";
|
|
446
|
+
try {
|
|
447
|
+
writeFileSync(tmp, JSON.stringify(withManifest(projection, facts), null, 2), "utf8");
|
|
448
|
+
renameSync(tmp, target);
|
|
449
|
+
}
|
|
450
|
+
catch (err) {
|
|
451
|
+
try {
|
|
452
|
+
rmSync(tmp, { force: true });
|
|
453
|
+
}
|
|
454
|
+
catch {
|
|
455
|
+
// best effort
|
|
456
|
+
}
|
|
457
|
+
throw err;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
export function resumeSession(file, currentCwd = process.cwd()) {
|
|
461
|
+
const facts = readSession(file);
|
|
462
|
+
const hasFacts = facts.length > 0;
|
|
463
|
+
const detected = sessionMetaFromFacts(facts);
|
|
464
|
+
const read = readProjectionResult(file);
|
|
465
|
+
const stored = read.projection;
|
|
466
|
+
const meta = {
|
|
467
|
+
cwd: detected.cwd || stored?.cwd || currentCwd,
|
|
468
|
+
providerId: detected.providerId || stored?.providerId || "",
|
|
469
|
+
model: detected.model || stored?.model || "",
|
|
470
|
+
};
|
|
471
|
+
const expected = projectionFingerprint(meta, hasFacts ? compactionSourceFromFacts(factsAfterClear(facts)) : (stored?.fingerprint.compactionSource ?? "none"));
|
|
472
|
+
const warnings = [];
|
|
473
|
+
let projection;
|
|
474
|
+
let rebuilt = false;
|
|
475
|
+
const currentSessionHash = hasFacts ? factsContentHash(facts) : undefined;
|
|
476
|
+
const sessionChanged = hasFacts && Boolean(stored?.manifest?.sessionHash) &&
|
|
477
|
+
stored.manifest.sessionHash !== currentSessionHash;
|
|
478
|
+
const coveredSeq = stored?.manifest?.lastFactSeq ?? stored?.lastFactSeq ?? 0;
|
|
479
|
+
const factsNotCovered = hasFacts && coveredSeq < lastFactSeqOf(facts);
|
|
480
|
+
if (stored && (read.status === "ok" || read.status === "legacy") &&
|
|
481
|
+
sameFingerprint(stored.fingerprint, expected) && !sessionChanged && !factsNotCovered) {
|
|
482
|
+
projection = stored;
|
|
483
|
+
if (read.status === "legacy")
|
|
484
|
+
warnings.push("Projection has no manifest; integrity could not be verified.");
|
|
485
|
+
}
|
|
486
|
+
else {
|
|
487
|
+
if (read.status === "hash_mismatch")
|
|
488
|
+
warnings.push("Projection manifest hash does not match its contents; ignoring the stored copy.");
|
|
489
|
+
else if (read.status === "corrupt")
|
|
490
|
+
warnings.push("Projection file is corrupt; rebuilding from session facts.");
|
|
491
|
+
else if (sessionChanged || factsNotCovered)
|
|
492
|
+
warnings.push("Session facts changed since the projection was saved; rebuilt from facts.");
|
|
493
|
+
else if (stored)
|
|
494
|
+
warnings.push("Projection fingerprint changed (tools/model/cwd); rebuilt from facts.");
|
|
495
|
+
if (hasFacts) {
|
|
496
|
+
projection = buildProjection(facts, meta);
|
|
497
|
+
rebuilt = true;
|
|
498
|
+
if (!stored)
|
|
499
|
+
warnings.push("No projection found; rebuilt from facts.");
|
|
500
|
+
try {
|
|
501
|
+
writeProjection(file, projection, facts);
|
|
502
|
+
}
|
|
503
|
+
catch {
|
|
504
|
+
warnings.push("Could not write the projection (filesystem is read-only?); using the in-memory rebuild.");
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
else if (stored && read.status !== "hash_mismatch" && read.status !== "corrupt") {
|
|
508
|
+
// The original JSONL was pruned. The verified projection is the
|
|
509
|
+
// only remaining evidence, so use it rather than re-deriving trust
|
|
510
|
+
// from facts that no longer exist.
|
|
511
|
+
projection = stored;
|
|
512
|
+
warnings.push("Session facts were pruned; using the stored projection.");
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
projection = emptyProjection(meta);
|
|
516
|
+
warnings.push("Session content is unavailable or unverifiable; resumed history is empty.");
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
const repaired = pairToolMessages(projection.messages);
|
|
520
|
+
if (repaired.repaired)
|
|
521
|
+
warnings.push("Tool call/result pairing was repaired during resume.");
|
|
522
|
+
const finalProjection = { ...projection, messages: repaired.messages };
|
|
523
|
+
if (detected.cwd && detected.cwd !== currentCwd)
|
|
524
|
+
warnings.push(`Session was recorded in ${detected.cwd}; current directory is ${currentCwd}.`);
|
|
525
|
+
return { projection: finalProjection, messages: finalProjection.messages, meta, warnings, rebuilt };
|
|
526
|
+
}
|
package/dist/skills/manager.d.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
export type SkillIntegrity = "ok" | "drift" | "unverified";
|
|
2
|
+
/**
|
|
3
|
+
* Pick the skills registry without treating an empty environment variable as
|
|
4
|
+
* "configured". `??` would keep `""` and make env overrides silently break;
|
|
5
|
+
* explicit null/empty checks keep `""` and `" "` on the config path. There is
|
|
6
|
+
* intentionally no built-in default registry.
|
|
7
|
+
*/
|
|
8
|
+
export declare function resolveRegistry(envValue?: string | null, configValue?: string | null): string;
|
|
9
|
+
export declare function hashSkillDirectory(folder: string): string;
|
|
10
|
+
export declare function rebaselineSkills(names?: string[]): Array<{
|
|
11
|
+
name: string;
|
|
12
|
+
ok: boolean;
|
|
13
|
+
warning?: string;
|
|
14
|
+
}>;
|
|
1
15
|
/** List skills currently installed under ~/.fastcode/skills. */
|
|
2
16
|
export declare function listInstalledSkills(): any[];
|
|
3
17
|
/** Look up a skill by name (matches folder name OR frontmatter name). */
|
|
@@ -9,6 +23,7 @@ export declare function installSkill(spec: any, opts?: any): Promise<{
|
|
|
9
23
|
source: any;
|
|
10
24
|
path: string;
|
|
11
25
|
body: any;
|
|
26
|
+
integrity: SkillIntegrity;
|
|
12
27
|
}>;
|
|
13
28
|
/** Remove an installed skill by name or folder. */
|
|
14
29
|
export declare function uninstallSkill(name: any): boolean;
|