-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathproduct-requirement-document.gts
385 lines (366 loc) · 11.7 KB
/
product-requirement-document.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import { Base64ImageField } from 'https://cardstack.com/base/base64-image';
import TextAreaField from 'https://cardstack.com/base/text-area';
import MarkdownField from 'https://cardstack.com/base/markdown';
import {
CardDef,
field,
contains,
StringField,
Component,
realmURL,
} from 'https://cardstack.com/base/card-api';
import { Button } from '@cardstack/boxel-ui/components';
import { ImagePlaceholder } from '@cardstack/boxel-ui/icons';
import { bool, cn, not } from '@cardstack/boxel-ui/helpers';
import { on } from '@ember/modifier'; // TODO: why is this a type error???
import { tracked } from '@glimmer/tracking';
import { AppCard } from './app-card';
import ClipboardListIcon from '@cardstack/boxel-icons/clipboard-list';
import CreateAiAssistantRoomCommand from '@cardstack/boxel-host/commands/create-ai-assistant-room';
import ShowCardCommand from '@cardstack/boxel-host/commands/show-card';
import SaveCardCommand from '@cardstack/boxel-host/commands/save-card';
import WriteTextFileCommand from '@cardstack/boxel-host/commands/write-text-file';
import GenerateCodeCommand from './AiAppGenerator/generate-code-command';
import { restartableTask } from 'ember-concurrency';
class Isolated extends Component<typeof ProductRequirementDocument> {
<template>
<section class='prd'>
<header>
<div class='header-button-group'>
<div class='title-group'>
<div
class={{cn
'app-icon-container'
placeholder=(not @model.thumbnail.base64)
}}
>
{{#if @model.thumbnail.base64}}
<@fields.thumbnail />
{{else}}
<ImagePlaceholder
class='icon-placeholder'
width='50'
height='50'
role='presentation'
/>
{{/if}}
</div>
<h1><@fields.title /></h1>
</div>
{{#if @model.moduleURL}}
<Button
{{on 'click' this.reset}}
class='generate-button'
@kind='primary-dark'
>
Reset
</Button>
{{else}}
<Button
{{on 'click' this.generateApp}}
class='generate-button'
@kind='primary-dark'
@disabled={{this._generateCode.isRunning}}
@loading={{this._generateCode.isRunning}}
data-test-generate-app
>
{{#unless this._generateCode.isRunning}}
<span class='generate-button-logo' />
{{/unless}}
Generate App Now
</Button>
{{/if}}
</div>
{{#if this.errorMessage}}
<p class='error'>{{this.errorMessage}}</p>
{{/if}}
<p class='description'><@fields.description /></p>
</header>
<div class='content'>
{{#if @model.moduleURL}}
<details open={{bool @model.moduleURL}}>
<summary><span>Module</span></summary>
<div class='details-content'>
{{#if @model.moduleURL}}
<Button
{{on 'click' this.viewModule}}
class='view-module-button'
@kind='text-only'
data-test-view-module
>
<@fields.moduleURL />
</Button>
{{/if}}
</div>
</details>
{{/if}}
{{#if @model.moduleURL}}
<details>
<summary><span>App</span></summary>
<div class='details-content'>
<Button
{{on 'click' this.createInstance}}
class='generate-button new-instance-button'
@kind='primary-dark'
@disabled={{this._createInstance.isRunning}}
@loading={{this._createInstance.isRunning}}
data-test-create-instance
>
{{#unless this._createInstance.isRunning}}
<span class='generate-button-logo' />
{{/unless}}
Create New Instance
</Button>
</div>
</details>
{{/if}}
<details open={{bool @model.overview}}>
<summary><span>Overview</span></summary>
<div class='details-content'>
<@fields.overview />
</div>
</details>
<details open={{bool @model.schema}}>
<summary><span>Schema</span></summary>
<div class='details-content'>
<@fields.schema />
</div>
</details>
</div>
</section>
<style scoped>
.prd {
padding: var(--boxel-sp) var(--boxel-sp-xxl);
}
.title-group {
display: flex;
align-items: center;
gap: var(--boxel-sp);
}
.header-button-group {
display: flex;
justify-content: space-between;
align-items: center;
gap: var(--boxel-sp);
}
.generate-button {
--icon-size: 20px;
--boxel-button-loading-icon-size: var(--icon-size);
padding: var(--boxel-sp-xxs) var(--boxel-sp);
justify-self: end;
gap: var(--boxel-sp-sm);
white-space: normal;
}
.generate-button :deep(svg) {
width: var(--icon-size);
height: var(--icon-size);
}
.generate-button :deep(.loading-indicator) {
width: var(--icon-size);
height: var(--icon-size);
margin-right: 0;
}
.generate-button-logo {
flex-shrink: 0;
display: inline-block;
width: var(--icon-size);
height: var(--icon-size);
background: url('./ai-assist-icon@2x.webp') no-repeat center;
background-size: contain;
}
.new-instance-button {
margin-top: var(--boxel-sp);
}
.app-icon-container {
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
width: 80px;
height: 80px;
border: 1px solid var(--boxel-200);
border-radius: var(--boxel-border-radius-xl);
}
.placeholder {
background-color: var(--boxel-200);
}
.icon-placeholder {
--icon-color: #212121;
}
h1 {
margin: 0;
font-weight: 600;
font-size: 1.5rem;
letter-spacing: var(--boxel-lsp-xs);
}
details {
margin-top: var(--boxel-sp-sm);
padding-top: var(--boxel-sp-sm);
border-top: 1px solid var(--boxel-200);
}
summary {
margin: 0;
font: 700 var(--boxel-font);
letter-spacing: var(--boxel-lsp-xs);
}
summary:hover {
cursor: pointer;
}
summary > span {
display: inline-block;
margin-left: var(--boxel-sp-xxxs);
}
.details-content {
margin-top: var(--boxel-sp);
}
.description {
margin-top: var(--boxel-sp-sm);
font: 500 var(--boxel-font);
letter-spacing: var(--boxel-lsp-xs);
}
.content {
margin-top: var(--boxel-sp-lg);
}
.error {
color: var(--boxel-danger);
font-weight: 600;
}
</style>
</template>
@tracked errorMessage = '';
get currentRealm() {
return this.args.model[realmURL];
}
generateApp = () => {
this._generateCode.perform();
};
createInstance = () => {
this._createInstance.perform();
};
private _generateCode = restartableTask(async () => {
let commandContext = this.args.context?.commandContext;
if (!commandContext) {
throw new Error('Missing commandContext');
}
this.errorMessage = '';
try {
let createRoomCommand = new CreateAiAssistantRoomCommand(commandContext);
let { roomId } = await createRoomCommand.execute({
name: 'AI Assistant Room',
});
let generateCodeCommand = new GenerateCodeCommand(commandContext);
let { code, appName } = await generateCodeCommand.execute({
productRequirements: this.args.model as ProductRequirementDocument,
roomId,
});
// Generate a unique name for the module using timestamp
let timestamp = Date.now();
let moduleName = `generated-apps/${timestamp}/${appName}`;
let filePath = `${moduleName}.gts`;
let moduleId = new URL(moduleName, this.currentRealm).href;
let writeFileCommand = new WriteTextFileCommand(commandContext);
await writeFileCommand.execute({
path: filePath,
content: code,
realm: this.currentRealm?.href,
});
this.args.model.moduleURL = moduleId;
} catch (e) {
console.error(e);
this.errorMessage =
e instanceof Error ? `Error: ${e.message}` : 'An error has occurred';
}
});
private _createInstance = restartableTask(async () => {
let commandContext = this.args.context?.commandContext;
if (!commandContext) {
throw new Error('Missing commandContext');
}
this.errorMessage = '';
try {
if (!this.currentRealm) {
throw new Error('Realm URL is not available');
}
if (!this.args.context?.actions?.createCard) {
throw new Error('Context action "createCard" is not available');
}
if (!this.args.model.moduleURL) {
throw new Error('Module URL is not available');
}
let { moduleURL } = this.args.model;
// get the app card def from the module
let loader = (import.meta as any).loader;
let module = await loader.import(moduleURL);
let MyAppCard = Object.values(module).find(
(declaration) =>
declaration &&
typeof declaration === 'function' &&
'isCardDef' in declaration &&
AppCard.isPrototypeOf(declaration),
) as typeof AppCard;
if (!MyAppCard) {
throw new Error('App definition not found');
}
let myAppCard = new MyAppCard({
moduleId: moduleURL,
});
let saveCardCommand = new SaveCardCommand(commandContext);
await saveCardCommand.execute({
realm: this.currentRealm.href,
card: myAppCard,
});
// show the app card
let showCardCommand = new ShowCardCommand(commandContext);
await showCardCommand.execute({
cardToShow: myAppCard,
});
if (!myAppCard) {
throw new Error('Could not create card');
}
} catch (e) {
console.error(e);
this.errorMessage =
e instanceof Error ? `Error: ${e.message}` : 'An error has occurred';
}
});
viewModule = () => {
this.errorMessage = '';
if (!this.args.model.moduleURL) {
this.errorMessage = 'Module url is not available';
return;
}
if (!this.args.context?.actions?.changeSubmode) {
this.errorMessage =
'Unable to view module. Context action "changeSubmode" is not available';
return;
}
this.args.context.actions.changeSubmode(
new URL(this.args.model.moduleURL),
'code',
);
};
reset = () => {
this.args.model.moduleURL = undefined;
};
}
export class ProductRequirementDocument extends CardDef {
static displayName = 'Product Requirements';
static icon = ClipboardListIcon;
@field appTitle = contains(StringField);
@field shortDescription = contains(TextAreaField);
@field thumbnail = contains(Base64ImageField);
@field overview = contains(MarkdownField);
@field schema = contains(MarkdownField);
@field moduleURL = contains(StringField);
@field title = contains(StringField, {
computeVia: function (this: ProductRequirementDocument) {
return this.appTitle ?? 'Untitled App';
},
});
@field description = contains(StringField, {
computeVia: function (this: ProductRequirementDocument) {
return this.shortDescription;
},
});
static isolated = Isolated;
}