@ai-sdk/google
Advanced tools
+3
-3
| { | ||
| "name": "@ai-sdk/google", | ||
| "version": "4.0.31", | ||
| "version": "4.0.32", | ||
| "type": "module", | ||
@@ -38,4 +38,4 @@ "license": "Apache-2.0", | ||
| "dependencies": { | ||
| "@ai-sdk/provider": "4.0.4", | ||
| "@ai-sdk/provider-utils": "5.0.18" | ||
| "@ai-sdk/provider": "4.0.5", | ||
| "@ai-sdk/provider-utils": "5.0.19" | ||
| }, | ||
@@ -42,0 +42,0 @@ "devDependencies": { |
+133
-85
| import { | ||
| AISDKError, | ||
| type Experimental_VideoModelV4, | ||
| type Experimental_VideoModelV4CallOptions as VideoModelV4CallOptions, | ||
| type Experimental_VideoModelV4File, | ||
| type Experimental_VideoModelV4OperationStartResult as VideoModelV4OperationStartResult, | ||
| type Experimental_VideoModelV4OperationStatusResult as VideoModelV4OperationStatusResult, | ||
| type SharedV4ProviderMetadata, | ||
| type SharedV4Warning, | ||
@@ -11,3 +15,2 @@ } from '@ai-sdk/provider'; | ||
| createJsonResponseHandler, | ||
| delay, | ||
| getFromApi, | ||
@@ -29,2 +32,4 @@ isSameOrigin, | ||
| type VideoModelV4 = Experimental_VideoModelV4; | ||
| interface GoogleVideoModelConfig { | ||
@@ -42,3 +47,3 @@ provider: string; | ||
| function getFirstFrameImage( | ||
| options: Parameters<Experimental_VideoModelV4['doGenerate']>[0], | ||
| options: VideoModelV4CallOptions, | ||
| ): Experimental_VideoModelV4File | undefined { | ||
@@ -50,3 +55,3 @@ return options.frameImages?.find(frame => frame.frameType === 'first_frame') | ||
| function resolveStartImage( | ||
| options: Parameters<Experimental_VideoModelV4['doGenerate']>[0], | ||
| options: VideoModelV4CallOptions, | ||
| ): Experimental_VideoModelV4File | undefined { | ||
@@ -57,3 +62,3 @@ return getFirstFrameImage(options) ?? options.image; | ||
| function getLastFrameImage( | ||
| options: Parameters<Experimental_VideoModelV4['doGenerate']>[0], | ||
| options: VideoModelV4CallOptions, | ||
| ): Experimental_VideoModelV4File | undefined { | ||
@@ -65,3 +70,3 @@ return options.frameImages?.find(frame => frame.frameType === 'last_frame') | ||
| function getInputReferences( | ||
| options: Parameters<Experimental_VideoModelV4['doGenerate']>[0], | ||
| options: VideoModelV4CallOptions, | ||
| ): Array<Experimental_VideoModelV4File> | undefined { | ||
@@ -162,6 +167,10 @@ if (options.frameImages != null && options.frameImages.length > 0) { | ||
| async doGenerate( | ||
| options: Parameters<Experimental_VideoModelV4['doGenerate']>[0], | ||
| ): Promise<Awaited<ReturnType<Experimental_VideoModelV4['doGenerate']>>> { | ||
| const currentDate = this.config._internal?.currentDate?.() ?? new Date(); | ||
| private async buildRequest( | ||
| options: Parameters<NonNullable<VideoModelV4['doStart']>>[0], | ||
| ): Promise<{ | ||
| instances: Array<Record<string, unknown>>; | ||
| parameters: Record<string, unknown>; | ||
| warnings: SharedV4Warning[]; | ||
| googleOptions: GoogleVideoModelOptions | undefined; | ||
| }> { | ||
| const warnings: SharedV4Warning[] = []; | ||
@@ -264,79 +273,21 @@ | ||
| const { value: operation } = await postJsonToApi({ | ||
| url: `${this.config.baseURL}/models/${this.modelId}:predictLongRunning`, | ||
| headers: combineHeaders( | ||
| await resolve(this.config.headers), | ||
| options.headers, | ||
| ), | ||
| body: { | ||
| instances, | ||
| parameters, | ||
| }, | ||
| successfulResponseHandler: createJsonResponseHandler( | ||
| googleOperationSchema, | ||
| ), | ||
| failedResponseHandler: googleFailedResponseHandler, | ||
| abortSignal: options.abortSignal, | ||
| fetch: this.config.fetch, | ||
| }); | ||
| return { instances, parameters, warnings, googleOptions }; | ||
| } | ||
| const operationName = operation.name; | ||
| if (!operationName) { | ||
| throw new AISDKError({ | ||
| name: 'GOOGLE_VIDEO_GENERATION_ERROR', | ||
| message: 'No operation name returned from API', | ||
| }); | ||
| } | ||
| const pollIntervalMs = googleOptions?.pollIntervalMs ?? 10000; // 10 seconds (per Google docs) | ||
| const pollTimeoutMs = googleOptions?.pollTimeoutMs ?? 600000; // 10 minutes | ||
| const startTime = Date.now(); | ||
| let finalOperation = operation; | ||
| let responseHeaders: Record<string, string> | undefined; | ||
| while (!finalOperation.done) { | ||
| if (Date.now() - startTime > pollTimeoutMs) { | ||
| throw new AISDKError({ | ||
| name: 'GOOGLE_VIDEO_GENERATION_TIMEOUT', | ||
| message: `Video generation timed out after ${pollTimeoutMs}ms`, | ||
| }); | ||
| } | ||
| await delay(pollIntervalMs); | ||
| if (options.abortSignal?.aborted) { | ||
| throw new AISDKError({ | ||
| name: 'GOOGLE_VIDEO_GENERATION_ABORTED', | ||
| message: 'Video generation request was aborted', | ||
| }); | ||
| } | ||
| const { value: statusOperation, responseHeaders: pollHeaders } = | ||
| await getFromApi({ | ||
| url: `${this.config.baseURL}/${operationName}`, | ||
| validateUrl: false, | ||
| headers: combineHeaders( | ||
| await resolve(this.config.headers), | ||
| options.headers, | ||
| ), | ||
| successfulResponseHandler: createJsonResponseHandler( | ||
| googleOperationSchema, | ||
| ), | ||
| failedResponseHandler: googleFailedResponseHandler, | ||
| abortSignal: options.abortSignal, | ||
| fetch: this.config.fetch, | ||
| }); | ||
| finalOperation = statusOperation; | ||
| responseHeaders = pollHeaders; | ||
| } | ||
| if (finalOperation.error) { | ||
| throw new AISDKError({ | ||
| name: 'GOOGLE_VIDEO_GENERATION_FAILED', | ||
| message: `Video generation failed: ${finalOperation.error.message}`, | ||
| }); | ||
| } | ||
| private async buildCompletedResult( | ||
| finalOperation: z.infer<typeof googleOperationSchema>, | ||
| responseHeaders: Record<string, string> | undefined, | ||
| warnings: SharedV4Warning[], | ||
| currentDate: Date, | ||
| ): Promise<{ | ||
| status: 'completed'; | ||
| videos: Array<{ type: 'url'; url: string; mediaType: string }>; | ||
| warnings: SharedV4Warning[]; | ||
| providerMetadata: SharedV4ProviderMetadata; | ||
| response: { | ||
| timestamp: Date; | ||
| modelId: string; | ||
| headers: Record<string, string> | undefined; | ||
| }; | ||
| }> { | ||
| const response = finalOperation.response; | ||
@@ -390,2 +341,3 @@ if ( | ||
| return { | ||
| status: 'completed', | ||
| videos, | ||
@@ -405,2 +357,98 @@ warnings, | ||
| } | ||
| async doStart( | ||
| options: Parameters<NonNullable<VideoModelV4['doStart']>>[0], | ||
| ): Promise<VideoModelV4OperationStartResult> { | ||
| const currentDate = this.config._internal?.currentDate?.() ?? new Date(); | ||
| const { instances, parameters, warnings } = | ||
| await this.buildRequest(options); | ||
| const { value: operation, responseHeaders } = await postJsonToApi({ | ||
| url: `${this.config.baseURL}/models/${this.modelId}:predictLongRunning`, | ||
| headers: combineHeaders( | ||
| await resolve(this.config.headers), | ||
| options.headers, | ||
| ), | ||
| body: { | ||
| instances, | ||
| parameters, | ||
| }, | ||
| successfulResponseHandler: createJsonResponseHandler( | ||
| googleOperationSchema, | ||
| ), | ||
| failedResponseHandler: googleFailedResponseHandler, | ||
| abortSignal: options.abortSignal, | ||
| fetch: this.config.fetch, | ||
| }); | ||
| const operationName = operation.name; | ||
| if (!operationName) { | ||
| throw new AISDKError({ | ||
| name: 'GOOGLE_VIDEO_GENERATION_ERROR', | ||
| message: 'No operation name returned from API', | ||
| }); | ||
| } | ||
| return { | ||
| operation: { operationName }, | ||
| warnings, | ||
| response: { | ||
| timestamp: currentDate, | ||
| modelId: this.modelId, | ||
| headers: responseHeaders, | ||
| }, | ||
| }; | ||
| } | ||
| async doStatus( | ||
| options: Parameters<NonNullable<VideoModelV4['doStatus']>>[0], | ||
| ): Promise<VideoModelV4OperationStatusResult> { | ||
| const currentDate = this.config._internal?.currentDate?.() ?? new Date(); | ||
| const { operationName } = options.operation as { operationName: string }; | ||
| const { value: statusOperation, responseHeaders } = await getFromApi({ | ||
| url: `${this.config.baseURL}/${operationName}`, | ||
| validateUrl: false, | ||
| headers: combineHeaders( | ||
| await resolve(this.config.headers), | ||
| options.headers, | ||
| ), | ||
| successfulResponseHandler: createJsonResponseHandler( | ||
| googleOperationSchema, | ||
| ), | ||
| failedResponseHandler: googleFailedResponseHandler, | ||
| abortSignal: options.abortSignal, | ||
| fetch: this.config.fetch, | ||
| }); | ||
| if (!statusOperation.done) { | ||
| return { | ||
| status: 'pending', | ||
| response: { | ||
| timestamp: currentDate, | ||
| modelId: this.modelId, | ||
| headers: responseHeaders, | ||
| }, | ||
| }; | ||
| } | ||
| if (statusOperation.error) { | ||
| return { | ||
| status: 'error' as const, | ||
| error: `Video generation failed: ${statusOperation.error.message}`, | ||
| response: { | ||
| timestamp: currentDate, | ||
| modelId: this.modelId, | ||
| headers: responseHeaders, | ||
| }, | ||
| }; | ||
| } | ||
| return this.buildCompletedResult( | ||
| statusOperation, | ||
| responseHeaders, | ||
| [], | ||
| currentDate, | ||
| ); | ||
| } | ||
| } | ||
@@ -407,0 +455,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
2099847
0.17%26648
0.25%+ Added
+ Added
- Removed
- Removed
Updated