backend: finalize phase updates, migrations, and integration wiring

This commit is contained in:
translator committed 2026-03-14 11:17:58 +09:00
1 parent 51d277f502
commit e5ca71f7c4
23 files changed
+2637 -87

No files matched your search

+50 -1
View File
@@ -19,10 +19,59 @@ import type {
const BASE = '/api'
const ADMIN_TOKEN_KEY = 'stemengine-admin-token'
export function getAdminToken(): string {
if (typeof window === 'undefined') return ''
return localStorage.getItem(ADMIN_TOKEN_KEY)?.trim() || ''
}
export function setAdminToken(token: string): void {
if (typeof window === 'undefined') return
const trimmed = token.trim()
if (!trimmed) {
localStorage.removeItem(ADMIN_TOKEN_KEY)
return
}
localStorage.setItem(ADMIN_TOKEN_KEY, trimmed)
}
export function clearAdminToken(): void {
if (typeof window === 'undefined') return
localStorage.removeItem(ADMIN_TOKEN_KEY)
}
function requiresAdminToken(url: string, method?: string): boolean {
const verb = (method ?? 'GET').toUpperCase()
// Normalize absolute URLs to pathnames; keep relative paths as-is.
let path = url
try {
path = new URL(url, window.location.origin).pathname
} catch {
// keep original string
}
if (path === `${BASE}/system/gpu-reset`) return true
if (path.startsWith(`${BASE}/projects/cleanup/`)) return true
if (path.startsWith(`${BASE}/models/`) && verb !== 'GET') return true
return false
}
async function request<T>(url: string, init?: RequestInit): Promise<T> {
const res = await fetch(url, init)
const headers = new Headers(init?.headers)
if (requiresAdminToken(url, init?.method)) {
const token = getAdminToken()
if (token) headers.set('X-Admin-Token', token)
}
const res = await fetch(url, { ...init, headers })
if (!res.ok) {
const text = await res.text()
if (res.status === 403 && requiresAdminToken(url, init?.method)) {
const detail = text || 'Forbidden'
throw new Error(`403: ${detail} (admin token required — set it in Settings → Integrations)`)
}
throw new Error(`${res.status}: ${text}`)
}
return res.json() as Promise<T>