dsh-code 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +21 -13
- package/README.md +21 -13
- package/lib/index.mjs +1156 -720
- package/lib/types/app.d.ts +2 -0
- package/lib/types/editor-keys.d.ts +105 -0
- package/lib/types/git-workflow.d.ts +6 -2
- package/lib/types/model-capabilities.d.ts +82 -0
- package/lib/types/provider-settings.d.ts +7 -0
- package/lib/types/render/lines.d.ts +25 -0
- package/lib/types/render/markdown.d.ts +1 -1
- package/lib/types/render/projection.d.ts +15 -1
- package/lib/types/render/text.d.ts +15 -9
- package/lib/types/render/width.d.ts +29 -0
- package/lib/types/session-directory.d.ts +27 -0
- package/lib/types/settings-file.d.ts +33 -0
- package/lib/types/store.d.ts +10 -0
- package/lib/types/subagents.d.ts +13 -3
- package/package.json +159 -159
- package/src/app.ts +1104 -1041
- package/src/editor-keys.ts +371 -0
- package/src/git-workflow.ts +10 -6
- package/src/index.ts +1637 -1523
- package/src/model-capabilities.ts +318 -0
- package/src/provider-settings.ts +16 -0
- package/src/render/lines.ts +403 -356
- package/src/render/markdown.ts +4 -7
- package/src/render/projection.ts +63 -40
- package/src/render/text.ts +152 -150
- package/src/render/width.ts +189 -0
- package/src/session-directory.ts +56 -0
- package/src/settings-file.ts +56 -0
- package/src/store.ts +26 -7
- package/src/subagents.ts +39 -6
package/src/render/lines.ts
CHANGED
|
@@ -1,356 +1,403 @@
|
|
|
1
|
-
/** Width-safe styled physical rows for bounded terminal panels. */
|
|
2
|
-
|
|
3
|
-
import { promptDisplayText, type TranscriptEntry } from './projection.ts'
|
|
4
|
-
import type { ToolDetail } from './tool-detail.ts'
|
|
5
|
-
import { renderMarkdown, visibleColumns, type MdStyle } from './markdown.ts'
|
|
6
|
-
import { formatTokens } from './status.ts'
|
|
7
|
-
import { displayText, truncateColumns } from './text.ts'
|
|
8
|
-
|
|
9
|
-
/** Presentation classes mapped to Ink colors by the app boundary. */
|
|
10
|
-
export type LineStyle = MdStyle | 'brand' | 'success' | 'error' | 'warn' | 'dimItalic'
|
|
11
|
-
|
|
12
|
-
/** One styled run within a physical terminal row. */
|
|
13
|
-
export interface StyledSegment {
|
|
14
|
-
text: string
|
|
15
|
-
style: LineStyle
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** One row guaranteed not to exceed the requested terminal width. */
|
|
19
|
-
export interface StyledLine {
|
|
20
|
-
segments: readonly StyledSegment[]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** Construct one segment without leaking mutable objects into cached rows. */
|
|
24
|
-
export function lineSegment(text: string, style: LineStyle = 'plain'): StyledSegment {
|
|
25
|
-
return { text, style }
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Append a character while merging adjacent runs with the same style. */
|
|
29
|
-
function appendSegment(target: StyledSegment[], text: string, style: LineStyle): void {
|
|
30
|
-
const previous = target[target.length - 1]
|
|
31
|
-
if (previous?.style === style) {
|
|
32
|
-
target[target.length - 1] = { text: previous.text + text, style }
|
|
33
|
-
return
|
|
34
|
-
}
|
|
35
|
-
target.push({ text, style })
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Sanitize and hard-wrap styled content into exact physical rows.
|
|
40
|
-
* Tabs become two visible spaces because terminal tab stops are contextual
|
|
41
|
-
* and therefore cannot participate in a deterministic row budget.
|
|
42
|
-
*/
|
|
43
|
-
export function styledLines(segments: readonly StyledSegment[], columns: number): readonly StyledLine[] {
|
|
44
|
-
const width = Math.max(1, Math.floor(columns))
|
|
45
|
-
const lines: StyledLine[] = []
|
|
46
|
-
let current: StyledSegment[] = []
|
|
47
|
-
let used = 0
|
|
48
|
-
const flush = (): void => {
|
|
49
|
-
lines.push({ segments: current })
|
|
50
|
-
current = []
|
|
51
|
-
used = 0
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
for (const segment of segments) {
|
|
55
|
-
const safe = displayText(segment.text).replaceAll('\t', ' ').replaceAll('\r', '')
|
|
56
|
-
for (const char of safe) {
|
|
57
|
-
if (char === '\n') {
|
|
58
|
-
flush()
|
|
59
|
-
continue
|
|
60
|
-
}
|
|
61
|
-
const cells = visibleColumns(char)
|
|
62
|
-
if (used > 0 && used + cells > width) flush()
|
|
63
|
-
appendSegment(current, char, segment.style)
|
|
64
|
-
used += cells
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
if (current.length > 0 || lines.length === 0) flush()
|
|
68
|
-
return lines
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Plain/dim text convenience over {@link styledLines}. */
|
|
72
|
-
export function textLines(text: string, columns: number, style: LineStyle = 'plain'): readonly StyledLine[] {
|
|
73
|
-
return styledLines([lineSegment(text, style)], columns)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** Prefix every wrapped physical row without exceeding the column budget. */
|
|
77
|
-
function prefixedStyledLines(segments: readonly StyledSegment[], columns: number, prefix: string, prefixStyle: LineStyle = 'plain'): readonly StyledLine[] {
|
|
78
|
-
const width = Math.max(1, Math.floor(columns))
|
|
79
|
-
const prefixWidth = Math.min(width, visibleColumns(prefix))
|
|
80
|
-
const bodyWidth = Math.max(1, width - prefixWidth)
|
|
81
|
-
return styledLines(segments, bodyWidth).map(line => ({
|
|
82
|
-
segments: [lineSegment(prefix, prefixStyle), ...line.segments],
|
|
83
|
-
}))
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Text convenience for a tool row whose continuation must keep its gutter. */
|
|
87
|
-
function prefixedTextLines(text: string, columns: number, prefix: string, style: LineStyle = 'plain'): readonly StyledLine[] {
|
|
88
|
-
return prefixedStyledLines([lineSegment(text, style)], columns, prefix, style)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Wrap styled segments with a hanging indent: the first physical row carries
|
|
93
|
-
* `firstPrefix` (often a marker plus gutter) and every wrapped continuation
|
|
94
|
-
* carries the narrower `contPrefix`, so long tool summaries and prompts
|
|
95
|
-
* align under their card instead of falling back to column zero. The first
|
|
96
|
-
* row may hold one prefix-width more than the continuations.
|
|
97
|
-
*/
|
|
98
|
-
function hangingStyledLines(
|
|
99
|
-
segments: readonly StyledSegment[],
|
|
100
|
-
columns: number,
|
|
101
|
-
firstPrefix: string,
|
|
102
|
-
firstStyle: LineStyle,
|
|
103
|
-
contPrefix: string,
|
|
104
|
-
contStyle: LineStyle = firstStyle,
|
|
105
|
-
): readonly StyledLine[] {
|
|
106
|
-
const width = Math.max(2, Math.floor(columns))
|
|
107
|
-
const firstPrefixText = truncateColumns(firstPrefix, Math.max(1, width - 1))
|
|
108
|
-
const contPrefixText = truncateColumns(contPrefix, Math.max(1, width - 1))
|
|
109
|
-
const firstBudget = Math.max(1, width - visibleColumns(firstPrefixText))
|
|
110
|
-
const contBudget = Math.max(1, width - visibleColumns(contPrefixText))
|
|
111
|
-
const lines: StyledLine[] = []
|
|
112
|
-
let current: StyledSegment[] = []
|
|
113
|
-
let used = 0
|
|
114
|
-
let budget = firstBudget
|
|
115
|
-
const flush = (): void => {
|
|
116
|
-
lines.push({ segments: current })
|
|
117
|
-
current = []
|
|
118
|
-
used = 0
|
|
119
|
-
budget = contBudget
|
|
120
|
-
}
|
|
121
|
-
for (const segment of segments) {
|
|
122
|
-
const safe = displayText(segment.text).replaceAll('\t', ' ').replaceAll('\r', '')
|
|
123
|
-
for (const char of safe) {
|
|
124
|
-
if (char === '\n') {
|
|
125
|
-
flush()
|
|
126
|
-
continue
|
|
127
|
-
}
|
|
128
|
-
const cells = visibleColumns(char)
|
|
129
|
-
if (used > 0 && used + cells > budget) flush()
|
|
130
|
-
appendSegment(current, char, segment.style)
|
|
131
|
-
used += cells
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
if (current.length > 0 || lines.length === 0) flush()
|
|
135
|
-
return lines.map((line, index) => ({
|
|
136
|
-
segments: [
|
|
137
|
-
lineSegment(index === 0 ? firstPrefixText : contPrefixText, index === 0 ? firstStyle : contStyle),
|
|
138
|
-
...line.segments,
|
|
139
|
-
],
|
|
140
|
-
}))
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/** Plain-text convenience over {@link hangingStyledLines}. */
|
|
144
|
-
function hangingTextLines(
|
|
145
|
-
text: string,
|
|
146
|
-
columns: number,
|
|
147
|
-
firstPrefix: string,
|
|
148
|
-
firstStyle: LineStyle = 'plain',
|
|
149
|
-
contPrefix = ' ',
|
|
150
|
-
contStyle: LineStyle = firstStyle,
|
|
151
|
-
): readonly StyledLine[] {
|
|
152
|
-
return hangingStyledLines([lineSegment(text, firstStyle)], columns, firstPrefix, firstStyle, contPrefix, contStyle)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Markdown rows re-hardened so a single long word cannot escape the budget. */
|
|
156
|
-
export function markdownLines(text: string, columns: number): readonly StyledLine[] {
|
|
157
|
-
const width = Math.max(1, Math.floor(columns))
|
|
158
|
-
const parsed = renderMarkdown(displayText(text), Math.max(10, width))
|
|
159
|
-
return parsed.flatMap(line => styledLines(
|
|
160
|
-
line.segments.map(segment => lineSegment(segment.text, segment.style)),
|
|
161
|
-
width,
|
|
162
|
-
))
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Codex-style reasoning rows: the marker occupies the reply gutter and every
|
|
167
|
-
* wrapped or explicit continuation starts with the same two-column indent, so
|
|
168
|
-
* reasoning content and assistant Markdown share one left edge.
|
|
169
|
-
*/
|
|
170
|
-
export function reasoningLines(text: string, columns: number): readonly StyledLine[] {
|
|
171
|
-
const width = Math.max(1, Math.floor(columns))
|
|
172
|
-
if (width < 3) return textLines(text, width, 'dimItalic')
|
|
173
|
-
const contentWidth = width - 2
|
|
174
|
-
const content = displayText(text).replaceAll('\t', ' ').replaceAll('\r', '')
|
|
175
|
-
.split('\n')
|
|
176
|
-
.flatMap(line => styledLines([lineSegment(line, 'dimItalic')], contentWidth))
|
|
177
|
-
return content.map((line, index) => ({
|
|
178
|
-
segments: [
|
|
179
|
-
lineSegment(index === 0 ? '✻ ' : ' ', 'dimItalic'),
|
|
180
|
-
...line.segments,
|
|
181
|
-
],
|
|
182
|
-
}))
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/** Expanded structured tool detail as scrollable, width-safe rows. */
|
|
186
|
-
/**
|
|
187
|
-
* Detail rows share the tool card's four-column hanging gutter: the summary
|
|
188
|
-
* (⎿) and delegation prompt (└) continuations already sit at four columns, so
|
|
189
|
-
* diff/read/web/raw rows align under them instead of floating two columns
|
|
190
|
-
* shallower.
|
|
191
|
-
*/
|
|
192
|
-
function toolDetailLines(detail: ToolDetail, columns: number): readonly StyledLine[] {
|
|
193
|
-
switch (detail.kind) {
|
|
194
|
-
case 'diff':
|
|
195
|
-
return detail.diffs.flatMap(diff => [
|
|
196
|
-
...prefixedTextLines(`${diff.path}${diff.truncated ? ' (diff truncated)' : ''}`, columns, ' ── ', 'dim'),
|
|
197
|
-
...diff.lines.flatMap(line => prefixedTextLines(
|
|
198
|
-
`${line.mark}${line.text}`,
|
|
199
|
-
columns,
|
|
200
|
-
' ',
|
|
201
|
-
line.mark === '+' ? 'success' : line.mark === '-' ? 'error' : 'dim',
|
|
202
|
-
)),
|
|
203
|
-
])
|
|
204
|
-
case 'read':
|
|
205
|
-
return [
|
|
206
|
-
...prefixedTextLines(
|
|
207
|
-
`${detail.path} · lines ${detail.offset}-${detail.lines.length > 0 ? detail.lines[detail.lines.length - 1]!.number : detail.offset - 1} of ${detail.totalLines}${detail.truncated ? ' (window truncated)' : ''}`,
|
|
208
|
-
columns,
|
|
209
|
-
' ── ',
|
|
210
|
-
'dim',
|
|
211
|
-
),
|
|
212
|
-
...detail.lines.flatMap(line => prefixedTextLines(`${String(line.number).padStart(5, ' ')} | ${line.text}`, columns, ' ', 'dim')),
|
|
213
|
-
]
|
|
214
|
-
case 'web-search':
|
|
215
|
-
return [
|
|
216
|
-
...detail.sources.flatMap(source => [
|
|
217
|
-
...prefixedStyledLines([
|
|
218
|
-
lineSegment(source.title ?? source.url, 'brand'),
|
|
219
|
-
lineSegment(` - ${source.url}`, 'dim'),
|
|
220
|
-
], columns, ' ? '),
|
|
221
|
-
...(source.snippet === '' ? [] : prefixedTextLines(source.snippet, columns, ' ', 'dim')),
|
|
222
|
-
]),
|
|
223
|
-
...prefixedTextLines(`${detail.sources.length} sources${detail.truncated ? ' (capped)' : ''}`, columns, ' ', 'dim'),
|
|
224
|
-
]
|
|
225
|
-
case 'web-fetch':
|
|
226
|
-
return prefixedTextLines(`${detail.url} · HTTP ${detail.statusCode}`, columns, ' ', 'dim')
|
|
227
|
-
case 'raw':
|
|
228
|
-
return [
|
|
229
|
-
...prefixedTextLines(detail.text, columns, ' ', 'dim'),
|
|
230
|
-
...prefixedTextLines(detail.truncated ? '… (output truncated)' : '(end of output)', columns, ' ', 'dim'),
|
|
231
|
-
]
|
|
232
|
-
default: {
|
|
233
|
-
const exhaustive: never = detail
|
|
234
|
-
return exhaustive
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/** Default compact tool-card window used while the Ctrl+R fold is closed. */
|
|
240
|
-
const DEFAULT_TOOL_ROWS = 3
|
|
241
|
-
|
|
242
|
-
/** Keep the invocation visible while making hidden tool output discoverable. */
|
|
243
|
-
function compactToolLines(lines: readonly StyledLine[], columns: number): readonly StyledLine[] {
|
|
244
|
-
if (lines.length <= DEFAULT_TOOL_ROWS) return lines
|
|
245
|
-
return [
|
|
246
|
-
...lines.slice(0, DEFAULT_TOOL_ROWS - 1),
|
|
247
|
-
...textLines(' … output hidden · Ctrl+R', columns, 'dim').slice(0, 1),
|
|
248
|
-
]
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Convert one durable transcript entry to its complete scrollable row model.
|
|
253
|
-
* The source entry stays intact; only the caller's visible slice is rendered.
|
|
254
|
-
* Wrapped continuations keep a hanging indent aligned under each row's
|
|
255
|
-
* content (Codex history-cell alignment) instead of resetting to column 0.
|
|
256
|
-
*/
|
|
257
|
-
export function transcriptEntryLines(
|
|
258
|
-
entry: TranscriptEntry,
|
|
259
|
-
columns: number,
|
|
260
|
-
showReasoning = true,
|
|
261
|
-
reasoningToggleHint = true,
|
|
262
|
-
showToolDetails = showReasoning,
|
|
263
|
-
): readonly StyledLine[] {
|
|
264
|
-
const width = Math.max(1, Math.floor(columns))
|
|
265
|
-
switch (entry.kind) {
|
|
266
|
-
case 'user':
|
|
267
|
-
return entry.notice
|
|
268
|
-
? hangingStyledLines([lineSegment(promptDisplayText(entry), 'dim')], width, '⤷ ', 'dim', ' ', 'dim')
|
|
269
|
-
: hangingStyledLines([lineSegment(promptDisplayText(entry), 'plain')], width, '❯ ', 'brand', ' ', 'plain')
|
|
270
|
-
case 'pending':
|
|
271
|
-
// Codex PendingSteer: a queued prompt renders exactly like an ordinary
|
|
272
|
-
// user row, so the durable user/message retires it without any flicker.
|
|
273
|
-
return hangingStyledLines([lineSegment(promptDisplayText(entry), 'plain')], width, '❯ ', 'brand', ' ', 'plain')
|
|
274
|
-
case 'assistant': {
|
|
275
|
-
const reasoning = entry.reasoning === ''
|
|
276
|
-
? []
|
|
277
|
-
: showReasoning
|
|
278
|
-
? reasoningLines(entry.reasoning, width)
|
|
279
|
-
: textLines(`✻ Thinking (${entry.reasoning.length} chars${reasoningToggleHint ? ', Ctrl+R to expand' : ''})`, width, 'dim')
|
|
280
|
-
// Every reply row carries the composer's two-column gutter, so reply
|
|
281
|
-
// text aligns with the input cursor (Codex LIVE_PREFIX alignment); the
|
|
282
|
-
// wrap budget shrinks by the same amount so no line double-wraps.
|
|
283
|
-
const body = markdownLines(entry.text, Math.max(10, width - 2))
|
|
284
|
-
.map(line => ({ segments: [{ text: ' ', style: 'plain' as const }, ...line.segments] }))
|
|
285
|
-
// A cancelled stream's delivered prefix settles as this entry; one
|
|
286
|
-
// bounded dim marker row distinguishes it from a completed reply.
|
|
287
|
-
const interrupted = entry.interrupted === true ? textLines(' ⏹ interrupted', width, 'dim') : []
|
|
288
|
-
return [...reasoning, ...body, ...interrupted]
|
|
289
|
-
}
|
|
290
|
-
case 'tool': {
|
|
291
|
-
const mark = entry.state === 'running' ? '●' : entry.state === 'error' ? '⨯' : '⏺'
|
|
292
|
-
const markStyle: LineStyle = entry.state === 'running' ? 'brand' : entry.state === 'error' ? 'error' : 'success'
|
|
293
|
-
const summaryStyle: LineStyle = entry.state === 'error' ? 'error' : 'dim'
|
|
294
|
-
const lines = [
|
|
295
|
-
// The invocation row hangs wrapped previews under the call badge.
|
|
296
|
-
...hangingStyledLines([
|
|
297
|
-
// Global call ordinal — the same number an error line references.
|
|
298
|
-
lineSegment(`[${entry.ordinal}] `, 'dim'),
|
|
299
|
-
lineSegment(entry.name, 'brand'),
|
|
300
|
-
lineSegment(entry.preview === '' ? '' : ` ${entry.preview}`, 'dim'),
|
|
301
|
-
], width, `${mark} `, markStyle, ' ', 'plain'),
|
|
302
|
-
// A delegation card carries what the child was asked (Codex's
|
|
303
|
-
// SpawnAgent prompt preview) while it runs, before any result.
|
|
304
|
-
...(entry.prompt === '' ? [] : hangingTextLines(entry.prompt, width, ' └ ', 'dim', ' ')),
|
|
305
|
-
...(entry.summary === '' ? [] : hangingTextLines(
|
|
306
|
-
entry.state === 'error' ? `call ${entry.ordinal}: ${entry.summary}` : entry.summary,
|
|
307
|
-
width, ' ⎿ ', summaryStyle, ' ', summaryStyle,
|
|
308
|
-
)),
|
|
309
|
-
...(entry.detail === undefined ? [] : toolDetailLines(entry.detail, width)),
|
|
310
|
-
]
|
|
311
|
-
return showToolDetails ? lines : compactToolLines(lines, width)
|
|
312
|
-
}
|
|
313
|
-
case 'command': {
|
|
314
|
-
const mark = entry.state === 'running' ? '●' : entry.state === 'error' ? '⨯' : '⏺'
|
|
315
|
-
const markStyle: LineStyle = entry.state === 'running' ? 'brand' : entry.state === 'error' ? 'error' : 'success'
|
|
316
|
-
const summaryStyle: LineStyle = entry.state === 'error' ? 'error' : 'dim'
|
|
317
|
-
return [
|
|
318
|
-
...hangingStyledLines([
|
|
319
|
-
lineSegment(`/${entry.name}`, 'brand'),
|
|
320
|
-
lineSegment(entry.args === '' ? '' : ` ${entry.args}`, 'dim'),
|
|
321
|
-
], width, `${mark} `, markStyle, ' ', 'plain'),
|
|
322
|
-
...(entry.summary === '' ? [] : hangingTextLines(entry.summary, width, ' ⎿ ', summaryStyle, ' ', summaryStyle)),
|
|
323
|
-
]
|
|
324
|
-
}
|
|
325
|
-
case 'turn-marker':
|
|
326
|
-
return textLines(` ⏹ ${entry.text}`, width, 'dim')
|
|
327
|
-
case 'compaction':
|
|
328
|
-
return textLines(entry.ok
|
|
329
|
-
? ` ⧉ compacted ~${formatTokens(entry.tokens)} tokens`
|
|
330
|
-
: ` ⧉ compaction failed: ${entry.error}`, width, 'dim')
|
|
331
|
-
case 'retry':
|
|
332
|
-
return textLines(
|
|
333
|
-
` ↻ retry ${entry.attempt}/${entry.max} · ${entry.code} · ${Math.round(entry.delayMs / 100) / 10}s`,
|
|
334
|
-
width,
|
|
335
|
-
entry.state === 'running' ? 'warn' : 'dim',
|
|
336
|
-
)
|
|
337
|
-
case 'files':
|
|
338
|
-
return entry.paths.length === 0
|
|
339
|
-
? textLines(' ⎄ no changed files', width, 'dim')
|
|
340
|
-
: [
|
|
341
|
-
...textLines(` ⎄ ${entry.paths.length} changed file${entry.paths.length === 1 ? '' : 's'}`, width, 'dim'),
|
|
342
|
-
...entry.paths.flatMap(path => hangingTextLines(path, width, ' ', 'dim', ' ')),
|
|
343
|
-
]
|
|
344
|
-
case 'error':
|
|
345
|
-
return textLines(entry.text, width, 'error')
|
|
346
|
-
default: {
|
|
347
|
-
const exhaustive: never = entry
|
|
348
|
-
return exhaustive
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/** Settled-history variant carrying the Ctrl+R reasoning fold. */
|
|
354
|
-
export function settledEntryLines(entry: TranscriptEntry, columns: number, showReasoning: boolean): readonly StyledLine[] {
|
|
355
|
-
return transcriptEntryLines(entry, columns, showReasoning, false, showReasoning)
|
|
356
|
-
}
|
|
1
|
+
/** Width-safe styled physical rows for bounded terminal panels. */
|
|
2
|
+
|
|
3
|
+
import { promptDisplayText, type TranscriptEntry } from './projection.ts'
|
|
4
|
+
import type { ToolDetail } from './tool-detail.ts'
|
|
5
|
+
import { renderMarkdown, visibleColumns, type MdStyle } from './markdown.ts'
|
|
6
|
+
import { formatTokens } from './status.ts'
|
|
7
|
+
import { displayText, truncateColumns } from './text.ts'
|
|
8
|
+
|
|
9
|
+
/** Presentation classes mapped to Ink colors by the app boundary. */
|
|
10
|
+
export type LineStyle = MdStyle | 'brand' | 'success' | 'error' | 'warn' | 'dimItalic'
|
|
11
|
+
|
|
12
|
+
/** One styled run within a physical terminal row. */
|
|
13
|
+
export interface StyledSegment {
|
|
14
|
+
text: string
|
|
15
|
+
style: LineStyle
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** One row guaranteed not to exceed the requested terminal width. */
|
|
19
|
+
export interface StyledLine {
|
|
20
|
+
segments: readonly StyledSegment[]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Construct one segment without leaking mutable objects into cached rows. */
|
|
24
|
+
export function lineSegment(text: string, style: LineStyle = 'plain'): StyledSegment {
|
|
25
|
+
return { text, style }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Append a character while merging adjacent runs with the same style. */
|
|
29
|
+
function appendSegment(target: StyledSegment[], text: string, style: LineStyle): void {
|
|
30
|
+
const previous = target[target.length - 1]
|
|
31
|
+
if (previous?.style === style) {
|
|
32
|
+
target[target.length - 1] = { text: previous.text + text, style }
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
target.push({ text, style })
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Sanitize and hard-wrap styled content into exact physical rows.
|
|
40
|
+
* Tabs become two visible spaces because terminal tab stops are contextual
|
|
41
|
+
* and therefore cannot participate in a deterministic row budget.
|
|
42
|
+
*/
|
|
43
|
+
export function styledLines(segments: readonly StyledSegment[], columns: number): readonly StyledLine[] {
|
|
44
|
+
const width = Math.max(1, Math.floor(columns))
|
|
45
|
+
const lines: StyledLine[] = []
|
|
46
|
+
let current: StyledSegment[] = []
|
|
47
|
+
let used = 0
|
|
48
|
+
const flush = (): void => {
|
|
49
|
+
lines.push({ segments: current })
|
|
50
|
+
current = []
|
|
51
|
+
used = 0
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
for (const segment of segments) {
|
|
55
|
+
const safe = displayText(segment.text).replaceAll('\t', ' ').replaceAll('\r', '')
|
|
56
|
+
for (const char of safe) {
|
|
57
|
+
if (char === '\n') {
|
|
58
|
+
flush()
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
const cells = visibleColumns(char)
|
|
62
|
+
if (used > 0 && used + cells > width) flush()
|
|
63
|
+
appendSegment(current, char, segment.style)
|
|
64
|
+
used += cells
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (current.length > 0 || lines.length === 0) flush()
|
|
68
|
+
return lines
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Plain/dim text convenience over {@link styledLines}. */
|
|
72
|
+
export function textLines(text: string, columns: number, style: LineStyle = 'plain'): readonly StyledLine[] {
|
|
73
|
+
return styledLines([lineSegment(text, style)], columns)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Prefix every wrapped physical row without exceeding the column budget. */
|
|
77
|
+
function prefixedStyledLines(segments: readonly StyledSegment[], columns: number, prefix: string, prefixStyle: LineStyle = 'plain'): readonly StyledLine[] {
|
|
78
|
+
const width = Math.max(1, Math.floor(columns))
|
|
79
|
+
const prefixWidth = Math.min(width, visibleColumns(prefix))
|
|
80
|
+
const bodyWidth = Math.max(1, width - prefixWidth)
|
|
81
|
+
return styledLines(segments, bodyWidth).map(line => ({
|
|
82
|
+
segments: [lineSegment(prefix, prefixStyle), ...line.segments],
|
|
83
|
+
}))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Text convenience for a tool row whose continuation must keep its gutter. */
|
|
87
|
+
function prefixedTextLines(text: string, columns: number, prefix: string, style: LineStyle = 'plain'): readonly StyledLine[] {
|
|
88
|
+
return prefixedStyledLines([lineSegment(text, style)], columns, prefix, style)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Wrap styled segments with a hanging indent: the first physical row carries
|
|
93
|
+
* `firstPrefix` (often a marker plus gutter) and every wrapped continuation
|
|
94
|
+
* carries the narrower `contPrefix`, so long tool summaries and prompts
|
|
95
|
+
* align under their card instead of falling back to column zero. The first
|
|
96
|
+
* row may hold one prefix-width more than the continuations.
|
|
97
|
+
*/
|
|
98
|
+
function hangingStyledLines(
|
|
99
|
+
segments: readonly StyledSegment[],
|
|
100
|
+
columns: number,
|
|
101
|
+
firstPrefix: string,
|
|
102
|
+
firstStyle: LineStyle,
|
|
103
|
+
contPrefix: string,
|
|
104
|
+
contStyle: LineStyle = firstStyle,
|
|
105
|
+
): readonly StyledLine[] {
|
|
106
|
+
const width = Math.max(2, Math.floor(columns))
|
|
107
|
+
const firstPrefixText = truncateColumns(firstPrefix, Math.max(1, width - 1))
|
|
108
|
+
const contPrefixText = truncateColumns(contPrefix, Math.max(1, width - 1))
|
|
109
|
+
const firstBudget = Math.max(1, width - visibleColumns(firstPrefixText))
|
|
110
|
+
const contBudget = Math.max(1, width - visibleColumns(contPrefixText))
|
|
111
|
+
const lines: StyledLine[] = []
|
|
112
|
+
let current: StyledSegment[] = []
|
|
113
|
+
let used = 0
|
|
114
|
+
let budget = firstBudget
|
|
115
|
+
const flush = (): void => {
|
|
116
|
+
lines.push({ segments: current })
|
|
117
|
+
current = []
|
|
118
|
+
used = 0
|
|
119
|
+
budget = contBudget
|
|
120
|
+
}
|
|
121
|
+
for (const segment of segments) {
|
|
122
|
+
const safe = displayText(segment.text).replaceAll('\t', ' ').replaceAll('\r', '')
|
|
123
|
+
for (const char of safe) {
|
|
124
|
+
if (char === '\n') {
|
|
125
|
+
flush()
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
const cells = visibleColumns(char)
|
|
129
|
+
if (used > 0 && used + cells > budget) flush()
|
|
130
|
+
appendSegment(current, char, segment.style)
|
|
131
|
+
used += cells
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (current.length > 0 || lines.length === 0) flush()
|
|
135
|
+
return lines.map((line, index) => ({
|
|
136
|
+
segments: [
|
|
137
|
+
lineSegment(index === 0 ? firstPrefixText : contPrefixText, index === 0 ? firstStyle : contStyle),
|
|
138
|
+
...line.segments,
|
|
139
|
+
],
|
|
140
|
+
}))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Plain-text convenience over {@link hangingStyledLines}. */
|
|
144
|
+
function hangingTextLines(
|
|
145
|
+
text: string,
|
|
146
|
+
columns: number,
|
|
147
|
+
firstPrefix: string,
|
|
148
|
+
firstStyle: LineStyle = 'plain',
|
|
149
|
+
contPrefix = ' ',
|
|
150
|
+
contStyle: LineStyle = firstStyle,
|
|
151
|
+
): readonly StyledLine[] {
|
|
152
|
+
return hangingStyledLines([lineSegment(text, firstStyle)], columns, firstPrefix, firstStyle, contPrefix, contStyle)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Markdown rows re-hardened so a single long word cannot escape the budget. */
|
|
156
|
+
export function markdownLines(text: string, columns: number): readonly StyledLine[] {
|
|
157
|
+
const width = Math.max(1, Math.floor(columns))
|
|
158
|
+
const parsed = renderMarkdown(displayText(text), Math.max(10, width))
|
|
159
|
+
return parsed.flatMap(line => styledLines(
|
|
160
|
+
line.segments.map(segment => lineSegment(segment.text, segment.style)),
|
|
161
|
+
width,
|
|
162
|
+
))
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Codex-style reasoning rows: the marker occupies the reply gutter and every
|
|
167
|
+
* wrapped or explicit continuation starts with the same two-column indent, so
|
|
168
|
+
* reasoning content and assistant Markdown share one left edge.
|
|
169
|
+
*/
|
|
170
|
+
export function reasoningLines(text: string, columns: number): readonly StyledLine[] {
|
|
171
|
+
const width = Math.max(1, Math.floor(columns))
|
|
172
|
+
if (width < 3) return textLines(text, width, 'dimItalic')
|
|
173
|
+
const contentWidth = width - 2
|
|
174
|
+
const content = displayText(text).replaceAll('\t', ' ').replaceAll('\r', '')
|
|
175
|
+
.split('\n')
|
|
176
|
+
.flatMap(line => styledLines([lineSegment(line, 'dimItalic')], contentWidth))
|
|
177
|
+
return content.map((line, index) => ({
|
|
178
|
+
segments: [
|
|
179
|
+
lineSegment(index === 0 ? '✻ ' : ' ', 'dimItalic'),
|
|
180
|
+
...line.segments,
|
|
181
|
+
],
|
|
182
|
+
}))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Expanded structured tool detail as scrollable, width-safe rows. */
|
|
186
|
+
/**
|
|
187
|
+
* Detail rows share the tool card's four-column hanging gutter: the summary
|
|
188
|
+
* (⎿) and delegation prompt (└) continuations already sit at four columns, so
|
|
189
|
+
* diff/read/web/raw rows align under them instead of floating two columns
|
|
190
|
+
* shallower.
|
|
191
|
+
*/
|
|
192
|
+
function toolDetailLines(detail: ToolDetail, columns: number): readonly StyledLine[] {
|
|
193
|
+
switch (detail.kind) {
|
|
194
|
+
case 'diff':
|
|
195
|
+
return detail.diffs.flatMap(diff => [
|
|
196
|
+
...prefixedTextLines(`${diff.path}${diff.truncated ? ' (diff truncated)' : ''}`, columns, ' ── ', 'dim'),
|
|
197
|
+
...diff.lines.flatMap(line => prefixedTextLines(
|
|
198
|
+
`${line.mark}${line.text}`,
|
|
199
|
+
columns,
|
|
200
|
+
' ',
|
|
201
|
+
line.mark === '+' ? 'success' : line.mark === '-' ? 'error' : 'dim',
|
|
202
|
+
)),
|
|
203
|
+
])
|
|
204
|
+
case 'read':
|
|
205
|
+
return [
|
|
206
|
+
...prefixedTextLines(
|
|
207
|
+
`${detail.path} · lines ${detail.offset}-${detail.lines.length > 0 ? detail.lines[detail.lines.length - 1]!.number : detail.offset - 1} of ${detail.totalLines}${detail.truncated ? ' (window truncated)' : ''}`,
|
|
208
|
+
columns,
|
|
209
|
+
' ── ',
|
|
210
|
+
'dim',
|
|
211
|
+
),
|
|
212
|
+
...detail.lines.flatMap(line => prefixedTextLines(`${String(line.number).padStart(5, ' ')} | ${line.text}`, columns, ' ', 'dim')),
|
|
213
|
+
]
|
|
214
|
+
case 'web-search':
|
|
215
|
+
return [
|
|
216
|
+
...detail.sources.flatMap(source => [
|
|
217
|
+
...prefixedStyledLines([
|
|
218
|
+
lineSegment(source.title ?? source.url, 'brand'),
|
|
219
|
+
lineSegment(` - ${source.url}`, 'dim'),
|
|
220
|
+
], columns, ' ? '),
|
|
221
|
+
...(source.snippet === '' ? [] : prefixedTextLines(source.snippet, columns, ' ', 'dim')),
|
|
222
|
+
]),
|
|
223
|
+
...prefixedTextLines(`${detail.sources.length} sources${detail.truncated ? ' (capped)' : ''}`, columns, ' ', 'dim'),
|
|
224
|
+
]
|
|
225
|
+
case 'web-fetch':
|
|
226
|
+
return prefixedTextLines(`${detail.url} · HTTP ${detail.statusCode}`, columns, ' ', 'dim')
|
|
227
|
+
case 'raw':
|
|
228
|
+
return [
|
|
229
|
+
...prefixedTextLines(detail.text, columns, ' ', 'dim'),
|
|
230
|
+
...prefixedTextLines(detail.truncated ? '… (output truncated)' : '(end of output)', columns, ' ', 'dim'),
|
|
231
|
+
]
|
|
232
|
+
default: {
|
|
233
|
+
const exhaustive: never = detail
|
|
234
|
+
return exhaustive
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Default compact tool-card window used while the Ctrl+R fold is closed. */
|
|
240
|
+
const DEFAULT_TOOL_ROWS = 3
|
|
241
|
+
|
|
242
|
+
/** Keep the invocation visible while making hidden tool output discoverable. */
|
|
243
|
+
function compactToolLines(lines: readonly StyledLine[], columns: number): readonly StyledLine[] {
|
|
244
|
+
if (lines.length <= DEFAULT_TOOL_ROWS) return lines
|
|
245
|
+
return [
|
|
246
|
+
...lines.slice(0, DEFAULT_TOOL_ROWS - 1),
|
|
247
|
+
...textLines(' … output hidden · Ctrl/Alt+R', columns, 'dim').slice(0, 1),
|
|
248
|
+
]
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Convert one durable transcript entry to its complete scrollable row model.
|
|
253
|
+
* The source entry stays intact; only the caller's visible slice is rendered.
|
|
254
|
+
* Wrapped continuations keep a hanging indent aligned under each row's
|
|
255
|
+
* content (Codex history-cell alignment) instead of resetting to column 0.
|
|
256
|
+
*/
|
|
257
|
+
export function transcriptEntryLines(
|
|
258
|
+
entry: TranscriptEntry,
|
|
259
|
+
columns: number,
|
|
260
|
+
showReasoning = true,
|
|
261
|
+
reasoningToggleHint = true,
|
|
262
|
+
showToolDetails = showReasoning,
|
|
263
|
+
): readonly StyledLine[] {
|
|
264
|
+
const width = Math.max(1, Math.floor(columns))
|
|
265
|
+
switch (entry.kind) {
|
|
266
|
+
case 'user':
|
|
267
|
+
return entry.notice
|
|
268
|
+
? hangingStyledLines([lineSegment(promptDisplayText(entry), 'dim')], width, '⤷ ', 'dim', ' ', 'dim')
|
|
269
|
+
: hangingStyledLines([lineSegment(promptDisplayText(entry), 'plain')], width, '❯ ', 'brand', ' ', 'plain')
|
|
270
|
+
case 'pending':
|
|
271
|
+
// Codex PendingSteer: a queued prompt renders exactly like an ordinary
|
|
272
|
+
// user row, so the durable user/message retires it without any flicker.
|
|
273
|
+
return hangingStyledLines([lineSegment(promptDisplayText(entry), 'plain')], width, '❯ ', 'brand', ' ', 'plain')
|
|
274
|
+
case 'assistant': {
|
|
275
|
+
const reasoning = entry.reasoning === ''
|
|
276
|
+
? []
|
|
277
|
+
: showReasoning
|
|
278
|
+
? reasoningLines(entry.reasoning, width)
|
|
279
|
+
: textLines(`✻ Thinking (${entry.reasoning.length} chars${reasoningToggleHint ? ', Ctrl/Alt+R to expand' : ''})`, width, 'dim')
|
|
280
|
+
// Every reply row carries the composer's two-column gutter, so reply
|
|
281
|
+
// text aligns with the input cursor (Codex LIVE_PREFIX alignment); the
|
|
282
|
+
// wrap budget shrinks by the same amount so no line double-wraps.
|
|
283
|
+
const body = markdownLines(entry.text, Math.max(10, width - 2))
|
|
284
|
+
.map(line => ({ segments: [{ text: ' ', style: 'plain' as const }, ...line.segments] }))
|
|
285
|
+
// A cancelled stream's delivered prefix settles as this entry; one
|
|
286
|
+
// bounded dim marker row distinguishes it from a completed reply.
|
|
287
|
+
const interrupted = entry.interrupted === true ? textLines(' ⏹ interrupted', width, 'dim') : []
|
|
288
|
+
return [...reasoning, ...body, ...interrupted]
|
|
289
|
+
}
|
|
290
|
+
case 'tool': {
|
|
291
|
+
const mark = entry.state === 'running' ? '●' : entry.state === 'error' ? '⨯' : '⏺'
|
|
292
|
+
const markStyle: LineStyle = entry.state === 'running' ? 'brand' : entry.state === 'error' ? 'error' : 'success'
|
|
293
|
+
const summaryStyle: LineStyle = entry.state === 'error' ? 'error' : 'dim'
|
|
294
|
+
const lines = [
|
|
295
|
+
// The invocation row hangs wrapped previews under the call badge.
|
|
296
|
+
...hangingStyledLines([
|
|
297
|
+
// Global call ordinal — the same number an error line references.
|
|
298
|
+
lineSegment(`[${entry.ordinal}] `, 'dim'),
|
|
299
|
+
lineSegment(entry.name, 'brand'),
|
|
300
|
+
lineSegment(entry.preview === '' ? '' : ` ${entry.preview}`, 'dim'),
|
|
301
|
+
], width, `${mark} `, markStyle, ' ', 'plain'),
|
|
302
|
+
// A delegation card carries what the child was asked (Codex's
|
|
303
|
+
// SpawnAgent prompt preview) while it runs, before any result.
|
|
304
|
+
...(entry.prompt === '' ? [] : hangingTextLines(entry.prompt, width, ' └ ', 'dim', ' ')),
|
|
305
|
+
...(entry.summary === '' ? [] : hangingTextLines(
|
|
306
|
+
entry.state === 'error' ? `call ${entry.ordinal}: ${entry.summary}` : entry.summary,
|
|
307
|
+
width, ' ⎿ ', summaryStyle, ' ', summaryStyle,
|
|
308
|
+
)),
|
|
309
|
+
...(entry.detail === undefined ? [] : toolDetailLines(entry.detail, width)),
|
|
310
|
+
]
|
|
311
|
+
return showToolDetails ? lines : compactToolLines(lines, width)
|
|
312
|
+
}
|
|
313
|
+
case 'command': {
|
|
314
|
+
const mark = entry.state === 'running' ? '●' : entry.state === 'error' ? '⨯' : '⏺'
|
|
315
|
+
const markStyle: LineStyle = entry.state === 'running' ? 'brand' : entry.state === 'error' ? 'error' : 'success'
|
|
316
|
+
const summaryStyle: LineStyle = entry.state === 'error' ? 'error' : 'dim'
|
|
317
|
+
return [
|
|
318
|
+
...hangingStyledLines([
|
|
319
|
+
lineSegment(`/${entry.name}`, 'brand'),
|
|
320
|
+
lineSegment(entry.args === '' ? '' : ` ${entry.args}`, 'dim'),
|
|
321
|
+
], width, `${mark} `, markStyle, ' ', 'plain'),
|
|
322
|
+
...(entry.summary === '' ? [] : hangingTextLines(entry.summary, width, ' ⎿ ', summaryStyle, ' ', summaryStyle)),
|
|
323
|
+
]
|
|
324
|
+
}
|
|
325
|
+
case 'turn-marker':
|
|
326
|
+
return textLines(` ⏹ ${entry.text}`, width, 'dim')
|
|
327
|
+
case 'compaction':
|
|
328
|
+
return textLines(entry.ok
|
|
329
|
+
? ` ⧉ compacted ~${formatTokens(entry.tokens)} tokens`
|
|
330
|
+
: ` ⧉ compaction failed: ${entry.error}`, width, 'dim')
|
|
331
|
+
case 'retry':
|
|
332
|
+
return textLines(
|
|
333
|
+
` ↻ retry ${entry.attempt}/${entry.max} · ${entry.code} · ${Math.round(entry.delayMs / 100) / 10}s`,
|
|
334
|
+
width,
|
|
335
|
+
entry.state === 'running' ? 'warn' : 'dim',
|
|
336
|
+
)
|
|
337
|
+
case 'files':
|
|
338
|
+
return entry.paths.length === 0
|
|
339
|
+
? textLines(' ⎄ no changed files', width, 'dim')
|
|
340
|
+
: [
|
|
341
|
+
...textLines(` ⎄ ${entry.paths.length} changed file${entry.paths.length === 1 ? '' : 's'}`, width, 'dim'),
|
|
342
|
+
...entry.paths.flatMap(path => hangingTextLines(path, width, ' ', 'dim', ' ')),
|
|
343
|
+
]
|
|
344
|
+
case 'error':
|
|
345
|
+
return textLines(entry.text, width, 'error')
|
|
346
|
+
default: {
|
|
347
|
+
const exhaustive: never = entry
|
|
348
|
+
return exhaustive
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Settled-history variant carrying the Ctrl+R reasoning fold. */
|
|
354
|
+
export function settledEntryLines(entry: TranscriptEntry, columns: number, showReasoning: boolean): readonly StyledLine[] {
|
|
355
|
+
return transcriptEntryLines(entry, columns, showReasoning, false, showReasoning)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** The flexible rows of the live region; chrome (composer/notice/status) is never reduced. */
|
|
359
|
+
export interface LiveAllocation {
|
|
360
|
+
/** Settled tail rows currently rendered in the live tree. */
|
|
361
|
+
readonly live: number
|
|
362
|
+
/** Rows reserved for the streaming reasoning tail or its marker. */
|
|
363
|
+
readonly reasoning: number
|
|
364
|
+
/** Rows reserved for the streaming answer tail. */
|
|
365
|
+
readonly answer: number
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** A clamped allocation plus the invariant-trip warning that triggered it. */
|
|
369
|
+
export interface LiveAllocationAudit {
|
|
370
|
+
readonly allocation: LiveAllocation
|
|
371
|
+
readonly warning?: string
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Clamp the live-region allocation so the flexible dynamic rows never exceed
|
|
376
|
+
* the post-chrome budget. By construction the caller derives these rows from
|
|
377
|
+
* the same budget; this is the runtime tripwire for a future edit that breaks
|
|
378
|
+
* that derivation. Reduction order: answer first (the freshest content is the
|
|
379
|
+
* live tail), then reasoning, then settled live rows; nothing goes negative.
|
|
380
|
+
* @param allocation - the intended row allocation.
|
|
381
|
+
* @param dynamicRows - the post-chrome row budget.
|
|
382
|
+
* @returns the clamped allocation and a warning string when clamping fired.
|
|
383
|
+
*/
|
|
384
|
+
export function clampLiveAllocation(allocation: LiveAllocation, dynamicRows: number): LiveAllocationAudit {
|
|
385
|
+
const live = Math.max(0, Math.floor(allocation.live))
|
|
386
|
+
const reasoning = Math.max(0, Math.floor(allocation.reasoning))
|
|
387
|
+
const answer = Math.max(0, Math.floor(allocation.answer))
|
|
388
|
+
const budget = Math.max(0, Math.floor(dynamicRows))
|
|
389
|
+
let excess = live + reasoning + answer - budget
|
|
390
|
+
if (excess <= 0) return { allocation: { live, reasoning, answer } }
|
|
391
|
+
const take = (from: number): number => {
|
|
392
|
+
const cut = Math.min(from, excess)
|
|
393
|
+
excess -= cut
|
|
394
|
+
return from - cut
|
|
395
|
+
}
|
|
396
|
+
const clampedAnswer = take(answer)
|
|
397
|
+
const clampedReasoning = excess > 0 ? take(reasoning) : reasoning
|
|
398
|
+
const clampedLive = excess > 0 ? take(live) : live
|
|
399
|
+
return {
|
|
400
|
+
allocation: { live: clampedLive, reasoning: clampedReasoning, answer: clampedAnswer },
|
|
401
|
+
warning: `live rows ${live} + ${reasoning} + ${answer} exceed the dynamic budget ${budget}; clamped to ${clampedLive}/${clampedReasoning}/${clampedAnswer}`,
|
|
402
|
+
}
|
|
403
|
+
}
|