sakuracode 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +257 -2
- package/README.zh-CN.md +258 -0
- package/dist/LICENSE +21 -0
- package/dist/THIRD_PARTY_NOTICES.md +13 -0
- package/dist/code/bin/sakuracode.mjs +5 -0
- package/dist/code/src/agent-session.js +41 -0
- package/dist/code/src/ansi.js +220 -0
- package/dist/code/src/cli.js +211 -0
- package/dist/code/src/config.js +183 -0
- package/dist/code/src/context-estimate.js +44 -0
- package/dist/code/src/exec-mode.js +84 -0
- package/dist/code/src/framework-testing.js +2 -0
- package/dist/code/src/framework.js +3 -0
- package/dist/code/src/idle-compaction.js +38 -0
- package/dist/code/src/images.js +18 -0
- package/dist/code/src/invocation.js +39 -0
- package/dist/code/src/markdown.js +383 -0
- package/dist/code/src/mentions.js +22 -0
- package/dist/code/src/permissions.js +40 -0
- package/dist/code/src/providers.js +56 -0
- package/dist/code/src/reports.js +81 -0
- package/dist/code/src/run-driver.js +76 -0
- package/dist/code/src/session-controller.js +468 -0
- package/dist/code/src/session-store.js +92 -0
- package/dist/code/src/shell.js +36 -0
- package/dist/code/src/slash-commands.js +83 -0
- package/dist/code/src/status-line.js +35 -0
- package/dist/code/src/toml.js +256 -0
- package/dist/code/src/transcript.js +288 -0
- package/dist/code/src/tui/app.js +273 -0
- package/dist/code/src/tui/keys.js +151 -0
- package/dist/code/src/tui/line-editor.js +324 -0
- package/dist/code/src/tui/live-region.js +71 -0
- package/dist/code/src/tui/screen.js +69 -0
- package/dist/code/src/tui/transcript-view.js +102 -0
- package/dist/licenses/codex-Apache-2.0.txt +201 -0
- package/dist/licenses/gitleaks-MIT.txt +21 -0
- package/dist/licenses/pi-mono-MIT.txt +21 -0
- package/dist/packages/agent/src/compose.js +91 -0
- package/dist/packages/agent/src/index.js +161 -0
- package/dist/packages/agent/src/restrict-tools.js +11 -0
- package/dist/packages/agent/src/system-prompt.js +49 -0
- package/dist/packages/agent/src/wake.js +71 -0
- package/dist/packages/env/src/confine.js +40 -0
- package/dist/packages/env/src/connect-ssh.js +242 -0
- package/dist/packages/env/src/index.js +4 -0
- package/dist/packages/env/src/local.js +206 -0
- package/dist/packages/env/src/remote-stream.js +143 -0
- package/dist/packages/env/src/remote-types.js +10 -0
- package/dist/packages/env/src/remote.js +183 -0
- package/dist/packages/evals/src/bfcl.js +71 -0
- package/dist/packages/evals/src/index.js +2 -0
- package/dist/packages/evals/src/png.js +30 -0
- package/dist/packages/evals/src/pool.js +22 -0
- package/dist/packages/evals/src/providers.js +23 -0
- package/dist/packages/kernel/src/channel.js +34 -0
- package/dist/packages/kernel/src/compaction.js +86 -0
- package/dist/packages/kernel/src/constants.js +5 -0
- package/dist/packages/kernel/src/goal.js +65 -0
- package/dist/packages/kernel/src/hooks.js +153 -0
- package/dist/packages/kernel/src/index.js +4 -0
- package/dist/packages/kernel/src/loop.js +556 -0
- package/dist/packages/kernel/src/prompts.js +99 -0
- package/dist/packages/kernel/src/system.js +49 -0
- package/dist/packages/kernel/src/types.js +23 -0
- package/dist/packages/kernel/src/usage.js +16 -0
- package/dist/packages/llm/src/chat/compat.js +16 -0
- package/dist/packages/llm/src/chat/errors.js +61 -0
- package/dist/packages/llm/src/chat/request.js +109 -0
- package/dist/packages/llm/src/chat/retry.js +57 -0
- package/dist/packages/llm/src/chat/stream.js +122 -0
- package/dist/packages/llm/src/index.js +120 -0
- package/dist/packages/plugins/agents/src/index.js +206 -0
- package/dist/packages/plugins/agents/src/view.js +60 -0
- package/dist/packages/plugins/context/src/index.js +67 -0
- package/dist/packages/plugins/limits/src/cost.js +3 -0
- package/dist/packages/plugins/limits/src/index.js +31 -0
- package/dist/packages/plugins/redaction/src/engine.js +110 -0
- package/dist/packages/plugins/redaction/src/index.js +69 -0
- package/dist/packages/plugins/redaction/src/restore.js +44 -0
- package/dist/packages/plugins/redaction/src/rules.gen.js +2059 -0
- package/dist/packages/plugins/resilience/src/index.js +86 -0
- package/dist/packages/plugins/review/src/chain.js +27 -0
- package/dist/packages/plugins/review/src/hard-rules.js +156 -0
- package/dist/packages/plugins/review/src/index.js +93 -0
- package/dist/packages/plugins/review/src/policy.js +65 -0
- package/dist/packages/plugins/review/src/reviewer.js +207 -0
- package/dist/packages/plugins/review/src/shell.js +124 -0
- package/dist/packages/plugins/tools/src/artifacts.js +61 -0
- package/dist/packages/plugins/tools/src/builtin/args.js +46 -0
- package/dist/packages/plugins/tools/src/builtin/bash.js +110 -0
- package/dist/packages/plugins/tools/src/builtin/edit.js +71 -0
- package/dist/packages/plugins/tools/src/builtin/image.js +84 -0
- package/dist/packages/plugins/tools/src/builtin/read.js +89 -0
- package/dist/packages/plugins/tools/src/builtin/write.js +35 -0
- package/dist/packages/plugins/tools/src/edit-match.js +159 -0
- package/dist/packages/plugins/tools/src/fold.js +65 -0
- package/dist/packages/plugins/tools/src/index.js +10 -0
- package/dist/packages/plugins/toolset/src/bm25.js +35 -0
- package/dist/packages/plugins/toolset/src/codemode.js +226 -0
- package/dist/packages/plugins/toolset/src/index.js +3 -0
- package/dist/packages/plugins/toolset/src/mcp.js +173 -0
- package/dist/packages/plugins/toolset/src/search.js +57 -0
- package/dist/packages/plugins/trace/src/index.js +29 -0
- package/dist/packages/plugins/trace/src/modules.js +28 -0
- package/dist/packages/session/src/file-log.js +144 -0
- package/dist/packages/session/src/fork.js +70 -0
- package/dist/packages/session/src/index.js +3 -0
- package/dist/packages/session/src/log-core.js +47 -0
- package/dist/packages/session/src/memory-log.js +7 -0
- package/dist/packages/session/src/projection.js +107 -0
- package/dist/packages/session/src/tree.js +35 -0
- package/dist/packages/testing/src/faux.js +69 -0
- package/dist/packages/testing/src/index.js +2 -0
- package/dist/packages/testing/src/rng.js +17 -0
- package/dist/sdk/index.js +10 -0
- package/dist/sdk/testing.js +2 -0
- package/package.json +54 -4
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Model providers: the built-in ones, the ones from the config file, and turning a choice into the framework's ModelConfig.
|
|
2
|
+
import { compatPresets } from "./framework.js";
|
|
3
|
+
export class ProviderSetupError extends Error {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "ProviderSetupError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/** Chat Completions providers that are known to work. The order is the order tried when the user chose none. */
|
|
10
|
+
export const BUILTIN_PROVIDERS = {
|
|
11
|
+
deepseek: { baseUrl: "https://api.deepseek.com", model: "deepseek-flash", keyEnv: "DEEPSEEK_API_KEY", contextWindow: 128_000, compat: "deepseek", images: true },
|
|
12
|
+
// OpenCode Go refuses a request without a session id (error MissingSessionID); the id is the terminal session's id.
|
|
13
|
+
opencode: { baseUrl: "https://opencode.ai/zen/go/v1", model: "deepseek-v4.1-flash", keyEnv: "OPENCODE_API_KEY", contextWindow: 128_000, compat: "deepseek", images: true, sessionHeader: "x-opencode-session" },
|
|
14
|
+
};
|
|
15
|
+
export function allProviders(settings) {
|
|
16
|
+
return { ...BUILTIN_PROVIDERS, ...settings.providers };
|
|
17
|
+
}
|
|
18
|
+
export function listProviders(settings, env) {
|
|
19
|
+
return Object.entries(allProviders(settings)).map(([name, definition]) => ({ name, definition, keyPresent: Boolean(env[definition.keyEnv]), builtin: name in BUILTIN_PROVIDERS && !(name in settings.providers) }));
|
|
20
|
+
}
|
|
21
|
+
/** The provider to use: the one the user chose, else the first one whose key is set. */
|
|
22
|
+
export function chooseProvider(settings, env) {
|
|
23
|
+
const all = allProviders(settings);
|
|
24
|
+
if (settings.provider !== undefined) {
|
|
25
|
+
if (!(settings.provider in all))
|
|
26
|
+
throw new ProviderSetupError(`unknown provider "${settings.provider}" (known: ${Object.keys(all).join(", ")})`);
|
|
27
|
+
return settings.provider;
|
|
28
|
+
}
|
|
29
|
+
const withKey = listProviders(settings, env).find((entry) => entry.keyPresent);
|
|
30
|
+
if (!withKey) {
|
|
31
|
+
const needed = Object.values(all).map((definition) => definition.keyEnv);
|
|
32
|
+
throw new ProviderSetupError(`no API key found; set one of: ${needed.join(", ")} (\`sakuracode providers\` shows what is set)`);
|
|
33
|
+
}
|
|
34
|
+
return withKey.name;
|
|
35
|
+
}
|
|
36
|
+
export function buildModelConfig(request) {
|
|
37
|
+
const definition = allProviders(request.settings)[request.provider];
|
|
38
|
+
if (!definition)
|
|
39
|
+
throw new ProviderSetupError(`unknown provider "${request.provider}"`);
|
|
40
|
+
const apiKey = request.env[definition.keyEnv];
|
|
41
|
+
if (!apiKey)
|
|
42
|
+
throw new ProviderSetupError(`provider "${request.provider}" needs the environment variable ${definition.keyEnv}`);
|
|
43
|
+
const compat = definition.compat === undefined ? undefined : compatPresets[definition.compat];
|
|
44
|
+
if (definition.compat !== undefined && !compat)
|
|
45
|
+
throw new ProviderSetupError(`provider "${request.provider}": unknown compat preset "${definition.compat}" (known: ${Object.keys(compatPresets).join(", ")})`);
|
|
46
|
+
const headers = { ...definition.headers, ...(definition.sessionHeader ? { [definition.sessionHeader]: request.sessionId } : {}) };
|
|
47
|
+
const withImages = compat || definition.images ? { ...compat, ...(definition.images ? { images: true } : {}) } : undefined;
|
|
48
|
+
return {
|
|
49
|
+
baseUrl: definition.baseUrl,
|
|
50
|
+
apiKey,
|
|
51
|
+
model: request.model ?? definition.model,
|
|
52
|
+
contextWindow: definition.contextWindow,
|
|
53
|
+
...(withImages ? { compat: withImages } : {}),
|
|
54
|
+
...(Object.keys(headers).length > 0 ? { headers } : {}),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { PERMISSION_NAMES, PERMISSION_PRESETS } from "./permissions.js";
|
|
2
|
+
const number = (value) => value.toLocaleString("en-US");
|
|
3
|
+
/** Token counts the short way (12.3k, 6.40M), as the mature tools print them. */
|
|
4
|
+
export function tokens(value) {
|
|
5
|
+
if (value < 1000)
|
|
6
|
+
return String(value);
|
|
7
|
+
if (value < 999_500)
|
|
8
|
+
return `${(value / 1000).toFixed(value < 100_000 ? 1 : 0)}k`;
|
|
9
|
+
return `${(value / 1_000_000).toFixed(2)}M`;
|
|
10
|
+
}
|
|
11
|
+
/** The share of one reply's input that came from the cache: cache read / (cache read + new input + cache write). */
|
|
12
|
+
function replyHitRate(usage) {
|
|
13
|
+
const total = usage.input + usage.cacheRead + usage.cacheWrite;
|
|
14
|
+
return total > 0 ? usage.cacheRead / total : undefined;
|
|
15
|
+
}
|
|
16
|
+
/** The middle one of the per-reply cache hit rates of the model replies in a log (compaction and goal-check requests are not replies). */
|
|
17
|
+
export function medianReplyHitRate(entries) {
|
|
18
|
+
const rates = [];
|
|
19
|
+
for (const entry of entries) {
|
|
20
|
+
if (entry.type !== "message" || entry.message.role !== "assistant" || !entry.message.usage)
|
|
21
|
+
continue;
|
|
22
|
+
const rate = replyHitRate(entry.message.usage);
|
|
23
|
+
if (rate !== undefined)
|
|
24
|
+
rates.push(rate);
|
|
25
|
+
}
|
|
26
|
+
if (rates.length === 0)
|
|
27
|
+
return undefined;
|
|
28
|
+
rates.sort((a, b) => a - b);
|
|
29
|
+
const middle = Math.floor(rates.length / 2);
|
|
30
|
+
return rates.length % 2 === 1 ? rates[middle] : (rates[middle - 1] + rates[middle]) / 2;
|
|
31
|
+
}
|
|
32
|
+
const percent = (share) => `${(share * 100).toFixed(1)}%`;
|
|
33
|
+
export function statusReport(input) {
|
|
34
|
+
const free = input.context.window > 0 ? Math.round((input.context.free / input.context.window) * 100) : 0;
|
|
35
|
+
const rows = [
|
|
36
|
+
["Session", `\`${input.sessionId}\``],
|
|
37
|
+
["Session file", `\`${input.sessionFile}\``],
|
|
38
|
+
["Directory", `\`${input.cwd}\` (not restricted: the agent may go anywhere)`],
|
|
39
|
+
["Model", `${input.model} via ${input.provider}${input.thinking ? `, thinking ${input.thinking}` : ""}`],
|
|
40
|
+
["Permissions", `${input.permissions}: ${PERMISSION_PRESETS[input.permissions].summary}`],
|
|
41
|
+
["Redaction", input.redaction ? "on" : "off"],
|
|
42
|
+
["Context", `${input.contextEstimated ? "about " : ""}${number(input.context.tokens)} of ${number(input.context.window)} tokens used${input.contextEstimated ? " (estimate until the next reply)" : ""}, ${input.contextEstimated ? "~" : ""}${free}% left${input.context.compactAt ? `, compacts at ${number(input.context.compactAt)}` : ""}`],
|
|
43
|
+
...usageRows(input),
|
|
44
|
+
["Goal", input.goal ? `${input.goal.status}: ${input.goal.objective}` : "none"],
|
|
45
|
+
["Idle compaction", input.idleCompaction],
|
|
46
|
+
["State", input.working ? "working" : "idle"],
|
|
47
|
+
];
|
|
48
|
+
return ["| Item | Value |", "|---|---|", ...rows.map(([key, value]) => `| **${key}** | ${value} |`)].join("\n");
|
|
49
|
+
}
|
|
50
|
+
export function modelsReport(listings, current) {
|
|
51
|
+
const rows = listings.map((entry) => {
|
|
52
|
+
const here = entry.name === current.provider;
|
|
53
|
+
return `| ${here ? "▶ " : ""}\`${entry.name}\` | ${here ? current.model : entry.definition.model} | \`${entry.definition.keyEnv}\` ${entry.keyPresent ? "set" : "**not set**"} |`;
|
|
54
|
+
});
|
|
55
|
+
return ["| Provider | Model | Key |", "|---|---|---|", ...rows, "", "Switch with `/model <provider>` or `/model <provider> <model>`."].join("\n");
|
|
56
|
+
}
|
|
57
|
+
export function permissionsReport(current, redaction) {
|
|
58
|
+
const rows = PERMISSION_NAMES.map((name) => `| ${name === current ? "▶ " : ""}\`${name}\` | ${PERMISSION_PRESETS[name].summary} |`);
|
|
59
|
+
return ["| Preset | Meaning |", "|---|---|", ...rows, "", `Redaction is **${redaction ? "on" : "off"}** (\`/redaction on\`). The working directory is never restricted.`].join("\n");
|
|
60
|
+
}
|
|
61
|
+
export function mcpReport(status) {
|
|
62
|
+
const names = Object.keys(status);
|
|
63
|
+
if (names.length === 0)
|
|
64
|
+
return "No MCP servers are configured. Add them under `[mcp.servers.NAME]` in the config file.";
|
|
65
|
+
return ["| Server | State | Tools |", "|---|---|---|", ...names.map((name) => `| \`${name}\` | ${status[name].ok ? "connected" : `failed: ${status[name].error ?? "unknown"}`} | ${status[name].toolCount} |`)].join("\n");
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Token use in the shape of the mature tools: the whole input with the cached part named (as Claude Code's /usage does),
|
|
69
|
+
* the cache hit rate (as its "Prompt cache" line does), the output, and the replies they belong to.
|
|
70
|
+
*/
|
|
71
|
+
function usageRows(input) {
|
|
72
|
+
const { usage } = input;
|
|
73
|
+
const totalInput = usage.input + usage.cacheRead + usage.cacheWrite;
|
|
74
|
+
const written = usage.cacheWrite > 0 ? `, ${tokens(usage.cacheWrite)} written to the cache` : "";
|
|
75
|
+
const hit = totalInput > 0 ? `${percent(usage.cacheRead / totalInput)} of input tokens from the cache${input.medianHitRate === undefined ? "" : `; median reply ${percent(input.medianHitRate)}`}` : "no requests yet";
|
|
76
|
+
return [
|
|
77
|
+
["Input", totalInput > 0 ? `${tokens(totalInput)} tokens: ${tokens(usage.cacheRead)} cached, ${tokens(usage.input)} new${written}` : "none yet"],
|
|
78
|
+
["Cache hit rate", `${hit} (${number(usage.turns)} replies; compaction and goal checks included in the totals, review requests not counted)`],
|
|
79
|
+
["Output", `${tokens(usage.output)} tokens`],
|
|
80
|
+
];
|
|
81
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { textOf } from "./transcript.js";
|
|
2
|
+
export function createRunDriver(transcript) {
|
|
3
|
+
let working = false;
|
|
4
|
+
let current;
|
|
5
|
+
let pending = [];
|
|
6
|
+
const eventListeners = new Set();
|
|
7
|
+
const changeListeners = new Set();
|
|
8
|
+
const changed = () => {
|
|
9
|
+
for (const listener of changeListeners)
|
|
10
|
+
listener();
|
|
11
|
+
};
|
|
12
|
+
/** The kernel announces a steered or queued message with a user `message_end` when it takes it. */
|
|
13
|
+
function delivered(event) {
|
|
14
|
+
if (event.type !== "message_end" || event.message.role !== "user")
|
|
15
|
+
return;
|
|
16
|
+
const text = textOf(event.message.content);
|
|
17
|
+
const index = pending.findIndex((item) => item.text === text);
|
|
18
|
+
if (index !== -1) {
|
|
19
|
+
pending.splice(index, 1);
|
|
20
|
+
changed();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
get working() {
|
|
25
|
+
return working;
|
|
26
|
+
},
|
|
27
|
+
pending: () => pending,
|
|
28
|
+
async run(agent, input, options) {
|
|
29
|
+
working = true;
|
|
30
|
+
pending = [];
|
|
31
|
+
current = { agent };
|
|
32
|
+
changed();
|
|
33
|
+
const handle = agent.run(input, options);
|
|
34
|
+
try {
|
|
35
|
+
for await (const event of handle.events) {
|
|
36
|
+
transcript.apply(event);
|
|
37
|
+
for (const listener of eventListeners)
|
|
38
|
+
listener(event);
|
|
39
|
+
delivered(event);
|
|
40
|
+
}
|
|
41
|
+
const result = await handle.result;
|
|
42
|
+
const leftover = pending;
|
|
43
|
+
pending = [];
|
|
44
|
+
return { result, leftover };
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
working = false;
|
|
48
|
+
current = undefined;
|
|
49
|
+
changed();
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
steer(text) {
|
|
53
|
+
if (!working || !current)
|
|
54
|
+
return;
|
|
55
|
+
pending.push({ kind: "steer", text });
|
|
56
|
+
current.agent.steer(text);
|
|
57
|
+
changed();
|
|
58
|
+
},
|
|
59
|
+
queue(text) {
|
|
60
|
+
if (!working || !current)
|
|
61
|
+
return;
|
|
62
|
+
pending.push({ kind: "queue", text });
|
|
63
|
+
current.agent.queue(text);
|
|
64
|
+
changed();
|
|
65
|
+
},
|
|
66
|
+
cancel: () => current?.agent.cancel(),
|
|
67
|
+
onEvent(listener) {
|
|
68
|
+
eventListeners.add(listener);
|
|
69
|
+
return () => eventListeners.delete(listener);
|
|
70
|
+
},
|
|
71
|
+
onChange(listener) {
|
|
72
|
+
changeListeners.add(listener);
|
|
73
|
+
return () => changeListeners.delete(listener);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|