feat: send test messages during bot connection checks

This commit is contained in:
xmanrui 2026-08-19 03:52:52 +08:00
parent 94da24538f
commit fb61c4235f
78 changed files with 3460 additions and 210 deletions

View file

@ -18,6 +18,7 @@ import {
imagePromptUserMessage,
promptContentForMessage,
} from '../shared/image-prompt.mjs';
import { rememberConnectionTestTarget } from '../shared/connection-test.mjs';
const INTERACTION_RESOLVED_TEXT = '这个问题已在其他客户端处理,无需再次回答。';
@ -250,6 +251,7 @@ export class WeixinHarnessBridge {
if (!hasImages && command === '/status') {
await this.#harness.ensureRunning({ signal: this.#signal });
await this.#send(sender, '微信与 DeepSeek Harness 连接正常。', contextToken, runId);
rememberConnectionTestTarget(this.#state, { toUserId: sender });
await this.#state.markSeen(messageId);
return;
}

View file

@ -6,6 +6,10 @@ import {
WeixinApiError,
} from './weixin-api.mjs';
import { deriveWeixinBotIdentity, maskWeixinAccountId } from './config-store.mjs';
import {
connectionTestMessage,
connectionTestTargetUnavailable,
} from '../shared/connection-test.mjs';
const ACTIVE_ATTEMPT_STATES = new Set([
'starting',
@ -240,6 +244,20 @@ export class WeixinController {
return this.status();
}
async sendConnectionTest(botId) {
const config = this.#configStore.get(botId);
if (!config) throw new Error('Unknown Weixin account');
return this.#withBotTransition(botId, async () => {
const runtime = this.#runtimes.get(botId);
if (!runtime?.status?.ready || typeof runtime.sendConnectionTest !== 'function') {
throw connectionTestTargetUnavailable('微信机器人');
}
return runtime.sendConnectionTest(connectionTestMessage(
`微信机器人(${maskWeixinAccountId(config.accountId)})`,
));
});
}
async deleteBot(botId) {
const config = this.#configStore.get(botId);
if (!config) throw new Error('Unknown Weixin account');

View file

@ -1,5 +1,9 @@
import { WeixinApiError } from './weixin-api.mjs';
import { createWeixinBridgeStatus, WeixinHarnessBridge } from './weixin-bridge.mjs';
import {
connectionTestTarget,
connectionTestTargetUnavailable,
} from '../shared/connection-test.mjs';
const DEFAULT_START_RETRY_DELAYS_MS = Object.freeze([250, 1_000, 3_000]);
@ -250,4 +254,25 @@ export class WeixinRuntime {
this.#status.weixinConnectionState = 'idle';
return this.status;
}
async sendConnectionTest(text) {
const remembered = connectionTestTarget(this.#state);
const toUserId = typeof remembered?.toUserId === 'string' && remembered.toUserId.trim()
? remembered.toUserId.trim()
: typeof this.#config.ownerUserId === 'string' && this.#config.ownerUserId.trim()
? this.#config.ownerUserId.trim()
: null;
if (!toUserId) throw connectionTestTargetUnavailable('微信机器人');
if (!this.#status.ready || !this.#abortController) {
throw new Error('Weixin runtime is not connected');
}
await this.#api.sendText({
baseUrl: this.#config.baseUrl,
token: this.#token,
toUserId,
text,
signal: this.#abortController.signal,
});
return { sent: true };
}
}