@@ -27,6 +27,7 @@ import {
27
27
LooseCardResource ,
28
28
ResolvedCodeRef ,
29
29
aiBotUsername ,
30
+ getClass ,
30
31
} from '@cardstack/runtime-common' ;
31
32
32
33
import {
@@ -42,6 +43,7 @@ import {
42
43
APP_BOXEL_CARD_FORMAT ,
43
44
APP_BOXEL_CARDFRAGMENT_MSGTYPE ,
44
45
APP_BOXEL_COMMAND_MSGTYPE ,
46
+ APP_BOXEL_COMMAND_DEFINITIONS_MSGTYPE ,
45
47
APP_BOXEL_COMMAND_RESULT_EVENT_TYPE ,
46
48
APP_BOXEL_COMMAND_RESULT_WITH_NO_OUTPUT_MSGTYPE ,
47
49
APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE ,
@@ -73,10 +75,11 @@ import type {
73
75
MatrixEvent as DiscreteMatrixEvent ,
74
76
CommandResultWithNoOutputContent ,
75
77
CommandResultWithOutputContent ,
78
+ CommandDefinitionsContent ,
76
79
} from 'https://cardstack.com/base/matrix-event' ;
77
80
78
81
import type { Tool } from 'https://cardstack.com/base/matrix-event' ;
79
- import { SkillCard } from 'https://cardstack.com/base/skill-card' ;
82
+ import { CommandField , SkillCard } from 'https://cardstack.com/base/skill-card' ;
80
83
81
84
import AddSkillsToRoomCommand from '../commands/add-skills-to-room' ;
82
85
import { importResource } from '../resources/import' ;
@@ -513,7 +516,8 @@ export default class MatrixService extends Service {
513
516
| CardMessageContent
514
517
| CardFragmentContent
515
518
| CommandResultWithNoOutputContent
516
- | CommandResultWithOutputContent ,
519
+ | CommandResultWithOutputContent
520
+ | CommandDefinitionsContent ,
517
521
) {
518
522
let roomData = await this . ensureRoomData ( roomId ) ;
519
523
return roomData . mutex . dispatch ( async ( ) => {
@@ -578,11 +582,61 @@ export default class MatrixService extends Service {
578
582
}
579
583
}
580
584
585
+ async addCommandDefinitionsToRoomHistory (
586
+ commandDefinitions : CommandField [ ] ,
587
+ roomId : string ,
588
+ ) {
589
+ // Create the command defs so getting the json schema
590
+ // and send it to the matrix room.
591
+ let commandDefinitionSchemas : {
592
+ codeRef : ResolvedCodeRef ;
593
+ tool : Tool ;
594
+ } [ ] = [ ] ;
595
+ const mappings = await basicMappings ( this . loaderService . loader ) ;
596
+ for ( let commandDef of commandDefinitions ) {
597
+ const Command = await getClass (
598
+ commandDef . codeRef ,
599
+ this . loaderService . loader ,
600
+ ) ;
601
+ const command = new Command ( this . commandService . commandContext ) ;
602
+ const name = commandDef . functionName ;
603
+ commandDefinitionSchemas . push ( {
604
+ codeRef : commandDef . codeRef ,
605
+ tool : {
606
+ type : 'function' ,
607
+ function : {
608
+ name,
609
+ description : command . description ,
610
+ parameters : {
611
+ type : 'object' ,
612
+ properties : {
613
+ description : {
614
+ type : 'string' ,
615
+ } ,
616
+ ...( await command . getInputJsonSchema ( this . cardAPI , mappings ) ) ,
617
+ } ,
618
+ required : [ 'attributes' , 'description' ] ,
619
+ } ,
620
+ } ,
621
+ } ,
622
+ } ) ;
623
+ }
624
+ await this . sendEvent ( roomId , 'm.room.message' , {
625
+ msgtype : APP_BOXEL_COMMAND_DEFINITIONS_MSGTYPE ,
626
+ body : 'Command Definitions' ,
627
+ data : {
628
+ commandDefinitions : commandDefinitionSchemas ,
629
+ } ,
630
+ } ) ;
631
+ }
632
+
581
633
async addSkillCardsToRoomHistory (
582
634
skills : SkillCard [ ] ,
583
635
roomId : string ,
584
636
opts ?: CardAPI . SerializeOpts ,
585
637
) : Promise < string [ ] > {
638
+ const commandDefinitions = skills . flatMap ( ( skill ) => skill . commands ) ;
639
+ await this . addCommandDefinitionsToRoomHistory ( commandDefinitions , roomId ) ;
586
640
return this . addCardsToRoom ( skills , roomId , this . skillCardHashes , opts ) ;
587
641
}
588
642
0 commit comments