Add session naming workflow and rename command
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/stig/goaichat/internal/chat"
|
||||
"github.com/stig/goaichat/internal/config"
|
||||
"github.com/stig/goaichat/internal/openai"
|
||||
"github.com/stig/goaichat/internal/storage"
|
||||
)
|
||||
|
||||
// App encapsulates the Goaichat application runtime wiring.
|
||||
@@ -21,6 +22,8 @@ type App struct {
|
||||
config *config.Config
|
||||
openAI *openai.Client
|
||||
chat *chat.Service
|
||||
store *storage.Manager
|
||||
repo *storage.Repository
|
||||
input io.Reader
|
||||
output io.Writer
|
||||
}
|
||||
@@ -71,6 +74,9 @@ func (a *App) Run(ctx context.Context) error {
|
||||
if err := a.initOpenAIClient(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.initStorage(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.initChatService(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -108,7 +114,7 @@ func (a *App) initChatService() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
service, err := chat.NewService(a.logger.With("component", "chat"), a.config.Model, a.openAI)
|
||||
service, err := chat.NewService(a.logger.With("component", "chat"), a.config.Model, a.openAI, a.repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -117,6 +123,32 @@ func (a *App) initChatService() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) initStorage() error {
|
||||
if a.store != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
manager, err := storage.NewManager(*a.config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
db, err := manager.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
repo, err := storage.NewRepository(db)
|
||||
if err != nil {
|
||||
_ = manager.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
a.store = manager
|
||||
a.repo = repo
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) runCLILoop(ctx context.Context) error {
|
||||
scanner := bufio.NewScanner(a.input)
|
||||
|
||||
@@ -163,11 +195,14 @@ func (a *App) runCLILoop(ctx context.Context) error {
|
||||
if _, err := fmt.Fprintf(a.output, "AI: %s\n", reply); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := a.maybeSuggestSessionName(ctx); err != nil {
|
||||
a.logger.WarnContext(ctx, "session name suggestion failed", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) handleCommand(ctx context.Context, input string) (handled bool, exit bool, err error) {
|
||||
_ = ctx
|
||||
trimmed := strings.TrimSpace(input)
|
||||
if trimmed == "" {
|
||||
return true, false, errors.New("no input provided")
|
||||
@@ -175,6 +210,86 @@ func (a *App) handleCommand(ctx context.Context, input string) (handled bool, ex
|
||||
if !strings.HasPrefix(trimmed, "/") {
|
||||
return false, false, nil
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
_, err := fmt.Fprintf(a.output, "Session renamed to %q.\n", applied)
|
||||
return true, false, err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
if a.repo == nil {
|
||||
_, err := fmt.Fprintln(a.output, "Storage not initialised; cannot open sessions.")
|
||||
return true, false, err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
if session == nil {
|
||||
_, err := fmt.Fprintf(a.output, "Session %q not found.\n", parts[1])
|
||||
return true, false, err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
chatMessages := make([]openai.ChatMessage, 0, len(messages))
|
||||
for _, message := range messages {
|
||||
role := strings.TrimSpace(message.Role)
|
||||
if role == "" {
|
||||
continue
|
||||
}
|
||||
chatMessages = append(chatMessages, openai.ChatMessage{Role: role, Content: message.Content})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
switch trimmed {
|
||||
case "/exit":
|
||||
@@ -183,11 +298,57 @@ func (a *App) handleCommand(ctx context.Context, input string) (handled bool, ex
|
||||
a.chat.Reset()
|
||||
_, err := fmt.Fprintln(a.output, "History cleared.")
|
||||
return true, false, err
|
||||
case "/list":
|
||||
if a.repo == nil {
|
||||
_, err := fmt.Fprintln(a.output, "History commands unavailable (storage not initialised).")
|
||||
return true, false, err
|
||||
}
|
||||
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
|
||||
}
|
||||
if len(sessions) == 0 {
|
||||
_, err := fmt.Fprintln(a.output, "No saved sessions.")
|
||||
return true, false, err
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
return true, false, nil
|
||||
case "/help":
|
||||
_, err := fmt.Fprintln(a.output, "Commands: /exit, /reset, /help (more coming soon)")
|
||||
_, err := fmt.Fprintln(a.output, "Commands: /exit, /reset, /list, /open <name>, /rename <name>, /help (more coming soon)")
|
||||
return true, false, err
|
||||
default:
|
||||
_, err := fmt.Fprintf(a.output, "Unknown command %q. Try /help.\n", trimmed)
|
||||
return true, false, err
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) maybeSuggestSessionName(ctx context.Context) error {
|
||||
if a.chat == nil {
|
||||
return nil
|
||||
}
|
||||
if !a.chat.ShouldSuggestSessionName() {
|
||||
return nil
|
||||
}
|
||||
|
||||
suggestion, err := a.chat.SuggestSessionName(ctx)
|
||||
if err != nil {
|
||||
a.chat.MarkSessionNameSuggested()
|
||||
return err
|
||||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user