11// app/client/ViduProxy.ts
22
3+ import { SunoEndpoint , ViduEndpoint } from "@/constant" ;
4+ import { api2ProviderBaseUrl } from "@/app/store" ;
5+ import { getRequestOptions } from "@/app/client/helper" ;
6+
37export interface ViduTaskGenerationRequest {
48 input : {
59 prompts : {
10+ type : string | "text" | "image" ;
611 content : string ;
712 enhance : boolean ;
8- type : "text" | "image" ;
913 } [ ] ;
1014 } ;
15+ type : string | "img2video" ;
1116 settings : {
1217 style : string ;
1318 aspect_ratio : string ;
1419 duration : number ;
15- model : string ;
20+ model : string | "vidu-1" ;
1621 } ;
17- type : string ;
22+ }
23+
24+ export interface ViduTaskGenerationResponse {
25+
1826}
1927
2028export class ViduAPI {
@@ -24,5 +32,52 @@ export class ViduAPI {
2432 this . apiKey = apiKey ;
2533 }
2634
35+ path ( endpoint : ViduEndpoint ) {
36+ return [ api2ProviderBaseUrl . Vidu , endpoint ] . join ( "/" ) ;
37+ }
38+
39+ async generateViduTask ( request : ViduTaskGenerationRequest , signal ?: AbortSignal , timeoutMs : number = 10000 ) {
40+ const controller = new AbortController ( ) ;
41+ const timeoutId = setTimeout ( ( ) => controller . abort ( ) , timeoutMs ) ;
42+ const abortSignal = signal || controller . signal ;
43+
44+ signal && signal . addEventListener ( "abort" , ( ) => controller . abort ( ) ) ;
45+
46+ try {
47+ const res = await fetch ( this . path ( ViduEndpoint . GENERATION ) , {
48+ ...getRequestOptions ( this . apiKey , request ) ,
49+ signal : abortSignal
50+ } ) ;
51+
52+ clearTimeout ( timeoutId ) ;
53+
54+ return res ;
55+ } catch ( e ) {
56+ console . error ( "[ViduProxy] failed to make a vidu generate-task request" , e ) ;
57+ throw e ;
58+ }
59+ }
60+
61+ async getViduTask ( id : string , signal ?: AbortSignal , timeoutMs : number = 10000 ) {
62+ const controller = new AbortController ( ) ;
63+ const timeoutId = setTimeout ( ( ) => controller . abort ( ) , timeoutMs ) ;
64+ const abortSignal = signal || controller . signal ;
65+
66+ signal && signal . addEventListener ( "abort" , ( ) => controller . abort ( ) ) ;
67+
68+ try {
69+ const res = await fetch ( this . path ( ViduEndpoint . TASK_GET ) . replace ( "{{id}}" , id ) , {
70+ ...getRequestOptions ( this . apiKey , "GET" ) ,
71+ signal : abortSignal
72+ } ) ;
73+
74+ clearTimeout ( timeoutId ) ;
75+
76+ return res ;
77+ } catch ( e ) {
78+ console . error ( "[ViduProxy] failed to make a vidu get-task request" , e ) ;
79+ throw e ;
80+ }
81+ }
2782
2883}
0 commit comments