import { toDisposable } from '../../../base/common/lifecycle.js';
import { MainContext } from './extHost.protocol.js';
import { LanguageModelToolDescription } from './extHostTypeConverters.js';
class ExtHostLanguageModelTools {
    constructor(mainContext) {
        this._registeredTools = ( new Map());
        this._allTools = ( new Map());
        this._proxy = ( mainContext.getProxy(MainContext.MainThreadLanguageModelTools));
        this._proxy.$getTools().then(tools => {
            for (const tool of tools) {
                this._allTools.set(tool.name, tool);
            }
        });
    }
    async invokeTool(name, parameters, token) {
        return await this._proxy.$invokeTool(name, parameters, token);
    }
    async $acceptToolDelta(delta) {
        if (delta.added) {
            this._allTools.set(delta.added.name, delta.added);
        }
        if (delta.removed) {
            this._allTools.delete(delta.removed);
        }
    }
    get tools() {
        return ( Array.from(( this._allTools.values()))
            .map(tool => LanguageModelToolDescription.to(tool)));
    }
    async $invokeTool(name, parameters, token) {
        const item = this._registeredTools.get(name);
        if (!item) {
            throw ( new Error(`Unknown tool ${name}`));
        }
        return await item.tool.invoke(parameters, token);
    }
    registerTool(extension, name, tool) {
        this._registeredTools.set(name, { extension, tool });
        this._proxy.$registerTool(name);
        return toDisposable(() => {
            this._registeredTools.delete(name);
            this._proxy.$unregisterTool(name);
        });
    }
}
export { ExtHostLanguageModelTools };
