@@ -87,7 +87,7 @@ func readMultilineInput(cmd *cobra.Command, scanner *bufio.Scanner) (string, err
8787}
8888
8989// generateInteractiveWithReadline provides an enhanced interactive mode with readline support
90- func generateInteractiveWithReadline (cmd * cobra.Command , desktopClient * desktop.Client , model string ) error {
90+ func generateInteractiveWithReadline (cmd * cobra.Command , client desktop.ChatClient , model string ) error {
9191 usage := func () {
9292 fmt .Fprintln (os .Stderr , "Available Commands:" )
9393 fmt .Fprintln (os .Stderr , " /bye Exit" )
@@ -141,7 +141,7 @@ func generateInteractiveWithReadline(cmd *cobra.Command, desktopClient *desktop.
141141 })
142142 if err != nil {
143143 // Fall back to basic input mode if readline initialization fails
144- return generateInteractiveBasic (cmd , desktopClient , model )
144+ return generateInteractiveBasic (cmd , client , model )
145145 }
146146
147147 // Disable history if the environment variable is set
@@ -245,7 +245,7 @@ func generateInteractiveWithReadline(cmd *cobra.Command, desktopClient *desktop.
245245 }
246246 }()
247247
248- err := chatWithMarkdownContext (chatCtx , cmd , desktopClient , model , userInput )
248+ err := chatWithMarkdownContext (chatCtx , cmd , client , model , userInput )
249249
250250 // Clean up signal handler
251251 signal .Stop (sigChan )
@@ -270,7 +270,7 @@ func generateInteractiveWithReadline(cmd *cobra.Command, desktopClient *desktop.
270270}
271271
272272// generateInteractiveBasic provides a basic interactive mode (fallback)
273- func generateInteractiveBasic (cmd * cobra.Command , desktopClient * desktop.Client , model string ) error {
273+ func generateInteractiveBasic (cmd * cobra.Command , client desktop.ChatClient , model string ) error {
274274 scanner := bufio .NewScanner (os .Stdin )
275275 for {
276276 userInput , err := readMultilineInput (cmd , scanner )
@@ -306,7 +306,7 @@ func generateInteractiveBasic(cmd *cobra.Command, desktopClient *desktop.Client,
306306 }
307307 }()
308308
309- err = chatWithMarkdownContext (chatCtx , cmd , desktopClient , model , userInput )
309+ err = chatWithMarkdownContext (chatCtx , cmd , client , model , userInput )
310310
311311 cancelChat ()
312312 signal .Stop (sigChan )
@@ -508,12 +508,12 @@ func renderMarkdown(content string) (string, error) {
508508}
509509
510510// chatWithMarkdown performs chat and streams the response with selective markdown rendering.
511- func chatWithMarkdown (cmd * cobra.Command , client * desktop.Client , model , prompt string ) error {
511+ func chatWithMarkdown (cmd * cobra.Command , client desktop.ChatClient , model , prompt string ) error {
512512 return chatWithMarkdownContext (cmd .Context (), cmd , client , model , prompt )
513513}
514514
515515// 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 {
516+ func chatWithMarkdownContext (ctx context.Context , cmd * cobra.Command , client desktop.ChatClient , model , prompt string ) error {
517517 colorMode , _ := cmd .Flags ().GetString ("color" )
518518 useMarkdown := shouldUseMarkdown (colorMode )
519519 debug , _ := cmd .Flags ().GetBool ("debug" )
@@ -571,6 +571,7 @@ func newRunCmd() *cobra.Command {
571571 var debug bool
572572 var colorMode string
573573 var detach bool
574+ var openaiURL string
574575
575576 const cmdArgs = "MODEL [PROMPT]"
576577 c := & cobra.Command {
@@ -617,6 +618,31 @@ func newRunCmd() *cobra.Command {
617618 }
618619 }
619620
621+ // Handle --openai flag: connect to external OpenAI-compatible endpoint
622+ if openaiURL != "" {
623+ if detach {
624+ return fmt .Errorf ("--detach flag cannot be used with --openai flag" )
625+ }
626+ openaiClient := desktop .NewOpenAIClient (openaiURL )
627+
628+ if prompt != "" {
629+ if err := chatWithMarkdown (cmd , openaiClient , model , prompt ); err != nil {
630+ return fmt .Errorf ("failed to generate a response: %w" , err )
631+ }
632+ cmd .Println ()
633+ return nil
634+ }
635+
636+ // Interactive mode
637+ if term .IsTerminal (int (os .Stdin .Fd ())) {
638+ termenv .SetDefaultOutput (
639+ termenv .NewOutput (asPrinter (cmd ), termenv .WithColorCache (true )),
640+ )
641+ return generateInteractiveWithReadline (cmd , openaiClient , model )
642+ }
643+ return generateInteractiveBasic (cmd , openaiClient , model )
644+ }
645+
620646 // Check if this is an NVIDIA NIM image
621647 if isNIMImage (model ) {
622648 // NIM images are handled differently - they run as Docker containers
@@ -733,6 +759,7 @@ func newRunCmd() *cobra.Command {
733759 c .Flags ().BoolVar (& debug , "debug" , false , "Enable debug logging" )
734760 c .Flags ().StringVar (& colorMode , "color" , "no" , "Use colored output (auto|yes|no)" )
735761 c .Flags ().BoolVarP (& detach , "detach" , "d" , false , "Load the model in the background without interaction" )
762+ c .Flags ().StringVar (& openaiURL , "openai" , "" , "Connect to an OpenAI-compatible endpoint URL" )
736763
737764 return c
738765}
0 commit comments