import { VertexAI } from "@google-cloud/vertexai";
import { getEnv } from "@llamaindex/env";
import { streamConverter } from "../utils.js";
import { getText } from "./utils.js";
/* To use Google's Vertex AI backend, it doesn't use api key authentication.
 *
 * To authenticate for local development:
 *
 *     ```
 *     npm install @google-cloud/vertexai
 *     gcloud auth application-default login
 *     ```
 * For production the prefered method is via a service account, more
 * details: https://cloud.google.com/docs/authentication/
 *
 * */ export class GeminiVertexSession {
    vertex;
    preview = false;
    constructor(options){
        const project = options?.project ?? getEnv("GOOGLE_VERTEX_PROJECT");
        const location = options?.location ?? getEnv("GOOGLE_VERTEX_LOCATION");
        if (!project || !location) {
            throw new Error("Set Google Vertex project and location in GOOGLE_VERTEX_PROJECT and GOOGLE_VERTEX_LOCATION env variables");
        }
        this.vertex = new VertexAI({
            ...options,
            project,
            location
        });
        this.preview = options?.preview ?? false;
    }
    getGenerativeModel(metadata) {
        if (this.preview) return this.vertex.preview.getGenerativeModel(metadata);
        return this.vertex.getGenerativeModel(metadata);
    }
    getResponseText(response) {
        return getText(response);
    }
    async *getChatStream(result) {
        yield* streamConverter(result.stream, (response)=>({
                delta: this.getResponseText(response),
                raw: response
            }));
    }
    getCompletionStream(result) {
        return streamConverter(result.stream, (response)=>({
                text: this.getResponseText(response),
                raw: response
            }));
    }
}
