diff --git a/src/slack/monitor/message-handler/prepare.ts b/src/slack/monitor/message-handler/prepare.ts index 7e3ccf5f9..72b7d9271 100644 --- a/src/slack/monitor/message-handler/prepare.ts +++ b/src/slack/monitor/message-handler/prepare.ts @@ -35,7 +35,6 @@ import { buildPairingReply } from "../../../pairing/pairing-messages.js"; import { upsertChannelPairingRequest } from "../../../pairing/pairing-store.js"; import { resolveAgentRoute } from "../../../routing/resolve-route.js"; import { resolveThreadSessionKeys } from "../../../routing/session-key.js"; -import { buildUntrustedChannelMetadata } from "../../../security/channel-metadata.js"; import { reactSlackMessage } from "../../actions.js"; import { sendMessageSlack } from "../../send.js"; import { resolveSlackThreadContext } from "../../threading.js"; @@ -49,6 +48,7 @@ import { resolveSlackThreadHistory, resolveSlackThreadStarter, } from "../media.js"; +import { resolveSlackRoomContextHints } from "../room-context.js"; export async function prepareSlackMessage(params: { ctx: SlackMonitorContext; @@ -452,18 +452,11 @@ export async function prepareSlackMessage(params: { const slackTo = isDirectMessage ? `user:${message.user}` : `channel:${message.channel}`; - const untrustedChannelMetadata = isRoomish - ? buildUntrustedChannelMetadata({ - source: "slack", - label: "Slack channel description", - entries: [channelInfo?.topic, channelInfo?.purpose], - }) - : undefined; - const systemPromptParts = [channelConfig?.systemPrompt?.trim() || null].filter( - (entry): entry is string => Boolean(entry), - ); - const groupSystemPrompt = - systemPromptParts.length > 0 ? systemPromptParts.join("\n\n") : undefined; + const { untrustedChannelMetadata, groupSystemPrompt } = resolveSlackRoomContextHints({ + isRoomish, + channelInfo, + channelConfig, + }); let threadStarterBody: string | undefined; let threadHistoryBody: string | undefined; diff --git a/src/slack/monitor/room-context.ts b/src/slack/monitor/room-context.ts new file mode 100644 index 000000000..653591362 --- /dev/null +++ b/src/slack/monitor/room-context.ts @@ -0,0 +1,31 @@ +import { buildUntrustedChannelMetadata } from "../../security/channel-metadata.js"; + +export function resolveSlackRoomContextHints(params: { + isRoomish: boolean; + channelInfo?: { topic?: string; purpose?: string }; + channelConfig?: { systemPrompt?: string | null } | null; +}): { + untrustedChannelMetadata?: ReturnType; + groupSystemPrompt?: string; +} { + if (!params.isRoomish) { + return {}; + } + + const untrustedChannelMetadata = buildUntrustedChannelMetadata({ + source: "slack", + label: "Slack channel description", + entries: [params.channelInfo?.topic, params.channelInfo?.purpose], + }); + + const systemPromptParts = [params.channelConfig?.systemPrompt?.trim() || null].filter( + (entry): entry is string => Boolean(entry), + ); + const groupSystemPrompt = + systemPromptParts.length > 0 ? systemPromptParts.join("\n\n") : undefined; + + return { + untrustedChannelMetadata, + groupSystemPrompt, + }; +} diff --git a/src/slack/monitor/slash.ts b/src/slack/monitor/slash.ts index 3ee2b3083..61046ede9 100644 --- a/src/slack/monitor/slash.ts +++ b/src/slack/monitor/slash.ts @@ -26,7 +26,6 @@ import { upsertChannelPairingRequest, } from "../../pairing/pairing-store.js"; import { resolveAgentRoute } from "../../routing/resolve-route.js"; -import { buildUntrustedChannelMetadata } from "../../security/channel-metadata.js"; import { normalizeAllowList, normalizeAllowListLower, @@ -38,6 +37,7 @@ import { buildSlackSlashCommandMatcher, resolveSlackSlashCommandConfig } from ". import { normalizeSlackChannelType } from "./context.js"; import { isSlackChannelAllowedByPolicy } from "./policy.js"; import { deliverSlackSlashReplies } from "./replies.js"; +import { resolveSlackRoomContextHints } from "./room-context.js"; type SlackBlock = { type: string; [key: string]: unknown }; @@ -387,18 +387,11 @@ export function registerSlackMonitorSlashCommands(params: { }, }); - const untrustedChannelMetadata = isRoomish - ? buildUntrustedChannelMetadata({ - source: "slack", - label: "Slack channel description", - entries: [channelInfo?.topic, channelInfo?.purpose], - }) - : undefined; - const systemPromptParts = [channelConfig?.systemPrompt?.trim() || null].filter( - (entry): entry is string => Boolean(entry), - ); - const groupSystemPrompt = - systemPromptParts.length > 0 ? systemPromptParts.join("\n\n") : undefined; + const { untrustedChannelMetadata, groupSystemPrompt } = resolveSlackRoomContextHints({ + isRoomish, + channelInfo, + channelConfig, + }); const ctxPayload = finalizeInboundContext({ Body: prompt,