@tonyclaw/llm-inspector 1.14.7 → 1.14.9

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.
Files changed (34) hide show
  1. package/.output/nitro.json +1 -1
  2. package/.output/public/assets/index-Dv-dj1xH.js +105 -0
  3. package/.output/public/assets/index-bqeypwJB.css +1 -0
  4. package/.output/public/assets/{main-BV7uNIIz.js → main-C8OUJKbz.js} +1 -1
  5. package/.output/server/_libs/lucide-react.mjs +87 -79
  6. package/.output/server/_libs/radix-ui__react-id.mjs +1 -1
  7. package/.output/server/_ssr/{index-BvHLASu8.mjs → index-_9xcAkkw.mjs} +861 -608
  8. package/.output/server/_ssr/index.mjs +2 -2
  9. package/.output/server/_ssr/{router-lUOA8pi6.mjs → router-CmanwZJc.mjs} +45 -14
  10. package/.output/server/{_tanstack-start-manifest_v-XNH7fVPN.mjs → _tanstack-start-manifest_v-BVIiyDeJ.mjs} +1 -1
  11. package/.output/server/index.mjs +23 -23
  12. package/package.json +1 -1
  13. package/src/components/ProxyViewer.tsx +137 -146
  14. package/src/components/providers/ProviderCard.tsx +79 -26
  15. package/src/components/providers/ProviderForm.tsx +37 -22
  16. package/src/components/providers/ProvidersPanel.tsx +79 -47
  17. package/src/components/providers/SettingsDialog.tsx +25 -15
  18. package/src/components/proxy-viewer/ConversationGroup.tsx +74 -11
  19. package/src/components/proxy-viewer/ConversationHeader.tsx +63 -2
  20. package/src/components/proxy-viewer/LogEntry.tsx +184 -54
  21. package/src/components/proxy-viewer/LogEntryHeader.tsx +148 -143
  22. package/src/components/proxy-viewer/ReplayDialog.tsx +16 -6
  23. package/src/components/proxy-viewer/StreamingChunkSequence.tsx +24 -16
  24. package/src/components/proxy-viewer/ThreadConnector.tsx +93 -0
  25. package/src/components/proxy-viewer/index.ts +2 -1
  26. package/src/lib/stopReason.ts +57 -0
  27. package/src/proxy/formats/anthropic/handler.ts +2 -5
  28. package/src/proxy/formats/openai/handler.ts +33 -7
  29. package/src/proxy/formats/openai/schemas.ts +1 -0
  30. package/src/proxy/formats/openai/stream.ts +24 -0
  31. package/src/proxy/handler.ts +8 -2
  32. package/src/proxy/schemas.ts +6 -3
  33. package/.output/public/assets/index-Cmi8TfeU.js +0 -105
  34. package/.output/public/assets/index-DXUNTCVh.css +0 -1
