Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(chat): introduce chatSendWithAck and update chatSend

Steven Enamakel4952b41d753c
diff --git a/app/src/services/chatService.ts b/app/src/services/chatService.ts
index 73d7ec52cf..d5bcac123d 100644
--- a/app/src/services/chatService.ts
+++ b/app/src/services/chatService.ts
@@ -953,7 +953,12 @@ interface ChatSendParams {
* one — used by `parallel` sends to register the forked turn's stream lane.
* `undefined` if the ack carried no id.
*/
-export async function chatSend(params: ChatSendParams): Promise<string | undefined> {
+export interface ChatSendAck {
+ requestId?: string;
+ queuedAttachments: ContentPart[];
+}
+
+export async function chatSendWithAck(params: ChatSendParams): Promise<ChatSendAck> {
const socket = socketService.getSocket();
const clientId = socket?.id;
if (!clientId) {
@@ -978,8 +983,20 @@ export async function chatSend(params: ChatSendParams): Promise<string | undefin
},
});
- const requestId = (result as { request_id?: unknown } | null)?.request_id;
- return typeof requestId === 'string' ? requestId : undefined;
+ const ack = result as
+ | { request_id?: unknown; queued_attachments?: unknown }
+ | null;
+ const requestId = ack?.request_id;
+ return {
+ requestId: typeof requestId === 'string' ? requestId : undefined,
+ queuedAttachments: Array.isArray(ack?.queued_attachments)
+ ? (ack.queued_attachments as ContentPart[])
+ : [],
+ };
+}
+
+export async function chatSend(params: ChatSendParams): Promise<string | undefined> {
+ return (await chatSendWithAck(params)).requestId;
}
/** `client_id` sent with a Stop issued while the socket has no id. */
diff --git a/app/src/store/queueSlice.ts b/app/src/store/queueSlice.ts
index 031c1b0e6d..98ac680cab 100644
--- a/app/src/store/queueSlice.ts
+++ b/app/src/store/queueSlice.ts
@@ -7,11 +7,10 @@
* filled and drained by the `queue_item_queued` / `queue_item_delivered` /
* `queue_item_removed` socket events. It is what the user sees, so it can
* never show a message the core no longer holds.
- * - `pendingFollowupsByThread` is the composer's: the full user message behind
- * every follow-up this client queued. The web channel never writes user
- * messages to the transcript, so `ChatRuntimeProvider` appends these when the
- * turn ends, after that turn's reply. A queue item only carries an 80-char
- * preview, which is why the message itself has to be kept here.
+ * - `pendingFollowupsByThread` keeps the local display until that queued
+ * request's terminal event confirms its user row was persisted by the core.
+ * Queue events carry only a preview; the ack's request ID links them to the
+ * eventual persisted row without a second frontend append.
*
* The two are linked by preview text only. The core mints the item id and the
* `channel_web_chat` ack does not return it, so removing an item drops the
@@ -39,10 +38,12 @@ export interface RunQueueItem {
/** A follow-up this client queued, held until it can be persisted. */
export interface PendingFollowup {
- /** The user message exactly as an interactive send would store it (text only). */
+ /** The user message shown while the core is executing this queued request. */
message: ThreadMessage;
- /** Its typed attachments; the core stages them when the follow-up is persisted. */
+ /** Sanitized opaque refs returned by the queue ack. */
attachments: ContentPart[];
+ /** The same ID used by the queue item and the turn's terminal event. */
+ requestId: string;
/** `clipQueuePreview` of the text sent to the core; matches the item's preview. */
preview: string;
}
@@ -120,6 +121,18 @@ const queueSlice = createSlice({
queueItemDelivered: (state, action: PayloadAction<{ threadId: string; itemId: string }>) => {
dropItem(state, action.payload.threadId, action.payload.itemId);
},
+ /** The runner persisted this queued request and its terminal event arrived. */
+ pendingFollowupCompleted: (
+ state,
+ action: PayloadAction<{ threadId: string; requestId: string }>
+ ) => {
+ const { threadId, requestId } = action.payload;
+ const pending = state.pendingFollowupsByThread[threadId];
+ if (!pending) return;
+ const remaining = pending.filter(item => item.requestId !== requestId);
+ if (remaining.length) state.pendingFollowupsByThread[threadId] = remaining;
+ else delete state.pendingFollowupsByThread[threadId];
+ },
/** The item was taken out of the queue and will never be sent. */
queueItemRemoved: (state, action: PayloadAction<{ threadId: string; itemId: string }>) => {
const { threadId, itemId } = action.payload;
@@ -138,21 +151,22 @@ const queueSlice = createSlice({
threadId: string;
message: ThreadMessage;
text: string;
+ requestId: string;
attachments?: ContentPart[];
}>
) => {
- const { threadId, message, text, attachments = [] } = action.payload;
+ const { threadId, message, text, requestId, attachments = [] } = action.payload;
const bucket = state.pendingFollowupsByThread[threadId] ?? [];
- bucket.push({ message, attachments, preview: clipQueuePreview(text, attachments) });
+ bucket.push({ message, attachments, requestId, preview: clipQueuePreview(text, attachments) });
state.pendingFollowupsByThread[threadId] = bucket;
},
},
extraReducers: builder => {
// The turn ended, so the core is dispatching whatever it still queued, and
// `ChatRuntimeProvider` has already persisted the pending follow-ups.
- builder.addCase(endInferenceTurn, (state, action) =>
- clearThread(state, action.payload.threadId)
- );
+ builder.addCase(endInferenceTurn, (state, action) => {
+ delete state.itemsByThread[action.payload.threadId];
+ });
builder.addCase(clearRuntimeForThread, (state, action) =>
clearThread(state, action.payload.threadId)
);
@@ -161,7 +175,12 @@ const queueSlice = createSlice({
},
});
-export const { queueItemQueued, queueItemDelivered, queueItemRemoved, pendingFollowupAdded } =
- queueSlice.actions;
+export const {
+ queueItemQueued,
+ queueItemDelivered,
+ queueItemRemoved,
+ pendingFollowupAdded,
+ pendingFollowupCompleted,
+} = queueSlice.actions;
export default queueSlice.reducer;