-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallAPI.js
More file actions
374 lines (338 loc) · 15.1 KB
/
callAPI.js
File metadata and controls
374 lines (338 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
const knownURI = {
'openrouter': 'https://openrouter.ai/api',
'aliyun': 'https://dashscope.aliyuncs.com/compatible-mode'
}
window.translate_known = function(name) {
return knownURI[name] || name
}
async function callAPI(messages, options = {}) {
const {
stream = true,
onToken = null,
onComplete = null,
onError = null,
onReason = null,
} = options;
try {
const requestBody = {
model: state.model,
messages: messages.map(msg => (msg.name + msg.content ? ({
role: msg.role,
content: msg.name ? msg.name + ": " + msg.content : msg.content,
}) : null)),
stream
};
requestBody.messages = requestBody.messages.filter(msg => msg);
state.parameters.forEach(param => {
requestBody[param.name] = JSON.parse(param.value);
});
// 在callAPI函数中的manual模式部分修改为:
if (state.endpoint === 'manual') {
const requestBody = {
model: state.model,
messages: messages.map(msg => (msg.name + msg.content ? ({
role: msg.role,
content: msg.name ? msg.name + ": " + msg.content : msg.content,
name: msg.name,
compressed: msg.compressed,
}) : null)),
stream
};
const manualResponse = await showManualInputDialog(
messages, // 传入完整的messages数组
requestBody
);
if (!manualResponse) {
throw new Error('Manual input cancelled');
}
if (stream) {
/*
const chars = manualResponse.split('');
let fullContent = '';
for (const char of chars) {
if (onToken) {
fullContent += char;
onToken(char, fullContent);
await new Promise(resolve => setTimeout(resolve, 10)); // 添加少许延迟
}
}*/
// treat all as a token because we are in manual mode
if (onToken) onToken(manualResponse, manualResponse);
if (onComplete) onComplete(manualResponse);
return manualResponse;
} else {
if (onComplete) onComplete(manualResponse);
return manualResponse;
}
}
// 如果是openrouter,使用完整API endpoint
/*
const apiEndpoint = state.endpoint === 'openrouter'
? 'https://openrouter.ai/api/v1/chat/completions'
: state.endpoint + '/v1/chat/completions';
*/
const apiEndpoint1 = (state.endpoint in knownURI ? knownURI[state.endpoint] : state.endpoint);
const apiEndpoint2 = apiEndpoint1;
const apiEndpoint = (apiEndpoint2 + '/chat/completions').replace(/(?<!:)\/\/+/g, '/');
const response = await fetch(apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${state.bearer}`,
'X-Title': 'UYP App',
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
throw new Error(`API Error: ${response.status}`);
}
if (stream) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let fullContent = '';
let fullReason = '';
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
while (true) {
const newlineIndex = buffer.indexOf('\n');
if (newlineIndex === -1) break;
const line = buffer.slice(0, newlineIndex);
buffer = buffer.slice(newlineIndex + 1);
if (line.trim() === '' || line.trim() === 'data: [DONE]') continue;
if (line.startsWith('data: ')) {
const jsonStr = line.slice(6);
try {
const json = JSON.parse(jsonStr);
if (json.choices && json.choices[0].delta && json.choices[0].delta.content) {
const token = json.choices[0].delta.content;
fullContent += token;
if (onToken) onToken(token, fullContent);
}
if (json.choices && json.choices[0].delta && (json.choices[0].delta.reasoning_content || json.choices[0].delta.reasoning)) {
const token = json.choices[0].delta.reasoning_content || json.choices[0].delta.reasoning;
fullReason += token;
if (onReason) {
onReason(token, fullReason);
} else {
if (onToken) onToken(token,
'<think>' + fullReason + '</think>' + fullContent);
}
}
} catch (e) {
console.warn('Failed to parse JSON:', jsonStr);
}
}
}
}
if (onComplete) onComplete(fullContent);
return fullContent;
} else {
const json = await response.json();
const content = json.choices[0].message.content;
if (onComplete) onComplete(content);
return content;
}
} catch (error) {
if (onError) onError(error);
throw error;
}
}
function showManualInputDialog(messages, requestBody) {
console.log(messages)
return new Promise((resolve) => {
const formatJSON = (obj) => {
return JSON.stringify(obj, null, 2);
};
const formattedMessages = messages.map(msg => {
let context
if (msg.compressed) {
context = "[这部分已经经过压缩]\n" + msg.compressed
} else {
context = msg.content
}
return `---[${msg.name}:${msg.role}]---\n\n${context}`;
}).join('\n\n');
const modalHtml = `
<div data-id="manualInputModal" style="
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
">
<div style="
background: white;
padding: 20px;
border-radius: 8px;
width: 90%;
max-width: 1200px;
max-height: 90vh;
display: flex;
flex-direction: column;
gap: 10px;
overflow-y: auto;
">
<h3 style="margin: 0;">手动输入回复</h3>
<div style="display: flex; gap: 10px; height: calc(90vh - 200px);">
<!-- 左侧面板 -->
<div style="
flex: 1;
display: flex;
flex-direction: column;
gap: 10px;
overflow-y: auto;
">
<div style="
border: 1px solid #e0e0e0;
border-radius: 4px;
padding: 10px;
">
<strong>完整会话记录:</strong>
<textarea data-id="formattedMessages" style="
width: 100%;
min-height: 200px;
background: #f8f9fa;
padding: 10px;
border-radius: 4px;
border: 1px solid #ddd;
margin: 5px 0;
font-size: 12px;
font-family: monospace;
resize: vertical;
">${formattedMessages}</textarea>
<div style="
display: flex;
gap: 10px;
margin-top: 5px;
">
<button data-id='copyMessages' style="
padding: 4px 12px;
background: #2196F3;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
">复制会话记录</button>
</div>
</div>
<div style="
border: 1px solid #e0e0e0;
border-radius: 4px;
padding: 10px;
">
<strong>请求体:</strong>
<textarea data-id="requestBodyJson" style="
width: 100%;
min-height: 200px;
background: #f8f9fa;
padding: 10px;
border-radius: 4px;
border: 1px solid #ddd;
margin: 5px 0;
font-size: 12px;
font-family: monospace;
resize: vertical;
"></textarea>
<button data-id='copyJson' style="
padding: 4px 12px;
background: #2196F3;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
margin-top: 5px;
">复制请求体</button>
</div>
</div>
<!-- 右侧面板 -->
<div style="
flex: 1;
display: flex;
flex-direction: column;
gap: 10px;
">
<strong>输入回复:</strong>
<textarea data-id="manualResponse" style="
flex-grow: 1;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
resize: none;
font-family: monospace;
white-space: pre-wrap;
"></textarea>
<div style="
display: flex;
justify-content: flex-end;
gap: 10px;
">
<button data-id='confirm' style="
padding: 8px 16px;
background: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
">提交</button>
</div>
</div>
</div>
</div>
</div>
`;
const modalContainer = document.createElement('div');
modalContainer.innerHTML = modalHtml;
document.body.appendChild(modalContainer);
const messagesText = modalContainer.querySelector('[data-id=formattedMessages]');
const copyMessages = modalContainer.querySelector('[data-id=copyMessages]');
const requestBodyJson = modalContainer.querySelector('[data-id=requestBodyJson]');
const copyJson = modalContainer.querySelector('[data-id=copyJson]');
const manualResponse = modalContainer.querySelector('[data-id=manualResponse]');
const confirm = modalContainer.querySelector('[data-id=confirm]');
messagesText.value = formattedMessages;
requestBodyJson.value = JSON.stringify(requestBody, null, 2);
copyMessages.addEventListener('click', () => copyToClipboard(messagesText, copyMessages));
copyJson.addEventListener('click', () => copyToClipboard(requestBodyJson, copyJson));
confirm.addEventListener('click', () => submitManualResponse());
// 复制到剪贴板功能
async function copyToClipboard (element, button) {
try {
await navigator.clipboard.writeText(element.value);
const originalText = button.textContent;
button.textContent = '已复制!';
button.style.background = '#4CAF50';
setTimeout(() => {
button.textContent = originalText;
button.style.background = '#2196F3';
}, 1000);
} catch (err) {
console.error('复制失败:', err);
alert('复制失败,请手动复制');
}
};
// 提交功能
function submitManualResponse() {
const response = manualResponse.value;
document.body.removeChild(modalContainer);
resolve(response);
};
// ESC键关闭功能
document.addEventListener('keydown', function escListener(e) {
if (e.key === 'Escape') {
document.body.removeChild(modalContainer);
document.removeEventListener('keydown', escListener);
throw new Error('用户取消');
}
});
});
}