@@ -25,11 +25,41 @@ export const OpenAIFormatHandler: FormatHandler = {
25
25
  extractTokens(responseBody: string): TokenUsage {
26
26
  const parsed = parseOpenAIResponse(responseBody);
27
27
  if (parsed) {
28
+ // OpenAI puts cached_tokens in usage.prompt_tokens_details (passthrough field)
29
+ let cacheReadInputTokens: number | null = null;
30
+ try {
31
+ const raw: unknown = JSON.parse(responseBody);
32
+ if (raw !== null && typeof raw === "object" && !Array.isArray(raw)) {
33
+ const usageDesc = Object.getOwnPropertyDescriptor(raw, "usage");
34
+ if (
35
+ usageDesc !== undefined &&
36
+ typeof usageDesc.value === "object" &&
37
+ usageDesc.value !== null
38
+ ) {
39
+ const detailsDesc = Object.getOwnPropertyDescriptor(
40
+ usageDesc.value,
41
+ "prompt_tokens_details",
42
+ );
43
+ if (
44
+ detailsDesc !== undefined &&
45
+ typeof detailsDesc.value === "object" &&
46
+ detailsDesc.value !== null
47
+ ) {
48
+ const cacheDesc = Object.getOwnPropertyDescriptor(detailsDesc.value, "cached_tokens");
49
+ if (cacheDesc !== undefined && typeof cacheDesc.value === "number") {
50
+ cacheReadInputTokens = cacheDesc.value;
51
+ }
52
+ }
53
+ }
54
+ }
55
+ } catch {
56
+ // ignore parse errors
57
+ }
28
58
  return {
29
59
  inputTokens: parsed.usage.prompt_tokens ?? null,
30
60
  outputTokens: parsed.usage.completion_tokens ?? null,
31
61
  cacheCreationInputTokens: null,
32
- cacheReadInputTokens: null,
62
+ cacheReadInputTokens,
33
63
  };
34
64
  }
35
65
  return {
@@ -55,12 +85,8 @@ export const OpenAIFormatHandler: FormatHandler = {
55
85
  const json: unknown = JSON.parse(rawBody);
56
86
  if (typeof json === "object" && json !== null && !Array.isArray(json)) {
57
87
  const keys = Object.keys(json);
58
- if (keys.includes("model") && keys.includes("messages")) {
59
- // OpenAI doesn't use "system" or "tools" keys at root level
60
- if (!keys.includes("system") && !keys.includes("tools")) {
61
- return true;
62
- }
63
- }
88
+ // OpenAI has `model` and `messages` at the top level, but NOT `system`
89
+ return keys.includes("model") && keys.includes("messages") && !keys.includes("system");
64
90
  }
65
91
  return false;
66
92
  } catch {
@@ -73,6 +73,7 @@ export const OpenAIRequestSchema = z.object({
73
73
  tools: z.array(OpenAIToolDefinition).optional(),
74
74
  tool_choice: z
75
75
  .union([
76
+ z.enum(["auto", "none", "required"]),
76
77
  z.object({ type: z.literal("auto") }),
77
78
  z.object({ type: z.literal("none") }),
78
79
  z.object({ type: z.literal("function"), function: z.object({ name: z.string() }) }),
@@ -93,6 +93,30 @@ export function extractOpenAIStream(
93
93
  promptTokens = chunk.usage.prompt_tokens ?? 0;
94
94
  completionTokens = chunk.usage.completion_tokens ?? 0;
95
95
  log.inputTokens = promptTokens;
96
+ // Extract cached_tokens from raw parsed object (passthrough field in usage)
97
+ if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
98
+ const usageDesc = Object.getOwnPropertyDescriptor(parsed, "usage");
99
+ if (
100
+ usageDesc !== undefined &&
101
+ typeof usageDesc.value === "object" &&
102
+ usageDesc.value !== null
103
+ ) {
104
+ const detailsDesc = Object.getOwnPropertyDescriptor(
105
+ usageDesc.value,
106
+ "prompt_tokens_details",
107
+ );
108
+ if (
109
+ detailsDesc !== undefined &&
110
+ typeof detailsDesc.value === "object" &&
111
+ detailsDesc.value !== null
112
+ ) {
113
+ const cacheDesc = Object.getOwnPropertyDescriptor(detailsDesc.value, "cached_tokens");
114
+ if (cacheDesc !== undefined && typeof cacheDesc.value === "number") {
115
+ log.cacheReadInputTokens = cacheDesc.value;
116
+ }
117
+ }
118
+ }
119
+ }
96
120
  usageCaptured = true;
97
121
  }
98
122
 
@@ -5,7 +5,7 @@ import { extractRequestMetadata } from "./schemas";
5
5
  import { registry } from "./formats";
6
6
  import { findProviderByModel } from "./providers";
7
7
  import { getClientInfo } from "./socketTracker";
8
- import { formatForPath, type FormatHandler } from "./formats";
8
+ import { formatForPath, formatRegistry, type FormatHandler } from "./formats";
9
9
  import {
10
10
  PROXY_IDENTITY,
11
11
  PRESERVE_HEADERS,
@@ -308,6 +308,12 @@ export async function handleProxy(req: Request): Promise<Response> {
308
308
  upstreamHeaders.forEach((value, key) => {
309
309
  upstreamHeadersObj[key.toLowerCase()] = value;
310
310
  });
311
+ // Detect the true format from the request body for accurate UI display.
312
+ // The path-based format (formatHandler.format) drives routing, but the body
313
+ // structure determines whether it's actually Anthropic or OpenAI.
314
+ const bodyFormat = formatRegistry.detectFormat(requestBody);
315
+ const displayApiFormat = bodyFormat !== "unknown" ? bodyFormat : formatHandler.format;
316
+
311
317
  const log = await createLog(
312
318
  req.method,
313
319
  parsed.apiPath,
@@ -316,7 +322,7 @@ export async function handleProxy(req: Request): Promise<Response> {
316
322
  clientInfo,
317
323
  rawHeaders,
318
324
  upstreamHeadersObj,
319
- formatHandler.format,
325
+ displayApiFormat,
320
326
  model,
321
327
  sessionId,
322
328
  preAcquiredId,
@@ -111,9 +111,12 @@ function detectFormat(rawBody: string | null): RequestFormat {
111
111
  try {
112
112
  const json: unknown = JSON.parse(rawBody);
113
113
  if (typeof json === "object" && json !== null && !Array.isArray(json)) {
114
- const keys = Object.keys(json);
115
- if (keys.includes("model") && keys.includes("messages")) {
116
- if (keys.includes("system") || keys.includes("tools")) {
114
+ const hasModel = safeGetProperty(json, "model") !== undefined;
115
+ const hasMessages = safeGetProperty(json, "messages") !== undefined;
116
+ if (hasModel && hasMessages) {
117
+ // Anthropic requests put `system` as a top-level key; OpenAI does not.
118
+ // Both formats can have `tools`, so we check `system` as the discriminator.
119
+ if (safeGetProperty(json, "system") !== undefined) {
117
120
  return "anthropic";
118
121
  }
119
122
  const msgVal = safeGetProperty(json, "messages");