This is an automated email from the ASF dual-hosted git repository.
lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git
The following commit(s) were added to refs/heads/master by this push:
new 8fcd6d216 fix(ai): clamp prompt template text on character boundaries
(#6823)
8fcd6d216 is described below
commit 8fcd6d216699fa16eaf164adc40fe68e3970ea90
Author: Apulupie <[email protected]>
AuthorDate: Fri Nov 27 12:43:42 2026 +0800
fix(ai): clamp prompt template text on character boundaries (#6823)
---
web/src/pages/ai/promptTemplates.test.ts | 26 ++++++++++++++++++++++++++
web/src/pages/ai/promptTemplates.ts | 18 +++++++++++++++++-
2 files changed, 43 insertions(+), 1 deletion(-)
diff --git a/web/src/pages/ai/promptTemplates.test.ts
b/web/src/pages/ai/promptTemplates.test.ts
index c78dcddf6..12340f11a 100644
--- a/web/src/pages/ai/promptTemplates.test.ts
+++ b/web/src/pages/ai/promptTemplates.test.ts
@@ -19,6 +19,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
MAX_CUSTOM_PROMPT_TEMPLATES,
MAX_PROMPT_TEMPLATE_BODY_LENGTH,
+ MAX_PROMPT_TEMPLATE_TITLE_LENGTH,
PROMPT_TEMPLATE_STORAGE_KEY,
applyPromptTemplate,
buildPromptTemplatePreview,
@@ -240,6 +241,31 @@ describe('AI prompt templates', () => {
expect(buildPromptTemplatePreview('x'.repeat(20), 8)).toBe('xxxxxxxx...');
});
+ it('does not split a supplementary character when it clamps a saved title',
() => {
+ const result = saveCustomPromptTemplate({
+ title: `${'a'.repeat(MAX_PROMPT_TEMPLATE_TITLE_LENGTH - 1)}\u{1F680}`,
+ body: 'inspect the broker',
+ });
+
+ expect(result.ok).toBe(true);
+ // The title cap is 80 UTF-16 units and the emoji needs two, so slice()
kept its high half:
+ // the saved template carried an unpaired surrogate, which the modal
renders as a replacement
+ // glyph. The character straddling the cut is dropped whole instead.
+
expect(result.template?.title).toBe('a'.repeat(MAX_PROMPT_TEMPLATE_TITLE_LENGTH
- 1));
+ });
+
+ it('does not split a supplementary character when it clamps a saved body',
() => {
+ // The body the operator saves is the composer's own text: 5999 units,
then an emoji whose
+ // halves straddle the 6000-unit cap, which becomes the prompt a template
is applied with.
+ const result = saveCustomPromptTemplate({
+ title: 'long body',
+ body: `${'a'.repeat(MAX_PROMPT_TEMPLATE_BODY_LENGTH - 1)}\u{1F680}`,
+ });
+
+ expect(result.ok).toBe(true);
+
expect(result.template?.body).toBe('a'.repeat(MAX_PROMPT_TEMPLATE_BODY_LENGTH -
1));
+ });
+
it('reports storage as unavailable when localStorage throws', () => {
vi.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
throw new DOMException('blocked', 'SecurityError');
diff --git a/web/src/pages/ai/promptTemplates.ts
b/web/src/pages/ai/promptTemplates.ts
index 31935d50d..3200204b7 100644
--- a/web/src/pages/ai/promptTemplates.ts
+++ b/web/src/pages/ai/promptTemplates.ts
@@ -220,9 +220,25 @@ const newTemplateId = (): string => {
return `custom-${random}`;
};
+/**
+ * Clamps `value` to `maxCodeUnits` UTF-16 units — the unit
`String.prototype.length` counts — without
+ * cutting between the two halves of a supplementary character: a character
that would straddle the
+ * cut is dropped whole. `slice` alone keeps its high surrogate, and a lone
surrogate is not text
+ * anybody can see: the browser renders it as the replacement glyph, so the
operator's emoji would
+ * arrive in the template list as `\uFFFD` either way — dropping it whole at
least leaves the title
+ * the operator can still read.
+ */
+const clampText = (value: string, maxCodeUnits: number): string => {
+ if (value.length <= maxCodeUnits) return value;
+ const cut = Math.max(0, maxCodeUnits);
+ const lastKeptUnit = cut === 0 ? 0 : value.charCodeAt(cut - 1);
+ const splitsASurrogatePair = lastKeptUnit >= 0xd800 && lastKeptUnit <=
0xdbff;
+ return value.slice(0, splitsASurrogatePair ? cut - 1 : cut);
+};
+
const normalizeText = (value: unknown, maxLength: number): string => {
const text = typeof value === 'string' ? value.trim() : '';
- return text.length > maxLength ? text.slice(0, maxLength) : text;
+ return clampText(text, maxLength);
};
const normalizeMode = (value: unknown): ChatMode =>