import { MetadataMode } from "../Node.js";
import { Response } from "../Response.js";
import { streamConverter } from "../llm/utils.js";
import { PromptMixin } from "../prompts/Mixin.js";
import { getResponseBuilder } from "./builders.js";
/**
 * A ResponseSynthesizer is used to generate a response from a query and a list of nodes.
 */ export class ResponseSynthesizer extends PromptMixin {
    responseBuilder;
    metadataMode;
    constructor({ responseBuilder, serviceContext, metadataMode = MetadataMode.NONE } = {}){
        super();
        this.responseBuilder = responseBuilder ?? getResponseBuilder(serviceContext);
        this.metadataMode = metadataMode;
    }
    _getPromptModules() {
        return {};
    }
    _getPrompts() {
        const prompts = this.responseBuilder.getPrompts?.();
        return {
            ...prompts
        };
    }
    _updatePrompts(promptsDict) {
        this.responseBuilder.updatePrompts?.(promptsDict);
    }
    async synthesize({ query, nodesWithScore, stream }) {
        const textChunks = nodesWithScore.map(({ node })=>node.getContent(this.metadataMode));
        if (stream) {
            const response = await this.responseBuilder.getResponse({
                query,
                textChunks,
                stream
            });
            return streamConverter(response, (chunk)=>new Response(chunk, nodesWithScore));
        }
        const response = await this.responseBuilder.getResponse({
            query,
            textChunks
        });
        return new Response(response, nodesWithScore);
    }
}
