import { initDatabase } from "@aigne/sqlite";
import { ExportResultCode } from "@opentelemetry/core";
import { insertTrace, isBlocklet } from "../../core/util.js";
import { migrate } from "../../server/migrate.js";
import getAIGNEHomePath from "../../server/utils/image-home-path.js";
import saveFiles from "../../server/utils/save-files.js";
import { validateTraceSpans } from "./util.js";
class HttpExporter {
    dbPath;
    _db;
    upsert;
    async getDb() {
        if (isBlocklet)
            return;
        const db = await initDatabase({ url: this.dbPath, wal: true });
        await migrate(db);
        return db;
    }
    constructor({ dbPath, exportFn, }) {
        this.dbPath = dbPath;
        this._db ??= this.getDb();
        this.upsert =
            exportFn ??
                (isBlocklet
                    ? async () => {
                        console.warn("Please setup AIGNEObserver.setExportFn to collect tracing data from agents.");
                    }
                    : this._upsertWithSQLite);
    }
    async _upsertWithSQLite(validatedData) {
        const db = await this._db;
        if (!db)
            throw new Error("Database not initialized");
        for (const trace of validatedData) {
            const dataDir = getAIGNEHomePath();
            for (const key of ["files", "images"]) {
                const items = trace.attributes?.output?.[key];
                if (trace?.attributes?.output?.[key] && items?.length) {
                    trace.attributes.output[key] = await saveFiles(items, { dataDir });
                }
            }
            await insertTrace(db, trace);
        }
    }
    async export(spans, resultCallback) {
        try {
            await this.upsert(validateTraceSpans(spans));
            resultCallback({ code: ExportResultCode.SUCCESS });
        }
        catch (error) {
            console.warn("[Observability] Failed to export spans:", error.cause?.message || error.message);
            resultCallback({ code: ExportResultCode.FAILED });
        }
    }
    shutdown() {
        return Promise.resolve();
    }
}
export default HttpExporter;
