为非流也加上请求器逻辑,优化重复代码
Browse files- src/api/client.js +84 -91
src/api/client.js
CHANGED
|
@@ -71,7 +71,6 @@ export async function generateAssistantResponse(requestBody, callback) {
|
|
| 71 |
throw new Error('没有可用的token,请运行 npm run login 获取token');
|
| 72 |
}
|
| 73 |
|
| 74 |
-
const url = config.api.url;
|
| 75 |
const headers = {
|
| 76 |
'Host': config.api.host,
|
| 77 |
'User-Agent': config.api.userAgent,
|
|
@@ -80,50 +79,70 @@ export async function generateAssistantResponse(requestBody, callback) {
|
|
| 80 |
'Accept-Encoding': 'gzip'
|
| 81 |
};
|
| 82 |
|
| 83 |
-
if (useNativeFetch) {
|
| 84 |
-
return await generateWithNativeFetch(url, headers, requestBody, callback, token);
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
const streamResponse = requester.antigravity_fetchStream(url, {
|
| 88 |
-
method: 'POST',
|
| 89 |
-
headers,
|
| 90 |
-
body: JSON.stringify(requestBody)
|
| 91 |
-
});
|
| 92 |
-
|
| 93 |
const state = { thinkingStarted: false, toolCalls: [] };
|
| 94 |
let buffer = '';
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
.
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
}
|
| 128 |
|
| 129 |
export async function getAvailableModels() {
|
|
@@ -133,15 +152,18 @@ export async function getAvailableModels() {
|
|
| 133 |
throw new Error('没有可用的token,请运行 npm run login 获取token');
|
| 134 |
}
|
| 135 |
|
| 136 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
method: 'POST',
|
| 138 |
-
headers
|
| 139 |
-
'Host': config.api.host,
|
| 140 |
-
'User-Agent': config.api.userAgent,
|
| 141 |
-
'Authorization': `Bearer ${token.access_token}`,
|
| 142 |
-
'Content-Type': 'application/json',
|
| 143 |
-
'Accept-Encoding': 'gzip'
|
| 144 |
-
},
|
| 145 |
body: JSON.stringify({})
|
| 146 |
});
|
| 147 |
|
|
@@ -158,38 +180,7 @@ export async function getAvailableModels() {
|
|
| 158 |
};
|
| 159 |
}
|
| 160 |
|
| 161 |
-
async function generateWithNativeFetch(url, headers, requestBody, callback, token) {
|
| 162 |
-
const response = await fetch(url, {
|
| 163 |
-
method: 'POST',
|
| 164 |
-
headers,
|
| 165 |
-
body: JSON.stringify(requestBody)
|
| 166 |
-
});
|
| 167 |
-
|
| 168 |
-
if (!response.ok) {
|
| 169 |
-
const errorBody = await response.text();
|
| 170 |
-
if (response.status === 403) {
|
| 171 |
-
tokenManager.disableCurrentToken(token);
|
| 172 |
-
throw new Error(`该账号没有使用权限,已自动禁用。错误详情: ${errorBody}`);
|
| 173 |
-
}
|
| 174 |
-
throw new Error(`API请求失败 (${response.status}): ${errorBody}`);
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
const state = { thinkingStarted: false, toolCalls: [] };
|
| 178 |
-
const reader = response.body.getReader();
|
| 179 |
-
const decoder = new TextDecoder();
|
| 180 |
-
let buffer = '';
|
| 181 |
-
|
| 182 |
-
while (true) {
|
| 183 |
-
const { done, value } = await reader.read();
|
| 184 |
-
if (done) break;
|
| 185 |
|
| 186 |
-
buffer += decoder.decode(value, { stream: true });
|
| 187 |
-
const lines = buffer.split('\n');
|
| 188 |
-
buffer = lines.pop();
|
| 189 |
-
|
| 190 |
-
lines.forEach(line => processStreamLine(line, state, callback));
|
| 191 |
-
}
|
| 192 |
-
}
|
| 193 |
|
| 194 |
export async function generateAssistantResponseNoStream(requestBody) {
|
| 195 |
const token = await tokenManager.getToken();
|
|
@@ -198,19 +189,21 @@ export async function generateAssistantResponseNoStream(requestBody) {
|
|
| 198 |
throw new Error('没有可用的token,请运行 npm run login 获取token');
|
| 199 |
}
|
| 200 |
|
| 201 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
method: 'POST',
|
| 203 |
-
headers
|
| 204 |
-
'Host': config.api.host,
|
| 205 |
-
'User-Agent': config.api.userAgent,
|
| 206 |
-
'Authorization': `Bearer ${token.access_token}`,
|
| 207 |
-
'Content-Type': 'application/json',
|
| 208 |
-
'Accept-Encoding': 'gzip'
|
| 209 |
-
},
|
| 210 |
body: JSON.stringify(requestBody)
|
| 211 |
});
|
| 212 |
-
|
| 213 |
-
if (!response.ok) {
|
| 214 |
const errorBody = await response.text();
|
| 215 |
if (response.status === 403) {
|
| 216 |
tokenManager.disableCurrentToken(token);
|
|
|
|
| 71 |
throw new Error('没有可用的token,请运行 npm run login 获取token');
|
| 72 |
}
|
| 73 |
|
|
|
|
| 74 |
const headers = {
|
| 75 |
'Host': config.api.host,
|
| 76 |
'User-Agent': config.api.userAgent,
|
|
|
|
| 79 |
'Accept-Encoding': 'gzip'
|
| 80 |
};
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
const state = { thinkingStarted: false, toolCalls: [] };
|
| 83 |
let buffer = '';
|
| 84 |
+
|
| 85 |
+
const processChunk = (chunk) => {
|
| 86 |
+
buffer += chunk;
|
| 87 |
+
const lines = buffer.split('\n');
|
| 88 |
+
buffer = lines.pop();
|
| 89 |
+
lines.forEach(line => processStreamLine(line, state, callback));
|
| 90 |
+
};
|
| 91 |
+
|
| 92 |
+
if (useNativeFetch) {
|
| 93 |
+
const response = await fetch(config.api.url, {
|
| 94 |
+
method: 'POST',
|
| 95 |
+
headers,
|
| 96 |
+
body: JSON.stringify(requestBody)
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
if (!response.ok) {
|
| 100 |
+
const errorBody = await response.text();
|
| 101 |
+
if (response.status === 403) tokenManager.disableCurrentToken(token);
|
| 102 |
+
throw new Error(`API请求失败 (${response.status}): ${errorBody}`);
|
| 103 |
+
}
|
| 104 |
|
| 105 |
+
const reader = response.body.getReader();
|
| 106 |
+
const decoder = new TextDecoder();
|
| 107 |
+
|
| 108 |
+
while (true) {
|
| 109 |
+
const { done, value } = await reader.read();
|
| 110 |
+
if (done) break;
|
| 111 |
+
processChunk(decoder.decode(value, { stream: true }));
|
| 112 |
+
}
|
| 113 |
+
} else {
|
| 114 |
+
const streamResponse = requester.antigravity_fetchStream(config.api.url, {
|
| 115 |
+
method: 'POST',
|
| 116 |
+
headers,
|
| 117 |
+
body: JSON.stringify(requestBody)
|
| 118 |
+
});
|
| 119 |
+
|
| 120 |
+
let errorBody = '';
|
| 121 |
+
let statusCode = null;
|
| 122 |
+
|
| 123 |
+
await new Promise((resolve, reject) => {
|
| 124 |
+
streamResponse
|
| 125 |
+
.onStart(({ status }) => {
|
| 126 |
+
statusCode = status;
|
| 127 |
+
if (status === 403) tokenManager.disableCurrentToken(token);
|
| 128 |
+
})
|
| 129 |
+
.onData((chunk) => {
|
| 130 |
+
if (statusCode !== 200) {
|
| 131 |
+
errorBody += chunk;
|
| 132 |
+
} else {
|
| 133 |
+
processChunk(chunk);
|
| 134 |
+
}
|
| 135 |
+
})
|
| 136 |
+
.onEnd(() => {
|
| 137 |
+
if (statusCode !== 200) {
|
| 138 |
+
reject(new Error(`API请求失败 (${statusCode}): ${errorBody}`));
|
| 139 |
+
} else {
|
| 140 |
+
resolve();
|
| 141 |
+
}
|
| 142 |
+
})
|
| 143 |
+
.onError(reject);
|
| 144 |
+
});
|
| 145 |
+
}
|
| 146 |
}
|
| 147 |
|
| 148 |
export async function getAvailableModels() {
|
|
|
|
| 152 |
throw new Error('没有可用的token,请运行 npm run login 获取token');
|
| 153 |
}
|
| 154 |
|
| 155 |
+
const headers = {
|
| 156 |
+
'Host': config.api.host,
|
| 157 |
+
'User-Agent': config.api.userAgent,
|
| 158 |
+
'Authorization': `Bearer ${token.access_token}`,
|
| 159 |
+
'Content-Type': 'application/json',
|
| 160 |
+
'Accept-Encoding': 'gzip'
|
| 161 |
+
};
|
| 162 |
+
|
| 163 |
+
const fetchFn = useNativeFetch ? fetch : (url, opts) => requester.antigravity_fetch(url, opts);
|
| 164 |
+
const response = await fetchFn(config.api.modelsUrl, {
|
| 165 |
method: 'POST',
|
| 166 |
+
headers,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
body: JSON.stringify({})
|
| 168 |
});
|
| 169 |
|
|
|
|
| 180 |
};
|
| 181 |
}
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
export async function generateAssistantResponseNoStream(requestBody) {
|
| 186 |
const token = await tokenManager.getToken();
|
|
|
|
| 189 |
throw new Error('没有可用的token,请运行 npm run login 获取token');
|
| 190 |
}
|
| 191 |
|
| 192 |
+
const headers = {
|
| 193 |
+
'Host': config.api.host,
|
| 194 |
+
'User-Agent': config.api.userAgent,
|
| 195 |
+
'Authorization': `Bearer ${token.access_token}`,
|
| 196 |
+
'Content-Type': 'application/json',
|
| 197 |
+
'Accept-Encoding': 'gzip'
|
| 198 |
+
};
|
| 199 |
+
|
| 200 |
+
const fetchFn = useNativeFetch ? fetch : (url, opts) => requester.antigravity_fetch(url, opts);
|
| 201 |
+
const response = await fetchFn(config.api.noStreamUrl, {
|
| 202 |
method: 'POST',
|
| 203 |
+
headers,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
body: JSON.stringify(requestBody)
|
| 205 |
});
|
| 206 |
+
if (response.status !== 200) {
|
|
|
|
| 207 |
const errorBody = await response.text();
|
| 208 |
if (response.status === 403) {
|
| 209 |
tokenManager.disableCurrentToken(token);
|