feat(session): persist model tier overrides per session

Store per-session config in SQLite and route /model and /reset through command fast-paths so channel sessions keep independent model selection across reconnects and restarts.
This commit is contained in:
William Valentin
2026-02-13 01:04:26 -08:00
parent 3472a0b926
commit 9f81c01603
35 changed files with 1438 additions and 144 deletions
+54 -10
View File
@@ -1,6 +1,8 @@
import type { Message } from '../models/types.js';
import type { SessionStore } from './store.js';
import { auditLogger } from '../audit/index.js';
import { SessionIndexer } from './indexer.js';
import { SessionSearch, type HistorySearchResult } from './search.js';
export interface Session {
id: string;
@@ -18,6 +20,7 @@ export class ManagedSession implements Session {
public readonly id: string,
private store: SessionStore,
private history: Message[] = [],
private indexer?: SessionIndexer,
) {}
addMessage(message: Message): Message {
@@ -26,16 +29,20 @@ export class ManagedSession implements Session {
timestamp: Date.now(),
};
this.history.push(messageWithTimestamp);
this.store.addMessage(this.id, messageWithTimestamp);
const content = typeof message.content === 'string'
? message.content
: JSON.stringify(message.content);
const metadata = this.indexer?.indexText(content);
this.store.addMessage(this.id, messageWithTimestamp, metadata);
auditLogger?.sessionMessage({
session_id: this.id,
role: message.role,
content_length: typeof message.content === 'string'
? message.content.length
content_length: typeof message.content === 'string'
? message.content.length
: JSON.stringify(message.content).length,
});
return messageWithTimestamp;
}
@@ -47,7 +54,7 @@ export class ManagedSession implements Session {
const messageCount = this.history.length;
this.history = [];
this.store.clearSession(this.id);
auditLogger?.sessionDelete({
session_id: this.id,
message_count: messageCount,
@@ -83,8 +90,25 @@ export class ManagedSession implements Session {
export class SessionManager {
private sessions: Map<string, ManagedSession> = new Map();
private indexer?: SessionIndexer;
private search?: SessionSearch;
constructor(private store: SessionStore) {}
constructor(private store: SessionStore, historyIndexConfig?: {
enabled: boolean;
maxKeywords: number;
searchLimit: number;
minScore: number;
}) {
if (historyIndexConfig?.enabled) {
this.indexer = new SessionIndexer({
maxKeywords: historyIndexConfig.maxKeywords,
});
this.search = new SessionSearch(store, {
limit: historyIndexConfig.searchLimit,
minScore: historyIndexConfig.minScore,
});
}
}
private makeSessionId(frontend: string, userId: string): string {
return `${frontend}:${userId}`;
@@ -96,9 +120,9 @@ export class SessionManager {
let session = this.sessions.get(id);
if (!session) {
const history = this.store.getMessages(id);
session = new ManagedSession(id, this.store, history);
session = new ManagedSession(id, this.store, history, this.indexer);
this.sessions.set(id, session);
auditLogger?.sessionCreate({
session_id: id,
frontend,
@@ -125,7 +149,7 @@ export class SessionManager {
for (const message of history) {
toSession.addMessage(message);
}
auditLogger?.sessionTransfer(fromSession.id, toSession.id, history.length);
}
@@ -162,4 +186,24 @@ export class SessionManager {
const session = this.getSession(frontend, userId);
session.deleteConfig(key);
}
searchHistory(query: string, opts?: { limit?: number; sessionId?: string }): HistorySearchResult[] {
if (!this.search) {
return [];
}
return this.search.search(query, opts);
}
reindexHistory(): number {
if (!this.indexer) {
return 0;
}
const rows = this.store.getAllMessagesWithMetadata();
for (const row of rows) {
const metadata = this.indexer.indexText(row.content);
this.store.updateMessageMetadata(row.id, metadata);
}
return rows.length;
}
}