@@ -133,7 +133,9 @@ describe("convertMessagesToGeminiContents", () => {
133133
134134 const contents = convertMessagesToGeminiContents ( messages ) ;
135135
136- const toolResponseContent = contents . find ( ( c ) => c . role === "function" ) ;
136+ const toolResponseContent = contents . find (
137+ ( c ) => c . role === "user" && c . parts . some ( ( p ) => "functionResponse" in p )
138+ ) ;
137139 expect ( toolResponseContent ) . toBeDefined ( ) ;
138140
139141 const functionResponsePart = toolResponseContent ! . parts ! . find (
@@ -168,7 +170,9 @@ describe("convertMessagesToGeminiContents", () => {
168170
169171 const contents = convertMessagesToGeminiContents ( messages ) ;
170172
171- const toolResponseContent = contents . find ( ( c ) => c . role === "function" ) ;
173+ const toolResponseContent = contents . find (
174+ ( c ) => c . role === "user" && c . parts . some ( ( p ) => "functionResponse" in p )
175+ ) ;
172176 expect ( toolResponseContent ) . toBeDefined ( ) ;
173177
174178 const functionResponsePart = toolResponseContent ! . parts . find (
@@ -213,7 +217,9 @@ describe("convertMessagesToGeminiContents", () => {
213217
214218 const contents = convertMessagesToGeminiContents ( messages ) ;
215219
216- const toolResponseContents = contents . filter ( ( c ) => c . role === "function" ) ;
220+ const toolResponseContents = contents . filter (
221+ ( c ) => c . role === "user" && c . parts . some ( ( p ) => "functionResponse" in p )
222+ ) ;
217223 expect ( toolResponseContents ) . toHaveLength ( 1 ) ;
218224
219225 const parts = toolResponseContents [ 0 ] . parts . filter (
@@ -260,13 +266,13 @@ describe("convertMessagesToGeminiContents", () => {
260266
261267 const contents = convertMessagesToGeminiContents ( messages ) ;
262268
263- // Should produce: user, model (functionCall parts), function (single merged turn)
269+ // Should produce: user, model (functionCall parts), user (single merged turn with functionResponses )
264270 expect ( contents ) . toHaveLength ( 3 ) ;
265271
266272 expect ( contents [ 1 ] . role ) . toBe ( "model" ) ;
267273
268274 const functionTurn = contents [ 2 ] ;
269- expect ( functionTurn . role ) . toBe ( "function " ) ;
275+ expect ( functionTurn . role ) . toBe ( "user " ) ;
270276 expect ( functionTurn . parts ) . toHaveLength ( 2 ) ;
271277
272278 const responses = functionTurn . parts . filter (
@@ -301,7 +307,9 @@ describe("convertMessagesToGeminiContents", () => {
301307
302308 const contents = convertMessagesToGeminiContents ( messages ) ;
303309
304- const toolResponseContent = contents . find ( ( c ) => c . role === "function" ) ;
310+ const toolResponseContent = contents . find (
311+ ( c ) => c . role === "user" && c . parts . some ( ( p ) => "functionResponse" in p )
312+ ) ;
305313 const functionResponsePart = toolResponseContent ! . parts . find (
306314 ( p ) => "functionResponse" in p && p . functionResponse
307315 ) as Gemini . Part . FunctionResponse ;
@@ -402,7 +410,9 @@ describe("convertMessagesToGeminiContents", () => {
402410
403411 const contents = convertMessagesToGeminiContents ( messages ) ;
404412
405- const toolResponseContent = contents . find ( ( c ) => c . role === "function" ) ;
413+ const toolResponseContent = contents . find (
414+ ( c ) => c . role === "user" && c . parts . some ( ( p ) => "functionResponse" in p )
415+ ) ;
406416 expect ( toolResponseContent ) . toBeDefined ( ) ;
407417
408418 const functionResponsePart = toolResponseContent ! . parts . find (
@@ -448,8 +458,10 @@ describe("convertMessagesToGeminiContents", () => {
448458
449459 const contents = convertMessagesToGeminiContents ( messages ) ;
450460
451- // Consecutive ToolMessages with the same "function" role are merged into one content
452- const toolResponseContents = contents . filter ( ( c ) => c . role === "function" ) ;
461+ // Consecutive ToolMessages with the same "user" role are merged into one content
462+ const toolResponseContents = contents . filter (
463+ ( c ) => c . role === "user" && c . parts . some ( ( p ) => "functionResponse" in p )
464+ ) ;
453465 expect ( toolResponseContents ) . toHaveLength ( 1 ) ;
454466
455467 const mergedParts = toolResponseContents [ 0 ] . parts . filter (
@@ -484,7 +496,9 @@ describe("convertMessagesToGeminiContents", () => {
484496
485497 const contents = convertMessagesToGeminiContents ( messages ) ;
486498
487- const toolResponseContent = contents . find ( ( c ) => c . role === "function" ) ;
499+ const toolResponseContent = contents . find (
500+ ( c ) => c . role === "user" && c . parts . some ( ( p ) => "functionResponse" in p )
501+ ) ;
488502 expect ( toolResponseContent ) . toBeDefined ( ) ;
489503
490504 const functionResponsePart = toolResponseContent ! . parts ! . find (
@@ -520,7 +534,9 @@ describe("convertMessagesToGeminiContents", () => {
520534
521535 const contents = convertMessagesToGeminiContents ( messages ) ;
522536
523- const toolResponseContent = contents . find ( ( c ) => c . role === "function" ) ;
537+ const toolResponseContent = contents . find (
538+ ( c ) => c . role === "user" && c . parts . some ( ( p ) => "functionResponse" in p )
539+ ) ;
524540 expect ( toolResponseContent ) . toBeDefined ( ) ;
525541
526542 const functionResponsePart = toolResponseContent ! . parts ! . find (
@@ -557,7 +573,9 @@ describe("convertMessagesToGeminiContents", () => {
557573
558574 const contents = convertMessagesToGeminiContents ( messages ) ;
559575
560- const toolResponseContent = contents . find ( ( c ) => c . role === "function" ) ;
576+ const toolResponseContent = contents . find (
577+ ( c ) => c . role === "user" && c . parts . some ( ( p ) => "functionResponse" in p )
578+ ) ;
561579 expect ( toolResponseContent ) . toBeDefined ( ) ;
562580
563581 const functionResponsePart = toolResponseContent ! . parts ! . find (
@@ -569,7 +587,6 @@ describe("convertMessagesToGeminiContents", () => {
569587 . id
570588 ) . toBeUndefined ( ) ;
571589 } ) ;
572- < < < << << HEAD
573590
574591 test ( "v1 contentBlocks: text-plain block produces fileData part" , ( ) => {
575592 const messages = [
@@ -723,6 +740,4 @@ describe("convertMessagesToGeminiContents", () => {
723740 ( userContent ! . parts [ 3 ] as Gemini . Part . FileData ) . fileData ! . fileUri
724741 ) . toBe ( "gs://bucket/report.pdf" ) ;
725742 } ) ;
726- === === =
727- >>> >>> > 968 b70618 ( Have generated IDs use a known pattern so they can be removed when being sent back to Gemini , which is not expecting them . )
728743} ) ;
0 commit comments