refactor: modularize slack/config/cron/daemon internals
parent
5d3032b293
commit
f9cbcfca0d
@ -0,0 +1,32 @@
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import JSON5 from "json5";
|
||||
|
||||
export function replaceSensitiveValuesInRaw(params: {
|
||||
raw: string;
|
||||
sensitiveValues: string[];
|
||||
redactedSentinel: string;
|
||||
}): string {
|
||||
const values = [...params.sensitiveValues].toSorted((a, b) => b.length - a.length);
|
||||
let result = params.raw;
|
||||
for (const value of values) {
|
||||
result = result.replaceAll(value, params.redactedSentinel);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function shouldFallbackToStructuredRawRedaction(params: {
|
||||
redactedRaw: string;
|
||||
originalConfig: unknown;
|
||||
restoreParsed: (parsed: unknown) => { ok: boolean; result?: unknown };
|
||||
}): boolean {
|
||||
try {
|
||||
const parsed = JSON5.parse(params.redactedRaw);
|
||||
const restored = params.restoreParsed(parsed);
|
||||
if (!restored.ok) {
|
||||
return true;
|
||||
}
|
||||
return !isDeepStrictEqual(restored.result, params.originalConfig);
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
export function isSecretRefShape(
|
||||
value: Record<string, unknown>,
|
||||
): value is Record<string, unknown> & { source: string; id: string } {
|
||||
return typeof value.source === "string" && typeof value.id === "string";
|
||||
}
|
||||
|
||||
export function redactSecretRefId(params: {
|
||||
value: Record<string, unknown> & { source: string; id: string };
|
||||
values: string[];
|
||||
redactedSentinel: string;
|
||||
isEnvVarPlaceholder: (value: string) => boolean;
|
||||
}): Record<string, unknown> {
|
||||
const { value, values, redactedSentinel, isEnvVarPlaceholder } = params;
|
||||
const redacted: Record<string, unknown> = { ...value };
|
||||
if (!isEnvVarPlaceholder(value.id)) {
|
||||
values.push(value.id);
|
||||
redacted.id = redactedSentinel;
|
||||
}
|
||||
return redacted;
|
||||
}
|
||||
@ -0,0 +1,59 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
shouldEnqueueCronMainSummary,
|
||||
shouldSkipHeartbeatOnlyDelivery,
|
||||
} from "./heartbeat-policy.js";
|
||||
|
||||
describe("shouldSkipHeartbeatOnlyDelivery", () => {
|
||||
it("suppresses empty payloads", () => {
|
||||
expect(shouldSkipHeartbeatOnlyDelivery([], 300)).toBe(true);
|
||||
});
|
||||
|
||||
it("suppresses when any payload is a heartbeat ack and no media is present", () => {
|
||||
expect(
|
||||
shouldSkipHeartbeatOnlyDelivery(
|
||||
[{ text: "Checked inbox and calendar." }, { text: "HEARTBEAT_OK" }],
|
||||
300,
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("does not suppress when media is present", () => {
|
||||
expect(
|
||||
shouldSkipHeartbeatOnlyDelivery(
|
||||
[{ text: "HEARTBEAT_OK", mediaUrl: "https://example.com/image.png" }],
|
||||
300,
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("shouldEnqueueCronMainSummary", () => {
|
||||
const isSystemEvent = (text: string) => text.includes("HEARTBEAT_OK");
|
||||
|
||||
it("enqueues only when delivery was requested but did not run", () => {
|
||||
expect(
|
||||
shouldEnqueueCronMainSummary({
|
||||
summaryText: "HEARTBEAT_OK",
|
||||
deliveryRequested: true,
|
||||
delivered: false,
|
||||
deliveryAttempted: false,
|
||||
suppressMainSummary: false,
|
||||
isCronSystemEvent: isSystemEvent,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("does not enqueue after attempted outbound delivery", () => {
|
||||
expect(
|
||||
shouldEnqueueCronMainSummary({
|
||||
summaryText: "HEARTBEAT_OK",
|
||||
deliveryRequested: true,
|
||||
delivered: false,
|
||||
deliveryAttempted: true,
|
||||
suppressMainSummary: false,
|
||||
isCronSystemEvent: isSystemEvent,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,48 @@
|
||||
import { stripHeartbeatToken } from "../auto-reply/heartbeat.js";
|
||||
|
||||
export type HeartbeatDeliveryPayload = {
|
||||
text?: string;
|
||||
mediaUrl?: string;
|
||||
mediaUrls?: string[];
|
||||
};
|
||||
|
||||
export function shouldSkipHeartbeatOnlyDelivery(
|
||||
payloads: HeartbeatDeliveryPayload[],
|
||||
ackMaxChars: number,
|
||||
): boolean {
|
||||
if (payloads.length === 0) {
|
||||
return true;
|
||||
}
|
||||
const hasAnyMedia = payloads.some(
|
||||
(payload) => (payload.mediaUrls?.length ?? 0) > 0 || Boolean(payload.mediaUrl),
|
||||
);
|
||||
if (hasAnyMedia) {
|
||||
return false;
|
||||
}
|
||||
return payloads.some((payload) => {
|
||||
const result = stripHeartbeatToken(payload.text, {
|
||||
mode: "heartbeat",
|
||||
maxAckChars: ackMaxChars,
|
||||
});
|
||||
return result.shouldSkip;
|
||||
});
|
||||
}
|
||||
|
||||
export function shouldEnqueueCronMainSummary(params: {
|
||||
summaryText: string | undefined;
|
||||
deliveryRequested: boolean;
|
||||
delivered: boolean | undefined;
|
||||
deliveryAttempted: boolean | undefined;
|
||||
suppressMainSummary: boolean;
|
||||
isCronSystemEvent: (text: string) => boolean;
|
||||
}): boolean {
|
||||
const summaryText = params.summaryText?.trim();
|
||||
return Boolean(
|
||||
summaryText &&
|
||||
params.isCronSystemEvent(summaryText) &&
|
||||
params.deliveryRequested &&
|
||||
!params.delivered &&
|
||||
params.deliveryAttempted !== true &&
|
||||
!params.suppressMainSummary,
|
||||
);
|
||||
}
|
||||
@ -0,0 +1,106 @@
|
||||
import { logVerbose } from "../../../globals.js";
|
||||
import type { SlackFile, SlackMessageEvent } from "../../types.js";
|
||||
import {
|
||||
MAX_SLACK_MEDIA_FILES,
|
||||
resolveSlackAttachmentContent,
|
||||
resolveSlackMedia,
|
||||
type SlackMediaResult,
|
||||
type SlackThreadStarter,
|
||||
} from "../media.js";
|
||||
|
||||
export type SlackResolvedMessageContent = {
|
||||
rawBody: string;
|
||||
effectiveDirectMedia: SlackMediaResult[] | null;
|
||||
};
|
||||
|
||||
function filterInheritedParentFiles(params: {
|
||||
files: SlackFile[] | undefined;
|
||||
isThreadReply: boolean;
|
||||
threadStarter: SlackThreadStarter | null;
|
||||
}): SlackFile[] | undefined {
|
||||
const { files, isThreadReply, threadStarter } = params;
|
||||
if (!isThreadReply || !files?.length) {
|
||||
return files;
|
||||
}
|
||||
if (!threadStarter?.files?.length) {
|
||||
return files;
|
||||
}
|
||||
const starterFileIds = new Set(threadStarter.files.map((file) => file.id));
|
||||
const filtered = files.filter((file) => !file.id || !starterFileIds.has(file.id));
|
||||
if (filtered.length < files.length) {
|
||||
logVerbose(
|
||||
`slack: filtered ${files.length - filtered.length} inherited parent file(s) from thread reply`,
|
||||
);
|
||||
}
|
||||
return filtered.length > 0 ? filtered : undefined;
|
||||
}
|
||||
|
||||
export async function resolveSlackMessageContent(params: {
|
||||
message: SlackMessageEvent;
|
||||
isThreadReply: boolean;
|
||||
threadStarter: SlackThreadStarter | null;
|
||||
isBotMessage: boolean;
|
||||
botToken: string;
|
||||
mediaMaxBytes: number;
|
||||
}): Promise<SlackResolvedMessageContent | null> {
|
||||
const ownFiles = filterInheritedParentFiles({
|
||||
files: params.message.files,
|
||||
isThreadReply: params.isThreadReply,
|
||||
threadStarter: params.threadStarter,
|
||||
});
|
||||
|
||||
const media = await resolveSlackMedia({
|
||||
files: ownFiles,
|
||||
token: params.botToken,
|
||||
maxBytes: params.mediaMaxBytes,
|
||||
});
|
||||
|
||||
const attachmentContent = await resolveSlackAttachmentContent({
|
||||
attachments: params.message.attachments,
|
||||
token: params.botToken,
|
||||
maxBytes: params.mediaMaxBytes,
|
||||
});
|
||||
|
||||
const mergedMedia = [...(media ?? []), ...(attachmentContent?.media ?? [])];
|
||||
const effectiveDirectMedia = mergedMedia.length > 0 ? mergedMedia : null;
|
||||
const mediaPlaceholder = effectiveDirectMedia
|
||||
? effectiveDirectMedia.map((item) => item.placeholder).join(" ")
|
||||
: undefined;
|
||||
|
||||
const fallbackFiles = ownFiles ?? [];
|
||||
const fileOnlyFallback =
|
||||
!mediaPlaceholder && fallbackFiles.length > 0
|
||||
? fallbackFiles
|
||||
.slice(0, MAX_SLACK_MEDIA_FILES)
|
||||
.map((file) => file.name?.trim() || "file")
|
||||
.join(", ")
|
||||
: undefined;
|
||||
const fileOnlyPlaceholder = fileOnlyFallback ? `[Slack file: ${fileOnlyFallback}]` : undefined;
|
||||
|
||||
const botAttachmentText =
|
||||
params.isBotMessage && !attachmentContent?.text
|
||||
? (params.message.attachments ?? [])
|
||||
.map((attachment) => attachment.text?.trim() || attachment.fallback?.trim())
|
||||
.filter(Boolean)
|
||||
.join("\n")
|
||||
: undefined;
|
||||
|
||||
const rawBody =
|
||||
[
|
||||
(params.message.text ?? "").trim(),
|
||||
attachmentContent?.text,
|
||||
botAttachmentText,
|
||||
mediaPlaceholder,
|
||||
fileOnlyPlaceholder,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n") || "";
|
||||
if (!rawBody) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
rawBody,
|
||||
effectiveDirectMedia,
|
||||
};
|
||||
}
|
||||
@ -0,0 +1,137 @@
|
||||
import { formatInboundEnvelope } from "../../../auto-reply/envelope.js";
|
||||
import { readSessionUpdatedAt } from "../../../config/sessions.js";
|
||||
import { logVerbose } from "../../../globals.js";
|
||||
import type { ResolvedSlackAccount } from "../../accounts.js";
|
||||
import type { SlackMessageEvent } from "../../types.js";
|
||||
import type { SlackMonitorContext } from "../context.js";
|
||||
import {
|
||||
resolveSlackMedia,
|
||||
resolveSlackThreadHistory,
|
||||
type SlackMediaResult,
|
||||
type SlackThreadStarter,
|
||||
} from "../media.js";
|
||||
|
||||
export type SlackThreadContextData = {
|
||||
threadStarterBody: string | undefined;
|
||||
threadHistoryBody: string | undefined;
|
||||
threadSessionPreviousTimestamp: number | undefined;
|
||||
threadLabel: string | undefined;
|
||||
threadStarterMedia: SlackMediaResult[] | null;
|
||||
};
|
||||
|
||||
export async function resolveSlackThreadContextData(params: {
|
||||
ctx: SlackMonitorContext;
|
||||
account: ResolvedSlackAccount;
|
||||
message: SlackMessageEvent;
|
||||
isThreadReply: boolean;
|
||||
threadTs: string | undefined;
|
||||
threadStarter: SlackThreadStarter | null;
|
||||
roomLabel: string;
|
||||
storePath: string;
|
||||
sessionKey: string;
|
||||
envelopeOptions: ReturnType<
|
||||
typeof import("../../../auto-reply/envelope.js").resolveEnvelopeFormatOptions
|
||||
>;
|
||||
effectiveDirectMedia: SlackMediaResult[] | null;
|
||||
}): Promise<SlackThreadContextData> {
|
||||
let threadStarterBody: string | undefined;
|
||||
let threadHistoryBody: string | undefined;
|
||||
let threadSessionPreviousTimestamp: number | undefined;
|
||||
let threadLabel: string | undefined;
|
||||
let threadStarterMedia: SlackMediaResult[] | null = null;
|
||||
|
||||
if (!params.isThreadReply || !params.threadTs) {
|
||||
return {
|
||||
threadStarterBody,
|
||||
threadHistoryBody,
|
||||
threadSessionPreviousTimestamp,
|
||||
threadLabel,
|
||||
threadStarterMedia,
|
||||
};
|
||||
}
|
||||
|
||||
const starter = params.threadStarter;
|
||||
if (starter?.text) {
|
||||
threadStarterBody = starter.text;
|
||||
const snippet = starter.text.replace(/\s+/g, " ").slice(0, 80);
|
||||
threadLabel = `Slack thread ${params.roomLabel}${snippet ? `: ${snippet}` : ""}`;
|
||||
if (!params.effectiveDirectMedia && starter.files && starter.files.length > 0) {
|
||||
threadStarterMedia = await resolveSlackMedia({
|
||||
files: starter.files,
|
||||
token: params.ctx.botToken,
|
||||
maxBytes: params.ctx.mediaMaxBytes,
|
||||
});
|
||||
if (threadStarterMedia) {
|
||||
const starterPlaceholders = threadStarterMedia.map((item) => item.placeholder).join(", ");
|
||||
logVerbose(`slack: hydrated thread starter file ${starterPlaceholders} from root message`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
threadLabel = `Slack thread ${params.roomLabel}`;
|
||||
}
|
||||
|
||||
const threadInitialHistoryLimit = params.account.config?.thread?.initialHistoryLimit ?? 20;
|
||||
threadSessionPreviousTimestamp = readSessionUpdatedAt({
|
||||
storePath: params.storePath,
|
||||
sessionKey: params.sessionKey,
|
||||
});
|
||||
|
||||
if (threadInitialHistoryLimit > 0 && !threadSessionPreviousTimestamp) {
|
||||
const threadHistory = await resolveSlackThreadHistory({
|
||||
channelId: params.message.channel,
|
||||
threadTs: params.threadTs,
|
||||
client: params.ctx.app.client,
|
||||
currentMessageTs: params.message.ts,
|
||||
limit: threadInitialHistoryLimit,
|
||||
});
|
||||
|
||||
if (threadHistory.length > 0) {
|
||||
const uniqueUserIds = [
|
||||
...new Set(
|
||||
threadHistory.map((item) => item.userId).filter((id): id is string => Boolean(id)),
|
||||
),
|
||||
];
|
||||
const userMap = new Map<string, { name?: string }>();
|
||||
await Promise.all(
|
||||
uniqueUserIds.map(async (id) => {
|
||||
const user = await params.ctx.resolveUserName(id);
|
||||
if (user) {
|
||||
userMap.set(id, user);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
const historyParts: string[] = [];
|
||||
for (const historyMsg of threadHistory) {
|
||||
const msgUser = historyMsg.userId ? userMap.get(historyMsg.userId) : null;
|
||||
const msgSenderName =
|
||||
msgUser?.name ?? (historyMsg.botId ? `Bot (${historyMsg.botId})` : "Unknown");
|
||||
const isBot = Boolean(historyMsg.botId);
|
||||
const role = isBot ? "assistant" : "user";
|
||||
const msgWithId = `${historyMsg.text}\n[slack message id: ${historyMsg.ts ?? "unknown"} channel: ${params.message.channel}]`;
|
||||
historyParts.push(
|
||||
formatInboundEnvelope({
|
||||
channel: "Slack",
|
||||
from: `${msgSenderName} (${role})`,
|
||||
timestamp: historyMsg.ts ? Math.round(Number(historyMsg.ts) * 1000) : undefined,
|
||||
body: msgWithId,
|
||||
chatType: "channel",
|
||||
envelope: params.envelopeOptions,
|
||||
}),
|
||||
);
|
||||
}
|
||||
threadHistoryBody = historyParts.join("\n\n");
|
||||
logVerbose(
|
||||
`slack: populated thread history with ${threadHistory.length} messages for new session`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
threadStarterBody,
|
||||
threadHistoryBody,
|
||||
threadSessionPreviousTimestamp,
|
||||
threadLabel,
|
||||
threadStarterMedia,
|
||||
};
|
||||
}
|
||||
Loading…
Reference in New Issue