(message: any)
| 5 | import { services } from "../utils/option"; |
| 6 | |
| 7 | async function common(message: any) { |
| 8 | try { |
| 9 | |
| 10 | const headers = new Headers({ |
| 11 | 'Content-Type': 'application/json', |
| 12 | 'Authorization': `Bearer ${config.token[config.service]}` |
| 13 | }); |
| 14 | |
| 15 | if(config.service === services.openrouter){ |
| 16 | headers.append('HTTP-Referer', 'https://fluent.thinkstu.com'); |
| 17 | headers.append('X-Title', 'FluentRead'); |
| 18 | } |
| 19 | |
| 20 | const url = config.proxy[config.service] || urls[config.service]; |
| 21 | |
| 22 | const resp = await fetch(url, { |
| 23 | method: method.POST, |
| 24 | headers, |
| 25 | body: commonMsgTemplate(message.origin) |
| 26 | }); |
| 27 | |
| 28 | if (!resp.ok) { |
| 29 | throw new Error(`翻译失败: ${resp.status} ${resp.statusText} body: ${await resp.text()}`); |
| 30 | } |
| 31 | |
| 32 | const result = await resp.json(); |
| 33 | return contentPostHandler(result.choices[0].message.content); |
| 34 | } catch (error) { |
| 35 | console.error('API调用失败:', error); |
| 36 | throw error; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | export default common; |
nothing calls this directly
no test coverage detected