@@ -2,24 +2,42 @@ import type { CommonRequestOptions } from '@xsai/shared'
2
2
3
3
import { requestHeaders , requestURL , responseJSON } from '@xsai/shared'
4
4
5
- export interface GenerateTranscriptionOptions < T extends GenerateTranscriptionOptionsTimeStampGranularities = undefined > extends CommonRequestOptions {
5
+ export interface GenerateTranscriptionOptions <
6
+ T1 extends GenerateTranscriptionOptionsResponseFormat ,
7
+ T2 extends GenerateTranscriptionOptionsTimeStampGranularities ,
8
+ > extends CommonRequestOptions {
6
9
file : Blob
7
10
fileName ?: string
8
11
language ?: string
9
12
prompt ?: string
13
+ /** @default `json` */
14
+ responseFormat ?: T1
10
15
temperature ?: string
11
16
/** @default `segment` */
12
- timestampGranularities ?: T
17
+ timestampGranularities ?: T2
13
18
}
14
19
20
+ export type GenerateTranscriptionOptionsResponseFormat = 'json' | 'verbose_json' | undefined
21
+
15
22
export type GenerateTranscriptionOptionsTimeStampGranularities = 'segment' | 'word' | undefined
16
23
17
- export interface GenerateTranscriptionResult < T extends GenerateTranscriptionOptionsTimeStampGranularities = undefined > {
18
- duration : number
19
- language : string
20
- segments : T extends 'word' ? never : GenerateTranscriptionResultSegment [ ]
24
+ export interface GenerateTranscriptionResult <
25
+ T1 extends GenerateTranscriptionOptionsResponseFormat ,
26
+ T2 extends GenerateTranscriptionOptionsTimeStampGranularities ,
27
+ > {
28
+ duration : T1 extends 'verbose_json' ? number : never
29
+ language : T1 extends 'verbose_json' ? string : never
30
+ segments : T1 extends 'verbose_json'
31
+ ? T2 extends 'word'
32
+ ? never
33
+ : GenerateTranscriptionResultSegment [ ]
34
+ : never
21
35
text : string
22
- words : T extends 'word' ? GenerateTranscriptionResultWord [ ] : never
36
+ words : T1 extends 'verbose_json'
37
+ ? T2 extends 'word'
38
+ ? GenerateTranscriptionResultWord [ ]
39
+ : never
40
+ : never
23
41
}
24
42
25
43
/** @see {@link https://platform.openai.com/docs/api-reference/audio/verbose-json-object#audio/verbose-json-object-segments } */
@@ -43,13 +61,20 @@ export interface GenerateTranscriptionResultWord {
43
61
word : string
44
62
}
45
63
46
- export const generateTranscription = async < T extends GenerateTranscriptionOptionsTimeStampGranularities = undefined > ( options : GenerateTranscriptionOptions < T > ) : Promise < GenerateTranscriptionResult < T > > => {
64
+ export const generateTranscription = async <
65
+ T1 extends GenerateTranscriptionOptionsResponseFormat = undefined ,
66
+ T2 extends GenerateTranscriptionOptionsTimeStampGranularities = undefined ,
67
+ > ( options : GenerateTranscriptionOptions < T1 , T2 > ) : Promise < GenerateTranscriptionResult < T1 , T2 > > => {
47
68
const body = new FormData ( )
48
69
49
70
body . append ( 'model' , options . model )
50
71
body . append ( 'file' , options . file , options . fileName )
51
- body . append ( 'response_format' , 'verbose_json' )
52
- body . append ( 'timestamp_granularities[]' , options . timestampGranularities ?? 'segment' )
72
+ body . append ( 'response_format' , options . responseFormat ?? 'json' )
73
+
74
+ // make ts happy
75
+ // eslint-disable-next-line ts/no-unnecessary-type-assertion
76
+ if ( options . responseFormat as GenerateTranscriptionOptionsResponseFormat === 'verbose_json' )
77
+ body . append ( 'timestamp_granularities[]' , options . timestampGranularities ?? 'segment' )
53
78
54
79
if ( options . language != null )
55
80
body . append ( 'language' , options . language )
@@ -66,5 +91,5 @@ export const generateTranscription = async <T extends GenerateTranscriptionOptio
66
91
method : 'POST' ,
67
92
signal : options . abortSignal ,
68
93
} )
69
- . then ( responseJSON < GenerateTranscriptionResult < T > > )
94
+ . then ( responseJSON < GenerateTranscriptionResult < T1 , T2 > > )
70
95
}
0 commit comments