Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(dashboard): stop shortened labels from splitting emoji (#1060)

Rudy Mizrahi Celekli0cbc3a271496
diff --git a/cli/beacon/internal/endpoint/dashboard/static/app.js b/cli/beacon/internal/endpoint/dashboard/static/app.js
index d4eeaec7..d301cc86 100644
--- a/cli/beacon/internal/endpoint/dashboard/static/app.js
+++ b/cli/beacon/internal/endpoint/dashboard/static/app.js
@@ -2316,11 +2316,31 @@ function clearFields(loadAfter = true) {
if (loadAfter) load({ updateLocation: true }).catch(console.error);
}
+const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" });
+
+// truncateMiddle shortens value to maxLength visible characters, keeping both ends. It cuts between
+// graphemes, so an emoji, flag or accented letter at the cut stays whole. It reads only the
+// characters it keeps, so a long prompt costs no more than a short one.
function truncateMiddle(value, maxLength) {
value = String(value || "");
+ // Every grapheme is at least one UTF-16 unit, so a string this short has nothing to cut.
if (value.length <= maxLength) return value;
- const keep = Math.floor((maxLength - 3) / 2);
- return `${value.slice(0, keep)}...${value.slice(value.length - keep)}`;
+ const segments = graphemes.segment(value);
+ const head = [];
+ for (const { segment } of segments) {
+ if (head.length === maxLength) {
+ const keep = Math.floor((maxLength - 3) / 2);
+ const tail = [];
+ for (let end = value.length; tail.length < keep; ) {
+ const last = segments.containing(end - 1);
+ tail.push(last.segment);
+ end = last.index;
+ }
+ return `${head.slice(0, keep).join("")}...${tail.reverse().join("")}`;
+ }
+ head.push(segment);
+ }
+ return value;
}
function escapeHTML(value) {
diff --git a/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/truncate-unicode.spec.ts b/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/truncate-unicode.spec.ts
new file mode 100644
index 00000000..32015fe9
--- /dev/null
+++ b/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/truncate-unicode.spec.ts
@@ -0,0 +1,23 @@
+import { expect, test } from '@playwright/test';
+
+// 180 characters fit, and the cut keeps 88 from each end. The characters that sit across each cut
+// are built from several code points (a family joined by zero-width joiners, a flag of two
+// regional indicators), so a cut by code unit or by code point would split them.
+for (const [name, first, last] of [
+ ['an emoji', 'πŸ˜€', 'πŸ˜€'],
+ ['a family emoji or a flag', 'πŸ‘¨β€πŸ‘©β€πŸ‘§', 'πŸ‡―πŸ‡΅'],
+]) {
+ test(`a long prompt is shortened without cutting ${name} in half`, async ({ page }) => {
+ const prompt = `${'a'.repeat(87)}${first}${'b'.repeat(40)}${last}${'c'.repeat(87)}`;
+ await page.route('**/api/status', (route) => route.fulfill({ json: {} }));
+ await page.route('**/api/summary?*', (route) => route.fulfill({ json: {} }));
+ await page.route('**/api/sessions?*', (route) =>
+ route.fulfill({ json: { sessions: [{ id: 'session-emoji', event_count: 1, original_prompt: prompt }] } }),
+ );
+ await page.goto('/');
+ const cell = page.locator('#events tr[data-session-id="session-emoji"] .col-message').first();
+ await expect(cell).toContainText('...');
+ const text = await cell.evaluate((node) => node.textContent || '');
+ expect(text).toContain(`${'a'.repeat(87)}${first}...${last}${'c'.repeat(87)}`);
+ });
+}