@alexwkleung/reika 0.7.0 → 0.8.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 +15 -0
- package/README.zh-CN.md +15 -0
- package/dist/agent/loop.js +12 -4
- package/dist/agent/prefillcost.js +16 -0
- package/dist/agent/prompt.js +5 -2
- package/dist/cli.js +2 -2
- package/dist/context/bootstrap.js +1 -1
- package/dist/debug.js +15 -0
- package/dist/{headless.js → headless/headless.js} +182 -48
- package/dist/{headlessargs.js → headless/headlessargs.js} +12 -2
- package/dist/headless/headlessprompt.js +132 -0
- package/dist/mcp/client.js +97 -5
- package/dist/mcp/manager.js +15 -2
- package/dist/provider/client.js +13 -0
- package/dist/session.js +8 -0
- package/dist/{skills.js → skills/skills.js} +93 -13
- package/dist/store/sessions.js +39 -2
- package/dist/store/transcript.js +35 -0
- package/dist/tools/_carriers.js +119 -0
- package/dist/tools/_danger.js +304 -82
- package/dist/tools/_readonly.js +13 -12
- package/dist/ui/App.js +150 -69
- package/dist/ui/Approval.js +80 -33
- package/dist/ui/Confirm.js +1 -27
- package/dist/ui/DiffView.js +23 -12
- package/dist/ui/Question.js +152 -19
- package/dist/ui/ResumeSelect.js +3 -2
- package/dist/ui/Status.js +19 -7
- package/dist/ui/commands.js +1 -1
- package/dist/ui/dialogspecs.js +38 -0
- package/dist/ui/layout.js +118 -0
- package/package.json +1 -1
- /package/dist/{skillmatch.js → skills/skillmatch.js} +0 -0
package/dist/store/sessions.js
CHANGED
|
@@ -35,10 +35,17 @@ export function serializeSession(sides, meta) {
|
|
|
35
35
|
if (sides.chat.length === 0)
|
|
36
36
|
return body;
|
|
37
37
|
// The header's title is derived from the agent side; a session spent in chat takes the chat's.
|
|
38
|
+
// `modes` covers the agent side only, so the chat side is counted here (#875): without it a
|
|
39
|
+
// chat-only file read as an empty timeline, and a listing could not tell it held a chat.
|
|
38
40
|
const nl = body.indexOf('\n');
|
|
39
41
|
const header = JSON.parse(body.slice(0, nl));
|
|
40
42
|
const chatTitle = header.title === undefined ? deriveTitle(sides.chat) : undefined;
|
|
41
|
-
const
|
|
43
|
+
const chatTurns = countRealTurns(sides.chat);
|
|
44
|
+
const head = JSON.stringify({
|
|
45
|
+
...header,
|
|
46
|
+
...(chatTitle !== undefined ? { title: chatTitle } : {}),
|
|
47
|
+
...(chatTurns > 0 ? { chatTurns } : {}),
|
|
48
|
+
});
|
|
42
49
|
const chat = sides.chat.map(m => JSON.stringify(m)).join('\n');
|
|
43
50
|
return `${head}${body.slice(nl)}${JSON.stringify(CHAT_SEGMENT)}\n${chat}\n`;
|
|
44
51
|
}
|
|
@@ -55,7 +62,10 @@ export async function writeSession(path, sides, meta) {
|
|
|
55
62
|
// Newest first. Only the header line is read — a session with its tool payloads can run to
|
|
56
63
|
// megabytes, and the list needs a title and a date. Unreadable or foreign files are skipped: a
|
|
57
64
|
// listing is never an error.
|
|
58
|
-
|
|
65
|
+
// `root` reads a header's `mode: "chat"` as a chat (#882): every file there is a /save, and a
|
|
66
|
+
// chat-mode /save writes the chat scrollback. Never in a project dir, where an auto-save taken
|
|
67
|
+
// right after Shift+Tab into chat says `chat` while holding only the agent side (#880).
|
|
68
|
+
export async function listSessions(dir, root = false) {
|
|
59
69
|
let names;
|
|
60
70
|
try {
|
|
61
71
|
names = (await readdir(dir)).filter(n => n.endsWith('.jsonl'));
|
|
@@ -69,12 +79,15 @@ export async function listSessions(dir) {
|
|
|
69
79
|
const meta = await readHeader(path);
|
|
70
80
|
if (!meta || typeof meta.savedAt !== 'string')
|
|
71
81
|
continue;
|
|
82
|
+
const chatTurns = typeof meta.chatTurns === 'number' && meta.chatTurns > 0 ? meta.chatTurns : undefined;
|
|
72
83
|
entries.push({
|
|
73
84
|
path,
|
|
74
85
|
...(typeof meta.title === 'string' ? { title: meta.title } : {}),
|
|
75
86
|
savedAt: meta.savedAt,
|
|
76
87
|
messageCount: typeof meta.messageCount === 'number' ? meta.messageCount : 0,
|
|
77
88
|
...(typeof meta.model === 'string' ? { model: meta.model } : {}),
|
|
89
|
+
...(chatTurns !== undefined ? { chatTurns } : {}),
|
|
90
|
+
...(chatTurns !== undefined || (root && meta.mode === 'chat') ? { chat: true } : {}),
|
|
78
91
|
});
|
|
79
92
|
}
|
|
80
93
|
return entries.sort((a, b) => b.savedAt.localeCompare(a.savedAt));
|
|
@@ -103,17 +116,32 @@ export async function loadSession(path) {
|
|
|
103
116
|
const lines = (await readFile(path, 'utf8')).split('\n').filter(l => l.trim().length > 0);
|
|
104
117
|
const sides = { agent: [], chat: [] };
|
|
105
118
|
let side = sides.agent;
|
|
119
|
+
let marked = false;
|
|
106
120
|
// Line 0 is the meta header.
|
|
107
121
|
for (const line of lines.slice(1)) {
|
|
108
122
|
const rec = JSON.parse(line);
|
|
109
123
|
if ('segment' in rec) {
|
|
124
|
+
marked = true;
|
|
110
125
|
side = sides.chat;
|
|
111
126
|
continue;
|
|
112
127
|
}
|
|
113
128
|
side.push(rec);
|
|
114
129
|
}
|
|
130
|
+
// A /save made in chat mode writes the chat scrollback through the plain transcript serializer,
|
|
131
|
+
// so it carries no segment marker (#880). Its prompts are what say so, not the header's `mode`:
|
|
132
|
+
// an auto-save taken right after Shift+Tab into chat is agent-only, unmarked, with `mode: "chat"`
|
|
133
|
+
// in its header too. Reading the prompts also fixes the chat saves made before this rule.
|
|
134
|
+
if (!marked && isChatOnly(sides.agent)) {
|
|
135
|
+
sides.chat = sides.agent;
|
|
136
|
+
sides.agent = [];
|
|
137
|
+
}
|
|
115
138
|
return sides;
|
|
116
139
|
}
|
|
140
|
+
// Every prompt was sent in chat mode — the session stamps each user message with its mode.
|
|
141
|
+
function isChatOnly(messages) {
|
|
142
|
+
const prompts = messages.filter(m => m.role === 'user' && !m.meta);
|
|
143
|
+
return prompts.length > 0 && prompts.every(m => m.role === 'user' && m.mode === 'chat');
|
|
144
|
+
}
|
|
117
145
|
// The model-facing history a resumed session continues from, rebuilt from its scrollback: what
|
|
118
146
|
// the loop pushes to history, minus what only the UI sees (command echoes, notices, subagent
|
|
119
147
|
// rows, compaction notes, shell-mode output). Folds are not restored — the scrollback keeps every
|
|
@@ -133,6 +161,15 @@ export function modelHistoryFromScrollback(messages) {
|
|
|
133
161
|
}
|
|
134
162
|
});
|
|
135
163
|
}
|
|
164
|
+
// The header for a /save. In chat mode the saved scrollback is the chat side, so it carries the
|
|
165
|
+
// chat count the listing filters on (#882) — counted here rather than in buildTranscriptMeta,
|
|
166
|
+
// whose auto-save callers pass the agent side under whatever mode is current.
|
|
167
|
+
export function saveHeader(meta, msgs) {
|
|
168
|
+
if (meta.mode !== 'chat')
|
|
169
|
+
return meta;
|
|
170
|
+
const chatTurns = countRealTurns(msgs);
|
|
171
|
+
return chatTurns > 0 ? { ...meta, chatTurns } : meta;
|
|
172
|
+
}
|
|
136
173
|
// Real prompts and shell commands — what makes a scrollback worth a file, as opposed to the
|
|
137
174
|
// splash notices or a /resume that was opened and left.
|
|
138
175
|
export function countRealTurns(messages) {
|
package/dist/store/transcript.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { randomBytes } from 'node:crypto';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
|
+
import { compactThreshold } from '../agent/compaction.js';
|
|
4
5
|
import { scrubDisplay } from '../ui/scrub.js';
|
|
5
6
|
import { changeLabel, contextFill, formatDurationMs, formatShrink, kFormat } from '../ui/format.js';
|
|
6
7
|
import { foldedAssistantFields } from '../ui/reasoningfold.js';
|
|
@@ -8,6 +9,40 @@ import { foldedAssistantFields } from '../ui/reasoningfold.js';
|
|
|
8
9
|
// persistent-sessions loader (which will append message records the same way) can migrate old
|
|
9
10
|
// files instead of choking on them. Start at 1; never reuse a number.
|
|
10
11
|
export const TRANSCRIPT_VERSION = 1;
|
|
12
|
+
export function buildTranscriptMeta(opts) {
|
|
13
|
+
const { msgs, status } = opts;
|
|
14
|
+
const { totals, lastUsage: last, shrink, contextWindow: window } = status;
|
|
15
|
+
const usable = window ? Math.round(compactThreshold(window, status.minGenTokens)) : undefined;
|
|
16
|
+
// Computed exactly as the status bar computes it — same turn count, same last-call-else-estimate
|
|
17
|
+
// context — so the header and a screenshot of the footer can never disagree (#199).
|
|
18
|
+
const usage = {
|
|
19
|
+
turns: msgs.filter(m => m.role === 'assistant').length,
|
|
20
|
+
promptTokens: totals.promptTokens,
|
|
21
|
+
completionTokens: totals.completionTokens,
|
|
22
|
+
...(totals.cachedTokens != null ? { cachedTokens: totals.cachedTokens } : {}),
|
|
23
|
+
contextTokens: last?.promptTokens ?? status.estimatedContext,
|
|
24
|
+
// No call has landed yet, so the context size above is the pre-send estimate.
|
|
25
|
+
...(last?.promptTokens == null ? { contextEstimated: true } : {}),
|
|
26
|
+
...(window ? { contextWindow: window } : {}),
|
|
27
|
+
...(usable ? { contextUsable: usable } : {}),
|
|
28
|
+
...(last?.cachedTokens != null ? { lastCachedTokens: last.cachedTokens } : {}),
|
|
29
|
+
...(shrink.sheds > 0 || shrink.folds > 0
|
|
30
|
+
? { ...shrink, shrinkEvents: status.shrinkEvents }
|
|
31
|
+
: {}),
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
version: TRANSCRIPT_VERSION,
|
|
35
|
+
// Stamped here: the serializers below are pure and take no clock.
|
|
36
|
+
savedAt: new Date().toISOString(),
|
|
37
|
+
model: opts.model,
|
|
38
|
+
baseURL: opts.baseURL,
|
|
39
|
+
cwd: opts.cwd,
|
|
40
|
+
messageCount: msgs.length,
|
|
41
|
+
mode: opts.mode,
|
|
42
|
+
usage,
|
|
43
|
+
...(opts.midTurn ? { midTurn: true } : {}),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
11
46
|
export const TITLE_MAX_CHARS = 80;
|
|
12
47
|
// The first thing the human typed, as one short line: what a session list or a resume prompt
|
|
13
48
|
// would show for this file (issue #309). Derived at save time rather than stamped by the UI so a
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// The words that sit between a segment's start and the command it actually runs: privilege and
|
|
2
|
+
// environment wrappers (`sudo -u me`, `env -i`), the two carriers that only bound or reprioritize a
|
|
3
|
+
// command (`timeout 5`, `nice -n 10`), and `command`/`nohup`/`exec`/`time`.
|
|
4
|
+
//
|
|
5
|
+
// One table, because two readers ask the same question about it and answered differently (#756).
|
|
6
|
+
// `_danger.ts`'s `stripVerbPrefix` puts the verb in verb position for the `^`-anchored approval
|
|
7
|
+
// patterns, where missing one costs a fetch that runs unprompted under `safe`: `timeout 5 curl x`
|
|
8
|
+
// read as a `timeout` line and reported nothing. `_readonly.ts`'s `dropCarriers` skips the carrier
|
|
9
|
+
// for plan mode's read-only gate and the sandbox's network reader, where a carrier it does not know
|
|
10
|
+
// is a read it refuses. A spelling only one of them learns is a hole in whichever it is not
|
|
11
|
+
// (#749/#752 were that drift, one level down), so the table is the single place a carrier is
|
|
12
|
+
// described, and both walks below are its only readers.
|
|
13
|
+
//
|
|
14
|
+
// Nothing here is a shell lexer: the walkers take the words their caller already produced —
|
|
15
|
+
// `_danger.ts`'s quote-aware `SHELL_WORD_RE` walk, `_readonly.ts`'s `words()`.
|
|
16
|
+
export const CARRIERS = {
|
|
17
|
+
// `sudo -u me curl x` used to keep the option's VALUE as the lead, and `curl` read as an argument
|
|
18
|
+
// (#794). Both lists are closed on purpose: an option nobody enumerated costs a label, which is
|
|
19
|
+
// the direction `_danger.ts` errs in.
|
|
20
|
+
sudo: {
|
|
21
|
+
values: [
|
|
22
|
+
'-u',
|
|
23
|
+
'-g',
|
|
24
|
+
'-U',
|
|
25
|
+
'-C',
|
|
26
|
+
'-D',
|
|
27
|
+
'-h',
|
|
28
|
+
'-p',
|
|
29
|
+
'-r',
|
|
30
|
+
'-t',
|
|
31
|
+
'-T',
|
|
32
|
+
'-R',
|
|
33
|
+
'--user',
|
|
34
|
+
'--group',
|
|
35
|
+
'--other-user',
|
|
36
|
+
'--close-from',
|
|
37
|
+
'--chdir',
|
|
38
|
+
'--host',
|
|
39
|
+
'--prompt',
|
|
40
|
+
'--role',
|
|
41
|
+
'--type',
|
|
42
|
+
'--command-timeout',
|
|
43
|
+
],
|
|
44
|
+
operands: 0,
|
|
45
|
+
transparent: false,
|
|
46
|
+
},
|
|
47
|
+
// `env` takes its options and its `VAR=value` operands in any order, so the walk keeps it in place
|
|
48
|
+
// across an assignment. `-S`/`--split-string` is here as well: its value is already read as a
|
|
49
|
+
// program of its own by `_danger.ts`'s `nestedBodies`, and skipping the value silently would be
|
|
50
|
+
// the one direction that file does not take.
|
|
51
|
+
env: {
|
|
52
|
+
values: ['-u', '-C', '-S', '-P', '--unset', '--chdir', '--split-string', '--path'],
|
|
53
|
+
operands: 0,
|
|
54
|
+
transparent: false,
|
|
55
|
+
},
|
|
56
|
+
exec: { values: ['-a', '--argv0'], operands: 0, transparent: false },
|
|
57
|
+
time: { values: ['-o', '-f', '--output', '--format'], operands: 0, transparent: false },
|
|
58
|
+
command: { values: [], operands: 0, transparent: false },
|
|
59
|
+
nohup: { values: [], operands: 0, transparent: false },
|
|
60
|
+
// `timeout [OPTION] DURATION COMMAND`: the DURATION is a positional operand, and `-s`/`-k` take
|
|
61
|
+
// values (`timeout -s KILL 5 curl`, `timeout -k 1s 5 curl`). GNU stops reading options at the
|
|
62
|
+
// first operand (`timeout 5 -k 1s curl` runs a command named `-k` — measured), so options are only
|
|
63
|
+
// read before it. `--signal=KILL` carries its value attached and eats no word.
|
|
64
|
+
timeout: { values: ['-s', '--signal', '-k', '--kill-after'], operands: 1, transparent: true },
|
|
65
|
+
// `nice [-n N | -N] COMMAND`, plus `--adjustment=N`. The legacy `nice -10 curl` is one word that
|
|
66
|
+
// starts with `-` and takes no value, which the option arm below already reads correctly.
|
|
67
|
+
nice: { values: ['-n', '--adjustment'], operands: 0, transparent: true },
|
|
68
|
+
};
|
|
69
|
+
const VALUE_SETS = new Map(Object.entries(CARRIERS).map(([name, carrier]) => [name, new Set(carrier.values)]));
|
|
70
|
+
/** Whether `word` (already read as an option) consumes the next word as its value. */
|
|
71
|
+
export function carrierOptionValue(carrier, word) {
|
|
72
|
+
if (VALUE_SETS.get(carrier)?.has(word))
|
|
73
|
+
return true;
|
|
74
|
+
// A BUNDLED short spelling (`sudo -iu me`, `env -iu PATH …`) follows getopt: the letters are read
|
|
75
|
+
// left to right, and the FIRST one that takes a value takes the rest of the bundle as that value
|
|
76
|
+
// (`-ume` is `-u me`, `-of x` is `-o f`) unless it is the bundle's LAST letter, in which case the
|
|
77
|
+
// value is the next word (`-iu me` is `-i -u me`). Anything else eats nothing. A long spelling
|
|
78
|
+
// carrying `=` (`--user=me`) keeps its value attached and never reaches here.
|
|
79
|
+
if (!/^-[a-zA-Z]+$/.test(word))
|
|
80
|
+
return false;
|
|
81
|
+
for (let i = 1; i < word.length; i++) {
|
|
82
|
+
if (VALUE_SETS.get(carrier)?.has(`-${word[i]}`))
|
|
83
|
+
return i === word.length - 1;
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The index of the word the carrier at `start` hands to — past its own options and operands — or -1
|
|
89
|
+
* when `wordList[start]` is not a carrier (or, with `transparentOnly`, not one that leaves the
|
|
90
|
+
* command's effect alone). Callers loop, because carriers nest: `timeout 5 nice gh …` is two.
|
|
91
|
+
*/
|
|
92
|
+
export function skipCarrier(wordList, start, transparentOnly = false) {
|
|
93
|
+
const carrier = CARRIERS[wordList[start] ?? ''];
|
|
94
|
+
if (!carrier || (transparentOnly && !carrier.transparent))
|
|
95
|
+
return -1;
|
|
96
|
+
let i = start + 1;
|
|
97
|
+
let operands = carrier.operands;
|
|
98
|
+
let optionsEnded = false;
|
|
99
|
+
while (i < wordList.length) {
|
|
100
|
+
const word = wordList[i];
|
|
101
|
+
// `--` ends the carrier's options, not its operands: `timeout -- 5 cat f` still has a DURATION.
|
|
102
|
+
if (!optionsEnded && word === '--') {
|
|
103
|
+
optionsEnded = true;
|
|
104
|
+
i++;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
// A bare `-` is an operand, not an option, so it falls through to the operand count.
|
|
108
|
+
if (!optionsEnded && word.startsWith('-') && word !== '-') {
|
|
109
|
+
i += carrierOptionValue(wordList[start], word) ? 2 : 1;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (operands <= 0)
|
|
113
|
+
break;
|
|
114
|
+
operands--;
|
|
115
|
+
optionsEnded = true;
|
|
116
|
+
i++;
|
|
117
|
+
}
|
|
118
|
+
return Math.min(i, wordList.length);
|
|
119
|
+
}
|