Files
archie/internal/gateway/session.go
T
sam a66da97ab7 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.
2026-07-29 03:43:34 +10:00

58 lines
2.1 KiB
Go

package gateway
import "time"
// SessionSource identifies the platform, bot identity, and conversation a
// gateway session belongs to. It is the composite natural key for session
// lookup.
type SessionSource struct {
// Platform is the adapter name ("telegram", "discord", "web").
Platform string `json:"platform"`
// BotUser is the bot identity serving this session (e.g. "archie",
// "winter"). Distinct bot identities on the same platform and channel
// produce distinct sessions.
BotUser string `json:"bot_user"`
// ChannelID is the platform conversation identifier (chat ID, channel
// name, DM thread).
ChannelID string `json:"channel_id"`
// ThreadID identifies the topic thread within the conversation, for
// platforms that support threading (Telegram supergroup topics,
// Slack threads, Discord forum posts). Empty for flat chats and the
// General topic. When non-empty, distinct threads within the same
// channel produce distinct sessions.
ThreadID string `json:"thread_id,omitempty"`
}
// SessionContext holds the full metadata for an active gateway session.
// SessionStore is the persistence layer; callers treat SessionContext as
// a value object.
type SessionContext struct {
// SessionID is the unique session identifier (UUID).
SessionID string `json:"session_id"`
// Source identifies the platform, bot, and conversation.
Source SessionSource `json:"source"`
// Title is an optional human-readable name for the session, set
// via /title. Empty until set.
Title string `json:"title,omitempty"`
// ParentSessionID is the session this one was branched from, set
// by /branch or /fork. Empty for root sessions.
ParentSessionID string `json:"parent_session_id,omitempty"`
// BranchName is the name given when branching, set via
// /branch <name>. Empty for root sessions.
BranchName string `json:"branch_name,omitempty"`
// CreatedAt is when the session was first established.
CreatedAt time.Time `json:"created_at"`
// LastActiveAt is the timestamp of the most recent message or event
// on this session. Updated by Touch.
LastActiveAt time.Time `json:"last_active_at"`
}