Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.
cmd: enable multi-line input and shift enter (#13694)
Parth Sareen75d7b5f92684
diff --git a/cmd/interactive.go b/cmd/interactive.goindex 9c4e32a2e..aad3eccfc 100644--- a/cmd/interactive.go+++ b/cmd/interactive.go@@ -116,7 +116,7 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error {Prompt: ">>> ",AltPrompt: "... ",Placeholder: "Send a message (/? for help)",- AltPlaceholder: `Use """ to end multi-line input`,+ AltPlaceholder: "Press Enter to send",})if err != nil {return errdiff --git a/readline/readline.go b/readline/readline.goindex c12327472..e18a25648 100644--- a/readline/readline.go+++ b/readline/readline.go@@ -5,6 +5,7 @@ import ("fmt""io""os"+ "strings")type Prompt struct {@@ -36,10 +37,11 @@ type Terminal struct {}type Instance struct {- Prompt *Prompt- Terminal *Terminal- History *History- Pasting bool+ Prompt *Prompt+ Terminal *Terminal+ History *History+ Pasting bool+ pastedLines []string}func New(prompt Prompt) (*Instance, error) {@@ -174,6 +176,8 @@ func (i *Instance) Readline() (string, error) {case CharEsc:esc = truecase CharInterrupt:+ i.pastedLines = nil+ i.Prompt.UseAlt = falsereturn "", ErrInterruptcase CharPrev:i.historyPrev(buf, ¤tLineBuf)@@ -188,7 +192,23 @@ func (i *Instance) Readline() (string, error) {case CharForward:buf.MoveRight()case CharBackspace, CharCtrlH:- buf.Remove()+ if buf.IsEmpty() && len(i.pastedLines) > 0 {+ lastIdx := len(i.pastedLines) - 1+ prevLine := i.pastedLines[lastIdx]+ i.pastedLines = i.pastedLines[:lastIdx]+ fmt.Print(CursorBOL + ClearToEOL + CursorUp + CursorBOL + ClearToEOL)+ if len(i.pastedLines) == 0 {+ fmt.Print(i.Prompt.Prompt)+ i.Prompt.UseAlt = false+ } else {+ fmt.Print(i.Prompt.AltPrompt)+ }+ for _, r := range prevLine {+ buf.Add(r)+ }+ } else {+ buf.Remove()+ }case CharTab:// todo: convert back to real tabsfor range 8 {@@ -211,13 +231,28 @@ func (i *Instance) Readline() (string, error) {case CharCtrlZ:fd := os.Stdin.Fd()return handleCharCtrlZ(fd, i.Terminal.termios)- case CharEnter, CharCtrlJ:+ case CharCtrlJ:+ i.pastedLines = append(i.pastedLines, buf.String())+ buf.Buf.Clear()+ buf.Pos = 0+ buf.DisplayPos = 0+ buf.LineHasSpace.Clear()+ fmt.Println()+ fmt.Print(i.Prompt.AltPrompt)+ i.Prompt.UseAlt = true+ continue+ case CharEnter:output := buf.String()+ if len(i.pastedLines) > 0 {+ output = strings.Join(i.pastedLines, "\n") + "\n" + output+ i.pastedLines = nil+ }if output != "" {i.History.Add(output)}buf.MoveToEnd()fmt.Println()+ i.Prompt.UseAlt = falsereturn output, nildefault:diff --git a/x/cmd/run.go b/x/cmd/run.goindex f32254ebe..1bd452cd8 100644--- a/x/cmd/run.go+++ b/x/cmd/run.go@@ -25,14 +25,6 @@ import ("github.com/ollama/ollama/x/tools")-// MultilineState tracks the state of multiline input-type MultilineState int--const (- MultilineNone MultilineState = iota- MultilineSystem-)-// Tool output capping constantsconst (// localModelTokenLimit is the token limit for local models (smaller context).@@ -656,7 +648,7 @@ func GenerateInteractive(cmd *cobra.Command, modelName string, wordWrap bool, opPrompt: ">>> ",AltPrompt: "... ",Placeholder: "Send a message (/? for help)",- AltPlaceholder: `Use """ to end multi-line input`,+ AltPlaceholder: "Press Enter to send",})if err != nil {return err@@ -707,7 +699,6 @@ func GenerateInteractive(cmd *cobra.Command, modelName string, wordWrap bool, opvar sb strings.Buildervar format stringvar system string- var multiline MultilineState = MultilineNonefor {line, err := scanner.Readline()@@ -721,37 +712,12 @@ func GenerateInteractive(cmd *cobra.Command, modelName string, wordWrap bool, op}scanner.Prompt.UseAlt = falsesb.Reset()- multiline = MultilineNonecontinuecase err != nil:return err}switch {- case multiline != MultilineNone:- // check if there's a multiline terminating string- before, ok := strings.CutSuffix(line, `"""`)- sb.WriteString(before)- if !ok {- fmt.Fprintln(&sb)- continue- }-- switch multiline {- case MultilineSystem:- system = sb.String()- newMessage := api.Message{Role: "system", Content: system}- if len(messages) > 0 && messages[len(messages)-1].Role == "system" {- messages[len(messages)-1] = newMessage- } else {- messages = append(messages, newMessage)- }- fmt.Println("Set system message.")- sb.Reset()- }-- multiline = MultilineNone- scanner.Prompt.UseAlt = falsecase strings.HasPrefix(line, "/exit"), strings.HasPrefix(line, "/bye"):return nilcase strings.HasPrefix(line, "/clear"):@@ -860,41 +826,18 @@ func GenerateInteractive(cmd *cobra.Command, modelName string, wordWrap bool, opoptions[args[2]] = fp[args[2]]case "system":if len(args) < 3 {- fmt.Println("Usage: /set system <message> or /set system \"\"\"<multi-line message>\"\"\"")- continue- }-- multiline = MultilineSystem-- line := strings.Join(args[2:], " ")- line, ok := strings.CutPrefix(line, `"""`)- if !ok {- multiline = MultilineNone- } else {- // only cut suffix if the line is multiline- line, ok = strings.CutSuffix(line, `"""`)- if ok {- multiline = MultilineNone- }- }-- sb.WriteString(line)- if multiline != MultilineNone {- scanner.Prompt.UseAlt = true+ fmt.Println("Usage: /set system <message>")continue}- system = sb.String()- newMessage := api.Message{Role: "system", Content: sb.String()}- // Check if the slice is not empty and the last message is from 'system'+ system = strings.Join(args[2:], " ")+ newMessage := api.Message{Role: "system", Content: system}if len(messages) > 0 && messages[len(messages)-1].Role == "system" {- // Replace the last messagemessages[len(messages)-1] = newMessage} else {messages = append(messages, newMessage)}fmt.Println("Set system message.")- sb.Reset()continuedefault:fmt.Printf("Unknown command '/set %s'. Type /? for help\n", args[1])@@ -1081,7 +1024,7 @@ func GenerateInteractive(cmd *cobra.Command, modelName string, wordWrap bool, opsb.WriteString(line)}- if sb.Len() > 0 && multiline == MultilineNone {+ if sb.Len() > 0 {newMessage := api.Message{Role: "user", Content: sb.String()}messages = append(messages, newMessage)