sakuracode 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +256 -2
- package/README.zh-CN.md +257 -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/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 +31 -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 +405 -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 +26 -0
- package/dist/code/src/toml.js +256 -0
- package/dist/code/src/transcript.js +288 -0
- package/dist/code/src/tui/app.js +267 -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,92 @@
|
|
|
1
|
+
// Where sessions live. The framework owns the conversation file (<id>.jsonl); this module adds a small metadata file beside it
|
|
2
|
+
// (<id>.meta.json) so that a session can be listed, found and titled without parsing the whole conversation.
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { mkdir, readdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
5
|
+
import { basename, join } from "node:path";
|
|
6
|
+
export class SessionLookupError extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "SessionLookupError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function createSessionStore(root, options = {}) {
|
|
13
|
+
const now = options.now ?? Date.now;
|
|
14
|
+
const metaPath = (id) => join(root, `${id}.meta.json`);
|
|
15
|
+
async function write(meta) {
|
|
16
|
+
await mkdir(root, { recursive: true });
|
|
17
|
+
const temporary = `${metaPath(meta.id)}.${process.pid}.tmp`;
|
|
18
|
+
await writeFile(temporary, `${JSON.stringify(meta, null, 2)}\n`);
|
|
19
|
+
await rename(temporary, metaPath(meta.id));
|
|
20
|
+
}
|
|
21
|
+
async function get(id) {
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(await readFile(metaPath(id), "utf8"));
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (error.code === "ENOENT")
|
|
27
|
+
return undefined;
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function list(filter = {}) {
|
|
32
|
+
let names;
|
|
33
|
+
try {
|
|
34
|
+
names = await readdir(root);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (error.code === "ENOENT")
|
|
38
|
+
return [];
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
const metas = await Promise.all(names.filter((name) => name.endsWith(".meta.json")).map((name) => get(name.slice(0, -".meta.json".length)).catch(() => undefined)));
|
|
42
|
+
return metas
|
|
43
|
+
.filter((meta) => meta !== undefined && (filter.cwd === undefined || meta.cwd === filter.cwd))
|
|
44
|
+
.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
root,
|
|
48
|
+
sessionFile: (id) => join(root, `${id}.jsonl`),
|
|
49
|
+
get,
|
|
50
|
+
list,
|
|
51
|
+
async create(input) {
|
|
52
|
+
const stamp = now();
|
|
53
|
+
const meta = { id: randomUUID(), cwd: input.cwd, provider: input.provider, model: input.model, ...(input.parentId ? { parentId: input.parentId } : {}), createdAt: stamp, updatedAt: stamp };
|
|
54
|
+
await write(meta);
|
|
55
|
+
return meta;
|
|
56
|
+
},
|
|
57
|
+
async update(id, patch) {
|
|
58
|
+
const current = await get(id);
|
|
59
|
+
if (!current)
|
|
60
|
+
throw new SessionLookupError(`no session ${id}`);
|
|
61
|
+
const next = { ...current, ...Object.fromEntries(Object.entries(patch).filter(([, value]) => value !== undefined)), updatedAt: now() };
|
|
62
|
+
await write(next);
|
|
63
|
+
return next;
|
|
64
|
+
},
|
|
65
|
+
async remove(id) {
|
|
66
|
+
await rm(metaPath(id), { force: true });
|
|
67
|
+
await rm(join(root, `${id}.jsonl`), { force: true });
|
|
68
|
+
},
|
|
69
|
+
async find(reference, filter = {}) {
|
|
70
|
+
if (reference === "last")
|
|
71
|
+
return (await list(filter))[0];
|
|
72
|
+
if (reference.endsWith(".jsonl"))
|
|
73
|
+
return get(basename(reference, ".jsonl"));
|
|
74
|
+
const exact = await get(reference);
|
|
75
|
+
if (exact)
|
|
76
|
+
return exact;
|
|
77
|
+
const matches = (await list(filter)).filter((meta) => meta.id.startsWith(reference));
|
|
78
|
+
if (matches.length > 1)
|
|
79
|
+
throw new SessionLookupError(`"${reference}" matches ${matches.length} sessions; give more characters`);
|
|
80
|
+
return matches[0];
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** A one-line label for pickers: the title, or the id when the session has none yet. */
|
|
85
|
+
export function sessionLabel(meta) {
|
|
86
|
+
return meta.title ?? `(untitled ${meta.id.slice(0, 8)})`;
|
|
87
|
+
}
|
|
88
|
+
/** The title is the first line of the first prompt, shortened. */
|
|
89
|
+
export function titleFromPrompt(prompt, limit = 60) {
|
|
90
|
+
const line = prompt.trim().split("\n")[0].replace(/\s+/g, " ");
|
|
91
|
+
return line.length > limit ? `${line.slice(0, limit - 1)}…` : line;
|
|
92
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Running a shell command for the user (`!cmd`, /diff). Not used by the agent; its own bash tool is separate.
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
const MAX_OUTPUT = 200_000;
|
|
4
|
+
export const runShell = (command, cwd, signal) => new Promise((resolve) => {
|
|
5
|
+
const child = spawn("/bin/sh", ["-c", command], { cwd, stdio: ["ignore", "pipe", "pipe"], detached: true });
|
|
6
|
+
let output = "";
|
|
7
|
+
let timedOut = false;
|
|
8
|
+
const collect = (chunk) => {
|
|
9
|
+
if (output.length < MAX_OUTPUT)
|
|
10
|
+
output += chunk.toString("utf8");
|
|
11
|
+
};
|
|
12
|
+
child.stdout.on("data", collect);
|
|
13
|
+
child.stderr.on("data", collect);
|
|
14
|
+
const kill = () => {
|
|
15
|
+
try {
|
|
16
|
+
if (child.pid)
|
|
17
|
+
process.kill(-child.pid, "SIGKILL");
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
child.kill("SIGKILL");
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const timer = setTimeout(() => {
|
|
24
|
+
timedOut = true;
|
|
25
|
+
kill();
|
|
26
|
+
}, 120_000);
|
|
27
|
+
signal?.addEventListener("abort", kill, { once: true });
|
|
28
|
+
child.on("error", (error) => {
|
|
29
|
+
clearTimeout(timer);
|
|
30
|
+
resolve({ output: `${output}${error.message}`, exitCode: null, timedOut });
|
|
31
|
+
});
|
|
32
|
+
child.on("close", (code) => {
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
resolve({ output: output.length >= MAX_OUTPUT ? `${output.slice(0, MAX_OUTPUT)}\n[output cut]` : output, exitCode: code, timedOut });
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Slash commands: the table, parsing a typed line, and completion. A handler talks to the application only through CommandHost.
|
|
2
|
+
const INIT_PROMPT = "[/init] Create an AGENTS.md file in the current directory that tells future coding agents how to work in this project: what it is, how to build, test and run it, the code layout, and the conventions you can see in the code. Look at the project first (package manifests, README, source layout, existing tests). Keep it short and factual; do not invent commands you have not seen. If an AGENTS.md already exists, improve it instead of replacing it.";
|
|
3
|
+
/** The prompt for a review: the uncommitted changes (default), the changes against a base branch, or one commit. */
|
|
4
|
+
export function reviewPrompt(target = {}) {
|
|
5
|
+
const what = target.base
|
|
6
|
+
? `the changes of this branch against \`${target.base}\` (use \`git diff ${target.base}...HEAD\` and \`git log ${target.base}..HEAD\`)`
|
|
7
|
+
: target.commit
|
|
8
|
+
? `the commit ${target.commit} (use \`git show ${target.commit}\`)`
|
|
9
|
+
: "the uncommitted changes: staged, unstaged and untracked files (use `git status`, `git diff` and `git diff --staged`; read untracked files directly; if there are no changes, review the last commit with `git show`)";
|
|
10
|
+
const focus = target.focus ? `\n\nFocus of this review: ${target.focus}` : "";
|
|
11
|
+
return `[/review] Review ${what}. Use your shell tool; read the surrounding code where needed. Report problems in order of severity: bugs, risky behaviour, missing tests, unclear code. For each point give the file and line and a concrete suggestion. Do not change any files.${focus}`;
|
|
12
|
+
}
|
|
13
|
+
export const COMMANDS = [
|
|
14
|
+
{ name: "help", aliases: ["?"], usage: "/help", summary: "list the commands", duringRun: true, run: (_args, host) => host.print(host.help()) },
|
|
15
|
+
{ name: "status", usage: "/status", summary: "session, model, permissions and token usage", duringRun: true, run: async (_args, host) => host.print(await host.status()) },
|
|
16
|
+
{
|
|
17
|
+
name: "model",
|
|
18
|
+
usage: "/model [provider[/model]]",
|
|
19
|
+
summary: "show or switch the provider and model",
|
|
20
|
+
run(args, host) {
|
|
21
|
+
const text = args.trim();
|
|
22
|
+
if (text === "")
|
|
23
|
+
return host.print(host.models());
|
|
24
|
+
const [first, second] = text.split(/[\s/]+/, 2);
|
|
25
|
+
return host.switchModel(first, second);
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
{ name: "permissions", aliases: ["approvals"], usage: "/permissions [auto|read-only|strict|full-access]", summary: "show or change what is reviewed", run: (args, host) => host.permissions(args.trim() || undefined) },
|
|
29
|
+
{
|
|
30
|
+
name: "redaction",
|
|
31
|
+
usage: "/redaction [on|off]",
|
|
32
|
+
summary: "hide secrets from the model (off by default)",
|
|
33
|
+
run(args, host) {
|
|
34
|
+
const value = args.trim().toLowerCase();
|
|
35
|
+
if (value !== "" && value !== "on" && value !== "off")
|
|
36
|
+
return host.fail("Usage: /redaction [on|off]");
|
|
37
|
+
return host.redaction(value === "" ? undefined : value);
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{ name: "compact", usage: "/compact", summary: "summarise the conversation to free context", run: (_args, host) => host.compact() },
|
|
41
|
+
{ name: "new", usage: "/new", summary: "start a new session", run: (_args, host) => host.newSession() },
|
|
42
|
+
{ name: "clear", usage: "/clear", summary: "clear the screen and start a new session", run: (_args, host) => host.clearScreen() },
|
|
43
|
+
{ name: "resume", usage: "/resume [id|last]", summary: "continue an earlier session", run: (args, host) => host.resume(args.trim() || undefined) },
|
|
44
|
+
{ name: "fork", usage: "/fork", summary: "copy this session into a new one and continue there", run: (_args, host) => host.fork() },
|
|
45
|
+
{ name: "undo", usage: "/undo", summary: "go back to before your last message", run: (_args, host) => host.undo() },
|
|
46
|
+
{
|
|
47
|
+
name: "rename",
|
|
48
|
+
usage: "/rename <name>",
|
|
49
|
+
summary: "give this session a name",
|
|
50
|
+
run(args, host) {
|
|
51
|
+
if (args.trim() === "")
|
|
52
|
+
return host.fail("Usage: /rename <name>");
|
|
53
|
+
return host.rename(args.trim());
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
{ name: "init", usage: "/init", summary: "have the agent write an AGENTS.md for this project", run: (_args, host) => host.runPrompt(INIT_PROMPT) },
|
|
57
|
+
{ name: "review", usage: "/review [focus]", summary: "have the agent review the uncommitted changes", run: (args, host) => host.runPrompt(reviewPrompt({ focus: args.trim() })) },
|
|
58
|
+
{ name: "diff", usage: "/diff", summary: "show git diff", duringRun: true, run: (_args, host) => host.diff() },
|
|
59
|
+
{ name: "goal", usage: "/goal [objective|clear]", summary: "show, set or clear the goal of this session", run: (args, host) => host.goal(args.trim() || undefined) },
|
|
60
|
+
{ name: "mcp", usage: "/mcp", summary: "show MCP servers", duringRun: true, run: (_args, host) => host.print(host.mcp()) },
|
|
61
|
+
{ name: "exit", aliases: ["quit", "q"], usage: "/exit", summary: "leave", duringRun: true, run: (_args, host) => host.exit() },
|
|
62
|
+
];
|
|
63
|
+
/** A line is a command when it starts with "/" followed by a word and then a space or the end; "/usr/bin/x" is not. */
|
|
64
|
+
export function parseSlash(line) {
|
|
65
|
+
const match = /^\/([A-Za-z?][\w-]*)(?:\s+([\s\S]*))?$/.exec(line.trimEnd());
|
|
66
|
+
if (!match)
|
|
67
|
+
return undefined;
|
|
68
|
+
return { name: match[1].toLowerCase(), args: match[2] ?? "" };
|
|
69
|
+
}
|
|
70
|
+
export function findCommand(name) {
|
|
71
|
+
return COMMANDS.find((command) => command.name === name || command.aliases?.includes(name));
|
|
72
|
+
}
|
|
73
|
+
/** Commands whose name starts with what was typed, for the popup. Typing a space ends completion. */
|
|
74
|
+
export function complete(input) {
|
|
75
|
+
if (!input.startsWith("/") || /\s/.test(input))
|
|
76
|
+
return [];
|
|
77
|
+
const prefix = input.slice(1).toLowerCase();
|
|
78
|
+
return COMMANDS.filter((command) => command.name.startsWith(prefix));
|
|
79
|
+
}
|
|
80
|
+
export function helpMarkdown() {
|
|
81
|
+
const rows = COMMANDS.map((command) => `| \`${command.usage}\` | ${command.summary} |`);
|
|
82
|
+
return ["| Command | What it does |", "|---|---|", ...rows, "", "While the agent works: **Enter** steers it, **Tab** queues a message, **Esc** cancels. `!cmd` runs a shell command yourself."].join("\n");
|
|
83
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// The status line under the prompt: model, effort, directory and how much context is left (the Codex layout).
|
|
2
|
+
import { createPalette, displayWidth, truncate } from "./ansi.js";
|
|
3
|
+
export function shortenPath(path, home, width) {
|
|
4
|
+
const display = home && (path === home || path.startsWith(`${home}/`)) ? `~${path.slice(home.length)}` : path;
|
|
5
|
+
if (display.length <= width)
|
|
6
|
+
return display;
|
|
7
|
+
const parts = display.split("/");
|
|
8
|
+
const tail = parts.slice(-2).join("/");
|
|
9
|
+
return `…/${tail}`.length <= width ? `…/${tail}` : `…${display.slice(-(width - 1))}`;
|
|
10
|
+
}
|
|
11
|
+
export function contextLeftPercent(free, window) {
|
|
12
|
+
return window <= 0 ? 0 : Math.max(0, Math.min(100, Math.round((free / window) * 100)));
|
|
13
|
+
}
|
|
14
|
+
export function formatStatusLine(info, width, color) {
|
|
15
|
+
const pal = createPalette(color);
|
|
16
|
+
const pieces = [
|
|
17
|
+
pal.bold(info.model) + (info.thinking ? pal.dim(` ${info.thinking}`) : ""),
|
|
18
|
+
info.contextLeftPercent === undefined ? "" : `${info.contextLeftPercent < 15 ? pal.red(`${info.contextLeftPercent}%`) : `${info.contextLeftPercent}%`} context left`,
|
|
19
|
+
info.permissions === "auto" ? "" : pal.yellow(info.permissions),
|
|
20
|
+
info.redaction ? pal.magenta("redaction") : "",
|
|
21
|
+
info.queued > 0 ? pal.cyan(`${info.queued} queued`) : "",
|
|
22
|
+
shortenPath(info.cwd, info.home, Math.max(12, Math.floor(width / 3))),
|
|
23
|
+
].filter((piece) => piece !== "");
|
|
24
|
+
const line = pieces.join(pal.dim(" · "));
|
|
25
|
+
return displayWidth(line) > width ? truncate(line, width) : line;
|
|
26
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// A parser for the TOML subset the config file uses: comments, [tables] and [dotted.tables], key = value with dotted keys,
|
|
2
|
+
// basic and literal strings, integers, floats, booleans, arrays (also over several lines) and inline tables.
|
|
3
|
+
// Not supported (reported as errors): dates, multi-line strings, arrays of tables.
|
|
4
|
+
export class TomlError extends Error {
|
|
5
|
+
constructor(message, line) {
|
|
6
|
+
super(`config line ${line}: ${message}`);
|
|
7
|
+
this.name = "TomlError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
class Cursor {
|
|
11
|
+
position = 0;
|
|
12
|
+
text;
|
|
13
|
+
constructor(text) {
|
|
14
|
+
this.text = text;
|
|
15
|
+
}
|
|
16
|
+
peek() {
|
|
17
|
+
return this.text[this.position];
|
|
18
|
+
}
|
|
19
|
+
rest() {
|
|
20
|
+
return this.text.slice(this.position);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function parseToml(source) {
|
|
24
|
+
const root = {};
|
|
25
|
+
let current = root;
|
|
26
|
+
const lines = source.replace(/\r\n?/g, "\n").split("\n");
|
|
27
|
+
for (let lineIndex = 0; lineIndex < lines.length; lineIndex += 1) {
|
|
28
|
+
const number = lineIndex + 1;
|
|
29
|
+
let line = stripComment(lines[lineIndex]).trim();
|
|
30
|
+
if (line === "")
|
|
31
|
+
continue;
|
|
32
|
+
if (line.startsWith("[["))
|
|
33
|
+
throw new TomlError("arrays of tables are not supported", number);
|
|
34
|
+
if (line.startsWith("[")) {
|
|
35
|
+
if (!line.endsWith("]"))
|
|
36
|
+
throw new TomlError("a table header must end with ]", number);
|
|
37
|
+
current = descend(root, parseKeyPath(line.slice(1, -1), number), number);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
// a value that opens a bracket may continue on the next lines
|
|
41
|
+
while (needsMore(line) && lineIndex + 1 < lines.length) {
|
|
42
|
+
lineIndex += 1;
|
|
43
|
+
line += ` ${stripComment(lines[lineIndex]).trim()}`;
|
|
44
|
+
}
|
|
45
|
+
const equals = findEquals(line);
|
|
46
|
+
if (equals === -1)
|
|
47
|
+
throw new TomlError("expected key = value", number);
|
|
48
|
+
const path = parseKeyPath(line.slice(0, equals), number);
|
|
49
|
+
const cursor = new Cursor(line.slice(equals + 1).trim());
|
|
50
|
+
const value = parseValue(cursor, number);
|
|
51
|
+
if (cursor.rest().trim() !== "")
|
|
52
|
+
throw new TomlError(`unexpected text after value: ${cursor.rest().trim()}`, number);
|
|
53
|
+
const holder = descend(current, path.slice(0, -1), number);
|
|
54
|
+
const key = path[path.length - 1];
|
|
55
|
+
if (key in holder)
|
|
56
|
+
throw new TomlError(`duplicate key ${key}`, number);
|
|
57
|
+
holder[key] = value;
|
|
58
|
+
}
|
|
59
|
+
return root;
|
|
60
|
+
}
|
|
61
|
+
function stripComment(line) {
|
|
62
|
+
let quote;
|
|
63
|
+
for (let index = 0; index < line.length; index += 1) {
|
|
64
|
+
const char = line[index];
|
|
65
|
+
if (quote) {
|
|
66
|
+
if (char === "\\" && quote === '"')
|
|
67
|
+
index += 1;
|
|
68
|
+
else if (char === quote)
|
|
69
|
+
quote = undefined;
|
|
70
|
+
}
|
|
71
|
+
else if (char === '"' || char === "'")
|
|
72
|
+
quote = char;
|
|
73
|
+
else if (char === "#")
|
|
74
|
+
return line.slice(0, index);
|
|
75
|
+
}
|
|
76
|
+
return line;
|
|
77
|
+
}
|
|
78
|
+
function needsMore(line) {
|
|
79
|
+
let depth = 0;
|
|
80
|
+
let quote;
|
|
81
|
+
for (let index = 0; index < line.length; index += 1) {
|
|
82
|
+
const char = line[index];
|
|
83
|
+
if (quote) {
|
|
84
|
+
if (char === "\\" && quote === '"')
|
|
85
|
+
index += 1;
|
|
86
|
+
else if (char === quote)
|
|
87
|
+
quote = undefined;
|
|
88
|
+
}
|
|
89
|
+
else if (char === '"' || char === "'")
|
|
90
|
+
quote = char;
|
|
91
|
+
else if (char === "[" || char === "{")
|
|
92
|
+
depth += 1;
|
|
93
|
+
else if (char === "]" || char === "}")
|
|
94
|
+
depth -= 1;
|
|
95
|
+
}
|
|
96
|
+
return depth > 0;
|
|
97
|
+
}
|
|
98
|
+
function findEquals(line) {
|
|
99
|
+
let quote;
|
|
100
|
+
for (let index = 0; index < line.length; index += 1) {
|
|
101
|
+
const char = line[index];
|
|
102
|
+
if (quote) {
|
|
103
|
+
if (char === quote)
|
|
104
|
+
quote = undefined;
|
|
105
|
+
}
|
|
106
|
+
else if (char === '"' || char === "'")
|
|
107
|
+
quote = char;
|
|
108
|
+
else if (char === "=")
|
|
109
|
+
return index;
|
|
110
|
+
}
|
|
111
|
+
return -1;
|
|
112
|
+
}
|
|
113
|
+
function parseKeyPath(text, line) {
|
|
114
|
+
const keys = [];
|
|
115
|
+
let index = 0;
|
|
116
|
+
const source = text.trim();
|
|
117
|
+
while (index < source.length) {
|
|
118
|
+
while (source[index] === " ")
|
|
119
|
+
index += 1;
|
|
120
|
+
const char = source[index];
|
|
121
|
+
if (char === '"' || char === "'") {
|
|
122
|
+
const cursor = new Cursor(source.slice(index));
|
|
123
|
+
keys.push(parseString(cursor, line));
|
|
124
|
+
index += cursor.position;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const match = /^[A-Za-z0-9_-]+/.exec(source.slice(index));
|
|
128
|
+
if (!match)
|
|
129
|
+
throw new TomlError(`invalid key near ${source.slice(index)}`, line);
|
|
130
|
+
keys.push(match[0]);
|
|
131
|
+
index += match[0].length;
|
|
132
|
+
}
|
|
133
|
+
while (source[index] === " ")
|
|
134
|
+
index += 1;
|
|
135
|
+
if (index < source.length) {
|
|
136
|
+
if (source[index] !== ".")
|
|
137
|
+
throw new TomlError(`invalid key near ${source.slice(index)}`, line);
|
|
138
|
+
index += 1;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (keys.length === 0)
|
|
142
|
+
throw new TomlError("empty key", line);
|
|
143
|
+
return keys;
|
|
144
|
+
}
|
|
145
|
+
function descend(root, path, line) {
|
|
146
|
+
let table = root;
|
|
147
|
+
for (const key of path) {
|
|
148
|
+
const existing = table[key];
|
|
149
|
+
if (existing === undefined) {
|
|
150
|
+
const created = {};
|
|
151
|
+
table[key] = created;
|
|
152
|
+
table = created;
|
|
153
|
+
}
|
|
154
|
+
else if (typeof existing === "object" && !Array.isArray(existing))
|
|
155
|
+
table = existing;
|
|
156
|
+
else
|
|
157
|
+
throw new TomlError(`${key} is not a table`, line);
|
|
158
|
+
}
|
|
159
|
+
return table;
|
|
160
|
+
}
|
|
161
|
+
function parseValue(cursor, line) {
|
|
162
|
+
skipSpace(cursor);
|
|
163
|
+
const char = cursor.peek();
|
|
164
|
+
if (char === '"' || char === "'")
|
|
165
|
+
return parseString(cursor, line);
|
|
166
|
+
if (char === "[")
|
|
167
|
+
return parseArray(cursor, line);
|
|
168
|
+
if (char === "{")
|
|
169
|
+
return parseInlineTable(cursor, line);
|
|
170
|
+
const match = /^[^\s,\]}]+/.exec(cursor.rest());
|
|
171
|
+
if (!match)
|
|
172
|
+
throw new TomlError("missing value", line);
|
|
173
|
+
cursor.position += match[0].length;
|
|
174
|
+
const word = match[0];
|
|
175
|
+
if (word === "true")
|
|
176
|
+
return true;
|
|
177
|
+
if (word === "false")
|
|
178
|
+
return false;
|
|
179
|
+
if (/^[+-]?\d[\d_]*$/.test(word))
|
|
180
|
+
return Number.parseInt(word.replace(/_/g, ""), 10);
|
|
181
|
+
if (/^[+-]?\d[\d_]*\.\d[\d_]*([eE][+-]?\d+)?$/.test(word))
|
|
182
|
+
return Number.parseFloat(word.replace(/_/g, ""));
|
|
183
|
+
throw new TomlError(`cannot read value ${word}`, line);
|
|
184
|
+
}
|
|
185
|
+
function skipSpace(cursor) {
|
|
186
|
+
while (cursor.peek() === " " || cursor.peek() === "\t")
|
|
187
|
+
cursor.position += 1;
|
|
188
|
+
}
|
|
189
|
+
const ESCAPES = { n: "\n", t: "\t", r: "\r", '"': '"', "\\": "\\", b: "\b", f: "\f" };
|
|
190
|
+
function parseString(cursor, line) {
|
|
191
|
+
const quote = cursor.peek();
|
|
192
|
+
cursor.position += 1;
|
|
193
|
+
let out = "";
|
|
194
|
+
while (cursor.position < cursor.text.length) {
|
|
195
|
+
const char = cursor.text[cursor.position];
|
|
196
|
+
cursor.position += 1;
|
|
197
|
+
if (char === quote)
|
|
198
|
+
return out;
|
|
199
|
+
if (char === "\\" && quote === '"') {
|
|
200
|
+
const next = cursor.text[cursor.position];
|
|
201
|
+
cursor.position += 1;
|
|
202
|
+
if (next === "u") {
|
|
203
|
+
const hex = cursor.text.slice(cursor.position, cursor.position + 4);
|
|
204
|
+
out += String.fromCodePoint(Number.parseInt(hex, 16));
|
|
205
|
+
cursor.position += 4;
|
|
206
|
+
}
|
|
207
|
+
else if (next !== undefined && ESCAPES[next] !== undefined)
|
|
208
|
+
out += ESCAPES[next];
|
|
209
|
+
else
|
|
210
|
+
throw new TomlError(`unknown escape \\${next ?? ""}`, line);
|
|
211
|
+
}
|
|
212
|
+
else
|
|
213
|
+
out += char;
|
|
214
|
+
}
|
|
215
|
+
throw new TomlError("unterminated string", line);
|
|
216
|
+
}
|
|
217
|
+
function parseArray(cursor, line) {
|
|
218
|
+
cursor.position += 1;
|
|
219
|
+
const items = [];
|
|
220
|
+
for (;;) {
|
|
221
|
+
skipSpace(cursor);
|
|
222
|
+
if (cursor.peek() === "]") {
|
|
223
|
+
cursor.position += 1;
|
|
224
|
+
return items;
|
|
225
|
+
}
|
|
226
|
+
items.push(parseValue(cursor, line));
|
|
227
|
+
skipSpace(cursor);
|
|
228
|
+
if (cursor.peek() === ",")
|
|
229
|
+
cursor.position += 1;
|
|
230
|
+
else if (cursor.peek() !== "]")
|
|
231
|
+
throw new TomlError("expected , or ] in array", line);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function parseInlineTable(cursor, line) {
|
|
235
|
+
cursor.position += 1;
|
|
236
|
+
const table = {};
|
|
237
|
+
for (;;) {
|
|
238
|
+
skipSpace(cursor);
|
|
239
|
+
if (cursor.peek() === "}") {
|
|
240
|
+
cursor.position += 1;
|
|
241
|
+
return table;
|
|
242
|
+
}
|
|
243
|
+
const equals = cursor.text.indexOf("=", cursor.position);
|
|
244
|
+
if (equals === -1)
|
|
245
|
+
throw new TomlError("expected key = value in inline table", line);
|
|
246
|
+
const path = parseKeyPath(cursor.text.slice(cursor.position, equals), line);
|
|
247
|
+
cursor.position = equals + 1;
|
|
248
|
+
const holder = descend(table, path.slice(0, -1), line);
|
|
249
|
+
holder[path[path.length - 1]] = parseValue(cursor, line);
|
|
250
|
+
skipSpace(cursor);
|
|
251
|
+
if (cursor.peek() === ",")
|
|
252
|
+
cursor.position += 1;
|
|
253
|
+
else if (cursor.peek() !== "}")
|
|
254
|
+
throw new TomlError("expected , or } in inline table", line);
|
|
255
|
+
}
|
|
256
|
+
}
|