@@ -8,20 +8,22 @@ 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 {
20
+ APP_BOXEL_COMMAND_RESULT_EVENT_TYPE ,
21
+ APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE ,
22
+ } from '../runtime-common/matrix-constants' ;
20
23
import {
21
24
APP_BOXEL_CARDFRAGMENT_MSGTYPE ,
22
25
APP_BOXEL_MESSAGE_MSGTYPE ,
23
26
APP_BOXEL_COMMAND_MSGTYPE ,
24
- APP_BOXEL_COMMAND_RESULT_MSGTYPE ,
25
27
APP_BOXEL_ROOM_SKILLS_EVENT_TYPE ,
26
28
} from '@cardstack/runtime-common/matrix-constants' ;
27
29
@@ -140,6 +142,16 @@ export function constructHistory(
140
142
}
141
143
}
142
144
let event = { ...rawEvent } as DiscreteMatrixEvent ;
145
+ if (
146
+ event . type === APP_BOXEL_COMMAND_RESULT_EVENT_TYPE &&
147
+ event . content . msgtype == APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE
148
+ ) {
149
+ let { cardEventId } = event . content . data ;
150
+ event . content . data . card = serializedCardFromFragments (
151
+ cardEventId ,
152
+ cardFragments ,
153
+ ) ;
154
+ }
143
155
if ( event . type !== 'm.room.message' ) {
144
156
continue ;
145
157
}
@@ -358,60 +370,20 @@ export function getToolChoice(
358
370
return 'auto' ;
359
371
}
360
372
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
373
function getCommandResult (
399
374
commandEvent : CommandEvent ,
400
375
history : DiscreteMatrixEvent [ ] ,
401
376
) {
402
- let maybeCommandResultEvent = history . find ( ( e ) => {
377
+ let commandResultEvent = history . find ( ( e ) => {
403
378
if (
404
379
isCommandResultEvent ( e ) &&
405
380
e . content [ 'm.relates_to' ] ?. event_id === commandEvent . event_id
406
381
) {
407
382
return true ;
408
383
}
409
384
return false ;
410
- } ) ;
411
- return maybeCommandResultEvent &&
412
- isCommandResultEvent ( maybeCommandResultEvent )
413
- ? maybeCommandResultEvent . content . result
414
- : undefined ;
385
+ } ) as CommandResultEvent | undefined ;
386
+ return commandResultEvent ;
415
387
}
416
388
417
389
function toToolCall ( event : CommandEvent ) : ChatCompletionMessageToolCall {
@@ -429,21 +401,26 @@ function toPromptMessageWithToolResult(
429
401
event : CommandEvent ,
430
402
history : DiscreteMatrixEvent [ ] ,
431
403
) : OpenAIPromptMessage {
432
- let commandResult = getCommandResult ( event as CommandEvent , history ) ;
404
+ let commandResult = getCommandResult ( event , history ) ;
405
+ let content = 'pending' ;
433
406
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
- } ;
407
+ let status = commandResult . content [ 'm.relates_to' ] ?. key ;
408
+ if (
409
+ commandResult . content . msgtype ===
410
+ APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE
411
+ ) {
412
+ content = `Command ${ status } , with result card: ${ JSON . stringify (
413
+ commandResult . content . data . card ,
414
+ ) } .\n`;
415
+ } else {
416
+ content = `Command ${ status } .\n` ;
417
+ }
446
418
}
419
+ return {
420
+ role : 'tool' ,
421
+ content,
422
+ tool_call_id : event . content . data . toolCall . id ,
423
+ } ;
447
424
}
448
425
449
426
export function getModifyPrompt (
@@ -570,24 +547,13 @@ export function cleanContent(content: string) {
570
547
return content . trim ( ) ;
571
548
}
572
549
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 ) => {
550
+ export const isCommandResultStatusApplied = ( event ?: MatrixEvent ) => {
585
551
if ( event === undefined ) {
586
552
return false ;
587
553
}
588
- let content = event . getContent ( ) ;
589
554
return (
590
- isCommandReactionEvent ( event ) && content [ 'm.relates_to' ] ?. key === 'applied'
555
+ isCommandResultEvent ( event . event as DiscreteMatrixEvent ) &&
556
+ event . getContent ( ) [ 'm.relates_to' ] ?. key === 'applied'
591
557
) ;
592
558
} ;
593
559
@@ -603,3 +569,15 @@ export function isCommandEvent(
603
569
typeof event . content . data . toolCall === 'object'
604
570
) ;
605
571
}
572
+
573
+ export function isCommandResultEvent (
574
+ event ?: DiscreteMatrixEvent ,
575
+ ) : event is CommandResultEvent {
576
+ if ( event === undefined ) {
577
+ return false ;
578
+ }
579
+ return (
580
+ event . type === APP_BOXEL_COMMAND_RESULT_EVENT_TYPE &&
581
+ event . content [ 'm.relates_to' ] ?. rel_type === 'm.annotation'
582
+ ) ;
583
+ }
0 commit comments