@waq666/pneuma 0.7.0 → 0.7.1
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 +206 -145
- package/lib/app/App.d.ts +3 -1
- package/lib/app/App.js +324 -172
- package/lib/app/App.js.nv-backup +1650 -0
- package/lib/app/ChatView.d.ts +40 -9
- package/lib/app/ChatView.js +150 -38
- package/lib/app/StatusBar.js +21 -92
- package/lib/app/TimelineRail.d.ts +22 -0
- package/lib/app/TimelineRail.js +160 -0
- package/lib/app/TimelineRail.js.negbak +98 -0
- package/lib/app/TimelineRail.js.nv-backup +160 -0
- package/lib/app/ToolCard.d.ts +7 -1
- package/lib/app/ToolCard.js +30 -6
- package/lib/app/ToolCard.js.nv-backup +64 -0
- package/lib/app/TranscriptOverlay.d.ts +23 -8
- package/lib/app/TranscriptOverlay.js +29 -10
- package/lib/app/TranscriptViewport.d.ts +78 -0
- package/lib/app/TranscriptViewport.js +218 -0
- package/lib/app/Welcome.js +13 -8
- package/lib/app/click.d.ts +15 -2
- package/lib/app/click.js +20 -6
- package/lib/app/keymap.d.ts +56 -0
- package/lib/app/keymap.js +56 -0
- package/lib/app/mouse.d.ts +33 -0
- package/lib/app/mouse.js +35 -0
- package/lib/app/pointer.d.ts +16 -0
- package/lib/app/pointer.js +60 -0
- package/lib/app/scroll-model.d.ts +240 -0
- package/lib/app/scroll-model.js +347 -0
- package/lib/app/snapshot-subscribe.d.ts +34 -0
- package/lib/app/snapshot-subscribe.js +110 -0
- package/lib/app/theme.d.ts +29 -1
- package/lib/app/theme.js +46 -1
- package/lib/app/ticker.d.ts +7 -0
- package/lib/app/ticker.js +53 -0
- package/lib/core/llm.d.ts +11 -0
- package/lib/core/llm.js +19 -2
- package/lib/plugins/subagent.js +36 -2
- package/lib/plugins/tool-search.js +11 -2
- package/lib/plugins/tool-search.js.nv-backup +434 -0
- package/package.json +1 -1
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 指针位置(SGR 1003 的运动事件)——只服务右侧刻度列的悬停。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单开一个模块:运动事件是**高频**的(鼠标划过屏幕每秒几十上百条)。要是把位置
|
|
5
|
+
* 存进 App 的 state,每动一下就是一次全树重渲染;这里只发布「指针在右侧那 RAIL_WIDTH 列
|
|
6
|
+
* 上、在第几行」这一个事实,而且**值没变就不通知**。订户只有刻度列那一个小部件,
|
|
7
|
+
* 指针在别处时它们一次渲染都不会有。
|
|
8
|
+
*
|
|
9
|
+
* 没有「离开」事件可听(SGR 不发),所以离开靠**列判定**自然清掉;终端尺寸变了也照此
|
|
10
|
+
* 重新判定(列数由调用方每次传入,不在这里缓存)。
|
|
11
|
+
*
|
|
12
|
+
* @module dsc-tui/app/pointer
|
|
13
|
+
*/
|
|
14
|
+
import { useSyncExternalStore } from 'react';
|
|
15
|
+
import { PAD } from './theme.js';
|
|
16
|
+
import { RAIL_WIDTH } from './scroll-model.js';
|
|
17
|
+
/** 指针在刻度列上时的终端行号(1 基,SGR 原样);不在那几列上 = null。 */
|
|
18
|
+
let gutterRow = null;
|
|
19
|
+
const listeners = new Set();
|
|
20
|
+
/**
|
|
21
|
+
* 指针是不是落在右侧刻度列上。
|
|
22
|
+
*
|
|
23
|
+
* 列要算准:帧有 `PAD.page` 列页边距,刻度列是**内容列的最后 RAIL_WIDTH 列**——直接拿
|
|
24
|
+
* 终端右缘(`columns - RAIL_WIDTH`)判会整体偏两列,指针压在刻度上也不认(第一次实现
|
|
25
|
+
* 就是这么错的,走查里点不亮)。
|
|
26
|
+
*/
|
|
27
|
+
export function onGutter(col, columns) {
|
|
28
|
+
return col - 1 >= columns - PAD.page - RAIL_WIDTH;
|
|
29
|
+
}
|
|
30
|
+
/** 记一次指针位置(运动事件每来一条调一次)。 */
|
|
31
|
+
export function publishPointer(col, row, columns) {
|
|
32
|
+
const next = onGutter(col, columns) ? row : null;
|
|
33
|
+
if (next === gutterRow)
|
|
34
|
+
return;
|
|
35
|
+
gutterRow = next;
|
|
36
|
+
for (const listener of [...listeners])
|
|
37
|
+
listener();
|
|
38
|
+
}
|
|
39
|
+
/** 清掉指针(失焦/退出这类没有 leave 事件的场合由调用方兜底)。 */
|
|
40
|
+
export function clearPointer() {
|
|
41
|
+
if (gutterRow === null)
|
|
42
|
+
return;
|
|
43
|
+
gutterRow = null;
|
|
44
|
+
for (const listener of [...listeners])
|
|
45
|
+
listener();
|
|
46
|
+
}
|
|
47
|
+
const subscribe = (listener) => {
|
|
48
|
+
listeners.add(listener);
|
|
49
|
+
return () => {
|
|
50
|
+
listeners.delete(listener);
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
/** 指针在刻度列上时的终端行号(1 基);不在 = null。 */
|
|
54
|
+
export function useGutterPointer() {
|
|
55
|
+
return useSyncExternalStore(subscribe, () => gutterRow);
|
|
56
|
+
}
|
|
57
|
+
/** 测试注入口:直接读当前值(探针不挂 React 也能验通知语义)。 */
|
|
58
|
+
export function readPointer() {
|
|
59
|
+
return gutterRow;
|
|
60
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 转录视口的**行滚动模型**(纯函数 + 行高表,不碰 React)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么以「行」为单位:条数是坏的坐标——一条派活 prompt 可以比一屏还高,按条滚永远
|
|
5
|
+
* 滚不进它(用户报的「点进子代理看不到工作过程」正是这个)。ink 是按行裁切越界文本的
|
|
6
|
+
* (`render-node-to-output.js` 的 clip + `output.js` 的 from/to 行切),所以「尾锚定 +
|
|
7
|
+
* 内容盒负 marginBottom」就等于逐行滚动,连折行都跟着走,不用自己算 CJK 折行。
|
|
8
|
+
*
|
|
9
|
+
* 视口 = [上占位 spacer] + [窗口内的条目] + [下占位 spacer],内容总高恰好等于估算出的
|
|
10
|
+
* 总行数;于是「从尾部往上退 N 行」这一个数字就是全部滚动状态(0 = 跟尾)。窗口只装
|
|
11
|
+
* 视口附近 ± overscan 行,所以会话再长、渲染量也是有界的。
|
|
12
|
+
*
|
|
13
|
+
* 行高表:settled 条目不可变 → 量到的行数可以永久缓存;live 尾每帧重测。没量过的条目
|
|
14
|
+
* 用估算(按显示宽度数折行),窗口滑到哪里就量到哪里,估算逐步被真值替换。
|
|
15
|
+
*
|
|
16
|
+
* @module dsc-tui/app/scroll-model
|
|
17
|
+
*/
|
|
18
|
+
import type { TranscriptEntry } from '../contract.js';
|
|
19
|
+
/** 窗口上下各多渲染几行:滚动时不留白,也不至于把整篇都拉进来。 */
|
|
20
|
+
export declare const OVERSCAN_LINES = 8;
|
|
21
|
+
/** 行高表:条目 id → 显示行数。宽度变了(刻度栏出现/消失)就整体作废。 */
|
|
22
|
+
export declare class HeightCache {
|
|
23
|
+
private table;
|
|
24
|
+
private width;
|
|
25
|
+
/** 换宽度(含首次)就清表:折行变了,旧行数全是错的。 */
|
|
26
|
+
useWidth(width: number): void;
|
|
27
|
+
get(id: number): number | undefined;
|
|
28
|
+
set(id: number, lines: number): void;
|
|
29
|
+
get size(): number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 没量过的条目按**折叠态实际画出来的行数**估:一件卡片的估算误差不是小数字——它直接
|
|
33
|
+
* 变成「跳过的那段内容有多高」,跳过段的高度又决定视口顶落在内容坐标的哪一行。
|
|
34
|
+
*
|
|
35
|
+
* 曾经的口径(按最长字段数折行、卡片再补框架行)在工具卡上错得离谱:结果原文十四行,
|
|
36
|
+
* 卡片折叠态其实只画**一行**(「⚙ 调用 xx 工具 · 状态」,参数与结果要点开才给)。误差
|
|
37
|
+
* 14 行/张,一屏十几张就是两百行——滚轮往上滚时这些误差被真实测量逐条替换,内容坐标
|
|
38
|
+
* 整个缩水,于是「前几格滚轮不动、再滚一下突然弹回底部」(用户报的连滚回跳),长会话
|
|
39
|
+
* 还会因为跳过段虚高把视口顶推出正文(整片空白)。
|
|
40
|
+
*
|
|
41
|
+
* 所以这里逐类照着渲染器数行(只数结构行,文本按显示宽度折行),宁可与真实差一两行,
|
|
42
|
+
* 也不要差十几行。`text` 仍是「按原文折行」的近似——markdown 的块间距与代码围栏行
|
|
43
|
+
* 一增一减,量级上互相抵消(真值一旦被量到就永久进表,估算只管没滚到过的那段)。
|
|
44
|
+
*/
|
|
45
|
+
export declare function estimateLines(entry: TranscriptEntry, width: number, options?: {
|
|
46
|
+
expandThinking?: boolean;
|
|
47
|
+
}): number;
|
|
48
|
+
/** 一行里的最长文本(估算用;不认识的就是空串)。 */
|
|
49
|
+
/** 一轮(= 一条非内部用户消息)在内容坐标里的位置。 */
|
|
50
|
+
export interface TurnMark {
|
|
51
|
+
/** 条目 id。 */
|
|
52
|
+
id: number;
|
|
53
|
+
/** 它在全量 transcript 里的下标(跳转时用来选窗口)。 */
|
|
54
|
+
index: number;
|
|
55
|
+
/** 内容行坐标(这一条的第一行)。 */
|
|
56
|
+
top: number;
|
|
57
|
+
}
|
|
58
|
+
/** 视口布局:窗口切到哪、占位多高、滚动量多少。 */
|
|
59
|
+
export interface ViewportLayout {
|
|
60
|
+
/** 窗口切片下标([start, end))。 */
|
|
61
|
+
start: number;
|
|
62
|
+
end: number;
|
|
63
|
+
/** 窗口上方跳过的行数(上占位 spacer 的高度)。 */
|
|
64
|
+
aboveLines: number;
|
|
65
|
+
/** 窗口下方跳过的行数(下占位 spacer 的高度)。 */
|
|
66
|
+
belowLines: number;
|
|
67
|
+
/** 每一条的行数(量到真值用真值,没量过是估算)——锚点换算要用同一份。 */
|
|
68
|
+
rows: number[];
|
|
69
|
+
/** 内容总行数(估算/实测混合)。 */
|
|
70
|
+
totalLines: number;
|
|
71
|
+
/** 可上退的最大行数(0 = 一屏装得下,不需要滚)。 */
|
|
72
|
+
maxShift: number;
|
|
73
|
+
/** 夹好的滚动量:从内容尾部往上退多少行;0 = 跟尾。 */
|
|
74
|
+
shift: number;
|
|
75
|
+
/** 解析出来的视口顶内容行号(null = 跟尾)。 */
|
|
76
|
+
top: number | null;
|
|
77
|
+
/** 全量 transcript 里的每一轮(刻度栏与跳转用;不在窗口里的也有)。 */
|
|
78
|
+
turns: TurnMark[];
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 视口顶的**内容锚点**:钉在某一条的第几行上,而不是「内容第几行」这个绝对行号。
|
|
82
|
+
*
|
|
83
|
+
* 为什么必须这样(用户报的「连滚不动 + 突然弹回底部」的根):「第几行」是**估算坐标系**
|
|
84
|
+
* 里的坐标——从没滚到过的那一段(跳过段)只有估算值,滚过去量到真值就会整体缩水或长高,
|
|
85
|
+
* 同一个行号指向的内容就换了。表现出来就是:滚轮前几格画面不动(行号在缩水的坐标里
|
|
86
|
+
* 追不上),再滚一下落到别处。钉住「哪一条的第几行」就没有这个问题:跳过段怎么变,
|
|
87
|
+
* 视口顶那一条都还在原地——因为**被跳过的那一段本来就不在画面里**,量它量到多少都不该
|
|
88
|
+
* 动摇视线。同一条内部的偏移是稳的(进了窗口的条目都量过真值)。
|
|
89
|
+
*/
|
|
90
|
+
export interface ScrollAnchor {
|
|
91
|
+
/** 条目 id(`TranscriptEntry.id`,含流头部那条伪条目的哨兵 id)。 */
|
|
92
|
+
id: number;
|
|
93
|
+
/** 该条在数组里的下标(命中失效时按 id 重新找)。 */
|
|
94
|
+
index: number;
|
|
95
|
+
/** 视口顶落在这一条内部的第几行(0 基)。 */
|
|
96
|
+
offset: number;
|
|
97
|
+
}
|
|
98
|
+
/** 行号 → 锚点:落在那一条的第几行。到(或超过)尾部就是「跟尾」(null)。 */
|
|
99
|
+
export declare function anchorAtRow(rows: readonly number[], entries: readonly TranscriptEntry[], row: number, maxTop: number): ScrollAnchor | null;
|
|
100
|
+
/** 锚点 → 行号(内容坐标)。`null`(跟尾)给 `totalLines`,调用方自己夹。 */
|
|
101
|
+
export declare function rowOfAnchor(rows: readonly number[], entries: readonly TranscriptEntry[], anchor: ScrollAnchor): number;
|
|
102
|
+
/** 估算要跟着渲染口径走的那点开关(目前只有「思考块是不是展开的」)。 */
|
|
103
|
+
export interface EstimateOptions {
|
|
104
|
+
/** 思考块当前是不是展开的(展开态比折叠态高得多,估算必须跟着变)。 */
|
|
105
|
+
expandThinking?: boolean;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 算出视口布局。
|
|
109
|
+
*
|
|
110
|
+
* @param entries - 全量条目(顺序即时间序)。
|
|
111
|
+
* @param heights - 行高表(含已量到的真值)。
|
|
112
|
+
* @param options - 内容宽度、视口行数、视口顶锚点(null = 跟尾)。
|
|
113
|
+
*/
|
|
114
|
+
export declare function layoutViewport(entries: TranscriptEntry[], heights: HeightCache, options: {
|
|
115
|
+
width: number;
|
|
116
|
+
viewportLines: number;
|
|
117
|
+
anchor: ScrollAnchor | null;
|
|
118
|
+
} & EstimateOptions): ViewportLayout;
|
|
119
|
+
/** 跳到某一轮时该用的滚动量:让这一轮的题面正好落在视口顶上。 */
|
|
120
|
+
export declare function shiftForTurn(turn: TurnMark, layout: Pick<ViewportLayout, 'totalLines' | 'maxShift'>, viewportLines: number): number;
|
|
121
|
+
/** 当前视口顶上那一轮(= 拥有视口首行的那一轮;视口在内容头部时算第 0 轮)。 */
|
|
122
|
+
export declare function activeTurnIndex(turns: TurnMark[], shift: number, layout: Pick<ViewportLayout, 'totalLines'>, viewportLines: number): number;
|
|
123
|
+
/** 刻度栏占的列数(dsh 同款 2 列:`━━` / ` ─` 对齐)。 */
|
|
124
|
+
export declare const RAIL_WIDTH = 2;
|
|
125
|
+
/** 窄于此不画(再窄正文就挤没了)。 */
|
|
126
|
+
export declare const RAIL_MIN_COLUMNS = 60;
|
|
127
|
+
/** 至少两轮才值得画。 */
|
|
128
|
+
export declare const RAIL_MIN_TURNS = 2;
|
|
129
|
+
/**
|
|
130
|
+
* 刻度栏的字形(照 dsh-TUI 的 TimelineRail 搬):当前轮 `━━` 重线、其余 ` ─` 短线
|
|
131
|
+
* (占满 2 列、右对齐,不靠测量对齐)、箭头 `▴`/`▾`。
|
|
132
|
+
*/
|
|
133
|
+
export declare const RAIL_GLYPH: {
|
|
134
|
+
readonly active: "━━";
|
|
135
|
+
readonly idle: " ─";
|
|
136
|
+
readonly up: " ▴";
|
|
137
|
+
readonly down: " ▾";
|
|
138
|
+
};
|
|
139
|
+
/** 画不画右侧那一列(轮数刻度)。 */
|
|
140
|
+
export declare function gutterEligible(options: {
|
|
141
|
+
columns: number;
|
|
142
|
+
viewportLines: number;
|
|
143
|
+
contentLines: number;
|
|
144
|
+
}): boolean;
|
|
145
|
+
/** 画不画**轮数刻度**(还要 ≥2 轮:一轮的刻度没有可跳的目标,整列就不画)。 */
|
|
146
|
+
export declare function railEligible(options: {
|
|
147
|
+
columns: number;
|
|
148
|
+
turnCount: number;
|
|
149
|
+
viewportLines: number;
|
|
150
|
+
contentLines: number;
|
|
151
|
+
}): boolean;
|
|
152
|
+
/** 刻度栏的一行:一轮的刻度、上/下箭头(`enabled` = 有目标可跳),或空行。 */
|
|
153
|
+
export type RailRow = {
|
|
154
|
+
kind: 'tick';
|
|
155
|
+
turn: number;
|
|
156
|
+
active: boolean;
|
|
157
|
+
} | {
|
|
158
|
+
kind: 'up';
|
|
159
|
+
enabled: boolean;
|
|
160
|
+
} | {
|
|
161
|
+
kind: 'down';
|
|
162
|
+
enabled: boolean;
|
|
163
|
+
};
|
|
164
|
+
export interface RailGeometry {
|
|
165
|
+
/** 每一行(0 基,从上到下)画什么;null = 空行。 */
|
|
166
|
+
rows: Array<RailRow | null>;
|
|
167
|
+
/** 「上一轮」跳转目标(轮下标;null = 上面没有了)。 */
|
|
168
|
+
up: number | null;
|
|
169
|
+
/** 「下一轮」跳转目标。 */
|
|
170
|
+
down: number | null;
|
|
171
|
+
/** ▲ 画在第几行(块内 0 基;-1 = 这次没有刻度栏)。 */
|
|
172
|
+
upRow: number;
|
|
173
|
+
/** ▼ 画在第几行。 */
|
|
174
|
+
downRow: number;
|
|
175
|
+
/** 第一格刻度画在第几行。 */
|
|
176
|
+
tickTop: number;
|
|
177
|
+
/** 窗口里的轮区间 [windowStart, windowEnd)。 */
|
|
178
|
+
windowStart: number;
|
|
179
|
+
windowEnd: number;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* 上/下一轮的跳转目标(刻度栏箭头与 `[`/`]` 键**共用这一条**,两边不可能对不上)。
|
|
183
|
+
*
|
|
184
|
+
* 两条 dsh 定的规矩:
|
|
185
|
+
* - ▲ 先对齐本轮的题面:读到本轮中段(视口顶在本轮题面之下)时,▲ 指的就是**本轮**
|
|
186
|
+
* ——再按一次才退到上一轮。少了这条,读到长回答中段按 ▲ 会直接跳过本轮的题面。
|
|
187
|
+
* - 名下的那一轮必须真能滚到视口顶(顶行号 ≤ 能滚到的最大顶行号):够不着的那一轮
|
|
188
|
+
* 点下去没反应,箭头却亮着,按几次都不动。够不着就不给目标(暗箭头 = 保证的空操作)。
|
|
189
|
+
*
|
|
190
|
+
* @param options - 轮数、当前轮、视口顶与最大顶行号(内容坐标)、每轮顶行号(可选)。
|
|
191
|
+
*/
|
|
192
|
+
export declare function turnJumpTarget(options: {
|
|
193
|
+
turnCount: number;
|
|
194
|
+
activeIndex: number;
|
|
195
|
+
direction: 'up' | 'down';
|
|
196
|
+
/** 视口当前顶行号(内容坐标);不给就按「题面已在视口顶之上」处理。 */
|
|
197
|
+
viewportTop?: number;
|
|
198
|
+
/** 能滚到的最大顶行号(= 内容总行数 − 视口行数);不给就不判可达性。 */
|
|
199
|
+
maxTop?: number;
|
|
200
|
+
/** 每轮的顶行号(内容坐标,`layoutViewport` 的 `turns[].top`)。 */
|
|
201
|
+
turnTops?: number[];
|
|
202
|
+
}): number | null;
|
|
203
|
+
/**
|
|
204
|
+
* 刻度栏几何(照 dsh-TUI 的 `computeRailGeometry`):一轮一格,▲ + 刻度 + ▼ 这一块在
|
|
205
|
+
* 列里**纵向居中**,两头没有目标时箭头照样画(暗色)——位置固定,视线才不用每帧重新找。
|
|
206
|
+
*
|
|
207
|
+
* 窗口:轮数多于格子(列高 − 2)时绕当前轮滑一格;贴尾时钉住最新的那几格(活动刻度
|
|
208
|
+
* 永远在窗口里,否则没有一格是高亮的)。行数不够画(列高 < 3)返回全空。
|
|
209
|
+
*
|
|
210
|
+
* **居中用的高度与窗口用的高度可以分开**(`centerRows` / `rows`),这不是冗余:聊天区
|
|
211
|
+
* 的可用高度会随「回到底部」胶囊、草稿变高这些**页脚**变化而少一行,块若按它居中,
|
|
212
|
+
* 一滚动整块就上下挪一格(用户报障「上下滑动时刻度会上下晃」)。所以调用方按**只在
|
|
213
|
+
* 改变终端尺寸时才变**的高度居中,窗口仍按真实列高算;`centerRows` 比列高大时夹住
|
|
214
|
+
* (块最矮也得整块留在列里,否则 ▼ 会被裁出去)。
|
|
215
|
+
*
|
|
216
|
+
* 每一行落什么还由 `rows` 数组直接给出(渲染与命中判定同源),`upRow`/`tickTop`/`downRow`
|
|
217
|
+
* 是给探针读的同一份几何。
|
|
218
|
+
*
|
|
219
|
+
* @param options - 轮数、当前轮、可用行数(= 视口行数)、是否贴尾、每轮顶行号等。
|
|
220
|
+
*/
|
|
221
|
+
export declare function railGeometry(options: {
|
|
222
|
+
turnCount: number;
|
|
223
|
+
activeIndex: number;
|
|
224
|
+
rows: number;
|
|
225
|
+
/** 视口贴着最新(贴尾时窗口钉在最新的那几轮上)。 */
|
|
226
|
+
atBottom?: boolean;
|
|
227
|
+
/**
|
|
228
|
+
* 居中用的高度(默认 = `rows`)。传一个**不随页脚变化**的高度(聊天区传「帧高 − 状态栏」),
|
|
229
|
+
* 滚动时刻度栏就不会跟着跳。
|
|
230
|
+
*/
|
|
231
|
+
centerRows?: number;
|
|
232
|
+
/** 视口当前顶行号(内容坐标)。 */
|
|
233
|
+
viewportTop?: number;
|
|
234
|
+
/** 能滚到的最大顶行号。 */
|
|
235
|
+
maxTop?: number;
|
|
236
|
+
/** 每轮的顶行号(内容坐标)。 */
|
|
237
|
+
turnTops?: number[];
|
|
238
|
+
}): RailGeometry;
|
|
239
|
+
/** 刻度栏某一行的命中判定:点到刻度给轮下标、箭头给 'up'/'down'、空行给 null。 */
|
|
240
|
+
export declare function railHitRow(geometry: RailGeometry, row: number): number | 'up' | 'down' | null;
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { displayWidth } from './click.js';
|
|
2
|
+
/** 窗口上下各多渲染几行:滚动时不留白,也不至于把整篇都拉进来。 */
|
|
3
|
+
export const OVERSCAN_LINES = 8;
|
|
4
|
+
/** 行高表:条目 id → 显示行数。宽度变了(刻度栏出现/消失)就整体作废。 */
|
|
5
|
+
export class HeightCache {
|
|
6
|
+
table = new Map();
|
|
7
|
+
width = null;
|
|
8
|
+
/** 换宽度(含首次)就清表:折行变了,旧行数全是错的。 */
|
|
9
|
+
useWidth(width) {
|
|
10
|
+
if (this.width === width)
|
|
11
|
+
return;
|
|
12
|
+
this.width = width;
|
|
13
|
+
this.table.clear();
|
|
14
|
+
}
|
|
15
|
+
get(id) {
|
|
16
|
+
return this.table.get(id);
|
|
17
|
+
}
|
|
18
|
+
set(id, lines) {
|
|
19
|
+
if (lines > 0)
|
|
20
|
+
this.table.set(id, lines);
|
|
21
|
+
}
|
|
22
|
+
get size() {
|
|
23
|
+
return this.table.size;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 没量过的条目按**折叠态实际画出来的行数**估:一件卡片的估算误差不是小数字——它直接
|
|
28
|
+
* 变成「跳过的那段内容有多高」,跳过段的高度又决定视口顶落在内容坐标的哪一行。
|
|
29
|
+
*
|
|
30
|
+
* 曾经的口径(按最长字段数折行、卡片再补框架行)在工具卡上错得离谱:结果原文十四行,
|
|
31
|
+
* 卡片折叠态其实只画**一行**(「⚙ 调用 xx 工具 · 状态」,参数与结果要点开才给)。误差
|
|
32
|
+
* 14 行/张,一屏十几张就是两百行——滚轮往上滚时这些误差被真实测量逐条替换,内容坐标
|
|
33
|
+
* 整个缩水,于是「前几格滚轮不动、再滚一下突然弹回底部」(用户报的连滚回跳),长会话
|
|
34
|
+
* 还会因为跳过段虚高把视口顶推出正文(整片空白)。
|
|
35
|
+
*
|
|
36
|
+
* 所以这里逐类照着渲染器数行(只数结构行,文本按显示宽度折行),宁可与真实差一两行,
|
|
37
|
+
* 也不要差十几行。`text` 仍是「按原文折行」的近似——markdown 的块间距与代码围栏行
|
|
38
|
+
* 一增一减,量级上互相抵消(真值一旦被量到就永久进表,估算只管没滚到过的那段)。
|
|
39
|
+
*/
|
|
40
|
+
export function estimateLines(entry, width, options) {
|
|
41
|
+
const usable = Math.max(8, width);
|
|
42
|
+
switch (entry.kind) {
|
|
43
|
+
// 题面:`❯ ` 前缀占两列,前缀与正文同行;前面还留一行空(见 ChatView 的 USER_TOP_GAP)
|
|
44
|
+
case 'user':
|
|
45
|
+
if (entry.internal === true)
|
|
46
|
+
return 1;
|
|
47
|
+
return 1 + folded(entry.text, usable, INDENT_DETAIL);
|
|
48
|
+
// markdown 正文:按原文折行(见上)
|
|
49
|
+
case 'text':
|
|
50
|
+
return folded(entry.text, usable);
|
|
51
|
+
// 思考块:折叠态 = 标签一行 + **恒定 3 行瀑布**(照 dsh 的 preview ticker:每行硬截到框宽、
|
|
52
|
+
// 不折行,所以行数与宽度、与流到哪一句都无关);展开态 = 标签一行 + 全文折行
|
|
53
|
+
case 'thinking':
|
|
54
|
+
if (options?.expandThinking === true) {
|
|
55
|
+
return 1 + folded(entry.text, usable, INDENT_DETAIL);
|
|
56
|
+
}
|
|
57
|
+
return entry.text === '' ? 1 : 1 + WATERFALL_ROWS;
|
|
58
|
+
// 工具行:**默认折叠成一行**「⚙ 调用 xx 工具 · 状态」(点击才展开参数与结果行)。
|
|
59
|
+
// 展开是用户手动触发的少数情形:估算按折叠态,展开后那一帧量到真值就顶上
|
|
60
|
+
// (行高表每次渲染都重量,见 TranscriptViewport 的量测循环)。
|
|
61
|
+
case 'tool':
|
|
62
|
+
return 1;
|
|
63
|
+
// 子代理卡:标题一行;跑动中再挂一行「当前工具」(有才挂)+ 三行瀑布;失败再挂一行原因
|
|
64
|
+
case 'subagent': {
|
|
65
|
+
const working = entry.sub.state === 'working';
|
|
66
|
+
const toolLine = working && entry.sub.lastTool !== undefined ? 1 : 0;
|
|
67
|
+
return (1 +
|
|
68
|
+
toolLine +
|
|
69
|
+
(working ? WATERFALL_ROWS : 0) +
|
|
70
|
+
(entry.sub.state === 'failed' && entry.sub.error !== undefined ? 1 : 0));
|
|
71
|
+
}
|
|
72
|
+
// 计划卡 / 系统说明:标题行 + 文件行;压缩通知多一行分隔线
|
|
73
|
+
case 'plan':
|
|
74
|
+
return 2;
|
|
75
|
+
case 'system':
|
|
76
|
+
return (entry.compaction === undefined ? 0 : 1) + Math.max(1, folded(entry.text, usable, INDENT_DETAIL));
|
|
77
|
+
// 以下都是一行平铺文字(轮尾标记、重试行、文件改动、截断提示)
|
|
78
|
+
default:
|
|
79
|
+
return 1;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** 瀑布窗口的行数(子代理卡与思考块共用;与 `ChatView` 的 `WATERFALL_ROWS` 同口径)。 */
|
|
83
|
+
const WATERFALL_ROWS = 3;
|
|
84
|
+
/** 缩进列数(与 theme 的 `INDENT.detail` 同口径;本模块不引 UI 层,故复写常量)。 */
|
|
85
|
+
const INDENT_DETAIL = 2;
|
|
86
|
+
/** 多行文本折行总数(空行算一行,与 ink 的空行渲染一致;**空串算 0 行**)。 */
|
|
87
|
+
function folded(text, usable, indent = 0) {
|
|
88
|
+
// 空串必须是 0:`MarkdownView` 对空正文不产出任何行(`parseMarkdown('')` 是空块表),
|
|
89
|
+
// 而「直播尾还没吐字」的那一帧正是一条空 text 条目——按 1 行估会让整叠内容比真实
|
|
90
|
+
// 高一行,滚动时最后一行就少看一条(曾经在会话列表探针里抓到这个 ±1)。
|
|
91
|
+
if (text === '')
|
|
92
|
+
return 0;
|
|
93
|
+
let lines = 0;
|
|
94
|
+
for (const raw of text.split('\n')) {
|
|
95
|
+
lines += Math.max(1, Math.ceil((displayWidth(raw) + indent) / usable));
|
|
96
|
+
}
|
|
97
|
+
return lines;
|
|
98
|
+
}
|
|
99
|
+
/** 行号 → 锚点:落在那一条的第几行。到(或超过)尾部就是「跟尾」(null)。 */
|
|
100
|
+
export function anchorAtRow(rows, entries, row, maxTop) {
|
|
101
|
+
if (row >= maxTop)
|
|
102
|
+
return null;
|
|
103
|
+
const target = Math.max(0, row);
|
|
104
|
+
let walk = 0;
|
|
105
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
106
|
+
const height = rows[index] ?? 0;
|
|
107
|
+
if (target < walk + height) {
|
|
108
|
+
return { id: entries[index].id, index, offset: target - walk };
|
|
109
|
+
}
|
|
110
|
+
walk += height;
|
|
111
|
+
}
|
|
112
|
+
// 空会话(或行号落在末尾之外):没有可钉的条目
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
/** 锚点 → 行号(内容坐标)。`null`(跟尾)给 `totalLines`,调用方自己夹。 */
|
|
116
|
+
export function rowOfAnchor(rows, entries, anchor) {
|
|
117
|
+
let index = entries[anchor.index]?.id === anchor.id ? anchor.index : -1;
|
|
118
|
+
if (index === -1)
|
|
119
|
+
index = entries.findIndex((entry) => entry.id === anchor.id);
|
|
120
|
+
if (index === -1)
|
|
121
|
+
return Number.MAX_SAFE_INTEGER;
|
|
122
|
+
let walk = 0;
|
|
123
|
+
for (let at = 0; at < index; at += 1)
|
|
124
|
+
walk += rows[at] ?? 0;
|
|
125
|
+
return walk + Math.max(0, Math.min(anchor.offset, (rows[index] ?? 1) - 1));
|
|
126
|
+
}
|
|
127
|
+
/** 每条的显示行数:量过就用真值,没量过用估算。 */
|
|
128
|
+
function lineOf(entry, heights, width, options) {
|
|
129
|
+
const measured = heights.get(entry.id);
|
|
130
|
+
return measured ?? estimateLines(entry, width, options);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 算出视口布局。
|
|
134
|
+
*
|
|
135
|
+
* @param entries - 全量条目(顺序即时间序)。
|
|
136
|
+
* @param heights - 行高表(含已量到的真值)。
|
|
137
|
+
* @param options - 内容宽度、视口行数、视口顶锚点(null = 跟尾)。
|
|
138
|
+
*/
|
|
139
|
+
export function layoutViewport(entries, heights, options) {
|
|
140
|
+
const { width, viewportLines } = options;
|
|
141
|
+
const viewport = Math.max(1, viewportLines);
|
|
142
|
+
const rows = entries.map((entry) => lineOf(entry, heights, width, options));
|
|
143
|
+
const totalLines = rows.reduce((sum, value) => sum + value, 0);
|
|
144
|
+
const maxShift = Math.max(0, totalLines - viewport);
|
|
145
|
+
const maxTop = Math.max(0, totalLines - viewport);
|
|
146
|
+
// 锚点 → 行号 → 从尾部退多少行;跟尾(null)就是 0
|
|
147
|
+
const wanted = options.anchor === null ? maxTop : rowOfAnchor(rows, entries, options.anchor);
|
|
148
|
+
const shift = Math.max(0, Math.min(maxShift, maxShift - Math.max(0, Math.min(wanted, maxTop))));
|
|
149
|
+
// 视口顶行(内容坐标):从尾部往上退 shift 行
|
|
150
|
+
const top = Math.max(0, totalLines - viewport - shift);
|
|
151
|
+
const windowFrom = top - OVERSCAN_LINES;
|
|
152
|
+
const wantedEnd = top + viewport + OVERSCAN_LINES;
|
|
153
|
+
let start = 0;
|
|
154
|
+
let above = 0;
|
|
155
|
+
while (start < entries.length && above + rows[start] <= windowFrom) {
|
|
156
|
+
above += rows[start];
|
|
157
|
+
start += 1;
|
|
158
|
+
}
|
|
159
|
+
let end = entries.length;
|
|
160
|
+
let cursor = above;
|
|
161
|
+
for (let index = start; index < entries.length; index += 1) {
|
|
162
|
+
cursor += rows[index];
|
|
163
|
+
if (cursor >= wantedEnd) {
|
|
164
|
+
end = index + 1;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const windowLines = rows.slice(start, end).reduce((sum, value) => sum + value, 0);
|
|
169
|
+
const below = Math.max(0, totalLines - above - windowLines);
|
|
170
|
+
const turns = [];
|
|
171
|
+
let walk = 0;
|
|
172
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
173
|
+
const entry = entries[index];
|
|
174
|
+
if (entry.kind === 'user' && entry.internal !== true) {
|
|
175
|
+
turns.push({ id: entry.id, index, top: walk });
|
|
176
|
+
}
|
|
177
|
+
walk += rows[index];
|
|
178
|
+
}
|
|
179
|
+
return { start, end, aboveLines: above, belowLines: below, rows, totalLines, maxShift, shift, top: shift === 0 ? null : top, turns };
|
|
180
|
+
}
|
|
181
|
+
/** 跳到某一轮时该用的滚动量:让这一轮的题面正好落在视口顶上。 */
|
|
182
|
+
export function shiftForTurn(turn, layout, viewportLines) {
|
|
183
|
+
const viewport = Math.max(1, viewportLines);
|
|
184
|
+
return Math.max(0, Math.min(layout.maxShift, layout.totalLines - viewport - turn.top));
|
|
185
|
+
}
|
|
186
|
+
/** 当前视口顶上那一轮(= 拥有视口首行的那一轮;视口在内容头部时算第 0 轮)。 */
|
|
187
|
+
export function activeTurnIndex(turns, shift, layout, viewportLines) {
|
|
188
|
+
if (turns.length === 0)
|
|
189
|
+
return -1;
|
|
190
|
+
const top = Math.max(0, layout.totalLines - Math.max(1, viewportLines) - shift);
|
|
191
|
+
let active = 0;
|
|
192
|
+
for (let index = 0; index < turns.length; index += 1) {
|
|
193
|
+
if (turns[index].top <= top)
|
|
194
|
+
active = index;
|
|
195
|
+
else
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
return active;
|
|
199
|
+
}
|
|
200
|
+
// ── 刻度栏几何 ──────────────────────────────────────────────────────────────
|
|
201
|
+
/** 刻度栏占的列数(dsh 同款 2 列:`━━` / ` ─` 对齐)。 */
|
|
202
|
+
export const RAIL_WIDTH = 2;
|
|
203
|
+
/** 窄于此不画(再窄正文就挤没了)。 */
|
|
204
|
+
export const RAIL_MIN_COLUMNS = 60;
|
|
205
|
+
/** 至少两轮才值得画。 */
|
|
206
|
+
export const RAIL_MIN_TURNS = 2;
|
|
207
|
+
/**
|
|
208
|
+
* 刻度栏的字形(照 dsh-TUI 的 TimelineRail 搬):当前轮 `━━` 重线、其余 ` ─` 短线
|
|
209
|
+
* (占满 2 列、右对齐,不靠测量对齐)、箭头 `▴`/`▾`。
|
|
210
|
+
*/
|
|
211
|
+
export const RAIL_GLYPH = {
|
|
212
|
+
active: '━━',
|
|
213
|
+
idle: ' ─',
|
|
214
|
+
up: ' ▴',
|
|
215
|
+
down: ' ▾',
|
|
216
|
+
};
|
|
217
|
+
/** 画不画右侧那一列(轮数刻度)。 */
|
|
218
|
+
export function gutterEligible(options) {
|
|
219
|
+
if (options.columns < RAIL_MIN_COLUMNS)
|
|
220
|
+
return false;
|
|
221
|
+
// 一屏装得下、连一行都塞不进这列——画出来只是噪音
|
|
222
|
+
if (options.viewportLines < 3)
|
|
223
|
+
return false;
|
|
224
|
+
if (options.contentLines <= options.viewportLines)
|
|
225
|
+
return false;
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
/** 画不画**轮数刻度**(还要 ≥2 轮:一轮的刻度没有可跳的目标,整列就不画)。 */
|
|
229
|
+
export function railEligible(options) {
|
|
230
|
+
if (options.turnCount < RAIL_MIN_TURNS)
|
|
231
|
+
return false;
|
|
232
|
+
return gutterEligible(options);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* 上/下一轮的跳转目标(刻度栏箭头与 `[`/`]` 键**共用这一条**,两边不可能对不上)。
|
|
236
|
+
*
|
|
237
|
+
* 两条 dsh 定的规矩:
|
|
238
|
+
* - ▲ 先对齐本轮的题面:读到本轮中段(视口顶在本轮题面之下)时,▲ 指的就是**本轮**
|
|
239
|
+
* ——再按一次才退到上一轮。少了这条,读到长回答中段按 ▲ 会直接跳过本轮的题面。
|
|
240
|
+
* - 名下的那一轮必须真能滚到视口顶(顶行号 ≤ 能滚到的最大顶行号):够不着的那一轮
|
|
241
|
+
* 点下去没反应,箭头却亮着,按几次都不动。够不着就不给目标(暗箭头 = 保证的空操作)。
|
|
242
|
+
*
|
|
243
|
+
* @param options - 轮数、当前轮、视口顶与最大顶行号(内容坐标)、每轮顶行号(可选)。
|
|
244
|
+
*/
|
|
245
|
+
export function turnJumpTarget(options) {
|
|
246
|
+
const { turnCount, activeIndex, direction } = options;
|
|
247
|
+
if (turnCount === 0 || activeIndex < 0 || activeIndex >= turnCount)
|
|
248
|
+
return null;
|
|
249
|
+
const tops = options.turnTops;
|
|
250
|
+
if (direction === 'up') {
|
|
251
|
+
const activeTop = tops?.[activeIndex];
|
|
252
|
+
const midTurn = activeTop !== undefined && options.viewportTop !== undefined && options.viewportTop > activeTop;
|
|
253
|
+
const target = midTurn ? activeIndex : activeIndex - 1;
|
|
254
|
+
return target >= 0 ? target : null;
|
|
255
|
+
}
|
|
256
|
+
const next = activeIndex + 1;
|
|
257
|
+
if (next >= turnCount)
|
|
258
|
+
return null;
|
|
259
|
+
const nextTop = tops?.[next];
|
|
260
|
+
if (nextTop !== undefined && options.maxTop !== undefined && nextTop > options.maxTop)
|
|
261
|
+
return null;
|
|
262
|
+
return next;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* 刻度栏几何(照 dsh-TUI 的 `computeRailGeometry`):一轮一格,▲ + 刻度 + ▼ 这一块在
|
|
266
|
+
* 列里**纵向居中**,两头没有目标时箭头照样画(暗色)——位置固定,视线才不用每帧重新找。
|
|
267
|
+
*
|
|
268
|
+
* 窗口:轮数多于格子(列高 − 2)时绕当前轮滑一格;贴尾时钉住最新的那几格(活动刻度
|
|
269
|
+
* 永远在窗口里,否则没有一格是高亮的)。行数不够画(列高 < 3)返回全空。
|
|
270
|
+
*
|
|
271
|
+
* **居中用的高度与窗口用的高度可以分开**(`centerRows` / `rows`),这不是冗余:聊天区
|
|
272
|
+
* 的可用高度会随「回到底部」胶囊、草稿变高这些**页脚**变化而少一行,块若按它居中,
|
|
273
|
+
* 一滚动整块就上下挪一格(用户报障「上下滑动时刻度会上下晃」)。所以调用方按**只在
|
|
274
|
+
* 改变终端尺寸时才变**的高度居中,窗口仍按真实列高算;`centerRows` 比列高大时夹住
|
|
275
|
+
* (块最矮也得整块留在列里,否则 ▼ 会被裁出去)。
|
|
276
|
+
*
|
|
277
|
+
* 每一行落什么还由 `rows` 数组直接给出(渲染与命中判定同源),`upRow`/`tickTop`/`downRow`
|
|
278
|
+
* 是给探针读的同一份几何。
|
|
279
|
+
*
|
|
280
|
+
* @param options - 轮数、当前轮、可用行数(= 视口行数)、是否贴尾、每轮顶行号等。
|
|
281
|
+
*/
|
|
282
|
+
export function railGeometry(options) {
|
|
283
|
+
const rows = Math.max(1, options.rows);
|
|
284
|
+
const turnCount = Math.max(0, options.turnCount);
|
|
285
|
+
/** 上下各留一行给 ▲/▼(dsh 同款:箭头行永远存在,有没有目标只改颜色)。 */
|
|
286
|
+
const maxTicks = rows - 2;
|
|
287
|
+
// 轮数不到两轮、或列高装不下「▲ + 一格 + ▼」——整列空(与 dsh 的 null 同义)
|
|
288
|
+
if (turnCount < RAIL_MIN_TURNS || maxTicks < 1) {
|
|
289
|
+
return {
|
|
290
|
+
rows: Array.from({ length: rows }, () => null),
|
|
291
|
+
up: null,
|
|
292
|
+
down: null,
|
|
293
|
+
upRow: -1,
|
|
294
|
+
downRow: -1,
|
|
295
|
+
tickTop: -1,
|
|
296
|
+
windowStart: 0,
|
|
297
|
+
windowEnd: 0,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
const activeIndex = Math.max(0, Math.min(options.activeIndex, turnCount - 1));
|
|
301
|
+
const shown = Math.min(turnCount, maxTicks);
|
|
302
|
+
let start = 0;
|
|
303
|
+
if (turnCount > maxTicks) {
|
|
304
|
+
const tailStart = turnCount - maxTicks;
|
|
305
|
+
start =
|
|
306
|
+
options.atBottom === true
|
|
307
|
+
? Math.min(activeIndex, tailStart)
|
|
308
|
+
: Math.min(Math.max(0, activeIndex - Math.floor(maxTicks / 2)), tailStart);
|
|
309
|
+
}
|
|
310
|
+
const up = turnJumpTarget({ ...options, turnCount, activeIndex, direction: 'up' });
|
|
311
|
+
const down = turnJumpTarget({ ...options, turnCount, activeIndex, direction: 'down' });
|
|
312
|
+
// 纵向居中(对齐 dsh):▲ + 刻度 + ▼ 整块居中,剩下的行上下平分。
|
|
313
|
+
// 居中高度可以是「稳定高度」(见 `centerRows`),但块必须整块留在列里——夹住下界
|
|
314
|
+
const blockLines = shown + 2;
|
|
315
|
+
const blockTop = Math.max(0, Math.min(Math.floor((Math.max(1, options.centerRows ?? rows) - blockLines) / 2), rows - blockLines));
|
|
316
|
+
const upRow = blockTop;
|
|
317
|
+
const tickTop = blockTop + 1;
|
|
318
|
+
const downRow = tickTop + shown;
|
|
319
|
+
const output = Array.from({ length: rows }, () => null);
|
|
320
|
+
output[upRow] = { kind: 'up', enabled: up !== null };
|
|
321
|
+
for (let offset = 0; offset < shown; offset += 1) {
|
|
322
|
+
const turn = start + offset;
|
|
323
|
+
output[tickTop + offset] = { kind: 'tick', turn, active: turn === activeIndex };
|
|
324
|
+
}
|
|
325
|
+
output[downRow] = { kind: 'down', enabled: down !== null };
|
|
326
|
+
return {
|
|
327
|
+
rows: output,
|
|
328
|
+
up,
|
|
329
|
+
down,
|
|
330
|
+
upRow,
|
|
331
|
+
downRow,
|
|
332
|
+
tickTop,
|
|
333
|
+
windowStart: start,
|
|
334
|
+
windowEnd: start + shown,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
/** 刻度栏某一行的命中判定:点到刻度给轮下标、箭头给 'up'/'down'、空行给 null。 */
|
|
338
|
+
export function railHitRow(geometry, row) {
|
|
339
|
+
const entry = geometry.rows[row];
|
|
340
|
+
if (entry === undefined || entry === null)
|
|
341
|
+
return null;
|
|
342
|
+
if (entry.kind === 'up')
|
|
343
|
+
return 'up';
|
|
344
|
+
if (entry.kind === 'down')
|
|
345
|
+
return 'down';
|
|
346
|
+
return entry.turn;
|
|
347
|
+
}
|