zhuyuan-ice-gl-zy001/QODER-CN-DEV-20260807/state-wake-20260812/bridge-chat/adapter.js
2026-08-20 00:01:53 +08:00

36 lines
1.5 KiB
JavaScript
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// adapter.js —— 壳层适配:同一套聊天前端跑在 Electron / Tauri / 浏览器(mock) 三种宿主
// 统一暴露 window.GHB = { send(to, text), on(event, cb) }
// 事件:connected{node_id} / message{from,text,msg_id} / sent{msg_id,to} / receipt{msg_id,verified} / error{message}
(() => {
const handlers = {};
const on = (ev, cb) => { (handlers[ev] = handlers[ev] || []).push(cb); };
const dispatch = (ev, payload) => (handlers[ev] || []).forEach((cb) => cb(payload));
if (window.ghbElectron) {
// Electron:preload 已注入 ipc 通道
window.ghbElectron.onEvent((ev) => dispatch(ev.event === 'message' ? 'message' : ev.event, ev));
window.GHB = {
kind: 'electron',
on,
send: (to, text) => window.ghbElectron.send(to, text),
};
} else if (window.__TAURI__ && window.__TAURI__.event && window.__TAURI__.core) {
// Tauri v2(withGlobalTauri):listen('ghb:event') + invoke('bridge_send')
window.__TAURI__.event.listen('ghb:event', (e) => dispatch(e.payload.event, e.payload));
window.GHB = {
kind: 'tauri',
on,
send: (to, text) => window.__TAURI__.core.invoke('bridge_send', { to, text }),
};
} else {
// 浏览器直开(无壳):只提示,不阻塞渲染
window.GHB = {
kind: 'mock',
on,
send: () => {
dispatch('error', { message: '未在壳层中运行:请通过 Electron / Tauri 打开' });
},
};
dispatch('error', { message: '桥接未接入(裸浏览器模式)' });
}
})();