Revert "Revert "Rewrite extension management tools"" (#5273)

Signed-off-by: Angela Ning <aning@squareup.com>
This commit is contained in:
Angela Ning
2025-10-21 11:34:17 -04:00
committed by GitHub
parent 3546ddb8c2
commit 09752aa7d0
19 changed files with 933 additions and 357 deletions
@@ -313,6 +313,31 @@ describe('Agent API', () => {
}),
});
});
it('should not mutate the original extension config', async () => {
const originalConfig: ExtensionConfig = {
type: 'stdio',
name: 'Extension Manager',
description: 'Test description',
cmd: 'python',
args: ['script.py'],
};
const mockResponse = {
ok: true,
text: vi.fn().mockResolvedValue('{"error": false}'),
};
mockFetch.mockResolvedValue(mockResponse);
const { replaceWithShims } = await import('./utils');
vi.mocked(replaceWithShims).mockResolvedValue('/path/to/shim');
await addToAgent(originalConfig, {}, 'test-session');
// Verify the original config was not mutated
expect(originalConfig.name).toBe('Extension Manager');
expect(originalConfig.cmd).toBe('python');
});
});
describe('removeFromAgent', () => {
@@ -156,21 +156,26 @@ export async function addToAgent(
options: ToastServiceOptions = {},
sessionId: string
): Promise<Response> {
// Create a copy to avoid mutating the original extension object
const extensionCopy: ExtensionConfig = { ...extension };
try {
if (extension.type === 'stdio') {
extension.cmd = await replaceWithShims(extension.cmd);
if (extensionCopy.type === 'stdio') {
extensionCopy.cmd = await replaceWithShims(extensionCopy.cmd);
}
extension.name = sanitizeName(extension.name);
extensionCopy.name = sanitizeName(extensionCopy.name);
return await extensionApiCall('/extensions/add', extension, options, sessionId);
return await extensionApiCall('/extensions/add', extensionCopy, options, sessionId);
} catch (error) {
// Check if this is a 428 error and make the message more descriptive
if (error instanceof Error && error.message && error.message.includes('428')) {
const enhancedError = new Error(
'Failed to add extension. Goose Agent was still starting up. Please try again.'
);
console.error(`Failed to add extension ${extension.name} to agent: ${enhancedError.message}`);
console.error(
`Failed to add extension ${extensionCopy.name} to agent: ${enhancedError.message}`
);
throw enhancedError;
}
throw error;