refactor(site/src): address the frontend self-review of slice 12c

Shorten the durable tool-call comment and drop the one in its test,
narrow the tool-call lookup with a type predicate, keep only the
non-obvious rollback note on the optimistic chat field helper, and let
that helper accept any chat field.
This commit is contained in:
Thomas Kosiewski committed 2026-10-01 03:09:58 +00:00
1 parent 858543b273
commit c6e503a692
3 files changed
+8 -14

No files matched your search

+2 -5
View File
@@ -1435,11 +1435,8 @@ export const unarchiveChat = (queryClient: QueryClient) => ({
type OptimisticChatUpdateContext = { previousChat?: TypesGen.Chat };
// Writes one chat field into the list and entity caches before the server
// responds, and restores it from the entity snapshot when the update fails.
const optimisticChatFieldUpdate = <
K extends "plan_mode" | "manage_automations_enabled",
>(
// The list rollback reads the field from the entity snapshot.
const optimisticChatFieldUpdate = <K extends keyof TypesGen.Chat>(
queryClient: QueryClient,
field: K,
) => ({
@@ -392,8 +392,6 @@ describe("useChatToolInvalidations", () => {
queryClient.setQueryData(chatAutomationsKey("org-1"), []);
queryClient.setQueryData(infiniteChatsKey, { pages: [], pageParams: [] });
const { store, setStreamState } = renderInvalidations();
// The server persists the assistant tool-call message before the tool
// runs, so the streamed result arrives without a live call.
const { toolResults } = createStreamState("manage_automations");
await act(async () => {
@@ -10,6 +10,7 @@ import {
invalidateChatsByWorkspace,
} from "#/api/queries/chats";
import { invalidateWorkspaceMutationQueries } from "#/api/queries/workspaces";
import type { ChatToolCallPart } from "#/api/typesGenerated";
import { asString } from "../ChatElements/runtimeTypeUtils";
import { parseArgs } from "../ChatElements/tools/utils";
import { type ChatStore, useChatSelector } from "./chatStore";
@@ -65,11 +66,8 @@ export function useChatToolInvalidations({
const toolResults = useChatSelector(store, selectStreamToolResults);
const processedToolCallIdsRef = useRef<Set<string>>(new Set());
const chatIDRef = useRef(chatID);
// Not subscribed: a tool call's args land before its result, so the
// snapshot is current whenever a new result triggers the effect. The
// server persists the assistant tool-call message before its tools run
// and the live stream resets, so a streamed result usually has only the
// durable call.
// Not subscribed: a call's args land before its result. The live call is
// usually gone by then, so fall back to the durable message.
const readToolCallArgs = useEffectEvent((toolCallID: string): unknown => {
const { streamState, messagesByID, orderedMessageIDs } =
store.getSnapshot();
@@ -83,9 +81,10 @@ export function useChatToolInvalidations({
continue;
}
const part = message.content?.find(
(part) => part.type === "tool-call" && part.tool_call_id === toolCallID,
(part): part is ChatToolCallPart =>
part.type === "tool-call" && part.tool_call_id === toolCallID,
);
if (part?.type === "tool-call") {
if (part) {
return part.args;
}
}