@@ -37,7 +37,7 @@ export class ConversationAggregate {
3737 private status : ConversationStatus ;
3838 private readonly messages : ConversationMessage [ ] ;
3939 private aiStatus : ConversationAIStatus ;
40- private totalTokens : number ;
40+ private totalTokensSpent : number ;
4141
4242 private newEvents : ConversationEvent [ ] = [ ] ;
4343
@@ -49,7 +49,7 @@ export class ConversationAggregate {
4949 this . status = { status : "ONGOING" } ;
5050 this . messages = [ ] ;
5151 this . aiStatus = { status : "IDLE" } ;
52- this . totalTokens = 0 ;
52+ this . totalTokensSpent = 0 ;
5353
5454 if ( createEvent ) {
5555 this . createAndApply ( createEvent ) ;
@@ -121,8 +121,9 @@ export class ConversationAggregate {
121121 message : {
122122 id : cmd . correlationId ,
123123 text : cmd . message ,
124- tokens : cmd . tokens ,
124+ tokens : cmd . messageTokens ,
125125 } ,
126+ totalTokensSpent : cmd . totalTokensSpent ,
126127 } ) ;
127128
128129 this . endConversationIfWentOverLimit ( ) ;
@@ -166,14 +167,14 @@ export class ConversationAggregate {
166167 }
167168
168169 private endConversationIfWentOverLimit ( ) : boolean {
169- if ( this . totalTokens > config . conversation . maxConversationTokens ) {
170+ if ( this . totalTokensSpent > config . conversation . maximumSpentTokens ) {
170171 this . createAndApply ( {
171172 type : "CONVERSATION_ENDED" ,
172173 conversationId : this . conversationId ,
173174 reason : {
174175 type : "MAXIMUM_CONVERSATION_TOKENS_REACHED" ,
175- maxConversationTokens : config . conversation . maxConversationTokens ,
176- totalTokens : this . totalTokens ,
176+ maximumSpentTokens : config . conversation . maximumSpentTokens ,
177+ totalTokensSpent : this . totalTokensSpent ,
177178 } ,
178179 } ) ;
179180
@@ -185,13 +186,12 @@ export class ConversationAggregate {
185186
186187 private addConversationMessage ( {
187188 message,
188- tokens,
189- } : {
189+ } : // tokens,
190+ {
190191 message : ConversationMessage ;
191192 tokens : number ;
192193 } ) : void {
193194 this . messages . push ( message ) ;
194- this . totalTokens += tokens ;
195195 }
196196
197197 private createAndApply (
@@ -223,6 +223,7 @@ export class ConversationAggregate {
223223 switch ( event . type ) {
224224 case "BOT_RESPONSE_ADDED" : {
225225 this . aiStatus = { status : "IDLE" } ;
226+ this . totalTokensSpent += event . totalTokensSpent ;
226227
227228 return this . addConversationMessage ( {
228229 message : {
0 commit comments