|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import type * as vscode from 'vscode'; |
| 7 | +import { CancellationToken } from '../../../base/common/cancellation.js'; |
| 8 | +import { ILogService } from '../../../platform/log/common/log.js'; |
| 9 | +import { HookTypeValue } from '../../contrib/chat/common/promptSyntax/hookSchema.js'; |
| 10 | +import { isToolInvocationContext, IToolInvocationContext } from '../../contrib/chat/common/tools/languageModelToolsService.js'; |
| 11 | +import { IHookCommandDto, MainContext, MainThreadHooksShape } from '../common/extHost.protocol.js'; |
| 12 | +import { IChatHookExecutionOptions, IExtHostHooks } from '../common/extHostHooks.js'; |
| 13 | +import { IExtHostRpcService } from '../common/extHostRpcService.js'; |
| 14 | +import { HookCommandResultKind, IHookCommandResult, IHookResult } from '../../contrib/chat/common/hooksExecutionService.js'; |
| 15 | +import * as typeConverters from '../common/extHostTypeConverters.js'; |
| 16 | + |
| 17 | +export class WorkerExtHostHooks implements IExtHostHooks { |
| 18 | + |
| 19 | + private readonly _mainThreadProxy: MainThreadHooksShape; |
| 20 | + |
| 21 | + constructor( |
| 22 | + @IExtHostRpcService extHostRpc: IExtHostRpcService, |
| 23 | + @ILogService private readonly _logService: ILogService |
| 24 | + ) { |
| 25 | + this._mainThreadProxy = extHostRpc.getProxy(MainContext.MainThreadHooks); |
| 26 | + } |
| 27 | + |
| 28 | + async executeHook(hookType: HookTypeValue, options: IChatHookExecutionOptions, token?: CancellationToken): Promise<vscode.ChatHookResult[]> { |
| 29 | + if (!options.toolInvocationToken || !isToolInvocationContext(options.toolInvocationToken)) { |
| 30 | + throw new Error('Invalid or missing tool invocation token'); |
| 31 | + } |
| 32 | + |
| 33 | + const context = options.toolInvocationToken as IToolInvocationContext; |
| 34 | + |
| 35 | + const results = await this._mainThreadProxy.$executeHook(hookType, context.sessionResource, options.input, token ?? CancellationToken.None); |
| 36 | + return results.map(r => typeConverters.ChatHookResult.to(r as IHookResult)); |
| 37 | + } |
| 38 | + |
| 39 | + async $runHookCommand(_hookCommand: IHookCommandDto, _input: unknown, _token: CancellationToken): Promise<IHookCommandResult> { |
| 40 | + this._logService.debug('[WorkerExtHostHooks] Hook commands are not supported in web worker context'); |
| 41 | + |
| 42 | + // Web worker cannot run shell commands - return an error |
| 43 | + return { |
| 44 | + kind: HookCommandResultKind.Error, |
| 45 | + result: 'Hook commands are not supported in web worker context' |
| 46 | + }; |
| 47 | + } |
| 48 | +} |
0 commit comments