feat(session): persist last request state for UI restoration
Sessions now record the input-bar state used for the most recent QA request (agent, model, KB scope, web search). The chat UI hydrates those settings on session reopen so users see the same configuration they used last time, instead of the global default. The state is stored in the existing sessions.agent_config JSONB column to avoid a new migration. Frontend snapshots the user's global defaults on session enter and restores them on session leave, so opening an old session does not pollute new-chat defaults.
This commit is contained in:
@@ -19,6 +19,10 @@ type SessionService interface {
|
||||
GetPagedSessionsByTenant(ctx context.Context, page *types.Pagination) (*types.PageResult, error)
|
||||
// UpdateSession updates a session
|
||||
UpdateSession(ctx context.Context, session *types.Session) error
|
||||
// UpdateSessionLastRequestState records the input-bar state used for the
|
||||
// most recent QA request on this session. Best-effort: callers should log
|
||||
// but not surface failures to the user.
|
||||
UpdateSessionLastRequestState(ctx context.Context, sessionID string, state *types.SessionLastRequestState) error
|
||||
// DeleteSession deletes a session
|
||||
DeleteSession(ctx context.Context, id string) error
|
||||
// BatchDeleteSessions deletes multiple sessions by IDs
|
||||
@@ -65,6 +69,10 @@ type SessionRepository interface {
|
||||
QueryPaged(ctx context.Context, q *types.SessionListQuery) ([]*types.SessionListItem, int64, error)
|
||||
// Update updates a session visible to the tenant/user scope.
|
||||
Update(ctx context.Context, session *types.Session, userID string) (int64, error)
|
||||
// UpdateLastRequestState persists the most recent input-bar state for a
|
||||
// session (agent, model, KB scope, etc.) so the chat UI can restore it
|
||||
// when the session is reopened. Scope rules match Update.
|
||||
UpdateLastRequestState(ctx context.Context, tenantID uint64, userID string, sessionID string, state *types.SessionLastRequestState) (int64, error)
|
||||
// SetPinned pins or unpins a session row scoped by tenant.
|
||||
// userID, when non-empty, is enforced so users cannot pin sessions they don't own.
|
||||
// Returns the number of rows affected; 0 means the session doesn't exist or is
|
||||
|
||||
@@ -89,6 +89,14 @@ type Session struct {
|
||||
// PinnedAt records when the session was pinned; nil when not pinned.
|
||||
PinnedAt *time.Time `json:"pinned_at,omitempty"`
|
||||
|
||||
// LastRequestState records the input-bar state used the last time this
|
||||
// session sent a question (agent, model, KB scope, web search, MCPs).
|
||||
// Persisted on every successful POST to /knowledge-chat or /agent-chat so
|
||||
// that reopening the session can restore the original request context to
|
||||
// the chat UI. Stored in the legacy sessions.agent_config JSONB column to
|
||||
// avoid a new migration; the shape used today is `SessionLastRequestState`.
|
||||
LastRequestState *SessionLastRequestState `json:"last_request_state,omitempty" gorm:"column:agent_config;type:jsonb"`
|
||||
|
||||
// // Strategy configuration
|
||||
// KnowledgeBaseID string `json:"knowledge_base_id"` // 关联的知识库ID
|
||||
// MaxRounds int `json:"max_rounds"` // 多轮保持轮数
|
||||
@@ -184,6 +192,54 @@ func (c *SummaryConfig) Scan(value interface{}) error {
|
||||
return json.Unmarshal(b, c)
|
||||
}
|
||||
|
||||
// SessionLastRequestState captures the user-facing input-bar state at the
|
||||
// time of the most recent QA request on a session. It is purely a UI memory
|
||||
// aid — none of the fields here drive backend behaviour. They are echoed back
|
||||
// to the frontend by GetSession so the chat input can restore the same agent,
|
||||
// model, KB scope, etc. the user had selected last time.
|
||||
type SessionLastRequestState struct {
|
||||
AgentID string `json:"agent_id,omitempty"`
|
||||
AgentEnabled bool `json:"agent_enabled"`
|
||||
ModelID string `json:"model_id,omitempty"`
|
||||
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"`
|
||||
KnowledgeIDs []string `json:"knowledge_ids,omitempty"`
|
||||
WebSearchEnabled bool `json:"web_search_enabled"`
|
||||
}
|
||||
|
||||
// Value implements driver.Valuer for SessionLastRequestState (JSONB).
|
||||
func (s *SessionLastRequestState) Value() (driver.Value, error) {
|
||||
if s == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return json.Marshal(s)
|
||||
}
|
||||
|
||||
// Scan implements sql.Scanner for SessionLastRequestState (JSONB).
|
||||
// Tolerates legacy values that may not match the current schema by silently
|
||||
// ignoring unmarshal errors — the stored row predates this struct.
|
||||
func (s *SessionLastRequestState) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
var b []byte
|
||||
switch v := value.(type) {
|
||||
case []byte:
|
||||
b = v
|
||||
case string:
|
||||
b = []byte(v)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
if len(b) == 0 {
|
||||
return nil
|
||||
}
|
||||
if err := json.Unmarshal(b, s); err != nil {
|
||||
// Tolerate legacy shapes from before this column was repurposed.
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the driver.Valuer interface, used to convert ContextConfig to database value
|
||||
func (c *ContextConfig) Value() (driver.Value, error) {
|
||||
return json.Marshal(c)
|
||||
|
||||
Reference in New Issue
Block a user