Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
/**
* Soft upper bound, in characters, for the conversation context fed to the
* utility model when generating a PR title and description. Sized to stay
* within the small model's context window while leaving room for the changed
* file summary and prompt scaffolding.
*/
const
MAX_PR_CONVERSATION_CONTEXT_CHARS
=
12_000
;
/**
* Soft upper bound, in characters, for the changed-file summary fed to the
* utility model when generating a PR title and description.
*/
const
MAX_PR_CHANGE_SUMMARY_CHARS
=
4_000
;
export
interface
PullRequestCreatedEvent
{
readonly
:
string
;
readonly
:
string
;
/** The head branch the pull request was created (or found) for. */
readonly
:
string
;
}
/**
* Server-side handler for the `create-pr` and `create-draft-pr` changeset
* operations advertised on git-backed sessions whose working directory has
* a GitHub remote. Operation availability is recomputed by
* `AgentHostChangesetOperationService.updateOperations`.
*
* The flow mirrors the Copilot CLI extension's `createPullRequest` helper
* (`extensions/copilot/src/extension/chatSessions/vscode-node/copilotCLIChatSessionsContribution.ts`):
*
* 1. Resolve session → working directory + current/base branch from
* {@link ISessionGitState}.
* 2. If the current branch is the base branch, create a generated session branch.
* 3. Commit any uncommitted working-tree changes.
* 4. Push the current branch to its GitHub upstream remote (with `--set-upstream` when missing).
* 5. Resolve `owner` / `repo` from {@link ISessionGitState.githubOwner}
* / {@link ISessionGitState.githubRepo} (populated by the git probe).
* 6. Reuse an existing PR for the branch, or POST `/repos/{owner}/{repo}/pulls`
* via {@link IAgentHostOctoKitService}.
* 7. Return the PR URL as an {@link InvokeChangesetOperationResult.followUp}.
*/
export
class
AgentHostPullRequestOperationHandler
implements
IChangesetOperationHandler
{
public
static
readonly
OPERATION_CREATE_PR
=
'create-pr'
;
public
static
readonly
OPERATION_CREATE_DRAFT_PR
=
'create-draft-pr'
;
public
static
readonly
OPERATION_CREATE_PR_AUTO_MERGE
=
'create-pr-auto-merge'
;
public
static
readonly
OPERATION_CREATE_PR_AUTO_SQUASH
=
'create-pr-auto-squash'
;
public
static
readonly
OPERATION_CREATE_PR_AUTO_REBASE
=
'create-pr-auto-rebase'
;
constructor
(
private
readonly
:
boolean
,
private
readonly
:
|
undefined
,
private
readonly
_getSessionState
:
(
:
string
)
=>
|
undefined
,
private
readonly
_resolveBaseBranchName
:
(
:
string
)
=>
Promise
<
string
|
undefined
>
,
private
readonly
_onPullRequestCreated
:
(
:
)
=>
void
,
@IAgentHostAuthenticationService
private
readonly
:
,
@IAgentHostGitService
private
readonly
:
,
@IAgentHostOctoKitService
private
readonly
:
,
@IAgentHostGitHubEndpointService
private
readonly
:
,
@ICopilotApiService
private
readonly
:
,
@IAgentBranchNameGenerator
private
readonly
:
,
@ILogService
private
readonly
:
,
)
{
}
async
invoke
(
:
,
:
)
:
Promise
<
>
{
const
=
new
AbortController
(
)
;
if
(
.
)
{
.
abort
(
)
;
}
const
=
.
onCancellationRequested
(
(
)
=>
.
abort
(
)
)
;
try
{
return
await
this
.
_invoke
(
,
,
.
)
;
}
finally
{
.
dispose
(
)
;
}
}
private
async
_invoke
(
:
,
:
,
:
)
:
Promise
<
>
{
const
=
parseChangesetUri
(
.
)
;
if
(
!
)
{
throw
new
ProtocolError
(
.
,
`Not a changeset URI: ${params.channel}`
)
;
}
this
.
_throwIfCancelled
(
)
;
const
=
.
;
const
=
this
.
_getSessionState
(
)
;
if
(
!
)
{
throw
new
ProtocolError
(
AHP_SESSION_NOT_FOUND
,
`Session not found: ${sessionUri}`
)
;
}
const
=
.
?.
[
0
]
;
if
(
!
)
{
throw
new
ProtocolError
(
.
,
`Session has no working directory: ${sessionUri}`
)
;
}
const
=
readSessionGitHubState
(
.
)
;
if
(
!
?.
||
!
?.
)
{
throw
new
ProtocolError
(
.
,
`Session's working directory is not a GitHub-backed git repo: ${sessionUri}`
,
)
;
}
const
=
URI
.
parse
(
)
;
const
=
readSessionGitState
(
.
)
;
const
=
await
this
.
_resolveBaseBranchName
(
)
;
let
=
await
this
.
.
getSessionGitState
(
,
)
??
;
let
=
?.
??
await
this
.
.
getCurrentBranch
(
)
;
if
(
!
)
{
throw
new
ProtocolError
(
.
,
`Could not determine current branch for ${workingDirectory}`
)
;
}
const
=
await
this
.
.
getDefaultBranch
(
)
;
const
=
??
?.
??
?.
;
if
(
!
)
{
throw
new
ProtocolError
(
.
,
`Could not determine base branch for ${workingDirectory}`
)
;
}
const
=
this
.
.
getRepoResource
(
)
;
const
=
this
.
.
getAuthToken
(
{
:
.
,
:
.
,
}
)
;
if
(
!
)
{
throw
new
ProtocolError
(
AHP_AUTH_REQUIRED
,
localize
(
'agentHost.changeset.pr.authRequired'
,
"Sign in to GitHub with repository access to create a pull request."
)
,
[
]
,
)
;
}
const
=
await
this
.
.
hasUncommittedChanges
(
)
;
// Create a new branch if the current branch is the same
// as the base branch and there are uncommitted changes
if
(
&&
===
)
{
const
=
.
?.
[
.
]
;
try
{
const
=
await
this
.
.
generateBranchName
(
{
:
URI
.
parse
(
)
.
.
split
(
'/'
)
.
filter
(
)
.
pop
(
)
??
,
:
.
.
find
(
=>
.
.
.
trim
(
)
)
?.
.
,
:
,
,
:
typeof
===
'string'
?
:
undefined
,
branchNameCollides
:
=>
this
.
.
branchExists
(
,
)
.
catch
(
(
)
=>
true
)
,
}
)
;
this
.
_throwIfCancelled
(
)
;
this
.
.
info
(
`[AgentHostPullRequestOperationHandler] Creating branch ${generatedBranchName} for session ${sessionUri}`
)
;
await
this
.
.
createBranch
(
,
,
{
:
true
}
)
;
=
;
=
await
this
.
.
getSessionGitState
(
,
)
;
}
catch
(
)
{
this
.
_throwIfCancelled
(
)
;
throw
new
ProtocolError
(
.
,
`Failed to create a branch before creating a pull request: ${err instanceofError? err.message :String(err)}`
)
;
}
}
if
(
)
{
this
.
_throwIfCancelled
(
)
;
this
.
.
info
(
`[AgentHostPullRequestOperationHandler] Committing uncommitted changes for session ${sessionUri}`
)
;
try
{
await
this
.
.
commitAll
(
,
this
.
_formatCommitMessage
(
)
)
;
}
catch
(
)
{
this
.
_throwIfCancelled
(
)
;
throw
new
ProtocolError
(
.
,
`Failed to commit changes before creating a pull request: ${err instanceofError? err.message :String(err)}`
)
;
}
}
this
.
_throwIfCancelled
(
)
;
const
=
await
this
.
.
computeSessionFileDiffs
(
,
{
,
:
}
)
;
if
(
===
undefined
)
{
throw
new
ProtocolError
(
.
,
localize
(
'agentHost.changeset.pr.computeChangesFailed'
,
"Could not compute branch changes to create a pull request."
)
)
;
}
if
(
!==
undefined
&&
.
===
0
)
{
throw
new
ProtocolError
(
.
,
localize
(
'agentHost.changeset.pr.noChanges'
,
"There are no branch changes to create a pull request for."
)
)
;
}
this
.
_throwIfCancelled
(
)
;
const
=
?.
;
const
=
?
parseUpstreamBranchName
(
?.
)
:
undefined
;
const
=
&&
?
:
.
;
const
=
?.
??
;
const
=
===
?
:
`${branchName}:${headBranch}`
;
const
=
===
.
?
:
`${headOwner}:${headBranch}`
;
this
.
.
info
(
`[AgentHostPullRequestOperationHandler] Pushing branch ${branchName} to ${upstreamBranch?.remote ??'origin'} for session ${sessionUri}`
)
;
const
=
await
this
.
.
hasUpstream
(
,
)
;
this
.
_throwIfCancelled
(
)
;
try
{
await
this
.
.
push
(
,
{
:
?.
,
:
,
:
!
}
)
;
}
catch
(
)
{
this
.
_throwIfCancelled
(
)
;
throw
new
ProtocolError
(
.
,
`Failed to push branch '${branchName}': ${err instanceofError? err.message :String(err)}`
/**
* Notifies listeners that the pull request now exists, optionally enables
* auto-merge with the configured {@link AutoMergeMethod} (best-effort: a
* failure to enable auto-merge does not fail the operation), and builds the
* result message describing what happened.
*/
private
async
_finalize
(
:
,
:
boolean
,
:
string
,
:
string
,
:
string
,
:
string
,
:
string
,
:
,
:
,
)
:
Promise
<
>
{
if
(
!
this
.
)
{
// No auto-merge configured
this
.
_onPullRequestCreated
(
{
:
,
:
.
,
}
)
;
return
this
.
_createResult
(
,
this
.
_buildMessage
(
,
,
'none'
,
undefined
)
)
;
}
let
:
string
|
undefined
;
let
:
'none'
|
'enabled'
|
'failed'
=
'none'
;
if
(
.
)
{
try
{
await
this
.
.
enablePullRequestAutoMerge
(
.
,
this
.
,
,
)
;
=
'enabled'
;
}
catch
(
)
{
this
.
_throwIfCancelled
(
)
;
=
instanceof
Error
?
.
:
String
(
)
;
=
'failed'
;
this
.
.
warn
(
`[AgentHostPullRequestOperationHandler] Failed to enable auto-merge for ${owner}/${repo}#${pr.number}: ${autoMergeError}`
)
;
}
}
else
{
=
localize
(
'agentHost.changeset.pr.autoMerge.noNodeId'
,
"the pull request identifier was not returned by GitHub."
"Pull request [#{0}]({1}) already exists, but auto-merge could not be enabled: {2}"
,
.
number
,
.
,
??
''
)
;
default
:
return
localize
(
'agentHost.changeset.pr.existing'
,
"Pull request [#{0}]({1}) already exists."
,
.
number
,
.
)
;
}
}
switch
(
)
{
case
'enabled'
:
return
localize
(
'agentHost.changeset.pr.created.autoMerge'
,
"Created pull request [#{0}]({1}) with auto-merge ({2}) enabled."
,
.
number
,
.
,
)
;
case
'failed'
:
return
localize
(
'agentHost.changeset.pr.created.autoMergeFailed'
,
"Created pull request [#{0}]({1}), but auto-merge could not be enabled: {2}"
,
.
number
,
.
,
??
''
)
;
default
:
return
this
.
?
localize
(
'agentHost.changeset.pr.createdDraft'
,
"Created draft pull request [#{0}]({1})."
,
.
number
,
.
)
:
localize
(
'agentHost.changeset.pr.created'
,
"Created pull request [#{0}]({1})."
,
.
number
,
.
)
;
}
}
private
_throwIfCancelled
(
:
)
:
void
{
if
(
.
)
{
throw
new
ProtocolError
(
.
,
localize
(
'agentHost.changeset.pr.cancelled'
,
"Pull request operation was cancelled."
)
)
;
}
}
private
_formatTitle
(
:
string
)
:
string
{
// Beautify a branch name like `feat/foo-bar` into `feat: foo bar`.
const
=
.
indexOf
(
'/'
)
;
if
(
>
0
&&
<
.
-
1
)
{
const
=
.
substring
(
0
,
)
;
const
=
.
substring
(
+
1
)
.
replace
(
/[-_]+/g
,
' '
)
;
return
`${prefix}: ${rest}`
;
}
return
.
replace
(
/[-_]+/g
,
' '
)
;
}
private
_formatCommitMessage
(
:
string
)
:
string
{
return
localize
(
'agentHost.changeset.pr.commitMessage'
,
"Agent Host changes for {0}"
,
)
;
}
private
_formatBody
(
:
string
,
:
string
)
:
string
{
return
localize
(
'agentHost.changeset.pr.body'
,
"Created from `{0}` targeting `{1}`."
,
,
)
;
}
/**
* Best-effort generation of a PR title and description using the utility
* model. The model is given the main session conversation (only the
* markdown text of user requests and agent responses — tool calls,
* subagents, and reasoning are excluded and the text is character-bounded)
* along with a summary of the changed files. Returns `undefined` when no
* Copilot OAuth credential is available or generation fails, so the caller can fall
* back to the branch-name based title/description. PR creation must never
* fail just because the model is unavailable.
*/
private
async
_generateTitleAndDescription
(
:
,
:
string
,
:
string
,
:
readonly
[
]
,
:
,
:
,
)
:
Promise
<
{
:
string
;
:
string
}
|
undefined
>
{
const
=
this
.
.
getCopilotResource
(
)
;
const
=
this
.
.
getAuthToken
(
{
:
.
,
:
.
,
}
)
;
if
(
!
)
{
return
undefined
;
}
const
=
buildConversationContext
(
.
,
{
:
MAX_PR_CONVERSATION_CONTEXT_CHARS
}
)
;
const
=
this
.
_summarizeDiffsForPrompt
(
)
;
if
(
!
&&
!
)
{
return
undefined
;
}
try
{
const
=
await
this
.
.
utilityChatCompletion
(
,
{
:
this
.
_buildTitleAndDescriptionPrompt
(
,
,
,
)
,
}
,
{
}
)
;
this
.
_throwIfCancelled
(
)
;
return
this
.
_parseTitleAndDescription
(
)
;
}
catch
(
)
{
if
(
.
)
{
return
undefined
;
}
this
.
.
warn
(
`[AgentHostPullRequestOperationHandler] Failed to generate PR title and description: ${err instanceofError? err.message :String(err)}`
)
;
return
undefined
;
}
}
private
_buildTitleAndDescriptionPrompt
(
:
string
,
:
string
,
:
string
|
undefined
,
:
string
)
:
[
]
{
const
:
string
[
]
=
[
`Branch: ${branchName}`
,
`Base branch: ${base}`
,
]
;
if
(
)
{
.
push
(
`Changed files:\n${changeSummary}`
)
;
}
if
(
)
{
.
push
(
`Conversation (the request that produced these changes):\n${conversation}`
)
;
}
return
[
{
:
'system'
,
:
[
'You write clear, concise GitHub pull request titles and descriptions.'
,
'The first line of your reply is the PR title: a short imperative summary under 72 characters, with no "Title:" prefix, no surrounding quotes, and no markdown heading.'
,
'After the title, add one blank line, then write the PR description in GitHub-flavored markdown.'
,
'Summarize what changed and why, grounded in the conversation and changed files. Use a short paragraph and/or bullet points.'
,
'Do not invent changes that are not supported by the provided context, and do not wrap the whole reply in code fences.'