MCPcopy
hub / github.com/Bistutu/FluentRead / newapi

Function newapi

entrypoints/service/newapi.ts:6–51  ·  view source on GitHub ↗
(message: any)

Source from the content-addressed store, hash-verified

4import { contentPostHandler } from "@/entrypoints/utils/check";
5
6async function newapi(message: any) {
7 try {
8 const headers = new Headers({
9 'Content-Type': 'application/json',
10 'Authorization': `Bearer ${config.token[config.service]}`
11 });
12
13 let url = config.newApiUrl
14
15 if (!url) {
16 throw new Error('New API地址未配置');
17 }
18
19 if (url.endsWith('/')) {
20 url = url.slice(0, -1); // 删除末尾的斜杠
21 }
22
23 // check has /v1
24 if (url.endsWith('/v1')) {
25 url += '/chat/completions';
26 } else if (!url.endsWith('/chat/completions')) {
27 url += '/v1/chat/completions';
28 }
29
30 const resp = await fetch(url, {
31 method: method.POST,
32 headers,
33 body: commonMsgTemplate(message.origin)
34 });
35
36 if (!resp.ok) {
37 throw new Error(`翻译失败: ${resp.status} ${resp.statusText} body: ${await resp.text()}`);
38 }
39
40 const result = await resp.json();
41
42 if (result.choices && result.choices.length > 0) {
43 return contentPostHandler(result.choices[0].message.content);
44 }
45
46 throw new Error('翻译失败: 上游未返回内容');
47 } catch (error) {
48 console.error('API调用失败:', error);
49 throw error;
50 }
51}
52
53export default newapi;

Callers

nothing calls this directly

Calls 2

commonMsgTemplateFunction · 0.90
contentPostHandlerFunction · 0.90

Tested by

no test coverage detected