refactor(sessions): add mergeSessionEntry
This commit is contained in:
@@ -133,6 +133,17 @@ export type SessionEntry = {
|
|||||||
skillsSnapshot?: SessionSkillSnapshot;
|
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 = {
|
export type GroupKeyResolution = {
|
||||||
key: string;
|
key: string;
|
||||||
legacyKey?: string;
|
legacyKey?: string;
|
||||||
@@ -487,17 +498,14 @@ export async function updateLastRoute(params: {
|
|||||||
const store = loadSessionStore(storePath);
|
const store = loadSessionStore(storePath);
|
||||||
const existing = store[sessionKey];
|
const existing = store[sessionKey];
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const sessionId = existing?.sessionId ?? crypto.randomUUID();
|
const next = mergeSessionEntry(existing, {
|
||||||
const next: SessionEntry = {
|
|
||||||
...(existing ?? { sessionId, updatedAt: 0 }),
|
|
||||||
sessionId,
|
|
||||||
updatedAt: Math.max(existing?.updatedAt ?? 0, now),
|
updatedAt: Math.max(existing?.updatedAt ?? 0, now),
|
||||||
lastProvider: provider,
|
lastProvider: provider,
|
||||||
lastTo: to?.trim() ? to.trim() : undefined,
|
lastTo: to?.trim() ? to.trim() : undefined,
|
||||||
lastAccountId: accountId?.trim()
|
lastAccountId: accountId?.trim()
|
||||||
? accountId.trim()
|
? accountId.trim()
|
||||||
: existing?.lastAccountId,
|
: existing?.lastAccountId,
|
||||||
};
|
});
|
||||||
store[sessionKey] = next;
|
store[sessionKey] = next;
|
||||||
await saveSessionStore(storePath, store);
|
await saveSessionStore(storePath, store);
|
||||||
return next;
|
return next;
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import {
|
|||||||
import { buildConfigSchema } from "../config/schema.js";
|
import { buildConfigSchema } from "../config/schema.js";
|
||||||
import {
|
import {
|
||||||
loadSessionStore,
|
loadSessionStore,
|
||||||
|
mergeSessionEntry,
|
||||||
resolveMainSessionKeyFromConfig,
|
resolveMainSessionKeyFromConfig,
|
||||||
type SessionEntry,
|
type SessionEntry,
|
||||||
saveSessionStore,
|
saveSessionStore,
|
||||||
@@ -810,16 +811,10 @@ export function createBridgeHandlers(ctx: BridgeHandlersContext) {
|
|||||||
});
|
});
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const sessionId = entry?.sessionId ?? randomUUID();
|
const sessionId = entry?.sessionId ?? randomUUID();
|
||||||
const sessionEntry: SessionEntry = {
|
const sessionEntry = mergeSessionEntry(entry, {
|
||||||
sessionId,
|
sessionId,
|
||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
thinkingLevel: entry?.thinkingLevel,
|
});
|
||||||
verboseLevel: entry?.verboseLevel,
|
|
||||||
reasoningLevel: entry?.reasoningLevel,
|
|
||||||
systemSent: entry?.systemSent,
|
|
||||||
lastProvider: entry?.lastProvider,
|
|
||||||
lastTo: entry?.lastTo,
|
|
||||||
};
|
|
||||||
const clientRunId = p.idempotencyKey;
|
const clientRunId = p.idempotencyKey;
|
||||||
registerAgentRunContext(clientRunId, { sessionKey: p.sessionKey });
|
registerAgentRunContext(clientRunId, { sessionKey: p.sessionKey });
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { randomUUID } from "node:crypto";
|
|||||||
import { resolveThinkingDefault } from "../../agents/model-selection.js";
|
import { resolveThinkingDefault } from "../../agents/model-selection.js";
|
||||||
import { resolveAgentTimeoutMs } from "../../agents/timeout.js";
|
import { resolveAgentTimeoutMs } from "../../agents/timeout.js";
|
||||||
import { agentCommand } from "../../commands/agent.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 { registerAgentRunContext } from "../../infra/agent-events.js";
|
||||||
import { defaultRuntime } from "../../runtime.js";
|
import { defaultRuntime } from "../../runtime.js";
|
||||||
import { resolveSendPolicy } from "../../sessions/send-policy.js";
|
import { resolveSendPolicy } from "../../sessions/send-policy.js";
|
||||||
@@ -197,17 +197,10 @@ export const chatHandlers: GatewayRequestHandlers = {
|
|||||||
});
|
});
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const sessionId = entry?.sessionId ?? randomUUID();
|
const sessionId = entry?.sessionId ?? randomUUID();
|
||||||
const sessionEntry: SessionEntry = {
|
const sessionEntry = mergeSessionEntry(entry, {
|
||||||
sessionId,
|
sessionId,
|
||||||
updatedAt: now,
|
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;
|
const clientRunId = p.idempotencyKey;
|
||||||
registerAgentRunContext(clientRunId, { sessionKey: p.sessionKey });
|
registerAgentRunContext(clientRunId, { sessionKey: p.sessionKey });
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user