mirror of
https://github.com/getpaseo/paseo.git
synced 2026-10-29 04:32:57 +00:00
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import type { Command } from "commander";
|
|
import type { ListResult } from "../../output/index.js";
|
|
import {
|
|
attachAgentNamesToMessages,
|
|
connectChatClient,
|
|
parseTimeoutMs,
|
|
toChatCommandError,
|
|
type ChatCommandOptions,
|
|
} from "./shared.js";
|
|
import { chatMessageSchema, type ChatMessageRow, toChatMessageRow } from "./schema.js";
|
|
|
|
export interface ChatWaitOptions extends ChatCommandOptions {
|
|
timeout?: string;
|
|
}
|
|
|
|
export async function runWaitCommand(
|
|
room: string,
|
|
options: ChatWaitOptions,
|
|
_command: Command,
|
|
): Promise<ListResult<ChatMessageRow>> {
|
|
const { client } = await connectChatClient(options.host);
|
|
try {
|
|
const latest = await client.readChatMessages({
|
|
room,
|
|
limit: 1,
|
|
});
|
|
const afterMessageId = latest.messages[0]?.id;
|
|
const payload = await client.waitForChatMessages({
|
|
room,
|
|
afterMessageId,
|
|
timeoutMs: parseTimeoutMs(options.timeout),
|
|
});
|
|
const messages = await attachAgentNamesToMessages(
|
|
client,
|
|
payload.messages.map(toChatMessageRow),
|
|
);
|
|
return {
|
|
type: "list",
|
|
data: messages,
|
|
schema: chatMessageSchema,
|
|
};
|
|
} catch (err) {
|
|
throw toChatCommandError("CHAT_WAIT_FAILED", "wait for chat messages", err);
|
|
} finally {
|
|
await client.close().catch(() => {});
|
|
}
|
|
}
|