Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

10.6 KB
Raw Blame
import { docsLanguageEntry } from './language-entry.js'
import { createLanguagePreference } from './language-preference.js'
import { h, defineAsyncComponent, nextTick } from 'vue'
import DefaultTheme, { VPButton } from 'vitepress/theme-without-fonts'
import DocBreadcrumb from './components/DocBreadcrumb.vue'
import AgentPrompt from './components/AgentPrompt.vue'
import ArchitectureDiagram from './components/ArchitectureDiagram.vue'
import IngestionPipelineDiagram from './components/IngestionPipelineDiagram.vue'
import MemoryExtractionDiagram from './components/MemoryExtractionDiagram.vue'
import PathLockDiagram from './components/PathLockDiagram.vue'
import StorageLayersDiagram from './components/StorageLayersDiagram.vue'
import VikingBotOverviewDiagram from './components/VikingBotOverviewDiagram.vue'
import SidebarFooter from './components/SidebarFooter.vue'
import NavScreenFooter from './components/NavScreenFooter.vue'
import ThemeToggle from './components/ThemeToggle.vue'
import LocaleSwitch from './components/LocaleSwitch.vue'
import { withBase } from 'vitepress'
import type { EnhanceAppContext } from 'vitepress'
import CopyMarkdownButton from './CopyMarkdownButton.vue'
import OpenVikingSearch from './OpenVikingSearch.vue'
import ApiExampleTabsEnhancer from './ApiExampleTabsEnhancer.vue'
import { initVikingBotWidget, syncVikingBotLocale } from './vikingbot-widget'
import { trackEvent, trackPageView } from './track'
import { startDocsAttribution } from './attribution'
import './custom.css'
import './reading.css'
import './header.css'

type OpenVikingPreference = {
  theme?: 'light' | 'dark'
}

const PREFERENCE_SESSION_KEY = 'openviking-preferences'
const PREFERENCE_COOKIE_KEY = 'openviking-preferences'
const PREFERENCE_TRANSFER_PREFIX = 'openviking-preferences:'
const VITEPRESS_THEME_KEY = 'vitepress-theme-appearance'
const LOCAL_PLAYGROUND_URL = 'http://localhost:8080/'
const MAIN_SITE_HOSTS = new Set([
  'www.openviking.ai',
  'openviking.ai',
  'www.openviking.net',
  'openviking.net',
  'localhost:8080',
  '127.0.0.1:8080'
])

function normalizeTheme(value: string | null): OpenVikingPreference['theme'] {
  if (value === 'light' || value === 'dark') return value
  return undefined
}

function readStoredPreference(): OpenVikingPreference {
  try {
    const rawPreference = sessionStorage.getItem(PREFERENCE_SESSION_KEY)
    if (!rawPreference) return {}
    const preference = JSON.parse(rawPreference) as OpenVikingPreference
    return {
      theme: normalizeTheme(preference.theme ?? null)
    }
  } catch {
    return {}
  }
}

function cookieDomain() {
  const hostname = window.location.hostname
  if (hostname === 'localhost' || hostname === '127.0.0.1') return ''
  if (hostname.endsWith('.openviking.ai') || hostname === 'openviking.ai') {
    return 'Domain=.openviking.ai'
  }
  if (hostname.endsWith('.openviking.net') || hostname === 'openviking.net') {
    return 'Domain=.openviking.net'
  }
  return ''
}

function readCookiePreference(): OpenVikingPreference {
  let cookies = ''
  try { cookies = document.cookie } catch { return {} }
  const cookie = cookies
    .split('; ')
    .find((item) => item.startsWith(`${PREFERENCE_COOKIE_KEY}=`))

  if (!cookie) return {}

  try {
    const rawPreference = decodeURIComponent(cookie.slice(PREFERENCE_COOKIE_KEY.length + 1))
    const preference = JSON.parse(rawPreference) as OpenVikingPreference
    return {
      theme: normalizeTheme(preference.theme ?? null)
    }
  } catch {
    return {}
  }
}

