97 lines
2.7 KiB
TypeScript
97 lines
2.7 KiB
TypeScript
import type { ClawdbotConfig } from "../config/config.js";
|
|
import { loadSessionStore } from "../config/sessions.js";
|
|
import { parseSessionLabel } from "../sessions/session-label.js";
|
|
import {
|
|
ErrorCodes,
|
|
type ErrorShape,
|
|
errorShape,
|
|
type SessionsResolveParams,
|
|
} from "./protocol/index.js";
|
|
import {
|
|
listSessionsFromStore,
|
|
loadCombinedSessionStoreForGateway,
|
|
resolveGatewaySessionStoreTarget,
|
|
} from "./session-utils.js";
|
|
|
|
export type SessionsResolveResult = { ok: true; key: string } | { ok: false; error: ErrorShape };
|
|
|
|
export function resolveSessionKeyFromResolveParams(params: {
|
|
cfg: ClawdbotConfig;
|
|
p: SessionsResolveParams;
|
|
}): SessionsResolveResult {
|
|
const { cfg, p } = params;
|
|
|
|
const key = typeof p.key === "string" ? p.key.trim() : "";
|
|
const hasKey = key.length > 0;
|
|
const hasLabel = typeof p.label === "string" && p.label.trim().length > 0;
|
|
if (hasKey && hasLabel) {
|
|
return {
|
|
ok: false,
|
|
error: errorShape(ErrorCodes.INVALID_REQUEST, "Provide either key or label (not both)"),
|
|
};
|
|
}
|
|
if (!hasKey && !hasLabel) {
|
|
return {
|
|
ok: false,
|
|
error: errorShape(ErrorCodes.INVALID_REQUEST, "Either key or label is required"),
|
|
};
|
|
}
|
|
|
|
if (hasKey) {
|
|
const target = resolveGatewaySessionStoreTarget({ cfg, key });
|
|
const store = loadSessionStore(target.storePath);
|
|
const existingKey = target.storeKeys.find((candidate) => store[candidate]);
|
|
if (!existingKey) {
|
|
return {
|
|
ok: false,
|
|
error: errorShape(ErrorCodes.INVALID_REQUEST, `No session found: ${key}`),
|
|
};
|
|
}
|
|
return { ok: true, key: target.canonicalKey };
|
|
}
|
|
|
|
const parsedLabel = parseSessionLabel(p.label);
|
|
if (!parsedLabel.ok) {
|
|
return {
|
|
ok: false,
|
|
error: errorShape(ErrorCodes.INVALID_REQUEST, parsedLabel.error),
|
|
};
|
|
}
|
|
|
|
const { storePath, store } = loadCombinedSessionStoreForGateway(cfg);
|
|
const list = listSessionsFromStore({
|
|
cfg,
|
|
storePath,
|
|
store,
|
|
opts: {
|
|
includeGlobal: p.includeGlobal === true,
|
|
includeUnknown: p.includeUnknown === true,
|
|
label: parsedLabel.label,
|
|
agentId: p.agentId,
|
|
spawnedBy: p.spawnedBy,
|
|
limit: 2,
|
|
},
|
|
});
|
|
if (list.sessions.length === 0) {
|
|
return {
|
|
ok: false,
|
|
error: errorShape(
|
|
ErrorCodes.INVALID_REQUEST,
|
|
`No session found with label: ${parsedLabel.label}`,
|
|
),
|
|
};
|
|
}
|
|
if (list.sessions.length > 1) {
|
|
const keys = list.sessions.map((s) => s.key).join(", ");
|
|
return {
|
|
ok: false,
|
|
error: errorShape(
|
|
ErrorCodes.INVALID_REQUEST,
|
|
`Multiple sessions found with label: ${parsedLabel.label} (${keys})`,
|
|
),
|
|
};
|
|
}
|
|
|
|
return { ok: true, key: String(list.sessions[0]?.key ?? "") };
|
|
}
|