Skip to content

Commit ac15077

Browse files
authored
feat(loops): Make builder sessions resumable from the loops page (#3745)
1 parent 0092213 commit ac15077

18 files changed

Lines changed: 553 additions & 19 deletions

apps/code/snapshots.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,10 @@ snapshots:
576576
hash: v1.k4693efd2.384486583f8d9b66a4bf1d9cc45036a07310cb9e1943ea5b52ff466f55a03d7b.F_sXrJ13XWHzAF8yhoKun_k4BY8Jcb3EDtT0L1I6nKw
577577
loops-loopslistview--long-mixed-list--light:
578578
hash: v1.k4693efd2.d80a0c55c4490276df53f781ac88ed9d9c6ec282bc14ddb80140b3ddcc44d32f.L6hw9ZixUiyTFKOybYzmjfUSIOayvmhueMTcmYt-IWo
579+
loops-loopslistview--with-builder-sessions--dark:
580+
hash: v1.k4693efd2.bc59812bf49ff161cf20e75dcf043367ae3d2817aa2c2cf91c91d9771c6b62f8.yOBz-llmwDVay7GIHlMwlMT3EuKjkoW0Vwmwvi4noCM
581+
loops-loopslistview--with-builder-sessions--light:
582+
hash: v1.k4693efd2.fb97e3fe3205ddf71d98c4e35446c73e394aaa9761d3bb35b146ddafc438df4d.UUEWc0G12NQibZTyskIHGS62mB36gKJ594GrJp1E9II
579583
scouts-scoutsfleetlist--filtered-to-you--dark:
580584
hash: v1.k4693efd2.a3af6e76ef36598eaa347bedc4430878ed62754660166398e0576a9978cd084b.x3Ky-O6HOw0srWdOmnnKJwFNpmGmQVWip0t7CwVaRXY
581585
scouts-scoutsfleetlist--filtered-to-you--light:

packages/ui/src/features/agent-applications/components/AgentChatSurface.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArrowUp, Stop } from "@phosphor-icons/react";
1+
import { ArrowUp, StopCircle } from "@phosphor-icons/react";
22
import {
33
InputGroup,
44
InputGroupAddon,
@@ -179,7 +179,7 @@ function Composer({
179179
onClick={onCancel}
180180
aria-label="Stop"
181181
>
182-
<Stop size={14} weight="fill" />
182+
<StopCircle size={14} weight="fill" />
183183
</InputGroupButton>
184184
</Tooltip>
185185
) : (

packages/ui/src/features/autoresearch/AutoresearchPanel.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { ChartLineUp, Pause, Play, Plus, Stop } from "@phosphor-icons/react";
1+
import {
2+
ChartLineUp,
3+
Pause,
4+
Play,
5+
Plus,
6+
StopCircle,
7+
} from "@phosphor-icons/react";
28
import type { AutoresearchService } from "@posthog/core/autoresearch/autoresearch";
39
import { AUTORESEARCH_SERVICE } from "@posthog/core/autoresearch/identifiers";
410
import type {
@@ -344,7 +350,7 @@ function RunHeader({
344350
color="red"
345351
onClick={() => service.stopRun(run.id)}
346352
>
347-
<Stop size={12} /> Stop
353+
<StopCircle size={12} /> Stop
348354
</Button>
349355
)}
350356
{!isLive && (

packages/ui/src/features/inbox/hooks/useInboxCloudTaskRunner.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
defaultEligibleModel,
1717
getCloudUrlFromRegion,
1818
} from "@posthog/shared";
19+
import type { Task } from "@posthog/shared/domain-types";
1920
import { useAuthStateValue } from "@posthog/ui/features/auth/store";
2021
import { showOfflineToast } from "@posthog/ui/features/connectivity/connectivityToast";
2122
import { resolveDefaultModel } from "@posthog/ui/features/inbox/hooks/resolveDefaultModel";
@@ -90,6 +91,8 @@ export interface UseInboxCloudTaskRunnerOptions {
9091
buildInput: (ctx: InboxCloudTaskInputContext) => TaskCreationInput;
9192
/** Telemetry extras merged into the TASK_CREATED event when the run succeeds. */
9293
analyticsExtras?: Record<string, unknown>;
94+
/** Called with the created task record, before any navigation happens. */
95+
onTaskCreated?: (task: Task) => void;
9396
/**
9497
* When false, the runner does not navigate to the created task. The task is
9598
* still added to the sidebar via `invalidateTasks`, and a success toast with a
@@ -120,6 +123,7 @@ export function useInboxCloudTaskRunner({
120123
loggerScope,
121124
buildInput,
122125
analyticsExtras,
126+
onTaskCreated,
123127
redirectOnSuccess = true,
124128
}: UseInboxCloudTaskRunnerOptions): UseInboxCloudTaskRunnerReturn {
125129
const [isRunning, setIsRunning] = useState(false);
@@ -225,6 +229,7 @@ export function useInboxCloudTaskRunner({
225229
const result = await taskService.createTask(input, (output) => {
226230
createdTask = output.task;
227231
invalidateTasks(output.task);
232+
onTaskCreated?.(output.task);
228233
if (redirectOnSuccess) {
229234
void openTask(output.task);
230235
}
@@ -299,6 +304,7 @@ export function useInboxCloudTaskRunner({
299304
buildInput,
300305
copy,
301306
analyticsExtras,
307+
onTaskCreated,
302308
modelResolver,
303309
taskService,
304310
redirectOnSuccess,

packages/ui/src/features/loops/components/LoopRunRow.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
type Icon,
77
Lightning,
88
Play,
9-
Stop,
109
Timer,
1110
Warning,
1211
X,
@@ -188,7 +187,6 @@ export function LoopRunRow({
188187
size="1"
189188
onClick={() => setStopOpen(true)}
190189
>
191-
<Stop size={12} weight="bold" />
192190
Stop run
193191
</Button>
194192
) : null}

packages/ui/src/features/loops/components/LoopsListView.stories.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,24 @@ export const LongMixedList: Story = {
173173
}),
174174
},
175175
};
176+
177+
export const WithBuilderSessions: Story = {
178+
args: {
179+
builderSessions: [
180+
{
181+
taskId: "builder-task-1",
182+
prompt: "Summarize my open PRs every weekday morning",
183+
startedAt: 1752000000000,
184+
identity: "us:2",
185+
},
186+
{
187+
taskId: "builder-task-2",
188+
prompt: "Build a loop",
189+
startedAt: 1752000600000,
190+
identity: "us:2",
191+
},
192+
],
193+
onResumeBuilderSession: () => {},
194+
onBuilderSessionStopped: () => {},
195+
},
196+
};

packages/ui/src/features/loops/components/LoopsListView.tsx

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
import { CloudIcon, PlusIcon, RepeatIcon } from "@phosphor-icons/react";
1+
import {
2+
ChatCircleDotsIcon,
3+
CloudIcon,
4+
PlusIcon,
5+
RepeatIcon,
6+
} from "@phosphor-icons/react";
27
import type { LoopSchemas } from "@posthog/api-client/loops";
38
import type { UserBasic } from "@posthog/shared/domain-types";
49
import { useOrgMembers } from "@posthog/ui/features/canvas/hooks/useOrgMembers";
10+
import { StopCloudRunDialog } from "@posthog/ui/features/sessions/components/StopCloudRunDialog";
511
import { useSetHeaderContent } from "@posthog/ui/hooks/useSetHeaderContent";
612
import { Button } from "@posthog/ui/primitives/Button";
7-
import { navigateToNewLoop } from "@posthog/ui/router/navigationBridge";
13+
import { toast } from "@posthog/ui/primitives/toast";
14+
import {
15+
navigateToNewLoop,
16+
navigateToTaskDetail,
17+
} from "@posthog/ui/router/navigationBridge";
818
import { Flex, Heading, Text } from "@radix-ui/themes";
919
import { useMemo, useState } from "react";
20+
import { useLoopBuilderSessions } from "../hooks/useLoopBuilderSessions";
1021
import { useLoopLimits, useLoops } from "../hooks/useLoops";
22+
import {
23+
type LoopBuilderSession,
24+
useLoopBuilderSessionStore,
25+
} from "../loopBuilderSessionStore";
1126
import { useLoopDraftStore } from "../loopDraftStore";
1227
import type { LoopTemplate } from "../loopTemplates";
1328
import { LoopBuilderComposer } from "./LoopBuilderComposer";
@@ -23,6 +38,7 @@ function loopLimitReason(max: number): string {
2338
}
2439

2540
const EMPTY_MEMBERS: UserBasic[] = [];
41+
const EMPTY_BUILDER_SESSIONS: LoopBuilderSession[] = [];
2642

2743
const SECTION_PREVIEW_COUNT = 5;
2844

@@ -31,6 +47,14 @@ function startBlankLoop(): void {
3147
navigateToNewLoop();
3248
}
3349

50+
function resumeBuilderSession(taskId: string): void {
51+
navigateToTaskDetail(taskId);
52+
}
53+
54+
function removeBuilderSession(taskId: string): void {
55+
useLoopBuilderSessionStore.getState().removeSession(taskId);
56+
}
57+
3458
function startLoopFromTemplate(template: LoopTemplate): void {
3559
useLoopDraftStore
3660
.getState()
@@ -60,6 +84,8 @@ export function LoopsListView() {
6084
);
6185
useSetHeaderContent(headerContent);
6286

87+
const builderSessions = useLoopBuilderSessions();
88+
6389
const allLoops = loops ?? [];
6490
const teamLoops = allLoops.filter((loop) => loop.visibility === "team");
6591
const {
@@ -79,8 +105,11 @@ export function LoopsListView() {
79105
membersLoading={membersLoading}
80106
membersError={membersError}
81107
membersComplete={membersComplete}
108+
builderSessions={builderSessions}
82109
onStartBlank={startBlankLoop}
83110
onStartFromTemplate={startLoopFromTemplate}
111+
onResumeBuilderSession={resumeBuilderSession}
112+
onBuilderSessionStopped={removeBuilderSession}
84113
/>
85114
);
86115
}
@@ -94,8 +123,11 @@ interface LoopsListViewPresentationProps {
94123
membersLoading?: boolean;
95124
membersError?: boolean;
96125
membersComplete?: boolean;
126+
builderSessions?: LoopBuilderSession[];
97127
onStartBlank: () => void;
98128
onStartFromTemplate: (template: LoopTemplate) => void;
129+
onResumeBuilderSession?: (taskId: string) => void;
130+
onBuilderSessionStopped?: (taskId: string) => void;
99131
}
100132

101133
export function LoopsListViewPresentation({
@@ -107,8 +139,11 @@ export function LoopsListViewPresentation({
107139
membersLoading = false,
108140
membersError = false,
109141
membersComplete = true,
142+
builderSessions = EMPTY_BUILDER_SESSIONS,
110143
onStartBlank,
111144
onStartFromTemplate,
145+
onResumeBuilderSession,
146+
onBuilderSessionStopped,
112147
}: LoopsListViewPresentationProps) {
113148
const personalLoops = loops.filter((loop) => loop.visibility === "personal");
114149
const teamLoops = loops.filter((loop) => loop.visibility === "team");
@@ -199,13 +234,79 @@ export function LoopsListViewPresentation({
199234
gap="2"
200235
className="mx-auto w-full max-w-5xl px-8 pb-6"
201236
>
237+
{builderSessions.map((session) => (
238+
<BuilderSessionRow
239+
key={session.taskId}
240+
session={session}
241+
onResume={onResumeBuilderSession}
242+
onStopped={onBuilderSessionStopped}
243+
/>
244+
))}
202245
<LoopBuilderComposer disabledReason={limitReason} />
203246
</Flex>
204247
</div>
205248
</Flex>
206249
);
207250
}
208251

252+
function BuilderSessionRow({
253+
session,
254+
onResume,
255+
onStopped,
256+
}: {
257+
session: LoopBuilderSession;
258+
onResume?: (taskId: string) => void;
259+
onStopped?: (taskId: string) => void;
260+
}) {
261+
const [confirmStop, setConfirmStop] = useState(false);
262+
263+
return (
264+
<Flex
265+
align="center"
266+
gap="3"
267+
className="rounded-(--radius-2) border border-border bg-(--color-panel-solid) px-3 py-2"
268+
>
269+
<ChatCircleDotsIcon size={16} className="shrink-0 text-(--accent-11)" />
270+
<Flex direction="column" className="min-w-0 flex-1">
271+
<Text className="font-medium text-[12px] text-gray-10 uppercase tracking-wide">
272+
Builder in progress
273+
</Text>
274+
<Text className="truncate text-[13px] text-gray-12">
275+
{session.prompt}
276+
</Text>
277+
</Flex>
278+
<Button
279+
variant="soft"
280+
color="red"
281+
size="1"
282+
onClick={() => setConfirmStop(true)}
283+
>
284+
Stop
285+
</Button>
286+
<Button
287+
variant="soft"
288+
size="1"
289+
onClick={() => onResume?.(session.taskId)}
290+
>
291+
Resume
292+
</Button>
293+
{confirmStop ? (
294+
<StopCloudRunDialog
295+
open={confirmStop}
296+
taskId={session.taskId}
297+
title="Stop loop builder"
298+
buttonLabel="Stop builder"
299+
onOpenChange={setConfirmStop}
300+
onStopped={() => {
301+
toast.success("Builder stopped");
302+
onStopped?.(session.taskId);
303+
}}
304+
/>
305+
) : null}
306+
</Flex>
307+
);
308+
}
309+
209310
function LoopListSection({
210311
title,
211312
loops,

0 commit comments

Comments
 (0)