Files
PaperClipAI/ui/src/lib/agent-config-patch.ts
T
Nicky LeachandPaperclip 8a058f9d79 fix: deduplicate adapter-agnostic config keys (#9058)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - When you swap an agent's adapter (e.g. from one LLM provider to
another), the server merges the incoming PATCH body with stored config —
keys listed in \`ADAPTER_AGNOSTIC_KEYS\` are preserved regardless of
which adapter is active
> - That constant was defined independently in two places:
\`server/src/agents.ts\` (used by the adapter-swap route) and
\`ui/src/lib/agent-config-patch.ts\` (used by the UI patch builder)
> - PR #8975 fixed the bug where \`paperclipSkillSync.desiredSkills\`
was dropped on adapter swap by adding it to the server-side constant,
but the UI-side copy was not updated in the same PR — creating ongoing
drift risk
> - This pull request hoists \`ADAPTER_AGNOSTIC_KEYS\` into
\`packages/shared\` so both consumers import the same constant
> - The benefit is a single source of truth: any future key addition is
made in one place and both the server route and the UI patch builder
pick it up automatically, with a drift guard to catch any accidental
re-duplication

## Linked Issues or Issue Description

Refs #8975 — follow-up deduplication: #8975 fixed the runtime bug but
left the constant duplicated across server and UI. This PR closes that
gap.

## What Changed

- Added \`ADAPTER_AGNOSTIC_KEYS\` constant and \`AdapterAgnosticKey\`
type to \`packages/shared/src/adapter-agnostic-keys.ts\`
- Updated \`server/src/agents.ts\` to import the shared constant,
removing the local copy
- Updated \`ui/src/lib/agent-config-patch.ts\` to import the shared
constant, removing the local copy
- Added \`packages/shared/src/adapter-agnostic-keys.test.ts\`: drift
guard asserting the expected key set and both consumer import sites

## Verification

\`\`\`bash
pnpm exec vitest run packages/shared/src/adapter-agnostic-keys.test.ts
ui/src/lib/agent-config-patch.test.ts
server/src/__tests__/agent-instructions-routes.test.ts
pnpm --filter @paperclipai/shared typecheck
pnpm --filter @paperclipai/server typecheck
pnpm --filter @paperclipai/ui typecheck
\`\`\`

All 15 tests pass across the three files; all three packages typecheck
clean.

## Risks

Low risk — behavior-preserving refactor. The key set is unchanged; only
the import source changes. The drift guard will fail loudly if someone
accidentally re-introduces a local copy or modifies one without updating
the other.

> For core feature work, check [\`ROADMAP.md\`](ROADMAP.md) first and
discuss it in \`#dev\` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See \`CONTRIBUTING.md\`.

## Model Used

- Provider: Anthropic
- Model: Claude Sonnet 4.6 (\`claude-sonnet-4-6\`)
- Context: standard context window, tool use enabled
- Reasoning: standard mode (no extended thinking)

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with \`Fixes: #\` /
\`Closes #\` / \`Refs #\` OR (b) described the issue in-PR following the
relevant issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub \`#NNN\` /
\`github.com/paperclipai/paperclip\` URLs)
- [ ] My branch name describes the change (e.g. \`docs/...\`,
\`fix/...\`) and contains no internal Paperclip ticket id or
instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-07-05 21:47:38 -07:00

109 lines
3.5 KiB
TypeScript

import { ADAPTER_AGNOSTIC_KEYS, type Agent } from "@paperclipai/shared";
export interface AgentModelProfileOverlay {
enabled?: boolean;
adapterConfig?: Record<string, unknown>;
/**
* Mark the cheap profile for clearing. When true, the patch removes
* `runtimeConfig.modelProfiles.cheap` instead of merging into it.
*/
cleared?: boolean;
}
export interface AgentConfigOverlay {
identity: Record<string, unknown>;
adapterType?: string;
adapterConfig: Record<string, unknown>;
heartbeat: Record<string, unknown>;
runtime: Record<string, unknown>;
modelProfiles?: { cheap?: AgentModelProfileOverlay };
}
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 (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;
}
const cheapOverlay = overlay.modelProfiles?.cheap;
const hasModelProfileChange = cheapOverlay !== undefined;
if (Object.keys(overlay.heartbeat).length > 0 || hasModelProfileChange) {
const existingRc = (agent.runtimeConfig ?? {}) as Record<string, unknown>;
const nextRuntimeConfig: Record<string, unknown> = (patch.runtimeConfig as Record<string, unknown> | undefined)
?? { ...existingRc };
if (Object.keys(overlay.heartbeat).length > 0) {
const existingHb = (existingRc.heartbeat ?? {}) as Record<string, unknown>;
nextRuntimeConfig.heartbeat = { ...existingHb, ...overlay.heartbeat };
}
if (hasModelProfileChange) {
const existingProfiles = ((existingRc.modelProfiles ?? {}) as Record<string, unknown>);
const existingCheap = ((existingProfiles.cheap ?? {}) as Record<string, unknown>);
const nextProfiles = { ...existingProfiles };
if (cheapOverlay?.cleared) {
delete nextProfiles.cheap;
} else if (cheapOverlay) {
const mergedAdapterConfig = {
...((existingCheap.adapterConfig ?? {}) as Record<string, unknown>),
...(cheapOverlay.adapterConfig ?? {}),
};
const enabled = cheapOverlay.enabled ?? (existingCheap.enabled !== false);
nextProfiles.cheap = {
...existingCheap,
enabled,
adapterConfig: mergedAdapterConfig,
};
}
if (Object.keys(nextProfiles).length === 0) {
delete nextRuntimeConfig.modelProfiles;
} else {
nextRuntimeConfig.modelProfiles = nextProfiles;
}
}
patch.runtimeConfig = nextRuntimeConfig;
}
if (Object.keys(overlay.runtime).length > 0) {
Object.assign(patch, overlay.runtime);
}
return patch;
}