refactor(sessions): add sessions.resolve + label helper (#570)

This commit is contained in:
Peter Steinberger
2026-01-09 16:59:54 +01:00
parent d099dabf37
commit c892fd174e
10 changed files with 446 additions and 83 deletions

View File

@@ -21,6 +21,7 @@ import type { ClawdbotConfig } from "../config/config.js";
import type { SessionEntry } from "../config/sessions.js";
import { isSubagentSessionKey } from "../routing/session-key.js";
import { normalizeSendPolicy } from "../sessions/send-policy.js";
import { parseSessionLabel } from "../sessions/session-label.js";
import {
ErrorCodes,
type ErrorShape,
@@ -28,28 +29,10 @@ import {
type SessionsPatchParams,
} from "./protocol/index.js";
export const SESSION_LABEL_MAX_LENGTH = 64;
function invalid(message: string): { ok: false; error: ErrorShape } {
return { ok: false, error: errorShape(ErrorCodes.INVALID_REQUEST, message) };
}
function normalizeLabel(
raw: unknown,
): { ok: true; label: string } | ReturnType<typeof invalid> {
const trimmed =
typeof raw === "string"
? raw.trim()
: typeof raw === "number" || typeof raw === "boolean"
? String(raw).trim()
: "";
if (!trimmed) return invalid("invalid label: empty");
if (trimmed.length > SESSION_LABEL_MAX_LENGTH) {
return invalid(`invalid label: too long (max ${SESSION_LABEL_MAX_LENGTH})`);
}
return { ok: true, label: trimmed };
}
export async function applySessionsPatchToStore(params: {
cfg: ClawdbotConfig;
store: Record<string, SessionEntry>;
@@ -93,15 +76,15 @@ export async function applySessionsPatchToStore(params: {
if (raw === null) {
delete next.label;
} else if (raw !== undefined) {
const normalized = normalizeLabel(raw);
if (!normalized.ok) return normalized;
const parsed = parseSessionLabel(raw);
if (!parsed.ok) return invalid(parsed.error);
for (const [key, entry] of Object.entries(store)) {
if (key === storeKey) continue;
if (entry?.label === normalized.label) {
return invalid(`label already in use: ${normalized.label}`);
if (entry?.label === parsed.label) {
return invalid(`label already in use: ${parsed.label}`);
}
}
next.label = normalized.label;
next.label = parsed.label;
}
}