Skip to content

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

feat(chat): add support for queued chat acknowledgements

Steven Enamakel3142e66ccf89
diff --git a/app/src/services/chatService.ts b/app/src/services/chatService.ts
index d5bcac123d..5c9ed8c27d 100644
--- a/app/src/services/chatService.ts
+++ b/app/src/services/chatService.ts
@@ -55,6 +55,7 @@ import {
} from './chatWire';
import { callCoreRpc } from './coreRpcClient';
import { socketService } from './socketService';
+import type { QueuedChatAck } from '../types/threadWire.generated';
const chatLog = debug('realtime:chat');
@@ -958,6 +959,38 @@ export interface ChatSendAck {
queuedAttachments: ContentPart[];
}
+function isRecord(value: unknown): value is Record<string, unknown> {
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
+}
+
+function isQueuedAttachment(value: unknown): value is ContentPart {
+ if (!isRecord(value) || typeof value.kind !== 'string') return false;
+ const mediaKinds = new Set(['image', 'video', 'audio', 'file', 'sticker']);
+ if (!mediaKinds.has(value.kind)) return true;
+ if (!isRecord(value.media)) return false;
+ const media = value.media;
+ return (
+ typeof media.opaqueId === 'string' &&
+ media.opaqueId.length > 0 &&
+ media.url == null &&
+ media.path == null
+ );
+}
+
+function isQueuedChatAck(value: unknown): value is QueuedChatAck {
+ return (
+ isRecord(value) &&
+ value.queued === true &&
+ typeof value.queue_mode === 'string' &&
+ typeof value.client_id === 'string' &&
+ typeof value.thread_id === 'string' &&
+ typeof value.request_id === 'string' &&
+ typeof value.queue_depth === 'number' &&
+ Array.isArray(value.queued_attachments) &&
+ value.queued_attachments.every(isQueuedAttachment)
+ );
+}
+
export async function chatSendWithAck(params: ChatSendParams): Promise<ChatSendAck> {
const socket = socketService.getSocket();
const clientId = socket?.id;
@@ -983,15 +1016,12 @@ export async function chatSendWithAck(params: ChatSendParams): Promise<ChatSendA
},
});
- const ack = result as
- | { request_id?: unknown; queued_attachments?: unknown }
- | null;
- const requestId = ack?.request_id;
+ if (!isQueuedChatAck(result)) {
+ throw new Error('Core returned an invalid chat queue acknowledgement');
+ }
return {
- requestId: typeof requestId === 'string' ? requestId : undefined,
- queuedAttachments: Array.isArray(ack?.queued_attachments)
- ? (ack.queued_attachments as ContentPart[])
- : [],
+ requestId: result.request_id,
+ queuedAttachments: result.queued_attachments,
};
}
diff --git a/crates/openhuman-core/src/threads/wire_schema.rs b/crates/openhuman-core/src/threads/wire_schema.rs
index 809c1cca97..4a48149ef3 100644
--- a/crates/openhuman-core/src/threads/wire_schema.rs
+++ b/crates/openhuman-core/src/threads/wire_schema.rs
@@ -20,6 +20,7 @@ pub struct RunLedgerGetWireResponse {
pub struct ThreadWireTypes {
pub thread: ConversationThreadSummary,
pub message: ConversationMessageRecord,
+ pub queued_chat_ack: super::rpc_models::QueuedChatAck,
pub threads_list: ConversationThreadsListResponse,
pub thread_messages: ConversationMessagesResponse,
pub thread_delete: DeleteConversationThreadResponse,
diff --git a/crates/openhuman-core/src/web_chat/ops/start_chat.rs b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
index 24ccbdfc2f..64c512dc02 100644
--- a/crates/openhuman-core/src/web_chat/ops/start_chat.rs
+++ b/crates/openhuman-core/src/web_chat/ops/start_chat.rs
@@ -439,7 +439,7 @@ async fn start_chat_inner(
item_id: Some(request_id.clone()),
text_preview: Some(preview),
});
- serde_json::to_string(&super::super::types::QueuedChatAck {
+ serde_json::to_string(&crate::threads::rpc_models::QueuedChatAck {
queued: true,
queue_mode: mode.to_string(),
client_id,