@@ -482,30 +482,33 @@ func handleTextResponse(resp *TextResponse) map[string]any {
482482}
483483
484484func handleSchemaResponse (resp * SchemaResponse ) map [string ]any {
485+ res := make (map [string ]any )
485486 if resp .Query != nil {
486- return map [ string ] any { "Question" : resp .Query .Question }
487+ res [ "Question" ] = resp .Query .Question
487488 }
488489 if resp .Result != nil {
489490 var formattedSources []map [string ]any
490491 for _ , ds := range resp .Result .Datasources {
491492 formattedSources = append (formattedSources , formatDatasourceAsDict (& ds ))
492493 }
493- return map [ string ] any { "Schema Resolved" : formattedSources }
494+ res [ "Schema Resolved" ] = formattedSources
494495 }
495- return nil
496+ if len (res ) == 0 {
497+ return nil
498+ }
499+ return res
496500}
497501
498502func handleDataResponse (resp * DataResponse , maxRows int ) map [string ]any {
503+ res := make (map [string ]any )
499504 if resp .Query != nil {
500- return map [string ]any {
501- "Retrieval Query" : map [string ]any {
502- "Query Name" : resp .Query .Name ,
503- "Question" : resp .Query .Question ,
504- },
505+ res ["Retrieval Query" ] = map [string ]any {
506+ "Query Name" : resp .Query .Name ,
507+ "Question" : resp .Query .Question ,
505508 }
506509 }
507510 if resp .GeneratedSQL != "" {
508- return map [ string ] any { "SQL Generated" : resp .GeneratedSQL }
511+ res [ "SQL Generated" ] = resp .GeneratedSQL
509512 }
510513 if resp .Result != nil {
511514 var headers []string
@@ -533,15 +536,16 @@ func handleDataResponse(resp *DataResponse, maxRows int) map[string]any {
533536 summary = fmt .Sprintf ("Showing the first %d of %d total rows." , numRowsToDisplay , totalRows )
534537 }
535538
536- return map [string ]any {
537- "Data Retrieved" : map [string ]any {
538- "headers" : headers ,
539- "rows" : compactRows ,
540- "summary" : summary ,
541- },
539+ res ["Data Retrieved" ] = map [string ]any {
540+ "headers" : headers ,
541+ "rows" : compactRows ,
542+ "summary" : summary ,
542543 }
543544 }
544- return nil
545+ if len (res ) == 0 {
546+ return nil
547+ }
548+ return res
545549}
546550
547551func handleError (resp * ErrorResponse ) map [string ]any {
@@ -557,9 +561,17 @@ func appendMessage(messages []map[string]any, newMessage map[string]any) []map[s
557561 if newMessage == nil {
558562 return messages
559563 }
560- if len (messages ) > 0 {
561- if _ , ok := messages [len (messages )- 1 ]["Data Retrieved" ]; ok {
562- messages = messages [:len (messages )- 1 ]
564+
565+ if _ , hasData := newMessage ["Data Retrieved" ]; hasData {
566+ // Only keep the last data result while preserving SQL and other metadata.
567+ for i := len (messages ) - 1 ; i >= 0 ; i -- {
568+ if _ , ok := messages [i ]["Data Retrieved" ]; ok {
569+ delete (messages [i ], "Data Retrieved" )
570+ if len (messages [i ]) == 0 {
571+ messages = append (messages [:i ], messages [i + 1 :]... )
572+ }
573+ break
574+ }
563575 }
564576 }
565577 return append (messages , newMessage )
0 commit comments