Skip to content

Commit 971b512

Browse files
committed
✨ feat(streamdown): add fixed and range TPS playground controls
1 parent 28cd5fe commit 971b512

6 files changed

Lines changed: 204 additions & 71 deletions

File tree

‎packages/streamdown/site/src/components/Controls.tsx‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const Segmented = <T extends string>({
6767
);
6868

6969
interface RangeProps {
70+
hint?: string;
7071
label: string;
7172
max: number;
7273
min: number;
@@ -76,12 +77,25 @@ interface RangeProps {
7677
value: number;
7778
}
7879

79-
export const Range = ({ label, max, min, onChange, step = 1, unit, value }: RangeProps) => (
80+
export const Range = ({ hint, label, max, min, onChange, step = 1, unit, value }: RangeProps) => (
8081
<div className="field">
8182
<label className="field-label" htmlFor={`range-${label}`}>
8283
<span>{label}</span>
8384
<span className="field-value">
84-
{value} {unit}
85+
<input
86+
className="field-value-input"
87+
max={max}
88+
min={min}
89+
step={step}
90+
type="number"
91+
value={value}
92+
onChange={(e) => {
93+
const next = Number(e.target.value);
94+
if (!Number.isFinite(next)) return;
95+
onChange(Math.min(max, Math.max(min, next)));
96+
}}
97+
/>
98+
{unit}
8599
</span>
86100
</label>
87101
<input
@@ -95,6 +109,7 @@ export const Range = ({ label, max, min, onChange, step = 1, unit, value }: Rang
95109
value={value}
96110
onChange={(e) => onChange(Number(e.target.value))}
97111
/>
112+
{hint ? <div className="field-hint">{hint}</div> : null}
98113
</div>
99114
);
100115

‎packages/streamdown/site/src/lib/useLocalStream.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ import { useCallback, useEffect, useState } from 'react';
22

33
interface UseLocalStreamOptions {
44
chunkSize: number;
5-
delayMs: number;
5+
delayMs?: number;
66
jitter?: number;
77
loop?: boolean;
8+
tps?: number;
9+
tpsMax?: number;
810
}
911

1012
const scatter = (value: number, jitter: number) =>
1113
value * (1 + (Math.random() * 2 - 1) * jitter);
1214

15+
const delayFromTps = (chunkSize: number, tps: number) =>
16+
Math.max(1, Math.round((chunkSize / Math.max(tps, 0.001)) * 1000));
17+
1318
export const useLocalStream = (
1419
content: string,
15-
{ chunkSize, delayMs, jitter = 0.5, loop = false }: UseLocalStreamOptions,
20+
{ chunkSize, delayMs = 24, jitter = 0.5, loop = false, tps, tpsMax }: UseLocalStreamOptions,
1621
) => {
1722
const [text, setText] = useState('');
1823
const [session, setSession] = useState(0);
@@ -28,23 +33,31 @@ export const useLocalStream = (
2833
if (cancelled) return;
2934

3035
const size = Math.max(1, Math.round(scatter(chunkSize, jitter)));
36+
const rate =
37+
tps == null
38+
? null
39+
: tpsMax != null && tpsMax > tps
40+
? tps + Math.random() * (tpsMax - tps)
41+
: tps;
42+
const wait =
43+
rate == null ? Math.max(1, scatter(delayMs, jitter)) : delayFromTps(size, rate);
3144
position = Math.min(content.length, position + size);
3245
setText(content.slice(0, position));
3346

3447
if (position < content.length) {
35-
timer = setTimeout(tick, Math.max(1, scatter(delayMs, jitter)));
48+
timer = setTimeout(tick, wait);
3649
} else if (loop) {
3750
timer = setTimeout(() => setSession((s) => s + 1), 3000);
3851
}
3952
};
4053

41-
timer = setTimeout(tick, delayMs);
54+
timer = setTimeout(tick, tps == null ? delayMs : delayFromTps(chunkSize, tps));
4255

4356
return () => {
4457
cancelled = true;
4558
clearTimeout(timer);
4659
};
47-
}, [content, chunkSize, delayMs, jitter, loop, session]);
60+
}, [content, chunkSize, delayMs, jitter, loop, session, tps, tpsMax]);
4861

4962
const restart = useCallback(() => setSession((s) => s + 1), []);
5063

‎packages/streamdown/site/src/sections/Playground.tsx‎

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,54 @@ const GRANULARITY_OPTIONS: { label: string; value: StreamAnimationGranularity }[
3434
{ label: 'Word', value: 'word' },
3535
];
3636

37+
const TPS_MODE_OPTIONS = [
38+
{ label: 'Fixed', value: 'fixed' },
39+
{ label: 'Range', value: 'range' },
40+
] as const;
41+
42+
type TpsMode = (typeof TPS_MODE_OPTIONS)[number]['value'];
43+
3744
export const Playground = () => {
3845
const [sampleKey, setSampleKey] = useState<SampleKey>('markdown');
3946
const [chunkSize, setChunkSize] = useState(6);
40-
const [delayMs, setDelayMs] = useState(24);
47+
const [tpsMode, setTpsMode] = useState<TpsMode>('fixed');
48+
const [tps, setTps] = useState(250);
49+
const [tpsMin, setTpsMin] = useState(80);
50+
const [tpsMax, setTpsMax] = useState(400);
4151
const [jitter, setJitter] = useState(50);
4252
const [smoothing, setSmoothing] = useState<StreamSmoothingPreset>('balanced');
4353
const [granularity, setGranularity] = useState<StreamAnimationGranularity>('char');
4454
const [latexGuard, setLatexGuard] = useState(true);
4555
const [preprocess, setPreprocess] = useState(true);
4656

57+
const rateMin = tpsMode === 'range' ? Math.min(tpsMin, tpsMax) : tps;
58+
const rateMax = tpsMode === 'range' ? Math.max(tpsMin, tpsMax) : tps;
59+
const batchDelayMs = Math.max(1, Math.round((chunkSize / Math.max(rateMin, 0.001)) * 1000));
60+
4761
const { onScroll: onOutputScroll, ref: outputRef } = useStickToBottom();
4862
const profiler = useMemo(() => createStreamdownProfiler({ label: 'playground' }), []);
4963
const { restart, text } = useLocalStream(samples[sampleKey].content, {
5064
chunkSize,
51-
delayMs,
5265
jitter: jitter / 100,
66+
tps: rateMin,
67+
tpsMax: rateMax,
5368
});
5469
const remarkPlugins = useMemo(() => [remarkGfm, remarkMath], []);
5570
const rehypePlugins = useMemo(() => [rehypeKatex], []);
5671

5772
useEffect(() => {
5873
profiler.reset('playground');
59-
}, [profiler, sampleKey, chunkSize, delayMs, jitter, smoothing, granularity, preprocess]);
74+
}, [
75+
profiler,
76+
sampleKey,
77+
chunkSize,
78+
rateMin,
79+
rateMax,
80+
jitter,
81+
smoothing,
82+
granularity,
83+
preprocess,
84+
]);
6085

