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(bot-group): finish the follow save after the panel closes

zqchrisa8cdd46274e0
diff --git a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
index 5b73ef1a25..5e50172499 100644
--- a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
+++ b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
@@ -171,7 +171,8 @@ export function ChatThreadPanel({ group, rootId, quoteSources = group.messages,
const result = await api().thread({ groupId: group.id, rootId, before, sourceMessageIds: shown.map(message => message.id), markRead });
if (!current()) return;
if (!result.ok) { setError(t(chatErrorKey(result.errorCode))); return; }
- if (!before && followWrite === followWriteRef.current) {
+ // A read that overlaps a follow save may carry the old choice; the save's own answer wins.
+ if (!before && followWrite === followWriteRef.current && !followSaveRef.current.saving) {
followSaveRef.current.saved = result.following;
setFollowing(result.following);
}
@@ -233,8 +234,11 @@ export function ChatThreadPanel({ group, rootId, quoteSources = group.messages,
if (save.saving) return;
save.saving = true;
followWriteRef.current += 1;
+ // The reader's last click is saved even if the panel closes meanwhile; only an account
+ // change stops the queue, and only an open panel shows the outcome.
const owner = getDataOwnerGeneration();
- const current = () => aliveRef.current && isDataOwnerGenerationCurrent(owner);
+ const ownerCurrent = () => isDataOwnerGenerationCurrent(owner);
+ const shown = () => aliveRef.current && ownerCurrent();
// A failed write may still have been saved (a timeout or a dropped connection): until a later
// write succeeds, the server's value is unknown and the newest click is always sent.
let uncertain = false;
@@ -249,9 +253,10 @@ export function ChatThreadPanel({ group, rootId, quoteSources = group.messages,
else failure = t(chatErrorKey(result.errorCode));
} catch { failure = t(key('requestFailed')); }
if (failure) uncertain = true;
- if (!current()) return;
+ if (!ownerCurrent()) return;
// A newer click is saved next; only the last outcome is shown.
if (save.wanted !== null) continue;
+ if (!shown()) continue;
setFollowing(save.saved);
if (failure) toast.error(failure);
}
@@ -260,7 +265,7 @@ export function ChatThreadPanel({ group, rootId, quoteSources = group.messages,
save.wanted = null;
followWriteRef.current += 1;
// Read what the server kept after a failure: the switch then shows the real choice.
- if (uncertain && current()) refreshRef.current();
+ if (uncertain && shown()) refreshRef.current();
}
}, [following, group.id, rootId, t]);
useEffect(() => {
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
index 055e117828..0b8498aad7 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
@@ -115,6 +115,25 @@ describe('chat interaction controls', () => {
expect(follow.getAttribute('aria-pressed')).toBe('false');
});
+ it('saves the last follow click even after the Thread panel closes', async () => {
+ mocks.thread.mockResolvedValue({ ok: true, root: message, replies: [], hasMore: false, following: false });
+ let finishFirst: (value: { ok: true; following: boolean }) => void = () => {};
+ mocks.followThread.mockReturnValueOnce(new Promise(resolve => { finishFirst = resolve; }))
+ .mockResolvedValueOnce({ ok: true, following: false });
+ const view = render(<ChatThreadPanel group={group} rootId="root" onClose={vi.fn()} />);
+ const follow = await waitFor(() => {
+ const button = document.querySelector('[data-thread-follow]') as HTMLButtonElement | null;
+ if (!button) throw new Error('follow switch not shown');
+ return button;
+ });
+ fireEvent.click(follow);
+ fireEvent.click(follow);
+ view.unmount();
+ await act(async () => { finishFirst({ ok: true, following: true }); });
+ await waitFor(() => expect(mocks.followThread).toHaveBeenCalledTimes(2));
+ expect(mocks.followThread.mock.calls.map(([input]) => input.following)).toEqual([true, false]);
+ });
+
it('refits a typed thread reply when the panel width changes, not when only its height does', () => {
const observed: Array<{ target: Element; callback: () => void }> = [];
vi.stubGlobal('ResizeObserver', class {