@@ -2,11 +2,7 @@ import { projectChangeRequestUrl } from "@t3tools/client-runtime/change-request-
22import { normalizeThreadPullRequestKey } from "@t3tools/client-runtime/thread-pull-requests" ;
33import { useAtomValue } from "@effect/atom-react" ;
44import { usePullRequestStack } from "~/state/usePullRequestStack" ;
5- import {
6- scopedThreadKey ,
7- scopeProjectRef ,
8- scopeThreadRef ,
9- } from "@t3tools/client-runtime/environment" ;
5+ import { scopeProjectRef , scopeThreadRef } from "@t3tools/client-runtime/environment" ;
106import {
117 isAtomCommandInterrupted ,
128 squashAtomCommandFailure ,
@@ -53,7 +49,10 @@ import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
5349import { isCommandPaletteOpen } from "~/commandPaletteBus" ;
5450import { resolveShortcutCommand , type ShortcutMatchContext } from "~/keybindings" ;
5551import { primaryServerKeybindingsAtom } from "~/state/server" ;
56- import { usePullRequestDefaultMergeMethodResolver } from "./usePullRequestActions" ;
52+ import {
53+ usePullRequestDefaultMergeMethodResolver ,
54+ writeHandoffToComposer ,
55+ } from "./usePullRequestActions" ;
5756import { offHeadPullRequestCheckoutToast } from "./pullRequestCheckoutToast" ;
5857import { usePreparePullRequestThreadAction } from "~/state/sourceControlActions" ;
5958import { cn } from "~/lib/utils" ;
@@ -118,9 +117,9 @@ import {
118117 buildExplainPullRequestHandoff ,
119118 buildFixFindingHandoff ,
120119 buildFixFindingsHandoff ,
120+ buildPullRequestCommentContext ,
121+ buildPullRequestContext ,
121122 buildResolveConflictsPrompt ,
122- handoffPrompt ,
123- handoffReviewComments ,
124123 latestPullRequestReviewOutcomes ,
125124 loadingPullRequestCheckoutCommand ,
126125 isPullRequestNotFound ,
@@ -140,9 +139,9 @@ import {
140139 isPullRequestConflicting ,
141140 resolveBaseFreshness ,
142141 resolvePullRequestMergeMethod ,
142+ type PullRequestChatSubject ,
143143 type PullRequestFinding ,
144144 shouldRefreshPullRequestActivity ,
145- stripPullRequestHandoffReferences ,
146145} from "./pullRequestDetail.logic" ;
147146import { resolvePickableEnvironments } from "./pullRequestProjectAssignment.logic" ;
148147import { PullRequestChecksPopover } from "./PullRequestChecksPopover" ;
@@ -209,12 +208,6 @@ const TABS: ReadonlyArray<{ value: PullRequestDetailTab; label: string }> = [
209208const loadCodeTab = ( ) => import ( "./PullRequestCodeTab" ) ;
210209const PullRequestCodeTab = lazy ( loadCodeTab ) ;
211210
212- // Persist handoffs across unmounts; replace only unchanged contributions to preserve user edits.
213- const lastHandoffPromptByDraft = new Map < string , string > ( ) ;
214-
215- const composerTargetKey = ( target : ScopedThreadRef | DraftId ) : string =>
216- typeof target === "string" ? target : scopedThreadKey ( target ) ;
217-
218211function reportCheckoutCommandError ( error : Error ) {
219212 toastManager . add ( {
220213 type : "error" ,
@@ -758,43 +751,6 @@ export function PullRequestDetailPanel({
758751 if ( guideWriter . data ?. guide == null ) selectTab ( "guide" ) ;
759752 } ;
760753
761- const writeTaskToComposer = ( target : ScopedThreadRef | DraftId , task : ThreadTask ) => {
762- const store = useComposerDraftStore . getState ( ) ;
763- const draft = store . getComposerDraft ( target ) ;
764- const key = composerTargetKey ( target ) ;
765- const previousCommentIds = new Set ( ( draft ?. reviewComments ?? [ ] ) . map ( ( comment ) => comment . id ) ) ;
766- const repeatedCommentIds = new Set (
767- ( task . reviewComments ?? [ ] )
768- . filter ( ( comment ) => previousCommentIds . has ( comment . id ) )
769- . map ( ( comment ) => comment . id ) ,
770- ) ;
771- const promptWithoutPreviousHandoff = stripPullRequestHandoffReferences (
772- draft ?. prompt ?? "" ,
773- draft ?. reviewComments ?? [ ] ,
774- repeatedCommentIds ,
775- ) ;
776- const prompt = handoffPrompt (
777- {
778- prompt : promptWithoutPreviousHandoff ,
779- lastHandoffPrompt : lastHandoffPromptByDraft . get ( key ) ,
780- } ,
781- task . prompt ,
782- ) ;
783- lastHandoffPromptByDraft . set ( key , task . prompt ) ;
784- store . setPrompt ( target , prompt ) ;
785- store . setReviewComments (
786- target ,
787- handoffReviewComments ( draft ?. reviewComments ?? [ ] , task . reviewComments ?? [ ] ) ,
788- ) ;
789- for ( const comment of task . reviewComments ?? [ ] ) {
790- if ( ! repeatedCommentIds . has ( comment . id ) ) continue ;
791- store . addReviewComment ( target , comment , {
792- allowDuplicateReference : true ,
793- insertAtCaret : false ,
794- } ) ;
795- }
796- } ;
797-
798754 const openThreadWithTask = async (
799755 projectRef : ReturnType < typeof scopeProjectRef > ,
800756 task : ThreadTask | null ,
@@ -808,7 +764,7 @@ export function PullRequestDetailPanel({
808764 ) ) ;
809765 if ( session === null ) return null ;
810766 if ( task === null ) return session ;
811- writeTaskToComposer ( session . draftId , task ) ;
767+ writeHandoffToComposer ( session . draftId , task ) ;
812768 return session ;
813769 } ;
814770
@@ -819,7 +775,7 @@ export function PullRequestDetailPanel({
819775 ) => {
820776 if ( ! detail || handoff !== null ) return ;
821777 if ( attachTarget !== null ) {
822- writeTaskToComposer ( attachTarget , task ) ;
778+ writeHandoffToComposer ( attachTarget , task ) ;
823779 toastManager . add ( {
824780 type : "success" ,
825781 title : "Added to the composer" ,
@@ -875,7 +831,7 @@ export function PullRequestDetailPanel({
875831 ) => {
876832 if ( ! handoffSummary || handoff !== null ) return ;
877833 if ( attachTarget !== null && task !== null ) {
878- writeTaskToComposer ( attachTarget , task ) ;
834+ writeHandoffToComposer ( attachTarget , task ) ;
879835 toastManager . add ( {
880836 type : "success" ,
881837 title : "Added to the composer" ,
@@ -1060,6 +1016,43 @@ export function PullRequestDetailPanel({
10601016 ) ;
10611017 } ;
10621018
1019+ /**
1020+ * Brings a remark into the composer beside this panel, with the pull request it belongs to,
1021+ * and nothing else: what to ask about it is the reader's to write. A remark on a line travels
1022+ * as its whole conversation, the way fixing one does.
1023+ */
1024+ const addCommentToChat = ( subject : PullRequestChatSubject ) => {
1025+ if ( ! detail || attachTarget === null ) return ;
1026+ const thread =
1027+ subject . kind === "comment"
1028+ ? detail . reviewThreads . find ( ( candidate ) =>
1029+ candidate . comments . some ( ( comment ) => comment . id === subject . comment . id ) ,
1030+ )
1031+ : undefined ;
1032+ const comment = buildPullRequestCommentContext (
1033+ detail ,
1034+ thread === undefined ? subject : { kind : "thread" , thread } ,
1035+ ) ;
1036+ if ( comment === null ) return ;
1037+ const store = useComposerDraftStore . getState ( ) ;
1038+ const reference = buildPullRequestContext ( {
1039+ number : detail . number ,
1040+ title : detail . title ,
1041+ url : detail . url ,
1042+ headBranch : detail . headBranch ,
1043+ baseBranch : detail . baseBranch ,
1044+ state : detail . state ,
1045+ isDraft : detail . isDraft ,
1046+ } ) ;
1047+ const hasReference = ( store . getComposerDraft ( attachTarget ) ?. reviewComments ?? [ ] ) . some (
1048+ ( existing ) => existing . pullRequest ?. url === detail . url ,
1049+ ) ;
1050+ // The active editor may be answering a provider question rather than editing this draft.
1051+ if ( ! hasReference ) store . addReviewComment ( attachTarget , reference , { insertAtCaret : false } ) ;
1052+ store . addReviewComment ( attachTarget , comment , { insertAtCaret : false } ) ;
1053+ toastManager . add ( { type : "success" , title : "Added to the composer" } ) ;
1054+ } ;
1055+
10631056 const startCheckout = ( mode : "worktree" | "local" ) => {
10641057 if ( ! handoffSummary ) return ;
10651058 void startHandoff ( `checkout:${ mode } ` , null , mode ) ;
@@ -1075,6 +1068,8 @@ export function PullRequestDetailPanel({
10751068 url : detail . url ,
10761069 headBranch : detail . headBranch ,
10771070 baseBranch : detail . baseBranch ,
1071+ state : detail . state ,
1072+ isDraft : detail . isDraft ,
10781073 finding,
10791074 } ) ,
10801075 ) ;
@@ -1090,6 +1085,8 @@ export function PullRequestDetailPanel({
10901085 url : detail . url ,
10911086 headBranch : detail . headBranch ,
10921087 baseBranch : detail . baseBranch ,
1088+ state : detail . state ,
1089+ isDraft : detail . isDraft ,
10931090 reviewThreads : detail . reviewThreads ,
10941091 comments : detail . comments ,
10951092 checks : checksStale ? [ ] : detail . checks ,
@@ -1760,6 +1757,7 @@ export function PullRequestDetailPanel({
17601757 fixFindingLabel = { handoffLabels . fixFinding }
17611758 fixCheckLabel = { handoffLabels . fixCheck }
17621759 { ...( canFixFindings ? { onFixFinding : startFixFinding } : { } ) }
1760+ { ...( attachTarget === null ? { } : { onAddToChat : addCommentToChat } ) }
17631761 onRefresh = { refreshDetail }
17641762 onRefreshChecks = { refreshFromHost }
17651763 />
@@ -1817,6 +1815,7 @@ export function PullRequestDetailPanel({
18171815 reference = { reference }
18181816 order = { timelineOrder }
18191817 onOpenCommit = { openCommit }
1818+ { ...( attachTarget === null ? { } : { onAddToChat : addCommentToChat } ) }
18201819 onRefresh = { refreshDetail }
18211820 />
18221821 ) }
@@ -1840,6 +1839,7 @@ export function PullRequestDetailPanel({
18401839 pendingFinding = { handoff }
18411840 fixFindingLabel = { handoffLabels . fixFinding }
18421841 { ...( canFixFindings ? { onFixFinding : startFixFinding } : { } ) }
1842+ { ...( attachTarget === null ? { } : { onAddToChat : addCommentToChat } ) }
18431843 onRefresh = { refreshDetail }
18441844 refreshToken = { refreshToken }
18451845 backgroundRefreshToken = { turnRefresh ?? 0 }
0 commit comments