mirror of
https://github.com/moltbot/moltbot.git
synced 2026-05-01 18:48:16 +00:00
feat(slack): add draft preview cleanup lifecycle
This commit is contained in:
@@ -103,4 +103,54 @@ describe("createSlackDraftStream", () => {
|
|||||||
expect(edit).not.toHaveBeenCalled();
|
expect(edit).not.toHaveBeenCalled();
|
||||||
expect(warn).toHaveBeenCalledTimes(1);
|
expect(warn).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("clear removes preview message when one exists", async () => {
|
||||||
|
const send = vi.fn(async () => ({
|
||||||
|
channelId: "C123",
|
||||||
|
messageId: "111.222",
|
||||||
|
}));
|
||||||
|
const edit = vi.fn(async () => {});
|
||||||
|
const remove = vi.fn(async () => {});
|
||||||
|
const stream = createSlackDraftStream({
|
||||||
|
target: "channel:C123",
|
||||||
|
token: "xoxb-test",
|
||||||
|
throttleMs: 250,
|
||||||
|
send,
|
||||||
|
edit,
|
||||||
|
remove,
|
||||||
|
});
|
||||||
|
|
||||||
|
stream.update("hello");
|
||||||
|
await stream.flush();
|
||||||
|
await stream.clear();
|
||||||
|
|
||||||
|
expect(remove).toHaveBeenCalledTimes(1);
|
||||||
|
expect(remove).toHaveBeenCalledWith("C123", "111.222", {
|
||||||
|
token: "xoxb-test",
|
||||||
|
accountId: undefined,
|
||||||
|
});
|
||||||
|
expect(stream.messageId()).toBeUndefined();
|
||||||
|
expect(stream.channelId()).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clear is a no-op when no preview message exists", async () => {
|
||||||
|
const send = vi.fn(async () => ({
|
||||||
|
channelId: "C123",
|
||||||
|
messageId: "111.222",
|
||||||
|
}));
|
||||||
|
const edit = vi.fn(async () => {});
|
||||||
|
const remove = vi.fn(async () => {});
|
||||||
|
const stream = createSlackDraftStream({
|
||||||
|
target: "channel:C123",
|
||||||
|
token: "xoxb-test",
|
||||||
|
throttleMs: 250,
|
||||||
|
send,
|
||||||
|
edit,
|
||||||
|
remove,
|
||||||
|
});
|
||||||
|
|
||||||
|
await stream.clear();
|
||||||
|
|
||||||
|
expect(remove).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { editSlackMessage } from "./actions.js";
|
import { deleteSlackMessage, editSlackMessage } from "./actions.js";
|
||||||
import { sendMessageSlack } from "./send.js";
|
import { sendMessageSlack } from "./send.js";
|
||||||
|
|
||||||
const SLACK_STREAM_MAX_CHARS = 4000;
|
const SLACK_STREAM_MAX_CHARS = 4000;
|
||||||
@@ -7,6 +7,7 @@ const DEFAULT_THROTTLE_MS = 1000;
|
|||||||
export type SlackDraftStream = {
|
export type SlackDraftStream = {
|
||||||
update: (text: string) => void;
|
update: (text: string) => void;
|
||||||
flush: () => Promise<void>;
|
flush: () => Promise<void>;
|
||||||
|
clear: () => Promise<void>;
|
||||||
stop: () => void;
|
stop: () => void;
|
||||||
forceNewMessage: () => void;
|
forceNewMessage: () => void;
|
||||||
messageId: () => string | undefined;
|
messageId: () => string | undefined;
|
||||||
@@ -25,11 +26,13 @@ export function createSlackDraftStream(params: {
|
|||||||
warn?: (message: string) => void;
|
warn?: (message: string) => void;
|
||||||
send?: typeof sendMessageSlack;
|
send?: typeof sendMessageSlack;
|
||||||
edit?: typeof editSlackMessage;
|
edit?: typeof editSlackMessage;
|
||||||
|
remove?: typeof deleteSlackMessage;
|
||||||
}): SlackDraftStream {
|
}): SlackDraftStream {
|
||||||
const maxChars = Math.min(params.maxChars ?? SLACK_STREAM_MAX_CHARS, SLACK_STREAM_MAX_CHARS);
|
const maxChars = Math.min(params.maxChars ?? SLACK_STREAM_MAX_CHARS, SLACK_STREAM_MAX_CHARS);
|
||||||
const throttleMs = Math.max(250, params.throttleMs ?? DEFAULT_THROTTLE_MS);
|
const throttleMs = Math.max(250, params.throttleMs ?? DEFAULT_THROTTLE_MS);
|
||||||
const send = params.send ?? sendMessageSlack;
|
const send = params.send ?? sendMessageSlack;
|
||||||
const edit = params.edit ?? editSlackMessage;
|
const edit = params.edit ?? editSlackMessage;
|
||||||
|
const remove = params.remove ?? deleteSlackMessage;
|
||||||
|
|
||||||
let streamMessageId: string | undefined;
|
let streamMessageId: string | undefined;
|
||||||
let streamChannelId: string | undefined;
|
let streamChannelId: string | undefined;
|
||||||
@@ -152,6 +155,31 @@ export function createSlackDraftStream(params: {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const clear = async () => {
|
||||||
|
stop();
|
||||||
|
if (inFlightPromise) {
|
||||||
|
await inFlightPromise;
|
||||||
|
}
|
||||||
|
const channelId = streamChannelId;
|
||||||
|
const messageId = streamMessageId;
|
||||||
|
streamChannelId = undefined;
|
||||||
|
streamMessageId = undefined;
|
||||||
|
lastSentText = "";
|
||||||
|
if (!channelId || !messageId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await remove(channelId, messageId, {
|
||||||
|
token: params.token,
|
||||||
|
accountId: params.accountId,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
params.warn?.(
|
||||||
|
`slack stream preview cleanup failed: ${err instanceof Error ? err.message : String(err)}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const forceNewMessage = () => {
|
const forceNewMessage = () => {
|
||||||
streamMessageId = undefined;
|
streamMessageId = undefined;
|
||||||
streamChannelId = undefined;
|
streamChannelId = undefined;
|
||||||
@@ -164,6 +192,7 @@ export function createSlackDraftStream(params: {
|
|||||||
return {
|
return {
|
||||||
update,
|
update,
|
||||||
flush,
|
flush,
|
||||||
|
clear,
|
||||||
stop,
|
stop,
|
||||||
forceNewMessage,
|
forceNewMessage,
|
||||||
messageId: () => streamMessageId,
|
messageId: () => streamMessageId,
|
||||||
|
|||||||
@@ -135,7 +135,8 @@ export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessag
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else if (mediaCount > 0) {
|
} else if (mediaCount > 0) {
|
||||||
draftStream?.stop();
|
await draftStream?.clear();
|
||||||
|
hasStreamedMessage = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const replyThreadTs = replyPlan.nextThreadTs();
|
const replyThreadTs = replyPlan.nextThreadTs();
|
||||||
@@ -215,6 +216,7 @@ export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessag
|
|||||||
const anyReplyDelivered = queuedFinal || (counts.block ?? 0) > 0 || (counts.final ?? 0) > 0;
|
const anyReplyDelivered = queuedFinal || (counts.block ?? 0) > 0 || (counts.final ?? 0) > 0;
|
||||||
|
|
||||||
if (!anyReplyDelivered) {
|
if (!anyReplyDelivered) {
|
||||||
|
await draftStream.clear();
|
||||||
if (prepared.isRoomish) {
|
if (prepared.isRoomish) {
|
||||||
clearHistoryEntriesIfEnabled({
|
clearHistoryEntriesIfEnabled({
|
||||||
historyMap: ctx.channelHistories,
|
historyMap: ctx.channelHistories,
|
||||||
|
|||||||
Reference in New Issue
Block a user