* OpenAI-compatible side query for OpenAI and Grok providers. * Both use the OpenAI SDK with different base URLs. * * Converts Anthropic-format params to OpenAI Chat Completions, sends a * non-streaming request, and wraps the response back into a BetaMessage * shape so callers remain provider-a
( opts: SideQueryOptions, )
| 402 | * permissionExplainer). |
| 403 | */ |
| 404 | async function sideQueryViaOpenAICompatible( |
| 405 | opts: SideQueryOptions, |
| 406 | ): Promise<BetaMessage> { |
| 407 | const { |
| 408 | model, |
| 409 | system, |
| 410 | messages, |
| 411 | tools, |
| 412 | tool_choice, |
| 413 | max_tokens = 1024, |
| 414 | temperature, |
| 415 | signal, |
| 416 | } = opts |
| 417 | |
| 418 | const provider = getAPIProvider() |
| 419 | const normalizedModel = normalizeModelStringForAPI(model) |
| 420 | |
| 421 | // Resolve model name and client per provider |
| 422 | let openaiModel: string |
| 423 | // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents |
| 424 | let client: import('openai').default |
| 425 | if (provider === 'grok') { |
| 426 | openaiModel = resolveGrokModel(normalizedModel) |
| 427 | client = getGrokClient({ maxRetries: opts.maxRetries ?? 2 }) |
| 428 | } else { |
| 429 | openaiModel = resolveOpenAIModel(normalizedModel) |
| 430 | client = getOpenAIClient({ maxRetries: opts.maxRetries ?? 2 }) |
| 431 | } |
| 432 | |
| 433 | // Build system prompt text |
| 434 | const systemText = extractSystemText(system) |
| 435 | |
| 436 | // Build OpenAI messages: system first, then user/assistant |
| 437 | const openaiMessages: Array<{ |
| 438 | role: 'system' | 'user' | 'assistant' |
| 439 | content: string |
| 440 | }> = [] |
| 441 | if (systemText) { |
| 442 | openaiMessages.push({ role: 'system', content: systemText }) |
| 443 | } |
| 444 | openaiMessages.push(...messageParamsToOpenAIRoleContent(messages)) |
| 445 | |
| 446 | // Convert tools and tool_choice if provided |
| 447 | const openaiTools = |
| 448 | tools && tools.length > 0 |
| 449 | ? anthropicToolsToOpenAI(tools as BetaToolUnion[]) |
| 450 | : undefined |
| 451 | const openaiToolChoice = tool_choice |
| 452 | ? anthropicToolChoiceToOpenAI(tool_choice) |
| 453 | : undefined |
| 454 | |
| 455 | const start = Date.now() |
| 456 | |
| 457 | const requestParams: Record<string, unknown> = { |
| 458 | model: openaiModel, |
| 459 | messages: openaiMessages, |
| 460 | max_tokens, |
| 461 | } |
no test coverage detected