@ai-sdk/gateway 4.0.152 → 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.
- package/CHANGELOG.md +6 -0
- package/dist/index.js +86 -26
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/gateway-batch.ts +3 -2
- package/src/gateway-decision-model.ts +3 -2
- package/src/gateway-embedding-model.ts +3 -2
- package/src/gateway-extraction-model.ts +3 -2
- package/src/gateway-image-model.ts +3 -2
- package/src/gateway-language-model.ts +4 -1
- package/src/gateway-reranking-model.ts +3 -2
- package/src/gateway-speech-model.ts +22 -20
- package/src/gateway-transcription-model.ts +14 -12
- package/src/gateway-video-model.ts +6 -3
- package/src/redact-gateway-request-credentials.ts +88 -0
package/package.json
CHANGED
package/src/gateway-batch.ts
CHANGED
|
@@ -31,6 +31,7 @@ import type { GatewayChatConfig } from './gateway-language-model';
|
|
|
31
31
|
import type { GatewayModelId } from './gateway-language-model-settings';
|
|
32
32
|
import { asGatewayError } from './errors';
|
|
33
33
|
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
34
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
34
35
|
|
|
35
36
|
export class GatewayBatch implements BatchV4<{ text: GatewayModelId }> {
|
|
36
37
|
readonly specificationVersion = 'v4' as const;
|
|
@@ -81,7 +82,7 @@ export class GatewayBatch implements BatchV4<{ text: GatewayModelId }> {
|
|
|
81
82
|
? { 'idempotency-key': idempotencyKey }
|
|
82
83
|
: undefined,
|
|
83
84
|
),
|
|
84
|
-
body: {
|
|
85
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
85
86
|
...(webhookUrl != null && { callbackUrl: webhookUrl }),
|
|
86
87
|
requests: requests.map(request => ({
|
|
87
88
|
id: request.id,
|
|
@@ -92,7 +93,7 @@ export class GatewayBatch implements BatchV4<{ text: GatewayModelId }> {
|
|
|
92
93
|
...(forwardedProviderOptions != null && {
|
|
93
94
|
providerOptions: forwardedProviderOptions,
|
|
94
95
|
}),
|
|
95
|
-
},
|
|
96
|
+
}),
|
|
96
97
|
successfulResponseHandler: createJsonResponseHandler(
|
|
97
98
|
gatewayBatchStartResponseSchema,
|
|
98
99
|
),
|
|
@@ -20,6 +20,7 @@ import { parseAuthMethod } from './errors/parse-auth-method';
|
|
|
20
20
|
import type { GatewayConfig } from './gateway-config';
|
|
21
21
|
import { gatewayDecisionProviderOptionsSchema } from './gateway-provider-options';
|
|
22
22
|
import { gatewayWarningSchema } from './gateway-warning-schema';
|
|
23
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
23
24
|
import { toGatewayStateParts } from './to-gateway-state-parts';
|
|
24
25
|
|
|
25
26
|
export class GatewayDecisionModel implements DecisionModelV4 {
|
|
@@ -81,7 +82,7 @@ export class GatewayDecisionModel implements DecisionModelV4 {
|
|
|
81
82
|
this.getModelConfigHeaders(),
|
|
82
83
|
await resolve(this.config.o11yHeaders),
|
|
83
84
|
),
|
|
84
|
-
body: {
|
|
85
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
85
86
|
stateParts,
|
|
86
87
|
questions,
|
|
87
88
|
maxOutputTokens,
|
|
@@ -89,7 +90,7 @@ export class GatewayDecisionModel implements DecisionModelV4 {
|
|
|
89
90
|
...(validatedProviderOptions
|
|
90
91
|
? { providerOptions: validatedProviderOptions }
|
|
91
92
|
: {}),
|
|
92
|
-
},
|
|
93
|
+
}),
|
|
93
94
|
successfulResponseHandler: createJsonResponseHandler(
|
|
94
95
|
gatewayDecisionResponseSchema,
|
|
95
96
|
),
|
|
@@ -20,6 +20,7 @@ import { z } from './zod';
|
|
|
20
20
|
import { asGatewayError } from './errors';
|
|
21
21
|
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
22
22
|
import type { GatewayConfig } from './gateway-config';
|
|
23
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
23
24
|
|
|
24
25
|
type GatewayEmbeddingConfig = GatewayConfig & {
|
|
25
26
|
provider: string;
|
|
@@ -79,11 +80,11 @@ export class GatewayEmbeddingModel implements EmbeddingModelV4 {
|
|
|
79
80
|
this.getModelConfigHeaders(),
|
|
80
81
|
await resolve(this.config.o11yHeaders),
|
|
81
82
|
),
|
|
82
|
-
body: {
|
|
83
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
83
84
|
values,
|
|
84
85
|
dimensions,
|
|
85
86
|
...(providerOptions ? { providerOptions } : {}),
|
|
86
|
-
},
|
|
87
|
+
}),
|
|
87
88
|
successfulResponseHandler: createJsonResponseHandler(
|
|
88
89
|
gatewayEmbeddingResponseSchema,
|
|
89
90
|
),
|
|
@@ -23,6 +23,7 @@ import { extractApiCallResponse } from './errors/extract-api-call-response';
|
|
|
23
23
|
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
24
24
|
import type { GatewayConfig } from './gateway-config';
|
|
25
25
|
import { gatewayWarningSchema } from './gateway-warning-schema';
|
|
26
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
26
27
|
import { toGatewayStateParts } from './to-gateway-state-parts';
|
|
27
28
|
|
|
28
29
|
export class GatewayExtractionModel implements ExtractionModelV4 {
|
|
@@ -84,14 +85,14 @@ export class GatewayExtractionModel implements ExtractionModelV4 {
|
|
|
84
85
|
this.getModelConfigHeaders(),
|
|
85
86
|
await resolve(this.config.o11yHeaders),
|
|
86
87
|
),
|
|
87
|
-
body: {
|
|
88
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
88
89
|
stateParts,
|
|
89
90
|
fields,
|
|
90
91
|
referenceDate,
|
|
91
92
|
maxOutputTokens,
|
|
92
93
|
reasoning,
|
|
93
94
|
...(providerOptions ? { providerOptions } : {}),
|
|
94
|
-
},
|
|
95
|
+
}),
|
|
95
96
|
successfulResponseHandler: createJsonResponseHandler(
|
|
96
97
|
gatewayExtractionResponseSchema,
|
|
97
98
|
),
|
|
@@ -23,6 +23,7 @@ import { z } from './zod';
|
|
|
23
23
|
import type { GatewayConfig } from './gateway-config';
|
|
24
24
|
import { asGatewayError } from './errors';
|
|
25
25
|
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
26
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
26
27
|
|
|
27
28
|
type GatewayImageConfig = GatewayConfig & {
|
|
28
29
|
provider: string;
|
|
@@ -104,7 +105,7 @@ export class GatewayImageModel implements ImageModelV4 {
|
|
|
104
105
|
this.getModelConfigHeaders(),
|
|
105
106
|
await resolve(this.config.o11yHeaders),
|
|
106
107
|
),
|
|
107
|
-
body: {
|
|
108
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
108
109
|
prompt,
|
|
109
110
|
n,
|
|
110
111
|
...(size && { size }),
|
|
@@ -117,7 +118,7 @@ export class GatewayImageModel implements ImageModelV4 {
|
|
|
117
118
|
files: files.map(file => maybeEncodeImageFile(file)),
|
|
118
119
|
}),
|
|
119
120
|
...(mask && { mask: maybeEncodeImageFile(mask) }),
|
|
120
|
-
},
|
|
121
|
+
}),
|
|
121
122
|
successfulResponseHandler: createJsonResponseHandler(
|
|
122
123
|
gatewayImageResponseSchema,
|
|
123
124
|
streamingImageDecoding
|
|
@@ -24,6 +24,7 @@ import type { GatewayConfig } from './gateway-config';
|
|
|
24
24
|
import type { GatewayModelId } from './gateway-language-model-settings';
|
|
25
25
|
import { asGatewayError } from './errors';
|
|
26
26
|
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
27
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
27
28
|
|
|
28
29
|
export type GatewayChatConfig = GatewayConfig & {
|
|
29
30
|
provider: string;
|
|
@@ -61,7 +62,9 @@ export class GatewayLanguageModel implements LanguageModelV4 {
|
|
|
61
62
|
const { abortSignal: _abortSignal, ...optionsWithoutSignal } = options;
|
|
62
63
|
|
|
63
64
|
return {
|
|
64
|
-
args:
|
|
65
|
+
args: withGatewayRequestTelemetryRedaction(
|
|
66
|
+
this.maybeEncodeFileParts(optionsWithoutSignal),
|
|
67
|
+
),
|
|
65
68
|
warnings: [],
|
|
66
69
|
};
|
|
67
70
|
}
|
|
@@ -17,6 +17,7 @@ import { z } from './zod';
|
|
|
17
17
|
import { asGatewayError } from './errors';
|
|
18
18
|
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
19
19
|
import type { GatewayConfig } from './gateway-config';
|
|
20
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
20
21
|
|
|
21
22
|
export class GatewayRerankingModel implements RerankingModelV4 {
|
|
22
23
|
readonly specificationVersion = 'v4';
|
|
@@ -59,12 +60,12 @@ export class GatewayRerankingModel implements RerankingModelV4 {
|
|
|
59
60
|
this.getModelConfigHeaders(),
|
|
60
61
|
await resolve(this.config.o11yHeaders),
|
|
61
62
|
),
|
|
62
|
-
body: {
|
|
63
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
63
64
|
documents,
|
|
64
65
|
query,
|
|
65
66
|
...(topN != null ? { topN } : {}),
|
|
66
67
|
...(providerOptions ? { providerOptions } : {}),
|
|
67
|
-
},
|
|
68
|
+
}),
|
|
68
69
|
successfulResponseHandler: createJsonResponseHandler(
|
|
69
70
|
gatewayRerankingResponseSchema,
|
|
70
71
|
),
|
|
@@ -43,6 +43,7 @@ import {
|
|
|
43
43
|
errorControllerWithGatewayError,
|
|
44
44
|
} from './gateway-stream-errors';
|
|
45
45
|
import { getProtocolsFromHeaders } from './gateway-transcription-model';
|
|
46
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
46
47
|
|
|
47
48
|
export class GatewaySpeechModel
|
|
48
49
|
implements SpeechModelV4, StreamingSpeechModelV4
|
|
@@ -93,7 +94,7 @@ export class GatewaySpeechModel
|
|
|
93
94
|
this.getModelConfigHeaders(),
|
|
94
95
|
await resolve(this.config.o11yHeaders),
|
|
95
96
|
),
|
|
96
|
-
body: {
|
|
97
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
97
98
|
text,
|
|
98
99
|
...(voice && { voice }),
|
|
99
100
|
...(outputFormat && { outputFormat }),
|
|
@@ -101,7 +102,7 @@ export class GatewaySpeechModel
|
|
|
101
102
|
...(speed != null && { speed }),
|
|
102
103
|
...(language && { language }),
|
|
103
104
|
...(providerOptions && { providerOptions }),
|
|
104
|
-
},
|
|
105
|
+
}),
|
|
105
106
|
successfulResponseHandler: createJsonResponseHandler(
|
|
106
107
|
gatewaySpeechResponseSchema,
|
|
107
108
|
),
|
|
@@ -149,24 +150,25 @@ export class GatewaySpeechModel
|
|
|
149
150
|
|
|
150
151
|
// First frame after the WebSocket opens, per the speech-stream envelope.
|
|
151
152
|
// Optional keys are omitted so the frame stays minimal.
|
|
152
|
-
const startFrame: Experimental_SpeechStreamStartFrame =
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
153
|
+
const startFrame: Experimental_SpeechStreamStartFrame =
|
|
154
|
+
withGatewayRequestTelemetryRedaction({
|
|
155
|
+
type: SPEECH_STREAM_START_FRAME_TYPE,
|
|
156
|
+
...(options.voice != null && { voice: options.voice }),
|
|
157
|
+
...(options.outputFormat != null && {
|
|
158
|
+
outputFormat: options.outputFormat,
|
|
159
|
+
}),
|
|
160
|
+
...(options.instructions != null && {
|
|
161
|
+
instructions: options.instructions,
|
|
162
|
+
}),
|
|
163
|
+
...(options.speed != null && { speed: options.speed }),
|
|
164
|
+
...(options.language != null && { language: options.language }),
|
|
165
|
+
...(options.providerOptions != null && {
|
|
166
|
+
providerOptions: options.providerOptions,
|
|
167
|
+
}),
|
|
168
|
+
...(options.includeRawChunks != null && {
|
|
169
|
+
includeRawChunks: options.includeRawChunks,
|
|
170
|
+
}),
|
|
171
|
+
});
|
|
170
172
|
|
|
171
173
|
return {
|
|
172
174
|
stream: createGatewaySpeechStream({
|
|
@@ -42,6 +42,7 @@ import {
|
|
|
42
42
|
GATEWAY_TRANSCRIPTION_SUBPROTOCOL,
|
|
43
43
|
getGatewayTranscriptionProtocols,
|
|
44
44
|
} from './gateway-realtime-auth';
|
|
45
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
45
46
|
|
|
46
47
|
export class GatewayTranscriptionModel implements TranscriptionModelV4 {
|
|
47
48
|
readonly specificationVersion = 'v4' as const;
|
|
@@ -87,14 +88,14 @@ export class GatewayTranscriptionModel implements TranscriptionModelV4 {
|
|
|
87
88
|
this.getModelConfigHeaders(),
|
|
88
89
|
await resolve(this.config.o11yHeaders),
|
|
89
90
|
),
|
|
90
|
-
body: {
|
|
91
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
91
92
|
audio:
|
|
92
93
|
audio instanceof Uint8Array
|
|
93
94
|
? convertUint8ArrayToBase64(audio)
|
|
94
95
|
: audio,
|
|
95
96
|
mediaType,
|
|
96
97
|
...(providerOptions && { providerOptions }),
|
|
97
|
-
},
|
|
98
|
+
}),
|
|
98
99
|
successfulResponseHandler: createJsonResponseHandler(
|
|
99
100
|
gatewayTranscriptionResponseSchema,
|
|
100
101
|
),
|
|
@@ -170,16 +171,17 @@ export class GatewayTranscriptionModel implements TranscriptionModelV4 {
|
|
|
170
171
|
|
|
171
172
|
// First frame after the WebSocket opens, per the transcription-stream
|
|
172
173
|
// envelope. Optional keys are omitted so the frame stays minimal.
|
|
173
|
-
const startFrame: Experimental_TranscriptionStreamStartFrame =
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
174
|
+
const startFrame: Experimental_TranscriptionStreamStartFrame =
|
|
175
|
+
withGatewayRequestTelemetryRedaction({
|
|
176
|
+
type: TRANSCRIPTION_STREAM_START_FRAME_TYPE,
|
|
177
|
+
inputAudioFormat: options.inputAudioFormat,
|
|
178
|
+
...(options.providerOptions != null && {
|
|
179
|
+
providerOptions: options.providerOptions,
|
|
180
|
+
}),
|
|
181
|
+
...(options.includeRawChunks != null && {
|
|
182
|
+
includeRawChunks: options.includeRawChunks,
|
|
183
|
+
}),
|
|
184
|
+
});
|
|
183
185
|
|
|
184
186
|
return {
|
|
185
187
|
stream: createGatewayTranscriptionStream({
|
|
@@ -28,6 +28,7 @@ import { z } from './zod';
|
|
|
28
28
|
import type { GatewayConfig } from './gateway-config';
|
|
29
29
|
import { asGatewayError } from './errors';
|
|
30
30
|
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
31
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
31
32
|
|
|
32
33
|
export class GatewayVideoModel implements VideoModelV4 {
|
|
33
34
|
readonly specificationVersion = 'v4' as const;
|
|
@@ -70,7 +71,9 @@ export class GatewayVideoModel implements VideoModelV4 {
|
|
|
70
71
|
await resolve(this.config.o11yHeaders),
|
|
71
72
|
{ accept: 'text/event-stream' },
|
|
72
73
|
),
|
|
73
|
-
body:
|
|
74
|
+
body: withGatewayRequestTelemetryRedaction(
|
|
75
|
+
this.buildRequestBody(options),
|
|
76
|
+
),
|
|
74
77
|
successfulResponseHandler: async ({
|
|
75
78
|
response,
|
|
76
79
|
url,
|
|
@@ -209,12 +212,12 @@ export class GatewayVideoModel implements VideoModelV4 {
|
|
|
209
212
|
this.getModelConfigHeaders(),
|
|
210
213
|
await resolve(this.config.o11yHeaders),
|
|
211
214
|
),
|
|
212
|
-
body: {
|
|
215
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
213
216
|
...this.buildRequestBody(options),
|
|
214
217
|
// The spec option is `webhookUrl`; the Gateway's wire contract for a
|
|
215
218
|
// completion webhook is `callbackUrl`.
|
|
216
219
|
...(webhookUrl && { callbackUrl: webhookUrl }),
|
|
217
|
-
},
|
|
220
|
+
}),
|
|
218
221
|
successfulResponseHandler: createJsonResponseHandler(
|
|
219
222
|
gatewayVideoStartResponseSchema,
|
|
220
223
|
),
|
|
@@ -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
|
+
}
|