Files
PaperClipAI/ui/src/lib/agent-config-patch.ts
T
8957941656 fix(ui): keep AI accounts compatible with harness selection (#22691)
Clear incompatible managed AI accounts when a saved agent changes harness.
Filter account choices by saved routing metadata and the selected model.
Keep supported Pi accounts before model selection and preserve heartbeat
and debug edits in connection saves.

Make managed OpenCode and Pi accounts optional. Preserve harness sign-in
through setup saves and reopening. Put Connect beside missing-default
errors and cover managed versus host-auth model selection.

Supersedes #17411. Fixes #14078.

Co-Authored-By: Luri Darmawan <luri@carik.id>
Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-11-23 13:36:10 -08:00

81 lines
2.6 KiB
TypeScript

import { ADAPTER_AGNOSTIC_KEYS, type Agent } from "@paperclipai/shared";
export interface AgentConfigOverlay {
runner?: import("@paperclipai/shared").AgentRunnerChoice;
identity: Record<string, unknown>;
adapterType?: string;
adapterConfig: Record<string, unknown>;
heartbeat: Record<string, unknown>;
debug: Record<string, unknown>;
runtime: Record<string, unknown>;
}
export function omitUndefinedEntries(value: Record<string, unknown>) {
return Object.fromEntries(
Object.entries(value).filter(([, entryValue]) => entryValue !== undefined),
);
}
export function buildAgentUpdatePatch(agent: Agent, overlay: AgentConfigOverlay) {
const patch: Record<string, unknown> = {};
if (overlay.runner !== undefined) patch.runner = overlay.runner;
if (Object.keys(overlay.identity).length > 0) {
Object.assign(patch, overlay.identity);
}
if (overlay.adapterType !== undefined) {
patch.adapterType = overlay.adapterType;
}
if (overlay.adapterType !== undefined || Object.keys(overlay.adapterConfig).length > 0) {
const existing = (agent.adapterConfig ?? {}) as Record<string, unknown>;
const nextAdapterConfig =
overlay.adapterType !== undefined
? {
...Object.fromEntries(
ADAPTER_AGNOSTIC_KEYS
.filter((key) => existing[key] !== undefined)
.map((key) => [key, existing[key]]),
),
...overlay.adapterConfig,
}
: {
...existing,
...overlay.adapterConfig,
};
patch.adapterConfig = omitUndefinedEntries(nextAdapterConfig);
patch.replaceAdapterConfig = true;
}
Object.assign(patch, overlay.runtime);
if (
Object.keys(overlay.heartbeat).length > 0
|| Object.keys(overlay.debug).length > 0
) {
const existingRc = (patch.runtimeConfig ?? agent.runtimeConfig ?? {}) as Record<string, unknown>;
const nextRuntimeConfig: Record<string, unknown> = { ...existingRc };
if (Object.keys(overlay.heartbeat).length > 0) {
const existingHb = (existingRc.heartbeat ?? {}) as Record<string, unknown>;
nextRuntimeConfig.heartbeat = { ...existingHb, ...overlay.heartbeat };
}
if (Object.keys(overlay.debug).length > 0) {
const existingDebug = (existingRc.debug ?? {}) as Record<string, unknown>;
const nextDebug = omitUndefinedEntries({ ...existingDebug, ...overlay.debug });
if (Object.keys(nextDebug).length === 0) {
delete nextRuntimeConfig.debug;
} else {
nextRuntimeConfig.debug = nextDebug;
}
}
patch.runtimeConfig = nextRuntimeConfig;
}
return patch;
}