Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 词典快照发出序号单调,桌面重连补推已在线手机

Dash065fd3ab58cc
diff --git a/apps/desktop/src/main/device-link/index.ts b/apps/desktop/src/main/device-link/index.ts
index 720dd643de..7d9fac5cfe 100644
--- a/apps/desktop/src/main/device-link/index.ts
+++ b/apps/desktop/src/main/device-link/index.ts
@@ -158,7 +158,12 @@ export function deviceLinkApiBase(): string {
}
type DeviceDirectoryResponse = {
- devices?: Array<{ deviceId?: unknown; name?: unknown }>;
+ devices?: Array<{
+ deviceId?: unknown;
+ name?: unknown;
+ online?: unknown;
+ platform?: unknown;
+ }>;
};
let controllerDisplayNameRefreshGeneration = 0;
const controllerDisplayNameFreshness = createControllerDisplayNameFreshnessTracker();
@@ -271,6 +276,20 @@ async function runControllerDisplayNamesFromDirectory(
void forgetLastKnownDeviceName(deviceId);
},
});
+ // presence 是增量:桌面刚连上 relay 时,已在线手机不会再发一帧上线。
+ // 目录补齐展示名的同时,给这些手机补一次只读词典投影。
+ for (const device of result.devices ?? []) {
+ const deviceId = typeof device.deviceId === 'string' ? device.deviceId : '';
+ if (
+ !deviceId
+ || device.online !== true
+ || !isMobilePlatform(typeof device.platform === 'string' ? device.platform : null)
+ || isDeviceRevoked(deviceId)
+ ) {
+ continue;
+ }
+ handleMobilePeerOnline(deviceId);
+ }
} catch (err) {
// 目录补齐是展示层 best-effort;失败时保留控制帧自报名 / 短 ID 回退,不影响建链。
log.warn(`device directory display-name refresh failed (non-fatal): ${String(err)}`);
diff --git a/apps/desktop/src/main/voice-input/__tests__/dictionarySyncDriver.test.ts b/apps/desktop/src/main/voice-input/__tests__/dictionarySyncDriver.test.ts
index a1cb5c1a3b..f0ad6feaa7 100644
--- a/apps/desktop/src/main/voice-input/__tests__/dictionarySyncDriver.test.ts
+++ b/apps/desktop/src/main/voice-input/__tests__/dictionarySyncDriver.test.ts
@@ -335,4 +335,17 @@ describe('手机只读投影', () => {
expect(snapshot.ok).toBe(true);
if (snapshot.ok) expect(snapshot.emittedAt).toEqual(expect.any(Number));
});
+
+ it('时钟回拨后同主机发出序号仍单调', () => {
+ const now = vi.spyOn(Date, 'now');
+ now.mockReturnValue(5_000);
+ const first = buildMobileDictionarySnapshot();
+ now.mockReturnValue(1_000);
+ const second = buildMobileDictionarySnapshot();
+ now.mockRestore();
+ expect(first.ok && second.ok).toBe(true);
+ if (first.ok && second.ok) {
+ expect(second.emittedAt).toBeGreaterThan(first.emittedAt ?? 0);
+ }
+ });
});
diff --git a/apps/desktop/src/main/voice-input/dictionarySyncDriver.ts b/apps/desktop/src/main/voice-input/dictionarySyncDriver.ts
index c37724d328..d5b5cc93da 100644
--- a/apps/desktop/src/main/voice-input/dictionarySyncDriver.ts
+++ b/apps/desktop/src/main/voice-input/dictionarySyncDriver.ts
@@ -73,6 +73,8 @@ export interface DictionarySyncTransport {
let transport: DictionarySyncTransport | null = null;
let debounceTimer: NodeJS.Timeout | null = null;
let intervalTimer: NodeJS.Timeout | null = null;
+/** 同进程内快照发出序号:取墙上时间与上一发+1 的较大值,时钟回拨也不会倒序。 */
+let lastEmittedAt = 0;
export function isDesktopPlatform(platform: string | undefined | null): boolean {
return typeof platform === 'string' && DESKTOP_PLATFORMS.has(platform);
@@ -112,6 +114,7 @@ export function stopVoiceDictionarySync(): void {
if (intervalTimer) clearInterval(intervalTimer);
debounceTimer = null;
intervalTimer = null;
+ lastEmittedAt = 0;
}
/** 对端桌面上线:立刻单发一次,让它尽快拿到本机状态(它也会回发自己的)。 */
@@ -246,9 +249,10 @@ function sendMobileSnapshotTo(
/** push 与主动 GET 共用:同一份投影必须带上发出时间,否则同代向量的兜底拉取盖不掉已有 push。 */
export function buildMobileDictionarySnapshot(): MobileVoiceDictionarySnapshotResult {
+ lastEmittedAt = Math.max(Date.now(), lastEmittedAt + 1);
return {
ok: true,
- emittedAt: Date.now(),
+ emittedAt: lastEmittedAt,
...readDictionaryProjectionForMobile(),
};
}