@langwatch/server 3.1.0-beta.19 → 3.1.0-beta.20

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 (75) hide show
  1. package/langwatch/prisma/migrations/20260425000000_consolidate_gateway_audit_into_audit_log/migration.sql +44 -0
  2. package/langwatch/prisma/migrations/20260426150000_drop_redundant_audit_log_org_index/migration.sql +25 -0
  3. package/langwatch/prisma/schema.prisma +34 -41
  4. package/langwatch/scripts/seed-gateway-dogfood.ts +26 -26
  5. package/langwatch/src/app/api/gateway-platform/[[...route]]/app.ts +8 -4
  6. package/langwatch/src/app/api/middleware/trace-limit.ts +10 -0
  7. package/langwatch/src/components/MainMenu.tsx +4 -15
  8. package/langwatch/src/components/SettingsLayout.tsx +1 -1
  9. package/langwatch/src/components/llmPromptConfigs/__tests__/LLMConfigPopover.test.tsx +45 -0
  10. package/langwatch/src/components/llmPromptConfigs/parameterRegistry.ts +22 -13
  11. package/langwatch/src/components/prompts/PromptEditorDrawer.tsx +5 -3
  12. package/langwatch/src/components/settings/AddCustomModelDialog.tsx +7 -6
  13. package/langwatch/src/evaluations-v3/__tests__/TableDisplay.integration.test.tsx +90 -5
  14. package/langwatch/src/evaluations-v3/__tests__/TableSettingsMenu.integration.test.tsx +26 -26
  15. package/langwatch/src/evaluations-v3/components/DatasetSection/TableCell.tsx +2 -10
  16. package/langwatch/src/evaluations-v3/components/EvaluationsV3Table.tsx +10 -4
  17. package/langwatch/src/evaluations-v3/components/TableSettingsMenu.tsx +6 -6
  18. package/langwatch/src/evaluations-v3/components/TargetSection/EvaluatorChip.tsx +19 -11
  19. package/langwatch/src/evaluations-v3/components/TargetSection/TargetCell.tsx +55 -42
  20. package/langwatch/src/evaluations-v3/components/TargetSection/TargetHeader.tsx +2 -1
  21. package/langwatch/src/evaluations-v3/components/TargetSection/__tests__/EvaluatorChip.test.tsx +57 -0
  22. package/langwatch/src/evaluations-v3/components/TargetSection/__tests__/TargetCell.test.tsx +199 -11
  23. package/langwatch/src/evaluations-v3/components/VirtualizedTableBody.tsx +1 -1
  24. package/langwatch/src/evaluations-v3/constants.ts +9 -0
  25. package/langwatch/src/evaluations-v3/types/__tests__/persistence.test.ts +1 -1
  26. package/langwatch/src/evaluations-v3/types.ts +13 -1
  27. package/langwatch/src/evaluations-v3/utils/__tests__/evaluatorEditorCallbacks.test.ts +32 -0
  28. package/langwatch/src/evaluations-v3/utils/evaluatorEditorCallbacks.ts +36 -5
  29. package/langwatch/src/optimization_studio/components/datasets/DatasetSelection.tsx +1 -6
  30. package/langwatch/src/optimization_studio/components/datasets/__tests__/DatasetSelection.integration.test.tsx +194 -0
  31. package/langwatch/src/optimization_studio/components/drawers/SignaturePromptEditorBridge.tsx +6 -6
  32. package/langwatch/src/optimization_studio/components/drawers/__tests__/SignaturePromptEditorBridge.integration.test.tsx +36 -11
  33. package/langwatch/src/pages/[project]/gateway/budgets/[id].tsx +1 -1
  34. package/langwatch/src/pages/[project]/gateway/virtual-keys/[id].tsx +1 -1
  35. package/langwatch/src/pages/settings/audit-log.tsx +82 -59
  36. package/langwatch/src/routes.tsx +0 -4
  37. package/langwatch/src/server/agents/agent.service.ts +6 -2
  38. package/langwatch/src/server/api/__tests__/rbac-integration.test.ts +53 -0
  39. package/langwatch/src/server/api/__tests__/rbac.auditLog.test.ts +100 -0
  40. package/langwatch/src/server/api/rbac.ts +12 -0
  41. package/langwatch/src/server/api/root.ts +0 -2
  42. package/langwatch/src/server/api/routers/organization.ts +1 -1
  43. package/langwatch/src/server/app-layer/organizations/repositories/organization.prisma.repository.ts +58 -120
  44. package/langwatch/src/server/app-layer/organizations/repositories/organization.repository.ts +8 -11
  45. package/langwatch/src/server/evaluators/evaluator.service.ts +6 -2
  46. package/langwatch/src/server/gateway/__tests__/auditLog.consolidation.integration.test.ts +237 -0
  47. package/langwatch/src/server/gateway/__tests__/cacheRule.service.unit.test.ts +2 -2
  48. package/langwatch/src/server/gateway/__tests__/providerCredential.service.unit.test.ts +6 -6
  49. package/langwatch/src/server/gateway/__tests__/virtualKey.service.unit.test.ts +6 -6
  50. package/langwatch/src/server/gateway/auditLog.repository.ts +46 -8
  51. package/langwatch/src/server/gateway/auditSerializer.ts +2 -2
  52. package/langwatch/src/server/gateway/budget.service.ts +6 -6
  53. package/langwatch/src/server/gateway/cacheRule.service.ts +5 -5
  54. package/langwatch/src/server/gateway/providerCredential.service.ts +5 -5
  55. package/langwatch/src/server/gateway/virtualKey.service.ts +9 -9
  56. package/langwatch/src/server/pat/__tests__/auth-middleware.unit.test.ts +138 -3
  57. package/langwatch/src/server/pat/auth-middleware.ts +68 -13
  58. package/langwatch/src/server/routes/__tests__/otel.unit.test.ts +241 -0
  59. package/langwatch/src/server/routes/otel.ts +281 -45
  60. package/langwatch/src/server/scenarios/adapters/__tests__/http-agent.adapter.test.ts +271 -6
  61. package/langwatch/src/server/scenarios/adapters/http-agent.adapter.ts +43 -35
  62. package/langwatch/src/server/scenarios/execution/http-template-engine.ts +124 -0
  63. package/langwatch/src/server/scenarios/execution/serialized-adapters/__tests__/http-agent.adapter.unit.test.ts +180 -0
  64. package/langwatch/src/server/scenarios/execution/serialized-adapters/http-agent.adapter.ts +26 -30
  65. package/langwatch/src/tasks/__tests__/migrateCustomModels.unit.test.ts +0 -2
  66. package/langwatch/src/tasks/migrateCustomModels.ts +1 -1
  67. package/langwatch/src/utils/__tests__/dbMultiTenancyProtection.unit.test.ts +0 -17
  68. package/langwatch/src/utils/permissionsConfig.ts +6 -0
  69. package/langwatch/src/utils/routes.ts +0 -5
  70. package/package.json +1 -1
  71. package/packages/server/dist/cli.cjs +39 -70
  72. package/langwatch/src/pages/[project]/gateway/audit.tsx +0 -606
  73. package/langwatch/src/server/api/routers/gatewayAudit.ts +0 -109
  74. package/langwatch/src/server/gateway/__tests__/audit.service.unit.test.ts +0 -156
  75. package/langwatch/src/server/gateway/audit.service.ts +0 -104
