Skip to content
Merged
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
68 changes: 16 additions & 52 deletions app/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 90 additions & 44 deletions lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,53 +182,80 @@ const buildProviders = () => {
// This will automatically clear the cached service instance
await updateUserKubeconfig(existingIdentity.user.id, sealosKubeconfig)

// Create aiproxy token
// Create aiproxy token if not already configured
try {
const tokenInfo = await createAiproxyToken(
`fullstackagent-${sealosUserId}`,
sealosKubeconfig
)

if (tokenInfo?.token?.key && tokenInfo.anthropicBaseUrl) {
// Store ANTHROPIC_API_KEY in UserConfig
await prisma.userConfig.upsert({
where: {
userId_key: {
userId: existingIdentity.user.id,
key: 'ANTHROPIC_API_KEY',
},
},
create: {
userId: existingIdentity.user.id,
key: 'ANTHROPIC_API_KEY',
value: tokenInfo.token.key,
category: 'anthropic',
isSecret: true,
},
update: {
value: tokenInfo.token.key,
// Check if all four configs already exist
const existingConfigs = await prisma.userConfig.findMany({
where: {
userId: existingIdentity.user.id,
key: {
in: ['ANTHROPIC_API_KEY', 'ANTHROPIC_API', 'ANTHROPIC_MODEL', 'ANTHROPIC_SMALL_FAST_MODEL'],
},
})
},
})

// Store ANTHROPIC_API (base URL) in UserConfig
await prisma.userConfig.upsert({
where: {
userId_key: {
userId: existingIdentity.user.id,
key: 'ANTHROPIC_API',
},
},
create: {
userId: existingIdentity.user.id,
key: 'ANTHROPIC_API',
value: tokenInfo.anthropicBaseUrl,
category: 'anthropic',
isSecret: false,
},
update: {
value: tokenInfo.anthropicBaseUrl,
},
})
const existingKeys = new Set(existingConfigs.map(config => config.key))

// Only create token if at least one config is missing
if (existingKeys.size < 4) {
const tokenInfo = await createAiproxyToken(
`fullstackagent-${sealosUserId}`,
sealosKubeconfig
)

if (tokenInfo?.token?.key && tokenInfo.anthropicBaseUrl) {
// Create ANTHROPIC_API_KEY only if not exists
if (!existingKeys.has('ANTHROPIC_API_KEY')) {
await prisma.userConfig.create({
data: {
userId: existingIdentity.user.id,
key: 'ANTHROPIC_API_KEY',
value: tokenInfo.token.key,
category: 'anthropic',
isSecret: true,
},
})
}

// Create ANTHROPIC_API only if not exists
if (!existingKeys.has('ANTHROPIC_API')) {
await prisma.userConfig.create({
data: {
userId: existingIdentity.user.id,
key: 'ANTHROPIC_API',
value: tokenInfo.anthropicBaseUrl,
category: 'anthropic',
isSecret: false,
},
})
}

// Create ANTHROPIC_MODEL only if not exists and value is provided
if (!existingKeys.has('ANTHROPIC_MODEL') && tokenInfo.anthropicModel) {
await prisma.userConfig.create({
data: {
userId: existingIdentity.user.id,
key: 'ANTHROPIC_MODEL',
value: tokenInfo.anthropicModel,
category: 'anthropic',
isSecret: false,
},
})
}

// Create ANTHROPIC_SMALL_FAST_MODEL only if not exists and value is provided
if (!existingKeys.has('ANTHROPIC_SMALL_FAST_MODEL') && tokenInfo.anthropicSmallFastModel) {
await prisma.userConfig.create({
data: {
userId: existingIdentity.user.id,
key: 'ANTHROPIC_SMALL_FAST_MODEL',
value: tokenInfo.anthropicSmallFastModel,
category: 'anthropic',
isSecret: false,
},
})
}
}
}
} catch (error) {
logger.error(`Failed to create aiproxy token for user ${sealosUserId}: ${error}`)
Expand Down Expand Up @@ -275,6 +302,25 @@ const buildProviders = () => {
category: 'anthropic',
isSecret: false,
})

// Add model configs if provided
if (aiproxyTokenInfo.anthropicModel) {
configs.push({
key: 'ANTHROPIC_MODEL',
value: aiproxyTokenInfo.anthropicModel,
category: 'anthropic',
isSecret: false,
})
}

if (aiproxyTokenInfo.anthropicSmallFastModel) {
configs.push({
key: 'ANTHROPIC_SMALL_FAST_MODEL',
value: aiproxyTokenInfo.anthropicSmallFastModel,
category: 'anthropic',
isSecret: false,
})
}
}

const newUser = await prisma.user.create({
Expand Down
4 changes: 4 additions & 0 deletions lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const env = createEnv({
// GitHub OAuth credentials
GITHUB_CLIENT_ID: z.string().optional(),
GITHUB_CLIENT_SECRET: z.string().optional(),
ANTHROPIC_BASE_URL: z.string().optional(),
AIPROXY_ENDPOINT: z.string().optional(),
ANTHROPIC_SMALL_FAST_MODEL: z.string().optional(),
ANTHROPIC_MODEL: z.string().optional(),
},
/*
* Environment variables available on the client (and server).
Expand Down
9 changes: 7 additions & 2 deletions lib/services/aiproxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { prisma } from '@/lib/db'
import { env } from '@/lib/env'
import { logger as baseLogger } from '@/lib/logger'

const logger = baseLogger.child({ module: 'lib/services/aiproxy' })
Expand All @@ -20,6 +21,8 @@ type AiproxyResponse = {
type AiproxyTokenResponse = {
token: TokenInfo
anthropicBaseUrl: string
anthropicModel?: string
anthropicSmallFastModel?: string
}

/**
Expand All @@ -33,8 +36,8 @@ export async function createAiproxyToken(
kubeconfig: string
): Promise<AiproxyTokenResponse | null> {
// Check if required environment variables are set
const aiproxyEndpoint = process.env.AIPROXY_ENDPOINT
const anthropicBaseUrl = process.env.ANTHROPIC_BASE_URL
const aiproxyEndpoint = env.AIPROXY_ENDPOINT
const anthropicBaseUrl = env.ANTHROPIC_BASE_URL

if (!aiproxyEndpoint || !anthropicBaseUrl) {
logger.warn('AIPROXY_ENDPOINT or ANTHROPIC_BASE_URL not configured, skipping token creation')
Expand Down Expand Up @@ -71,6 +74,8 @@ export async function createAiproxyToken(
return {
token: result.data,
anthropicBaseUrl: anthropicBaseUrl,
anthropicModel: env.ANTHROPIC_MODEL,
anthropicSmallFastModel: env.ANTHROPIC_SMALL_FAST_MODEL,
}
} catch (error) {
logger.error(`Error creating aiproxy token: ${error}`)
Expand Down
52 changes: 0 additions & 52 deletions public/favicon.svg

This file was deleted.

16 changes: 16 additions & 0 deletions public/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading