Fix SSE reliability and message delivery for production environments (#85)

* Switch to sync prompt endpoint for reliable message delivery

Use the synchronous /message endpoint instead of /prompt_async to ensure
the UI receives authoritative response data. This matches OpenCode TUI
behavior and prevents message desync during sub-agent tasks.

* Add SSE heartbeat to prevent Cloudflare 524 timeout
This commit is contained in:
Chris Scott authored and GitHub committed 2026-01-27 09:50:15 -05:00
1 parent d78bb120ed
commit a05535e0ef
6 files changed
+61 -10

No files matched your search

+29 -6
View File
@@ -328,15 +328,26 @@ export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?:
requestData.variant = variant;
}
await client.sendPrompt(sessionID, requestData);
const response = await client.sendPrompt(sessionID, requestData);
return { optimisticUserID, userPromptText };
return { optimisticUserID, userPromptText, response };
},
onError: (error, variables) => {
const { sessionID } = variables;
const messagesQueryKey = ["opencode", "messages", opcodeUrl, sessionID, directory];
const axiosError = error as { code?: string; response?: unknown };
const isNetworkError = axiosError.code === 'ECONNABORTED' ||
axiosError.code === 'ERR_NETWORK' ||
!axiosError.response;
if (isNetworkError) {
return;
}
setSessionStatus(sessionID, { type: "idle" });
queryClient.setQueryData<MessageListResponse>(
["opencode", "messages", opcodeUrl, sessionID, directory],
messagesQueryKey,
(old) => old?.filter((msg) => !msg.info.id.startsWith("optimistic_")),
);
@@ -348,16 +359,28 @@ export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?:
},
onSuccess: async (data, variables) => {
const { sessionID } = variables;
const { optimisticUserID, userPromptText } = data;
const { optimisticUserID, userPromptText, response } = data;
const messagesQueryKey = ["opencode", "messages", opcodeUrl, sessionID, directory];
queryClient.setQueryData<MessageListResponse>(
["opencode", "messages", opcodeUrl, sessionID, directory],
messagesQueryKey,
(old) => {
if (!old) return old;
return old.filter((msg) => msg.info.id !== optimisticUserID);
const withoutOptimistic = old.filter((msg) => msg.info.id !== optimisticUserID);
const existingIdx = withoutOptimistic.findIndex(m => m.info.id === response.info.id);
if (existingIdx >= 0) {
const updated = [...withoutOptimistic];
updated[existingIdx] = { info: response.info, parts: response.parts };
return updated;
}
return [...withoutOptimistic, { info: response.info, parts: response.parts }];
},
);
setSessionStatus(sessionID, { type: "idle" });
queryClient.invalidateQueries({
queryKey: ["opencode", "session", opcodeUrl, sessionID, directory],
});