fix: finalize inbound contexts
This commit is contained in:
59
src/auto-reply/reply/inbound-context.ts
Normal file
59
src/auto-reply/reply/inbound-context.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { normalizeChatType } from "../../channels/chat-type.js";
|
||||
import { resolveConversationLabel } from "../../channels/conversation-label.js";
|
||||
import type { MsgContext } from "../templating.js";
|
||||
import { normalizeInboundTextNewlines } from "./inbound-text.js";
|
||||
|
||||
export type FinalizeInboundContextOptions = {
|
||||
forceBodyForAgent?: boolean;
|
||||
forceBodyForCommands?: boolean;
|
||||
forceChatType?: boolean;
|
||||
forceConversationLabel?: boolean;
|
||||
};
|
||||
|
||||
function normalizeTextField(value: unknown): string | undefined {
|
||||
if (typeof value !== "string") return undefined;
|
||||
return normalizeInboundTextNewlines(value);
|
||||
}
|
||||
|
||||
export function finalizeInboundContext<T extends Record<string, unknown>>(
|
||||
ctx: T,
|
||||
opts: FinalizeInboundContextOptions = {},
|
||||
): T & MsgContext {
|
||||
const normalized = ctx as T & MsgContext;
|
||||
|
||||
normalized.Body = normalizeInboundTextNewlines(
|
||||
typeof normalized.Body === "string" ? normalized.Body : "",
|
||||
);
|
||||
normalized.RawBody = normalizeTextField(normalized.RawBody);
|
||||
normalized.CommandBody = normalizeTextField(normalized.CommandBody);
|
||||
normalized.Transcript = normalizeTextField(normalized.Transcript);
|
||||
normalized.ThreadStarterBody = normalizeTextField(normalized.ThreadStarterBody);
|
||||
|
||||
const chatType = normalizeChatType(normalized.ChatType);
|
||||
if (chatType && (opts.forceChatType || normalized.ChatType !== chatType)) {
|
||||
normalized.ChatType = chatType;
|
||||
}
|
||||
|
||||
const bodyForAgentSource = opts.forceBodyForAgent
|
||||
? normalized.Body
|
||||
: (normalized.BodyForAgent ?? normalized.Body);
|
||||
normalized.BodyForAgent = normalizeInboundTextNewlines(bodyForAgentSource);
|
||||
|
||||
const bodyForCommandsSource = opts.forceBodyForCommands
|
||||
? (normalized.CommandBody ?? normalized.RawBody ?? normalized.Body)
|
||||
: (normalized.BodyForCommands ??
|
||||
normalized.CommandBody ??
|
||||
normalized.RawBody ??
|
||||
normalized.Body);
|
||||
normalized.BodyForCommands = normalizeInboundTextNewlines(bodyForCommandsSource);
|
||||
|
||||
const explicitLabel = normalized.ConversationLabel?.trim();
|
||||
if (opts.forceConversationLabel || !explicitLabel) {
|
||||
const resolved = resolveConversationLabel(normalized)?.trim();
|
||||
if (resolved) normalized.ConversationLabel = resolved;
|
||||
} else {
|
||||
normalized.ConversationLabel = explicitLabel;
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
Reference in New Issue
Block a user