style: fix code formatting

This commit is contained in:
Tzufucius 2026-02-17 10:22:48 +08:00
parent 634c02f040
commit 0d4eda3449

View file

@ -1,160 +1,160 @@
package command package command
import ( import (
"context" "context"
"fmt" "fmt"
"sort" "sort"
"strings" "strings"
"github.com/sipeed/picoclaw/pkg/bus" "github.com/sipeed/picoclaw/pkg/bus"
) )
type SessionCommand struct{} type SessionCommand struct{}
func (c *SessionCommand) Name() string { func (c *SessionCommand) Name() string {
return "/session" return "/session"
} }
func (c *SessionCommand) Description() string { func (c *SessionCommand) Description() string {
return "Manage conversation sessions (new, switch, ls, remove)" return "Manage conversation sessions (new, switch, ls, remove)"
} }
func (c *SessionCommand) Execute(ctx context.Context, agent AgentState, args []string, msg bus.InboundMessage) (string, error) { func (c *SessionCommand) Execute(ctx context.Context, agent AgentState, args []string, msg bus.InboundMessage) (string, error) {
if len(args) < 1 { if len(args) < 1 {
return "Usage: /session <new|switch|ls|remove> [name]", nil return "Usage: /session <new|switch|ls|remove> [name]", nil
} }
subcmd := args[0] subcmd := args[0]
// Use ChatID from message for unique session base key channel:chatID // Use ChatID from message for unique session base key channel:chatID
baseKey := fmt.Sprintf("%s:%s", msg.Channel, msg.ChatID) baseKey := fmt.Sprintf("%s:%s", msg.Channel, msg.ChatID)
// Define interfaces for accessing SessionManager and StateManager // Define interfaces for accessing SessionManager and StateManager
type SessionManager interface { type SessionManager interface {
ListSessions(prefix string) []string ListSessions(prefix string) []string
DeleteSession(key string) error DeleteSession(key string) error
} }
type StateManager interface { type StateManager interface {
SetUserSession(userID, sessionName string) error SetUserSession(userID, sessionName string) error
GetUserSession(userID string) string GetUserSession(userID string) string
} }
type ManagersProvider interface { type ManagersProvider interface {
GetSessionManager() interface{} GetSessionManager() interface{}
GetStateManager() interface{} GetStateManager() interface{}
} }
provider, ok := agent.(ManagersProvider) provider, ok := agent.(ManagersProvider)
if !ok { if !ok {
return "Error: Agent does not support session management", nil return "Error: Agent does not support session management", nil
} }
smRaw := provider.GetSessionManager() smRaw := provider.GetSessionManager()
stRaw := provider.GetStateManager() stRaw := provider.GetStateManager()
sm, okSm := smRaw.(SessionManager) sm, okSm := smRaw.(SessionManager)
st, okSt := stRaw.(StateManager) st, okSt := stRaw.(StateManager)
if !okSm || !okSt { if !okSm || !okSt {
return "Error: Failed to access internal managers", nil return "Error: Failed to access internal managers", nil
} }
switch subcmd { switch subcmd {
case "ls", "list": case "ls", "list":
return c.handleList(sm, st, baseKey) return c.handleList(sm, st, baseKey)
case "new", "switch": case "new", "switch":
if len(args) < 2 { if len(args) < 2 {
return fmt.Sprintf("Usage: /session %s <name>", subcmd), nil return fmt.Sprintf("Usage: /session %s <name>", subcmd), nil
} }
name := args[1] name := args[1]
return c.handleSwitch(st, baseKey, name) return c.handleSwitch(st, baseKey, name)
case "remove", "rm", "delete": case "remove", "rm", "delete":
if len(args) < 2 { if len(args) < 2 {
return "Usage: /session remove <name>", nil return "Usage: /session remove <name>", nil
} }
name := args[1] name := args[1]
return c.handleRemove(sm, st, baseKey, name) return c.handleRemove(sm, st, baseKey, name)
default: default:
return fmt.Sprintf("Unknown subcommand: %s", subcmd), nil return fmt.Sprintf("Unknown subcommand: %s", subcmd), nil
} }
} }
func (c *SessionCommand) handleList(sm interface { func (c *SessionCommand) handleList(sm interface {
ListSessions(prefix string) []string ListSessions(prefix string) []string
}, st interface { }, st interface {
GetUserSession(userID string) string GetUserSession(userID string) string
}, baseKey string) (string, error) { }, baseKey string) (string, error) {
keys := sm.ListSessions(baseKey) keys := sm.ListSessions(baseKey)
currentSessionName := st.GetUserSession(baseKey) currentSessionName := st.GetUserSession(baseKey)
if currentSessionName == "" { if currentSessionName == "" {
currentSessionName = "default" currentSessionName = "default"
} }
sessionMap := make(map[string]bool) sessionMap := make(map[string]bool)
sessionMap["default"] = true // Default always implicitly exists sessionMap["default"] = true // Default always implicitly exists
for _, key := range keys { for _, key := range keys {
if key == baseKey { if key == baseKey {
sessionMap["default"] = true sessionMap["default"] = true
} else if strings.HasPrefix(key, baseKey+":") { } else if strings.HasPrefix(key, baseKey+":") {
name := strings.TrimPrefix(key, baseKey+":") name := strings.TrimPrefix(key, baseKey+":")
sessionMap[name] = true sessionMap[name] = true
} }
} }
var names []string var names []string
for name := range sessionMap { for name := range sessionMap {
names = append(names, name) names = append(names, name)
} }
sort.Strings(names) sort.Strings(names)
var sb strings.Builder var sb strings.Builder
sb.WriteString("Available sessions:\n") sb.WriteString("Available sessions:\n")
for _, name := range names { for _, name := range names {
marker := " " marker := " "
if name == currentSessionName { if name == currentSessionName {
marker = "*" marker = "*"
} }
sb.WriteString(fmt.Sprintf("%s %s\n", marker, name)) sb.WriteString(fmt.Sprintf("%s %s\n", marker, name))
} }
return sb.String(), nil return sb.String(), nil
} }
func (c *SessionCommand) handleSwitch(st interface { func (c *SessionCommand) handleSwitch(st interface {
SetUserSession(userID, sessionName string) error SetUserSession(userID, sessionName string) error
}, baseKey, name string) (string, error) { }, baseKey, name string) (string, error) {
target := name target := name
if name == "default" { if name == "default" {
target = "" // empty string denotes default session in state target = "" // empty string denotes default session in state
} }
if err := st.SetUserSession(baseKey, target); err != nil { if err := st.SetUserSession(baseKey, target); err != nil {
return "", fmt.Errorf("failed to switch session: %w", err) return "", fmt.Errorf("failed to switch session: %w", err)
} }
return fmt.Sprintf("Switched to session: %s", name), nil return fmt.Sprintf("Switched to session: %s", name), nil
} }
func (c *SessionCommand) handleRemove(sm interface { func (c *SessionCommand) handleRemove(sm interface {
DeleteSession(key string) error DeleteSession(key string) error
}, st interface { }, st interface {
GetUserSession(userID string) string GetUserSession(userID string) string
}, baseKey, name string) (string, error) { }, baseKey, name string) (string, error) {
if name == "default" { if name == "default" {
return "Error: Cannot remove default session", nil return "Error: Cannot remove default session", nil
} }
current := st.GetUserSession(baseKey) current := st.GetUserSession(baseKey)
if current == name { if current == name {
return "Error: Cannot remove active session. Switch to another session first.", nil return "Error: Cannot remove active session. Switch to another session first.", nil
} }
// Session key format: baseKey:name // Session key format: baseKey:name
sessionKey := fmt.Sprintf("%s:%s", baseKey, name) sessionKey := fmt.Sprintf("%s:%s", baseKey, name)
if err := sm.DeleteSession(sessionKey); err != nil { if err := sm.DeleteSession(sessionKey); err != nil {
return "", fmt.Errorf("failed to remove session: %w", err) return "", fmt.Errorf("failed to remove session: %w", err)
} }
return fmt.Sprintf("Session '%s' removed.", name), nil return fmt.Sprintf("Session '%s' removed.", name), nil
} }