d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { getErrorMessage } from '@sim/utils/errors'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import {
|
|
providerModelsResponseSchema,
|
|
togetherProviderModelsQuerySchema,
|
|
togetherUpstreamResponseSchema,
|
|
} from '@/lib/api/contracts/providers'
|
|
import { validationErrorResponse } from '@/lib/api/server'
|
|
import { getBYOKKey } from '@/lib/api-key/byok'
|
|
import { getSession } from '@/lib/auth'
|
|
import { env } from '@/lib/core/config/env'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
|
|
import { filterBlacklistedModels, isProviderBlacklisted } from '@/providers/utils'
|
|
|
|
const logger = createLogger('TogetherModelsAPI')
|
|
|
|
/** Together's catalog includes non-text models; only chat models work with chat completions. */
|
|
const NON_CHAT_MODEL_TYPES = new Set([
|
|
'image',
|
|
'video',
|
|
'audio',
|
|
'transcribe',
|
|
'embedding',
|
|
'moderation',
|
|
'rerank',
|
|
])
|
|
|
|
export const GET = withRouteHandler(async (request: NextRequest) => {
|
|
if (isProviderBlacklisted('together')) {
|
|
logger.info('Together provider is blacklisted, returning empty models')
|
|
return NextResponse.json({ models: [] })
|
|
}
|
|
|
|
let apiKey: string | undefined
|
|
|
|
const queryValidation = togetherProviderModelsQuerySchema.safeParse({
|
|
workspaceId: request.nextUrl.searchParams.get('workspaceId') ?? undefined,
|
|
})
|
|
if (!queryValidation.success) return validationErrorResponse(queryValidation.error)
|
|
const { workspaceId } = queryValidation.data
|
|
if (workspaceId) {
|
|
const session = await getSession()
|
|
if (session?.user?.id) {
|
|
const permission = await getUserEntityPermissions(session.user.id, 'workspace', workspaceId)
|
|
if (permission) {
|
|
const byokResult = await getBYOKKey(workspaceId, 'together')
|
|
if (byokResult) {
|
|
apiKey = byokResult.apiKey
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!apiKey) {
|
|
apiKey = env.TOGETHER_API_KEY
|
|
}
|
|
|
|
if (!apiKey) {
|
|
logger.info('No Together API key available, returning empty models')
|
|
return NextResponse.json({ models: [] })
|
|
}
|
|
|
|
try {
|
|
const response = await fetch('https://api.together.ai/v1/models', {
|
|
headers: {
|
|
Authorization: `Bearer ${apiKey}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
cache: 'no-store',
|
|
})
|
|
|
|
if (!response.ok) {
|
|
logger.warn('Failed to fetch Together models', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
})
|
|
return NextResponse.json({ models: [] })
|
|
}
|
|
|
|
const data = togetherUpstreamResponseSchema.parse(await response.json())
|
|
|
|
const allModels: string[] = []
|
|
for (const model of data) {
|
|
if (model.type && NON_CHAT_MODEL_TYPES.has(model.type)) continue
|
|
allModels.push(`together/${model.id}`)
|
|
}
|
|
|
|
const uniqueModels = Array.from(new Set(allModels))
|
|
const models = filterBlacklistedModels(uniqueModels)
|
|
|
|
logger.info('Successfully fetched Together models', {
|
|
count: models.length,
|
|
filtered: uniqueModels.length - models.length,
|
|
})
|
|
|
|
return NextResponse.json(providerModelsResponseSchema.parse({ models }))
|
|
} catch (error) {
|
|
logger.error('Error fetching Together models', {
|
|
error: getErrorMessage(error, 'Unknown error'),
|
|
})
|
|
return NextResponse.json({ models: [] })
|
|
}
|
|
})
|