This commit is contained in:
luke 2025-06-23 14:06:27 +08:00
parent 1b0b291dad
commit 254b2591d2
3 changed files with 25 additions and 12 deletions

View File

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

View File

@ -14,4 +14,5 @@
| 2025.06.19 | V2.0.8 | Luke.Ye | 对象存储上传优化,适配后端新接口 | | 2025.06.19 | V2.0.8 | Luke.Ye | 对象存储上传优化,适配后端新接口 |
| 2025.06.20 | V2.0.9 | Luke.Ye | 上传记录页面支持删除历史记录会同时清空对象存储和ES | | 2025.06.20 | V2.0.9 | Luke.Ye | 上传记录页面支持删除历史记录会同时清空对象存储和ES |
| 2025.06.22 | V2.0.10 | Luke.Ye | 搜索结果页分页展示优化 | | 2025.06.22 | V2.0.10 | Luke.Ye | 搜索结果页分页展示优化 |
| 2025.06.23 | V2.0.11 | Luke.Ye | 准备做llm接口迁移 |

View File

@ -367,10 +367,9 @@ async function fetchLLMAnswer(question, container, contentDiv) {
if (!question || !question.trim()) return; if (!question || !question.trim()) return;
const config = await loadMergedConfig(); const config = await loadMergedConfig();
const llmChatCfg = config.apiConfig.llmChat;
const llmToken = await getLLMToken(); const llmToken = await getLLMToken();
const workspaceSlug = await getLLMWorkspaceSlug(llmToken); const userToken = await loadToken();
const url = llmChatCfg.url.replace("${workId}", workspaceSlug); const url = "http://app-test.wisdompulse.cn/api/v1/llm/ask";
const userItem = document.createElement("div"); const userItem = document.createElement("div");
userItem.className = "llm-msg-item llm-user"; userItem.className = "llm-msg-item llm-user";
@ -386,14 +385,16 @@ async function fetchLLMAnswer(question, container, contentDiv) {
let collectedSources = []; let collectedSources = [];
let jumpMap = {}; let jumpMap = {};
try { try {
const body = {}; const body = {
body[llmChatCfg.requestParams[0].field] = question; llmToken,
body[llmChatCfg.requestParams[1].field] = []; question
// 可选扩展参数可追加到 body 中,如 slug/wsName/attachments
};
const res = await fetch(url, { const res = await fetch(url, {
method: "POST", method: "POST",
headers: { headers: {
"Authorization": `Bearer ${llmToken}`, "Authorization": `${userToken}`,
"Content-Type": "application/json" "Content-Type": "application/json"
}, },
body: JSON.stringify(body) body: JSON.stringify(body)
@ -415,10 +416,22 @@ async function fetchLLMAnswer(question, container, contentDiv) {
if (value) { if (value) {
buffer += decoder.decode(value, { stream: true }); buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n"); const lines = buffer.split("\n");
buffer = lines.pop(); buffer = lines.pop(); // 剩下不完整的一行留给下一次
for (let line of lines) { for (let line of lines) {
if (line.startsWith("data: ")) { if (line.startsWith("data: ")) {
const chunk = JSON.parse(line.slice(6)); const chunkStr = line.slice(6).trim();
if (!chunkStr) continue;
let chunk;
try {
chunk = JSON.parse(chunkStr);
} catch (err) {
console.warn("非法 JSON", chunkStr);
continue;
}
// 解析内容片段
if (chunk.sources) { if (chunk.sources) {
for (const s of chunk.sources) { for (const s of chunk.sources) {
if (!collectedSources.find(x => x.id === s.id)) { if (!collectedSources.find(x => x.id === s.id)) {
@ -442,7 +455,6 @@ async function fetchLLMAnswer(question, container, contentDiv) {
answerStr += content; answerStr += content;
} }
// 渲染 markdown代码块加复制按钮
let html = ""; let html = "";
try { try {
html = window.marked ? wrapHtmlWithCopyBtn(marked.parse(answerStr)) : answerStr.replace(/\n/g, "<br>"); html = window.marked ? wrapHtmlWithCopyBtn(marked.parse(answerStr)) : answerStr.replace(/\n/g, "<br>");
@ -456,7 +468,6 @@ async function fetchLLMAnswer(question, container, contentDiv) {
bindAllCopyButtons(msgBox); bindAllCopyButtons(msgBox);
insertCopyFullAnswerBtn(msgBox, answerStr); insertCopyFullAnswerBtn(msgBox, answerStr);
contentDiv.scrollTop = contentDiv.scrollHeight; contentDiv.scrollTop = contentDiv.scrollHeight;
} }
} }
@ -478,3 +489,4 @@ async function fetchLLMAnswer(question, container, contentDiv) {
msgBox.innerHTML += `<span style='color:red;'>LLM异常${e}</span>`; msgBox.innerHTML += `<span style='color:red;'>LLM异常${e}</span>`;
} }
} }