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(teammates): 例行任务暂停或恢复后近况开关跟着刷新

zqchrisde4341db901c
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/botGlancePopover.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/botGlancePopover.test.tsx
index f5dcc7df4b..545af148bb 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/botGlancePopover.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/botGlancePopover.test.tsx
@@ -85,6 +85,7 @@ const list = vi.fn(async () => ({ items, version: 1 as const }));
const update = vi.fn(async (_botId: string, patch: { id: string }) => ({ ...todo(patch.id), revision: 4 }));
const openPath = vi.fn(async () => ({ ok: true }));
let dailyOn = false;
+let routinesChanged: (() => void) | null = null;
const dailyCheck = vi.fn(async (_botId: string, enabled?: boolean) => (dailyOn = enabled ?? dailyOn));
beforeEach(() => {
@@ -97,6 +98,7 @@ beforeEach(() => {
(window as unknown as { electronAPI: unknown }).electronAPI = {
localDb: { bots: { todos: { list, update, dailyCheck } } },
maker: { onBotWorkbenchChanged: () => () => undefined },
+ routines: { onChanged: (listener: () => void) => { routinesChanged = listener; return () => { routinesChanged = null; }; } },
deviceLink: { invoke: vi.fn(), onRemotePush: () => () => undefined },
openPath,
};
@@ -152,6 +154,11 @@ describe('BotGlanceButton', () => {
fireEvent.click(toggle);
await waitFor(() => expect(toggle.getAttribute('aria-checked')).toBe('true'));
expect(dailyCheck).toHaveBeenLastCalledWith('bot-1', true);
+
+ // Paused from the routines list while 近况 is open: the switch follows.
+ dailyOn = false;
+ await act(async () => routinesChanged?.());
+ await waitFor(() => expect(toggle.getAttribute('aria-checked')).toBe('false'));
});
it('lights the entry dot for a due todo until 近况 is opened, and marks the row itself', async () => {
diff --git a/apps/desktop/src/renderer/features/bots/botGlanceTodos.ts b/apps/desktop/src/renderer/features/bots/botGlanceTodos.ts
index 9071193fdb..64085ace97 100644
--- a/apps/desktop/src/renderer/features/bots/botGlanceTodos.ts
+++ b/apps/desktop/src/renderer/features/bots/botGlanceTodos.ts
@@ -122,6 +122,13 @@ async function readTodos(host: GlanceTodoHost): Promise<GlanceTodos> {
};
}
+/** 只重读「每天帮我查一次」:例行任务列表里暂停或恢复它时用,不必重读整份待办。 */
+async function readDailyCheck(host: GlanceTodoHost): Promise<boolean | null> {
+ if (!host.deviceId) return window.electronAPI.localDb.bots.todos.dailyCheck(host.botId);
+ const page = await readRemotePage(host.deviceId, host.botId, { view: 'open', order: 'due', offset: 0, limit: 1 });
+ return page.dailyCheck ?? null;
+}
+
/** 打开或关掉伙伴的「每天帮我查一次」(宿主上的内置例行任务);返回宿主确认后的状态。 */
export async function setGlanceDailyCheck(host: GlanceTodoHost, enabled: boolean): Promise<boolean> {
if (!host.deviceId) return window.electronAPI.localDb.bots.todos.dailyCheck(host.botId, enabled);
@@ -177,6 +184,13 @@ export function useGlanceTodos(host: GlanceTodoHost, enabled = true) {
}
}, [botId, deviceId, enabled]);
+ const refreshDailyCheck = useCallback(async () => {
+ const owner = getDataOwnerGeneration();
+ const dailyCheck = await readDailyCheck({ botId, deviceId }).catch(() => undefined);
+ if (dailyCheck === undefined || !isDataOwnerGenerationCurrent(owner)) return;
+ setState((previous) => ({ ...previous, todos: { ...previous.todos, dailyCheck } }));
+ }, [botId, deviceId]);
+
useEffect(() => {
setState({ todos: EMPTY, loaded: false, failed: false });
if (!enabled || !botId) return;
@@ -186,6 +200,8 @@ export function useGlanceTodos(host: GlanceTodoHost, enabled = true) {
if (push.deviceId !== deviceId || push.channel !== REMOTE_RESOURCE_CHANGED_CHANNEL) return;
if (!isDeviceLinkRemotePushCurrent(push, stamp)) return;
const payload = parseRemoteResourceChangedPayload(push.payload);
+ // 例行任务列表里暂停或恢复「每天查待办」:只重读开关。
+ if (payload?.collectionId === 'routines') return void refreshDailyCheck();
if (payload?.collectionId !== 'teammates') return;
if (
payload.resourceRefs?.length &&
@@ -197,6 +213,7 @@ export function useGlanceTodos(host: GlanceTodoHost, enabled = true) {
: window.electronAPI.maker.onBotWorkbenchChanged((event) => {
if (event.botId === botId) void refresh();
});
+ const offRoutines = deviceId ? undefined : window.electronAPI.routines?.onChanged(() => void refreshDailyCheck());
const foreground = () => {
if (document.visibilityState === 'visible') void refresh();
};
@@ -205,10 +222,11 @@ export function useGlanceTodos(host: GlanceTodoHost, enabled = true) {
return () => {
epoch.current++;
unsubscribe?.();
+ offRoutines?.();
document.removeEventListener('visibilitychange', foreground);
window.removeEventListener('focus', foreground);
};
- }, [botId, deviceId, enabled, refresh]);
+ }, [botId, deviceId, enabled, refresh, refreshDailyCheck]);
// 浮层开着时也要跟上时间:「稍后再说」到时回到列表,跨过午夜时重算今天到期的红点。
const nextDeferredAt = state.todos.nextDeferredAt;
diff --git a/apps/mobile/src/session/useCompanionGlanceTodos.ts b/apps/mobile/src/session/useCompanionGlanceTodos.ts
index 66cdedd81e..89c5286757 100644
--- a/apps/mobile/src/session/useCompanionGlanceTodos.ts
+++ b/apps/mobile/src/session/useCompanionGlanceTodos.ts
@@ -97,18 +97,34 @@ export function useCompanionGlanceTodos({ deviceId, deviceName, botId, enabled }
}
}, [botId, deviceId, deviceName, enabled, i18n.language, invoke, openLink]);
+ /** Only the 「每天帮我查一次」 state, re-read when the routine is paused or resumed on the desktop. */
+ const refreshDailyCheck = useCallback(async () => {
+ if (!enabled) return;
+ try {
+ await openLink(deviceId);
+ const resource = await getRemoteResource(invoke, { deviceId, deviceName }, todosResourceRef(botId), i18n.language,
+ ['teammate-todos'], JSON.stringify({ view: 'open', order: 'due', offset: 0, limit: 1 }));
+ const value = (resource.blocks?.find(block => block.primitive === 'teammate-todos')?.data as Page | undefined)?.dailyCheck;
+ if (typeof value === 'boolean') setState(previous => ({ ...previous, todos: { ...previous.todos, dailyCheck: value } }));
+ } catch {
+ // The next full read corrects it.
+ }
+ }, [botId, deviceId, deviceName, enabled, i18n.language, invoke, openLink]);
+
useEffect(() => {
setState({ todos: EMPTY, loaded: false, failed: false });
if (!enabled) return;
void refresh();
const offChanged = onRemoteResourceChanged((id, payload) => {
- if (id !== deviceId || payload.collectionId !== 'teammates') return;
+ if (id !== deviceId) return;
+ if (payload.collectionId === 'routines') { void refreshDailyCheck(); return; }
+ if (payload.collectionId !== 'teammates') return;
if (payload.resourceRefs && !payload.resourceRefs.some(ref => ref.id === botId || ref.id === `todos:${botId}`)) return;
void refresh();
});
const appState = AppState.addEventListener('change', next => { if (next === 'active') void refresh(); });
return () => { epoch.current++; offChanged(); appState.remove(); };
- }, [botId, deviceId, enabled, onRemoteResourceChanged, refresh]);
+ }, [botId, deviceId, enabled, onRemoteResourceChanged, refresh, refreshDailyCheck]);
// Keep up with time while the chat is open: a deferred todo comes back, and past midnight
// today's due dots are recomputed.