-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcreate-ai-assistant-room.ts
66 lines (57 loc) · 2.03 KB
/
create-ai-assistant-room.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
import { service } from '@ember/service';
import format from 'date-fns/format';
import { aiBotUsername } from '@cardstack/runtime-common';
import {
APP_BOXEL_ACTIVE_LLM,
DEFAULT_LLM,
} from '@cardstack/runtime-common/matrix-constants';
import type * as BaseCommandModule from 'https://cardstack.com/base/command';
import HostBaseCommand from '../lib/host-base-command';
import type MatrixService from '../services/matrix-service';
export default class CreateAiAssistantRoomCommand extends HostBaseCommand<
typeof BaseCommandModule.CreateAIAssistantRoomInput,
typeof BaseCommandModule.CreateAIAssistantRoomResult
> {
@service declare private matrixService: MatrixService;
async getInputType() {
let commandModule = await this.loadCommandModule();
const { CreateAIAssistantRoomInput } = commandModule;
return CreateAIAssistantRoomInput;
}
protected async run(
input: BaseCommandModule.CreateAIAssistantRoomInput,
): Promise<BaseCommandModule.CreateAIAssistantRoomResult> {
let { matrixService } = this;
let { userId } = matrixService;
if (!userId) {
throw new Error(
`bug: there is no userId associated with the matrix client`,
);
}
let server = userId!.split(':')[1];
let aiBotFullId = `@${aiBotUsername}:${server}`;
let { room_id: roomId } = await matrixService.createRoom({
preset: matrixService.privateChatPreset,
invite: [aiBotFullId],
name: input.name,
topic: undefined,
room_alias_name: encodeURIComponent(
`${input.name} - ${format(
new Date(),
"yyyy-MM-dd'T'HH:mm:ss.SSSxxx",
)} - ${userId}`,
),
});
await this.matrixService.setPowerLevel(
roomId,
aiBotFullId,
matrixService.aiBotPowerLevel,
);
await this.matrixService.sendStateEvent(roomId, APP_BOXEL_ACTIVE_LLM, {
model: DEFAULT_LLM,
});
let commandModule = await this.loadCommandModule();
const { CreateAIAssistantRoomResult } = commandModule;
return new CreateAIAssistantRoomResult({ roomId });
}
}