
import { __decorate, __param } from '../../../../../../../../external/tslib/tslib.es6.js';
import { findLast } from '../../../../../base/common/arraysFind.js';
import { Emitter } from '../../../../../base/common/event.js';
import { Iterable } from '../../../../../base/common/iterator.js';
import { Disposable, toDisposable } from '../../../../../base/common/lifecycle.js';
import { revive } from '../../../../../base/common/marshalling.js';
import { equalsIgnoreCase } from '../../../../../base/common/strings.js';
import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.service.js';
import { ContextKeyExpr } from '../../../../../platform/contextkey/common/contextkey.js';
import { IContextKeyService } from '../../../../../platform/contextkey/common/contextkey.service.js';
import { ExtensionIdentifier } from '../../../../../platform/extensions/common/extensions.js';
import { ChatContextKeys } from '../actions/chatContextKeys.js';
import { ChatAgentLocation, ChatModeKind, ChatConfiguration } from '../constants.js';
import { ILanguageModelsService } from '../languageModels.service.js';

let ChatAgentService = class ChatAgentService extends Disposable {
    static {
        this.AGENT_LEADER = "@";
    }
    constructor(contextKeyService, configurationService) {
        super();
        this.contextKeyService = contextKeyService;
        this.configurationService = configurationService;
        this._agents = ( new Map());
        this._onDidChangeAgents = this._register(( new Emitter()));
        this.onDidChangeAgents = this._onDidChangeAgents.event;
        this._agentsContextKeys = ( new Set());
        this._hasToolsAgent = false;
        this._chatParticipantDetectionProviders = ( new Map());
        this._agentCompletionProviders = ( new Map());
        this._hasDefaultAgent = ChatContextKeys.enabled.bindTo(this.contextKeyService);
        this._extensionAgentRegistered = ChatContextKeys.extensionParticipantRegistered.bindTo(this.contextKeyService);
        this._defaultAgentRegistered = ChatContextKeys.panelParticipantRegistered.bindTo(this.contextKeyService);
        this._register(contextKeyService.onDidChangeContext(e => {
            if (e.affectsSome(this._agentsContextKeys)) {
                this._updateContextKeys();
            }
        }));
    }
    registerAgent(id, data) {
        const existingAgent = this.getAgent(id);
        if (existingAgent) {
            throw ( new Error(`Agent already registered: ${JSON.stringify(id)}`));
        }
        const that = this;
        const commands = data.slashCommands;
        data = {
            ...data,
            get slashCommands() {
                return commands.filter(
                    c => !c.when || that.contextKeyService.contextMatchesRules(ContextKeyExpr.deserialize(c.when))
                );
            }
        };
        const entry = {
            data
        };
        this._agents.set(id, entry);
        this._updateAgentsContextKeys();
        this._updateContextKeys();
        this._onDidChangeAgents.fire(undefined);
        return toDisposable(() => {
            this._agents.delete(id);
            this._updateAgentsContextKeys();
            this._updateContextKeys();
            this._onDidChangeAgents.fire(undefined);
        });
    }
    _updateAgentsContextKeys() {
        this._agentsContextKeys.clear();
        for (const agent of ( this._agents.values())) {
            if (agent.data.when) {
                const expr = ContextKeyExpr.deserialize(agent.data.when);
                for (const key of expr?.keys() || []) {
                    this._agentsContextKeys.add(key);
                }
            }
        }
    }
    _updateContextKeys() {
        let extensionAgentRegistered = false;
        let defaultAgentRegistered = false;
        let toolsAgentRegistered = false;
        for (const agent of this.getAgents()) {
            if (agent.isDefault) {
                if (!agent.isCore) {
                    extensionAgentRegistered = true;
                }
                if (agent.id === "chat.setup" || agent.id === "github.copilot.editsAgent") {
                    toolsAgentRegistered = true;
                } else {
                    defaultAgentRegistered = true;
                }
            }
        }
        this._defaultAgentRegistered.set(defaultAgentRegistered);
        this._extensionAgentRegistered.set(extensionAgentRegistered);
        if (toolsAgentRegistered !== this._hasToolsAgent) {
            this._hasToolsAgent = toolsAgentRegistered;
            this._onDidChangeAgents.fire(this.getDefaultAgent(ChatAgentLocation.Chat, ChatModeKind.Agent));
        }
    }
    registerAgentImplementation(id, agentImpl) {
        const entry = this._agents.get(id);
        if (!entry) {
            throw ( new Error(`Unknown agent: ${JSON.stringify(id)}`));
        }
        if (entry.impl) {
            throw ( new Error(`Agent already has implementation: ${JSON.stringify(id)}`));
        }
        if (entry.data.isDefault) {
            this._hasDefaultAgent.set(true);
        }
        entry.impl = agentImpl;
        this._onDidChangeAgents.fire(( new MergedChatAgent(entry.data, agentImpl)));
        return toDisposable(() => {
            entry.impl = undefined;
            this._onDidChangeAgents.fire(undefined);
            if (entry.data.isDefault) {
                this._hasDefaultAgent.set(( Iterable.some(( this._agents.values()), agent => agent.data.isDefault && !!agent.impl)));
            }
        });
    }
    registerDynamicAgent(data, agentImpl) {
        data.isDynamic = true;
        const agent = {
            data,
            impl: agentImpl
        };
        this._agents.set(data.id, agent);
        this._onDidChangeAgents.fire(( new MergedChatAgent(data, agentImpl)));
        return toDisposable(() => {
            this._agents.delete(data.id);
            this._onDidChangeAgents.fire(undefined);
        });
    }
    registerAgentCompletionProvider(id, provider) {
        this._agentCompletionProviders.set(id, provider);
        return {
            dispose: () => {
                this._agentCompletionProviders.delete(id);
            }
        };
    }
    async getAgentCompletionItems(id, query, token) {
        return (await this._agentCompletionProviders.get(id)?.(query, token)) ?? [];
    }
    updateAgent(id, updateMetadata) {
        const agent = this._agents.get(id);
        if (!agent?.impl) {
            throw ( new Error(`No activated agent with id ${JSON.stringify(id)} registered`));
        }
        agent.data.metadata = {
            ...agent.data.metadata,
            ...updateMetadata
        };
        this._onDidChangeAgents.fire(( new MergedChatAgent(agent.data, agent.impl)));
    }
    getDefaultAgent(location, mode = ChatModeKind.Ask) {
        return this._preferExtensionAgent(this.getActivatedAgents().filter(a => {
            if (mode && !a.modes.includes(mode)) {
                return false;
            }
            return !!a.isDefault && a.locations.includes(location);
        }));
    }
    get hasToolsAgent() {
        return !!this.configurationService.getValue(ChatConfiguration.AgentEnabled);
    }
    getContributedDefaultAgent(location) {
        return this._preferExtensionAgent(
            this.getAgents().filter(a => !!a.isDefault && a.locations.includes(location))
        );
    }
    _preferExtensionAgent(agents) {
        return findLast(agents, agent => !agent.isCore) ?? agents.at(-1);
    }
    getAgent(id, includeDisabled = false) {
        if (!this._agentIsEnabled(id) && !includeDisabled) {
            return;
        }
        return this._agents.get(id)?.data;
    }
    _agentIsEnabled(idOrAgent) {
        const entry = typeof idOrAgent === "string" ? this._agents.get(idOrAgent) : idOrAgent;
        return !entry?.data.when || this.contextKeyService.contextMatchesRules(ContextKeyExpr.deserialize(entry.data.when));
    }
    getAgentByFullyQualifiedId(id) {
        const agent = Iterable.find(( this._agents.values()), a => getFullyQualifiedId(a.data) === id)?.data;
        if (agent && !this._agentIsEnabled(agent.id)) {
            return;
        }
        return agent;
    }
    getAgents() {
        return ( Array.from(( this._agents.values())).map(entry => entry.data)).filter(a => this._agentIsEnabled(a.id));
    }
    getActivatedAgents() {
        return ( Array.from(( this._agents.values())).filter(a => !!a.impl).filter(a => this._agentIsEnabled(a.data.id)).map(a => ( new MergedChatAgent(a.data, a.impl))));
    }
    getAgentsByName(name) {
        return this._preferExtensionAgents(this.getAgents().filter(a => a.name === name));
    }
    _preferExtensionAgents(agents) {
        const extensionAgents = agents.filter(a => !a.isCore);
        return extensionAgents.length > 0 ? extensionAgents : agents;
    }
    agentHasDupeName(id) {
        const agent = this.getAgent(id);
        if (!agent) {
            return false;
        }
        return this.getAgentsByName(agent.name).filter(a => a.extensionId.value !== agent.extensionId.value).length > 0;
    }
    async invokeAgent(id, request, progress, history, token) {
        const data = this._agents.get(id);
        if (!data?.impl) {
            throw ( new Error(`No activated agent with id "${id}"`));
        }
        return await data.impl.invoke(request, progress, history, token);
    }
    setRequestTools(id, requestId, tools) {
        const data = this._agents.get(id);
        if (!data?.impl) {
            throw ( new Error(`No activated agent with id "${id}"`));
        }
        data.impl.setRequestTools?.(requestId, tools);
    }
    setYieldRequested(id, requestId, value) {
        const data = this._agents.get(id);
        if (!data?.impl) {
            return;
        }
        data.impl.setYieldRequested?.(requestId, value);
    }
    async getFollowups(id, request, result, history, token) {
        const data = this._agents.get(id);
        if (!data?.impl?.provideFollowups) {
            return [];
        }
        return data.impl.provideFollowups(request, result, history, token);
    }
    async getChatTitle(id, history, token) {
        const data = this._agents.get(id);
        if (!data?.impl?.provideChatTitle) {
            return undefined;
        }
        return data.impl.provideChatTitle(history, token);
    }
    async getChatSummary(id, history, token) {
        const data = this._agents.get(id);
        if (!data?.impl?.provideChatSummary) {
            return undefined;
        }
        return data.impl.provideChatSummary(history, token);
    }
    registerChatParticipantDetectionProvider(handle, provider) {
        this._chatParticipantDetectionProviders.set(handle, provider);
        return toDisposable(() => {
            this._chatParticipantDetectionProviders.delete(handle);
        });
    }
    hasChatParticipantDetectionProviders() {
        return this._chatParticipantDetectionProviders.size > 0;
    }
    async detectAgentOrCommand(request, history, options, token) {
        const provider = Iterable.first(( this._chatParticipantDetectionProviders.values()));
        if (!provider) {
            return;
        }
        const participants = this.getAgents().reduce((acc, a) => {
            if (a.locations.includes(options.location)) {
                acc.push({
                    participant: a.id,
                    disambiguation: a.disambiguation ?? []
                });
                for (const command of a.slashCommands) {
                    acc.push({
                        participant: a.id,
                        command: command.name,
                        disambiguation: command.disambiguation ?? []
                    });
                }
            }
            return acc;
        }, []);
        const result = await provider.provideParticipantDetection(request, history, {
            ...options,
            participants
        }, token);
        if (!result) {
            return;
        }
        const agent = this.getAgent(result.participant);
        if (!agent) {
            return;
        }
        if (!result.command) {
            return {
                agent
            };
        }
        const command = agent?.slashCommands.find(c => c.name === result.command);
        if (!command) {
            return;
        }
        return {
            agent,
            command
        };
    }
};
ChatAgentService = ( __decorate([( __param(0, IContextKeyService)), ( __param(1, IConfigurationService))], ChatAgentService));
class MergedChatAgent {
    constructor(data, impl) {
        this.data = data;
        this.impl = impl;
    }
    get id() {
        return this.data.id;
    }
    get name() {
        return this.data.name ?? "";
    }
    get fullName() {
        return this.data.fullName ?? "";
    }
    get description() {
        return this.data.description ?? "";
    }
    get extensionId() {
        return this.data.extensionId;
    }
    get extensionVersion() {
        return this.data.extensionVersion;
    }
    get extensionPublisherId() {
        return this.data.extensionPublisherId;
    }
    get extensionPublisherDisplayName() {
        return this.data.publisherDisplayName;
    }
    get extensionDisplayName() {
        return this.data.extensionDisplayName;
    }
    get isDefault() {
        return this.data.isDefault;
    }
    get isCore() {
        return this.data.isCore;
    }
    get metadata() {
        return this.data.metadata;
    }
    get slashCommands() {
        return this.data.slashCommands;
    }
    get locations() {
        return this.data.locations;
    }
    get modes() {
        return this.data.modes;
    }
    get disambiguation() {
        return this.data.disambiguation;
    }
    async invoke(request, progress, history, token) {
        return this.impl.invoke(request, progress, history, token);
    }
    setRequestTools(requestId, tools) {
        this.impl.setRequestTools?.(requestId, tools);
    }
    setYieldRequested(requestId, value) {
        this.impl.setYieldRequested?.(requestId, value);
    }
    async provideFollowups(request, result, history, token) {
        if (this.impl.provideFollowups) {
            return this.impl.provideFollowups(request, result, history, token);
        }
        return [];
    }
    toJSON() {
        return this.data;
    }
}
let ChatAgentNameService = class ChatAgentNameService {
    constructor(languageModelsService) {
        this.languageModelsService = languageModelsService;
    }
    getAgentNameRestriction(chatAgentData) {
        if (chatAgentData.isCore) {
            return true;
        }
        const nameAllowed = this.checkAgentNameRestriction(chatAgentData.name, chatAgentData).get();
        const fullNameAllowed = !chatAgentData.fullName || this.checkAgentNameRestriction(chatAgentData.fullName.replace(/\s/g, ""), chatAgentData).get();
        return nameAllowed && fullNameAllowed;
    }
    checkAgentNameRestriction(name, chatAgentData) {
        const allowList = ( this.languageModelsService.restrictedChatParticipants.map(registry => registry[name.toLowerCase()]));
        return ( allowList.map(allowList => {
            if (!allowList) {
                return true;
            }
            return ( allowList.some(id => equalsIgnoreCase(
                id,
                id.includes(".") ? chatAgentData.extensionId.value : chatAgentData.extensionPublisherId
            )));
        }));
    }
};
ChatAgentNameService = ( __decorate([( __param(0, ILanguageModelsService))], ChatAgentNameService));
function getFullyQualifiedId(chatAgentData) {
    return `${chatAgentData.extensionId.value}.${chatAgentData.id}`;
}
function isSerializableChatAgentData(obj) {
    return obj.name !== undefined;
}
function reviveSerializedAgent(raw) {
    const normalized = isSerializableChatAgentData(raw) ? raw : {
        ...raw,
        name: raw.id
    };
    if (!normalized.extensionPublisherId) {
        normalized.extensionPublisherId = raw.extensionPublisher ?? "";
    }
    if (!normalized.extensionDisplayName) {
        normalized.extensionDisplayName = "";
    }
    if (!normalized.extensionId) {
        normalized.extensionId = ( new ExtensionIdentifier(""));
    }
    return revive(normalized);
}

export { ChatAgentNameService, ChatAgentService, MergedChatAgent, getFullyQualifiedId, reviveSerializedAgent };
