Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

refactor(chat): address code review feedback

Naiyuan Qing102831919c66
diff --git a/packages/core/chat/queries.ts b/packages/core/chat/queries.ts
index 3571fab133..f168681281 100644
--- a/packages/core/chat/queries.ts
+++ b/packages/core/chat/queries.ts
@@ -1,6 +1,13 @@
import { queryOptions } from "@tanstack/react-query";
import { api } from "../api";
+// NOTE on workspace scoping:
+// `wsId` is used only as part of queryKey for cache isolation per workspace.
+// The actual workspace context comes from ApiClient's X-Workspace-ID header,
+// which is set by useWorkspaceStore.switchWorkspace(). Callers must ensure the
+// header is in sync with the wsId they pass here — otherwise cache writes will
+// be misattributed during a workspace switch race window.
+
export const chatKeys = {
all: (wsId: string) => ["chat", wsId] as const,
sessions: (wsId: string) => [...chatKeys.all(wsId), "sessions"] as const,
diff --git a/packages/core/package.json b/packages/core/package.json
index f0f4bfedaa..373a9208a0 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -37,7 +37,6 @@
"./chat": "./chat/index.ts",
"./chat/queries": "./chat/queries.ts",
"./chat/mutations": "./chat/mutations.ts",
- "./chat/store": "./chat/store.ts",
"./runtimes": "./runtimes/index.ts",
"./runtimes/queries": "./runtimes/queries.ts",
"./runtimes/mutations": "./runtimes/mutations.ts",
diff --git a/packages/core/platform/core-provider.tsx b/packages/core/platform/core-provider.tsx
index 2bba2d1eec..65e26b7c29 100644
--- a/packages/core/platform/core-provider.tsx
+++ b/packages/core/platform/core-provider.tsx
@@ -19,6 +19,7 @@ import type { StorageAdapter } from "../types/storage";
let initialized = false;
let authStore: ReturnType<typeof createAuthStore>;
let workspaceStore: ReturnType<typeof createWorkspaceStore>;
+let chatStore: ReturnType<typeof createChatStore>;
function initCore(
apiBaseUrl: string,
storage: StorageAdapter,
@@ -48,7 +49,8 @@ function initCore(
workspaceStore = createWorkspaceStore(api, { storage });
registerWorkspaceStore(workspaceStore);
- registerChatStore(createChatStore({ storage }));
+ chatStore = createChatStore({ storage });
+ registerChatStore(chatStore);
initialized = true;
}
diff --git a/packages/views/chat/components/chat-window.tsx b/packages/views/chat/components/chat-window.tsx
index c17aa958cd..609baf627d 100644
--- a/packages/views/chat/components/chat-window.tsx
+++ b/packages/views/chat/components/chat-window.tsx
@@ -100,9 +100,25 @@ export function ChatWindow() {
const { subscribe } = useWS();
useEffect(() => {
+ // Returns true if the event was for our pending task and was handled.
+ // Caller still decides whether to invalidate cache (chat:done / completed do; failed doesn't).
+ const matchesPending = (taskId: string) =>
+ !!pendingTaskRef.current && taskId === pendingTaskRef.current;
+
+ const finalizePending = (invalidateCache: boolean) => {
+ if (invalidateCache) {
+ const sid = useChatStore.getState().activeSessionId;
+ if (sid) {
+ qc.invalidateQueries({ queryKey: chatKeys.messages(sid) });
+ }
+ }
+ clearTimeline();
+ setPendingTask(null);
+ };
+
const unsubMessage = subscribe("task:message", (payload) => {
const p = payload as TaskMessagePayload;
- if (!pendingTaskRef.current || p.task_id !== pendingTaskRef.current) return;
+ if (!matchesPending(p.task_id)) return;
addTimelineItem({
seq: p.seq,
type: p.type,
@@ -115,35 +131,20 @@ export function ChatWindow() {
const unsubDone = subscribe("chat:done", (payload) => {
const p = payload as ChatDonePayload;
- if (!pendingTaskRef.current || p.task_id !== pendingTaskRef.current) return;
- const activeSessionId = useChatStore.getState().activeSessionId;
- if (activeSessionId) {
- qc.invalidateQueries({
- queryKey: chatKeys.messages(activeSessionId),
- });
- }
- clearTimeline();
- setPendingTask(null);
+ if (!matchesPending(p.task_id)) return;
+ finalizePending(true);
});
const unsubCompleted = subscribe("task:completed", (payload) => {
const p = payload as { task_id: string };
- if (!pendingTaskRef.current || p.task_id !== pendingTaskRef.current) return;
- const activeSessionId = useChatStore.getState().activeSessionId;
- if (activeSessionId) {
- qc.invalidateQueries({
- queryKey: chatKeys.messages(activeSessionId),
- });
- }
- clearTimeline();
- setPendingTask(null);
+ if (!matchesPending(p.task_id)) return;
+ finalizePending(true);
});
const unsubFailed = subscribe("task:failed", (payload) => {
const p = payload as { task_id: string };
- if (!pendingTaskRef.current || p.task_id !== pendingTaskRef.current) return;
- clearTimeline();
- setPendingTask(null);
+ if (!matchesPending(p.task_id)) return;
+ finalizePending(false);
});
return () => {
@@ -152,7 +153,6 @@ export function ChatWindow() {
unsubCompleted();
unsubFailed();
};
-
}, [subscribe, addTimelineItem, clearTimeline, setPendingTask, qc]);
const handleSend = useCallback(