Files
openclaw/src/gateway/server-utils.ts
lsr911 0990bccb32 fix(gateway): use truncateUtf16Safe for voice-wake trigger truncation (#103210)
* fix(gateway): use truncateUtf16Safe for voice-wake trigger truncation

Replace naive .slice(0, 64) with truncateUtf16Safe() in
normalizeVoiceWakeTriggers to prevent surrogate pair splitting
in user-configured voice wake trigger phrases.

Voice wake triggers are user-configurable text strings that may
contain emoji or non-BMP characters. A naive .slice(0, 64) at
a surrogate pair boundary produces a lone surrogate, which
corrupts the trigger text.

Co-Authored-By: Claude <noreply@anthropic.com>

* test: add proof scripts for C1 sanitize and voice-wake UTF-16

* chore: remove unrelated proof script from voice-wake PR

ClawSweeper review: the console-sanitization proof script belongs
to #103226, not this gateway voice-wake fix. Remove it to keep the
branch clean.

Ref: #103210 review

* fix(gateway): harden voice wake unicode boundary

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-07-10 14:41:25 +01:00

44 lines
1.6 KiB
TypeScript

// Gateway generic server utilities.
// Normalizes voice-wake triggers and formats unknown errors for logs/responses.
import { normalizeTrimmedStringList } from "@openclaw/normalization-core/string-normalization";
import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice";
import { defaultVoiceWakeTriggers } from "../infra/voicewake.js";
/** Normalizes voice-wake trigger config with bounded count/length and defaults. */
export function normalizeVoiceWakeTriggers(input: unknown): string[] {
const cleaned = normalizeTrimmedStringList(input)
.slice(0, 32)
.map((value) => truncateUtf16Safe(value, 64));
return cleaned.length > 0 ? cleaned : defaultVoiceWakeTriggers();
}
/** Formats unknown gateway errors without throwing on unusual status/code shapes. */
export function formatError(err: unknown): string {
if (err instanceof Error) {
return err.message;
}
if (typeof err === "string") {
return err;
}
const statusValue = (err as { status?: unknown })?.status;
const codeValue = (err as { code?: unknown })?.code;
const hasStatus = statusValue !== undefined;
const hasCode = codeValue !== undefined;
if (hasStatus || hasCode) {
const statusText =
typeof statusValue === "string" || typeof statusValue === "number"
? String(statusValue)
: "unknown";
const codeText =
typeof codeValue === "string" || typeof codeValue === "number"
? String(codeValue)
: "unknown";
return `status=${statusText} code=${codeText}`;
}
try {
return JSON.stringify(err, null, 2);
} catch {
return String(err);
}
}