| 157 | ): Promise<string> { |
| 158 | const url = `${trimBase(provider.base_url)}/v1beta/models/${provider.model}:generateContent`; |
| 159 | const headers: Record<string, string> = { "Content-Type": "application/json" }; |
| 160 | const params: Record<string, string> = {}; |
| 161 | |
| 162 | if (provider.auth_method === "query_param" || provider.auth_method === "bearer_token") { |
| 163 | params["key"] = provider.api_key; |
| 164 | } |
| 165 | if (provider.auth_method === "api_key_header") { |
| 166 | headers["x-goog-api-key"] = provider.api_key; |
| 167 | } |
| 168 | |
| 169 | const response = await axios.post( |
| 170 | url, |
| 171 | { |
| 172 | contents: [{ role: "user", parts: [{ text: `${prompt}\n\n${content}` }] }] |
| 173 | }, |
| 174 | { headers, params, timeout } |
| 175 | ); |
| 176 | |
| 177 | const parts = response.data?.candidates?.[0]?.content?.parts || []; |
| 178 | return parts.map((p: any) => p.text || "").join(""); |
| 179 | } |
| 180 | |
| 181 | async function callAI(provider: Provider, prompt: string, content: string, timeout: number): Promise<string> { |
| 182 | if (provider.type === "gemini") { |
| 183 | return await callGemini(provider, prompt, content, timeout); |
| 184 | } |
| 185 | return await callOpenAI(provider, prompt, content, timeout); |
| 186 | } |
| 187 | |
| 188 | // ========== 消息收集 ========== |
| 189 | type MessageData = { time: string; sender: string; text: string }; |