11import { GenerateContentConfig , GoogleGenAI , GoogleGenAIOptions , Part } from '@google/genai'
22import { Provider } from '../model.js'
33import { buildJsonSchema } from '../schema/json.js'
4- import { Schema } from 'zod'
4+ import { Schema , ZodString } from 'zod'
55import { Message } from '../step.js'
66
77function buildAdditionalConfig ( schema : Schema ) : GenerateContentConfig {
8+ if ( schema instanceof ZodString ) {
9+ return { }
10+ }
811 let responseSchema = buildJsonSchema ( schema , false )
912 return {
1013 responseMimeType : 'application/json' ,
@@ -16,65 +19,41 @@ export function gemini(options: GoogleGenAIOptions): Provider {
1619 const client = new GoogleGenAI ( options )
1720 return {
1821 async query ( model , messages , schema , abortSignal ) {
19- if ( schema == null ) {
20- return query ( model , client , messages , abortSignal )
21- }
22- return query ( model , client , messages , abortSignal , buildAdditionalConfig ( schema ) )
22+ const additionalConfig = buildAdditionalConfig ( schema )
23+ const chat = client . chats . create ( {
24+ model,
25+ history : messages . slice ( 0 , - 1 ) . map ( ( { role, content } ) => ( {
26+ role : role === 'assistant' ? 'model' : role ,
27+ parts : content . map ( messageContentToPartUnion ) ,
28+ } ) ) ,
29+ } )
30+ const response = await chat . sendMessage ( {
31+ config : { abortSignal, ...additionalConfig } ,
32+ message : messages . at ( - 1 ) ! . content . map ( messageContentToPartUnion ) ,
33+ } )
34+ return response . text ?? ''
2335 } ,
2436 async * streamingQuery ( model , messages , schema , abortSignal ) {
25- if ( schema == null ) {
26- return streamingQueryOpenai ( model , client , messages , abortSignal )
37+ const additionalConfig = buildAdditionalConfig ( schema )
38+ const chat = client . chats . create ( {
39+ model,
40+ history : messages . slice ( 0 , - 1 ) . map ( ( { role, content } ) => ( {
41+ role : role === 'assistant' ? 'model' : role ,
42+ parts : content . map ( messageContentToPartUnion ) ,
43+ } ) ) ,
44+ } )
45+ const response = await chat . sendMessageStream ( {
46+ config : { abortSignal, ...additionalConfig } ,
47+ message : messages . at ( - 1 ) ! . content . map ( messageContentToPartUnion ) ,
48+ } )
49+
50+ for await ( const chunk of response ) {
51+ yield chunk . text ?? ''
2752 }
28- return streamingQueryOpenai ( model , client , messages , abortSignal , buildAdditionalConfig ( schema ) )
2953 } ,
3054 }
3155}
3256
33- export async function * streamingQueryOpenai (
34- model : string ,
35- client : GoogleGenAI ,
36- messages : Array < Message > ,
37- abortSignal : AbortSignal | undefined ,
38- additionalConfig ?: GenerateContentConfig ,
39- ) : AsyncIterable < string > {
40- const chat = client . chats . create ( {
41- model,
42- history : messages . slice ( 0 , - 1 ) . map ( ( { role, content } ) => ( {
43- role : role === 'assistant' ? 'model' : role ,
44- parts : content . map ( messageContentToPartUnion ) ,
45- } ) ) ,
46- } )
47- const response = await chat . sendMessageStream ( {
48- config : { abortSignal, ...additionalConfig } ,
49- message : messages . at ( - 1 ) ! . content . map ( messageContentToPartUnion ) ,
50- } )
51-
52- for await ( const chunk of response ) {
53- yield chunk . text ?? ''
54- }
55- }
56-
57- export async function query (
58- model : string ,
59- client : GoogleGenAI ,
60- messages : Array < Message > ,
61- abortSignal : AbortSignal | undefined ,
62- additionalConfig ?: GenerateContentConfig ,
63- ) : Promise < string > {
64- const chat = client . chats . create ( {
65- model,
66- history : messages . slice ( 0 , - 1 ) . map ( ( { role, content } ) => ( {
67- role : role === 'assistant' ? 'model' : role ,
68- parts : content . map ( messageContentToPartUnion ) ,
69- } ) ) ,
70- } )
71- const response = await chat . sendMessage ( {
72- config : { abortSignal, ...additionalConfig } ,
73- message : messages . at ( - 1 ) ! . content . map ( messageContentToPartUnion ) ,
74- } )
75- return response . text ?? ''
76- }
77-
7857function messageContentToPartUnion ( content : Message [ 'content' ] [ number ] ) : Part {
7958 switch ( content . type ) {
8059 case 'image_url' :
0 commit comments