Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pm-stack/dispatch/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,16 @@ export function titleFromPrompt(prompt: string, max = 120): string {
.split("\n")
.map((l) => l.trim())
.find((l) => l.length > 0) ?? "dispatch request";
return first.length > max ? first.slice(0, max - 1) + "…" : first;
if (first.length <= max) return first;

// Keep the UTF-16 cap and suffix, but don't leave half of a surrogate pair.
let end = max - 1;
if (end > 0 && end < first.length) {
const before = first.charCodeAt(end - 1);
const after = first.charCodeAt(end);
if (before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff) {
end--;
}
}
return first.slice(0, end) + "…";
}
7 changes: 7 additions & 0 deletions pm-stack/dispatch/test/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ describe("request validation", () => {
test("titleFromPrompt takes the first non-empty line, capped", () => {
expect(titleFromPrompt("\n\n fix the thing \nmore")).toBe("fix the thing");
expect(titleFromPrompt("x".repeat(200)).length).toBe(120);
expect(titleFromPrompt("x".repeat(200))).toBe("x".repeat(119) + "…");
});

test("titleFromPrompt does not split astral code points when truncating", () => {
expect(titleFromPrompt("x".repeat(118) + "😀" + "y".repeat(10))).toBe("x".repeat(118) + "…");
expect(titleFromPrompt("ab😀cde", 4)).toBe("ab…");
expect(titleFromPrompt("a😀bcde", 4)).toBe("a😀…");
});
});

Expand Down
Loading