Enable streaming UI updates for chat responses

This commit is contained in:
2025-10-01 20:09:40 +02:00
parent 4271ee3d73
commit a3e6b105d0
5 changed files with 375 additions and 162 deletions

View File

@@ -18,14 +18,16 @@ import (
// App encapsulates the Goaichat application runtime wiring.
type App struct {
logger *slog.Logger
config *config.Config
openAI *openai.Client
chat *chat.Service
store *storage.Manager
repo *storage.Repository
input io.Reader
output io.Writer
logger *slog.Logger
config *config.Config
openAI *openai.Client
chat *chat.Service
store *storage.Manager
repo *storage.Repository
input io.Reader
output io.Writer
status string
streamBuffer strings.Builder
}
// New constructs a new App instance.
@@ -152,12 +154,10 @@ func (a *App) initStorage() error {
func (a *App) runCLILoop(ctx context.Context) error {
scanner := bufio.NewScanner(a.input)
if _, err := fmt.Fprintln(a.output, "Type your message. Use /exit to quit, /reset to clear history."); err != nil {
return err
}
a.setStatus("Type your message. Use /exit to quit, /reset to clear history.")
for {
if _, err := fmt.Fprint(a.output, "> "); err != nil {
if err := a.renderUI(); err != nil {
return err
}
@@ -169,12 +169,11 @@ func (a *App) runCLILoop(ctx context.Context) error {
}
input := scanner.Text()
a.setStatus("")
handled, exit, err := a.handleCommand(ctx, input)
if err != nil {
if _, writeErr := fmt.Fprintf(a.output, "Command error: %v\n", err); writeErr != nil {
return writeErr
}
a.setStatus("Command error: %v", err)
continue
}
if handled {
@@ -184,17 +183,25 @@ func (a *App) runCLILoop(ctx context.Context) error {
continue
}
reply, err := a.chat.Send(ctx, input)
if err != nil {
if _, writeErr := fmt.Fprintf(a.output, "Error: %v\n", err); writeErr != nil {
return writeErr
streamEnabled := a.chat != nil && a.chat.StreamingEnabled()
a.clearStreamingContent()
var handler openai.ChatCompletionStreamHandler
if streamEnabled {
handler = func(event openai.ChatCompletionStreamEvent) error {
if event.Done || event.Content == "" {
return nil
}
a.appendStreamingContent(event.Content)
return nil
}
}
if _, err := a.chat.Send(ctx, input, handler); err != nil {
a.setStatus("Error: %v", err)
continue
}
a.clearStreamingContent()
if _, err := fmt.Fprintf(a.output, "AI: %s\n", reply); err != nil {
return err
}
a.setStatus("")
if err := a.maybeSuggestSessionName(ctx); err != nil {
a.logger.WarnContext(ctx, "session name suggestion failed", "error", err)
@@ -213,66 +220,66 @@ func (a *App) handleCommand(ctx context.Context, input string) (handled bool, ex
if strings.HasPrefix(trimmed, "/rename") {
parts := strings.Fields(trimmed)
if len(parts) < 2 {
_, err := fmt.Fprintln(a.output, "Usage: /rename <session-name>")
return true, false, err
a.setStatus("Usage: /rename <session-name>")
return true, false, nil
}
rawName := strings.Join(parts[1:], " ")
normalized := chat.NormalizeSessionName(rawName)
if normalized == "" {
_, err := fmt.Fprintln(a.output, "Session name cannot be empty.")
return true, false, err
a.setStatus("Session name cannot be empty.")
return true, false, nil
}
if a.repo != nil {
existing, fetchErr := a.repo.GetSessionByName(ctx, normalized)
if fetchErr != nil {
_, err := fmt.Fprintf(a.output, "Failed to verify name availability: %v\n", fetchErr)
return true, false, err
a.setStatus("Failed to verify name availability: %v", fetchErr)
return true, false, nil
}
if existing != nil {
currentID := a.chat.CurrentSessionID()
if currentID == 0 || existing.ID != currentID {
_, err := fmt.Fprintf(a.output, "Session name %q is already in use.\n", normalized)
return true, false, err
a.setStatus("Session name %q is already in use.", normalized)
return true, false, nil
}
}
}
applied, setErr := a.chat.SetSessionName(ctx, rawName)
if setErr != nil {
_, err := fmt.Fprintf(a.output, "Failed to rename session: %v\n", setErr)
return true, false, err
a.setStatus("Failed to rename session: %v", setErr)
return true, false, nil
}
_, err := fmt.Fprintf(a.output, "Session renamed to %q.\n", applied)
return true, false, err
a.setStatus("Session renamed to %q.", applied)
return true, false, nil
}
if strings.HasPrefix(trimmed, "/open") {
parts := strings.Fields(trimmed)
if len(parts) != 2 {
_, err := fmt.Fprintln(a.output, "Usage: /open <session-name>")
return true, false, err
a.setStatus("Usage: /open <session-name>")
return true, false, nil
}
if a.repo == nil {
_, err := fmt.Fprintln(a.output, "Storage not initialised; cannot open sessions.")
return true, false, err
a.setStatus("Storage not initialised; cannot open sessions.")
return true, false, nil
}
session, fetchErr := a.repo.GetSessionByName(ctx, parts[1])
if fetchErr != nil {
_, err := fmt.Fprintf(a.output, "Failed to fetch session: %v\n", fetchErr)
return true, false, err
a.setStatus("Failed to fetch session: %v", fetchErr)
return true, false, nil
}
if session == nil {
_, err := fmt.Fprintf(a.output, "Session %q not found.\n", parts[1])
return true, false, err
a.setStatus("Session %q not found.", parts[1])
return true, false, nil
}
messages, msgErr := a.repo.GetMessages(ctx, session.ID)
if msgErr != nil {
_, err := fmt.Fprintf(a.output, "Failed to load messages: %v\n", msgErr)
return true, false, err
a.setStatus("Failed to load messages: %v", msgErr)
return true, false, nil
}
chatMessages := make([]openai.ChatMessage, 0, len(messages))
@@ -287,8 +294,8 @@ func (a *App) handleCommand(ctx context.Context, input string) (handled bool, ex
summaryPresent := session.Summary.Valid && strings.TrimSpace(session.Summary.String) != ""
a.chat.RestoreSession(session.ID, session.Name, chatMessages, summaryPresent)
_, err := fmt.Fprintf(a.output, "Loaded session %q with %d messages.\n", session.Name, len(chatMessages))
return true, false, err
a.setStatus("Loaded session %s with %d messages.", session.Name, len(chatMessages))
return true, false, nil
}
switch trimmed {
@@ -296,38 +303,38 @@ func (a *App) handleCommand(ctx context.Context, input string) (handled bool, ex
return true, true, nil
case "/reset":
a.chat.Reset()
_, err := fmt.Fprintln(a.output, "History cleared.")
return true, false, err
a.setStatus("History cleared.")
return true, false, nil
case "/list":
if a.repo == nil {
_, err := fmt.Fprintln(a.output, "History commands unavailable (storage not initialised).")
return true, false, err
a.setStatus("History commands unavailable (storage not initialised).")
return true, false, nil
}
sessions, listErr := a.repo.ListSessions(ctx)
if listErr != nil {
_, err := fmt.Fprintf(a.output, "Failed to list sessions: %v\n", listErr)
return true, false, err
a.setStatus("Failed to list sessions: %v", listErr)
return true, false, nil
}
if len(sessions) == 0 {
_, err := fmt.Fprintln(a.output, "No saved sessions.")
return true, false, err
a.setStatus("No saved sessions.")
return true, false, nil
}
var lines []string
for _, session := range sessions {
summary := session.Summary.String
if summary == "" {
summary = "(no summary)"
}
if _, err := fmt.Fprintf(a.output, "- %s [%s]: %s\n", session.Name, session.ModelName, summary); err != nil {
return true, false, err
}
lines = append(lines, fmt.Sprintf("- %s [%s]: %s", session.Name, session.ModelName, summary))
}
a.setStatus("%s", strings.Join(lines, "\n"))
return true, false, nil
case "/help":
_, err := fmt.Fprintln(a.output, "Commands: /exit, /reset, /list, /open <name>, /rename <name>, /help (more coming soon)")
return true, false, err
a.setStatus("Commands: /exit, /reset, /list, /open <name>, /rename <name>, /help (more coming soon)")
return true, false, nil
default:
_, err := fmt.Fprintf(a.output, "Unknown command %q. Try /help.\n", trimmed)
return true, false, err
a.setStatus("Unknown command %q. Try /help.", trimmed)
return true, false, nil
}
}
@@ -346,9 +353,120 @@ func (a *App) maybeSuggestSessionName(ctx context.Context) error {
}
a.chat.MarkSessionNameSuggested()
if _, err := fmt.Fprintf(a.output, "Suggested session name: %s\nUse /rename %s to apply it now.\n", suggestion, suggestion); err != nil {
a.setStatus("Suggested session name: %s\nUse /rename %s to apply it now.", suggestion, suggestion)
return nil
}
func (a *App) setStatus(msg string, args ...any) {
if a == nil {
return
}
if msg == "" {
a.status = ""
return
}
if len(args) == 0 {
a.status = msg
return
}
a.status = fmt.Sprintf(msg, args...)
}
func (a *App) renderUI() error {
if a == nil {
return errors.New("app is nil")
}
if _, err := fmt.Fprint(a.output, "\033[2J\033[H"); err != nil {
return err
}
sessionName := "(unnamed session)"
if a.chat != nil && strings.TrimSpace(a.chat.SessionName()) != "" {
sessionName = a.chat.SessionName()
}
title := fmt.Sprintf("goaichat - %s", sessionName)
underline := strings.Repeat("=", len(title))
if _, err := fmt.Fprintf(a.output, "%s\n%s\n\n", title, underline); err != nil {
return err
}
if _, err := fmt.Fprintln(a.output, "Conversation"); err != nil {
return err
}
if _, err := fmt.Fprintln(a.output, strings.Repeat("-", len("Conversation"))); err != nil {
return err
}
if a.chat != nil {
history := a.chat.History()
for _, msg := range history {
label := roleLabel(msg.Role)
if _, err := fmt.Fprintf(a.output, "%s: %s\n", label, msg.Content); err != nil {
return err
}
}
}
if a.streamBuffer.Len() > 0 {
if _, err := fmt.Fprintf(a.output, "AI: %s\n", a.streamBuffer.String()); err != nil {
return err
}
}
if _, err := fmt.Fprintln(a.output); err != nil {
return err
}
if status := strings.TrimSpace(a.status); status != "" {
if _, err := fmt.Fprintln(a.output, "Status"); err != nil {
return err
}
if _, err := fmt.Fprintln(a.output, strings.Repeat("-", len("Status"))); err != nil {
return err
}
if _, err := fmt.Fprintf(a.output, "%s\n\n", status); err != nil {
return err
}
}
if _, err := fmt.Fprint(a.output, "> "); err != nil {
return err
}
return nil
}
func roleLabel(role string) string {
switch role {
case "assistant":
return "AI"
case "user":
return "You"
case "system":
return "System"
default:
if strings.TrimSpace(role) == "" {
return "Unknown"
}
return strings.Title(role)
}
}
func (a *App) appendStreamingContent(chunk string) {
if a == nil || chunk == "" {
return
}
a.streamBuffer.WriteString(chunk)
if err := a.renderUI(); err != nil && a.logger != nil {
a.logger.Warn("stream render failed", "error", err)
}
}
func (a *App) clearStreamingContent() {
if a == nil {
return
}
a.streamBuffer.Reset()
}