Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions apps/studio/electron/main/chat/llmProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createAnthropic } from '@ai-sdk/anthropic';
import { createAmazonBedrock, type AmazonBedrockProviderSettings } from '@ai-sdk/amazon-bedrock';
import { createAnthropic, type AnthropicProviderSettings } from '@ai-sdk/anthropic';
import type { StreamRequestType } from '@onlook/models/chat';
import { BASE_PROXY_ROUTE, FUNCTIONS_ROUTE, ProxyRoutes } from '@onlook/models/constants';
import { CLAUDE_MODELS, LLMProvider } from '@onlook/models/llm';
import { BEDROCK_MODEL_MAP, CLAUDE_MODELS, LLMProvider } from '@onlook/models/llm';
import { type LanguageModelV1 } from 'ai';
import { getRefreshedAuthTokens } from '../auth';

export interface OnlookPayload {
requestType: StreamRequestType;
}
Expand All @@ -28,30 +30,42 @@ async function getAnthropicProvider(
const apiKey = import.meta.env.VITE_ANTHROPIC_API_KEY;
const proxyUrl = `${import.meta.env.VITE_SUPABASE_API_URL}${FUNCTIONS_ROUTE}${BASE_PROXY_ROUTE}${ProxyRoutes.ANTHROPIC}`;

const config: {
apiKey?: string;
baseURL?: string;
headers?: Record<string, string>;
} = {};

if (apiKey) {
config.apiKey = apiKey;
} else {
const authTokens = await getRefreshedAuthTokens();
if (!authTokens) {
throw new Error('No auth tokens found');
}
config.apiKey = '';
config.baseURL = proxyUrl;
config.headers = {
Authorization: `Bearer ${authTokens.accessToken}`,
'X-Onlook-Request-Type': payload.requestType,
'anthropic-beta': 'output-128k-2025-02-19',
};
return createAnthropicDirect({ apiKey }, model);
}

const config: AnthropicProviderSettings = {};

// Use proxy URL
const authTokens = await getRefreshedAuthTokens();
if (!authTokens) {
throw new Error('No auth tokens found');
}
config.baseURL = proxyUrl;
config.headers = {
Authorization: `Bearer ${authTokens.accessToken}`,
'X-Onlook-Request-Type': payload.requestType,
'anthropic-beta': 'output-128k-2025-02-19',
};
return createAnthropicBedrock(config, model);
}

const createAnthropicBedrock = (
config: AmazonBedrockProviderSettings,
claudeModel: CLAUDE_MODELS,
) => {
config.accessKeyId = '';
config.secretAccessKey = '';
config.region = '';

const bedrock = createAmazonBedrock(config);
const bedrockModel = BEDROCK_MODEL_MAP[claudeModel];
return bedrock(bedrockModel);
};

const createAnthropicDirect = (config: AnthropicProviderSettings, model: CLAUDE_MODELS) => {
const anthropic = createAnthropic(config);
return anthropic(model, {
cacheControl: true,
});
}
};
3 changes: 2 additions & 1 deletion apps/studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@ai-sdk/amazon-bedrock": "^2.2.10",
"@ai-sdk/anthropic": "^1.1.17",
"@codemirror/autocomplete": "^6.18.6",
"@codemirror/closebrackets": "^0.19.2",
Expand All @@ -59,9 +60,9 @@
"@parcel/watcher": "^2.5.1",
"@shikijs/monaco": "^1.22.0",
"@supabase/supabase-js": "^2.45.6",
"@uiw/react-codemirror": "^4.21.21",
"@trainloop/sdk": "^0.1.8",
"@types/webfontloader": "^1.6.38",
"@uiw/react-codemirror": "^4.21.21",
"@xterm/xterm": "^5.6.0-beta.98",
"ai": "^4.2.6",
"browser-image-compression": "^2.0.2",
Expand Down
Loading