Skip to content

Commit f3eb44a

Browse files
authored
add sensible defaults and match previous behvaior (#621)
1 parent 002c25a commit f3eb44a

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

cmd/main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ func (opt *Options) bindCLIFlags(f *pflag.FlagSet) error {
346346
func RunRootCommand(ctx context.Context, opt Options, args []string) error {
347347
var err error // Declare err once for the whole function
348348

349+
// Automatically upgrade backend to filesystem if session persistence flags are requested explicitly.
350+
if (opt.NewSession || opt.ResumeSession != "" || opt.ListSessions || opt.DeleteSession != "") && opt.SessionBackend == "memory" {
351+
klog.Infof("Upgrading session-backend to 'filesystem' based on provided flags")
352+
opt.SessionBackend = "filesystem"
353+
}
354+
349355
// Validate flag combinations
350356
if opt.ExternalTools && !opt.MCPServer {
351357
return fmt.Errorf("--external-tools can only be used with --mcp-server")
@@ -421,7 +427,9 @@ func RunRootCommand(ctx context.Context, opt Options, args []string) error {
421427
if err != nil {
422428
return fmt.Errorf("failed to create a new session: %w", err)
423429
}
424-
klog.Infof("Created new session: %s\n", session.ID)
430+
if opt.SessionBackend == "filesystem" {
431+
klog.Infof("Created new session: %s\n", session.ID)
432+
}
425433
} else {
426434
if opt.ResumeSession == "" || opt.ResumeSession == "latest" {
427435
session, err = sessionManager.GetLatestSession()
@@ -437,7 +445,9 @@ func RunRootCommand(ctx context.Context, opt Options, args []string) error {
437445
if err != nil {
438446
return fmt.Errorf("failed to create new session: %w", err)
439447
}
440-
klog.Infof("No previous session found. Created new session: %s\n", session.ID)
448+
if opt.SessionBackend == "filesystem" {
449+
klog.Infof("No previous session found. Created new session: %s\n", session.ID)
450+
}
441451
}
442452
} else {
443453
sessionID := opt.ResumeSession

pkg/agent/conversation.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,17 @@ func (c *Agent) Run(ctx context.Context, initialQuery string) error {
426426
} else {
427427
if len(c.Session.Messages) > 0 {
428428
// Resuming existing session
429-
greetingMessage := fmt.Sprintf("Welcome back. What can I help you with today?\n (Don't want to continue your last session? Use --new-session)\n\n%s", c.Session.String())
429+
greetingMessage := "Welcome back. What can I help you with today?\n (Don't want to continue your last session? Use --new-session)"
430+
if c.SessionBackend == "filesystem" {
431+
greetingMessage = fmt.Sprintf("%s\n\n%s", greetingMessage, c.Session.String())
432+
}
430433
c.addMessage(api.MessageSourceAgent, api.MessageTypeText, greetingMessage)
431434
} else {
432435
// Starting new session
433-
greetingMessage := fmt.Sprintf("Hey there, what can I help you with today?\n\n%s", c.Session.String())
436+
greetingMessage := "Hey there, what can I help you with today?"
437+
if c.SessionBackend == "filesystem" {
438+
greetingMessage = fmt.Sprintf("%s\n\n%s", greetingMessage, c.Session.String())
439+
}
434440
c.addMessage(api.MessageSourceAgent, api.MessageTypeText, greetingMessage)
435441
}
436442
}
@@ -811,7 +817,10 @@ func (c *Agent) handleMetaQuery(ctx context.Context, query string) (answer strin
811817
case "tools":
812818
return "Available tools:\n\n - " + strings.Join(c.Tools.Names(), "\n - ") + "\n\n", true, nil
813819
case "session":
814-
return c.Session.String(), true, nil
820+
if c.SessionBackend != "filesystem" {
821+
return "Ephemeral session (memory backed). No persistent info available.", true, nil
822+
}
823+
return fmt.Sprintf("Current session:\n\n%s", c.Session.String()), true, nil
815824

816825
case "save-session":
817826
savedSessionID, err := c.SaveSession()

pkg/agent/conversation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestHandleMetaQuery(t *testing.T) {
193193
if err != nil {
194194
t.Fatalf("creating session: %v", err)
195195
}
196-
a := &Agent{ChatMessageStore: sess.ChatMessageStore}
196+
a := &Agent{ChatMessageStore: sess.ChatMessageStore, SessionBackend: "filesystem"}
197197
a.Session = sess
198198
return a
199199
},

pkg/sessions/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func NewSessionManager(backend string) (*SessionManager, error) {
4949

5050
func (sm *SessionManager) NewSession(meta Metadata) (*api.Session, error) {
5151
suffix := fmt.Sprintf("%04d", rand.Intn(10000))
52-
sessionID := time.Now().Format("20060102") + suffix
52+
sessionID := time.Now().Format("20060102") + "-" + suffix
5353

5454
now := time.Now()
5555
session := &api.Session{

0 commit comments

Comments
 (0)