diff --git a/.gitignore b/.gitignore index f6699de..53d18b1 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,4 @@ computr/frontend/.svelte-kit/ .DS_Store Thumbs.db -# uv -uv.lock - -.cptr \ No newline at end of file +.cptr diff --git a/cptr/app.py b/cptr/app.py index c4b06db..f47a414 100644 --- a/cptr/app.py +++ b/cptr/app.py @@ -28,11 +28,14 @@ app = FastAPI() async def startup(): await init_db() from cptr.env import STARTUP_TOKEN + app.state.startup_token = STARTUP_TOKEN # Reconcile stuck chat state from prior crash/restart from cptr.env import ENABLE_CHAT_RECONCILE_ON_STARTUP + if ENABLE_CHAT_RECONCILE_ON_STARTUP: from cptr.utils.chat_task import reconcile_chat_state + await reconcile_chat_state() @@ -41,7 +44,13 @@ async def startup(): async def auth_middleware(request: Request, call_next): path = request.url.path # Skip auth for: auth endpoints, health, static assets, HTML pages - if path.startswith("/api/auth") or path == "/api/health" or path == "/api/config" or path == "/api/changelog" or path == "/manifest.json": + if ( + path.startswith("/api/auth") + or path == "/api/health" + or path == "/api/config" + or path == "/api/changelog" + or path == "/manifest.json" + ): return await call_next(request) if path.startswith("/_app/") or not path.startswith("/api/"): return await call_next(request) @@ -73,20 +82,21 @@ async def auth_middleware(request: Request, call_next): # Added after auth middleware so it wraps outermost (runs before auth), # since proxied sub-resources don't need cptr auth. from cptr.utils.proxy_middleware import ProxyFallbackMiddleware + app.add_middleware(ProxyFallbackMiddleware) # Path normalization middleware (Windows: \ → / in JSON responses) import platform + if platform.system() == "Windows": import json as _json @app.middleware("http") async def normalize_paths_middleware(request: Request, call_next): response = await call_next(request) - if ( - response.headers.get("content-type", "").startswith("application/json") - and hasattr(response, "body") + if response.headers.get("content-type", "").startswith("application/json") and hasattr( + response, "body" ): try: body = b"" @@ -99,6 +109,7 @@ if platform.system() == "Windows": _normalize_obj(data) new_body = _json.dumps(data, ensure_ascii=False).encode("utf-8") from starlette.responses import Response as StarletteResponse + return StarletteResponse( content=new_body, status_code=response.status_code, @@ -148,7 +159,7 @@ async def get_config(): version = "dev" return { - "auth_mode": mode.value if hasattr(mode, 'value') else str(mode), + "auth_mode": mode.value if hasattr(mode, "value") else str(mode), "needs_setup": needs_setup, "signup_enabled": signup_enabled, "version": version, @@ -172,6 +183,7 @@ app.include_router(workspace_router) @app.get("/api/health") async def health(): import os + return {"status": "ok", "uptime_seconds": int(time.time() - START_TIME), "pid": os.getpid()} @@ -179,6 +191,7 @@ async def health(): async def get_changelog(): """Return parsed CHANGELOG.md as structured JSON (max 5 versions).""" from cptr.utils.changelog import CHANGELOG + return {key: CHANGELOG[key] for idx, key in enumerate(CHANGELOG) if idx < 5} @@ -212,7 +225,12 @@ async def pwa_manifest(): "icons": [ {"src": "/icon-192.png", "sizes": "192x192", "type": "image/png"}, {"src": "/icon-512.png", "sizes": "512x512", "type": "image/png"}, - {"src": "/icon-512.png", "sizes": "512x512", "type": "image/png", "purpose": "maskable"}, + { + "src": "/icon-512.png", + "sizes": "512x512", + "type": "image/png", + "purpose": "maskable", + }, ], "categories": ["developer", "productivity", "utilities"], } @@ -221,7 +239,9 @@ async def pwa_manifest(): # Frontend (unchanged) FRONTEND_BUILD_DIR = Path(__file__).parent / "frontend" / "build" if FRONTEND_BUILD_DIR.exists(): - app.mount("/_app", StaticFiles(directory=str(FRONTEND_BUILD_DIR / "_app")), name="frontend-assets") + app.mount( + "/_app", StaticFiles(directory=str(FRONTEND_BUILD_DIR / "_app")), name="frontend-assets" + ) @app.get("/{full_path:path}") async def serve_spa(request: Request, full_path: str): diff --git a/cptr/cli.py b/cptr/cli.py index 3065a53..871f5d7 100644 --- a/cptr/cli.py +++ b/cptr/cli.py @@ -16,7 +16,8 @@ def cli(): def run(host: str, port: int, reload: bool, headless: bool): """Start the cptr server.""" import os, secrets - display_host = 'localhost' if host == '0.0.0.0' else host + + display_host = "localhost" if host == "0.0.0.0" else host token = secrets.token_hex(32) os.environ["CPTR_STARTUP_TOKEN"] = token @@ -25,6 +26,7 @@ def run(host: str, port: int, reload: bool, headless: bool): print(f"\n ➜ {url}\n") if not headless: import threading, webbrowser + threading.Timer(1.5, lambda: webbrowser.open(url)).start() uvicorn.run( "cptr.app:application", diff --git a/cptr/frontend/.prettierignore b/cptr/frontend/.prettierignore new file mode 100644 index 0000000..c276f58 --- /dev/null +++ b/cptr/frontend/.prettierignore @@ -0,0 +1,5 @@ +build +.svelte-kit +node_modules +package-lock.json +static diff --git a/cptr/frontend/.prettierrc b/cptr/frontend/.prettierrc new file mode 100644 index 0000000..3f7802c --- /dev/null +++ b/cptr/frontend/.prettierrc @@ -0,0 +1,15 @@ +{ + "useTabs": true, + "singleQuote": true, + "trailingComma": "none", + "printWidth": 100, + "plugins": ["prettier-plugin-svelte"], + "overrides": [ + { + "files": "*.svelte", + "options": { + "parser": "svelte" + } + } + ] +} diff --git a/cptr/frontend/package-lock.json b/cptr/frontend/package-lock.json index 4f00058..66ae7c2 100644 --- a/cptr/frontend/package-lock.json +++ b/cptr/frontend/package-lock.json @@ -1,12 +1,12 @@ { "name": "frontend", - "version": "0.0.1", + "version": "0.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "frontend", - "version": "0.0.1", + "version": "0.1.0", "dependencies": { "@codemirror/lang-cpp": "^6.0.3", "@codemirror/lang-css": "^6.3.1", @@ -78,6 +78,8 @@ "@sveltejs/kit": "^2.57.0", "@sveltejs/vite-plugin-svelte": "^7.0.0", "@types/sortablejs": "^1.15.9", + "prettier": "^3.7.4", + "prettier-plugin-svelte": "^3.4.0", "svelte": "^5.55.2", "svelte-check": "^4.4.6", "typescript": "^6.0.2", @@ -3687,6 +3689,33 @@ "node": ">=4" } }, + "node_modules/prettier": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.8.3.tgz", + "integrity": "sha512-7igPTM53cGHMW8xWuVTydi2KO233VFiTNyF5hLJqpilHfmn8C8gPf+PS7dUT64YcXFbiMGZxS9pCSxL/Dxm/Jw==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/prettier-plugin-svelte": { + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/prettier-plugin-svelte/-/prettier-plugin-svelte-3.5.2.tgz", + "integrity": "sha512-ItFouLvzSFE3ulNl4DKoWM3BGcbDCNVpIyy/Y3F2gC3aNiGLxtFUdffVqO5Z5hhYG+DFT5KULWaxmeFFpdbvaQ==", + "dev": true, + "license": "MIT", + "peerDependencies": { + "prettier": "^3.0.0", + "svelte": "^3.2.0 || ^4.0.0-next.0 || ^5.0.0-next.0" + } + }, "node_modules/process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", diff --git a/cptr/frontend/package.json b/cptr/frontend/package.json index d5596c6..95a4012 100644 --- a/cptr/frontend/package.json +++ b/cptr/frontend/package.json @@ -8,6 +8,8 @@ "build": "vite build", "preview": "vite preview", "prepare": "svelte-kit sync || echo ''", + "format": "prettier --write .", + "format:check": "prettier --check .", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch" }, @@ -17,6 +19,8 @@ "@sveltejs/kit": "^2.57.0", "@sveltejs/vite-plugin-svelte": "^7.0.0", "@types/sortablejs": "^1.15.9", + "prettier": "^3.7.4", + "prettier-plugin-svelte": "^3.4.0", "svelte": "^5.55.2", "svelte-check": "^4.4.6", "typescript": "^6.0.2", diff --git a/cptr/frontend/src/app.css b/cptr/frontend/src/app.css index aa9bb0f..807c3bd 100644 --- a/cptr/frontend/src/app.css +++ b/cptr/frontend/src/app.css @@ -1,163 +1,181 @@ -@import "tailwindcss"; +@import 'tailwindcss'; @plugin "@tailwindcss/typography"; @custom-variant dark (&:where(.dark, .dark *)); @theme { - --font-sans: "Inter", -apple-system, BlinkMacSystemFont, ui-sans-serif, system-ui, sans-serif; - --font-mono: "JetBrains Mono", "Fira Code", ui-monospace, monospace; + --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, ui-sans-serif, system-ui, sans-serif; + --font-mono: 'JetBrains Mono', 'Fira Code', ui-monospace, monospace; - /* Exact Open WebUI gray scale - pure neutral, zero chroma */ - --color-gray-50: oklch(0.98 0 0); - --color-gray-100: oklch(0.94 0 0); - --color-gray-200: oklch(0.92 0 0); - --color-gray-300: oklch(0.85 0 0); - --color-gray-400: oklch(0.77 0 0); - --color-gray-500: oklch(0.69 0 0); - --color-gray-600: oklch(0.51 0 0); - --color-gray-700: oklch(0.42 0 0); - --color-gray-800: oklch(0.32 0 0); - --color-gray-850: oklch(0.27 0 0); - --color-gray-900: oklch(0.2 0 0); - --color-gray-950: oklch(0.16 0 0); + /* Exact Open WebUI gray scale - pure neutral, zero chroma */ + --color-gray-50: oklch(0.98 0 0); + --color-gray-100: oklch(0.94 0 0); + --color-gray-200: oklch(0.92 0 0); + --color-gray-300: oklch(0.85 0 0); + --color-gray-400: oklch(0.77 0 0); + --color-gray-500: oklch(0.69 0 0); + --color-gray-600: oklch(0.51 0 0); + --color-gray-700: oklch(0.42 0 0); + --color-gray-800: oklch(0.32 0 0); + --color-gray-850: oklch(0.27 0 0); + --color-gray-900: oklch(0.2 0 0); + --color-gray-950: oklch(0.16 0 0); } @layer base { - *, - ::after, - ::before, - ::backdrop, - ::file-selector-button { - border-color: var(--color-gray-200, currentColor); - } + *, + ::after, + ::before, + ::backdrop, + ::file-selector-button { + border-color: var(--color-gray-200, currentColor); + } - html { - color-scheme: dark light; - touch-action: manipulation; - overscroll-behavior: none; - } + html { + color-scheme: dark light; + touch-action: manipulation; + overscroll-behavior: none; + } - button { - @apply cursor-pointer; - } + button { + @apply cursor-pointer; + } } /* Scrollbar - Open WebUI style */ ::-webkit-scrollbar-thumb { - --tw-border-opacity: 1; - background-color: rgba(215, 215, 215, 0.6); - border-color: rgba(255, 255, 255, var(--tw-border-opacity)); - border-radius: 9999px; - border-width: 1px; + --tw-border-opacity: 1; + background-color: rgba(215, 215, 215, 0.6); + border-color: rgba(255, 255, 255, var(--tw-border-opacity)); + border-radius: 9999px; + border-width: 1px; } /* Dark theme scrollbar styles */ .dark ::-webkit-scrollbar-thumb { - background-color: rgba(67, 67, 67, 0.6); - border-color: rgba(0, 0, 0, var(--tw-border-opacity)); + background-color: rgba(67, 67, 67, 0.6); + border-color: rgba(0, 0, 0, var(--tw-border-opacity)); } ::-webkit-scrollbar { - height: 0.45rem; - width: 0.45rem; + height: 0.45rem; + width: 0.45rem; } ::-webkit-scrollbar-track { - background-color: transparent; - border-radius: 9999px; + background-color: transparent; + border-radius: 9999px; } .scrollbar-hidden { - scrollbar-width: none; - -ms-overflow-style: none; + scrollbar-width: none; + -ms-overflow-style: none; } .scrollbar-hidden::-webkit-scrollbar { - display: none; + display: none; } /* CodeMirror - match Open WebUI */ .cm-editor { - height: 100%; - width: 100%; + height: 100%; + width: 100%; } .cm-editor.cm-focused { - outline: none; + outline: none; } .dark .cm-editor { - background-color: #000 !important; + background-color: #000 !important; } .dark .cm-gutters { - background-color: #000 !important; - border: none !important; + background-color: #000 !important; + border: none !important; } .dark .cm-activeLine { - background-color: rgba(255, 255, 255, 0.04) !important; + background-color: rgba(255, 255, 255, 0.04) !important; } .dark .cm-activeLineGutter { - background-color: rgba(255, 255, 255, 0.04) !important; + background-color: rgba(255, 255, 255, 0.04) !important; } :not(.dark) .cm-gutters { - border: none !important; + border: none !important; } .cm-scroller:active::-webkit-scrollbar-thumb, .cm-scroller:focus::-webkit-scrollbar-thumb, .cm-scroller:hover::-webkit-scrollbar-thumb { - visibility: visible; + visibility: visible; } .cm-scroller::-webkit-scrollbar-thumb { - visibility: hidden; + visibility: hidden; } .cm-scroller::-webkit-scrollbar-corner { - display: none; + display: none; } /* Shimmer animation, Open WebUI style */ @keyframes shimmer { - 0% { background-position: 100% 0; } - 100% { background-position: -100% 0; } + 0% { + background-position: 100% 0; + } + 100% { + background-position: -100% 0; + } } .shimmer { - background: linear-gradient(110deg, #b4b4b4 0%, #b4b4b4 43%, #e8e8e8 50%, #b4b4b4 57%, #b4b4b4 100%); - background-size: 200% 100%; - background-clip: text; - -webkit-background-clip: text; - -webkit-text-fill-color: transparent; - animation: shimmer 1.5s cubic-bezier(0.7, 0, 1, 0.4) infinite; - color: #b4b4b4; + background: linear-gradient( + 110deg, + #b4b4b4 0%, + #b4b4b4 43%, + #e8e8e8 50%, + #b4b4b4 57%, + #b4b4b4 100% + ); + background-size: 200% 100%; + background-clip: text; + -webkit-background-clip: text; + -webkit-text-fill-color: transparent; + animation: shimmer 1.5s cubic-bezier(0.7, 0, 1, 0.4) infinite; + color: #b4b4b4; } .dark .shimmer { - background: linear-gradient(110deg, #9a9a9a 0%, #9a9a9a 43%, #5e5e5e 50%, #9a9a9a 57%, #9a9a9a 100%); - background-size: 200% 100%; - background-clip: text; - -webkit-background-clip: text; - -webkit-text-fill-color: transparent; - animation: shimmer 1.5s cubic-bezier(0.7, 0, 1, 0.4) infinite; - color: #9a9a9a; + background: linear-gradient( + 110deg, + #9a9a9a 0%, + #9a9a9a 43%, + #5e5e5e 50%, + #9a9a9a 57%, + #9a9a9a 100% + ); + background-size: 200% 100%; + background-clip: text; + -webkit-background-clip: text; + -webkit-text-fill-color: transparent; + animation: shimmer 1.5s cubic-bezier(0.7, 0, 1, 0.4) infinite; + color: #9a9a9a; } /* Inline code (codespan), Open WebUI style */ .codespan { - padding: 0.15rem 0.3rem; - font-size: 0.85em; - @apply font-mono rounded-md text-gray-800 bg-gray-100 dark:text-gray-200 dark:bg-gray-800 mx-0.5; + padding: 0.15rem 0.3rem; + font-size: 0.85em; + @apply font-mono rounded-md text-gray-800 bg-gray-100 dark:text-gray-200 dark:bg-gray-800 mx-0.5; } /* Tippy tooltip theme */ .tippy-box[data-theme~='cptr'] { - background: #1a1a1a; - color: #e0e0e0; - font-size: 11px; - font-weight: 500; - padding: 2px 4px; - border-radius: 6px; - box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3); + background: #1a1a1a; + color: #e0e0e0; + font-size: 11px; + font-weight: 500; + padding: 2px 4px; + border-radius: 6px; + box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3); } diff --git a/cptr/frontend/src/app.html b/cptr/frontend/src/app.html index 2a05c0a..ac0a430 100644 --- a/cptr/frontend/src/app.html +++ b/cptr/frontend/src/app.html @@ -2,7 +2,10 @@
- + diff --git a/cptr/frontend/src/lib/apis/admin.ts b/cptr/frontend/src/lib/apis/admin.ts index c07b50d..86735f8 100644 --- a/cptr/frontend/src/lib/apis/admin.ts +++ b/cptr/frontend/src/lib/apis/admin.ts @@ -26,25 +26,25 @@ export const deleteUser = (userId: string) => export const updateRole = (userId: string, role: string) => fetchJSON(`/api/admin/users/${userId}/role`, { ...jsonBody({ role }), - method: 'PUT', + method: 'PUT' }); export const updateUserProfile = (userId: string, display_name: string | null) => fetchJSON(`/api/admin/users/${userId}/profile`, { ...jsonBody({ display_name }), - method: 'PUT', + method: 'PUT' }); export const resetPassword = (userId: string, password: string) => fetchJSON(`/api/admin/users/${userId}/password`, { ...jsonBody({ password }), - method: 'PUT', + method: 'PUT' }); export const updateUsername = (userId: string, username: string) => fetchJSON(`/api/admin/users/${userId}/username`, { ...jsonBody({ username }), - method: 'PUT', + method: 'PUT' }); export const getAdminConfig = async (): Promise