|
| 1 | +(function(){ |
| 2 | +// Shared helper to determine the backend base URL used by the frontend. |
| 3 | +// Exposes a default export and also attaches to window.AIDefaultBackendBase for |
| 4 | +// non-module consumers in the minimal build. |
| 5 | +getSharedApiKey; |
| 6 | +defaultBackendBase; |
| 7 | +const PLUGIN_NAME = 'AIOverhaul'; |
| 8 | +// Local default to keep the UI functional before plugin config loads. |
| 9 | +const DEFAULT_BACKEND_BASE = 'http://localhost:4153'; |
| 10 | +const CONFIG_QUERY = `query AIOverhaulPluginConfig($ids: [ID!]) { |
| 11 | + configuration { |
| 12 | + plugins(include: $ids) |
| 13 | + } |
| 14 | +}`; |
| 15 | +const SHARED_KEY_EVENT = 'AISharedApiKeyUpdated'; |
| 16 | +const SHARED_KEY_HEADER = 'x-ai-api-key'; |
| 17 | +const SHARED_KEY_QUERY = 'api_key'; |
| 18 | +const SHARED_KEY_STORAGE = 'ai_shared_api_key'; |
| 19 | +let configLoaded = false; |
| 20 | +let configLoading = false; |
| 21 | +let sharedApiKeyValue = ''; |
| 22 | +function getOrigin() { |
| 23 | + try { |
| 24 | + if (typeof location !== 'undefined' && location.origin) { |
| 25 | + return location.origin.replace(/\/$/, ''); |
| 26 | + } |
| 27 | + } |
| 28 | + catch { } |
| 29 | + return ''; |
| 30 | +} |
| 31 | +function normalizeBase(raw) { |
| 32 | + if (typeof raw !== 'string') |
| 33 | + return null; |
| 34 | + const trimmed = raw.trim(); |
| 35 | + if (!trimmed) |
| 36 | + return ''; |
| 37 | + const cleaned = trimmed.replace(/\/$/, ''); |
| 38 | + const origin = getOrigin(); |
| 39 | + if (origin && cleaned === origin) { |
| 40 | + return ''; |
| 41 | + } |
| 42 | + return cleaned; |
| 43 | +} |
| 44 | +function interpretBool(raw) { |
| 45 | + if (typeof raw === 'boolean') |
| 46 | + return raw; |
| 47 | + if (typeof raw === 'number') |
| 48 | + return raw !== 0; |
| 49 | + if (typeof raw === 'string') { |
| 50 | + const lowered = raw.trim().toLowerCase(); |
| 51 | + if (!lowered) |
| 52 | + return false; |
| 53 | + if (['1', 'true', 'yes', 'on'].includes(lowered)) |
| 54 | + return true; |
| 55 | + if (['0', 'false', 'no', 'off'].includes(lowered)) |
| 56 | + return false; |
| 57 | + } |
| 58 | + return null; |
| 59 | +} |
| 60 | +function normalizeSharedKey(raw) { |
| 61 | + if (typeof raw !== 'string') |
| 62 | + return ''; |
| 63 | + return raw.trim(); |
| 64 | +} |
| 65 | +function setSharedApiKey(raw) { |
| 66 | + const normalized = normalizeSharedKey(raw); |
| 67 | + if (normalized === sharedApiKeyValue) |
| 68 | + return; |
| 69 | + sharedApiKeyValue = normalized; |
| 70 | + try { |
| 71 | + if (normalized) { |
| 72 | + try { |
| 73 | + sessionStorage.setItem(SHARED_KEY_STORAGE, normalized); |
| 74 | + } |
| 75 | + catch { } |
| 76 | + } |
| 77 | + else { |
| 78 | + try { |
| 79 | + sessionStorage.removeItem(SHARED_KEY_STORAGE); |
| 80 | + } |
| 81 | + catch { } |
| 82 | + } |
| 83 | + window.AI_SHARED_API_KEY = normalized; |
| 84 | + window.dispatchEvent(new CustomEvent(SHARED_KEY_EVENT, { detail: normalized })); |
| 85 | + } |
| 86 | + catch { } |
| 87 | +} |
| 88 | +function getSharedApiKey() { |
| 89 | + if (sharedApiKeyValue) |
| 90 | + return sharedApiKeyValue; |
| 91 | + try { |
| 92 | + const stored = sessionStorage.getItem(SHARED_KEY_STORAGE); |
| 93 | + if (typeof stored === 'string' && stored.trim()) { |
| 94 | + sharedApiKeyValue = stored.trim(); |
| 95 | + return sharedApiKeyValue; |
| 96 | + } |
| 97 | + } |
| 98 | + catch { } |
| 99 | + try { |
| 100 | + const globalValue = window.AI_SHARED_API_KEY; |
| 101 | + if (typeof globalValue === 'string') { |
| 102 | + sharedApiKeyValue = globalValue.trim(); |
| 103 | + return sharedApiKeyValue; |
| 104 | + } |
| 105 | + } |
| 106 | + catch { } |
| 107 | + return ''; |
| 108 | +} |
| 109 | +function withSharedKeyHeaders(init) { |
| 110 | + const key = getSharedApiKey(); |
| 111 | + if (!key) |
| 112 | + return init ? init : {}; |
| 113 | + const next = { ...(init || {}) }; |
| 114 | + const headers = new Headers((init === null || init === void 0 ? void 0 : init.headers) || {}); |
| 115 | + headers.set(SHARED_KEY_HEADER, key); |
| 116 | + next.headers = headers; |
| 117 | + return next; |
| 118 | +} |
| 119 | +function appendSharedApiKeyQuery(url) { |
| 120 | + const key = getSharedApiKey(); |
| 121 | + if (!key) |
| 122 | + return url; |
| 123 | + try { |
| 124 | + const base = getOrigin() || undefined; |
| 125 | + const resolved = new URL(url, url.startsWith('http://') || url.startsWith('https://') || url.startsWith('ws://') || url.startsWith('wss://') ? undefined : base); |
| 126 | + resolved.searchParams.set(SHARED_KEY_QUERY, key); |
| 127 | + return resolved.toString(); |
| 128 | + } |
| 129 | + catch { |
| 130 | + const sep = url.includes('?') ? '&' : '?'; |
| 131 | + return `${url}${sep}${SHARED_KEY_QUERY}=${encodeURIComponent(key)}`; |
| 132 | + } |
| 133 | +} |
| 134 | +function applyPluginConfig(base, captureEvents, sharedKey) { |
| 135 | + if (base !== undefined) { |
| 136 | + const normalized = normalizeBase(base); |
| 137 | + if (normalized !== null) { |
| 138 | + const value = normalized || ''; |
| 139 | + try { |
| 140 | + window.AI_BACKEND_URL = value; |
| 141 | + window.dispatchEvent(new CustomEvent('AIBackendBaseUpdated', { detail: value })); |
| 142 | + } |
| 143 | + catch { } |
| 144 | + } |
| 145 | + } |
| 146 | + if (captureEvents !== undefined && captureEvents !== null) { |
| 147 | + const normalized = !!captureEvents; |
| 148 | + try { |
| 149 | + window.__AI_INTERACTIONS_ENABLED__ = normalized; |
| 150 | + } |
| 151 | + catch { } |
| 152 | + try { |
| 153 | + const tracker = window.stashAIInteractionTracker; |
| 154 | + if (tracker) { |
| 155 | + if (typeof tracker.setEnabled === 'function') |
| 156 | + tracker.setEnabled(normalized); |
| 157 | + else if (typeof tracker.configure === 'function') |
| 158 | + tracker.configure({ enabled: normalized }); |
| 159 | + } |
| 160 | + } |
| 161 | + catch { } |
| 162 | + } |
| 163 | + if (sharedKey !== undefined) { |
| 164 | + setSharedApiKey(sharedKey); |
| 165 | + } |
| 166 | +} |
| 167 | +async function loadPluginConfig() { |
| 168 | + var _a, _b, _c, _d, _e, _f, _g, _h; |
| 169 | + if (configLoaded || configLoading) |
| 170 | + return; |
| 171 | + configLoading = true; |
| 172 | + try { |
| 173 | + const resp = await fetch('/graphql', { |
| 174 | + method: 'POST', |
| 175 | + headers: { 'content-type': 'application/json' }, |
| 176 | + credentials: 'same-origin', |
| 177 | + body: JSON.stringify({ query: CONFIG_QUERY, variables: { ids: [PLUGIN_NAME] } }), |
| 178 | + }); |
| 179 | + if (!resp.ok) |
| 180 | + return; |
| 181 | + const payload = await resp.json().catch(() => null); |
| 182 | + const plugins = (_b = (_a = payload === null || payload === void 0 ? void 0 : payload.data) === null || _a === void 0 ? void 0 : _a.configuration) === null || _b === void 0 ? void 0 : _b.plugins; |
| 183 | + if (plugins && typeof plugins === 'object') { |
| 184 | + const entry = plugins[PLUGIN_NAME]; |
| 185 | + if (entry && typeof entry === 'object') { |
| 186 | + const backendBase = (_d = (_c = entry.backend_base_url) !== null && _c !== void 0 ? _c : entry.backendBaseUrl) !== null && _d !== void 0 ? _d : entry.backendBaseURL; |
| 187 | + const captureEvents = (_f = (_e = entry.capture_events) !== null && _e !== void 0 ? _e : entry.captureEvents) !== null && _f !== void 0 ? _f : entry.captureEventsEnabled; |
| 188 | + const sharedKey = (_h = (_g = entry.shared_api_key) !== null && _g !== void 0 ? _g : entry.sharedApiKey) !== null && _h !== void 0 ? _h : entry.sharedKey; |
| 189 | + applyPluginConfig(backendBase, interpretBool(captureEvents), typeof sharedKey === 'string' ? sharedKey : undefined); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + catch { } |
| 194 | + finally { |
| 195 | + configLoaded = true; |
| 196 | + configLoading = false; |
| 197 | + } |
| 198 | +} |
| 199 | +function defaultBackendBase() { |
| 200 | + try { |
| 201 | + if (!configLoaded) |
| 202 | + loadPluginConfig(); |
| 203 | + } |
| 204 | + catch { } |
| 205 | + if (typeof window.AI_BACKEND_URL === 'string') { |
| 206 | + const explicit = normalizeBase(window.AI_BACKEND_URL); |
| 207 | + if (explicit !== null && explicit !== undefined) { |
| 208 | + return explicit; |
| 209 | + } |
| 210 | + return ''; |
| 211 | + } |
| 212 | + return DEFAULT_BACKEND_BASE; |
| 213 | +} |
| 214 | +// Also attach as a global so files that are executed before this module can still |
| 215 | +// use the shared function when available. |
| 216 | +try { |
| 217 | + window.AIDefaultBackendBase = defaultBackendBase; |
| 218 | + defaultBackendBase.loadPluginConfig = loadPluginConfig; |
| 219 | + defaultBackendBase.applyPluginConfig = applyPluginConfig; |
| 220 | + window.AISharedApiKeyHelper = { |
| 221 | + get: getSharedApiKey, |
| 222 | + withHeaders: withSharedKeyHeaders, |
| 223 | + appendQuery: appendSharedApiKeyQuery, |
| 224 | + }; |
| 225 | +} |
| 226 | +catch { } |
| 227 | +})(); |
| 228 | + |
0 commit comments