function writeCookiePreference(preference: OpenVikingPreference) {
  const nextPreference = mergePreferences(readCookiePreference(), preference)
  document.cookie = [
    `${PREFERENCE_COOKIE_KEY}=${encodeURIComponent(JSON.stringify(nextPreference))}`,
    'Path=/',
    'Max-Age=31536000',
    'SameSite=Lax',
    cookieDomain()
  ]
    .filter(Boolean)
    .join('; ')
}

function mergePreferences(
  base: OpenVikingPreference,
  incoming: OpenVikingPreference
): OpenVikingPreference {
  return {
    theme: incoming.theme ?? base.theme
  }
}

function writeStoredPreference(preference: OpenVikingPreference) {
  const nextPreference = mergePreferences(readStoredPreference(), preference)
  try { sessionStorage.setItem(PREFERENCE_SESSION_KEY, JSON.stringify(nextPreference)) } catch { /* Optional theme persistence. */ }
  try { writeCookiePreference(nextPreference) } catch { /* Optional theme persistence. */ }
}

function readTransferredPreference(): OpenVikingPreference {
  if (typeof window === 'undefined') return {}
  if (!window.name.startsWith(PREFERENCE_TRANSFER_PREFIX)) return {}

  try {
    const preference = JSON.parse(
      window.name.slice(PREFERENCE_TRANSFER_PREFIX.length)
    ) as OpenVikingPreference
    return {
      theme: normalizeTheme(preference.theme ?? null)
    }
  } catch {
    return {}
  }
}

function writeTransferredPreference(preference: OpenVikingPreference) {
  if (!preference.theme) return

  window.name = `${PREFERENCE_TRANSFER_PREFIX}${JSON.stringify(preference)}`
}

function readPersistedPreference() {
  return mergePreferences(
    mergePreferences(readStoredPreference(), readCookiePreference()),
    readTransferredPreference()
  )
}

function applyPreference(preference: OpenVikingPreference) {
  const normalizedPreference: OpenVikingPreference = {
    theme: normalizeTheme(preference.theme ?? null)
  }

  if (normalizedPreference.theme) {
    writeStoredPreference(normalizedPreference)
    writeTransferredPreference(mergePreferences(readStoredPreference(), normalizedPreference))
  }

  if (normalizedPreference.theme) {
    try { localStorage.setItem(VITEPRESS_THEME_KEY, normalizedPreference.theme) } catch { /* Still apply the theme. */ }
    document.documentElement.classList.toggle('dark', normalizedPreference.theme === 'dark')
  }
}

function syncPreferenceFromPeerSite() {
  if (typeof window === 'undefined') return

  applyPreference(readPersistedPreference())
}

function currentDocsTheme(): OpenVikingPreference['theme'] {
  return document.documentElement.classList.contains('dark') ? 'dark' : 'light'
}

function syncCurrentDocsPreference() {
  const preference = mergePreferences(readPersistedPreference(), {
    theme: currentDocsTheme()
  })
  writeStoredPreference(preference)
  writeTransferredPreference(preference)
}

function watchThemePreference() {
  const observer = new MutationObserver(syncCurrentDocsPreference)
  observer.observe(document.documentElement, {
    attributes: true,
    attributeFilter: ['class']
  })
}

function mainSiteUrlWithPreference(href: string, useLocalPreview = true) {
  const url = new URL(href, window.location.href)
  const isLocalDocs = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
  const isMainSite = MAIN_SITE_HOSTS.has(url.host)

  if (!isMainSite) return undefined

  if (
    useLocalPreview && isLocalDocs &&
    ['www.openviking.ai', 'openviking.ai', 'www.openviking.net', 'openviking.net'].includes(
      url.hostname
    )
  ) {
    const localUrl = new URL(LOCAL_PLAYGROUND_URL)
    localUrl.pathname = url.pathname
    localUrl.search = url.search
    localUrl.hash = url.hash
    return localUrl
  }

  return url
}

