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,273 @@
|
|
|
1
|
+
// The interactive session: connects the terminal (keys in, painted lines out) to the session controller.
|
|
2
|
+
// Everything that decides what a line means lives in the controller; everything about how it looks lives in the views.
|
|
3
|
+
import { pastedImagePath } from "../images.js";
|
|
4
|
+
import { complete } from "../slash-commands.js";
|
|
5
|
+
import { createController } from "../session-controller.js";
|
|
6
|
+
import { formatStatusLine } from "../status-line.js";
|
|
7
|
+
import { createTranscript } from "../transcript.js";
|
|
8
|
+
import { createKeyDecoder } from "./keys.js";
|
|
9
|
+
import { createLineEditor } from "./line-editor.js";
|
|
10
|
+
import { createIdleCompaction } from "../idle-compaction.js";
|
|
11
|
+
import { createLiveRegion } from "./live-region.js";
|
|
12
|
+
import { composeLive } from "./screen.js";
|
|
13
|
+
import { renderBanner, renderBlock, spacingBetween } from "./transcript-view.js";
|
|
14
|
+
const EXIT_CONFIRM_MS = 2000;
|
|
15
|
+
export async function runTui(options) {
|
|
16
|
+
const { stdin, stdout, color } = options;
|
|
17
|
+
const transcript = createTranscript();
|
|
18
|
+
const live = createLiveRegion(stdout, { pinned: true });
|
|
19
|
+
const editor = createLineEditor();
|
|
20
|
+
const decoder = createKeyDecoder();
|
|
21
|
+
const width = () => Math.max(30, stdout.columns ?? 80);
|
|
22
|
+
let picker;
|
|
23
|
+
let popupIndex = 0;
|
|
24
|
+
let popupDismissedFor;
|
|
25
|
+
let hint;
|
|
26
|
+
let hintTimer;
|
|
27
|
+
let frame = 0;
|
|
28
|
+
let workStartedAt = Date.now();
|
|
29
|
+
let lastCtrlC = 0;
|
|
30
|
+
let lastEscape = 0;
|
|
31
|
+
let paintTimer;
|
|
32
|
+
let finished = false;
|
|
33
|
+
let finish;
|
|
34
|
+
const done = new Promise((resolve) => (finish = resolve));
|
|
35
|
+
let lastPrinted;
|
|
36
|
+
const surface = {
|
|
37
|
+
changed: () => schedulePaint(),
|
|
38
|
+
pickSession(sessions) {
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
picker = { title: "Resume a session", items: sessions, index: 0, resolve };
|
|
41
|
+
schedulePaint();
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
clearScreen() {
|
|
45
|
+
stdout.write("\u001b[2J\u001b[3J\u001b[H");
|
|
46
|
+
live.cleared();
|
|
47
|
+
lastPrinted = undefined;
|
|
48
|
+
},
|
|
49
|
+
prefill: (text) => editor.set(text),
|
|
50
|
+
exit: () => finish(0),
|
|
51
|
+
};
|
|
52
|
+
const controller = createController({ ...options.controller, transcript, surface });
|
|
53
|
+
// ----- painting -----
|
|
54
|
+
const popupCommands = () => {
|
|
55
|
+
const value = editor.value();
|
|
56
|
+
return picker || value === popupDismissedFor ? [] : complete(value);
|
|
57
|
+
};
|
|
58
|
+
function paint() {
|
|
59
|
+
paintTimer = undefined;
|
|
60
|
+
if (finished)
|
|
61
|
+
return;
|
|
62
|
+
const state = transcript.state();
|
|
63
|
+
const commands = popupCommands();
|
|
64
|
+
popupIndex = Math.min(popupIndex, Math.max(0, commands.length - 1));
|
|
65
|
+
const view = composeLive({
|
|
66
|
+
width: width(),
|
|
67
|
+
color,
|
|
68
|
+
composer: editor.layout(width() - 3),
|
|
69
|
+
statusLine: formatStatusLine(controller.statusInfo(), width() - 2, color),
|
|
70
|
+
working: controller.driver.working,
|
|
71
|
+
elapsedSeconds: Math.floor((Date.now() - workStartedAt) / 1000),
|
|
72
|
+
frame,
|
|
73
|
+
runningTools: state.running,
|
|
74
|
+
streamingTail: state.pending,
|
|
75
|
+
reasoning: state.reasoning,
|
|
76
|
+
pending: controller.pending(),
|
|
77
|
+
popup: commands.length > 0 ? { commands, index: popupIndex } : undefined,
|
|
78
|
+
picker,
|
|
79
|
+
hint,
|
|
80
|
+
maxComposerRows: Math.max(3, Math.min(8, Math.floor((stdout.rows ?? 24) / 3))),
|
|
81
|
+
});
|
|
82
|
+
live.setLive(view.lines, view.cursor);
|
|
83
|
+
}
|
|
84
|
+
function schedulePaint() {
|
|
85
|
+
paintTimer ??= setTimeout(paint, 16);
|
|
86
|
+
}
|
|
87
|
+
function printBlock(block) {
|
|
88
|
+
const spacing = spacingBetween(lastPrinted, block);
|
|
89
|
+
lastPrinted = block;
|
|
90
|
+
live.print([...Array(spacing).fill(""), ...renderBlock(block, { width: width(), color, cwd: options.controller.cwd })]);
|
|
91
|
+
}
|
|
92
|
+
const { settings } = options.controller;
|
|
93
|
+
const idle = createIdleCompaction({
|
|
94
|
+
enabled: settings.autoCompactIdle,
|
|
95
|
+
idleMs: settings.cacheTtlMinutes * 60_000,
|
|
96
|
+
retryMs: Math.min(30_000, settings.cacheTtlMinutes * 60_000),
|
|
97
|
+
busy: () => controller.driver.working,
|
|
98
|
+
typing: () => editor.value() !== "" || picker !== undefined,
|
|
99
|
+
compact: () => controller.compactIdle(settings.autoCompactMinTokens, settings.cacheTtlMinutes),
|
|
100
|
+
});
|
|
101
|
+
idle.touch();
|
|
102
|
+
controller.driver.onChange(() => {
|
|
103
|
+
if (controller.driver.working)
|
|
104
|
+
workStartedAt = Date.now();
|
|
105
|
+
idle.touch();
|
|
106
|
+
schedulePaint();
|
|
107
|
+
});
|
|
108
|
+
const spinner = setInterval(() => {
|
|
109
|
+
if (!controller.driver.working)
|
|
110
|
+
return;
|
|
111
|
+
frame += 1;
|
|
112
|
+
schedulePaint();
|
|
113
|
+
}, 100);
|
|
114
|
+
function showHint(text) {
|
|
115
|
+
hint = text;
|
|
116
|
+
clearTimeout(hintTimer);
|
|
117
|
+
hintTimer = setTimeout(() => {
|
|
118
|
+
hint = undefined;
|
|
119
|
+
schedulePaint();
|
|
120
|
+
}, EXIT_CONFIRM_MS);
|
|
121
|
+
schedulePaint();
|
|
122
|
+
}
|
|
123
|
+
// ----- keys -----
|
|
124
|
+
function submitLine(mode) {
|
|
125
|
+
let text = editor.value();
|
|
126
|
+
const commands = popupCommands();
|
|
127
|
+
if (commands.length > 0 && text !== `/${commands[popupIndex].name}`)
|
|
128
|
+
text = `/${commands[popupIndex].name}`;
|
|
129
|
+
if (text.trim() === "")
|
|
130
|
+
return;
|
|
131
|
+
const typed = text === editor.value();
|
|
132
|
+
const toSend = typed ? editor.expanded() : text;
|
|
133
|
+
if (mode === "queue" && !controller.driver.working)
|
|
134
|
+
mode = "steer";
|
|
135
|
+
editor.remember(text);
|
|
136
|
+
editor.clear();
|
|
137
|
+
popupIndex = 0;
|
|
138
|
+
void controller.submit(toSend, mode).catch((error) => transcript.add({ kind: "notice", level: "error", text: error.message }));
|
|
139
|
+
}
|
|
140
|
+
function onPickerKey(key) {
|
|
141
|
+
const current = picker;
|
|
142
|
+
if (key.name === "up")
|
|
143
|
+
current.index = Math.max(0, current.index - 1);
|
|
144
|
+
else if (key.name === "down")
|
|
145
|
+
current.index = Math.min(current.items.length - 1, current.index + 1);
|
|
146
|
+
else if (key.name === "enter") {
|
|
147
|
+
picker = undefined;
|
|
148
|
+
current.resolve(current.items[current.index]);
|
|
149
|
+
}
|
|
150
|
+
else if (key.name === "escape" || (key.name === "c" && key.ctrl)) {
|
|
151
|
+
picker = undefined;
|
|
152
|
+
current.resolve(undefined);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function onKey(key) {
|
|
156
|
+
if (picker)
|
|
157
|
+
return onPickerKey(key);
|
|
158
|
+
const commands = popupCommands();
|
|
159
|
+
if (commands.length > 0 && (key.name === "up" || key.name === "down")) {
|
|
160
|
+
popupIndex = (popupIndex + (key.name === "down" ? 1 : commands.length - 1)) % commands.length;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
// a dropped or pasted image file becomes an @mention, which attaches it
|
|
164
|
+
const dropped = key.name === "paste" ? pastedImagePath(key.text ?? "", options.controller.cwd) : undefined;
|
|
165
|
+
const action = editor.handle(dropped ? { name: "paste", text: `@${dropped} ` } : key);
|
|
166
|
+
if (action === "shift-tab") {
|
|
167
|
+
// the command popup uses Tab; Shift+Tab never changes the mode while it is open
|
|
168
|
+
if (commands.length === 0)
|
|
169
|
+
void controller.cyclePermissions().catch((error) => showHint(error.message));
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (action !== "none")
|
|
173
|
+
popupIndex = action === "tab" ? popupIndex : 0;
|
|
174
|
+
if (action === "submit")
|
|
175
|
+
submitLine("steer");
|
|
176
|
+
else if (action === "tab") {
|
|
177
|
+
if (commands.length > 0)
|
|
178
|
+
editor.set(`/${commands[popupIndex].name} `);
|
|
179
|
+
else if (controller.driver.working && editor.value().trim() !== "")
|
|
180
|
+
submitLine("queue");
|
|
181
|
+
}
|
|
182
|
+
else if (action === "escape") {
|
|
183
|
+
if (controller.driver.working)
|
|
184
|
+
controller.cancel();
|
|
185
|
+
else if (commands.length > 0)
|
|
186
|
+
popupDismissedFor = editor.value();
|
|
187
|
+
else if (editor.value() === "") {
|
|
188
|
+
// Esc twice on an empty prompt goes back to before the last message, as in Codex
|
|
189
|
+
if (Date.now() - lastEscape < 800) {
|
|
190
|
+
lastEscape = 0;
|
|
191
|
+
void controller.submit("/undo");
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
lastEscape = Date.now();
|
|
195
|
+
showHint("Press Esc again to edit your previous message.");
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
else if (action === "interrupt") {
|
|
200
|
+
if (controller.driver.working)
|
|
201
|
+
controller.cancel();
|
|
202
|
+
else if (editor.value() !== "")
|
|
203
|
+
editor.clear();
|
|
204
|
+
else if (Date.now() - lastCtrlC < EXIT_CONFIRM_MS)
|
|
205
|
+
finish(0);
|
|
206
|
+
else {
|
|
207
|
+
lastCtrlC = Date.now();
|
|
208
|
+
showHint("Press Ctrl-C again to leave.");
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
else if (action === "eof")
|
|
212
|
+
finish(0);
|
|
213
|
+
}
|
|
214
|
+
// ----- lifecycle -----
|
|
215
|
+
function restoreTerminal() {
|
|
216
|
+
stdout.write("\u001b[?2004l\u001b[?25h");
|
|
217
|
+
stdin.setRawMode?.(false);
|
|
218
|
+
stdin.pause?.();
|
|
219
|
+
}
|
|
220
|
+
try {
|
|
221
|
+
await controller.start(options.start);
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
clearInterval(spinner);
|
|
225
|
+
stdout.write(`${error.message}\n`);
|
|
226
|
+
return 1;
|
|
227
|
+
}
|
|
228
|
+
transcript.subscribe((change) => (change.type === "commit" ? printBlock(change.block) : schedulePaint()));
|
|
229
|
+
stdin.setEncoding?.("utf8");
|
|
230
|
+
stdin.setRawMode?.(true);
|
|
231
|
+
stdin.resume?.();
|
|
232
|
+
stdout.write("\u001b[?2004h");
|
|
233
|
+
stdin.on("data", (chunk) => {
|
|
234
|
+
if (finished)
|
|
235
|
+
return;
|
|
236
|
+
idle.touch();
|
|
237
|
+
for (const key of decoder.push(chunk.toString()))
|
|
238
|
+
onKey(key);
|
|
239
|
+
schedulePaint();
|
|
240
|
+
});
|
|
241
|
+
stdout.on?.("resize", () => {
|
|
242
|
+
live.forget();
|
|
243
|
+
schedulePaint();
|
|
244
|
+
});
|
|
245
|
+
const info = controller.statusInfo();
|
|
246
|
+
const meta = controller.currentMeta();
|
|
247
|
+
live.print(renderBanner({ version: options.version, model: info.model, provider: info.provider, cwd: info.cwd, permissions: info.permissions, resumed: options.start.resume !== undefined ? meta?.title ?? meta?.id.slice(0, 8) : undefined }, { width: width(), color }));
|
|
248
|
+
if (options.start.resume !== undefined) {
|
|
249
|
+
for (const block of transcript.blocks())
|
|
250
|
+
printBlock(block);
|
|
251
|
+
}
|
|
252
|
+
paint();
|
|
253
|
+
if (options.initialPrompt)
|
|
254
|
+
void controller.submit(options.initialPrompt).catch((error) => transcript.add({ kind: "notice", level: "error", text: error.message }));
|
|
255
|
+
const code = await done;
|
|
256
|
+
finished = true;
|
|
257
|
+
clearInterval(spinner);
|
|
258
|
+
clearTimeout(paintTimer);
|
|
259
|
+
clearTimeout(hintTimer);
|
|
260
|
+
idle.stop();
|
|
261
|
+
if (controller.driver.working) {
|
|
262
|
+
controller.cancel();
|
|
263
|
+
for (let waited = 0; controller.driver.working && waited < 3000; waited += 50)
|
|
264
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
265
|
+
}
|
|
266
|
+
live.clear();
|
|
267
|
+
restoreTerminal();
|
|
268
|
+
const saved = controller.currentMeta();
|
|
269
|
+
await controller.close();
|
|
270
|
+
if (saved?.title !== undefined)
|
|
271
|
+
stdout.write(`\nSession saved. Resume it with:\n sakuracode resume ${saved.id}\n`);
|
|
272
|
+
return code;
|
|
273
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Decoding raw terminal input into key events. Understands arrows, Home/End/Delete, Ctrl and Alt combinations,
|
|
2
|
+
// Shift-Enter in the common encodings, and bracketed paste (which may arrive in several chunks).
|
|
3
|
+
const PASTE_START = "\u001b[200~";
|
|
4
|
+
const PASTE_END = "\u001b[201~";
|
|
5
|
+
const CSI_FINAL = { A: "up", B: "down", C: "right", D: "left", H: "home", F: "end", Z: "tab" };
|
|
6
|
+
const CSI_TILDE = { "1": "home", "2": "insert", "3": "delete", "4": "end", "5": "pageup", "6": "pagedown", "7": "home", "8": "end" };
|
|
7
|
+
export function createKeyDecoder() {
|
|
8
|
+
let pasteBuffer;
|
|
9
|
+
let carry = "";
|
|
10
|
+
function decode(input) {
|
|
11
|
+
const events = [];
|
|
12
|
+
let text = "";
|
|
13
|
+
const flushText = () => {
|
|
14
|
+
if (text)
|
|
15
|
+
events.push({ name: "char", text });
|
|
16
|
+
text = "";
|
|
17
|
+
};
|
|
18
|
+
let index = 0;
|
|
19
|
+
while (index < input.length) {
|
|
20
|
+
const char = input[index];
|
|
21
|
+
if (char === "\u001b") {
|
|
22
|
+
flushText();
|
|
23
|
+
const parsed = parseEscape(input, index);
|
|
24
|
+
if (parsed.incomplete) {
|
|
25
|
+
carry = input.slice(index);
|
|
26
|
+
return events;
|
|
27
|
+
}
|
|
28
|
+
events.push(...parsed.events);
|
|
29
|
+
index += parsed.length;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const code = char.charCodeAt(0);
|
|
33
|
+
if (char === "\r") {
|
|
34
|
+
flushText();
|
|
35
|
+
events.push({ name: "enter" });
|
|
36
|
+
}
|
|
37
|
+
else if (char === "\n") {
|
|
38
|
+
flushText();
|
|
39
|
+
events.push({ name: "enter", ctrl: true });
|
|
40
|
+
}
|
|
41
|
+
else if (char === "\t") {
|
|
42
|
+
flushText();
|
|
43
|
+
events.push({ name: "tab" });
|
|
44
|
+
}
|
|
45
|
+
else if (code === 0x7f || code === 0x08) {
|
|
46
|
+
flushText();
|
|
47
|
+
events.push({ name: "backspace" });
|
|
48
|
+
}
|
|
49
|
+
else if (code >= 1 && code <= 26) {
|
|
50
|
+
flushText();
|
|
51
|
+
events.push({ name: String.fromCharCode(code + 96), ctrl: true });
|
|
52
|
+
}
|
|
53
|
+
else if (code < 32) {
|
|
54
|
+
flushText();
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
text += char;
|
|
58
|
+
index += 1;
|
|
59
|
+
}
|
|
60
|
+
flushText();
|
|
61
|
+
return events;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
push(chunk) {
|
|
65
|
+
let input = carry + chunk;
|
|
66
|
+
carry = "";
|
|
67
|
+
const events = [];
|
|
68
|
+
for (;;) {
|
|
69
|
+
if (pasteBuffer !== undefined) {
|
|
70
|
+
const end = input.indexOf(PASTE_END);
|
|
71
|
+
if (end === -1) {
|
|
72
|
+
pasteBuffer += input;
|
|
73
|
+
return events;
|
|
74
|
+
}
|
|
75
|
+
events.push({ name: "paste", text: (pasteBuffer + input.slice(0, end)).replace(/\r\n?/g, "\n") });
|
|
76
|
+
pasteBuffer = undefined;
|
|
77
|
+
input = input.slice(end + PASTE_END.length);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const start = input.indexOf(PASTE_START);
|
|
81
|
+
if (start === -1) {
|
|
82
|
+
events.push(...decode(input));
|
|
83
|
+
return events;
|
|
84
|
+
}
|
|
85
|
+
events.push(...decode(input.slice(0, start)));
|
|
86
|
+
pasteBuffer = "";
|
|
87
|
+
input = input.slice(start + PASTE_START.length);
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function modifiers(code) {
|
|
93
|
+
const bits = code - 1;
|
|
94
|
+
return { ...(bits & 1 ? { shift: true } : {}), ...(bits & 2 ? { alt: true } : {}), ...(bits & 4 ? { ctrl: true } : {}) };
|
|
95
|
+
}
|
|
96
|
+
function parseEscape(input, index) {
|
|
97
|
+
const next = input[index + 1];
|
|
98
|
+
if (next === undefined)
|
|
99
|
+
return { events: [{ name: "escape" }], length: 1 };
|
|
100
|
+
if (next === "[") {
|
|
101
|
+
const match = /^\u001b\[([0-9;:?]*)([@-~])/.exec(input.slice(index));
|
|
102
|
+
if (!match)
|
|
103
|
+
return { events: [], length: 0, incomplete: true };
|
|
104
|
+
const [whole, params = "", final = ""] = match;
|
|
105
|
+
const numbers = params.split(";").map((part) => Number.parseInt(part, 10));
|
|
106
|
+
const mod = numbers[1] ? modifiers(numbers[1]) : {};
|
|
107
|
+
if (final === "~") {
|
|
108
|
+
if (numbers[0] === 27 && numbers[2] === 13)
|
|
109
|
+
return { events: [{ name: "enter", ...modifiers(numbers[1] ?? 1) }], length: whole.length };
|
|
110
|
+
const name = CSI_TILDE[String(numbers[0])];
|
|
111
|
+
return { events: name ? [{ name, ...mod }] : [], length: whole.length };
|
|
112
|
+
}
|
|
113
|
+
if (final === "u") {
|
|
114
|
+
const code = numbers[0] ?? 0;
|
|
115
|
+
const m = modifiers(numbers[1] ?? 1);
|
|
116
|
+
if (code === 13)
|
|
117
|
+
return { events: [{ name: "enter", ...m }], length: whole.length };
|
|
118
|
+
if (code === 27)
|
|
119
|
+
return { events: [{ name: "escape" }], length: whole.length };
|
|
120
|
+
if (code === 9)
|
|
121
|
+
return { events: [{ name: "tab", ...m }], length: whole.length };
|
|
122
|
+
if (code === 127)
|
|
123
|
+
return { events: [{ name: "backspace", ...m }], length: whole.length };
|
|
124
|
+
if (code >= 32 && !m.ctrl && !m.alt)
|
|
125
|
+
return { events: [{ name: "char", text: String.fromCodePoint(code) }], length: whole.length };
|
|
126
|
+
if (code >= 97 && code <= 122)
|
|
127
|
+
return { events: [{ name: String.fromCharCode(code), ...m }], length: whole.length };
|
|
128
|
+
return { events: [], length: whole.length };
|
|
129
|
+
}
|
|
130
|
+
const name = CSI_FINAL[final];
|
|
131
|
+
if (name === "tab")
|
|
132
|
+
return { events: [{ name: "tab", shift: true }], length: whole.length };
|
|
133
|
+
return { events: name ? [{ name, ...mod }] : [], length: whole.length };
|
|
134
|
+
}
|
|
135
|
+
if (next === "O") {
|
|
136
|
+
const final = input[index + 2];
|
|
137
|
+
if (final === undefined)
|
|
138
|
+
return { events: [], length: 0, incomplete: true };
|
|
139
|
+
const name = CSI_FINAL[final];
|
|
140
|
+
return { events: name ? [{ name }] : [], length: 3 };
|
|
141
|
+
}
|
|
142
|
+
if (next === "\r")
|
|
143
|
+
return { events: [{ name: "enter", alt: true }], length: 2 };
|
|
144
|
+
if (next === "\u007f")
|
|
145
|
+
return { events: [{ name: "backspace", alt: true }], length: 2 };
|
|
146
|
+
const code = next.codePointAt(0);
|
|
147
|
+
const length = 1 + String.fromCodePoint(code).length;
|
|
148
|
+
if (code >= 1 && code <= 26)
|
|
149
|
+
return { events: [{ name: String.fromCharCode(code + 96), ctrl: true, alt: true }], length };
|
|
150
|
+
return { events: [{ name: "char", text: String.fromCodePoint(code), alt: true }], length };
|
|
151
|
+
}
|