openpencil/apps/web/server/utils/proxy-dispatcher.ts
Kayshen-X a7d73ebb62 feat(ai): pencil-style agentic design tool-loop, multi-chat tabs, #27 panel restyle
Built-in design generation now runs as an agentic MCP tool-loop (reusing the
agent-rs BuiltInProvider), gated behind OPENPENCIL_DESIGN_AGENT_LOOP / the
Settings experimental toggle; the orchestrator stays the default.

- design-agent system prompt + in-process design toolset (parity-locked with
  the MCP surface) + flag-gated Intent::Design routing
- spawn_agents execution as sequential sub-loops + live creation-mode badges
  (per-agent glow + 'N/M designing...' header)
- new MCP tools: get_guidelines, ToolSearch, get_screenshot, get_editor_state,
  export_nodes, spawn_agents; style-guide local audit
- #27 AI panel restyle: rounded tool cards + green check-rings, gray user
  bubbles, model-pill bottom toolbar, header, empty-state pills, the
  PARALLEL AGENTS (agent_team_size) 1x-6x chip dropdown
- multi-chat tabs: ChatSessions model (Deref-to-active) + tab row UI
  (switch / close / + / Cmd+T) with each run bound to its tab

Large checkpoint commit spanning the working tree (Rust shell crates).
2026-07-02 21:21:06 +08:00

51 lines
2.1 KiB
TypeScript

import { setGlobalDispatcher, EnvHttpProxyAgent } from 'undici';
/**
* Configure undici's global fetch dispatcher to honor HTTPS_PROXY /
* HTTP_PROXY / NO_PROXY env vars. Node's native `fetch` (built on
* undici) does NOT auto-route through the system proxy the way `curl`
* does — it goes direct, which fails with `ECONNREFUSED` on machines
* that route outbound HTTPS through a local proxy (clash / mihomo /
* corporate gateway). The image-search endpoint already swallows that
* failure and silently falls back to Wikimedia, but Wikimedia is
* blocked on the same machines, so designs land with empty image
* placeholders.
*
* `EnvHttpProxyAgent` is undici's built-in env-aware dispatcher: it
* reads HTTPS_PROXY / HTTP_PROXY / NO_PROXY (case-insensitive)
* directly from `process.env`, applies the bypass list to no-proxy
* hosts, and routes the rest through the configured proxy. When no
* proxy env var is set, requests pass through unchanged (production
* deploys, CI), so this is a safe no-op there.
*
* Why static ESM import (not `require('undici')`):
* The previous version did `require('undici')` inside a try/catch,
* thinking that would let it run on both CJS and ESM. In an ESM
* module (which is what Vite/Nitro produces in dev) `require` is
* undefined and the call threw a ReferenceError that got caught and
* silenced — meaning the proxy was never installed, and the
* image-search endpoint kept ECONNREFUSED-ing on proxied dev
* machines. undici ships inside Node 18+ itself (Node's fetch is
* built on it) and is always resolvable as an ESM module, so a
* static import is the right shape.
*
* Idempotent: first call installs the dispatcher, subsequent calls
* are no-ops, so endpoints can call this from their own module init
* without coordinating.
*/
let configured = false;
export function configureProxyDispatcher(): void {
if (configured) return;
configured = true;
const proxy =
process.env.HTTPS_PROXY ??
process.env.https_proxy ??
process.env.HTTP_PROXY ??
process.env.http_proxy;
if (!proxy) return;
setGlobalDispatcher(new EnvHttpProxyAgent());
}