function syncPreferenceToMainSiteLinks() {
  document.addEventListener(
    'click',
    (event) => {
      const link = event.target instanceof Element ? event.target.closest('a') : null
      if (!link) return

      const url = mainSiteUrlWithPreference(link.href, !link.closest('.ov-site-switcher'))
      if (!url) return

      const preference = mergePreferences(readPersistedPreference(), {
        theme: currentDocsTheme()
      })
      writeStoredPreference(preference)
      writeTransferredPreference(preference)

      link.href = url.toString()
    },
    true
  )
}

function startPreferenceSync() {
  syncPreferenceFromPeerSite()
  syncCurrentDocsPreference()
  watchThemePreference()
  syncPreferenceToMainSiteLinks()
  window.addEventListener('pageshow', syncPreferenceFromPeerSite)
}

syncPreferenceFromPeerSite()

if (typeof window !== 'undefined') {
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', startPreferenceSync, { once: true })
  } else {
    startPreferenceSync()
  }
}

export default {
  extends: DefaultTheme,
  Layout() {
    return h(DefaultTheme.Layout, null, {
      'doc-before': () => [h(DocBreadcrumb), h('div', { class: 'doc-page-actions' }, [
        h(CopyMarkdownButton)
      ])],
      'sidebar-nav-after': () => h(SidebarFooter),
      'doc-after': () => h(ApiExampleTabsEnhancer),
      'nav-bar-content-after': () => [h(OpenVikingSearch), h(LocaleSwitch), h(ThemeToggle)],
      'nav-screen-content-after': () => h(NavScreenFooter)
    })
  },
  enhanceApp({ app, router }: EnhanceAppContext) {
    app.component('VPButton', VPButton)
    app.component('AgentPrompt', AgentPrompt)
    app.component('ArchitectureDiagram', ArchitectureDiagram)
    app.component('IngestionPipelineDiagram', IngestionPipelineDiagram)
    app.component('MemoryExtractionDiagram', MemoryExtractionDiagram)
    app.component('PathLockDiagram', PathLockDiagram)
    app.component('StorageLayersDiagram', StorageLayersDiagram)
    app.component('VikingBotOverviewDiagram', VikingBotOverviewDiagram)
    app.component('DocsHome', defineAsyncComponent(() => import('./components/DocsHome.vue')))
    if (import.meta.env.SSR || typeof window === 'undefined') return

    const attribution = startDocsAttribution(withBase('/'))
    import.meta.hot?.dispose(attribution.dispose)
    const policy = createLanguagePreference()
    const previousBeforeHook = router.onBeforeRouteChange
    router.onBeforeRouteChange = async (to: string) => {
      if (await previousBeforeHook?.(to) === false) return false
      const languageTarget = docsLanguageEntry(new URL(to, location.href).href, withBase('/'), policy)
      const target = attribution.routeTarget(languageTarget ?? to) ?? languageTarget
      if (target) {
        void router.go(target)
        return false
      }
    }
    trackPageView(window.location.pathname)
    document.addEventListener('click', (event) => {
      const block = (event.target as Element).closest?.('.vp-doc div[class*="language-"] > button.copy')?.parentElement
      if (!block) return
      const language = [...block.classList].find(name => name.startsWith('language-'))?.slice('language-'.length) ?? ''
      trackEvent('copy-code', { language })
    })
    initVikingBotWidget()

    const previousHook = router.onAfterRouteChanged
    router.onAfterRouteChanged = (to: string) => {
      previousHook?.(to)
      trackPageView(to.split('?')[0].split('#')[0])
      // Update the existing widget after VitePress applies the page language.
      syncVikingBotLocale()
      void nextTick().then(attribution.refresh)
    }
  }
}