From 32eccae44b27c3fec5fbc3270b6657819d75a426 Mon Sep 17 00:00:00 2001 From: Samuel Lijin Date: Thu, 19 Sep 2024 19:57:22 -0700 Subject: [PATCH] fix: make anthropic work in fiddle/vscode (#970) Anthropic now reacts to the `Origin` header being set, and reacts to such requests complaining about CORS issues. To get around this without magically injecting the Anthropic headers, just delete the `origin` header. --- typescript/fiddle-proxy/server.js | 1 + typescript/vscode-ext/packages/vscode/src/extension.ts | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/typescript/fiddle-proxy/server.js b/typescript/fiddle-proxy/server.js index ccc7b469d..959cb4340 100644 --- a/typescript/fiddle-proxy/server.js +++ b/typescript/fiddle-proxy/server.js @@ -87,6 +87,7 @@ app.use( for (const [header, value] of Object.entries(headers)) { proxyReq.setHeader(header, value) } + proxyReq.removeHeader('origin') } catch (err) { // This is not console.warn because it's not important console.log('baml-original-url is not parsable', err) diff --git a/typescript/vscode-ext/packages/vscode/src/extension.ts b/typescript/vscode-ext/packages/vscode/src/extension.ts index 68d512969..64b59c11e 100644 --- a/typescript/vscode-ext/packages/vscode/src/extension.ts +++ b/typescript/vscode-ext/packages/vscode/src/extension.ts @@ -203,9 +203,9 @@ export function activate(context: vscode.ExtensionContext) { // Extract the original target URL from the custom header let originalUrl = req.headers['baml-original-url'] if (typeof originalUrl === 'string') { + // For some reason, Node doesn't like deleting headers in the proxyReq function. delete req.headers['baml-original-url'] - - req.headers['origin'] = `http://localhost:${port}` + delete req.headers['origin'] // Ensure the URL does not end with a slash if (originalUrl.endsWith('/')) { @@ -218,6 +218,9 @@ export function activate(context: vscode.ExtensionContext) { }, logger: console, on: { + proxyReq: (proxyReq, req, res) => { + console.debug('Proxying an LLM request (to bypass CORS)', { proxyReq, req, res }) + }, proxyRes: (proxyRes, req, res) => { proxyRes.headers['Access-Control-Allow-Origin'] = '*' },