@ai-sdk/gateway 4.0.0-beta.3 → 4.0.0-beta.30

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,191 @@
1
+ import {
2
+ createJsonErrorResponseHandler,
3
+ createJsonResponseHandler,
4
+ getFromApi,
5
+ lazySchema,
6
+ resolve,
7
+ zodSchema,
8
+ } from '@ai-sdk/provider-utils';
9
+ import { z } from 'zod/v4';
10
+ import { asGatewayError } from './errors';
11
+ import type { GatewayConfig } from './gateway-config';
12
+
13
+ export interface GatewaySpendReportParams {
14
+ /** Start date in YYYY-MM-DD format (inclusive) */
15
+ startDate: string;
16
+ /** End date in YYYY-MM-DD format (inclusive) */
17
+ endDate: string;
18
+ /** Primary aggregation dimension. Defaults to 'day'. */
19
+ groupBy?: 'day' | 'user' | 'model' | 'tag' | 'provider' | 'credential_type';
20
+ /** Time granularity when groupBy is 'day'. */
21
+ datePart?: 'day' | 'hour';
22
+ /** Filter to a specific user's spend. */
23
+ userId?: string;
24
+ /** Filter to a specific model (e.g. 'anthropic/claude-sonnet-4.5'). */
25
+ model?: string;
26
+ /** Filter to a specific provider (e.g. 'anthropic'). */
27
+ provider?: string;
28
+ /** Filter to BYOK or system credentials. */
29
+ credentialType?: 'byok' | 'system';
30
+ /** Filter to requests with these tags. */
31
+ tags?: string[];
32
+ }
33
+
34
+ export interface GatewaySpendReportRow {
35
+ /** Date string (present when groupBy is 'day') */
36
+ day?: string;
37
+ /** Hour timestamp (present when groupBy is 'day' and datePart is 'hour') */
38
+ hour?: string;
39
+ /** User identifier (present when groupBy is 'user') */
40
+ user?: string;
41
+ /** Model identifier (present when groupBy is 'model') */
42
+ model?: string;
43
+ /** Tag value (present when groupBy is 'tag') */
44
+ tag?: string;
45
+ /** Provider name (present when groupBy is 'provider') */
46
+ provider?: string;
47
+ /** Credential type (present when groupBy is 'credential_type') */
48
+ credentialType?: 'byok' | 'system';
49
+
50
+ /** Total cost in USD */
51
+ totalCost: number;
52
+ /** Market cost in USD */
53
+ marketCost?: number;
54
+ /** Number of input tokens */
55
+ inputTokens?: number;
56
+ /** Number of output tokens */
57
+ outputTokens?: number;
58
+ /** Number of cached input tokens */
59
+ cachedInputTokens?: number;
60
+ /** Number of cache creation input tokens */
61
+ cacheCreationInputTokens?: number;
62
+ /** Number of reasoning tokens */
63
+ reasoningTokens?: number;
64
+ /** Number of requests */
65
+ requestCount?: number;
66
+ }
67
+
68
+ export interface GatewaySpendReportResponse {
69
+ results: GatewaySpendReportRow[];
70
+ }
71
+
72
+ export class GatewaySpendReport {
73
+ constructor(private readonly config: GatewayConfig) {}
74
+
75
+ async getSpendReport(
76
+ params: GatewaySpendReportParams,
77
+ ): Promise<GatewaySpendReportResponse> {
78
+ try {
79
+ const baseUrl = new URL(this.config.baseURL);
80
+
81
+ const searchParams = new URLSearchParams();
82
+ searchParams.set('start_date', params.startDate);
83
+ searchParams.set('end_date', params.endDate);
84
+
85
+ if (params.groupBy) {
86
+ searchParams.set('group_by', params.groupBy);
87
+ }
88
+ if (params.datePart) {
89
+ searchParams.set('date_part', params.datePart);
90
+ }
91
+ if (params.userId) {
92
+ searchParams.set('user_id', params.userId);
93
+ }
94
+ if (params.model) {
95
+ searchParams.set('model', params.model);
96
+ }
97
+ if (params.provider) {
98
+ searchParams.set('provider', params.provider);
99
+ }
100
+ if (params.credentialType) {
101
+ searchParams.set('credential_type', params.credentialType);
102
+ }
103
+ if (params.tags && params.tags.length > 0) {
104
+ searchParams.set('tags', params.tags.join(','));
105
+ }
106
+
107
+ const { value } = await getFromApi({
108
+ url: `${baseUrl.origin}/v1/report?${searchParams.toString()}`,
109
+ headers: await resolve(this.config.headers()),
110
+ successfulResponseHandler: createJsonResponseHandler(
111
+ gatewaySpendReportResponseSchema,
112
+ ),
113
+ failedResponseHandler: createJsonErrorResponseHandler({
114
+ errorSchema: z.any(),
115
+ errorToMessage: data => data,
116
+ }),
117
+ fetch: this.config.fetch,
118
+ });
119
+
120
+ return value;
121
+ } catch (error) {
122
+ throw await asGatewayError(error);
123
+ }
124
+ }
125
+ }
126
+
127
+ const gatewaySpendReportResponseSchema = lazySchema(() =>
128
+ zodSchema(
129
+ z.object({
130
+ results: z.array(
131
+ z
132
+ .object({
133
+ day: z.string().optional(),
134
+ hour: z.string().optional(),
135
+ user: z.string().optional(),
136
+ model: z.string().optional(),
137
+ tag: z.string().optional(),
138
+ provider: z.string().optional(),
139
+ credential_type: z.enum(['byok', 'system']).optional(),
140
+ total_cost: z.number(),
141
+ market_cost: z.number().optional(),
142
+ input_tokens: z.number().optional(),
143
+ output_tokens: z.number().optional(),
144
+ cached_input_tokens: z.number().optional(),
145
+ cache_creation_input_tokens: z.number().optional(),
146
+ reasoning_tokens: z.number().optional(),
147
+ request_count: z.number().optional(),
148
+ })
149
+ .transform(
150
+ ({
151
+ credential_type,
152
+ total_cost,
153
+ market_cost,
154
+ input_tokens,
155
+ output_tokens,
156
+ cached_input_tokens,
157
+ cache_creation_input_tokens,
158
+ reasoning_tokens,
159
+ request_count,
160
+ ...rest
161
+ }) => ({
162
+ ...rest,
163
+ ...(credential_type !== undefined
164
+ ? { credentialType: credential_type }
165
+ : {}),
166
+ totalCost: total_cost,
167
+ ...(market_cost !== undefined ? { marketCost: market_cost } : {}),
168
+ ...(input_tokens !== undefined
169
+ ? { inputTokens: input_tokens }
170
+ : {}),
171
+ ...(output_tokens !== undefined
172
+ ? { outputTokens: output_tokens }
173
+ : {}),
174
+ ...(cached_input_tokens !== undefined
175
+ ? { cachedInputTokens: cached_input_tokens }
176
+ : {}),
177
+ ...(cache_creation_input_tokens !== undefined
178
+ ? { cacheCreationInputTokens: cache_creation_input_tokens }
179
+ : {}),
180
+ ...(reasoning_tokens !== undefined
181
+ ? { reasoningTokens: reasoning_tokens }
182
+ : {}),
183
+ ...(request_count !== undefined
184
+ ? { requestCount: request_count }
185
+ : {}),
186
+ }),
187
+ ),
188
+ ),
189
+ }),
190
+ ),
191
+ );
@@ -1,10 +1,10 @@
1
1
  import type {
2
- Experimental_VideoModelV3,
3
- Experimental_VideoModelV3CallOptions,
4
- Experimental_VideoModelV3File,
5
- Experimental_VideoModelV3VideoData,
6
- SharedV3ProviderMetadata,
7
- SharedV3Warning,
2
+ Experimental_VideoModelV4,
3
+ Experimental_VideoModelV4CallOptions,
4
+ Experimental_VideoModelV4File,
5
+ Experimental_VideoModelV4VideoData,
6
+ SharedV4ProviderMetadata,
7
+ SharedV4Warning,
8
8
  } from '@ai-sdk/provider';
