forked from PAIR-code/deliberate-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured_prompt_editor.ts
More file actions
566 lines (522 loc) · 17.5 KB
/
structured_prompt_editor.ts
File metadata and controls
566 lines (522 loc) · 17.5 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import '../../pair-components/icon_button';
import '../../pair-components/menu';
import '../../pair-components/textarea';
import '../../pair-components/textarea_template';
import {MobxLitElement} from '@adobe/lit-mobx';
import {CSSResultGroup, html, nothing, TemplateResult} from 'lit';
import {customElement, property} from 'lit/decorators.js';
import {core} from '../../core/core';
import {ExperimentEditor} from '../../services/experiment.editor';
import {renderConditionEditor} from '../../shared/condition_editor.utils';
import {
Condition,
ConditionOperator,
ConditionTarget,
createConditionGroup,
createDefaultPromptItemGroup,
createDefaultStageContextPromptItem,
createShuffleConfig,
getConditionTargetsFromStages,
PromptItem,
PromptItemGroup,
PromptItemType,
SeedStrategy,
ShuffleConfig,
StageContextPromptItem,
StageKind,
TextPromptItem,
} from '@deliberation-lab/utils';
import {styles} from './structured_prompt_editor.scss';
/** Editor for configuring structured prompt. */
@customElement('structured-prompt-editor')
export class EditorComponent extends MobxLitElement {
static override styles: CSSResultGroup = [styles];
private readonly experimentEditor = core.getService(ExperimentEditor);
@property() prompt: PromptItem[] = [];
@property() stageId = '';
@property() onUpdate: (prompt: PromptItem[]) => void = (
prompt: PromptItem[],
) => {};
/** Get condition targets from all survey stages before (and including) the current stage. */
private getConditionTargets() {
return getConditionTargetsFromStages(
this.experimentEditor.stages,
this.stageId,
{includeCurrentStage: true},
);
}
/** Check if the current stage supports conditions (only private chat, not group chat). */
private supportsConditions(): boolean {
const stage = this.experimentEditor.getStage(this.stageId);
return stage?.kind === StageKind.PRIVATE_CHAT;
}
override render() {
return this.renderPromptPreview();
}
private updatePromptItem = (
item: PromptItem,
updates: Partial<PromptItem>,
) => {
Object.assign(item, updates);
this.onUpdate(this.prompt);
};
private addPromptItem(targetArray: PromptItem[], item: PromptItem) {
targetArray.push(item);
this.onUpdate(this.prompt);
}
private deletePromptItem(targetArray: PromptItem[], index: number) {
targetArray.splice(index, 1);
this.onUpdate(this.prompt);
}
private movePromptItem(
targetArray: PromptItem[],
index: number,
direction: number,
) {
const newIndex = index + direction;
if (newIndex >= 0 && newIndex < targetArray.length) {
[targetArray[index], targetArray[newIndex]] = [
targetArray[newIndex],
targetArray[index],
];
this.onUpdate(this.prompt);
}
}
private renderPromptPreview() {
return html`
<div class="prompt-wrapper">
<div class="header">
<div class="title">Prompt editor</div>
${this.renderAddMenu(this.prompt, true)}
</div>
<div class="prompt">${this.renderPromptItems(this.prompt)}</div>
</div>
`;
}
private renderAddMenu(targetArray: PromptItem[], isRoot: boolean) {
const addText = () => {
this.addPromptItem(targetArray, {type: PromptItemType.TEXT, text: ''});
};
const addProfileContext = () => {
this.addPromptItem(targetArray, {type: PromptItemType.PROFILE_CONTEXT});
};
const addProfileInfo = () => {
this.addPromptItem(targetArray, {type: PromptItemType.PROFILE_INFO});
};
const addStageContext = () => {
this.addPromptItem(
targetArray,
createDefaultStageContextPromptItem(this.stageId),
);
};
const addAllStageContext = () => {
this.addPromptItem(targetArray, createDefaultStageContextPromptItem(''));
};
const addGroup = () => {
this.addPromptItem(targetArray, createDefaultPromptItemGroup());
};
return html`
<pr-menu
name="Add item"
icon="add_circle"
color="neutral"
variant="default"
size=${isRoot ? 'medium' : 'small'}
>
<div class="menu-wrapper">
<div class="menu-item" role="button" @click=${addText}>
Freeform text
</div>
<div class="menu-item" role="button" @click=${addStageContext}>
Context from single stage
</div>
<div class="menu-item" role="button" @click=${addAllStageContext}>
Context from all stages before this stage
</div>
<div class="menu-item" role="button" @click=${addProfileContext}>
Custom agent context
</div>
<div class="menu-item" role="button" @click=${addProfileInfo}>
Profile info (avatar, name, pronouns)
</div>
<div class="menu-item" role="button" @click=${addGroup}>
Group of items
</div>
</div>
</pr-menu>
`;
}
private renderItemEditor(item: PromptItem): TemplateResult | typeof nothing {
switch (item.type) {
case PromptItemType.TEXT:
return this.renderTextPromptItemEditor(item as TextPromptItem);
case PromptItemType.STAGE_CONTEXT:
return this.renderStageContextPromptItemEditor(
item as StageContextPromptItem,
);
case PromptItemType.PROFILE_INFO:
return html`
<details>
<summary class="chip tertiary">Profile info</summary>
<div class="chip-collapsible">
Name, avatar, pronouns (if defined)
</div>
</details>
`;
case PromptItemType.PROFILE_CONTEXT:
return html`
<details>
<summary class="chip tertiary">Custom agent context</summary>
<div class="chip-collapsible">
Context string provided when specific agent is created (or empty
string if none)
</div>
</details>
`;
case PromptItemType.GROUP:
return this.renderPromptItemGroupEditor(item as PromptItemGroup);
default:
return nothing;
}
}
private renderPromptItems(items: PromptItem[], isNested = false) {
if (items.length === 0) {
return html`<div
class="${isNested ? 'empty-group' : 'prompt-item-wrapper'}"
>
${isNested ? 'No items in group yet' : '⚠️ No items added yet'}
</div>`;
}
const conditionTargets = this.getConditionTargets();
const supportsConditions =
this.supportsConditions() && conditionTargets.length > 0;
return items.map((item, index) => {
const hasCondition = item.condition !== undefined;
return html`
<div class="prompt-item-wrapper ${isNested ? 'nested' : ''}">
<div class="prompt-item-row">
<div class="prompt-item-editor">${this.renderItemEditor(item)}</div>
<div class="prompt-item-actions">
${supportsConditions && item.type !== PromptItemType.GROUP
? html`
<pr-icon-button
icon="rule"
color=${hasCondition ? 'primary' : 'neutral'}
variant="default"
size="small"
title=${hasCondition
? 'Remove display condition'
: 'Add display condition'}
@click=${() => this.toggleCondition(item)}
>
</pr-icon-button>
`
: nothing}
<pr-icon-button
icon="arrow_upward"
color="neutral"
variant="default"
size="small"
@click=${() => this.movePromptItem(items, index, -1)}
>
</pr-icon-button>
<pr-icon-button
icon="arrow_downward"
color="neutral"
variant="default"
size="small"
@click=${() => this.movePromptItem(items, index, 1)}
>
</pr-icon-button>
<pr-icon-button
icon="close"
color="neutral"
variant="default"
size="small"
@click=${() => this.deletePromptItem(items, index)}
>
</pr-icon-button>
</div>
</div>
${hasCondition
? this.renderPromptItemCondition(item, conditionTargets)
: nothing}
</div>
`;
});
}
private toggleCondition(item: PromptItem) {
if (item.condition !== undefined) {
// Remove condition
this.updatePromptItem(item, {condition: undefined});
} else {
// Add an empty condition group - user will add conditions via the editor
this.updatePromptItem(item, {
condition: createConditionGroup(ConditionOperator.AND, []),
});
}
}
private renderPromptItemCondition(
item: PromptItem,
conditionTargets: ConditionTarget[],
) {
const onConditionChange = (condition: Condition | undefined) => {
this.updatePromptItem(item, {condition});
};
return html`
<div class="prompt-item-condition">
${renderConditionEditor({
condition: item.condition,
targets: conditionTargets,
canEdit: this.experimentEditor.canEditStages,
onConditionChange,
})}
</div>
`;
}
private renderTextPromptItemEditor(item: TextPromptItem) {
const onInput = (e: InputEvent) => {
const text = (e.target as HTMLTextAreaElement).value;
this.updatePromptItem(item, {text: text});
};
return html`
<pr-textarea-template
placeholder="Add freeform text here"
.value=${item.text}
@input=${onInput}
>
</pr-textarea-template>
`;
}
private renderStageContextPromptItemEditor(item: StageContextPromptItem) {
// Get available stages up to and including current stage
const currentStageIndex = this.experimentEditor.stages.findIndex(
(stage) => stage.id === this.stageId,
);
const availableStages =
currentStageIndex >= 0
? this.experimentEditor.stages.slice(0, currentStageIndex + 1)
: [];
// Create a map from stage ID to index for quick lookup
const stageIndexMap = new Map(
availableStages.map((stage, idx) => [stage.id, idx]),
);
const allStagesText =
'Context for all stages before and including this stage';
const getTitle = () => {
if (!item.stageId) {
return allStagesText;
}
const stageIndex = stageIndexMap.get(item.stageId);
if (stageIndex !== undefined) {
const stage = availableStages[stageIndex];
return `Stage context: ${stageIndex + 1}. ${stage.name}`;
}
return 'Stage context';
};
return html`
<details>
<summary class="chip primary">${getTitle()}</summary>
<div class="chip-collapsible secondary">
<div class="stage-selector">
<label>Select stage:</label>
<select
.value=${item.stageId}
@change=${(e: Event) =>
this.updatePromptItem(item, {
stageId: (e.target as HTMLSelectElement).value,
})}
>
${availableStages.map(
(stage, stageIndex) => html`
<option
value=${stage.id}
?selected=${stage.id === item.stageId}
>
${stageIndex + 1}. ${stage.name}
</option>
`,
)}
<option value=${''} ?selected=${!item.stageId}>
${allStagesText}
</option>
</select>
</div>
<label class="checkbox-wrapper">
<input
type="checkbox"
.checked=${item.includePrimaryText}
@change=${() =>
this.updatePromptItem(item, {
includePrimaryText: !item.includePrimaryText,
})}
/>
<div>Include stage description</div>
</label>
<label class="checkbox-wrapper">
<input
type="checkbox"
.checked=${item.includeInfoText}
@change=${() =>
this.updatePromptItem(item, {
includeInfoText: !item.includeInfoText,
})}
/>
<div>Include stage info popup</div>
</label>
<label class="checkbox-wrapper">
<input
type="checkbox"
.checked=${item.includeStageDisplay}
@change=${() =>
this.updatePromptItem(item, {
includeStageDisplay: !item.includeStageDisplay,
})}
/>
<div>
Include stage content (e.g., chat history, survey questions)
</div>
</label>
<label class="checkbox-wrapper">
<input
type="checkbox"
.checked=${item.includeParticipantAnswers}
@change=${() =>
this.updatePromptItem(item, {
includeParticipantAnswers: !item.includeParticipantAnswers,
})}
/>
<div>Include participant stage answers</div>
</label>
</div>
</details>
`;
}
private renderPromptItemGroupEditor(group: PromptItemGroup) {
return html`
<details open>
<summary class="chip secondary">
Group:
<input
type="text"
class="group-title-input"
.value=${group.title}
@input=${(e: Event) =>
this.updatePromptItem(group, {
title: (e.target as HTMLInputElement).value,
})}
@click=${(e: Event) => e.stopPropagation()}
/>
${renderShuffleIndicator(group.shuffleConfig)}
</summary>
<div class="chip-collapsible group-content">
${this.renderShuffleEditor(group)}
<div class="group-header">
${this.renderAddMenu(group.items, false)}
</div>
<div class="group-items">
${this.renderPromptItems(group.items, true)}
</div>
</div>
</details>
`;
}
private renderShuffleEditor(group: PromptItemGroup) {
if (!group.shuffleConfig) return nothing;
const shuffleConfig = group.shuffleConfig;
return html`
<div class="shuffle-config">
<label class="checkbox-wrapper">
<input
type="checkbox"
.checked=${shuffleConfig.shuffle}
@change=${() =>
this.updatePromptItem(group, {
shuffleConfig: createShuffleConfig({
...shuffleConfig,
shuffle: !shuffleConfig.shuffle,
}),
})}
/>
<div>Shuffle items in this group</div>
</label>
${shuffleConfig.shuffle
? html`
<label>using seed:</label>
<select
.value=${shuffleConfig.seed}
@change=${(e: Event) =>
this.updatePromptItem(group, {
shuffleConfig: createShuffleConfig({
...shuffleConfig,
seed: ((e.target as HTMLSelectElement).value ||
'') as SeedStrategy,
}),
})}
>
<option
value=${SeedStrategy.EXPERIMENT}
?selected=${shuffleConfig.seed === SeedStrategy.EXPERIMENT}
>
Experiment ID
</option>
<option
value=${SeedStrategy.COHORT}
?selected=${shuffleConfig.seed === SeedStrategy.COHORT}
>
Cohort ID
</option>
<option
value=${SeedStrategy.PARTICIPANT}
?selected=${shuffleConfig.seed === SeedStrategy.PARTICIPANT}
>
Participant ID
</option>
<option
value=${SeedStrategy.CUSTOM}
?selected=${shuffleConfig.seed === SeedStrategy.CUSTOM}
>
Custom seed
</option>
</select>
${shuffleConfig.seed === SeedStrategy.CUSTOM
? html`
<input
type="text"
placeholder="Enter custom seed"
.value=${shuffleConfig.customSeed}
@input=${(e: Event) =>
this.updatePromptItem(group, {
shuffleConfig: createShuffleConfig({
...shuffleConfig,
customSeed: (e.target as HTMLInputElement).value,
}),
})}
/>
`
: ''}
`
: ''}
</div>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'structured-prompt-editor': EditorComponent;
}
}
/** Helper function to generate shuffle indicator template */
export function renderShuffleIndicator(
shuffleConfig: ShuffleConfig | undefined,
): TemplateResult | typeof nothing {
if (!shuffleConfig?.shuffle) return nothing;
return html`
<span title="Shuffle enabled">
🔀
${shuffleConfig.seed}${shuffleConfig.seed === SeedStrategy.CUSTOM
? `: ${shuffleConfig.customSeed}`
: ''}
</span>
`;
}