-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathLoopForm.tsx
More file actions
593 lines (559 loc) · 19.1 KB
/
Copy pathLoopForm.tsx
File metadata and controls
593 lines (559 loc) · 19.1 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import {
ArrowLeft,
ArrowRight,
CaretRight,
Check,
} from "@phosphor-icons/react";
import { type LoopSchemas, LoopsApiError } from "@posthog/api-client/loops";
import { SettingsOptionSelect } from "@posthog/ui/features/settings/SettingsOptionSelect";
import { useSetHeaderContent } from "@posthog/ui/hooks/useSetHeaderContent";
import { Button } from "@posthog/ui/primitives/Button";
import { toast } from "@posthog/ui/primitives/toast";
import {
navigateToLoopDetail,
navigateToLoops,
} from "@posthog/ui/router/navigationBridge";
import { Box, Flex, Text, TextArea, TextField } from "@radix-ui/themes";
import { type ReactNode, useEffect, useState } from "react";
import { useAuthStateValue } from "../../auth/store";
import { useCreateLoop, useUpdateLoop } from "../hooks/useLoopMutations";
import { summarizeTrigger } from "../loopDisplay";
import { useLoopDraftStore } from "../loopDraftStore";
import {
emptyLoopFormValues,
formValuesToLoopWrite,
isAutoFixEnabled,
isLoopFormValid,
isTriggerDraftValid,
type LoopContextTargetDraft,
type LoopFormValues,
loopToFormValues,
normalizeLoopFormValues,
} from "../loopFormTypes";
import { loopEffectiveModel } from "../loopModelDefaults";
import { LoopBehaviorFields } from "./LoopBehaviorFields";
import { LoopContextFields } from "./LoopContextFields";
import { Field } from "./LoopFormPrimitives";
import { LoopModelFields } from "./LoopModelFields";
import { LoopNotificationsFields } from "./LoopNotificationsFields";
import { LoopRepositoryPicker } from "./LoopRepositoryPicker";
import { LoopTriggerEditor } from "./LoopTriggerEditor";
const VISIBILITY_OPTIONS: {
value: LoopSchemas.LoopVisibilityEnum;
label: string;
}[] = [
{ value: "personal", label: "Personal (only you)" },
{ value: "team", label: "Team (everyone on the project)" },
];
const ADAPTER_LABELS: Record<LoopSchemas.LoopRuntimeAdapterEnum, string> = {
claude: "Claude Code",
codex: "Codex",
};
const STEPS = ["Prompt", "When", "Options", "Review"] as const;
interface LoopFormProps {
/** Present in edit mode; absent when creating a new loop. */
loop?: LoopSchemas.Loop;
}
export function LoopForm({ loop }: LoopFormProps) {
const isEdit = !!loop;
const projectId = useAuthStateValue((state) => state.currentProjectId);
const [values, setValues] = useState<LoopFormValues>(() => {
if (loop) return normalizeLoopFormValues(loopToFormValues(loop));
// One-shot prefill from the landing prompt or a template; merged over the
// blank defaults. Read (not consumed) here, then cleared in the effect
// below so the manual "New loop" button always opens a blank form.
const prefill = useLoopDraftStore.getState().prefill;
return normalizeLoopFormValues({
...emptyLoopFormValues(),
...(prefill ?? {}),
});
});
const [step, setStep] = useState(0);
// Open when editing a loop that already pins a model, so the pinned value
// is visible without hunting for it.
const [showAdvanced, setShowAdvanced] = useState(
() => !!(loop && (loop.model || loop.reasoning_effort)),
);
useEffect(() => {
if (!loop) useLoopDraftStore.getState().setPrefill(null);
}, [loop]);
const createLoop = useCreateLoop();
const updateLoop = useUpdateLoop(loop?.id ?? "");
const isSubmitting = isEdit ? updateLoop.isPending : createLoop.isPending;
const canSubmit = isLoopFormValid(values) && !isSubmitting;
// Per-step gate for the Next button. The final Create button is gated on the
// whole form being valid, so jumping between steps can't submit a bad loop.
const stepComplete = [
!!values.name.trim() && !!values.instructions.trim(),
values.triggers.every(isTriggerDraftValid),
true,
isLoopFormValid(values),
];
const isLastStep = step === STEPS.length - 1;
useSetHeaderContent(
<Text className="font-medium text-[13px]">
{isEdit ? `Edit ${loop.name}` : "New loop"}
</Text>,
);
const triggerEndpointPath =
isEdit && projectId != null
? `/api/projects/${projectId}/loops/${loop.id}/trigger/`
: null;
const patch = (next: Partial<LoopFormValues>) =>
setValues((prev) => ({ ...prev, ...next }));
const handleCancel = () => {
if (isEdit) {
navigateToLoopDetail(loop.id);
} else {
navigateToLoops();
}
};
const handleSubmit = async () => {
if (!canSubmit) return;
const body = formValuesToLoopWrite(values);
try {
if (isEdit) {
const updated = await updateLoop.mutateAsync(body);
navigateToLoopDetail(updated.id);
} else {
const created = await createLoop.mutateAsync(body);
navigateToLoopDetail(created.id);
}
} catch (error) {
const safetyLimit =
error instanceof LoopsApiError ? error.safetyLimit : null;
if (safetyLimit) {
// A safety/abuse ceiling, not a normal failure: tell the user plainly so they can
// course-correct (delete a loop, remove triggers) or contact support for a raise.
toast.error("Safety limit reached", {
description: safetyLimit.detail,
});
return;
}
toast.error(isEdit ? "Failed to save loop" : "Failed to create loop", {
description:
error instanceof LoopsApiError
? (error.detail ?? error.message)
: error instanceof Error
? error.message
: undefined,
});
}
};
return (
<Box className="flex h-full items-center justify-center p-6">
<Flex
direction="column"
className="max-h-full w-full max-w-[600px] overflow-hidden rounded-(--radius-3) border border-border bg-(--color-panel-solid) shadow-xl"
>
<Box className="border-border border-b px-5 pt-5 pb-4">
<Stepper current={step} complete={stepComplete} onSelect={setStep} />
</Box>
<Box className="min-h-0 flex-1 overflow-y-auto px-5 py-5">
{step === 0 ? (
<Step
title="What should this loop do?"
description="Name it and write the prompt the agent runs on every fire."
>
<Field label="Name" required>
<TextField.Root
size="2"
value={values.name}
placeholder="Daily standup summary"
disabled={isSubmitting}
autoFocus
onChange={(e) => patch({ name: e.target.value })}
/>
</Field>
<Field label="Description">
<TextField.Root
size="2"
value={values.description}
placeholder="A short summary shown on the Loops list"
disabled={isSubmitting}
onChange={(e) => patch({ description: e.target.value })}
/>
</Field>
<Field label="Instructions" required>
<TextArea
value={values.instructions}
placeholder="Summarize failing CI runs from the last 24 hours and post the summary to #eng-standup."
disabled={isSubmitting}
className="min-h-[220px] text-[13px] leading-relaxed"
onChange={(e) => patch({ instructions: e.target.value })}
/>
</Field>
</Step>
) : null}
{step === 1 ? (
<Step
title="When should it run?"
description="Add one or more triggers. Leave empty to run it only on demand."
>
<LoopTriggerEditor
triggers={values.triggers}
triggerEndpointPath={triggerEndpointPath}
disabled={isSubmitting}
onChange={(triggers) => patch({ triggers })}
/>
</Step>
) : null}
{step === 2 ? (
<Step
title="Options"
description="Who can see it and how you hear about runs."
>
<Field
label="Visibility"
className="max-w-[340px]"
hint={
values.contextTarget
? "Loops attached to a context post runs to its shared feed, so they're visible to everyone on the project."
: undefined
}
>
<SettingsOptionSelect
value={values.visibility}
options={VISIBILITY_OPTIONS}
disabled={isSubmitting || !!values.contextTarget}
ariaLabel="Visibility"
onValueChange={(value) =>
patch({
visibility: value as LoopSchemas.LoopVisibilityEnum,
})
}
/>
</Field>
<Divider />
<Field
label="Context"
hint="Attach this loop to a context to file its runs in the feed and keep its context.md or a canvas up to date."
>
<LoopContextFields
value={values.contextTarget}
disabled={isSubmitting}
onChange={(contextTarget) =>
patch(
contextTarget
? { contextTarget, visibility: "team" }
: { contextTarget },
)
}
/>
</Field>
<Divider />
<Field
label="Repository"
hint={
values.repositories.length > 1
? `${values.repositories.length - 1} more ${
values.repositories.length === 2
? "repository stays"
: "repositories stay"
} attached to this loop.`
: "Optional. Leave empty for a report-only loop that works purely through connectors."
}
>
<LoopRepositoryPicker
value={values.repositories[0] ?? null}
disabled={isSubmitting}
onChange={(repository) =>
setValues((prev) => ({
...prev,
repositories: repository
? [repository, ...prev.repositories.slice(1)]
: prev.repositories.slice(1),
}))
}
/>
</Field>
<Divider />
<Field label="Notifications">
<LoopNotificationsFields
notifications={values.notifications}
disabled={isSubmitting}
onChange={(notifications) => patch({ notifications })}
/>
</Field>
<Divider />
<Flex direction="column" gap="4">
<button
type="button"
onClick={() => setShowAdvanced((open) => !open)}
className="flex items-center gap-1.5 text-left"
>
<CaretRight
size={12}
className={`text-gray-10 transition-transform ${
showAdvanced ? "rotate-90" : ""
}`}
/>
<Text className="font-medium text-[12.5px] text-gray-11">
Advanced
</Text>
<Text className="text-[11.5px] text-gray-9">
Behavior, model and reasoning
</Text>
</button>
{showAdvanced ? (
<Flex direction="column" gap="4">
<Field label="Behavior">
<LoopBehaviorFields
behaviors={values.behaviors}
disabled={isSubmitting}
onChange={(behaviors) => patch({ behaviors })}
/>
</Field>
<LoopModelFields
adapter={values.runtimeAdapter}
model={values.model}
reasoningEffort={values.reasoningEffort}
disabled={isSubmitting}
onAdapterChange={(runtimeAdapter) =>
patch({ runtimeAdapter })
}
onModelChange={(model) => patch({ model })}
onReasoningEffortChange={(reasoningEffort) =>
patch({ reasoningEffort })
}
/>
</Flex>
) : null}
</Flex>
</Step>
) : null}
{step === 3 ? (
<Step
title="Review"
description="Check everything before you create the loop."
>
<ReviewList values={values} />
</Step>
) : null}
</Box>
<Flex
align="center"
justify="between"
gap="3"
className="border-border border-t px-5 py-3"
>
<Button
variant="soft"
color="gray"
size="2"
disabled={isSubmitting}
onClick={handleCancel}
>
Cancel
</Button>
<Flex gap="2" className="shrink-0">
{step > 0 ? (
<Button
variant="outline"
color="gray"
size="2"
disabled={isSubmitting}
onClick={() => setStep((s) => s - 1)}
>
<ArrowLeft size={13} />
Back
</Button>
) : null}
{isLastStep ? (
<Button
variant="solid"
size="2"
loading={isSubmitting}
disabled={!canSubmit}
onClick={() => void handleSubmit()}
>
{isEdit ? "Save changes" : "Create loop"}
</Button>
) : (
<Button
variant="solid"
size="2"
disabled={!stepComplete[step] || isSubmitting}
onClick={() => setStep((s) => s + 1)}
>
Next
<ArrowRight size={13} />
</Button>
)}
</Flex>
</Flex>
</Flex>
</Box>
);
}
function Stepper({
current,
complete,
onSelect,
}: {
current: number;
complete: boolean[];
onSelect: (step: number) => void;
}) {
return (
<Flex align="center" gap="0">
{STEPS.map((label, index) => {
const isCurrent = index === current;
const isDone = index < current && complete[index];
// Steps navigate freely in both directions; skipping ahead is safe
// because Next stays gated per step and Create on the whole form.
return (
<Flex
key={label}
align="center"
className="min-w-0 flex-1 last:flex-none"
>
<button
type="button"
onClick={() => onSelect(index)}
className="flex min-w-0 cursor-pointer items-center gap-2"
>
<Flex
align="center"
justify="center"
className={`size-5 shrink-0 rounded-full border font-medium text-[11px] ${
isCurrent
? "border-(--accent-9) bg-(--accent-9) text-(--accent-contrast)"
: isDone
? "border-(--accent-7) bg-(--accent-3) text-(--accent-11)"
: "border-(--gray-7) text-gray-11"
}`}
>
{isDone ? <Check size={12} weight="bold" /> : index + 1}
</Flex>
<Text
className={`truncate text-[12.5px] ${
isCurrent ? "font-medium text-gray-12" : "text-gray-11"
}`}
>
{label}
</Text>
</button>
{index < STEPS.length - 1 ? (
<Box className="mx-2 h-px min-w-4 flex-1 bg-(--gray-5)" />
) : null}
</Flex>
);
})}
</Flex>
);
}
function Step({
title,
description,
children,
}: {
title: string;
description: string;
children: ReactNode;
}) {
return (
<Flex direction="column" gap="4">
<Flex direction="column" gap="1">
<Text className="font-medium text-[15px] text-gray-12">{title}</Text>
<Text className="text-[12.5px] text-gray-10 leading-snug">
{description}
</Text>
</Flex>
{children}
</Flex>
);
}
function Divider() {
return <Box className="h-px bg-(--gray-4)" />;
}
function ReviewList({ values }: { values: LoopFormValues }) {
const reasoning = values.reasoningEffort ?? "auto";
const channels = (["push", "email", "slack"] as const).filter(
(channel) => values.notifications[channel]?.enabled,
);
return (
<Flex
direction="column"
className="divide-y divide-(--gray-4) rounded-(--radius-3) border border-border"
>
<ReviewRow label="Name" value={values.name || "Not set"} />
<ReviewRow
label="Visibility"
value={values.visibility === "team" ? "Team" : "Personal"}
/>
<ReviewRow
label="Prompt"
value={values.instructions.trim() || "No prompt"}
multiline
/>
<ReviewRow
label="Model"
value={`${ADAPTER_LABELS[values.runtimeAdapter]} · ${
values.model ||
`${loopEffectiveModel(values.runtimeAdapter, "")} (default)`
} · ${reasoning} reasoning`}
/>
<ReviewRow
label="Context"
value={describeContext(values.contextTarget)}
/>
<ReviewRow
label="Repository"
value={
values.repositories.length > 0
? values.repositories.map((repo) => repo.full_name).join(", ")
: "None (report-only)"
}
/>
<ReviewRow
label="Triggers"
value={
values.triggers.length === 0
? "Manual only"
: values.triggers.map(summarizeTrigger).join(", ")
}
/>
<ReviewRow
label="Auto-fix PRs"
value={isAutoFixEnabled(values.behaviors) ? "On" : "Off"}
/>
<ReviewRow
label="Notifications"
value={channels.length === 0 ? "None" : channels.join(", ")}
/>
</Flex>
);
}
function ReviewRow({
label,
value,
multiline,
}: {
label: string;
value: string;
multiline?: boolean;
}) {
return (
<Flex gap="4" className="px-3 py-2.5">
<Text className="w-24 shrink-0 text-[12px] text-gray-10">{label}</Text>
<Text
className={`min-w-0 flex-1 text-[12.5px] text-gray-12 ${
multiline ? "whitespace-pre-wrap" : "truncate"
}`}
>
{value}
</Text>
</Flex>
);
}
function describeContext(target: LoopContextTargetDraft | null): string {
if (!target) return "Not attached";
const outputs: string[] = [];
if (target.outputs.post_to_feed) outputs.push("feed");
if (target.outputs.update_context) outputs.push("context.md");
if (target.outputs.canvas_id) outputs.push("canvas");
return outputs.length > 0
? `#${target.name} (${outputs.join(", ")})`
: `#${target.name}`;
}