Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-6758 feat(core): add truncateWithEllipsis string helper (#7646)

Danila Katalshov11861145abc5
diff --git a/apps/desktop/src/renderer/src/components/daemon-panel.tsx b/apps/desktop/src/renderer/src/components/daemon-panel.tsx
index 3e041834d1..4edf278683 100644
--- a/apps/desktop/src/renderer/src/components/daemon-panel.tsx
+++ b/apps/desktop/src/renderer/src/components/daemon-panel.tsx
@@ -15,6 +15,7 @@ import {
Trash2,
X,
} from "lucide-react";
+import { truncateWithEllipsis } from "@multica/core/utils";
import { cn } from "@multica/ui/lib/utils";
import { copyText } from "@multica/ui/lib/clipboard";
import { Button } from "@multica/ui/components/ui/button";
@@ -652,7 +653,9 @@ function EmptyState({
// ---------- Helpers ----------
function truncateValue(value: string, max = 32): string {
- return value.length > max ? `${value.slice(0, max)}…` : value;
+ // `max` is a content-character budget; the shared helper counts the ellipsis
+ // toward its own budget, so pass `max + 1`.
+ return truncateWithEllipsis(value, max + 1);
}
function highlight(text: string, query: string): ReactNode {
diff --git a/packages/core/utils.test.ts b/packages/core/utils.test.ts
index e4ad123742..4d538785eb 100644
--- a/packages/core/utils.test.ts
+++ b/packages/core/utils.test.ts
@@ -1,5 +1,11 @@
import { afterEach, describe, expect, it, vi } from "vitest";
-import { createRequestId, createSafeId, generateUUID, isImeComposing } from "./utils";
+import {
+ createRequestId,
+ createSafeId,
+ generateUUID,
+ isImeComposing,
+ truncateWithEllipsis,
+} from "./utils";
afterEach(() => {
vi.unstubAllGlobals();
@@ -53,3 +59,50 @@ describe("isImeComposing", () => {
expect(isImeComposing({ isComposing: false, keyCode: 13 })).toBe(false);
});
});
+
+describe("truncateWithEllipsis", () => {
+ it("returns text shorter than maxLength unchanged", () => {
+ expect(truncateWithEllipsis("hello", 10)).toBe("hello");
+ });
+
+ it("returns text exactly equal to maxLength unchanged", () => {
+ expect(truncateWithEllipsis("hello", 5)).toBe("hello");
+ });
+
+ it("truncates longer text and appends an ellipsis within maxLength", () => {
+ const result = truncateWithEllipsis("hello world", 8);
+ expect(result).toBe("hello w…");
+ expect(Array.from(result)).toHaveLength(8);
+ expect(result.endsWith("…")).toBe(true);
+ });
+
+ it("trims trailing whitespace before the ellipsis", () => {
+ // The cut lands right after the space; it must not leave "hello …".
+ expect(truncateWithEllipsis("hello world", 7)).toBe("hello…");
+ });
+
+ it("never splits an astral character into a lone surrogate", () => {
+ const result = truncateWithEllipsis("🎉🎉🎉🎉🎉 party", 8);
+ expect(result).toBe("🎉🎉🎉🎉🎉 p…");
+ expect(Array.from(result)).toHaveLength(8);
+ // No unpaired high surrogate left dangling.
+ expect(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/.test(result)).toBe(false);
+ });
+
+ it("measures the limit in code points, not UTF-16 code units", () => {
+ // Five emoji are 10 code units but 5 visible characters, so they fit
+ // within maxLength 8 and must not be truncated.
+ expect(truncateWithEllipsis("🎉🎉🎉🎉🎉", 8)).toBe("🎉🎉🎉🎉🎉");
+ });
+
+ it("truncates CJK content by code point", () => {
+ expect(truncateWithEllipsis("你好世界你好世界", 5)).toBe("你好世界…");
+ });
+
+ it("handles degenerate maxLength values without throwing", () => {
+ expect(truncateWithEllipsis("hello", 1)).toBe("…");
+ expect(truncateWithEllipsis("hello", 0)).toBe("");
+ expect(truncateWithEllipsis("hello", -5)).toBe("");
+ expect(truncateWithEllipsis("hello", Number.NaN)).toBe("…");
+ });
+});
diff --git a/packages/core/utils.ts b/packages/core/utils.ts
index d21f435dda..d287f7f97b 100644
--- a/packages/core/utils.ts
+++ b/packages/core/utils.ts
@@ -62,3 +62,33 @@ export function isImeComposing(event: {
const e = event.nativeEvent ?? event;
return Boolean(e.isComposing) || e.keyCode === 229;
}
+
+/**
+ * Truncate `text` to at most `maxLength` code points, appending an ellipsis
+ * (`…`, U+2026) when it is longer. Text at or under the limit is returned
+ * unchanged.
+ *
+ * The ellipsis counts toward the limit, so a truncated result is at most
+ * `maxLength` code points: the visible text is sliced to `maxLength - 1` and
+ * has trailing whitespace trimmed before the ellipsis is appended. The bound
+ * is measured in Unicode code points, not UTF-16 code units, so an emoji is
+ * never cut into a lone surrogate — a returned string may therefore exceed
+ * `maxLength` in `.length` (code units) while staying within `maxLength`
+ * visible characters. When `maxLength` leaves no room for visible text, only
+ * the ellipsis is returned; `maxLength <= 0` returns the empty string.
+ */
+export function truncateWithEllipsis(text: string, maxLength: number): string {
+ // Fast path on code units: may fail to return early on astral text, but
+ // never truncates wrongly — the code-point check below is authoritative.
+ if (text.length <= maxLength) {
+ return text;
+ }
+ if (maxLength <= 1) {
+ return maxLength <= 0 ? "" : "…";
+ }
+ const chars = Array.from(text);
+ if (chars.length <= maxLength) {
+ return text;
+ }
+ return `${chars.slice(0, maxLength - 1).join("").trimEnd()}…`;
+}
diff --git a/packages/views/common/task-transcript/trace-event-presenter.test.ts b/packages/views/common/task-transcript/trace-event-presenter.test.ts
index 02daab9cca..57e91eb69c 100644
--- a/packages/views/common/task-transcript/trace-event-presenter.test.ts
+++ b/packages/views/common/task-transcript/trace-event-presenter.test.ts
@@ -16,6 +16,16 @@ import {
unwrapToolOutput,
} from "./trace-event-presenter";
+describe("traceEventSummary truncation", () => {
+ it("truncates a long summary with the single-character ellipsis", () => {
+ const summary = traceEventSummary({ type: "thinking", content: "x".repeat(300) });
+ expect(summary.endsWith("…")).toBe(true);
+ expect(summary.endsWith("...")).toBe(false);
+ // 200 content code points plus the ellipsis.
+ expect(Array.from(summary)).toHaveLength(201);
+ });
+});
+
describe("traceEventKind / traceEventLabel", () => {
it("maps the five persisted types and keeps unknown types as generic", () => {
expect(traceEventKind({ type: "text" })).toBe("agent");
diff --git a/packages/views/common/task-transcript/trace-event-presenter.ts b/packages/views/common/task-transcript/trace-event-presenter.ts
index 1eb646cfe4..35194c7361 100644
--- a/packages/views/common/task-transcript/trace-event-presenter.ts
+++ b/packages/views/common/task-transcript/trace-event-presenter.ts
@@ -12,6 +12,8 @@
// This module owns no React and no fetching, so it is unit-testable in
// isolation and independent of whichever list shell renders the events.
+import { truncateWithEllipsis } from "@multica/core/utils";
+
export interface TraceEvent {
seq?: number;
type: string;
@@ -89,7 +91,10 @@ export function stripShellWrapper(command: string): string {
}
function clip(value: string, max: number): string {
- return value.length > max ? value.slice(0, max) + "..." : value;
+ // Delegate to the shared helper so truncation is code-point-safe and uses the
+ // standard single-character ellipsis. `max` here is a content-character budget,
+ // so pass `max + 1` — the helper counts the ellipsis toward its own budget.
+ return truncateWithEllipsis(value, max + 1);
}
/**