fix(vscode): avoid writing null agent config fields
Omit unset agent fields on create Delete cleared agent fields in VS Code config updates Cover null field removal with a regression test
This commit is contained in:
@@ -177,6 +177,8 @@ const SLOW_HEALTH_POLL_BASE_MS = 800;
|
||||
const SLOW_HEALTH_POLL_INCREMENT_MS = 200;
|
||||
const SLOW_HEALTH_POLL_MAX_MS = 2000;
|
||||
|
||||
const hasValue = <T>(value: T | null | undefined): value is T => value !== null && value !== undefined;
|
||||
|
||||
export interface AgentDraft {
|
||||
name: string;
|
||||
scope: AgentScope;
|
||||
@@ -345,8 +347,8 @@ export const useAgentsStore = create<AgentsStore>()(
|
||||
if (config.description) agentConfig.description = config.description;
|
||||
if (config.model) agentConfig.model = config.model;
|
||||
if (config.variant) agentConfig.variant = config.variant;
|
||||
if (config.temperature !== undefined) agentConfig.temperature = config.temperature ?? null;
|
||||
if (config.top_p !== undefined) agentConfig.top_p = config.top_p ?? null;
|
||||
if (hasValue(config.temperature)) agentConfig.temperature = config.temperature;
|
||||
if (hasValue(config.top_p)) agentConfig.top_p = config.top_p;
|
||||
if (config.prompt) agentConfig.prompt = config.prompt;
|
||||
if (config.permission) agentConfig.permission = config.permission;
|
||||
if (config.disable !== undefined) agentConfig.disable = config.disable;
|
||||
|
||||
@@ -52,6 +52,39 @@ afterEach(() => {
|
||||
const readJson = (filePath) => JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
|
||||
describe('VS Code config bridge plugin parity', () => {
|
||||
test('removes agent fields when update payload sends null', async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'openchamber-vscode-agent-null-'));
|
||||
tempRoots.push(root);
|
||||
const ctx = createCtx(root);
|
||||
const configDir = path.join(root, '.opencode');
|
||||
const configPath = path.join(configDir, 'opencode.json');
|
||||
fs.mkdirSync(configDir, { recursive: true });
|
||||
fs.writeFileSync(configPath, JSON.stringify({
|
||||
agent: {
|
||||
build: {
|
||||
variant: 'fast',
|
||||
temperature: 0.3,
|
||||
top_p: 0.8,
|
||||
mode: 'subagent',
|
||||
},
|
||||
},
|
||||
}, null, 2), 'utf8');
|
||||
|
||||
const updated = await handleConfigBridgeMessage({
|
||||
id: 'update-agent-null-fields',
|
||||
type: 'api:config/agents',
|
||||
payload: {
|
||||
method: 'PATCH',
|
||||
name: 'build',
|
||||
directory: root,
|
||||
body: { variant: null, temperature: null, top_p: null },
|
||||
},
|
||||
}, ctx, deps);
|
||||
|
||||
expect(updated?.success).toBe(true);
|
||||
expect(readJson(configPath).agent.build).toEqual({ mode: 'subagent' });
|
||||
});
|
||||
|
||||
test('creates, lists, updates, and deletes project plugin entries', async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'openchamber-vscode-plugins-'));
|
||||
tempRoots.push(root);
|
||||
|
||||
@@ -1605,12 +1605,30 @@ export const createAgent = (agentName: string, config: Record<string, unknown>,
|
||||
}
|
||||
|
||||
// Extract scope and prompt from config - scope is only used for path determination, not written to file
|
||||
const { prompt, scope: _ignored, ...frontmatter } = config as Record<string, unknown> & { prompt?: unknown; scope?: unknown };
|
||||
const { prompt, scope: _ignored, ...rawFrontmatter } = config as Record<string, unknown> & { prompt?: unknown; scope?: unknown };
|
||||
void _ignored; // Scope is only used for path determination
|
||||
const frontmatter = Object.fromEntries(
|
||||
Object.entries(rawFrontmatter).filter(([, value]) => value !== null && value !== undefined)
|
||||
);
|
||||
writeMdFile(targetPath, frontmatter, typeof prompt === 'string' ? prompt : '');
|
||||
resetAgentLookupCache(globalAgentLookupCache);
|
||||
};
|
||||
|
||||
const deleteAgentJsonField = (config: Record<string, unknown>, agentName: string, field: string): boolean => {
|
||||
const agentMap = config.agent as Record<string, unknown> | undefined;
|
||||
const current = agentMap?.[agentName] as Record<string, unknown> | undefined;
|
||||
if (!agentMap || !current || !(field in current)) return false;
|
||||
|
||||
delete current[field];
|
||||
if (Object.keys(current).length === 0) {
|
||||
delete agentMap[agentName];
|
||||
}
|
||||
if (Object.keys(agentMap).length === 0) {
|
||||
delete config.agent;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
export const updateAgent = (agentName: string, updates: Record<string, unknown>, workingDirectory?: string) => {
|
||||
ensureDirs();
|
||||
|
||||
@@ -1757,6 +1775,19 @@ export const updateAgent = (agentName: string, updates: Record<string, unknown>,
|
||||
const hasMdField = Boolean(mdData?.frontmatter?.[field] !== undefined);
|
||||
const hasJsonField = Boolean(jsonSection?.[field] !== undefined);
|
||||
|
||||
if (value === null) {
|
||||
if (hasMdField && mdData) {
|
||||
delete mdData.frontmatter[field];
|
||||
mdModified = true;
|
||||
}
|
||||
|
||||
if (hasJsonField && deleteAgentJsonField(config, agentName, field)) {
|
||||
jsonModified = true;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// JSON takes precedence over md, so update JSON first if field exists there
|
||||
if (hasJsonField) {
|
||||
if (!config.agent) config.agent = {};
|
||||
|
||||
@@ -409,7 +409,10 @@ function createAgent(agentName, config, workingDirectory, scope) {
|
||||
targetScope = AGENT_SCOPE.USER;
|
||||
}
|
||||
|
||||
const { prompt, scope: _scopeFromConfig, ...frontmatter } = config;
|
||||
const { prompt, scope: _scopeFromConfig, ...rawFrontmatter } = config;
|
||||
const frontmatter = Object.fromEntries(
|
||||
Object.entries(rawFrontmatter).filter(([, value]) => value !== null && value !== undefined)
|
||||
);
|
||||
|
||||
writeMdFile(targetPath, frontmatter, prompt || '');
|
||||
console.log(`Created new agent: ${agentName} (scope: ${targetScope}, path: ${targetPath})`);
|
||||
|
||||
Reference in New Issue
Block a user