-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmessage-command.ts
91 lines (77 loc) · 2.42 KB
/
message-command.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { setOwner } from '@ember/owner';
import type Owner from '@ember/owner';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { ResolvedCodeRef } from '@cardstack/runtime-common';
import type CardService from '@cardstack/host/services/card-service';
import type CommandService from '@cardstack/host/services/command-service';
import type MatrixService from '@cardstack/host/services/matrix-service';
import type { CardDef } from 'https://cardstack.com/base/card-api';
import { Message } from './message';
type CommandStatus = 'applied' | 'ready' | 'applying';
export default class MessageCommand {
@tracked name: string;
@tracked payload: any;
@tracked commandStatus?: CommandStatus;
@tracked commandResultCardEventId?: string;
constructor(
public message: Message,
public toolCallId: string,
name: string,
payload: any, //arguments of toolCall. Its not called arguments due to lint
public codeRef: ResolvedCodeRef | undefined,
public eventId: string,
commandStatus: CommandStatus,
commandResultCardEventId: string | undefined,
owner: Owner,
) {
setOwner(this, owner);
this.name = name;
this.payload = payload;
this.commandStatus = commandStatus;
this.commandResultCardEventId = commandResultCardEventId;
}
@service declare commandService: CommandService;
@service declare matrixService: MatrixService;
@service declare cardService: CardService;
get status() {
if (
this.commandService.currentlyExecutingCommandEventIds.has(this.eventId)
) {
return 'applying';
}
return this.commandStatus;
}
get commandResultCardDoc() {
if (!this.commandResultCardEventId) {
return undefined;
}
let roomResource = this.matrixService.roomResources.get(
this.message.roomId,
);
if (!roomResource) {
return undefined;
}
try {
let cardDoc = roomResource.serializedCardFromFragments(
this.commandResultCardEventId,
);
return cardDoc;
} catch {
// the command result card fragments might not be loaded yet
return undefined;
}
}
async getCommandResultCard(): Promise<CardDef | undefined> {
let cardDoc = this.commandResultCardDoc;
if (!cardDoc) {
return undefined;
}
let card = await this.cardService.createFromSerialized(
cardDoc.data,
cardDoc,
undefined,
);
return card;
}
}