|
| 1 | +import type { TaskCreationInput } from "@posthog/core/task-detail/taskService"; |
| 2 | +import { |
| 3 | + type InboxCloudTaskInputContext, |
| 4 | + useInboxCloudTaskRunner, |
| 5 | +} from "@posthog/ui/features/inbox/hooks/useInboxCloudTaskRunner"; |
| 6 | +import { useUserRepositoryIntegration } from "@posthog/ui/features/integrations/useIntegrations"; |
| 7 | +import { |
| 8 | + resolveDefaultCloudRepository, |
| 9 | + useSettingsStore, |
| 10 | +} from "@posthog/ui/features/settings/settingsStore"; |
| 11 | +import { useCallback, useMemo, useRef } from "react"; |
| 12 | +import { buildLoopBuilderPrompt } from "../loopBuilderPrompt"; |
| 13 | + |
| 14 | +interface UseLoopBuilderTaskReturn { |
| 15 | + /** Start an auto-mode cloud session that builds a loop from `instructions` and navigate to it. */ |
| 16 | + runTask: (instructions: string) => Promise<void>; |
| 17 | + /** True while the session is being created. */ |
| 18 | + isRunning: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * The loops prompt box: start a cloud sandbox agent whose job is to build a Loop |
| 23 | + * with the user (ask clarifying questions, confirm, then create it via the PostHog |
| 24 | + * MCP `loops-create` tool). Mirrors `useScoutChatTask` — a repo-less, auto-mode |
| 25 | + * cloud task seeded with a canned instruction prompt. The user's typed text rides |
| 26 | + * in through a ref so the fixed `buildInput` closure reads the latest submission. |
| 27 | + */ |
| 28 | +export function useLoopBuilderTask(): UseLoopBuilderTaskReturn { |
| 29 | + const instructionsRef = useRef(""); |
| 30 | + const { repositories } = useUserRepositoryIntegration(); |
| 31 | + const lastUsedCloudRepository = useSettingsStore( |
| 32 | + (state) => state.lastUsedCloudRepository, |
| 33 | + ); |
| 34 | + |
| 35 | + const cloudRepository = useMemo( |
| 36 | + () => resolveDefaultCloudRepository(repositories, lastUsedCloudRepository), |
| 37 | + [lastUsedCloudRepository, repositories], |
| 38 | + ); |
| 39 | + |
| 40 | + const buildInput = useCallback( |
| 41 | + (ctx: InboxCloudTaskInputContext): TaskCreationInput => { |
| 42 | + const prompt = buildLoopBuilderPrompt({ |
| 43 | + instructions: instructionsRef.current, |
| 44 | + }); |
| 45 | + return { |
| 46 | + content: prompt, |
| 47 | + taskDescription: "Create a loop", |
| 48 | + // Building a loop is pure PostHog-MCP work; it runs repo-less when no |
| 49 | + // personal repo resolves, and passes one through harmlessly when it does. |
| 50 | + repository: ctx.cloudRepository, |
| 51 | + githubUserIntegrationId: ctx.githubUserIntegrationId ?? undefined, |
| 52 | + workspaceMode: "cloud", |
| 53 | + executionMode: "auto", |
| 54 | + adapter: ctx.adapter, |
| 55 | + model: ctx.model, |
| 56 | + reasoningLevel: ctx.reasoningLevel, |
| 57 | + }; |
| 58 | + }, |
| 59 | + [], |
| 60 | + ); |
| 61 | + |
| 62 | + const copy = useMemo( |
| 63 | + () => ({ |
| 64 | + loadingTitle: "Starting loop builder...", |
| 65 | + errorTitle: "Failed to start loop builder", |
| 66 | + missingRepository: "Connect a GitHub repository before building a loop", |
| 67 | + missingIntegration: "Connect a GitHub integration to build a loop", |
| 68 | + signedOut: "Sign in to build a loop", |
| 69 | + missingModel: |
| 70 | + "Couldn't resolve a default model. Open a task once and pick a model, then try again.", |
| 71 | + }), |
| 72 | + [], |
| 73 | + ); |
| 74 | + |
| 75 | + const { run, isRunning } = useInboxCloudTaskRunner({ |
| 76 | + cloudRepository, |
| 77 | + // Loop creation is pure PostHog-MCP work; a missing repo must not block it. |
| 78 | + allowMissingRepository: true, |
| 79 | + loggerScope: "loop-builder", |
| 80 | + copy, |
| 81 | + buildInput, |
| 82 | + }); |
| 83 | + |
| 84 | + const runTask = useCallback( |
| 85 | + async (instructions: string) => { |
| 86 | + instructionsRef.current = instructions; |
| 87 | + await run(); |
| 88 | + }, |
| 89 | + [run], |
| 90 | + ); |
| 91 | + |
| 92 | + return { runTask, isRunning }; |
| 93 | +} |
0 commit comments