Skip to content

Commit cf64b4a

Browse files
committed
Add support for API keys on openAI compatible custom models
1 parent 36913a4 commit cf64b4a

File tree

8 files changed

+334
-16
lines changed

8 files changed

+334
-16
lines changed

.changeset/dirty-grapes-buy.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@srcbook/api': patch
3+
'@srcbook/web': patch
4+
'srcbook': patch
5+
---
6+
7+
Add support for API keys on openAI compatible models

packages/api/ai/config.mts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export async function getModel(): Promise<LanguageModel> {
5555
}
5656
const openaiCompatible = createOpenAI({
5757
compatibility: 'compatible',
58-
apiKey: 'bogus', // required but unused
58+
apiKey: config.customApiKey || 'bogus', // use custom API key if set, otherwise use a bogus key
5959
baseURL: aiBaseUrl,
6060
});
6161
return openaiCompatible(model);

packages/api/db/schema.mts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const configs = sqliteTable('config', {
1010
anthropicKey: text('anthropic_api_key'),
1111
xaiKey: text('xai_api_key'),
1212
geminiKey: text('gemini_api_key'),
13+
customApiKey: text('custom_api_key'),
1314
// TODO: This is deprecated in favor of SRCBOOK_DISABLE_ANALYTICS env variable. Remove this.
1415
enabledAnalytics: integer('enabled_analytics', { mode: 'boolean' }).notNull().default(true),
1516
// Stable ID for posthog
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE `config` ADD `custom_api_key` text;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
{
2+
"version": "6",
3+
"dialect": "sqlite",
4+
"id": "e4b05cbe-90d2-41a7-96fc-2130bd54bc16",
5+
"prevId": "c148b92f-4dbc-4a31-887d-dfaebd4db615",
6+
"tables": {
7+
"apps": {
8+
"name": "apps",
9+
"columns": {
10+
"id": {
11+
"name": "id",
12+
"type": "integer",
13+
"primaryKey": true,
14+
"notNull": true,
15+
"autoincrement": false
16+
},
17+
"name": {
18+
"name": "name",
19+
"type": "text",
20+
"primaryKey": false,
21+
"notNull": true,
22+
"autoincrement": false
23+
},
24+
"external_id": {
25+
"name": "external_id",
26+
"type": "text",
27+
"primaryKey": false,
28+
"notNull": true,
29+
"autoincrement": false
30+
},
31+
"history": {
32+
"name": "history",
33+
"type": "text",
34+
"primaryKey": false,
35+
"notNull": true,
36+
"autoincrement": false,
37+
"default": "'[]'"
38+
},
39+
"history_version": {
40+
"name": "history_version",
41+
"type": "integer",
42+
"primaryKey": false,
43+
"notNull": true,
44+
"autoincrement": false,
45+
"default": 1
46+
},
47+
"created_at": {
48+
"name": "created_at",
49+
"type": "integer",
50+
"primaryKey": false,
51+
"notNull": true,
52+
"autoincrement": false,
53+
"default": "(unixepoch())"
54+
},
55+
"updated_at": {
56+
"name": "updated_at",
57+
"type": "integer",
58+
"primaryKey": false,
59+
"notNull": true,
60+
"autoincrement": false,
61+
"default": "(unixepoch())"
62+
}
63+
},
64+
"indexes": {
65+
"apps_external_id_unique": {
66+
"name": "apps_external_id_unique",
67+
"columns": [
68+
"external_id"
69+
],
70+
"isUnique": true
71+
}
72+
},
73+
"foreignKeys": {},
74+
"compositePrimaryKeys": {},
75+
"uniqueConstraints": {}
76+
},
77+
"config": {
78+
"name": "config",
79+
"columns": {
80+
"base_dir": {
81+
"name": "base_dir",
82+
"type": "text",
83+
"primaryKey": false,
84+
"notNull": true,
85+
"autoincrement": false
86+
},
87+
"default_language": {
88+
"name": "default_language",
89+
"type": "text",
90+
"primaryKey": false,
91+
"notNull": true,
92+
"autoincrement": false,
93+
"default": "'typescript'"
94+
},
95+
"openai_api_key": {
96+
"name": "openai_api_key",
97+
"type": "text",
98+
"primaryKey": false,
99+
"notNull": false,
100+
"autoincrement": false
101+
},
102+
"anthropic_api_key": {
103+
"name": "anthropic_api_key",
104+
"type": "text",
105+
"primaryKey": false,
106+
"notNull": false,
107+
"autoincrement": false
108+
},
109+
"xai_api_key": {
110+
"name": "xai_api_key",
111+
"type": "text",
112+
"primaryKey": false,
113+
"notNull": false,
114+
"autoincrement": false
115+
},
116+
"gemini_api_key": {
117+
"name": "gemini_api_key",
118+
"type": "text",
119+
"primaryKey": false,
120+
"notNull": false,
121+
"autoincrement": false
122+
},
123+
"custom_api_key": {
124+
"name": "custom_api_key",
125+
"type": "text",
126+
"primaryKey": false,
127+
"notNull": false,
128+
"autoincrement": false
129+
},
130+
"enabled_analytics": {
131+
"name": "enabled_analytics",
132+
"type": "integer",
133+
"primaryKey": false,
134+
"notNull": true,
135+
"autoincrement": false,
136+
"default": true
137+
},
138+
"srcbook_installation_id": {
139+
"name": "srcbook_installation_id",
140+
"type": "text",
141+
"primaryKey": false,
142+
"notNull": true,
143+
"autoincrement": false,
144+
"default": "'q2rr0ckhmrca5rt22825iv43p4'"
145+
},
146+
"ai_provider": {
147+
"name": "ai_provider",
148+
"type": "text",
149+
"primaryKey": false,
150+
"notNull": true,
151+
"autoincrement": false,
152+
"default": "'openai'"
153+
},
154+
"ai_model": {
155+
"name": "ai_model",
156+
"type": "text",
157+
"primaryKey": false,
158+
"notNull": false,
159+
"autoincrement": false,
160+
"default": "'gpt-4o'"
161+
},
162+
"ai_base_url": {
163+
"name": "ai_base_url",
164+
"type": "text",
165+
"primaryKey": false,
166+
"notNull": false,
167+
"autoincrement": false
168+
},
169+
"subscription_email": {
170+
"name": "subscription_email",
171+
"type": "text",
172+
"primaryKey": false,
173+
"notNull": false,
174+
"autoincrement": false
175+
}
176+
},
177+
"indexes": {},
178+
"foreignKeys": {},
179+
"compositePrimaryKeys": {},
180+
"uniqueConstraints": {}
181+
},
182+
"secrets": {
183+
"name": "secrets",
184+
"columns": {
185+
"id": {
186+
"name": "id",
187+
"type": "integer",
188+
"primaryKey": true,
189+
"notNull": true,
190+
"autoincrement": false
191+
},
192+
"name": {
193+
"name": "name",
194+
"type": "text",
195+
"primaryKey": false,
196+
"notNull": true,
197+
"autoincrement": false
198+
},
199+
"value": {
200+
"name": "value",
201+
"type": "text",
202+
"primaryKey": false,
203+
"notNull": true,
204+
"autoincrement": false
205+
}
206+
},
207+
"indexes": {
208+
"secrets_name_unique": {
209+
"name": "secrets_name_unique",
210+
"columns": [
211+
"name"
212+
],
213+
"isUnique": true
214+
}
215+
},
216+
"foreignKeys": {},
217+
"compositePrimaryKeys": {},
218+
"uniqueConstraints": {}
219+
},
220+
"secrets_to_sessions": {
221+
"name": "secrets_to_sessions",
222+
"columns": {
223+
"id": {
224+
"name": "id",
225+
"type": "integer",
226+
"primaryKey": true,
227+
"notNull": true,
228+
"autoincrement": false
229+
},
230+
"session_id": {
231+
"name": "session_id",
232+
"type": "text",
233+
"primaryKey": false,
234+
"notNull": true,
235+
"autoincrement": false
236+
},
237+
"secret_id": {
238+
"name": "secret_id",
239+
"type": "integer",
240+
"primaryKey": false,
241+
"notNull": true,
242+
"autoincrement": false
243+
}
244+
},
245+
"indexes": {
246+
"secrets_to_sessions_session_id_secret_id_unique": {
247+
"name": "secrets_to_sessions_session_id_secret_id_unique",
248+
"columns": [
249+
"session_id",
250+
"secret_id"
251+
],
252+
"isUnique": true
253+
}
254+
},
255+
"foreignKeys": {
256+
"secrets_to_sessions_secret_id_secrets_id_fk": {
257+
"name": "secrets_to_sessions_secret_id_secrets_id_fk",
258+
"tableFrom": "secrets_to_sessions",
259+
"tableTo": "secrets",
260+
"columnsFrom": [
261+
"secret_id"
262+
],
263+
"columnsTo": [
264+
"id"
265+
],
266+
"onDelete": "no action",
267+
"onUpdate": "no action"
268+
}
269+
},
270+
"compositePrimaryKeys": {},
271+
"uniqueConstraints": {}
272+
}
273+
},
274+
"enums": {},
275+
"_meta": {
276+
"schemas": {},
277+
"tables": {},
278+
"columns": {}
279+
},
280+
"internal": {
281+
"indexes": {}
282+
}
283+
}

packages/api/drizzle/meta/_journal.json

+7
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@
106106
"when": 1732197490638,
107107
"tag": "0014_Gemini_Integration",
108108
"breakpoints": true
109+
},
110+
{
111+
"idx": 15,
112+
"version": "6",
113+
"when": 1737324288698,
114+
"tag": "0015_add_custom_api_key",
115+
"breakpoints": true
109116
}
110117
]
111118
}

0 commit comments

Comments
 (0)