Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/vs/platform/agentHost/common/agentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export interface IAgentToolStartEvent extends IAgentProgressEventBase {
/** Human-readable display name for this tool. */
readonly displayName: string;
/** Message describing the tool invocation in progress (e.g., "Running `echo hello`"). */
readonly invocationMessage: string;
readonly invocationMessage: StringOrMarkdown;
/** A representative input string for display in the UI (e.g., the shell command). */
readonly toolInput?: string;
/** Hint for the renderer about how to display this tool (e.g., 'terminal' for shell commands, 'subagent' for subagent-spawning tools). */
Expand Down
124 changes: 5 additions & 119 deletions src/vs/platform/agentHost/node/copilot/copilotAgentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type { PermissionRequest, PermissionRequestResult, Tool, ToolResultObject } from '@github/copilot-sdk';
import type { PermissionRequestResult, Tool, ToolResultObject } from '@github/copilot-sdk';
import { DeferredPromise } from '../../../../base/common/async.js';
import { Emitter } from '../../../../base/common/event.js';
import { Disposable, IReference, toDisposable } from '../../../../base/common/lifecycle.js';
import { URI } from '../../../../base/common/uri.js';
import { generateUuid } from '../../../../base/common/uuid.js';
import { IInstantiationService } from '../../../instantiation/common/instantiation.js';
import { ILogService } from '../../../log/common/log.js';
import { localize } from '../../../../nls.js';
import { IAgentAttachment, IAgentMessageEvent, IAgentProgressEvent, IAgentSubagentStartedEvent, IAgentToolCompleteEvent, IAgentToolReadyEvent, IAgentToolStartEvent } from '../../common/agentService.js';
import { IAgentAttachment, IAgentMessageEvent, IAgentProgressEvent, IAgentSubagentStartedEvent, IAgentToolCompleteEvent, IAgentToolStartEvent } from '../../common/agentService.js';
import { ISessionDatabase, ISessionDataService } from '../../common/sessionDataService.js';
import { SessionInputAnswerState, SessionInputAnswerValueKind, SessionInputQuestionKind, SessionInputResponseKind, ToolResultContentType, type ISessionInputAnswer, type ISessionInputRequest, type IPendingMessage, type IToolCallResult, type IToolResultContent } from '../../common/state/sessionState.js';
import { CopilotSessionWrapper } from './copilotSessionWrapper.js';
import { getEditFilePath, getInvocationMessage, getPastTenseMessage, getShellLanguage, getToolDisplayName, getToolInputString, getToolKind, isEditTool, isHiddenTool, isShellTool } from './copilotToolDisplay.js';
import { getEditFilePath, getInvocationMessage, getPastTenseMessage, getPermissionDisplay, getShellLanguage, getToolDisplayName, getToolInputString, getToolKind, isEditTool, isHiddenTool, isShellTool, tryStringify, type ITypedPermissionRequest } from './copilotToolDisplay.js';
import { FileEditTracker } from './fileEditTracker.js';
import { mapSessionEvents } from './mapSessionEvents.js';
import type { ShellManager } from './copilotShellTools.js';
Expand Down Expand Up @@ -66,119 +65,6 @@ interface IUserInputResponse {
wasFreeform: boolean;
}

/**
* Extends the SDK's {@link PermissionRequest} with the known extra properties
* that arrive on the index-signature. The SDK defines these as `[key: string]: unknown`
* so this interface adds proper types for the fields we actually use.
*/
interface ITypedPermissionRequest extends PermissionRequest {
/** File path — set for `read` permission requests. */
path?: string;
/** File path — set for `write` permission requests. */
fileName?: string;
/** Full shell command text — set for `shell` permission requests. */
fullCommandText?: string;
/** Human-readable intention describing the operation. */
intention?: string;
/** MCP server name — set for `mcp` permission requests. */
serverName?: string;
/** Tool name — set for `mcp` and `custom-tool` permission requests. */
toolName?: string;
/** Tool arguments — set for `custom-tool` permission requests. */
args?: Record<string, unknown>;
}

function tryStringify(value: unknown): string | undefined {
try {
return JSON.stringify(value);
} catch {
return undefined;
}
}

/**
* Derives display fields from a permission request for the tool confirmation UI.
*/
/** Safely extract a string value from an SDK field that may be `unknown` at runtime. */
function str(value: unknown): string | undefined {
return typeof value === 'string' ? value : undefined;
}

function getPermissionDisplay(request: ITypedPermissionRequest): {
confirmationTitle: string;
invocationMessage: string;
toolInput?: string;
/** Normalized permission kind for auto-approval routing. */
permissionKind: IAgentToolReadyEvent['permissionKind'];
} {
const path = str(request.path) ?? str(request.fileName);
const fullCommandText = str(request.fullCommandText);
const intention = str(request.intention);
const serverName = str(request.serverName);
const toolName = str(request.toolName);

switch (request.kind) {
case 'shell':
return {
confirmationTitle: localize('copilot.permission.shell.title', "Run in terminal"),
invocationMessage: intention ?? localize('copilot.permission.shell.message', "Run command"),
toolInput: fullCommandText,
permissionKind: 'shell',
};
case 'custom-tool': {
// Custom tool overrides (e.g. our shell tool). Extract the actual
// tool args from the SDK's wrapper envelope.
const args = typeof request.args === 'object' && request.args !== null ? request.args as Record<string, unknown> : undefined;
const command = typeof args?.command === 'string' ? args.command : undefined;
const sdkToolName = str(request.toolName);
if (command && sdkToolName && isShellTool(sdkToolName)) {
return {
confirmationTitle: localize('copilot.permission.shell.title', "Run in terminal"),
invocationMessage: localize('copilot.permission.shell.message', "Run command"),
toolInput: command,
permissionKind: 'shell',
};
}
return {
confirmationTitle: toolName ?? localize('copilot.permission.default.title', "Permission request"),
invocationMessage: localize('copilot.permission.default.message', "Permission request"),
toolInput: args ? tryStringify(args) : tryStringify(request),
permissionKind: request.kind,
};
}
case 'write':
return {
confirmationTitle: localize('copilot.permission.write.title', "Write file"),
invocationMessage: path ? localize('copilot.permission.write.message', "Edit {0}", path) : localize('copilot.permission.write.messageGeneric', "Edit file"),
toolInput: tryStringify(path ? { path } : request) ?? undefined,
permissionKind: 'write',
};
case 'mcp': {
const title = toolName ?? localize('copilot.permission.mcp.defaultTool', "MCP Tool");
return {
confirmationTitle: serverName ? `${serverName}: ${title}` : title,
invocationMessage: serverName ? `${serverName}: ${title}` : title,
toolInput: tryStringify({ serverName, toolName }) ?? undefined,
permissionKind: 'mcp',
};
}
case 'read':
return {
confirmationTitle: localize('copilot.permission.read.title', "Read file"),
invocationMessage: intention ?? localize('copilot.permission.read.message', "Read file"),
toolInput: tryStringify(path ? { path, intention } : request) ?? undefined,
permissionKind: 'read',
};
default:
return {
confirmationTitle: localize('copilot.permission.default.title', "Permission request"),
invocationMessage: localize('copilot.permission.default.message', "Permission request"),
toolInput: tryStringify(request) ?? undefined,
permissionKind: request.kind,
};
}
}

/**
* Options for constructing a {@link CopilotAgentSession}.
*/
Expand Down Expand Up @@ -481,7 +367,7 @@ export class CopilotAgentSession extends Disposable {
this._pendingPermissions.set(toolCallId, deferred);

// Derive display information from the permission request kind
const { confirmationTitle, invocationMessage, toolInput, permissionKind } = getPermissionDisplay(request);
const { confirmationTitle, invocationMessage, toolInput, permissionKind, permissionPath } = getPermissionDisplay(request);

// Fire a tool_ready event to transition the tool to PendingConfirmation
this._onDidSessionProgress.fire({
Expand All @@ -492,7 +378,7 @@ export class CopilotAgentSession extends Disposable {
toolInput,
confirmationTitle,
permissionKind,
permissionPath: str(request.path) ?? str(request.fileName),
permissionPath,
});

const approved = await deferred.p;
Expand Down
Loading
Loading