@@ -26,13 +26,18 @@ import { Task, makeRequest } from '../requests/request';
26
26
import { createEnhancedContentResponse } from '../requests/response-helpers' ;
27
27
import { processStream } from '../requests/stream-reader' ;
28
28
import { ApiSettings } from '../types/internal' ;
29
+ import { BackendType } from '../public-types' ;
30
+ import * as GoogleAIMapper from '../googleai-mappers' ;
29
31
30
32
export async function generateContentStream (
31
33
apiSettings : ApiSettings ,
32
34
model : string ,
33
35
params : GenerateContentRequest ,
34
36
requestOptions ?: RequestOptions ,
35
37
) : Promise < GenerateContentStreamResult > {
38
+ if ( apiSettings . backend . backendType === BackendType . GOOGLE_AI ) {
39
+ params = GoogleAIMapper . mapGenerateContentRequest ( params ) ;
40
+ }
36
41
const response = await makeRequest (
37
42
model ,
38
43
Task . STREAM_GENERATE_CONTENT ,
@@ -41,7 +46,7 @@ export async function generateContentStream(
41
46
JSON . stringify ( params ) ,
42
47
requestOptions ,
43
48
) ;
44
- return processStream ( response ) ;
49
+ return processStream ( response , apiSettings ) ;
45
50
}
46
51
47
52
export async function generateContent (
@@ -50,6 +55,9 @@ export async function generateContent(
50
55
params : GenerateContentRequest ,
51
56
requestOptions ?: RequestOptions ,
52
57
) : Promise < GenerateContentResult > {
58
+ if ( apiSettings . backend . backendType === BackendType . GOOGLE_AI ) {
59
+ params = GoogleAIMapper . mapGenerateContentRequest ( params ) ;
60
+ }
53
61
const response = await makeRequest (
54
62
model ,
55
63
Task . GENERATE_CONTENT ,
@@ -58,9 +66,21 @@ export async function generateContent(
58
66
JSON . stringify ( params ) ,
59
67
requestOptions ,
60
68
) ;
61
- const responseJson : GenerateContentResponse = await response . json ( ) ;
62
- const enhancedResponse = createEnhancedContentResponse ( responseJson ) ;
69
+ const generateContentResponse = await processGenerateContentResponse ( response , apiSettings ) ;
70
+ const enhancedResponse = createEnhancedContentResponse ( generateContentResponse ) ;
63
71
return {
64
72
response : enhancedResponse ,
65
73
} ;
66
74
}
75
+
76
+ async function processGenerateContentResponse (
77
+ response : Response ,
78
+ apiSettings : ApiSettings ,
79
+ ) : Promise < GenerateContentResponse > {
80
+ const responseJson = await response . json ( ) ;
81
+ if ( apiSettings . backend . backendType === BackendType . GOOGLE_AI ) {
82
+ return GoogleAIMapper . mapGenerateContentResponse ( responseJson ) ;
83
+ } else {
84
+ return responseJson ;
85
+ }
86
+ }
0 commit comments