-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1909 from cardstack/cs-7642-implement-host-comman…
…d-to-create-room Extract commands for CreateAiAssistantRoom and AddSkillsToRoom
- Loading branch information
Showing
33 changed files
with
586 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { service } from '@ember/service'; | ||
|
||
import type * as BaseCommandModule from 'https://cardstack.com/base/command'; | ||
|
||
import HostBaseCommand from '../lib/host-base-command'; | ||
|
||
import { SKILLS_STATE_EVENT_TYPE } from '../services/matrix-service'; | ||
|
||
import type MatrixService from '../services/matrix-service'; | ||
|
||
export default class AddSkillsToRoomCommand extends HostBaseCommand< | ||
BaseCommandModule.AddSkillsToRoomInput, | ||
undefined | ||
> { | ||
@service private declare matrixService: MatrixService; | ||
|
||
async getInputType() { | ||
let commandModule = await this.loadCommandModule(); | ||
const { AddSkillsToRoomInput } = commandModule; | ||
return AddSkillsToRoomInput; | ||
} | ||
|
||
protected async run( | ||
input: BaseCommandModule.AddSkillsToRoomInput, | ||
): Promise<undefined> { | ||
let { matrixService } = this; | ||
let { roomId, skills } = input; | ||
let roomSkillEventIds = await matrixService.addSkillCardsToRoomHistory( | ||
skills, | ||
roomId, | ||
{ includeComputeds: true, maybeRelativeURL: null }, | ||
); | ||
await matrixService.updateStateEvent( | ||
roomId, | ||
SKILLS_STATE_EVENT_TYPE, | ||
'', | ||
async (oldContent: Record<string, any>) => { | ||
return { | ||
enabledEventIds: [ | ||
...new Set([ | ||
...(oldContent.enabledEventIds || []), | ||
...roomSkillEventIds, | ||
]), | ||
], | ||
disabledEventIds: [...(oldContent.disabledEventIds || [])], | ||
}; | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { service } from '@ember/service'; | ||
|
||
import format from 'date-fns/format'; | ||
|
||
import { aiBotUsername } from '@cardstack/runtime-common'; | ||
|
||
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< | ||
BaseCommandModule.CreateAIAssistantRoomInput, | ||
BaseCommandModule.CreateAIAssistantRoomResult | ||
> { | ||
@service private declare 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, | ||
); | ||
let commandModule = await this.loadCommandModule(); | ||
const { CreateAIAssistantRoomResult } = commandModule; | ||
return new CreateAIAssistantRoomResult({ roomId }); | ||
} | ||
} |
Oops, something went wrong.