refactor(sessions): add mergeSessionEntry

This commit is contained in:
Peter Steinberger
2026-01-10 16:02:56 +01:00
parent 70c1732dd1
commit 44564df028
3 changed files with 19 additions and 23 deletions

View File

@@ -133,6 +133,17 @@ export type SessionEntry = {
skillsSnapshot?: SessionSkillSnapshot;
};
export function mergeSessionEntry(
existing: SessionEntry | undefined,
patch: Partial<SessionEntry>,
): SessionEntry {
const sessionId =
patch.sessionId ?? existing?.sessionId ?? crypto.randomUUID();
const updatedAt = patch.updatedAt ?? existing?.updatedAt ?? Date.now();
if (!existing) return { ...patch, sessionId, updatedAt };
return { ...existing, ...patch, sessionId, updatedAt };
}
export type GroupKeyResolution = {
key: string;
legacyKey?: string;
@@ -487,17 +498,14 @@ export async function updateLastRoute(params: {
const store = loadSessionStore(storePath);
const existing = store[sessionKey];
const now = Date.now();
const sessionId = existing?.sessionId ?? crypto.randomUUID();
const next: SessionEntry = {
...(existing ?? { sessionId, updatedAt: 0 }),
sessionId,
const next = mergeSessionEntry(existing, {
updatedAt: Math.max(existing?.updatedAt ?? 0, now),
lastProvider: provider,
lastTo: to?.trim() ? to.trim() : undefined,
lastAccountId: accountId?.trim()
? accountId.trim()
: existing?.lastAccountId,
};
});
store[sessionKey] = next;
await saveSessionStore(storePath, store);
return next;

View File

@@ -23,6 +23,7 @@ import {
import { buildConfigSchema } from "../config/schema.js";
import {
loadSessionStore,
mergeSessionEntry,
resolveMainSessionKeyFromConfig,
type SessionEntry,
saveSessionStore,
@@ -810,16 +811,10 @@ export function createBridgeHandlers(ctx: BridgeHandlersContext) {
});
const now = Date.now();
const sessionId = entry?.sessionId ?? randomUUID();
const sessionEntry: SessionEntry = {
const sessionEntry = mergeSessionEntry(entry, {
sessionId,
updatedAt: now,
thinkingLevel: entry?.thinkingLevel,
verboseLevel: entry?.verboseLevel,
reasoningLevel: entry?.reasoningLevel,
systemSent: entry?.systemSent,
lastProvider: entry?.lastProvider,
lastTo: entry?.lastTo,
};
});
const clientRunId = p.idempotencyKey;
registerAgentRunContext(clientRunId, { sessionKey: p.sessionKey });

View File

@@ -3,7 +3,7 @@ import { randomUUID } from "node:crypto";
import { resolveThinkingDefault } from "../../agents/model-selection.js";
import { resolveAgentTimeoutMs } from "../../agents/timeout.js";
import { agentCommand } from "../../commands/agent.js";
import { type SessionEntry, saveSessionStore } from "../../config/sessions.js";
import { mergeSessionEntry, saveSessionStore } from "../../config/sessions.js";
import { registerAgentRunContext } from "../../infra/agent-events.js";
import { defaultRuntime } from "../../runtime.js";
import { resolveSendPolicy } from "../../sessions/send-policy.js";
@@ -197,17 +197,10 @@ export const chatHandlers: GatewayRequestHandlers = {
});
const now = Date.now();
const sessionId = entry?.sessionId ?? randomUUID();
const sessionEntry: SessionEntry = {
const sessionEntry = mergeSessionEntry(entry, {
sessionId,
updatedAt: now,
thinkingLevel: entry?.thinkingLevel,
verboseLevel: entry?.verboseLevel,
reasoningLevel: entry?.reasoningLevel,
systemSent: entry?.systemSent,
sendPolicy: entry?.sendPolicy,
lastProvider: entry?.lastProvider,
lastTo: entry?.lastTo,
};
});
const clientRunId = p.idempotencyKey;
registerAgentRunContext(clientRunId, { sessionKey: p.sessionKey });