-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Very basic chat agent API/UX * Add custom name/avatar, and restore them in persisted sessions * Show agent subcommands on the top level * Show editor decorations for subcommands * Fix unit tests * Implement unregister * Revert slash command content widget change, still used by inline editor * Remove content widget reference * Fix leaked disposable
- Loading branch information
1 parent
6d8c845
commit bc6b75e
Showing
15 changed files
with
716 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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 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,65 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { DisposableMap } from 'vs/base/common/lifecycle'; | ||
import { revive } from 'vs/base/common/marshalling'; | ||
import { IProgress } from 'vs/platform/progress/common/progress'; | ||
import { ExtHostChatAgentsShape, ExtHostContext, MainContext, MainThreadChatAgentsShape } from 'vs/workbench/api/common/extHost.protocol'; | ||
import { IChatAgentMetadata, IChatAgentService } from 'vs/workbench/contrib/chat/common/chatAgents'; | ||
import { IChatSlashFragment } from 'vs/workbench/contrib/chat/common/chatSlashCommands'; | ||
import { IExtHostContext, extHostNamedCustomer } from 'vs/workbench/services/extensions/common/extHostCustomers'; | ||
|
||
|
||
@extHostNamedCustomer(MainContext.MainThreadChatAgents) | ||
export class MainThreadChatAgents implements MainThreadChatAgentsShape { | ||
|
||
private readonly _agents = new DisposableMap<number>; | ||
private readonly _pendingProgress = new Map<number, IProgress<IChatSlashFragment>>(); | ||
private readonly _proxy: ExtHostChatAgentsShape; | ||
|
||
constructor( | ||
extHostContext: IExtHostContext, | ||
@IChatAgentService private readonly _chatAgentService: IChatAgentService | ||
) { | ||
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostChatAgents); | ||
} | ||
|
||
$unregisterAgent(handle: number): void { | ||
this._agents.deleteAndDispose(handle); | ||
} | ||
|
||
dispose(): void { | ||
this._agents.clearAndDisposeAll(); | ||
} | ||
|
||
$registerAgent(handle: number, name: string, metadata: IChatAgentMetadata): void { | ||
if (!this._chatAgentService.hasAgent(name)) { | ||
// dynamic! | ||
this._chatAgentService.registerAgentData({ | ||
id: name, | ||
metadata: revive(metadata) | ||
}); | ||
} | ||
|
||
const d = this._chatAgentService.registerAgentCallback(name, async (prompt, progress, history, token) => { | ||
const requestId = Math.random(); | ||
this._pendingProgress.set(requestId, progress); | ||
try { | ||
return await this._proxy.$invokeAgent(handle, requestId, prompt, { history }, token); | ||
} finally { | ||
this._pendingProgress.delete(requestId); | ||
} | ||
}); | ||
this._agents.set(handle, d); | ||
} | ||
|
||
async $handleProgressChunk(requestId: number, chunk: IChatSlashFragment): Promise<void> { | ||
this._pendingProgress.get(requestId)?.report(revive(chunk)); | ||
} | ||
|
||
$unregisterCommand(handle: number): void { | ||
this._agents.deleteAndDispose(handle); | ||
} | ||
} |
This file contains 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 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 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,91 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { DeferredPromise, raceCancellation } from 'vs/base/common/async'; | ||
import { CancellationToken } from 'vs/base/common/cancellation'; | ||
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle'; | ||
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions'; | ||
import { ILogService } from 'vs/platform/log/common/log'; | ||
import { Progress } from 'vs/platform/progress/common/progress'; | ||
import { ExtHostChatAgentsShape, IMainContext, MainContext, MainThreadChatAgentsShape } from 'vs/workbench/api/common/extHost.protocol'; | ||
import { ExtHostChatProvider } from 'vs/workbench/api/common/extHostChatProvider'; | ||
import * as typeConvert from 'vs/workbench/api/common/extHostTypeConverters'; | ||
import { ChatMessageRole } from 'vs/workbench/api/common/extHostTypes'; | ||
import { IChatMessage } from 'vs/workbench/contrib/chat/common/chatProvider'; | ||
import type * as vscode from 'vscode'; | ||
|
||
export class ExtHostChatAgents implements ExtHostChatAgentsShape { | ||
|
||
private static _idPool = 0; | ||
|
||
private readonly _agents = new Map<number, { extension: ExtensionIdentifier; agent: vscode.ChatAgent }>(); | ||
private readonly _proxy: MainThreadChatAgentsShape; | ||
|
||
constructor( | ||
mainContext: IMainContext, | ||
private readonly _extHostChatProvider: ExtHostChatProvider, | ||
private readonly _logService: ILogService, | ||
) { | ||
this._proxy = mainContext.getProxy(MainContext.MainThreadChatAgents); | ||
} | ||
|
||
registerAgent(extension: ExtensionIdentifier, name: string, agent: vscode.ChatAgent, metadata: vscode.ChatAgentMetadata): IDisposable { | ||
const handle = ExtHostChatAgents._idPool++; | ||
this._agents.set(handle, { extension, agent }); | ||
this._proxy.$registerAgent(handle, name, metadata); | ||
|
||
return toDisposable(() => { | ||
this._proxy.$unregisterAgent(handle); | ||
this._agents.delete(handle); | ||
}); | ||
} | ||
|
||
async $invokeAgent(handle: number, requestId: number, prompt: string, context: { history: IChatMessage[] }, token: CancellationToken): Promise<any> { | ||
const data = this._agents.get(handle); | ||
if (!data) { | ||
this._logService.warn(`[CHAT](${handle}) CANNOT invoke agent because the agent is not registered`); | ||
return; | ||
} | ||
|
||
let done = false; | ||
function throwIfDone() { | ||
if (done) { | ||
throw new Error('Only valid while executing the command'); | ||
} | ||
} | ||
|
||
const commandExecution = new DeferredPromise<void>(); | ||
token.onCancellationRequested(() => commandExecution.complete()); | ||
setTimeout(() => commandExecution.complete(), 3 * 1000); | ||
this._extHostChatProvider.allowListExtensionWhile(data.extension, commandExecution.p); | ||
|
||
const task = data.agent( | ||
{ role: ChatMessageRole.User, content: prompt }, | ||
{ history: context.history.map(typeConvert.ChatMessage.to) }, | ||
new Progress<vscode.ChatAgentResponse>(p => { | ||
throwIfDone(); | ||
this._proxy.$handleProgressChunk(requestId, { content: isInteractiveProgressFileTree(p.message) ? p.message : p.message.value }); | ||
}), | ||
token | ||
); | ||
|
||
try { | ||
return await raceCancellation(Promise.resolve(task).then((v) => { | ||
if (v && 'followUp' in v) { | ||
const convertedFollowup = v?.followUp?.map(f => typeConvert.ChatFollowup.from(f)); | ||
return { followUp: convertedFollowup }; | ||
} | ||
return undefined; | ||
}), token); | ||
} finally { | ||
done = true; | ||
commandExecution.complete(); | ||
} | ||
} | ||
} | ||
|
||
function isInteractiveProgressFileTree(thing: unknown): thing is vscode.InteractiveProgressFileTree { | ||
return !!thing && typeof thing === 'object' && 'treeData' in thing; | ||
} |
This file contains 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
Oops, something went wrong.