/** * @license * Copyright 2026 aphrody-code * SPDX-License-Identifier: Apache-2.0 */ import { homedir } from "node:os"; import { join } from "node:path"; import { existsSync } from "node:fs"; import { GeminiRustClient } from "./rust.ts"; import { GeminiPythonClient } from "./python.ts"; import { GeminiWebAdapter } from "./web.ts"; import { loadCookieJar } from "../cookies/cookie-loader.ts"; import { resolveCookiePath } from "../utils/paths.ts"; import type { GeminiAskOptions, GeminiChatResponse, GeminiEngineStatus } from "./types.ts"; export * from "./types.ts"; export * from "./rust.ts"; export * from "./python.ts"; export * from "./web.ts"; export class GeminiToolbox { public rust: GeminiRustClient; public python: GeminiPythonClient; public web: GeminiWebAdapter; private cookiePath: string; constructor(cookiePath?: string) { this.cookiePath = cookiePath ? resolveCookiePath(cookiePath) : resolveCookiePath("gemini"); this.rust = new GeminiRustClient(this.cookiePath); this.python = new GeminiPythonClient(); this.web = new GeminiWebAdapter(this.cookiePath); } async getStatus(): Promise { const home = homedir(); const aphrodyHome = join(home, ".aphrody"); const agentHome = join(home, ".gemini", "antigravity-cli"); let cookieCount = 0; let webAvailable = false; let webError: string | undefined; try { const cookies = await loadCookieJar(this.cookiePath); cookieCount = cookies.length; webAvailable = cookies.some(c => c.name === "__Secure-1PSID"); if (!webAvailable) { webError = "Missing __Secure-1PSID in cookie jar"; } } catch (e: any) { webError = e.message; } const rustAvailable = this.rust.isAvailable(); const pythonAvailable = this.python.isAvailable(); const pythonPath = this.python.getPythonPath() ?? "not found"; return { web: { available: webAvailable, cookiePath: this.cookiePath, cookieCount, error: webError, }, rust: { available: rustAvailable, dllPath: process.env.BXC_RUST_BRIDGE_LIB || "target/release/bxc_rust_bridge.dll", }, python: { available: pythonAvailable, pythonPath, sdk: "google-genai / aphrody / antigravity-sdk-python", }, aphrody: { available: existsSync(aphrodyHome), homeDir: aphrodyHome, agentHomeDir: agentHome, }, }; } async ask(prompt: string, opts: GeminiAskOptions = {}): Promise { const engine = opts.engine ?? "auto"; if (engine === "rust") { return await this.rust.ask(prompt, opts); } if (engine === "python") { return await this.python.ask(prompt, opts); } if (engine === "web") { return await this.web.ask(prompt, opts); } // Auto strategy: Try Rust -> Web -> Python try { return await this.rust.ask(prompt, opts); } catch (rustErr) { try { return await this.web.ask(prompt, opts); } catch (webErr) { if (this.python.isAvailable()) { return await this.python.ask(prompt, opts); } throw new Error(`All Gemini engines failed. Rust: ${rustErr}; Web: ${webErr}`); } } } async chat(conversationKey: string, prompt: string, opts: GeminiAskOptions = {}): Promise { return await this.web.chat(conversationKey, prompt, opts); } async getConversations(cookiePath?: string) { return await this.web.getConversations(cookiePath); } } export const defaultGeminiToolbox = new GeminiToolbox();