-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add commit and pull request authoring #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { useEffect, useRef, useState } from "react"; | ||
| import { SparkleIcon } from "@phosphor-icons/react/Sparkle"; | ||
| import type { AssistantMessage, SessionEntry } from "../../shared/pi-types.ts"; | ||
| import { rpc } from "../lib/rpc.ts"; | ||
| import { activeConversation, useAppStore } from "../lib/store.ts"; | ||
| import { Button } from "@/components/ui/button.tsx"; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogDescription, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| } from "@/components/ui/dialog.tsx"; | ||
| import { Input } from "@/components/ui/input.tsx"; | ||
| import { Textarea } from "@/components/ui/textarea.tsx"; | ||
|
|
||
| function assistantText(entries: SessionEntry[], since: number): string | null { | ||
| for (let index = entries.length - 1; index >= 0; index--) { | ||
| const entry = entries[index]; | ||
| const message = (entry as { message?: unknown }).message; | ||
| if (entry.type !== "message" || !message || (message as { role?: string }).role !== "assistant") continue; | ||
| const assistant = message as AssistantMessage; | ||
| if (assistant.timestamp < since) return null; | ||
| const text = assistant.content.filter((part) => part.type === "text").map((part) => part.text).join("\n").trim(); | ||
| if (text) return text; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export default function CommitDialog({ projectDir, onClose }: { projectDir: string | null; onClose: () => void }) { | ||
| const conversation = useAppStore(activeConversation); | ||
| const refreshGit = useAppStore((s) => s.refreshGit); | ||
| const [message, setMessage] = useState(""); | ||
| const [title, setTitle] = useState(""); | ||
| const [body, setBody] = useState(""); | ||
| const [draftingSince, setDraftingSince] = useState<number | null>(null); | ||
| const sawDraftRun = useRef(false); | ||
| const [busy, setBusy] = useState<"commit" | "pr" | null>(null); | ||
| const [error, setError] = useState<string | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!draftingSince) return; | ||
| const draft = assistantText(conversation.entries, draftingSince); | ||
| if (draft) { | ||
| setMessage(draft); | ||
| setTitle(draft.split("\n")[0].slice(0, 256)); | ||
| setDraftingSince(null); | ||
| return; | ||
| } | ||
| if (conversation.running) { | ||
| sawDraftRun.current = true; | ||
| } else if (sawDraftRun.current) { | ||
| setDraftingSince(null); | ||
| setError("Pi finished without a usable commit-message draft."); | ||
| } | ||
| }, [conversation.entries, conversation.running, draftingSince]); | ||
|
|
||
| useEffect(() => { | ||
| setMessage(""); | ||
|
nonlooped marked this conversation as resolved.
|
||
| setTitle(""); | ||
| setBody(""); | ||
| setError(null); | ||
| setDraftingSince(null); | ||
| sawDraftRun.current = false; | ||
| }, [projectDir]); | ||
|
|
||
| async function askPi() { | ||
| if (!projectDir || !conversation.sessionFile || conversation.running) return; | ||
| setError(null); | ||
| const since = Date.now(); | ||
| sawDraftRun.current = false; | ||
| setDraftingSince(since); | ||
| const result = await rpc.request.submit({ | ||
| projectDir, | ||
| sessionFile: conversation.sessionFile, | ||
| message: "Inspect the currently staged Git changes. Draft only a concise Conventional Commit message, with an optional body only when it adds necessary context. Do not commit or change files.", | ||
| }); | ||
| if (!result.ok) { | ||
| setDraftingSince(null); | ||
| setError(result.error ?? "Pi could not draft a commit message."); | ||
| } | ||
| } | ||
|
|
||
| async function commit() { | ||
| if (!projectDir || !message.trim()) return; | ||
| setBusy("commit"); | ||
| setError(null); | ||
| const result = await rpc.request.gitCommit({ projectDir, message }); | ||
| setBusy(null); | ||
| if (!result.ok) return setError(result.error ?? "Git could not create the commit."); | ||
| await refreshGit(); | ||
| } | ||
|
|
||
| async function createPr() { | ||
| if (!projectDir || !title.trim()) return; | ||
| setBusy("pr"); | ||
| setError(null); | ||
| const result = await rpc.request.gitPushAndCreatePr({ projectDir, title, body }); | ||
| setBusy(null); | ||
| if (!result.ok) return setError(result.error ?? "GitHub CLI could not open the pull request."); | ||
| if (result.url) window.open(result.url, "_blank", "noopener,noreferrer"); | ||
| onClose(); | ||
| } | ||
|
|
||
| const canAskPi = Boolean(conversation.sessionFile) && !conversation.running && !draftingSince; | ||
| return ( | ||
| <Dialog open={projectDir !== null} onOpenChange={(open) => !open && onClose()}> | ||
| <DialogContent> | ||
| <DialogHeader> | ||
| <DialogTitle className="font-heading text-base font-semibold">Commit changes</DialogTitle> | ||
| <DialogDescription> | ||
| Stage the changes you want first. Pi can draft the message from the staged diff; you can edit it before committing. | ||
| </DialogDescription> | ||
| </DialogHeader> | ||
| <div className="flex flex-col gap-3"> | ||
| <Textarea value={message} onChange={(event) => setMessage(event.target.value)} placeholder="feat: describe the change" aria-label="Commit message" /> | ||
| <Button variant="outline" onClick={() => void askPi()} disabled={!canAskPi}> | ||
| <SparkleIcon data-icon="inline-start" /> | ||
| {draftingSince ? "Pi is drafting…" : "Draft with Pi"} | ||
| </Button> | ||
| {!conversation.sessionFile ? <p className="text-xs text-muted-foreground">Open a chat to ask Pi for a draft.</p> : null} | ||
| {conversation.running ? <p className="text-xs text-muted-foreground">Wait for Pi’s current turn before asking for a draft.</p> : null} | ||
| <div className="flex flex-col gap-2 border-t pt-3"> | ||
| <p className="text-xs font-medium">Open a pull request</p> | ||
| <Input value={title} onChange={(event) => setTitle(event.target.value)} placeholder="Pull request title" aria-label="Pull request title" /> | ||
| <Textarea value={body} onChange={(event) => setBody(event.target.value)} placeholder="What changed and how it was checked" aria-label="Pull request description" /> | ||
| </div> | ||
| {error ? <p className="text-xs whitespace-pre-wrap text-destructive">{error}</p> : null} | ||
| </div> | ||
| <DialogFooter> | ||
| <Button variant="ghost" onClick={onClose} disabled={busy !== null}>Close</Button> | ||
| <Button variant="outline" onClick={() => void commit()} disabled={!message.trim() || busy !== null}>Commit</Button> | ||
| <Button onClick={() => void createPr()} disabled={!title.trim() || busy !== null}> | ||
| {busy === "pr" ? "Opening PR…" : "Push & open PR"} | ||
| </Button> | ||
| </DialogFooter> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.