|
| 1 | +import { browser } from '$app/environment'; |
| 2 | +import { SSEWEBSOCKETPROXYURL } from '$lib/constants/chat-tunnel'; |
| 3 | +import { createSSETunnelResponse } from '$lib/utils/websocket-tunnel'; |
| 4 | + |
| 5 | +type NormalizedRequest = { |
| 6 | + url: string; |
| 7 | + method: string; |
| 8 | + headers?: HeadersInit; |
| 9 | + body: string; |
| 10 | + signal?: AbortSignal; |
| 11 | +}; |
| 12 | + |
| 13 | +const globalState = globalThis as typeof globalThis & { |
| 14 | + __chatTunnelPatched?: boolean; |
| 15 | + __chatTunnelOriginalFetch?: typeof fetch; |
| 16 | +}; |
| 17 | + |
| 18 | +function resolveTunnelUrl(value: unknown): string | null { |
| 19 | + if (value === null || value === undefined) { |
| 20 | + return null; |
| 21 | + } |
| 22 | + |
| 23 | + const raw = typeof value === 'string' ? value : value.toString(); |
| 24 | + const trimmed = raw.trim(); |
| 25 | + |
| 26 | + return trimmed.length > 0 ? trimmed : null; |
| 27 | +} |
| 28 | + |
| 29 | +const RESOLVED_TUNNEL_URL = resolveTunnelUrl(SSEWEBSOCKETPROXYURL); |
| 30 | + |
| 31 | +function normalizeRequest(input: RequestInfo | URL, init?: RequestInit): NormalizedRequest | null { |
| 32 | + let url: string; |
| 33 | + |
| 34 | + if (typeof input === 'string') { |
| 35 | + url = input; |
| 36 | + } else if (input instanceof URL) { |
| 37 | + url = input.toString(); |
| 38 | + } else { |
| 39 | + url = input.url; |
| 40 | + } |
| 41 | + |
| 42 | + const method = (init?.method || (input instanceof Request ? input.method : 'GET')).toUpperCase(); |
| 43 | + const headers = init?.headers ?? (input instanceof Request ? input.headers : undefined); |
| 44 | + const signal = init?.signal ?? (input instanceof Request ? input.signal : undefined); |
| 45 | + |
| 46 | + let body: string | undefined; |
| 47 | + |
| 48 | + if (init?.body !== undefined) { |
| 49 | + if (typeof init.body === 'string') { |
| 50 | + body = init.body; |
| 51 | + } else { |
| 52 | + return null; |
| 53 | + } |
| 54 | + } else if (!(input instanceof Request)) { |
| 55 | + body = undefined; |
| 56 | + } else { |
| 57 | + // We cannot synchronously read Request bodies; skip interception. |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + if (body === undefined) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + return { url, method, headers, body, signal }; |
| 66 | +} |
| 67 | + |
| 68 | +function shouldUseTunnel(request: NormalizedRequest, tunnelUrl: string | null): boolean { |
| 69 | + if (!tunnelUrl) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + if (request.method !== 'POST') { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + let payload: unknown; |
| 78 | + try { |
| 79 | + payload = JSON.parse(request.body); |
| 80 | + } catch { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + const stream = |
| 85 | + typeof payload === 'object' && |
| 86 | + payload !== null && |
| 87 | + (payload as Record<string, unknown>)['stream']; |
| 88 | + if (stream !== true) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + try { |
| 93 | + const resolvedUrl = new URL(request.url, window.location.href); |
| 94 | + if (!resolvedUrl.pathname.endsWith('/v1/chat/completions')) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + } catch { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | +async function forwardThroughTunnel( |
| 105 | + request: NormalizedRequest, |
| 106 | + tunnelUrl: string |
| 107 | +): Promise<Response> { |
| 108 | + const resolvedTarget = new URL(request.url, window.location.href).toString(); |
| 109 | + |
| 110 | + return createSSETunnelResponse({ |
| 111 | + socketUrl: tunnelUrl, |
| 112 | + targetUrl: resolvedTarget, |
| 113 | + method: request.method, |
| 114 | + headers: request.headers, |
| 115 | + body: request.body, |
| 116 | + abortSignal: request.signal |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +function patchFetch(): void { |
| 121 | + if (!browser) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (globalState.__chatTunnelPatched) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + const originalFetch = globalThis.fetch.bind(globalThis); |
| 130 | + globalState.__chatTunnelOriginalFetch = originalFetch; |
| 131 | + |
| 132 | + globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => { |
| 133 | + const tunnelUrl = RESOLVED_TUNNEL_URL; |
| 134 | + |
| 135 | + if (!tunnelUrl) { |
| 136 | + return originalFetch(input as RequestInfo, init); |
| 137 | + } |
| 138 | + |
| 139 | + const normalized = normalizeRequest(input, init); |
| 140 | + if (!normalized || !shouldUseTunnel(normalized, tunnelUrl)) { |
| 141 | + return originalFetch(input as RequestInfo, init); |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + return await forwardThroughTunnel(normalized, tunnelUrl); |
| 146 | + } catch (error) { |
| 147 | + console.warn('WebSocket tunnel failed, falling back to direct fetch:', error); |
| 148 | + return originalFetch(input as RequestInfo, init); |
| 149 | + } |
| 150 | + }; |
| 151 | + |
| 152 | + globalState.__chatTunnelPatched = true; |
| 153 | +} |
| 154 | + |
| 155 | +patchFetch(); |
0 commit comments