@@ -1,606 +0,0 @@
1
- import {
2
- Badge,
3
- Box,
4
- Button,
5
- Card,
6
- Code,
7
- EmptyState,
8
- HStack,
9
- IconButton,
10
- NativeSelect,
11
- Spacer,
12
- Spinner,
13
- Table,
14
- Text,
15
- VStack,
16
- } from "@chakra-ui/react";
17
- import { ArrowLeft, ChevronDown, ChevronRight, FileClock } from "lucide-react";
18
- import { useState } from "react";
19
-
20
- import { DashboardLayout } from "~/components/DashboardLayout";
21
- import { withPermissionGuard } from "~/components/WithPermissionGuard";
22
- import { GatewayErrorPanel } from "~/components/gateway/GatewayErrorPanel";
23
- import { GatewayLayout } from "~/components/gateway/GatewayLayout";
24
- import { Link } from "~/components/ui/link";
25
- import { PageLayout } from "~/components/ui/layouts/PageLayout";
26
- import { Tooltip } from "~/components/ui/tooltip";
27
- import { useOrganizationTeamProject } from "~/hooks/useOrganizationTeamProject";
28
- import { api } from "~/utils/api";
29
- import { useRouter } from "~/utils/compat/next-router";
30
- import { formatTimeAgo } from "~/utils/formatTimeAgo";
31
-
32
- type AuditAction =
33
- | "VIRTUAL_KEY_CREATED"
34
- | "VIRTUAL_KEY_UPDATED"
35
- | "VIRTUAL_KEY_ROTATED"
36
- | "VIRTUAL_KEY_REVOKED"
37
- | "VIRTUAL_KEY_DELETED"
38
- | "BUDGET_CREATED"
39
- | "BUDGET_UPDATED"
40
- | "BUDGET_DELETED"
41
- | "PROVIDER_BINDING_CREATED"
42
- | "PROVIDER_BINDING_UPDATED"
43
- | "PROVIDER_BINDING_DELETED"
44
- | "CACHE_RULE_CREATED"
45
- | "CACHE_RULE_UPDATED"
46
- | "CACHE_RULE_DELETED";
47
-
48
- type TargetKind = "virtual_key" | "budget" | "provider_binding" | "cache_rule";
49
-
50
- const ACTION_OPTIONS: Array<{ label: string; value: AuditAction | "" }> = [
51
- { label: "All actions", value: "" },
52
- { label: "VK created", value: "VIRTUAL_KEY_CREATED" },
53
- { label: "VK updated", value: "VIRTUAL_KEY_UPDATED" },
54
- { label: "VK rotated", value: "VIRTUAL_KEY_ROTATED" },
55
- { label: "VK revoked", value: "VIRTUAL_KEY_REVOKED" },
56
- { label: "VK deleted", value: "VIRTUAL_KEY_DELETED" },
57
- { label: "Budget created", value: "BUDGET_CREATED" },
58
- { label: "Budget updated", value: "BUDGET_UPDATED" },
59
- { label: "Budget deleted", value: "BUDGET_DELETED" },
60
- { label: "Provider binding created", value: "PROVIDER_BINDING_CREATED" },
61
- { label: "Provider binding updated", value: "PROVIDER_BINDING_UPDATED" },
62
- { label: "Provider binding deleted", value: "PROVIDER_BINDING_DELETED" },
63
- { label: "Cache rule created", value: "CACHE_RULE_CREATED" },
64
- { label: "Cache rule updated", value: "CACHE_RULE_UPDATED" },
65
- { label: "Cache rule deleted", value: "CACHE_RULE_DELETED" },
66
- ];
67
-
68
- const TARGET_OPTIONS: Array<{ label: string; value: TargetKind | "" }> = [
69
- { label: "All targets", value: "" },
70
- { label: "Virtual key", value: "virtual_key" },
71
- { label: "Budget", value: "budget" },
72
- { label: "Provider binding", value: "provider_binding" },
73
- { label: "Cache rule", value: "cache_rule" },
74
- ];
75
-
76
- function AuditLogPage() {
77
- const { organization, project } = useOrganizationTeamProject();
78
- const router = useRouter();
79
- // URL-seeded filter for deep-links from detail pages, e.g.
80
- // /gateway/audit?targetKind=virtual_key&targetId=vk_xxx. Users
81
- // clear it via the "Clear filter" button inline with the header.
82
- const urlTargetKind =
83
- typeof router.query.targetKind === "string"
84
- ? (router.query.targetKind as TargetKind)
85
- : "";
86
- const urlTargetId =
87
- typeof router.query.targetId === "string" ? router.query.targetId : "";
88
- const [action, setAction] = useState<AuditAction | "">("");
89
- const [targetKind, setTargetKind] = useState<TargetKind | "">(urlTargetKind);
90
- const [targetId, setTargetId] = useState(urlTargetId);
91
-
92
- const listQuery = api.gatewayAudit.list.useInfiniteQuery(
93
- {
94
- organizationId: organization?.id ?? "",
95
- action: action || undefined,
96
- targetKind: targetKind || undefined,
97
- targetId: targetId || undefined,
98
- limit: 50,
99
- },
100
- {
101
- enabled: !!organization?.id,
102
- getNextPageParam: (last) => last.nextCursor,
103
- },
104
- );
105
-
106
- const clearTarget = () => {
107
- setTargetKind("");
108
- setTargetId("");
109
- const { targetKind: _tk, targetId: _tid, ...rest } = router.query;
110
- void router.replace({ pathname: router.pathname, query: rest });
111
- };
112
-
113
- const entries = (listQuery.data?.pages ?? []).flatMap((p) => p.entries);
114
-
115
- // Render a back-link to the originating resource when the audit log
116
- // was opened from a detail page (VK, budget) via ?targetKind&targetId.
117
- // Lets operators return to the entity they were investigating instead
118
- // of browser-back or clearing the filter chip and losing context.
119
- const backLink = (() => {
120
- if (!urlTargetKind || !urlTargetId || !project?.slug) return null;
121
- if (urlTargetKind === "virtual_key") {
122
- return {
123
- href: `/${project.slug}/gateway/virtual-keys/${urlTargetId}`,
124
- label: "Virtual key",
125
- };
126
- }
127
- if (urlTargetKind === "budget") {
128
- return {
129
- href: `/${project.slug}/gateway/budgets/${urlTargetId}`,
130
- label: "Budget",
131
- };
132
- }
133
- return null;
134
- })();
135
-
136
- return (
137
- <GatewayLayout>
138
- <>
139
- <PageLayout.Header>
140
- {backLink && (
141
- <HStack>
142
- <Link href={backLink.href} color="fg.muted" fontSize="sm">
143
- <HStack gap={1}>
144
- <ArrowLeft size={14} /> {backLink.label}
145
- </HStack>
146
- </Link>
147
- </HStack>
148
- )}
149
- <PageLayout.Heading>Audit log</PageLayout.Heading>
150
- <Spacer />
151
- </PageLayout.Header>
152
-
153
- <Box padding={6} width="full" maxWidth="1600px" marginX="auto">
154
- <HStack gap={3} mb={4}>
155
- <Box>
156
- <NativeSelect.Root size="sm">
157
- <NativeSelect.Field
158
- value={action}
159
- onChange={(e) =>
160
- setAction(e.target.value as AuditAction | "")
161
- }
162
- >
163
- {ACTION_OPTIONS.map((opt) => (
164
- <option key={opt.value || "all"} value={opt.value}>
165
- {opt.label}
166
- </option>
167
- ))}
168
- </NativeSelect.Field>
169
- </NativeSelect.Root>
170
- </Box>
171
- <Box>
172
- <NativeSelect.Root size="sm">
173
- <NativeSelect.Field
174
- value={targetKind}
175
- onChange={(e) => {
176
- setTargetKind(e.target.value as TargetKind | "");
177
- if (!e.target.value) setTargetId("");
178
- }}
179
- >
180
- {TARGET_OPTIONS.map((opt) => (
181
- <option key={opt.value || "all"} value={opt.value}>
182
- {opt.label}
183
- </option>
184
- ))}
185
- </NativeSelect.Field>
186
- </NativeSelect.Root>
187
- </Box>
188
- {targetId && (
189
- <Badge
190
- colorPalette="orange"
191
- variant="surface"
192
- gap={1}
193
- cursor="pointer"
194
- onClick={clearTarget}
195
- title="Clear target filter"
196
- >
197
- target = {targetId.slice(0, 24)}… ×
198
- </Badge>
199
- )}
200
- </HStack>
201
-
202
- {listQuery.isLoading ? (
203
- <Spinner />
204
- ) : listQuery.isError ? (
205
- <GatewayErrorPanel
206
- title="Failed to load audit log"
207
- error={listQuery.error}
208
- onRetry={() => listQuery.refetch()}
209
- />
210
- ) : entries.length === 0 ? (
211
- <EmptyState.Root>
212
- <EmptyState.Content>
213
- <EmptyState.Indicator>
214
- <FileClock size={32} />
215
- </EmptyState.Indicator>
216
- <EmptyState.Title>No matching audit entries</EmptyState.Title>
217
- <EmptyState.Description>
218
- Audit entries are written in the same transaction as every
219
- gateway mutation — VK create/update/rotate/revoke, budget
220
- CRUD, provider binding changes. Once anyone touches those
221
- resources they show up here.
222
- </EmptyState.Description>
223
- </EmptyState.Content>
224
- </EmptyState.Root>
225
- ) : (
226
- <VStack align="stretch" gap={3}>
227
- <Card.Root width="full" overflow="hidden">
228
- <Card.Body paddingY={0} paddingX={0}>
229
- <Table.Root variant="line" size="md" width="full">
230
- <Table.Header>
231
- <Table.Row>
232
- <Table.ColumnHeader width="32px"></Table.ColumnHeader>
233
- <Table.ColumnHeader>When</Table.ColumnHeader>
234
- <Table.ColumnHeader>Actor</Table.ColumnHeader>
235
- <Table.ColumnHeader>Action</Table.ColumnHeader>
236
- <Table.ColumnHeader>Target</Table.ColumnHeader>
237
- </Table.Row>
238
- </Table.Header>
239
- <Table.Body>
240
- {entries.map((entry) => (
241
- <AuditRow
242
- key={entry.id}
243
- entry={entry}
244
- projectSlug={project?.slug ?? ""}
245
- />
246
- ))}
247
- </Table.Body>
248
- </Table.Root>
249
- </Card.Body>
250
- </Card.Root>
251
- {listQuery.hasNextPage && (
252
- <HStack justify="center">
253
- <Button
254
- variant="outline"
255
- size="sm"
256
- onClick={() => listQuery.fetchNextPage()}
257
- loading={listQuery.isFetchingNextPage}
258
- >
259
- Load 50 more
260
- </Button>
261
- </HStack>
262
- )}
263
- </VStack>
264
- )}
265
- </Box>
266
- </>
267
- </GatewayLayout>
268
- );
269
- }
270
-
271
- type AuditEntry = {
272
- id: string;
273
- actorUserId: string | null;
274
- actorName: string | null;
275
- actorEmail: string | null;
276
- action: AuditAction;
277
- targetKind: string;
278
- targetId: string;
279
- before: unknown;
280
- after: unknown;
281
- createdAt: string;
282
- };
283
-
284
- function AuditRow({
285
- entry,
286
- projectSlug,
287
- }: {
288
- entry: AuditEntry;
289
- projectSlug: string;
290
- }) {
291
- const [expanded, setExpanded] = useState(false);
292
- const targetHref = resolveTargetHref(
293
- entry.targetKind,
294
- entry.targetId,
295
- projectSlug,
296
- );
297
- return (
298
- <>
299
- <Table.Row>
300
- <Table.Cell>
301
- <IconButton
302
- aria-label={expanded ? "Collapse" : "Expand"}
303
- size="xs"
304
- variant="ghost"
305
- onClick={() => setExpanded((s) => !s)}
306
- >
307
- {expanded ? <ChevronDown size={14} /> : <ChevronRight size={14} />}
308
- </IconButton>
309
- </Table.Cell>
310
- <Table.Cell>
311
- <Tooltip content={new Date(entry.createdAt).toLocaleString()}>
312
- <Text fontSize="xs" color="fg.muted">
313
- {formatTimeAgo(new Date(entry.createdAt).getTime())}
314
- </Text>
315
- </Tooltip>
316
- </Table.Cell>
317
- <Table.Cell>
318
- {entry.actorName || entry.actorEmail ? (
319
- <VStack align="start" gap={0}>
320
- {entry.actorName && (
321
- <Text fontSize="sm">{entry.actorName}</Text>
322
- )}
323
- {entry.actorEmail && (
324
- <Text fontSize="xs" color="fg.muted">
325
- {entry.actorEmail}
326
- </Text>
327
- )}
328
- </VStack>
329
- ) : (
330
- <Text fontSize="xs" color="fg.muted">
331
- system / api token
332
- </Text>
333
- )}
334
- </Table.Cell>
335
- <Table.Cell>
336
- <ActionBadge action={entry.action} />
337
- </Table.Cell>
338
- <Table.Cell>
339
- <HStack gap={2}>
340
- <Badge colorPalette="gray" fontSize="2xs">
341
- {entry.targetKind.replace("_", " ")}
342
- </Badge>
343
- {targetHref ? (
344
- <Link href={targetHref}>
345
- <Code fontSize="xs">{entry.targetId}</Code>
346
- </Link>
347
- ) : (
348
- <Code fontSize="xs">{entry.targetId}</Code>
349
- )}
350
- </HStack>
351
- </Table.Cell>
352
- </Table.Row>
353
- {expanded && (
354
- <Table.Row>
355
- <Table.Cell colSpan={5} background="bg.subtle">
356
- <VStack align="stretch" gap={3} padding={2}>
357
- <DiffSummary before={entry.before} after={entry.after} />
358
- <HStack align="stretch" gap={4}>
359
- <JsonPanel label="Before" value={entry.before} />
360
- <JsonPanel label="After" value={entry.after} />
361
- </HStack>
362
- </VStack>
363
- </Table.Cell>
364
- </Table.Row>
365
- )}
366
- </>
367
- );
368
- }
369
-
370
- function DiffSummary({ before, after }: { before: unknown; after: unknown }) {
371
- const kind = diffKind(before, after);
372
- if (kind === "unsupported") return null;
373
-
374
- if (kind === "create") {
375
- const fields = flattenObject(after);
376
- if (fields.length === 0) return null;
377
- return (
378
- <VStack align="stretch" gap={1}>
379
- <Text fontSize="xs" fontWeight="semibold" color="fg.muted">
380
- Created with {fields.length} field{fields.length === 1 ? "" : "s"}
381
- </Text>
382
- <VStack align="stretch" gap={0.5}>
383
- {fields.map(({ key, value }) => (
384
- <HStack key={key} gap={2} fontSize="xs" align="start">
385
- <Code fontSize="2xs" minWidth="140px" flexShrink={0}>
386
- {key}
387
- </Code>
388
- <Code
389
- fontSize="2xs"
390
- colorPalette="green"
391
- variant="surface"
392
- maxWidth="80%"
393
- overflow="hidden"
394
- textOverflow="ellipsis"
395
- whiteSpace="nowrap"
396
- >
397
- {formatValue(value)}
398
- </Code>
399
- </HStack>
400
- ))}
401
- </VStack>
402
- </VStack>
403
- );
404
- }
405
-
406
- if (kind === "delete") {
407
- const fields = flattenObject(before);
408
- if (fields.length === 0) return null;
409
- return (
410
- <Text fontSize="xs" color="fg.muted">
411
- Deleted ({fields.length} field{fields.length === 1 ? "" : "s"} removed —
412
- see Before pane below).
413
- </Text>
414
- );
415
- }
416
-
417
- // update: both sides objects.
418
- const changes = computeShallowDiff(before, after) ?? [];
419
- if (changes.length === 0) {
420
- return (
421
- <Text fontSize="xs" color="fg.muted">
422
- No field changes at the top level (nested fields may still differ — see
423
- Before/After below).
424
- </Text>
425
- );
426
- }
427
- return (
428
- <VStack align="stretch" gap={1}>
429
- <Text fontSize="xs" fontWeight="semibold" color="fg.muted">
430
- Changed fields ({changes.length})
431
- </Text>
432
- <VStack align="stretch" gap={0.5}>
433
- {changes.map(({ key, before: b, after: a }) => (
434
- <HStack key={key} gap={2} fontSize="xs" align="start">
435
- <Code fontSize="2xs" minWidth="140px" flexShrink={0}>
436
- {key}
437
- </Code>
438
- <Code
439
- fontSize="2xs"
440
- colorPalette="red"
441
- variant="surface"
442
- maxWidth="40%"
443
- overflow="hidden"
444
- textOverflow="ellipsis"
445
- whiteSpace="nowrap"
446
- >
447
- {formatValue(b)}
448
- </Code>
449
- <Text color="fg.muted">→</Text>
450
- <Code
451
- fontSize="2xs"
452
- colorPalette="green"
453
- variant="surface"
454
- maxWidth="40%"
455
- overflow="hidden"
456
- textOverflow="ellipsis"
457
- whiteSpace="nowrap"
458
- >
459
- {formatValue(a)}
460
- </Code>
461
- </HStack>
462
- ))}
463
- </VStack>
464
- </VStack>
465
- );
466
- }
467
-
468
- function diffKind(
469
- before: unknown,
470
- after: unknown,
471
- ): "create" | "delete" | "update" | "unsupported" {
472
- const beforeEmpty = before === null || before === undefined;
473
- const afterEmpty = after === null || after === undefined;
474
- if (beforeEmpty && afterEmpty) return "unsupported";
475
- if (beforeEmpty && isPlainObject(after)) return "create";
476
- if (afterEmpty && isPlainObject(before)) return "delete";
477
- if (isPlainObject(before) && isPlainObject(after)) return "update";
478
- return "unsupported";
479
- }
480
-
481
- function isPlainObject(v: unknown): v is Record<string, unknown> {
482
- return typeof v === "object" && v !== null && !Array.isArray(v);
483
- }
484
-
485
- function flattenObject(
486
- v: unknown,
487
- ): Array<{ key: string; value: unknown }> {
488
- if (!isPlainObject(v)) return [];
489
- return Object.entries(v).map(([key, value]) => ({ key, value }));
490
- }
491
-
492
- function computeShallowDiff(
493
- before: unknown,
494
- after: unknown,
495
- ): Array<{ key: string; before: unknown; after: unknown }> | null {
496
- if (
497
- before === null ||
498
- before === undefined ||
499
- after === null ||
500
- after === undefined
501
- ) {
502
- return null;
503
- }
504
- if (
505
- typeof before !== "object" ||
506
- typeof after !== "object" ||
507
- Array.isArray(before) ||
508
- Array.isArray(after)
509
- ) {
510
- return null;
511
- }
512
- const b = before as Record<string, unknown>;
513
- const a = after as Record<string, unknown>;
514
- const keys = Array.from(new Set([...Object.keys(b), ...Object.keys(a)]));
515
- const changes: Array<{ key: string; before: unknown; after: unknown }> = [];
516
- for (const key of keys) {
517
- const bv = b[key];
518
- const av = a[key];
519
- // Cheap stringify compare — not ideal for deep object equality, but
520
- // matches the 'did this top-level field change' question we care about.
521
- if (JSON.stringify(bv) !== JSON.stringify(av)) {
522
- changes.push({ key, before: bv, after: av });
523
- }
524
- }
525
- return changes;
526
- }
527
-
528
- function formatValue(v: unknown): string {
529
- if (v === null) return "null";
530
- if (v === undefined) return "—";
531
- if (typeof v === "string") return `"${v}"`;
532
- if (typeof v === "number" || typeof v === "boolean") return String(v);
533
- return JSON.stringify(v);
534
- }
535
-
536
- function JsonPanel({ label, value }: { label: string; value: unknown }) {
537
- const text =
538
- value === null || value === undefined
539
- ? "—"
540
- : JSON.stringify(value, null, 2);
541
- return (
542
- <VStack
543
- flex={1}
544
- align="stretch"
545
- gap={1}
546
- borderWidth="1px"
547
- borderColor="border.subtle"
548
- borderRadius="md"
549
- padding={2}
550
- background="white"
551
- >
552
- <Text fontSize="xs" fontWeight="semibold" color="fg.muted">
553
- {label}
554
- </Text>
555
- <Box
556
- as="pre"
557
- fontSize="xs"
558
- fontFamily="mono"
559
- overflow="auto"
560
- maxHeight="320px"
561
- whiteSpace="pre-wrap"
562
- >
563
- {text}
564
- </Box>
565
- </VStack>
566
- );
567
- }
568
-
569
- function ActionBadge({ action }: { action: AuditAction }) {
570
- const palette =
571
- action.endsWith("DELETED") || action.endsWith("REVOKED")
572
- ? "red"
573
- : action.endsWith("CREATED") || action.endsWith("ROTATED")
574
- ? "green"
575
- : "blue";
576
- const label = action.toLowerCase().replace(/_/g, " ");
577
- return (
578
- <Badge colorPalette={palette} fontSize="2xs">
579
- {label}
580
- </Badge>
581
- );
582
- }
583
-
584
- function resolveTargetHref(
585
- kind: string,
586
- id: string,
587
- projectSlug: string,
588
- ): string | null {
589
- if (!projectSlug) return null;
590
- switch (kind) {
591
- case "virtual_key":
592
- return `/${projectSlug}/gateway/virtual-keys/${id}`;
593
- case "budget":
594
- return `/${projectSlug}/gateway/budgets/${id}`;
595
- case "provider_binding":
596
- return `/${projectSlug}/gateway/providers`;
597
- case "cache_rule":
598
- return `/${projectSlug}/gateway/cache-rules`;
599
- default:
600
- return null;
601
- }
602
- }
603
-
604
- export default withPermissionGuard("gatewayLogs:view", {
605
- layoutComponent: DashboardLayout,
606
- })(AuditLogPage);