@@ -5,6 +5,7 @@ import type {
5
5
Conversation ,
6
6
ConversationsService ,
7
7
SomeMessage ,
8
+ ToolMessage ,
8
9
} from "mongodb-rag-core" ;
9
10
import { type AppConfig } from "../../app" ;
10
11
import {
@@ -386,15 +387,66 @@ describe("POST /responses", () => {
386
387
387
388
it ( "Should store function_call messages when `store: true`" , async ( ) => {
388
389
const store = true ;
389
- const functionCallType = "function_call" ;
390
+ const functionCallName = "my_function" ;
391
+ const functionCallArguments = `{"query": "value"}` ;
390
392
const functionCallOutputType = "function_call_output" ;
393
+ const functionCallOutput = `{"result": "success"}` ;
391
394
const requestBody : Partial < CreateResponseRequest [ "body" ] > = {
392
395
store,
393
396
input : [
394
397
{
395
- type : functionCallType ,
398
+ type : "function_call" ,
396
399
call_id : "call123" ,
397
- name : "my_function" ,
400
+ name : functionCallName ,
401
+ arguments : functionCallArguments ,
402
+ status : "in_progress" ,
403
+ } ,
404
+ {
405
+ type : functionCallOutputType ,
406
+ call_id : "call123" ,
407
+ output : functionCallOutput ,
408
+ status : "completed" ,
409
+ } ,
410
+ {
411
+ role : "user" ,
412
+ content : "What is MongoDB?" ,
413
+ } ,
414
+ ] ,
415
+ } ;
416
+ const stream = await makeClientAndRequest ( requestBody ) ;
417
+
418
+ const results = await expectValidResponses ( { requestBody, stream } ) ;
419
+
420
+ const updatedConversation = await conversations . findByMessageId ( {
421
+ messageId : getMessageIdFromResults ( results ) ,
422
+ } ) ;
423
+ if ( ! updatedConversation ) {
424
+ return expect ( updatedConversation ) . not . toBeNull ( ) ;
425
+ }
426
+ const messages = updatedConversation . messages as Array < ToolMessage > ;
427
+
428
+ expect ( updatedConversation . storeMessageContent ) . toEqual ( store ) ;
429
+
430
+ expect ( messages [ 0 ] . role ) . toEqual ( "tool" ) ;
431
+ expect ( messages [ 0 ] . name ) . toEqual ( functionCallName ) ;
432
+ expect ( messages [ 0 ] . content ) . toEqual ( functionCallArguments ) ;
433
+
434
+ expect ( messages [ 1 ] . role ) . toEqual ( "tool" ) ;
435
+ expect ( messages [ 1 ] . name ) . toEqual ( functionCallOutputType ) ;
436
+ expect ( messages [ 1 ] . content ) . toEqual ( functionCallOutput ) ;
437
+ } ) ;
438
+
439
+ it ( "Should not store function_call message content when `store: false`" , async ( ) => {
440
+ const store = false ;
441
+ const functionCallName = "my_function" ;
442
+ const functionCallOutputType = "function_call_output" ;
443
+ const requestBody : Partial < CreateResponseRequest [ "body" ] > = {
444
+ store,
445
+ input : [
446
+ {
447
+ type : "function_call" ,
448
+ call_id : "call123" ,
449
+ name : functionCallName ,
398
450
arguments : `{"query": "value"}` ,
399
451
status : "in_progress" ,
400
452
} ,
@@ -420,16 +472,17 @@ describe("POST /responses", () => {
420
472
if ( ! updatedConversation ) {
421
473
return expect ( updatedConversation ) . not . toBeNull ( ) ;
422
474
}
475
+ const messages = updatedConversation . messages as Array < ToolMessage > ;
423
476
424
477
expect ( updatedConversation . storeMessageContent ) . toEqual ( store ) ;
425
478
426
- expect ( updatedConversation . messages [ 0 ] . role ) . toEqual ( "system" ) ;
427
- expect ( updatedConversation . messages [ 0 ] . content ) . toEqual ( functionCallType ) ;
479
+ expect ( messages [ 0 ] . role ) . toEqual ( "tool" ) ;
480
+ expect ( messages [ 0 ] . name ) . toEqual ( functionCallName ) ;
481
+ expect ( messages [ 0 ] . content ) . toEqual ( "" ) ;
428
482
429
- expect ( updatedConversation . messages [ 1 ] . role ) . toEqual ( "system" ) ;
430
- expect ( updatedConversation . messages [ 1 ] . content ) . toEqual (
431
- functionCallOutputType
432
- ) ;
483
+ expect ( messages [ 1 ] . role ) . toEqual ( "tool" ) ;
484
+ expect ( messages [ 1 ] . name ) . toEqual ( functionCallOutputType ) ;
485
+ expect ( messages [ 1 ] . content ) . toEqual ( "" ) ;
433
486
} ) ;
434
487
} ) ;
435
488
0 commit comments