mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-09 10:23:17 +08:00
54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
const targets = new WeakMap();
|
|
|
|
export const CONNECTION_TEST_STATE_IDENTITY = Symbol('dsh-im.connection-test-state-identity');
|
|
|
|
function stateIdentity(state) {
|
|
return state?.[CONNECTION_TEST_STATE_IDENTITY] ?? state;
|
|
}
|
|
|
|
function cleanText(value) {
|
|
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
|
}
|
|
|
|
export function rememberConnectionTestTarget(state, target) {
|
|
if (!state || !target || typeof target !== 'object') return false;
|
|
try {
|
|
targets.set(stateIdentity(state), structuredClone(target));
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function connectionTestTarget(state) {
|
|
const target = state ? targets.get(stateIdentity(state)) : null;
|
|
return target ? structuredClone(target) : null;
|
|
}
|
|
|
|
export function connectionTestMessage(botName, channelLabel = '机器人') {
|
|
const name = cleanText(botName) ?? channelLabel;
|
|
return `✅ DeepSeek Harness 连接测试成功\n这条消息由插件页面中的“${name}”机器人卡片发出。`;
|
|
}
|
|
|
|
export function connectionTestTargetUnavailable(channelLabel = '机器人') {
|
|
const error = new Error(`${channelLabel}尚未收到可用于测试的私聊消息。`);
|
|
error.code = 'test-target-unavailable';
|
|
return error;
|
|
}
|
|
|
|
export async function sendRememberedConnectionTest({ state, send, text, channelLabel }) {
|
|
const target = connectionTestTarget(state);
|
|
if (!target) throw connectionTestTargetUnavailable(channelLabel);
|
|
await send(target, text);
|
|
return { sent: true };
|
|
}
|
|
|
|
export function publicConnectionTestResult(error) {
|
|
if (!error) return Object.freeze({ sent: true });
|
|
return Object.freeze({
|
|
sent: false,
|
|
code: error?.code === 'test-target-unavailable'
|
|
? 'test-target-unavailable'
|
|
: 'test-message-failed',
|
|
});
|
|
}
|