@firstpick/pi-package-webui 0.9.4 → 0.9.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 +7 -2
- package/bin/pi-webui.mjs +90 -5
- package/lib/optional-feature-catalog.mjs +1 -1
- package/lib/session-actions.mjs +20 -2
- package/lib/ui-layout-settings.mjs +37 -6
- package/package.json +1 -1
- package/public/app.js +2311 -257
- package/public/index.html +82 -55
- package/public/service-worker.js +4 -1
- package/public/stream-derived-output.mjs +799 -0
- package/public/stream-markdown-tail.mjs +204 -0
- package/public/stream-output-controller.mjs +222 -51
- package/public/stream-render-scheduler.mjs +85 -0
- package/public/styles.css +276 -20
- package/public/transcript-renderer.mjs +73 -24
- package/tests/boot-failure-diagnostics.test.mjs +3 -3
- package/tests/browser/composer-action-grid-reorder.spec.mjs +114 -31
- package/tests/browser/control-deck-side-panels.spec.mjs +42 -0
- package/tests/browser/control-visibility.spec.mjs +396 -0
- package/tests/browser/mobile-foundation.spec.mjs +28 -0
- package/tests/browser/persistent-ui-layout.spec.mjs +9 -9
- package/tests/browser/stream-output-isolation.spec.mjs +308 -3
- package/tests/browser/user-noticeable-fixes.spec.mjs +204 -0
- package/tests/codex-fast-mode-static.test.mjs +2 -2
- package/tests/composer-action-grid-reorder-static.test.mjs +8 -7
- package/tests/control-deck-side-panels-static.test.mjs +3 -1
- package/tests/control-visibility-static.test.mjs +294 -0
- package/tests/controls-layout-static.test.mjs +3 -3
- package/tests/cwd-footer-reliability-static.test.mjs +1 -1
- package/tests/fast-mode-client-static.test.mjs +1 -1
- package/tests/feature-decision-output-popup-static.test.mjs +2 -2
- package/tests/file-viewer-image-static.test.mjs +3 -3
- package/tests/fixtures/fake-pi.mjs +57 -10
- package/tests/fixtures/streaming-workloads.mjs +167 -0
- package/tests/footer-provider-usage-visual-static.test.mjs +15 -0
- package/tests/foreground-transcript-catch-up-static.test.mjs +7 -1
- package/tests/git-workflow-preferences.test.mjs +2 -2
- package/tests/http-endpoints-harness.test.mjs +4 -4
- package/tests/interaction-state-stability-static.test.mjs +3 -1
- package/tests/intercom-conversation-viewer-static.test.mjs +4 -4
- package/tests/intercom-conversations-http.test.mjs +16 -0
- package/tests/main-output-loading-static.test.mjs +3 -3
- package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
- package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
- package/tests/mobile-foundation-static.test.mjs +3 -3
- package/tests/mobile-phone-experience-static.test.mjs +1 -1
- package/tests/mobile-static.test.mjs +77 -66
- package/tests/open-issue-wizard-static.test.mjs +2 -2
- package/tests/persistent-ui-layout-static.test.mjs +7 -7
- package/tests/prompt-input-vertical-scroll-static.test.mjs +26 -0
- package/tests/questionnaire-dialog.test.mjs +3 -3
- package/tests/runtime-error-visibility.test.mjs +3 -3
- package/tests/session-auth-harness.test.mjs +26 -1
- package/tests/session-sampling-ui-static.test.mjs +3 -3
- package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
- package/tests/stream-derived-output.test.mjs +121 -0
- package/tests/stream-markdown-tail.test.mjs +218 -0
- package/tests/stream-output-controller.test.mjs +119 -4
- package/tests/stream-output-isolation-static.test.mjs +34 -6
- package/tests/stream-output-workloads.test.mjs +276 -0
- package/tests/stream-render-scheduler.test.mjs +115 -0
- package/tests/streaming-ui-coupling.test.mjs +1 -1
- package/tests/subagent-observability-static.test.mjs +2 -2
- package/tests/syntax-highlighting-static.test.mjs +3 -3
- package/tests/theme-customizer-static.test.mjs +1 -1
- package/tests/thinking-format-parser.test.mjs +15 -0
- package/tests/todo-filter-static.test.mjs +148 -0
- package/tests/transcript-markdown-tail.test.mjs +137 -0
- package/tests/ui-layout-settings.test.mjs +54 -7
- package/tests/webui-settings-locking.test.mjs +3 -3
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
export const STREAMING_MARKDOWN_PLAIN_TAIL_LIMIT = 16 * 1024;
|
|
2
|
+
|
|
3
|
+
const LINE_LEADING = "leading";
|
|
4
|
+
const LINE_TICKS = "ticks";
|
|
5
|
+
const LINE_AFTER_TICKS = "after-ticks";
|
|
6
|
+
const LINE_LANGUAGE = "language";
|
|
7
|
+
const LINE_TRAILING = "trailing";
|
|
8
|
+
const LINE_INVALID = "invalid";
|
|
9
|
+
|
|
10
|
+
function initialLineState() {
|
|
11
|
+
return {
|
|
12
|
+
lineBlank: true,
|
|
13
|
+
lineStage: LINE_LEADING,
|
|
14
|
+
lineTicks: 0,
|
|
15
|
+
lineLanguage: "",
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function initialState() {
|
|
20
|
+
return {
|
|
21
|
+
value: "",
|
|
22
|
+
scanOffset: 0,
|
|
23
|
+
boundary: 0,
|
|
24
|
+
inFence: false,
|
|
25
|
+
fenceContentOffset: -1,
|
|
26
|
+
fenceLanguage: "",
|
|
27
|
+
fenceTicks: 0,
|
|
28
|
+
scannedChars: 0,
|
|
29
|
+
fallback: false,
|
|
30
|
+
...initialLineState(),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isLineWhitespace(character) {
|
|
35
|
+
return character !== "\n" && /\s/.test(character);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function isFenceLanguageCharacter(character) {
|
|
39
|
+
return /[A-Za-z0-9_.+-]/.test(character);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function invalidateLineFence(state) {
|
|
43
|
+
state.lineStage = LINE_INVALID;
|
|
44
|
+
state.lineLanguage = "";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function advanceOpeningFenceCandidate(state, character) {
|
|
48
|
+
if (isLineWhitespace(character)) {
|
|
49
|
+
if (state.lineStage === LINE_LANGUAGE) state.lineStage = LINE_TRAILING;
|
|
50
|
+
else if (state.lineStage !== LINE_TRAILING) state.lineStage = LINE_AFTER_TICKS;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (isFenceLanguageCharacter(character) && state.lineStage !== LINE_TRAILING) {
|
|
54
|
+
state.lineStage = LINE_LANGUAGE;
|
|
55
|
+
state.lineLanguage += character;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
invalidateLineFence(state);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function advanceLineCharacter(state, character) {
|
|
62
|
+
// An invalid delimiter candidate necessarily already contains a non-space;
|
|
63
|
+
// later code/paragraph characters need only advance the outer scan cursor.
|
|
64
|
+
if (state.lineStage === LINE_INVALID) return;
|
|
65
|
+
const whitespace = isLineWhitespace(character);
|
|
66
|
+
if (!whitespace) state.lineBlank = false;
|
|
67
|
+
|
|
68
|
+
if (state.lineStage === LINE_LEADING) {
|
|
69
|
+
if (whitespace) return;
|
|
70
|
+
if (character === "`") {
|
|
71
|
+
state.lineStage = LINE_TICKS;
|
|
72
|
+
state.lineTicks = 1;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
invalidateLineFence(state);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (state.lineStage === LINE_TICKS) {
|
|
80
|
+
// CommonMark fences are three OR MORE backticks (```` ````md ```` wraps nested ``` blocks).
|
|
81
|
+
if (character === "`") {
|
|
82
|
+
state.lineTicks += 1;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (state.lineTicks < 3) {
|
|
86
|
+
invalidateLineFence(state);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
state.lineStage = LINE_AFTER_TICKS;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (state.inFence) {
|
|
93
|
+
if (!whitespace) invalidateLineFence(state);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
advanceOpeningFenceCandidate(state, character);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function lineIsFenceDelimiter(state) {
|
|
100
|
+
if (state.lineTicks < 3 || state.lineStage === LINE_INVALID) return false;
|
|
101
|
+
// A closing fence needs at least as many backticks as the opening one.
|
|
102
|
+
if (state.inFence) return state.lineTicks >= (state.fenceTicks || 3) && (state.lineStage === LINE_TICKS || state.lineStage === LINE_AFTER_TICKS);
|
|
103
|
+
return [LINE_TICKS, LINE_AFTER_TICKS, LINE_LANGUAGE, LINE_TRAILING].includes(state.lineStage);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function finishLine(state, nextOffset) {
|
|
107
|
+
const fenceDelimiter = lineIsFenceDelimiter(state);
|
|
108
|
+
const blank = state.lineBlank;
|
|
109
|
+
if (state.inFence) {
|
|
110
|
+
if (fenceDelimiter) {
|
|
111
|
+
state.inFence = false;
|
|
112
|
+
state.fenceContentOffset = -1;
|
|
113
|
+
state.fenceLanguage = "";
|
|
114
|
+
state.fenceTicks = 0;
|
|
115
|
+
// A closed fence is an independently stable block. Committing it here
|
|
116
|
+
// prevents later prose from causing the completed code to tokenize again.
|
|
117
|
+
state.boundary = nextOffset;
|
|
118
|
+
}
|
|
119
|
+
} else if (fenceDelimiter) {
|
|
120
|
+
state.inFence = true;
|
|
121
|
+
state.fenceContentOffset = nextOffset;
|
|
122
|
+
state.fenceLanguage = state.lineLanguage;
|
|
123
|
+
state.fenceTicks = state.lineTicks;
|
|
124
|
+
}
|
|
125
|
+
if (!state.inFence && blank) state.boundary = nextOffset;
|
|
126
|
+
Object.assign(state, initialLineState());
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function scanSuffix(state, value, start) {
|
|
130
|
+
for (let offset = start; offset < value.length; offset += 1) {
|
|
131
|
+
const character = value[offset];
|
|
132
|
+
state.scannedChars += 1;
|
|
133
|
+
if (character === "\n") finishLine(state, offset + 1);
|
|
134
|
+
else advanceLineCharacter(state, character);
|
|
135
|
+
}
|
|
136
|
+
state.scanOffset = value.length;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Advances streaming Markdown boundary state from only a caller-confirmed
|
|
141
|
+
* appended suffix. A non-append snapshot deliberately rebuilds from the full
|
|
142
|
+
* authoritative value and reports that fallback through `fallback`.
|
|
143
|
+
*/
|
|
144
|
+
export function advanceStreamingMarkdownTail(previous, text, { complete = false, appendOnly = false, plainTailLimit = STREAMING_MARKDOWN_PLAIN_TAIL_LIMIT } = {}) {
|
|
145
|
+
const value = String(text || "");
|
|
146
|
+
const incremental = !!previous && appendOnly && value.length >= previous.value.length;
|
|
147
|
+
const state = incremental ? { ...previous } : initialState();
|
|
148
|
+
const start = incremental ? previous.value.length : 0;
|
|
149
|
+
state.scannedChars = 0;
|
|
150
|
+
state.fallback = !!previous && !incremental;
|
|
151
|
+
scanSuffix(state, value, start);
|
|
152
|
+
state.value = value;
|
|
153
|
+
|
|
154
|
+
if (complete) {
|
|
155
|
+
return {
|
|
156
|
+
boundary: value.length,
|
|
157
|
+
state,
|
|
158
|
+
scannedChars: state.scannedChars,
|
|
159
|
+
fallback: state.fallback,
|
|
160
|
+
tailKind: "complete",
|
|
161
|
+
liveMode: "authoritative",
|
|
162
|
+
fenceContentOffset: -1,
|
|
163
|
+
fenceLanguage: "",
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const pendingFence = lineIsFenceDelimiter(state);
|
|
168
|
+
const pendingClose = state.inFence && pendingFence;
|
|
169
|
+
const pendingOpening = !state.inFence && pendingFence;
|
|
170
|
+
const tailLength = value.length - state.boundary;
|
|
171
|
+
let liveMode = "markdown";
|
|
172
|
+
let tailKind = "text";
|
|
173
|
+
let fenceContentOffset = state.fenceContentOffset;
|
|
174
|
+
let fenceLanguage = state.fenceLanguage;
|
|
175
|
+
|
|
176
|
+
if (pendingClose) {
|
|
177
|
+
// The existing parser already recognizes this delimiter as a close even
|
|
178
|
+
// before a trailing newline arrives, so render the tail authoritatively once.
|
|
179
|
+
liveMode = "authoritative";
|
|
180
|
+
tailKind = "closed-fence";
|
|
181
|
+
} else if (state.inFence) {
|
|
182
|
+
liveMode = "open-fence";
|
|
183
|
+
tailKind = "open-fence";
|
|
184
|
+
} else if (pendingOpening) {
|
|
185
|
+
liveMode = "open-fence";
|
|
186
|
+
tailKind = "open-fence";
|
|
187
|
+
fenceContentOffset = value.length;
|
|
188
|
+
fenceLanguage = state.lineLanguage;
|
|
189
|
+
} else if (tailLength > Math.max(0, Number(plainTailLimit) || 0)) {
|
|
190
|
+
liveMode = "plain";
|
|
191
|
+
tailKind = "long-text";
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
boundary: state.boundary,
|
|
196
|
+
state,
|
|
197
|
+
scannedChars: state.scannedChars,
|
|
198
|
+
fallback: state.fallback,
|
|
199
|
+
tailKind,
|
|
200
|
+
liveMode,
|
|
201
|
+
fenceContentOffset,
|
|
202
|
+
fenceLanguage,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
@@ -13,6 +13,7 @@ const MESSAGE_UPDATE_KINDS = Object.freeze({
|
|
|
13
13
|
|
|
14
14
|
const COALESCIBLE_DELTA_TYPES = new Set(["thinking_delta", "text_delta", "toolcall_delta"]);
|
|
15
15
|
const DIAGNOSTIC_INDEX_LIMIT = 4096;
|
|
16
|
+
const URGENT_PRESSURE_ENTRY_LIMIT = 1;
|
|
16
17
|
export const DEFAULT_STREAM_PENDING_ENTRY_LIMIT = 128;
|
|
17
18
|
export const DEFAULT_STREAM_PENDING_BYTE_LIMIT = 256 * 1024;
|
|
18
19
|
|
|
@@ -49,6 +50,10 @@ export function classifyTranscriptStreamEvent(event) {
|
|
|
49
50
|
});
|
|
50
51
|
}
|
|
51
52
|
|
|
53
|
+
function defaultNow() {
|
|
54
|
+
return typeof globalThis.performance?.now === "function" ? globalThis.performance.now() : Date.now();
|
|
55
|
+
}
|
|
56
|
+
|
|
52
57
|
function defaultScheduleFrame(callback) {
|
|
53
58
|
if (typeof globalThis.requestAnimationFrame === "function") return globalThis.requestAnimationFrame(callback);
|
|
54
59
|
return globalThis.setTimeout(callback, 0);
|
|
@@ -59,20 +64,107 @@ function defaultCancelFrame(handle) {
|
|
|
59
64
|
else globalThis.clearTimeout(handle);
|
|
60
65
|
}
|
|
61
66
|
|
|
67
|
+
function defaultSchedulePressureDrain(callback) {
|
|
68
|
+
return globalThis.setTimeout(callback, 0);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function defaultCancelPressureDrain(handle) {
|
|
72
|
+
globalThis.clearTimeout(handle);
|
|
73
|
+
}
|
|
74
|
+
|
|
62
75
|
function positiveLimit(value, fallback, name) {
|
|
63
76
|
if (value === undefined) return fallback;
|
|
64
77
|
if (!Number.isSafeInteger(value) || value < 1) throw new TypeError(`${name} must be a positive safe integer`);
|
|
65
78
|
return value;
|
|
66
79
|
}
|
|
67
80
|
|
|
68
|
-
function
|
|
81
|
+
function jsonStringCodeUnits(value) {
|
|
82
|
+
let units = 2;
|
|
83
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
84
|
+
const code = value.charCodeAt(index);
|
|
85
|
+
if (code === 0x22 || code === 0x5c || code === 0x08 || code === 0x09 || code === 0x0a || code === 0x0c || code === 0x0d) {
|
|
86
|
+
units += 2;
|
|
87
|
+
} else if (code < 0x20) {
|
|
88
|
+
units += 6;
|
|
89
|
+
} else if (code >= 0xd800 && code <= 0xdbff) {
|
|
90
|
+
const next = value.charCodeAt(index + 1);
|
|
91
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
92
|
+
units += 2;
|
|
93
|
+
index += 1;
|
|
94
|
+
} else {
|
|
95
|
+
units += 6;
|
|
96
|
+
}
|
|
97
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) {
|
|
98
|
+
units += 6;
|
|
99
|
+
} else {
|
|
100
|
+
units += 1;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return units;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function plainJsonCodeUnits(value, ancestors = new Set()) {
|
|
107
|
+
if (value === null) return 4;
|
|
108
|
+
if (typeof value === "string") return jsonStringCodeUnits(value);
|
|
109
|
+
if (typeof value === "boolean") return value ? 4 : 5;
|
|
110
|
+
if (typeof value === "number") return Number.isFinite(value) ? String(value).length : 4;
|
|
111
|
+
if (typeof value !== "object") return null;
|
|
112
|
+
if (ancestors.has(value)) return null;
|
|
113
|
+
const prototype = Object.getPrototypeOf(value);
|
|
114
|
+
if (prototype !== Object.prototype && prototype !== null && !Array.isArray(value)) return null;
|
|
115
|
+
if (typeof value.toJSON === "function") return null;
|
|
116
|
+
ancestors.add(value);
|
|
117
|
+
let units = 2;
|
|
118
|
+
let count = 0;
|
|
119
|
+
if (Array.isArray(value)) {
|
|
120
|
+
for (const item of value) {
|
|
121
|
+
const itemUnits = plainJsonCodeUnits(item, ancestors);
|
|
122
|
+
units += (count > 0 ? 1 : 0) + (itemUnits === null ? 4 : itemUnits);
|
|
123
|
+
count += 1;
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
for (const key of Object.keys(value)) {
|
|
127
|
+
const item = value[key];
|
|
128
|
+
if (["undefined", "function", "symbol"].includes(typeof item)) continue;
|
|
129
|
+
const itemUnits = plainJsonCodeUnits(item, ancestors);
|
|
130
|
+
if (itemUnits === null) {
|
|
131
|
+
ancestors.delete(value);
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
units += (count > 0 ? 1 : 0) + jsonStringCodeUnits(key) + 1 + itemUnits;
|
|
135
|
+
count += 1;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
ancestors.delete(value);
|
|
139
|
+
return units;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function conservativeEventBytes(event) {
|
|
143
|
+
const codeUnits = plainJsonCodeUnits(event);
|
|
144
|
+
if (codeUnits !== null) return Math.max(1, codeUnits * 2);
|
|
69
145
|
try {
|
|
70
|
-
|
|
146
|
+
const serialized = JSON.stringify(event);
|
|
147
|
+
return typeof serialized === "string" ? Math.max(1, serialized.length * 2) : DEFAULT_STREAM_PENDING_BYTE_LIMIT;
|
|
71
148
|
} catch {
|
|
72
149
|
return DEFAULT_STREAM_PENDING_BYTE_LIMIT;
|
|
73
150
|
}
|
|
74
151
|
}
|
|
75
152
|
|
|
153
|
+
function eventByteAccounting(event, classification) {
|
|
154
|
+
const bytes = conservativeEventBytes(event);
|
|
155
|
+
const delta = event?.assistantMessageEvent?.delta;
|
|
156
|
+
if (!COALESCIBLE_DELTA_TYPES.has(classification?.subtype) || typeof delta !== "string") {
|
|
157
|
+
return { bytes, envelopeBytes: bytes, payloadBytes: 0, incrementalDelta: false };
|
|
158
|
+
}
|
|
159
|
+
const payloadBytes = Math.max(0, (jsonStringCodeUnits(delta) - 2) * 2);
|
|
160
|
+
return {
|
|
161
|
+
bytes,
|
|
162
|
+
envelopeBytes: Math.max(0, bytes - payloadBytes),
|
|
163
|
+
payloadBytes,
|
|
164
|
+
incrementalDelta: true,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
76
168
|
function eventDebugIndex(event) {
|
|
77
169
|
const value = Number(event?.isolationDeltaIndex ?? event?.streamIsolationIndex);
|
|
78
170
|
return Number.isSafeInteger(value) && value >= 0 ? value : null;
|
|
@@ -102,11 +194,9 @@ function mergedAdjacentEntry(previous, incoming) {
|
|
|
102
194
|
const previousKey = entryCoalesceKey(previous);
|
|
103
195
|
if (!previousKey || previousKey !== entryCoalesceKey(incoming)) return null;
|
|
104
196
|
if (incoming.classification.kind === "tool-execution") {
|
|
105
|
-
const event = incoming.event;
|
|
106
197
|
return {
|
|
107
198
|
...incoming,
|
|
108
|
-
|
|
109
|
-
bytes: eventByteSize(event),
|
|
199
|
+
receivedAt: previous.receivedAt !== undefined ? previous.receivedAt : incoming.receivedAt,
|
|
110
200
|
sourceCount: previous.sourceCount + incoming.sourceCount,
|
|
111
201
|
sourceIndexes: [...previous.sourceIndexes, ...incoming.sourceIndexes].slice(-DIAGNOSTIC_INDEX_LIMIT),
|
|
112
202
|
};
|
|
@@ -120,10 +210,13 @@ function mergedAdjacentEntry(previous, incoming) {
|
|
|
120
210
|
delta: `${typeof previousUpdate.delta === "string" ? previousUpdate.delta : ""}${typeof incomingUpdate.delta === "string" ? incomingUpdate.delta : ""}`,
|
|
121
211
|
},
|
|
122
212
|
};
|
|
213
|
+
const payloadBytes = previous.payloadBytes + incoming.payloadBytes;
|
|
123
214
|
return {
|
|
124
215
|
...incoming,
|
|
125
216
|
event,
|
|
126
|
-
|
|
217
|
+
receivedAt: previous.receivedAt !== undefined ? previous.receivedAt : incoming.receivedAt,
|
|
218
|
+
bytes: incoming.envelopeBytes + payloadBytes,
|
|
219
|
+
payloadBytes,
|
|
127
220
|
sourceCount: previous.sourceCount + incoming.sourceCount,
|
|
128
221
|
sourceIndexes: [...previous.sourceIndexes, ...incoming.sourceIndexes].slice(-DIAGNOSTIC_INDEX_LIMIT),
|
|
129
222
|
};
|
|
@@ -132,14 +225,16 @@ function mergedAdjacentEntry(previous, incoming) {
|
|
|
132
225
|
/**
|
|
133
226
|
* Owns the bounded frame queue for high-frequency transcript stream events.
|
|
134
227
|
* Adjacent compatible deltas are losslessly coalesced while event-kind and
|
|
135
|
-
* semantic barriers retain server order.
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
* is inert unless explicitly supplied by a test/debug caller.
|
|
228
|
+
* semantic barriers retain server order. First pressure uses one explicitly
|
|
229
|
+
* bounded urgent entry and a near-term task; repeated pressure, barriers, and
|
|
230
|
+
* oversize events retain synchronous lossless fallback behavior.
|
|
139
231
|
*/
|
|
140
232
|
export function createStreamOutputController({
|
|
141
233
|
scheduleFrame = defaultScheduleFrame,
|
|
142
234
|
cancelFrame = defaultCancelFrame,
|
|
235
|
+
schedulePressureDrain = defaultSchedulePressureDrain,
|
|
236
|
+
cancelPressureDrain = defaultCancelPressureDrain,
|
|
237
|
+
now = defaultNow,
|
|
143
238
|
isOwnerCurrent = () => true,
|
|
144
239
|
applyTextUpdate = () => {},
|
|
145
240
|
applyThinkingUpdate = () => {},
|
|
@@ -157,6 +252,10 @@ export function createStreamOutputController({
|
|
|
157
252
|
if (typeof scheduleFrame !== "function" || typeof cancelFrame !== "function") {
|
|
158
253
|
throw new TypeError("scheduleFrame and cancelFrame must be functions");
|
|
159
254
|
}
|
|
255
|
+
if (typeof schedulePressureDrain !== "function" || typeof cancelPressureDrain !== "function") {
|
|
256
|
+
throw new TypeError("schedulePressureDrain and cancelPressureDrain must be functions");
|
|
257
|
+
}
|
|
258
|
+
if (typeof now !== "function") throw new TypeError("now must be a function");
|
|
160
259
|
const entryLimit = positiveLimit(maxPendingEntries, DEFAULT_STREAM_PENDING_ENTRY_LIMIT, "maxPendingEntries");
|
|
161
260
|
const byteLimit = positiveLimit(maxPendingBytes, DEFAULT_STREAM_PENDING_BYTE_LIMIT, "maxPendingBytes");
|
|
162
261
|
const diagnosticsEnabled = typeof onDiagnostic === "function";
|
|
@@ -171,9 +270,13 @@ export function createStreamOutputController({
|
|
|
171
270
|
: () => {};
|
|
172
271
|
|
|
173
272
|
let frameHandle = null;
|
|
273
|
+
let pressureHandle = null;
|
|
174
274
|
let pending = [];
|
|
175
275
|
let pendingBytes = 0;
|
|
276
|
+
let urgentEntry = null;
|
|
176
277
|
|
|
278
|
+
const totalCount = () => pending.length + (urgentEntry ? 1 : 0);
|
|
279
|
+
const totalBytes = () => pendingBytes + (urgentEntry?.bytes || 0);
|
|
177
280
|
const ownerIsCurrent = (owner) => owner === undefined || isOwnerCurrent(owner) === true;
|
|
178
281
|
|
|
179
282
|
const reportStale = (event, owner, classification, phase) => {
|
|
@@ -220,68 +323,124 @@ export function createStreamOutputController({
|
|
|
220
323
|
return applied;
|
|
221
324
|
};
|
|
222
325
|
|
|
223
|
-
const
|
|
326
|
+
const cancelScheduledDrains = () => {
|
|
327
|
+
if (frameHandle !== null) cancelFrame(frameHandle);
|
|
328
|
+
if (pressureHandle !== null) cancelPressureDrain(pressureHandle);
|
|
224
329
|
frameHandle = null;
|
|
225
|
-
|
|
226
|
-
|
|
330
|
+
pressureHandle = null;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const drain = () => {
|
|
334
|
+
cancelScheduledDrains();
|
|
335
|
+
if (!pending.length && !urgentEntry) return 0;
|
|
336
|
+
const entries = urgentEntry ? [...pending, urgentEntry] : pending;
|
|
227
337
|
pending = [];
|
|
228
338
|
pendingBytes = 0;
|
|
339
|
+
urgentEntry = null;
|
|
340
|
+
const drainStart = diagnosticsEnabled ? now() : 0;
|
|
229
341
|
let applied = 0;
|
|
230
342
|
for (const entry of entries) {
|
|
231
343
|
if (applyEntry(entry)) applied += 1;
|
|
232
344
|
}
|
|
233
345
|
if (applied > 0) applyFollowScroll();
|
|
234
|
-
|
|
346
|
+
if (diagnosticsEnabled) {
|
|
347
|
+
let maxAgeMs = 0;
|
|
348
|
+
for (const entry of entries) {
|
|
349
|
+
if (typeof entry.receivedAt === "number") maxAgeMs = Math.max(maxAgeMs, drainStart - entry.receivedAt);
|
|
350
|
+
}
|
|
351
|
+
emitDiagnostic({
|
|
352
|
+
type: "batch",
|
|
353
|
+
entries: entries.length,
|
|
354
|
+
sourceCount: entries.reduce((total, entry) => total + entry.sourceCount, 0),
|
|
355
|
+
applied,
|
|
356
|
+
maxAgeMs,
|
|
357
|
+
drainMs: now() - drainStart,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
235
360
|
return applied;
|
|
236
361
|
};
|
|
237
362
|
|
|
238
363
|
const schedule = () => {
|
|
239
|
-
if (frameHandle !== null) return;
|
|
240
|
-
frameHandle = scheduleFrame(
|
|
364
|
+
if (frameHandle !== null || pressureHandle !== null) return;
|
|
365
|
+
frameHandle = scheduleFrame(() => {
|
|
366
|
+
frameHandle = null;
|
|
367
|
+
drain();
|
|
368
|
+
});
|
|
241
369
|
};
|
|
242
370
|
|
|
243
|
-
const
|
|
244
|
-
if (
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
371
|
+
const schedulePressure = () => {
|
|
372
|
+
if (pressureHandle !== null) return;
|
|
373
|
+
pressureHandle = schedulePressureDrain(() => {
|
|
374
|
+
pressureHandle = null;
|
|
375
|
+
drain();
|
|
376
|
+
});
|
|
249
377
|
};
|
|
250
378
|
|
|
379
|
+
const flush = () => drain();
|
|
380
|
+
|
|
251
381
|
const applyOversize = (entry) => {
|
|
382
|
+
const applyStart = diagnosticsEnabled ? now() : 0;
|
|
252
383
|
const applied = applyEntry(entry) ? 1 : 0;
|
|
253
384
|
if (applied) applyFollowScroll();
|
|
254
|
-
|
|
385
|
+
if (diagnosticsEnabled) {
|
|
386
|
+
const maxAgeMs = typeof entry.receivedAt === "number" ? Math.max(0, applyStart - entry.receivedAt) : 0;
|
|
387
|
+
emitDiagnostic({ type: "batch", entries: 1, sourceCount: entry.sourceCount, applied, direct: true, maxAgeMs, drainMs: now() - applyStart });
|
|
388
|
+
}
|
|
255
389
|
return applied;
|
|
256
390
|
};
|
|
257
391
|
|
|
392
|
+
const reportOverflow = (reason, entry) => {
|
|
393
|
+
const record = { reason, pendingCount: totalCount(), pendingBytes: totalBytes(), incomingBytes: entry.bytes };
|
|
394
|
+
onOverflow(record);
|
|
395
|
+
emitDiagnostic({ type: "overflow", ...record });
|
|
396
|
+
};
|
|
397
|
+
|
|
258
398
|
const enqueue = (entry) => {
|
|
259
|
-
const last = pending[pending.length - 1];
|
|
399
|
+
const last = urgentEntry || pending[pending.length - 1];
|
|
260
400
|
const merged = last ? mergedAdjacentEntry(last, entry) : null;
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
401
|
+
const mergedFits = urgentEntry
|
|
402
|
+
? merged?.bytes <= byteLimit
|
|
403
|
+
: merged && pendingBytes - last.bytes + merged.bytes <= byteLimit;
|
|
404
|
+
if (mergedFits) {
|
|
405
|
+
if (urgentEntry) urgentEntry = merged;
|
|
406
|
+
else {
|
|
407
|
+
pending[pending.length - 1] = merged;
|
|
408
|
+
pendingBytes = pendingBytes - last.bytes + merged.bytes;
|
|
409
|
+
}
|
|
410
|
+
emitDiagnostic({ type: "queued", coalesced: true, pendingCount: totalCount(), pendingBytes: totalBytes(), sourceCount: merged.sourceCount });
|
|
265
411
|
return;
|
|
266
412
|
}
|
|
267
413
|
|
|
268
|
-
if (
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
414
|
+
if (entry.bytes > byteLimit) {
|
|
415
|
+
if (totalCount()) {
|
|
416
|
+
emitDiagnostic({ type: "pressure", action: "synchronous-fallback", reason: "oversize-event", pendingCount: totalCount(), pendingBytes: totalBytes() });
|
|
417
|
+
flush();
|
|
418
|
+
}
|
|
419
|
+
reportOverflow("oversize-event", entry);
|
|
420
|
+
applyOversize(entry);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (urgentEntry) {
|
|
425
|
+
emitDiagnostic({ type: "pressure", action: "synchronous-fallback", reason: merged ? "urgent-merge-limit" : "urgent-slot-full", pendingCount: totalCount(), pendingBytes: totalBytes() });
|
|
272
426
|
flush();
|
|
427
|
+
enqueue(entry);
|
|
428
|
+
return;
|
|
273
429
|
}
|
|
274
430
|
|
|
275
|
-
if (entry.bytes > byteLimit) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
431
|
+
if (merged || pending.length >= entryLimit || pendingBytes + entry.bytes > byteLimit) {
|
|
432
|
+
const reason = merged ? "coalesced-byte-limit" : pending.length >= entryLimit ? "entry-limit" : "byte-limit";
|
|
433
|
+
reportOverflow(reason, entry);
|
|
434
|
+
urgentEntry = entry;
|
|
435
|
+
emitDiagnostic({ type: "pressure", action: "deferred", reason, pendingCount: totalCount(), pendingBytes: totalBytes() });
|
|
436
|
+
emitDiagnostic({ type: "queued", coalesced: false, urgent: true, pendingCount: totalCount(), pendingBytes: totalBytes(), sourceCount: entry.sourceCount });
|
|
437
|
+
schedulePressure();
|
|
279
438
|
return;
|
|
280
439
|
}
|
|
281
440
|
|
|
282
441
|
pending.push(entry);
|
|
283
442
|
pendingBytes += entry.bytes;
|
|
284
|
-
emitDiagnostic({ type: "queued", coalesced: false, pendingCount:
|
|
443
|
+
emitDiagnostic({ type: "queued", coalesced: false, pendingCount: totalCount(), pendingBytes: totalBytes(), sourceCount: entry.sourceCount });
|
|
285
444
|
};
|
|
286
445
|
|
|
287
446
|
return Object.freeze({
|
|
@@ -294,49 +453,61 @@ export function createStreamOutputController({
|
|
|
294
453
|
reportStale(event, owner, classification, "dispatch");
|
|
295
454
|
return true;
|
|
296
455
|
}
|
|
456
|
+
const accounting = eventByteAccounting(event, classification);
|
|
297
457
|
const entry = {
|
|
298
458
|
event,
|
|
299
459
|
classification,
|
|
300
460
|
owner,
|
|
301
|
-
|
|
461
|
+
receivedAt: diagnosticsEnabled ? now() : undefined,
|
|
462
|
+
...accounting,
|
|
302
463
|
sourceCount: 1,
|
|
303
464
|
sourceIndexes: diagnosticsEnabled && index !== null ? [index] : [],
|
|
304
465
|
};
|
|
305
466
|
enqueue(entry);
|
|
306
467
|
if (classification.barrier) {
|
|
307
|
-
emitDiagnostic({ type: "barrier", reason: classification.subtype, pendingCount:
|
|
468
|
+
emitDiagnostic({ type: "barrier", reason: classification.subtype, pendingCount: totalCount(), pendingBytes: totalBytes() });
|
|
308
469
|
flush();
|
|
309
|
-
} else if (
|
|
470
|
+
} else if (totalCount()) {
|
|
310
471
|
schedule();
|
|
311
472
|
}
|
|
312
473
|
return true;
|
|
313
474
|
},
|
|
314
475
|
flush,
|
|
315
476
|
barrier(reason = "semantic") {
|
|
316
|
-
emitDiagnostic({ type: "barrier", reason: String(reason || "semantic"), pendingCount:
|
|
477
|
+
emitDiagnostic({ type: "barrier", reason: String(reason || "semantic"), pendingCount: totalCount(), pendingBytes: totalBytes() });
|
|
317
478
|
return flush();
|
|
318
479
|
},
|
|
319
480
|
cancel(owner) {
|
|
320
|
-
if (owner === undefined)
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
481
|
+
if (owner === undefined) {
|
|
482
|
+
pending = [];
|
|
483
|
+
urgentEntry = null;
|
|
484
|
+
} else {
|
|
485
|
+
pending = pending.filter((entry) => entry.owner !== owner);
|
|
486
|
+
if (urgentEntry?.owner === owner) urgentEntry = null;
|
|
326
487
|
}
|
|
327
|
-
|
|
488
|
+
pendingBytes = pending.reduce((total, entry) => total + entry.bytes, 0);
|
|
489
|
+
if (!totalCount()) cancelScheduledDrains();
|
|
490
|
+
emitDiagnostic({ type: "cancel", owner, pendingCount: totalCount(), pendingBytes: totalBytes() });
|
|
328
491
|
},
|
|
329
492
|
pendingCount() {
|
|
330
|
-
return
|
|
493
|
+
return totalCount();
|
|
331
494
|
},
|
|
332
495
|
pendingBytes() {
|
|
333
|
-
return
|
|
496
|
+
return totalBytes();
|
|
334
497
|
},
|
|
335
498
|
limits() {
|
|
336
|
-
return Object.freeze({
|
|
499
|
+
return Object.freeze({
|
|
500
|
+
maxPendingEntries: entryLimit,
|
|
501
|
+
maxPendingBytes: byteLimit,
|
|
502
|
+
maxUrgentEntries: URGENT_PRESSURE_ENTRY_LIMIT,
|
|
503
|
+
maxUrgentBytes: byteLimit,
|
|
504
|
+
});
|
|
337
505
|
},
|
|
338
506
|
hasScheduledFrame() {
|
|
339
507
|
return frameHandle !== null;
|
|
340
508
|
},
|
|
509
|
+
hasScheduledPressureDrain() {
|
|
510
|
+
return pressureHandle !== null;
|
|
511
|
+
},
|
|
341
512
|
});
|
|
342
513
|
}
|