@asterxsk/kiln 0.1.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/LICENSE +21 -0
- package/README.md +170 -0
- package/agent/AGENTS.md +67 -0
- package/agent/README.md +5 -0
- package/agent/extensions/AGENTS.md +68 -0
- package/agent/extensions/ask-user/index.ts +418 -0
- package/agent/extensions/ask-user/install.ps1 +23 -0
- package/agent/extensions/ask-user/install.sh +21 -0
- package/agent/extensions/ask-user/package-lock.json +769 -0
- package/agent/extensions/ask-user/package.json +19 -0
- package/agent/extensions/ask-user/prompt.ts +45 -0
- package/agent/extensions/ask-user/tsconfig.json +7 -0
- package/agent/extensions/background-terminals/docs/implementation-guide.md +942 -0
- package/agent/extensions/background-terminals/index.ts +627 -0
- package/agent/extensions/background-terminals/install.ps1 +23 -0
- package/agent/extensions/background-terminals/install.sh +21 -0
- package/agent/extensions/background-terminals/manager.test.ts +735 -0
- package/agent/extensions/background-terminals/output.test.ts +109 -0
- package/agent/extensions/background-terminals/package-lock.json +769 -0
- package/agent/extensions/background-terminals/package.json +17 -0
- package/agent/extensions/background-terminals/prompt.test.ts +125 -0
- package/agent/extensions/background-terminals/ps.test.ts +82 -0
- package/agent/extensions/background-terminals/result-delivery.test.ts +44 -0
- package/agent/extensions/background-terminals/src/domain.ts +87 -0
- package/agent/extensions/background-terminals/src/manager.ts +907 -0
- package/agent/extensions/background-terminals/src/output.ts +84 -0
- package/agent/extensions/background-terminals/src/prompt.ts +142 -0
- package/agent/extensions/background-terminals/src/result-delivery.ts +27 -0
- package/agent/extensions/background-terminals/src/runtime.ts +36 -0
- package/agent/extensions/background-terminals/src/ui/output-view.ts +79 -0
- package/agent/extensions/background-terminals/src/ui/ps.ts +621 -0
- package/agent/extensions/background-terminals/tsconfig.json +7 -0
- package/agent/extensions/destructive/README.md +31 -0
- package/agent/extensions/destructive/index.ts +88 -0
- package/agent/extensions/destructive/install.ps1 +23 -0
- package/agent/extensions/destructive/install.sh +21 -0
- package/agent/extensions/file-search/index.spec.ts +443 -0
- package/agent/extensions/file-search/index.ts +459 -0
- package/agent/extensions/file-search/install.ps1 +23 -0
- package/agent/extensions/file-search/install.sh +21 -0
- package/agent/extensions/file-search/package-lock.json +2253 -0
- package/agent/extensions/file-search/package.json +23 -0
- package/agent/extensions/file-search/src/args.ts +122 -0
- package/agent/extensions/file-search/src/binaries.ts +422 -0
- package/agent/extensions/file-search/src/output.ts +126 -0
- package/agent/extensions/file-search/src/process.ts +146 -0
- package/agent/extensions/file-search/src/prompt.ts +52 -0
- package/agent/extensions/file-search/tsconfig.json +7 -0
- package/agent/extensions/goal/README.md +50 -0
- package/agent/extensions/goal/index.ts +155 -0
- package/agent/extensions/goal/install.ps1 +23 -0
- package/agent/extensions/goal/install.sh +21 -0
- package/agent/extensions/modelconf/PLAN.md +915 -0
- package/agent/extensions/modelconf/README.md +66 -0
- package/agent/extensions/modelconf/index.ts +296 -0
- package/agent/extensions/modelconf/install.ps1 +23 -0
- package/agent/extensions/modelconf/install.sh +21 -0
- package/agent/extensions/modelconf/package-lock.json +1809 -0
- package/agent/extensions/modelconf/package.json +13 -0
- package/agent/extensions/modelconf/src/fuzzy.ts +26 -0
- package/agent/extensions/modelconf/src/glob.ts +13 -0
- package/agent/extensions/modelconf/src/persistence.test.ts +46 -0
- package/agent/extensions/modelconf/src/persistence.ts +164 -0
- package/agent/extensions/modelconf/src/ui/ModelConfView.test.ts +75 -0
- package/agent/extensions/modelconf/src/ui/ModelConfView.ts +1101 -0
- package/agent/extensions/modelconf/tsconfig.json +15 -0
- package/agent/extensions/pi-web-access/CHANGELOG.md +690 -0
- package/agent/extensions/pi-web-access/LICENSE +21 -0
- package/agent/extensions/pi-web-access/README.md +470 -0
- package/agent/extensions/pi-web-access/SECURITY.md +5 -0
- package/agent/extensions/pi-web-access/activity.ts +101 -0
- package/agent/extensions/pi-web-access/auth-fetch.ts +148 -0
- package/agent/extensions/pi-web-access/banner.png +0 -0
- package/agent/extensions/pi-web-access/brightdata-unlocker.ts +272 -0
- package/agent/extensions/pi-web-access/chrome-cookies.ts +669 -0
- package/agent/extensions/pi-web-access/content-find.ts +139 -0
- package/agent/extensions/pi-web-access/credential-source.ts +191 -0
- package/agent/extensions/pi-web-access/data-uri-sanitize.ts +406 -0
- package/agent/extensions/pi-web-access/datalab-pdf-extract.ts +568 -0
- package/agent/extensions/pi-web-access/declared-web-links.ts +173 -0
- package/agent/extensions/pi-web-access/evidence/CONTRACT-EVIDENCE.md +496 -0
- package/agent/extensions/pi-web-access/evidence/contract-probe.mjs +140 -0
- package/agent/extensions/pi-web-access/exa.ts +526 -0
- package/agent/extensions/pi-web-access/extract.ts +1196 -0
- package/agent/extensions/pi-web-access/feature-config.ts +29 -0
- package/agent/extensions/pi-web-access/fetch-params.ts +111 -0
- package/agent/extensions/pi-web-access/gemini-adc.ts +298 -0
- package/agent/extensions/pi-web-access/gemini-api.ts +353 -0
- package/agent/extensions/pi-web-access/gemini-pdf-extract.ts +108 -0
- package/agent/extensions/pi-web-access/gemini-search.ts +21 -0
- package/agent/extensions/pi-web-access/gemini-url-context.ts +128 -0
- package/agent/extensions/pi-web-access/gemini-web-config.ts +101 -0
- package/agent/extensions/pi-web-access/gemini-web.ts +487 -0
- package/agent/extensions/pi-web-access/github-api.ts +197 -0
- package/agent/extensions/pi-web-access/github-extract.ts +746 -0
- package/agent/extensions/pi-web-access/github-issue-pr.ts +700 -0
- package/agent/extensions/pi-web-access/index.ts +1737 -0
- package/agent/extensions/pi-web-access/package-lock.json +5808 -0
- package/agent/extensions/pi-web-access/package.json +64 -0
- package/agent/extensions/pi-web-access/page-query.ts +96 -0
- package/agent/extensions/pi-web-access/pdf-extract.ts +409 -0
- package/agent/extensions/pi-web-access/pi-web-fetch-demo.mp4 +0 -0
- package/agent/extensions/pi-web-access/promise-try.d.ts +7 -0
- package/agent/extensions/pi-web-access/query-rewrite.ts +51 -0
- package/agent/extensions/pi-web-access/render-search-error.ts +170 -0
- package/agent/extensions/pi-web-access/rsc-extract.ts +338 -0
- package/agent/extensions/pi-web-access/search-types.ts +20 -0
- package/agent/extensions/pi-web-access/source-check.ts +282 -0
- package/agent/extensions/pi-web-access/ssrf-protection.ts +526 -0
- package/agent/extensions/pi-web-access/storage.ts +521 -0
- package/agent/extensions/pi-web-access/summary-model-scope.ts +125 -0
- package/agent/extensions/pi-web-access/test/auth-fetch.test.mjs +208 -0
- package/agent/extensions/pi-web-access/test/brightdata-unlocker.test.mjs +840 -0
- package/agent/extensions/pi-web-access/test/chrome-cookie-extraction.test.mjs +441 -0
- package/agent/extensions/pi-web-access/test/config-path.test.mjs +283 -0
- package/agent/extensions/pi-web-access/test/content-find.test.mjs +25 -0
- package/agent/extensions/pi-web-access/test/credential-source.test.mjs +118 -0
- package/agent/extensions/pi-web-access/test/data-uri-sanitize.test.mjs +210 -0
- package/agent/extensions/pi-web-access/test/datalab-pdf-extract.test.mjs +552 -0
- package/agent/extensions/pi-web-access/test/declared-web-links.test.mjs +212 -0
- package/agent/extensions/pi-web-access/test/fetch-answer-storage.test.mjs +40 -0
- package/agent/extensions/pi-web-access/test/fetch-cache-storage.test.mjs +334 -0
- package/agent/extensions/pi-web-access/test/fetch-content-domain-policy.test.mjs +95 -0
- package/agent/extensions/pi-web-access/test/fetch-modes.test.mjs +53 -0
- package/agent/extensions/pi-web-access/test/fetch-not-found-guidance.test.mjs +92 -0
- package/agent/extensions/pi-web-access/test/fetch-params.test.mjs +86 -0
- package/agent/extensions/pi-web-access/test/fetch-render-call.test.mjs +34 -0
- package/agent/extensions/pi-web-access/test/fetch-routing.test.mjs +173 -0
- package/agent/extensions/pi-web-access/test/gemini-adc-auth.test.mjs +257 -0
- package/agent/extensions/pi-web-access/test/gemini-api-transport.test.mjs +170 -0
- package/agent/extensions/pi-web-access/test/gemini-pdf-extract.test.mjs +133 -0
- package/agent/extensions/pi-web-access/test/gemini-web-cookie-opt-in.test.mjs +178 -0
- package/agent/extensions/pi-web-access/test/gemini-web-header-overflow.test.mjs +148 -0
- package/agent/extensions/pi-web-access/test/get-search-content.test.mjs +223 -0
- package/agent/extensions/pi-web-access/test/github-extract.test.mjs +378 -0
- package/agent/extensions/pi-web-access/test/github-issue-pr.test.mjs +565 -0
- package/agent/extensions/pi-web-access/test/inline-content-config.test.mjs +99 -0
- package/agent/extensions/pi-web-access/test/lazy-extract-load.test.mjs +118 -0
- package/agent/extensions/pi-web-access/test/local-video-oversize.test.mjs +52 -0
- package/agent/extensions/pi-web-access/test/package-typebox-dependency.test.mjs +50 -0
- package/agent/extensions/pi-web-access/test/page-query.test.mjs +51 -0
- package/agent/extensions/pi-web-access/test/pdf-config.test.mjs +140 -0
- package/agent/extensions/pi-web-access/test/pdf-extract.test.mjs +500 -0
- package/agent/extensions/pi-web-access/test/proxy-transport.test.mjs +286 -0
- package/agent/extensions/pi-web-access/test/query-rewrite.test.mjs +52 -0
- package/agent/extensions/pi-web-access/test/rsc-fallback.test.mjs +102 -0
- package/agent/extensions/pi-web-access/test/search-error-render.test.mjs +152 -0
- package/agent/extensions/pi-web-access/test/search-providers.test.mjs +274 -0
- package/agent/extensions/pi-web-access/test/source-check.test.mjs +179 -0
- package/agent/extensions/pi-web-access/test/ssrf-allow-ranges-config.test.mjs +205 -0
- package/agent/extensions/pi-web-access/test/ssrf-protection.test.mjs +456 -0
- package/agent/extensions/pi-web-access/test/summary-model-scope.test.mjs +106 -0
- package/agent/extensions/pi-web-access/test/tool-registration-config.test.mjs +182 -0
- package/agent/extensions/pi-web-access/test/web-search-answer-render.test.mjs +66 -0
- package/agent/extensions/pi-web-access/test/youtube-extract-errors.test.mjs +64 -0
- package/agent/extensions/pi-web-access/tsconfig.json +11 -0
- package/agent/extensions/pi-web-access/utils.ts +451 -0
- package/agent/extensions/pi-web-access/video-extract.ts +392 -0
- package/agent/extensions/pi-web-access/youtube-extract.ts +328 -0
- package/agent/extensions/shared/activity-status.ts +31 -0
- package/agent/extensions/shared/child-session.test.ts +270 -0
- package/agent/extensions/shared/child-session.ts +148 -0
- package/agent/extensions/shared/context-utilization.test.ts +48 -0
- package/agent/extensions/shared/context-utilization.ts +47 -0
- package/agent/extensions/shared/dashboard-state.ts +99 -0
- package/agent/extensions/shared/install.ps1 +23 -0
- package/agent/extensions/shared/install.sh +21 -0
- package/agent/extensions/shared/tool-call-timeout.test.ts +117 -0
- package/agent/extensions/shared/tool-call-timeout.ts +104 -0
- package/agent/extensions/skillsconf/README.md +74 -0
- package/agent/extensions/skillsconf/index.ts +92 -0
- package/agent/extensions/skillsconf/install.ps1 +23 -0
- package/agent/extensions/skillsconf/install.sh +21 -0
- package/agent/extensions/skillsconf/package-lock.json +1809 -0
- package/agent/extensions/skillsconf/package.json +18 -0
- package/agent/extensions/skillsconf/src/delete-skill.test.ts +64 -0
- package/agent/extensions/skillsconf/src/delete-skill.ts +45 -0
- package/agent/extensions/skillsconf/src/filter.test.ts +80 -0
- package/agent/extensions/skillsconf/src/filter.ts +74 -0
- package/agent/extensions/skillsconf/src/fuzzy.ts +26 -0
- package/agent/extensions/skillsconf/src/persistence.test.ts +72 -0
- package/agent/extensions/skillsconf/src/persistence.ts +169 -0
- package/agent/extensions/skillsconf/src/ui/SkillConfView.test.ts +337 -0
- package/agent/extensions/skillsconf/src/ui/SkillConfView.ts +758 -0
- package/agent/extensions/skillsconf/src/ui/text-input.ts +91 -0
- package/agent/extensions/skillsconf/src/ui/tui-helpers.ts +144 -0
- package/agent/extensions/skillsconf/tsconfig.json +15 -0
- package/agent/extensions/status line/index.ts +282 -0
- package/agent/extensions/status line/install.ps1 +23 -0
- package/agent/extensions/status line/install.sh +21 -0
- package/agent/extensions/subagents/by-the-way.test.ts +29 -0
- package/agent/extensions/subagents/claude.test.ts +119 -0
- package/agent/extensions/subagents/codex.test.ts +102 -0
- package/agent/extensions/subagents/context-usage.test.ts +107 -0
- package/agent/extensions/subagents/docs/design-plan.md +568 -0
- package/agent/extensions/subagents/docs/effect-v4-extension-guide.md +354 -0
- package/agent/extensions/subagents/docs/effect-v4-notes.md +571 -0
- package/agent/extensions/subagents/index.ts +779 -0
- package/agent/extensions/subagents/install.ps1 +23 -0
- package/agent/extensions/subagents/install.sh +21 -0
- package/agent/extensions/subagents/manager.test.ts +276 -0
- package/agent/extensions/subagents/package-lock.json +2244 -0
- package/agent/extensions/subagents/package.json +19 -0
- package/agent/extensions/subagents/result-delivery.test.ts +27 -0
- package/agent/extensions/subagents/src/backend.ts +73 -0
- package/agent/extensions/subagents/src/backends/claude.ts +701 -0
- package/agent/extensions/subagents/src/backends/codex.ts +1060 -0
- package/agent/extensions/subagents/src/backends/pi.ts +575 -0
- package/agent/extensions/subagents/src/backends/stub.ts +300 -0
- package/agent/extensions/subagents/src/by-the-way.ts +21 -0
- package/agent/extensions/subagents/src/domain.ts +253 -0
- package/agent/extensions/subagents/src/format.ts +74 -0
- package/agent/extensions/subagents/src/manager.ts +736 -0
- package/agent/extensions/subagents/src/prompt.ts +92 -0
- package/agent/extensions/subagents/src/result-delivery.ts +20 -0
- package/agent/extensions/subagents/src/runtime.ts +53 -0
- package/agent/extensions/subagents/src/ui/takeover.ts +583 -0
- package/agent/extensions/subagents/src/ui/transcript.ts +201 -0
- package/agent/extensions/subagents/takeover.test.ts +29 -0
- package/agent/extensions/subagents/tsconfig.json +7 -0
- package/agent/extensions/taste/index.ts +443 -0
- package/agent/extensions/taste/install.ps1 +23 -0
- package/agent/extensions/taste/install.sh +21 -0
- package/agent/extensions/todo/AGENTS.md +38 -0
- package/agent/extensions/todo/LICENSE +21 -0
- package/agent/extensions/todo/config.ts +55 -0
- package/agent/extensions/todo/index.ts +151 -0
- package/agent/extensions/todo/install.ps1 +23 -0
- package/agent/extensions/todo/install.sh +21 -0
- package/agent/extensions/todo/locales/de.json +17 -0
- package/agent/extensions/todo/locales/en.json +15 -0
- package/agent/extensions/todo/locales/es.json +17 -0
- package/agent/extensions/todo/locales/fr.json +17 -0
- package/agent/extensions/todo/locales/pt-BR.json +17 -0
- package/agent/extensions/todo/locales/pt.json +17 -0
- package/agent/extensions/todo/locales/ru.json +17 -0
- package/agent/extensions/todo/locales/uk.json +17 -0
- package/agent/extensions/todo/locales/zh.json +17 -0
- package/agent/extensions/todo/package-lock.json +3358 -0
- package/agent/extensions/todo/package.json +67 -0
- package/agent/extensions/todo/state/i18n-bridge.ts +64 -0
- package/agent/extensions/todo/state/invariants.ts +20 -0
- package/agent/extensions/todo/state/replay.ts +38 -0
- package/agent/extensions/todo/state/selectors.ts +107 -0
- package/agent/extensions/todo/state/state-reducer.ts +326 -0
- package/agent/extensions/todo/state/state.ts +18 -0
- package/agent/extensions/todo/state/store.ts +82 -0
- package/agent/extensions/todo/state/task-graph.ts +57 -0
- package/agent/extensions/todo/todo-overlay.ts +200 -0
- package/agent/extensions/todo/todo.ts +155 -0
- package/agent/extensions/todo/tool/response-envelope.ts +109 -0
- package/agent/extensions/todo/tool/types.ts +206 -0
- package/agent/extensions/todo/verify-ref-system.js +0 -0
- package/agent/extensions/todo/view/format.ts +177 -0
- package/agent/extensions/trim-context/README.md +54 -0
- package/agent/extensions/trim-context/index.ts +487 -0
- package/agent/extensions/trim-context/install.ps1 +23 -0
- package/agent/extensions/trim-context/install.sh +21 -0
- package/agent/install.ps1 +527 -0
- package/agent/install.sh +511 -0
- package/agent/keybindings.json +7 -0
- package/bin/kiln.js +359 -0
- package/package.json +21 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { mkdtemp, writeFile } from "node:fs/promises";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { test } from "node:test";
|
|
7
|
+
|
|
8
|
+
const geminiApiModuleUrl = new URL("../gemini-api.ts", import.meta.url).href;
|
|
9
|
+
const geminiAdcModuleUrl = new URL("../gemini-adc.ts", import.meta.url).href;
|
|
10
|
+
|
|
11
|
+
function runChild(script, env) {
|
|
12
|
+
const childEnv = { ...process.env };
|
|
13
|
+
for (const key of [
|
|
14
|
+
"PI_CODING_AGENT_DIR",
|
|
15
|
+
"XDG_CONFIG_HOME",
|
|
16
|
+
"GEMINI_API_KEY",
|
|
17
|
+
"GOOGLE_GEMINI_BASE_URL",
|
|
18
|
+
"CLOUDFLARE_API_KEY",
|
|
19
|
+
"GOOGLE_APPLICATION_CREDENTIALS",
|
|
20
|
+
"GOOGLE_CLOUD_PROJECT",
|
|
21
|
+
"GOOGLE_CLOUD_LOCATION",
|
|
22
|
+
"GCLOUD_PROJECT",
|
|
23
|
+
]) {
|
|
24
|
+
delete childEnv[key];
|
|
25
|
+
}
|
|
26
|
+
// macOS homedir() ignores HOME, so pin the ADC file explicitly so tests are hermetic.
|
|
27
|
+
Object.assign(childEnv, env);
|
|
28
|
+
return spawnSync(process.execPath, ["--input-type=module"], {
|
|
29
|
+
input: script,
|
|
30
|
+
encoding: "utf8",
|
|
31
|
+
env: childEnv,
|
|
32
|
+
maxBuffer: 2 * 1024 * 1024,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function writeAdc(root, extra = {}) {
|
|
37
|
+
return writeFile(
|
|
38
|
+
join(root, "adc.json"),
|
|
39
|
+
JSON.stringify({
|
|
40
|
+
client_id: "synthetic-client-id",
|
|
41
|
+
client_secret: "synthetic-client-secret",
|
|
42
|
+
refresh_token: "synthetic-refresh-token",
|
|
43
|
+
type: "authorized_user",
|
|
44
|
+
universe_domain: "googleapis.com",
|
|
45
|
+
...extra,
|
|
46
|
+
}) + "\n",
|
|
47
|
+
"utf8",
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
test("Gemini ADC availability requires geminiAuth adc, project, location, and an ADC file", async () => {
|
|
52
|
+
const root = await mkdtemp(join(tmpdir(), "pi-web-access-gemini-adc-avail-"));
|
|
53
|
+
await writeFile(join(root, "web-search.json"), JSON.stringify({ geminiAuth: "adc", geminiProject: "p", geminiLocation: "l" }) + "\n", "utf8");
|
|
54
|
+
|
|
55
|
+
// ADC file does not exist yet -> not available.
|
|
56
|
+
const child = runChild(`
|
|
57
|
+
const { isGeminiApiAvailable } = await import(${JSON.stringify(geminiApiModuleUrl)});
|
|
58
|
+
const adc = await import(${JSON.stringify(geminiAdcModuleUrl)});
|
|
59
|
+
console.log(JSON.stringify({ adcAvail: adc.isGeminiAdcAvailable(), apiAvail: isGeminiApiAvailable() }));
|
|
60
|
+
`, { HOME: root, USERPROFILE: root, PI_CODING_AGENT_DIR: root, GOOGLE_APPLICATION_CREDENTIALS: join(root, "adc.json") });
|
|
61
|
+
assert.equal(child.status, 0, child.stderr);
|
|
62
|
+
assert.deepEqual(JSON.parse(child.stdout.trim()), { adcAvail: false, apiAvail: false });
|
|
63
|
+
|
|
64
|
+
// Now write an ADC file; availability should flip on.
|
|
65
|
+
await writeAdc(root);
|
|
66
|
+
const child2 = runChild(`
|
|
67
|
+
const { isGeminiApiAvailable } = await import(${JSON.stringify(geminiApiModuleUrl)});
|
|
68
|
+
const adc = await import(${JSON.stringify(geminiAdcModuleUrl)});
|
|
69
|
+
console.log(JSON.stringify({ adcAvail: adc.isGeminiAdcAvailable(), apiAvail: isGeminiApiAvailable(), missingProject: (await import(${JSON.stringify(geminiAdcModuleUrl)})).getAdcProject() }));
|
|
70
|
+
`, { HOME: root, USERPROFILE: root, PI_CODING_AGENT_DIR: root, GOOGLE_APPLICATION_CREDENTIALS: join(root, "adc.json") });
|
|
71
|
+
assert.equal(child2.status, 0, child2.stderr);
|
|
72
|
+
const avail = JSON.parse(child2.stdout.trim());
|
|
73
|
+
assert.equal(avail.adcAvail, true);
|
|
74
|
+
assert.equal(avail.apiAvail, true);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("Gemini ADC config parse errors stay visible", async () => {
|
|
78
|
+
const root = await mkdtemp(join(tmpdir(), "pi-web-access-gemini-adc-config-"));
|
|
79
|
+
await writeFile(join(root, "web-search.json"), "not valid JSON", "utf8");
|
|
80
|
+
|
|
81
|
+
const child = runChild(`
|
|
82
|
+
const adc = await import(${JSON.stringify(geminiAdcModuleUrl)});
|
|
83
|
+
let error = null;
|
|
84
|
+
try {
|
|
85
|
+
adc.isGeminiAdcAvailable();
|
|
86
|
+
} catch (err) {
|
|
87
|
+
error = err.message;
|
|
88
|
+
}
|
|
89
|
+
console.log(JSON.stringify({ error }));
|
|
90
|
+
`, { HOME: root, USERPROFILE: root, PI_CODING_AGENT_DIR: root, GOOGLE_APPLICATION_CREDENTIALS: join(root, "adc.json") });
|
|
91
|
+
|
|
92
|
+
assert.equal(child.status, 0, child.stderr);
|
|
93
|
+
const { error } = JSON.parse(child.stdout.trim());
|
|
94
|
+
assert.match(error, /Failed to parse .*web-search\.json/);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("Gemini ADC credential parse errors include the file path", async () => {
|
|
98
|
+
const root = await mkdtemp(join(tmpdir(), "pi-web-access-gemini-adc-file-"));
|
|
99
|
+
const adcPath = join(root, "adc.json");
|
|
100
|
+
await writeFile(adcPath, "not valid JSON", "utf8");
|
|
101
|
+
|
|
102
|
+
const child = runChild(`
|
|
103
|
+
const adc = await import(${JSON.stringify(geminiAdcModuleUrl)});
|
|
104
|
+
let error = null;
|
|
105
|
+
try {
|
|
106
|
+
await adc.getAdcAccessToken();
|
|
107
|
+
} catch (err) {
|
|
108
|
+
error = err.message;
|
|
109
|
+
}
|
|
110
|
+
console.log(JSON.stringify({ error }));
|
|
111
|
+
`, { HOME: root, USERPROFILE: root, PI_CODING_AGENT_DIR: root, GOOGLE_APPLICATION_CREDENTIALS: adcPath });
|
|
112
|
+
|
|
113
|
+
assert.equal(child.status, 0, child.stderr);
|
|
114
|
+
const { error } = JSON.parse(child.stdout.trim());
|
|
115
|
+
assert.match(error, /Failed to parse Google Application Default Credentials file at /);
|
|
116
|
+
assert.match(error, /adc\.json/);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("Gemini ADC credential shape errors include the file path", async () => {
|
|
120
|
+
const root = await mkdtemp(join(tmpdir(), "pi-web-access-gemini-adc-shape-"));
|
|
121
|
+
const adcPath = join(root, "adc.json");
|
|
122
|
+
await writeFile(adcPath, JSON.stringify({ type: "authorized_user", client_id: "id", client_secret: "secret" }) + "\n", "utf8");
|
|
123
|
+
|
|
124
|
+
const child = runChild(`
|
|
125
|
+
const adc = await import(${JSON.stringify(geminiAdcModuleUrl)});
|
|
126
|
+
let error = null;
|
|
127
|
+
try {
|
|
128
|
+
await adc.getAdcAccessToken();
|
|
129
|
+
} catch (err) {
|
|
130
|
+
error = err.message;
|
|
131
|
+
}
|
|
132
|
+
console.log(JSON.stringify({ error }));
|
|
133
|
+
`, { HOME: root, USERPROFILE: root, PI_CODING_AGENT_DIR: root, GOOGLE_APPLICATION_CREDENTIALS: adcPath });
|
|
134
|
+
|
|
135
|
+
assert.equal(child.status, 0, child.stderr);
|
|
136
|
+
const { error } = JSON.parse(child.stdout.trim());
|
|
137
|
+
assert.match(error, /Failed to parse Google Application Default Credentials file at /);
|
|
138
|
+
assert.match(error, /authorized_user file is missing refresh_token/);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("Gemini ADC rejects malformed token success responses", async () => {
|
|
142
|
+
const root = await mkdtemp(join(tmpdir(), "pi-web-access-gemini-adc-token-shape-"));
|
|
143
|
+
await writeAdc(root);
|
|
144
|
+
const env = { HOME: root, USERPROFILE: root, PI_CODING_AGENT_DIR: root, GOOGLE_APPLICATION_CREDENTIALS: join(root, "adc.json") };
|
|
145
|
+
|
|
146
|
+
const child = runChild(`
|
|
147
|
+
globalThis.fetch = async () => new Response(JSON.stringify({ access_token: true, expires_in: "bad" }), { status: 200 });
|
|
148
|
+
const adc = await import(${JSON.stringify(geminiAdcModuleUrl)});
|
|
149
|
+
let error = null;
|
|
150
|
+
try {
|
|
151
|
+
await adc.getAdcAccessToken();
|
|
152
|
+
} catch (err) {
|
|
153
|
+
error = err.message;
|
|
154
|
+
}
|
|
155
|
+
console.log(JSON.stringify({ error }));
|
|
156
|
+
`, env);
|
|
157
|
+
|
|
158
|
+
assert.equal(child.status, 0, child.stderr);
|
|
159
|
+
const { error } = JSON.parse(child.stdout.trim());
|
|
160
|
+
assert.match(error, /Gemini ADC token exchange returned no access_token/);
|
|
161
|
+
|
|
162
|
+
const expires = runChild(`
|
|
163
|
+
globalThis.fetch = async () => new Response(JSON.stringify({ access_token: "token", expires_in: "bad" }), { status: 200 });
|
|
164
|
+
const adc = await import(${JSON.stringify(geminiAdcModuleUrl)});
|
|
165
|
+
let error = null;
|
|
166
|
+
try {
|
|
167
|
+
await adc.getAdcAccessToken();
|
|
168
|
+
} catch (err) {
|
|
169
|
+
error = err.message;
|
|
170
|
+
}
|
|
171
|
+
console.log(JSON.stringify({ error }));
|
|
172
|
+
`, env);
|
|
173
|
+
|
|
174
|
+
assert.equal(expires.status, 0, expires.stderr);
|
|
175
|
+
assert.match(JSON.parse(expires.stdout.trim()).error, /Gemini ADC token exchange returned invalid expires_in/);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("Gemini ADC classifies only credential rejections as CredentialResolutionError", async () => {
|
|
179
|
+
const root = await mkdtemp(join(tmpdir(), "pi-web-access-gemini-adc-classify-"));
|
|
180
|
+
await writeAdc(root);
|
|
181
|
+
const env = { HOME: root, USERPROFILE: root, PI_CODING_AGENT_DIR: root, GOOGLE_APPLICATION_CREDENTIALS: join(root, "adc.json") };
|
|
182
|
+
|
|
183
|
+
const classify = (status, body) => runChild(`
|
|
184
|
+
globalThis.fetch = async () => new Response(${JSON.stringify(body)}, { status: ${status} });
|
|
185
|
+
const adc = await import(${JSON.stringify(geminiAdcModuleUrl)});
|
|
186
|
+
let threw = null;
|
|
187
|
+
try {
|
|
188
|
+
await adc.getAdcAccessToken();
|
|
189
|
+
} catch (err) {
|
|
190
|
+
threw = { name: err.name, category: err.category, message: err.message };
|
|
191
|
+
}
|
|
192
|
+
console.log(JSON.stringify({ threw }));
|
|
193
|
+
`, env);
|
|
194
|
+
|
|
195
|
+
// Credential rejections -> CredentialResolutionError.
|
|
196
|
+
for (const status of [400, 401, 403]) {
|
|
197
|
+
const out = classify(status, JSON.stringify({ error: "denied" }));
|
|
198
|
+
assert.equal(out.status, 0, out.stderr);
|
|
199
|
+
const { threw } = JSON.parse(out.stdout.trim());
|
|
200
|
+
assert.equal(threw.name, "CredentialResolutionError", `status ${status}`);
|
|
201
|
+
assert.equal(threw.category, "oauth-credential-rejected", `status ${status}`);
|
|
202
|
+
assert.match(threw.message, /OAuth token exchange rejected/, `status ${status}`);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Transient failures stay plain Errors so fallbacks still run.
|
|
206
|
+
for (const status of [429, 404, 500, 502, 503]) {
|
|
207
|
+
const out = classify(status, "upstream boom");
|
|
208
|
+
assert.equal(out.status, 0, out.stderr);
|
|
209
|
+
const { threw } = JSON.parse(out.stdout.trim());
|
|
210
|
+
assert.equal(threw.name, "Error", `status ${status}`);
|
|
211
|
+
assert.equal(threw.category, undefined, `status ${status}`);
|
|
212
|
+
assert.match(threw.message, new RegExp(`token exchange failed \\(${status}\\)`), `status ${status}`);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("Gemini ADC does not advertise the Files-API (YouTube/video) path", async () => {
|
|
217
|
+
const root = await mkdtemp(join(tmpdir(), "pi-web-access-gemini-adc-video-"));
|
|
218
|
+
await writeAdc(root);
|
|
219
|
+
await writeFile(
|
|
220
|
+
join(root, "web-search.json"),
|
|
221
|
+
JSON.stringify({ geminiAuth: "adc", geminiProject: "p", geminiLocation: "l" }) + "\n",
|
|
222
|
+
"utf8",
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
// ADC only: generate-content paths are available, the Files-API path is not.
|
|
226
|
+
const adcOnly = runChild(`
|
|
227
|
+
const api = await import(${JSON.stringify(geminiApiModuleUrl)});
|
|
228
|
+
const adc = await import(${JSON.stringify(geminiAdcModuleUrl)});
|
|
229
|
+
console.log(JSON.stringify({
|
|
230
|
+
adcAvail: adc.isGeminiAdcAvailable(),
|
|
231
|
+
apiAvail: api.isGeminiApiAvailable(),
|
|
232
|
+
videoAvail: api.isGeminiApiAvailableWithVideo(),
|
|
233
|
+
}));
|
|
234
|
+
`, { HOME: root, USERPROFILE: root, PI_CODING_AGENT_DIR: root, GOOGLE_APPLICATION_CREDENTIALS: join(root, "adc.json") });
|
|
235
|
+
assert.equal(adcOnly.status, 0, adcOnly.stderr);
|
|
236
|
+
assert.deepEqual(JSON.parse(adcOnly.stdout.trim()), {
|
|
237
|
+
adcAvail: true,
|
|
238
|
+
apiAvail: true,
|
|
239
|
+
videoAvail: false,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// ADC + API key: the Files-API path becomes available again.
|
|
243
|
+
await writeFile(
|
|
244
|
+
join(root, "web-search.json"),
|
|
245
|
+
JSON.stringify({ geminiAuth: "adc", geminiProject: "p", geminiLocation: "l", geminiApiKey: "synthetic-key" }) + "\n",
|
|
246
|
+
"utf8",
|
|
247
|
+
);
|
|
248
|
+
const withKey = runChild(`
|
|
249
|
+
const api = await import(${JSON.stringify(geminiApiModuleUrl)});
|
|
250
|
+
console.log(JSON.stringify({
|
|
251
|
+
apiAvail: api.isGeminiApiAvailable(),
|
|
252
|
+
videoAvail: api.isGeminiApiAvailableWithVideo(),
|
|
253
|
+
}));
|
|
254
|
+
`, { HOME: root, USERPROFILE: root, PI_CODING_AGENT_DIR: root, GOOGLE_APPLICATION_CREDENTIALS: join(root, "adc.json") });
|
|
255
|
+
assert.equal(withKey.status, 0, withKey.stderr);
|
|
256
|
+
assert.deepEqual(JSON.parse(withKey.stdout.trim()), { apiAvail: true, videoAvail: true });
|
|
257
|
+
});
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { mkdtemp, writeFile } from "node:fs/promises";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { test } from "node:test";
|
|
7
|
+
|
|
8
|
+
const videoModuleUrl = new URL("../video-extract.ts", import.meta.url).href;
|
|
9
|
+
|
|
10
|
+
function runChild(script, env) {
|
|
11
|
+
const childEnv = { ...process.env };
|
|
12
|
+
for (const key of ["PI_CODING_AGENT_DIR", "XDG_CONFIG_HOME", "GEMINI_API_KEY", "GOOGLE_GEMINI_BASE_URL", "CLOUDFLARE_API_KEY"]) {
|
|
13
|
+
delete childEnv[key];
|
|
14
|
+
}
|
|
15
|
+
Object.assign(childEnv, env);
|
|
16
|
+
return spawnSync(process.execPath, ["--input-type=module"], {
|
|
17
|
+
input: script,
|
|
18
|
+
encoding: "utf8",
|
|
19
|
+
env: childEnv,
|
|
20
|
+
maxBuffer: 2 * 1024 * 1024,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
test("Gemini generate, upload, status, and delete use only shared header auth", async () => {
|
|
25
|
+
const root = await mkdtemp(join(tmpdir(), "pi-web-access-gemini-transport-"));
|
|
26
|
+
const mediaPath = join(root, "synthetic.webm");
|
|
27
|
+
await writeFile(mediaPath, "synthetic media", "utf8");
|
|
28
|
+
await writeFile(join(root, "web-search.json"), JSON.stringify({ geminiApiKey: "synthetic-gemini-key" }) + "\n", "utf8");
|
|
29
|
+
|
|
30
|
+
const child = runChild(`
|
|
31
|
+
const requests = [];
|
|
32
|
+
globalThis.fetch = async (url, init = {}) => {
|
|
33
|
+
const request = {
|
|
34
|
+
url: String(url),
|
|
35
|
+
method: init.method ?? "GET",
|
|
36
|
+
headers: Object.fromEntries(new Headers(init.headers)),
|
|
37
|
+
};
|
|
38
|
+
requests.push(request);
|
|
39
|
+
if (request.url.endsWith("/upload/v1beta/files")) {
|
|
40
|
+
return new Response("", {
|
|
41
|
+
status: 200,
|
|
42
|
+
headers: { "x-goog-upload-url": "https://generativelanguage.googleapis.com/upload/v1beta/files?upload_id=synthetic" },
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (request.url.includes("upload_id=synthetic")) {
|
|
46
|
+
return new Response(JSON.stringify({ file: { name: "files/synthetic", uri: "files/synthetic" } }), {
|
|
47
|
+
status: 200,
|
|
48
|
+
headers: { "content-type": "application/json" },
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
if (request.url.endsWith("/v1beta/files/synthetic") && request.method === "GET") {
|
|
52
|
+
return new Response(JSON.stringify({ state: "ACTIVE" }), {
|
|
53
|
+
status: 200,
|
|
54
|
+
headers: { "content-type": "application/json" },
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (request.url.includes(":generateContent")) {
|
|
58
|
+
return new Response(JSON.stringify({ candidates: [{ content: { parts: [{ text: "# Synthetic video" }] } }] }), {
|
|
59
|
+
status: 200,
|
|
60
|
+
headers: { "content-type": "application/json" },
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
if (request.url.endsWith("/v1beta/files/synthetic") && request.method === "DELETE") {
|
|
64
|
+
return new Response("", { status: 204 });
|
|
65
|
+
}
|
|
66
|
+
throw new Error("Unexpected fetch: " + request.url);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const { extractVideo } = await import(${JSON.stringify(videoModuleUrl)});
|
|
70
|
+
const result = await extractVideo({
|
|
71
|
+
absolutePath: ${JSON.stringify(mediaPath)},
|
|
72
|
+
mimeType: "video/webm",
|
|
73
|
+
sizeBytes: 15,
|
|
74
|
+
maxSizeBytes: 50 * 1024 * 1024,
|
|
75
|
+
withinUploadLimit: true,
|
|
76
|
+
});
|
|
77
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
78
|
+
console.log(JSON.stringify({ result, requests }));
|
|
79
|
+
`, {
|
|
80
|
+
HOME: root,
|
|
81
|
+
USERPROFILE: root,
|
|
82
|
+
PI_CODING_AGENT_DIR: root,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
assert.equal(child.status, 0, child.stderr);
|
|
86
|
+
const output = JSON.parse(child.stdout.trim());
|
|
87
|
+
assert.equal(output.result.content, "# Synthetic video");
|
|
88
|
+
assert.deepEqual(output.requests.map(request => request.method), ["POST", "PUT", "GET", "POST", "DELETE"]);
|
|
89
|
+
const generateRequest = output.requests.find(request => request.url.includes(":generateContent"));
|
|
90
|
+
assert.equal(generateRequest.url, "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.6-flash:generateContent");
|
|
91
|
+
for (const request of output.requests) {
|
|
92
|
+
assert.equal(request.headers["x-goog-api-key"], "synthetic-gemini-key");
|
|
93
|
+
const url = new URL(request.url);
|
|
94
|
+
assert.equal(url.searchParams.has("key"), false);
|
|
95
|
+
assert.equal(url.searchParams.has("api_key"), false);
|
|
96
|
+
assert.equal(request.url.includes("synthetic-gemini-key"), false);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("Gemini video upload, status, and delete respect a configured geminiBaseUrl relay", async () => {
|
|
101
|
+
const root = await mkdtemp(join(tmpdir(), "pi-web-access-gemini-relay-"));
|
|
102
|
+
const mediaPath = join(root, "synthetic.webm");
|
|
103
|
+
await writeFile(mediaPath, "synthetic media", "utf8");
|
|
104
|
+
await writeFile(join(root, "web-search.json"), JSON.stringify({ geminiApiKey: "synthetic-gemini-key", geminiBaseUrl: "https://relay.example.com" }) + "\n", "utf8");
|
|
105
|
+
|
|
106
|
+
const child = runChild(`
|
|
107
|
+
const requests = [];
|
|
108
|
+
globalThis.fetch = async (url, init = {}) => {
|
|
109
|
+
const request = {
|
|
110
|
+
url: String(url),
|
|
111
|
+
method: init.method ?? "GET",
|
|
112
|
+
headers: Object.fromEntries(new Headers(init.headers)),
|
|
113
|
+
};
|
|
114
|
+
requests.push(request);
|
|
115
|
+
if (request.url.endsWith("/upload/v1beta/files")) {
|
|
116
|
+
return new Response("", {
|
|
117
|
+
status: 200,
|
|
118
|
+
headers: { "x-goog-upload-url": "https://relay.example.com/upload/v1beta/files?upload_id=synthetic" },
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (request.url.includes("upload_id=synthetic")) {
|
|
122
|
+
return new Response(JSON.stringify({ file: { name: "files/synthetic", uri: "files/synthetic" } }), {
|
|
123
|
+
status: 200,
|
|
124
|
+
headers: { "content-type": "application/json" },
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (request.url.endsWith("/v1beta/files/synthetic") && request.method === "GET") {
|
|
128
|
+
return new Response(JSON.stringify({ state: "ACTIVE" }), {
|
|
129
|
+
status: 200,
|
|
130
|
+
headers: { "content-type": "application/json" },
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (request.url.includes(":generateContent")) {
|
|
134
|
+
return new Response(JSON.stringify({ candidates: [{ content: { parts: [{ text: "# Relay video" }] } }] }), {
|
|
135
|
+
status: 200,
|
|
136
|
+
headers: { "content-type": "application/json" },
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (request.url.endsWith("/v1beta/files/synthetic") && request.method === "DELETE") {
|
|
140
|
+
return new Response("", { status: 204 });
|
|
141
|
+
}
|
|
142
|
+
throw new Error("Unexpected fetch: " + request.url);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const { extractVideo } = await import(${JSON.stringify(videoModuleUrl)});
|
|
146
|
+
const result = await extractVideo({
|
|
147
|
+
absolutePath: ${JSON.stringify(mediaPath)},
|
|
148
|
+
mimeType: "video/webm",
|
|
149
|
+
sizeBytes: 15,
|
|
150
|
+
maxSizeBytes: 50 * 1024 * 1024,
|
|
151
|
+
withinUploadLimit: true,
|
|
152
|
+
});
|
|
153
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
154
|
+
console.log(JSON.stringify({ result, requests }));
|
|
155
|
+
`, {
|
|
156
|
+
HOME: root,
|
|
157
|
+
USERPROFILE: root,
|
|
158
|
+
PI_CODING_AGENT_DIR: root,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
assert.equal(child.status, 0, child.stderr);
|
|
162
|
+
const output = JSON.parse(child.stdout.trim());
|
|
163
|
+
assert.equal(output.result.content, "# Relay video");
|
|
164
|
+
assert.deepEqual(output.requests.map(request => request.method), ["POST", "PUT", "GET", "POST", "DELETE"]);
|
|
165
|
+
for (const request of output.requests) {
|
|
166
|
+
assert.equal(new URL(request.url).origin, "https://relay.example.com", request.url);
|
|
167
|
+
assert.equal(request.headers["x-goog-api-key"], "synthetic-gemini-key");
|
|
168
|
+
assert.equal(request.url.includes("generativelanguage.googleapis.com"), false);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { after, test } from "node:test";
|
|
3
|
+
|
|
4
|
+
const originalFetch = globalThis.fetch;
|
|
5
|
+
const originalApiKey = process.env.GEMINI_API_KEY;
|
|
6
|
+
process.env.GEMINI_API_KEY = "synthetic-gemini-key";
|
|
7
|
+
|
|
8
|
+
const { extractPDFViaGemini } = await import("../gemini-pdf-extract.ts");
|
|
9
|
+
|
|
10
|
+
after(() => {
|
|
11
|
+
globalThis.fetch = originalFetch;
|
|
12
|
+
if (originalApiKey === undefined) delete process.env.GEMINI_API_KEY;
|
|
13
|
+
else process.env.GEMINI_API_KEY = originalApiKey;
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("Gemini PDF conversion sends inline PDF data and normalizes valid Markdown", async () => {
|
|
17
|
+
let capturedUrl = "";
|
|
18
|
+
let capturedInit;
|
|
19
|
+
globalThis.fetch = async (url, init) => {
|
|
20
|
+
capturedUrl = String(url);
|
|
21
|
+
capturedInit = init;
|
|
22
|
+
return jsonResponse({
|
|
23
|
+
candidates: [{
|
|
24
|
+
finishReason: "STOP",
|
|
25
|
+
content: { parts: [{ text: "```markdown\n<!-- Page 1 -->\n# Sample Document\n\nFirst page.\n\n<!-- Page 2 -->\n\nSecond page.\n```" }] },
|
|
26
|
+
}],
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const input = Uint8Array.from([1, 2, 3, 4]).buffer;
|
|
31
|
+
const markdown = await extractPDFViaGemini(input, {
|
|
32
|
+
pages: 5,
|
|
33
|
+
maxPages: 2,
|
|
34
|
+
title: "Sample Document",
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
assert.match(capturedUrl, /gemini-3\.6-flash:generateContent$/);
|
|
38
|
+
assert.equal(new Headers(capturedInit.headers).get("x-goog-api-key"), "synthetic-gemini-key");
|
|
39
|
+
const body = JSON.parse(capturedInit.body);
|
|
40
|
+
assert.equal(body.contents[0].role, "user");
|
|
41
|
+
assert.equal(body.contents[0].parts[0].inlineData.mimeType, "application/pdf");
|
|
42
|
+
assert.equal(body.contents[0].parts[0].inlineData.data, Buffer.from(input).toString("base64"));
|
|
43
|
+
assert.match(body.contents[0].parts[1].text, /pages 1 through 2/);
|
|
44
|
+
assert.match(markdown, /^<!-- Page 1 -->/);
|
|
45
|
+
assert.doesNotMatch(markdown, /^<!-- Page 1 -->\s*\n# Sample Document/m);
|
|
46
|
+
assert.match(markdown, /<!-- Page 2 -->/);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("Gemini PDF conversion supports Cloudflare AI Gateway without a direct Gemini key", async () => {
|
|
50
|
+
const savedApiKey = process.env.GEMINI_API_KEY;
|
|
51
|
+
const savedBaseUrl = process.env.GOOGLE_GEMINI_BASE_URL;
|
|
52
|
+
const savedCloudflareKey = process.env.CLOUDFLARE_API_KEY;
|
|
53
|
+
delete process.env.GEMINI_API_KEY;
|
|
54
|
+
process.env.GOOGLE_GEMINI_BASE_URL = "https://gateway.ai.cloudflare.com/v1/example/gemini";
|
|
55
|
+
process.env.CLOUDFLARE_API_KEY = "synthetic-cloudflare-key";
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
globalThis.fetch = async (url, init) => {
|
|
59
|
+
assert.match(String(url), /^https:\/\/gateway\.ai\.cloudflare\.com\//);
|
|
60
|
+
assert.equal(new Headers(init.headers).get("cf-aig-authorization"), "Bearer synthetic-cloudflare-key");
|
|
61
|
+
assert.equal(new Headers(init.headers).get("x-goog-api-key"), null);
|
|
62
|
+
return jsonResponse({
|
|
63
|
+
candidates: [{
|
|
64
|
+
finishReason: "STOP",
|
|
65
|
+
content: { parts: [{ text: "<!-- Page 1 -->\nGateway result" }] },
|
|
66
|
+
}],
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const markdown = await extractPDFViaGemini(new ArrayBuffer(1), {
|
|
71
|
+
pages: 1,
|
|
72
|
+
maxPages: 1,
|
|
73
|
+
title: "Document",
|
|
74
|
+
});
|
|
75
|
+
assert.match(markdown, /Gateway result/);
|
|
76
|
+
} finally {
|
|
77
|
+
if (savedApiKey === undefined) delete process.env.GEMINI_API_KEY;
|
|
78
|
+
else process.env.GEMINI_API_KEY = savedApiKey;
|
|
79
|
+
if (savedBaseUrl === undefined) delete process.env.GOOGLE_GEMINI_BASE_URL;
|
|
80
|
+
else process.env.GOOGLE_GEMINI_BASE_URL = savedBaseUrl;
|
|
81
|
+
if (savedCloudflareKey === undefined) delete process.env.CLOUDFLARE_API_KEY;
|
|
82
|
+
else process.env.CLOUDFLARE_API_KEY = savedCloudflareKey;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("Gemini PDF conversion rejects truncated output", async () => {
|
|
87
|
+
globalThis.fetch = async () => jsonResponse({
|
|
88
|
+
candidates: [{
|
|
89
|
+
finishReason: "MAX_TOKENS",
|
|
90
|
+
content: { parts: [{ text: "<!-- Page 1 -->\nPartial" }] },
|
|
91
|
+
}],
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
await assert.rejects(
|
|
95
|
+
extractPDFViaGemini(new ArrayBuffer(1), { pages: 1, maxPages: 1, title: "Document" }),
|
|
96
|
+
/MAX_TOKENS/,
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("Gemini PDF conversion rejects missing or out-of-sequence page markers", async () => {
|
|
101
|
+
globalThis.fetch = async () => jsonResponse({
|
|
102
|
+
candidates: [{
|
|
103
|
+
finishReason: "STOP",
|
|
104
|
+
content: { parts: [{ text: "<!-- Page 1 -->\nFirst\n\n<!-- Page 3 -->\nThird" }] },
|
|
105
|
+
}],
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
await assert.rejects(
|
|
109
|
+
extractPDFViaGemini(new ArrayBuffer(1), { pages: 2, maxPages: 2, title: "Document" }),
|
|
110
|
+
/out of sequence/,
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("Gemini PDF conversion surfaces safety blocks and empty responses", async () => {
|
|
115
|
+
globalThis.fetch = async () => jsonResponse({ promptFeedback: { blockReason: "SAFETY" } });
|
|
116
|
+
await assert.rejects(
|
|
117
|
+
extractPDFViaGemini(new ArrayBuffer(1), { pages: 1, maxPages: 1, title: "Document" }),
|
|
118
|
+
/blocked PDF extraction: SAFETY/,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
globalThis.fetch = async () => jsonResponse({ candidates: [{ finishReason: "STOP", content: { parts: [] } }] });
|
|
122
|
+
await assert.rejects(
|
|
123
|
+
extractPDFViaGemini(new ArrayBuffer(1), { pages: 1, maxPages: 1, title: "Document" }),
|
|
124
|
+
/empty PDF extraction/,
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
function jsonResponse(value) {
|
|
129
|
+
return new Response(JSON.stringify(value), {
|
|
130
|
+
status: 200,
|
|
131
|
+
headers: { "content-type": "application/json" },
|
|
132
|
+
});
|
|
133
|
+
}
|