From 7f1f9473a079e6f20ca43a6116a04faee1ddcb82 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 17 Jan 2026 03:45:59 +0000 Subject: [PATCH] refactor: dedupe message action helpers --- src/agents/tools/message-tool.test.ts | 12 ++++-- src/agents/tools/message-tool.ts | 5 ++- src/infra/outbound/message-action-runner.ts | 44 +++++++++++++++------ 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/agents/tools/message-tool.test.ts b/src/agents/tools/message-tool.test.ts index 92a343818..9174d22ba 100644 --- a/src/agents/tools/message-tool.test.ts +++ b/src/agents/tools/message-tool.test.ts @@ -8,9 +8,15 @@ const mocks = vi.hoisted(() => ({ appendAssistantMessageToSessionTranscript: vi.fn(async () => ({ ok: true, sessionFile: "x" })), })); -vi.mock("../../infra/outbound/message-action-runner.js", () => ({ - runMessageAction: mocks.runMessageAction, -})); +vi.mock("../../infra/outbound/message-action-runner.js", async () => { + const actual = await vi.importActual< + typeof import("../../infra/outbound/message-action-runner.js") + >("../../infra/outbound/message-action-runner.js"); + return { + ...actual, + runMessageAction: mocks.runMessageAction, + }; +}); vi.mock("../../config/sessions.js", async () => { const actual = await vi.importActual( diff --git a/src/agents/tools/message-tool.ts b/src/agents/tools/message-tool.ts index ac9020284..57d6badc0 100644 --- a/src/agents/tools/message-tool.ts +++ b/src/agents/tools/message-tool.ts @@ -14,7 +14,7 @@ import { resolveMirroredTranscriptText, } from "../../config/sessions.js"; import { GATEWAY_CLIENT_IDS, GATEWAY_CLIENT_MODES } from "../../gateway/protocol/client-info.js"; -import { runMessageAction } from "../../infra/outbound/message-action-runner.js"; +import { getToolResult, runMessageAction } from "../../infra/outbound/message-action-runner.js"; import { resolveSessionAgentId } from "../agent-scope.js"; import { normalizeAccountId } from "../../routing/session-key.js"; import { channelTargetSchema, channelTargetsSchema, stringEnum } from "../schema/typebox.js"; @@ -227,7 +227,8 @@ export function createMessageTool(options?: MessageToolOptions): AnyAgentTool { } } - if ("toolResult" in result && result.toolResult) return result.toolResult; + const toolResult = getToolResult(result); + if (toolResult) return toolResult; return jsonResult(result.payload); }, }; diff --git a/src/infra/outbound/message-action-runner.ts b/src/infra/outbound/message-action-runner.ts index c49ac3790..c6aef33d7 100644 --- a/src/infra/outbound/message-action-runner.ts +++ b/src/infra/outbound/message-action-runner.ts @@ -23,6 +23,7 @@ import { sendMessage, sendPoll } from "./message.js"; import { applyCrossContextDecoration, buildCrossContextDecoration, + type CrossContextDecoration, enforceCrossContextPolicy, shouldApplyCrossContextMarker, } from "./outbound-policy.js"; @@ -99,6 +100,12 @@ export type MessageActionRunResult = dryRun: boolean; }; +export function getToolResult( + result: MessageActionRunResult, +): AgentToolResult | undefined { + return "toolResult" in result ? result.toolResult : undefined; +} + function extractToolPayload(result: AgentToolResult): unknown { if (result.details !== undefined) return result.details; const textBlock = Array.isArray(result.content) @@ -121,6 +128,28 @@ function extractToolPayload(result: AgentToolResult): unknown { return result.content ?? result; } +function applyCrossContextMessageDecoration({ + params, + message, + decoration, + preferEmbeds, +}: { + params: Record; + message: string; + decoration: CrossContextDecoration; + preferEmbeds: boolean; +}): string { + const applied = applyCrossContextDecoration({ + message, + decoration, + preferEmbeds, + }); + params.message = applied.message; + if (applied.embeds?.length) { + params.embeds = applied.embeds; + } + return applied.message; +} function readBooleanParam(params: Record, key: string): boolean | undefined { const raw = params[key]; if (typeof raw === "boolean") return raw; @@ -335,16 +364,12 @@ export async function runMessageAction( }) : null; if (decoration) { - const applied = applyCrossContextDecoration({ + message = applyCrossContextMessageDecoration({ + params, message, decoration, preferEmbeds: true, }); - message = applied.message; - params.message = applied.message; - if (applied.embeds?.length) { - params.embeds = applied.embeds; - } } const mediaUrl = readStringParam(params, "media", { trim: false }); @@ -434,15 +459,12 @@ export async function runMessageAction( : null; if (decoration) { const base = typeof params.message === "string" ? params.message : ""; - const applied = applyCrossContextDecoration({ + applyCrossContextMessageDecoration({ + params, message: base, decoration, preferEmbeds: true, }); - params.message = applied.message; - if (applied.embeds?.length) { - params.embeds = applied.embeds; - } } if (!dryRun) {