Add self-contained relay deployment project and operational guidance
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
"use strict";
|
||||
const byId = id => document.getElementById(id);
|
||||
const statusNode = byId("status");
|
||||
let sending = false;
|
||||
let loggedIn = false;
|
||||
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||
function note(text) { statusNode.textContent = text; }
|
||||
function addMessage(text, role) {
|
||||
const item = document.createElement("div");
|
||||
item.className = "message " + role;
|
||||
item.textContent = text;
|
||||
byId("messages").appendChild(item);
|
||||
item.scrollIntoView({block:"nearest"});
|
||||
}
|
||||
async function refresh() {
|
||||
try {
|
||||
const response = await fetch("/web/session", {cache:"no-store", credentials:"same-origin"});
|
||||
if (!response.ok) {
|
||||
loggedIn = false;
|
||||
byId("pair-panel").hidden = false;
|
||||
byId("chat-panel").hidden = true;
|
||||
note(response.status === 401 ? "请使用PC上的一次性配对码连接。" : "会话服务暂不可用。");
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
loggedIn = true;
|
||||
byId("pair-panel").hidden = true;
|
||||
byId("chat-panel").hidden = false;
|
||||
byId("device-state").textContent = data.online ? "你的电脑已在线" : "你的电脑当前离线";
|
||||
if (!sending) note(data.online ? "已连接本人设备,可以开始对话。" : "请在PC启动桥接端,随后再发送消息。");
|
||||
} catch (_) { if (!sending) note("网络连接失败,请检查到中继的连接。"); }
|
||||
}
|
||||
byId("pair-form").addEventListener("submit", async event => {
|
||||
event.preventDefault();
|
||||
byId("pair-button").disabled = true;
|
||||
try {
|
||||
const response = await fetch("/web/claim", {method:"POST", credentials:"same-origin",
|
||||
headers:{"Content-Type":"application/json"}, body:JSON.stringify({code:byId("code").value.trim()})});
|
||||
if (!response.ok) throw new Error(response.status === 404 ? "配对码已过期或已使用,请在PC重新生成。" : "配对失败,请确认地址、配对码和设备绑定。");
|
||||
byId("code").value = "";
|
||||
await refresh();
|
||||
} catch (error) { note(error.message); }
|
||||
finally { byId("pair-button").disabled = false; }
|
||||
});
|
||||
byId("chat-form").addEventListener("submit", async event => {
|
||||
event.preventDefault();
|
||||
if (sending || !loggedIn) return;
|
||||
const text = byId("message").value.trim();
|
||||
if (!text) return;
|
||||
const bytes = new Uint8Array(16);
|
||||
crypto.getRandomValues(bytes);
|
||||
const requestId = Array.from(bytes, n => n.toString(16).padStart(2,"0")).join("");
|
||||
sending = true;
|
||||
byId("send").disabled = true;
|
||||
byId("logout").disabled = true;
|
||||
addMessage(text, "user");
|
||||
byId("message").value = "";
|
||||
note("正在等待你的电脑和模型返回结果…");
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), 200000);
|
||||
try {
|
||||
const response = await fetch("/web/chat", {method:"POST", credentials:"same-origin", signal:controller.signal,
|
||||
headers:{"Content-Type":"application/json"}, body:JSON.stringify({text, request_id:requestId})});
|
||||
if (!response.ok) throw new Error(response.status === 401 ? "网页会话已失效,请重新配对。" : "请求失败,HTTP " + response.status + "。请核查执行状态后再决定是否重发。");
|
||||
const data = await response.json();
|
||||
addMessage(data.result || "未收到文本结果。", data.status === "completed" ? "assistant" : "notice");
|
||||
note("请求状态:" + data.status);
|
||||
} catch (error) {
|
||||
addMessage(error.name === "AbortError" ? "等待超时,执行状态未知。系统没有自动重发。" : error.message, "notice");
|
||||
note("请求ID:" + requestId + "。请保留此ID排查,勿盲目重发。");
|
||||
} finally {
|
||||
clearTimeout(timer); sending = false;
|
||||
byId("send").disabled = false; byId("logout").disabled = false;
|
||||
}
|
||||
});
|
||||
byId("logout").addEventListener("click", async () => {
|
||||
try {
|
||||
const response = await fetch("/web/logout", {method:"POST", credentials:"same-origin"});
|
||||
if (!response.ok) throw new Error("退出失败,请稍后重试。");
|
||||
byId("messages").replaceChildren();
|
||||
await refresh();
|
||||
} catch (error) { note(error.message); }
|
||||
});
|
||||
byId("login").addEventListener("click", async () => {
|
||||
const popup = window.open("about:blank", "hainuo_authorize");
|
||||
if (!popup) { note("请允许打开授权窗口。"); return; }
|
||||
popup.opener = null;
|
||||
byId("login").disabled = true;
|
||||
try {
|
||||
const started = await fetch("/oauth/start", {method:"POST", headers:{"Content-Type":"application/json"}, body:"{}", cache:"no-store"});
|
||||
if (!started.ok) throw new Error("海能Work授权尚未配置,可先使用PC配对码。");
|
||||
const transaction = await started.json();
|
||||
const url = new URL(transaction.authorization_url);
|
||||
if (url.protocol !== "https:") throw new Error("授权地址必须使用HTTPS。");
|
||||
popup.location = url.href;
|
||||
note("请在授权窗口完成登录。");
|
||||
const until = Date.now() + transaction.expires_in * 1000;
|
||||
let token;
|
||||
while (Date.now() < until) {
|
||||
await sleep(2000);
|
||||
const response = await fetch("/oauth/claim?state=" + encodeURIComponent(transaction.state), {
|
||||
headers:{"X-OAuth-Claim-Secret":transaction.claim_secret}, cache:"no-store"});
|
||||
if (response.status === 202) continue;
|
||||
if (!response.ok) throw new Error("授权已失效,请重新开始。");
|
||||
token = (await response.json()).user_access_token;
|
||||
break;
|
||||
}
|
||||
if (!token) throw new Error("等待授权超时。");
|
||||
const response = await fetch("/web/platform-session", {method:"POST", headers:{Authorization:"Bearer " + token}});
|
||||
token = null;
|
||||
if (!response.ok) throw new Error("请先在海能Work完成本人设备绑定。");
|
||||
await refresh();
|
||||
} catch (error) { note(error.message); try { popup.close(); } catch (_) {} }
|
||||
finally { byId("login").disabled = false; }
|
||||
});
|
||||
refresh();
|
||||
setInterval(() => { if (!sending) refresh(); }, 15000);
|
||||
Reference in New Issue
Block a user