172 lines
5.4 KiB
TypeScript
172 lines
5.4 KiB
TypeScript
import {
|
|
resolveEffectiveMessagesConfig,
|
|
resolveHumanDelayConfig,
|
|
resolveIdentityName,
|
|
} from "../../../agents/identity.js";
|
|
import {
|
|
extractShortModelName,
|
|
type ResponsePrefixContext,
|
|
} from "../../../auto-reply/reply/response-prefix-template.js";
|
|
import { dispatchInboundMessage } from "../../../auto-reply/dispatch.js";
|
|
import { clearHistoryEntriesIfEnabled } from "../../../auto-reply/reply/history.js";
|
|
import { removeAckReactionAfterReply } from "../../../channels/ack-reactions.js";
|
|
import { createTypingCallbacks } from "../../../channels/typing.js";
|
|
import { createReplyDispatcherWithTyping } from "../../../auto-reply/reply/reply-dispatcher.js";
|
|
import { danger, logVerbose, shouldLogVerbose } from "../../../globals.js";
|
|
import { removeSlackReaction } from "../../actions.js";
|
|
import { resolveSlackThreadTargets } from "../../threading.js";
|
|
|
|
import { createSlackReplyDeliveryPlan, deliverReplies } from "../replies.js";
|
|
|
|
import type { PreparedSlackMessage } from "./types.js";
|
|
|
|
export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessage) {
|
|
const { ctx, account, message, route } = prepared;
|
|
const cfg = ctx.cfg;
|
|
const runtime = ctx.runtime;
|
|
|
|
const { statusThreadTs } = resolveSlackThreadTargets({
|
|
message,
|
|
replyToMode: ctx.replyToMode,
|
|
});
|
|
|
|
const messageTs = message.ts ?? message.event_ts;
|
|
const incomingThreadTs = message.thread_ts;
|
|
let didSetStatus = false;
|
|
|
|
// Shared mutable ref for "replyToMode=first". Both tool + auto-reply flows
|
|
// mark this to ensure only the first reply is threaded.
|
|
const hasRepliedRef = { value: false };
|
|
const replyPlan = createSlackReplyDeliveryPlan({
|
|
replyToMode: ctx.replyToMode,
|
|
incomingThreadTs,
|
|
messageTs,
|
|
hasRepliedRef,
|
|
});
|
|
|
|
const typingCallbacks = createTypingCallbacks({
|
|
start: async () => {
|
|
didSetStatus = true;
|
|
await ctx.setSlackThreadStatus({
|
|
channelId: message.channel,
|
|
threadTs: statusThreadTs,
|
|
status: "is typing...",
|
|
});
|
|
},
|
|
stop: async () => {
|
|
if (!didSetStatus) return;
|
|
await ctx.setSlackThreadStatus({
|
|
channelId: message.channel,
|
|
threadTs: statusThreadTs,
|
|
status: "",
|
|
});
|
|
},
|
|
onStartError: (err) => {
|
|
runtime.error?.(danger(`slack typing cue failed: ${String(err)}`));
|
|
},
|
|
onStopError: (err) => {
|
|
runtime.error?.(danger(`slack typing stop failed: ${String(err)}`));
|
|
},
|
|
});
|
|
|
|
// Create mutable context for response prefix template interpolation
|
|
let prefixContext: ResponsePrefixContext = {
|
|
identityName: resolveIdentityName(cfg, route.agentId),
|
|
};
|
|
|
|
const { dispatcher, replyOptions, markDispatchIdle } = createReplyDispatcherWithTyping({
|
|
responsePrefix: resolveEffectiveMessagesConfig(cfg, route.agentId).responsePrefix,
|
|
responsePrefixContextProvider: () => prefixContext,
|
|
humanDelay: resolveHumanDelayConfig(cfg, route.agentId),
|
|
deliver: async (payload) => {
|
|
const replyThreadTs = replyPlan.nextThreadTs();
|
|
await deliverReplies({
|
|
replies: [payload],
|
|
target: prepared.replyTarget,
|
|
token: ctx.botToken,
|
|
accountId: account.accountId,
|
|
runtime,
|
|
textLimit: ctx.textLimit,
|
|
replyThreadTs,
|
|
});
|
|
replyPlan.markSent();
|
|
},
|
|
onError: (err, info) => {
|
|
runtime.error?.(danger(`slack ${info.kind} reply failed: ${String(err)}`));
|
|
typingCallbacks.onIdle?.();
|
|
},
|
|
onReplyStart: typingCallbacks.onReplyStart,
|
|
onIdle: typingCallbacks.onIdle,
|
|
});
|
|
|
|
const { queuedFinal, counts } = await dispatchInboundMessage({
|
|
ctx: prepared.ctxPayload,
|
|
cfg,
|
|
dispatcher,
|
|
replyOptions: {
|
|
...replyOptions,
|
|
skillFilter: prepared.channelConfig?.skills,
|
|
hasRepliedRef,
|
|
disableBlockStreaming:
|
|
typeof account.config.blockStreaming === "boolean"
|
|
? !account.config.blockStreaming
|
|
: undefined,
|
|
onModelSelected: (ctx) => {
|
|
// Mutate the object directly instead of reassigning to ensure the closure sees updates
|
|
prefixContext.provider = ctx.provider;
|
|
prefixContext.model = extractShortModelName(ctx.model);
|
|
prefixContext.modelFull = `${ctx.provider}/${ctx.model}`;
|
|
prefixContext.thinkingLevel = ctx.thinkLevel ?? "off";
|
|
},
|
|
},
|
|
});
|
|
markDispatchIdle();
|
|
|
|
if (!queuedFinal) {
|
|
if (prepared.isRoomish) {
|
|
clearHistoryEntriesIfEnabled({
|
|
historyMap: ctx.channelHistories,
|
|
historyKey: prepared.historyKey,
|
|
limit: ctx.historyLimit,
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (shouldLogVerbose()) {
|
|
const finalCount = counts.final;
|
|
logVerbose(
|
|
`slack: delivered ${finalCount} reply${finalCount === 1 ? "" : "ies"} to ${prepared.replyTarget}`,
|
|
);
|
|
}
|
|
|
|
removeAckReactionAfterReply({
|
|
removeAfterReply: ctx.removeAckAfterReply,
|
|
ackReactionPromise: prepared.ackReactionPromise,
|
|
ackReactionValue: prepared.ackReactionValue,
|
|
remove: () =>
|
|
removeSlackReaction(
|
|
message.channel,
|
|
prepared.ackReactionMessageTs ?? "",
|
|
prepared.ackReactionValue,
|
|
{
|
|
token: ctx.botToken,
|
|
client: ctx.app.client,
|
|
},
|
|
),
|
|
onError: (err) => {
|
|
logVerbose(
|
|
`slack: failed to remove ack reaction from ${message.channel}/${message.ts}: ${String(err)}`,
|
|
);
|
|
},
|
|
});
|
|
|
|
if (prepared.isRoomish) {
|
|
clearHistoryEntriesIfEnabled({
|
|
historyMap: ctx.channelHistories,
|
|
historyKey: prepared.historyKey,
|
|
limit: ctx.historyLimit,
|
|
});
|
|
}
|
|
}
|