synabun 2.4.2 → 2.4.3
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/CHANGELOG.md +20 -0
- package/README.md +1 -1
- package/docs/browser-extension-bridge.md +23 -3
- package/lib/release-npm-ci.mjs +23 -1
- package/mcp-server/dist/services/neural-interface.js +1 -1
- package/mcp-server/dist/services/neural-interface.js.map +1 -1
- package/mcp-server/dist/tools/browser-extension-setup.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-extension-setup.js +8 -2
- package/mcp-server/dist/tools/browser-extension-setup.js.map +1 -1
- package/mcp-server/package-lock.json +5 -5
- package/mcp-server/package.json +1 -1
- package/mcp-server/src/services/neural-interface.ts +1 -1
- package/mcp-server/src/tools/browser-extension-setup.ts +8 -2
- package/neural-interface/i18n/en.json +50 -1
- package/neural-interface/i18n/pt-BR.json +57 -8
- package/neural-interface/lib/assistant-api.js +33 -0
- package/neural-interface/lib/assistant-brains/opencode.js +121 -7
- package/neural-interface/lib/assistant-dispatch.js +26 -2
- package/neural-interface/lib/assistant-envelope.js +40 -0
- package/neural-interface/lib/assistant-runtime.js +95 -10
- package/neural-interface/lib/browser-extension-adapter.js +1 -3
- package/neural-interface/lib/browser-extension-archive.js +149 -0
- package/neural-interface/lib/browser-extension-bridge.js +7 -4
- package/neural-interface/lib/browser-extension-folder.js +596 -76
- package/neural-interface/lib/browser-extension-package.js +200 -0
- package/neural-interface/lib/browser-extension-pin.js +84 -0
- package/neural-interface/lib/browser-extension-pin.json +33 -0
- package/neural-interface/lib/browser-extension-setup.js +105 -18
- package/neural-interface/lib/cli-install.js +23 -3
- package/neural-interface/lib/cli-update-planner.js +67 -4
- package/neural-interface/lib/native-loop-providers.js +10 -1
- package/neural-interface/lib/native-loop-runtime.js +20 -2
- package/neural-interface/lib/opencode-shell-read.js +107 -0
- package/neural-interface/lib/opencode-stop-failures.js +40 -0
- package/neural-interface/lib/opencode-turn-release.js +173 -0
- package/neural-interface/lib/opencode-v2-client.js +4 -1
- package/neural-interface/lib/opencode-v2-ws-requests.js +34 -0
- package/neural-interface/lib/opencode2-client.js +341 -6
- package/neural-interface/lib/opencode2-translate.js +161 -4
- package/neural-interface/lib/terminal-token-redaction.js +83 -0
- package/neural-interface/lib/whatsapp/bridge.js +52 -6
- package/neural-interface/package-lock.json +5 -5
- package/neural-interface/package.json +1 -1
- package/neural-interface/public/extension-setup.html +5 -2
- package/neural-interface/public/shared/agy/agy-panel.js +38 -10
- package/neural-interface/public/shared/assistant/asst-api.js +25 -0
- package/neural-interface/public/shared/assistant/asst-panel.js +69 -1
- package/neural-interface/public/shared/assistant/asst-rack-styles.js +23 -0
- package/neural-interface/public/shared/assistant/asst-render.js +138 -6
- package/neural-interface/public/shared/assistant/asst-shell-logic.js +200 -0
- package/neural-interface/public/shared/assistant/asst-shell.js +111 -0
- package/neural-interface/public/shared/assistant/asst-styles.js +1 -0
- package/neural-interface/public/shared/cdx/cdx-tabs.js +25 -0
- package/neural-interface/public/shared/extension-setup/es-page.js +10 -4
- package/neural-interface/public/shared/extension-setup/es-view.js +25 -10
- package/neural-interface/public/shared/mtc/mtc-panel.js +17 -2
- package/neural-interface/public/shared/ocp-v2/ocp-v2-events.js +5 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-manager.js +1 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +34 -2
- package/neural-interface/public/shared/ocp-v2/ocp-v2-rehydrate.js +7 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-render.js +26 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-send.js +39 -6
- package/neural-interface/public/shared/ocp-v2/ocp-v2-session-actions.js +20 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-shell-block.js +165 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-shell-logic.js +346 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-shell.js +108 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-state.js +49 -3
- package/neural-interface/public/shared/ocp-v2/ocp-v2-styles.js +40 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-tools-logic.js +20 -5
- package/neural-interface/public/shared/ocp-v2/ocp-v2-tools.js +7 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-ws.js +7 -2
- package/neural-interface/public/shared/settings/settings-browser-extension.js +54 -12
- package/neural-interface/public/shared/shell-stop-view.js +83 -0
- package/neural-interface/public/shared/tour/tour-actor.js +14 -3
- package/neural-interface/public/shared/tray-auto-close.js +36 -6
- package/neural-interface/public/shared/ui-claude-panel.js +20 -0
- package/neural-interface/public/shared/ui-native-loop-router.js +45 -3
- package/neural-interface/public/shared/ui-scroll-follow.js +4 -0
- package/neural-interface/server.js +249 -35
- package/npm-shrinkwrap.json +5 -5
- package/package.json +1 -2
- package/browser-extension/content.js +0 -231
- package/browser-extension/engine.js +0 -658
- package/browser-extension/icons/icon-128.png +0 -0
- package/browser-extension/icons/icon-16.png +0 -0
- package/browser-extension/icons/icon-32.png +0 -0
- package/browser-extension/icons/icon-48.png +0 -0
- package/browser-extension/icons/status-off-16.png +0 -0
- package/browser-extension/icons/status-off-32.png +0 -0
- package/browser-extension/icons/status-off-48.png +0 -0
- package/browser-extension/icons/status-on-16.png +0 -0
- package/browser-extension/icons/status-on-32.png +0 -0
- package/browser-extension/icons/status-on-48.png +0 -0
- package/browser-extension/manifest.json +0 -58
- package/browser-extension/popup-strings.js +0 -250
- package/browser-extension/popup-view.js +0 -151
- package/browser-extension/popup.css +0 -166
- package/browser-extension/popup.html +0 -108
- package/browser-extension/popup.js +0 -359
- package/browser-extension/service-worker.js +0 -283
- package/browser-extension/setup-content.js +0 -36
- package/browser-extension/tokens.css +0 -326
- /package/{browser-extension/protocol.js → neural-interface/lib/browser-extension-protocol.js} +0 -0
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
import { hydrateTranscript } from './ocp-v2-rehydrate.js';
|
|
38
38
|
import { captureBinding, trackBinding, isBindingToken, searchOnBinding } from './ocp-v2-binding.js';
|
|
39
39
|
import { takeGreetingId } from './ocp-v2-greeting.js';
|
|
40
|
+
import { t } from '../i18n.js';
|
|
40
41
|
|
|
41
42
|
const STOR_MODE = 'opencode-v2-mode'; // the last agent picked (build / plan / a custom one)
|
|
42
43
|
const STOR_HISTORY = 'ocp-v2-prompt-history';
|
|
@@ -1564,11 +1565,16 @@ export function mountCompose(rootEl, store = getDefaultStore(), opts = {}) {
|
|
|
1564
1565
|
const unsubInner = store.subscribe((event, state) => {
|
|
1565
1566
|
if ((event?.type === 'running:set' && !state.running) || !turn.isCurrent()) cleanup();
|
|
1566
1567
|
});
|
|
1567
|
-
// Working time only: the clock stops while the session waits on the user (a permission, a question)
|
|
1568
|
+
// Working time only: the clock stops while the session waits on the user (a permission, a question) or on
|
|
1569
|
+
// a command of its own still running in the background (a long test run: `sessionStatus.background`).
|
|
1568
1570
|
clock = createTurnClock({
|
|
1569
1571
|
ms: TURN_LIMIT_MS,
|
|
1570
|
-
paused: () => sessionWaitsOnUser(turn.sessionId),
|
|
1571
|
-
subscribe:
|
|
1572
|
+
paused: () => sessionWaitsOnUser(turn.sessionId) || (turn.isCurrent() && store.getState().sessionStatus?.background === true),
|
|
1573
|
+
subscribe: (changed) => {
|
|
1574
|
+
const offAsks = onAsksChanged(changed);
|
|
1575
|
+
const offStatus = store.subscribe((event) => { if (event?.type === 'status:set') changed(); });
|
|
1576
|
+
return () => { try { offAsks?.(); } catch { /* gone */ } offStatus(); };
|
|
1577
|
+
},
|
|
1572
1578
|
onExpire: () => {
|
|
1573
1579
|
if (run.isCurrent()) store.pushError({ message: 'OpenCode turn timed out waiting for session.idle.' });
|
|
1574
1580
|
endTurn(store, run);
|
|
@@ -1580,7 +1586,7 @@ export function mountCompose(rootEl, store = getDefaultStore(), opts = {}) {
|
|
|
1580
1586
|
});
|
|
1581
1587
|
}
|
|
1582
1588
|
|
|
1583
|
-
async function doAbort() {
|
|
1589
|
+
async function doAbort({ force = false } = {}) {
|
|
1584
1590
|
// `turn` is the session Stop was pressed in, and the turn that was running
|
|
1585
1591
|
// there. The loop-stop request and the abort are for that session; what is
|
|
1586
1592
|
// cleared afterwards is cleared only while the panel is still on that
|
|
@@ -1600,15 +1606,42 @@ export function mountCompose(rootEl, store = getDefaultStore(), opts = {}) {
|
|
|
1600
1606
|
}
|
|
1601
1607
|
_suppressAbortErrorUntil = Date.now() + 15000;
|
|
1602
1608
|
_suppressAbortErrorCount += 1;
|
|
1609
|
+
let stopped = null;
|
|
1603
1610
|
try {
|
|
1604
|
-
await api.abort(turn.sessionId);
|
|
1611
|
+
stopped = await api.abort(turn.sessionId, force ? { force: true } : undefined);
|
|
1605
1612
|
} catch (err) {
|
|
1606
1613
|
console.warn('[ocp-v2-send] abort failed', err);
|
|
1607
1614
|
} finally {
|
|
1608
|
-
|
|
1615
|
+
// The banner of an earlier failed Stop of this session goes: this answer says what holds now.
|
|
1616
|
+
if (turn.isCurrent()) dismissStopNotices();
|
|
1617
|
+
// Its words are loaded only when a Stop has something to say (not before the app is shown).
|
|
1618
|
+
const view = (stopped?.code === 'SHELL_STOP_FAILED' || stopped?.abandoned?.length) ? await import('../shell-stop-view.js').catch(() => null) : null;
|
|
1619
|
+
const failure = view?.describeStopFailure(stopped, t) || null;
|
|
1620
|
+
if (stopped?.code === 'SHELL_STOP_FAILED') {
|
|
1621
|
+
// A command Stop could not end still runs: the turn stays on (its Stop button too), and the panel says which
|
|
1622
|
+
// and why. No "aborted" send failure follows (the prompt's wait goes on), so none is held back for it.
|
|
1623
|
+
_suppressAbortErrorCount = Math.max(0, _suppressAbortErrorCount - 1);
|
|
1624
|
+
if (turn.isCurrent()) {
|
|
1625
|
+
store.pushError({
|
|
1626
|
+
message: failure?.message || stopped.error || 'Stop could not end a command still running in the background. Press Stop again.',
|
|
1627
|
+
kind: STOP_NOTICE,
|
|
1628
|
+
// From the second failure on: end the turn here, telling plainly what may still run.
|
|
1629
|
+
...(failure?.canForce ? { actions: [{ label: view.shellStopText('anyway', {}, t), run: () => { doAbort({ force: true }).catch(() => {}); } }] } : {}),
|
|
1630
|
+
});
|
|
1631
|
+
}
|
|
1632
|
+
} else {
|
|
1633
|
+
clearStoppedTurn();
|
|
1634
|
+
const left = view?.describeAbandoned(stopped?.abandoned, t) || null;
|
|
1635
|
+
if (left && turn.isCurrent()) store.pushError({ message: left, kind: STOP_NOTICE, notice: true });
|
|
1636
|
+
}
|
|
1609
1637
|
}
|
|
1610
1638
|
}
|
|
1611
1639
|
|
|
1640
|
+
const STOP_NOTICE = 'shell-stop';
|
|
1641
|
+
function dismissStopNotices() {
|
|
1642
|
+
for (const err of store.getState().errors || []) if (err?.kind === STOP_NOTICE && err.id) store.dismissError(err.id);
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1612
1645
|
function shouldSuppressAbortError(err) {
|
|
1613
1646
|
if (_suppressAbortErrorCount <= 0) return false;
|
|
1614
1647
|
if (Date.now() > _suppressAbortErrorUntil) return false;
|
|
@@ -252,11 +252,30 @@ export async function loadSelectedSession(store, api, sessionId, info, apply) {
|
|
|
252
252
|
*/
|
|
253
253
|
export async function stopForNavigation(store, api, nav) {
|
|
254
254
|
if (nav.sessionId && nav.isCurrent() && store.getState().running) {
|
|
255
|
-
|
|
255
|
+
// The panel leaves this session: a command Stop cannot end is given up on, as on a tab's close (Stop anyway).
|
|
256
|
+
let stopped = null;
|
|
257
|
+
try { stopped = await api.abort(nav.sessionId, { force: true }); } catch (err) { console.warn('[ocp-v2-session-actions] abort before navigation failed', err); }
|
|
258
|
+
// What it gave up on may still be running: the panel's next view says so (a notice outlives the clear).
|
|
259
|
+
await noteAbandoned(store, stopped);
|
|
256
260
|
}
|
|
257
261
|
return nav.isCurrent();
|
|
258
262
|
}
|
|
259
263
|
|
|
264
|
+
/**
|
|
265
|
+
* A forced Stop's answer that left commands running (`abandoned`): one notice in the panel, in the words of the
|
|
266
|
+
* panel's own Stop anyway (shell-stop-view.js, loaded only then). Nothing for any other answer.
|
|
267
|
+
*/
|
|
268
|
+
export async function noteAbandoned(store, stopped, translate = null) {
|
|
269
|
+
if (!Array.isArray(stopped?.abandoned) || !stopped.abandoned.length) return false;
|
|
270
|
+
const view = await import('../shell-stop-view.js').catch(() => null);
|
|
271
|
+
let tr = translate;
|
|
272
|
+
if (!tr) { try { tr = (await import('../i18n.js')).t; } catch { tr = null; } }
|
|
273
|
+
const message = view?.describeAbandoned(stopped.abandoned, tr) || null;
|
|
274
|
+
if (!message) return false;
|
|
275
|
+
store.pushError({ message, kind: 'shell-stop', notice: true });
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
|
|
260
279
|
/**
|
|
261
280
|
* Create the session of a navigation. Resolves `{ session, activate }`:
|
|
262
281
|
* `activate` is false when the panel was rebound while the session was being
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
2
|
+
// A terminal block: what a shell command printed, drawn as it is read
|
|
3
|
+
//
|
|
4
|
+
// The block of the OpenCode side panel's bash card (ocp-v2-shell.js) and of the
|
|
5
|
+
// Assistant panel's shell calls (assistant/asst-shell.js). No socket, no store
|
|
6
|
+
// of its own: the caller says how to read the command's state, how to hear of a
|
|
7
|
+
// change and how to ask for the whole output, and gives the class names and the
|
|
8
|
+
// words of its own panel. The block updates itself in place (appended text,
|
|
9
|
+
// never a rebuilt card), so a long test run costs one text append per page and
|
|
10
|
+
// the reader's place in it is kept. Everything a command printed goes in as
|
|
11
|
+
// text, never as markup.
|
|
12
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
13
|
+
|
|
14
|
+
const NEAR_BOTTOM_PX = 24;
|
|
15
|
+
|
|
16
|
+
function el(tag, className, text) {
|
|
17
|
+
const node = document.createElement(tag);
|
|
18
|
+
if (className) node.className = className;
|
|
19
|
+
if (text != null) node.textContent = text;
|
|
20
|
+
return node;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* createTerminalBlock({ shellId, read, subscribe, loadAll, place, classes, words, showEvent })
|
|
25
|
+
* read() → { view, shell }: the output as far as it was read (ocp-v2-shell-logic.js
|
|
26
|
+
* createShellFollower's view) and the command ({ status, … }); either may be null
|
|
27
|
+
* subscribe(draw) → unsubscribe; `draw` is called when that command or its output changed
|
|
28
|
+
* loadAll() ask for the whole output, from its first byte
|
|
29
|
+
* place { follow, top }: where the reader left the block (kept by the caller, so a block built
|
|
30
|
+
* again for the same command opens at the same place)
|
|
31
|
+
* classes { wrap, note, noteText, load, out, foot, state, jump, copy }
|
|
32
|
+
* words { load, loading, jump, copy, copied, label, noOutputYet, noOutput, reading, running,
|
|
33
|
+
* note(view) → the line above the output when it does not show everything ('' when it does) }
|
|
34
|
+
* showEvent the event the caller dispatches on the block when it gets a size (a card opened by hand)
|
|
35
|
+
* It follows the output while the reader is at its end, and leaves the reader alone once they scrolled up.
|
|
36
|
+
*/
|
|
37
|
+
export function createTerminalBlock({ shellId, read, subscribe, loadAll, place = { follow: true, top: 0 }, classes, words, showEvent = 'shell-term-show' }) {
|
|
38
|
+
const wrap = el('div', classes.wrap);
|
|
39
|
+
wrap.dataset.shellId = shellId;
|
|
40
|
+
|
|
41
|
+
const note = el('div', classes.note);
|
|
42
|
+
const noteText = el('span', classes.noteText);
|
|
43
|
+
const loadButton = el('button', classes.load, words.load);
|
|
44
|
+
loadButton.type = 'button';
|
|
45
|
+
note.append(noteText, loadButton);
|
|
46
|
+
note.hidden = true;
|
|
47
|
+
|
|
48
|
+
const out = el('pre', classes.out);
|
|
49
|
+
out.tabIndex = 0;
|
|
50
|
+
out.setAttribute('role', 'log');
|
|
51
|
+
out.setAttribute('aria-label', words.label);
|
|
52
|
+
// A screen reader is not read every line of a test run; the card's status says when it ends.
|
|
53
|
+
out.setAttribute('aria-live', 'off');
|
|
54
|
+
const textNode = document.createTextNode('');
|
|
55
|
+
out.appendChild(textNode);
|
|
56
|
+
|
|
57
|
+
const foot = el('div', classes.foot);
|
|
58
|
+
const state = el('span', classes.state);
|
|
59
|
+
const jump = el('button', classes.jump, words.jump);
|
|
60
|
+
jump.type = 'button';
|
|
61
|
+
jump.hidden = true;
|
|
62
|
+
const copy = el('button', classes.copy, words.copy);
|
|
63
|
+
copy.type = 'button';
|
|
64
|
+
foot.append(state, jump, copy);
|
|
65
|
+
wrap.append(note, out, foot);
|
|
66
|
+
|
|
67
|
+
let shownFrom = -1;
|
|
68
|
+
let shownLength = 0;
|
|
69
|
+
let drawn = false;
|
|
70
|
+
let toTop = false; // the whole output was asked for: its first page opens at the top
|
|
71
|
+
|
|
72
|
+
const atEnd = () => out.scrollHeight - out.scrollTop - out.clientHeight < NEAR_BOTTOM_PX;
|
|
73
|
+
out.addEventListener('scroll', () => {
|
|
74
|
+
place.follow = atEnd();
|
|
75
|
+
place.top = out.scrollTop;
|
|
76
|
+
jump.hidden = place.follow;
|
|
77
|
+
});
|
|
78
|
+
jump.addEventListener('click', (event) => {
|
|
79
|
+
event.stopPropagation();
|
|
80
|
+
place.follow = true;
|
|
81
|
+
out.scrollTop = out.scrollHeight;
|
|
82
|
+
jump.hidden = true;
|
|
83
|
+
});
|
|
84
|
+
loadButton.addEventListener('click', (event) => {
|
|
85
|
+
event.stopPropagation();
|
|
86
|
+
loadButton.disabled = true;
|
|
87
|
+
loadButton.textContent = words.loading;
|
|
88
|
+
// The whole output is read from its start: the reader starts at its top.
|
|
89
|
+
place.follow = false;
|
|
90
|
+
place.top = 0;
|
|
91
|
+
toTop = true;
|
|
92
|
+
loadAll();
|
|
93
|
+
});
|
|
94
|
+
copy.addEventListener('click', async (event) => {
|
|
95
|
+
event.stopPropagation();
|
|
96
|
+
try {
|
|
97
|
+
await navigator.clipboard.writeText(textNode.data);
|
|
98
|
+
copy.textContent = words.copied;
|
|
99
|
+
setTimeout(() => { if (copy.isConnected) copy.textContent = words.copy; }, 1200);
|
|
100
|
+
} catch { /* clipboard unavailable */ }
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
function draw() {
|
|
104
|
+
const current = read() || {};
|
|
105
|
+
const view = current.view || null;
|
|
106
|
+
const running = current.shell?.status === 'running';
|
|
107
|
+
const text = view?.text || '';
|
|
108
|
+
const from = view ? view.from : 0;
|
|
109
|
+
// Appended when the kept text only grew; set anew when its start moved (a long output's window, the full output).
|
|
110
|
+
// A carriage return rewrites the line it is on and never one before it, so of what is shown only its last line
|
|
111
|
+
// can have changed: compared, and replaced when it did (review S6: a progress line of the same width or longer
|
|
112
|
+
// kept showing, and copying, its first state).
|
|
113
|
+
if (drawn && from === shownFrom && textNode.data.length === shownLength) {
|
|
114
|
+
const stable = textNode.data.lastIndexOf('\n') + 1;
|
|
115
|
+
if (text.length >= shownLength && text.slice(stable, shownLength) === textNode.data.slice(stable)) {
|
|
116
|
+
if (text.length > shownLength) textNode.appendData(text.slice(shownLength));
|
|
117
|
+
} else if (text.slice(0, stable) === textNode.data.slice(0, stable)) {
|
|
118
|
+
textNode.replaceData(stable, shownLength - stable, text.slice(stable));
|
|
119
|
+
} else {
|
|
120
|
+
textNode.data = text;
|
|
121
|
+
}
|
|
122
|
+
} else if (!drawn || from !== shownFrom || text.length !== shownLength) {
|
|
123
|
+
textNode.data = text;
|
|
124
|
+
if (toTop && view?.full) { toTop = false; out.scrollTop = 0; }
|
|
125
|
+
}
|
|
126
|
+
shownFrom = from;
|
|
127
|
+
shownLength = text.length;
|
|
128
|
+
|
|
129
|
+
const noteWords = words.note(view);
|
|
130
|
+
note.hidden = !noteWords;
|
|
131
|
+
noteText.textContent = noteWords;
|
|
132
|
+
const canLoad = !!view && view.partial && !view.full && !view.gone;
|
|
133
|
+
loadButton.hidden = !canLoad;
|
|
134
|
+
if (canLoad) { loadButton.disabled = false; loadButton.textContent = words.load; }
|
|
135
|
+
|
|
136
|
+
wrap.dataset.running = running ? 'true' : 'false';
|
|
137
|
+
state.textContent = view?.error && !view.done ? view.error
|
|
138
|
+
: !text && running ? words.noOutputYet
|
|
139
|
+
: !text && view && (view.done || view.gone) ? words.noOutput
|
|
140
|
+
: !text ? words.reading
|
|
141
|
+
: running ? words.running : '';
|
|
142
|
+
copy.hidden = !text;
|
|
143
|
+
out.hidden = !text;
|
|
144
|
+
|
|
145
|
+
if (!drawn) {
|
|
146
|
+
drawn = true;
|
|
147
|
+
out.scrollTop = place.follow ? out.scrollHeight : place.top;
|
|
148
|
+
} else if (place.follow) {
|
|
149
|
+
out.scrollTop = out.scrollHeight;
|
|
150
|
+
}
|
|
151
|
+
jump.hidden = place.follow || !text;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const unsubscribe = subscribe(() => {
|
|
155
|
+
if (!wrap.isConnected) { unsubscribe(); return; }
|
|
156
|
+
draw();
|
|
157
|
+
});
|
|
158
|
+
draw();
|
|
159
|
+
// The block is measured once it is in the page (a card built open, or opened later).
|
|
160
|
+
const settle = () => { if (wrap.isConnected) out.scrollTop = place.follow ? out.scrollHeight : place.top; };
|
|
161
|
+
globalThis.requestAnimationFrame?.(settle);
|
|
162
|
+
// A card opened by hand shows its block for the first time: it has a height only now.
|
|
163
|
+
wrap.addEventListener(showEvent, settle);
|
|
164
|
+
return wrap;
|
|
165
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
2
|
+
// OpenCode V2 — a shell command's output, live (no DOM, no socket)
|
|
3
|
+
//
|
|
4
|
+
// OpenCode 2 does not send what a command prints with its tool call: it keeps
|
|
5
|
+
// the combined stdout and stderr, in the order written, and answers pages of it
|
|
6
|
+
// by byte position while the command runs and after (`shell:output`). This
|
|
7
|
+
// module reads those pages and keeps what the transcript shows:
|
|
8
|
+
// createTerminalBuffer() the text as a terminal would leave it, windowed
|
|
9
|
+
// createShellFollower() reads one command to its end, then stops
|
|
10
|
+
// followShells() one follower per command of a store's session
|
|
11
|
+
// shellCardState() what a command means for its tool card
|
|
12
|
+
// The store holds the result (shellOutput), ocp-v2-shell.js draws it.
|
|
13
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
/** What the transcript keeps of one command's output until the reader asks for all of it. */
|
|
16
|
+
export const SHELL_WINDOW_CHARS = 256 * 1024;
|
|
17
|
+
/** The most "Load full output" keeps in the page; a longer output says so and names where it stops. */
|
|
18
|
+
export const SHELL_FULL_MAX_CHARS = 8 * 1024 * 1024;
|
|
19
|
+
/** How often a running command is asked for more: at once while it prints, slower while it is quiet. */
|
|
20
|
+
export const SHELL_POLL_MIN_MS = 400;
|
|
21
|
+
export const SHELL_POLL_MAX_MS = 2500;
|
|
22
|
+
|
|
23
|
+
// CSI / OSC sequences and the other escapes a test runner colours its lines with.
|
|
24
|
+
// eslint-disable-next-line no-control-regex
|
|
25
|
+
const ANSI = /\x1b\[[0-9;?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b[@-Z\\-_]/g;
|
|
26
|
+
// A sequence the page cut in two waits for its other half.
|
|
27
|
+
// eslint-disable-next-line no-control-regex
|
|
28
|
+
const ANSI_TAIL = /\x1b(?:\[[0-9;?]*[ -/]*|\][^\x07\x1b]*)?$/;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The text of a command as a terminal would leave it: escape sequences gone, a
|
|
32
|
+
* line rewritten with a carriage return (a progress bar) kept as its last
|
|
33
|
+
* form, CRLF as one line end. At most `maxChars` are kept: the oldest whole
|
|
34
|
+
* lines go first, and `dropped` counts the characters that went.
|
|
35
|
+
*/
|
|
36
|
+
export function createTerminalBuffer({ maxChars = SHELL_WINDOW_CHARS } = {}) {
|
|
37
|
+
let text = '';
|
|
38
|
+
let dropped = 0;
|
|
39
|
+
let pending = ''; // an escape sequence, or a lone "\r", still waiting for what follows
|
|
40
|
+
let limit = Math.max(1024, Number(maxChars) || SHELL_WINDOW_CHARS);
|
|
41
|
+
|
|
42
|
+
function trim() {
|
|
43
|
+
if (text.length <= limit) return;
|
|
44
|
+
let cut = text.length - limit;
|
|
45
|
+
const lineEnd = text.indexOf('\n', cut);
|
|
46
|
+
// The line that crosses the limit goes whole, unless it is the last one.
|
|
47
|
+
if (lineEnd !== -1 && lineEnd < text.length - 1) cut = lineEnd + 1;
|
|
48
|
+
dropped += cut;
|
|
49
|
+
text = text.slice(cut);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function append(chunk) {
|
|
53
|
+
let incoming = pending + String(chunk ?? '');
|
|
54
|
+
pending = '';
|
|
55
|
+
const tail = ANSI_TAIL.exec(incoming);
|
|
56
|
+
if (tail) { pending = tail[0]; incoming = incoming.slice(0, tail.index); }
|
|
57
|
+
if (incoming.endsWith('\r')) { pending = `\r${pending}`; incoming = incoming.slice(0, -1); }
|
|
58
|
+
incoming = incoming.replace(ANSI, '').replace(/\r\n/g, '\n');
|
|
59
|
+
if (!incoming) return;
|
|
60
|
+
if (incoming.includes('\r')) {
|
|
61
|
+
// A carriage return goes back to the start of the line: what follows replaces it.
|
|
62
|
+
for (const piece of incoming.split(/(\r)/)) {
|
|
63
|
+
if (piece === '\r') text = text.slice(0, text.lastIndexOf('\n') + 1);
|
|
64
|
+
else text += piece;
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
text += incoming;
|
|
68
|
+
}
|
|
69
|
+
trim();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
append,
|
|
74
|
+
text: () => text,
|
|
75
|
+
/** Characters no longer kept (the start of a long output). */
|
|
76
|
+
dropped: () => dropped,
|
|
77
|
+
setLimit(maxChars) { limit = Math.max(1024, Number(maxChars) || limit); trim(); },
|
|
78
|
+
reset() { text = ''; dropped = 0; pending = ''; },
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const replyFailed = (res) => !res || res.ok === false || (typeof res.status === 'number' && res.status >= 400);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Reads one command's output to its end.
|
|
86
|
+
* api.shellOutput({ sessionId, shellId, cwd, cursor, tail, limit }) → { data: { output, cursor, size, from } }
|
|
87
|
+
* isRunning() the command still runs (the store's word); once it does not,
|
|
88
|
+
* a read that brings nothing new is the last one
|
|
89
|
+
* isCurrent() false once the panel left the session: nothing more is read
|
|
90
|
+
* onUpdate(view) { text, from, size, done, gone, error, full, capped } after every page
|
|
91
|
+
* The first read starts `window` characters before the end (a panel that opens
|
|
92
|
+
* on a long output shows its end at once); `loadAll()` reads it again from its
|
|
93
|
+
* first byte. Nothing is polled once the command ended and was read to its end.
|
|
94
|
+
*/
|
|
95
|
+
export function createShellFollower({
|
|
96
|
+
api, sessionId, shellId, cwd, isRunning = () => false, isCurrent = () => true, onUpdate = () => {},
|
|
97
|
+
windowChars = SHELL_WINDOW_CHARS, setTimer = setTimeout, clearTimer = clearTimeout,
|
|
98
|
+
} = {}) {
|
|
99
|
+
const buffer = createTerminalBuffer({ maxChars: windowChars });
|
|
100
|
+
let cursor = null; // null: nothing read yet (the first read asks for the tail)
|
|
101
|
+
let from = 0; // the byte the kept text starts at, as far as the server said
|
|
102
|
+
let size = 0;
|
|
103
|
+
let timer = null;
|
|
104
|
+
let reading = false;
|
|
105
|
+
let stopped = false;
|
|
106
|
+
let done = false;
|
|
107
|
+
let gone = false;
|
|
108
|
+
let error = '';
|
|
109
|
+
let full = false;
|
|
110
|
+
let capped = false;
|
|
111
|
+
let delay = SHELL_POLL_MIN_MS;
|
|
112
|
+
let failures = 0;
|
|
113
|
+
let generation = 0; // a read begun before loadAll() belongs to the reading it replaced
|
|
114
|
+
|
|
115
|
+
const view = () => ({
|
|
116
|
+
text: buffer.text(), from: from + buffer.dropped(), size, done, gone, error, full, capped,
|
|
117
|
+
// Something of the output is not in `text` (its start): the reader can ask for it.
|
|
118
|
+
partial: from + buffer.dropped() > 0,
|
|
119
|
+
});
|
|
120
|
+
const publish = () => { try { onUpdate(view()); } catch (err) { console.warn('[ocp-v2-shell] update failed', err); } };
|
|
121
|
+
|
|
122
|
+
function schedule(ms) {
|
|
123
|
+
if (stopped || done || gone) return;
|
|
124
|
+
if (timer) clearTimer(timer);
|
|
125
|
+
timer = setTimer(() => { timer = null; read(); }, ms);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function read() {
|
|
129
|
+
if (stopped || done || gone || reading) return;
|
|
130
|
+
if (!isCurrent()) { stop(); return; }
|
|
131
|
+
reading = true;
|
|
132
|
+
const mine = generation;
|
|
133
|
+
const wasRunning = isRunning();
|
|
134
|
+
let res;
|
|
135
|
+
try {
|
|
136
|
+
res = await api.shellOutput(cursor == null
|
|
137
|
+
? { sessionId, shellId, cwd, tail: full ? undefined : windowChars, cursor: full ? 0 : undefined }
|
|
138
|
+
: { sessionId, shellId, cwd, cursor });
|
|
139
|
+
} catch (err) { res = { ok: false, error: err?.message || String(err) }; }
|
|
140
|
+
reading = false;
|
|
141
|
+
if (stopped) return;
|
|
142
|
+
if (mine !== generation) { schedule(0); return; }
|
|
143
|
+
if (!isCurrent()) { stop(); return; }
|
|
144
|
+
if (replyFailed(res) || !res.data || typeof res.data !== 'object') {
|
|
145
|
+
// OpenCode keeps a finished command's output for a while only.
|
|
146
|
+
if (res?.status === 404) { gone = true; publish(); return; }
|
|
147
|
+
failures += 1;
|
|
148
|
+
error = (typeof res?.error === 'string' && res.error) || 'The output could not be read.';
|
|
149
|
+
if (failures >= 5 && !isRunning()) { done = true; publish(); return; }
|
|
150
|
+
publish();
|
|
151
|
+
schedule(Math.min(SHELL_POLL_MAX_MS * 2, SHELL_POLL_MIN_MS * 2 ** failures));
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
failures = 0;
|
|
155
|
+
error = '';
|
|
156
|
+
const page = res.data;
|
|
157
|
+
const text = typeof page.output === 'string' ? page.output : '';
|
|
158
|
+
if (cursor == null) from = Math.max(0, Number(page.from) || 0);
|
|
159
|
+
const next = Math.max(0, Number(page.cursor) || 0);
|
|
160
|
+
size = Math.max(next, Number(page.size) || 0);
|
|
161
|
+
const moved = cursor == null || next > cursor;
|
|
162
|
+
if (text) buffer.append(text);
|
|
163
|
+
cursor = next;
|
|
164
|
+
if (full && buffer.dropped() > 0) capped = true;
|
|
165
|
+
const behind = cursor < size;
|
|
166
|
+
// Read to its end after the command stopped (as it was before this read): nothing more will come.
|
|
167
|
+
if (!wasRunning && !isRunning() && !behind && !text) done = true;
|
|
168
|
+
else if (!wasRunning && !isRunning() && !behind) { publish(); schedule(0); return; }
|
|
169
|
+
publish();
|
|
170
|
+
if (done) return;
|
|
171
|
+
if (behind) { delay = SHELL_POLL_MIN_MS; schedule(0); return; }
|
|
172
|
+
delay = moved && text ? SHELL_POLL_MIN_MS : Math.min(SHELL_POLL_MAX_MS, Math.round(delay * 1.5));
|
|
173
|
+
schedule(isRunning() ? delay : 0);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function stop() {
|
|
177
|
+
stopped = true;
|
|
178
|
+
if (timer) { clearTimer(timer); timer = null; }
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
start() { if (cursor == null && !reading) read(); },
|
|
183
|
+
stop,
|
|
184
|
+
/** The store says the command changed (it ended): read now instead of at the next tick. */
|
|
185
|
+
poke() { if (!done && !gone && !stopped) schedule(0); },
|
|
186
|
+
/** Read the whole output again, from its first byte, keeping up to SHELL_FULL_MAX_CHARS. */
|
|
187
|
+
loadAll() {
|
|
188
|
+
if (stopped) return;
|
|
189
|
+
generation += 1;
|
|
190
|
+
full = true;
|
|
191
|
+
capped = false;
|
|
192
|
+
done = false;
|
|
193
|
+
gone = false;
|
|
194
|
+
buffer.reset();
|
|
195
|
+
buffer.setLimit(SHELL_FULL_MAX_CHARS);
|
|
196
|
+
cursor = null;
|
|
197
|
+
from = 0;
|
|
198
|
+
schedule(0);
|
|
199
|
+
},
|
|
200
|
+
view,
|
|
201
|
+
isDone: () => done || gone,
|
|
202
|
+
isStopped: () => stopped,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Keeps one follower per command of the store's session, for as long as the
|
|
208
|
+
* store is on that session: started when a command appears (an event, or the
|
|
209
|
+
* server's list after a reload), stopped when the store moves to another
|
|
210
|
+
* session. `supported()`: the server answers `shell:output` (an older one does
|
|
211
|
+
* not, and the cards then show what the tool call itself returned).
|
|
212
|
+
* Finished commands no block shows keep their text up to `kept` (the oldest
|
|
213
|
+
* goes first, review S5: a long session kept up to 8 MB of text per command it
|
|
214
|
+
* ever ran); one that comes on screen again is read afresh (watch).
|
|
215
|
+
* Returns { loadAll(shellId), watch(shellId) → release, has(shellId), stop() }.
|
|
216
|
+
*/
|
|
217
|
+
export const SHELL_OUTPUT_KEPT_FINISHED = 12;
|
|
218
|
+
|
|
219
|
+
export function followShells(store, api, { supported = () => true, setTimer, clearTimer, kept = SHELL_OUTPUT_KEPT_FINISHED } = {}) {
|
|
220
|
+
const followers = new Map(); // shellId → follower
|
|
221
|
+
const usedAt = new Map(); // shellId → when its output was last read or shown
|
|
222
|
+
const watched = new Map(); // shellId → how many blocks show it
|
|
223
|
+
const evicted = new Set(); // finished commands whose text was let go: read again only when shown
|
|
224
|
+
let clock = 0;
|
|
225
|
+
let stopped = false;
|
|
226
|
+
let binding = typeof store.getBinding === 'function' ? store.getBinding() : 0;
|
|
227
|
+
const bindingNow = () => (typeof store.getBinding === 'function' ? store.getBinding() : 0);
|
|
228
|
+
|
|
229
|
+
function clear() {
|
|
230
|
+
for (const follower of followers.values()) follower.stop();
|
|
231
|
+
followers.clear();
|
|
232
|
+
usedAt.clear();
|
|
233
|
+
evicted.clear();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Let go of the text of finished commands nobody looks at, beyond `kept`. */
|
|
237
|
+
function trim() {
|
|
238
|
+
const idle = [...followers].filter(([id, follower]) => follower.isDone() && !watched.get(id)
|
|
239
|
+
&& store.getState().shells.get(id)?.status !== 'running');
|
|
240
|
+
if (idle.length <= kept) return;
|
|
241
|
+
idle.sort((a, b) => (usedAt.get(a[0]) || 0) - (usedAt.get(b[0]) || 0));
|
|
242
|
+
for (const [id, follower] of idle.slice(0, idle.length - kept)) {
|
|
243
|
+
follower.stop();
|
|
244
|
+
followers.delete(id);
|
|
245
|
+
usedAt.delete(id);
|
|
246
|
+
evicted.add(id);
|
|
247
|
+
store.setShellOutput(id, null);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function follow(shellId) {
|
|
252
|
+
const state = store.getState();
|
|
253
|
+
const mine = binding;
|
|
254
|
+
const follower = createShellFollower({
|
|
255
|
+
api, sessionId: state.sessionId, shellId, cwd: state.cwd || state.sessionInfo?.directory || undefined,
|
|
256
|
+
isRunning: () => store.getState().shells.get(shellId)?.status === 'running',
|
|
257
|
+
isCurrent: () => !stopped && bindingNow() === mine,
|
|
258
|
+
onUpdate: (view) => {
|
|
259
|
+
if (stopped || bindingNow() !== mine || followers.get(shellId) !== follower) return;
|
|
260
|
+
usedAt.set(shellId, ++clock);
|
|
261
|
+
store.setShellOutput(shellId, view);
|
|
262
|
+
if (view?.done || view?.gone) trim();
|
|
263
|
+
},
|
|
264
|
+
...(setTimer ? { setTimer } : {}), ...(clearTimer ? { clearTimer } : {}),
|
|
265
|
+
});
|
|
266
|
+
followers.set(shellId, follower);
|
|
267
|
+
usedAt.set(shellId, ++clock);
|
|
268
|
+
follower.start();
|
|
269
|
+
return follower;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function sync() {
|
|
273
|
+
if (stopped) return;
|
|
274
|
+
const now = bindingNow();
|
|
275
|
+
if (now !== binding) { binding = now; clear(); }
|
|
276
|
+
const state = store.getState();
|
|
277
|
+
if (!state.sessionId || !supported() || typeof api?.shellOutput !== 'function') return;
|
|
278
|
+
for (const [shellId, shell] of state.shells) {
|
|
279
|
+
const follower = followers.get(shellId);
|
|
280
|
+
if (!follower) {
|
|
281
|
+
if (!evicted.has(shellId)) follow(shellId);
|
|
282
|
+
} else if (shell.status !== 'running') {
|
|
283
|
+
follower.poke();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const unsubscribe = store.subscribe((event) => {
|
|
289
|
+
if (event?.type === 'shell:update' || event?.type === 'session:set') sync();
|
|
290
|
+
});
|
|
291
|
+
sync();
|
|
292
|
+
return {
|
|
293
|
+
loadAll(shellId) { followers.get(shellId)?.loadAll(); },
|
|
294
|
+
/** A block shows `shellId`: its text is kept (and read again if it was let go) until the release. */
|
|
295
|
+
watch(shellId) {
|
|
296
|
+
if (stopped) return () => {};
|
|
297
|
+
watched.set(shellId, (watched.get(shellId) || 0) + 1);
|
|
298
|
+
usedAt.set(shellId, ++clock);
|
|
299
|
+
if (evicted.delete(shellId) && store.getState().shells.has(shellId) && supported()) follow(shellId);
|
|
300
|
+
let released = false;
|
|
301
|
+
return () => {
|
|
302
|
+
if (released) return;
|
|
303
|
+
released = true;
|
|
304
|
+
const left = (watched.get(shellId) || 1) - 1;
|
|
305
|
+
if (left > 0) watched.set(shellId, left); else watched.delete(shellId);
|
|
306
|
+
trim();
|
|
307
|
+
};
|
|
308
|
+
},
|
|
309
|
+
has: (shellId) => followers.has(shellId),
|
|
310
|
+
stop() { stopped = true; try { unsubscribe(); } catch { /* gone */ } clear(); watched.clear(); },
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* What a command means for the card of its tool call.
|
|
316
|
+
* status 'running' | 'completed' | 'error' | '' (the call's own status stands)
|
|
317
|
+
* note a few words for the card's head ("in the background", "exit 1", "stopped")
|
|
318
|
+
*/
|
|
319
|
+
export function shellCardState(shell) {
|
|
320
|
+
if (!shell || typeof shell !== 'object') return { status: '', note: '', running: false };
|
|
321
|
+
if (shell.status === 'running') {
|
|
322
|
+
return { status: 'running', note: shell.background ? 'in the background' : '', running: true };
|
|
323
|
+
}
|
|
324
|
+
if (shell.status === 'killed') return { status: 'error', note: 'stopped', running: false };
|
|
325
|
+
if (shell.status === 'timeout') return { status: 'error', note: 'timed out', running: false };
|
|
326
|
+
const exit = typeof shell.exit === 'number' ? shell.exit : null;
|
|
327
|
+
if (exit != null && exit !== 0) return { status: shell.background ? 'error' : '', note: `exit ${exit}`, running: false };
|
|
328
|
+
return { status: shell.background ? 'completed' : '', note: '', running: false };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** "1.2 MB", "340 KB", "512 B". */
|
|
332
|
+
export function formatBytes(bytes) {
|
|
333
|
+
const n = Math.max(0, Number(bytes) || 0);
|
|
334
|
+
if (n >= 1024 * 1024) return `${(n / (1024 * 1024)).toFixed(1)} MB`;
|
|
335
|
+
if (n >= 1024) return `${Math.round(n / 1024)} KB`;
|
|
336
|
+
return `${n} B`;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** The line above a terminal block when it does not show everything: '' when it does. */
|
|
340
|
+
export function shellOutputNote(view) {
|
|
341
|
+
if (!view) return '';
|
|
342
|
+
if (view.gone) return 'OpenCode no longer keeps the output of this command.';
|
|
343
|
+
if (view.capped) return `Showing the last ${formatBytes(view.text.length)} of ${formatBytes(view.size)}: the whole output is too long to show here.`;
|
|
344
|
+
if (view.partial) return `Showing the last ${formatBytes(view.text.length)} of ${formatBytes(view.size)}.`;
|
|
345
|
+
return '';
|
|
346
|
+
}
|