This repository was archived by the owner on Oct 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: enhance chat interface #144
Draft
fiam
wants to merge
7
commits into
docker:main
Choose a base branch
from
fiam:chat-eye-candy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f41b2ec
feat: enhance chat interface with comprehensive UI improvements
fiam 239eafe
ui: use a single > for the prompt
fiam 01c3c68
fix: make ctrl-c clear the prompt without quitting
fiam 5f72781
fix: tests
fiam 26567c9
fix: keep previous ctrl-c behavior
fiam 13906b0
fix: avoid printing an extra ...
fiam 1c7b3fa
fix: simplify history handling, align it with typical shell behavior
fiam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Package history provides a command history stored inside the Docker CLI configuration. | ||
package history | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/docker/cli/cli/command" | ||
) | ||
|
||
const MaxHistoryLength = 100 | ||
|
||
type History struct { | ||
configPath string | ||
history []string | ||
} | ||
|
||
// New creates a new History instance and loads all previous history, if it exists. | ||
func New(cli *command.DockerCli) (*History, error) { | ||
dirname := filepath.Dir(cli.ConfigFile().Filename) | ||
p := filepath.Join(dirname, "model-cli", "history.txt") | ||
h := &History{configPath: p} | ||
if err := h.load(); err != nil { | ||
return nil, err | ||
} | ||
return h, nil | ||
} | ||
|
||
func (h *History) load() error { | ||
data, err := os.ReadFile(h.configPath) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
var history []string | ||
seen := make(map[string]bool) | ||
for line := range strings.SplitSeq(strings.TrimSuffix(string(data), "\n"), "\n") { | ||
if !seen[line] { | ||
history = append(history, line) | ||
seen[line] = true | ||
} | ||
} | ||
h.history = history | ||
return nil | ||
} | ||
|
||
// Append adds a new entry to the history and updates the history file. | ||
func (h *History) Append(question string) error { | ||
if strings.Contains(question, "\n") { | ||
return nil | ||
} | ||
|
||
h.history = append(h.history, question) | ||
if len(h.history) > MaxHistoryLength { | ||
h.history = h.history[len(h.history)-MaxHistoryLength:] | ||
} | ||
buf := strings.Join(h.history, "\n") | ||
|
||
if err := os.MkdirAll(filepath.Dir(h.configPath), 0700); err != nil { | ||
return err | ||
} | ||
|
||
if err := os.WriteFile(h.configPath+".tmp", []byte(buf), 0600); err != nil { | ||
return err | ||
} | ||
_ = os.Remove(h.configPath) | ||
return os.Rename(h.configPath+".tmp", h.configPath) | ||
} | ||
|
||
// Suggestions returns a list of suggested inputs based on the current input. | ||
func (h *History) Suggestions(text string) []string { | ||
var suggestions []string | ||
|
||
text = strings.ToLower(text) | ||
for _, line := range h.history { | ||
if strings.HasPrefix(strings.ToLower(line), text) { | ||
suggestions = append(suggestions, line) | ||
} | ||
} | ||
|
||
return suggestions | ||
} | ||
|
||
// Previous returns the previous input in the history based on the current input and cursor position. | ||
func (h *History) Previous(text string, cursorPosition int, from int) (int, string) { | ||
n := len(h.history) | ||
text = strings.ToLower(text[0:cursorPosition]) | ||
for dec := range n - 1 { | ||
index := mod(from-dec-1, n) | ||
line := h.history[index] | ||
if strings.HasPrefix(strings.ToLower(line), text) { | ||
return index, line | ||
} | ||
} | ||
return from, text | ||
} | ||
|
||
// Next returns the next input in the history based on the current input and cursor position. | ||
func (h *History) Next(text string, cursorPosition int, from int) (int, string) { | ||
n := len(h.history) | ||
text = strings.ToLower(text[0:cursorPosition]) | ||
for inc := range n - 1 { | ||
index := mod(from+inc+1, n) | ||
line := h.history[index] | ||
if strings.HasPrefix(strings.ToLower(line), text) { | ||
return index, line | ||
} | ||
} | ||
return from, text | ||
} | ||
|
||
func mod(a, b int) int { | ||
return (a%b + b) % b | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should discuss where we want to store this, because we also have chat history as a near-term roadmap item for the GUI, and I think it would be nice if we could share chat output between them. I get that this is more of a
readline()
-style history, but I think we should discuss with @mia-docker and @qodesmith about how we want the two to interact.