@@ -32,6 +32,8 @@ type RecentMessageContext = {
3232 text : string ;
3333 isBot : boolean ;
3434} ;
35+ const CONTEXT_MESSAGE_LIMIT = 30 ;
36+ const LOG_MESSAGE_PREVIEW_LENGTH = 80 ;
3537
3638let reviewBot : Chat < {
3739 discord : ReturnType < typeof createDiscordAdapter > ;
@@ -67,6 +69,19 @@ function toReviewChannelId(guildId: string, reviewChannelId: string): string {
6769 return `discord:${ guildId } :${ reviewChannelId } ` ;
6870}
6971
72+ function buildContextLog ( messages : RecentMessageContext [ ] ) : string {
73+ return messages
74+ . map ( ( message , index ) => {
75+ const role = message . isBot ? "bot" : "user" ;
76+ const preview = message . text . replace ( / \s + / g, " " ) . slice (
77+ 0 ,
78+ LOG_MESSAGE_PREVIEW_LENGTH ,
79+ ) ;
80+ return `${ index + 1 } /${ messages . length } role=${ role } author="${ message . author } " chars=${ message . text . length } preview="${ preview } "` ;
81+ } )
82+ . join ( " | " ) ;
83+ }
84+
7085function registerHandlers (
7186 bot : Chat < { discord : ReturnType < typeof createDiscordAdapter > } > ,
7287) : void {
@@ -134,11 +149,23 @@ function registerHandlers(
134149 text,
135150 isBot : recentMessage . author . isBot === true ,
136151 } ) ;
137- if ( recentMessages . length >= 30 ) {
152+ if ( recentMessages . length >= CONTEXT_MESSAGE_LIMIT ) {
138153 break ;
139154 }
140155 }
141156 recentMessages . reverse ( ) ;
157+ console . info (
158+ `[discord-context] thread=${ thread . id } fetched=${ recentMessages . length } limit=${ CONTEXT_MESSAGE_LIMIT } promptChars=${ userPrompt . length } ` ,
159+ ) ;
160+ if ( recentMessages . length > 0 ) {
161+ console . info (
162+ `[discord-context] thread=${ thread . id } messages=${ buildContextLog ( recentMessages ) } ` ,
163+ ) ;
164+ } else {
165+ console . info (
166+ `[discord-context] thread=${ thread . id } no prior text messages found` ,
167+ ) ;
168+ }
142169
143170 const response = await runDiscordPromptAgent ( {
144171 prompt : userPrompt . slice ( 0 , 4000 ) ,
@@ -148,6 +175,9 @@ function registerHandlers(
148175 } catch ( error ) {
149176 const errorMessage =
150177 error instanceof Error ? error . message : "Unknown error" ;
178+ console . error (
179+ `[discord-context] thread=${ thread . id } prompt_failed=${ errorMessage } ` ,
180+ ) ;
151181 await thread . post (
152182 `I hit an error while processing that request: ${ errorMessage } ` ,
153183 ) ;
0 commit comments