-
Notifications
You must be signed in to change notification settings - Fork 40.1k
feat: implement GitHub pull request operations in agent host #318256
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
Draft
DonJayamanne
wants to merge
5
commits into
main
Choose a base branch
from
don/pr-changeset-action
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f0a34f3
feat: implement GitHub pull request operations in agent host
DonJayamanne f5692fd
fix: update error handling for session not found in PullRequestOperat…
DonJayamanne ff9d00b
WIP
DonJayamanne 5c17ffe
fix: bind context for pull request creation handlers in AgentHostPull…
DonJayamanne ce8f901
fix: update pull request messages to use markdown format in tests
DonJayamanne 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { CancellationToken } from '../../../base/common/cancellation.js'; | ||
| import type { IDisposable } from '../../../base/common/lifecycle.js'; | ||
| import type { InvokeChangesetOperationParams, InvokeChangesetOperationResult } from './state/protocol/channels-changeset/commands.js'; | ||
| import type { ChangesetOperation, ISessionGitState } from './state/sessionState.js'; | ||
|
|
||
| /** | ||
| * Server-side handler for a changeset operation advertised via | ||
| * `changeset/operationsChanged`. | ||
| * | ||
| * The agent service validates the request shape (changeset exists, operation id | ||
| * known, target scope matches) before invoking the handler; the handler is only | ||
| * responsible for executing the operation. | ||
| */ | ||
| export interface IChangesetOperationHandler { | ||
| invoke(params: InvokeChangesetOperationParams, token: CancellationToken): Promise<InvokeChangesetOperationResult>; | ||
| } | ||
|
|
||
| /** | ||
| * Context used by changeset operation contributions to decide which operations | ||
| * to advertise for a session changeset. | ||
| * | ||
| * Keep this interface intentionally small. Add new fields here only when a | ||
| * contribution genuinely needs them to compute operation availability. Likely | ||
| * future additions include the concrete changeset URI, the session state, the | ||
| * changeset state, or the working directory URI. | ||
| */ | ||
| export interface IChangesetOperationContext { | ||
| /** String form of the session URI that owns the changeset. */ | ||
| readonly sessionKey: string; | ||
| /** Current git metadata for the session. This is enough for the PR operations today. */ | ||
| readonly gitState: ISessionGitState; | ||
| } | ||
|
|
||
| export interface IChangesetOperationRegistry { | ||
| registerChangesetOperationHandler(operationId: string, handler: IChangesetOperationHandler): IDisposable; | ||
| onDidChangeOperations(sessionKey: string): void; | ||
| refreshSessionGitState(sessionKey: string): Promise<void>; | ||
| } | ||
|
|
||
| export interface IChangesetOperationContribution extends IDisposable { | ||
| registerHandlers(registry: IChangesetOperationRegistry): IDisposable; | ||
| getOperations(context: IChangesetOperationContext): readonly ChangesetOperation[] | undefined; | ||
| } | ||
|
|
||
| export interface IChangesetOperationContributionService extends IDisposable { | ||
| registerContribution(contribution: IChangesetOperationContribution): IDisposable; | ||
| getOperations(context: IChangesetOperationContext): readonly ChangesetOperation[] | undefined; | ||
| refreshOperationsFromCurrentState(sessionKey: string): void; | ||
| updateOperations(sessionKey: string, gitState: ISessionGitState): void; | ||
| invokeChangesetOperation(params: InvokeChangesetOperationParams): Promise<InvokeChangesetOperationResult>; | ||
| } |
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
126 changes: 126 additions & 0 deletions
126
src/vs/platform/agentHost/node/agentHostChangesetOperationContributionService.ts
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,126 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { Disposable, DisposableMap, toDisposable, type IDisposable } from '../../../base/common/lifecycle.js'; | ||
| import { CancellationToken } from '../../../base/common/cancellation.js'; | ||
| import { buildSessionChangesetUri } from '../common/changesetUri.js'; | ||
| import type { InvokeChangesetOperationParams, InvokeChangesetOperationResult } from '../common/state/protocol/channels-changeset/commands.js'; | ||
| import { AHP_SESSION_NOT_FOUND, JsonRpcErrorCodes, ProtocolError } from '../common/state/sessionProtocol.js'; | ||
| import { ActionType } from '../common/state/sessionActions.js'; | ||
| import { ChangesetOperationScope, ChangesetOperationTargetKind, readSessionGitState, type ChangesetOperation, type ISessionGitState } from '../common/state/sessionState.js'; | ||
| import type { IChangesetOperationContribution, IChangesetOperationContributionService, IChangesetOperationContext, IChangesetOperationHandler, IChangesetOperationRegistry } from '../common/changesetOperation.js'; | ||
| import { AgentHostStateManager } from './agentHostStateManager.js'; | ||
| import { AgentHostSessionGitStateService } from './agentHostSessionGitStateService.js'; | ||
|
|
||
| export class AgentHostChangesetOperationContributionService extends Disposable implements IChangesetOperationContributionService { | ||
|
|
||
| private readonly _contributions = new Set<IChangesetOperationContribution>(); | ||
| private readonly _handlerRegistrations = this._register(new DisposableMap<IChangesetOperationContribution>()); | ||
| private readonly _changesetOperationHandlers = new Map<string, IChangesetOperationHandler>(); | ||
| private readonly _registry: IChangesetOperationRegistry; | ||
|
|
||
| constructor( | ||
| private readonly _stateManager: AgentHostStateManager, | ||
| private readonly _sessionGitStateService: AgentHostSessionGitStateService, | ||
| ) { | ||
| super(); | ||
| this._registry = { | ||
| registerChangesetOperationHandler: (operationId, handler) => this._registerChangesetOperationHandler(operationId, handler), | ||
| onDidChangeOperations: sessionKey => this.refreshOperationsFromCurrentState(sessionKey), | ||
| refreshSessionGitState: sessionKey => this._refreshSessionGitStateAndOperations(sessionKey), | ||
| }; | ||
| } | ||
|
|
||
| registerContribution(contribution: IChangesetOperationContribution): IDisposable { | ||
| if (this._contributions.has(contribution)) { | ||
| throw new Error('Changeset operation contribution already registered'); | ||
| } | ||
| this._contributions.add(contribution); | ||
| this._registerContributionHandlers(contribution); | ||
| return toDisposable(() => { | ||
| this._handlerRegistrations.deleteAndDispose(contribution); | ||
| this._contributions.delete(contribution); | ||
| contribution.dispose(); | ||
| }); | ||
| } | ||
|
|
||
| getOperations(context: IChangesetOperationContext): readonly ChangesetOperation[] | undefined { | ||
| const operations: ChangesetOperation[] = []; | ||
| for (const contribution of this._contributions) { | ||
| const contributed = contribution.getOperations(context); | ||
| if (contributed) { | ||
| operations.push(...contributed); | ||
| } | ||
| } | ||
| return operations.length > 0 ? operations : undefined; | ||
| } | ||
|
|
||
| refreshOperationsFromCurrentState(sessionKey: string): void { | ||
| const gitState = readSessionGitState(this._stateManager.getSessionState(sessionKey)?._meta); | ||
| if (!gitState) { | ||
| return; | ||
| } | ||
| this.updateOperations(sessionKey, gitState); | ||
| } | ||
|
|
||
| updateOperations(sessionKey: string, gitState: ISessionGitState): void { | ||
| const branchUri = buildSessionChangesetUri(sessionKey); | ||
| const operations = this.getOperations({ sessionKey, gitState }); | ||
| this._stateManager.dispatchServerAction(branchUri, { | ||
| type: ActionType.ChangesetOperationsChanged, | ||
| operations: operations ? [...operations] : undefined, | ||
| }); | ||
| } | ||
|
|
||
| private async _refreshSessionGitStateAndOperations(sessionKey: string): Promise<void> { | ||
| const gitState = await this._sessionGitStateService.refreshSessionGitState(sessionKey); | ||
| if (gitState) { | ||
| this.updateOperations(sessionKey, gitState); | ||
| } | ||
| } | ||
|
|
||
| async invokeChangesetOperation(params: InvokeChangesetOperationParams): Promise<InvokeChangesetOperationResult> { | ||
| const state = this._stateManager.getChangesetState(params.channel); | ||
| if (!state) { | ||
| throw new ProtocolError(AHP_SESSION_NOT_FOUND, `Changeset not found: ${params.channel}`); | ||
| } | ||
| const op = state.operations?.find(o => o.id === params.operationId); | ||
| if (!op) { | ||
| throw new ProtocolError(JsonRpcErrorCodes.InvalidParams, `Unknown operation '${params.operationId}' on changeset ${params.channel}`); | ||
| } | ||
| const targetKind: ChangesetOperationScope = params.target?.kind === ChangesetOperationTargetKind.Resource | ||
| ? ChangesetOperationScope.Resource | ||
| : params.target?.kind === ChangesetOperationTargetKind.Range | ||
| ? ChangesetOperationScope.Range | ||
| : ChangesetOperationScope.Changeset; | ||
| if (!op.scopes.includes(targetKind)) { | ||
| throw new ProtocolError(JsonRpcErrorCodes.InvalidParams, `Operation '${params.operationId}' does not support scope '${targetKind}' (allowed: ${op.scopes.join(', ')})`); | ||
| } | ||
| const handler = this._changesetOperationHandlers.get(params.operationId); | ||
| if (!handler) { | ||
| throw new ProtocolError(JsonRpcErrorCodes.InternalError, `No operation handler registered for '${params.operationId}' on changeset ${params.channel}`); | ||
| } | ||
| return handler.invoke(params, CancellationToken.None); | ||
| } | ||
|
|
||
| private _registerChangesetOperationHandler(operationId: string, handler: IChangesetOperationHandler): IDisposable { | ||
| if (this._changesetOperationHandlers.has(operationId)) { | ||
| throw new Error(`Changeset operation handler already registered for '${operationId}'`); | ||
| } | ||
| this._changesetOperationHandlers.set(operationId, handler); | ||
| return toDisposable(() => { | ||
| if (this._changesetOperationHandlers.get(operationId) === handler) { | ||
| this._changesetOperationHandlers.delete(operationId); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private _registerContributionHandlers(contribution: IChangesetOperationContribution): void { | ||
| if (this._handlerRegistrations.has(contribution)) { | ||
| return; | ||
| } | ||
| this._handlerRegistrations.set(contribution, contribution.registerHandlers(this._registry)); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/vs/platform/agentHost/node/agentHostChangesetOperationContributions.ts
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,22 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { DisposableStore, type IDisposable } from '../../../base/common/lifecycle.js'; | ||
| import type { IInstantiationService } from '../../instantiation/common/instantiation.js'; | ||
| import type { IChangesetOperationContributionService } from '../common/changesetOperation.js'; | ||
| import { AgentHostPullRequestOperationContribution } from './agentHostPullRequestOperationProvider.js'; | ||
| import type { AgentHostStateManager } from './agentHostStateManager.js'; | ||
|
|
||
| export function registerDefaultChangesetOperationContributions( | ||
| service: IChangesetOperationContributionService, | ||
| instantiationService: IInstantiationService, | ||
| stateManager: AgentHostStateManager, | ||
| ): IDisposable { | ||
| const store = new DisposableStore(); | ||
| store.add(service.registerContribution( | ||
| instantiationService.createInstance(AgentHostPullRequestOperationContribution, stateManager) | ||
| )); | ||
| return store; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Auth token changes.