|
| 1 | +import { scopeProjectRef } from "@t3tools/client-runtime/environment"; |
| 2 | +import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | +import { |
| 4 | + BookOpenIcon, |
| 5 | + ChevronDownIcon, |
| 6 | + GitCompareArrowsIcon, |
| 7 | + HistoryIcon, |
| 8 | + MessageCircleQuestionIcon, |
| 9 | + type LucideIcon, |
| 10 | + SparklesIcon, |
| 11 | +} from "lucide-react"; |
| 12 | +import { useState } from "react"; |
| 13 | + |
| 14 | +import { APP_VERSION } from "../../branding"; |
| 15 | +import { type DraftId, useComposerDraftStore } from "../../composerDraftStore"; |
| 16 | +import { useNewThreadHandler } from "../../hooks/useHandleNewThread"; |
| 17 | +import { readProjects } from "../../state/entities"; |
| 18 | +import { forkReleases, forkUpstreamPending } from "../../state/forkChangelog"; |
| 19 | +import { useEnvironmentQuery } from "../../state/query"; |
| 20 | +import { handoffPrompt } from "../pullRequest/pullRequestDetail.logic"; |
| 21 | +import { Button } from "../ui/button"; |
| 22 | +import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; |
| 23 | +import { toastManager } from "../ui/toast"; |
| 24 | +import { |
| 25 | + buildForkChangelogAgentTask, |
| 26 | + FORK_CHANGELOG_CHIP_ID_PREFIX, |
| 27 | + type ForkChangelogAgentAction, |
| 28 | + type ForkChangelogAgentInput, |
| 29 | + type ForkChangelogTab, |
| 30 | + installedReleaseIndex, |
| 31 | + pickForkProject, |
| 32 | +} from "./forkChangelog.logic"; |
| 33 | + |
| 34 | +/** What the last hand-off wrote into a draft, so the next one replaces it instead of stacking. */ |
| 35 | +const lastPromptByDraft = new Map<DraftId, string>(); |
| 36 | + |
| 37 | +/** |
| 38 | + * Fork feature: hands the changelog to an agent, like "Ask a question" on a pull request. Each |
| 39 | + * action opens a thread in the fork project with a changelog chip and a prompt to read and send. |
| 40 | + * The menu reads the same cached queries as the open tab, so it costs no extra GitHub call. |
| 41 | + */ |
| 42 | +export function ForkChangelogAgentMenu({ |
| 43 | + environmentId, |
| 44 | + tab, |
| 45 | + onOpened, |
| 46 | +}: { |
| 47 | + readonly environmentId: EnvironmentId; |
| 48 | + readonly tab: ForkChangelogTab; |
| 49 | + /** Called once the thread holds the task, so the dialog can get out of the way. */ |
| 50 | + readonly onOpened: () => void; |
| 51 | +}) { |
| 52 | + return tab === "releases" ? ( |
| 53 | + <ReleasesAgentMenu environmentId={environmentId} onOpened={onOpened} /> |
| 54 | + ) : ( |
| 55 | + <UpstreamAgentMenu environmentId={environmentId} onOpened={onOpened} /> |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +interface MenuAction { |
| 60 | + readonly action: ForkChangelogAgentAction; |
| 61 | + readonly label: string; |
| 62 | + readonly description: string; |
| 63 | + readonly Icon: LucideIcon; |
| 64 | +} |
| 65 | + |
| 66 | +const ASK: MenuAction = { |
| 67 | + action: "ask", |
| 68 | + label: "Ask a question", |
| 69 | + description: "Opens a thread that knows this changelog.", |
| 70 | + Icon: MessageCircleQuestionIcon, |
| 71 | +}; |
| 72 | + |
| 73 | +function ReleasesAgentMenu({ |
| 74 | + environmentId, |
| 75 | + onOpened, |
| 76 | +}: { |
| 77 | + readonly environmentId: EnvironmentId; |
| 78 | + readonly onOpened: () => void; |
| 79 | +}) { |
| 80 | + const releases = useEnvironmentQuery(forkReleases({ environmentId, input: {} })); |
| 81 | + const input: ForkChangelogAgentInput = { |
| 82 | + tab: "releases", |
| 83 | + installedVersion: APP_VERSION, |
| 84 | + releases: releases.data?.releases ?? null, |
| 85 | + upstream: null, |
| 86 | + }; |
| 87 | + const actions: MenuAction[] = [ |
| 88 | + ASK, |
| 89 | + { |
| 90 | + action: "summarize-releases", |
| 91 | + label: "Summarize the changelog", |
| 92 | + description: "The listed releases, grouped by theme.", |
| 93 | + Icon: BookOpenIcon, |
| 94 | + }, |
| 95 | + ]; |
| 96 | + // Only when a listed release is newer than the installed one. |
| 97 | + if (installedReleaseIndex(input) > 0) { |
| 98 | + actions.push({ |
| 99 | + action: "since-installed", |
| 100 | + label: "What changed since my version", |
| 101 | + description: `What the releases after ${APP_VERSION} add.`, |
| 102 | + Icon: HistoryIcon, |
| 103 | + }); |
| 104 | + } |
| 105 | + return ( |
| 106 | + <AgentMenu actions={actions} environmentId={environmentId} input={input} onOpened={onOpened} /> |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +function UpstreamAgentMenu({ |
| 111 | + environmentId, |
| 112 | + onOpened, |
| 113 | +}: { |
| 114 | + readonly environmentId: EnvironmentId; |
| 115 | + readonly onOpened: () => void; |
| 116 | +}) { |
| 117 | + const pending = useEnvironmentQuery(forkUpstreamPending({ environmentId, input: {} })); |
| 118 | + const upstream = pending.data; |
| 119 | + const input: ForkChangelogAgentInput = { |
| 120 | + tab: "upstream", |
| 121 | + installedVersion: APP_VERSION, |
| 122 | + releases: null, |
| 123 | + upstream: upstream === null ? null : { total: upstream.total, compareUrl: upstream.compareUrl }, |
| 124 | + }; |
| 125 | + const actions: MenuAction[] = [ASK]; |
| 126 | + if (upstream !== null && upstream.total > 0) { |
| 127 | + actions.push( |
| 128 | + { |
| 129 | + action: "summarize-upstream", |
| 130 | + label: "Summarize upstream changes", |
| 131 | + description: "The upstream commits fork main does not have yet.", |
| 132 | + Icon: BookOpenIcon, |
| 133 | + }, |
| 134 | + { |
| 135 | + action: "upstream-impact", |
| 136 | + label: "Check the impact on the fork", |
| 137 | + description: "Where they touch fork changes, before the next Fork Sync.", |
| 138 | + Icon: GitCompareArrowsIcon, |
| 139 | + }, |
| 140 | + ); |
| 141 | + } |
| 142 | + return ( |
| 143 | + <AgentMenu actions={actions} environmentId={environmentId} input={input} onOpened={onOpened} /> |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +function AgentMenu({ |
| 148 | + actions, |
| 149 | + environmentId, |
| 150 | + input, |
| 151 | + onOpened, |
| 152 | +}: { |
| 153 | + readonly actions: ReadonlyArray<MenuAction>; |
| 154 | + readonly environmentId: EnvironmentId; |
| 155 | + readonly input: ForkChangelogAgentInput; |
| 156 | + readonly onOpened: () => void; |
| 157 | +}) { |
| 158 | + const newThread = useNewThreadHandler(); |
| 159 | + const [pending, setPending] = useState<ForkChangelogAgentAction | null>(null); |
| 160 | + |
| 161 | + const start = async (action: ForkChangelogAgentAction) => { |
| 162 | + if (pending !== null) return; |
| 163 | + const project = pickForkProject(readProjects(), environmentId); |
| 164 | + if (project === null) { |
| 165 | + toastManager.add({ |
| 166 | + type: "error", |
| 167 | + title: "No fork project here", |
| 168 | + description: "Add a checkout of the fork as a project on this machine, then ask again.", |
| 169 | + }); |
| 170 | + return; |
| 171 | + } |
| 172 | + setPending(action); |
| 173 | + const session = await newThread(scopeProjectRef(environmentId, project.id)).then( |
| 174 | + (result) => result, |
| 175 | + () => null, |
| 176 | + ); |
| 177 | + setPending(null); |
| 178 | + if (session === null) { |
| 179 | + toastManager.add({ |
| 180 | + type: "error", |
| 181 | + title: "Could not open a thread", |
| 182 | + description: "Try again from the project, or open a thread first.", |
| 183 | + }); |
| 184 | + return; |
| 185 | + } |
| 186 | + const task = buildForkChangelogAgentTask(action, input); |
| 187 | + const store = useComposerDraftStore.getState(); |
| 188 | + const draft = store.getComposerDraft(session.draftId); |
| 189 | + store.setPrompt( |
| 190 | + session.draftId, |
| 191 | + handoffPrompt( |
| 192 | + { prompt: draft?.prompt ?? "", lastHandoffPrompt: lastPromptByDraft.get(session.draftId) }, |
| 193 | + task.prompt, |
| 194 | + ), |
| 195 | + ); |
| 196 | + lastPromptByDraft.set(session.draftId, task.prompt); |
| 197 | + store.setReviewComments(session.draftId, [ |
| 198 | + ...(draft?.reviewComments ?? []).filter( |
| 199 | + (comment) => !comment.id.startsWith(FORK_CHANGELOG_CHIP_ID_PREFIX), |
| 200 | + ), |
| 201 | + task.chip, |
| 202 | + ]); |
| 203 | + onOpened(); |
| 204 | + toastManager.add({ |
| 205 | + type: "success", |
| 206 | + title: "Asked in a thread", |
| 207 | + description: |
| 208 | + task.prompt.length > 0 |
| 209 | + ? "The question is in the composer. Read it over, then send." |
| 210 | + : "The changelog is in the composer. Type your question, then send.", |
| 211 | + }); |
| 212 | + }; |
| 213 | + |
| 214 | + return ( |
| 215 | + <Menu> |
| 216 | + <MenuTrigger render={<Button size="sm" variant="outline" />}> |
| 217 | + <SparklesIcon className="size-3.5" /> |
| 218 | + <span>{pending === null ? "Ask agent" : "Opening…"}</span> |
| 219 | + <ChevronDownIcon className="size-3.5" /> |
| 220 | + </MenuTrigger> |
| 221 | + <MenuPopup align="end" side="bottom"> |
| 222 | + {actions.map(({ action, label, description, Icon }) => ( |
| 223 | + <MenuItem disabled={pending !== null} key={action} onClick={() => void start(action)}> |
| 224 | + <Icon className="mt-1 size-3.5 shrink-0 self-start" /> |
| 225 | + <span className="flex min-w-0 flex-col"> |
| 226 | + <span>{label}</span> |
| 227 | + <span className="text-xs text-muted-foreground">{description}</span> |
| 228 | + </span> |
| 229 | + </MenuItem> |
| 230 | + ))} |
| 231 | + </MenuPopup> |
| 232 | + </Menu> |
| 233 | + ); |
| 234 | +} |
0 commit comments