Skip to content

Commit e3cda69

Browse files
authored
Merge pull request #26 from Yarden-zamir/feat/fork-changelog-ask-agent
feat(fork): ask an agent about the changelog
2 parents 208878d + 8cd2fd7 commit e3cda69

5 files changed

Lines changed: 480 additions & 19 deletions

File tree

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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+
}

‎apps/web/src/components/forkChangelog/ForkChangelogDialog.tsx‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@ import {
3838
import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from "../ui/empty";
3939
import { Spinner } from "../ui/spinner";
4040
import { Toggle, ToggleGroup } from "../ui/toggle-group";
41-
import { linkCommitSubject } from "./forkChangelog.logic";
41+
import { ForkChangelogAgentMenu } from "./ForkChangelogAgentMenu";
42+
import { type ForkChangelogTab, linkCommitSubject } from "./forkChangelog.logic";
4243

4344
/** How often the open changelog re-reads a queued or running Fork Sync run. */
4445
const SYNC_REFRESH_MS = 5_000;
4546
/** Each release reads GitHub, so the timeline adds older releases on request. */
4647
const RELEASE_PAGE_SIZE = 10;
4748

48-
type ForkChangelogTab = "releases" | "upstream";
49-
5049
interface ChangelogClock {
5150
readonly nowMs: number;
5251
readonly timestampFormat: TimestampFormat;
@@ -75,6 +74,7 @@ export function ForkChangelogDialog({
7574
readonly onOpenChange: (open: boolean) => void;
7675
}) {
7776
const [tab, setTab] = useState<ForkChangelogTab>("releases");
77+
const environmentId = usePrimaryEnvironmentId();
7878

7979
return (
8080
<Dialog open={open} onOpenChange={onOpenChange}>
@@ -85,30 +85,44 @@ export function ForkChangelogDialog({
8585
Fork releases, and the upstream changes that fork main does not have yet.
8686
</DialogDescription>
8787
{/* The header stays in place while the panel below scrolls. */}
88-
<ToggleGroup
89-
aria-label="Changelog view"
90-
value={[tab]}
91-
onValueChange={(next) => {
92-
const value = next[0];
93-
if (value === "releases" || value === "upstream") setTab(value);
94-
}}
95-
>
96-
<Toggle value="releases">Releases</Toggle>
97-
<Toggle value="upstream">Upstream</Toggle>
98-
</ToggleGroup>
88+
<div className="flex flex-wrap items-center justify-between gap-2">
89+
<ToggleGroup
90+
aria-label="Changelog view"
91+
value={[tab]}
92+
onValueChange={(next) => {
93+
const value = next[0];
94+
if (value === "releases" || value === "upstream") setTab(value);
95+
}}
96+
>
97+
<Toggle value="releases">Releases</Toggle>
98+
<Toggle value="upstream">Upstream</Toggle>
99+
</ToggleGroup>
100+
{environmentId === null ? null : (
101+
<ForkChangelogAgentMenu
102+
environmentId={environmentId}
103+
tab={tab}
104+
onOpened={() => onOpenChange(false)}
105+
/>
106+
)}
107+
</div>
99108
</DialogHeader>
100109
<DialogPanel>
101110
<ChangelogClockProvider>
102-
<ForkChangelogBody tab={tab} />
111+
<ForkChangelogBody environmentId={environmentId} tab={tab} />
103112
</ChangelogClockProvider>
104113
</DialogPanel>
105114
</DialogPopup>
106115
</Dialog>
107116
);
108117
}
109118

110-
function ForkChangelogBody({ tab }: { readonly tab: ForkChangelogTab }) {
111-
const environmentId = usePrimaryEnvironmentId();
119+
function ForkChangelogBody({
120+
environmentId,
121+
tab,
122+
}: {
123+
readonly environmentId: EnvironmentId | null;
124+
readonly tab: ForkChangelogTab;
125+
}) {
112126
if (environmentId === null) {
113127
return <Notice>Connect to an environment to read the changelog.</Notice>;
114128
}

‎apps/web/src/components/forkChangelog/forkChangelog.logic.test.ts‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { linkCommitSubject } from "./forkChangelog.logic";
3+
import { EnvironmentId } from "@t3tools/contracts";
4+
5+
import {
6+
buildForkChangelogAgentTask,
7+
linkCommitSubject,
8+
pickForkProject,
9+
} from "./forkChangelog.logic";
410

511
describe("linkCommitSubject", () => {
612
it("links a pull request number to the repository of the commit", () => {
@@ -24,3 +30,60 @@ describe("linkCommitSubject", () => {
2430
}
2531
});
2632
});
33+
34+
describe("pickForkProject", () => {
35+
const mac = EnvironmentId.make("mac");
36+
const identity = (canonicalKey: string, originKey?: string) => ({
37+
canonicalKey,
38+
locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl: canonicalKey },
39+
...(originKey ? { origin: { canonicalKey: originKey } } : {}),
40+
});
41+
42+
it("finds a fork checkout that also has an upstream remote, on the asked environment", () => {
43+
const projects = [
44+
{ name: "other", environmentId: mac, repositoryIdentity: identity("github.com/a/b") },
45+
{
46+
name: "vps fork",
47+
environmentId: EnvironmentId.make("vps"),
48+
repositoryIdentity: identity("github.com/yarden-zamir/t3code"),
49+
},
50+
{ name: "no git", environmentId: mac, repositoryIdentity: null },
51+
{
52+
name: "mac fork",
53+
environmentId: mac,
54+
repositoryIdentity: identity(
55+
"github.com/pingdotgg/t3code",
56+
"github.com/yarden-zamir/t3code",
57+
),
58+
},
59+
];
60+
expect(pickForkProject(projects, mac)?.name).toBe("mac fork");
61+
expect(pickForkProject(projects.slice(0, 1), mac)).toBeNull();
62+
});
63+
});
64+
65+
describe("buildForkChangelogAgentTask", () => {
66+
const releases = [
67+
{ tag: "v3", version: "3" },
68+
{ tag: "v2", version: "2" },
69+
{ tag: "v1", version: "1" },
70+
];
71+
const input = { tab: "releases" as const, releases, upstream: null };
72+
73+
it("names only the releases newer than the installed version", () => {
74+
const { chip } = buildForkChangelogAgentTask("since-installed", {
75+
...input,
76+
installedVersion: "2",
77+
});
78+
expect(chip.text).toContain("newer than the installed version are: `v3`.");
79+
});
80+
81+
it("does not guess a range when the installed version is not listed", () => {
82+
const { chip } = buildForkChangelogAgentTask("since-installed", {
83+
...input,
84+
installedVersion: "0.0.0-dev",
85+
});
86+
expect(chip.text).toContain("The installed version is not a listed release.");
87+
expect(chip.text).not.toContain("newer than the installed version are");
88+
});
89+
});

0 commit comments

Comments
 (0)