Skip to content

Commit de882bf

Browse files
committed
refactor(site/src/pages/AgentsPage/components/ChatConversation): delete setStreamState
No production code calls it; the tool invalidation tests apply tool-result parts instead.
1 parent 0b86d22 commit de882bf

3 files changed

Lines changed: 36 additions & 99 deletions

File tree

‎site/src/pages/AgentsPage/components/ChatConversation/chatStore.createStore.test.ts‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -208,27 +208,6 @@ describe("advanceSnapshotVersion", () => {
208208
});
209209
});
210210

211-
// ---------------------------------------------------------------------------
212-
// setStreamState
213-
// ---------------------------------------------------------------------------
214-
215-
describe("setStreamState", () => {
216-
it("does not notify when setting the same stream state reference", () => {
217-
const store = createChatStore();
218-
store.applyMessagePart({ type: "text", text: "hello" });
219-
const streamState = store.getSnapshot().streamState;
220-
expect(streamState).not.toBeNull();
221-
222-
let notified = false;
223-
store.subscribe(() => {
224-
notified = true;
225-
});
226-
227-
store.setStreamState(streamState);
228-
expect(notified).toBe(false);
229-
});
230-
});
231-
232211
// ---------------------------------------------------------------------------
233212
// setClientError / clearClientError
234213
// ---------------------------------------------------------------------------

‎site/src/pages/AgentsPage/components/ChatConversation/chatStore.ts‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ export type ChatStore = {
216216
status: TypesGen.ChatStatus | null,
217217
request: () => Promise<unknown>,
218218
) => Promise<void>;
219-
setStreamState: (streamState: StreamState | null) => void;
220219
setClientError: (reason: ChatDetailError | null) => void;
221220
clearClientError: () => void;
222221
setRetryState: (state: RetryState) => void;
@@ -543,20 +542,6 @@ export const createChatStore = (): ChatStore => {
543542
throw error;
544543
}
545544
},
546-
setStreamState: (streamState) => {
547-
if (state.streamState === streamState) {
548-
return;
549-
}
550-
setState((current) => {
551-
if (current.streamState === streamState) {
552-
return current;
553-
}
554-
return {
555-
...current,
556-
streamState,
557-
};
558-
});
559-
},
560545
setClientError: (reason) => {
561546
setState((current) => {
562547
if (chatDetailErrorsEqual(current.clientError, reason)) {

‎site/src/pages/AgentsPage/components/ChatConversation/useChatToolInvalidations.test.tsx‎

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,25 @@ import {
88
chatsByWorkspace,
99
} from "#/api/queries/chats";
1010
import { getWorkspaceQuotaQueryKey } from "#/api/queries/workspaceQuota";
11+
import type * as TypesGen from "#/api/typesGenerated";
1112
import { workspacesQueryKeyPrefix } from "#/api/queries/workspaces";
1213
import { createChatStore } from "./chatStore";
13-
import type { StreamState } from "./types";
1414
import { useChatToolInvalidations } from "./useChatToolInvalidations";
1515

1616
const ORGANIZATION_NAME = "coder";
1717
const USERNAME = "alice";
1818

19-
type ToolResultOverrides = Partial<StreamState["toolResults"][string]>;
20-
21-
const createStreamState = (
19+
const toolResult = (
2220
name: string,
23-
id = "tool-1",
24-
resultOverrides: ToolResultOverrides = {},
25-
): StreamState => ({
26-
blocks: [],
27-
toolCalls: {
28-
[id]: {
29-
id,
30-
name,
31-
args: {},
32-
},
33-
},
34-
toolResults: {
35-
[id]: {
36-
id,
37-
name,
38-
isError: false,
39-
...resultOverrides,
40-
},
41-
},
42-
sources: [],
21+
overrides: Partial<TypesGen.ChatToolResultPart> = {},
22+
): TypesGen.ChatToolResultPart => ({
23+
type: "tool-result",
24+
tool_call_id: "tool-1",
25+
tool_name: name,
26+
result: {},
27+
...overrides,
4328
});
4429

45-
const createTestStore = (initial: StreamState | null = null) => {
46-
const store = createChatStore();
47-
store.setStreamState(initial);
48-
49-
return {
50-
store,
51-
setStreamState: store.setStreamState,
52-
};
53-
};
54-
5530
const createWrapper = (
5631
queryClient: QueryClient,
5732
): React.FC<React.PropsWithChildren> => {
@@ -82,7 +57,7 @@ describe("useChatToolInvalidations", () => {
8257
organizationName?: string;
8358
username?: string;
8459
} = {}) => {
85-
const { store, setStreamState } = createTestStore();
60+
const store = createChatStore();
8661
const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");
8762
const result = renderHook(
8863
(props: { chatID: string; organizationName: string; username: string }) =>
@@ -99,15 +74,15 @@ describe("useChatToolInvalidations", () => {
9974
return {
10075
...result,
10176
invalidateSpy,
102-
setStreamState,
77+
applyMessagePart: store.applyMessagePart,
10378
};
10479
};
10580

10681
it("dispatches chat binding and workspace mutation invalidations on create_workspace completion", async () => {
107-
const { invalidateSpy, setStreamState } = renderInvalidations();
82+
const { invalidateSpy, applyMessagePart } = renderInvalidations();
10883

10984
await act(async () => {
110-
setStreamState(createStreamState("create_workspace"));
85+
applyMessagePart(toolResult("create_workspace"));
11186
});
11287

11388
await waitFor(() => {
@@ -135,10 +110,10 @@ describe("useChatToolInvalidations", () => {
135110
queryClient.setQueryData(chatsByWorkspace(["ws-1"]).queryKey, {
136111
"ws-1": "chat-1",
137112
});
138-
const { setStreamState } = renderInvalidations();
113+
const { applyMessagePart } = renderInvalidations();
139114

140115
await act(async () => {
141-
setStreamState(createStreamState("create_workspace"));
116+
applyMessagePart(toolResult("create_workspace"));
142117
});
143118

144119
await waitFor(() => {
@@ -159,10 +134,10 @@ describe("useChatToolInvalidations", () => {
159134
});
160135

161136
it("dispatches workspace mutation invalidations on start_workspace completion", async () => {
162-
const { invalidateSpy, setStreamState } = renderInvalidations();
137+
const { invalidateSpy, applyMessagePart } = renderInvalidations();
163138

164139
await act(async () => {
165-
setStreamState(createStreamState("start_workspace"));
140+
applyMessagePart(toolResult("start_workspace"));
166141
});
167142

168143
await waitFor(() => {
@@ -186,12 +161,10 @@ describe("useChatToolInvalidations", () => {
186161
});
187162

188163
it("dispatches workspace mutation invalidations for errored workspace tools", async () => {
189-
const { invalidateSpy, setStreamState } = renderInvalidations();
164+
const { invalidateSpy, applyMessagePart } = renderInvalidations();
190165

191166
await act(async () => {
192-
setStreamState(
193-
createStreamState("stop_workspace", "tool-1", { isError: true }),
194-
);
167+
applyMessagePart(toolResult("stop_workspace", { is_error: true }));
195168
});
196169

197170
await waitFor(() => {
@@ -210,12 +183,12 @@ describe("useChatToolInvalidations", () => {
210183
});
211184

212185
it("invalidates workspace queries without quota when the quota key is incomplete", async () => {
213-
const { invalidateSpy, setStreamState } = renderInvalidations({
186+
const { invalidateSpy, applyMessagePart } = renderInvalidations({
214187
organizationName: "",
215188
});
216189

217190
await act(async () => {
218-
setStreamState(createStreamState("create_workspace"));
191+
applyMessagePart(toolResult("create_workspace"));
219192
});
220193

221194
await waitFor(() => {
@@ -234,29 +207,28 @@ describe("useChatToolInvalidations", () => {
234207
});
235208

236209
it("does not invalidate queries for tools without invalidation signals", async () => {
237-
const { invalidateSpy, setStreamState } = renderInvalidations();
210+
const { invalidateSpy, applyMessagePart } = renderInvalidations();
238211

239212
await act(async () => {
240-
setStreamState(createStreamState("read_file"));
213+
applyMessagePart(toolResult("read_file"));
241214
});
242215

243216
expect(invalidateSpy).not.toHaveBeenCalled();
244217
});
245218

246219
it("does not process the same tool call ID twice", async () => {
247-
const { invalidateSpy, setStreamState } = renderInvalidations();
248-
const streamState = createStreamState("create_workspace");
220+
const { invalidateSpy, applyMessagePart } = renderInvalidations();
249221

250222
await act(async () => {
251-
setStreamState(streamState);
223+
applyMessagePart(toolResult("create_workspace"));
252224
});
253225

254226
await waitFor(() => {
255227
expect(invalidateSpy).toHaveBeenCalledTimes(4);
256228
});
257229

258230
await act(async () => {
259-
setStreamState(streamState);
231+
applyMessagePart(toolResult("create_workspace"));
260232
});
261233

262234
await waitFor(() => {
@@ -265,10 +237,10 @@ describe("useChatToolInvalidations", () => {
265237
});
266238

267239
it("resets processed tool call IDs when chatID changes", async () => {
268-
const { invalidateSpy, rerender, setStreamState } = renderInvalidations();
240+
const { invalidateSpy, rerender, applyMessagePart } = renderInvalidations();
269241

270242
await act(async () => {
271-
setStreamState(createStreamState("create_workspace"));
243+
applyMessagePart(toolResult("create_workspace"));
272244
});
273245

274246
await waitFor(() => {
@@ -282,7 +254,7 @@ describe("useChatToolInvalidations", () => {
282254
});
283255

284256
await act(async () => {
285-
setStreamState(createStreamState("create_workspace"));
257+
applyMessagePart(toolResult("create_workspace"));
286258
});
287259

288260
await waitFor(() => {
@@ -295,20 +267,21 @@ describe("useChatToolInvalidations", () => {
295267
});
296268

297269
it("waits for completed tool results before invalidating", async () => {
298-
const { invalidateSpy, setStreamState } = renderInvalidations();
270+
const { invalidateSpy, applyMessagePart } = renderInvalidations();
299271

300272
await act(async () => {
301-
setStreamState(
302-
createStreamState("create_workspace", "tool-1", {
303-
isStreaming: true,
273+
applyMessagePart(
274+
toolResult("create_workspace", {
275+
result: undefined,
276+
result_delta: "{",
304277
}),
305278
);
306279
});
307280

308281
expect(invalidateSpy).not.toHaveBeenCalled();
309282

310283
await act(async () => {
311-
setStreamState(createStreamState("create_workspace"));
284+
applyMessagePart(toolResult("create_workspace"));
312285
});
313286

314287
await waitFor(() => {
@@ -317,7 +290,7 @@ describe("useChatToolInvalidations", () => {
317290
});
318291

319292
it("does nothing when streamState is null", () => {
320-
const { store } = createTestStore(null);
293+
const store = createChatStore();
321294
const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");
322295

323296
renderHook(

0 commit comments

Comments
 (0)