@@ -118,6 +118,16 @@ interface SourceControlTextGenerationSettings {
118118 readonly style : SourceControlWritingStyleSettings ;
119119}
120120
121+ export interface GitCreatePullRequestResult {
122+ readonly status : "created" | "opened_existing" ;
123+ /** Absent when the host accepted the PR but did not list it back yet. */
124+ readonly url ?: string ;
125+ readonly number ?: number ;
126+ readonly baseBranch : string ;
127+ readonly headBranch : string ;
128+ readonly title : string ;
129+ }
130+
121131export class GitManager extends Context . Service <
122132 GitManager ,
123133 {
@@ -153,6 +163,13 @@ export class GitManager extends Context.Service<
153163 input : GitRunStackedActionInput ,
154164 options ?: GitRunStackedActionOptions ,
155165 ) => Effect . Effect < GitRunStackedActionResult , GitManagerServiceError > ;
166+ /** Opens a PR for the checkout's pushed branch, or returns the one already open. */
167+ readonly createPullRequest : ( input : {
168+ readonly cwd : string ;
169+ readonly title : string ;
170+ readonly body : string ;
171+ readonly baseBranch ?: string | undefined ;
172+ } ) => Effect . Effect < GitCreatePullRequestResult , GitManagerServiceError > ;
156173 readonly subscribePullRequestStateChanges : Effect . Effect <
157174 Stream . Stream < ThreadPullRequestKey > ,
158175 never ,
@@ -2312,12 +2329,17 @@ export const make = Effect.gen(function* () {
23122329 } ) ;
23132330 } ) ;
23142331
2315- const runPrStep = Effect . fn ( "runPrStep" ) ( function * (
2316- settings : SourceControlTextGenerationSettings ,
2332+ const openPrFromBranch = Effect . fn ( "openPrFromBranch" ) ( function * < E > (
23172333 cwd : string ,
23182334 fallbackBranch : string | null ,
2319- threadId : ThreadId | undefined ,
23202335 emit : GitActionProgressEmitter ,
2336+ requestedBase : string | undefined ,
2337+ content : ( input : {
2338+ readonly provider : SourceControlProvider . SourceControlProvider [ "Service" ] ;
2339+ readonly baseBranch : string ;
2340+ readonly baseRef : string ;
2341+ readonly headBranch : string ;
2342+ } ) => Effect . Effect < { readonly title : string ; readonly body : string } , E > ,
23212343 ) {
23222344 const provider = yield * sourceControlProvider ( cwd ) ;
23232345 const terms = sourceControlDefinitions . get ( provider . kind ) . changeRequest ;
@@ -2355,42 +2377,14 @@ export const make = Effect.gen(function* () {
23552377 } ;
23562378 }
23572379
2358- const { baseBranch, baseRef } = yield * resolvePrBase (
2359- cwd ,
2360- branch ,
2361- details . upstreamRef ,
2362- headContext ,
2363- ) ;
2364- yield * emit ( {
2365- kind : "phase_started" ,
2366- phase : "pr" ,
2367- label : `Generating ${ terms . shortLabel } content...` ,
2368- } ) ;
2369- const baseRangeRef = yield * resolveBaseRangeRef ( cwd , baseRef ) ;
2370- const rangeContext = yield * gitCore . readRangeContext ( cwd , baseRangeRef ) ;
2371- const policy = yield * resolveStylePolicy ( cwd , settings ) ;
2372- const changeRequestTemplate =
2373- settings . style . followChangeRequestTemplates && provider . readChangeRequestTemplate
2374- ? Option . getOrUndefined (
2375- yield * provider . readChangeRequestTemplate ( { cwd, treeish : baseRangeRef } ) ,
2376- )
2377- : undefined ;
2378- const threadContext = yield * readThreadContext ( threadId ) ;
2379-
2380- const generated = yield * textGeneration . generatePrContent ( {
2381- cwd,
2380+ const resolved = yield * resolvePrBase ( cwd , branch , details . upstreamRef , headContext ) ;
2381+ const baseBranch = requestedBase ?? resolved . baseBranch ;
2382+ const generated = yield * content ( {
2383+ provider,
23822384 baseBranch,
2385+ baseRef : requestedBase ?? resolved . baseRef ,
23832386 headBranch : headContext . headBranch ,
2384- commitSummary : limitContext ( rangeContext . commitSummary , 20_000 ) ,
2385- diffSummary : limitContext ( rangeContext . diffSummary , 20_000 ) ,
2386- diffPatch : limitContext ( rangeContext . diffPatch , 60_000 ) ,
2387- ...( changeRequestTemplate ? { changeRequestTemplate } : { } ) ,
2388- ...( threadContext ? { threadContext } : { } ) ,
2389- ...( policy ? { policy } : { } ) ,
2390- modelSelection : settings . modelSelection ,
2391- projectId : settings . projectId ,
23922387 } ) ;
2393-
23942388 const bodyFile = path . join (
23952389 tempDir ,
23962390 `t3code-pr-body-${ process . pid } -${ yield * randomUUIDv4 ( cwd ) } .md` ,
@@ -2449,6 +2443,65 @@ export const make = Effect.gen(function* () {
24492443 } ;
24502444 } ) ;
24512445
2446+ const runPrStep = (
2447+ settings : SourceControlTextGenerationSettings ,
2448+ cwd : string ,
2449+ fallbackBranch : string | null ,
2450+ threadId : ThreadId | undefined ,
2451+ emit : GitActionProgressEmitter ,
2452+ ) =>
2453+ openPrFromBranch (
2454+ cwd ,
2455+ fallbackBranch ,
2456+ emit ,
2457+ undefined ,
2458+ ( { provider, baseBranch, baseRef, headBranch } ) =>
2459+ Effect . gen ( function * ( ) {
2460+ const terms = sourceControlDefinitions . get ( provider . kind ) . changeRequest ;
2461+ yield * emit ( {
2462+ kind : "phase_started" ,
2463+ phase : "pr" ,
2464+ label : `Generating ${ terms . shortLabel } content...` ,
2465+ } ) ;
2466+ const baseRangeRef = yield * resolveBaseRangeRef ( cwd , baseRef ) ;
2467+ const rangeContext = yield * gitCore . readRangeContext ( cwd , baseRangeRef ) ;
2468+ const policy = yield * resolveStylePolicy ( cwd , settings ) ;
2469+ const changeRequestTemplate =
2470+ settings . style . followChangeRequestTemplates && provider . readChangeRequestTemplate
2471+ ? Option . getOrUndefined (
2472+ yield * provider . readChangeRequestTemplate ( { cwd, treeish : baseRangeRef } ) ,
2473+ )
2474+ : undefined ;
2475+ const threadContext = yield * readThreadContext ( threadId ) ;
2476+
2477+ return yield * textGeneration . generatePrContent ( {
2478+ cwd,
2479+ baseBranch,
2480+ headBranch,
2481+ commitSummary : limitContext ( rangeContext . commitSummary , 20_000 ) ,
2482+ diffSummary : limitContext ( rangeContext . diffSummary , 20_000 ) ,
2483+ diffPatch : limitContext ( rangeContext . diffPatch , 60_000 ) ,
2484+ ...( changeRequestTemplate ? { changeRequestTemplate } : { } ) ,
2485+ ...( threadContext ? { threadContext } : { } ) ,
2486+ ...( policy ? { policy } : { } ) ,
2487+ modelSelection : settings . modelSelection ,
2488+ projectId : settings . projectId ,
2489+ } ) ;
2490+ } ) ,
2491+ ) ;
2492+
2493+ const createPullRequest : GitManager [ "Service" ] [ "createPullRequest" ] = Effect . fn (
2494+ "GitManager.createPullRequest" ,
2495+ ) ( function * ( input ) {
2496+ return yield * openPrFromBranch (
2497+ input . cwd ,
2498+ null ,
2499+ ( ) => Effect . void ,
2500+ input . baseBranch ,
2501+ ( ) => Effect . succeed ( { title : input . title , body : input . body } ) ,
2502+ ) ;
2503+ } ) ;
2504+
24522505 const localStatus : GitManager [ "Service" ] [ "localStatus" ] = Effect . fn ( "localStatus" ) (
24532506 function * ( input ) {
24542507 const cacheKey = yield * normalizeStatusCacheKey ( input . cwd ) ;
@@ -3397,6 +3450,7 @@ export const make = Effect.gen(function* () {
33973450 resolvePullRequest,
33983451 preparePullRequestThread,
33993452 runStackedAction,
3453+ createPullRequest,
34003454 subscribePullRequestStateChanges : PubSub . subscribe ( pullRequestStateChanges ) . pipe (
34013455 Effect . map ( ( subscription ) => Stream . fromSubscription ( subscription ) ) ,
34023456 ) ,
0 commit comments