ceulen 0.5.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +317 -12
- package/README.md +87 -91
- package/extensions/lib/advisor-marker.ts +33 -0
- package/extensions/lib/panel.ts +25 -2
- package/extensions/lib/rate.ts +24 -0
- package/extensions/lib/registry.ts +93 -7
- package/extensions/lib/tools.ts +47 -0
- package/extensions/lib/usage-store.ts +33 -0
- package/extensions/modules/advisor/commands/advisor.ts +244 -0
- package/extensions/modules/advisor/configPanel.ts +194 -0
- package/extensions/modules/advisor/index.ts +217 -0
- package/extensions/modules/advisor/lib/config.ts +248 -0
- package/extensions/modules/advisor/lib/emission-guard.ts +153 -0
- package/extensions/modules/advisor/lib/isolated-model.ts +160 -0
- package/extensions/modules/advisor/lib/model-picker.ts +47 -0
- package/extensions/modules/advisor/lib/watcher.ts +286 -0
- package/extensions/modules/advisor/test/compliance.test.ts +125 -0
- package/extensions/modules/advisor/test/config.test.ts +202 -0
- package/extensions/modules/advisor/test/configPanel.test.ts +223 -0
- package/extensions/modules/advisor/test/emission-guard.test.ts +201 -0
- package/extensions/modules/advisor/test/isolated-model.test.ts +311 -0
- package/extensions/modules/advisor/test/model-picker.test.ts +32 -0
- package/extensions/modules/advisor/test/module.test.ts +586 -0
- package/extensions/modules/advisor/test/watcher.test.ts +478 -0
- package/extensions/modules/classifier/configPanel.ts +104 -0
- package/extensions/modules/classifier/index.ts +297 -0
- package/extensions/modules/classifier/lib/settings.ts +95 -0
- package/extensions/modules/classifier/test/classifier.test.ts +475 -0
- package/extensions/modules/composer/index.ts +152 -3
- package/extensions/modules/composer/lib/footer.ts +119 -0
- package/extensions/modules/composer/lib/shapes.ts +265 -26
- package/extensions/modules/composer/test/editor.test.ts +1 -1
- package/extensions/modules/composer/test/footer.test.ts +96 -0
- package/extensions/modules/composer/test/module.test.ts +113 -11
- package/extensions/modules/composer/test/screen.test.ts +29 -1
- package/extensions/modules/composer/test/shapes.test.ts +224 -8
- package/extensions/modules/config/defaultTools.ts +96 -0
- package/extensions/modules/config/index.ts +147 -7
- package/extensions/modules/config/piSettings.ts +254 -69
- package/extensions/modules/config/test/defaultTools.test.ts +122 -0
- package/extensions/modules/config/test/menu.test.ts +64 -0
- package/extensions/modules/config/test/panel.test.ts +38 -23
- package/extensions/modules/config/test/piSettings.test.ts +130 -3
- package/extensions/modules/config/test/renderTools.test.ts +71 -0
- package/extensions/modules/config/test/tools.test.ts +146 -0
- package/extensions/modules/fff/index.ts +2094 -0
- package/extensions/modules/fff/lib/query.ts +129 -0
- package/extensions/modules/fff/test/index.test.ts +607 -0
- package/extensions/modules/fff/test/query.test.ts +71 -0
- package/extensions/modules/munin/configPanel.ts +105 -0
- package/extensions/modules/munin/index.ts +641 -0
- package/extensions/modules/munin/lib/helpers.ts +471 -0
- package/extensions/modules/munin/lib/retry.ts +20 -0
- package/extensions/modules/munin/lib/sdk.ts +218 -0
- package/extensions/modules/munin/test/config.test.ts +168 -0
- package/extensions/modules/munin/test/configPanel.test.ts +161 -0
- package/extensions/modules/munin/test/extension.test.ts +224 -0
- package/extensions/modules/munin/test/helpers.test.ts +523 -0
- package/extensions/modules/munin/test/retry.test.ts +145 -0
- package/extensions/modules/munin/test/stale-protocol.test.ts +554 -0
- package/extensions/modules/plan/configPanel.ts +152 -0
- package/extensions/modules/plan/index.ts +917 -0
- package/extensions/modules/plan/lib/plan-tools.ts +72 -0
- package/extensions/modules/plan/lib/prompt.ts +65 -0
- package/extensions/modules/plan/lib/settings.ts +166 -0
- package/extensions/modules/plan/lib/shell-gate.ts +200 -0
- package/extensions/modules/plan/test/configPanel.test.ts +96 -0
- package/extensions/modules/plan/test/harness.ts +133 -0
- package/extensions/modules/plan/test/module.test.ts +305 -0
- package/extensions/modules/plan/test/settings.test.ts +114 -0
- package/extensions/modules/plan/test/shell-gate.test.ts +103 -0
- package/extensions/modules/ponytail/index.ts +23 -73
- package/extensions/modules/ponytail/lib/config.ts +1 -6
- package/extensions/modules/ponytail/test/extension.test.ts +1 -85
- package/extensions/modules/repair/configPanel.ts +96 -0
- package/extensions/modules/repair/index.ts +414 -0
- package/extensions/modules/repair/lib/auto-bg.ts +333 -0
- package/extensions/modules/repair/lib/edit-repair.ts +155 -0
- package/extensions/modules/repair/lib/editor.ts +251 -0
- package/extensions/modules/repair/lib/guards.ts +34 -0
- package/extensions/modules/repair/lib/input-repair.ts +324 -0
- package/extensions/modules/repair/lib/patch.ts +481 -0
- package/extensions/modules/repair/lib/settings.ts +114 -0
- package/extensions/modules/repair/test/auto-bg.test.ts +405 -0
- package/extensions/modules/repair/test/edit-repair.test.ts +224 -0
- package/extensions/modules/repair/test/editor.test.ts +141 -0
- package/extensions/modules/repair/test/guards.test.ts +64 -0
- package/extensions/modules/repair/test/input-repair.test.ts +231 -0
- package/extensions/modules/repair/test/patch.test.ts +563 -0
- package/extensions/modules/repair/test/settings.test.ts +167 -0
- package/extensions/modules/repair/test/wrapper.test.ts +210 -0
- package/extensions/modules/router/commands/commands.ts +4 -0
- package/extensions/modules/router/lib/client.ts +61 -0
- package/extensions/modules/router/lib/provider.ts +34 -13
- package/extensions/modules/router/lib/systemone.ts +152 -0
- package/extensions/modules/router/test/unit.test.ts +179 -0
- package/extensions/modules/rtk/findFallback.js +68 -0
- package/extensions/modules/rtk/index.ts +248 -0
- package/extensions/modules/rtk/safe-rewrite.js +74 -0
- package/extensions/modules/rtk/test/findFallback.test.js +66 -0
- package/extensions/modules/rtk/test/index.test.js +131 -0
- package/extensions/modules/rtk/test/safe-rewrite.test.js +96 -0
- package/extensions/modules/rtk/version-gate.js +34 -0
- package/extensions/modules/serena/index.ts +629 -0
- package/extensions/modules/serena/lib/detect.ts +73 -0
- package/extensions/modules/serena/lib/env.ts +76 -0
- package/extensions/modules/serena/lib/guidance.ts +26 -0
- package/extensions/modules/serena/lib/normalize.ts +27 -0
- package/extensions/modules/serena/lib/symbol-key.ts +20 -0
- package/extensions/modules/serena/lib/truncate.ts +20 -0
- package/extensions/modules/serena/test/env.test.ts +122 -0
- package/extensions/modules/serena/test/index.test.ts +347 -0
- package/extensions/modules/serena/test/worker.test.ts +562 -0
- package/extensions/modules/serena/worker.ts +858 -0
- package/extensions/modules/steering/configPanel.ts +122 -0
- package/extensions/modules/steering/index.ts +616 -0
- package/extensions/modules/steering/lib/anchor.ts +175 -0
- package/extensions/modules/steering/lib/errors.ts +253 -0
- package/extensions/modules/steering/lib/family.ts +33 -0
- package/extensions/modules/steering/lib/guidance.ts +172 -0
- package/extensions/modules/steering/lib/history.ts +170 -0
- package/extensions/modules/steering/lib/settings.ts +127 -0
- package/extensions/modules/steering/test/anchor-regressions.test.ts +93 -0
- package/extensions/modules/steering/test/anchor-wiring.test.ts +99 -0
- package/extensions/modules/steering/test/anchor.test.ts +160 -0
- package/extensions/modules/steering/test/cache-stats.test.ts +43 -0
- package/extensions/modules/steering/test/errors.test.ts +309 -0
- package/extensions/modules/steering/test/family.test.ts +43 -0
- package/extensions/modules/steering/test/guidance.test.ts +357 -0
- package/extensions/modules/steering/test/harness.ts +93 -0
- package/extensions/modules/steering/test/history.test.ts +118 -0
- package/extensions/modules/steering/test/settings.test.ts +182 -0
- package/extensions/modules/steering/test/status.test.ts +71 -0
- package/extensions/modules/steering/test/toolCall.test.ts +99 -0
- package/extensions/modules/subagent/agents/planner.md +14 -0
- package/extensions/modules/subagent/agents/reviewer.md +42 -0
- package/extensions/modules/subagent/agents/scout.md +47 -0
- package/extensions/modules/subagent/agents/tester.md +12 -0
- package/extensions/modules/subagent/agents/worker.md +18 -0
- package/extensions/modules/subagent/configPanel.ts +219 -0
- package/extensions/modules/subagent/index.ts +2451 -0
- package/extensions/modules/subagent/lib/agents.ts +408 -0
- package/extensions/modules/subagent/lib/background.ts +383 -0
- package/extensions/modules/subagent/lib/herdr.ts +1019 -0
- package/extensions/modules/subagent/lib/history.ts +126 -0
- package/extensions/modules/subagent/lib/model.ts +156 -0
- package/extensions/modules/subagent/lib/render.ts +335 -0
- package/extensions/modules/subagent/lib/result.ts +110 -0
- package/extensions/modules/subagent/lib/roles.ts +296 -0
- package/extensions/modules/subagent/lib/routing.ts +223 -0
- package/extensions/modules/subagent/lib/runner.ts +559 -0
- package/extensions/modules/subagent/lib/schema.ts +21 -0
- package/extensions/modules/subagent/lib/security.ts +752 -0
- package/extensions/modules/subagent/lib/settings.ts +159 -0
- package/extensions/modules/subagent/lib/thread-viewer.ts +319 -0
- package/extensions/modules/subagent/lib/threads.ts +135 -0
- package/extensions/modules/subagent/lib/types.ts +18 -0
- package/extensions/modules/subagent/lib/widget.ts +475 -0
- package/extensions/modules/subagent/test/configPanel.test.ts +132 -0
- package/extensions/modules/subagent/test/herdr-advisor.test.ts +40 -0
- package/extensions/modules/subagent/test/routing.test.ts +350 -0
- package/extensions/modules/subagent/test/widget.test.ts +208 -0
- package/extensions/modules/usage/index.ts +59 -16
- package/extensions/modules/usage/test/genrate.test.ts +70 -0
- package/extensions/modules/usage/test/parsers.test.ts +13 -0
- package/extensions/modules/ux/UPSTREAM +24 -0
- package/extensions/modules/ux/index.ts +393 -0
- package/extensions/modules/ux/lib/audit.ts +562 -0
- package/extensions/modules/ux/lib/config.ts +136 -0
- package/extensions/modules/ux/lib/instructions.ts +77 -0
- package/extensions/modules/ux/test/audit.test.ts +756 -0
- package/extensions/modules/ux/test/extension.test.ts +394 -0
- package/extensions/modules/ux/test/instructions.test.ts +19 -0
- package/extensions/modules/web/configPanel.ts +193 -0
- package/extensions/modules/web/index.ts +978 -0
- package/extensions/modules/web/lib/agy.ts +209 -0
- package/extensions/modules/web/lib/brave.ts +42 -0
- package/extensions/modules/web/lib/cdp.ts +686 -0
- package/extensions/modules/web/lib/chatapi.ts +101 -0
- package/extensions/modules/web/lib/chatgpt.ts +796 -0
- package/extensions/modules/web/lib/chrome.ts +328 -0
- package/extensions/modules/web/lib/config.ts +136 -0
- package/extensions/modules/web/lib/content.ts +99 -0
- package/extensions/modules/web/lib/crawl4ai.ts +172 -0
- package/extensions/modules/web/lib/extract.ts +235 -0
- package/extensions/modules/web/lib/firecrawl.ts +56 -0
- package/extensions/modules/web/lib/format.ts +197 -0
- package/extensions/modules/web/lib/gemini-auth.ts +286 -0
- package/extensions/modules/web/lib/gemini-dr.ts +338 -0
- package/extensions/modules/web/lib/gemini.ts +551 -0
- package/extensions/modules/web/lib/imageapi.ts +559 -0
- package/extensions/modules/web/lib/retry.ts +71 -0
- package/extensions/modules/web/lib/search.ts +237 -0
- package/extensions/modules/web/lib/searxng.ts +69 -0
- package/extensions/modules/web/lib/settings.ts +148 -0
- package/extensions/modules/web/scripts/cdp-smoke.ts +77 -0
- package/extensions/modules/web/scripts/gemini-smoke.ts +107 -0
- package/extensions/modules/web/test/agy.test.ts +383 -0
- package/extensions/modules/web/test/cdp.test.ts +717 -0
- package/extensions/modules/web/test/chat.test.ts +171 -0
- package/extensions/modules/web/test/chatgpt.test.ts +633 -0
- package/extensions/modules/web/test/chrome.test.ts +282 -0
- package/extensions/modules/web/test/config.test.ts +218 -0
- package/extensions/modules/web/test/configPanel.test.ts +132 -0
- package/extensions/modules/web/test/crawl.test.ts +134 -0
- package/extensions/modules/web/test/crawl4ai.test.ts +105 -0
- package/extensions/modules/web/test/extension.test.ts +118 -0
- package/extensions/modules/web/test/extract.test.ts +436 -0
- package/extensions/modules/web/test/fixtures/dr-confirm.bin +12 -0
- package/extensions/modules/web/test/fixtures/dr-plan.bin +20 -0
- package/extensions/modules/web/test/fixtures/interaction.html +31 -0
- package/extensions/modules/web/test/format.test.ts +230 -0
- package/extensions/modules/web/test/gemini-auth.test.ts +465 -0
- package/extensions/modules/web/test/gemini-dr.test.ts +249 -0
- package/extensions/modules/web/test/gemini.test.ts +472 -0
- package/extensions/modules/web/test/hook.test.ts +101 -0
- package/extensions/modules/web/test/image.test.ts +1089 -0
- package/extensions/modules/web/test/retry.test.ts +119 -0
- package/extensions/modules/web/test/screenshot.test.ts +109 -0
- package/extensions/modules/web/test/search.test.ts +210 -0
- package/extensions/modules/web/test/settings.test.ts +165 -0
- package/extensions/modules/web/test/skill.test.ts +72 -0
- package/extensions/modules/web/types.d.ts +3 -0
- package/extensions/modules/web/vendor/package.json +1 -0
- package/extensions/modules/web/vendor/readability/LICENSE.md +13 -0
- package/extensions/modules/web/vendor/readability/Readability-readerable.js +123 -0
- package/extensions/modules/web/vendor/readability/Readability.js +2787 -0
- package/extensions/modules/web/vendor/readability/index.d.ts +64 -0
- package/extensions/modules/web/vendor/readability/index.js +9 -0
- package/extensions/modules/web/vendor/turndown/LICENSE +21 -0
- package/extensions/modules/web/vendor/turndown/turndown.cjs.js +784 -0
- package/extensions/modules/web/vendor/turndown-plugin-gfm/LICENSE +21 -0
- package/extensions/modules/web/vendor/turndown-plugin-gfm/turndown-plugin-gfm.cjs.js +163 -0
- package/extensions/modules/zai/configPanel.ts +151 -0
- package/extensions/modules/zai/index.ts +186 -0
- package/extensions/modules/zai/lib/anthropic.ts +159 -0
- package/extensions/modules/zai/lib/settings.ts +198 -0
- package/extensions/modules/zai/lib/signing.ts +718 -0
- package/extensions/modules/zai/lib/throttle.ts +198 -0
- package/extensions/modules/zai/test/anthropic.test.ts +132 -0
- package/extensions/modules/zai/test/configPanel.test.ts +207 -0
- package/extensions/modules/zai/test/extension.test.ts +249 -0
- package/extensions/modules/zai/test/settings.test.ts +183 -0
- package/extensions/modules/zai/test/signing-wiring.test.ts +108 -0
- package/extensions/modules/zai/test/signing.test.ts +345 -0
- package/extensions/modules/zai/test/throttle.test.ts +329 -0
- package/package.json +19 -4
- package/scripts/validate-themes.mjs +75 -0
- package/skills/munin/SKILL.md +117 -0
- package/skills/ux-capture/SKILL.md +202 -0
- package/skills/ux-design/SKILL.md +279 -0
- package/skills/ux-presets/SKILL.md +484 -0
- package/skills/ux-routing/SKILL.md +46 -0
- package/skills/web/SKILL.md +153 -0
- package/themes/alabaster.json +93 -0
- package/themes/amethyst.json +96 -0
- package/themes/anthracite.json +93 -0
- package/themes/basalt.json +91 -0
- package/themes/birch.json +95 -0
- package/themes/catppuccin-mocha.json +91 -0
- package/themes/dark-abyss.json +91 -0
- package/themes/dark-arctic.json +104 -0
- package/themes/dark-aurora.json +95 -0
- package/themes/dark-catppuccin.json +107 -0
- package/themes/dark-cavern.json +91 -0
- package/themes/dark-celestial.json +106 -0
- package/themes/dark-copper.json +95 -0
- package/themes/dark-cosmos.json +90 -0
- package/themes/dark-cyberpunk.json +102 -0
- package/themes/dark-dracula.json +98 -0
- package/themes/dark-eclipse.json +91 -0
- package/themes/dark-ember.json +95 -0
- package/themes/dark-equinox.json +90 -0
- package/themes/dark-forest.json +96 -0
- package/themes/dark-github.json +105 -0
- package/themes/dark-gruvbox.json +112 -0
- package/themes/dark-lavender.json +95 -0
- package/themes/dark-lunar.json +89 -0
- package/themes/dark-midnight.json +95 -0
- package/themes/dark-monochrome.json +94 -0
- package/themes/dark-monokai.json +98 -0
- package/themes/dark-nebula.json +90 -0
- package/themes/dark-neon-noir.json +100 -0
- package/themes/dark-nord.json +98 -0
- package/themes/dark-ocean.json +101 -0
- package/themes/dark-one.json +100 -0
- package/themes/dark-poimandres.json +115 -0
- package/themes/dark-rainforest.json +91 -0
- package/themes/dark-reef.json +91 -0
- package/themes/dark-retro.json +92 -0
- package/themes/dark-rose-pine.json +96 -0
- package/themes/dark-sakura.json +95 -0
- package/themes/dark-slate.json +95 -0
- package/themes/dark-solarized.json +97 -0
- package/themes/dark-solstice.json +90 -0
- package/themes/dark-starfall.json +91 -0
- package/themes/dark-sunset.json +99 -0
- package/themes/dark-swamp.json +90 -0
- package/themes/dark-synthwave.json +103 -0
- package/themes/dark-taiga.json +91 -0
- package/themes/dark-terminal.json +95 -0
- package/themes/dark-tokyo-night.json +101 -0
- package/themes/dark-tundra.json +91 -0
- package/themes/dark-twilight.json +91 -0
- package/themes/dark-volcanic.json +91 -0
- package/themes/dark.json +80 -0
- package/themes/graphite.json +92 -0
- package/themes/light-arctic.json +107 -0
- package/themes/light-aurora-day.json +91 -0
- package/themes/light-canyon.json +91 -0
- package/themes/light-catppuccin.json +106 -0
- package/themes/light-cirrus.json +90 -0
- package/themes/light-coral.json +95 -0
- package/themes/light-cyberpunk.json +96 -0
- package/themes/light-dawn.json +90 -0
- package/themes/light-dunes.json +91 -0
- package/themes/light-eucalyptus.json +95 -0
- package/themes/light-forest.json +100 -0
- package/themes/light-frost.json +95 -0
- package/themes/light-github.json +115 -0
- package/themes/light-glacier.json +91 -0
- package/themes/light-gruvbox.json +108 -0
- package/themes/light-haze.json +90 -0
- package/themes/light-honeycomb.json +95 -0
- package/themes/light-lagoon.json +91 -0
- package/themes/light-lavender.json +95 -0
- package/themes/light-meadow.json +91 -0
- package/themes/light-mint.json +95 -0
- package/themes/light-monochrome.json +101 -0
- package/themes/light-ocean.json +99 -0
- package/themes/light-one.json +99 -0
- package/themes/light-opal.json +91 -0
- package/themes/light-orchard.json +91 -0
- package/themes/light-paper.json +95 -0
- package/themes/light-poimandres.json +115 -0
- package/themes/light-prism.json +90 -0
- package/themes/light-retro.json +98 -0
- package/themes/light-sand.json +95 -0
- package/themes/light-savanna.json +91 -0
- package/themes/light-solarized.json +102 -0
- package/themes/light-soleil.json +90 -0
- package/themes/light-sunset.json +99 -0
- package/themes/light-synthwave.json +98 -0
- package/themes/light-tokyo-night.json +111 -0
- package/themes/light-wetland.json +91 -0
- package/themes/light-zenith.json +89 -0
- package/themes/light.json +80 -0
- package/themes/limestone.json +94 -0
- package/themes/mahogany.json +97 -0
- package/themes/marble.json +93 -0
- package/themes/mirage.json +79 -0
- package/themes/obsidian.json +91 -0
- package/themes/onyx.json +91 -0
- package/themes/pearl.json +93 -0
- package/themes/porcelain.json +91 -0
- package/themes/quartz.json +96 -0
- package/themes/sandstone.json +95 -0
- package/themes/titanium.json +90 -0
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
// ponytail: vendored from @bacnh85/pi-web 0.17.8
|
|
2
|
+
// Gemini web-tier (gemini.google.com) research backend, cookie-authed via the
|
|
3
|
+
// __Secure-1PSID cookie. Thin wrapper over the `gemini-reverse` npm package
|
|
4
|
+
// (CJS), lazily dynamic-imported so pi startup pays zero cost when unused.
|
|
5
|
+
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import http from "node:http";
|
|
8
|
+
import https from "node:https";
|
|
9
|
+
import { urlToHttpOptions } from "node:url";
|
|
10
|
+
import { findEnvValue } from "./config";
|
|
11
|
+
import { ensureKeepalive, loadCookieStore, resolvePsidts, saveCookieStore, type PostFn } from "./gemini-auth";
|
|
12
|
+
import { CHROME_UA, DeepResearchError, geminiDeepResearch, type DrHttp } from "./gemini-dr";
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// Config
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
export interface GeminiWebConfig {
|
|
19
|
+
psid?: string;
|
|
20
|
+
psidSource: string;
|
|
21
|
+
psidts?: string;
|
|
22
|
+
proxy?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function loadGeminiWebConfig(cwd = process.cwd(), includeCwdEnv = false): GeminiWebConfig {
|
|
26
|
+
const psid = findEnvValue("GEMINI_WEB_SECURE_1PSID", cwd, includeCwdEnv);
|
|
27
|
+
const psidts = findEnvValue("GEMINI_WEB_SECURE_1PSIDTS", cwd, includeCwdEnv);
|
|
28
|
+
const proxy = findEnvValue("GEMINI_WEB_PROXY", cwd, includeCwdEnv);
|
|
29
|
+
return { psid: psid.value, psidSource: psid.value ? psid.source : "not set", psidts: psidts.value, proxy: proxy.value };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// Minimal structural view of the gemini-reverse client (keeps tests injectable
|
|
34
|
+
// and decouples us from upstream type drift).
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
export interface GeminiOutputLike {
|
|
38
|
+
text?: string | null;
|
|
39
|
+
model?: string;
|
|
40
|
+
candidates?: Array<{ text?: string | null }>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface GeminiResearchLike {
|
|
44
|
+
text?: string | null;
|
|
45
|
+
plan?: { title?: string | null; eta_text?: string | null };
|
|
46
|
+
statuses?: Array<Record<string, unknown>>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface GeminiImageLike {
|
|
50
|
+
save(opts?: { path?: string; filename?: string }): Promise<string>;
|
|
51
|
+
url?: string;
|
|
52
|
+
alt?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface GeminiImageOutputLike {
|
|
56
|
+
text?: string | null;
|
|
57
|
+
model?: string;
|
|
58
|
+
images?: GeminiImageLike[];
|
|
59
|
+
generated_images?: GeminiImageLike[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface GeminiClientLike {
|
|
63
|
+
ask(prompt: string, opts?: Record<string, unknown>): Promise<GeminiOutputLike>;
|
|
64
|
+
research(
|
|
65
|
+
prompt: string,
|
|
66
|
+
opts?: { wait?: boolean; pollInterval?: number; timeout?: number; onStatus?: (s: Record<string, unknown>) => void },
|
|
67
|
+
): Promise<GeminiResearchLike>;
|
|
68
|
+
// Optional so pre-0.9 fake clients (ask+research only) keep compiling.
|
|
69
|
+
newChat?(opts?: { model?: string }): {
|
|
70
|
+
generateContent(o: { prompt: string }): Promise<GeminiImageOutputLike>;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export type GeminiClientFactory = (
|
|
75
|
+
opts: { secure_1psid?: string; secure_1psidts?: string; proxy?: string },
|
|
76
|
+
) => GeminiClientLike | Promise<GeminiClientLike>;
|
|
77
|
+
|
|
78
|
+
// Cached per config (psid|proxy) so a config change re-creates the client.
|
|
79
|
+
let cached: { key: string; client: GeminiClientLike } | null = null;
|
|
80
|
+
|
|
81
|
+
// Google ships ~25KB of response headers on every Gemini page (a 19.7KB
|
|
82
|
+
// content-security-policy + 3.9KB reporting-endpoints) — over Node's default
|
|
83
|
+
// 16KB parser cap, so the http parser rejects them (HPE_HEADER_OVERFLOW; the
|
|
84
|
+
// same cap exists in undici, but gemini-reverse uses axios's node http adapter).
|
|
85
|
+
// Node honors a per-request maxHeaderSize override, and gemini.google.com also
|
|
86
|
+
// gates privileged surfaces (image gen, DR, RotateCookies) on browser-grade
|
|
87
|
+
// client headers, which gemini-reverse never sends — so lazily patch
|
|
88
|
+
// http.request/https.request to inject both for gemini.google.com hosts only.
|
|
89
|
+
// ponytail: process-wide patch, scoped to one hostname; if it ever misbehaves,
|
|
90
|
+
// revert to NODE_OPTIONS=--max-http-header-size=262144 (cap) or dropping the
|
|
91
|
+
// UA merge (headers).
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Returns the request options to pass through with the cap + browser headers
|
|
95
|
+
* injected when the target host is gemini.google.com, or null when the call
|
|
96
|
+
* must pass through untouched. Normalizes all http.request input forms
|
|
97
|
+
* (options object, string, URL) — string/URL forms become a fresh options
|
|
98
|
+
* object.
|
|
99
|
+
*
|
|
100
|
+
* gemini.google.com gates privileged surfaces (image gen, Deep Research,
|
|
101
|
+
* RotateCookies) on browser-grade client headers: gemini-reverse sends
|
|
102
|
+
* axios's default UA, while the Chrome-UA DR client and rotation both work
|
|
103
|
+
* over plain Node TLS (live-proven 2026-09-13/14). Non-browser user-agents
|
|
104
|
+
* are replaced; every other key is added only when absent (never clobbered).
|
|
105
|
+
*
|
|
106
|
+
* @internal exported for tests
|
|
107
|
+
*/
|
|
108
|
+
export function injectGeminiRequestTweaks(options: unknown): Record<string, unknown> | null {
|
|
109
|
+
let opts: Record<string, unknown>;
|
|
110
|
+
if (typeof options === "string") {
|
|
111
|
+
opts = urlToHttpOptions(new URL(options)) as Record<string, unknown>;
|
|
112
|
+
} else if (options instanceof URL) {
|
|
113
|
+
opts = urlToHttpOptions(options) as Record<string, unknown>;
|
|
114
|
+
} else if (options && typeof options === "object") {
|
|
115
|
+
opts = options as Record<string, unknown>;
|
|
116
|
+
} else {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
const host = String(opts.hostname ?? opts.host ?? "").split(":")[0];
|
|
120
|
+
if (host !== "gemini.google.com" || opts.maxHeaderSize) return null;
|
|
121
|
+
opts.maxHeaderSize = 256 * 1024;
|
|
122
|
+
mergeBrowserHeaders(opts);
|
|
123
|
+
return opts;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const BROWSER_HEADERS: Record<string, string> = {
|
|
127
|
+
"user-agent": CHROME_UA,
|
|
128
|
+
"sec-ch-ua": '"Chromium";v="145", "Google Chrome";v="145", "Not-A.Brand";v="24"',
|
|
129
|
+
"sec-ch-ua-mobile": "?0",
|
|
130
|
+
"sec-ch-ua-platform": '"Windows"',
|
|
131
|
+
"sec-fetch-dest": "empty",
|
|
132
|
+
"sec-fetch-mode": "cors",
|
|
133
|
+
"sec-fetch-site": "same-origin",
|
|
134
|
+
"accept-language": "en-US,en;q=0.9",
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/** Absent-keys-only merge — except user-agent, which is REPLACED when the
|
|
138
|
+
* current value is a non-browser UA (axios injects its own default, which
|
|
139
|
+
* poisons the privileged-surface gate; that replacement is the point).
|
|
140
|
+
* An existing browser UA (any transport's) is left alone. */
|
|
141
|
+
function mergeBrowserHeaders(opts: Record<string, unknown>): void {
|
|
142
|
+
const headers = (opts.headers ??= {}) as Record<string, unknown>;
|
|
143
|
+
const present = new Set(Object.keys(headers).map((k) => k.toLowerCase()));
|
|
144
|
+
const uaKey = Object.keys(headers).find((k) => k.toLowerCase() === "user-agent");
|
|
145
|
+
const ua = String(uaKey !== undefined ? headers[uaKey] : "");
|
|
146
|
+
if (/chrome\//i.test(ua)) {
|
|
147
|
+
// existing browser UA (any case) — leave exactly as-is
|
|
148
|
+
} else {
|
|
149
|
+
if (uaKey !== undefined) delete headers[uaKey]; // drop non-browser UA (any case) — two UA headers is worse than one
|
|
150
|
+
headers["user-agent"] = CHROME_UA;
|
|
151
|
+
}
|
|
152
|
+
for (const [key, value] of Object.entries(BROWSER_HEADERS)) {
|
|
153
|
+
if (key !== "user-agent" && !present.has(key)) headers[key] = value;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** @internal exported for tests — returns the http.request args to forward with the tweaks applied */
|
|
158
|
+
export function applyHeaderCapArgs(args: unknown[]): unknown[] {
|
|
159
|
+
const [options, ...rest] = args;
|
|
160
|
+
if (options && typeof options === "object" && !(options instanceof URL)) {
|
|
161
|
+
injectGeminiRequestTweaks(options); // options-object form: mutate in place
|
|
162
|
+
return args;
|
|
163
|
+
}
|
|
164
|
+
if (typeof options === "string" || options instanceof URL) {
|
|
165
|
+
const parsed = urlToHttpOptions(options instanceof URL ? options : new URL(options)) as Record<string, unknown>;
|
|
166
|
+
if (String(parsed.hostname ?? "").split(":")[0] === "gemini.google.com") {
|
|
167
|
+
const follow = rest[0];
|
|
168
|
+
if (follow && typeof follow === "object" && typeof follow !== "function") {
|
|
169
|
+
// 3-arg request(url, options, cb): merge into the caller's options —
|
|
170
|
+
// replacing arg1 with a plain object would misbind Node's signature
|
|
171
|
+
// (the options object would be read as the callback). The follow-on
|
|
172
|
+
// object legitimately lacks hostname (Node merges it from the URL).
|
|
173
|
+
if (!(follow as Record<string, unknown>).maxHeaderSize) {
|
|
174
|
+
(follow as Record<string, unknown>).maxHeaderSize = 256 * 1024;
|
|
175
|
+
mergeBrowserHeaders(follow as Record<string, unknown>);
|
|
176
|
+
}
|
|
177
|
+
return args;
|
|
178
|
+
}
|
|
179
|
+
const opts = injectGeminiRequestTweaks(options); // 2-arg (url, cb): (optionsObj, cb) is valid
|
|
180
|
+
if (opts) return [opts, ...rest];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return args;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
let headerCapPatched = false;
|
|
187
|
+
function patchHeaderCap(): void {
|
|
188
|
+
if (headerCapPatched) return;
|
|
189
|
+
headerCapPatched = true;
|
|
190
|
+
for (const mod of [http, https]) {
|
|
191
|
+
const real = mod.request as unknown as (...args: unknown[]) => unknown;
|
|
192
|
+
const patched = function (this: unknown, ...args: unknown[]) {
|
|
193
|
+
try {
|
|
194
|
+
args = applyHeaderCapArgs(args);
|
|
195
|
+
} catch { /* malformed input — let the real request surface the error */ }
|
|
196
|
+
return real.call(this, ...args);
|
|
197
|
+
} as typeof mod.request;
|
|
198
|
+
mod.request = patched;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** @internal exported for tests — resolves the real gemini-reverse module */
|
|
203
|
+
export async function loadDefaultFactory(): Promise<GeminiClientFactory> {
|
|
204
|
+
patchHeaderCap();
|
|
205
|
+
const mod = (await import("gemini-reverse")) as unknown as Record<string, unknown>;
|
|
206
|
+
// CJS interop: named export usually works, but fall back to default.Gemini.
|
|
207
|
+
const Gemini = (mod.Gemini ?? (mod as { default?: Record<string, unknown> }).default?.Gemini) as
|
|
208
|
+
| (new (opts: Record<string, unknown>) => GeminiClientLike)
|
|
209
|
+
| undefined;
|
|
210
|
+
if (typeof Gemini !== "function") {
|
|
211
|
+
throw new Error("gemini-reverse: Gemini export not found (unexpected package shape)");
|
|
212
|
+
}
|
|
213
|
+
// ponytail: generous per-request cap (covers research); per-mode ask/research
|
|
214
|
+
// timeouts are enforced by raceGuard below.
|
|
215
|
+
// ponytail: Google's rotating __Secure-1PSIDTS is required for sensitive
|
|
216
|
+
// surfaces (image generation refuses with "You might be signed out" when it
|
|
217
|
+
// is missing); the page HTML no longer carries SNlM0e/rotations for plain
|
|
218
|
+
// clients, so the user passes it explicitly and we inject it pre-init.
|
|
219
|
+
return (opts) => {
|
|
220
|
+
const client = new Gemini({ secure_1psid: opts.secure_1psid, proxy: opts.proxy ?? null, timeout: 1_800_000 });
|
|
221
|
+
if (opts.secure_1psidts) {
|
|
222
|
+
(client as unknown as { cookies: Record<string, string> }).cookies["__Secure-1PSIDTS"] = opts.secure_1psidts;
|
|
223
|
+
}
|
|
224
|
+
return client;
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function getClient(config: GeminiWebConfig, factory?: GeminiClientFactory, storePath?: string): Promise<GeminiClientLike> {
|
|
229
|
+
// Store-first: the keepalive keeps __Secure-1PSIDTS rotated and persisted;
|
|
230
|
+
// the static env copy is only the bootstrap identity (and wins when the
|
|
231
|
+
// user pastes a fresh cookie, because the store is keyed to the old psid).
|
|
232
|
+
const psidts = resolvePsidts(config.psid, config.psidts, storePath);
|
|
233
|
+
const key = `${config.psid ?? ""}|${psidts ?? ""}|${config.proxy ?? ""}`;
|
|
234
|
+
if (cached?.key === key) return cached.client;
|
|
235
|
+
const make = factory ?? (await loadDefaultFactory());
|
|
236
|
+
const client = await make({ secure_1psid: config.psid, secure_1psidts: psidts, proxy: config.proxy });
|
|
237
|
+
// Keepalive arms only for the real transport — fake factories (tests) get
|
|
238
|
+
// no background rotation timer.
|
|
239
|
+
if (!factory) ensureKeepalive(config);
|
|
240
|
+
cached = { key, client };
|
|
241
|
+
return client;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** @internal test hook */
|
|
245
|
+
export function __resetGeminiClientCache(): void {
|
|
246
|
+
cached = null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function errorName(err: unknown): string {
|
|
250
|
+
const e = err as { name?: string; constructor?: { name?: string } } | null;
|
|
251
|
+
const name = e?.name;
|
|
252
|
+
// Subclasses that don't set this.name inherit the generic "Error" — prefer
|
|
253
|
+
// the constructor name in that case (how we detect upstream error classes).
|
|
254
|
+
if (name && name !== "Error") return name;
|
|
255
|
+
return e?.constructor?.name ?? name ?? "";
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function isAuthError(err: unknown): boolean {
|
|
259
|
+
return errorName(err) === "AuthError";
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// After a successful run, persist any rotated __Secure-1PSIDTS the client
|
|
263
|
+
// absorbed from response Set-Cookie headers (gemini-reverse merges them into
|
|
264
|
+
// client.cookies). Without this, the rotated value dies with the process —
|
|
265
|
+
// exactly the static-snapshot decay OmniRoute #7676 describes.
|
|
266
|
+
// Best-effort: a store-write failure (EACCES, ENOSPC, bad path) must never
|
|
267
|
+
// convert a succeeded tool call into an error.
|
|
268
|
+
function persistRotatedTs(client: GeminiClientLike, config: GeminiWebConfig, storePath?: string): void {
|
|
269
|
+
try {
|
|
270
|
+
if (!config.psid) return;
|
|
271
|
+
const ts = (client as unknown as { cookies?: Record<string, string> }).cookies?.["__Secure-1PSIDTS"];
|
|
272
|
+
if (!ts || ts === config.psidts) return;
|
|
273
|
+
const store = loadCookieStore(storePath);
|
|
274
|
+
if (store?.psid === config.psid && store.psidts === ts) return;
|
|
275
|
+
saveCookieStore({ psid: config.psid, psidts: ts, updatedAt: Date.now() }, storePath);
|
|
276
|
+
} catch {
|
|
277
|
+
/* best-effort persistence — the call already succeeded */
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// One AuthError retry: rotate the cookie via Google's RotateCookies endpoint
|
|
282
|
+
// (refreshGeminiAuth persists a fresh value, or clears the store when the
|
|
283
|
+
// server says the session is dead), then rebuild the client — re-running init
|
|
284
|
+
// also absorbs rotated Set-Cookies Google hands back. Only one retry: a
|
|
285
|
+
// second AuthError propagates.
|
|
286
|
+
export async function withGeminiClient<T>(
|
|
287
|
+
config: GeminiWebConfig,
|
|
288
|
+
run: (client: GeminiClientLike) => Promise<T>,
|
|
289
|
+
factory?: GeminiClientFactory,
|
|
290
|
+
auth?: { rotatePost?: PostFn; storePath?: string },
|
|
291
|
+
): Promise<T> {
|
|
292
|
+
const attempt = async (client: GeminiClientLike): Promise<T> => {
|
|
293
|
+
const result = await run(client);
|
|
294
|
+
persistRotatedTs(client, config, auth?.storePath);
|
|
295
|
+
return result;
|
|
296
|
+
};
|
|
297
|
+
let client = await getClient(config, factory, auth?.storePath);
|
|
298
|
+
for (let i = 0; ; i++) {
|
|
299
|
+
try {
|
|
300
|
+
return await attempt(client);
|
|
301
|
+
} catch (err) {
|
|
302
|
+
if (!isAuthError(err) || !config.psid || i > 0) throw err;
|
|
303
|
+
// rebuild + retry once: re-running init absorbs fresh Set-Cookies.
|
|
304
|
+
// (Rotation is deliberately NOT auto-invoked — RotateCookies-issued TS
|
|
305
|
+
// poison gemini's privileged surfaces; see lib/gemini-auth.ts.)
|
|
306
|
+
cached = null;
|
|
307
|
+
client = await getClient(config, factory, auth?.storePath);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// ponytail: gemini-reverse polls aren't cancellable — abort/timeout rejects the
|
|
313
|
+
// tool call promptly, but the underlying client poll finishes/times out in the
|
|
314
|
+
// background (ceiling; real cancellation needs upstream AbortSignal support).
|
|
315
|
+
// Shared with lib/imageapi.ts (same abort semantics for plain fetch calls).
|
|
316
|
+
export function raceGuard<T>(
|
|
317
|
+
promise: Promise<T>,
|
|
318
|
+
opts: { signal?: AbortSignal; timeoutMs?: number; label: string },
|
|
319
|
+
): Promise<T> {
|
|
320
|
+
const { signal, timeoutMs, label } = opts;
|
|
321
|
+
if (!signal && !timeoutMs) return promise;
|
|
322
|
+
return new Promise<T>((resolve, reject) => {
|
|
323
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
324
|
+
const onAbort = () => {
|
|
325
|
+
clearTimeout(timer);
|
|
326
|
+
const err = new Error(`${label} aborted`);
|
|
327
|
+
err.name = "AbortError";
|
|
328
|
+
reject(err);
|
|
329
|
+
};
|
|
330
|
+
const onTimeout = () => {
|
|
331
|
+
signal?.removeEventListener("abort", onAbort);
|
|
332
|
+
const err = new Error(`${label} timed out after ${timeoutMs}ms`);
|
|
333
|
+
err.name = "TimeoutError";
|
|
334
|
+
reject(err);
|
|
335
|
+
};
|
|
336
|
+
if (signal) {
|
|
337
|
+
if (signal.aborted) {
|
|
338
|
+
// Mark the guarded promise handled BEFORE rejecting with AbortError —
|
|
339
|
+
// otherwise a later rejection of the underlying call becomes a fatal
|
|
340
|
+
// process-level unhandledRejection.
|
|
341
|
+
promise.catch(() => {});
|
|
342
|
+
return onAbort();
|
|
343
|
+
}
|
|
344
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
345
|
+
}
|
|
346
|
+
if (timeoutMs) timer = setTimeout(onTimeout, timeoutMs);
|
|
347
|
+
promise.then(
|
|
348
|
+
(v) => {
|
|
349
|
+
signal?.removeEventListener("abort", onAbort);
|
|
350
|
+
clearTimeout(timer);
|
|
351
|
+
resolve(v);
|
|
352
|
+
},
|
|
353
|
+
(e) => {
|
|
354
|
+
signal?.removeEventListener("abort", onAbort);
|
|
355
|
+
clearTimeout(timer);
|
|
356
|
+
reject(e);
|
|
357
|
+
},
|
|
358
|
+
);
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// ---------------------------------------------------------------------------
|
|
363
|
+
// Source extraction (parsed web output has no structured citations field —
|
|
364
|
+
// URLs are pulled from the answer/report markdown text)
|
|
365
|
+
// ---------------------------------------------------------------------------
|
|
366
|
+
|
|
367
|
+
const MD_LINK_RE = /\[[^\]]*\]\((https?:\/\/[^)\s]+)\)/g;
|
|
368
|
+
const BARE_URL_RE = /https?:\/\/[^\s<>()\[\]{}"'`]+/g;
|
|
369
|
+
|
|
370
|
+
export function extractSources(text: string, cap = 30): string[] {
|
|
371
|
+
if (!text) return [];
|
|
372
|
+
const seen = new Set<string>();
|
|
373
|
+
const sources: string[] = [];
|
|
374
|
+
const push = (raw: string) => {
|
|
375
|
+
const url = raw.replace(/[.,;:!?)\]]+$/, "");
|
|
376
|
+
if (!seen.has(url)) {
|
|
377
|
+
seen.add(url);
|
|
378
|
+
sources.push(url);
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
for (const m of text.matchAll(MD_LINK_RE)) push(m[1]);
|
|
382
|
+
for (const m of text.matchAll(BARE_URL_RE)) push(m[0]);
|
|
383
|
+
return sources.slice(0, cap);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// ---------------------------------------------------------------------------
|
|
387
|
+
// Error mapping
|
|
388
|
+
// ---------------------------------------------------------------------------
|
|
389
|
+
|
|
390
|
+
export function describeGeminiError(err: unknown): string {
|
|
391
|
+
switch (errorName(err)) {
|
|
392
|
+
case "AuthError":
|
|
393
|
+
return "Gemini web session expired. Re-copy __Secure-1PSID and __Secure-1PSIDTS from a fresh incognito login to gemini.google.com (F12 → Application → Cookies) into ~/.pi/agent/.env.local, then restart pi. Important: don't use that Google session in your daily browser — an open Gemini tab supersedes the pasted cookie within minutes (verified 2026-09-14).";
|
|
394
|
+
case "DeepResearchError":
|
|
395
|
+
return `Deep Research transport error: ${err instanceof Error ? err.message : String(err)}. The pure-Node client follows the 2026-09-14-validated wire shapes; repeated failures usually mean the session is stale (re-paste the cookie from an incognito login) or the web protocol drifted.`;
|
|
396
|
+
case "UsageLimitExceeded":
|
|
397
|
+
return "Gemini web usage limit reached. Try again later or pick a different model.";
|
|
398
|
+
case "TemporarilyBlocked":
|
|
399
|
+
return "Gemini web temporarily blocked this IP. Wait a while or set GEMINI_WEB_PROXY (e.g. http://host:port).";
|
|
400
|
+
case "ModelInvalid":
|
|
401
|
+
return "Gemini model unavailable for this account. Try another model or drop the model parameter.";
|
|
402
|
+
case "AbortError":
|
|
403
|
+
case "TimeoutError":
|
|
404
|
+
return err instanceof Error ? err.message : String(err);
|
|
405
|
+
default: {
|
|
406
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
407
|
+
if (/Cannot poll|research_id/.test(msg)) {
|
|
408
|
+
return `Gemini web research polling failed (${msg}). The web protocol's plan/report shape has drifted from the client library — the plan step engaged but the poll could not find its research id. This is a protocol-drift limitation of the embedded gemini-reverse client, not an account-tier gate.`;
|
|
409
|
+
}
|
|
410
|
+
if (/Unknown API error/.test(msg)) {
|
|
411
|
+
return `Gemini web rejected the request (${msg}). The 1184 code is unreliable from this client — it also fires on expired-cookie sessions (verified 2026-09-13). For research mode: Deep Research plan creation has succeeded on a free-tier account via a browser-grade client (2026-09-13), but Gemini itself has also described Deep Research as Pro/Ultra-gated, so the tier requirement is unverified. mode=ask still works.`;
|
|
412
|
+
}
|
|
413
|
+
return `Gemini web error: ${msg}`;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// ---------------------------------------------------------------------------
|
|
419
|
+
// Public API
|
|
420
|
+
// ---------------------------------------------------------------------------
|
|
421
|
+
|
|
422
|
+
export interface GeminiAskResult {
|
|
423
|
+
text: string;
|
|
424
|
+
model?: string;
|
|
425
|
+
guest: boolean;
|
|
426
|
+
sources: string[];
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
export async function geminiAsk(
|
|
430
|
+
query: string,
|
|
431
|
+
opts: { config: GeminiWebConfig; model?: string; timeoutMs?: number; signal?: AbortSignal; factory?: GeminiClientFactory },
|
|
432
|
+
): Promise<GeminiAskResult> {
|
|
433
|
+
const out = await raceGuard(
|
|
434
|
+
withGeminiClient(
|
|
435
|
+
opts.config,
|
|
436
|
+
(client) => client.ask(query, { temporary: true, ...(opts.model ? { model: opts.model } : {}) }),
|
|
437
|
+
opts.factory,
|
|
438
|
+
),
|
|
439
|
+
{ signal: opts.signal, timeoutMs: opts.timeoutMs ?? 120_000, label: "web_research ask" },
|
|
440
|
+
);
|
|
441
|
+
const text = String(out?.text ?? out?.candidates?.[0]?.text ?? "");
|
|
442
|
+
return { text, model: out?.model, guest: !opts.config.psid, sources: extractSources(text) };
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
export interface GeminiResearchResult {
|
|
446
|
+
text: string;
|
|
447
|
+
title?: string | null;
|
|
448
|
+
eta?: string | null;
|
|
449
|
+
guest: boolean;
|
|
450
|
+
sources: string[];
|
|
451
|
+
/** Set when the cycle ran but the report could not be retrieved (stale session) — text carries the plan/confirm transcript instead. */
|
|
452
|
+
partial?: string;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
export async function geminiResearch(
|
|
456
|
+
query: string,
|
|
457
|
+
opts: {
|
|
458
|
+
config: GeminiWebConfig;
|
|
459
|
+
timeoutMs?: number;
|
|
460
|
+
signal?: AbortSignal;
|
|
461
|
+
onStatus?: (s: Record<string, unknown>) => void;
|
|
462
|
+
factory?: GeminiClientFactory;
|
|
463
|
+
/** @internal test injection — raw DR transport */
|
|
464
|
+
drHttp?: DrHttp;
|
|
465
|
+
},
|
|
466
|
+
): Promise<GeminiResearchResult> {
|
|
467
|
+
if (!opts.config.psid) {
|
|
468
|
+
throw new Error(
|
|
469
|
+
"Deep Research requires GEMINI_WEB_SECURE_1PSID (gemini.google.com cookie) in ~/.pi/agent/.env.local — guest mode does not support it. Tier availability is unverified: free-tier plan creation has succeeded via browser-grade clients, but Gemini has also described Deep Research as Pro/Ultra-gated.",
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
const timeoutMs = opts.timeoutMs ?? 600_000;
|
|
473
|
+
// Pure-Node DR client (plan → confirm → poll) — gemini-reverse's research
|
|
474
|
+
// path drifts (1184 / missing research_id, see 0.11.x history).
|
|
475
|
+
const dr = await raceGuard(
|
|
476
|
+
geminiDeepResearch({
|
|
477
|
+
cookie: { psid: opts.config.psid, psidts: resolvePsidts(opts.config.psid, opts.config.psidts) },
|
|
478
|
+
query,
|
|
479
|
+
timeoutMs,
|
|
480
|
+
signal: opts.signal,
|
|
481
|
+
...(opts.drHttp ? { http: opts.drHttp } : {}),
|
|
482
|
+
}),
|
|
483
|
+
// +5s grace so the DR client's own poll-timeout (better semantics) fires first.
|
|
484
|
+
{ signal: opts.signal, timeoutMs: timeoutMs + 5_000, label: "web_research research" },
|
|
485
|
+
);
|
|
486
|
+
return {
|
|
487
|
+
title: dr.title ?? null,
|
|
488
|
+
eta: null,
|
|
489
|
+
text: dr.text || dr.partial || "Deep research completed but returned no report text.",
|
|
490
|
+
guest: false,
|
|
491
|
+
sources: dr.sources,
|
|
492
|
+
partial: dr.partial,
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// ---------------------------------------------------------------------------
|
|
497
|
+
// Image generation (free web tier): prompt → GeneratedImage.save() paths
|
|
498
|
+
// ---------------------------------------------------------------------------
|
|
499
|
+
|
|
500
|
+
export interface GeminiImageResult {
|
|
501
|
+
paths: string[];
|
|
502
|
+
model?: string;
|
|
503
|
+
guest: boolean;
|
|
504
|
+
text: string;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export async function geminiGenerateImage(
|
|
508
|
+
prompt: string,
|
|
509
|
+
opts: {
|
|
510
|
+
config: GeminiWebConfig;
|
|
511
|
+
outDir: string;
|
|
512
|
+
model?: string;
|
|
513
|
+
timeoutMs?: number;
|
|
514
|
+
signal?: AbortSignal;
|
|
515
|
+
factory?: GeminiClientFactory;
|
|
516
|
+
auth?: { rotatePost?: PostFn; storePath?: string };
|
|
517
|
+
},
|
|
518
|
+
): Promise<GeminiImageResult> {
|
|
519
|
+
// Create the dir before generating — a bad out_dir must not waste a
|
|
520
|
+
// generation (same contract as the API path's mkdir).
|
|
521
|
+
fs.mkdirSync(opts.outDir, { recursive: true });
|
|
522
|
+
const out = await raceGuard(
|
|
523
|
+
withGeminiClient(
|
|
524
|
+
opts.config,
|
|
525
|
+
async (client) => {
|
|
526
|
+
if (typeof client.newChat !== "function") {
|
|
527
|
+
throw new Error("gemini-reverse client exposes no newChat (unexpected package shape)");
|
|
528
|
+
}
|
|
529
|
+
const chat = client.newChat(opts.model ? { model: opts.model } : undefined);
|
|
530
|
+
return chat.generateContent({ prompt });
|
|
531
|
+
},
|
|
532
|
+
opts.factory,
|
|
533
|
+
opts.auth,
|
|
534
|
+
),
|
|
535
|
+
{ signal: opts.signal, timeoutMs: opts.timeoutMs ?? 180_000, label: "web_image gemini" },
|
|
536
|
+
);
|
|
537
|
+
// gemini-reverse's ModelOutput always defines generated_images (defaults to
|
|
538
|
+
// []), so only fall back to `images` when it is empty — not when absent.
|
|
539
|
+
const images = out?.generated_images?.length ? out.generated_images : (out?.images ?? []);
|
|
540
|
+
const text = String(out?.text ?? "");
|
|
541
|
+
if (!images.length) {
|
|
542
|
+
throw new Error(
|
|
543
|
+
text
|
|
544
|
+
? `Gemini replied with text but no images: ${text.slice(0, 200)} — the Gemini web tier gates image generation on browser-grade TLS fingerprints and refuses plain-Node clients (verified 2026-09-14: identical cookie + payload generate via a chrome-impersonating transport). Use provider=zai (ZAI_API_KEY) or provider=custom; pinning provider=gemini will not change this until pi-web ships an impersonating transport.`
|
|
545
|
+
: "Gemini returned no images — generation may be unavailable for this account/region (guest mode may not support it; set GEMINI_WEB_SECURE_1PSID).",
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
const paths: string[] = [];
|
|
549
|
+
for (const img of images) paths.push(await img.save({ path: opts.outDir }));
|
|
550
|
+
return { paths, model: out?.model, guest: !opts.config.psid, text };
|
|
551
|
+
}
|