/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { BaseDeclarativeTool, BaseToolInvocation, Kind, } from './tools.js';
function summarizePrompt(prompt, maxLength = 160) {
    if (!prompt) {
        return '';
    }
    const firstLine = prompt
        .split(/\r?\n/)
        .map((line) => line.trim())
        .find((line) => line.length > 0) ?? prompt.trim();
    if (firstLine.length <= maxLength) {
        return firstLine;
    }
    const truncated = firstLine.slice(0, maxLength - 1).trimEnd();
    return `${truncated}…`;
}
class ListSubagentsToolInvocation extends BaseToolInvocation {
    subagentManager;
    constructor(params, subagentManager) {
        super(params);
        this.subagentManager = subagentManager;
    }
    getDescription() {
        return 'Enumerate registered subagents with their profiles and descriptions.';
    }
    async execute() {
        const names = await this.subagentManager.listSubagents();
        if (names.length === 0) {
            const message = 'No subagents are currently registered. Use the /subagent CLI command to create one.';
            return {
                llmContent: message,
                returnDisplay: message,
                metadata: { count: 0 },
            };
        }
        const summaries = [];
        for (const name of names.sort()) {
            try {
                const config = await this.subagentManager.loadSubagent(name);
                summaries.push(this.buildSummary(config));
            }
            catch (error) {
                const detail = error instanceof Error ? error.message : String(error ?? 'unknown');
                summaries.push({
                    name,
                    profile: 'unknown',
                    updatedAt: '',
                    description: `Unable to load subagent: ${detail}`,
                });
            }
        }
        const display = summaries
            .map((summary) => {
            const profilePart = summary.profile && summary.profile !== 'unknown'
                ? ` (profile: ${summary.profile})`
                : '';
            return `- **${summary.name}**${profilePart} — ${summary.description}`;
        })
            .join('\n') || 'No subagents are currently registered.';
        return {
            llmContent: JSON.stringify(summaries, null, 2),
            returnDisplay: display,
            metadata: { count: summaries.length },
        };
    }
    buildSummary(config) {
        return {
            name: config.name,
            profile: config.profile,
            updatedAt: config.updatedAt,
            description: summarizePrompt(config.systemPrompt),
        };
    }
}
/**
 * Tool that enumerates all available subagents and surfaces their metadata.
 */
export class ListSubagentsTool extends BaseDeclarativeTool {
    config;
    dependencies;
    static Name = 'list_subagents';
    constructor(config, dependencies = {}) {
        super(ListSubagentsTool.Name, 'ListSubagents', 'Lists all configured subagents with their associated profiles and high-level descriptions.', Kind.Read, {
            type: 'object',
            properties: {},
            additionalProperties: false,
        });
        this.config = config;
        this.dependencies = dependencies;
    }
    createInvocation(params) {
        const manager = this.dependencies.getSubagentManager?.() ??
            (typeof this.config.getSubagentManager === 'function'
                ? this.config.getSubagentManager()
                : undefined);
        if (!manager) {
            throw new Error('SubagentManager service is unavailable. Please configure subagents before invoking this tool.');
        }
        return new ListSubagentsToolInvocation(params, manager);
    }
    validateToolParamValues() {
        return null;
    }
}
//# sourceMappingURL=list-subagents.js.map