import { ResultErr, ResultOk } from '@polywrap/result';
import { AgentFunctionBase } from './AgentFunctionBase.js';
import { LlmQueryBuilder } from '../../agent-core/index.js';
export class LlmAgentFunctionBase extends AgentFunctionBase {
    llm;
    tokenizer;
    constructor(llm, tokenizer) {
        super();
        this.llm = llm;
        this.tokenizer = tokenizer;
    }
    queryBuilder() {
        return new LlmQueryBuilder(this.llm, this.tokenizer);
    }
    askLlm(question, opts) {
        return this.queryBuilder().message('user', question).build().content(opts);
    }
    async askAgent(agent, runArgs, context) {
        const iterator = agent.run(runArgs);
        const messages = [];
        while (true) {
            const response = await iterator.next();
            if (response.done) {
                if (!response.value.ok) {
                    return ResultErr(response.value.error);
                }
                return ResultOk({ output: response.value.value, messages });
            }
            if (response.value.type === 'message' && response.value.content) {
                messages.push(response.value.content);
            }
            response.value && (await context.logger.info(response.value.title));
        }
    }
}
