@tonyclaw/llm-inspector 1.16.4 → 1.17.0
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/.output/nitro.json +1 -1
- package/.output/public/assets/CompareDrawer-C4fie5g5.js +1 -0
- package/.output/public/assets/ReplayDialog-Dme5uOR9.js +1 -0
- package/.output/public/assets/RequestAnatomy-ChBLDNFH.js +1 -0
- package/.output/public/assets/ResponseView-wGeqBzVU.js +1 -0
- package/.output/public/assets/StreamingChunkSequence-zeJZQLqT.js +1 -0
- package/.output/public/assets/index-DoGvsnbA.css +1 -0
- package/.output/public/assets/index-DpbutOvo.js +101 -0
- package/.output/public/assets/json-viewer-BV-WUszW.js +14 -0
- package/.output/public/assets/{main-DbWwVQFh.js → main-DRu10KNQ.js} +1 -1
- package/.output/server/_libs/lucide-react.mjs +105 -85
- package/.output/server/_ssr/CompareDrawer-C4-CQL5w.mjs +1040 -0
- package/.output/server/_ssr/ReplayDialog-BTb1Bam8.mjs +321 -0
- package/.output/server/_ssr/RequestAnatomy-CZFV1IvL.mjs +351 -0
- package/.output/server/_ssr/ResponseView-CTZekh65.mjs +601 -0
- package/.output/server/_ssr/StreamingChunkSequence-C38Ynabd.mjs +301 -0
- package/.output/server/_ssr/{index-C-z-fZtq.mjs → index-Cnu-QzAy.mjs} +1141 -2443
- package/.output/server/_ssr/index.mjs +2 -2
- package/.output/server/_ssr/json-viewer-DROqpjS9.mjs +510 -0
- package/.output/server/_ssr/{router-CNM9Kbi0.mjs → router-pP4GCTQx.mjs} +42 -18
- package/.output/server/{_tanstack-start-manifest_v-BWfLeIsC.mjs → _tanstack-start-manifest_v-CphS4rZd.mjs} +1 -1
- package/.output/server/index.mjs +69 -27
- package/package.json +1 -1
- package/src/components/OnboardingBanner.tsx +2 -2
- package/src/components/ProxyViewer.tsx +44 -27
- package/src/components/ProxyViewerContainer.tsx +5 -25
- package/src/components/providers/SettingsDialog.tsx +52 -1
- package/src/components/proxy-viewer/ConversationGroup.tsx +5 -1
- package/src/components/proxy-viewer/ConversationHeader.tsx +4 -1
- package/src/components/proxy-viewer/LogEntry.tsx +217 -181
- package/src/components/proxy-viewer/LogEntryHeader.tsx +181 -40
- package/src/components/proxy-viewer/ThreadConnector.tsx +17 -2
- package/src/components/proxy-viewer/TurnGroup.tsx +124 -72
- package/src/components/proxy-viewer/anatomy/RequestAnatomy.tsx +98 -0
- package/src/components/proxy-viewer/anatomy/SegmentBar.tsx +196 -0
- package/src/components/proxy-viewer/anatomy/tokenEstimate.ts +53 -0
- package/src/components/proxy-viewer/anatomy/types.ts +39 -0
- package/src/components/proxy-viewer/anatomy/useAnatomyJump.ts +114 -0
- package/src/components/proxy-viewer/formats/anthropic/ContentBlocks.tsx +3 -23
- package/src/components/proxy-viewer/formats/anthropic/thinkingExtract.ts +21 -0
- package/src/components/proxy-viewer/formats/openai/ResponseView.tsx +5 -3
- package/src/components/proxy-viewer/lazy.ts +37 -0
- package/src/components/proxy-viewer/log-formats/anthropic.ts +146 -0
- package/src/components/proxy-viewer/log-formats/openai.ts +127 -0
- package/src/components/proxy-viewer/log-formats/types.ts +7 -0
- package/src/components/proxy-viewer/log-formats/unknown.ts +4 -0
- package/src/components/proxy-viewer/logEntryVisibility.ts +39 -0
- package/src/components/proxy-viewer/useKeyboardNavigation.ts +190 -0
- package/src/components/proxy-viewer/viewerState.ts +8 -0
- package/src/components/ui/crab-variants.tsx +11 -0
- package/src/components/ui/json-expansion-button.tsx +56 -0
- package/src/components/ui/json-viewer-bulk.ts +97 -0
- package/src/components/ui/json-viewer.tsx +58 -183
- package/src/lib/runtimeConfig.ts +9 -0
- package/src/lib/useOnboarding.ts +7 -1
- package/src/lib/useStripConfig.ts +33 -2
- package/src/lib/utils.ts +2 -3
- package/src/proxy/config.ts +17 -7
- package/src/routes/api/config.ts +7 -0
- package/src/routes/api/logs.stream.ts +26 -16
- package/.output/public/assets/index-DRRCmu5p.css +0 -1
- package/.output/public/assets/index-X7CHS7fS.js +0 -107
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Info } from "lucide-react";
|
|
2
|
+
import { type JSX, useMemo } from "react";
|
|
3
|
+
import { cn, formatTokens } from "../../../lib/utils";
|
|
4
|
+
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "../../ui/tooltip";
|
|
5
|
+
import { SegmentBar } from "./SegmentBar";
|
|
6
|
+
import type { AnatomySegment } from "./types";
|
|
7
|
+
|
|
8
|
+
export type RequestAnatomyProps = {
|
|
9
|
+
/** Parsed request body, or `null` if it cannot be parsed. */
|
|
10
|
+
parsed: unknown | null;
|
|
11
|
+
/** Server-reported input token count, or `null` if unknown. */
|
|
12
|
+
inputTokens: number | null;
|
|
13
|
+
/** Optional callback fired when the user activates a segment. */
|
|
14
|
+
onSegmentActivate?: (segment: AnatomySegment) => void;
|
|
15
|
+
/** Pre-computed segments; if provided, `parsed` is ignored. */
|
|
16
|
+
segments?: AnatomySegment[] | null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const DIVERGENCE_AMBER_THRESHOLD = 0.25;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Render the request anatomy view: a one-line summary plus the
|
|
23
|
+
* stacked segment bar. Returns `null` when the request cannot be
|
|
24
|
+
* parsed or the segment list is empty.
|
|
25
|
+
*/
|
|
26
|
+
export function RequestAnatomy({
|
|
27
|
+
parsed,
|
|
28
|
+
inputTokens,
|
|
29
|
+
onSegmentActivate,
|
|
30
|
+
segments: providedSegments,
|
|
31
|
+
}: RequestAnatomyProps): JSX.Element | null {
|
|
32
|
+
// Caller is expected to pre-compute segments in production (see
|
|
33
|
+
// LogEntry). When called standalone, fall back to deriving from
|
|
34
|
+
// `parsed` directly using the unknown adapter is not appropriate —
|
|
35
|
+
// the caller is responsible for choosing the right adapter.
|
|
36
|
+
const segments = useMemo(() => providedSegments ?? null, [providedSegments]);
|
|
37
|
+
const total = useMemo(() => (segments ?? []).reduce((sum, s) => sum + s.size, 0), [segments]);
|
|
38
|
+
|
|
39
|
+
// Show divergence warning when both estimate and server numbers exist.
|
|
40
|
+
const showDivergenceWarning = useMemo(() => {
|
|
41
|
+
if (segments === null || segments === undefined) return false;
|
|
42
|
+
if (inputTokens === null) return false;
|
|
43
|
+
if (total === 0) return false;
|
|
44
|
+
const ratio = Math.abs(inputTokens - total) / Math.max(inputTokens, total);
|
|
45
|
+
return ratio >= DIVERGENCE_AMBER_THRESHOLD;
|
|
46
|
+
}, [inputTokens, segments, total]);
|
|
47
|
+
|
|
48
|
+
const summaryColorClass = useMemo(() => {
|
|
49
|
+
if (inputTokens === null) return "text-muted-foreground";
|
|
50
|
+
if (showDivergenceWarning) return "text-amber-400";
|
|
51
|
+
return "text-muted-foreground";
|
|
52
|
+
}, [inputTokens, showDivergenceWarning]);
|
|
53
|
+
|
|
54
|
+
if (segments === null || segments === undefined) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
if (segments.length === 0) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
if (parsed === null && providedSegments === undefined) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<TooltipProvider delayDuration={150}>
|
|
66
|
+
<div className="px-4 py-3 space-y-2" data-testid="anatomy-root">
|
|
67
|
+
<div className="flex items-center gap-2 text-xs">
|
|
68
|
+
<span className="font-mono tabular-nums text-foreground">
|
|
69
|
+
~{formatTokens(total)} tokens
|
|
70
|
+
</span>
|
|
71
|
+
{inputTokens !== null && (
|
|
72
|
+
<span className={cn("font-mono tabular-nums", summaryColorClass)}>
|
|
73
|
+
(server: {formatTokens(inputTokens)})
|
|
74
|
+
</span>
|
|
75
|
+
)}
|
|
76
|
+
{showDivergenceWarning && (
|
|
77
|
+
<Tooltip>
|
|
78
|
+
<TooltipTrigger asChild>
|
|
79
|
+
<button
|
|
80
|
+
type="button"
|
|
81
|
+
className="inline-flex items-center text-amber-400 hover:text-amber-300"
|
|
82
|
+
aria-label="Token estimate diverges from server"
|
|
83
|
+
>
|
|
84
|
+
<Info className="size-3.5" />
|
|
85
|
+
</button>
|
|
86
|
+
</TooltipTrigger>
|
|
87
|
+
<TooltipContent className="max-w-xs text-xs">
|
|
88
|
+
Bar uses a token estimate heuristic (~4 ASCII chars / token, ~1 CJK / emoji char per
|
|
89
|
+
token). The server's reported value is the source of truth for cost.
|
|
90
|
+
</TooltipContent>
|
|
91
|
+
</Tooltip>
|
|
92
|
+
)}
|
|
93
|
+
</div>
|
|
94
|
+
<SegmentBar segments={segments} totalTokens={total} onActivate={onSegmentActivate} />
|
|
95
|
+
</div>
|
|
96
|
+
</TooltipProvider>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { type JSX, memo, useMemo } from "react";
|
|
2
|
+
import { cn, formatTokens } from "../../../lib/utils";
|
|
3
|
+
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "../../ui/tooltip";
|
|
4
|
+
import type { AnatomyRole, AnatomySegment } from "./types";
|
|
5
|
+
|
|
6
|
+
const ROLE_COLOR_CLASSES: Record<AnatomyRole, string> = {
|
|
7
|
+
system: "bg-sky-500/70",
|
|
8
|
+
user: "bg-emerald-500/70",
|
|
9
|
+
assistant: "bg-violet-500/70",
|
|
10
|
+
tool: "bg-amber-500/70",
|
|
11
|
+
tools: "bg-slate-500/70",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const ROLE_FOCUS_RING: Record<AnatomyRole, string> = {
|
|
15
|
+
system: "focus-visible:ring-sky-300",
|
|
16
|
+
user: "focus-visible:ring-emerald-300",
|
|
17
|
+
assistant: "focus-visible:ring-violet-300",
|
|
18
|
+
tool: "focus-visible:ring-amber-300",
|
|
19
|
+
tools: "focus-visible:ring-slate-300",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const MAX_VISIBLE_SEGMENTS = 12;
|
|
23
|
+
const MIN_SEGMENT_PERCENT = 1;
|
|
24
|
+
|
|
25
|
+
const TOOLTIP_PREVIEW_LIMIT = 80;
|
|
26
|
+
const LABEL_TRUNCATE_LIMIT = 24;
|
|
27
|
+
|
|
28
|
+
function truncateLabel(label: string): string {
|
|
29
|
+
if (label.length <= LABEL_TRUNCATE_LIMIT) return label;
|
|
30
|
+
return `${label.slice(0, LABEL_TRUNCATE_LIMIT - 1)}…`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function truncatePreview(text: string): string {
|
|
34
|
+
const singleLine = text.replace(/\s+/g, " ").trim();
|
|
35
|
+
if (singleLine.length <= TOOLTIP_PREVIEW_LIMIT) return singleLine;
|
|
36
|
+
return `${singleLine.slice(0, TOOLTIP_PREVIEW_LIMIT)}…`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type SegmentBarProps = {
|
|
40
|
+
segments: ReadonlyArray<AnatomySegment>;
|
|
41
|
+
totalTokens: number;
|
|
42
|
+
onActivate?: (segment: AnatomySegment) => void;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Render a horizontal stacked bar showing the relative size of each
|
|
47
|
+
* request segment, with role-based color, per-segment labels, and
|
|
48
|
+
* an overflow pill that aggregates any segments beyond the visible
|
|
49
|
+
* cap. Click or keyboard activation (Enter / Space) calls `onActivate`.
|
|
50
|
+
*/
|
|
51
|
+
export const SegmentBar = memo(function SegmentBar({
|
|
52
|
+
segments,
|
|
53
|
+
totalTokens,
|
|
54
|
+
onActivate,
|
|
55
|
+
}: SegmentBarProps): JSX.Element {
|
|
56
|
+
const total = useMemo(() => {
|
|
57
|
+
if (totalTokens > 0) return totalTokens;
|
|
58
|
+
return segments.reduce((sum, s) => sum + s.size, 0);
|
|
59
|
+
}, [segments, totalTokens]);
|
|
60
|
+
|
|
61
|
+
const visibleSegments = segments.slice(0, MAX_VISIBLE_SEGMENTS);
|
|
62
|
+
const overflowSegments = segments.slice(MAX_VISIBLE_SEGMENTS);
|
|
63
|
+
const overflowSize = overflowSegments.reduce((sum, s) => sum + s.size, 0);
|
|
64
|
+
const overflowCount = overflowSegments.length;
|
|
65
|
+
const overflowStartIndex = MAX_VISIBLE_SEGMENTS;
|
|
66
|
+
const overflowEndIndex = overflowStartIndex + overflowCount - 1;
|
|
67
|
+
const hasOverflow = overflowCount > 0;
|
|
68
|
+
|
|
69
|
+
const visibleTotal = useMemo(
|
|
70
|
+
() => visibleSegments.reduce((sum, s) => sum + s.size, 0),
|
|
71
|
+
[visibleSegments],
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const ariaLabel = useMemo(
|
|
75
|
+
() =>
|
|
76
|
+
`Request anatomy: ~${formatTokens(total)} tokens across ${segments.length} segment${segments.length === 1 ? "" : "s"}`,
|
|
77
|
+
[segments.length, total],
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
if (segments.length === 0 || total <= 0) {
|
|
81
|
+
return <div role="img" aria-label={ariaLabel} className="h-6" />;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<TooltipProvider delayDuration={150}>
|
|
86
|
+
<div role="img" aria-label={ariaLabel} className="flex flex-col gap-1.5">
|
|
87
|
+
<div
|
|
88
|
+
className="relative flex h-6 w-full overflow-hidden rounded border border-border/40 bg-muted/30"
|
|
89
|
+
data-testid="anatomy-segment-bar"
|
|
90
|
+
>
|
|
91
|
+
{visibleSegments.map((segment, index) => {
|
|
92
|
+
const rawPercent = total > 0 ? (segment.size / total) * 100 : 0;
|
|
93
|
+
const percent = Math.max(MIN_SEGMENT_PERCENT, rawPercent);
|
|
94
|
+
return (
|
|
95
|
+
<Tooltip key={`${segment.path}-${index}`}>
|
|
96
|
+
<TooltipTrigger asChild>
|
|
97
|
+
<button
|
|
98
|
+
type="button"
|
|
99
|
+
role="button"
|
|
100
|
+
tabIndex={0}
|
|
101
|
+
onClick={() => onActivate?.(segment)}
|
|
102
|
+
onKeyDown={(e) => {
|
|
103
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
104
|
+
e.preventDefault();
|
|
105
|
+
onActivate?.(segment);
|
|
106
|
+
}
|
|
107
|
+
}}
|
|
108
|
+
data-anatomy-path={segment.path}
|
|
109
|
+
aria-label={`${segment.label}, ~${formatTokens(segment.size)} tokens`}
|
|
110
|
+
className={cn(
|
|
111
|
+
"h-full border-r border-background/80 last:border-r-0",
|
|
112
|
+
"opacity-90 hover:opacity-100 focus:opacity-100",
|
|
113
|
+
"focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-1 focus-visible:ring-offset-background",
|
|
114
|
+
ROLE_COLOR_CLASSES[segment.role],
|
|
115
|
+
ROLE_FOCUS_RING[segment.role],
|
|
116
|
+
)}
|
|
117
|
+
style={{ width: `${percent}%` }}
|
|
118
|
+
/>
|
|
119
|
+
</TooltipTrigger>
|
|
120
|
+
<TooltipContent side="bottom" className="max-w-sm text-xs p-2 space-y-0.5">
|
|
121
|
+
<div className="font-semibold">
|
|
122
|
+
{segment.label} · ~{formatTokens(segment.size)} tokens
|
|
123
|
+
</div>
|
|
124
|
+
<div className="text-muted-foreground">
|
|
125
|
+
{segment.characters.toLocaleString()} chars
|
|
126
|
+
</div>
|
|
127
|
+
{segment.text.length > 0 && (
|
|
128
|
+
<div className="text-muted-foreground/90 break-words whitespace-pre-wrap">
|
|
129
|
+
{truncatePreview(segment.text)}
|
|
130
|
+
</div>
|
|
131
|
+
)}
|
|
132
|
+
</TooltipContent>
|
|
133
|
+
</Tooltip>
|
|
134
|
+
);
|
|
135
|
+
})}
|
|
136
|
+
{hasOverflow && (
|
|
137
|
+
<Tooltip>
|
|
138
|
+
<TooltipTrigger asChild>
|
|
139
|
+
<div
|
|
140
|
+
role="img"
|
|
141
|
+
aria-label={`${overflowCount} additional segments`}
|
|
142
|
+
className="flex h-full items-center justify-center bg-muted-foreground/30 px-1.5 text-[10px] font-mono text-background"
|
|
143
|
+
style={{
|
|
144
|
+
width: `${Math.max(MIN_SEGMENT_PERCENT, (overflowSize / total) * 100)}%`,
|
|
145
|
+
}}
|
|
146
|
+
>
|
|
147
|
+
… +{overflowCount}
|
|
148
|
+
</div>
|
|
149
|
+
</TooltipTrigger>
|
|
150
|
+
<TooltipContent side="bottom" className="text-xs">
|
|
151
|
+
{overflowCount} more segment{overflowCount === 1 ? "" : "s"} (indices{" "}
|
|
152
|
+
{overflowStartIndex}–{overflowEndIndex})
|
|
153
|
+
</TooltipContent>
|
|
154
|
+
</Tooltip>
|
|
155
|
+
)}
|
|
156
|
+
</div>
|
|
157
|
+
|
|
158
|
+
<div className="flex w-full gap-1 text-[10px] text-muted-foreground">
|
|
159
|
+
{visibleSegments.map((segment, index) => {
|
|
160
|
+
const rawPercent = total > 0 ? (segment.size / total) * 100 : 0;
|
|
161
|
+
const percent = Math.max(MIN_SEGMENT_PERCENT, rawPercent);
|
|
162
|
+
return (
|
|
163
|
+
<div
|
|
164
|
+
key={`label-${segment.path}-${index}`}
|
|
165
|
+
className="flex flex-col gap-0.5 truncate"
|
|
166
|
+
style={{ width: `${percent}%` }}
|
|
167
|
+
title={`${segment.label} · ~${formatTokens(segment.size)} tokens`}
|
|
168
|
+
>
|
|
169
|
+
<span className="truncate font-mono text-foreground/80">
|
|
170
|
+
{truncateLabel(segment.label)}
|
|
171
|
+
</span>
|
|
172
|
+
<span className="truncate font-mono text-muted-foreground/70">
|
|
173
|
+
~{formatTokens(segment.size)}
|
|
174
|
+
</span>
|
|
175
|
+
</div>
|
|
176
|
+
);
|
|
177
|
+
})}
|
|
178
|
+
{hasOverflow && (
|
|
179
|
+
<div
|
|
180
|
+
className="flex flex-col gap-0.5 truncate text-muted-foreground"
|
|
181
|
+
style={{ width: `${Math.max(MIN_SEGMENT_PERCENT, (overflowSize / total) * 100)}%` }}
|
|
182
|
+
>
|
|
183
|
+
<span className="truncate font-mono text-foreground/60">… +{overflowCount}</span>
|
|
184
|
+
<span className="truncate font-mono text-muted-foreground/60">
|
|
185
|
+
~{formatTokens(overflowSize)}
|
|
186
|
+
</span>
|
|
187
|
+
</div>
|
|
188
|
+
)}
|
|
189
|
+
</div>
|
|
190
|
+
|
|
191
|
+
{/* Spacer to maintain minimum bar height for tiny segments */}
|
|
192
|
+
{visibleTotal < total * 0.1 && <div className="h-0" />}
|
|
193
|
+
</div>
|
|
194
|
+
</TooltipProvider>
|
|
195
|
+
);
|
|
196
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Blended 4-chars-per-token heuristic with CJK / emoji adjustment.
|
|
3
|
+
*
|
|
4
|
+
* Rationale: Anthropic and OpenAI both document ~4 chars/token for
|
|
5
|
+
* English / Latin / punctuation. CJK characters, Hangul, kana, fullwidth
|
|
6
|
+
* forms, and emoji tokenize roughly 1:1 in practice. We use a simple
|
|
7
|
+
* two-bucket counter rather than a full BPE tokenizer to keep the
|
|
8
|
+
* estimate cheap and synchronous — the result is shown with a leading
|
|
9
|
+
* `~` so the user is never misled about precision.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** A single Unicode code-point character from `text`. */
|
|
13
|
+
function forEachCodePoint(text: string, visit: (ch: string) => void): void {
|
|
14
|
+
// `Array.from` iterates by code point, so surrogate pairs and
|
|
15
|
+
// combining marks stay together.
|
|
16
|
+
for (const ch of Array.from(text)) {
|
|
17
|
+
visit(ch);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const CJK_REGEX = /[\u3000-\u9FFF\uAC00-\uD7FF\uF900-\uFAFF\uFF00-\uFFEF\u{1F300}-\u{1FAFF}]/u;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Estimate the token count of a piece of text.
|
|
25
|
+
*
|
|
26
|
+
* - CJK / Hangul / kana / fullwidth / emoji → 1 token per character
|
|
27
|
+
* - Everything else → 1 token per 4 characters, rounded up
|
|
28
|
+
*
|
|
29
|
+
* Returns 0 for the empty string.
|
|
30
|
+
*/
|
|
31
|
+
export function estimateTokens(text: string): number {
|
|
32
|
+
if (text.length === 0) return 0;
|
|
33
|
+
let cjk = 0;
|
|
34
|
+
let other = 0;
|
|
35
|
+
forEachCodePoint(text, (ch) => {
|
|
36
|
+
if (CJK_REGEX.test(ch)) {
|
|
37
|
+
cjk += 1;
|
|
38
|
+
} else {
|
|
39
|
+
other += 1;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return Math.ceil(cjk * 1) + Math.ceil(other / 4);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Count Unicode code points in a string (graphemes ≈ characters for our purposes). */
|
|
46
|
+
export function countCharacters(text: string): number {
|
|
47
|
+
if (text.length === 0) return 0;
|
|
48
|
+
let n = 0;
|
|
49
|
+
forEachCodePoint(text, () => {
|
|
50
|
+
n += 1;
|
|
51
|
+
});
|
|
52
|
+
return n;
|
|
53
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Role of a request segment. Drives the segment's color in the anatomy bar.
|
|
3
|
+
*
|
|
4
|
+
* - `system`: the system prompt (Anthropic `system`, or leading system-role message in OpenAI)
|
|
5
|
+
* - `user`: a user-role message
|
|
6
|
+
* - `assistant`: an assistant-role message
|
|
7
|
+
* - `tool`: a tool-role message (OpenAI only — Anthropic uses tool_result blocks inside user messages)
|
|
8
|
+
* - `tools`: the synthetic "tool definitions" segment that aggregates the `tools` array
|
|
9
|
+
*/
|
|
10
|
+
export type AnatomyRole = "system" | "user" | "assistant" | "tool" | "tools";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A single segment of a request as visualized in the Anatomy tab.
|
|
14
|
+
*
|
|
15
|
+
* `path` is a JSON-pointer-style path into the parsed body
|
|
16
|
+
* (e.g. `/system`, `/messages/0`, `/tools`). The path is opaque to
|
|
17
|
+
* consumers — it is matched against the same path emitted by the
|
|
18
|
+
* request analyzer, and is used to drive click-to-jump in the
|
|
19
|
+
* `JsonViewer`. A `null` path is allowed for the synthetic `tools`
|
|
20
|
+
* segment in the rare case where the tools array cannot be located.
|
|
21
|
+
*/
|
|
22
|
+
export type AnatomySegment = {
|
|
23
|
+
/** Stable role used for color and label prefix. */
|
|
24
|
+
role: AnatomyRole;
|
|
25
|
+
/** Human-readable short label shown below the segment (e.g. "system", "[3] user", "tools"). */
|
|
26
|
+
label: string;
|
|
27
|
+
/** Estimated token count for this segment. Drives bar width. */
|
|
28
|
+
size: number;
|
|
29
|
+
/** Character count for this segment (raw text length). Used in the tooltip. */
|
|
30
|
+
characters: number;
|
|
31
|
+
/** Raw text of the segment used to compute `size`. Used in the tooltip preview. */
|
|
32
|
+
text: string;
|
|
33
|
+
/**
|
|
34
|
+
* JSON-pointer-style path into the parsed body (e.g. `/messages/3`).
|
|
35
|
+
* `null` when the segment does not correspond to a single node
|
|
36
|
+
* (e.g. the synthetic `tools` segment that aggregates the array).
|
|
37
|
+
*/
|
|
38
|
+
path: string;
|
|
39
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { type RefObject, useCallback, useRef } from "react";
|
|
2
|
+
import type { AnatomySegment } from "./types";
|
|
3
|
+
|
|
4
|
+
const HIGHLIGHT_DURATION_MS = 1200;
|
|
5
|
+
/**
|
|
6
|
+
* Cap on the number of animation frames we are willing to wait
|
|
7
|
+
* before giving up on finding the target row. Each frame is
|
|
8
|
+
* ~16ms at 60Hz, so 12 frames gives ~200ms — enough headroom for
|
|
9
|
+
* the cascade of `useEffect` -> `setExpanded` calls that fires
|
|
10
|
+
* for each ancestor on the way down to the target.
|
|
11
|
+
*/
|
|
12
|
+
const MAX_HIGHLIGHT_ATTEMPTS = 12;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Build a `jumpToSegment` callback that:
|
|
16
|
+
*
|
|
17
|
+
* 1. Optionally switches the active tab to the Request tab (so the
|
|
18
|
+
* target row is in the DOM and visible).
|
|
19
|
+
* 2. Asks the JSON viewer to expand the path's ancestors via the
|
|
20
|
+
* `expandToPath` prop (which is reset to `null` shortly after).
|
|
21
|
+
* 3. Polls via `requestAnimationFrame` for the target row to appear
|
|
22
|
+
* (the cascade of `useEffect` -> `setExpanded` calls needs
|
|
23
|
+
* multiple frames for deep paths), then scrolls the matching
|
|
24
|
+
* row into view and applies a 1.2 s primary-color ring
|
|
25
|
+
* highlight via a temporary class toggle.
|
|
26
|
+
*/
|
|
27
|
+
export function useAnatomyJump(options: {
|
|
28
|
+
/** Ref to the DOM root that contains the JSON tree rows. */
|
|
29
|
+
containerRef: RefObject<HTMLElement | null>;
|
|
30
|
+
/** Setter for the `JsonViewer`'s `expandToPath` prop. */
|
|
31
|
+
setExpandToPath: (path: string | null) => void;
|
|
32
|
+
/**
|
|
33
|
+
* Optional hook called before the DOM lookup so the caller can
|
|
34
|
+
* make the Request tab active. The hook should be synchronous
|
|
35
|
+
* (it triggers a React state update which is flushed by the
|
|
36
|
+
* subsequent `requestAnimationFrame`).
|
|
37
|
+
*/
|
|
38
|
+
ensureTabActive?: () => void;
|
|
39
|
+
highlightMs?: number;
|
|
40
|
+
}): (segment: AnatomySegment) => void {
|
|
41
|
+
const { containerRef, setExpandToPath, ensureTabActive, highlightMs } = options;
|
|
42
|
+
const highlightTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
43
|
+
|
|
44
|
+
return useCallback(
|
|
45
|
+
(segment: AnatomySegment) => {
|
|
46
|
+
ensureTabActive?.();
|
|
47
|
+
|
|
48
|
+
// Trigger the JSON viewer to expand ancestors of this path.
|
|
49
|
+
// We pass the same path; the JsonViewer resets the prop via
|
|
50
|
+
// the caller clearing the state after the scroll lands.
|
|
51
|
+
setExpandToPath(segment.path);
|
|
52
|
+
|
|
53
|
+
const applyHighlight = (target: HTMLElement): void => {
|
|
54
|
+
target.scrollIntoView({ behavior: "smooth", block: "center" });
|
|
55
|
+
target.classList.add(
|
|
56
|
+
"ring-2",
|
|
57
|
+
"ring-primary/60",
|
|
58
|
+
"ring-offset-1",
|
|
59
|
+
"ring-offset-background",
|
|
60
|
+
"rounded-sm",
|
|
61
|
+
"transition-shadow",
|
|
62
|
+
);
|
|
63
|
+
if (highlightTimer.current !== null) clearTimeout(highlightTimer.current);
|
|
64
|
+
highlightTimer.current = setTimeout(() => {
|
|
65
|
+
target.classList.remove(
|
|
66
|
+
"ring-2",
|
|
67
|
+
"ring-primary/60",
|
|
68
|
+
"ring-offset-1",
|
|
69
|
+
"ring-offset-background",
|
|
70
|
+
"rounded-sm",
|
|
71
|
+
"transition-shadow",
|
|
72
|
+
);
|
|
73
|
+
// Clear the expand signal AFTER the highlight fades so the
|
|
74
|
+
// user sees the tree in its final (expanded) state, not
|
|
75
|
+
// suddenly collapsing the moment we release control.
|
|
76
|
+
setExpandToPath(null);
|
|
77
|
+
}, highlightMs ?? HIGHLIGHT_DURATION_MS);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const escape = (value: string): string => {
|
|
81
|
+
if (typeof CSS !== "undefined" && typeof CSS.escape === "function") {
|
|
82
|
+
return CSS.escape(value);
|
|
83
|
+
}
|
|
84
|
+
return value.replace(/(["'\\[\](){}])/g, "\\$1");
|
|
85
|
+
};
|
|
86
|
+
const selector = `[data-anatomy-path="${escape(segment.path)}"]`;
|
|
87
|
+
|
|
88
|
+
const tryFindTarget = (attemptsLeft: number): void => {
|
|
89
|
+
const container = containerRef.current;
|
|
90
|
+
if (container !== null) {
|
|
91
|
+
const target = container.querySelector<HTMLElement>(selector);
|
|
92
|
+
if (target !== null) {
|
|
93
|
+
applyHighlight(target);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (attemptsLeft > 0) {
|
|
98
|
+
window.requestAnimationFrame(() => tryFindTarget(attemptsLeft - 1));
|
|
99
|
+
} else {
|
|
100
|
+
// Path not in the DOM after a generous wait — reset state
|
|
101
|
+
// and bail. This is the right behavior for malformed paths
|
|
102
|
+
// (e.g. when the format adapter returns a path that does
|
|
103
|
+
// not exist in the current request body).
|
|
104
|
+
setExpandToPath(null);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// Wait one frame for the tab-switch / expandToPath state
|
|
109
|
+
// update to commit before we start probing the DOM.
|
|
110
|
+
window.requestAnimationFrame(() => tryFindTarget(MAX_HIGHLIGHT_ATTEMPTS));
|
|
111
|
+
},
|
|
112
|
+
[containerRef, ensureTabActive, highlightMs, setExpandToPath],
|
|
113
|
+
);
|
|
114
|
+
}
|
|
@@ -4,8 +4,10 @@ import ReactMarkdown from "react-markdown";
|
|
|
4
4
|
import type { ResponseContentBlockType } from "../../../../proxy/schemas";
|
|
5
5
|
import { Badge } from "../../../ui/badge";
|
|
6
6
|
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../../../ui/collapsible";
|
|
7
|
-
import { JsonViewer
|
|
7
|
+
import { JsonViewer } from "../../../ui/json-viewer";
|
|
8
|
+
import { safeJsonValue } from "../../../ui/json-viewer-bulk";
|
|
8
9
|
import { ScrollArea } from "../../../ui/scroll-area";
|
|
10
|
+
import { extractThinkingFromContent } from "./thinkingExtract";
|
|
9
11
|
|
|
10
12
|
function assertNever(_value: never): JSX.Element {
|
|
11
13
|
return <></>;
|
|
@@ -37,28 +39,6 @@ function SystemReminderBlock({ text }: { text: string }): JSX.Element {
|
|
|
37
39
|
);
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
// Regex to extract content wrapped in <think>...</thinking> tags (MiniMax format: <think>...</think>)
|
|
41
|
-
const THINKING_TAG_REGEX = /<think>([\s\S]*?)<\/think>/gi;
|
|
42
|
-
const THINKING_TAG_REGEX_SINGLE = /<think>([\s\S]*?)<\/think>/i;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Extract thinking content wrapped in <think> tags (thought balloon emoji) from text.
|
|
46
|
-
* Returns { thinking, remainingText } where thinking is the extracted content
|
|
47
|
-
* and remainingText is the original text with thinking tags removed.
|
|
48
|
-
*/
|
|
49
|
-
export function extractThinkingFromContent(text: string): {
|
|
50
|
-
thinking: string | null;
|
|
51
|
-
remainingText: string;
|
|
52
|
-
} {
|
|
53
|
-
const match = THINKING_TAG_REGEX_SINGLE.exec(text);
|
|
54
|
-
if (!match || match[1] === undefined) {
|
|
55
|
-
return { thinking: null, remainingText: text };
|
|
56
|
-
}
|
|
57
|
-
const thinking = match[1].trim();
|
|
58
|
-
const remainingText = text.replace(THINKING_TAG_REGEX, "").trim();
|
|
59
|
-
return { thinking, remainingText };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
42
|
export const TextBlock = memo(function TextBlock({ text }: { text: string }): JSX.Element {
|
|
63
43
|
if (text.includes("<system-reminder>")) {
|
|
64
44
|
return <SystemReminderBlock text={text} />;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Regex to extract content wrapped in <think>...</thinking> tags (MiniMax format: <think>...</think>)
|
|
2
|
+
const THINKING_TAG_REGEX = /<think>([\s\S]*?)<\/think>/gi;
|
|
3
|
+
const THINKING_TAG_REGEX_SINGLE = /<think>([\s\S]*?)<\/think>/i;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Extract thinking content wrapped in <think> tags (thought balloon emoji) from text.
|
|
7
|
+
* Returns { thinking, remainingText } where thinking is the extracted content
|
|
8
|
+
* and remainingText is the original text with thinking tags removed.
|
|
9
|
+
*/
|
|
10
|
+
export function extractThinkingFromContent(text: string): {
|
|
11
|
+
thinking: string | null;
|
|
12
|
+
remainingText: string;
|
|
13
|
+
} {
|
|
14
|
+
const match = THINKING_TAG_REGEX_SINGLE.exec(text);
|
|
15
|
+
if (!match || match[1] === undefined) {
|
|
16
|
+
return { thinking: null, remainingText: text };
|
|
17
|
+
}
|
|
18
|
+
const thinking = match[1].trim();
|
|
19
|
+
const remainingText = text.replace(THINKING_TAG_REGEX, "").trim();
|
|
20
|
+
return { thinking, remainingText };
|
|
21
|
+
}
|
|
@@ -4,15 +4,17 @@ import ReactMarkdown from "react-markdown";
|
|
|
4
4
|
import type { OpenAIResponse, OpenAIToolCall } from "../../../../proxy/schemas";
|
|
5
5
|
import { formatTokens } from "../../../../lib/utils";
|
|
6
6
|
import { Badge } from "../../../ui/badge";
|
|
7
|
-
import { JsonViewer
|
|
7
|
+
import { JsonViewer } from "../../../ui/json-viewer";
|
|
8
|
+
import { safeJsonValue } from "../../../ui/json-viewer-bulk";
|
|
8
9
|
import { ScrollArea } from "../../../ui/scroll-area";
|
|
9
10
|
import { Separator } from "../../../ui/separator";
|
|
10
11
|
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../../../ui/collapsible";
|
|
11
12
|
import { ChevronDown, ChevronRight } from "lucide-react";
|
|
12
|
-
import {
|
|
13
|
+
import { ThinkingBlock } from "../anthropic/ContentBlocks";
|
|
14
|
+
import { extractThinkingFromContent } from "../anthropic/thinkingExtract";
|
|
13
15
|
|
|
14
16
|
// Re-export for use in other components
|
|
15
|
-
export { extractThinkingFromContent } from "../anthropic/
|
|
17
|
+
export { extractThinkingFromContent } from "../anthropic/thinkingExtract";
|
|
16
18
|
|
|
17
19
|
/** Best-effort JSON parse of an OpenAI `function.arguments` string. Returns
|
|
18
20
|
* the parsed object, or `null` on parse failure so the renderer can fall
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { lazy } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Lazy-loaded heavy leaf components. Each is split into its own chunk by Vite
|
|
5
|
+
* (named after the source file) so the main bundle stays small. Imports use
|
|
6
|
+
* the `.then(m => ({ default: m.X }))` adapter so the original modules can
|
|
7
|
+
* keep their named exports without needing a default export.
|
|
8
|
+
*/
|
|
9
|
+
export const LazyCompareDrawer = lazy(() =>
|
|
10
|
+
import("./CompareDrawer").then((m) => ({ default: m.CompareDrawer })),
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
export const LazyReplayDialog = lazy(() =>
|
|
14
|
+
import("./ReplayDialog").then((m) => ({ default: m.ReplayDialog })),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
export const LazyRequestAnatomy = lazy(() =>
|
|
18
|
+
import("./anatomy/RequestAnatomy").then((m) => ({ default: m.RequestAnatomy })),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
export const LazyResponseView = lazy(() =>
|
|
22
|
+
import("./ResponseView").then((m) => ({ default: m.ResponseView })),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
export const LazyStreamingChunkSequence = lazy(() =>
|
|
26
|
+
import("./StreamingChunkSequence").then((m) => ({
|
|
27
|
+
default: m.StreamingChunkSequence,
|
|
28
|
+
})),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
export const LazyJsonViewer = lazy(() =>
|
|
32
|
+
import("../ui/json-viewer").then((m) => ({ default: m.JsonViewer })),
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
export const LazyJsonViewerFromString = lazy(() =>
|
|
36
|
+
import("../ui/json-viewer").then((m) => ({ default: m.JsonViewerFromString })),
|
|
37
|
+
);
|