@@ -8,20 +8,19 @@ import type {
8
8
MatrixEvent as DiscreteMatrixEvent ,
9
9
CardFragmentContent ,
10
10
CommandEvent ,
11
- CommandResultEvent ,
12
- ReactionEvent ,
13
11
Tool ,
14
12
SkillsConfigEvent ,
13
+ CommandResultEvent ,
15
14
} from 'https://cardstack.com/base/matrix-event' ;
16
15
import { MatrixEvent , type IRoomEvent } from 'matrix-js-sdk' ;
17
16
import { ChatCompletionMessageToolCall } from 'openai/resources/chat/completions' ;
18
17
import * as Sentry from '@sentry/node' ;
19
18
import { logger } from '@cardstack/runtime-common' ;
19
+ import { APP_BOXEL_COMMAND_RESULT_EVENT_TYPE } from '../runtime-common/matrix-constants' ;
20
20
import {
21
21
APP_BOXEL_CARDFRAGMENT_MSGTYPE ,
22
22
APP_BOXEL_MESSAGE_MSGTYPE ,
23
23
APP_BOXEL_COMMAND_MSGTYPE ,
24
- APP_BOXEL_COMMAND_RESULT_MSGTYPE ,
25
24
APP_BOXEL_ROOM_SKILLS_EVENT_TYPE ,
26
25
} from '@cardstack/runtime-common/matrix-constants' ;
27
26
@@ -140,6 +139,15 @@ export function constructHistory(
140
139
}
141
140
}
142
141
let event = { ...rawEvent } as DiscreteMatrixEvent ;
142
+ if ( event . type === APP_BOXEL_COMMAND_RESULT_EVENT_TYPE ) {
143
+ let { cardEventId } = event . content . data ;
144
+ if ( cardEventId ) {
145
+ event . content . data . card = serializedCardFromFragments (
146
+ cardEventId ,
147
+ cardFragments ,
148
+ ) ;
149
+ }
150
+ }
143
151
if ( event . type !== 'm.room.message' ) {
144
152
continue ;
145
153
}
@@ -358,60 +366,20 @@ export function getToolChoice(
358
366
return 'auto' ;
359
367
}
360
368
361
- export function isCommandResultEvent (
362
- event : DiscreteMatrixEvent ,
363
- ) : event is CommandResultEvent {
364
- return (
365
- event . type === 'm.room.message' &&
366
- typeof event . content === 'object' &&
367
- event . content . msgtype === APP_BOXEL_COMMAND_RESULT_MSGTYPE
368
- ) ;
369
- }
370
-
371
- export function isReactionEvent (
372
- event : DiscreteMatrixEvent ,
373
- ) : event is ReactionEvent {
374
- return (
375
- event . type === 'm.reaction' &&
376
- event . content [ 'm.relates_to' ] . rel_type === 'm.annotation'
377
- ) ;
378
- }
379
-
380
- function getReactionStatus (
381
- commandEvent : DiscreteMatrixEvent ,
382
- history : DiscreteMatrixEvent [ ] ,
383
- ) {
384
- let maybeReactionEvent = history . find ( ( e ) => {
385
- if (
386
- isReactionEvent ( e ) &&
387
- e . content [ 'm.relates_to' ] ?. event_id === commandEvent . event_id
388
- ) {
389
- return true ;
390
- }
391
- return false ;
392
- } ) ;
393
- return maybeReactionEvent && isReactionEvent ( maybeReactionEvent )
394
- ? maybeReactionEvent . content [ 'm.relates_to' ] . key
395
- : undefined ;
396
- }
397
-
398
369
function getCommandResult (
399
370
commandEvent : CommandEvent ,
400
371
history : DiscreteMatrixEvent [ ] ,
401
372
) {
402
- let maybeCommandResultEvent = history . find ( ( e ) => {
373
+ let commandResultEvent = history . find ( ( e ) => {
403
374
if (
404
375
isCommandResultEvent ( e ) &&
405
376
e . content [ 'm.relates_to' ] ?. event_id === commandEvent . event_id
406
377
) {
407
378
return true ;
408
379
}
409
380
return false ;
410
- } ) ;
411
- return maybeCommandResultEvent &&
412
- isCommandResultEvent ( maybeCommandResultEvent )
413
- ? maybeCommandResultEvent . content . result
414
- : undefined ;
381
+ } ) as CommandResultEvent | undefined ;
382
+ return commandResultEvent ;
415
383
}
416
384
417
385
function toToolCall ( event : CommandEvent ) : ChatCompletionMessageToolCall {
@@ -429,21 +397,23 @@ function toPromptMessageWithToolResult(
429
397
event : CommandEvent ,
430
398
history : DiscreteMatrixEvent [ ] ,
431
399
) : OpenAIPromptMessage {
432
- let commandResult = getCommandResult ( event as CommandEvent , history ) ;
400
+ let commandResult = getCommandResult ( event , history ) ;
401
+ let content = 'pending' ;
433
402
if ( commandResult ) {
434
- return {
435
- role : 'tool' ,
436
- content : commandResult ,
437
- tool_call_id : event . content . data . toolCall . id ,
438
- } ;
439
- } else {
440
- let reactionStatus = getReactionStatus ( event , history ) ;
441
- return {
442
- role : 'tool' ,
443
- content : reactionStatus ?? 'pending' ,
444
- tool_call_id : event . content . data . toolCall . id ,
445
- } ;
403
+ let status = commandResult . content [ 'm.relates_to' ] ?. key ;
404
+ if ( commandResult . content . data . card ) {
405
+ content = `Command ${ status } , with result card: ${ JSON . stringify (
406
+ commandResult . content . data . card ,
407
+ ) } .\n`;
408
+ } else {
409
+ content = `Command ${ status } .\n` ;
410
+ }
446
411
}
412
+ return {
413
+ role : 'tool' ,
414
+ content,
415
+ tool_call_id : event . content . data . toolCall . id ,
416
+ } ;
447
417
}
448
418
449
419
export function getModifyPrompt (
@@ -570,24 +540,13 @@ export function cleanContent(content: string) {
570
540
return content . trim ( ) ;
571
541
}
572
542
573
- export const isCommandReactionEvent = ( event ?: MatrixEvent ) => {
574
- if ( event === undefined ) {
575
- return false ;
576
- }
577
- let content = event . getContent ( ) ;
578
- return (
579
- event . getType ( ) === 'm.reaction' &&
580
- content [ 'm.relates_to' ] ?. rel_type === 'm.annotation'
581
- ) ;
582
- } ;
583
-
584
- export const isCommandReactionStatusApplied = ( event ?: MatrixEvent ) => {
543
+ export const isCommandResultStatusApplied = ( event ?: MatrixEvent ) => {
585
544
if ( event === undefined ) {
586
545
return false ;
587
546
}
588
- let content = event . getContent ( ) ;
589
547
return (
590
- isCommandReactionEvent ( event ) && content [ 'm.relates_to' ] ?. key === 'applied'
548
+ isCommandResultEvent ( event . event as DiscreteMatrixEvent ) &&
549
+ event . getContent ( ) [ 'm.relates_to' ] ?. key === 'applied'
591
550
) ;
592
551
} ;
593
552
@@ -603,3 +562,15 @@ export function isCommandEvent(
603
562
typeof event . content . data . toolCall === 'object'
604
563
) ;
605
564
}
565
+
566
+ export function isCommandResultEvent (
567
+ event ?: DiscreteMatrixEvent ,
568
+ ) : event is CommandResultEvent {
569
+ if ( event === undefined ) {
570
+ return false ;
571
+ }
572
+ return (
573
+ event . type === APP_BOXEL_COMMAND_RESULT_EVENT_TYPE &&
574
+ event . content [ 'm.relates_to' ] ?. rel_type === 'm.annotation'
575
+ ) ;
576
+ }
0 commit comments