@@ -19,19 +19,17 @@ import { MatrixEvent, type IRoomEvent } from 'matrix-js-sdk';
19
19
import { ChatCompletionMessageToolCall } from 'openai/resources/chat/completions' ;
20
20
import * as Sentry from '@sentry/node' ;
21
21
import { logger } from '@cardstack/runtime-common' ;
22
- import {
23
- APP_BOXEL_COMMAND_REQUESTS_KEY ,
24
- APP_BOXEL_COMMAND_DEFINITIONS_MSGTYPE ,
25
- APP_BOXEL_COMMAND_RESULT_EVENT_TYPE ,
26
- APP_BOXEL_COMMAND_RESULT_REL_TYPE ,
27
- APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE ,
28
- } from '../runtime-common/matrix-constants' ;
29
22
import {
30
23
APP_BOXEL_CARDFRAGMENT_MSGTYPE ,
31
24
APP_BOXEL_MESSAGE_MSGTYPE ,
32
25
APP_BOXEL_ROOM_SKILLS_EVENT_TYPE ,
33
26
DEFAULT_LLM ,
34
27
APP_BOXEL_ACTIVE_LLM ,
28
+ APP_BOXEL_COMMAND_REQUESTS_KEY ,
29
+ APP_BOXEL_COMMAND_DEFINITIONS_MSGTYPE ,
30
+ APP_BOXEL_COMMAND_RESULT_EVENT_TYPE ,
31
+ APP_BOXEL_COMMAND_RESULT_REL_TYPE ,
32
+ APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE ,
35
33
} from '@cardstack/runtime-common/matrix-constants' ;
36
34
37
35
let log = logger ( 'ai-bot' ) ;
@@ -654,7 +652,7 @@ export async function getModifyPrompt(
654
652
continue ;
655
653
}
656
654
if ( isCommandResultEvent ( event ) ) {
657
- continue ;
655
+ continue ; // we'll include these with the tool calls
658
656
}
659
657
if (
660
658
'isStreamingFinished' in event . content &&
@@ -663,40 +661,39 @@ export async function getModifyPrompt(
663
661
continue ;
664
662
}
665
663
let body = event . content . body ;
666
- if ( body ) {
667
- if ( event . sender === aiBotUserId ) {
668
- let toolCalls = toToolCalls ( event ) ;
669
- let historicalMessage : OpenAIPromptMessage = {
670
- role : 'assistant' ,
671
- content : body ,
672
- } ;
673
- if ( toolCalls . length ) {
674
- historicalMessage . tool_calls = toolCalls ;
675
- }
676
- historicalMessages . push ( historicalMessage ) ;
677
- if ( toolCalls . length ) {
678
- toPromptMessageWithToolResults ( event , history ) . forEach ( ( message ) =>
679
- historicalMessages . push ( message ) ,
680
- ) ;
681
- }
682
- } else {
683
- if (
684
- event . content . msgtype === APP_BOXEL_MESSAGE_MSGTYPE &&
685
- event . content . data ?. context ?. openCardIds
686
- ) {
687
- body = `User message: ${ body }
664
+ if ( event . sender === aiBotUserId ) {
665
+ let toolCalls = toToolCalls ( event ) ;
666
+ let historicalMessage : OpenAIPromptMessage = {
667
+ role : 'assistant' ,
668
+ content : body ,
669
+ } ;
670
+ if ( toolCalls . length ) {
671
+ historicalMessage . tool_calls = toolCalls ;
672
+ }
673
+ historicalMessages . push ( historicalMessage ) ;
674
+ if ( toolCalls . length ) {
675
+ toPromptMessageWithToolResults ( event , history ) . forEach ( ( message ) =>
676
+ historicalMessages . push ( message ) ,
677
+ ) ;
678
+ }
679
+ }
680
+ if ( body && event . sender !== aiBotUserId ) {
681
+ if (
682
+ event . content . msgtype === APP_BOXEL_MESSAGE_MSGTYPE &&
683
+ event . content . data ?. context ?. openCardIds
684
+ ) {
685
+ body = `User message: ${ body }
688
686
Context: the user has the following cards open: ${ JSON . stringify (
689
687
event . content . data . context . openCardIds ,
690
688
) } `;
691
- } else {
692
- body = `User message: ${ body }
689
+ } else {
690
+ body = `User message: ${ body }
693
691
Context: the user has no open cards.` ;
694
- }
695
- historicalMessages . push ( {
696
- role : 'user' ,
697
- content : body ,
698
- } ) ;
699
692
}
693
+ historicalMessages . push ( {
694
+ role : 'user' ,
695
+ content : body ,
696
+ } ) ;
700
697
}
701
698
}
702
699
@@ -799,27 +796,3 @@ export function isCommandResultEvent(
799
796
APP_BOXEL_COMMAND_RESULT_REL_TYPE
800
797
) ;
801
798
}
802
-
803
- export function eventRequiresResponse ( event : MatrixEvent ) {
804
- // If it's a message, we should respond unless it's a card fragment
805
- if ( event . getType ( ) === 'm.room.message' ) {
806
- if (
807
- event . getContent ( ) . msgtype === APP_BOXEL_CARDFRAGMENT_MSGTYPE ||
808
- event . getContent ( ) . msgtype === APP_BOXEL_COMMAND_DEFINITIONS_MSGTYPE
809
- ) {
810
- return false ;
811
- }
812
- return true ;
813
- }
814
-
815
- // If it's a command result with output, we should respond
816
- if (
817
- event . getType ( ) === APP_BOXEL_COMMAND_RESULT_EVENT_TYPE &&
818
- event . getContent ( ) . msgtype === APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE
819
- ) {
820
- return true ;
821
- }
822
-
823
- // If it's a different type, or a command result without output, we should not respond
824
- return false ;
825
- }
0 commit comments