MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / createFireworksRequestWithFallback

Function createFireworksRequestWithFallback

web/src/llm-api/fireworks.ts:832–951  ·  view source on GitHub ↗
(params: {
  body: ChatCompletionRequestBody
  originalModel: string
  fetch: typeof globalThis.fetch
  logger: Logger
  useCustomDeployment?: boolean
  deploymentMap?: Record<string, string>
  sessionId: string
  now?: Date
})

Source from the content-addressed store, hash-verified

830 * so freebuff can offer MiniMax as the always-on option.
831 */
832export async function createFireworksRequestWithFallback(params: {
833 body: ChatCompletionRequestBody
834 originalModel: string
835 fetch: typeof globalThis.fetch
836 logger: Logger
837 useCustomDeployment?: boolean
838 deploymentMap?: Record<string, string>
839 sessionId: string
840 now?: Date
841}): Promise<Response> {
842 const { body, originalModel, fetch, logger, sessionId } = params
843 const now = params.now ?? new Date()
844 const useCustomDeployment =
845 params.useCustomDeployment ?? FIREWORKS_USE_CUSTOM_DEPLOYMENT
846 const deploymentMap = params.deploymentMap ?? FIREWORKS_DEPLOYMENT_MAP
847 const deploymentModelId = deploymentMap[originalModel]
848 const hasDeployment = useCustomDeployment && Boolean(deploymentModelId)
849 const isHoursGatedModel = FIREWORKS_HOURS_GATED_MODELS.has(originalModel)
850 const shouldFallbackToStandardApi =
851 body.codebuff_metadata?.cost_mode === 'lite'
852
853 const createStandardApiRequest = () =>
854 createFireworksRequest({ body, originalModel, fetch, sessionId })
855
856 if (isHoursGatedModel && !isDeploymentHours(now)) {
857 if (shouldFallbackToStandardApi) {
858 logger.info(
859 { model: originalModel },
860 'Falling back to Fireworks standard API outside deployment hours',
861 )
862 return createStandardApiRequest()
863 }
864 return new Response(
865 JSON.stringify({
866 error: {
867 message: `${originalModel} is only available during ${FREEBUFF_DEPLOYMENT_HOURS_LABEL}. Use minimax/minimax-m2.7 outside those hours.`,
868 code: 'DEPLOYMENT_OUTSIDE_HOURS',
869 type: 'availability_error',
870 },
871 }),
872 { status: 503, statusText: 'Service Unavailable' },
873 )
874 }
875
876 if (hasDeployment && isDeploymentCoolingDown()) {
877 if (shouldFallbackToStandardApi) {
878 logger.info(
879 { model: originalModel },
880 'Falling back to Fireworks standard API during deployment cooldown',
881 )
882 return createStandardApiRequest()
883 }
884 return new Response(
885 JSON.stringify({
886 error: {
887 message: `${originalModel} deployment is temporarily unavailable. Use minimax/minimax-m2.7 while it recovers.`,
888 code: 'DEPLOYMENT_COOLDOWN',
889 type: 'availability_error',

Callers 3

handleFireworksNonStreamFunction · 0.85
handleFireworksStreamFunction · 0.85

Calls 6

getErrorObjectFunction · 0.90
isDeploymentHoursFunction · 0.85
createStandardApiRequestFunction · 0.85
isDeploymentCoolingDownFunction · 0.85
createFireworksRequestFunction · 0.85
markDeploymentScalingUpFunction · 0.85

Tested by

no test coverage detected