refactor: dedupe acp delivery readers

This commit is contained in:
Peter Steinberger
2026-04-07 05:49:26 +01:00
parent 9c04bdf6de
commit bd99671756
6 changed files with 22 additions and 17 deletions

View File

@@ -6,6 +6,7 @@ import { resolveSessionStorePathForAcp } from "../../../acp/runtime/session-meta
import { loadSessionStore } from "../../../config/sessions.js";
import type { SessionEntry } from "../../../config/sessions/types.js";
import { getSessionBindingService } from "../../../infra/outbound/session-binding-service.js";
import { normalizeOptionalString } from "../../../shared/string-coerce.js";
import type { CommandHandlerResult, HandleCommandsParams } from "../commands-types.js";
import { resolveAcpCommandBindingContext } from "./context.js";
import {
@@ -139,7 +140,7 @@ function formatAcpSessionLine(params: {
return "";
}
const marker = params.currentSessionKey === params.key ? "*" : " ";
const label = params.entry.label?.trim() || acp.agent;
const label = normalizeOptionalString(params.entry.label) || acp.agent;
const threadText = params.threadId ? `, thread:${params.threadId}` : "";
return `${marker} ${label} (${acp.mode}, ${acp.state}, backend:${acp.backend}${threadText}) -> ${params.key}`;
}

View File

@@ -3,13 +3,14 @@ import path from "node:path";
import type { OpenClawConfig } from "../../../config/config.js";
import { resolveBundledPluginWorkspaceSourcePath } from "../../../plugins/bundled-plugin-metadata.js";
import { resolveBundledPluginInstallCommandHint } from "../../../plugins/bundled-sources.js";
import { normalizeOptionalString } from "../../../shared/string-coerce.js";
export function resolveConfiguredAcpBackendId(cfg: OpenClawConfig): string {
return cfg.acp?.backend?.trim() || "acpx";
return normalizeOptionalString(cfg.acp?.backend) || "acpx";
}
export function resolveAcpInstallCommandHint(cfg: OpenClawConfig): string {
const configured = cfg.acp?.runtime?.installCommand?.trim();
const configured = normalizeOptionalString(cfg.acp?.runtime?.installCommand);
if (configured) {
return configured;
}

View File

@@ -1,4 +1,5 @@
import { callGateway } from "../../../gateway/call.js";
import { normalizeOptionalString } from "../../../shared/string-coerce.js";
import { resolveEffectiveResetTargetSessionKey } from "../acp-reset-target.js";
import { resolveRequesterSessionKey } from "../commands-subagents/shared.js";
import type { HandleCommandsParams } from "../commands-types.js";
@@ -57,7 +58,7 @@ export async function resolveAcpTargetSessionKey(params: {
commandParams: HandleCommandsParams;
token?: string;
}): Promise<{ ok: true; sessionKey: string } | { ok: false; error: string }> {
const token = params.token?.trim() || "";
const token = normalizeOptionalString(params.token) ?? "";
if (token) {
const resolved = await resolveSessionKeyByToken(token);
if (!resolved) {

View File

@@ -3,6 +3,7 @@ import type { OpenClawConfig } from "../../config/config.js";
import type { TtsAutoMode } from "../../config/types.tts.js";
import { logVerbose } from "../../globals.js";
import { formatErrorMessage } from "../../infra/errors.js";
import { normalizeOptionalString } from "../../shared/string-coerce.js";
import { resolveStatusTtsSnapshot } from "../../tts/status-config.js";
import { resolveConfiguredTtsMode } from "../../tts/tts-config.js";
import type { FinalizedMsgContext } from "../templating.js";
@@ -53,7 +54,7 @@ type ToolMessageHandle = {
};
function normalizeDeliveryChannel(value: string | undefined): string | undefined {
const normalized = value?.trim().toLowerCase();
const normalized = normalizeOptionalString(value)?.toLowerCase();
return normalized || undefined;
}
@@ -62,7 +63,7 @@ function resolveDeliveryAccountId(params: {
channel: string | undefined;
accountId: string | undefined;
}): string | undefined {
const explicit = params.accountId?.trim();
const explicit = normalizeOptionalString(params.accountId);
if (explicit) {
return explicit;
}
@@ -74,9 +75,7 @@ function resolveDeliveryAccountId(params: {
params.cfg.channels as Record<string, { defaultAccount?: unknown } | undefined> | undefined
)?.[channelId];
const configuredDefault = channelCfg?.defaultAccount;
return typeof configuredDefault === "string" && configuredDefault.trim()
? configuredDefault.trim()
: undefined;
return normalizeOptionalString(configuredDefault);
}
async function shouldTreatDeliveredTextAsVisible(params: {
@@ -85,7 +84,7 @@ async function shouldTreatDeliveredTextAsVisible(params: {
text: string | undefined;
routed: boolean;
}): Promise<boolean> {
if (!params.text?.trim()) {
if (!normalizeOptionalString(params.text)) {
return false;
}
if (params.kind === "final") {
@@ -249,7 +248,7 @@ export function createAcpDispatchDeliveryCoordinator(params: {
if (!handle?.messageId) {
return false;
}
const message = payload.text?.trim();
const message = normalizeOptionalString(payload.text);
if (!message) {
return false;
}
@@ -284,7 +283,7 @@ export function createAcpDispatchDeliveryCoordinator(params: {
payload: ReplyPayload,
meta?: AcpDispatchDeliveryMeta,
): Promise<boolean> => {
if (kind === "block" && payload.text?.trim()) {
if (kind === "block" && normalizeOptionalString(payload.text)) {
if (state.accumulatedBlockText.length > 0) {
state.accumulatedBlockText += "\n";
}
@@ -311,7 +310,7 @@ export function createAcpDispatchDeliveryCoordinator(params: {
});
if (params.shouldRouteToOriginating && params.originatingChannel && params.originatingTo) {
const toolCallId = meta?.toolCallId?.trim();
const toolCallId = normalizeOptionalString(meta?.toolCallId);
if (kind === "tool" && meta?.allowEdit === true && toolCallId) {
const edited = await tryEditToolMessage(ttsPayload, toolCallId);
if (edited) {

View File

@@ -1,8 +1,9 @@
import { getBundledChannelPlugin } from "../../channels/plugins/bundled.js";
import { getLoadedChannelPlugin, normalizeChannelId } from "../../channels/plugins/index.js";
import { normalizeOptionalString } from "../../shared/string-coerce.js";
export function extractExplicitGroupId(raw: string | undefined | null): string | undefined {
const trimmed = (raw ?? "").trim();
const trimmed = normalizeOptionalString(raw) ?? "";
if (!trimmed) {
return undefined;
}
@@ -24,7 +25,8 @@ export function extractExplicitGroupId(raw: string | undefined | null): string |
return joined || undefined;
}
}
const channelId = normalizeChannelId(parts[0] ?? "") ?? parts[0]?.trim().toLowerCase();
const channelId =
normalizeChannelId(parts[0] ?? "") ?? normalizeOptionalString(parts[0])?.toLowerCase();
const messaging = channelId
? (getLoadedChannelPlugin(channelId)?.messaging ??
getBundledChannelPlugin(channelId)?.messaging)

View File

@@ -3,6 +3,7 @@ import type { MessagingToolSend } from "../../agents/pi-embedded-runner.js";
import { getChannelPlugin, normalizeChannelId } from "../../channels/plugins/index.js";
import { normalizeTargetForProvider } from "../../infra/outbound/target-normalization.js";
import { normalizeOptionalAccountId } from "../../routing/account-id.js";
import { normalizeOptionalString } from "../../shared/string-coerce.js";
import type { ReplyPayload } from "../types.js";
export function filterMessagingToolDuplicates(params: {
@@ -61,7 +62,7 @@ export function filterMessagingToolMediaDuplicates(params: {
}
function normalizeProviderForComparison(value?: string): string | undefined {
const trimmed = value?.trim();
const trimmed = normalizeOptionalString(value);
if (!trimmed) {
return undefined;
}
@@ -74,7 +75,7 @@ function normalizeProviderForComparison(value?: string): string | undefined {
}
function normalizeThreadIdForComparison(value?: string): string | undefined {
const trimmed = value?.trim();
const trimmed = normalizeOptionalString(value);
if (!trimmed) {
return undefined;
}