Aethel Bot OSS repository! aethel.xyz
bot fun ai discord discord-bot aethel

fix: custom gemini api integration

Changed files
+26 -35
src
commands
utilities
+26 -35
src/commands/utilities/ai.ts
··· 501 501 502 502 if (host === 'generativelanguage.googleapis.com') { 503 503 const base = apiUrl.replace(/\/$/, ''); 504 - const mdl = model.startsWith('models/') ? model : `models/${model}`; 505 - const endpoint = `${base}/v1beta/${mdl}:generateContent?key=${encodeURIComponent(apiKey)}`; 504 + const modelName = model.replace(/^models\//, ''); 505 + const endpoint = `${base}/v1beta/models/${modelName}:generateContent?key=${encodeURIComponent(apiKey)}`; 506 506 507 507 const resp = await fetch(endpoint, { 508 508 method: 'POST', ··· 635 635 .join('\n\n'); 636 636 637 637 const base = config.finalApiUrl.replace(/\/$/, ''); 638 - const mdl = config.finalModel.startsWith('models/') 639 - ? config.finalModel 640 - : `models/${config.finalModel}`; 641 - const endpoint = `${base}/v1beta/${mdl}:generateContent?key=${encodeURIComponent( 638 + const modelName = config.finalModel.replace(/^models\//, ''); 639 + const endpoint = `${base}/v1beta/models/${modelName}:generateContent?key=${encodeURIComponent( 642 640 config.finalApiKey || '', 643 641 )}`; 644 642 ··· 652 650 ], 653 651 }, 654 652 ], 655 - temperature: 0.2, 656 - maxOutputTokens: Math.min(maxTokens, 3000), 653 + generationConfig: { 654 + temperature: 0.2, 655 + maxOutputTokens: Math.min(maxTokens, 3000), 656 + }, 657 657 }; 658 658 659 659 const resp = await fetch(endpoint, { ··· 672 672 const extractTextFromGemini = (obj: unknown): string | null => { 673 673 if (!obj) return null; 674 674 try { 675 + const response = obj as Record<string, unknown>; 676 + 677 + if (Array.isArray(response.candidates) && response.candidates.length > 0) { 678 + const candidate = response.candidates[0] as Record<string, unknown>; 679 + if (candidate.content && typeof candidate.content === 'object') { 680 + const content = candidate.content as { parts?: Array<{ text?: string }> }; 681 + if (Array.isArray(content.parts) && content.parts.length > 0) { 682 + return content.parts 683 + .map((part) => part.text || '') 684 + .filter(Boolean) 685 + .join('\n'); 686 + } 687 + } 688 + } 689 + 675 690 const o = obj as Record<string, unknown>; 676 691 if (Array.isArray(o.candidates) && o.candidates.length) { 677 692 const cand = o.candidates[0] as unknown; ··· 692 707 .filter(Boolean) 693 708 .join('\n'); 694 709 } 695 - if (typeof (cand as Record<string, unknown>).output === 'object') { 696 - const outObj = (cand as Record<string, unknown>).output as Record<string, unknown>; 697 - if (Array.isArray(outObj.content)) { 698 - return (outObj.content as unknown[]) 699 - .map( 700 - (p) => 701 - (p as Record<string, unknown>)?.text || 702 - (p as Record<string, unknown>)?.textRaw || 703 - '', 704 - ) 705 - .filter(Boolean) 706 - .join('\n'); 707 - } 708 - } 709 710 } 710 711 711 - if (Array.isArray(o.outputs) && o.outputs.length) { 712 - const out = o.outputs[0] as unknown; 713 - if (typeof out === 'string') return out; 714 - if (Array.isArray((out as Record<string, unknown>).content)) { 715 - return ((out as Record<string, unknown>).content as unknown[]) 716 - .map((p) => ((p as Record<string, unknown>)?.text as string) || '') 717 - .filter(Boolean) 718 - .join('\n'); 719 - } 720 - } 721 - 712 + // Last resort: try to find any text in the response 722 713 const seen = new Set<unknown>(); 723 714 const queue: unknown[] = [obj]; 724 715 while (queue.length) { ··· 1466 1457 else if (m.startsWith('pplx') || m.includes('perplexity')) providerValue = 'perplexity'; 1467 1458 else if (m.startsWith('deepseek')) providerValue = 'deepseek'; 1468 1459 else if (m.startsWith('moonshot') || m.includes('kimi')) providerValue = 'moonshot'; 1469 - else if (m.startsWith('anthropic') || m.includes('anthropic')) 1460 + else if (m.startsWith('anthropic') || m.includes('anthropic')) { 1470 1461 providerValue = 'anthropic'; 1471 - else if (m.includes('/')) providerValue = 'openrouter'; 1462 + } else if (m.includes('/')) providerValue = 'openrouter'; 1472 1463 else providerValue = 'openai'; 1473 1464 } 1474 1465 if (!apiKey || !model) {