@alexwkleung/reika 0.4.0 → 0.6.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 +3 -3
- package/README.zh-CN.md +20 -20
- package/dist/agent/loop.js +35 -3
- package/dist/agent/shellruns.js +81 -0
- package/dist/approval.js +6 -0
- package/dist/cli.js +3 -0
- package/dist/config.js +2 -1
- package/dist/debug.js +135 -16
- package/dist/headlessargs.js +1 -1
- package/dist/provider/transport.js +13 -1
- package/dist/search/_chrome.js +92 -19
- package/dist/search/cdp.js +6 -1
- package/dist/store/sessions.js +5 -4
- package/dist/tools/_danger.js +383 -38
- package/dist/tools/_heredoc.js +22 -8
- package/dist/tools/_readonly.js +132 -4
- package/dist/tools/_writetargets.js +3 -6
- package/dist/tools/bash.js +2 -1
- package/dist/ui/App.js +109 -18
- package/dist/ui/Approval.js +15 -5
- package/dist/ui/Scrollback.js +9 -3
- package/dist/ui/framedrops.js +34 -0
- package/dist/ui/harnessecho.js +24 -0
- package/dist/ui/reasoningfold.js +8 -1
- package/dist/ui/syncframe.js +7 -2
- package/package.json +1 -1
package/dist/search/_chrome.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { spawn } from 'node:child_process';
|
|
1
|
+
import { execFile, spawn } from 'node:child_process';
|
|
2
2
|
import { accessSync, constants, mkdirSync } from 'node:fs';
|
|
3
3
|
import { homedir, platform } from 'node:os';
|
|
4
4
|
import { join } from 'node:path';
|
|
@@ -28,19 +28,38 @@ export class ChromeHost {
|
|
|
28
28
|
async newTab() {
|
|
29
29
|
await this.ensure();
|
|
30
30
|
this.touch();
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
31
|
+
// Not `PUT /json/new`: that opens a foreground tab, which activates Chrome and takes focus on
|
|
32
|
+
// every search. A background target in a minimized window of its own never shows, and
|
|
33
|
+
// minimizing or showing one tab's window never moves another search's tab.
|
|
34
|
+
const browser = await openSession(await this.browserWsUrl());
|
|
35
|
+
let targetId;
|
|
36
|
+
try {
|
|
37
|
+
const res = (await browser.call('Target.createTarget', {
|
|
38
|
+
url: 'about:blank',
|
|
39
|
+
newWindow: true,
|
|
40
|
+
background: true,
|
|
41
|
+
windowState: 'minimized',
|
|
42
|
+
}));
|
|
43
|
+
if (res.error)
|
|
44
|
+
throw new Error(`could not open a tab (CDP ${res.error.message ?? 'error'})`);
|
|
45
|
+
targetId = res.result?.targetId;
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
browser.close();
|
|
49
|
+
}
|
|
50
|
+
if (!targetId)
|
|
51
|
+
throw new Error('CDP returned a tab with no target id');
|
|
52
|
+
const list = (await (await fetch(`http://127.0.0.1:${this.port}/json/list`)).json());
|
|
53
|
+
const tab = list.find(t => t.id === targetId);
|
|
54
|
+
if (!tab?.webSocketDebuggerUrl || !tab.id)
|
|
36
55
|
throw new Error('CDP returned a tab with no debugger URL');
|
|
37
56
|
const session = await openSession(tab.webSocketDebuggerUrl);
|
|
38
57
|
const port = this.port;
|
|
39
58
|
const touch = () => this.touch();
|
|
40
|
-
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
//
|
|
59
|
+
const activate = () => this.activate();
|
|
60
|
+
// The window should already be minimized from creation; this covers a Chrome that predates
|
|
61
|
+
// `windowState` on `Target.createTarget`. Only a bot check (`show`) is allowed to surface it;
|
|
62
|
+
// see CdpSearchProvider.
|
|
44
63
|
await setWindowState(session, 'minimized');
|
|
45
64
|
return {
|
|
46
65
|
async navigate(url) {
|
|
@@ -61,9 +80,11 @@ export class ChromeHost {
|
|
|
61
80
|
async show() {
|
|
62
81
|
touch();
|
|
63
82
|
await setWindowState(session, 'normal');
|
|
64
|
-
// Page.bringToFront
|
|
65
|
-
//
|
|
83
|
+
// Page.bringToFront raises the tab within its own browser, but a browser that never took
|
|
84
|
+
// focus stays behind the active app, so the instance is activated too — by pid, since
|
|
85
|
+
// activating by name would raise the user's own Chrome instead (#238).
|
|
66
86
|
await session.call('Page.bringToFront').catch(() => undefined);
|
|
87
|
+
await activate();
|
|
67
88
|
},
|
|
68
89
|
async hide() {
|
|
69
90
|
await setWindowState(session, 'minimized');
|
|
@@ -119,17 +140,17 @@ export class ChromeHost {
|
|
|
119
140
|
'--disable-sync',
|
|
120
141
|
'--password-store=basic',
|
|
121
142
|
'--mute-audio',
|
|
143
|
+
// Not `--no-startup-window`: Chrome then ignores Quit from the Dock, and it kept focus no
|
|
144
|
+
// better than a hidden launch does (measured on macOS 26, Chrome 155).
|
|
122
145
|
'about:blank',
|
|
123
146
|
];
|
|
124
147
|
debugLog(`[cdp] launching ${exe} on port ${this.port}`);
|
|
125
148
|
// Not headless, deliberately: a fresh headless profile is the shape search engines CAPTCHA.
|
|
126
|
-
//
|
|
127
|
-
//
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
stdio: 'ignore',
|
|
132
|
-
}).unref();
|
|
149
|
+
// `open -g` alone still let Chrome activate itself when its first window opened, so macOS
|
|
150
|
+
// also launches it hidden (`-j`): a real browser that never takes focus.
|
|
151
|
+
const hidden = platform() === 'darwin' && !this.binary;
|
|
152
|
+
if (hidden) {
|
|
153
|
+
spawn('/usr/bin/open', ['-g', '-j', '-na', macAppName() ?? 'Google Chrome', '--args', ...flags], { detached: true, stdio: 'ignore' }).unref();
|
|
133
154
|
}
|
|
134
155
|
else {
|
|
135
156
|
spawn(exe, flags, { detached: true, stdio: 'ignore' }).unref();
|
|
@@ -138,6 +159,10 @@ export class ChromeHost {
|
|
|
138
159
|
while (Date.now() < deadline) {
|
|
139
160
|
if (await this.alive()) {
|
|
140
161
|
debugLog(`[cdp] browser ready on port ${this.port}`);
|
|
162
|
+
// Searches open their own windows, and the startup one would surface beside the first bot
|
|
163
|
+
// check's tab. macOS only: elsewhere closing the last window quits Chrome.
|
|
164
|
+
if (hidden)
|
|
165
|
+
await this.closeStartupWindow();
|
|
141
166
|
return;
|
|
142
167
|
}
|
|
143
168
|
await sleep(LAUNCH_POLL_MS);
|
|
@@ -153,6 +178,54 @@ export class ChromeHost {
|
|
|
153
178
|
this.idleTimer = setTimeout(() => void this.shutdown(), IDLE_SHUTDOWN_MS);
|
|
154
179
|
this.idleTimer.unref?.();
|
|
155
180
|
}
|
|
181
|
+
async closeStartupWindow() {
|
|
182
|
+
try {
|
|
183
|
+
const res = await fetch(`http://127.0.0.1:${this.port}/json/list`);
|
|
184
|
+
const targets = (await res.json());
|
|
185
|
+
for (const t of targets) {
|
|
186
|
+
if (t.type === 'page' && t.url === 'about:blank' && t.id)
|
|
187
|
+
await fetch(`http://127.0.0.1:${this.port}/json/close/${t.id}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
/* cosmetic: a leftover blank window never fails a search */
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async browserWsUrl() {
|
|
195
|
+
const res = await fetch(`http://127.0.0.1:${this.port}/json/version`, {
|
|
196
|
+
signal: AbortSignal.timeout(1500),
|
|
197
|
+
});
|
|
198
|
+
const { webSocketDebuggerUrl } = (await res.json());
|
|
199
|
+
if (!webSocketDebuggerUrl)
|
|
200
|
+
throw new Error('CDP reported no browser debugger URL');
|
|
201
|
+
return webSocketDebuggerUrl;
|
|
202
|
+
}
|
|
203
|
+
// The pid comes from CDP because `open` doesn't report the one it launched. Best-effort, and a
|
|
204
|
+
// no-op off macOS: the window is restored either way, activation only saves the user a click.
|
|
205
|
+
async activate() {
|
|
206
|
+
if (platform() !== 'darwin')
|
|
207
|
+
return;
|
|
208
|
+
try {
|
|
209
|
+
const browser = await openSession(await this.browserWsUrl());
|
|
210
|
+
let pid;
|
|
211
|
+
try {
|
|
212
|
+
const res = (await browser.call('SystemInfo.getProcessInfo'));
|
|
213
|
+
pid = res.result?.processInfo?.find(p => p.type === 'browser')?.id;
|
|
214
|
+
}
|
|
215
|
+
finally {
|
|
216
|
+
browser.close();
|
|
217
|
+
}
|
|
218
|
+
if (!Number.isInteger(pid))
|
|
219
|
+
return;
|
|
220
|
+
const script = `ObjC.import('AppKit'); $.NSRunningApplication.runningApplicationWithProcessIdentifier(${pid}).activateWithOptions($.NSApplicationActivateAllWindows)`;
|
|
221
|
+
await new Promise(resolve => {
|
|
222
|
+
execFile('/usr/bin/osascript', ['-l', 'JavaScript', '-e', script], () => resolve());
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
/* best-effort */
|
|
227
|
+
}
|
|
228
|
+
}
|
|
156
229
|
async shutdown() {
|
|
157
230
|
if (this.idleTimer)
|
|
158
231
|
clearTimeout(this.idleTimer);
|
package/dist/search/cdp.js
CHANGED
|
@@ -8,6 +8,9 @@ import { SearchUnavailableError } from './types.js';
|
|
|
8
8
|
// Bing, so it isn't correlated with the engine most likely to block us next. See #235.
|
|
9
9
|
const SEARCH_URL = 'https://search.brave.com/search?q=';
|
|
10
10
|
const OWN_HOSTS = /(^|\.)(brave\.com|search\.brave\.com)$/;
|
|
11
|
+
// Brave ends some result anchors with a date line ("May 8, 2026", "3 days ago"), which the
|
|
12
|
+
// last-line rule below then reported as the result's title.
|
|
13
|
+
export const DATE_LINE = /^(?:(?:jan|feb|mar|apr|may|jun|jul|aug|sep|sept|oct|nov|dec)[a-z]*\.? \d{1,2},? \d{4}|\d+ (?:second|minute|hour|day|week|month|year)s? ago)$/i;
|
|
11
14
|
// Deliberately structure-agnostic: every visible link, filtered and deduped. No result-card
|
|
12
15
|
// selectors, so a SERP redesign can't silently empty the results the way class-name scraping does.
|
|
13
16
|
// The snippet walks up from the anchor until a container holds meaningfully more text than the
|
|
@@ -24,9 +27,11 @@ function extractionScript(maxResults) {
|
|
|
24
27
|
// A result anchor's innerText is "SiteName / breadcrumb › path / actual title" on separate lines.
|
|
25
28
|
// The last line is the title; the earlier ones are chrome that would otherwise be fed to the model
|
|
26
29
|
// as part of it. No class names involved, so a redesign degrades this to "whole text", not to junk.
|
|
30
|
+
const dateLine = ${DATE_LINE.toString()};
|
|
27
31
|
const titleOf = a => {
|
|
28
32
|
const lines = (a.innerText || '').split('\\n').map(clean).filter(Boolean);
|
|
29
|
-
|
|
33
|
+
const named = lines.filter(l => !dateLine.test(l));
|
|
34
|
+
return named.length ? named[named.length - 1] : '';
|
|
30
35
|
};
|
|
31
36
|
// The container repeats the anchor's own text before the description; removing it stops every
|
|
32
37
|
// snippet from opening with a copy of the title it sits under.
|
package/dist/store/sessions.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createHash, randomBytes } from 'node:crypto';
|
|
|
2
2
|
import { open, mkdir, readdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { homedir } from 'node:os';
|
|
4
4
|
import { basename, join } from 'node:path';
|
|
5
|
+
import { sessionStem } from '../debug.js';
|
|
5
6
|
import { deriveTitle, serializeJsonl } from './transcript.js';
|
|
6
7
|
// Session persistence and /resume (#1). An auto-save is the same canonical .jsonl /save writes
|
|
7
8
|
// (meta line, one message per line), written unredacted — it is a local resume source, and a
|
|
@@ -19,10 +20,10 @@ export function projectHistoryDir(cwd, root = ROOT_HISTORY_DIR) {
|
|
|
19
20
|
return join(root, 'projects', `${slug}-${hash}`);
|
|
20
21
|
}
|
|
21
22
|
// Fixed for the life of the session: every auto-save rewrites this one file, so a session is one
|
|
22
|
-
// entry in the /resume list rather than one per save.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return join(dir, `${
|
|
23
|
+
// entry in the /resume list rather than one per save. `stem` names it after the process's debug
|
|
24
|
+
// log (#761), so the two pair.
|
|
25
|
+
export function newSessionPath(dir, startedAt, stem = sessionStem(startedAt)) {
|
|
26
|
+
return join(dir, `${stem}.jsonl`);
|
|
26
27
|
}
|
|
27
28
|
// Chat mode keeps its own history (see the mode table in AGENTS.md), so a file carries both sides:
|
|
28
29
|
// the agent side's messages, then this marker, then the chat side's. Absent when chat was never
|