openpencil/apps/web/server/api/ai
Fini 00670404df fix(ai): image-search server fetches honor HTTPS_PROXY env var
Root cause for all-blank-placeholders on the food-app brief: Node's
native fetch (used by the Nitro dev server's image-search endpoint)
ignores the system proxy by default. On machines that route outbound
HTTPS through a local proxy (clash / mihomo / corporate gateway —
mine sits at 127.0.0.1:7897), every Openverse + Wikimedia call from
the server silently ECONNREFUSEDs. The endpoint's catch block returns
`null` for Openverse → falls back to Wikimedia → that ECONNREFUSEDs
too → returns `[]`. Browser shows zero filled images.

Direct curl from the same machine uses HTTPS_PROXY automatically, which
is why a manual API check (e.g. `curl https://api.openverse.org/...`)
returned 240 results for "salmon sushi" while
`/api/ai/image-search?query=salmon%20sushi` returned `{results:[]}`.

`apps/web/server/utils/proxy-dispatcher.ts::configureProxyDispatcher`:
- Reads HTTPS_PROXY / https_proxy / HTTP_PROXY / http_proxy.
- If set, installs `undici.ProxyAgent` as the global fetch dispatcher
  via `setGlobalDispatcher`. From that point on every server-side
  `fetch()` routes through the proxy.
- Idempotent — multiple endpoints can call it without re-installing.
- No-op when no proxy env var is present (production / CI).
- Dynamic `require('undici')` so a build target that strips undici
  doesn't crash at import time.

Wired into `image-search.ts` at module top so the dispatcher is
configured before the first request lands. Other endpoints making
external fetches can opt in with the same single-line call.

Verified standalone via Bun: with the helper in place,
`fetch('https://api.openverse.org/v1/images/?q=salmon+sushi')` returns
240 results. The dev server itself needs a restart to pick up the
server-side change (Vite server-code HMR doesn't re-evaluate Nitro
modules).
2026-05-05 12:22:38 +08:00
..
agent.ts V0.7.3 (#111) 2026-04-15 22:19:12 +08:00
chat.ts fix(ai): unwrap fetch error.cause for actionable network failures 2026-04-26 19:20:32 +08:00
connect-acp.ts V0.7.1 (#102) 2026-04-13 21:30:23 +08:00
connect-agent.ts V0.7.2-bugfix (#109) 2026-04-14 21:42:56 +08:00
generate.ts V0.7.2-bugfix (#109) 2026-04-14 21:42:56 +08:00
icon.ts V0.7.0 (#95) 2026-04-11 23:25:13 +08:00
image-generate.ts V0.7.0 (#95) 2026-04-11 23:25:13 +08:00
image-search.ts fix(ai): image-search server fetches honor HTTPS_PROXY env var 2026-05-05 12:22:38 +08:00
image-service-test.ts V0.7.0 (#95) 2026-04-11 23:25:13 +08:00
install-agent.ts V0.7.0 (#95) 2026-04-11 23:25:13 +08:00
mcp-install.ts V0.7.3 (#111) 2026-04-15 22:19:12 +08:00
models.ts V0.7.0 (#95) 2026-04-11 23:25:13 +08:00
provider-models.ts fix(ai): unwrap fetch error.cause for actionable network failures 2026-04-26 19:20:32 +08:00
provider-url.ts fix(ai): unwrap fetch error.cause for actionable network failures 2026-04-26 19:20:32 +08:00
validate.ts V0.7.2-bugfix (#109) 2026-04-14 21:42:56 +08:00