mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-12 19:12:34 +08:00
feat: send test messages during bot connection checks
This commit is contained in:
parent
94da24538f
commit
fb61c4235f
78 changed files with 3460 additions and 210 deletions
54
src/channels/shared/connection-test.mjs
Normal file
54
src/channels/shared/connection-test.mjs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
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}发送 /status,再点击“检查连接”。`);
|
||||
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',
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue