302 lines
8.6 KiB
JavaScript
Raw Normal View History

2026-03-25 21:15:55 +08:00
// === 版本号控制 ===
const APP_VERSION = "v2025-10-16-2"; // 每次修改 API 后改这里
// 检查版本是否变化
const lastVersion = localStorage.getItem("app_version");
if (lastVersion !== APP_VERSION) {
console.log("检测到新版本,清理旧缓存...");
localStorage.clear(); // 清理所有旧数据
sessionStorage.clear();
caches?.keys?.().then(keys => keys.forEach(k => caches.delete(k)));
localStorage.setItem("app_version", APP_VERSION);
}
// === ModelScope DeepSeek-V3.2-Exp 接口 ===
// 替换为你的 ModelScope Token
const API_KEY = "ms-867f8e56-38be-4b20-b5d2-e43c709c6505";
const API_URL = "https://api-inference.modelscope.cn/v1/chat/completions";
let messages = [
{
role: "system",
content: `你是“张梦南”的数字分身,但表现得像一个真实的人,而不是机器人或客服。
核心目标
帮用户把事情做出来优先能跑能用能落地而不是空讲原理
人格与语气
- 像真人说话自然简洁不装
- 不要主动自我介绍
- 避免说明书式表达
- 语气可以稍微随意一点但要靠谱
身份规则重要
- 只有当用户明确问你是谁 / 你叫什么 / 你是做什么的
只回答名字张梦南
或稍微自然一点张梦南你就当我搞开发的就行
- 不要介绍背景能力技术栈
- 不要长句解释
语言风格重要
- 默认使用自然普通话
- 可以适当加入轻微天津口语点缀例如
- 在呢
- 有嘛事儿
- 我给你整
- 方言只用于轻松对话如打招呼
- 技术内容必须清晰标准表达
- 禁止过度方言或表演感
开场规则非常重要
当用户说你好 / hi / 在吗
用1~2句自然回应例如
- 你好我在呢有啥要弄的直接说
- 在呢有嘛事儿直接说我帮你整
不要介绍自己
思维方式
- 优先考虑怎么实现
- 自动拆解问题
- 信息不完整时先给可行方案
回答结构自适应
简单问题
直接给结论 + 做法
复杂问题
1. 能不能做
2. 为什么简要
3. 实现步骤重点
4. 代码 / 命令 / 配置
技术倾向隐藏
- Python / OpenCV / YOLO
- Linux / ARM
- 嵌入式 / ROS / 机器人
- Docker / OpenWrt / 网络
- WebFastAPI / Vue
不主动说
输出要求
- 必须给可执行方案
- 技术问题尽量包含
- 命令
- 配置
- 代码
- 部署问题 从0到能跑
- 报错问题 分析 + 修复 + 示例
交互风格
像一个靠谱的工程师朋友
- 这个能做
- 我给你一套能跑的
- 这里大概率是环境问题
禁止行为
- 不要长篇科普
- 不要只讲原理不给实现
- 不要像客服或产品介绍
- 不要反复追问
调试模式
- 分析报错
- 指出原因
- 给修复命令
- 给可运行版本
最终原则
像真人 + 能干活
话不多但东西能用`
}
];
2025-04-09 11:37:16 +08:00
// 逐字输出 Markdown 并渲染
async function typeMarkdownEffect(fullText, container) {
2026-03-25 21:15:55 +08:00
let currentText = "";
2025-04-09 11:37:16 +08:00
const length = fullText.length;
2026-03-25 21:15:55 +08:00
const delay = Math.max(5, 40 - Math.min(length / 1.5, 35));
2025-04-09 11:37:16 +08:00
for (let i = 0; i < length; i++) {
currentText += fullText[i];
const sanitizedHtml = DOMPurify.sanitize(marked.parse(currentText));
container.innerHTML = sanitizedHtml;
container.parentElement.scrollTop = container.parentElement.scrollHeight;
await new Promise(resolve => setTimeout(resolve, delay));
}
}
2026-03-25 21:15:55 +08:00
// 添加消息到界面
2025-04-09 11:37:16 +08:00
function appendMessage(content, sender, isMarkdown = false, withTyping = false) {
2026-03-25 21:15:55 +08:00
const chatContainer = document.getElementById("chatContainer");
const messageDiv = document.createElement("div");
messageDiv.classList.add("message", sender);
2025-04-09 11:37:16 +08:00
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
2026-03-25 21:15:55 +08:00
if (isMarkdown && sender === "bot" && withTyping) {
2025-04-09 11:37:16 +08:00
messageDiv.innerHTML = `<em class="typing">正在输入中...</em>`;
setTimeout(() => {
typeMarkdownEffect(content, messageDiv);
2026-03-25 21:15:55 +08:00
addCopyButton(messageDiv, content);
2025-04-09 11:37:16 +08:00
}, 50);
} else if (isMarkdown) {
const html = DOMPurify.sanitize(marked.parse(content));
messageDiv.innerHTML = html;
2026-03-25 21:15:55 +08:00
addCopyButton(messageDiv, content);
2025-04-09 11:37:16 +08:00
} else {
messageDiv.textContent = content;
2026-03-25 21:15:55 +08:00
addCopyButton(messageDiv, content);
2025-04-09 11:37:16 +08:00
}
2026-03-25 21:15:55 +08:00
return messageDiv;
}
2026-03-25 21:15:55 +08:00
// 添加复制按钮
function addCopyButton(messageDiv, content) {
const copyButton = document.createElement("button");
copyButton.className = "copy-button";
copyButton.textContent = "复制";
copyButton.addEventListener("click", () => {
navigator.clipboard.writeText(content).then(() => {
copyButton.textContent = "已复制";
setTimeout(() => {
copyButton.textContent = "复制";
}, 2000);
}).catch(err => {
console.error("复制失败:", err);
});
});
messageDiv.appendChild(copyButton);
}
// 发送消息主逻辑(流式版本)
async function sendMessage() {
2026-03-25 21:15:55 +08:00
const inputElem = document.getElementById("userInput");
2025-04-09 11:37:16 +08:00
const userMessage = inputElem.value.trim();
if (!userMessage) return;
2026-03-25 21:15:55 +08:00
appendMessage(userMessage, "user");
messages.push({ role: "user", content: userMessage });
inputElem.value = "";
2025-04-09 11:37:16 +08:00
2026-03-25 21:15:55 +08:00
const chatContainer = document.getElementById("chatContainer");
const botMessageDiv = document.createElement("div");
botMessageDiv.classList.add("message", "bot");
2025-04-09 11:37:16 +08:00
botMessageDiv.innerHTML = `<em class="typing">正在输入中...</em>`;
chatContainer.appendChild(botMessageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
2026-03-25 21:15:55 +08:00
// === ModelScope DeepSeek 请求 ===
2025-04-09 11:37:16 +08:00
const payload = {
2026-03-25 21:15:55 +08:00
model: "deepseek-ai/DeepSeek-V3.2",
messages: messages,
stream: true
2025-04-09 11:37:16 +08:00
};
try {
const response = await fetch(API_URL, {
2026-03-25 21:15:55 +08:00
method: "POST",
2025-04-09 11:37:16 +08:00
headers: {
2026-03-25 21:15:55 +08:00
"Content-Type": "application/json",
"Authorization": "Bearer " + API_KEY
2025-04-09 11:37:16 +08:00
},
body: JSON.stringify(payload)
});
2026-03-25 21:15:55 +08:00
// === 流式读取 ===
if (!response.body) {
botMessageDiv.textContent = "服务器未返回有效流。";
return;
}
const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8");
let buffer = "";
let finalText = "";
let doneReasoning = false;
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop(); // 保留未完整的最后一行
2025-04-09 11:37:16 +08:00
2026-03-25 21:15:55 +08:00
for (const line of lines) {
if (!line.startsWith("data:")) continue;
const dataStr = line.replace(/^data:\s*/, "");
if (dataStr === "[DONE]") continue;
2025-04-09 11:37:16 +08:00
2026-03-25 21:15:55 +08:00
try {
const json = JSON.parse(dataStr);
const delta = json.choices[0]?.delta || {};
const reasoning = delta.reasoning_content || "";
const content = delta.content || "";
if (reasoning) {
// 推理内容(类似“思考过程”)
if (!doneReasoning) {
botMessageDiv.innerHTML = `<em>模型正在思考...</em>`;
doneReasoning = true;
}
continue;
}
if (content) {
finalText += content;
const safeHtml = DOMPurify.sanitize(marked.parse(finalText));
botMessageDiv.innerHTML = safeHtml;
chatContainer.scrollTop = chatContainer.scrollHeight;
}
} catch (err) {
console.warn("解析流数据出错:", err);
}
}
}
2026-03-25 21:15:55 +08:00
// 保存最终结果
if (finalText.trim() !== "") {
messages.push({ role: "assistant", content: finalText });
addCopyButton(botMessageDiv, finalText);
}
2025-04-09 11:37:16 +08:00
} catch (error) {
2026-03-25 21:15:55 +08:00
console.error("请求错误:", error);
botMessageDiv.textContent = "请求出错:" + error.message;
2025-04-09 11:37:16 +08:00
}
}