src/vs/platform/agentHost/node/agentHostDiscardChangesOperationHandler.ts
87 LOC · 87 covered · 0 uncovered · 26 ranges · 931 concepts · 14 introducers · 490 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
agentHostDiscardChangesOperationHandler.ts ×5
* 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 { basename } from '../../../base/common/resources.js';
import { URI } from '../../../base/common/uri.js';
import { localize } from '../../../nls.js';
import { ChangesetKind, parseChangesetUri } from '../common/changesetUri.js';
import { type IChangesetOperationHandler } from '../common/agentHostChangesetOperationService.js';
import { ChangesetOperationTargetKind, type InvokeChangesetOperationParams, type InvokeChangesetOperationResult } from '../common/state/protocol/channels-changeset/commands.js';
import { AHP_SESSION_NOT_FOUND, JsonRpcErrorCodes, ProtocolError } from '../common/state/sessionProtocol.js';
import { type SessionState } from '../common/state/sessionState.js';
import { ILogService } from '../../log/common/log.js';
import { IAgentHostGitService } from '../common/agentHostGitService.js';
export class AgentHostDiscardChangesOperationHandler implements IChangesetOperationHandler {
public static readonly OPERATION_DISCARD_CHANGES = 'discard-changes';
constructor(
private readonly _getSessionState: (sessionKey: string) => SessionState | undefined,
agentHostDiscardChangesOperationHandler.ts ×1
@IAgentHostGitService private readonly _agentHostGitService: IAgentHostGitService,
@ILogService private readonly _logService: ILogService,
) { }
async invoke(params: InvokeChangesetOperationParams, token: CancellationToken): Promise<InvokeChangesetOperationResult> {
if (token.isCancellationRequested) {
}
const cancellationListener = token.onCancellationRequested(() => abortController.abort());
agentHostDiscardChangesOperationHandler.ts ×6
try {
return await this._invoke(params, token, abortController.signal);
} finally {
cancellationListener.dispose();
}
}
private async _invoke(params: InvokeChangesetOperationParams, token: CancellationToken, _signal: AbortSignal): Promise<InvokeChangesetOperationResult> {
const parsed = parseChangesetUri(params.channel);
agentHostDiscardChangesOperationHandler.ts ×6
if (!parsed || parsed.kind !== ChangesetKind.Uncommitted) {
throw new ProtocolError(JsonRpcErrorCodes.InvalidParams, `Not an uncommitted changeset URI: ${params.channel}`);
agentHostDiscardChangesOperationHandler.ts ×1
}
const sessionUri = parsed.sessionUri;
const sessionState = this._getSessionState(sessionUri);
if (!sessionState) {
throw new ProtocolError(AHP_SESSION_NOT_FOUND, `Session not found: ${sessionUri}`);
agentHostDiscardChangesOperationHandler.ts ×1
}
if (params.target?.kind !== ChangesetOperationTargetKind.Resource) {
agentHostDiscardChangesOperationHandler.ts ×6
JsonRpcErrorCodes.InvalidParams,
`Operation '${AgentHostDiscardChangesOperationHandler.OPERATION_DISCARD_CHANGES}' requires a resource target.`);
}
const workingDirectoryStr = sessionState.workingDirectories?.[0];
throw new ProtocolError(JsonRpcErrorCodes.InternalError, `Session has no working directory: ${sessionUri}`);
agentHostDiscardChangesOperationHandler.ts ×1
}
const workingDirectory = URI.parse(workingDirectoryStr);
const resource = URI.parse(params.target.resource);
this._logService.info(`[AgentHostDiscardChangesOperationHandler] Restoring '${resource.fsPath}' for session ${sessionUri}`);
try {
await this._agentHostGitService.restore(workingDirectory, [resource.fsPath]);
} catch (err) {
throw new ProtocolError(
JsonRpcErrorCodes.InternalError,
`Failed to discard changes: ${err instanceof Error ? err.message : String(err)}`);
}
return { message: { markdown: localize('agentHost.changeset.discardChanges.discarded', "Discarded changes to `{0}`.", basename(resource)) } };
private _throwIfCancelled(token: CancellationToken): void {
throw new ProtocolError(JsonRpcErrorCodes.InternalError, localize('agentHost.changeset.discardChanges.cancelled', "Discard changes operation was cancelled."));
agentHostDiscardChangesOperationHandler.ts ×2
}