9
9
  import { APICallError } from '@ai-sdk/provider';
10
10
  import {
@@ -21,8 +21,8 @@ import type { GatewayConfig } from './gateway-config';
21
21
  import { asGatewayError } from './errors';
22
22
  import { parseAuthMethod } from './errors/parse-auth-method';
23
23
 
24
- export class GatewayVideoModel implements Experimental_VideoModelV3 {
25
- readonly specificationVersion = 'v3' as const;
24
+ export class GatewayVideoModel implements Experimental_VideoModelV4 {
25
+ readonly specificationVersion = 'v4' as const;
26
26
  // Set a very large number to prevent client-side splitting of requests
27
27
  readonly maxVideosPerCall = Number.MAX_SAFE_INTEGER;
28
28
 
@@ -50,10 +50,10 @@ export class GatewayVideoModel implements Experimental_VideoModelV3 {
50
50
  providerOptions,
51
51
  headers,
52
52
  abortSignal,
53
- }: Experimental_VideoModelV3CallOptions): Promise<{
54
- videos: Array<Experimental_VideoModelV3VideoData>;
55
- warnings: Array<SharedV3Warning>;
56
- providerMetadata?: SharedV3ProviderMetadata;
53
+ }: Experimental_VideoModelV4CallOptions): Promise<{
54
+ videos: Array<Experimental_VideoModelV4VideoData>;
55
+ warnings: Array<SharedV4Warning>;
56
+ providerMetadata?: SharedV4ProviderMetadata;
57
57
  response: {
58
58
  timestamp: Date;
59
59
  modelId: string;
@@ -170,7 +170,7 @@ export class GatewayVideoModel implements Experimental_VideoModelV3 {
170
170
  videos: responseBody.videos,
171
171
  warnings: responseBody.warnings ?? [],
172
172
  providerMetadata:
173
- responseBody.providerMetadata as SharedV3ProviderMetadata,
173
+ responseBody.providerMetadata as SharedV4ProviderMetadata,
174
174
  response: {
175
175
  timestamp: new Date(),
176
176
  modelId: this.modelId,
@@ -188,13 +188,13 @@ export class GatewayVideoModel implements Experimental_VideoModelV3 {
188
188
 
189
189
  private getModelConfigHeaders() {
190
190
  return {
191
- 'ai-video-model-specification-version': '3',
191
+ 'ai-video-model-specification-version': '4',
192
192
  'ai-model-id': this.modelId,
193
193
  };
194
194
  }
195
195
  }
196
196
 
197
- function maybeEncodeVideoFile(file: Experimental_VideoModelV3File) {
197
+ function maybeEncodeVideoFile(file: Experimental_VideoModelV4File) {
198
198
  if (file.type === 'file' && file.data instanceof Uint8Array) {
199
199
  return {
200
200
  ...file,
package/src/index.ts CHANGED
@@ -5,6 +5,15 @@ export type {
5
5
  GatewayLanguageModelSpecification,
6
6
  } from './gateway-model-entry';
7
7
  export type { GatewayCreditsResponse } from './gateway-fetch-metadata';
8
+ export type {
9
+ GatewaySpendReportParams,
10
+ GatewaySpendReportRow,
11
+ GatewaySpendReportResponse,
12
+ } from './gateway-spend-report';
13
+ export type {
14
+ GatewayGenerationInfoParams,
15
+ GatewayGenerationInfo,
16
+ } from './gateway-generation-info';
8
17
  export type { GatewayLanguageModelEntry as GatewayModelEntry } from './gateway-model-entry';
9
18
  export {
10
19
  createGatewayProvider,
@@ -16,9 +25,9 @@ export type {
16
25
  GatewayProviderSettings,
17
26
  } from './gateway-provider';
18
27
  export type {
19
- GatewayLanguageModelOptions,
20
- /** @deprecated Use `GatewayLanguageModelOptions` instead. */
21
- GatewayLanguageModelOptions as GatewayProviderOptions,
28
+ GatewayProviderOptions,
29
+ /** @deprecated Use `GatewayProviderOptions` instead. */
30
+ GatewayProviderOptions as GatewayLanguageModelOptions,
22
31
  } from './gateway-provider-options';
23
32
  export {
24
33
  GatewayError,