Skip to content

Commit 669b352

Browse files
committed
refactor: dedupe auto-reply trimmed readers
1 parent c7ecc1e commit 669b352

14 files changed

Lines changed: 68 additions & 48 deletions

‎src/auto-reply/command-auth.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { OpenClawConfig } from "../config/config.js";
55
import {
66
normalizeLowercaseStringOrEmpty,
77
normalizeOptionalLowercaseString,
8+
normalizeOptionalString,
89
} from "../shared/string-coerce.js";
910
import { normalizeStringEntries } from "../shared/string-normalization.js";
1011
import {
@@ -256,7 +257,7 @@ function buildProviderAllowFromResolution(params: {
256257

257258
function describeAllowFromResolutionError(err: unknown): string {
258259
if (err instanceof Error) {
259-
const name = err.name.trim();
260+
const name = normalizeOptionalString(err.name) ?? "";
260261
return name || "Error";
261262
}
262263
return "unknown_error";
@@ -275,7 +276,7 @@ function resolveOwnerAllowFromList(params: {
275276
}
276277
const filtered: string[] = [];
277278
for (const entry of raw) {
278-
const trimmed = String(entry ?? "").trim();
279+
const trimmed = normalizeOptionalString(String(entry ?? "")) ?? "";
279280
if (!trimmed) {
280281
continue;
281282
}
@@ -466,7 +467,7 @@ function shouldUseFromAsSenderFallback(params: {
466467
from?: string | null;
467468
chatType?: string | null;
468469
}): boolean {
469-
const from = (params.from ?? "").trim();
470+
const from = normalizeOptionalString(params.from) ?? "";
470471
if (!from) {
471472
return false;
472473
}
@@ -490,7 +491,7 @@ function resolveSenderCandidates(params: {
490491
const { plugin, cfg, accountId } = params;
491492
const candidates: string[] = [];
492493
const pushCandidate = (value?: string | null) => {
493-
const trimmed = (value ?? "").trim();
494+
const trimmed = normalizeOptionalString(value) ?? "";
494495
if (!trimmed) {
495496
return;
496497
}
@@ -527,7 +528,7 @@ function resolveFallbackAllowFrom(params: {
527528
providerId?: ChannelId;
528529
accountId?: string | null;
529530
}): Array<string | number> {
530-
const providerId = params.providerId?.trim();
531+
const providerId = normalizeOptionalString(params.providerId);
531532
if (!providerId) {
532533
return [];
533534
}
@@ -629,8 +630,8 @@ export function resolveCommandAuthorization(params: {
629630
cfg,
630631
);
631632
const plugin = providerId ? getChannelPlugin(providerId) : undefined;
632-
const from = (ctx.From ?? "").trim();
633-
const to = (ctx.To ?? "").trim();
633+
const from = normalizeOptionalString(ctx.From) ?? "";
634+
const to = normalizeOptionalString(ctx.To) ?? "";
634635
const commandsAllowFromConfigured = Boolean(
635636
cfg.commands?.allowFrom && typeof cfg.commands.allowFrom === "object",
636637
);

‎src/auto-reply/commands-args.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { normalizeOptionalLowercaseString } from "../shared/string-coerce.js";
1+
import {
2+
normalizeOptionalLowercaseString,
3+
normalizeOptionalString,
4+
} from "../shared/string-coerce.js";
25
import type { CommandArgValues } from "./commands-registry.types.js";
36

47
export type CommandArgsFormatter = (values: CommandArgValues) => string | undefined;
@@ -9,13 +12,13 @@ function normalizeArgValue(value: unknown): string | undefined {
912
}
1013
let text: string;
1114
if (typeof value === "string") {
12-
text = value.trim();
15+
text = normalizeOptionalString(value) ?? "";
1316
} else if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
14-
text = String(value).trim();
17+
text = normalizeOptionalString(String(value)) ?? "";
1518
} else if (typeof value === "symbol") {
16-
text = value.toString().trim();
19+
text = normalizeOptionalString(value.toString()) ?? "";
1720
} else if (typeof value === "function") {
18-
text = value.toString().trim();
21+
text = normalizeOptionalString(value.toString()) ?? "";
1922
} else {
2023
// Objects and arrays
2124
text = JSON.stringify(value);

‎src/auto-reply/heartbeat.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parseDurationMs } from "../cli/parse-duration.js";
2+
import { normalizeOptionalString } from "../shared/string-coerce.js";
23
import { escapeRegExp } from "../utils.js";
34
import { HEARTBEAT_TOKEN } from "./tokens.js";
45

@@ -60,7 +61,7 @@ export function isHeartbeatContentEffectivelyEmpty(content: string | undefined |
6061
}
6162

6263
export function resolveHeartbeatPrompt(raw?: string): string {
63-
const trimmed = typeof raw === "string" ? raw.trim() : "";
64+
const trimmed = normalizeOptionalString(raw) ?? "";
6465
return trimmed || HEARTBEAT_PROMPT;
6566
}
6667

‎src/auto-reply/model-runtime.ts‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import type { SessionEntry } from "../config/sessions.js";
2-
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
2+
import {
3+
normalizeLowercaseStringOrEmpty,
4+
normalizeOptionalString,
5+
} from "../shared/string-coerce.js";
36

47
export function formatProviderModelRef(providerRaw: string, modelRaw: string): string {
5-
const provider = String(providerRaw ?? "").trim();
6-
const model = String(modelRaw ?? "").trim();
8+
const provider = normalizeOptionalString(providerRaw) ?? "";
9+
const model = normalizeOptionalString(modelRaw) ?? "";
710
if (!provider) {
811
return model;
912
}
@@ -27,7 +30,7 @@ type ModelRef = {
2730
};
2831

2932
function normalizeModelWithinProvider(provider: string, modelRaw: string): string {
30-
const model = String(modelRaw ?? "").trim();
33+
const model = normalizeOptionalString(modelRaw) ?? "";
3134
if (!provider || !model) {
3235
return model;
3336
}
@@ -46,11 +49,11 @@ function normalizeModelRef(
4649
fallbackProvider: string,
4750
parseEmbeddedProvider = false,
4851
): ModelRef {
49-
const trimmed = String(rawModel ?? "").trim();
52+
const trimmed = normalizeOptionalString(rawModel) ?? "";
5053
const slashIndex = parseEmbeddedProvider ? trimmed.indexOf("/") : -1;
5154
if (slashIndex > 0) {
52-
const provider = trimmed.slice(0, slashIndex).trim();
53-
const model = trimmed.slice(slashIndex + 1).trim();
55+
const provider = normalizeOptionalString(trimmed.slice(0, slashIndex)) ?? "";
56+
const model = normalizeOptionalString(trimmed.slice(slashIndex + 1)) ?? "";
5457
if (provider && model) {
5558
return {
5659
provider,
@@ -59,7 +62,7 @@ function normalizeModelRef(
5962
};
6063
}
6164
}
62-
const provider = String(fallbackProvider ?? "").trim();
65+
const provider = normalizeOptionalString(fallbackProvider) ?? "";
6366
const dedupedModel = normalizeModelWithinProvider(provider, trimmed);
6467
return {
6568
provider,
@@ -78,8 +81,8 @@ export function resolveSelectedAndActiveModel(params: {
7881
activeDiffers: boolean;
7982
} {
8083
const selected = normalizeModelRef(params.selectedModel, params.selectedProvider);
81-
const runtimeModel = params.sessionEntry?.model?.trim();
82-
const runtimeProvider = params.sessionEntry?.modelProvider?.trim();
84+
const runtimeModel = normalizeOptionalString(params.sessionEntry?.model);
85+
const runtimeProvider = normalizeOptionalString(params.sessionEntry?.modelProvider);
8386

8487
const active = runtimeModel
8588
? normalizeModelRef(runtimeModel, runtimeProvider || selected.provider, !runtimeProvider)

‎src/auto-reply/reply/commands-acp/targets.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function resolveSessionKeyByToken(token: string): Promise<string | null> {
2424
params,
2525
timeoutMs: 8_000,
2626
});
27-
const key = typeof resolved?.key === "string" ? resolved.key.trim() : "";
27+
const key = normalizeOptionalString(resolved?.key) ?? "";
2828
if (key) {
2929
return key;
3030
}
@@ -36,11 +36,9 @@ async function resolveSessionKeyByToken(token: string): Promise<string | null> {
3636
}
3737

3838
export function resolveBoundAcpThreadSessionKey(params: HandleCommandsParams): string | undefined {
39-
const commandTargetSessionKey =
40-
typeof params.ctx.CommandTargetSessionKey === "string"
41-
? params.ctx.CommandTargetSessionKey.trim()
42-
: "";
43-
const activeSessionKey = commandTargetSessionKey || params.sessionKey.trim();
39+
const commandTargetSessionKey = normalizeOptionalString(params.ctx.CommandTargetSessionKey) ?? "";
40+
const activeSessionKey =
41+
commandTargetSessionKey || (normalizeOptionalString(params.sessionKey) ?? "");
4442
const bindingContext = resolveAcpCommandBindingContext(params);
4543
return resolveEffectiveResetTargetSessionKey({
4644
cfg: params.cfg,

‎src/auto-reply/reply/commands-session.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function resolveSessionBindingLastActivityAt(binding: SessionBindingRecord): num
8181

8282
function resolveSessionBindingBoundBy(binding: SessionBindingRecord): string {
8383
const raw = binding.metadata?.boundBy;
84-
return typeof raw === "string" ? raw.trim() : "";
84+
return normalizeOptionalString(raw) ?? "";
8585
}
8686

8787
type UpdatedLifecycleBinding = {

‎src/auto-reply/reply/commands-subagents/action-spawn.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { spawnSubagentDirect } from "../../../agents/subagent-spawn.js";
2+
import { normalizeOptionalString } from "../../../shared/string-coerce.js";
23
import type { CommandHandlerResult } from "../commands-types.js";
34
import { type SubagentsCommandContext, stopWithText } from "./shared.js";
45

@@ -29,10 +30,9 @@ export async function handleSubagentsSpawnAction(
2930
);
3031
}
3132

32-
const commandTo = typeof params.command.to === "string" ? params.command.to.trim() : "";
33-
const originatingTo =
34-
typeof params.ctx.OriginatingTo === "string" ? params.ctx.OriginatingTo.trim() : "";
35-
const fallbackTo = typeof params.ctx.To === "string" ? params.ctx.To.trim() : "";
33+
const commandTo = normalizeOptionalString(params.command.to) ?? "";
34+
const originatingTo = normalizeOptionalString(params.ctx.OriginatingTo) ?? "";
35+
const fallbackTo = normalizeOptionalString(params.ctx.To) ?? "";
3636
const normalizedTo = originatingTo || commandTo || fallbackTo || undefined;
3737

3838
const result = await spawnSubagentDirect(

‎src/auto-reply/reply/commands-subagents/shared.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ export async function resolveFocusTargetSession(params: {
338338
method: "sessions.resolve",
339339
params: attempt,
340340
});
341-
const key = typeof resolved?.key === "string" ? resolved.key.trim() : "";
341+
const key = normalizeOptionalString(resolved?.key) ?? "";
342342
if (!key) {
343343
continue;
344344
}

‎src/auto-reply/reply/commands-tts.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { logVerbose } from "../../globals.js";
2-
import { normalizeOptionalLowercaseString } from "../../shared/string-coerce.js";
2+
import {
3+
normalizeOptionalLowercaseString,
4+
normalizeOptionalString,
5+
} from "../../shared/string-coerce.js";
36
import {
47
canonicalizeSpeechProviderId,
58
getSpeechProvider,
@@ -47,7 +50,10 @@ function parseTtsCommand(normalized: string): ParsedTtsCommand | null {
4750
return { action: "status", args: "" };
4851
}
4952
const [action, ...tail] = rest.split(/\s+/);
50-
return { action: normalizeOptionalLowercaseString(action) ?? "", args: tail.join(" ").trim() };
53+
return {
54+
action: normalizeOptionalLowercaseString(action) ?? "",
55+
args: normalizeOptionalString(tail.join(" ")) ?? "",
56+
};
5157
}
5258

5359
function formatAttemptDetails(attempts: TtsAttemptDetail[] | undefined): string | undefined {

‎src/auto-reply/reply/directive-handling.model.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function pushUniqueCatalogEntry(params: {
3838
fallbackNameToId: boolean;
3939
}) {
4040
const provider = normalizeProviderId(params.provider);
41-
const id = String(params.id ?? "").trim();
41+
const id = normalizeOptionalString(params.id) ?? "";
4242
if (!provider || !id) {
4343
return;
4444
}
@@ -83,7 +83,7 @@ function buildModelPickerCatalog(params: {
8383
};
8484

8585
const pushRaw = (raw?: string) => {
86-
const value = String(raw ?? "").trim();
86+
const value = normalizeOptionalString(raw) ?? "";
8787
if (!value) {
8888
return;
8989
}

0 commit comments

Comments
 (0)