MCPcopy
hub / github.com/CodebuffAI/codebuff / getModelForRequest

Function getModelForRequest

sdk/src/impl/model-provider.ts:115–163  ·  view source on GitHub ↗
(
  params: ModelRequestParams,
)

Source from the content-addressed store, hash-verified

113 * This function is async because it may need to refresh the OAuth token.
114 */
115export async function getModelForRequest(
116 params: ModelRequestParams,
117): Promise<ModelResult> {
118 const { apiKey, model, skipChatGptOAuth, costMode } = params
119
120 // Check if we should use ChatGPT OAuth direct
121 // Only attempt for allowlisted models; non-allowlisted models silently fall through to backend.
122 if (
123 CHATGPT_OAUTH_ENABLED &&
124 !skipChatGptOAuth &&
125 isOpenAIProviderModel(model) &&
126 isChatGptOAuthModelAllowed(model)
127 ) {
128 // In free mode, rate-limited ChatGPT OAuth must not silently fall through to
129 // the Codebuff backend — freebuff should only use the direct OpenAI route or fail.
130 if (isChatGptOAuthRateLimited()) {
131 if (isFreeMode(costMode)) {
132 throw new Error(
133 'ChatGPT rate limit reached. Please wait a few minutes and try again.',
134 )
135 }
136 } else {
137 const chatGptOAuthCredentials = await getValidChatGptOAuthCredentials()
138
139 if (chatGptOAuthCredentials) {
140 return {
141 model: createOpenAIOAuthModel(
142 model,
143 chatGptOAuthCredentials.accessToken,
144 ),
145 isChatGptOAuth: true,
146 }
147 }
148
149 // In free mode, if credentials are unavailable, don't fall through to backend.
150 if (isFreeMode(costMode)) {
151 throw new Error(
152 'ChatGPT OAuth credentials unavailable. Please reconnect with /connect:chatgpt.',
153 )
154 }
155 }
156 }
157
158 // Default: use Codebuff backend
159 return {
160 model: createCodebuffBackendModel(apiKey, model),
161 isChatGptOAuth: false,
162 }
163}
164
165/**
166 * Create an OpenAI model that routes through the ChatGPT backend API (Codex endpoint).

Callers 4

promptAiSdkStreamFunction · 0.90
promptAiSdkFunction · 0.90
promptAiSdkStructuredFunction · 0.90

Calls 7

isOpenAIProviderModelFunction · 0.90
isFreeModeFunction · 0.90
createOpenAIOAuthModelFunction · 0.85

Tested by

no test coverage detected