gateway: implement session lifecycle and context commands (archie-core-alm.5)

Add gateway/runtime-owned commands that remain usable when LLM credits
are exhausted:

- /start — acknowledgement without consuming a reply
- /new [name] (alias: /reset) — create a fresh session
- /topic [off|help|<session-id>] — session switching for threaded chats
- /retry — remove last assistant message, replay last user turn
- /undo [N] — remove last N messages from history (default: 1)
- /title [name] — set or display session title
- /branch [name] (alias: /fork) — create child session with inherited history
- /compress [here N|focus topic|--preview|--dry-run] (alias: /compact)

Design:
- sessionTracker maps channel+thread to active session, auto-creating
  implicit sessions with the legacy sessionKey format for backward
  compatibility with existing persisted data
- /new and /branch create explicit UUID-backed sessions
- SessionStore extended with DeleteRecentMessages, MessageCount, and
  SaveMessages for undo/retry/branch/compress operations
- SessionContext extended with Title, ParentSessionID, BranchName
- All commands report 'not configured' when session store isn't wired

Session resolution is wired into the LLM responder so /new, /branch,
and /topic switches affect subsequent chat turns.
This commit is contained in:
sam committed 2026-07-29 03:43:34 +10:00
1 parent d92ac87c25
commit a66da97ab7
9 files changed
+1494 -31

No files matched your search

+22 -1
View File
@@ -362,6 +362,15 @@ func run() int {
sessionStore = gateway.NewSessionStoreMemory(cfg.BotUser)
}
// Resolve the session ID for a message, creating one if needed.
// Uses the session tracking layer so /new, /branch, and /topic
// commands that switch the active session are respected by the
// LLM responder.
resolveSession := func(ctx context.Context, msg gateway.Message) string {
key := sessionKey(msg)
return key
}
// Build an LLM responder from the runtime. Gateway-local commands
// (/status, /model, /spawn) are handled directly; all
// other messages are routed through the LLM with conversation
@@ -374,7 +383,7 @@ func run() int {
// assembled text is returned either way, so both the blocking
// and streaming responders share this single path.
respond := func(ctx context.Context, msg gateway.Message, onDelta func(string)) (string, error) {
sessionKey := sessionKey(msg)
sessionKey := resolveSession(ctx, msg)
if err := sessionStore.SaveMessage(ctx, sessionKey, msg); err != nil {
return "", fmt.Errorf("save inbound chat message: %w", err)
}
@@ -501,6 +510,18 @@ func run() int {
router := gateway.NewRouter(st, llmResponder, "telegram")
router.LLMStream = llmStream
router.Models = chatModels
router.InitSessions(sessionStore)
// Wire session resolution so LLM turns respect active sessions
// set by /new, /branch, and /topic commands.
resolveSession = func(ctx context.Context, msg gateway.Message) string {
key, err := router.ResolveSessionKey(ctx, msg)
if err != nil {
return sessionKey(msg)
}
return key
}
configureTaskCommands(router, chatTasks, chatController, defaultChatIdentity)
startGateways = append(startGateways, func() {
go func() {