2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { CancellationToken } from '../../../base/common/cancellation.js';
7
>
import { URI } from '../../../base/common/uri.js';
8
>
import { localize } from '../../../nls.js';
9
>
import { IAgentService } from '../common/agentService.js';
10
>
import { IAgentHostGitHubEndpointService } from './agentHostGitHubEndpointService.js';
11
>
import { parseChangesetUri } from '../common/changesetUri.js';
12
>
import { AHP_AUTH_REQUIRED, AHP_SESSION_NOT_FOUND, JsonRpcErrorCodes, ProtocolError } from '../common/state/sessionProtocol.js';
13
>
import { readSessionGitHubState, readSessionGitState, type ChangesetOperationFollowUp, type ISessionFileDiff, type ISessionWithDefaultChat } from '../common/state/sessionState.js';
14
>
import { ILogService } from '../../log/common/log.js';
15
>
import { IAgentHostGitService } from '../common/agentHostGitService.js';
16
>
import { type IChangesetOperationHandler } from '../common/agentHostChangesetOperationService.js';
17
>
import { type AutoMergeMethod, type CreatedPullRequest, IAgentHostOctoKitService } from './shared/agentHostOctoKitService.js';
18
>
import type { InvokeChangesetOperationParams, InvokeChangesetOperationResult } from '../common/state/protocol/channels-changeset/commands.js';
19
>
import { ICopilotApiService, type ICopilotUtilityChatMessage } from './shared/copilotApiService.js';
20
>
import { buildConversationContext } from '../common/agentHostConversationContext.js';
21
>
22
>
/**
23
>
* Soft upper bound, in characters, for the conversation context fed to the
24
>
* utility model when generating a PR title and description. Sized to stay
25
>
* within the small model's context window while leaving room for the changed
26
>
* file summary and prompt scaffolding.
27
>
*/
28
>
const MAX_PR_CONVERSATION_CONTEXT_CHARS = 12_000;
29
>
30
>
/**
31
>
* Soft upper bound, in characters, for the changed-file summary fed to the
32
>
* utility model when generating a PR title and description.
33
>
*/
34
>
const MAX_PR_CHANGE_SUMMARY_CHARS = 4_000;
35
>
36
>
export interface PullRequestCreatedEvent {
37
>
readonly sessionKey: string;
38
>
readonly pullRequestUrl: string;
39
>
}
40
>
41
>
/**
42
>
* Server-side handler for the `create-pr` and `create-draft-pr` changeset
43
>
* operations advertised on git-backed sessions whose working directory has
44
>
* a GitHub remote. Operation availability is recomputed by
45
>
* `AgentHostChangesetOperationService.updateOperations`.
46
>
*
47
>
* The flow mirrors the Copilot CLI extension's `createPullRequest` helper
48
>
* (`extensions/copilot/src/extension/chatSessions/vscode-node/copilotCLIChatSessionsContribution.ts`):
49
>
*
50
>
* 1. Resolve session → working directory + current/base branch from
51
>
* {@link ISessionGitState}.
52
>
* 2. Commit any uncommitted working-tree changes.
53
>
* 3. Push the current branch to `origin` (with `--set-upstream` when missing).
54
>
* 4. Resolve `owner` / `repo` from {@link ISessionGitState.githubOwner}
55
>
* / {@link ISessionGitState.githubRepo} (populated by the git probe).
56
>
* 5. Reuse an existing PR for the branch, or POST `/repos/{owner}/{repo}/pulls`
57
>
* via {@link IAgentHostOctoKitService}.
58
>
* 6. Return the PR URL as an {@link InvokeChangesetOperationResult.followUp}.
59
>
*/
60
>
export class AgentHostPullRequestOperationHandler implements IChangesetOperationHandler {
61
>
62
>
public static readonly OPERATION_CREATE_PR = 'create-pr';
63
>
public static readonly OPERATION_CREATE_DRAFT_PR = 'create-draft-pr';
64
>
public static readonly OPERATION_CREATE_PR_AUTO_MERGE = 'create-pr-auto-merge';
65
>
public static readonly OPERATION_CREATE_PR_AUTO_SQUASH = 'create-pr-auto-squash';
66
>
public static readonly OPERATION_CREATE_PR_AUTO_REBASE = 'create-pr-auto-rebase';
67
>
68
>
constructor(
69
private readonly _draft: boolean,
70
private readonly _autoMergeMethod: AutoMergeMethod | undefined,