import { AgentOutputType, ChatMessageBuilder, } from '../agent-core/index.js';
import { GoalVerifierAgent } from '../agents/index.js';
import { LlmAgentFunctionBase } from './utils/index.js';
export class VerifyGoalAchievedFunction extends LlmAgentFunctionBase {
    constructor(llm, tokenizer) {
        super(llm, tokenizer);
    }
    name = 'verifyGoalAchieved';
    description = 'Verifies that the goal has been achieved.';
    parameters = {
        type: 'object',
        properties: {},
        required: [],
        additionalProperties: false,
    };
    onSuccess(name, rawParams, messages, result) {
        return {
            outputs: [result],
            messages: [
                ChatMessageBuilder.functionCall(name, rawParams),
                ...messages.map(x => ({
                    role: 'assistant',
                    content: x,
                })),
                ChatMessageBuilder.functionCallResult(name, result.content || 'Successfully accomplished the task.'),
            ],
        };
    }
    onFailure(name, params, rawParams, error) {
        return {
            outputs: [
                {
                    type: AgentOutputType.Error,
                    title: `"${name}" failed to accomplish the task "${params.task}"`,
                    content: `Error: ${error}`,
                },
            ],
            messages: [ChatMessageBuilder.functionCall(name, rawParams), ChatMessageBuilder.functionCallResult(name, `Error: ${error}`)],
        };
    }
    buildExecutor({ context }) {
        return async (params, rawParams) => {
            const result = await this.askAgent(new GoalVerifierAgent(context.cloneEmpty()), { messagesToVerify: context.chat.messages }, context);
            if (!result.ok) {
                return this.onFailure(this.name, params, rawParams, result.error);
            }
            return this.onSuccess(this.name, rawParams, [], result.value.output);
        };
    }
}
