paseo-dsh 0.2.13 → 0.2.15
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/settings-screen.tsx +148 -0
- package/client/stream-stats.tsx +127 -28
- package/client/web.ts +117 -0
- package/index.client.tsx +12 -1
- package/index.server.ts +2 -0
- package/package.json +2 -1
- package/server/session.ts +23 -0
- package/shared/settings.ts +29 -0
package/client/react-native.d.ts
CHANGED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { PluginSurfaceProps } from "@getpaseo/plugin/client";
|
|
2
|
+
import { useSettings } from "@getpaseo/plugin/client";
|
|
3
|
+
import { Pressable, Text, View } from "react-native";
|
|
4
|
+
import { DEFAULT_STREAM_STATS, streamStatsSettings, type StreamStatsSettings } from "../shared/settings";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Settings → Plugins → 回答统计行.
|
|
8
|
+
*
|
|
9
|
+
* The host renders plugin settings only as screens a plugin registers, so the
|
|
10
|
+
* two numbers the stats row reads are edited here: how far the row sits under
|
|
11
|
+
* the completed-turn footer, and how large its text is. Both default to the
|
|
12
|
+
* geometry Paseo's own footer draws with, so leaving this page alone keeps the
|
|
13
|
+
* row looking like part of that footer.
|
|
14
|
+
*/
|
|
15
|
+
const FIELDS: Array<{
|
|
16
|
+
key: keyof StreamStatsSettings;
|
|
17
|
+
label: string;
|
|
18
|
+
hint: string;
|
|
19
|
+
min: number;
|
|
20
|
+
max: number;
|
|
21
|
+
step: number;
|
|
22
|
+
unit: string;
|
|
23
|
+
}> = [
|
|
24
|
+
{
|
|
25
|
+
key: "gapPx",
|
|
26
|
+
label: "与页脚的间距",
|
|
27
|
+
hint: "回合页脚最后一行文字到统计行的距离",
|
|
28
|
+
min: 0,
|
|
29
|
+
max: 24,
|
|
30
|
+
step: 1,
|
|
31
|
+
unit: "px",
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
key: "fontSize",
|
|
35
|
+
label: "字号",
|
|
36
|
+
hint: "Paseo 自己的回合页脚用 13px",
|
|
37
|
+
min: 10,
|
|
38
|
+
max: 16,
|
|
39
|
+
step: 1,
|
|
40
|
+
unit: "px",
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
export function StreamStatsSettingsScreen({ theme }: PluginSurfaceProps) {
|
|
45
|
+
const settings = useSettings(streamStatsSettings);
|
|
46
|
+
const values = settings.status === "ready" ? settings.values : DEFAULT_STREAM_STATS;
|
|
47
|
+
const editable = settings.status === "ready" && !settings.saving;
|
|
48
|
+
|
|
49
|
+
const set = (key: keyof StreamStatsSettings, next: number) => {
|
|
50
|
+
if (settings.status !== "ready" || settings.saving) return;
|
|
51
|
+
if (settings.values[key] === next) return;
|
|
52
|
+
void settings.save({ ...settings.values, [key]: next }, settings.revision);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const muted = theme.colors.foregroundMuted;
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<View style={{ padding: 20, gap: 18 }}>
|
|
59
|
+
{FIELDS.map((field) => {
|
|
60
|
+
const value = values[field.key];
|
|
61
|
+
return (
|
|
62
|
+
<View key={field.key} style={{ gap: 6 }}>
|
|
63
|
+
<Text style={{ color: theme.colors.foreground, fontSize: 14 }}>{field.label}</Text>
|
|
64
|
+
<Text style={{ color: muted, fontSize: 12 }}>{field.hint}</Text>
|
|
65
|
+
<View style={{ flexDirection: "row", alignItems: "center", gap: 10 }}>
|
|
66
|
+
<StepButton
|
|
67
|
+
label="−"
|
|
68
|
+
disabled={!editable || value <= field.min}
|
|
69
|
+
border={theme.colors.border}
|
|
70
|
+
color={theme.colors.foreground}
|
|
71
|
+
onPress={() => set(field.key, Math.max(field.min, value - field.step))}
|
|
72
|
+
/>
|
|
73
|
+
<Text
|
|
74
|
+
style={{
|
|
75
|
+
color: theme.colors.foreground,
|
|
76
|
+
fontSize: 14,
|
|
77
|
+
minWidth: 64,
|
|
78
|
+
textAlign: "center",
|
|
79
|
+
fontVariant: ["tabular-nums"],
|
|
80
|
+
}}
|
|
81
|
+
>
|
|
82
|
+
{`${value} ${field.unit}`}
|
|
83
|
+
</Text>
|
|
84
|
+
<StepButton
|
|
85
|
+
label="+"
|
|
86
|
+
disabled={!editable || value >= field.max}
|
|
87
|
+
border={theme.colors.border}
|
|
88
|
+
color={theme.colors.foreground}
|
|
89
|
+
onPress={() => set(field.key, Math.min(field.max, value + field.step))}
|
|
90
|
+
/>
|
|
91
|
+
</View>
|
|
92
|
+
</View>
|
|
93
|
+
);
|
|
94
|
+
})}
|
|
95
|
+
<Pressable
|
|
96
|
+
accessibilityRole="button"
|
|
97
|
+
accessibilityLabel="恢复默认"
|
|
98
|
+
disabled={!editable}
|
|
99
|
+
onPress={() => {
|
|
100
|
+
if (settings.status !== "ready" || settings.saving) return;
|
|
101
|
+
void settings.save({ ...DEFAULT_STREAM_STATS }, settings.revision);
|
|
102
|
+
}}
|
|
103
|
+
style={{ alignSelf: "flex-start", paddingVertical: 6 }}
|
|
104
|
+
>
|
|
105
|
+
<Text style={{ color: muted, fontSize: 12 }}>恢复默认(间距 6px,字号 13px)</Text>
|
|
106
|
+
</Pressable>
|
|
107
|
+
{settings.status === "ready" ? null : (
|
|
108
|
+
<Text style={{ color: theme.colors.statusWarning, fontSize: 12 }}>
|
|
109
|
+
{settings.status === "loading"
|
|
110
|
+
? "正在读取设置…"
|
|
111
|
+
: `设置不可用:${settings.error}`}
|
|
112
|
+
</Text>
|
|
113
|
+
)}
|
|
114
|
+
{settings.saveError === null ? null : (
|
|
115
|
+
<Text style={{ color: theme.colors.statusDanger, fontSize: 12 }}>{settings.saveError}</Text>
|
|
116
|
+
)}
|
|
117
|
+
</View>
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function StepButton(props: {
|
|
122
|
+
label: string;
|
|
123
|
+
disabled: boolean;
|
|
124
|
+
border: string;
|
|
125
|
+
color: string;
|
|
126
|
+
onPress: () => void;
|
|
127
|
+
}) {
|
|
128
|
+
return (
|
|
129
|
+
<Pressable
|
|
130
|
+
accessibilityRole="button"
|
|
131
|
+
accessibilityLabel={props.label}
|
|
132
|
+
disabled={props.disabled}
|
|
133
|
+
onPress={props.onPress}
|
|
134
|
+
style={{
|
|
135
|
+
width: 30,
|
|
136
|
+
height: 30,
|
|
137
|
+
alignItems: "center",
|
|
138
|
+
justifyContent: "center",
|
|
139
|
+
borderWidth: 1,
|
|
140
|
+
borderColor: props.border,
|
|
141
|
+
borderRadius: 6,
|
|
142
|
+
opacity: props.disabled ? 0.4 : 1,
|
|
143
|
+
}}
|
|
144
|
+
>
|
|
145
|
+
<Text style={{ color: props.color, fontSize: 15 }}>{props.label}</Text>
|
|
146
|
+
</Pressable>
|
|
147
|
+
);
|
|
148
|
+
}
|
package/client/stream-stats.tsx
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import type { PluginClientContext, PluginTimelineItemProps } from "@getpaseo/plugin/client";
|
|
2
|
-
import {
|
|
2
|
+
import { useSettings } from "@getpaseo/plugin/client";
|
|
3
|
+
import { Fragment, useLayoutEffect, useRef, useState } from "react";
|
|
3
4
|
import { Pressable, Text, View } from "react-native";
|
|
4
5
|
import { z } from "zod";
|
|
6
|
+
import { DEFAULT_STREAM_STATS, streamStatsSettings } from "../shared/settings";
|
|
7
|
+
import { measureFooterLift, watchFooter } from "./web";
|
|
5
8
|
|
|
6
9
|
export const streamStatsSchema = z.object({
|
|
7
10
|
firstTokenMs: z.number().optional(),
|
|
@@ -20,10 +23,38 @@ export const streamStatsSchema = z.object({
|
|
|
20
23
|
steps: z.number().optional(),
|
|
21
24
|
sessionTokens: z.number().optional(),
|
|
22
25
|
cacheHit: z.number().optional(),
|
|
26
|
+
/** Last stats row of a turn: Paseo puts its completed-turn footer above it. */
|
|
27
|
+
final: z.boolean().optional(),
|
|
23
28
|
});
|
|
24
29
|
|
|
25
30
|
type StreamStats = z.infer<typeof streamStatsSchema>;
|
|
26
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Paseo draws the completed-turn footer — copy / model / time — directly under
|
|
34
|
+
* the answer and keeps its own bottom padding free under it, while this row is
|
|
35
|
+
* an ordinary timeline item spaced by the host's item gap. Neither number is
|
|
36
|
+
* ours to rely on: a theme plugin restyles that footer, and the same footer is
|
|
37
|
+
* also drawn under turns that are no longer the newest, where the host's
|
|
38
|
+
* margins differ (the row sits 35px under the footer while its turn is latest,
|
|
39
|
+
* 23px once a newer turn arrives).
|
|
40
|
+
*
|
|
41
|
+
* On web the row measures the footer actually painted above it (in `./web`,
|
|
42
|
+
* the only client file allowed to touch the DOM) and lifts itself to the gap
|
|
43
|
+
* the user chose in Settings → Plugins. Native has no DOM to measure; it keeps
|
|
44
|
+
* the geometry the host's footer was tuned for.
|
|
45
|
+
*/
|
|
46
|
+
/** Paseo's `SPACING[8]` (`TURN_FOOTER_BOTTOM_SPACING`): the footer's bottom padding. */
|
|
47
|
+
const TURN_FOOTER_BOTTOM_PADDING = 32;
|
|
48
|
+
/**
|
|
49
|
+
* The furthest the row may pull up. Measured gaps can exceed Paseo's own 32px
|
|
50
|
+
* footer padding once a theme restyles the footer, so the cap is twice that:
|
|
51
|
+
* generous enough for themed footers, still bounded so a mis-measure cannot
|
|
52
|
+
* drag the row across the answer.
|
|
53
|
+
*/
|
|
54
|
+
const MAX_ROW_LIFT = -2 * TURN_FOOTER_BOTTOM_PADDING;
|
|
55
|
+
/** Paseo's footer sets its text in a 18px line box at 13px. */
|
|
56
|
+
const LINE_HEIGHT_RATIO = 18 / 13;
|
|
57
|
+
|
|
27
58
|
function formatLatency(ms: number): string {
|
|
28
59
|
return ms < 1000 ? `${Math.round(ms)}ms` : `${(ms / 1000).toFixed(1)}s`;
|
|
29
60
|
}
|
|
@@ -32,21 +63,19 @@ function formatNumber(value: number): string {
|
|
|
32
63
|
return String(Math.round(value)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
33
64
|
}
|
|
34
65
|
|
|
35
|
-
function
|
|
36
|
-
if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}M`;
|
|
37
|
-
if (value >= 10_000) return `${Math.round(value / 1000)}K`;
|
|
38
|
-
if (value >= 1000) return `${(value / 1000).toFixed(1)}K`;
|
|
39
|
-
return String(Math.round(value));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function contextLine(data: StreamStats): string | undefined {
|
|
66
|
+
function contextRatio(data: StreamStats): number | undefined {
|
|
43
67
|
if (data.contextUsed === undefined || data.contextMax === undefined || data.contextMax <= 0) {
|
|
44
68
|
return undefined;
|
|
45
69
|
}
|
|
46
|
-
|
|
70
|
+
return Math.max(0, Math.min(1, data.contextUsed / data.contextMax));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function contextLine(data: StreamStats): string | undefined {
|
|
74
|
+
const ratio = contextRatio(data);
|
|
75
|
+
if (ratio === undefined) return undefined;
|
|
47
76
|
const filled = Math.round(ratio * 10);
|
|
48
77
|
const bar = `${"█".repeat(filled)}${"░".repeat(10 - filled)}`;
|
|
49
|
-
return `上下文 ${formatNumber(data.contextUsed)} / ${formatNumber(data.contextMax)} ${bar} ${Math.round(ratio * 100)}%`;
|
|
78
|
+
return `上下文 ${formatNumber(data.contextUsed ?? 0)} / ${formatNumber(data.contextMax ?? 0)} ${bar} ${Math.round(ratio * 100)}%`;
|
|
50
79
|
}
|
|
51
80
|
|
|
52
81
|
function tokensLine(data: StreamStats): string | undefined {
|
|
@@ -69,44 +98,114 @@ function toolsLine(data: StreamStats): string | undefined {
|
|
|
69
98
|
return `工具 ${data.toolCalls} 次${duration}`;
|
|
70
99
|
}
|
|
71
100
|
|
|
72
|
-
/** One
|
|
101
|
+
/** One compact line under an answer; tap to expand the full picture. */
|
|
73
102
|
function StreamStatsRow({ item, theme, layout }: PluginTimelineItemProps<StreamStats>) {
|
|
74
103
|
const [open, setOpen] = useState(false);
|
|
104
|
+
const [lift, setLift] = useState(0);
|
|
105
|
+
const rowRef = useRef<unknown>(null);
|
|
106
|
+
/** The lift currently in the DOM, which the next measurement has to discount. */
|
|
107
|
+
const appliedLift = useRef(0);
|
|
108
|
+
const settings = useSettings(streamStatsSettings);
|
|
109
|
+
const gap = settings.status === "ready" ? settings.values.gapPx : DEFAULT_STREAM_STATS.gapPx;
|
|
110
|
+
const fontSize = settings.status === "ready" ? settings.values.fontSize : DEFAULT_STREAM_STATS.fontSize;
|
|
75
111
|
const data = item.data;
|
|
76
|
-
|
|
112
|
+
|
|
113
|
+
// The ref must describe the margin the DOM actually carries, not the one a
|
|
114
|
+
// pending render is about to apply: a ResizeObserver callback between
|
|
115
|
+
// `setLift` and its commit would otherwise discount a lift the frame has not
|
|
116
|
+
// moved by yet and measure against the wrong origin.
|
|
117
|
+
useLayoutEffect(() => {
|
|
118
|
+
appliedLift.current = lift;
|
|
119
|
+
}, [lift]);
|
|
120
|
+
|
|
121
|
+
const applyLift = (next: number) => {
|
|
122
|
+
if (appliedLift.current === next) return;
|
|
123
|
+
setLift(next);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// The footer above this row is the host's markup and, with a theme plugin
|
|
127
|
+
// installed, that plugin's restyling of it — so its height and padding are
|
|
128
|
+
// measured instead of assumed. Everything DOM lives in `./web`; off web it
|
|
129
|
+
// reports nothing, the row keeps the host's own gap, and native falls back to
|
|
130
|
+
// the constant `nativeLift` below.
|
|
131
|
+
useLayoutEffect(() => {
|
|
132
|
+
if (data.final !== true) {
|
|
133
|
+
applyLift(0);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const measure = () => {
|
|
137
|
+
const next = measureFooterLift(rowRef.current, {
|
|
138
|
+
appliedLift: appliedLift.current,
|
|
139
|
+
gap,
|
|
140
|
+
maxLift: MAX_ROW_LIFT,
|
|
141
|
+
});
|
|
142
|
+
applyLift(next ?? 0);
|
|
143
|
+
};
|
|
144
|
+
measure();
|
|
145
|
+
const stop = watchFooter(rowRef.current, measure);
|
|
146
|
+
return stop ?? undefined;
|
|
147
|
+
// `applyLift` only reads refs and a state setter, so the values that can
|
|
148
|
+
// change what is measured are the only dependencies.
|
|
149
|
+
}, [data.final, gap]);
|
|
150
|
+
|
|
151
|
+
const muted = theme.colors.foregroundMuted;
|
|
152
|
+
const base = {
|
|
153
|
+
color: muted,
|
|
154
|
+
fontSize,
|
|
155
|
+
lineHeight: Math.round(fontSize * LINE_HEIGHT_RATIO),
|
|
156
|
+
fontVariant: ["tabular-nums"] as const,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const ratio = contextRatio(data);
|
|
160
|
+
const segments: Array<{ text: string; color: string }> = [];
|
|
77
161
|
if (data.turns !== undefined && data.steps !== undefined) {
|
|
78
|
-
|
|
162
|
+
segments.push({ text: `${data.turns} 轮 ${data.steps} 步`, color: muted });
|
|
163
|
+
}
|
|
164
|
+
if (data.firstTokenMs !== undefined) {
|
|
165
|
+
segments.push({ text: `首字 ${formatLatency(data.firstTokenMs)}`, color: muted });
|
|
166
|
+
}
|
|
167
|
+
if (data.tokensPerSecond !== undefined) {
|
|
168
|
+
segments.push({ text: `${Math.round(data.tokensPerSecond)} tok/s`, color: muted });
|
|
169
|
+
}
|
|
170
|
+
if (ratio !== undefined) {
|
|
171
|
+
segments.push({ text: `上下文 ${Math.round(ratio * 100)}%`, color: muted });
|
|
79
172
|
}
|
|
80
|
-
if (data.
|
|
81
|
-
|
|
82
|
-
if (data.sessionTokens !== undefined) summary.push(`${formatCompact(data.sessionTokens)} tok`);
|
|
83
|
-
if (data.cacheHit !== undefined) summary.push(`缓存命中 ${Math.round(data.cacheHit * 100)}%`);
|
|
84
|
-
if (summary.length === 0 && data.firstTokenMs !== undefined) {
|
|
85
|
-
summary.push(`首字 ${formatLatency(data.firstTokenMs)}`);
|
|
173
|
+
if (data.cacheHit !== undefined) {
|
|
174
|
+
segments.push({ text: `缓存命中 ${Math.round(data.cacheHit * 100)}%`, color: muted });
|
|
86
175
|
}
|
|
87
|
-
if (
|
|
176
|
+
if (segments.length === 0) return null;
|
|
88
177
|
|
|
89
178
|
const details = [contextLine(data), tokensLine(data), routeLine(data), toolsLine(data)].filter(
|
|
90
179
|
(line): line is string => line !== undefined,
|
|
91
180
|
);
|
|
92
|
-
const
|
|
93
|
-
const muted = { color: theme.colors.foregroundMuted, fontSize };
|
|
181
|
+
const nativeLift = gap - TURN_FOOTER_BOTTOM_PADDING;
|
|
94
182
|
|
|
95
183
|
return (
|
|
96
184
|
<Pressable
|
|
185
|
+
ref={rowRef}
|
|
97
186
|
accessibilityRole="button"
|
|
98
187
|
accessibilityLabel={open ? "收起回答统计" : "展开回答统计"}
|
|
99
188
|
onPress={() => setOpen((value) => !value)}
|
|
100
|
-
style={{
|
|
189
|
+
style={{
|
|
190
|
+
alignSelf: "flex-start",
|
|
191
|
+
marginTop: data.final === true ? (layout.platform === "web" ? lift : nativeLift) : 0,
|
|
192
|
+
marginBottom: 0,
|
|
193
|
+
paddingLeft: 2,
|
|
194
|
+
}}
|
|
101
195
|
>
|
|
102
|
-
<View style={{ flexDirection: "row", alignItems: "center",
|
|
103
|
-
|
|
104
|
-
|
|
196
|
+
<View style={{ flexDirection: "row", alignItems: "center", flexWrap: "wrap" }}>
|
|
197
|
+
{segments.map((segment, index) => (
|
|
198
|
+
<Fragment key={segment.text}>
|
|
199
|
+
{index > 0 ? <Text style={base}> · </Text> : null}
|
|
200
|
+
<Text style={{ ...base, color: segment.color }}>{segment.text}</Text>
|
|
201
|
+
</Fragment>
|
|
202
|
+
))}
|
|
203
|
+
<Text style={base}>{open ? " ▴" : " ▾"}</Text>
|
|
105
204
|
</View>
|
|
106
205
|
{open ? (
|
|
107
206
|
<View style={{ marginTop: 2, gap: 1 }}>
|
|
108
207
|
{details.map((line) => (
|
|
109
|
-
<Text key={line} style={
|
|
208
|
+
<Text key={line} style={base}>
|
|
110
209
|
{line}
|
|
111
210
|
</Text>
|
|
112
211
|
))}
|
package/client/web.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { Platform } from "react-native";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The one client file that reads the DOM.
|
|
5
|
+
*
|
|
6
|
+
* Paseo runs plugin client code on iOS and Android too, where none of these
|
|
7
|
+
* globals exist, so every export here is a no-op off web and the shared
|
|
8
|
+
* renderers only ever see numbers (or null). Keeping `getBoundingClientRect`,
|
|
9
|
+
* `querySelectorAll` and `ResizeObserver` in this file means a host change to
|
|
10
|
+
* its own layout costs one file to fix, and an SSR build has one place to
|
|
11
|
+
* stub out.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** The DOM shape read here; `tsconfig` ships no DOM typings. */
|
|
15
|
+
interface MeasuredNode {
|
|
16
|
+
parentElement: MeasuredNode | null;
|
|
17
|
+
previousElementSibling: MeasuredNode | null;
|
|
18
|
+
children: ArrayLike<MeasuredNode>;
|
|
19
|
+
textContent: string | null;
|
|
20
|
+
getBoundingClientRect(): { top: number; bottom: number; height: number };
|
|
21
|
+
querySelectorAll(selector: string): ArrayLike<MeasuredNode>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type ResizeObserverConstructor = new (callback: () => void) => {
|
|
25
|
+
observe(target: unknown): void;
|
|
26
|
+
disconnect(): void;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Where the footer above an item ends and where the item's own frame starts. */
|
|
30
|
+
interface FooterAnchor {
|
|
31
|
+
frame: MeasuredNode;
|
|
32
|
+
above: MeasuredNode;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The frame this item was laid out in, found from the row rendered inside it:
|
|
37
|
+
* wrapper frames that hold nothing but this row are stepped over until a
|
|
38
|
+
* sibling with height appears above.
|
|
39
|
+
*/
|
|
40
|
+
function locate(row: MeasuredNode): FooterAnchor | null {
|
|
41
|
+
let frame: MeasuredNode | null = row;
|
|
42
|
+
for (let depth = 0; depth < 8 && frame !== null; depth += 1) {
|
|
43
|
+
const above = frame.previousElementSibling;
|
|
44
|
+
if (above !== null && above.getBoundingClientRect().height > 0) return { frame, above };
|
|
45
|
+
frame = frame.parentElement;
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Bottom edge of the last visible line of text inside `node`. */
|
|
51
|
+
function lastTextBottom(node: MeasuredNode): number | null {
|
|
52
|
+
let bottom: number | null = null;
|
|
53
|
+
const lines = node.querySelectorAll("*");
|
|
54
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
55
|
+
const line = lines[index];
|
|
56
|
+
if (line === undefined || line.children.length > 0) continue;
|
|
57
|
+
if (line.textContent === null || line.textContent.trim() === "") continue;
|
|
58
|
+
const rect = line.getBoundingClientRect();
|
|
59
|
+
if (rect.height <= 0) continue;
|
|
60
|
+
bottom = bottom === null ? rect.bottom : Math.max(bottom, rect.bottom);
|
|
61
|
+
}
|
|
62
|
+
return bottom;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function webRow(row: unknown): MeasuredNode | null {
|
|
66
|
+
if (Platform.OS !== "web" || row === null || row === undefined) return null;
|
|
67
|
+
return row as MeasuredNode;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* How far `row` has to pull itself up so its frame sits `gap` px under the last
|
|
72
|
+
* line of text drawn above it — the completed-turn footer, or the answer's own
|
|
73
|
+
* last line when a theme draws no footer.
|
|
74
|
+
*
|
|
75
|
+
* `appliedLift` is the margin the row is carrying right now: the frame has
|
|
76
|
+
* already moved by it, so it is taken back out before measuring. Without that,
|
|
77
|
+
* the row would measure the position it just moved itself to and bounce between
|
|
78
|
+
* two places.
|
|
79
|
+
*
|
|
80
|
+
* Returns null when there is nothing to measure — native, a row that is not in
|
|
81
|
+
* the DOM yet, no sibling above — and the caller falls back to its own layout.
|
|
82
|
+
*/
|
|
83
|
+
export function measureFooterLift(
|
|
84
|
+
row: unknown,
|
|
85
|
+
options: { appliedLift: number; gap: number; maxLift: number },
|
|
86
|
+
): number | null {
|
|
87
|
+
const node = webRow(row);
|
|
88
|
+
if (node === null) return null;
|
|
89
|
+
const anchor = locate(node);
|
|
90
|
+
if (anchor === null) return null;
|
|
91
|
+
const footerBottom = lastTextBottom(anchor.above);
|
|
92
|
+
if (footerBottom === null) return null;
|
|
93
|
+
const naturalTop = anchor.frame.getBoundingClientRect().top - options.appliedLift;
|
|
94
|
+
const lift = options.gap - (naturalTop - footerBottom);
|
|
95
|
+
return Math.max(options.maxLift, Math.min(0, Math.round(lift)));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Calls `onResize` whenever the footer above `row` changes size — a theme
|
|
100
|
+
* switching, a font loading, the turn being pushed down by a newer one.
|
|
101
|
+
* Returns a cleanup, or null off web or when there is nothing to watch.
|
|
102
|
+
*/
|
|
103
|
+
export function watchFooter(row: unknown, onResize: () => void): (() => void) | null {
|
|
104
|
+
const node = webRow(row);
|
|
105
|
+
if (node === null) return null;
|
|
106
|
+
const anchor = locate(node);
|
|
107
|
+
if (anchor === null) return null;
|
|
108
|
+
const Observer = (globalThis as { ResizeObserver?: ResizeObserverConstructor }).ResizeObserver;
|
|
109
|
+
if (Observer === undefined) return null;
|
|
110
|
+
const observer = new Observer(onResize);
|
|
111
|
+
observer.observe(anchor.above);
|
|
112
|
+
// The 12px between "latest turn" and "pushed down by a newer turn" can come
|
|
113
|
+
// from the container around this item rather than from the footer's own box.
|
|
114
|
+
// Watching the parent catches both without polling.
|
|
115
|
+
if (anchor.frame.parentElement !== null) observer.observe(anchor.frame.parentElement);
|
|
116
|
+
return () => observer.disconnect();
|
|
117
|
+
}
|
package/index.client.tsx
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import type { PluginClientContext } from "@getpaseo/plugin/client";
|
|
2
|
+
import { StreamStatsSettingsScreen } from "./client/settings-screen";
|
|
2
3
|
import { contributeStreamStats } from "./client/stream-stats";
|
|
3
4
|
|
|
4
5
|
export default function contribute(client: PluginClientContext) {
|
|
5
|
-
|
|
6
|
+
const removeStats = contributeStreamStats(client);
|
|
7
|
+
const removeSettings = client.addSettingsScreen({
|
|
8
|
+
id: "stream-stats",
|
|
9
|
+
title: "回答统计行",
|
|
10
|
+
icon: "SlidersHorizontal",
|
|
11
|
+
Component: StreamStatsSettingsScreen,
|
|
12
|
+
});
|
|
13
|
+
return () => {
|
|
14
|
+
removeStats();
|
|
15
|
+
removeSettings();
|
|
16
|
+
};
|
|
6
17
|
}
|
package/index.server.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PluginServerContext } from "@getpaseo/plugin/server";
|
|
2
2
|
import { ensurePaseoProfile } from "./server/bootstrap";
|
|
3
3
|
import { createDshProvider } from "./server/provider";
|
|
4
|
+
import { streamStatsSettings } from "./shared/settings";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Register the DeepSeek Harness provider, provisioning the DSH profile it needs.
|
|
@@ -24,6 +25,7 @@ export default function contribute(server: PluginServerContext) {
|
|
|
24
25
|
);
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
server.registerSettings(streamStatsSettings);
|
|
27
29
|
server.registerProvider(createDshProvider());
|
|
28
30
|
return () => {};
|
|
29
31
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "paseo-dsh",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.15",
|
|
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",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"paseo-plugin.json",
|
|
27
27
|
"index.server.ts",
|
|
28
28
|
"server/",
|
|
29
|
+
"shared/",
|
|
29
30
|
"lib/",
|
|
30
31
|
"bin/",
|
|
31
32
|
"icon.svg",
|
package/server/session.ts
CHANGED
|
@@ -114,6 +114,18 @@ export class DshSession {
|
|
|
114
114
|
/** Tool call start times and the current turn's tool total. */
|
|
115
115
|
#toolStarts = new Map<string, number>();
|
|
116
116
|
#turnTool = { count: 0, ms: 0 };
|
|
117
|
+
/**
|
|
118
|
+
* Whether the running turn was stopped by the user. A cancelled turn may not
|
|
119
|
+
* get the completed-turn footer that the stats row is pulled into, so it
|
|
120
|
+
* keeps the default spacing instead of risking an overlap.
|
|
121
|
+
*/
|
|
122
|
+
#turnInterrupted = false;
|
|
123
|
+
/**
|
|
124
|
+
* Whether the stats row is the newest timeline item. If tools arrive after
|
|
125
|
+
* it, the completed-turn footer belongs to the end of the turn rather than
|
|
126
|
+
* directly above this row, so the row must keep the default spacing.
|
|
127
|
+
*/
|
|
128
|
+
#statsIsTail = false;
|
|
117
129
|
/** Most recent stats row, so context and tool totals can update it in place. */
|
|
118
130
|
#lastStats: { id: string; data: Record<string, unknown> } | undefined;
|
|
119
131
|
/** Per-step timing for the streamed-stats row. */
|
|
@@ -609,6 +621,7 @@ export class DshSession {
|
|
|
609
621
|
* conversation stays continuable — killing the process stranded it instead.
|
|
610
622
|
*/
|
|
611
623
|
async interrupt(): Promise<void> {
|
|
624
|
+
this.#turnInterrupted = true;
|
|
612
625
|
this.#flushLive();
|
|
613
626
|
this.#clearLive();
|
|
614
627
|
await this.#require().cancel();
|
|
@@ -636,6 +649,7 @@ export class DshSession {
|
|
|
636
649
|
if (this.#turnId === null) {
|
|
637
650
|
this.#toolStarts.clear();
|
|
638
651
|
this.#turnTool = { count: 0, ms: 0 };
|
|
652
|
+
this.#turnInterrupted = false;
|
|
639
653
|
// A later usage refresh must not rewrite the previous turn's row.
|
|
640
654
|
this.#lastStats = undefined;
|
|
641
655
|
}
|
|
@@ -653,6 +667,14 @@ export class DshSession {
|
|
|
653
667
|
const turnId = this.#turnId;
|
|
654
668
|
this.#turnId = null;
|
|
655
669
|
this.#options.emit({ type: "session.turn", sessionId: this.id, turnId, state: "completed" });
|
|
670
|
+
// Paseo renders the completed-turn footer (copy / model / time) beneath
|
|
671
|
+
// the answer only after this event, and reserves its own padding under
|
|
672
|
+
// that footer. Flag the last stats row in the same tick so the client can
|
|
673
|
+
// close the gap without first flashing the row in the wrong position.
|
|
674
|
+
if (this.#lastStats !== undefined && !this.#turnInterrupted && this.#statsIsTail) {
|
|
675
|
+
this.#lastStats.data = { ...this.#lastStats.data, final: true };
|
|
676
|
+
this.#emitStatsRow();
|
|
677
|
+
}
|
|
656
678
|
// The turn's request reported fresh provider usage; refresh occupancy now
|
|
657
679
|
// so the readout is current even when no further turn arrives.
|
|
658
680
|
void this.refreshUsage();
|
|
@@ -840,6 +862,7 @@ export class DshSession {
|
|
|
840
862
|
}
|
|
841
863
|
|
|
842
864
|
#emitItem(item: ProviderTimelineItem): void {
|
|
865
|
+
this.#statsIsTail = item.type === "plugin" && item.kind === "dsh-stream-stats";
|
|
843
866
|
this.#options.emit({ type: "timeline.item", sessionId: this.id, item });
|
|
844
867
|
}
|
|
845
868
|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { defineSettings } from "@getpaseo/plugin";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* How the answer-stats row sits under a completed turn's footer.
|
|
6
|
+
*
|
|
7
|
+
* The defaults are what the row was drawn against: Paseo draws that footer at
|
|
8
|
+
* `STREAM_METADATA_FONT_SIZE` (13px), and 6px above the row reads as one block
|
|
9
|
+
* with it. A theme plugin redraws the footer, and a reader may want a
|
|
10
|
+
* different rhythm or text size, so both numbers are host settings instead of
|
|
11
|
+
* constants — registered by `index.server.ts`, read by the row and by the
|
|
12
|
+
* settings screen.
|
|
13
|
+
*/
|
|
14
|
+
export const streamStatsSettings = defineSettings({
|
|
15
|
+
id: "stream-stats",
|
|
16
|
+
scope: "host",
|
|
17
|
+
version: 1,
|
|
18
|
+
schema: z.object({
|
|
19
|
+
/** Space between the footer's last line and the stats row, in px. */
|
|
20
|
+
gapPx: z.number().min(0).max(24).default(6),
|
|
21
|
+
/** The row's text size; Paseo's own footer draws at 13. */
|
|
22
|
+
fontSize: z.number().min(10).max(16).default(13),
|
|
23
|
+
}),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export type StreamStatsSettings = z.output<typeof streamStatsSettings.schema>;
|
|
27
|
+
|
|
28
|
+
/** What the row falls back to while the settings are still loading. */
|
|
29
|
+
export const DEFAULT_STREAM_STATS: StreamStatsSettings = { gapPx: 6, fontSize: 13 };
|