feat: add session status store with optimistic active state and SSE polling (#272)

* feat: add session status store with optimistic active state and SSE polling

- Adds sessionStatusStore with setOptimisticActive (auto-timeout), statusCache deduplication, and replaceStatuses/clearStatus

- Integrates store in useSendPrompt (set optimistic on send, clear on error)

- Adds session.status SSE event handling in useSSE with 5s polling fallback

- Refactors test to mock the store instead of using real store instance

- Fixes repoId null check in useRepoActivity

* feat: replace event-source-polyfill with native SSE for PromptInput

* fix: keep stop button visible and hide clear prompt button during active session

* feat: restore failed queued prompts and preserve optimistic active status during poll

* test: add MessageThread tests for optimistic editing state and resend flow

* fix: handle queued prompt edge cases during pending submission and SSE errors
This commit is contained in:
Chris Scott authored and GitHub committed 2026-06-10 08:35:19 -04:00
1 parent 931f1215e6
commit 4e32fd5b00
16 files changed
+833 -61

No files matched your search

+28 -2
View File
@@ -12,6 +12,7 @@ import type { paths, components } from "../api/opencode-types";
import { parseNetworkError } from "../lib/opencode-errors";
import { showToast } from "../lib/toast";
import { useSendErrorStore } from "../stores/sendErrorStore";
import { useSessionStatus } from "../stores/sessionStatusStore";
import { invalidateSessionListCaches, messagesQueryKey } from "../lib/queryInvalidation";
type AssistantMessage = components["schemas"]["AssistantMessage"];
@@ -366,6 +367,16 @@ const createOptimisticUserMessageInfo = (
return { ...message, agent, variant } as Message;
};
const getPromptText = (prompt: string | undefined, parts: ContentPart[] | undefined): string => {
if (prompt !== undefined) return prompt;
if (!parts) return "";
return parts
.filter((part): part is ContentPart & { type: "text" } => part.type === "text")
.map((part) => part.content)
.join("")
.trim();
};
export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?: string) => {
const client = useOpenCodeClient(opcodeUrl, directory);
const queryClient = useQueryClient();
@@ -411,6 +422,7 @@ export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?:
queryKey,
(old) => [...(old || []), optimisticMessageWithParts],
);
useSessionStatus.getState().setOptimisticActive(sessionID);
const requestData: SendPromptRequest = {
parts: parts?.map((part) =>
@@ -469,7 +481,15 @@ export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?:
}
if (queued) {
await client.sendPromptAsync(sessionID, requestData);
useSendErrorStore.getState().setQueuedPrompt(sessionID, getPromptText(prompt, parts));
try {
await client.sendPromptAsync(sessionID, requestData);
} catch (error) {
useSendErrorStore.getState().clearQueuedPrompt(sessionID);
throw error;
}
return { optimisticUserID, queued: true };
}
@@ -478,9 +498,13 @@ export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?:
return { optimisticUserID, response, queued: false };
},
onError: (error, variables) => {
const { sessionID } = variables;
const { sessionID, queued } = variables;
const queryKey = messagesQueryKey(opcodeUrl, sessionID, directory);
if (queued) {
useSendErrorStore.getState().clearQueuedPrompt(sessionID);
}
queryClient.setQueryData<MessageWithParts[]>(
queryKey,
(old) => old?.filter((msgWithParts) => !msgWithParts.info.id.startsWith("optimistic_")),
@@ -493,6 +517,8 @@ export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?:
return;
}
useSessionStatus.getState().clearStatus(sessionID);
const parsed = parseNetworkError(error);
useSendErrorStore.getState().setError({
sessionID,