Skip to content

Commit 8b9eb03

Browse files
committed
feat: use the user provided codeium api key if available when making requests
1 parent 0a191be commit 8b9eb03

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

packages/api/ai/autocomplete/index.mts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import { type CodiumCompletionResult } from "@srcbook/shared";
88
const EDITOR_API_KEY = 'd49954eb-cfba-4992-980f-d8fb37f0e942';
99
const LANGUAGE_SERVER_PROTO_FILE_PATH = path.join(__dirname, "language_server.proto");
1010

11-
export async function runCodiumAiAutocomplete(source: string, cursorOffset: number): Promise<CodiumCompletionResult> {
11+
export async function runCodiumAiAutocomplete(
12+
apiKey: string | null,
13+
source: string,
14+
cursorOffset: number,
15+
): Promise<CodiumCompletionResult> {
1216
const protos = await protobuf.load(LANGUAGE_SERVER_PROTO_FILE_PATH)
1317
const GetCompletionsRequest = protos.lookupType("GetCompletionsRequest");
1418
const Metadata = protos.lookupType("Metadata");
@@ -18,13 +22,14 @@ export async function runCodiumAiAutocomplete(source: string, cursorOffset: numb
1822
const GetCompletionsResponse = protos.lookupType("GetCompletionsResponse");
1923

2024
const sessionId = `react-editor-${crypto.randomUUID()}`;
25+
const apiKey = apiKey ?? EDITOR_API_KEY;
2126

2227
const payload = {
2328
otherDocuments: [],
2429
metadata: Metadata.create({
2530
ideName: 'web',
2631
extensionVersion: '1.0.12',
27-
apiKey: 'd49954eb-cfba-4992-980f-d8fb37f0e942',
32+
apiKey,
2833
ideVersion: 'unknown',
2934
extensionName: '@codeium/react-code-editor',
3035
sessionId,
@@ -56,7 +61,7 @@ export async function runCodiumAiAutocomplete(source: string, cursorOffset: numb
5661
headers: {
5762
'Connect-Protocol-Version': '1',
5863
'Content-Type': 'application/proto',
59-
Authorization: `Basic ${EDITOR_API_KEY}-${sessionId}`,
64+
Authorization: `Basic ${apiKey}-${sessionId}`,
6065
},
6166
});
6267
// console.log('RESPONSE:', response.status);

packages/api/server/http.mts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,11 @@ router.post('/feedback', cors(), async (req, res) => {
352352
router.options('/ai-autocomplete', cors());
353353
router.post('/ai-autocomplete', cors(), async (req, res) => {
354354
const { source, cursorOffset } = req.body;
355+
const config = await getConfig();
356+
355357
let result;
356358
try {
357-
result = await runCodiumAiAutocomplete(source, cursorOffset);
359+
result = await runCodiumAiAutocomplete(config.codeiumApiKey, source, cursorOffset);
358360
} catch (err) {
359361
console.error('Error running ai autocomplete:', err);
360362
return res.json({ error: true });

0 commit comments

Comments
 (0)