@ai-sdk/gateway 4.0.151 → 4.0.153

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.
@@ -0,0 +1,88 @@
1
+ import { withTelemetryRedaction } from '@ai-sdk/provider-utils';
2
+
3
+ const REDACTED = '[redacted]';
4
+
5
+ /**
6
+ * Gateway request bodies can carry request-scoped BYOK credentials in
7
+ * `providerOptions.gateway.byok`. The credentials must reach the Gateway and
8
+ * stay on application-facing errors, but telemetry-only copies must not expose
9
+ * them.
10
+ */
11
+ export function withGatewayRequestTelemetryRedaction<T>(value: T): T {
12
+ if (!isRecord(value)) return value;
13
+
14
+ const body = value as Record<string, unknown>;
15
+ if (!hasByok(body.providerOptions) && !hasRequestByok(body.requests)) {
16
+ return value;
17
+ }
18
+
19
+ return withTelemetryRedaction(body, redacted => ({
20
+ ...redacted,
21
+ ...(hasByok(redacted.providerOptions) && {
22
+ providerOptions: redactProviderOptions(redacted.providerOptions),
23
+ }),
24
+ ...(Array.isArray(redacted.requests) && {
25
+ requests: redacted.requests.map(redactRequestProviderOptions),
26
+ }),
27
+ })) as T;
28
+ }
29
+
30
+ /** Batch bodies nest the same provider options in every request's options. */
31
+ function redactRequestProviderOptions(request: unknown): unknown {
32
+ if (!isRecord(request) || !isRecord(request.options)) return request;
33
+
34
+ const options = request.options;
35
+ if (!hasByok(options.providerOptions)) return request;
36
+
37
+ return {
38
+ ...request,
39
+ options: {
40
+ ...options,
41
+ providerOptions: redactProviderOptions(options.providerOptions),
42
+ },
43
+ };
44
+ }
45
+
46
+ function redactProviderOptions(value: unknown): unknown {
47
+ if (!isRecord(value) || !isRecord(value.gateway)) return value;
48
+
49
+ return {
50
+ ...value,
51
+ gateway: { ...value.gateway, byok: redactByok(value.gateway.byok) },
52
+ };
53
+ }
54
+
55
+ /** Keep provider names and credential shapes; replace every BYOK value. */
56
+ function redactByok(value: unknown): unknown {
57
+ if (Array.isArray(value)) return value.map(redactByok);
58
+
59
+ if (isRecord(value)) {
60
+ return Object.fromEntries(
61
+ Object.entries(value).map(([key, entry]) => [key, redactByok(entry)]),
62
+ );
63
+ }
64
+
65
+ return REDACTED;
66
+ }
67
+
68
+ function hasByok(providerOptions: unknown): boolean {
69
+ return (
70
+ isRecord(providerOptions) &&
71
+ isRecord(providerOptions.gateway) &&
72
+ providerOptions.gateway.byok !== undefined
73
+ );
74
+ }
75
+
76
+ function hasRequestByok(requests: unknown): boolean {
77
+ return (
78
+ Array.isArray(requests) &&
79
+ requests.some(request => {
80
+ if (!isRecord(request) || !isRecord(request.options)) return false;
81
+ return hasByok(request.options.providerOptions);
82
+ })
83
+ );
84
+ }
85
+
86
+ function isRecord(value: unknown): value is Record<string, unknown> {
87
+ return value != null && typeof value === 'object' && !Array.isArray(value);
88
+ }