MCPcopy Create free account
hub / github.com/OpenHands/OpenHands / makeConfigInjectionScript

Function makeConfigInjectionScript

scripts/static-server.mjs:382–452  ·  view source on GitHub ↗
(
  sessionApiKey,
  authRequired,
  runtimeServicesInfo,
  lockToCloud,
  basePath,
  vscodeBasePath,
  disableTelemetry,
)

Source from the content-addressed store, hash-verified

380}
381
382function makeConfigInjectionScript(
383 sessionApiKey,
384 authRequired,
385 runtimeServicesInfo,
386 lockToCloud,
387 basePath,
388 vscodeBasePath,
389 disableTelemetry,
390) {
391 const parts = [];
392
393 if (sessionApiKey) {
394 const keyLiteral = serializeForInlineScript(sessionApiKey);
395 // Window global — read at module init by getBakedSessionApiKey().
396 // Set first so it's available even if the localStorage write throws.
397 parts.push(`window.__AGENT_CANVAS_SESSION_API_KEY__=${keyLiteral};`);
398 // Always overwrite when the stored key differs from the runtime key.
399 // A previous session may have persisted a now-stale key; the runtime
400 // value (from --session-api-key) is the server's truth.
401 parts.push(
402 `try{` +
403 `var _k='openhands-agent-server-config',` +
404 `_c=JSON.parse(localStorage.getItem(_k)||'{}');` +
405 `if(_c.sessionApiKey!==${keyLiteral}){` +
406 `_c.sessionApiKey=${keyLiteral};` +
407 `localStorage.setItem(_k,JSON.stringify(_c));` +
408 `}` +
409 `}catch(e){}`,
410 );
411 }
412
413 if (authRequired) {
414 parts.push(`window.__AGENT_CANVAS_AUTH_REQUIRED__=true;`);
415 }
416
417 if (runtimeServicesInfo) {
418 // Stored as the raw JSON string so the browser-side parser
419 // (parseRuntimeServicesInfo) can JSON.parse it exactly like the
420 // VITE_RUNTIME_SERVICES_INFO env var. JSON.stringify produces a safe JS
421 // string literal for the inline <script>.
422 parts.push(
423 `window.__AGENT_CANVAS_RUNTIME_SERVICES_INFO__=${serializeForInlineScript(runtimeServicesInfo)};`,
424 );
425 }
426
427 if (lockToCloud) {
428 parts.push(
429 `window.__AGENT_CANVAS_LOCK_TO_CLOUD__=${serializeForInlineScript(lockToCloud)};`,
430 );
431 }
432
433 if (basePath && basePath !== "/") {
434 parts.push(
435 `window.__AGENT_CANVAS_BASE_PATH__=${serializeForInlineScript(basePath)};`,
436 );
437 }
438
439 if (vscodeBasePath) {

Callers 1

serveInjectedIndexHtmlFunction · 0.85

Calls 1

serializeForInlineScriptFunction · 0.85

Tested by

no test coverage detected