@deepseek-harness-tui/dsh-tui 0.18.0 → 0.19.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 +35 -10
- package/bin/dsh-tui.js +46 -3
- package/guide/dsh-tui-guide/codex-backend.en.md +2 -2
- package/guide/dsh-tui-guide/codex-backend.md +2 -2
- package/guide/dsh-tui-guide/configuration.en.md +7 -2
- package/guide/dsh-tui-guide/configuration.md +7 -2
- package/guide/dsh-tui-guide/getting-started.en.md +168 -4
- package/guide/dsh-tui-guide/getting-started.md +121 -14
- package/guide/dsh-tui-guide/interaction.en.md +11 -10
- package/guide/dsh-tui-guide/interaction.md +5 -4
- package/guide/dsh-tui-guide/user-guide.en.md +6 -5
- package/guide/dsh-tui-guide/user-guide.md +6 -5
- package/lib/settings.json +1 -1
- package/lib/types/adapter/ports/channel-ui.d.ts +10 -7
- package/lib/types/adapter/ports/channel-ui.d.ts.map +1 -1
- package/lib/types/adapter/ports/channel-view.d.ts +28 -5
- package/lib/types/adapter/ports/channel-view.d.ts.map +1 -1
- package/lib/types/agent/events.d.ts +12 -5
- package/lib/types/agent/events.d.ts.map +1 -1
- package/lib/types/agent/session.d.ts +5 -0
- package/lib/types/agent/session.d.ts.map +1 -1
- package/lib/types/backends/claude/translate/core.d.ts +2 -2
- package/lib/types/backends/claude/translate/core.d.ts.map +1 -1
- package/lib/types/backends/claude/translate/core.js +10 -6
- package/lib/types/backends/codex/session/commands.d.ts +2 -0
- package/lib/types/backends/codex/session/commands.d.ts.map +1 -1
- package/lib/types/backends/codex/session/commands.js +8 -4
- package/lib/types/backends/codex/session/input.d.ts +11 -7
- package/lib/types/backends/codex/session/input.d.ts.map +1 -1
- package/lib/types/backends/codex/session/input.js +68 -15
- package/lib/types/backends/codex/session/session.d.ts.map +1 -1
- package/lib/types/backends/codex/session/session.js +2 -3
- package/lib/types/channel/projection.d.ts.map +1 -1
- package/lib/types/channel/projection.js +78 -10
- package/lib/types/channel/questions.d.ts +19 -3
- package/lib/types/channel/questions.d.ts.map +1 -1
- package/lib/types/channel/sanitize.d.ts.map +1 -1
- package/lib/types/channel/sanitize.js +8 -4
- package/lib/types/channel/subagent-failure.d.ts +49 -0
- package/lib/types/channel/subagent-failure.d.ts.map +1 -0
- package/lib/types/channel/subagent-failure.js +59 -0
- package/lib/types/channel/transcript.d.ts +17 -0
- package/lib/types/channel/transcript.d.ts.map +1 -1
- package/lib/types/channel/transcript.js +35 -0
- package/lib/types/commands.js +1 -1
- package/lib/types/components/MessageList.d.ts.map +1 -1
- package/lib/types/components/MessageList.js +11 -7
- package/lib/types/components/PromptInput.d.ts.map +1 -1
- package/lib/types/components/PromptInput.js +61 -6
- package/lib/types/components/SubagentDashboard.d.ts.map +1 -1
- package/lib/types/components/SubagentDashboard.js +7 -1
- package/lib/types/components/messages/AssistantToolUseMessage.d.ts.map +1 -1
- package/lib/types/components/messages/AssistantToolUseMessage.js +5 -2
- package/lib/types/components/questions/AskUserQuestionPanel.d.ts +5 -8
- package/lib/types/components/questions/AskUserQuestionPanel.d.ts.map +1 -1
- package/lib/types/components/questions/AskUserQuestionPanel.js +7 -4
- package/lib/types/components/questions/PlanReviewPanel.d.ts +3 -6
- package/lib/types/components/questions/PlanReviewPanel.d.ts.map +1 -1
- package/lib/types/components/revealScope.d.ts +15 -0
- package/lib/types/components/revealScope.d.ts.map +1 -0
- package/lib/types/components/revealScope.js +17 -0
- package/lib/types/components/sidePanel/btw/BtwComposer.d.ts +34 -1
- package/lib/types/components/sidePanel/btw/BtwComposer.d.ts.map +1 -1
- package/lib/types/components/sidePanel/btw/BtwComposer.js +108 -7
- package/lib/types/components/sidePanel/btw/BtwPanelAdapter.d.ts.map +1 -1
- package/lib/types/components/sidePanel/btw/BtwPanelAdapter.js +1 -1
- package/lib/types/components/sidePanel/btw/BtwThreadScene.d.ts.map +1 -1
- package/lib/types/components/sidePanel/btw/BtwThreadScene.js +1 -1
- package/lib/types/components/smoothReveal.d.ts +36 -6
- package/lib/types/components/smoothReveal.d.ts.map +1 -1
- package/lib/types/components/smoothReveal.js +92 -21
- package/lib/types/components/workspaces/HomeWorkspaceRow.d.ts +4 -14
- package/lib/types/components/workspaces/HomeWorkspaceRow.d.ts.map +1 -1
- package/lib/types/components/workspaces/HomeWorkspaceRow.js +26 -17
- package/lib/types/dsh-adapter/backend/session.d.ts.map +1 -1
- package/lib/types/dsh-adapter/backend/session.js +12 -0
- package/lib/types/dsh-adapter/backend/translate.d.ts.map +1 -1
- package/lib/types/dsh-adapter/backend/translate.js +6 -1
- package/lib/types/dsh-adapter/channel/attached-context.d.ts +15 -2
- package/lib/types/dsh-adapter/channel/attached-context.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/attached-context.js +26 -3
- package/lib/types/dsh-adapter/channel/binding-events.d.ts +3 -0
- package/lib/types/dsh-adapter/channel/binding-events.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/binding-events.js +43 -2
- package/lib/types/dsh-adapter/channel/context-bookkeeping.d.ts +2 -6
- package/lib/types/dsh-adapter/channel/context-bookkeeping.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/context-bookkeeping.js +7 -1
- package/lib/types/dsh-adapter/channel/core/compose.d.ts +2 -6
- package/lib/types/dsh-adapter/channel/core/compose.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/core/compose.js +1 -1
- package/lib/types/dsh-adapter/channel/core/local-actions.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/core/local-actions.js +3 -26
- package/lib/types/dsh-adapter/channel/emitter.js +1 -1
- package/lib/types/dsh-adapter/channel/extensions.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/extensions.js +8 -0
- package/lib/types/dsh-adapter/channel/input-actions.d.ts +2 -2
- package/lib/types/dsh-adapter/channel/input-actions.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/input-actions.js +34 -10
- package/lib/types/dsh-adapter/channel/input-delivery.d.ts +3 -7
- package/lib/types/dsh-adapter/channel/input-delivery.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/input-delivery.js +14 -5
- package/lib/types/dsh-adapter/channel/model-actions.d.ts +4 -1
- package/lib/types/dsh-adapter/channel/model-actions.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/model-actions.js +26 -9
- package/lib/types/dsh-adapter/channel/model-selection.d.ts +27 -2
- package/lib/types/dsh-adapter/channel/model-selection.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/model-selection.js +81 -37
- package/lib/types/dsh-adapter/channel/model-switch.d.ts +1 -1
- package/lib/types/dsh-adapter/channel/model-switch.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/session-rewind.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/session-rewind.js +6 -2
- package/lib/types/dsh-adapter/channel/session-tree-actions.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/session-tree-actions.js +8 -4
- package/lib/types/dsh-adapter/channel/settings-host.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/settings-host.js +6 -3
- package/lib/types/dsh-adapter/channel/transcript.d.ts +3 -2
- package/lib/types/dsh-adapter/channel/transcript.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/transcript.js +13 -4
- package/lib/types/dsh-adapter/channel/types.d.ts +11 -2
- package/lib/types/dsh-adapter/channel/types.d.ts.map +1 -1
- package/lib/types/dsh-adapter/contract.d.ts +26 -3
- package/lib/types/dsh-adapter/contract.d.ts.map +1 -1
- package/lib/types/dsh-adapter/contract.js +44 -5
- package/lib/types/dsh-adapter/fresh-agent.d.ts +1 -1
- package/lib/types/dsh-adapter/fresh-agent.d.ts.map +1 -1
- package/lib/types/dsh-adapter/fresh-agent.js +60 -11
- package/lib/types/dsh-adapter/host-access.d.ts.map +1 -1
- package/lib/types/dsh-adapter/host-access.js +60 -11
- package/lib/types/dsh-adapter/host-contract.d.ts +1 -1
- package/lib/types/dsh-adapter/host-contract.d.ts.map +1 -1
- package/lib/types/dsh-adapter/host-contract.js +4 -2
- package/lib/types/dsh-adapter/host-dsh.d.ts +0 -8
- package/lib/types/dsh-adapter/host-dsh.d.ts.map +1 -1
- package/lib/types/dsh-adapter/host-dsh.js +27 -6
- package/lib/types/dsh-adapter/host-entry.js +4 -0
- package/lib/types/dsh-adapter/oauth/profiles.d.ts.map +1 -1
- package/lib/types/dsh-adapter/oauth/profiles.js +14 -1
- package/lib/types/dsh-adapter/plugin.d.ts.map +1 -1
- package/lib/types/dsh-adapter/plugin.js +143 -44
- package/lib/types/dsh-adapter/presets.d.ts +38 -11
- package/lib/types/dsh-adapter/presets.d.ts.map +1 -1
- package/lib/types/dsh-adapter/presets.js +46 -17
- package/lib/types/dsh-adapter/process-exit.d.ts +1 -1
- package/lib/types/dsh-adapter/process-exit.js +1 -1
- package/lib/types/dsh-adapter/questions.d.ts +3 -4
- package/lib/types/dsh-adapter/questions.d.ts.map +1 -1
- package/lib/types/dsh-adapter/questions.js +1 -1
- package/lib/types/dsh-adapter/session-list-metadata.js +2 -2
- package/lib/types/dsh-adapter/subagents.d.ts +5 -0
- package/lib/types/dsh-adapter/subagents.d.ts.map +1 -1
- package/lib/types/dsh-adapter/subagents.js +22 -1
- package/lib/types/dsh-adapter/team-mode.d.ts +17 -10
- package/lib/types/dsh-adapter/team-mode.d.ts.map +1 -1
- package/lib/types/dsh-adapter/team-mode.js +19 -12
- package/lib/types/dsh-adapter/tool-scope.d.ts +24 -0
- package/lib/types/dsh-adapter/tool-scope.d.ts.map +1 -0
- package/lib/types/dsh-adapter/tool-scope.js +127 -0
- package/lib/types/dsh-adapter/types.d.ts +2 -1
- package/lib/types/dsh-adapter/types.d.ts.map +1 -1
- package/lib/types/heldInputs.d.ts +55 -16
- package/lib/types/heldInputs.d.ts.map +1 -1
- package/lib/types/heldInputs.js +277 -41
- package/lib/types/i18n.d.ts +227 -11
- package/lib/types/i18n.d.ts.map +1 -1
- package/lib/types/i18n.js +60 -5
- package/lib/types/ink/components/App.d.ts +2 -0
- package/lib/types/ink/components/App.d.ts.map +1 -1
- package/lib/types/ink/components/App.js +8 -0
- package/lib/types/ink/events/input-event.d.ts.map +1 -1
- package/lib/types/ink/events/input-event.js +7 -6
- package/lib/types/ink/hit-test.d.ts +2 -0
- package/lib/types/ink/hit-test.d.ts.map +1 -1
- package/lib/types/ink/hit-test.js +10 -0
- package/lib/types/ink/hooks/use-program-status.d.ts +1 -1
- package/lib/types/ink/hooks/use-program-status.d.ts.map +1 -1
- package/lib/types/ink/hooks/use-program-status.js +3 -2
- package/lib/types/ink/ink.d.ts +1 -0
- package/lib/types/ink/ink.d.ts.map +1 -1
- package/lib/types/ink/ink.js +5 -2
- package/lib/types/ink/parse-keypress.d.ts.map +1 -1
- package/lib/types/ink/parse-keypress.js +25 -0
- package/lib/types/ink/terminal.d.ts +13 -2
- package/lib/types/ink/terminal.d.ts.map +1 -1
- package/lib/types/ink/terminal.js +29 -5
- package/lib/types/screens/Chat.d.ts.map +1 -1
- package/lib/types/screens/Chat.js +43 -2
- package/lib/types/screens/SessionSupervisor.d.ts +3 -6
- package/lib/types/screens/SessionSupervisor.d.ts.map +1 -1
- package/lib/types/screens/SessionSupervisor.js +5 -8
- package/lib/types/screens/sessionSupervisor/ForeignSessionPanes.d.ts +1 -2
- package/lib/types/screens/sessionSupervisor/ForeignSessionPanes.d.ts.map +1 -1
- package/lib/types/screens/sessionSupervisor/ForeignSessionPanes.js +2 -2
- package/lib/types/screens/sessionSupervisor/model.d.ts +1 -1
- package/lib/types/screens/sessionSupervisor/model.js +1 -1
- package/lib/types/sessionMounts.d.ts +12 -5
- package/lib/types/sessionMounts.d.ts.map +1 -1
- package/lib/types/sessionMounts.js +33 -6
- package/lib/types/update.d.ts +412 -20
- package/lib/types/update.d.ts.map +1 -1
- package/lib/types/update.js +2094 -193
- package/lib/types/updateHost.d.ts +198 -0
- package/lib/types/updateHost.d.ts.map +1 -0
- package/lib/types/updateHost.js +608 -0
- package/lib/types/utils/processName.d.ts +16 -0
- package/lib/types/utils/processName.d.ts.map +1 -0
- package/lib/types/utils/processName.js +27 -0
- package/package.json +111 -108
- package/tui-profile/feed/README.md +1 -1
- package/tui-profile/feed/first-party.json +1 -1
package/lib/types/heldInputs.js
CHANGED
|
@@ -4,26 +4,82 @@
|
|
|
4
4
|
* path rides the replacement's env, and the replacement parks them as held
|
|
5
5
|
* dock rows. The backend copies die with the old process (the kernel records
|
|
6
6
|
* them as canceled), so nothing re-sends them until the user sends the dock.
|
|
7
|
-
*
|
|
7
|
+
* Staged images live in the old process's memory: their bytes ride the file
|
|
8
|
+
* and the replacement stages them again.
|
|
8
9
|
*/
|
|
9
10
|
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
10
11
|
import { randomBytes } from 'node:crypto';
|
|
11
12
|
import { basename, dirname, join, resolve } from 'node:path';
|
|
13
|
+
import { sniffImageMediaType } from './utils/imageResize.js';
|
|
12
14
|
import { DATA_DIR } from './utils/paths.js';
|
|
13
|
-
/** env marker naming the file the replacement takes its held
|
|
15
|
+
/** env marker naming the file the replacement takes its held inputs from. */
|
|
14
16
|
export const HELD_INPUTS_ENV = 'DSH_TUI_HELD_INPUTS';
|
|
15
17
|
const HELD_INPUTS_FILE = /^held-inputs-\d+-[0-9a-f]{12}\.json$/u;
|
|
16
18
|
/** A restart that stalled leaves its file for the printed resume command; one
|
|
17
19
|
* nobody resumed within this long is swept at a later launch. */
|
|
18
20
|
const HELD_INPUTS_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
21
|
+
/** How long one image read may take before the restart goes on without it. */
|
|
22
|
+
const IMAGE_READ_MS = 2000;
|
|
19
23
|
/**
|
|
20
|
-
*
|
|
24
|
+
* The old process's side: the pending rows with their staged images' bytes.
|
|
25
|
+
* Never rejects; an image that cannot be read in time stays behind (its token
|
|
26
|
+
* then sends as text, which the composer warns about).
|
|
27
|
+
*/
|
|
28
|
+
export async function collectHeldInputs(pending, stagedImage) {
|
|
29
|
+
const readImage = async (ref) => {
|
|
30
|
+
const image = stagedImage(ref.stageId);
|
|
31
|
+
if (image === undefined)
|
|
32
|
+
return undefined;
|
|
33
|
+
// A ref'd timer, unlike AbortSignal.timeout: while the exit waits on this
|
|
34
|
+
// read the process must stay up until the replacement is spawned.
|
|
35
|
+
const abort = new AbortController();
|
|
36
|
+
let timer;
|
|
37
|
+
try {
|
|
38
|
+
const data = await Promise.race([
|
|
39
|
+
image.read(abort.signal),
|
|
40
|
+
new Promise((_, reject) => {
|
|
41
|
+
timer = setTimeout(() => {
|
|
42
|
+
abort.abort();
|
|
43
|
+
reject(new Error('image read timed out'));
|
|
44
|
+
}, IMAGE_READ_MS);
|
|
45
|
+
}),
|
|
46
|
+
]);
|
|
47
|
+
const mediaType = sniffImageMediaType(data);
|
|
48
|
+
if (mediaType === undefined)
|
|
49
|
+
return undefined;
|
|
50
|
+
return { token: ref.token, data, mediaType, ...(image.name === undefined ? {} : { name: image.name }) };
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
return Promise.all(pending.map(async (item) => {
|
|
60
|
+
const images = (await Promise.all(item.images.map(readImage))).filter((image) => image !== undefined);
|
|
61
|
+
const contexts = (item.contexts ?? []).map((context) => ({
|
|
62
|
+
source: context.source,
|
|
63
|
+
sourceId: context.sourceId,
|
|
64
|
+
title: context.title,
|
|
65
|
+
content: context.content,
|
|
66
|
+
truncated: context.truncated,
|
|
67
|
+
}));
|
|
68
|
+
return {
|
|
69
|
+
text: item.text,
|
|
70
|
+
...(images.length === 0 ? {} : { images }),
|
|
71
|
+
...(contexts.length === 0 ? {} : { contexts }),
|
|
72
|
+
};
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Write the inputs for a replacement process to take.
|
|
21
77
|
* @returns The env marker to hand the replacement, or undefined when there
|
|
22
78
|
* is nothing to carry or the write failed (best effort: a restart never
|
|
23
79
|
* waits on it).
|
|
24
80
|
*/
|
|
25
|
-
export function writeHeldInputs(
|
|
26
|
-
const kept =
|
|
81
|
+
export function writeHeldInputs(inputs, dir = DATA_DIR, pid = process.pid) {
|
|
82
|
+
const kept = inputs.map(input => typeof input === 'string' ? { text: input } : input).filter(input => input.text.trim() !== '');
|
|
27
83
|
if (kept.length === 0)
|
|
28
84
|
return undefined;
|
|
29
85
|
// Unique per restart: a stalled restart's file stays for its printed
|
|
@@ -31,26 +87,68 @@ export function writeHeldInputs(texts, dir = DATA_DIR, pid = process.pid) {
|
|
|
31
87
|
const file = join(dir, `held-inputs-${pid}-${randomBytes(6).toString('hex')}.json`);
|
|
32
88
|
try {
|
|
33
89
|
mkdirSync(dir, { recursive: true });
|
|
34
|
-
|
|
35
|
-
|
|
90
|
+
const records = kept.map(input => ({
|
|
91
|
+
text: input.text,
|
|
92
|
+
...(input.images === undefined || input.images.length === 0 ? {} : {
|
|
93
|
+
images: input.images.map(image => ({ ...image, data: Buffer.from(image.data).toString('base64') })),
|
|
94
|
+
}),
|
|
95
|
+
...(input.contexts === undefined || input.contexts.length === 0 ? {} : { contexts: input.contexts.map(context => ({ ...context })) }),
|
|
96
|
+
}));
|
|
97
|
+
// The user's own prompt text and images: readable by them alone.
|
|
98
|
+
// Both shapes in one file: a replacement that is an older build reads
|
|
99
|
+
// `texts` (it knows nothing of images), this build prefers `inputs`.
|
|
100
|
+
writeFileSync(file, JSON.stringify({ inputs: records, texts: kept.map(input => input.text) }), { mode: 0o600, flag: 'wx' });
|
|
36
101
|
return { [HELD_INPUTS_ENV]: file };
|
|
37
102
|
}
|
|
38
103
|
catch {
|
|
39
|
-
|
|
104
|
+
// Images are the luxury here: a plain-text file still hands the words
|
|
105
|
+
// over, and the resume command it prints stays honest about the rest.
|
|
106
|
+
try {
|
|
107
|
+
rmSync(file, { force: true });
|
|
108
|
+
writeFileSync(file, JSON.stringify({ texts: kept.map(input => input.text) }), { mode: 0o600, flag: 'wx' });
|
|
109
|
+
return { [HELD_INPUTS_ENV]: file };
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
40
114
|
}
|
|
41
115
|
}
|
|
42
116
|
/**
|
|
43
117
|
* The one handoff this process has not settled yet. The DSH launcher's boot
|
|
44
118
|
* recompose applies the entry a second time, after the first pass consumed
|
|
45
|
-
* the env marker; that pass must still find the
|
|
119
|
+
* the env marker; that pass must still find the inputs. A pass claims it
|
|
120
|
+
* before holding: staging images is async, and a stale listener must not park
|
|
121
|
+
* the same inputs again while the first pass is still staging.
|
|
46
122
|
*/
|
|
47
123
|
let waiting;
|
|
124
|
+
/**
|
|
125
|
+
* Passes that asked for the handoff while another one was staging its images.
|
|
126
|
+
* Staging is async, so the holder can stop (a replaced screen, a failed
|
|
127
|
+
* boot pass) before it parks anything: it releases the claim, and without a
|
|
128
|
+
* wake-up here the ready sibling that already lost the race would never look
|
|
129
|
+
* again — its inputs would stay on disk with nobody holding them.
|
|
130
|
+
*/
|
|
131
|
+
const claimWaiters = new Set();
|
|
48
132
|
/** Read the marker once per handoff; without a fresh one, hand back the
|
|
49
133
|
* pending handoff (the recompose asking again). */
|
|
50
134
|
function carriedInputs(env, dir) {
|
|
51
135
|
if (env[HELD_INPUTS_ENV] !== undefined)
|
|
52
136
|
waiting = readHeldInputs(env, dir);
|
|
53
|
-
return waiting ?? {
|
|
137
|
+
return waiting ?? { inputs: [] };
|
|
138
|
+
}
|
|
139
|
+
/** The write side of a carried image, bounded like the read side: a channel
|
|
140
|
+
* that never answers must not keep the handoff claim forever. */
|
|
141
|
+
function stageWithin(work) {
|
|
142
|
+
let timer;
|
|
143
|
+
const timeout = new Promise((_, reject) => {
|
|
144
|
+
timer = setTimeout(() => reject(new Error('image staging timed out')), IMAGE_READ_MS);
|
|
145
|
+
// A guard for an exit path musn't keep the process up by itself.
|
|
146
|
+
timer.unref?.();
|
|
147
|
+
});
|
|
148
|
+
return Promise.race([work, timeout]).finally(() => {
|
|
149
|
+
if (timer !== undefined)
|
|
150
|
+
clearTimeout(timer);
|
|
151
|
+
});
|
|
54
152
|
}
|
|
55
153
|
/** Best effort: a file another process still holds open must not decide
|
|
56
154
|
* whether this one starts. */
|
|
@@ -62,49 +160,140 @@ function removeHeldFile(file) {
|
|
|
62
160
|
}
|
|
63
161
|
}
|
|
64
162
|
/**
|
|
65
|
-
* The replacement's side: park the
|
|
66
|
-
* held rows once the screen's channel is ready (never sent by themselves)
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
163
|
+
* The replacement's side: park the inputs a `/restart` handed this process as
|
|
164
|
+
* held rows once the screen's channel is ready (never sent by themselves),
|
|
165
|
+
* their images staged again first. An image the new session refuses is left
|
|
166
|
+
* out; its token stays in the text. A pass that cannot reach a ready channel
|
|
167
|
+
* leaves the handoff pending for the next `apply()` instead of dropping it.
|
|
168
|
+
* @returns What stops the adoption, or undefined when nothing was carried.
|
|
70
169
|
*/
|
|
71
170
|
export function adoptHeldInputs(channel, env = process.env, dir = DATA_DIR) {
|
|
72
|
-
const
|
|
73
|
-
const {
|
|
74
|
-
// The file goes only once its
|
|
75
|
-
// dies before
|
|
76
|
-
// parent prints. Then files nobody resumed are swept.
|
|
171
|
+
const handoff = carriedInputs(env, dir);
|
|
172
|
+
const { inputs, file } = handoff;
|
|
173
|
+
// The file goes only once its inputs are on screen: a replacement that
|
|
174
|
+
// dies (or stops) before then, staging images included, leaves it for the
|
|
175
|
+
// resume command its parent prints. Then files nobody resumed are swept.
|
|
77
176
|
const settle = () => {
|
|
78
177
|
if (file !== undefined)
|
|
79
178
|
removeHeldFile(file);
|
|
80
179
|
waiting = undefined;
|
|
180
|
+
claimWaiters.clear();
|
|
81
181
|
sweepHeldInputs(dir);
|
|
82
182
|
};
|
|
83
|
-
if (
|
|
183
|
+
if (inputs.length === 0) {
|
|
84
184
|
settle();
|
|
85
185
|
return undefined;
|
|
86
186
|
}
|
|
87
|
-
|
|
88
|
-
|
|
187
|
+
let stopped = false;
|
|
188
|
+
/** Hand the claim back and wake the pass that lost it. The file is still on
|
|
189
|
+
* disk: only a pass that actually parks the inputs may settle the handoff. */
|
|
190
|
+
const release = () => {
|
|
191
|
+
if (waiting !== handoff)
|
|
192
|
+
return;
|
|
193
|
+
handoff.claimed = false;
|
|
194
|
+
for (const wake of [...claimWaiters]) {
|
|
195
|
+
claimWaiters.delete(wake);
|
|
196
|
+
wake();
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
const hold = async () => {
|
|
200
|
+
let generation;
|
|
201
|
+
try {
|
|
202
|
+
generation = channel.stagedImageGeneration();
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
// The screen is gone before staging even started: nobody here can park
|
|
206
|
+
// the rows. Keep the file and let the next pass (or the resume command)
|
|
207
|
+
// take the handoff instead of leaving the claim held.
|
|
208
|
+
release();
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const submissions = await Promise.all(inputs.map(async (input) => {
|
|
212
|
+
const contexts = (input.contexts ?? []).map((context) => ({
|
|
213
|
+
id: `ctx-held-${randomBytes(4).toString('hex')}`,
|
|
214
|
+
source: context.source,
|
|
215
|
+
sourceId: context.sourceId,
|
|
216
|
+
title: context.title,
|
|
217
|
+
content: context.content,
|
|
218
|
+
chars: context.content.length,
|
|
219
|
+
truncated: context.truncated,
|
|
220
|
+
}));
|
|
221
|
+
const withContexts = contexts.length === 0 ? {} : { contexts };
|
|
222
|
+
if (input.images === undefined)
|
|
223
|
+
return { text: input.text, ...withContexts };
|
|
224
|
+
const refs = await Promise.all(input.images.map(async (image) => {
|
|
225
|
+
try {
|
|
226
|
+
const { stageId } = await stageWithin(channel.stageComposerImage({ data: image.data, mediaType: image.mediaType, ...(image.name === undefined ? {} : { name: image.name }) }, generation));
|
|
227
|
+
return { token: image.token, stageId };
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
}));
|
|
233
|
+
const images = refs.filter((ref) => ref !== undefined);
|
|
234
|
+
return { text: input.text, ...(images.length === 0 ? {} : { images }), ...withContexts };
|
|
235
|
+
}));
|
|
236
|
+
if (stopped) {
|
|
237
|
+
// Nothing was parked: hand the claim back so a later apply (the entry's
|
|
238
|
+
// next boot pass) can still take these inputs. The file is still there.
|
|
239
|
+
release();
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
channel.holdInputs(submissions);
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
// The screen refused the rows (disposed mid-staging): keep the file for
|
|
247
|
+
// the resume command and let a later pass take the handoff.
|
|
248
|
+
release();
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
89
251
|
settle();
|
|
90
|
-
|
|
252
|
+
};
|
|
253
|
+
// Whoever holds first owns the handoff, and owns it from before the async
|
|
254
|
+
// staging starts: a stale listener whose channel turns ready mid-staging
|
|
255
|
+
// must not park the same inputs a second time.
|
|
256
|
+
const claim = () => {
|
|
257
|
+
if (handoff.claimed === true || waiting !== handoff)
|
|
258
|
+
return false;
|
|
259
|
+
handoff.claimed = true;
|
|
260
|
+
return true;
|
|
261
|
+
};
|
|
262
|
+
const take = () => {
|
|
263
|
+
if (stopped || waiting !== handoff)
|
|
264
|
+
return;
|
|
265
|
+
if (!claim()) {
|
|
266
|
+
// A sibling pass owns the handoff and is staging images; it may stop
|
|
267
|
+
// before it parks anything, so wait to be woken instead of returning.
|
|
268
|
+
claimWaiters.add(take);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
claimWaiters.delete(take);
|
|
272
|
+
// A throw outside the per-image catch must not leave the claim held.
|
|
273
|
+
void hold().catch(() => { release(); });
|
|
274
|
+
};
|
|
275
|
+
const stop = () => {
|
|
276
|
+
stopped = true;
|
|
277
|
+
claimWaiters.delete(take);
|
|
278
|
+
};
|
|
279
|
+
if (channel.ready) {
|
|
280
|
+
take();
|
|
281
|
+
return stop;
|
|
91
282
|
}
|
|
92
283
|
const unsubscribe = channel.subscribe(() => {
|
|
93
284
|
if (!channel.ready)
|
|
94
285
|
return;
|
|
95
286
|
unsubscribe();
|
|
96
|
-
|
|
97
|
-
// same texts must never be parked twice.
|
|
98
|
-
if (waiting !== inputs)
|
|
99
|
-
return;
|
|
100
|
-
channel.holdInputs(texts);
|
|
101
|
-
settle();
|
|
287
|
+
take();
|
|
102
288
|
});
|
|
103
|
-
return
|
|
289
|
+
return () => {
|
|
290
|
+
stop();
|
|
291
|
+
unsubscribe();
|
|
292
|
+
};
|
|
104
293
|
}
|
|
105
294
|
/**
|
|
106
295
|
* The resume command after a restart that ended before any replacement took
|
|
107
|
-
* the held
|
|
296
|
+
* the held inputs (a stall, a failed spawn): their file stays, and the command
|
|
108
297
|
* hands it to the next launch. Windows has no shared env-assignment syntax:
|
|
109
298
|
* PowerShell and cmd.exe each get their own copy-pasteable line.
|
|
110
299
|
*/
|
|
@@ -120,19 +309,42 @@ export function heldInputsResumeCommand(command, env, platform = process.platfor
|
|
|
120
309
|
return `${HELD_INPUTS_ENV}='${file.replaceAll('\'', '\'\\\'\'')}' ${command}`;
|
|
121
310
|
}
|
|
122
311
|
/**
|
|
123
|
-
* Take (read and delete) the
|
|
312
|
+
* Take (read and delete) the inputs a `/restart` handed this process. One
|
|
124
313
|
* shot: the env marker is consumed too, so no child inherits it.
|
|
125
314
|
*/
|
|
126
315
|
export function takeHeldInputs(env = process.env, dir = DATA_DIR) {
|
|
127
316
|
const marked = env[HELD_INPUTS_ENV] !== undefined;
|
|
128
|
-
const {
|
|
317
|
+
const { inputs, file } = readHeldInputs(env, dir);
|
|
129
318
|
if (file !== undefined)
|
|
130
319
|
removeHeldFile(file);
|
|
131
|
-
if (marked)
|
|
320
|
+
if (marked) {
|
|
132
321
|
waiting = undefined;
|
|
133
|
-
|
|
322
|
+
claimWaiters.clear();
|
|
323
|
+
}
|
|
324
|
+
return inputs;
|
|
134
325
|
}
|
|
135
|
-
|
|
326
|
+
const heldContextOf = (value) => {
|
|
327
|
+
if (value === null || typeof value !== 'object')
|
|
328
|
+
return undefined;
|
|
329
|
+
const { source, sourceId, title, content, truncated } = value;
|
|
330
|
+
if (source !== 'panel' || typeof sourceId !== 'string' || typeof title !== 'string' || typeof content !== 'string')
|
|
331
|
+
return undefined;
|
|
332
|
+
return { source, sourceId, title, content, truncated: truncated === true };
|
|
333
|
+
};
|
|
334
|
+
const heldImageOf = (value) => {
|
|
335
|
+
if (value === null || typeof value !== 'object')
|
|
336
|
+
return undefined;
|
|
337
|
+
const { token, data, name } = value;
|
|
338
|
+
if (typeof token !== 'string' || typeof data !== 'string')
|
|
339
|
+
return undefined;
|
|
340
|
+
const bytes = new Uint8Array(Buffer.from(data, 'base64'));
|
|
341
|
+
// The bytes decide the type, not the file's word for it.
|
|
342
|
+
const mediaType = sniffImageMediaType(bytes);
|
|
343
|
+
if (mediaType === undefined)
|
|
344
|
+
return undefined;
|
|
345
|
+
return { token, data: bytes, mediaType, ...(typeof name === 'string' ? { name } : {}) };
|
|
346
|
+
};
|
|
347
|
+
/** Read the inputs and consume the env marker; `file` is what the caller
|
|
136
348
|
* deletes once it is done with them. */
|
|
137
349
|
function readHeldInputs(env, dir) {
|
|
138
350
|
const file = env[HELD_INPUTS_ENV];
|
|
@@ -140,14 +352,38 @@ function readHeldInputs(env, dir) {
|
|
|
140
352
|
// Only ever a file writeHeldInputs named, in the directory it writes to:
|
|
141
353
|
// the marker is env input, and the caller deletes what it names.
|
|
142
354
|
if (file === undefined || !HELD_INPUTS_FILE.test(basename(file)) || dirname(resolve(file)) !== resolve(dir))
|
|
143
|
-
return {
|
|
355
|
+
return { inputs: [] };
|
|
144
356
|
try {
|
|
145
357
|
const parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
146
|
-
const
|
|
147
|
-
|
|
358
|
+
const record = parsed !== null && typeof parsed === 'object' ? parsed : undefined;
|
|
359
|
+
// A file an older build wrote holds `{texts: [...]}` (and no images). The
|
|
360
|
+
// resume command that build printed still names it: read it instead of
|
|
361
|
+
// deleting the queued texts as an empty handoff.
|
|
362
|
+
const inputs = record?.inputs ?? record?.texts;
|
|
363
|
+
if (!Array.isArray(inputs))
|
|
364
|
+
return { inputs: [], file };
|
|
365
|
+
return {
|
|
366
|
+
inputs: inputs.flatMap((input) => {
|
|
367
|
+
if (typeof input === 'string')
|
|
368
|
+
return input.trim() === '' ? [] : [{ text: input }];
|
|
369
|
+
if (input === null || typeof input !== 'object')
|
|
370
|
+
return [];
|
|
371
|
+
const { text, images, contexts } = input;
|
|
372
|
+
if (typeof text !== 'string' || text.trim() === '')
|
|
373
|
+
return [];
|
|
374
|
+
const held = Array.isArray(images) ? images.map(heldImageOf).filter((image) => image !== undefined) : [];
|
|
375
|
+
const carried = Array.isArray(contexts) ? contexts.map(heldContextOf).filter((context) => context !== undefined) : [];
|
|
376
|
+
return [{
|
|
377
|
+
text,
|
|
378
|
+
...(held.length === 0 ? {} : { images: held }),
|
|
379
|
+
...(carried.length === 0 ? {} : { contexts: carried }),
|
|
380
|
+
}];
|
|
381
|
+
}),
|
|
382
|
+
file,
|
|
383
|
+
};
|
|
148
384
|
}
|
|
149
385
|
catch {
|
|
150
|
-
return {
|
|
386
|
+
return { inputs: [], file };
|
|
151
387
|
}
|
|
152
388
|
}
|
|
153
389
|
/** Remove held-input files older than the resume window (best effort). */
|