-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathai-command-example.gts
86 lines (69 loc) · 2.3 KB
/
ai-command-example.gts
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
import { on } from '@ember/modifier';
import CreateAiAssistantRoomCommand from '@cardstack/boxel-host/commands/create-ai-assistant-room';
import SendAiAssistantMessageCommand from '@cardstack/boxel-host/commands/send-ai-assistant-message';
import { Button } from '@cardstack/boxel-ui/components';
import { CardContainer } from '@cardstack/boxel-ui/components';
import { Command } from '@cardstack/runtime-common';
import {
CardDef,
Component,
StringField,
field,
contains,
} from 'https://cardstack.com/base/card-api';
export class WeatherLocationInput extends CardDef {
@field location = contains(StringField);
}
export class WeatherReport extends CardDef {
@field temperature = contains(StringField);
@field conditions = contains(StringField);
}
class GetWeatherCommand extends Command<
typeof WeatherLocationInput,
typeof WeatherReport
> {
inputType = WeatherLocationInput;
async getInputType() {
return WeatherLocationInput;
}
protected async run(_input: WeatherLocationInput): Promise<WeatherReport> {
return new WeatherReport({
temperature: '25 C',
conditions: 'Sunny',
});
}
}
export class AiCommandExample extends CardDef {
static displayName = 'AI Command Example';
@field location = contains(StringField);
static isolated = class Isolated extends Component<typeof AiCommandExample> {
getWeather = async () => {
let commandContext = this.args.context?.commandContext;
if (!commandContext) {
throw new Error('No command context found');
}
let getWeatherCommand = new GetWeatherCommand(commandContext);
let createAIAssistantRoomCommand = new CreateAiAssistantRoomCommand(
commandContext,
);
let { roomId } = await createAIAssistantRoomCommand.execute({
name: 'Weather Assistant',
});
let sendMessageCommand = new SendAiAssistantMessageCommand(
commandContext,
);
await sendMessageCommand.execute({
roomId,
prompt: `What is the weather in ${this.args.model.location}?`,
commands: [{ command: getWeatherCommand, autoExecute: true }],
});
};
<template>
<CardContainer>
<Button data-test-get-weather {{on 'click' this.getWeather}}>
Get Weather
</Button>
</CardContainer>
</template>
};
}