Skip to content

Commit f062226

Browse files
committed
fix(discord): expose thread title prompt formatter
1 parent 55060de commit f062226

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

extensions/discord/src/monitor/thread-title.generate.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const prepareSimpleCompletionModelForAgentMock =
1111
const extractAssistantTextMock = vi.fn<typeof agentRuntimeModule.extractAssistantText>();
1212

1313
let generateThreadTitle: typeof import("./thread-title.js").generateThreadTitle;
14+
let buildThreadTitleCompletionUserMessage: typeof import("./thread-title.js").buildThreadTitleCompletionUserMessage;
1415

1516
function firstCompletionArgs(): Parameters<
1617
typeof agentRuntimeModule.completeWithPreparedSimpleCompletionModel
@@ -41,7 +42,9 @@ function hasLoneSurrogate(value: string): boolean {
4142
}
4243

4344
beforeAll(async () => {
44-
({ generateThreadTitle } = await import("./thread-title.js"));
45+
({ buildThreadTitleCompletionUserMessage, generateThreadTitle } = await import(
46+
"./thread-title.js"
47+
));
4548
});
4649

4750
beforeEach(() => {
@@ -236,6 +239,19 @@ describe("generateThreadTitle", () => {
236239
expect(content).toContain(`${"d".repeat(319)}...`);
237240
});
238241

242+
it("builds prompt text without lone surrogates at truncation boundaries", () => {
243+
const content = buildThreadTitleCompletionUserMessage({
244+
sourceText: `${"m".repeat(599)}😀tail`,
245+
channelName: `${"n".repeat(119)}😀tail`,
246+
channelDescription: `${"d".repeat(319)}😀tail`,
247+
});
248+
249+
expect(hasLoneSurrogate(content)).toBe(false);
250+
expect(content).toContain(`${"m".repeat(599)}...`);
251+
expect(content).toContain(`${"n".repeat(119)}...`);
252+
expect(content).toContain(`${"d".repeat(319)}...`);
253+
});
254+
239255
it("clamps completion budget to the selected model output cap", async () => {
240256
prepareSimpleCompletionModelForAgentMock.mockResolvedValueOnce({
241257
selection: {

extensions/discord/src/monitor/thread-title.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ export async function generateThreadTitle(params: {
5252
}
5353

5454
try {
55-
const promptText = truncateThreadTitleSourceText(sourceText);
56-
const userMessage = buildThreadTitleUserMessage({
57-
sourceText: promptText,
55+
const userMessage = buildThreadTitleCompletionUserMessage({
56+
sourceText,
5857
channelName: params.channelName,
5958
channelDescription: params.channelDescription,
6059
});
@@ -105,11 +104,12 @@ async function completeThreadTitle(params: {
105104
});
106105
}
107106

108-
function buildThreadTitleUserMessage(params: {
107+
export function buildThreadTitleCompletionUserMessage(params: {
109108
sourceText: string;
110109
channelName?: string;
111110
channelDescription?: string;
112111
}): string {
112+
const sourceText = truncateThreadTitleSourceText(params.sourceText);
113113
const channelName = normalizeTitleContextField(
114114
params.channelName,
115115
MAX_THREAD_TITLE_CHANNEL_NAME_CHARS,
@@ -125,7 +125,7 @@ function buildThreadTitleUserMessage(params: {
125125
if (channelDescription) {
126126
messageLines.push(`Channel description: ${channelDescription}`);
127127
}
128-
messageLines.push(`Message:\n${params.sourceText}`);
128+
messageLines.push(`Message:\n${sourceText}`);
129129
return messageLines.join("\n\n");
130130
}
131131

0 commit comments

Comments
 (0)