Skip to content

Commit

Permalink
feature: default provider api key per workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
geclos committed Sep 18, 2024
1 parent c1eb4c6 commit d8ac5c1
Show file tree
Hide file tree
Showing 15 changed files with 2,274 additions and 46 deletions.
2 changes: 2 additions & 0 deletions apps/infra/Pulumi.core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ config:
secure: v1:WABt/tJjfsAKMplU:RRLtj5mTu301x4sn2Tr91u4O0Msd3mWVJutcDg==
infra:DEFAULT_PROJECT_ID:
secure: v1:M0giTdD2+Mjre0ps:YIaUlExLI41EFdcpf+6Fxec=
infra:DEFAULT_PROVIDER_API_KEY:
secure: v1:MQQUsg8lG76xgofP:ZzfbqWLvtWamnPHW7a4rX8A9f0SMaCx3jXUPZlfPbdcsPugv6ocLbdUQ75PoSzu4Xk9fPnx9Re6kKD/EoZkA2A5mX6QGgJFmJQ1E+0QDzatR3TGx1Jl2IZ2tgtUAkhreiP0T+gCBFdJ68bL0CG1D9OD5pDWa8//XgtHeXkR7j4Z1QeYtfkj2uvJJNLGUGOFprFleCnGMR8kVYeUDZfNMUIHVUh6pTuDeVBCbYF26qQT7JAEJDU2z8MGx
infra:DEFAULT_PROVIDER_ID:
secure: v1:N7RvQfTlIJlrU5N5:NdbEHtxvPSMIOhapDNuLVxU=
infra:LATITUDE_LLM_AWS_ACCESS_KEY:
Expand Down
10 changes: 5 additions & 5 deletions apps/infra/src/core/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ const workersWebsocketsSecretToken = createSecretWithVersion(
'WORKERS_WEBSOCKET_SECRET_TOKEN',
'Secret token key for websockets in workers',
)
const defaultProviderId = createSecretWithVersion(
'DEFAULT_PROVIDER_ID',
'Default provider ID',
)
const defaultProjectId = createSecretWithVersion(
'DEFAULT_PROJECT_ID',
'Default project ID',
)
const defaultProviderApiKey = createSecretWithVersion(
'DEFAULT_PROVIDER_API_KEY',
'Default provider API key',
)

export const mailerApiKeyArn = mailerApiKey.arn
export const sentryDsnArn = sentryDsn.arn
Expand All @@ -76,5 +76,5 @@ export const awsAccessSecretArn = awsAccessSecret.arn
export const websocketsSecretTokenArn = websocketsSecretToken.arn
export const websocketsSecretRefreshTokenArn = websocketsSecretRefreshToken.arn
export const workersWebsocketsSecretTokenArn = workersWebsocketsSecretToken.arn
export const defaultProviderIdArn = defaultProviderId.arn
export const defaultProjectIdArn = defaultProjectId.arn
export const defaultProviderApiKeyArn = defaultProviderApiKey.arn
11 changes: 6 additions & 5 deletions apps/infra/src/deployments/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ const workersWebsocketsSecretTokenArn = coreStack.requireOutput(
const sentryDsnArn = coreStack.requireOutput('sentryDsnArn')
const sentryOrgArn = coreStack.requireOutput('sentryOrgArn')
const sentryProjectArn = coreStack.requireOutput('sentryProjectArn')
const defaultProviderIdArn = coreStack.requireOutput('defaultProviderIdArn')
const defaultProjectIdArn = coreStack.requireOutput('defaultProjectIdArn')
const defaultProviderApiKeyArn = coreStack.requireOutput(
'defaultProviderApiKeyArn',
)

const getSecretString = (arn: pulumi.Output<any>) => {
return arn.apply((secretId) =>
Expand All @@ -49,12 +51,12 @@ const websocketSecretRefreshToken = getSecretString(
const workersWebsocketsSecretToken = getSecretString(
workersWebsocketsSecretTokenArn,
)
const defaultProviderId = getSecretString(defaultProviderIdArn)
const defaultProjectId = getSecretString(defaultProjectIdArn)

export const sentryDsn = getSecretString(sentryDsnArn)
export const sentryOrg = getSecretString(sentryOrgArn)
export const sentryProject = getSecretString(sentryProjectArn)
export const defaultProviderApiKey = getSecretString(defaultProviderApiKeyArn)

export const dbUrl = pulumi.interpolate`postgresql://${dbUsername}:${dbPassword}@${dbEndpoint}/${dbName}?sslmode=verify-full&sslrootcert=/app/packages/core/src/assets/eu-central-1-bundle.pem`
export const environment = pulumi
Expand All @@ -68,8 +70,8 @@ export const environment = pulumi
sentryDsn,
sentryOrg,
sentryProject,
defaultProviderId,
defaultProjectId,
defaultProviderApiKey,
])
.apply(() => {
return [
Expand Down Expand Up @@ -107,7 +109,6 @@ export const environment = pulumi
{ name: 'AWS_ACCESS_KEY', value: awsAccessKey },
{ name: 'AWS_ACCESS_SECRET', value: awsAccessSecret },
{ name: 'DEFAULT_PROJECT_ID', value: defaultProjectId },
{ name: 'DEFAULT_PROVIDER_ID', value: defaultProviderId },
{ name: 'DEFAULT_PROVIDER_MODEL', value: 'gpt-4o-mini' },
{ name: 'DEFAULT_PROVIDER_API_KEY', value: defaultProviderApiKey },
]
})
2 changes: 1 addition & 1 deletion apps/web/src/components/EditorHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export default function EditorHeader({
<Select
name='provider'
label='Provider'
placeholder='Using default provider'
placeholder='Choose a provider'
options={providerOptions}
value={selectedProvider}
disabled={
Expand Down
14 changes: 14 additions & 0 deletions apps/web/src/services/user/setupService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Providers } from '@latitude-data/core/browser'
import { SessionData } from '@latitude-data/core/data-access'
import { Result } from '@latitude-data/core/lib/Result'
import Transaction, {
PromisedResult,
} from '@latitude-data/core/lib/Transaction'
import { createProviderApiKey } from '@latitude-data/core/services/providerApiKeys/create'
import { createUser } from '@latitude-data/core/services/users/createUser'
import { createWorkspace } from '@latitude-data/core/services/workspaces/create'
import { env } from '@latitude-data/env'

export default function setupService({
email,
Expand Down Expand Up @@ -35,6 +38,17 @@ export default function setupService({
if (result.error) return result

const workspace = result.value
const resultProviderApiKey = await createProviderApiKey(
{
workspace,
provider: Providers.OpenAI,
name: 'OpenAI',
token: env.DEFAULT_PROVIDER_API_KEY,
authorId: user.id,
},
tx,
)
if (resultProviderApiKey.error) return resultProviderApiKey

return Result.ok({
user,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/drizzle/0055_remarkable_slayback.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "latitude"."provider_api_keys" DROP CONSTRAINT "provider_apikeys_token_provider_unique";--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "provider_apikeys_token_provider_unique" ON "latitude"."provider_api_keys" USING btree ("token","provider","workspace_id");
Loading

0 comments on commit d8ac5c1

Please sign in to comment.