refactor: dedupe message action helpers
This commit is contained in:
@@ -8,9 +8,15 @@ const mocks = vi.hoisted(() => ({
|
|||||||
appendAssistantMessageToSessionTranscript: vi.fn(async () => ({ ok: true, sessionFile: "x" })),
|
appendAssistantMessageToSessionTranscript: vi.fn(async () => ({ ok: true, sessionFile: "x" })),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("../../infra/outbound/message-action-runner.js", () => ({
|
vi.mock("../../infra/outbound/message-action-runner.js", async () => {
|
||||||
runMessageAction: mocks.runMessageAction,
|
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 () => {
|
vi.mock("../../config/sessions.js", async () => {
|
||||||
const actual = await vi.importActual<typeof import("../../config/sessions.js")>(
|
const actual = await vi.importActual<typeof import("../../config/sessions.js")>(
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import {
|
|||||||
resolveMirroredTranscriptText,
|
resolveMirroredTranscriptText,
|
||||||
} from "../../config/sessions.js";
|
} from "../../config/sessions.js";
|
||||||
import { GATEWAY_CLIENT_IDS, GATEWAY_CLIENT_MODES } from "../../gateway/protocol/client-info.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 { resolveSessionAgentId } from "../agent-scope.js";
|
||||||
import { normalizeAccountId } from "../../routing/session-key.js";
|
import { normalizeAccountId } from "../../routing/session-key.js";
|
||||||
import { channelTargetSchema, channelTargetsSchema, stringEnum } from "../schema/typebox.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);
|
return jsonResult(result.payload);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import { sendMessage, sendPoll } from "./message.js";
|
|||||||
import {
|
import {
|
||||||
applyCrossContextDecoration,
|
applyCrossContextDecoration,
|
||||||
buildCrossContextDecoration,
|
buildCrossContextDecoration,
|
||||||
|
type CrossContextDecoration,
|
||||||
enforceCrossContextPolicy,
|
enforceCrossContextPolicy,
|
||||||
shouldApplyCrossContextMarker,
|
shouldApplyCrossContextMarker,
|
||||||
} from "./outbound-policy.js";
|
} from "./outbound-policy.js";
|
||||||
@@ -99,6 +100,12 @@ export type MessageActionRunResult =
|
|||||||
dryRun: boolean;
|
dryRun: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function getToolResult(
|
||||||
|
result: MessageActionRunResult,
|
||||||
|
): AgentToolResult<unknown> | undefined {
|
||||||
|
return "toolResult" in result ? result.toolResult : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
function extractToolPayload(result: AgentToolResult<unknown>): unknown {
|
function extractToolPayload(result: AgentToolResult<unknown>): unknown {
|
||||||
if (result.details !== undefined) return result.details;
|
if (result.details !== undefined) return result.details;
|
||||||
const textBlock = Array.isArray(result.content)
|
const textBlock = Array.isArray(result.content)
|
||||||
@@ -121,6 +128,28 @@ function extractToolPayload(result: AgentToolResult<unknown>): unknown {
|
|||||||
return result.content ?? result;
|
return result.content ?? result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function applyCrossContextMessageDecoration({
|
||||||
|
params,
|
||||||
|
message,
|
||||||
|
decoration,
|
||||||
|
preferEmbeds,
|
||||||
|
}: {
|
||||||
|
params: Record<string, unknown>;
|
||||||
|
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<string, unknown>, key: string): boolean | undefined {
|
function readBooleanParam(params: Record<string, unknown>, key: string): boolean | undefined {
|
||||||
const raw = params[key];
|
const raw = params[key];
|
||||||
if (typeof raw === "boolean") return raw;
|
if (typeof raw === "boolean") return raw;
|
||||||
@@ -335,16 +364,12 @@ export async function runMessageAction(
|
|||||||
})
|
})
|
||||||
: null;
|
: null;
|
||||||
if (decoration) {
|
if (decoration) {
|
||||||
const applied = applyCrossContextDecoration({
|
message = applyCrossContextMessageDecoration({
|
||||||
|
params,
|
||||||
message,
|
message,
|
||||||
decoration,
|
decoration,
|
||||||
preferEmbeds: true,
|
preferEmbeds: true,
|
||||||
});
|
});
|
||||||
message = applied.message;
|
|
||||||
params.message = applied.message;
|
|
||||||
if (applied.embeds?.length) {
|
|
||||||
params.embeds = applied.embeds;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const mediaUrl = readStringParam(params, "media", { trim: false });
|
const mediaUrl = readStringParam(params, "media", { trim: false });
|
||||||
@@ -434,15 +459,12 @@ export async function runMessageAction(
|
|||||||
: null;
|
: null;
|
||||||
if (decoration) {
|
if (decoration) {
|
||||||
const base = typeof params.message === "string" ? params.message : "";
|
const base = typeof params.message === "string" ? params.message : "";
|
||||||
const applied = applyCrossContextDecoration({
|
applyCrossContextMessageDecoration({
|
||||||
|
params,
|
||||||
message: base,
|
message: base,
|
||||||
decoration,
|
decoration,
|
||||||
preferEmbeds: true,
|
preferEmbeds: true,
|
||||||
});
|
});
|
||||||
params.message = applied.message;
|
|
||||||
if (applied.embeds?.length) {
|
|
||||||
params.embeds = applied.embeds;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dryRun) {
|
if (!dryRun) {
|
||||||
|
|||||||
Reference in New Issue
Block a user