优化test页面

This commit is contained in:
luke 2025-07-04 00:05:15 +08:00
parent 57db5e4369
commit 92ce1bf574
4 changed files with 57 additions and 39 deletions

View File

@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "渠道应用开发部知识库",
"version": "2.0.13",
"version": "2.0.14",
"description": "通过关键词快速搜索内部文档",
"permissions": ["storage"],
"host_permissions": [

View File

@ -8,6 +8,11 @@
<h3>SSE 结果:</h3>
<pre id="output"></pre>
<script src="../scripts/config.js"></script>
<script src="../scripts/utils.js"></script>
<script src="../scripts/auth.js"></script>
<script src="../scripts/llm.js"></script>
<script src="../scripts/popup.js"></script>
<script src="../scripts/test.js"></script>
</body>
</html>

View File

@ -17,4 +17,6 @@
| 2025.06.23 | V2.0.11 | Luke.Ye | 准备做llm接口迁移 |
| 2025.06.24 | V2.0.12 | Luke.Ye | 添加测试页面 |
| 2025.06.28 | V2.0.13 | Luke.Ye | 优化分页展示 |
| 2025.07.03 | V2.0.14 | Luke.Ye | test页面逻辑优化 |

View File

@ -1,48 +1,59 @@
const output = document.getElementById("output");
document.addEventListener("DOMContentLoaded", async () => {
const output = document.getElementById("output");
fetch("http://localhost:18081/api/v1/llm/demo", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "text/event-stream",
"Authorization": "Bearer test"
},
body: JSON.stringify({})
}).then(response => {
const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8");
let buffer = "";
llmToken = await getLLMToken();
const auth = await loadUserToken();
if (!auth) {
handleLogout(auth);
}
const userToken = auth.token;
const url = "http://app-test.wisdompulse.cn/api/v1/llm/ask";
const question = "请背诵王勃的《滕王阁序》,开头是'豫章故郡,洪都新府'这句,并基于这篇文章写一个小故事"
const body = { llmToken, question };
function read() {
reader.read().then(({ done, value }) => {
if (done) {
output.textContent += "\n[连接结束]";
return;
}
fetch(url, {
method: "POST",
headers: {
"Authorization": `${userToken}`,
"Accept": "text/event-stream",
"Content-Type": "application/json"
},
body: JSON.stringify(body)
}).then(response => {
const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8");
let buffer = "";
buffer += decoder.decode(value, { stream: true });
function read() {
reader.read().then(({ done, value }) => {
if (done) {
output.textContent += "\n[连接结束]";
return;
}
// 拆解成 SSE 事件格式:每段以 \n\n 分隔
const parts = buffer.split('\n\n');
buffer = parts.pop(); // 留给下一次未完整的部分
buffer += decoder.decode(value, { stream: true });
for (const part of parts) {
const line = part.trim();
if (line.startsWith('data:')) {
const jsonStr = line.substring(5).trim();
try {
const data = JSON.parse(jsonStr);
output.textContent += `收到:${data.content || '[无内容]'}\n`;
} catch (e) {
output.textContent += `解析失败:${jsonStr}\n`;
// 拆解成 SSE 事件格式:每段以 \n\n 分隔
const parts = buffer.split('\n\n');
buffer = parts.pop(); // 留给下一次未完整的部分
for (const part of parts) {
const line = part.trim();
if (line.startsWith('data:')) {
const jsonStr = line.substring(5).trim();
try {
const data = JSON.parse(jsonStr);
output.textContent += `${data.textResponse || '[无内容]'}`;
} catch (e) {
output.textContent += `解析失败:${jsonStr}\n`;
}
}
}
}
read();
});
}
read();
});
}
read();
read();
});
});