paseo-dsh 0.2.16 → 0.2.18
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/client/react-native.d.ts +1 -0
- package/client/stream-stats.tsx +77 -5
- package/client/web.ts +351 -0
- package/package.json +1 -1
- package/server/bridge-assets.ts +1 -1
- package/server/dsh-process.ts +85 -9
- package/server/provider.ts +34 -7
- package/server/session.ts +558 -21
- package/server/timeline.ts +37 -8
package/client/react-native.d.ts
CHANGED
package/client/stream-stats.tsx
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { PluginClientContext, PluginTimelineItemProps } from "@getpaseo/plugin/client";
|
|
2
|
-
import { Fragment, useState } from "react";
|
|
2
|
+
import { Fragment, useLayoutEffect, useRef, useState } from "react";
|
|
3
3
|
import { Pressable, Text, View } from "react-native";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
+
import { measureFooterGeometry, rowIsLaidOut, watchRow } from "./web";
|
|
5
6
|
|
|
6
7
|
export const streamStatsSchema = z.object({
|
|
7
8
|
firstTokenMs: z.number().optional(),
|
|
@@ -20,10 +21,24 @@ export const streamStatsSchema = z.object({
|
|
|
20
21
|
steps: z.number().optional(),
|
|
21
22
|
sessionTokens: z.number().optional(),
|
|
22
23
|
cacheHit: z.number().optional(),
|
|
24
|
+
/** Set by the daemon on the last row of a completed turn: the copy footer is above it. */
|
|
25
|
+
final: z.boolean().optional(),
|
|
23
26
|
});
|
|
24
27
|
|
|
25
28
|
type StreamStats = z.infer<typeof streamStatsSchema>;
|
|
26
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Paseo paints the completed-turn footer at `STREAM_METADATA_FONT_SIZE` (13px).
|
|
32
|
+
* Until the page can be measured that is also what this row draws at, so the
|
|
33
|
+
* two lines read as one block instead of two sizes.
|
|
34
|
+
*/
|
|
35
|
+
const FOOTER_FONT_SIZE = 13;
|
|
36
|
+
/** Space under the footer's last line, as a fraction of this row's text size. */
|
|
37
|
+
const GAP_EM = 0.46;
|
|
38
|
+
/** The row's own small offsets, also fractions of its text size (~2px / ~1px at 13). */
|
|
39
|
+
const INSET_EM = 0.15;
|
|
40
|
+
const DETAIL_GAP_EM = 0.1;
|
|
41
|
+
|
|
27
42
|
function formatLatency(ms: number): string {
|
|
28
43
|
return ms < 1000 ? `${Math.round(ms)}ms` : `${(ms / 1000).toFixed(1)}s`;
|
|
29
44
|
}
|
|
@@ -70,10 +85,66 @@ function toolsLine(data: StreamStats): string | undefined {
|
|
|
70
85
|
/** One compact line under an answer; tap to expand the full picture. */
|
|
71
86
|
function StreamStatsRow({ item, theme, layout }: PluginTimelineItemProps<StreamStats>) {
|
|
72
87
|
const [open, setOpen] = useState(false);
|
|
88
|
+
const [fontSize, setFontSize] = useState(FOOTER_FONT_SIZE);
|
|
89
|
+
const [lift, setLift] = useState(0);
|
|
90
|
+
const rowRef = useRef<unknown>(null);
|
|
91
|
+
/** The lift the DOM currently carries; the next measurement discounts it. */
|
|
92
|
+
const appliedLift = useRef(0);
|
|
73
93
|
const data = item.data;
|
|
74
|
-
|
|
94
|
+
|
|
95
|
+
// The ref must describe the margin the DOM actually carries, not the one a
|
|
96
|
+
// pending render is about to apply.
|
|
97
|
+
useLayoutEffect(() => {
|
|
98
|
+
appliedLift.current = lift;
|
|
99
|
+
}, [lift]);
|
|
100
|
+
|
|
101
|
+
// Only the last row of a completed turn has the footer directly above it, and
|
|
102
|
+
// only the web build can see where that footer was painted. Everything else
|
|
103
|
+
// keeps the host's spacing: a guessed offset is what used to ride up over the
|
|
104
|
+
// answer once a second turn pushed the first one down.
|
|
105
|
+
useLayoutEffect(() => {
|
|
106
|
+
if (data.final !== true || layout.platform !== "web") {
|
|
107
|
+
setLift(0);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const measure = () => {
|
|
111
|
+
const found = measureFooterGeometry(rowRef.current, {
|
|
112
|
+
appliedLift: appliedLift.current,
|
|
113
|
+
gapEm: GAP_EM,
|
|
114
|
+
fallbackFontSize: FOOTER_FONT_SIZE,
|
|
115
|
+
});
|
|
116
|
+
if (found === null) {
|
|
117
|
+
// A hidden tab (zero-height row) says nothing about the layout: keep
|
|
118
|
+
// the last margin so switching back does not un-tuck the row.
|
|
119
|
+
if (rowIsLaidOut(rowRef.current)) setLift(0);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
setLift(found.lift);
|
|
123
|
+
setFontSize(found.fontSize);
|
|
124
|
+
};
|
|
125
|
+
measure();
|
|
126
|
+
// The footer is not necessarily in its final place when the row mounts: it
|
|
127
|
+
// can arrive a frame later, and the host may move it after the turn is
|
|
128
|
+
// marked complete. Re-measure a few times while that settles, and keep
|
|
129
|
+
// watching the containers in case it moves again (a new turn pushing this
|
|
130
|
+
// one down, a theme swap, a font load).
|
|
131
|
+
const retries = [80, 240, 700, 1500].map((ms) => setTimeout(measure, ms));
|
|
132
|
+
const stop = watchRow(rowRef.current, measure);
|
|
133
|
+
return () => {
|
|
134
|
+
for (const timer of retries) clearTimeout(timer);
|
|
135
|
+
stop?.();
|
|
136
|
+
};
|
|
137
|
+
}, [data.final, layout.platform]);
|
|
138
|
+
|
|
75
139
|
const muted = theme.colors.foregroundMuted;
|
|
76
|
-
const
|
|
140
|
+
const lineHeight = Math.round(fontSize * (18 / 13));
|
|
141
|
+
const inset = Math.max(1, Math.round(fontSize * INSET_EM));
|
|
142
|
+
const base = {
|
|
143
|
+
color: muted,
|
|
144
|
+
fontSize,
|
|
145
|
+
lineHeight,
|
|
146
|
+
fontVariant: ["tabular-nums"] as const,
|
|
147
|
+
};
|
|
77
148
|
|
|
78
149
|
const ratio = contextRatio(data);
|
|
79
150
|
const segments: Array<{ text: string; color: string }> = [];
|
|
@@ -100,10 +171,11 @@ function StreamStatsRow({ item, theme, layout }: PluginTimelineItemProps<StreamS
|
|
|
100
171
|
|
|
101
172
|
return (
|
|
102
173
|
<Pressable
|
|
174
|
+
ref={rowRef}
|
|
103
175
|
accessibilityRole="button"
|
|
104
176
|
accessibilityLabel={open ? "收起回答统计" : "展开回答统计"}
|
|
105
177
|
onPress={() => setOpen((value) => !value)}
|
|
106
|
-
style={{ alignSelf: "flex-start", marginTop:
|
|
178
|
+
style={{ alignSelf: "flex-start", marginTop: lift, marginBottom: 0, paddingLeft: inset }}
|
|
107
179
|
>
|
|
108
180
|
<View style={{ flexDirection: "row", alignItems: "center", flexWrap: "wrap" }}>
|
|
109
181
|
{segments.map((segment, index) => (
|
|
@@ -115,7 +187,7 @@ function StreamStatsRow({ item, theme, layout }: PluginTimelineItemProps<StreamS
|
|
|
115
187
|
<Text style={base}>{open ? " ▴" : " ▾"}</Text>
|
|
116
188
|
</View>
|
|
117
189
|
{open ? (
|
|
118
|
-
<View style={{ marginTop:
|
|
190
|
+
<View style={{ marginTop: inset, gap: Math.max(1, Math.round(fontSize * DETAIL_GAP_EM)) }}>
|
|
119
191
|
{details.map((line) => (
|
|
120
192
|
<Text key={line} style={base}>
|
|
121
193
|
{line}
|
package/client/web.ts
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { Platform } from "react-native";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Where the completed-turn footer is, measured instead of guessed.
|
|
5
|
+
*
|
|
6
|
+
* Paseo reserves a fixed strip under that footer (`turnFooterSlot` carries
|
|
7
|
+
* `paddingBottom: SPACING[8]`, 32px) and the stats row is an ordinary timeline
|
|
8
|
+
* item placed after it, so the row floats in that strip with the gap the host
|
|
9
|
+
* happens to give it. The strip is not ours to change, and it is not stable
|
|
10
|
+
* either: a theme plugin restyles the footer, and the same turn is laid out
|
|
11
|
+
* differently once a newer turn pushes it down. Any constant offset therefore
|
|
12
|
+
* fits one combination and overlaps the answer in another.
|
|
13
|
+
*
|
|
14
|
+
* So the row measures the footer actually painted above it. All DOM access
|
|
15
|
+
* lives in this file: Paseo also runs plugin client code on iOS and Android,
|
|
16
|
+
* where these globals do not exist, and the renderer only ever sees the numbers
|
|
17
|
+
* returned here (or null, meaning "leave the host's layout alone").
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** The DOM shape read here; `tsconfig` ships no DOM typings. */
|
|
21
|
+
interface MeasuredNode {
|
|
22
|
+
parentElement: MeasuredNode | null;
|
|
23
|
+
previousElementSibling: MeasuredNode | null;
|
|
24
|
+
children: ArrayLike<MeasuredNode>;
|
|
25
|
+
childNodes: ArrayLike<MeasuredNode>;
|
|
26
|
+
nodeType: number;
|
|
27
|
+
nodeValue: string | null;
|
|
28
|
+
textContent: string | null;
|
|
29
|
+
getBoundingClientRect(): { top: number; bottom: number; height: number };
|
|
30
|
+
querySelectorAll(selector: string): ArrayLike<MeasuredNode>;
|
|
31
|
+
ownerDocument: TextDocument | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface TextRect {
|
|
35
|
+
top: number;
|
|
36
|
+
bottom: number;
|
|
37
|
+
height: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface TextRange {
|
|
41
|
+
selectNodeContents(node: unknown): void;
|
|
42
|
+
getBoundingClientRect(): TextRect;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface TextDocument {
|
|
46
|
+
createRange(): TextRange;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface ComputedStyle {
|
|
50
|
+
fontSize?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type ResizeObserverConstructor = new (callback: () => void) => {
|
|
54
|
+
observe(target: unknown): void;
|
|
55
|
+
disconnect(): void;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
type MutationObserverConstructor = new (callback: () => void) => {
|
|
59
|
+
observe(target: unknown, options: { childList: boolean; subtree: boolean }): void;
|
|
60
|
+
disconnect(): void;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The footer is one short metadata line (copy, model, time, elapsed). An answer
|
|
65
|
+
* or a whole pushed-down turn is much longer; using one of those as the anchor
|
|
66
|
+
* is exactly how the row ends up on top of the text.
|
|
67
|
+
*/
|
|
68
|
+
const MAX_FOOTER_TEXT = 120;
|
|
69
|
+
/** The footer's first line is its copy control; prose that mentions the word is not. */
|
|
70
|
+
const FOOTER_PREFIX = /^(复制|copy)/i;
|
|
71
|
+
/** Wrapper levels to step over before giving up: item frame, list cell, row. */
|
|
72
|
+
const MAX_WRAPPER_DEPTH = 6;
|
|
73
|
+
/** Ancestors watched for layout changes: row, wrappers, turn block, list. */
|
|
74
|
+
const WATCH_DEPTH = 6;
|
|
75
|
+
/**
|
|
76
|
+
* Largest plausible distance, in footer line heights, between the footer's last
|
|
77
|
+
* line and the row the host laid out under it. A wrong anchor lands far outside
|
|
78
|
+
* this; a themed footer that adds padding still lands inside it.
|
|
79
|
+
*/
|
|
80
|
+
const MAX_GAP_LINES = 4;
|
|
81
|
+
|
|
82
|
+
/** Whether the row is actually laid out (an inactive tab renders zero-height). */
|
|
83
|
+
export function rowIsLaidOut(row: unknown): boolean {
|
|
84
|
+
const node = webRow(row);
|
|
85
|
+
if (node === null) return false;
|
|
86
|
+
return node.getBoundingClientRect().height > 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface FooterGeometry {
|
|
90
|
+
/** Margin that puts the row's top `gapEm` under the footer's last line. */
|
|
91
|
+
lift: number;
|
|
92
|
+
/** Text size the footer is painted at, so the row can match it. */
|
|
93
|
+
fontSize: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function webRow(row: unknown): MeasuredNode | null {
|
|
97
|
+
if (Platform.OS !== "web" || row === null || row === undefined) return null;
|
|
98
|
+
return row as MeasuredNode;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function lineHeightFor(fontSize: number): number {
|
|
102
|
+
// Paseo sets the footer's 13px text in an 18px line box.
|
|
103
|
+
return fontSize * (18 / 13);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const TEXT_NODE = 3;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Elements that directly hold a piece of visible text. Reading text nodes
|
|
110
|
+
* rather than leaf elements means inline text (a bare string child, which a
|
|
111
|
+
* theme plugin can produce) is measured too.
|
|
112
|
+
*/
|
|
113
|
+
function textRuns(node: MeasuredNode, depth = 0): MeasuredNode[] {
|
|
114
|
+
const runs: MeasuredNode[] = [];
|
|
115
|
+
if (depth > 12) return runs;
|
|
116
|
+
for (let index = 0; index < node.childNodes.length; index += 1) {
|
|
117
|
+
const child = node.childNodes[index];
|
|
118
|
+
if (child === undefined) continue;
|
|
119
|
+
if (child.nodeType === TEXT_NODE) {
|
|
120
|
+
const text = child.nodeValue?.trim() ?? "";
|
|
121
|
+
if (text !== "" && node.getBoundingClientRect().height > 0) runs.push(node);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
runs.push(...textRuns(child, depth + 1));
|
|
125
|
+
}
|
|
126
|
+
return runs;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The box the glyphs of a text node actually occupy. A range is used because
|
|
131
|
+
* the element around the text may carry padding (a themed footer that sets its
|
|
132
|
+
* text directly), and the element box would then report the padding as text.
|
|
133
|
+
*/
|
|
134
|
+
function textRect(node: MeasuredNode): TextRect {
|
|
135
|
+
const document = node.ownerDocument;
|
|
136
|
+
if (document !== null) {
|
|
137
|
+
const range = document.createRange();
|
|
138
|
+
range.selectNodeContents(node);
|
|
139
|
+
const rect = range.getBoundingClientRect();
|
|
140
|
+
if (rect.height > 0) return rect;
|
|
141
|
+
}
|
|
142
|
+
return node.getBoundingClientRect();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The font size the footer is painted at: read from a text leaf rather than the
|
|
147
|
+
* footer box, because the box inherits the content size and only adds padding.
|
|
148
|
+
*/
|
|
149
|
+
function footerFontSize(node: MeasuredNode, fallback: number): number {
|
|
150
|
+
const read = (globalThis as { getComputedStyle?: (element: unknown) => ComputedStyle })
|
|
151
|
+
.getComputedStyle;
|
|
152
|
+
if (read === undefined) return fallback;
|
|
153
|
+
let largest = 0;
|
|
154
|
+
for (const leaf of textRuns(node)) {
|
|
155
|
+
const parsed = Number.parseFloat(read(leaf).fontSize ?? "");
|
|
156
|
+
if (Number.isFinite(parsed) && parsed > largest) largest = parsed;
|
|
157
|
+
}
|
|
158
|
+
return largest > 0 ? largest : fallback;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* A short block whose visible text starts with the copy control. Answers can
|
|
163
|
+
* mention "copy" too, and a message block can contain a copy button of its own,
|
|
164
|
+
* so neither containing the word nor owning a copy control is enough: the text
|
|
165
|
+
* itself has to start with it.
|
|
166
|
+
*/
|
|
167
|
+
function looksLikeFooter(node: MeasuredNode): boolean {
|
|
168
|
+
const text = node.textContent?.trim() ?? "";
|
|
169
|
+
return text !== "" && text.length <= MAX_FOOTER_TEXT && FOOTER_PREFIX.test(text);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Elements that paint text, used to find the copy line without scanning every box. */
|
|
173
|
+
function textBearing(node: MeasuredNode): MeasuredNode[] {
|
|
174
|
+
const out: MeasuredNode[] = [];
|
|
175
|
+
const all = node.querySelectorAll("*");
|
|
176
|
+
for (let index = 0; index < all.length; index += 1) {
|
|
177
|
+
const element = all[index];
|
|
178
|
+
if (element === undefined || element.children.length > 0) continue;
|
|
179
|
+
const text = element.textContent?.trim() ?? "";
|
|
180
|
+
if (text !== "" && element.getBoundingClientRect().height > 0) out.push(element);
|
|
181
|
+
}
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The footer inside `block`, if one is painted there.
|
|
187
|
+
*
|
|
188
|
+
* Paseo does not always put the footer in a sibling row: for a completed turn it
|
|
189
|
+
* is drawn at the end of the answer block, so the row's previous sibling is the
|
|
190
|
+
* whole answer. Look at the sibling first and then inside it, and take the last
|
|
191
|
+
* copy line found — the footer is the last thing in that block, never the first
|
|
192
|
+
* line of the answer.
|
|
193
|
+
*/
|
|
194
|
+
function findFooter(block: MeasuredNode): MeasuredNode | null {
|
|
195
|
+
if (looksLikeFooter(block)) return block;
|
|
196
|
+
let found: MeasuredNode | null = null;
|
|
197
|
+
let lowest = Number.NEGATIVE_INFINITY;
|
|
198
|
+
for (const element of textBearing(block)) {
|
|
199
|
+
if (!FOOTER_PREFIX.test(element.textContent?.trim() ?? "")) continue;
|
|
200
|
+
// A message block can carry copy affordances of its own (the hover
|
|
201
|
+
// toolbar). The footer is the one painted lowest, right above this row.
|
|
202
|
+
const bottom = element.getBoundingClientRect().bottom;
|
|
203
|
+
if (bottom > lowest) {
|
|
204
|
+
lowest = bottom;
|
|
205
|
+
found = element;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (found === null) return null;
|
|
209
|
+
// And it has to be the last text in the block: a copy control floating in
|
|
210
|
+
// the middle of an answer is not the footer, and measuring it would tuck the
|
|
211
|
+
// row under the wrong line.
|
|
212
|
+
const lowestText = lastTextBottom(block);
|
|
213
|
+
if (lowestText !== null && lowest < lowestText - 2) return null;
|
|
214
|
+
return found;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The frame this item was laid out in and the footer above it.
|
|
219
|
+
*
|
|
220
|
+
* The row is rendered inside one or more wrappers that hold nothing but it, so
|
|
221
|
+
* empty predecessors are stepped over until a sibling with height appears. That
|
|
222
|
+
* sibling has to contain the footer; anything else means this row is not tucked
|
|
223
|
+
* under one, and moving it would land on the answer.
|
|
224
|
+
*/
|
|
225
|
+
function locateFooter(row: MeasuredNode): { frame: MeasuredNode; footer: MeasuredNode } | null {
|
|
226
|
+
let frame: MeasuredNode | null = row;
|
|
227
|
+
for (let depth = 0; depth < MAX_WRAPPER_DEPTH && frame !== null; depth += 1) {
|
|
228
|
+
const above = frame.previousElementSibling;
|
|
229
|
+
if (above !== null && above.getBoundingClientRect().height > 0) {
|
|
230
|
+
const footer = findFooter(above);
|
|
231
|
+
return footer === null ? null : { frame, footer };
|
|
232
|
+
}
|
|
233
|
+
frame = frame.parentElement;
|
|
234
|
+
}
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Bottom edge of the last painted line of text inside `node`. */
|
|
239
|
+
function lastTextBottom(node: MeasuredNode): number | null {
|
|
240
|
+
let bottom: number | null = null;
|
|
241
|
+
for (const leaf of textRuns(node)) {
|
|
242
|
+
const rect = textRect(leaf);
|
|
243
|
+
bottom = bottom === null ? rect.bottom : Math.max(bottom, rect.bottom);
|
|
244
|
+
}
|
|
245
|
+
return bottom;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* How far `row` pulls itself up so its top sits `gapEm` under the footer's last
|
|
250
|
+
* line, plus the size that footer is painted at.
|
|
251
|
+
*
|
|
252
|
+
* `appliedLift` is the margin the row carries right now. The frame has already
|
|
253
|
+
* moved by it, so it is subtracted before measuring; otherwise the row measures
|
|
254
|
+
* the position it just moved itself to and oscillates between two places.
|
|
255
|
+
*
|
|
256
|
+
* Returns null when there is nothing safe to measure: native, no footer above,
|
|
257
|
+
* a sibling that only looks like a footer, or a gap too large to be the footer.
|
|
258
|
+
* The caller then keeps the host's spacing rather than guessing.
|
|
259
|
+
*/
|
|
260
|
+
export function measureFooterGeometry(
|
|
261
|
+
row: unknown,
|
|
262
|
+
options: { appliedLift: number; gapEm: number; fallbackFontSize: number },
|
|
263
|
+
): FooterGeometry | null {
|
|
264
|
+
const node = webRow(row);
|
|
265
|
+
if (node === null) return null;
|
|
266
|
+
const anchor = locateFooter(node);
|
|
267
|
+
if (anchor === null) return null;
|
|
268
|
+
|
|
269
|
+
const footerBottom = lastTextBottom(anchor.footer);
|
|
270
|
+
if (footerBottom === null) return null;
|
|
271
|
+
|
|
272
|
+
const fontSize = footerFontSize(anchor.footer, options.fallbackFontSize);
|
|
273
|
+
// The margin is applied to the row itself, not to the wrapper the sibling is
|
|
274
|
+
// found through: the wrapper keeps its top while the row is pulled up inside
|
|
275
|
+
// it. Take the lift back out of the row's own box.
|
|
276
|
+
const naturalTop = node.getBoundingClientRect().top - options.appliedLift;
|
|
277
|
+
const gapNow = naturalTop - footerBottom;
|
|
278
|
+
// Level with or above the footer text already: this layout is not the one the
|
|
279
|
+
// lift is for, and moving further up would cover the answer.
|
|
280
|
+
if (gapNow <= 0) return null;
|
|
281
|
+
// Too far for a footer to be the element above: it is some other block.
|
|
282
|
+
if (gapNow > lineHeightFor(fontSize) * MAX_GAP_LINES) return null;
|
|
283
|
+
|
|
284
|
+
const gap = Math.round(fontSize * options.gapEm);
|
|
285
|
+
const lift = Math.max(-Math.round(lineHeightFor(fontSize) * MAX_GAP_LINES), gap - gapNow);
|
|
286
|
+
return { lift: Math.min(0, lift), fontSize };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Calls `onResize` when the row's neighbourhood changes — a theme switching, a
|
|
291
|
+
* font loading, the footer mounting a frame late, or the turn being pushed down
|
|
292
|
+
* by a newer one.
|
|
293
|
+
*
|
|
294
|
+
* Resizing is only half of it: when a new turn is appended the row's own box and
|
|
295
|
+
* its ancestors keep their size and only move, so a ResizeObserver never fires.
|
|
296
|
+
* A MutationObserver on the same ancestors catches the insertion, and the two
|
|
297
|
+
* are coalesced so a burst of changes costs one measurement, taken after the
|
|
298
|
+
* layout has settled.
|
|
299
|
+
*
|
|
300
|
+
* Returns a cleanup, or null off web.
|
|
301
|
+
*/
|
|
302
|
+
export function watchRow(row: unknown, onResize: () => void): (() => void) | null {
|
|
303
|
+
const node = webRow(row);
|
|
304
|
+
if (node === null) return null;
|
|
305
|
+
const Resize = (globalThis as { ResizeObserver?: ResizeObserverConstructor }).ResizeObserver;
|
|
306
|
+
const Mutation = (globalThis as { MutationObserver?: MutationObserverConstructor })
|
|
307
|
+
.MutationObserver;
|
|
308
|
+
if (Resize === undefined && Mutation === undefined) return null;
|
|
309
|
+
|
|
310
|
+
let pending: ReturnType<typeof setTimeout> | null = null;
|
|
311
|
+
const schedule = () => {
|
|
312
|
+
if (pending !== null) clearTimeout(pending);
|
|
313
|
+
pending = setTimeout(() => {
|
|
314
|
+
pending = null;
|
|
315
|
+
onResize();
|
|
316
|
+
}, 60);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
const stop: Array<() => void> = [];
|
|
320
|
+
if (Resize !== undefined) {
|
|
321
|
+
const observer = new Resize(schedule);
|
|
322
|
+
if (Mutation !== undefined) {
|
|
323
|
+
const mutations = new Mutation(schedule);
|
|
324
|
+
let frame: MeasuredNode | null = node;
|
|
325
|
+
for (let depth = 0; depth < WATCH_DEPTH && frame !== null; depth += 1) {
|
|
326
|
+
observer.observe(frame);
|
|
327
|
+
// Child insertions are where a new turn shows up; character data
|
|
328
|
+
// (streaming text) is deliberately not watched.
|
|
329
|
+
mutations.observe(frame, { childList: true, subtree: true });
|
|
330
|
+
frame = frame.parentElement;
|
|
331
|
+
}
|
|
332
|
+
stop.push(() => mutations.disconnect());
|
|
333
|
+
} else {
|
|
334
|
+
let frame: MeasuredNode | null = node;
|
|
335
|
+
for (let depth = 0; depth < WATCH_DEPTH && frame !== null; depth += 1) {
|
|
336
|
+
observer.observe(frame);
|
|
337
|
+
frame = frame.parentElement;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
stop.push(() => observer.disconnect());
|
|
341
|
+
} else if (Mutation !== undefined) {
|
|
342
|
+
const mutations = new Mutation(schedule);
|
|
343
|
+
mutations.observe(node.parentElement ?? node, { childList: true, subtree: true });
|
|
344
|
+
stop.push(() => mutations.disconnect());
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return () => {
|
|
348
|
+
if (pending !== null) clearTimeout(pending);
|
|
349
|
+
for (const dispose of stop) dispose();
|
|
350
|
+
};
|
|
351
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "paseo-dsh",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.18",
|
|
4
4
|
"description": "DeepSeek Harness (dsh) as a native Paseo provider: question cards, plan mode, thinking levels, steering, images and a real slash menu. 把 DeepSeek Harness 接成 Paseo 原生 provider。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"paseo",
|