@@ -154,6 +154,7 @@ func generateInteractiveWithReadline(cmd *cobra.Command, desktopClient *desktop.
154154
155155 var sb strings.Builder
156156 var multiline bool
157+ var conversationHistory []desktop.OpenAIChatMessage
157158
158159 // Add a helper function to handle file inclusion when @ is pressed
159160 // We'll implement a basic version here that shows a message when @ is pressed
@@ -245,7 +246,7 @@ func generateInteractiveWithReadline(cmd *cobra.Command, desktopClient *desktop.
245246 }
246247 }()
247248
248- err := chatWithMarkdownContext (chatCtx , cmd , desktopClient , model , userInput )
249+ assistantResponse , err := chatWithMarkdownContext (chatCtx , cmd , desktopClient , model , userInput , conversationHistory )
249250
250251 // Clean up signal handler
251252 signal .Stop (sigChan )
@@ -263,6 +264,16 @@ func generateInteractiveWithReadline(cmd *cobra.Command, desktopClient *desktop.
263264 continue
264265 }
265266
267+ // Add the user message and assistant response to conversation history
268+ conversationHistory = append (conversationHistory , desktop.OpenAIChatMessage {
269+ Role : "user" ,
270+ Content : userInput ,
271+ })
272+ conversationHistory = append (conversationHistory , desktop.OpenAIChatMessage {
273+ Role : "assistant" ,
274+ Content : assistantResponse ,
275+ })
276+
266277 cmd .Println ()
267278 sb .Reset ()
268279 }
@@ -272,6 +283,8 @@ func generateInteractiveWithReadline(cmd *cobra.Command, desktopClient *desktop.
272283// generateInteractiveBasic provides a basic interactive mode (fallback)
273284func generateInteractiveBasic (cmd * cobra.Command , desktopClient * desktop.Client , model string ) error {
274285 scanner := bufio .NewScanner (os .Stdin )
286+ var conversationHistory []desktop.OpenAIChatMessage
287+
275288 for {
276289 userInput , err := readMultilineInput (cmd , scanner )
277290 if err != nil {
@@ -306,7 +319,7 @@ func generateInteractiveBasic(cmd *cobra.Command, desktopClient *desktop.Client,
306319 }
307320 }()
308321
309- err = chatWithMarkdownContext (chatCtx , cmd , desktopClient , model , userInput )
322+ assistantResponse , err : = chatWithMarkdownContext (chatCtx , cmd , desktopClient , model , userInput , conversationHistory )
310323
311324 cancelChat ()
312325 signal .Stop (sigChan )
@@ -322,6 +335,16 @@ func generateInteractiveBasic(cmd *cobra.Command, desktopClient *desktop.Client,
322335 continue
323336 }
324337
338+ // Add the user message and assistant response to conversation history
339+ conversationHistory = append (conversationHistory , desktop.OpenAIChatMessage {
340+ Role : "user" ,
341+ Content : userInput ,
342+ })
343+ conversationHistory = append (conversationHistory , desktop.OpenAIChatMessage {
344+ Role : "assistant" ,
345+ Content : assistantResponse ,
346+ })
347+
325348 cmd .Println ()
326349 }
327350 return nil
@@ -509,40 +532,42 @@ func renderMarkdown(content string) (string, error) {
509532
510533// chatWithMarkdown performs chat and streams the response with selective markdown rendering.
511534func chatWithMarkdown (cmd * cobra.Command , client * desktop.Client , model , prompt string ) error {
512- return chatWithMarkdownContext (cmd .Context (), cmd , client , model , prompt )
535+ _ , err := chatWithMarkdownContext (cmd .Context (), cmd , client , model , prompt , nil )
536+ return err
513537}
514538
515539// chatWithMarkdownContext performs chat with context support and streams the response with selective markdown rendering.
516- func chatWithMarkdownContext (ctx context.Context , cmd * cobra.Command , client * desktop.Client , model , prompt string ) error {
540+ // It accepts an optional conversation history and returns the assistant's response for history tracking.
541+ func chatWithMarkdownContext (ctx context.Context , cmd * cobra.Command , client * desktop.Client , model , prompt string , conversationHistory []desktop.OpenAIChatMessage ) (string , error ) {
517542 colorMode , _ := cmd .Flags ().GetString ("color" )
518543 useMarkdown := shouldUseMarkdown (colorMode )
519544 debug , _ := cmd .Flags ().GetBool ("debug" )
520545
521546 // Process file inclusions first (files referenced with @ symbol)
522547 prompt , err := processFileInclusions (prompt )
523548 if err != nil {
524- return fmt .Errorf ("failed to process file inclusions: %w" , err )
549+ return "" , fmt .Errorf ("failed to process file inclusions: %w" , err )
525550 }
526551
527552 var imageURLs []string
528553 cleanedPrompt , imgs , err := processImagesInPrompt (prompt )
529554 if err != nil {
530- return fmt .Errorf ("failed to process images: %w" , err )
555+ return "" , fmt .Errorf ("failed to process images: %w" , err )
531556 }
532557 prompt = cleanedPrompt
533558 imageURLs = imgs
534559
535560 if ! useMarkdown {
536561 // Simple case: just stream as plain text
537- return client .ChatWithContext (ctx , model , prompt , imageURLs , func (content string ) {
562+ return client .ChatWithMessagesContext (ctx , model , conversationHistory , prompt , imageURLs , func (content string ) {
538563 cmd .Print (content )
539564 }, false )
540565 }
541566
542567 // For markdown: use streaming buffer to render code blocks as they complete
543568 markdownBuffer := NewStreamingMarkdownBuffer ()
544569
545- err = client .ChatWithContext (ctx , model , prompt , imageURLs , func (content string ) {
570+ assistantResponse , err : = client .ChatWithMessagesContext (ctx , model , conversationHistory , prompt , imageURLs , func (content string ) {
546571 // Use the streaming markdown buffer to intelligently render content
547572 rendered , err := markdownBuffer .AddContent (content , true )
548573 if err != nil {
@@ -556,15 +581,15 @@ func chatWithMarkdownContext(ctx context.Context, cmd *cobra.Command, client *de
556581 }
557582 }, true )
558583 if err != nil {
559- return err
584+ return assistantResponse , err
560585 }
561586
562587 // Flush any remaining content from the markdown buffer
563588 if remaining , flushErr := markdownBuffer .Flush (true ); flushErr == nil && remaining != "" {
564589 cmd .Print (remaining )
565590 }
566591
567- return nil
592+ return assistantResponse , nil
568593}
569594
570595func newRunCmd () * cobra.Command {
0 commit comments