@@ -229,18 +229,15 @@ <h2 class="text-2xl font-bold text-white mb-2">設定</h2>
229229 < div class ="input-group ">
230230 < label for ="modelSelect "> 選擇 AI 模型:</ label >
231231 < select id ="modelSelect " class ="focus:border-blue-400 ">
232- < option value ="gemini-2.5-flash-preview-05-20 "> Gemini 2.5 Flash (預覽版 05-20) </ option >
232+ < option value ="gemini-2.5-flash "> Gemini 2.5 Flash</ option >
233233 <!-- 將來可以在這裡添加更多模型選項 -->
234234 </ select >
235235 </ div >
236236 < div class ="input-group ">
237237
238238 < label for ="apiKeySelect "> 選擇 API Key 配置:</ label >
239239 < select id ="apiKeySelect " class ="focus:border-blue-400 ">
240- < option value ="AIzaSyBasaBU3srwcOqVQoyT7uZmtXPa4NRi6gU "> TEST1 AIzaSyBasaBU3srwcOqVQoyT7uZmtXPa4NRi6gU)</ option >
241- < option value ="AIzaSyC_1HJbHryk0t9mwRSEJnlhjYDIzSuo8oY "> TEST2 AIzaSyC_1HJbHryk0t9mwRSEJnlhjYDIzSuo8oY</ option >
242- < option value ="AIzaSyCOFOoppNQRakvBcKyKmWHEHpMBPODi9s4 "> TEST3 AIzaSyCOFOoppNQRakvBcKyKmWHEHpMBPODi9s4</ option >
243- < option value ="AIzaSyBl2ysPjnepR6exiYgeTnjim3IBEagTY8w "> MY AIzaSyBl2ysPjnepR6exiYgeTnjim3IBEagTY8w</ option >
240+ < option value =""> default api</ option >
244241 < option value ="AIzaSyBl2ysPjnepR6exiYgeTnjim3IBEagTY9w "> ERROR TEST</ option >
245242 < option value ="manual "> 手動輸入 API Key</ option > <!-- 新增手動輸入選項 -->
246243 </ select >
@@ -449,6 +446,9 @@ <h2 class="text-2xl font-bold text-white mb-2">設定</h2>
449446 try {
450447 const response = await fetch ( url , options ) ;
451448 if ( response . status === 429 ) { // Too Many Requests
449+ if ( i === retries - 1 ) {
450+ throw new Error ( `API 已達請求上限,請稍後再試。 (API rate limit exceeded after ${ retries } retries)` ) ;
451+ }
452452 console . warn ( `API 限流,正在重試... (第 ${ i + 1 } 次)` ) ;
453453 await new Promise ( res => setTimeout ( res , delay ) ) ;
454454 delay *= 2 ; // 雙倍延遲
@@ -517,18 +517,40 @@ <h2 class="text-2xl font-bold text-white mb-2">設定</h2>
517517 }
518518
519519 const selectedModel = modelSelect . value ;
520- const selectedApiKey = apiKeySelect . value ;
521520 const maxTokens = parseInt ( maxTokensInput . value , 10 ) ;
522521
523- // 使用選擇的 API Key 構建 URL
524- const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/${ selectedModel } :generateContent?key=${ selectedApiKey } ` ;
522+ let apiUrl ;
523+ let payload ;
524+
525+ if ( apiKeySelect . value === "" ) { // 使用後端代理 (default api)
526+ apiUrl = '/api/proxy/text' ; // 指向我們自己的後端代理
527+ payload = {
528+ prompt : userPrompt , // 代理只需要 prompt
529+ // 如果你的代理需要處理歷史紀錄,也需要傳遞
530+ // history: includeHistoryCheckbox.checked ? chatHistory.map(msg => ({ role: msg.role, parts: msg.parts })) : []
531+ } ;
532+ // 注意:當使用代理時,我們需要確保發送的 payload 格式與代理期望的格式一致。
533+ // 這裡我們假設代理期望的格式是 { prompt: "..." }。
534+ // 如果要支持歷史紀錄,後端代理 `server.js` 也需要修改以接收和處理 `history`。
535+ // 為了簡化,我們暫時只傳遞當前的 prompt。
536+ const proxyContents = includeHistoryCheckbox . checked ? chatHistory . map ( msg => ( { role : msg . role , parts : msg . parts } ) ) : [ { role : "user" , parts : [ { text : userPrompt } ] } ] ;
537+ payload = {
538+ contents : proxyContents ,
539+ generationConfig : {
540+ maxOutputTokens : maxTokens
541+ }
542+ } ;
525543
526- const payload = {
527- contents : contentsToSend ,
528- generationConfig : {
529- maxOutputTokens : maxTokens // 設定最大生成字數
530- }
531- } ;
544+
545+ } else { // 直接呼叫 Google API
546+ apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/${ selectedModel } :generateContent?key=${ currentApiKey } ` ;
547+ payload = {
548+ contents : contentsToSend ,
549+ generationConfig : {
550+ maxOutputTokens : maxTokens
551+ }
552+ } ;
553+ }
532554
533555 try {
534556 const response = await fetchWithExponentialBackoff ( apiUrl , {
@@ -634,6 +656,7 @@ <h2 class="text-2xl font-bold text-white mb-2">設定</h2>
634656 // 應用 Tailwind 樣式
635657 input . className = 'focus:border-blue-400 mt-2 w-full p-3 rounded-xl border border-gray-600 bg-gray-700 text-gray-100 outline-none transition duration-200 ease-in-out' ;
636658 manualApiKeyInputContainer . appendChild ( input ) ;
659+
637660 input . focus ( ) ; // 聚焦到新的輸入框
638661 }
639662 } ) ;
0 commit comments