Skip to content

Commit

Permalink
fix: make anthropic work in fiddle/vscode (#970)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sxlijin committed Sep 20, 2024
1 parent 847f3a9 commit 32eccae
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions typescript/fiddle-proxy/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions typescript/vscode-ext/packages/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')) {
Expand All @@ -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'] = '*'
},
Expand Down

0 comments on commit 32eccae

Please sign in to comment.