6186
return (
6287
<section className="playground" id="playground">
@@ -83,12 +108,53 @@ export const Playground = () => {
83108
if (key in paragraphBatchCases) {
84109
const preset = paragraphBatchCases[key as keyof typeof paragraphBatchCases];
85110
setChunkSize(preset.chunkSize);
86-
setDelayMs(preset.delayMs);
111+
setTps(Math.round((preset.chunkSize / preset.delayMs) * 1000));
112+
setTpsMode('fixed');
87113
setJitter(0);
88114
}
89115
restart();
90116
}}
91117
/>
118+
<Segmented
119+
label="TPS"
120+
options={TPS_MODE_OPTIONS}
121+
value={tpsMode}
122+
onChange={setTpsMode}
123+
/>
124+
{tpsMode === 'fixed' ? (
125+
<Range
126+
hint={`${batchDelayMs}ms / batch at ${chunkSize} ch`}
127+
label="Rate"
128+
max={5000}
129+
min={10}
130+
step={10}
131+
unit="chars/s"
132+
value={tps}
133+
onChange={setTps}
134+
/>
135+
) : (
136+
<>
137+
<Range
138+
label="TPS min"
139+
max={5000}
140+
min={10}
141+
step={10}
142+
unit="chars/s"
143+
value={tpsMin}
144+
onChange={setTpsMin}
145+
/>
146+
<Range
147+
hint={`${Math.max(1, Math.round((chunkSize / Math.max(rateMax, 0.001)) * 1000))}–${batchDelayMs}ms / batch`}
148+
label="TPS max"
149+
max={5000}
150+
min={10}
151+
step={10}
152+
unit="chars/s"
153+
value={tpsMax}
154+
onChange={setTpsMax}
155+
/>
156+
</>
157+
)}
92158
<Range
93159
label="Chunk size"
94160
max={100}
@@ -97,14 +163,6 @@ export const Playground = () => {
97163
value={chunkSize}
98164
onChange={setChunkSize}
99165
/>
100-
<Range
101-
label="Chunk delay"
102-
max={500}
103-
min={4}
104-
unit="ms"
105-
value={delayMs}
106-
onChange={setDelayMs}
107-
/>
108166
<Range
109167
label="Jitter"
110168
max={100}

‎packages/streamdown/site/src/styles.css‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,43 @@ section[id] {
394394
}
395395

396396
.field-value {
397+
display: inline-flex;
398+
align-items: baseline;
399+
gap: 4px;
397400
font-family: var(--mono);
398401
font-size: 12px;
399402
color: var(--fg);
400403
font-variant-numeric: tabular-nums;
401404
}
402405

406+
.field-value-input {
407+
width: 4.8em;
408+
margin: 0;
409+
padding: 0;
410+
border: 0;
411+
background: transparent;
412+
color: inherit;
413+
font: inherit;
414+
font-variant-numeric: tabular-nums;
415+
text-align: right;
416+
-moz-appearance: textfield;
417+
}
418+
419+
.field-value-input::-webkit-inner-spin-button,
420+
.field-value-input::-webkit-outer-spin-button {
421+
appearance: none;
422+
}
423+
424+
.field-value-input:focus {
425+
outline: none;
426+
}
427+
428+
.field-hint {
429+
margin-top: 6px;
430+
font-size: 11px;
431+
color: var(--fg-3);
432+
}
433+
403434
.seg {
404435
display: flex;
405436
gap: 2px;

0 commit comments

Comments
 (0)