66 extractAssistantText ,
77 prepareSimpleCompletionModelForAgent ,
88} from "openclaw/plugin-sdk/simple-completion-runtime" ;
9+ import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime" ;
910import { withAbortTimeout } from "./timeouts.js" ;
1011
1112const DEFAULT_THREAD_TITLE_TIMEOUT_MS = 60_000 ;
@@ -51,9 +52,8 @@ export async function generateThreadTitle(params: {
5152 }
5253
5354 try {
54- const promptText = truncateThreadTitleSourceText ( sourceText ) ;
55- const userMessage = buildThreadTitleUserMessage ( {
56- sourceText : promptText ,
55+ const userMessage = buildThreadTitleCompletionUserMessage ( {
56+ sourceText,
5757 channelName : params . channelName ,
5858 channelDescription : params . channelDescription ,
5959 } ) ;
@@ -104,11 +104,12 @@ async function completeThreadTitle(params: {
104104 } ) ;
105105}
106106
107- function buildThreadTitleUserMessage ( params : {
107+ function buildThreadTitleCompletionUserMessage ( params : {
108108 sourceText : string ;
109109 channelName ?: string ;
110110 channelDescription ?: string ;
111111} ) : string {
112+ const sourceText = truncateThreadTitleSourceText ( params . sourceText ) ;
112113 const channelName = normalizeTitleContextField (
113114 params . channelName ,
114115 MAX_THREAD_TITLE_CHANNEL_NAME_CHARS ,
@@ -124,15 +125,15 @@ function buildThreadTitleUserMessage(params: {
124125 if ( channelDescription ) {
125126 messageLines . push ( `Channel description: ${ channelDescription } ` ) ;
126127 }
127- messageLines . push ( `Message:\n${ params . sourceText } ` ) ;
128+ messageLines . push ( `Message:\n${ sourceText } ` ) ;
128129 return messageLines . join ( "\n\n" ) ;
129130}
130131
131132function truncateThreadTitleSourceText ( sourceText : string ) : string {
132133 if ( sourceText . length <= MAX_THREAD_TITLE_SOURCE_CHARS ) {
133134 return sourceText ;
134135 }
135- return `${ sourceText . slice ( 0 , MAX_THREAD_TITLE_SOURCE_CHARS ) } ...` ;
136+ return `${ truncateUtf16Safe ( sourceText , MAX_THREAD_TITLE_SOURCE_CHARS ) } ...` ;
136137}
137138
138139function resolveThreadTitleTimeoutMs ( timeoutMs : number | undefined ) : number {
@@ -201,5 +202,5 @@ function normalizeTitleContextField(raw: string | undefined, maxChars: number):
201202 if ( singleLine . length <= maxChars ) {
202203 return singleLine ;
203204 }
204- return `${ singleLine . slice ( 0 , maxChars ) } ...` ;
205+ return `${ truncateUtf16Safe ( singleLine , maxChars ) } ...` ;
205206}
0 commit comments