Skip to content

Commit 75537ec

Browse files
committed
allow all openrouter models
1 parent 403ad4b commit 75537ec

File tree

6 files changed

+42
-28
lines changed

6 files changed

+42
-28
lines changed

backend/src/llm-apis/vercel-ai-sdk/ai-sdk.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import {
66
Model,
77
OpenAIModel,
88
openaiModels,
9-
openrouterModels,
109
type GeminiModel,
11-
type openrouterModel,
1210
} from '@codebuff/common/constants'
1311
import {
1412
endsAgentStepParam,
@@ -17,11 +15,13 @@ import {
1715
toolNameParam,
1816
} from '@codebuff/common/constants/tools'
1917
import { Message } from '@codebuff/common/types/message'
18+
import { buildArray } from '@codebuff/common/util/array'
2019
import { errorToObject } from '@codebuff/common/util/object'
2120
import { withTimeout } from '@codebuff/common/util/promise'
2221
import { generateCompactId } from '@codebuff/common/util/string'
2322
import { OpenRouterUsageAccounting } from '@codebuff/internal/openrouter-ai-sdk'
2423
import {
24+
APICallError,
2525
CoreAssistantMessage,
2626
CoreMessage,
2727
CoreUserMessage,
@@ -56,11 +56,8 @@ const modelToAiSDKModel = (model: Model): LanguageModelV1 => {
5656
if (Object.values(openaiModels).includes(model as OpenAIModel)) {
5757
return openai.languageModel(model)
5858
}
59-
// All Claude models go through OpenRouter
60-
if (Object.values(openrouterModels).includes(model as openrouterModel)) {
61-
return openRouterLanguageModel(model)
62-
}
63-
throw new Error('Unknown model: ' + model)
59+
// All other models go through OpenRouter
60+
return openRouterLanguageModel(model)
6461
}
6562

6663
// TODO: Add retries & fallbacks: likely by allowing this to instead of "model"
@@ -128,21 +125,20 @@ export const promptAiSdkStream = async function* (
128125
},
129126
'Error from AI SDK'
130127
)
131-
if (process.env.ENVIRONMENT !== 'prod') {
132-
throw chunk.error instanceof Error
133-
? new Error(
134-
`Error from AI SDK (${options.model}): ${chunk.error.message}`,
135-
{
136-
cause: chunk.error,
137-
}
138-
)
139-
: new Error(
140-
`Error from AI SDK (${options.model}): ${
141-
typeof chunk.error === 'string'
142-
? chunk.error
143-
: JSON.stringify(chunk.error)
144-
}`
145-
)
128+
if (process.env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod') {
129+
const errorBody = APICallError.isInstance(chunk.error)
130+
? chunk.error.responseBody
131+
: undefined
132+
const mainErrorMessage =
133+
chunk.error instanceof Error
134+
? chunk.error.message
135+
: typeof chunk.error === 'string'
136+
? chunk.error
137+
: JSON.stringify(chunk.error)
138+
const errorMessage = `Error from AI SDK (model ${options.model}): ${buildArray([mainErrorMessage, errorBody]).join('\n')}`
139+
throw new Error(errorMessage, {
140+
cause: chunk.error,
141+
})
146142
}
147143
}
148144
if (chunk.type === 'reasoning') {

backend/src/run-agent-step.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { AgentResponseTrace, insertTrace } from '@codebuff/bigquery'
22
import { trackEvent } from '@codebuff/common/analytics'
3-
import { ASYNC_AGENTS_ENABLED, models } from '@codebuff/common/constants'
3+
import {
4+
ASYNC_AGENTS_ENABLED,
5+
supportsCacheControl,
6+
} from '@codebuff/common/constants'
47
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
58
import {
69
getToolCallString,
@@ -362,7 +365,7 @@ export const runAgentStep = async (
362365
const agentMessages = getCoreMessagesSubset(
363366
agentMessagesUntruncated,
364367
systemTokens,
365-
agentTemplate.model !== models.openrouter_grok_4
368+
supportsCacheControl(agentTemplate.model)
366369
)
367370

368371
const debugPromptCaching = false

backend/src/tools/stream-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export async function processStreamWithTools<T extends string>(options: {
179179
if (!agentTemplate.toolNames.includes(toolCall.toolName)) {
180180
toolResults.push({
181181
toolName,
182-
toolCallId: generateCompactId(),
182+
toolCallId: toolCall.toolCallId,
183183
result: `Tool \`${toolName}\` is not currently available. Make sure to only use tools listed in the system instructions.`,
184184
})
185185
return

common/src/constants.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,22 @@ export const providerModelNames = {
286286
),
287287
}
288288

289-
export type Model = (typeof models)[keyof typeof models]
289+
export type Model = (typeof models)[keyof typeof models] | (string & {})
290+
291+
const modelsGeneric = models satisfies Record<string, string> as Record<
292+
string,
293+
string | undefined
294+
>
295+
const nonCacheableModels = [
296+
models.openrouter_grok_4,
297+
] satisfies string[] as string[]
298+
export function supportsCacheControl(model: Model): boolean {
299+
if (modelsGeneric[model] === undefined) {
300+
// Default to no cache control for unknown models
301+
return false
302+
}
303+
return !nonCacheableModels.includes(model)
304+
}
290305

291306
export const TEST_USER_ID = 'test-user-id'
292307

common/src/types/dynamic-agent-template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const DynamicAgentTemplateSchema = z.object({
4242
// Required fields for new agents
4343
name: z.string(),
4444
purpose: z.string(),
45-
model: z.enum(filteredModels as [string, ...string[]]),
45+
model: z.string(),
4646
outputMode: z
4747
.enum(['last_message', 'report', 'all_messages'])
4848
.default('last_message'),

common/src/types/session-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const AgentStateSchema: z.ZodType<{
3939
}> = z.lazy(() =>
4040
z.object({
4141
agentId: z.string(),
42-
agentType: agentTemplateTypeSchema.nullable(),
42+
agentType: z.string().nullable(),
4343
agentContext: z.record(z.string(), subgoalSchema),
4444
subagents: AgentStateSchema.array(),
4545
messageHistory: CodebuffMessageSchema.array(),

0 commit comments

Comments
 (0)