|
|
|
|
@ -6,13 +6,10 @@ import {
|
|
|
|
|
} from "../../bindings/records.js";
|
|
|
|
|
import { shouldSuppressLocalExecApprovalPrompt } from "../../channels/plugins/exec-approval-local.js";
|
|
|
|
|
import type { OpenClawConfig } from "../../config/config.js";
|
|
|
|
|
import {
|
|
|
|
|
loadSessionStore,
|
|
|
|
|
parseSessionThreadInfo,
|
|
|
|
|
resolveSessionStoreEntry,
|
|
|
|
|
resolveStorePath,
|
|
|
|
|
type SessionEntry,
|
|
|
|
|
} from "../../config/sessions.js";
|
|
|
|
|
import { parseSessionThreadInfo } from "../../config/sessions/delivery-info.js";
|
|
|
|
|
import { resolveStorePath } from "../../config/sessions/paths.js";
|
|
|
|
|
import { loadSessionStore, resolveSessionStoreEntry } from "../../config/sessions/store.js";
|
|
|
|
|
import type { SessionEntry } from "../../config/sessions/types.js";
|
|
|
|
|
import { logVerbose } from "../../globals.js";
|
|
|
|
|
import { fireAndForgetHook } from "../../hooks/fire-and-forget.js";
|
|
|
|
|
import { createInternalHookEvent, triggerInternalHook } from "../../hooks/internal-hooks.js";
|
|
|
|
|
@ -41,19 +38,47 @@ import {
|
|
|
|
|
} from "../../plugins/conversation-binding.js";
|
|
|
|
|
import { getGlobalHookRunner, getGlobalPluginRegistry } from "../../plugins/hook-runner-global.js";
|
|
|
|
|
import { resolveSendPolicy } from "../../sessions/send-policy.js";
|
|
|
|
|
import { maybeApplyTtsToPayload, normalizeTtsAutoMode, resolveTtsConfig } from "../../tts/tts.js";
|
|
|
|
|
import { normalizeTtsAutoMode, resolveConfiguredTtsMode } from "../../tts/tts-config.js";
|
|
|
|
|
import { INTERNAL_MESSAGE_CHANNEL, normalizeMessageChannel } from "../../utils/message-channel.js";
|
|
|
|
|
import { getReplyFromConfig } from "../reply.js";
|
|
|
|
|
import type { FinalizedMsgContext } from "../templating.js";
|
|
|
|
|
import type { GetReplyOptions, ReplyPayload } from "../types.js";
|
|
|
|
|
import { formatAbortReplyText, tryFastAbortFromMessage } from "./abort.js";
|
|
|
|
|
import { shouldBypassAcpDispatchForCommand, tryDispatchAcpReply } from "./dispatch-acp.js";
|
|
|
|
|
import type { BlockReplyContext, GetReplyOptions, ReplyPayload } from "../types.js";
|
|
|
|
|
import { shouldSkipDuplicateInbound } from "./inbound-dedupe.js";
|
|
|
|
|
import type { ReplyDispatcher, ReplyDispatchKind } from "./reply-dispatcher.js";
|
|
|
|
|
import { shouldSuppressReasoningPayload } from "./reply-payloads.js";
|
|
|
|
|
import { isRoutableChannel, routeReply } from "./route-reply.js";
|
|
|
|
|
import { resolveRunTypingPolicy } from "./typing-policy.js";
|
|
|
|
|
|
|
|
|
|
let routeReplyRuntimePromise: Promise<typeof import("./route-reply.runtime.js")> | null = null;
|
|
|
|
|
let getReplyFromConfigRuntimePromise: Promise<
|
|
|
|
|
typeof import("./get-reply-from-config.runtime.js")
|
|
|
|
|
> | null = null;
|
|
|
|
|
let abortRuntimePromise: Promise<typeof import("./abort.runtime.js")> | null = null;
|
|
|
|
|
let dispatchAcpRuntimePromise: Promise<typeof import("./dispatch-acp.runtime.js")> | null = null;
|
|
|
|
|
let ttsRuntimePromise: Promise<typeof import("../../tts/tts.runtime.js")> | null = null;
|
|
|
|
|
|
|
|
|
|
function loadRouteReplyRuntime() {
|
|
|
|
|
routeReplyRuntimePromise ??= import("./route-reply.runtime.js");
|
|
|
|
|
return routeReplyRuntimePromise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadGetReplyFromConfigRuntime() {
|
|
|
|
|
getReplyFromConfigRuntimePromise ??= import("./get-reply-from-config.runtime.js");
|
|
|
|
|
return getReplyFromConfigRuntimePromise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadAbortRuntime() {
|
|
|
|
|
abortRuntimePromise ??= import("./abort.runtime.js");
|
|
|
|
|
return abortRuntimePromise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadDispatchAcpRuntime() {
|
|
|
|
|
dispatchAcpRuntimePromise ??= import("./dispatch-acp.runtime.js");
|
|
|
|
|
return dispatchAcpRuntimePromise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadTtsRuntime() {
|
|
|
|
|
ttsRuntimePromise ??= import("../../tts/tts.runtime.js");
|
|
|
|
|
return ttsRuntimePromise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const AUDIO_PLACEHOLDER_RE = /^<media:audio>(\s*\([^)]*\))?$/i;
|
|
|
|
|
const AUDIO_HEADER_RE = /^\[Audio\b/i;
|
|
|
|
|
const normalizeMediaType = (value: string): string => value.split(";")[0]?.trim().toLowerCase();
|
|
|
|
|
@ -126,7 +151,7 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
cfg: OpenClawConfig;
|
|
|
|
|
dispatcher: ReplyDispatcher;
|
|
|
|
|
replyOptions?: Omit<GetReplyOptions, "onToolResult" | "onBlockReply">;
|
|
|
|
|
replyResolver?: typeof getReplyFromConfig;
|
|
|
|
|
replyResolver?: typeof import("./get-reply-from-config.runtime.js").getReplyFromConfig;
|
|
|
|
|
}): Promise<DispatchFromConfigResult> {
|
|
|
|
|
const { ctx, cfg, dispatcher } = params;
|
|
|
|
|
const diagnosticsEnabled = isDiagnosticsEnabled(cfg);
|
|
|
|
|
@ -230,9 +255,10 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
currentSurface === INTERNAL_MESSAGE_CHANNEL &&
|
|
|
|
|
(surfaceChannel === INTERNAL_MESSAGE_CHANNEL || !surfaceChannel) &&
|
|
|
|
|
ctx.ExplicitDeliverRoute !== true;
|
|
|
|
|
const routeReplyRuntime = await loadRouteReplyRuntime();
|
|
|
|
|
const shouldRouteToOriginating = Boolean(
|
|
|
|
|
!isInternalWebchatTurn &&
|
|
|
|
|
isRoutableChannel(originatingChannel) &&
|
|
|
|
|
routeReplyRuntime.isRoutableChannel(originatingChannel) &&
|
|
|
|
|
originatingTo &&
|
|
|
|
|
originatingChannel !== currentSurface,
|
|
|
|
|
);
|
|
|
|
|
@ -259,7 +285,7 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
if (abortSignal?.aborted) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const result = await routeReply({
|
|
|
|
|
const result = await routeReplyRuntime.routeReply({
|
|
|
|
|
payload,
|
|
|
|
|
channel: originatingChannel,
|
|
|
|
|
to: originatingTo,
|
|
|
|
|
@ -282,7 +308,7 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
mode: "additive" | "terminal",
|
|
|
|
|
): Promise<boolean> => {
|
|
|
|
|
if (shouldRouteToOriginating && originatingChannel && originatingTo) {
|
|
|
|
|
const result = await routeReply({
|
|
|
|
|
const result = await routeReplyRuntime.routeReply({
|
|
|
|
|
payload,
|
|
|
|
|
channel: originatingChannel,
|
|
|
|
|
to: originatingTo,
|
|
|
|
|
@ -418,15 +444,16 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
markProcessing();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const fastAbort = await tryFastAbortFromMessage({ ctx, cfg });
|
|
|
|
|
const abortRuntime = await loadAbortRuntime();
|
|
|
|
|
const fastAbort = await abortRuntime.tryFastAbortFromMessage({ ctx, cfg });
|
|
|
|
|
if (fastAbort.handled) {
|
|
|
|
|
const payload = {
|
|
|
|
|
text: formatAbortReplyText(fastAbort.stoppedSubagents),
|
|
|
|
|
text: abortRuntime.formatAbortReplyText(fastAbort.stoppedSubagents),
|
|
|
|
|
} satisfies ReplyPayload;
|
|
|
|
|
let queuedFinal = false;
|
|
|
|
|
let routedFinalCount = 0;
|
|
|
|
|
if (shouldRouteToOriginating && originatingChannel && originatingTo) {
|
|
|
|
|
const result = await routeReply({
|
|
|
|
|
const result = await routeReplyRuntime.routeReply({
|
|
|
|
|
payload,
|
|
|
|
|
channel: originatingChannel,
|
|
|
|
|
to: originatingTo,
|
|
|
|
|
@ -456,7 +483,8 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
return { queuedFinal, counts };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bypassAcpForCommand = shouldBypassAcpDispatchForCommand(ctx, cfg);
|
|
|
|
|
const dispatchAcpRuntime = await loadDispatchAcpRuntime();
|
|
|
|
|
const bypassAcpForCommand = dispatchAcpRuntime.shouldBypassAcpDispatchForCommand(ctx, cfg);
|
|
|
|
|
|
|
|
|
|
const sendPolicy = resolveSendPolicy({
|
|
|
|
|
cfg,
|
|
|
|
|
@ -481,7 +509,7 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const shouldSendToolSummaries = ctx.ChatType !== "group" && ctx.CommandSource !== "native";
|
|
|
|
|
const acpDispatch = await tryDispatchAcpReply({
|
|
|
|
|
const acpDispatch = await dispatchAcpRuntime.tryDispatchAcpReply({
|
|
|
|
|
ctx,
|
|
|
|
|
cfg,
|
|
|
|
|
dispatcher,
|
|
|
|
|
@ -508,6 +536,7 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
// TTS audio separately from the accumulated block content.
|
|
|
|
|
let accumulatedBlockText = "";
|
|
|
|
|
let blockCount = 0;
|
|
|
|
|
const { maybeApplyTtsToPayload } = await loadTtsRuntime();
|
|
|
|
|
|
|
|
|
|
const resolveToolDeliveryPayload = (payload: ReplyPayload): ReplyPayload | null => {
|
|
|
|
|
if (
|
|
|
|
|
@ -547,7 +576,9 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
systemEvent: shouldRouteToOriginating,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const replyResult = await (params.replyResolver ?? getReplyFromConfig)(
|
|
|
|
|
const replyResolver =
|
|
|
|
|
params.replyResolver ?? (await loadGetReplyFromConfigRuntime()).getReplyFromConfig;
|
|
|
|
|
const replyResult = await replyResolver(
|
|
|
|
|
ctx,
|
|
|
|
|
{
|
|
|
|
|
...params.replyOptions,
|
|
|
|
|
@ -575,12 +606,12 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
};
|
|
|
|
|
return run();
|
|
|
|
|
},
|
|
|
|
|
onBlockReply: (payload: ReplyPayload, context) => {
|
|
|
|
|
onBlockReply: (payload: ReplyPayload, context?: BlockReplyContext) => {
|
|
|
|
|
const run = async () => {
|
|
|
|
|
// Suppress reasoning payloads — channels using this generic dispatch
|
|
|
|
|
// path (WhatsApp, web, etc.) do not have a dedicated reasoning lane.
|
|
|
|
|
// Telegram has its own dispatch path that handles reasoning splitting.
|
|
|
|
|
if (shouldSuppressReasoningPayload(payload)) {
|
|
|
|
|
if (payload.isReasoning === true) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Accumulate block text for TTS generation after streaming.
|
|
|
|
|
@ -617,7 +648,7 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
// Command handling prepared a trailing prompt after ACP in-place reset.
|
|
|
|
|
// Route that tail through ACP now (same turn) instead of embedded dispatch.
|
|
|
|
|
ctx.AcpDispatchTailAfterReset = false;
|
|
|
|
|
const acpTailDispatch = await tryDispatchAcpReply({
|
|
|
|
|
const acpTailDispatch = await dispatchAcpRuntime.tryDispatchAcpReply({
|
|
|
|
|
ctx,
|
|
|
|
|
cfg,
|
|
|
|
|
dispatcher,
|
|
|
|
|
@ -647,7 +678,7 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
for (const reply of replies) {
|
|
|
|
|
// Suppress reasoning payloads from channel delivery — channels using this
|
|
|
|
|
// generic dispatch path do not have a dedicated reasoning lane.
|
|
|
|
|
if (shouldSuppressReasoningPayload(reply)) {
|
|
|
|
|
if (reply.isReasoning === true) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const ttsReply = await maybeApplyTtsToPayload({
|
|
|
|
|
@ -660,7 +691,7 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
});
|
|
|
|
|
if (shouldRouteToOriginating && originatingChannel && originatingTo) {
|
|
|
|
|
// Route final reply to originating channel.
|
|
|
|
|
const result = await routeReply({
|
|
|
|
|
const result = await routeReplyRuntime.routeReply({
|
|
|
|
|
payload: ttsReply,
|
|
|
|
|
channel: originatingChannel,
|
|
|
|
|
to: originatingTo,
|
|
|
|
|
@ -685,7 +716,7 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ttsMode = resolveTtsConfig(cfg).mode ?? "final";
|
|
|
|
|
const ttsMode = resolveConfiguredTtsMode(cfg);
|
|
|
|
|
// Generate TTS-only reply after block streaming completes (when there's no final reply).
|
|
|
|
|
// This handles the case where block streaming succeeds and drops final payloads,
|
|
|
|
|
// but we still want TTS audio to be generated from the accumulated block content.
|
|
|
|
|
@ -712,7 +743,7 @@ export async function dispatchReplyFromConfig(params: {
|
|
|
|
|
audioAsVoice: ttsSyntheticReply.audioAsVoice,
|
|
|
|
|
};
|
|
|
|
|
if (shouldRouteToOriginating && originatingChannel && originatingTo) {
|
|
|
|
|
const result = await routeReply({
|
|
|
|
|
const result = await routeReplyRuntime.routeReply({
|
|
|
|
|
payload: ttsOnlyPayload,
|
|
|
|
|
channel: originatingChannel,
|
|
|
|
|
to: originatingTo,
|
|
|
|
|
|