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
14 changes: 10 additions & 4 deletions src/vs/platform/agentHost/node/agentChatContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ function createAgentChatSessionSettings(snapshot: ISessionStateSnapshot | undefi
* registry at every {@link IAgentChatOwnerRef.get}. Before the chat is
* registered (a provider creating it) it answers `session`, the session the
* operation was issued for; after the chat leaves the registry it answers the
* last owner it saw, so teardown keeps addressing that session.
* last owner it saw, so teardown keeps addressing that session. Before the
* session is registered, `recordedDefaultChat` supplies its persisted chat format.
*/
export function createChatOwnerRef(stateManager: AgentHostStateManager, chat: URI | ProtocolURI, session: URI | ProtocolURI): IAgentChatOwnerRef {
export function createChatOwnerRef(stateManager: AgentHostStateManager, chat: URI | ProtocolURI, session: URI | ProtocolURI, recordedDefaultChat?: ProtocolURI): IAgentChatOwnerRef {
const chatKey = toKey(chat);
const sessionKey = toKey(session);
let last = toKey(session);
let lastUri = toUri(session);
const getOwner = () => {
Expand All @@ -67,7 +69,10 @@ export function createChatOwnerRef(stateManager: AgentHostStateManager, chat: UR
};
return {
get: getOwner,
getSubagentChatUriFormat: () => chatUriFormatOf(stateManager.getDefaultChat(getOwner().toString())),
getSubagentChatUriFormat: () => {
const owner = getOwner().toString();
return chatUriFormatOf(stateManager.getDefaultChat(owner) ?? (owner === sessionKey ? recordedDefaultChat : undefined));
},
isDefaultChat: () => {
const defaultChat = stateManager.getDefaultChat(getOwner().toString());
return defaultChat === undefined ? undefined : defaultChat === chatKey;
Expand Down Expand Up @@ -171,9 +176,10 @@ function buildAgentChatContext(stateManager: AgentHostStateManager, authenticati
: isChatWorkspaceless(stateManager, chatKey, sessionState, sessionSettings?.workspaceless === true);
const isToolChat = typeof origin === 'object' && origin !== null && !Array.isArray(origin) && origin.kind === ChatOriginKind.Tool;
const chatSurface = stateManager.getSessionSurfaceMeta(sessionKey)?.surface;
const recordedDefaultChat = sessionState && [...sessionState.chats].find(([, entry]) => entry.isDefault)?.[0];
return {
resource: storageResource === sessionKey ? sessionUri : storageResource === chatKey ? toUri(chat) : URI.parse(storageResource),
owner: createChatOwnerRef(stateManager, chat, session),
owner: createChatOwnerRef(stateManager, chat, session, recordedDefaultChat),
sessionSettings,
workspaceless,
launchWorkingDirectories: isToolChat || workspaceless || !launch?.length ? undefined : launch.map(entry => URI.parse(entry)),
Expand Down
1 change: 1 addition & 0 deletions src/vs/platform/agentHost/node/copilot/copilotAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9510,6 +9510,7 @@ export class CopilotAgent extends Disposable implements IAgent {
const backing = this._chatBackings.get(context.chatKey);
const result = await mapSessionEvents(context.resource, reference.object, events, context.chat, context.ownerSession, createCopilotSessionPathMapper(metadata.workingDirectory && toCopilotWorkspace(metadata.workingDirectory), resourceJoinPath(this._copilotHomeService.home, 'session-state', sdkSessionId)), {
logService: this._logService,
resolveSubagentChatUri: toolCallId => subagentChatUri(context.ownerRef.getSubagentChatUriFormat(), context.ownerSession.toString(), context.chatKey, toolCallId),
workingDirectory: metadata.workingDirectory,
clientToolNames: new Set(this._activeClients.get(context.ownerSession)?.toolSet.merged().map(tool => tool.name) ?? []),
model: backing?.model ?? metadata.model,
Expand Down
27 changes: 19 additions & 8 deletions src/vs/platform/agentHost/node/copilot/copilotAgentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,10 @@ export class CopilotAgentSession extends Disposable {
markdownPartIds: new Map<string, string>(),
};
private readonly _resolveAgentName = (agentId: string) => this._subagents.getDisplayName(agentId);
private readonly _resolveSubagentChat = (agentId: string) => {
const toolCallId = this._subagents.getToolCallId(agentId);
return toolCallId === undefined ? undefined : this._subagentChatUri(toolCallId);
};
readonly modelCallTurnCorrelation = new ModelCallTurnCorrelation();
readonly compactionTurnCorrelation = new CompactionTurnCorrelation();
private readonly _subagentDirectUsageByToolCallId = new Map<string, DirectUsageAccumulator>();
Expand Down Expand Up @@ -3425,6 +3429,7 @@ export class CopilotAgentSession extends Disposable {
continue;
}
agentTaskIds.add(task.id);
this._subagents.seedToolCallId(task.id, task.toolCallId);
const displayName = subagentChatTitle(task.description, task.displayName);
if (displayName) {
this._subagents.seedDisplayName(task.id, displayName);
Expand Down Expand Up @@ -3708,7 +3713,7 @@ export class CopilotAgentSession extends Disposable {
return {
parameters,
meta: this._createToolCallMeta(toolName, parameters),
invocationMessage: getStreamingInvocationMessage(toolName, getToolDisplayName(toolName), partialInput, path => this._resolveDisplayFilePath(path), this._resolveAgentName),
invocationMessage: getStreamingInvocationMessage(toolName, getToolDisplayName(toolName), partialInput, path => this._resolveDisplayFilePath(path), this._resolveAgentName, this._resolveSubagentChat),
};
}

Expand Down Expand Up @@ -3864,7 +3869,7 @@ export class CopilotAgentSession extends Disposable {
toolCallId,
...(toolCall.contributor ? { contributor: toolCall.contributor } : {}),
...(toolCall.intention !== undefined ? { intention: toolCall.intention } : {}),
invocationMessage: getInvocationMessage(toolCall.toolName, toolCall.displayName, toolCall.parameters, path => this._resolveDisplayFilePath(path), this._resolveAgentName),
invocationMessage: getInvocationMessage(toolCall.toolName, toolCall.displayName, toolCall.parameters, path => this._resolveDisplayFilePath(path), this._resolveAgentName, this._resolveSubagentChat),
toolInput: getToolInputString(toolCall.toolName, toolCall.parameters, tryStringify(toolCall.parameters)),
confirmed: ToolCallConfirmationReason.NotNeeded,
_meta: toolCall.meta && toToolCallMeta(toolCall.meta),
Expand Down Expand Up @@ -4834,7 +4839,7 @@ export class CopilotAgentSession extends Disposable {
toolCallId,
...(tracked.contributor ? { contributor: tracked.contributor } : {}),
...(tracked.intention !== undefined ? { intention: tracked.intention } : {}),
invocationMessage: getInvocationMessage(tracked.toolName, tracked.displayName, tracked.parameters, path => this._resolveDisplayFilePath(path), this._resolveAgentName),
invocationMessage: getInvocationMessage(tracked.toolName, tracked.displayName, tracked.parameters, path => this._resolveDisplayFilePath(path), this._resolveAgentName, this._resolveSubagentChat),
toolInput: getToolInputString(tracked.toolName, tracked.parameters, tracked.parameters ? tryStringify(tracked.parameters) : undefined),
confirmed: ToolCallConfirmationReason.NotNeeded,
_meta: toToolCallMeta({ ...(tracked.meta ?? {}), toolSearchCandidates: candidates }),
Expand Down Expand Up @@ -8049,6 +8054,7 @@ export class CopilotAgentSession extends Disposable {
return this._withDatabase(async db => {
const result = await mapSessionEvents(this._storageUri, db, events, this._chatChannelUri, this._ownerSessionUri, this._pathMapper, {
logService: this._logService,
resolveSubagentChatUri: toolCallId => this._subagentChatUri(toolCallId),
workingDirectory: this._workingDirectory,
clientToolNames: this._clientToolNames,
resolveSkill: (name, origin) => this._mcpCustomizations.skillForInvocation(name, origin),
Expand Down Expand Up @@ -8078,6 +8084,11 @@ export class CopilotAgentSession extends Disposable {
}

private _seedSubagentDisplayNames(events: readonly SessionEvent[]): void {
for (const event of events) {
if ((event.type === 'subagent.started' || event.type === 'subagent.completed' || event.type === 'subagent.failed') && event.agentId) {
this._subagents.seedToolCallId(event.agentId, event.data.toolCallId);
}
}
for (const [agentId, displayName] of getCopilotSubagentDisplayNames(events)) {
this._subagents.seedDisplayName(agentId, displayName);
}
Expand Down Expand Up @@ -9000,7 +9011,7 @@ export class CopilotAgentSession extends Disposable {
toolCallId,
toolName: request.toolName,
displayName,
invocationMessage: getInvocationMessage(request.toolName, displayName, parameters, path => this._resolveDisplayFilePath(path), this._resolveAgentName),
invocationMessage: getInvocationMessage(request.toolName, displayName, parameters, path => this._resolveDisplayFilePath(path), this._resolveAgentName, this._resolveSubagentChat),
toolInput: getToolInputString(request.toolName, parameters, tryStringify(parameters)),
riskAssessment: judgeRationale
? {
Expand Down Expand Up @@ -11023,7 +11034,7 @@ export class CopilotAgentSession extends Disposable {
this._nonPtyShellTerminals.completeBackgroundShell(e.data.kind.shellId, e.data.kind.exitCode);
}
this._seedSubagentDisplayNames([e]);
const notification = buildCopilotSystemNotification(e, this._resolveAgentName);
const notification = buildCopilotSystemNotification(e, this._resolveAgentName, this._resolveSubagentChat);
if (!notification) {
this._logService.trace(`[Copilot:${sessionId}] Ignoring system.notification kind=${e.data.kind.type}`);
return;
Expand Down Expand Up @@ -11576,7 +11587,7 @@ export class CopilotAgentSession extends Disposable {
toolCallId: e.data.toolCallId,
...(contributor ? { contributor } : {}),
...(intention !== undefined ? { intention } : {}),
invocationMessage: getInvocationMessage(e.data.toolName, displayName, parameters, path => this._resolveDisplayFilePath(path), this._resolveAgentName),
invocationMessage: getInvocationMessage(e.data.toolName, displayName, parameters, path => this._resolveDisplayFilePath(path), this._resolveAgentName, this._resolveSubagentChat),
toolInput: getToolInputString(e.data.toolName, parameters, toolArgs),
confirmed: ToolCallConfirmationReason.NotNeeded,
_meta: toToolCallMeta(meta),
Expand Down Expand Up @@ -11616,7 +11627,7 @@ export class CopilotAgentSession extends Disposable {
toolCallId: e.data.toolCallId,
...(contributor ? { contributor } : {}),
...(intention !== undefined ? { intention } : {}),
invocationMessage: getInvocationMessage(e.data.toolName, displayName, parameters, path => this._resolveDisplayFilePath(path), this._resolveAgentName),
invocationMessage: getInvocationMessage(e.data.toolName, displayName, parameters, path => this._resolveDisplayFilePath(path), this._resolveAgentName, this._resolveSubagentChat),
toolInput: getToolInputString(e.data.toolName, parameters, toolArgs),
confirmed: ToolCallConfirmationReason.NotNeeded,
_meta: toToolCallMeta(clientToolAutoApproved ? { ...meta, autoApproveBySetting: true } : meta),
Expand Down Expand Up @@ -11829,7 +11840,7 @@ export class CopilotAgentSession extends Disposable {
const complete = (authoritativeOutput?: string) => {
const result: ToolCallResult = {
success: e.data.success,
pastTenseMessage: getPastTenseMessage(tracked.toolName, displayName, tracked.parameters, e.data.success, e.data.success ? toolOutput : undefined, path => this._resolveDisplayFilePath(path), this._resolveAgentName, tracked.meta?.[imageGenerationToolMetaKey]),
pastTenseMessage: getPastTenseMessage(tracked.toolName, displayName, tracked.parameters, e.data.success, e.data.success ? toolOutput : undefined, path => this._resolveDisplayFilePath(path), this._resolveAgentName, tracked.meta?.[imageGenerationToolMetaKey], this._resolveSubagentChat),
content: content.length > 0 ? content : undefined,
structuredContent: e.data.result?.structuredContent as Record<string, unknown> | undefined,
resultMetadata: getSdkMcpResultMeta(e.data.result),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export class CopilotSubagentTracking extends Disposable {
return this._toolCallsByAgent.get(agentId);
}

seedToolCallId(agentId: string, toolCallId: string): void {
if (!this._toolCallsByAgent.has(agentId)) {
this._toolCallsByAgent.set(agentId, toolCallId);
}
}

getAgentId(toolCallId: string): string | undefined {
for (const [agentId, candidate] of this._toolCallsByAgent) {
if (candidate === toolCallId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { softAssertNever } from '../../../../base/common/assert.js';
import { appendEscapedMarkdownInlineCode } from '../../../../base/common/htmlContent.js';
import { localize } from '../../../../nls.js';
import { subagentChatTitle } from '../../common/agent.js';
import { getSubagentMetadata, getToolKind, type ToolAgentNameResolver } from './copilotToolDisplay.js';
import { formatSubagentChatLink, getSubagentMetadata, getToolKind, type ToolAgentNameResolver } from './copilotToolDisplay.js';

export interface ICopilotSystemNotification {
/** Text for a new system-origin AHP turn; derived from SDK `data.kind` metadata, e.g. shell completion `description`. */
Expand Down Expand Up @@ -69,7 +69,7 @@ export function getCopilotSubagentDisplayNames(events: readonly SessionEvent[]):
return names;
}

export function buildCopilotSystemNotification(event: SessionEventPayload<'system.notification'>, resolveAgentName?: ToolAgentNameResolver): ICopilotSystemNotification | undefined {
export function buildCopilotSystemNotification(event: SessionEventPayload<'system.notification'>, resolveAgentName?: ToolAgentNameResolver, resolveAgentChat?: (agentId: string) => string | undefined): ICopilotSystemNotification | undefined {
const data = event.data;
const kind: SystemNotification = data.kind;
const content = cleanSystemNotificationContent(data.content);
Expand All @@ -91,6 +91,18 @@ export function buildCopilotSystemNotification(event: SessionEventPayload<'syste
case 'agent_completed':
case 'agent_idle': {
const name = resolveAgentName?.(kind.agentId)?.trim() || getCopilotSubagentDisplayInfo(event)?.displayName;
const chat = name ? resolveAgentChat?.(kind.agentId) : undefined;
if (name && chat) {
const link = formatSubagentChatLink(name, chat);
return {
messageText: kind.type === 'agent_idle'
? localize('agentHost.copilot.systemNotification.linkedAgentIdle', "{0} is complete", link)
: kind.status === 'failed'
? localize('agentHost.copilot.systemNotification.linkedAgentFailed', "{0} failed", link)
: localize('agentHost.copilot.systemNotification.linkedAgentCompleted', "{0} completed", link),
startsTurn: true,
};
}
const formattedName = name ? appendEscapedMarkdownInlineCode(name) : undefined;
if (kind.type === 'agent_idle') {
return {
Expand Down
Loading