tuxevil-rotator 4.2.2 → 4.4.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/CHANGELOG.md +48 -1
- package/README.md +11 -4
- package/package.json +2 -1
- package/plugins/opencode-discovery/capabilities.d.mts +7 -0
- package/plugins/opencode-discovery/capabilities.mjs +54 -0
- package/plugins/opencode-discovery/index.d.mts +2 -1
- package/plugins/opencode-discovery/index.mjs +6 -1
- package/plugins/opencode-discovery/package.json +3 -1
- package/src/access-evidence.ts +153 -0
- package/src/access-probe.ts +85 -0
- package/src/admin-auth.ts +4 -1
- package/src/audio-transcription.ts +32 -6
- package/src/auto-access-shadow.ts +11 -0
- package/src/auto-model-access.ts +185 -0
- package/src/auto-output-health.ts +12 -0
- package/src/auto-routing-evaluation.ts +80 -0
- package/src/auto-routing-quality.ts +308 -0
- package/src/auto-routing.ts +403 -26
- package/src/cli.ts +3 -3
- package/src/compat/model-specs.ts +4 -0
- package/src/compat.ts +253 -37
- package/src/compression/index.ts +6 -6
- package/src/compression/lite.ts +3 -9
- package/src/compression/rtk/index.ts +0 -3
- package/src/config-defaults.ts +7 -0
- package/src/dashboard-app.ts +1 -0
- package/src/dashboard.ts +5 -0
- package/src/db-store.ts +10 -0
- package/src/index.ts +2 -0
- package/src/install-agent.ts +211 -129
- package/src/install-opencode.ts +80 -18
- package/src/model-access-api.ts +74 -0
- package/src/model-access.ts +56 -0
- package/src/ollama-output-gate.ts +18 -0
- package/src/onboarding.ts +1 -1
- package/src/provider-catalog-evidence.ts +26 -0
- package/src/providers/google-antigravity/catalog.ts +14 -6
- package/src/providers/google-antigravity/dynamic-catalog.ts +87 -3
- package/src/providers/google-antigravity/forward.ts +4 -2
- package/src/providers/google-antigravity/quota.ts +7 -0
- package/src/providers/ollama/context-window.ts +13 -0
- package/src/providers/ollama/forward.ts +10 -0
- package/src/providers/ollama/quota.ts +4 -0
- package/src/providers/ollama/translators.ts +19 -2
- package/src/providers/openai-codex/catalog.ts +157 -18
- package/src/providers/openai-codex/compat.ts +5 -1
- package/src/providers/openai-codex/credentials.ts +22 -6
- package/src/providers/openai-codex/index.ts +9 -1
- package/src/providers/openai-codex/quota.ts +19 -6
- package/src/providers/opencode-zen/forward.ts +1 -1
- package/src/providers/opencode-zen/quota.ts +2 -0
- package/src/proxy.ts +113 -5
- package/src/quota-observation.ts +26 -0
- package/src/quota-pools.ts +322 -0
- package/src/rotator.ts +167 -40
- package/src/settings-repository.ts +1 -0
- package/src/types.ts +106 -29
- package/src/usage-limits-auth.ts +24 -0
- package/src/usage-limits.ts +148 -0
- package/src/validators.ts +26 -0
- package/src/web/app.tsx +4 -0
- package/src/web/components/icons.tsx +15 -0
- package/src/web/components/overlays.tsx +4 -0
- package/src/web/components/ui.tsx +9 -9
- package/src/web/lib/audio-recorder.ts +159 -0
- package/src/web/lib/colors.ts +4 -0
- package/src/web/lib/playground-storage.ts +261 -0
- package/src/web/lib/playground.ts +483 -0
- package/src/web/lib/pool-navigation.ts +4 -0
- package/src/web/lib/pools.ts +2 -243
- package/src/web/nav.ts +1 -0
- package/src/web/pages/AccountDetail.tsx +2 -1
- package/src/web/pages/ModelAccess.tsx +88 -0
- package/src/web/pages/Models.tsx +2 -0
- package/src/web/pages/Overview.tsx +26 -9
- package/src/web/pages/Playground.tsx +1692 -0
- package/src/web/pages/Transcription.tsx +562 -0
- package/src/web/state/api.ts +6 -0
- package/src/web/state/playground-client.ts +136 -0
- package/src/web/state/playground.ts +69 -0
- package/src/web/state/router.ts +2 -2
- package/src/web/state/transcription-client.ts +93 -0
- package/src/web/styles/app.css +501 -0
- package/tools/telemetry-receiver/receiver.js +59 -46
|
@@ -0,0 +1,1692 @@
|
|
|
1
|
+
import { Fragment, type JSX } from "preact";
|
|
2
|
+
import { useEffect, useId, useRef, useState } from "preact/hooks";
|
|
3
|
+
import {
|
|
4
|
+
Button,
|
|
5
|
+
EmptyState,
|
|
6
|
+
KeyValue,
|
|
7
|
+
PageHeader,
|
|
8
|
+
Panel,
|
|
9
|
+
Pill,
|
|
10
|
+
Segmented,
|
|
11
|
+
Spinner,
|
|
12
|
+
Switch,
|
|
13
|
+
Tag,
|
|
14
|
+
} from "../components/ui.js";
|
|
15
|
+
import { Icon } from "../components/icons.js";
|
|
16
|
+
import { Menu, type MenuItem } from "../components/overlays.js";
|
|
17
|
+
import { confirmAction, toast } from "../state/actions.js";
|
|
18
|
+
import { api } from "../state/api.js";
|
|
19
|
+
import { query, setQuery } from "../state/router.js";
|
|
20
|
+
import { masked, masker, overview } from "../state/store.js";
|
|
21
|
+
import {
|
|
22
|
+
currentDocument,
|
|
23
|
+
initializePlayground,
|
|
24
|
+
saveWorkspace,
|
|
25
|
+
storageError,
|
|
26
|
+
updateDocument,
|
|
27
|
+
workspace,
|
|
28
|
+
} from "../state/playground.js";
|
|
29
|
+
import { PlaygroundError, runChat } from "../state/playground-client.js";
|
|
30
|
+
import {
|
|
31
|
+
buildChatRequest,
|
|
32
|
+
newTranscription,
|
|
33
|
+
CASES,
|
|
34
|
+
complianceJobs,
|
|
35
|
+
complianceRequest,
|
|
36
|
+
DEFAULT_PARAMETERS,
|
|
37
|
+
emptyResult,
|
|
38
|
+
errorVerdict,
|
|
39
|
+
evaluateCompliance,
|
|
40
|
+
isAudioOnlyModel,
|
|
41
|
+
newDocument,
|
|
42
|
+
newId,
|
|
43
|
+
newPanel,
|
|
44
|
+
runnableComplianceModels,
|
|
45
|
+
userMessage,
|
|
46
|
+
VERDICT_LABELS,
|
|
47
|
+
type CaseId,
|
|
48
|
+
type ChatRequest,
|
|
49
|
+
type ChatResult,
|
|
50
|
+
type ComplianceResult,
|
|
51
|
+
type ImageAttachment,
|
|
52
|
+
type Parameters,
|
|
53
|
+
type PlaygroundDocument,
|
|
54
|
+
type PlaygroundPanel,
|
|
55
|
+
type PlaygroundTab,
|
|
56
|
+
type Turn,
|
|
57
|
+
type Verdict,
|
|
58
|
+
} from "../lib/playground.js";
|
|
59
|
+
import { parsePlaygroundDocument } from "../lib/playground-storage.js";
|
|
60
|
+
import type { Tone } from "../lib/status.js";
|
|
61
|
+
import { TranscriptionPage } from "./Transcription.js";
|
|
62
|
+
import { copyText } from "./shared.js";
|
|
63
|
+
import { formatMs, formatUsd } from "../lib/format.js";
|
|
64
|
+
|
|
65
|
+
const TABS: Array<{ value: PlaygroundTab; label: string }> = [
|
|
66
|
+
{ value: "chat", label: "Chat" },
|
|
67
|
+
{ value: "compare", label: "Compare" },
|
|
68
|
+
{ value: "compliance", label: "Compliance" },
|
|
69
|
+
{ value: "transcription", label: "Transcription" },
|
|
70
|
+
];
|
|
71
|
+
const DESCRIPTION =
|
|
72
|
+
"Chat with, compare and test models through the rotator. Requests use real quota.";
|
|
73
|
+
const MAX_COMPLIANCE_MODELS = 3;
|
|
74
|
+
const VERDICT_TONES: Record<Verdict, Tone> = {
|
|
75
|
+
passed: "ok",
|
|
76
|
+
failed: "bad",
|
|
77
|
+
error: "bad",
|
|
78
|
+
unsupported: "warn",
|
|
79
|
+
cancelled: "muted",
|
|
80
|
+
};
|
|
81
|
+
const CANCELLED_MESSAGE = "Request cancelled.";
|
|
82
|
+
const errorText = (err: unknown): string =>
|
|
83
|
+
err instanceof Error ? err.message : String(err);
|
|
84
|
+
/** Seconds keep playgrounds saved in the same minute apart; under the privacy
|
|
85
|
+
* mask every title reads "Saved playground". */
|
|
86
|
+
const savedAt = (ms: number): string =>
|
|
87
|
+
new Date(ms).toLocaleString([], {
|
|
88
|
+
month: "short",
|
|
89
|
+
day: "numeric",
|
|
90
|
+
hour: "2-digit",
|
|
91
|
+
minute: "2-digit",
|
|
92
|
+
second: "2-digit",
|
|
93
|
+
});
|
|
94
|
+
const historyLabel = (d: PlaygroundDocument): string =>
|
|
95
|
+
`${masked.value ? "Saved playground" : d.title} · ${savedAt(d.updatedAt)}`;
|
|
96
|
+
const copy = async (text: string): Promise<void> =>
|
|
97
|
+
toast((await copyText(text)) ? "Copied to the clipboard" : "Copy failed", "info");
|
|
98
|
+
/** Times that were never measured (null, or 0 before a response) read "—";
|
|
99
|
+
* formatMs alone turns null into "0ms". */
|
|
100
|
+
const duration = (ms: number | null | undefined): string =>
|
|
101
|
+
ms ? formatMs(ms) : "—";
|
|
102
|
+
|
|
103
|
+
interface ComplianceProgress {
|
|
104
|
+
done: number;
|
|
105
|
+
total: number;
|
|
106
|
+
retry: boolean;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function PlaygroundPage(): JSX.Element {
|
|
110
|
+
const doc = currentDocument();
|
|
111
|
+
const requestedTab = query.value.get("tab");
|
|
112
|
+
const tab: PlaygroundTab =
|
|
113
|
+
requestedTab === "compare" ||
|
|
114
|
+
requestedTab === "compliance" ||
|
|
115
|
+
requestedTab === "transcription"
|
|
116
|
+
? requestedTab
|
|
117
|
+
: "chat";
|
|
118
|
+
// null until the first load settles, so "No enabled models" never flashes.
|
|
119
|
+
const [models, setModels] = useState<string[] | null>(null);
|
|
120
|
+
const [modelError, setModelError] = useState("");
|
|
121
|
+
const [busy, setBusy] = useState<string[]>([]);
|
|
122
|
+
const [checking, setChecking] = useState(false);
|
|
123
|
+
const [progress, setProgress] = useState<ComplianceProgress | null>(null);
|
|
124
|
+
const [finished, setFinished] = useState("");
|
|
125
|
+
const [audioBusy, setAudioBusy] = useState(false);
|
|
126
|
+
const [maxBodyBytes, setMaxBodyBytes] = useState(25 * 1024 * 1024);
|
|
127
|
+
const controllers = useRef(new Map<string, AbortController>());
|
|
128
|
+
const importRef = useRef<HTMLInputElement>(null);
|
|
129
|
+
const imageRef = useRef<HTMLInputElement>(null);
|
|
130
|
+
const routing = JSON.stringify([
|
|
131
|
+
overview.value?.modelCatalogRevision,
|
|
132
|
+
overview.value?.accounts.map((a) => [
|
|
133
|
+
a.email,
|
|
134
|
+
a.status,
|
|
135
|
+
a.disabledProviders,
|
|
136
|
+
a.invalidProviders,
|
|
137
|
+
]),
|
|
138
|
+
]);
|
|
139
|
+
const running = checking || busy.length > 0 || audioBusy;
|
|
140
|
+
const available = models ?? [];
|
|
141
|
+
|
|
142
|
+
const loadModels = async () => {
|
|
143
|
+
try {
|
|
144
|
+
const response = await api.models();
|
|
145
|
+
setModels([...new Set(response.data.map((m) => m.id))].sort());
|
|
146
|
+
setModelError("");
|
|
147
|
+
} catch (err) {
|
|
148
|
+
setModelError(errorText(err));
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
void initializePlayground();
|
|
153
|
+
}, []);
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
void loadModels();
|
|
156
|
+
}, [routing]);
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
void api
|
|
159
|
+
.playgroundConfig()
|
|
160
|
+
.then((config) => setMaxBodyBytes(config.maxBodyBytes))
|
|
161
|
+
.catch(() => {});
|
|
162
|
+
const active = controllers.current;
|
|
163
|
+
return () => {
|
|
164
|
+
for (const controller of active.values()) controller.abort();
|
|
165
|
+
};
|
|
166
|
+
}, []);
|
|
167
|
+
|
|
168
|
+
function patchPanel(
|
|
169
|
+
id: string,
|
|
170
|
+
update: (panel: PlaygroundPanel) => PlaygroundPanel,
|
|
171
|
+
immediate = false,
|
|
172
|
+
): void {
|
|
173
|
+
updateDocument(
|
|
174
|
+
(d) => ({
|
|
175
|
+
...d,
|
|
176
|
+
chat: d.chat.id === id ? update(d.chat) : d.chat,
|
|
177
|
+
compare: d.compare.map((p) => (p.id === id ? update(p) : p)),
|
|
178
|
+
}),
|
|
179
|
+
immediate,
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
function changeParams(id: string, patch: Partial<Parameters>): void {
|
|
183
|
+
updateDocument((d) => ({
|
|
184
|
+
...d,
|
|
185
|
+
chat:
|
|
186
|
+
d.chat.id === id
|
|
187
|
+
? { ...d.chat, params: { ...d.chat.params, ...patch } }
|
|
188
|
+
: d.chat,
|
|
189
|
+
compare: d.compare.map((p) =>
|
|
190
|
+
p.id === id || (d.sync && d.compare.some((p) => p.id === id))
|
|
191
|
+
? { ...p, params: { ...p.params, ...patch } }
|
|
192
|
+
: p,
|
|
193
|
+
),
|
|
194
|
+
}));
|
|
195
|
+
}
|
|
196
|
+
function ensureSize(request: ChatRequest): void {
|
|
197
|
+
if (new Blob([JSON.stringify(request)]).size > maxBodyBytes)
|
|
198
|
+
throw new Error(
|
|
199
|
+
`Request exceeds the server's ${(maxBodyBytes / 1024 / 1024).toFixed(1)} MiB limit. Remove images or start a new conversation.`,
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
async function executePanel(
|
|
203
|
+
panel: PlaygroundPanel,
|
|
204
|
+
request: ChatRequest,
|
|
205
|
+
retry: boolean,
|
|
206
|
+
): Promise<void> {
|
|
207
|
+
const documentId = doc!.id;
|
|
208
|
+
const controller = new AbortController();
|
|
209
|
+
controllers.current.set(panel.id, controller);
|
|
210
|
+
setBusy((ids) => [...ids, panel.id]);
|
|
211
|
+
const turns = retry ? panel.turns.slice(0, -1) : panel.turns;
|
|
212
|
+
const index = turns.length;
|
|
213
|
+
patchPanel(panel.id, (p) => ({
|
|
214
|
+
...p,
|
|
215
|
+
turns: [...turns, { request, result: null }],
|
|
216
|
+
}));
|
|
217
|
+
const setTurn = (result: ChatResult, error?: string) => {
|
|
218
|
+
if (currentDocument()?.id !== documentId) return;
|
|
219
|
+
patchPanel(
|
|
220
|
+
panel.id,
|
|
221
|
+
(p) => ({
|
|
222
|
+
...p,
|
|
223
|
+
turns: p.turns.map((turn, i) =>
|
|
224
|
+
i === index
|
|
225
|
+
? { request, result, ...(error ? { error } : {}) }
|
|
226
|
+
: turn,
|
|
227
|
+
),
|
|
228
|
+
}),
|
|
229
|
+
result.done || !!error,
|
|
230
|
+
);
|
|
231
|
+
};
|
|
232
|
+
try {
|
|
233
|
+
setTurn(
|
|
234
|
+
await runChat(request, controller.signal, (result) => setTurn(result)),
|
|
235
|
+
);
|
|
236
|
+
} catch (err) {
|
|
237
|
+
if (err instanceof PlaygroundError) setTurn(err.result, err.message);
|
|
238
|
+
else toast(errorText(err), "error");
|
|
239
|
+
} finally {
|
|
240
|
+
controllers.current.delete(panel.id);
|
|
241
|
+
setBusy((ids) => ids.filter((id) => id !== panel.id));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
async function send(retryPanel?: PlaygroundPanel): Promise<void> {
|
|
245
|
+
if (!doc) return;
|
|
246
|
+
try {
|
|
247
|
+
const panels = retryPanel
|
|
248
|
+
? [retryPanel]
|
|
249
|
+
: tab === "compare"
|
|
250
|
+
? doc.compare
|
|
251
|
+
: [doc.chat];
|
|
252
|
+
const next = userMessage(doc.draft, doc.images);
|
|
253
|
+
if (!retryPanel && !doc.draft.trim() && !doc.images.length) return;
|
|
254
|
+
const requests = panels.map((panel) => {
|
|
255
|
+
if (!available.includes(panel.model))
|
|
256
|
+
throw new Error("Choose an enabled model in every panel.");
|
|
257
|
+
const lastRequest = panel.turns.at(-1)?.request;
|
|
258
|
+
const message =
|
|
259
|
+
retryPanel && lastRequest
|
|
260
|
+
? lastRequest.messages[lastRequest.messages.length - 1]
|
|
261
|
+
: next;
|
|
262
|
+
const request = buildChatRequest(panel, message, !!retryPanel);
|
|
263
|
+
ensureSize(request);
|
|
264
|
+
return request;
|
|
265
|
+
});
|
|
266
|
+
if (!retryPanel)
|
|
267
|
+
updateDocument((d) => ({
|
|
268
|
+
...d,
|
|
269
|
+
draft: "",
|
|
270
|
+
images: [],
|
|
271
|
+
title:
|
|
272
|
+
d.title === "Untitled playground"
|
|
273
|
+
? doc.draft.trim().slice(0, 60) || "Image conversation"
|
|
274
|
+
: d.title,
|
|
275
|
+
}));
|
|
276
|
+
await Promise.all(
|
|
277
|
+
panels.map((panel, i) =>
|
|
278
|
+
executePanel(panel, requests[i], !!retryPanel),
|
|
279
|
+
),
|
|
280
|
+
);
|
|
281
|
+
} catch (err) {
|
|
282
|
+
toast(errorText(err), "error");
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
async function attach(files: File[]): Promise<void> {
|
|
286
|
+
const documentId = doc?.id;
|
|
287
|
+
try {
|
|
288
|
+
const attachments: ImageAttachment[] = [];
|
|
289
|
+
if (
|
|
290
|
+
files.some(
|
|
291
|
+
(file) =>
|
|
292
|
+
!["image/png", "image/jpeg", "image/webp"].includes(file.type),
|
|
293
|
+
)
|
|
294
|
+
)
|
|
295
|
+
throw new Error("Choose PNG, JPEG or WebP images.");
|
|
296
|
+
if (
|
|
297
|
+
(files.reduce((sum, file) => sum + file.size, 0) * 4) / 3 >
|
|
298
|
+
maxBodyBytes
|
|
299
|
+
)
|
|
300
|
+
throw new Error("Images exceed the request size limit.");
|
|
301
|
+
for (const file of files) {
|
|
302
|
+
const url = await new Promise<string>((resolve, reject) => {
|
|
303
|
+
const reader = new FileReader();
|
|
304
|
+
reader.onload = () => resolve(String(reader.result));
|
|
305
|
+
reader.onerror = () => reject(reader.error);
|
|
306
|
+
reader.readAsDataURL(file);
|
|
307
|
+
});
|
|
308
|
+
attachments.push({ name: file.name, url });
|
|
309
|
+
}
|
|
310
|
+
if (currentDocument()?.id === documentId)
|
|
311
|
+
updateDocument((d) => ({
|
|
312
|
+
...d,
|
|
313
|
+
images: [...d.images, ...attachments],
|
|
314
|
+
}));
|
|
315
|
+
} catch (err) {
|
|
316
|
+
toast(errorText(err), "error");
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function newPlayground(): void {
|
|
320
|
+
const fresh = newDocument();
|
|
321
|
+
saveWorkspace(
|
|
322
|
+
{
|
|
323
|
+
...workspace.value!,
|
|
324
|
+
currentId: fresh.id,
|
|
325
|
+
documents: [...workspace.value!.documents, fresh],
|
|
326
|
+
},
|
|
327
|
+
true,
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
function exportDoc(): void {
|
|
331
|
+
if (!doc) return;
|
|
332
|
+
const url = URL.createObjectURL(
|
|
333
|
+
new Blob([JSON.stringify(doc, null, 2)], { type: "application/json" }),
|
|
334
|
+
);
|
|
335
|
+
const a = document.createElement("a");
|
|
336
|
+
a.href = url;
|
|
337
|
+
a.download = `playground-${doc.id}.json`;
|
|
338
|
+
a.click();
|
|
339
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
340
|
+
}
|
|
341
|
+
async function importDoc(file: File): Promise<void> {
|
|
342
|
+
try {
|
|
343
|
+
if (file.size > 100 * 1024 * 1024)
|
|
344
|
+
throw new Error("Playground imports must be smaller than 100 MiB.");
|
|
345
|
+
const imported = parsePlaygroundDocument(JSON.parse(await file.text()));
|
|
346
|
+
const state = workspace.value!;
|
|
347
|
+
const restored = { ...imported, id: newId(), updatedAt: Date.now() };
|
|
348
|
+
saveWorkspace(
|
|
349
|
+
{
|
|
350
|
+
...state,
|
|
351
|
+
currentId: restored.id,
|
|
352
|
+
documents: [...state.documents, restored],
|
|
353
|
+
},
|
|
354
|
+
true,
|
|
355
|
+
);
|
|
356
|
+
toast("Imported playground. No requests were sent.", "success");
|
|
357
|
+
} catch (err) {
|
|
358
|
+
toast(errorText(err), "error");
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
async function deleteHistory(all: boolean): Promise<void> {
|
|
362
|
+
if (!doc || !workspace.value) return;
|
|
363
|
+
if (
|
|
364
|
+
!(await confirmAction({
|
|
365
|
+
title: all
|
|
366
|
+
? "Delete all playground history?"
|
|
367
|
+
: "Delete this playground?",
|
|
368
|
+
body: "This removes the local copy from this browser. Export anything you want to keep first.",
|
|
369
|
+
confirmLabel: "Delete",
|
|
370
|
+
danger: true,
|
|
371
|
+
}))
|
|
372
|
+
)
|
|
373
|
+
return;
|
|
374
|
+
const remaining = all
|
|
375
|
+
? []
|
|
376
|
+
: workspace.value.documents.filter((d) => d.id !== doc.id);
|
|
377
|
+
if (!remaining.length) remaining.push(newDocument());
|
|
378
|
+
saveWorkspace(
|
|
379
|
+
{ version: 1, currentId: remaining[0].id, documents: remaining },
|
|
380
|
+
true,
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
async function runCompliance(retry = false): Promise<void> {
|
|
384
|
+
if (!doc) return;
|
|
385
|
+
const jobs = complianceJobs(doc, retry);
|
|
386
|
+
if (!jobs.length) return;
|
|
387
|
+
const documentId = doc.id;
|
|
388
|
+
if (jobs.some((job) => !available.includes(job.model))) {
|
|
389
|
+
toast("Select enabled models before running compliance.", "error");
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
const jobKey = (model: string, caseId: CaseId) =>
|
|
393
|
+
JSON.stringify([model, caseId]);
|
|
394
|
+
const selected = new Set(
|
|
395
|
+
jobs.flatMap((job) =>
|
|
396
|
+
job.cases.map((caseId) => jobKey(job.model, caseId)),
|
|
397
|
+
),
|
|
398
|
+
);
|
|
399
|
+
updateDocument((d) => ({
|
|
400
|
+
...d,
|
|
401
|
+
results: retry
|
|
402
|
+
? d.results.filter((r) => !selected.has(jobKey(r.model, r.caseId)))
|
|
403
|
+
: [],
|
|
404
|
+
}));
|
|
405
|
+
let done = 0;
|
|
406
|
+
setFinished("");
|
|
407
|
+
setProgress({ done, total: selected.size, retry });
|
|
408
|
+
setChecking(true);
|
|
409
|
+
try {
|
|
410
|
+
await Promise.all(
|
|
411
|
+
jobs.map(async ({ model, cases }) => {
|
|
412
|
+
const controller = new AbortController();
|
|
413
|
+
controllers.current.set(`compliance:${model}`, controller);
|
|
414
|
+
try {
|
|
415
|
+
for (const caseId of cases) {
|
|
416
|
+
const request = complianceRequest(model, caseId);
|
|
417
|
+
let row: ComplianceResult;
|
|
418
|
+
try {
|
|
419
|
+
if (controller.signal.aborted)
|
|
420
|
+
throw new PlaygroundError(
|
|
421
|
+
CANCELLED_MESSAGE,
|
|
422
|
+
emptyResult(),
|
|
423
|
+
true,
|
|
424
|
+
);
|
|
425
|
+
const result = await runChat(request, controller.signal);
|
|
426
|
+
row = {
|
|
427
|
+
model,
|
|
428
|
+
caseId,
|
|
429
|
+
...evaluateCompliance(caseId, result),
|
|
430
|
+
turn: { request, result },
|
|
431
|
+
};
|
|
432
|
+
} catch (err) {
|
|
433
|
+
row = {
|
|
434
|
+
model,
|
|
435
|
+
caseId,
|
|
436
|
+
verdict:
|
|
437
|
+
err instanceof PlaygroundError && err.cancelled
|
|
438
|
+
? "cancelled"
|
|
439
|
+
: errorVerdict(
|
|
440
|
+
err instanceof PlaygroundError
|
|
441
|
+
? err.result.status
|
|
442
|
+
: 0,
|
|
443
|
+
errorText(err),
|
|
444
|
+
),
|
|
445
|
+
explanation: errorText(err),
|
|
446
|
+
turn: {
|
|
447
|
+
request,
|
|
448
|
+
result: err instanceof PlaygroundError ? err.result : null,
|
|
449
|
+
error: errorText(err),
|
|
450
|
+
},
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
if (currentDocument()?.id === documentId)
|
|
454
|
+
updateDocument((d) => ({ ...d, results: [...d.results, row] }));
|
|
455
|
+
done += 1;
|
|
456
|
+
setProgress({ done, total: selected.size, retry });
|
|
457
|
+
}
|
|
458
|
+
} finally {
|
|
459
|
+
controllers.current.delete(`compliance:${model}`);
|
|
460
|
+
}
|
|
461
|
+
}),
|
|
462
|
+
);
|
|
463
|
+
} finally {
|
|
464
|
+
setChecking(false);
|
|
465
|
+
setProgress(null);
|
|
466
|
+
const latest = currentDocument();
|
|
467
|
+
if (latest?.id === documentId) {
|
|
468
|
+
const passed = latest.results.filter(
|
|
469
|
+
(r) => r.verdict === "passed",
|
|
470
|
+
).length;
|
|
471
|
+
setFinished(
|
|
472
|
+
`Compliance run finished: ${passed} of ${latest.results.length} tests passed.`,
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (!doc)
|
|
479
|
+
return (
|
|
480
|
+
<>
|
|
481
|
+
<PageHeader title="Playground" description={DESCRIPTION} />
|
|
482
|
+
<div class="panel">
|
|
483
|
+
<div class="panel-loading">
|
|
484
|
+
<Spinner />
|
|
485
|
+
</div>
|
|
486
|
+
</div>
|
|
487
|
+
</>
|
|
488
|
+
);
|
|
489
|
+
|
|
490
|
+
const compare = tab === "compare";
|
|
491
|
+
const stopAll = running && (
|
|
492
|
+
<Button
|
|
493
|
+
size="sm"
|
|
494
|
+
icon="stop"
|
|
495
|
+
onClick={() => {
|
|
496
|
+
for (const c of controllers.current.values()) c.abort();
|
|
497
|
+
}}
|
|
498
|
+
>
|
|
499
|
+
Stop all
|
|
500
|
+
</Button>
|
|
501
|
+
);
|
|
502
|
+
const selectedCount = runnableComplianceModels(doc).length;
|
|
503
|
+
const testCount = complianceJobs(doc).reduce(
|
|
504
|
+
(sum, job) => sum + job.cases.length,
|
|
505
|
+
0,
|
|
506
|
+
);
|
|
507
|
+
const failedCount = complianceJobs(doc, true).reduce(
|
|
508
|
+
(sum, job) => sum + job.cases.length,
|
|
509
|
+
0,
|
|
510
|
+
);
|
|
511
|
+
const progressLabel = progress
|
|
512
|
+
? `Running ${progress.done}/${progress.total}`
|
|
513
|
+
: "";
|
|
514
|
+
|
|
515
|
+
return (
|
|
516
|
+
<>
|
|
517
|
+
<PageHeader
|
|
518
|
+
title="Playground"
|
|
519
|
+
description={DESCRIPTION}
|
|
520
|
+
actions={
|
|
521
|
+
<>
|
|
522
|
+
<label class="inline-select pg-history">
|
|
523
|
+
<span class="sr-only">Saved playground</span>
|
|
524
|
+
<select
|
|
525
|
+
class="select"
|
|
526
|
+
title={historyLabel(doc)}
|
|
527
|
+
disabled={running}
|
|
528
|
+
value={doc.id}
|
|
529
|
+
onChange={(e) =>
|
|
530
|
+
saveWorkspace(
|
|
531
|
+
{ ...workspace.value!, currentId: e.currentTarget.value },
|
|
532
|
+
true,
|
|
533
|
+
)
|
|
534
|
+
}
|
|
535
|
+
>
|
|
536
|
+
{workspace.value!.documents.map((d) => (
|
|
537
|
+
<option key={d.id} value={d.id}>
|
|
538
|
+
{historyLabel(d)}
|
|
539
|
+
</option>
|
|
540
|
+
))}
|
|
541
|
+
</select>
|
|
542
|
+
</label>
|
|
543
|
+
<Menu
|
|
544
|
+
label="More playground actions"
|
|
545
|
+
size="md"
|
|
546
|
+
triggerClass="btn btn-secondary btn-md btn-icon"
|
|
547
|
+
items={[
|
|
548
|
+
{ heading: "Saved in this browser" },
|
|
549
|
+
{ label: "Export", icon: "download", onSelect: exportDoc },
|
|
550
|
+
{
|
|
551
|
+
label: "Import…",
|
|
552
|
+
icon: "upload",
|
|
553
|
+
disabled: running,
|
|
554
|
+
onSelect: () => importRef.current?.click(),
|
|
555
|
+
},
|
|
556
|
+
"separator",
|
|
557
|
+
{
|
|
558
|
+
label: "Delete this playground…",
|
|
559
|
+
icon: "trash",
|
|
560
|
+
danger: true,
|
|
561
|
+
disabled: running,
|
|
562
|
+
onSelect: () => void deleteHistory(false),
|
|
563
|
+
},
|
|
564
|
+
{
|
|
565
|
+
label: "Delete all history…",
|
|
566
|
+
danger: true,
|
|
567
|
+
disabled: running,
|
|
568
|
+
onSelect: () => void deleteHistory(true),
|
|
569
|
+
},
|
|
570
|
+
]}
|
|
571
|
+
/>
|
|
572
|
+
<Button
|
|
573
|
+
variant="primary"
|
|
574
|
+
icon="plus"
|
|
575
|
+
class="pg-new"
|
|
576
|
+
disabled={running}
|
|
577
|
+
onClick={newPlayground}
|
|
578
|
+
>
|
|
579
|
+
New playground
|
|
580
|
+
</Button>
|
|
581
|
+
<input
|
|
582
|
+
ref={importRef}
|
|
583
|
+
type="file"
|
|
584
|
+
accept="application/json,.json"
|
|
585
|
+
class="sr-only"
|
|
586
|
+
aria-label="Import playground"
|
|
587
|
+
onChange={(e) => {
|
|
588
|
+
const file = e.currentTarget.files?.[0];
|
|
589
|
+
e.currentTarget.value = "";
|
|
590
|
+
if (file) void importDoc(file);
|
|
591
|
+
}}
|
|
592
|
+
/>
|
|
593
|
+
</>
|
|
594
|
+
}
|
|
595
|
+
/>
|
|
596
|
+
{storageError.value && (
|
|
597
|
+
<div class="callout tone-warn" role="alert">
|
|
598
|
+
<Icon name="warning" size={16} />
|
|
599
|
+
<div>
|
|
600
|
+
<strong>Local history could not be saved</strong>
|
|
601
|
+
<p>
|
|
602
|
+
Your work is still in memory; export it to keep it.{" "}
|
|
603
|
+
{masker.value.text(storageError.value)}
|
|
604
|
+
</p>
|
|
605
|
+
<Button size="sm" icon="download" onClick={exportDoc}>
|
|
606
|
+
Export
|
|
607
|
+
</Button>
|
|
608
|
+
</div>
|
|
609
|
+
</div>
|
|
610
|
+
)}
|
|
611
|
+
{modelError && (
|
|
612
|
+
<div class="callout tone-bad" role="alert">
|
|
613
|
+
<Icon name="critical" size={16} />
|
|
614
|
+
<div>
|
|
615
|
+
<strong>Could not load models</strong>
|
|
616
|
+
<p>{modelError}</p>
|
|
617
|
+
<Button size="sm" icon="refresh" onClick={() => void loadModels()}>
|
|
618
|
+
Reload models
|
|
619
|
+
</Button>
|
|
620
|
+
</div>
|
|
621
|
+
</div>
|
|
622
|
+
)}
|
|
623
|
+
<div class="toolbar">
|
|
624
|
+
<Segmented
|
|
625
|
+
class="pg-modes"
|
|
626
|
+
label="Playground mode"
|
|
627
|
+
value={tab}
|
|
628
|
+
options={TABS}
|
|
629
|
+
onChange={(value) => setQuery({ tab: value }, { replace: false })}
|
|
630
|
+
/>
|
|
631
|
+
{(running || compare) && (
|
|
632
|
+
<div class="toolbar-right">
|
|
633
|
+
{stopAll}
|
|
634
|
+
{compare && (
|
|
635
|
+
<>
|
|
636
|
+
<Switch
|
|
637
|
+
label="Same parameters for all models"
|
|
638
|
+
checked={doc.sync}
|
|
639
|
+
disabled={running}
|
|
640
|
+
onChange={(on) =>
|
|
641
|
+
updateDocument((d) => ({
|
|
642
|
+
...d,
|
|
643
|
+
sync: on,
|
|
644
|
+
compare: on
|
|
645
|
+
? d.compare.map((p) => ({
|
|
646
|
+
...p,
|
|
647
|
+
params: { ...d.compare[0].params },
|
|
648
|
+
}))
|
|
649
|
+
: d.compare,
|
|
650
|
+
}))
|
|
651
|
+
}
|
|
652
|
+
/>
|
|
653
|
+
{doc.compare.length < 3 && (
|
|
654
|
+
<Button
|
|
655
|
+
size="sm"
|
|
656
|
+
icon="plus"
|
|
657
|
+
disabled={running}
|
|
658
|
+
onClick={() =>
|
|
659
|
+
updateDocument((d) => ({
|
|
660
|
+
...d,
|
|
661
|
+
compare: [
|
|
662
|
+
...d.compare,
|
|
663
|
+
{
|
|
664
|
+
...newPanel(),
|
|
665
|
+
params: d.sync
|
|
666
|
+
? { ...d.compare[0].params }
|
|
667
|
+
: { ...newPanel().params },
|
|
668
|
+
},
|
|
669
|
+
],
|
|
670
|
+
}))
|
|
671
|
+
}
|
|
672
|
+
>
|
|
673
|
+
Add model
|
|
674
|
+
</Button>
|
|
675
|
+
)}
|
|
676
|
+
</>
|
|
677
|
+
)}
|
|
678
|
+
</div>
|
|
679
|
+
)}
|
|
680
|
+
</div>
|
|
681
|
+
{models && !models.length && (
|
|
682
|
+
<div class="panel">
|
|
683
|
+
<EmptyState
|
|
684
|
+
icon="models"
|
|
685
|
+
title="No enabled models"
|
|
686
|
+
action={
|
|
687
|
+
<Button icon="refresh" onClick={() => void loadModels()}>
|
|
688
|
+
Reload models
|
|
689
|
+
</Button>
|
|
690
|
+
}
|
|
691
|
+
>
|
|
692
|
+
Connect a provider and enable models on the Models page, then
|
|
693
|
+
reload the catalog.
|
|
694
|
+
</EmptyState>
|
|
695
|
+
</div>
|
|
696
|
+
)}
|
|
697
|
+
{models === null && !modelError ? (
|
|
698
|
+
<div class="panel">
|
|
699
|
+
<div class="panel-loading">
|
|
700
|
+
<Spinner />
|
|
701
|
+
</div>
|
|
702
|
+
</div>
|
|
703
|
+
) : tab === "transcription" ? (
|
|
704
|
+
<TranscriptionPage
|
|
705
|
+
key={doc.id}
|
|
706
|
+
state={doc.transcription ?? newTranscription()}
|
|
707
|
+
models={available}
|
|
708
|
+
maxBodyBytes={maxBodyBytes}
|
|
709
|
+
disabled={checking || busy.length > 0}
|
|
710
|
+
onBusyChange={setAudioBusy}
|
|
711
|
+
controllers={controllers.current}
|
|
712
|
+
onChange={(update) =>
|
|
713
|
+
updateDocument(
|
|
714
|
+
(d) => ({
|
|
715
|
+
...d,
|
|
716
|
+
transcription: update(d.transcription ?? newTranscription()),
|
|
717
|
+
}),
|
|
718
|
+
true,
|
|
719
|
+
)
|
|
720
|
+
}
|
|
721
|
+
/>
|
|
722
|
+
) : tab === "compliance" ? (
|
|
723
|
+
<>
|
|
724
|
+
<Panel
|
|
725
|
+
title="API compliance"
|
|
726
|
+
subtitle="Checks Chat Completions behavior through the rotator. Select up to three models. Uses real quota."
|
|
727
|
+
actions={
|
|
728
|
+
<>
|
|
729
|
+
<span class="sr-only" role="status">
|
|
730
|
+
{progress
|
|
731
|
+
? `Running ${progress.done} of ${progress.total} tests`
|
|
732
|
+
: finished}
|
|
733
|
+
</span>
|
|
734
|
+
<Button
|
|
735
|
+
icon="refresh"
|
|
736
|
+
loading={!!progress?.retry}
|
|
737
|
+
disabled={running || !failedCount}
|
|
738
|
+
onClick={() => void runCompliance(true)}
|
|
739
|
+
>
|
|
740
|
+
{progress?.retry ? progressLabel : "Retry failed"}
|
|
741
|
+
</Button>
|
|
742
|
+
<Button
|
|
743
|
+
variant="primary"
|
|
744
|
+
icon="play"
|
|
745
|
+
loading={!!progress && !progress.retry}
|
|
746
|
+
disabled={running || !testCount}
|
|
747
|
+
onClick={() => void runCompliance()}
|
|
748
|
+
>
|
|
749
|
+
{progress && !progress.retry
|
|
750
|
+
? progressLabel
|
|
751
|
+
: testCount
|
|
752
|
+
? `Run ${testCount} ${testCount === 1 ? "test" : "tests"}`
|
|
753
|
+
: "Run tests"}
|
|
754
|
+
</Button>
|
|
755
|
+
</>
|
|
756
|
+
}
|
|
757
|
+
>
|
|
758
|
+
<div class="form pg-picker">
|
|
759
|
+
<div class="field">
|
|
760
|
+
<div class="field-label-row">
|
|
761
|
+
<span class="field-label">Models</span>
|
|
762
|
+
<span class="muted">
|
|
763
|
+
{selectedCount} of {MAX_COMPLIANCE_MODELS} selected
|
|
764
|
+
</span>
|
|
765
|
+
<span class="spacer" />
|
|
766
|
+
{doc.complianceModels.length > 0 && !running && (
|
|
767
|
+
<button
|
|
768
|
+
type="button"
|
|
769
|
+
class="link-button"
|
|
770
|
+
onClick={() =>
|
|
771
|
+
updateDocument((d) => ({ ...d, complianceModels: [] }))
|
|
772
|
+
}
|
|
773
|
+
>
|
|
774
|
+
Clear
|
|
775
|
+
</button>
|
|
776
|
+
)}
|
|
777
|
+
</div>
|
|
778
|
+
<div
|
|
779
|
+
class="model-groups"
|
|
780
|
+
role="group"
|
|
781
|
+
aria-label="Compliance models"
|
|
782
|
+
>
|
|
783
|
+
<div class="model-grid">
|
|
784
|
+
{[...new Set([...available, ...doc.complianceModels])].map(
|
|
785
|
+
(model) => {
|
|
786
|
+
const checked = doc.complianceModels.includes(model);
|
|
787
|
+
const audioOnly = isAudioOnlyModel(model);
|
|
788
|
+
return (
|
|
789
|
+
<label key={model} class="checkbox model-option">
|
|
790
|
+
<input
|
|
791
|
+
type="checkbox"
|
|
792
|
+
checked={checked}
|
|
793
|
+
disabled={
|
|
794
|
+
running ||
|
|
795
|
+
(!checked &&
|
|
796
|
+
(audioOnly ||
|
|
797
|
+
selectedCount >= MAX_COMPLIANCE_MODELS))
|
|
798
|
+
}
|
|
799
|
+
onChange={(e) =>
|
|
800
|
+
updateDocument((d) => ({
|
|
801
|
+
...d,
|
|
802
|
+
complianceModels: e.currentTarget.checked
|
|
803
|
+
? [...d.complianceModels, model]
|
|
804
|
+
: d.complianceModels.filter(
|
|
805
|
+
(m) => m !== model,
|
|
806
|
+
),
|
|
807
|
+
}))
|
|
808
|
+
}
|
|
809
|
+
/>
|
|
810
|
+
<span class="truncate" title={model}>
|
|
811
|
+
{model}
|
|
812
|
+
</span>
|
|
813
|
+
{!available.includes(model) ? (
|
|
814
|
+
<Pill tone="warn" dot={false}>
|
|
815
|
+
Unavailable
|
|
816
|
+
</Pill>
|
|
817
|
+
) : (
|
|
818
|
+
audioOnly && (
|
|
819
|
+
<Pill tone="muted" dot={false}>
|
|
820
|
+
Audio only
|
|
821
|
+
</Pill>
|
|
822
|
+
)
|
|
823
|
+
)}
|
|
824
|
+
</label>
|
|
825
|
+
);
|
|
826
|
+
},
|
|
827
|
+
)}
|
|
828
|
+
</div>
|
|
829
|
+
</div>
|
|
830
|
+
</div>
|
|
831
|
+
<div class="field">
|
|
832
|
+
<div class="field-label-row">
|
|
833
|
+
<span class="field-label">Tests</span>
|
|
834
|
+
</div>
|
|
835
|
+
<div
|
|
836
|
+
class="model-groups"
|
|
837
|
+
role="group"
|
|
838
|
+
aria-label="Compliance tests"
|
|
839
|
+
>
|
|
840
|
+
<div class="model-grid">
|
|
841
|
+
{CASES.map((c) => (
|
|
842
|
+
<label key={c.id} class="checkbox model-option">
|
|
843
|
+
<input
|
|
844
|
+
type="checkbox"
|
|
845
|
+
disabled={running}
|
|
846
|
+
checked={doc.cases.includes(c.id)}
|
|
847
|
+
onChange={(e) =>
|
|
848
|
+
updateDocument((d) => ({
|
|
849
|
+
...d,
|
|
850
|
+
cases: e.currentTarget.checked
|
|
851
|
+
? [...d.cases, c.id]
|
|
852
|
+
: d.cases.filter((id) => id !== c.id),
|
|
853
|
+
}))
|
|
854
|
+
}
|
|
855
|
+
/>
|
|
856
|
+
<span class="truncate" title={c.label}>
|
|
857
|
+
{c.label}
|
|
858
|
+
</span>
|
|
859
|
+
</label>
|
|
860
|
+
))}
|
|
861
|
+
</div>
|
|
862
|
+
</div>
|
|
863
|
+
</div>
|
|
864
|
+
</div>
|
|
865
|
+
</Panel>
|
|
866
|
+
<Panel title="Results" flush>
|
|
867
|
+
<ComplianceResults results={doc.results} running={checking} />
|
|
868
|
+
</Panel>
|
|
869
|
+
</>
|
|
870
|
+
) : (
|
|
871
|
+
<>
|
|
872
|
+
{compare ? (
|
|
873
|
+
<div class={`pg-compare pg-compare-${doc.compare.length}`}>
|
|
874
|
+
{doc.compare.map((panel, i) => (
|
|
875
|
+
<ConversationPanel
|
|
876
|
+
key={panel.id}
|
|
877
|
+
panel={panel}
|
|
878
|
+
label={`Model ${i + 1}`}
|
|
879
|
+
compare
|
|
880
|
+
models={available}
|
|
881
|
+
busy={busy.includes(panel.id)}
|
|
882
|
+
running={running}
|
|
883
|
+
onModel={(model) =>
|
|
884
|
+
patchPanel(panel.id, (p) => ({ ...p, model, turns: [] }))
|
|
885
|
+
}
|
|
886
|
+
onParams={(patch) => changeParams(panel.id, patch)}
|
|
887
|
+
onRetry={() => void send(panel)}
|
|
888
|
+
onClear={() =>
|
|
889
|
+
patchPanel(panel.id, (p) => ({ ...p, turns: [] }))
|
|
890
|
+
}
|
|
891
|
+
onRemove={
|
|
892
|
+
doc.compare.length > 2
|
|
893
|
+
? () => {
|
|
894
|
+
updateDocument((d) => ({
|
|
895
|
+
...d,
|
|
896
|
+
compare: d.compare.filter((p) => p.id !== panel.id),
|
|
897
|
+
}));
|
|
898
|
+
// The removed column took focus with it; move to
|
|
899
|
+
// the model select of the column now in its place.
|
|
900
|
+
requestAnimationFrame(() => {
|
|
901
|
+
const selects = document.querySelectorAll<HTMLElement>(
|
|
902
|
+
".pg-compare .pg-model select",
|
|
903
|
+
);
|
|
904
|
+
selects[Math.min(i, selects.length - 1)]?.focus();
|
|
905
|
+
});
|
|
906
|
+
}
|
|
907
|
+
: undefined
|
|
908
|
+
}
|
|
909
|
+
onStop={() => controllers.current.get(panel.id)?.abort()}
|
|
910
|
+
/>
|
|
911
|
+
))}
|
|
912
|
+
</div>
|
|
913
|
+
) : (
|
|
914
|
+
<ConversationPanel
|
|
915
|
+
key={doc.chat.id}
|
|
916
|
+
panel={doc.chat}
|
|
917
|
+
label="Model"
|
|
918
|
+
compare={false}
|
|
919
|
+
models={available}
|
|
920
|
+
busy={busy.includes(doc.chat.id)}
|
|
921
|
+
running={running}
|
|
922
|
+
onModel={(model) =>
|
|
923
|
+
patchPanel(doc.chat.id, (p) => ({ ...p, model, turns: [] }))
|
|
924
|
+
}
|
|
925
|
+
onParams={(patch) => changeParams(doc.chat.id, patch)}
|
|
926
|
+
onRetry={() => void send(doc.chat)}
|
|
927
|
+
onClear={() =>
|
|
928
|
+
patchPanel(doc.chat.id, (p) => ({ ...p, turns: [] }))
|
|
929
|
+
}
|
|
930
|
+
onStop={() => controllers.current.get(doc.chat.id)?.abort()}
|
|
931
|
+
/>
|
|
932
|
+
)}
|
|
933
|
+
<form
|
|
934
|
+
class="panel pg-composer"
|
|
935
|
+
onSubmit={(e) => {
|
|
936
|
+
e.preventDefault();
|
|
937
|
+
if (!running) void send();
|
|
938
|
+
}}
|
|
939
|
+
>
|
|
940
|
+
<div class="panel-body">
|
|
941
|
+
<textarea
|
|
942
|
+
class="input"
|
|
943
|
+
rows={3}
|
|
944
|
+
aria-label={compare ? "Message all models" : "Message"}
|
|
945
|
+
placeholder={
|
|
946
|
+
compare
|
|
947
|
+
? "Message all models…"
|
|
948
|
+
: "Ask a question or attach an image…"
|
|
949
|
+
}
|
|
950
|
+
value={doc.draft}
|
|
951
|
+
disabled={running}
|
|
952
|
+
onInput={(e) =>
|
|
953
|
+
updateDocument((d) => ({
|
|
954
|
+
...d,
|
|
955
|
+
draft: e.currentTarget.value,
|
|
956
|
+
}))
|
|
957
|
+
}
|
|
958
|
+
/>
|
|
959
|
+
{!!doc.images.length && (
|
|
960
|
+
<div class="pg-attachments">
|
|
961
|
+
{doc.images.map((image, i) => (
|
|
962
|
+
<div key={i} class="pg-attachment">
|
|
963
|
+
{masked.value ? (
|
|
964
|
+
<Icon name="image" size={20} />
|
|
965
|
+
) : (
|
|
966
|
+
<img src={image.url} alt={image.name} />
|
|
967
|
+
)}
|
|
968
|
+
<Button
|
|
969
|
+
variant="ghost"
|
|
970
|
+
size="sm"
|
|
971
|
+
icon="x"
|
|
972
|
+
iconOnly
|
|
973
|
+
disabled={running}
|
|
974
|
+
onClick={() =>
|
|
975
|
+
updateDocument((d) => ({
|
|
976
|
+
...d,
|
|
977
|
+
images: d.images.filter((_, index) => i !== index),
|
|
978
|
+
}))
|
|
979
|
+
}
|
|
980
|
+
>
|
|
981
|
+
{`Remove ${masked.value ? "image" : image.name}`}
|
|
982
|
+
</Button>
|
|
983
|
+
</div>
|
|
984
|
+
))}
|
|
985
|
+
</div>
|
|
986
|
+
)}
|
|
987
|
+
<div class="pg-composer-foot">
|
|
988
|
+
<Button
|
|
989
|
+
icon="image"
|
|
990
|
+
title="Requires a vision model"
|
|
991
|
+
disabled={running}
|
|
992
|
+
onClick={() => imageRef.current?.click()}
|
|
993
|
+
>
|
|
994
|
+
Attach images
|
|
995
|
+
</Button>
|
|
996
|
+
<input
|
|
997
|
+
ref={imageRef}
|
|
998
|
+
type="file"
|
|
999
|
+
multiple
|
|
1000
|
+
accept="image/png,image/jpeg,image/webp"
|
|
1001
|
+
class="sr-only"
|
|
1002
|
+
aria-label="Attach images"
|
|
1003
|
+
onChange={(e) => {
|
|
1004
|
+
const files = Array.from(e.currentTarget.files ?? []);
|
|
1005
|
+
e.currentTarget.value = "";
|
|
1006
|
+
void attach(files);
|
|
1007
|
+
}}
|
|
1008
|
+
/>
|
|
1009
|
+
<span class="spacer" />
|
|
1010
|
+
<Button
|
|
1011
|
+
variant="primary"
|
|
1012
|
+
type="submit"
|
|
1013
|
+
disabled={
|
|
1014
|
+
running ||
|
|
1015
|
+
!available.length ||
|
|
1016
|
+
(!doc.draft.trim() && !doc.images.length)
|
|
1017
|
+
}
|
|
1018
|
+
>
|
|
1019
|
+
{compare ? "Send to all" : "Send"}
|
|
1020
|
+
</Button>
|
|
1021
|
+
</div>
|
|
1022
|
+
</div>
|
|
1023
|
+
</form>
|
|
1024
|
+
</>
|
|
1025
|
+
)}
|
|
1026
|
+
</>
|
|
1027
|
+
);
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
/** Chat's "Conversation" panel and each Compare column. */
|
|
1031
|
+
function ConversationPanel({
|
|
1032
|
+
panel,
|
|
1033
|
+
label,
|
|
1034
|
+
compare,
|
|
1035
|
+
models,
|
|
1036
|
+
busy,
|
|
1037
|
+
running,
|
|
1038
|
+
onModel,
|
|
1039
|
+
onParams,
|
|
1040
|
+
onRetry,
|
|
1041
|
+
onClear,
|
|
1042
|
+
onRemove,
|
|
1043
|
+
onStop,
|
|
1044
|
+
}: {
|
|
1045
|
+
panel: PlaygroundPanel;
|
|
1046
|
+
/** Accessible name of the model select ("Model", "Model 1", …). */
|
|
1047
|
+
label: string;
|
|
1048
|
+
compare: boolean;
|
|
1049
|
+
models: string[];
|
|
1050
|
+
busy: boolean;
|
|
1051
|
+
running: boolean;
|
|
1052
|
+
onModel: (model: string) => void;
|
|
1053
|
+
onParams: (patch: Partial<Parameters>) => void;
|
|
1054
|
+
onRetry: () => void;
|
|
1055
|
+
onClear: () => void;
|
|
1056
|
+
onRemove?: () => void;
|
|
1057
|
+
onStop: () => void;
|
|
1058
|
+
}): JSX.Element {
|
|
1059
|
+
const [paramsOpen, setParamsOpen] = useState(false);
|
|
1060
|
+
const turnsRef = useRef<HTMLDivElement>(null);
|
|
1061
|
+
const paramsId = `pg-params-${panel.id}`;
|
|
1062
|
+
const tags = parameterTags(panel.params);
|
|
1063
|
+
const idle = running || !panel.turns.length;
|
|
1064
|
+
const items: MenuItem[] = [
|
|
1065
|
+
{
|
|
1066
|
+
label: "Retry last turn",
|
|
1067
|
+
icon: "refresh",
|
|
1068
|
+
disabled: idle,
|
|
1069
|
+
onSelect: onRetry,
|
|
1070
|
+
},
|
|
1071
|
+
{ label: "Clear conversation", icon: "x", disabled: idle, onSelect: onClear },
|
|
1072
|
+
];
|
|
1073
|
+
if (onRemove)
|
|
1074
|
+
items.push("separator", {
|
|
1075
|
+
label: "Remove model",
|
|
1076
|
+
icon: "trash",
|
|
1077
|
+
danger: true,
|
|
1078
|
+
disabled: running,
|
|
1079
|
+
onSelect: onRemove,
|
|
1080
|
+
});
|
|
1081
|
+
const actions = (
|
|
1082
|
+
<>
|
|
1083
|
+
<label class="pg-model">
|
|
1084
|
+
<span class="sr-only">{label}</span>
|
|
1085
|
+
<select
|
|
1086
|
+
class="select"
|
|
1087
|
+
title={panel.model || "Choose a model"}
|
|
1088
|
+
disabled={running}
|
|
1089
|
+
value={panel.model}
|
|
1090
|
+
onChange={(e) => onModel(e.currentTarget.value)}
|
|
1091
|
+
>
|
|
1092
|
+
<option value="">Choose a model</option>
|
|
1093
|
+
{panel.model && !models.includes(panel.model) && (
|
|
1094
|
+
<option value={panel.model}>{panel.model} (unavailable)</option>
|
|
1095
|
+
)}
|
|
1096
|
+
{models.map((model) => (
|
|
1097
|
+
<option value={model} key={model}>
|
|
1098
|
+
{model}
|
|
1099
|
+
</option>
|
|
1100
|
+
))}
|
|
1101
|
+
</select>
|
|
1102
|
+
</label>
|
|
1103
|
+
{compare && busy && (
|
|
1104
|
+
<Button
|
|
1105
|
+
size="sm"
|
|
1106
|
+
icon="stop"
|
|
1107
|
+
class="pg-collapsible"
|
|
1108
|
+
title={`Stop ${label}`}
|
|
1109
|
+
aria-label={`Stop ${label}`}
|
|
1110
|
+
onClick={onStop}
|
|
1111
|
+
>
|
|
1112
|
+
Stop
|
|
1113
|
+
</Button>
|
|
1114
|
+
)}
|
|
1115
|
+
<Button
|
|
1116
|
+
variant="ghost"
|
|
1117
|
+
size="sm"
|
|
1118
|
+
icon="settings"
|
|
1119
|
+
class="pg-collapsible"
|
|
1120
|
+
title="Parameters"
|
|
1121
|
+
aria-label={compare ? `${label} parameters` : undefined}
|
|
1122
|
+
aria-expanded={paramsOpen}
|
|
1123
|
+
aria-controls={paramsOpen ? paramsId : undefined}
|
|
1124
|
+
onClick={() => setParamsOpen(!paramsOpen)}
|
|
1125
|
+
>
|
|
1126
|
+
Parameters
|
|
1127
|
+
</Button>
|
|
1128
|
+
<Menu
|
|
1129
|
+
label={compare ? `${label} actions` : "Conversation actions"}
|
|
1130
|
+
items={items}
|
|
1131
|
+
disabled={
|
|
1132
|
+
!items.some(
|
|
1133
|
+
(item) =>
|
|
1134
|
+
typeof item === "object" && "onSelect" in item && !item.disabled,
|
|
1135
|
+
)
|
|
1136
|
+
}
|
|
1137
|
+
/>
|
|
1138
|
+
</>
|
|
1139
|
+
);
|
|
1140
|
+
return (
|
|
1141
|
+
<Panel
|
|
1142
|
+
class="pg-panel"
|
|
1143
|
+
title={compare ? undefined : "Conversation"}
|
|
1144
|
+
actions={actions}
|
|
1145
|
+
>
|
|
1146
|
+
{tags.length > 0 && (
|
|
1147
|
+
<span class="tag-row">
|
|
1148
|
+
{tags.map((tag) => (
|
|
1149
|
+
<Tag key={tag}>{tag}</Tag>
|
|
1150
|
+
))}
|
|
1151
|
+
</span>
|
|
1152
|
+
)}
|
|
1153
|
+
{paramsOpen && (
|
|
1154
|
+
<div id={paramsId} class="pg-params">
|
|
1155
|
+
<ParameterEditor
|
|
1156
|
+
params={panel.params}
|
|
1157
|
+
disabled={running}
|
|
1158
|
+
onChange={onParams}
|
|
1159
|
+
/>
|
|
1160
|
+
</div>
|
|
1161
|
+
)}
|
|
1162
|
+
<div
|
|
1163
|
+
ref={turnsRef}
|
|
1164
|
+
class="pg-turns"
|
|
1165
|
+
tabIndex={-1}
|
|
1166
|
+
aria-live="polite"
|
|
1167
|
+
aria-busy={busy}
|
|
1168
|
+
>
|
|
1169
|
+
{panel.turns.length ? (
|
|
1170
|
+
panel.turns.map((turn, i) => (
|
|
1171
|
+
<TurnView
|
|
1172
|
+
key={i}
|
|
1173
|
+
turn={turn}
|
|
1174
|
+
detailsLabel={
|
|
1175
|
+
compare
|
|
1176
|
+
? `${label}, turn ${i + 1} details`
|
|
1177
|
+
: `Turn ${i + 1} details`
|
|
1178
|
+
}
|
|
1179
|
+
onRetry={
|
|
1180
|
+
i === panel.turns.length - 1 && !busy
|
|
1181
|
+
? () => {
|
|
1182
|
+
// The callout holding Retry is replaced by the new
|
|
1183
|
+
// turn; keep focus in this conversation.
|
|
1184
|
+
turnsRef.current?.focus({ preventScroll: true });
|
|
1185
|
+
onRetry();
|
|
1186
|
+
}
|
|
1187
|
+
: undefined
|
|
1188
|
+
}
|
|
1189
|
+
running={running}
|
|
1190
|
+
/>
|
|
1191
|
+
))
|
|
1192
|
+
) : (
|
|
1193
|
+
<EmptyState icon="play" title="No messages yet">
|
|
1194
|
+
Send a message to test this model.
|
|
1195
|
+
</EmptyState>
|
|
1196
|
+
)}
|
|
1197
|
+
</div>
|
|
1198
|
+
</Panel>
|
|
1199
|
+
);
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
/** Parameters that differ from a new panel, shown as tags in the panel body. */
|
|
1203
|
+
function parameterTags(params: Parameters): string[] {
|
|
1204
|
+
const tags: string[] = [];
|
|
1205
|
+
if (params.system.trim()) tags.push("System prompt");
|
|
1206
|
+
if (params.temperature !== DEFAULT_PARAMETERS.temperature)
|
|
1207
|
+
tags.push(`Temperature ${params.temperature}`);
|
|
1208
|
+
if (params.maxTokens !== DEFAULT_PARAMETERS.maxTokens)
|
|
1209
|
+
tags.push(
|
|
1210
|
+
params.maxTokens ? `Max tokens ${params.maxTokens}` : "No max tokens",
|
|
1211
|
+
);
|
|
1212
|
+
if (params.effort) tags.push(`Reasoning ${params.effort}`);
|
|
1213
|
+
if (params.tools.trim()) tags.push("Tools");
|
|
1214
|
+
if (params.stream !== DEFAULT_PARAMETERS.stream)
|
|
1215
|
+
tags.push(params.stream ? "Streaming" : "Streaming off");
|
|
1216
|
+
return tags;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
function ParameterEditor({
|
|
1220
|
+
params,
|
|
1221
|
+
disabled,
|
|
1222
|
+
onChange,
|
|
1223
|
+
}: {
|
|
1224
|
+
params: Parameters;
|
|
1225
|
+
disabled: boolean;
|
|
1226
|
+
onChange: (patch: Partial<Parameters>) => void;
|
|
1227
|
+
}): JSX.Element {
|
|
1228
|
+
const id = useId();
|
|
1229
|
+
return (
|
|
1230
|
+
<div class="form">
|
|
1231
|
+
<label class="field">
|
|
1232
|
+
<span class="field-label">System prompt</span>
|
|
1233
|
+
<textarea
|
|
1234
|
+
class="input"
|
|
1235
|
+
rows={3}
|
|
1236
|
+
placeholder="Optional instructions sent before every message"
|
|
1237
|
+
disabled={disabled}
|
|
1238
|
+
value={params.system}
|
|
1239
|
+
onInput={(e) => onChange({ system: e.currentTarget.value })}
|
|
1240
|
+
/>
|
|
1241
|
+
</label>
|
|
1242
|
+
<div class="form-grid">
|
|
1243
|
+
<label class="field">
|
|
1244
|
+
<span class="field-label">Temperature</span>
|
|
1245
|
+
<input
|
|
1246
|
+
class="input"
|
|
1247
|
+
type="number"
|
|
1248
|
+
min="0"
|
|
1249
|
+
max="2"
|
|
1250
|
+
step="0.1"
|
|
1251
|
+
placeholder="Provider default"
|
|
1252
|
+
disabled={disabled}
|
|
1253
|
+
value={params.temperature}
|
|
1254
|
+
onInput={(e) => onChange({ temperature: e.currentTarget.value })}
|
|
1255
|
+
/>
|
|
1256
|
+
</label>
|
|
1257
|
+
<label class="field">
|
|
1258
|
+
<span class="field-label">Max tokens</span>
|
|
1259
|
+
<input
|
|
1260
|
+
class="input"
|
|
1261
|
+
type="number"
|
|
1262
|
+
min="1"
|
|
1263
|
+
step="1"
|
|
1264
|
+
placeholder="Provider default"
|
|
1265
|
+
disabled={disabled}
|
|
1266
|
+
value={params.maxTokens}
|
|
1267
|
+
onInput={(e) => onChange({ maxTokens: e.currentTarget.value })}
|
|
1268
|
+
/>
|
|
1269
|
+
</label>
|
|
1270
|
+
<label class="field">
|
|
1271
|
+
<span class="field-label">Reasoning effort</span>
|
|
1272
|
+
<select
|
|
1273
|
+
class="select"
|
|
1274
|
+
disabled={disabled}
|
|
1275
|
+
value={params.effort}
|
|
1276
|
+
onChange={(e) => onChange({ effort: e.currentTarget.value })}
|
|
1277
|
+
>
|
|
1278
|
+
<option value="">Provider default</option>
|
|
1279
|
+
{["minimal", "low", "medium", "high", "xhigh"].map((effort) => (
|
|
1280
|
+
<option key={effort} value={effort}>
|
|
1281
|
+
{effort}
|
|
1282
|
+
</option>
|
|
1283
|
+
))}
|
|
1284
|
+
</select>
|
|
1285
|
+
</label>
|
|
1286
|
+
</div>
|
|
1287
|
+
<div class="field">
|
|
1288
|
+
<label class="field-label" for={`${id}-tools`}>
|
|
1289
|
+
Tools JSON (optional)
|
|
1290
|
+
</label>
|
|
1291
|
+
<textarea
|
|
1292
|
+
id={`${id}-tools`}
|
|
1293
|
+
class="code-input"
|
|
1294
|
+
rows={4}
|
|
1295
|
+
spellcheck={false}
|
|
1296
|
+
aria-describedby={`${id}-tools-hint`}
|
|
1297
|
+
placeholder='[{"type":"function","function":{"name":"example","parameters":{"type":"object"}}}]'
|
|
1298
|
+
disabled={disabled}
|
|
1299
|
+
value={params.tools}
|
|
1300
|
+
onInput={(e) => onChange({ tools: e.currentTarget.value })}
|
|
1301
|
+
/>
|
|
1302
|
+
<span class="field-hint" id={`${id}-tools-hint`}>
|
|
1303
|
+
Sent as entered; support depends on the model. Tool calls are shown,
|
|
1304
|
+
not executed.
|
|
1305
|
+
</span>
|
|
1306
|
+
</div>
|
|
1307
|
+
<Switch
|
|
1308
|
+
label="Stream response"
|
|
1309
|
+
checked={params.stream}
|
|
1310
|
+
disabled={disabled}
|
|
1311
|
+
onChange={(on) => onChange({ stream: on })}
|
|
1312
|
+
/>
|
|
1313
|
+
</div>
|
|
1314
|
+
);
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
function TurnView({
|
|
1318
|
+
turn,
|
|
1319
|
+
detailsLabel,
|
|
1320
|
+
onRetry,
|
|
1321
|
+
running,
|
|
1322
|
+
}: {
|
|
1323
|
+
turn: Turn;
|
|
1324
|
+
/** Accessible name of the Details toggle ("Turn 2 details", …). */
|
|
1325
|
+
detailsLabel: string;
|
|
1326
|
+
/** Offered on a failed last turn. */
|
|
1327
|
+
onRetry?: () => void;
|
|
1328
|
+
running: boolean;
|
|
1329
|
+
}): JSX.Element {
|
|
1330
|
+
const [open, setOpen] = useState(false);
|
|
1331
|
+
const result = turn.result;
|
|
1332
|
+
const lastMessage = turn.request.messages.at(-1)?.content;
|
|
1333
|
+
const m = masker.value;
|
|
1334
|
+
const content =
|
|
1335
|
+
typeof lastMessage === "string"
|
|
1336
|
+
? [{ type: "text" as const, text: lastMessage }]
|
|
1337
|
+
: (lastMessage ?? []);
|
|
1338
|
+
const images = content.flatMap((part) =>
|
|
1339
|
+
part.type === "image_url" ? [part.image_url.url] : [],
|
|
1340
|
+
);
|
|
1341
|
+
return (
|
|
1342
|
+
<article class="pg-turn">
|
|
1343
|
+
<div class="pg-user">
|
|
1344
|
+
<span class="field-label">You</span>
|
|
1345
|
+
{content.map(
|
|
1346
|
+
(part, i) =>
|
|
1347
|
+
part.type === "text" && (
|
|
1348
|
+
<p key={i} class="pg-message">
|
|
1349
|
+
{m.text(part.text)}
|
|
1350
|
+
</p>
|
|
1351
|
+
),
|
|
1352
|
+
)}
|
|
1353
|
+
{!!images.length && (
|
|
1354
|
+
<div class="pg-attachments">
|
|
1355
|
+
{images.map((url, i) => (
|
|
1356
|
+
<div key={i} class="pg-attachment">
|
|
1357
|
+
{masked.value ? (
|
|
1358
|
+
<>
|
|
1359
|
+
<Icon name="image" size={20} />
|
|
1360
|
+
<span class="sr-only">Image hidden</span>
|
|
1361
|
+
</>
|
|
1362
|
+
) : (
|
|
1363
|
+
<img src={url} alt="Attached image" />
|
|
1364
|
+
)}
|
|
1365
|
+
</div>
|
|
1366
|
+
))}
|
|
1367
|
+
</div>
|
|
1368
|
+
)}
|
|
1369
|
+
</div>
|
|
1370
|
+
<div class="pg-assistant">
|
|
1371
|
+
<span class="field-label">Assistant</span>
|
|
1372
|
+
{result?.reasoning && (
|
|
1373
|
+
<div
|
|
1374
|
+
class="pg-reasoning"
|
|
1375
|
+
tabIndex={0}
|
|
1376
|
+
role="region"
|
|
1377
|
+
aria-label="Reasoning"
|
|
1378
|
+
>
|
|
1379
|
+
{m.text(result.reasoning)}
|
|
1380
|
+
</div>
|
|
1381
|
+
)}
|
|
1382
|
+
{result?.content ? (
|
|
1383
|
+
<p class="pg-message">{m.text(result.content)}</p>
|
|
1384
|
+
) : (
|
|
1385
|
+
!turn.error &&
|
|
1386
|
+
!result?.tools.length && (
|
|
1387
|
+
<p class="pg-message muted">
|
|
1388
|
+
{result?.done ? "Empty response." : "Waiting for response…"}
|
|
1389
|
+
</p>
|
|
1390
|
+
)
|
|
1391
|
+
)}
|
|
1392
|
+
{!!result?.tools.length && (
|
|
1393
|
+
<>
|
|
1394
|
+
<span class="field-label">Tool calls (not executed)</span>
|
|
1395
|
+
<pre class="code">
|
|
1396
|
+
{m.text(JSON.stringify(result.tools, null, 2))}
|
|
1397
|
+
</pre>
|
|
1398
|
+
</>
|
|
1399
|
+
)}
|
|
1400
|
+
{turn.error && (
|
|
1401
|
+
<div class="callout tone-bad" role="alert">
|
|
1402
|
+
<Icon name="critical" size={16} />
|
|
1403
|
+
<div>
|
|
1404
|
+
<strong>
|
|
1405
|
+
{turn.error === CANCELLED_MESSAGE
|
|
1406
|
+
? "Request stopped"
|
|
1407
|
+
: "Request failed"}
|
|
1408
|
+
</strong>
|
|
1409
|
+
<p>{m.text(turn.error)}</p>
|
|
1410
|
+
{onRetry && (
|
|
1411
|
+
<Button
|
|
1412
|
+
size="sm"
|
|
1413
|
+
icon="refresh"
|
|
1414
|
+
disabled={running}
|
|
1415
|
+
onClick={onRetry}
|
|
1416
|
+
>
|
|
1417
|
+
Retry
|
|
1418
|
+
</Button>
|
|
1419
|
+
)}
|
|
1420
|
+
</div>
|
|
1421
|
+
</div>
|
|
1422
|
+
)}
|
|
1423
|
+
<div class="pg-turn-foot">
|
|
1424
|
+
{result && <Metrics result={result} class="pg-metrics" />}
|
|
1425
|
+
<Button
|
|
1426
|
+
variant="ghost"
|
|
1427
|
+
size="sm"
|
|
1428
|
+
icon={open ? "chevronDown" : "chevronRight"}
|
|
1429
|
+
aria-label={detailsLabel}
|
|
1430
|
+
aria-expanded={open}
|
|
1431
|
+
onClick={() => setOpen(!open)}
|
|
1432
|
+
>
|
|
1433
|
+
Details
|
|
1434
|
+
</Button>
|
|
1435
|
+
</div>
|
|
1436
|
+
{open && <TurnInspector turn={turn} />}
|
|
1437
|
+
</div>
|
|
1438
|
+
</article>
|
|
1439
|
+
);
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
function Metrics({
|
|
1443
|
+
result,
|
|
1444
|
+
class: className,
|
|
1445
|
+
}: {
|
|
1446
|
+
result: ChatResult;
|
|
1447
|
+
class?: string;
|
|
1448
|
+
}): JSX.Element {
|
|
1449
|
+
const cost = Number(result.metadata["x-rotator-cost-usd"] ?? NaN);
|
|
1450
|
+
return (
|
|
1451
|
+
<div class={`inspector-meta${className ? ` ${className}` : ""}`}>
|
|
1452
|
+
<span>Total {duration(result.durationMs)}</span>
|
|
1453
|
+
<span>First content {duration(result.firstContentMs)}</span>
|
|
1454
|
+
<span>
|
|
1455
|
+
Tokens {result.inputTokens ?? "—"} / {result.outputTokens ?? "—"}
|
|
1456
|
+
</span>
|
|
1457
|
+
<span>Finish {result.finishReason ?? "—"}</span>
|
|
1458
|
+
<span>
|
|
1459
|
+
List-price value {Number.isFinite(cost) ? formatUsd(cost) : "—"}
|
|
1460
|
+
</span>
|
|
1461
|
+
</div>
|
|
1462
|
+
);
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
const INSPECTOR_TABS = [
|
|
1466
|
+
{ value: "request", label: "Request" },
|
|
1467
|
+
{ value: "response", label: "Response" },
|
|
1468
|
+
{ value: "routing", label: "Routing" },
|
|
1469
|
+
] as const;
|
|
1470
|
+
type InspectorTab = (typeof INSPECTOR_TABS)[number]["value"];
|
|
1471
|
+
|
|
1472
|
+
/** Inline image data would swamp the preview; keep a recognizable prefix. */
|
|
1473
|
+
const omitImageData = (_: string, value: unknown): unknown =>
|
|
1474
|
+
typeof value === "string" && value.startsWith("data:image/")
|
|
1475
|
+
? `${value.slice(0, 45)}… [image data omitted from preview]`
|
|
1476
|
+
: value;
|
|
1477
|
+
|
|
1478
|
+
/** Request / Response / Routing payloads, as in the Requests inspector. */
|
|
1479
|
+
function TurnInspector({
|
|
1480
|
+
turn,
|
|
1481
|
+
meta,
|
|
1482
|
+
note,
|
|
1483
|
+
}: {
|
|
1484
|
+
turn: Turn;
|
|
1485
|
+
/** Repeat the turn metrics above the tabs (table rows have no room). */
|
|
1486
|
+
meta?: boolean;
|
|
1487
|
+
/** A line above everything else, such as a compliance explanation. */
|
|
1488
|
+
note?: string;
|
|
1489
|
+
}): JSX.Element {
|
|
1490
|
+
const [tab, setTab] = useState<InspectorTab>("request");
|
|
1491
|
+
const result = turn.result;
|
|
1492
|
+
const metrics = (
|
|
1493
|
+
<>
|
|
1494
|
+
{note && <p>{note}</p>}
|
|
1495
|
+
{meta && result && <Metrics result={result} />}
|
|
1496
|
+
</>
|
|
1497
|
+
);
|
|
1498
|
+
// The turn list is a polite live region; raw JSON must not be read out
|
|
1499
|
+
// when Details opens or a tab switches.
|
|
1500
|
+
if (masked.value)
|
|
1501
|
+
return (
|
|
1502
|
+
<div class="inspector" aria-live="off">
|
|
1503
|
+
{metrics}
|
|
1504
|
+
<p class="muted">Raw details hidden while the privacy mask is on.</p>
|
|
1505
|
+
</div>
|
|
1506
|
+
);
|
|
1507
|
+
const raw = result?.raw ?? [];
|
|
1508
|
+
const routing = Object.entries(result?.metadata ?? {});
|
|
1509
|
+
const payload =
|
|
1510
|
+
tab === "request"
|
|
1511
|
+
? turn.request
|
|
1512
|
+
: tab === "response"
|
|
1513
|
+
? raw.length > 1
|
|
1514
|
+
? raw
|
|
1515
|
+
: raw[0]
|
|
1516
|
+
: routing.length
|
|
1517
|
+
? result?.metadata
|
|
1518
|
+
: undefined;
|
|
1519
|
+
const text =
|
|
1520
|
+
payload === undefined ? "" : JSON.stringify(payload, omitImageData, 2);
|
|
1521
|
+
// Copy keeps inline images so a copied request can be replayed as is.
|
|
1522
|
+
const full = payload === undefined ? "" : JSON.stringify(payload, null, 2);
|
|
1523
|
+
return (
|
|
1524
|
+
<div class="inspector" aria-live="off">
|
|
1525
|
+
{metrics}
|
|
1526
|
+
<div class="tabs tabs-sm" role="tablist" aria-label="Turn details">
|
|
1527
|
+
{INSPECTOR_TABS.map((t) => (
|
|
1528
|
+
<button
|
|
1529
|
+
key={t.value}
|
|
1530
|
+
type="button"
|
|
1531
|
+
role="tab"
|
|
1532
|
+
aria-selected={tab === t.value}
|
|
1533
|
+
class={tab === t.value ? "is-active" : undefined}
|
|
1534
|
+
onClick={() => setTab(t.value)}
|
|
1535
|
+
>
|
|
1536
|
+
{t.label}
|
|
1537
|
+
</button>
|
|
1538
|
+
))}
|
|
1539
|
+
<span class="tabs-spacer" />
|
|
1540
|
+
<Button
|
|
1541
|
+
size="sm"
|
|
1542
|
+
variant="ghost"
|
|
1543
|
+
icon="copy"
|
|
1544
|
+
disabled={!text}
|
|
1545
|
+
onClick={() => void copy(full)}
|
|
1546
|
+
>
|
|
1547
|
+
Copy
|
|
1548
|
+
</Button>
|
|
1549
|
+
</div>
|
|
1550
|
+
{!text ? (
|
|
1551
|
+
<p class="muted">Nothing recorded.</p>
|
|
1552
|
+
) : tab === "routing" ? (
|
|
1553
|
+
<KeyValue items={routing.map(([key, value]) => [key, value])} />
|
|
1554
|
+
) : (
|
|
1555
|
+
<pre class="code">{text}</pre>
|
|
1556
|
+
)}
|
|
1557
|
+
</div>
|
|
1558
|
+
);
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
const SUMMARY: Array<[Verdict, string, string]> = [
|
|
1562
|
+
["passed", "passed", "passed"],
|
|
1563
|
+
["failed", "failed", "failed"],
|
|
1564
|
+
["unsupported", "not supported", "not supported"],
|
|
1565
|
+
["error", "error", "errors"],
|
|
1566
|
+
["cancelled", "cancelled", "cancelled"],
|
|
1567
|
+
];
|
|
1568
|
+
|
|
1569
|
+
function ComplianceResults({
|
|
1570
|
+
results,
|
|
1571
|
+
running,
|
|
1572
|
+
}: {
|
|
1573
|
+
results: ComplianceResult[];
|
|
1574
|
+
running: boolean;
|
|
1575
|
+
}): JSX.Element {
|
|
1576
|
+
const [open, setOpen] = useState<string | null>(null);
|
|
1577
|
+
const m = masker.value;
|
|
1578
|
+
if (!results.length)
|
|
1579
|
+
return running ? (
|
|
1580
|
+
<div class="panel-loading">
|
|
1581
|
+
<Spinner />
|
|
1582
|
+
</div>
|
|
1583
|
+
) : (
|
|
1584
|
+
<EmptyState icon="checkCircle" title="No results yet">
|
|
1585
|
+
Choose models and tests, then run them.
|
|
1586
|
+
</EmptyState>
|
|
1587
|
+
);
|
|
1588
|
+
const counts = new Map<Verdict, number>();
|
|
1589
|
+
for (const r of results)
|
|
1590
|
+
counts.set(r.verdict, (counts.get(r.verdict) ?? 0) + 1);
|
|
1591
|
+
// Parallel runs finish out of order; group by model, then test order.
|
|
1592
|
+
const order = CASES.map((c) => c.id);
|
|
1593
|
+
const rows = results
|
|
1594
|
+
.map((row, index) => ({ row, index }))
|
|
1595
|
+
.sort(
|
|
1596
|
+
(a, b) =>
|
|
1597
|
+
a.row.model.localeCompare(b.row.model) ||
|
|
1598
|
+
order.indexOf(a.row.caseId) - order.indexOf(b.row.caseId) ||
|
|
1599
|
+
a.index - b.index,
|
|
1600
|
+
);
|
|
1601
|
+
return (
|
|
1602
|
+
<>
|
|
1603
|
+
<div class="stats stats-inline">
|
|
1604
|
+
{SUMMARY.map(([verdict, one, many]) => {
|
|
1605
|
+
const count = counts.get(verdict) ?? 0;
|
|
1606
|
+
return (
|
|
1607
|
+
(count > 0 || verdict === "passed") && (
|
|
1608
|
+
<span key={verdict}>
|
|
1609
|
+
<strong class="num">{count}</strong>{" "}
|
|
1610
|
+
{count === 1 ? one : many}
|
|
1611
|
+
</span>
|
|
1612
|
+
)
|
|
1613
|
+
);
|
|
1614
|
+
})}
|
|
1615
|
+
</div>
|
|
1616
|
+
<div class="table-scroll">
|
|
1617
|
+
<table class="table table-compact pg-results">
|
|
1618
|
+
<thead>
|
|
1619
|
+
<tr>
|
|
1620
|
+
<th scope="col">Model</th>
|
|
1621
|
+
<th scope="col">Test</th>
|
|
1622
|
+
<th scope="col">Result</th>
|
|
1623
|
+
<th scope="col" class="num-col hide-sm">
|
|
1624
|
+
Duration
|
|
1625
|
+
</th>
|
|
1626
|
+
<th scope="col">
|
|
1627
|
+
<span class="sr-only">Details</span>
|
|
1628
|
+
</th>
|
|
1629
|
+
</tr>
|
|
1630
|
+
</thead>
|
|
1631
|
+
<tbody>
|
|
1632
|
+
{rows.map(({ row, index }) => {
|
|
1633
|
+
const key = JSON.stringify([row.model, row.caseId]);
|
|
1634
|
+
const expanded = open === key;
|
|
1635
|
+
const toggle = () => setOpen(expanded ? null : key);
|
|
1636
|
+
const explanation = m.text(row.explanation);
|
|
1637
|
+
const testLabel =
|
|
1638
|
+
CASES.find((c) => c.id === row.caseId)?.label ?? row.caseId;
|
|
1639
|
+
return (
|
|
1640
|
+
<Fragment key={`${key}:${index}`}>
|
|
1641
|
+
<tr
|
|
1642
|
+
class={`history-row${expanded ? " is-open" : ""}`}
|
|
1643
|
+
onClick={toggle}
|
|
1644
|
+
>
|
|
1645
|
+
<td>{row.model}</td>
|
|
1646
|
+
<td>{testLabel}</td>
|
|
1647
|
+
<td>
|
|
1648
|
+
<Pill
|
|
1649
|
+
tone={VERDICT_TONES[row.verdict]}
|
|
1650
|
+
title={row.verdict === "passed" ? explanation : undefined}
|
|
1651
|
+
>
|
|
1652
|
+
{VERDICT_LABELS[row.verdict]}
|
|
1653
|
+
</Pill>
|
|
1654
|
+
{row.verdict !== "passed" && explanation && (
|
|
1655
|
+
<div class="cell-sub">{explanation}</div>
|
|
1656
|
+
)}
|
|
1657
|
+
</td>
|
|
1658
|
+
<td class="num num-col hide-sm">
|
|
1659
|
+
{duration(row.turn.result?.durationMs)}
|
|
1660
|
+
</td>
|
|
1661
|
+
<td class="cell-action">
|
|
1662
|
+
<Button
|
|
1663
|
+
size="sm"
|
|
1664
|
+
variant="ghost"
|
|
1665
|
+
iconOnly
|
|
1666
|
+
icon={expanded ? "chevronDown" : "chevronRight"}
|
|
1667
|
+
aria-expanded={expanded}
|
|
1668
|
+
onClick={(event) => {
|
|
1669
|
+
event.stopPropagation();
|
|
1670
|
+
toggle();
|
|
1671
|
+
}}
|
|
1672
|
+
>
|
|
1673
|
+
{`${expanded ? "Hide" : "Show"} details for ${row.model}, ${testLabel}`}
|
|
1674
|
+
</Button>
|
|
1675
|
+
</td>
|
|
1676
|
+
</tr>
|
|
1677
|
+
{expanded && (
|
|
1678
|
+
<tr class="history-detail">
|
|
1679
|
+
<td colSpan={5}>
|
|
1680
|
+
<TurnInspector turn={row.turn} meta note={explanation} />
|
|
1681
|
+
</td>
|
|
1682
|
+
</tr>
|
|
1683
|
+
)}
|
|
1684
|
+
</Fragment>
|
|
1685
|
+
);
|
|
1686
|
+
})}
|
|
1687
|
+
</tbody>
|
|
1688
|
+
</table>
|
|
1689
|
+
</div>
|
|
1690
|
+
</>
|
|
1691
|
+
);
|
|
1692
|
+
}
|