@tonyclaw/llm-inspector 1.12.0 → 1.13.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/index-B0anmGQr.css +1 -0
- package/.output/public/assets/index-H_thmL2_.js +105 -0
- package/.output/public/assets/{main-BYCM7aJx.js → main-C3tLo75s.js} +3 -3
- package/.output/server/_libs/lucide-react.mjs +4 -4
- package/.output/server/_ssr/{index-DhChP_jV.mjs → index-C8VC13EA.mjs} +781 -163
- package/.output/server/_ssr/index.mjs +2 -2
- package/.output/server/_ssr/{router-PZjNwOcw.mjs → router-D5ccnemB.mjs} +2 -2
- package/.output/server/{_tanstack-start-manifest_v-l1kWkG0h.mjs → _tanstack-start-manifest_v-DUbXa1lt.mjs} +1 -1
- package/.output/server/index.mjs +26 -26
- package/package.json +1 -1
- package/src/components/ProxyViewer.tsx +126 -2
- package/src/components/proxy-viewer/CompareDrawer.tsx +388 -0
- package/src/components/proxy-viewer/ConversationGroup.tsx +8 -0
- package/src/components/proxy-viewer/LogEntry.tsx +14 -1
- package/src/components/proxy-viewer/LogEntryHeader.tsx +28 -0
- package/src/components/proxy-viewer/requestDiff.ts +277 -0
- package/.output/public/assets/index-DVgdkDgq.js +0 -105
- package/.output/public/assets/index-DZx2yk8v.css +0 -1
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path-aligned JSON-tree diff helpers for the "compare two log requests" feature.
|
|
3
|
+
*
|
|
4
|
+
* Given two captured log Request payloads, we:
|
|
5
|
+
* 1. Normalize each payload (deep-clone, sort object keys, keep array order)
|
|
6
|
+
* so that non-semantic differences (key order, string whitespace) do not
|
|
7
|
+
* generate false positives.
|
|
8
|
+
* 2. Walk the two trees in lockstep, emitting a list of path-anchored
|
|
9
|
+
* `DiffOp`s. Equal subtrees collapse into a single op at the subtree
|
|
10
|
+
* root, so the renderer can choose to expand or hide them.
|
|
11
|
+
* 3. Emit ops in path-sorted order (depth-first, sibling order = object-key
|
|
12
|
+
* ascending then array-index ascending) so the renderer can lay them
|
|
13
|
+
* out linearly.
|
|
14
|
+
*
|
|
15
|
+
* No runtime dependencies. Mirrors the `cacheTrend.ts` pure-helper pattern.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type JsonPrimitive = string | number | boolean | null;
|
|
19
|
+
export type JsonValue = JsonPrimitive | JsonValue[] | { [key: string]: JsonValue };
|
|
20
|
+
|
|
21
|
+
export type JsonNode =
|
|
22
|
+
| { kind: "object"; value: Record<string, JsonNode> }
|
|
23
|
+
| { kind: "array"; value: JsonNode[] }
|
|
24
|
+
| { kind: "primitive"; value: JsonPrimitive };
|
|
25
|
+
|
|
26
|
+
export type DiffOp =
|
|
27
|
+
| { kind: "equal"; path: string; value: JsonNode }
|
|
28
|
+
| { kind: "added"; path: string; value: JsonNode }
|
|
29
|
+
| { kind: "removed"; path: string; value: JsonNode }
|
|
30
|
+
| {
|
|
31
|
+
kind: "changed";
|
|
32
|
+
path: string;
|
|
33
|
+
left: JsonNode;
|
|
34
|
+
right: JsonNode;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** A single segment of a JSON path: an object key or an array index. */
|
|
38
|
+
export type PathSegment = string | number;
|
|
39
|
+
|
|
40
|
+
const ROOT_PATH = "";
|
|
41
|
+
|
|
42
|
+
/** Render a path segment list as a human-readable string.
|
|
43
|
+
*
|
|
44
|
+
* - Root (empty list) → `""` (no label; the renderer hides the gutter)
|
|
45
|
+
* - Object key → `.foo`
|
|
46
|
+
* - Array index → `[3]`
|
|
47
|
+
* - Top-level object key → `messages` (no leading dot)
|
|
48
|
+
*
|
|
49
|
+
* Examples:
|
|
50
|
+
* [] → ""
|
|
51
|
+
* ["messages"] → "messages"
|
|
52
|
+
* ["messages", 3] → "messages[3]"
|
|
53
|
+
* ["messages", 3, "content"] → "messages[3].content"
|
|
54
|
+
* ["messages", 3, "content", 0] → "messages[3].content[0]"
|
|
55
|
+
*/
|
|
56
|
+
export function formatPath(segments: PathSegment[]): string {
|
|
57
|
+
if (segments.length === 0) return ROOT_PATH;
|
|
58
|
+
let out = "";
|
|
59
|
+
for (let i = 0; i < segments.length; i++) {
|
|
60
|
+
const seg = segments[i];
|
|
61
|
+
if (seg === undefined) continue;
|
|
62
|
+
if (typeof seg === "number") {
|
|
63
|
+
out += `[${seg}]`;
|
|
64
|
+
} else if (i === 0) {
|
|
65
|
+
out += seg;
|
|
66
|
+
} else {
|
|
67
|
+
out += `.${seg}`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
74
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Normalize a captured Request body for comparison.
|
|
79
|
+
*
|
|
80
|
+
* Captured Request bodies are sometimes already-parsed objects and sometimes
|
|
81
|
+
* JSON-encoded strings (depending on which capture path produced the log).
|
|
82
|
+
* We:
|
|
83
|
+
* 1. Try `JSON.parse` if the input is a string.
|
|
84
|
+
* 2. Deep-walk the value, building a fresh `JsonNode` tree.
|
|
85
|
+
* 3. Sort object keys lexicographically (key order is not semantically
|
|
86
|
+
* meaningful for most LLM SDKs but varies between SDK versions, so we
|
|
87
|
+
* canonicalize it).
|
|
88
|
+
* 4. Keep array order (semantically meaningful: `messages[]` order is
|
|
89
|
+
* conversation order; reordering it would silently destroy the diff).
|
|
90
|
+
* 5. Leave primitives alone (no whitespace trim — those can be meaningful
|
|
91
|
+
* in user-message text).
|
|
92
|
+
*
|
|
93
|
+
* Idempotent at the data level: re-normalizing the same shape (in any key
|
|
94
|
+
* order) yields the same tree.
|
|
95
|
+
*/
|
|
96
|
+
export function normalizeRequest(raw: unknown): JsonNode {
|
|
97
|
+
if (typeof raw === "string") {
|
|
98
|
+
try {
|
|
99
|
+
return toNode(JSON.parse(raw));
|
|
100
|
+
} catch {
|
|
101
|
+
// Unparseable string: wrap as a primitive string node so the diff can
|
|
102
|
+
// still operate on it (changed vs. another string, or added/removed
|
|
103
|
+
// vs. an object).
|
|
104
|
+
return { kind: "primitive", value: raw };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return toNode(raw);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function toNode(value: unknown): JsonNode {
|
|
111
|
+
if (value === null) return { kind: "primitive", value: null };
|
|
112
|
+
if (typeof value === "string") return { kind: "primitive", value };
|
|
113
|
+
if (typeof value === "number") return { kind: "primitive", value };
|
|
114
|
+
if (typeof value === "boolean") return { kind: "primitive", value };
|
|
115
|
+
if (Array.isArray(value)) {
|
|
116
|
+
return { kind: "array", value: value.map((v) => toNode(v)) };
|
|
117
|
+
}
|
|
118
|
+
if (isPlainObject(value)) {
|
|
119
|
+
const out: Record<string, JsonNode> = {};
|
|
120
|
+
for (const k of Object.keys(value).sort()) {
|
|
121
|
+
out[k] = toNode(value[k]);
|
|
122
|
+
}
|
|
123
|
+
return { kind: "object", value: out };
|
|
124
|
+
}
|
|
125
|
+
// Functions, symbols, bigints, undefined — treat as null in the diff model.
|
|
126
|
+
return { kind: "primitive", value: null };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Compute the path-aligned diff of two normalized trees.
|
|
131
|
+
*
|
|
132
|
+
* Emits `DiffOp[]` in path-sorted order. For each path:
|
|
133
|
+
* - if both trees have the same value, emit a single `equal` op at the
|
|
134
|
+
* deepest common ancestor (so equal subtrees collapse into one op);
|
|
135
|
+
* - if only the right has it, emit `added`;
|
|
136
|
+
* - if only the left has it, emit `removed`;
|
|
137
|
+
* - if both have it but it differs, emit `changed` (and recurse into
|
|
138
|
+
* objects/arrays so the user sees *which* field changed).
|
|
139
|
+
*
|
|
140
|
+
* Pure: does not mutate `left` or `right`.
|
|
141
|
+
*/
|
|
142
|
+
export function diffTrees(left: JsonNode, right: JsonNode): DiffOp[] {
|
|
143
|
+
const ops: DiffOp[] = [];
|
|
144
|
+
walk([], left, right, ops);
|
|
145
|
+
return ops;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function walk(segments: PathSegment[], left: JsonNode, right: JsonNode, out: DiffOp[]): void {
|
|
149
|
+
const path = formatPath(segments);
|
|
150
|
+
|
|
151
|
+
if (nodeEqual(left, right)) {
|
|
152
|
+
out.push({ kind: "equal", path, value: left });
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Type mismatch or primitive change.
|
|
157
|
+
if (left.kind !== right.kind) {
|
|
158
|
+
out.push({ kind: "changed", path, left, right });
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (left.kind === "primitive" && right.kind === "primitive") {
|
|
163
|
+
out.push({ kind: "changed", path, left, right });
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (left.kind === "object" && right.kind === "object") {
|
|
168
|
+
const leftKeys = Object.keys(left.value);
|
|
169
|
+
const rightKeys = Object.keys(right.value);
|
|
170
|
+
const rightKeySet = new Set(rightKeys);
|
|
171
|
+
|
|
172
|
+
for (const k of leftKeys) {
|
|
173
|
+
const lChild = left.value[k];
|
|
174
|
+
if (lChild === undefined) continue;
|
|
175
|
+
if (!rightKeySet.has(k)) {
|
|
176
|
+
out.push({
|
|
177
|
+
kind: "removed",
|
|
178
|
+
path: formatPath([...segments, k]),
|
|
179
|
+
value: lChild,
|
|
180
|
+
});
|
|
181
|
+
} else {
|
|
182
|
+
const rChild = right.value[k];
|
|
183
|
+
if (rChild === undefined) continue;
|
|
184
|
+
walk([...segments, k], lChild, rChild, out);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
for (const k of rightKeys) {
|
|
188
|
+
if (leftKeys.includes(k)) continue;
|
|
189
|
+
const rChild = right.value[k];
|
|
190
|
+
if (rChild === undefined) continue;
|
|
191
|
+
out.push({
|
|
192
|
+
kind: "added",
|
|
193
|
+
path: formatPath([...segments, k]),
|
|
194
|
+
value: rChild,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (left.kind === "array" && right.kind === "array") {
|
|
201
|
+
const minLen = Math.min(left.value.length, right.value.length);
|
|
202
|
+
for (let i = 0; i < minLen; i++) {
|
|
203
|
+
const lChild = left.value[i];
|
|
204
|
+
const rChild = right.value[i];
|
|
205
|
+
if (lChild === undefined || rChild === undefined) continue;
|
|
206
|
+
walk([...segments, i], lChild, rChild, out);
|
|
207
|
+
}
|
|
208
|
+
for (let i = minLen; i < right.value.length; i++) {
|
|
209
|
+
const rChild = right.value[i];
|
|
210
|
+
if (rChild === undefined) continue;
|
|
211
|
+
out.push({
|
|
212
|
+
kind: "added",
|
|
213
|
+
path: formatPath([...segments, i]),
|
|
214
|
+
value: rChild,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
for (let i = minLen; i < left.value.length; i++) {
|
|
218
|
+
const lChild = left.value[i];
|
|
219
|
+
if (lChild === undefined) continue;
|
|
220
|
+
out.push({
|
|
221
|
+
kind: "removed",
|
|
222
|
+
path: formatPath([...segments, i]),
|
|
223
|
+
value: lChild,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function nodeEqual(a: JsonNode, b: JsonNode): boolean {
|
|
230
|
+
if (a.kind !== b.kind) return false;
|
|
231
|
+
if (a.kind === "primitive" && b.kind === "primitive") {
|
|
232
|
+
return a.value === b.value;
|
|
233
|
+
}
|
|
234
|
+
if (a.kind === "array" && b.kind === "array") {
|
|
235
|
+
if (a.value.length !== b.value.length) return false;
|
|
236
|
+
for (let i = 0; i < a.value.length; i++) {
|
|
237
|
+
const ai = a.value[i];
|
|
238
|
+
const bi = b.value[i];
|
|
239
|
+
if (ai === undefined || bi === undefined) return false;
|
|
240
|
+
if (!nodeEqual(ai, bi)) return false;
|
|
241
|
+
}
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
if (a.kind === "object" && b.kind === "object") {
|
|
245
|
+
const aKeys = Object.keys(a.value);
|
|
246
|
+
const bKeys = Object.keys(b.value);
|
|
247
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
248
|
+
for (const k of aKeys) {
|
|
249
|
+
const av = a.value[k];
|
|
250
|
+
const bv = b.value[k];
|
|
251
|
+
if (av === undefined || bv === undefined) return false;
|
|
252
|
+
if (!nodeEqual(av, bv)) return false;
|
|
253
|
+
}
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Render a JsonNode to a compact human-readable preview. Used by the
|
|
260
|
+
* diff view's gutter or change rows where we want a one-line summary
|
|
261
|
+
* without pretty-printing the whole subtree. */
|
|
262
|
+
export function previewNode(node: JsonNode, maxLen = 80): string {
|
|
263
|
+
let s: string;
|
|
264
|
+
switch (node.kind) {
|
|
265
|
+
case "primitive":
|
|
266
|
+
s = node.value === null ? "null" : JSON.stringify(node.value);
|
|
267
|
+
break;
|
|
268
|
+
case "array":
|
|
269
|
+
s = `[… ${node.value.length} items]`;
|
|
270
|
+
break;
|
|
271
|
+
case "object":
|
|
272
|
+
s = `{… ${Object.keys(node.value).length} keys}`;
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
if (s.length > maxLen) s = `${s.slice(0, maxLen - 1)}…`;
|
|
276
|
+
return s;
|
|
277
|
+
}
|