@firstpick/pi-package-webui 0.8.4 → 0.8.6
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 +24 -2
- package/bin/pi-webui.mjs +383 -31
- package/index.ts +8 -1
- package/lib/git-workflow-preferences.mjs +3 -0
- package/lib/session-summary-core.mjs +4 -3
- package/lib/trust-boundaries.mjs +13 -0
- package/lib/workspace-session-summaries.mjs +400 -0
- package/package.json +2 -2
- package/public/app.js +1242 -267
- package/public/index.html +77 -4
- package/public/service-worker.js +3 -1
- package/public/stream-output-controller.mjs +327 -0
- package/public/styles.css +249 -70
- package/public/theme-contract.mjs +244 -0
- package/session-summary.ts +164 -4
- package/tests/aur-review-widget-static.test.mjs +2 -1
- package/tests/boot-failure-diagnostics.test.mjs +2 -2
- package/tests/browser/interaction-continuity.spec.mjs +2 -2
- package/tests/browser/session-summary.spec.mjs +131 -3
- package/tests/browser/stream-output-isolation.spec.mjs +510 -0
- package/tests/codex-fast-mode-static.test.mjs +2 -2
- package/tests/composer-action-grid-reorder-static.test.mjs +2 -2
- package/tests/fast-mode-client-static.test.mjs +3 -2
- package/tests/feature-decision-output-popup-static.test.mjs +1 -1
- package/tests/fixtures/fake-pi.mjs +203 -2
- package/tests/git-workflow-preferences.test.mjs +7 -1
- package/tests/guided-git-aur-review.test.mjs +4 -3
- package/tests/http-endpoints-harness.test.mjs +224 -13
- package/tests/mobile-continuity-tablet-static.test.mjs +2 -2
- package/tests/mobile-foundation-static.test.mjs +3 -4
- package/tests/mobile-phone-experience-static.test.mjs +1 -1
- package/tests/mobile-static.test.mjs +63 -29
- package/tests/native-parity.test.mjs +1 -1
- package/tests/open-issue-wizard-static.test.mjs +2 -3
- package/tests/optional-feature-migration-frontend.test.mjs +3 -0
- package/tests/persistent-ui-layout-static.test.mjs +2 -2
- package/tests/questionnaire-dialog.test.mjs +2 -2
- package/tests/remote-auth-settings-harness.test.mjs +9 -0
- package/tests/session-summary-core.test.mjs +183 -1
- package/tests/side-panel-section-reorder-static.test.mjs +2 -2
- package/tests/stream-lifecycle-core.test.mjs +134 -0
- package/tests/stream-output-controller.test.mjs +252 -0
- package/tests/stream-output-isolation-static.test.mjs +89 -0
- package/tests/streaming-ui-coupling.test.mjs +50 -0
- package/tests/subagent-gate-visibility.test.mjs +1 -1
- package/tests/subagent-launch-slots-static.test.mjs +1 -1
- package/tests/theme-contract.test.mjs +104 -0
- package/tests/theme-customizer-static.test.mjs +82 -0
- package/tests/theme-scheme-mode.test.mjs +15 -0
- package/tests/workspace-session-summaries.test.mjs +288 -0
package/public/index.html
CHANGED
|
@@ -207,9 +207,7 @@
|
|
|
207
207
|
</div>
|
|
208
208
|
</div>
|
|
209
209
|
<div class="terminal-sidebar-actions" aria-label="Tab and workspace actions">
|
|
210
|
-
<button id="splitTabButton" class="terminal-split-button" type="button" aria-label="Split the active terminal to the right" aria-pressed="false"><svg class="terminal-split-icon terminal-split-icon-split" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="4" y="5" width="16" height="14" rx="2" fill="none" stroke="currentColor" stroke-width="2"/><path d="M12 5v14M15 12h4m0 0-2-2m2 2-2 2" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg><svg class="terminal-split-icon terminal-split-icon-unsplit" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="4" y="5" width="16" height="14" rx="2" fill="none" stroke="currentColor" stroke-width="2"/><path d="M12 5v14M19 12h-4m0 0 2-2m-2 2 2 2" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button>
|
|
211
210
|
<button id="workspaceSaveButton" class="terminal-workspace-save-button" type="button" aria-label="Save workspace" data-tooltip="Save workspace: • Captures all open tabs, sessions, order, custom groups, and the active tab. • Available when at least one tab is open."><svg class="terminal-workspace-save-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M5 4h11l3 3v13H5z" fill="none" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/><path d="M8 4v6h7V4M8 19v-5h8v5" fill="none" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></svg></button>
|
|
212
|
-
<button id="summaryHeaderButton" class="terminal-summary-button" type="button" aria-label="Open session summary" aria-controls="sessionSummaryOverlay" hidden><svg class="composer-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M5 4h14v16H5z" fill="none" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/><path d="M8 8h8M8 12h8M8 16h5" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg></button>
|
|
213
211
|
<button id="commandPaletteButton" class="terminal-command-palette-button" type="button" aria-label="Open command palette" data-tooltip="Command palette: • Search and run actions, slash commands, models, and tabs. • Shortcut: Ctrl/Cmd+K."><svg class="composer-icon command-palette-launcher-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M5 7.5h14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><path d="M5 12h9" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><path d="M5 16.5h5" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><path d="M17 14.5 19.5 17 17 19.5" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button>
|
|
214
212
|
<button id="workspaceDashboardToggleButton" class="terminal-dashboard-button" type="button" aria-controls="workspaceDashboard" aria-expanded="false" aria-label="Show workspace overview" data-tooltip="Workspace overview: • Shows current tab, cwd, model, context, session, and queue. • Opens common workspace/session actions from one place."><svg class="composer-icon workspace-overview-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M4 11.4 12 4l8 7.4" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M6.5 10.5V20h11v-9.5" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M10 20v-5h4v5" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button>
|
|
215
213
|
</div>
|
|
@@ -385,7 +383,6 @@
|
|
|
385
383
|
<div id="composerActionsPanel" class="composer-actions-panel" aria-label="Composer actions">
|
|
386
384
|
<button id="newSessionButton" class="composer-new-button" type="button" data-composer-action-id="new" data-composer-action-span="1">New</button>
|
|
387
385
|
<button id="compactButton" class="composer-compact-button" type="button" title="Compact the current Pi session context" data-composer-action-id="compact" data-composer-action-span="2">Compact</button>
|
|
388
|
-
<button id="summaryActionButton" class="composer-icon-button composer-summary-button" type="button" data-composer-action-id="summary" data-composer-action-span="1" aria-label="Open session summary" aria-controls="sessionSummaryOverlay" hidden><svg class="composer-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M5 4h14v16H5z" fill="none" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/><path d="M8 8h8M8 12h8M8 16h5" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg></button>
|
|
389
386
|
<button
|
|
390
387
|
id="gitWorkflowButton"
|
|
391
388
|
class="composer-icon-button composer-git-button"
|
|
@@ -749,6 +746,7 @@
|
|
|
749
746
|
</div>
|
|
750
747
|
<input id="themeSearchInput" class="model-search-input theme-search-input" type="search" placeholder="Search themes…" autocomplete="off" spellcheck="false" hidden />
|
|
751
748
|
<select id="themeSelect" title="Theme"></select>
|
|
749
|
+
<button id="themeCustomizeButton" class="theme-customize-button" type="button" aria-haspopup="dialog" aria-controls="themeCustomizerDialog">Customize…</button>
|
|
752
750
|
<div id="themeSearchResults" class="model-search-results theme-search-results" role="listbox" aria-label="Theme search results" hidden></div>
|
|
753
751
|
</div>
|
|
754
752
|
|
|
@@ -1181,6 +1179,81 @@
|
|
|
1181
1179
|
</form>
|
|
1182
1180
|
</dialog>
|
|
1183
1181
|
|
|
1182
|
+
<dialog id="themeCustomizerDialog" class="extension-dialog theme-customizer-dialog" aria-labelledby="themeCustomizerTitle" aria-describedby="themeCustomizerDescription">
|
|
1183
|
+
<form class="theme-customizer-shell" method="dialog">
|
|
1184
|
+
<header class="theme-customizer-header">
|
|
1185
|
+
<div>
|
|
1186
|
+
<span class="theme-customizer-kicker">Pi-native theme</span>
|
|
1187
|
+
<h2 id="themeCustomizerTitle">Customize theme</h2>
|
|
1188
|
+
<p id="themeCustomizerDescription" class="muted">Preview changes in this browser, then save a JSON theme file without changing your Light, Dark, or Auto selection.</p>
|
|
1189
|
+
</div>
|
|
1190
|
+
<span id="themeCustomizerSourceBadge" class="theme-scope-badge">Bundled source</span>
|
|
1191
|
+
</header>
|
|
1192
|
+
|
|
1193
|
+
<div class="theme-customizer-scroll">
|
|
1194
|
+
<section class="theme-customizer-identity" aria-label="Theme destination">
|
|
1195
|
+
<label for="themeCustomizerName">Theme name
|
|
1196
|
+
<input id="themeCustomizerName" type="text" maxlength="75" autocomplete="off" spellcheck="false" aria-describedby="themeCustomizerNameHint" />
|
|
1197
|
+
</label>
|
|
1198
|
+
<p id="themeCustomizerNameHint" class="muted">Saved as this exact name plus <code>.json</code>.</p>
|
|
1199
|
+
<label for="themeCustomizerScope">Save destination
|
|
1200
|
+
<select id="themeCustomizerScope" aria-describedby="themeCustomizerScopeHint">
|
|
1201
|
+
<option id="themeCustomizerProjectOption" value="project">This project</option>
|
|
1202
|
+
<option value="global">Global themes</option>
|
|
1203
|
+
</select>
|
|
1204
|
+
</label>
|
|
1205
|
+
<p id="themeCustomizerScopeHint" class="muted">Project themes require the active project to be trusted by Pi.</p>
|
|
1206
|
+
</section>
|
|
1207
|
+
|
|
1208
|
+
<section id="themeCustomizerPreview" class="theme-customizer-preview" aria-label="Live theme sample">
|
|
1209
|
+
<strong>Live sample</strong>
|
|
1210
|
+
<p>Valid drafts preview across WebUI. <a href="#themeCustomizerJson">Links</a>, <code>inline code</code>, and status colours update without writing storage or disk.</p>
|
|
1211
|
+
<div><span class="success">Success</span><span class="warning">Warning</span><span class="error">Error</span></div>
|
|
1212
|
+
</section>
|
|
1213
|
+
|
|
1214
|
+
<details open>
|
|
1215
|
+
<summary>Visual colors <span class="muted">51 required tokens</span></summary>
|
|
1216
|
+
<div id="themeCustomizerVisualFields" class="theme-customizer-visual-fields"></div>
|
|
1217
|
+
</details>
|
|
1218
|
+
|
|
1219
|
+
<details>
|
|
1220
|
+
<summary>Optional fields</summary>
|
|
1221
|
+
<div class="theme-customizer-optional-fields">
|
|
1222
|
+
<label for="themeCustomizerThinkingMax">thinkingMax <span class="muted">optional; blank uses thinkingXhigh</span>
|
|
1223
|
+
<input id="themeCustomizerThinkingMax" type="text" autocomplete="off" spellcheck="false" />
|
|
1224
|
+
</label>
|
|
1225
|
+
<label for="themeCustomizerVars">Variables <span class="muted">JSON object</span>
|
|
1226
|
+
<textarea id="themeCustomizerVars" rows="5" spellcheck="false">{}</textarea>
|
|
1227
|
+
</label>
|
|
1228
|
+
<fieldset>
|
|
1229
|
+
<legend>Export colors</legend>
|
|
1230
|
+
<div id="themeCustomizerExportFields" class="theme-customizer-export-fields"></div>
|
|
1231
|
+
</fieldset>
|
|
1232
|
+
</div>
|
|
1233
|
+
</details>
|
|
1234
|
+
|
|
1235
|
+
<details>
|
|
1236
|
+
<summary>Advanced JSON</summary>
|
|
1237
|
+
<label class="sr-only" for="themeCustomizerJson">Advanced Pi theme JSON</label>
|
|
1238
|
+
<textarea id="themeCustomizerJson" class="theme-customizer-json" rows="18" spellcheck="false" aria-describedby="themeCustomizerStatus"></textarea>
|
|
1239
|
+
</details>
|
|
1240
|
+
|
|
1241
|
+
<section id="themeCustomizerOverwrite" class="theme-customizer-overwrite" role="alert" hidden>
|
|
1242
|
+
<strong>Replace this exact target?</strong>
|
|
1243
|
+
<p id="themeCustomizerOverwriteMessage"></p>
|
|
1244
|
+
<button id="themeCustomizerOverwriteButton" class="danger" type="button">Replace existing file</button>
|
|
1245
|
+
</section>
|
|
1246
|
+
<p id="themeCustomizerStatus" class="theme-customizer-status muted" role="status" aria-live="polite" aria-atomic="true">Valid changes preview immediately.</p>
|
|
1247
|
+
</div>
|
|
1248
|
+
|
|
1249
|
+
<menu class="theme-customizer-actions">
|
|
1250
|
+
<button id="themeCustomizerCancelButton" type="button">Cancel</button>
|
|
1251
|
+
<button id="themeCustomizerResetButton" type="button">Reset draft</button>
|
|
1252
|
+
<button id="themeCustomizerSaveButton" class="primary" type="button">Save theme</button>
|
|
1253
|
+
</menu>
|
|
1254
|
+
</form>
|
|
1255
|
+
</dialog>
|
|
1256
|
+
|
|
1184
1257
|
<dialog id="confirmationDialog" class="extension-dialog confirmation-dialog">
|
|
1185
1258
|
<form method="dialog">
|
|
1186
1259
|
<h2 id="confirmationTitle">Confirm action</h2>
|
|
@@ -1563,7 +1636,7 @@
|
|
|
1563
1636
|
</form>
|
|
1564
1637
|
</dialog>
|
|
1565
1638
|
|
|
1566
|
-
<script id="webuiBootLoader" data-app-src="/app.js?v=
|
|
1639
|
+
<script id="webuiBootLoader" data-app-src="/app.js?v=113">
|
|
1567
1640
|
(() => {
|
|
1568
1641
|
"use strict";
|
|
1569
1642
|
|
package/public/service-worker.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const CACHE_NAME = "pi-webui-pwa-
|
|
1
|
+
const CACHE_NAME = "pi-webui-pwa-v74";
|
|
2
2
|
const APP_SHELL = [
|
|
3
3
|
"/",
|
|
4
4
|
"/index.html",
|
|
@@ -11,6 +11,8 @@ const APP_SHELL = [
|
|
|
11
11
|
"/guided-git-command-state.mjs",
|
|
12
12
|
"/guided-git-review-state.mjs",
|
|
13
13
|
"/fast-output-live.mjs",
|
|
14
|
+
"/stream-output-controller.mjs",
|
|
15
|
+
"/theme-contract.mjs",
|
|
14
16
|
"/transcript-renderer.mjs",
|
|
15
17
|
"/subagent-launch-slot-state.mjs",
|
|
16
18
|
"/subagent-gate-visibility.mjs",
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
const MESSAGE_UPDATE_KINDS = Object.freeze({
|
|
2
|
+
thinking_start: "thinking",
|
|
3
|
+
thinking_delta: "thinking",
|
|
4
|
+
thinking_end: "thinking",
|
|
5
|
+
text_start: "text",
|
|
6
|
+
text_delta: "text",
|
|
7
|
+
text_end: "text",
|
|
8
|
+
toolcall_start: "tool-call",
|
|
9
|
+
toolcall_delta: "tool-call",
|
|
10
|
+
toolcall_end: "tool-call",
|
|
11
|
+
error: "stream-error",
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const COALESCIBLE_DELTA_TYPES = new Set(["thinking_delta", "text_delta", "toolcall_delta"]);
|
|
15
|
+
const DIAGNOSTIC_INDEX_LIMIT = 4096;
|
|
16
|
+
export const DEFAULT_STREAM_PENDING_ENTRY_LIMIT = 128;
|
|
17
|
+
export const DEFAULT_STREAM_PENDING_BYTE_LIMIT = 256 * 1024;
|
|
18
|
+
|
|
19
|
+
export const TRANSCRIPT_STREAM_MESSAGE_UPDATE_TYPES = Object.freeze(Object.keys(MESSAGE_UPDATE_KINDS));
|
|
20
|
+
|
|
21
|
+
export function classifyTranscriptStreamEvent(event) {
|
|
22
|
+
if (event?.type === "tool_execution_update") {
|
|
23
|
+
return Object.freeze({ kind: "tool-execution", subtype: "tool_execution_update", recognized: true, barrier: false });
|
|
24
|
+
}
|
|
25
|
+
if (event?.type !== "message_update") return null;
|
|
26
|
+
const subtype = typeof event.assistantMessageEvent?.type === "string" ? event.assistantMessageEvent.type : "";
|
|
27
|
+
const kind = MESSAGE_UPDATE_KINDS[subtype];
|
|
28
|
+
if (!kind) return Object.freeze({ kind: "unknown-message-update", subtype, recognized: false, barrier: false });
|
|
29
|
+
return Object.freeze({
|
|
30
|
+
kind,
|
|
31
|
+
subtype,
|
|
32
|
+
recognized: true,
|
|
33
|
+
barrier: subtype.endsWith("_end") || subtype === "error",
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function defaultScheduleFrame(callback) {
|
|
38
|
+
if (typeof globalThis.requestAnimationFrame === "function") return globalThis.requestAnimationFrame(callback);
|
|
39
|
+
return globalThis.setTimeout(callback, 0);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function defaultCancelFrame(handle) {
|
|
43
|
+
if (typeof globalThis.cancelAnimationFrame === "function") globalThis.cancelAnimationFrame(handle);
|
|
44
|
+
else globalThis.clearTimeout(handle);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function positiveLimit(value, fallback, name) {
|
|
48
|
+
if (value === undefined) return fallback;
|
|
49
|
+
if (!Number.isSafeInteger(value) || value < 1) throw new TypeError(`${name} must be a positive safe integer`);
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function eventByteSize(event) {
|
|
54
|
+
try {
|
|
55
|
+
return Math.max(1, JSON.stringify(event).length * 2);
|
|
56
|
+
} catch {
|
|
57
|
+
return DEFAULT_STREAM_PENDING_BYTE_LIMIT;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function eventDebugIndex(event) {
|
|
62
|
+
const value = Number(event?.isolationDeltaIndex ?? event?.streamIsolationIndex);
|
|
63
|
+
return Number.isSafeInteger(value) && value >= 0 ? value : null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function toolCallIdentity(event) {
|
|
67
|
+
const update = event?.assistantMessageEvent || {};
|
|
68
|
+
return String(event?.toolCallId || update.toolCallId || update.toolCall?.id || update.id || "");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function entryCoalesceKey({ event, classification, owner }) {
|
|
72
|
+
if (COALESCIBLE_DELTA_TYPES.has(classification.subtype)) {
|
|
73
|
+
const update = event?.assistantMessageEvent || {};
|
|
74
|
+
if (typeof update.delta !== "string") return "";
|
|
75
|
+
const contentIndex = Number.isInteger(Number(update.contentIndex)) ? Number(update.contentIndex) : "";
|
|
76
|
+
const toolId = classification.subtype === "toolcall_delta" ? toolCallIdentity(event) : "";
|
|
77
|
+
return `${String(owner ?? "")}|${classification.subtype}|${contentIndex}|${toolId}`;
|
|
78
|
+
}
|
|
79
|
+
if (classification.kind === "tool-execution" && event?.partialResult && typeof event.partialResult === "object") {
|
|
80
|
+
const toolId = toolCallIdentity(event);
|
|
81
|
+
return toolId ? `${String(owner ?? "")}|tool-execution|${toolId}` : "";
|
|
82
|
+
}
|
|
83
|
+
return "";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function mergedAdjacentEntry(previous, incoming) {
|
|
87
|
+
const previousKey = entryCoalesceKey(previous);
|
|
88
|
+
if (!previousKey || previousKey !== entryCoalesceKey(incoming)) return null;
|
|
89
|
+
if (incoming.classification.kind === "tool-execution") {
|
|
90
|
+
const event = incoming.event;
|
|
91
|
+
return {
|
|
92
|
+
...incoming,
|
|
93
|
+
event,
|
|
94
|
+
bytes: eventByteSize(event),
|
|
95
|
+
sourceCount: previous.sourceCount + incoming.sourceCount,
|
|
96
|
+
sourceIndexes: [...previous.sourceIndexes, ...incoming.sourceIndexes].slice(-DIAGNOSTIC_INDEX_LIMIT),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const previousUpdate = previous.event.assistantMessageEvent || {};
|
|
100
|
+
const incomingUpdate = incoming.event.assistantMessageEvent || {};
|
|
101
|
+
const event = {
|
|
102
|
+
...incoming.event,
|
|
103
|
+
assistantMessageEvent: {
|
|
104
|
+
...incomingUpdate,
|
|
105
|
+
delta: `${typeof previousUpdate.delta === "string" ? previousUpdate.delta : ""}${typeof incomingUpdate.delta === "string" ? incomingUpdate.delta : ""}`,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
...incoming,
|
|
110
|
+
event,
|
|
111
|
+
bytes: eventByteSize(event),
|
|
112
|
+
sourceCount: previous.sourceCount + incoming.sourceCount,
|
|
113
|
+
sourceIndexes: [...previous.sourceIndexes, ...incoming.sourceIndexes].slice(-DIAGNOSTIC_INDEX_LIMIT),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Owns the bounded frame queue for high-frequency transcript stream events.
|
|
119
|
+
* Adjacent compatible deltas are losslessly coalesced while event-kind and
|
|
120
|
+
* semantic barriers retain server order. Overflow synchronously drains the
|
|
121
|
+
* current batch; a single oversize event is applied directly, never dropped.
|
|
122
|
+
* Every injected sink is transcript-specific and the optional diagnostic hook
|
|
123
|
+
* is inert unless explicitly supplied by a test/debug caller.
|
|
124
|
+
*/
|
|
125
|
+
export function createStreamOutputController({
|
|
126
|
+
scheduleFrame = defaultScheduleFrame,
|
|
127
|
+
cancelFrame = defaultCancelFrame,
|
|
128
|
+
isOwnerCurrent = () => true,
|
|
129
|
+
applyTextUpdate = () => {},
|
|
130
|
+
applyThinkingUpdate = () => {},
|
|
131
|
+
applyToolCallUpdate = () => {},
|
|
132
|
+
applyToolExecutionUpdate = () => {},
|
|
133
|
+
applyStreamError = () => {},
|
|
134
|
+
applyFollowScroll = () => {},
|
|
135
|
+
onUnknownStreamEvent = () => {},
|
|
136
|
+
onStaleOwner = () => {},
|
|
137
|
+
onOverflow = () => {},
|
|
138
|
+
onDiagnostic,
|
|
139
|
+
maxPendingEntries,
|
|
140
|
+
maxPendingBytes,
|
|
141
|
+
} = {}) {
|
|
142
|
+
if (typeof scheduleFrame !== "function" || typeof cancelFrame !== "function") {
|
|
143
|
+
throw new TypeError("scheduleFrame and cancelFrame must be functions");
|
|
144
|
+
}
|
|
145
|
+
const entryLimit = positiveLimit(maxPendingEntries, DEFAULT_STREAM_PENDING_ENTRY_LIMIT, "maxPendingEntries");
|
|
146
|
+
const byteLimit = positiveLimit(maxPendingBytes, DEFAULT_STREAM_PENDING_BYTE_LIMIT, "maxPendingBytes");
|
|
147
|
+
const diagnosticsEnabled = typeof onDiagnostic === "function";
|
|
148
|
+
const emitDiagnostic = diagnosticsEnabled
|
|
149
|
+
? (record) => {
|
|
150
|
+
try {
|
|
151
|
+
onDiagnostic(Object.freeze(record));
|
|
152
|
+
} catch {
|
|
153
|
+
// Test/debug instrumentation must never alter stream behavior.
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
: () => {};
|
|
157
|
+
|
|
158
|
+
let frameHandle = null;
|
|
159
|
+
let pending = [];
|
|
160
|
+
let pendingBytes = 0;
|
|
161
|
+
|
|
162
|
+
const ownerIsCurrent = (owner) => owner === undefined || isOwnerCurrent(owner) === true;
|
|
163
|
+
|
|
164
|
+
const reportStale = (event, owner, classification, phase) => {
|
|
165
|
+
onStaleOwner(event, owner);
|
|
166
|
+
emitDiagnostic({ type: "stale", phase, owner, kind: classification?.kind || "", subtype: classification?.subtype || "", index: eventDebugIndex(event) });
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const applyEntry = ({ event, classification, owner, sourceCount = 1, sourceIndexes = [] }) => {
|
|
170
|
+
if (!ownerIsCurrent(owner)) {
|
|
171
|
+
reportStale(event, owner, classification, "apply");
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
let applied = true;
|
|
175
|
+
switch (classification.kind) {
|
|
176
|
+
case "text":
|
|
177
|
+
applyTextUpdate(event, classification);
|
|
178
|
+
break;
|
|
179
|
+
case "thinking":
|
|
180
|
+
applyThinkingUpdate(event, classification);
|
|
181
|
+
break;
|
|
182
|
+
case "tool-call":
|
|
183
|
+
applyToolCallUpdate(event, classification);
|
|
184
|
+
break;
|
|
185
|
+
case "tool-execution":
|
|
186
|
+
applyToolExecutionUpdate(event, classification);
|
|
187
|
+
break;
|
|
188
|
+
case "stream-error":
|
|
189
|
+
applyStreamError(event, classification);
|
|
190
|
+
break;
|
|
191
|
+
default:
|
|
192
|
+
onUnknownStreamEvent(event, classification, owner);
|
|
193
|
+
applied = false;
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
emitDiagnostic({
|
|
197
|
+
type: "apply",
|
|
198
|
+
owner,
|
|
199
|
+
kind: classification.kind,
|
|
200
|
+
subtype: classification.subtype,
|
|
201
|
+
applied,
|
|
202
|
+
sourceCount,
|
|
203
|
+
sourceIndexes: Object.freeze([...sourceIndexes]),
|
|
204
|
+
});
|
|
205
|
+
return applied;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const drain = () => {
|
|
209
|
+
frameHandle = null;
|
|
210
|
+
if (!pending.length) return 0;
|
|
211
|
+
const entries = pending;
|
|
212
|
+
pending = [];
|
|
213
|
+
pendingBytes = 0;
|
|
214
|
+
let applied = 0;
|
|
215
|
+
for (const entry of entries) {
|
|
216
|
+
if (applyEntry(entry)) applied += 1;
|
|
217
|
+
}
|
|
218
|
+
if (applied > 0) applyFollowScroll();
|
|
219
|
+
emitDiagnostic({ type: "batch", entries: entries.length, sourceCount: entries.reduce((total, entry) => total + entry.sourceCount, 0), applied });
|
|
220
|
+
return applied;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const schedule = () => {
|
|
224
|
+
if (frameHandle !== null) return;
|
|
225
|
+
frameHandle = scheduleFrame(drain);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const flush = () => {
|
|
229
|
+
if (frameHandle !== null) {
|
|
230
|
+
cancelFrame(frameHandle);
|
|
231
|
+
frameHandle = null;
|
|
232
|
+
}
|
|
233
|
+
return drain();
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const applyOversize = (entry) => {
|
|
237
|
+
const applied = applyEntry(entry) ? 1 : 0;
|
|
238
|
+
if (applied) applyFollowScroll();
|
|
239
|
+
emitDiagnostic({ type: "batch", entries: 1, sourceCount: entry.sourceCount, applied, direct: true });
|
|
240
|
+
return applied;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const enqueue = (entry) => {
|
|
244
|
+
const last = pending[pending.length - 1];
|
|
245
|
+
const merged = last ? mergedAdjacentEntry(last, entry) : null;
|
|
246
|
+
if (merged && pendingBytes - last.bytes + merged.bytes <= byteLimit) {
|
|
247
|
+
pending[pending.length - 1] = merged;
|
|
248
|
+
pendingBytes = pendingBytes - last.bytes + merged.bytes;
|
|
249
|
+
emitDiagnostic({ type: "queued", coalesced: true, pendingCount: pending.length, pendingBytes, sourceCount: merged.sourceCount });
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (merged || pending.length >= entryLimit || pendingBytes + entry.bytes > byteLimit) {
|
|
254
|
+
const reason = merged ? "coalesced-byte-limit" : pending.length >= entryLimit ? "entry-limit" : "byte-limit";
|
|
255
|
+
onOverflow({ reason, pendingCount: pending.length, pendingBytes, incomingBytes: entry.bytes });
|
|
256
|
+
emitDiagnostic({ type: "overflow", reason, pendingCount: pending.length, pendingBytes, incomingBytes: entry.bytes });
|
|
257
|
+
flush();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (entry.bytes > byteLimit) {
|
|
261
|
+
onOverflow({ reason: "oversize-event", pendingCount: 0, pendingBytes: 0, incomingBytes: entry.bytes });
|
|
262
|
+
emitDiagnostic({ type: "overflow", reason: "oversize-event", pendingCount: 0, pendingBytes: 0, incomingBytes: entry.bytes });
|
|
263
|
+
applyOversize(entry);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
pending.push(entry);
|
|
268
|
+
pendingBytes += entry.bytes;
|
|
269
|
+
emitDiagnostic({ type: "queued", coalesced: false, pendingCount: pending.length, pendingBytes, sourceCount: entry.sourceCount });
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
return Object.freeze({
|
|
273
|
+
dispatch(event, { owner } = {}) {
|
|
274
|
+
const classification = classifyTranscriptStreamEvent(event);
|
|
275
|
+
if (!classification) return false;
|
|
276
|
+
const index = eventDebugIndex(event);
|
|
277
|
+
emitDiagnostic({ type: "receipt", owner, kind: classification.kind, subtype: classification.subtype, index });
|
|
278
|
+
if (!ownerIsCurrent(owner)) {
|
|
279
|
+
reportStale(event, owner, classification, "dispatch");
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
const entry = {
|
|
283
|
+
event,
|
|
284
|
+
classification,
|
|
285
|
+
owner,
|
|
286
|
+
bytes: eventByteSize(event),
|
|
287
|
+
sourceCount: 1,
|
|
288
|
+
sourceIndexes: diagnosticsEnabled && index !== null ? [index] : [],
|
|
289
|
+
};
|
|
290
|
+
enqueue(entry);
|
|
291
|
+
if (classification.barrier) {
|
|
292
|
+
emitDiagnostic({ type: "barrier", reason: classification.subtype, pendingCount: pending.length, pendingBytes });
|
|
293
|
+
flush();
|
|
294
|
+
} else if (pending.length) {
|
|
295
|
+
schedule();
|
|
296
|
+
}
|
|
297
|
+
return true;
|
|
298
|
+
},
|
|
299
|
+
flush,
|
|
300
|
+
barrier(reason = "semantic") {
|
|
301
|
+
emitDiagnostic({ type: "barrier", reason: String(reason || "semantic"), pendingCount: pending.length, pendingBytes });
|
|
302
|
+
return flush();
|
|
303
|
+
},
|
|
304
|
+
cancel(owner) {
|
|
305
|
+
if (owner === undefined) pending = [];
|
|
306
|
+
else pending = pending.filter((entry) => entry.owner !== owner);
|
|
307
|
+
pendingBytes = pending.reduce((total, entry) => total + entry.bytes, 0);
|
|
308
|
+
if (!pending.length && frameHandle !== null) {
|
|
309
|
+
cancelFrame(frameHandle);
|
|
310
|
+
frameHandle = null;
|
|
311
|
+
}
|
|
312
|
+
emitDiagnostic({ type: "cancel", owner, pendingCount: pending.length, pendingBytes });
|
|
313
|
+
},
|
|
314
|
+
pendingCount() {
|
|
315
|
+
return pending.length;
|
|
316
|
+
},
|
|
317
|
+
pendingBytes() {
|
|
318
|
+
return pendingBytes;
|
|
319
|
+
},
|
|
320
|
+
limits() {
|
|
321
|
+
return Object.freeze({ maxPendingEntries: entryLimit, maxPendingBytes: byteLimit });
|
|
322
|
+
},
|
|
323
|
+
hasScheduledFrame() {
|
|
324
|
+
return frameHandle !== null;
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
}
|