import { AgentOutputType, ChatMessageBuilder } from '../agent-core/index.js';
import { AgentFunctionBase } from './utils/index.js';
export class OnGoalAchievedFunction extends AgentFunctionBase {
    name = 'agent_onGoalAchieved';
    description = "Informs the user that the goal has been achieved. Returns as message a complete and explicit answer for user's question";
    parameters = {
        type: 'object',
        properties: {
            message: {
                type: 'string',
                description: "Complete and explicit answer for user's question",
            },
        },
        required: ['message'],
        additionalProperties: false,
    };
    buildExecutor(agent) {
        return async (params, rawParams) => {
            return new Promise((resolve, reject) => {
                try {
                    resolve(this.onSuccess(agent, params, rawParams, params.message));
                }
                catch (error) {
                    reject(this.onFailure(agent, params, rawParams, error.toString()));
                }
            });
        };
    }
    onSuccess(agent, params, rawParams, result) {
        return {
            outputs: [
                {
                    type: AgentOutputType.Success,
                    title: `[${agent.config.prompts.name}] ${this.name}`,
                    content: params.message,
                },
            ],
            messages: [ChatMessageBuilder.functionCall(this.name, rawParams), ChatMessageBuilder.functionCallResult(this.name, result)],
        };
    }
    onFailure(agent, params, rawParams, error) {
        return {
            outputs: [
                {
                    type: AgentOutputType.Error,
                    title: `[${agent.config.prompts.name}] Error in ${this.name}: ${error}`,
                    content: params.message,
                },
            ],
            messages: [
                ChatMessageBuilder.functionCall(this.name, rawParams),
                ChatMessageBuilder.functionCallResult(this.name, `Failed calling ${this.name}:\n${error}`),
            ],
        };
    }
}
