* Gemini side query. Converts Anthropic-format params to Gemini * generateContent format, sends a non-streaming request via fetch, * and wraps the response back into a BetaMessage shape.
( opts: SideQueryOptions, )
| 547 | * and wraps the response back into a BetaMessage shape. |
| 548 | */ |
| 549 | async function sideQueryViaGemini( |
| 550 | opts: SideQueryOptions, |
| 551 | ): Promise<BetaMessage> { |
| 552 | const { |
| 553 | model, |
| 554 | system, |
| 555 | messages, |
| 556 | tools, |
| 557 | tool_choice, |
| 558 | max_tokens = 1024, |
| 559 | temperature, |
| 560 | signal, |
| 561 | } = opts |
| 562 | |
| 563 | const normalizedModel = normalizeModelStringForAPI(model) |
| 564 | const geminiModel = resolveGeminiModel(normalizedModel) |
| 565 | |
| 566 | // Build Gemini contents from Anthropic MessageParam[] |
| 567 | const contents: Array<{ |
| 568 | role: 'user' | 'model' |
| 569 | parts: Array<{ text: string }> |
| 570 | }> = [] |
| 571 | for (const m of messages) { |
| 572 | if (m.role !== 'user' && m.role !== 'assistant') continue |
| 573 | const text = |
| 574 | typeof m.content === 'string' |
| 575 | ? m.content |
| 576 | : Array.isArray(m.content) |
| 577 | ? m.content |
| 578 | .filter( |
| 579 | (b): b is { type: 'text'; text: string } => b.type === 'text', |
| 580 | ) |
| 581 | .map(b => b.text) |
| 582 | .join('\n') |
| 583 | : '' |
| 584 | if (text) { |
| 585 | contents.push({ |
| 586 | role: m.role === 'assistant' ? 'model' : 'user', |
| 587 | parts: [{ text }], |
| 588 | }) |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // Build system instruction |
| 593 | const systemText = extractSystemText(system) |
| 594 | const systemInstruction = systemText |
| 595 | ? { parts: [{ text: systemText }] } |
| 596 | : undefined |
| 597 | |
| 598 | // Convert tools and tool_choice |
| 599 | const geminiTools = |
| 600 | tools && tools.length > 0 |
| 601 | ? anthropicToolsToGemini(tools as BetaToolUnion[]) |
| 602 | : undefined |
| 603 | const geminiToolConfig = tool_choice |
| 604 | ? anthropicToolChoiceToGemini(tool_choice) |
| 605 | : undefined |
| 606 |
no test coverage detected