wyxrouter 0.4.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +222 -0
- package/LICENSE +21 -0
- package/README.md +96 -0
- package/README.zh-CN.md +1311 -0
- package/assets/pixel-router-2d.png +0 -0
- package/cli/LICENSE +42 -0
- package/cli/README.md +125 -0
- package/cli/app/package.json +58 -0
- package/cli/cli.js +806 -0
- package/cli/package.json +48 -0
- package/i18n/README.ja-JP.md +1209 -0
- package/i18n/README.ru.md +1311 -0
- package/i18n/README.vi.md +1310 -0
- package/i18n/README.zh-CN.md +1306 -0
- package/images/9router.png +0 -0
- package/jsconfig.json +12 -0
- package/next.config.mjs +71 -0
- package/open-sse/config/appConstants.js +203 -0
- package/open-sse/config/codexInstructions.js +119 -0
- package/open-sse/config/constants.js +4 -0
- package/open-sse/config/defaultThinkingSignature.js +12 -0
- package/open-sse/config/errorConfig.js +85 -0
- package/open-sse/config/googleTtsLanguages.js +62 -0
- package/open-sse/config/kiroConstants.js +322 -0
- package/open-sse/config/models.js +13 -0
- package/open-sse/config/ollamaModels.js +19 -0
- package/open-sse/config/providerModels.js +944 -0
- package/open-sse/config/providers.js +458 -0
- package/open-sse/config/runtimeConfig.js +72 -0
- package/open-sse/config/ttsModels.js +129 -0
- package/open-sse/executors/antigravity.js +504 -0
- package/open-sse/executors/azure.js +57 -0
- package/open-sse/executors/base.js +185 -0
- package/open-sse/executors/codex.js +469 -0
- package/open-sse/executors/commandcode.js +88 -0
- package/open-sse/executors/cursor.js +795 -0
- package/open-sse/executors/default.js +497 -0
- package/open-sse/executors/gemini-cli.js +89 -0
- package/open-sse/executors/github.js +379 -0
- package/open-sse/executors/grok-web.js +345 -0
- package/open-sse/executors/iflow.js +108 -0
- package/open-sse/executors/index.js +75 -0
- package/open-sse/executors/kiro.js +508 -0
- package/open-sse/executors/ollama-local.js +14 -0
- package/open-sse/executors/opencode-go.js +41 -0
- package/open-sse/executors/opencode.js +32 -0
- package/open-sse/executors/perplexity-web.js +507 -0
- package/open-sse/executors/qoder.js +450 -0
- package/open-sse/executors/qwen.js +129 -0
- package/open-sse/executors/vertex.js +131 -0
- package/open-sse/executors/xiaomi-tokenplan.js +19 -0
- package/open-sse/handlers/chatCore/nonStreamingHandler.js +230 -0
- package/open-sse/handlers/chatCore/requestDetail.js +102 -0
- package/open-sse/handlers/chatCore/sseToJsonHandler.js +231 -0
- package/open-sse/handlers/chatCore/streamingHandler.js +103 -0
- package/open-sse/handlers/chatCore.js +287 -0
- package/open-sse/handlers/embeddingProviders/_base.js +4 -0
- package/open-sse/handlers/embeddingProviders/gemini.js +54 -0
- package/open-sse/handlers/embeddingProviders/index.js +23 -0
- package/open-sse/handlers/embeddingProviders/openai.js +39 -0
- package/open-sse/handlers/embeddingProviders/openaiCompatNode.js +13 -0
- package/open-sse/handlers/embeddingsCore.js +126 -0
- package/open-sse/handlers/fetch/index.js +237 -0
- package/open-sse/handlers/imageGenerationCore.js +189 -0
- package/open-sse/handlers/imageProviders/_base.js +31 -0
- package/open-sse/handlers/imageProviders/blackForestLabs.js +43 -0
- package/open-sse/handlers/imageProviders/cloudflareAi.js +178 -0
- package/open-sse/handlers/imageProviders/codex.js +198 -0
- package/open-sse/handlers/imageProviders/comfyui.js +8 -0
- package/open-sse/handlers/imageProviders/falAi.js +41 -0
- package/open-sse/handlers/imageProviders/gemini.js +25 -0
- package/open-sse/handlers/imageProviders/huggingface.js +22 -0
- package/open-sse/handlers/imageProviders/index.js +40 -0
- package/open-sse/handlers/imageProviders/nanobanana.js +58 -0
- package/open-sse/handlers/imageProviders/openai.js +40 -0
- package/open-sse/handlers/imageProviders/runwayml.js +47 -0
- package/open-sse/handlers/imageProviders/sdwebui.js +17 -0
- package/open-sse/handlers/imageProviders/stabilityAi.js +34 -0
- package/open-sse/handlers/responsesHandler.js +103 -0
- package/open-sse/handlers/search/callers.js +371 -0
- package/open-sse/handlers/search/chatSearch.js +409 -0
- package/open-sse/handlers/search/index.js +201 -0
- package/open-sse/handlers/search/normalizers.js +223 -0
- package/open-sse/handlers/sttCore.js +194 -0
- package/open-sse/handlers/ttsCore.js +74 -0
- package/open-sse/handlers/ttsProviders/_base.js +39 -0
- package/open-sse/handlers/ttsProviders/edgeTts.js +89 -0
- package/open-sse/handlers/ttsProviders/elevenlabs.js +48 -0
- package/open-sse/handlers/ttsProviders/gemini.js +117 -0
- package/open-sse/handlers/ttsProviders/genericFormats.js +169 -0
- package/open-sse/handlers/ttsProviders/googleTts.js +54 -0
- package/open-sse/handlers/ttsProviders/index.js +50 -0
- package/open-sse/handlers/ttsProviders/localDevice.js +87 -0
- package/open-sse/handlers/ttsProviders/minimax.js +59 -0
- package/open-sse/handlers/ttsProviders/openai.js +30 -0
- package/open-sse/handlers/ttsProviders/openrouter.js +70 -0
- package/open-sse/index.js +82 -0
- package/open-sse/rtk/applyFilter.js +15 -0
- package/open-sse/rtk/autodetect.js +111 -0
- package/open-sse/rtk/caveman.js +100 -0
- package/open-sse/rtk/cavemanPrompts.js +78 -0
- package/open-sse/rtk/constants.js +55 -0
- package/open-sse/rtk/filters/buildOutput.js +127 -0
- package/open-sse/rtk/filters/dedupLog.js +44 -0
- package/open-sse/rtk/filters/find.js +49 -0
- package/open-sse/rtk/filters/gitDiff.js +92 -0
- package/open-sse/rtk/filters/gitStatus.js +117 -0
- package/open-sse/rtk/filters/grep.js +48 -0
- package/open-sse/rtk/filters/ls.js +79 -0
- package/open-sse/rtk/filters/readNumbered.js +27 -0
- package/open-sse/rtk/filters/searchList.js +52 -0
- package/open-sse/rtk/filters/smartTruncate.js +15 -0
- package/open-sse/rtk/filters/tree.js +32 -0
- package/open-sse/rtk/index.js +155 -0
- package/open-sse/rtk/registry.js +38 -0
- package/open-sse/services/accountFallback.js +238 -0
- package/open-sse/services/combo.js +198 -0
- package/open-sse/services/compact.js +71 -0
- package/open-sse/services/kiroModels.js +332 -0
- package/open-sse/services/model.js +261 -0
- package/open-sse/services/oauthCredentialManager.js +151 -0
- package/open-sse/services/projectId.js +306 -0
- package/open-sse/services/provider.js +356 -0
- package/open-sse/services/qoderModels.js +214 -0
- package/open-sse/services/tokenRefresh.js +939 -0
- package/open-sse/services/usage.js +1496 -0
- package/open-sse/transformer/responsesTransformer.js +439 -0
- package/open-sse/transformer/streamToJsonConverter.js +103 -0
- package/open-sse/translator/formats.js +36 -0
- package/open-sse/translator/helpers/claudeHelper.js +216 -0
- package/open-sse/translator/helpers/geminiHelper.js +372 -0
- package/open-sse/translator/helpers/imageHelper.js +34 -0
- package/open-sse/translator/helpers/maxTokensHelper.js +27 -0
- package/open-sse/translator/helpers/openaiHelper.js +130 -0
- package/open-sse/translator/helpers/responsesApiHelper.js +139 -0
- package/open-sse/translator/helpers/toolCallHelper.js +148 -0
- package/open-sse/translator/index.js +251 -0
- package/open-sse/translator/request/antigravity-to-openai.js +229 -0
- package/open-sse/translator/request/claude-to-openai.js +232 -0
- package/open-sse/translator/request/gemini-to-openai.js +147 -0
- package/open-sse/translator/request/openai-responses.js +318 -0
- package/open-sse/translator/request/openai-to-claude.js +401 -0
- package/open-sse/translator/request/openai-to-commandcode.js +170 -0
- package/open-sse/translator/request/openai-to-cursor.js +183 -0
- package/open-sse/translator/request/openai-to-gemini.js +470 -0
- package/open-sse/translator/request/openai-to-kiro.js +629 -0
- package/open-sse/translator/request/openai-to-kiro.old.js +278 -0
- package/open-sse/translator/request/openai-to-ollama.js +192 -0
- package/open-sse/translator/request/openai-to-vertex.js +42 -0
- package/open-sse/translator/response/claude-to-openai.js +206 -0
- package/open-sse/translator/response/commandcode-to-openai.js +197 -0
- package/open-sse/translator/response/cursor-to-openai.js +30 -0
- package/open-sse/translator/response/gemini-to-openai.js +245 -0
- package/open-sse/translator/response/kiro-to-openai.js +195 -0
- package/open-sse/translator/response/ollama-to-openai.js +152 -0
- package/open-sse/translator/response/openai-responses.js +590 -0
- package/open-sse/translator/response/openai-to-antigravity.js +122 -0
- package/open-sse/translator/response/openai-to-claude.js +266 -0
- package/open-sse/utils/bypassHandler.js +298 -0
- package/open-sse/utils/claudeCloaking.js +155 -0
- package/open-sse/utils/claudeHeaderCache.js +70 -0
- package/open-sse/utils/clientDetector.js +63 -0
- package/open-sse/utils/cursorChecksum.js +149 -0
- package/open-sse/utils/cursorProtobuf.js +904 -0
- package/open-sse/utils/debugLog.js +14 -0
- package/open-sse/utils/error.js +147 -0
- package/open-sse/utils/ollamaTransform.js +85 -0
- package/open-sse/utils/proxyFetch.js +368 -0
- package/open-sse/utils/reasoningContentInjector.js +79 -0
- package/open-sse/utils/requestLogger.js +260 -0
- package/open-sse/utils/responsesStreamHelpers.js +49 -0
- package/open-sse/utils/sessionManager.js +82 -0
- package/open-sse/utils/stream.js +462 -0
- package/open-sse/utils/streamHandler.js +250 -0
- package/open-sse/utils/streamHelpers.js +122 -0
- package/open-sse/utils/toolDeduper.js +49 -0
- package/open-sse/utils/usageTracking.js +347 -0
- package/package.json +100 -0
- package/postcss.config.mjs +12 -0
- package/public/favicon.svg +11 -0
- package/public/file.svg +1 -0
- package/public/globe.svg +1 -0
- package/public/i18n/literals/ar.json +195 -0
- package/public/i18n/literals/bn.json +195 -0
- package/public/i18n/literals/cs.json +195 -0
- package/public/i18n/literals/da.json +195 -0
- package/public/i18n/literals/de.json +195 -0
- package/public/i18n/literals/el.json +195 -0
- package/public/i18n/literals/es.json +195 -0
- package/public/i18n/literals/fi.json +195 -0
- package/public/i18n/literals/fr.json +195 -0
- package/public/i18n/literals/he.json +195 -0
- package/public/i18n/literals/hi.json +195 -0
- package/public/i18n/literals/hu.json +195 -0
- package/public/i18n/literals/id.json +195 -0
- package/public/i18n/literals/it.json +195 -0
- package/public/i18n/literals/ja.json +195 -0
- package/public/i18n/literals/ko.json +195 -0
- package/public/i18n/literals/nl.json +195 -0
- package/public/i18n/literals/no.json +195 -0
- package/public/i18n/literals/pl.json +195 -0
- package/public/i18n/literals/pt-BR.json +195 -0
- package/public/i18n/literals/pt-PT.json +195 -0
- package/public/i18n/literals/ro.json +195 -0
- package/public/i18n/literals/ru.json +195 -0
- package/public/i18n/literals/sv.json +195 -0
- package/public/i18n/literals/th.json +195 -0
- package/public/i18n/literals/tl.json +195 -0
- package/public/i18n/literals/tr.json +195 -0
- package/public/i18n/literals/uk.json +195 -0
- package/public/i18n/literals/ur.json +195 -0
- package/public/i18n/literals/vi.json +195 -0
- package/public/i18n/literals/zh-CN.json +772 -0
- package/public/i18n/literals/zh-TW.json +195 -0
- package/public/icons/discord.svg +4 -0
- package/public/icons/icon-192.svg +4 -0
- package/public/icons/icon-512.svg +4 -0
- package/public/next.svg +1 -0
- package/public/providers/alicode-intl.png +0 -0
- package/public/providers/alicode.png +0 -0
- package/public/providers/amp.png +0 -0
- package/public/providers/anthropic-m.png +0 -0
- package/public/providers/anthropic.png +0 -0
- package/public/providers/antigravity.png +0 -0
- package/public/providers/assemblyai.png +0 -0
- package/public/providers/aws-polly.png +0 -0
- package/public/providers/azure.png +0 -0
- package/public/providers/black-forest-labs.png +0 -0
- package/public/providers/blackbox.png +0 -0
- package/public/providers/brave-search.png +0 -0
- package/public/providers/byteplus.png +0 -0
- package/public/providers/cartesia.png +0 -0
- package/public/providers/cerebras.png +0 -0
- package/public/providers/chutes.png +0 -0
- package/public/providers/claude.png +0 -0
- package/public/providers/cline.png +0 -0
- package/public/providers/cloudflare-ai.png +0 -0
- package/public/providers/codebuddy.svg +37 -0
- package/public/providers/codex.png +0 -0
- package/public/providers/cohere.png +0 -0
- package/public/providers/comfyui.png +0 -0
- package/public/providers/commandcode.png +0 -0
- package/public/providers/continue.png +0 -0
- package/public/providers/copilot.png +0 -0
- package/public/providers/coqui.png +0 -0
- package/public/providers/cursor.png +0 -0
- package/public/providers/deepgram.png +0 -0
- package/public/providers/deepseek-tui.png +0 -0
- package/public/providers/deepseek.png +0 -0
- package/public/providers/droid.png +0 -0
- package/public/providers/edge-tts.png +0 -0
- package/public/providers/elevenlabs.png +0 -0
- package/public/providers/exa.png +0 -0
- package/public/providers/fal-ai.png +0 -0
- package/public/providers/firecrawl.png +0 -0
- package/public/providers/fireworks.png +0 -0
- package/public/providers/gemini-cli.png +0 -0
- package/public/providers/gemini.png +0 -0
- package/public/providers/github.png +0 -0
- package/public/providers/glm-cn.png +0 -0
- package/public/providers/glm.png +0 -0
- package/public/providers/google-pse.png +0 -0
- package/public/providers/google-tts.png +0 -0
- package/public/providers/grok-web.png +0 -0
- package/public/providers/groq.png +0 -0
- package/public/providers/hermes.png +0 -0
- package/public/providers/huggingface.png +0 -0
- package/public/providers/hyperbolic.png +0 -0
- package/public/providers/iflow.png +0 -0
- package/public/providers/inworld.png +0 -0
- package/public/providers/jcode.png +0 -0
- package/public/providers/jina-ai.png +0 -0
- package/public/providers/jina-reader.png +0 -0
- package/public/providers/kilocode.png +0 -0
- package/public/providers/kimi-coding.png +0 -0
- package/public/providers/kimi.png +0 -0
- package/public/providers/kiro.png +0 -0
- package/public/providers/linkup.png +0 -0
- package/public/providers/local-device.png +0 -0
- package/public/providers/minimax-cn.png +0 -0
- package/public/providers/minimax.png +0 -0
- package/public/providers/mistral.png +0 -0
- package/public/providers/nanobanana.png +0 -0
- package/public/providers/nebius.png +0 -0
- package/public/providers/nvidia.png +0 -0
- package/public/providers/oai-cc.png +0 -0
- package/public/providers/oai-r.png +0 -0
- package/public/providers/ollama-local.png +0 -0
- package/public/providers/ollama.png +0 -0
- package/public/providers/openai.png +0 -0
- package/public/providers/openclaw.png +0 -0
- package/public/providers/opencode-go.png +0 -0
- package/public/providers/opencode.png +0 -0
- package/public/providers/openrouter.png +0 -0
- package/public/providers/perplexity-web.png +0 -0
- package/public/providers/perplexity.png +0 -0
- package/public/providers/playht.png +0 -0
- package/public/providers/qoder.png +0 -0
- package/public/providers/qwen.png +0 -0
- package/public/providers/recraft.png +0 -0
- package/public/providers/roo.png +0 -0
- package/public/providers/runwayml.png +0 -0
- package/public/providers/sdwebui.png +0 -0
- package/public/providers/searchapi.png +0 -0
- package/public/providers/searxng.png +0 -0
- package/public/providers/serper.png +0 -0
- package/public/providers/siliconflow.png +0 -0
- package/public/providers/stability-ai.png +0 -0
- package/public/providers/tavily.png +0 -0
- package/public/providers/together.png +0 -0
- package/public/providers/topaz.png +0 -0
- package/public/providers/tortoise.png +0 -0
- package/public/providers/vertex-partner.png +0 -0
- package/public/providers/vertex.png +0 -0
- package/public/providers/volcengine-ark.png +0 -0
- package/public/providers/voyage-ai.png +0 -0
- package/public/providers/xai.png +0 -0
- package/public/providers/xiaomi-mimo.png +0 -0
- package/public/providers/xiaomi-tokenplan.png +0 -0
- package/public/providers/youcom.png +0 -0
- package/public/sw.js +22 -0
- package/public/vercel.svg +1 -0
- package/public/window.svg +1 -0
- package/scripts/compact-request-details.mjs +71 -0
- package/scripts/import-codex-gptjson.mjs +342 -0
- package/scripts/start-standalone.mjs +25 -0
- package/scripts/translate-readme.js +201 -0
- package/src/app/(dashboard)/dashboard/automation/page.js +294 -0
- package/src/app/(dashboard)/dashboard/basic-chat/BasicChatPageClient.js +967 -0
- package/src/app/(dashboard)/dashboard/basic-chat/page.js +5 -0
- package/src/app/(dashboard)/dashboard/cli-tools/CLIToolsPageClient.js +66 -0
- package/src/app/(dashboard)/dashboard/cli-tools/[toolId]/ToolDetailClient.js +173 -0
- package/src/app/(dashboard)/dashboard/cli-tools/[toolId]/page.js +11 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/AntigravityToolCard.js +481 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ApiKeySelect.js +66 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/BaseUrlSelect.js +174 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ClaudeToolCard.js +390 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ClineToolCard.js +301 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/CodexToolCard.js +458 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/CopilotToolCard.js +323 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/CoworkToolCard.js +640 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/DeepSeekTuiToolCard.js +338 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/DefaultToolCard.js +271 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/DroidToolCard.js +410 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/EndpointPresetControl.js +128 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/HermesToolCard.js +317 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/JcodeToolCard.js +380 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/KiloToolCard.js +275 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/MitmLinkCard.js +40 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/MitmServerCard.js +329 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/MitmToolCard.js +318 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/OpenClawToolCard.js +388 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/OpenCodeToolCard.js +500 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ToolSummaryCard.js +39 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/cliEndpointMatch.js +13 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/index.js +19 -0
- package/src/app/(dashboard)/dashboard/cli-tools/page.js +7 -0
- package/src/app/(dashboard)/dashboard/combos/page.js +612 -0
- package/src/app/(dashboard)/dashboard/console-log/ConsoleLogClient.js +91 -0
- package/src/app/(dashboard)/dashboard/console-log/page.js +8 -0
- package/src/app/(dashboard)/dashboard/endpoint/EndpointPageClient.js +1555 -0
- package/src/app/(dashboard)/dashboard/endpoint/page.js +7 -0
- package/src/app/(dashboard)/dashboard/media-providers/[kind]/[id]/page.js +1903 -0
- package/src/app/(dashboard)/dashboard/media-providers/[kind]/page.js +289 -0
- package/src/app/(dashboard)/dashboard/media-providers/combo/[id]/page.js +410 -0
- package/src/app/(dashboard)/dashboard/media-providers/web/page.js +209 -0
- package/src/app/(dashboard)/dashboard/mitm/MitmPageClient.js +117 -0
- package/src/app/(dashboard)/dashboard/mitm/page.js +5 -0
- package/src/app/(dashboard)/dashboard/page.js +7 -0
- package/src/app/(dashboard)/dashboard/profile/page.js +1140 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/AddApiKeyModal.js +389 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/AddCustomModelModal.js +125 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/CompatibleModelsSection.js +250 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/ConnectionRow.js +299 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/CooldownTimer.js +42 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/EditCompatibleNodeModal.js +161 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/ModelRow.js +95 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/PassthroughModelsSection.js +183 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/page.js +2053 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/page.new.js +1724 -0
- package/src/app/(dashboard)/dashboard/providers/components/ConnectionsCard.js +497 -0
- package/src/app/(dashboard)/dashboard/providers/components/ModelAvailabilityBadge.js +185 -0
- package/src/app/(dashboard)/dashboard/providers/components/ModelsCard.js +294 -0
- package/src/app/(dashboard)/dashboard/providers/new/page.js +220 -0
- package/src/app/(dashboard)/dashboard/providers/page.js +1365 -0
- package/src/app/(dashboard)/dashboard/proxy-pools/page.js +1092 -0
- package/src/app/(dashboard)/dashboard/quota/page.js +11 -0
- package/src/app/(dashboard)/dashboard/skills/page.js +112 -0
- package/src/app/(dashboard)/dashboard/translator/page.js +303 -0
- package/src/app/(dashboard)/dashboard/usage/components/OverviewCards.js +35 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/ProviderLimitCard.js +185 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/QuotaProgressBar.js +127 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/QuotaTable.js +259 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/index.js +1394 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/utils.js +244 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderTopology.js +327 -0
- package/src/app/(dashboard)/dashboard/usage/components/RequestDetailsTab.js +433 -0
- package/src/app/(dashboard)/dashboard/usage/components/UsageChart.js +141 -0
- package/src/app/(dashboard)/dashboard/usage/components/UsageTable.js +247 -0
- package/src/app/(dashboard)/dashboard/usage/page.js +75 -0
- package/src/app/(dashboard)/layout.js +6 -0
- package/src/app/api/auth/login/route.js +76 -0
- package/src/app/api/auth/logout/route.js +12 -0
- package/src/app/api/auth/oidc/callback/route.js +87 -0
- package/src/app/api/auth/oidc/start/route.js +52 -0
- package/src/app/api/auth/oidc/test/route.js +84 -0
- package/src/app/api/auth/status/route.js +45 -0
- package/src/app/api/cli-tools/all-statuses/route.js +46 -0
- package/src/app/api/cli-tools/antigravity-mitm/alias/route.js +53 -0
- package/src/app/api/cli-tools/antigravity-mitm/route.js +202 -0
- package/src/app/api/cli-tools/claude-settings/route.js +203 -0
- package/src/app/api/cli-tools/cline-settings/route.js +133 -0
- package/src/app/api/cli-tools/codex-gateway/accounts/route.js +16 -0
- package/src/app/api/cli-tools/codex-settings/route.js +239 -0
- package/src/app/api/cli-tools/copilot-settings/route.js +148 -0
- package/src/app/api/cli-tools/cowork-mcp-registry/route.js +77 -0
- package/src/app/api/cli-tools/cowork-mcp-tools/route.js +95 -0
- package/src/app/api/cli-tools/cowork-settings/route.js +412 -0
- package/src/app/api/cli-tools/deepseek-tui-settings/route.js +164 -0
- package/src/app/api/cli-tools/droid-settings/route.js +213 -0
- package/src/app/api/cli-tools/hermes-settings/route.js +175 -0
- package/src/app/api/cli-tools/jcode-settings/route.js +216 -0
- package/src/app/api/cli-tools/kilo-settings/route.js +131 -0
- package/src/app/api/cli-tools/openclaw-settings/route.js +292 -0
- package/src/app/api/cli-tools/opencode-settings/route.js +259 -0
- package/src/app/api/combos/[id]/route.js +81 -0
- package/src/app/api/combos/route.js +48 -0
- package/src/app/api/health/route.js +15 -0
- package/src/app/api/init/route.js +4 -0
- package/src/app/api/keys/[id]/route.js +58 -0
- package/src/app/api/keys/route.js +42 -0
- package/src/app/api/locale/route.js +30 -0
- package/src/app/api/mcp/[plugin]/message/route.js +21 -0
- package/src/app/api/mcp/[plugin]/sse/route.js +37 -0
- package/src/app/api/media-providers/tts/deepgram/voices/route.js +65 -0
- package/src/app/api/media-providers/tts/elevenlabs/voices/route.js +71 -0
- package/src/app/api/media-providers/tts/inworld/voices/route.js +61 -0
- package/src/app/api/media-providers/tts/minimax/voices/route.js +113 -0
- package/src/app/api/media-providers/tts/voices/route.js +99 -0
- package/src/app/api/models/alias/route.js +53 -0
- package/src/app/api/models/availability/route.js +103 -0
- package/src/app/api/models/custom/route.js +48 -0
- package/src/app/api/models/disabled/route.js +50 -0
- package/src/app/api/models/route.js +64 -0
- package/src/app/api/models/test/ping.js +191 -0
- package/src/app/api/models/test/route.js +14 -0
- package/src/app/api/oauth/[provider]/[action]/route.js +343 -0
- package/src/app/api/oauth/codebuddy/bulk-import/[jobId]/cancel/route.js +19 -0
- package/src/app/api/oauth/codebuddy/bulk-import/[jobId]/manual/[workerId]/route.js +30 -0
- package/src/app/api/oauth/codebuddy/bulk-import/[jobId]/route.js +23 -0
- package/src/app/api/oauth/codebuddy/bulk-import/latest/route.js +25 -0
- package/src/app/api/oauth/codebuddy/bulk-import/route.js +49 -0
- package/src/app/api/oauth/codebuddy/quota-cookie/route.js +133 -0
- package/src/app/api/oauth/codex/import-token/route.js +96 -0
- package/src/app/api/oauth/cursor/auto-import/route.js +258 -0
- package/src/app/api/oauth/cursor/import/route.js +100 -0
- package/src/app/api/oauth/gitlab/pat/route.js +62 -0
- package/src/app/api/oauth/iflow/cookie/route.js +137 -0
- package/src/app/api/oauth/kiro/auto-import/route.js +85 -0
- package/src/app/api/oauth/kiro/bulk-import/[jobId]/cancel/route.js +18 -0
- package/src/app/api/oauth/kiro/bulk-import/[jobId]/manual/[workerId]/route.js +29 -0
- package/src/app/api/oauth/kiro/bulk-import/[jobId]/route.js +22 -0
- package/src/app/api/oauth/kiro/bulk-import/latest/route.js +25 -0
- package/src/app/api/oauth/kiro/bulk-import/route.js +49 -0
- package/src/app/api/oauth/kiro/import/route.js +110 -0
- package/src/app/api/oauth/kiro/social-authorize/route.js +27 -0
- package/src/app/api/oauth/kiro/social-exchange/route.js +41 -0
- package/src/app/api/pricing/route.js +134 -0
- package/src/app/api/provider-nodes/[id]/route.js +101 -0
- package/src/app/api/provider-nodes/route.js +104 -0
- package/src/app/api/provider-nodes/validate/route.js +201 -0
- package/src/app/api/providers/[id]/models/route.js +526 -0
- package/src/app/api/providers/[id]/route.js +189 -0
- package/src/app/api/providers/[id]/test/route.js +23 -0
- package/src/app/api/providers/[id]/test/testUtils.js +714 -0
- package/src/app/api/providers/[id]/test-models/route.js +66 -0
- package/src/app/api/providers/client/route.js +126 -0
- package/src/app/api/providers/kilo/free-models/route.js +55 -0
- package/src/app/api/providers/route.js +206 -0
- package/src/app/api/providers/suggested-models/filters.js +20 -0
- package/src/app/api/providers/suggested-models/route.js +32 -0
- package/src/app/api/providers/test-batch/route.js +131 -0
- package/src/app/api/providers/validate/route.js +637 -0
- package/src/app/api/proxy-pools/[id]/route.js +123 -0
- package/src/app/api/proxy-pools/[id]/test/route.js +70 -0
- package/src/app/api/proxy-pools/cloudflare-deploy/route.js +145 -0
- package/src/app/api/proxy-pools/deno-deploy/route.js +175 -0
- package/src/app/api/proxy-pools/route.js +93 -0
- package/src/app/api/proxy-pools/vercel-deploy/route.js +142 -0
- package/src/app/api/settings/database/route.js +36 -0
- package/src/app/api/settings/proxy-test/route.js +23 -0
- package/src/app/api/settings/require-login/route.js +15 -0
- package/src/app/api/settings/route.js +100 -0
- package/src/app/api/shutdown/route.js +24 -0
- package/src/app/api/tags/route.js +18 -0
- package/src/app/api/translator/console-logs/route.js +24 -0
- package/src/app/api/translator/console-logs/stream/route.js +79 -0
- package/src/app/api/translator/load/route.js +45 -0
- package/src/app/api/translator/save/route.js +44 -0
- package/src/app/api/translator/send/route.js +94 -0
- package/src/app/api/translator/translate/route.js +90 -0
- package/src/app/api/tunnel/disable/route.js +12 -0
- package/src/app/api/tunnel/enable/route.js +16 -0
- package/src/app/api/tunnel/status/route.js +13 -0
- package/src/app/api/tunnel/tailscale-check/route.js +50 -0
- package/src/app/api/tunnel/tailscale-disable/route.js +12 -0
- package/src/app/api/tunnel/tailscale-enable/route.js +12 -0
- package/src/app/api/tunnel/tailscale-install/route.js +72 -0
- package/src/app/api/usage/[connectionId]/route.js +188 -0
- package/src/app/api/usage/chart/route.js +21 -0
- package/src/app/api/usage/history/route.js +12 -0
- package/src/app/api/usage/logs/route.js +12 -0
- package/src/app/api/usage/providers/route.js +42 -0
- package/src/app/api/usage/request-details/route.js +57 -0
- package/src/app/api/usage/request-logs/route.js +13 -0
- package/src/app/api/usage/stats/route.js +23 -0
- package/src/app/api/usage/stream/route.js +79 -0
- package/src/app/api/v1/api/chat/route.js +37 -0
- package/src/app/api/v1/audio/speech/route.js +16 -0
- package/src/app/api/v1/audio/transcriptions/route.js +19 -0
- package/src/app/api/v1/audio/voices/route.js +68 -0
- package/src/app/api/v1/chat/completions/route.js +35 -0
- package/src/app/api/v1/embeddings/route.js +21 -0
- package/src/app/api/v1/images/generations/route.js +16 -0
- package/src/app/api/v1/messages/count_tokens/route.js +52 -0
- package/src/app/api/v1/messages/route.js +36 -0
- package/src/app/api/v1/models/[kind]/route.js +55 -0
- package/src/app/api/v1/models/info/route.js +110 -0
- package/src/app/api/v1/models/route.js +451 -0
- package/src/app/api/v1/responses/compact/route.js +37 -0
- package/src/app/api/v1/responses/route.js +30 -0
- package/src/app/api/v1/route.js +1 -0
- package/src/app/api/v1/search/route.js +21 -0
- package/src/app/api/v1/web/fetch/route.js +21 -0
- package/src/app/api/v1beta/models/[...path]/route.js +328 -0
- package/src/app/api/v1beta/models/route.js +44 -0
- package/src/app/api/version/route.js +45 -0
- package/src/app/api/version/shutdown/route.js +15 -0
- package/src/app/api/version/update/route.js +21 -0
- package/src/app/callback/page.js +148 -0
- package/src/app/dashboard/settings/pricing/page.js +173 -0
- package/src/app/favicon.ico +0 -0
- package/src/app/globals.css +496 -0
- package/src/app/landing/components/AnimatedBackground.js +57 -0
- package/src/app/landing/components/Features.js +133 -0
- package/src/app/landing/components/FlowAnimation.js +175 -0
- package/src/app/landing/components/Footer.js +61 -0
- package/src/app/landing/components/GetStarted.js +97 -0
- package/src/app/landing/components/HeroSection.js +47 -0
- package/src/app/landing/components/HowItWorks.js +66 -0
- package/src/app/landing/components/Navigation.js +72 -0
- package/src/app/landing/page.js +106 -0
- package/src/app/layout.js +49 -0
- package/src/app/login/page.js +197 -0
- package/src/app/manifest.js +30 -0
- package/src/app/page.js +5 -0
- package/src/dashboardGuard.js +242 -0
- package/src/i18n/RuntimeI18nProvider.js +27 -0
- package/src/i18n/config.js +146 -0
- package/src/i18n/runtime.js +162 -0
- package/src/lib/appUpdater.js +200 -0
- package/src/lib/auth/dashboardSession.js +68 -0
- package/src/lib/auth/loginLimiter.js +52 -0
- package/src/lib/auth/oidc.js +234 -0
- package/src/lib/consoleLogBuffer.js +79 -0
- package/src/lib/dataDir.js +29 -0
- package/src/lib/db/adapters/betterSqliteAdapter.js +55 -0
- package/src/lib/db/adapters/bunSqliteAdapter.js +63 -0
- package/src/lib/db/adapters/nodeSqliteAdapter.js +84 -0
- package/src/lib/db/adapters/sqljsAdapter.js +115 -0
- package/src/lib/db/backup.js +35 -0
- package/src/lib/db/driver.js +85 -0
- package/src/lib/db/helpers/jsonCol.js +9 -0
- package/src/lib/db/helpers/kvStore.js +39 -0
- package/src/lib/db/helpers/metaStore.js +22 -0
- package/src/lib/db/index.js +171 -0
- package/src/lib/db/migrate.js +286 -0
- package/src/lib/db/migrations/001-initial.js +14 -0
- package/src/lib/db/migrations/index.js +10 -0
- package/src/lib/db/paths.js +18 -0
- package/src/lib/db/repos/aliasRepo.js +62 -0
- package/src/lib/db/repos/apiKeysRepo.js +75 -0
- package/src/lib/db/repos/combosRepo.js +73 -0
- package/src/lib/db/repos/connectionsRepo.js +226 -0
- package/src/lib/db/repos/disabledModelsRepo.js +56 -0
- package/src/lib/db/repos/nodesRepo.js +95 -0
- package/src/lib/db/repos/pricingRepo.js +108 -0
- package/src/lib/db/repos/proxyPoolsRepo.js +103 -0
- package/src/lib/db/repos/requestDetailsRepo.js +259 -0
- package/src/lib/db/repos/settingsRepo.js +104 -0
- package/src/lib/db/repos/usageRepo.js +731 -0
- package/src/lib/db/schema.js +157 -0
- package/src/lib/db/version.js +21 -0
- package/src/lib/disabledModelsDb.js +4 -0
- package/src/lib/localDb.js +21 -0
- package/src/lib/mcp/stdioSseBridge.js +198 -0
- package/src/lib/mitmAliasCache.js +46 -0
- package/src/lib/network/connectionProxy.js +160 -0
- package/src/lib/network/initOutboundProxy.js +25 -0
- package/src/lib/network/outboundProxy.js +68 -0
- package/src/lib/network/proxyTest.js +91 -0
- package/src/lib/oauth/constants/oauth.js +284 -0
- package/src/lib/oauth/constants/xai.js +61 -0
- package/src/lib/oauth/providers.js +1506 -0
- package/src/lib/oauth/services/antigravity.js +321 -0
- package/src/lib/oauth/services/claude.js +136 -0
- package/src/lib/oauth/services/codebuddyBulkImportManager.js +454 -0
- package/src/lib/oauth/services/codex.js +144 -0
- package/src/lib/oauth/services/cursor.js +179 -0
- package/src/lib/oauth/services/gemini.js +240 -0
- package/src/lib/oauth/services/github.js +225 -0
- package/src/lib/oauth/services/iflow.js +202 -0
- package/src/lib/oauth/services/index.js +17 -0
- package/src/lib/oauth/services/kiro.js +334 -0
- package/src/lib/oauth/services/kiroBulkImportManager.js +778 -0
- package/src/lib/oauth/services/kiroConnections.js +93 -0
- package/src/lib/oauth/services/kiroGoogleAutomation.js +1136 -0
- package/src/lib/oauth/services/oauth.js +157 -0
- package/src/lib/oauth/services/openai.js +123 -0
- package/src/lib/oauth/services/qoder.js +216 -0
- package/src/lib/oauth/services/qwen.js +170 -0
- package/src/lib/oauth/services/xai.js +238 -0
- package/src/lib/oauth/utils/banner.js +63 -0
- package/src/lib/oauth/utils/pkce.js +39 -0
- package/src/lib/oauth/utils/server.js +415 -0
- package/src/lib/oauth/utils/ui.js +48 -0
- package/src/lib/providerNormalization.js +45 -0
- package/src/lib/qoder/constants.js +64 -0
- package/src/lib/qoder/cosy.js +175 -0
- package/src/lib/qoder/encoding.js +55 -0
- package/src/lib/requestDetailsDb.js +4 -0
- package/src/lib/tunnel/cloudflare/cloudflared.js +449 -0
- package/src/lib/tunnel/cloudflare/config.js +9 -0
- package/src/lib/tunnel/cloudflare/healthCheck.js +29 -0
- package/src/lib/tunnel/cloudflare/manager.js +151 -0
- package/src/lib/tunnel/cloudflare/pid.js +23 -0
- package/src/lib/tunnel/index.js +48 -0
- package/src/lib/tunnel/shared/dnsResolver.js +17 -0
- package/src/lib/tunnel/shared/internetCheck.js +26 -0
- package/src/lib/tunnel/shared/state.js +41 -0
- package/src/lib/tunnel/shared/watchdogConfig.js +8 -0
- package/src/lib/tunnel/tailscale/config.js +7 -0
- package/src/lib/tunnel/tailscale/healthCheck.js +29 -0
- package/src/lib/tunnel/tailscale/manager.js +129 -0
- package/src/lib/tunnel/tailscale/tailscale.js +790 -0
- package/src/lib/updater/updater.js +235 -0
- package/src/lib/usage/fetcher.js +208 -0
- package/src/lib/usageDb.js +7 -0
- package/src/mitm/antigravityIdeVersion.js +50 -0
- package/src/mitm/cert/generate.js +32 -0
- package/src/mitm/cert/install.js +269 -0
- package/src/mitm/cert/rootCA.js +173 -0
- package/src/mitm/config.js +87 -0
- package/src/mitm/dbReader.js +22 -0
- package/src/mitm/dns/dnsConfig.js +266 -0
- package/src/mitm/handlers/antigravity.js +33 -0
- package/src/mitm/handlers/base.js +226 -0
- package/src/mitm/handlers/copilot.js +35 -0
- package/src/mitm/handlers/cursor.js +15 -0
- package/src/mitm/handlers/kiro.js +526 -0
- package/src/mitm/logger.js +106 -0
- package/src/mitm/manager.js +851 -0
- package/src/mitm/paths.js +32 -0
- package/src/mitm/server.js +435 -0
- package/src/mitm/winElevated.js +81 -0
- package/src/models/index.js +38 -0
- package/src/proxy.js +5 -0
- package/src/shared/components/AddCustomEmbeddingModal.js +183 -0
- package/src/shared/components/Avatar.js +88 -0
- package/src/shared/components/Badge.js +54 -0
- package/src/shared/components/BulkAccountAutomationModal.js +508 -0
- package/src/shared/components/Button.js +56 -0
- package/src/shared/components/Card.js +116 -0
- package/src/shared/components/ChangelogModal.js +97 -0
- package/src/shared/components/CodeBuddyQuotaCookieModal.js +109 -0
- package/src/shared/components/ComboFormModal.js +176 -0
- package/src/shared/components/CursorAuthModal.js +212 -0
- package/src/shared/components/DonateModal.js +136 -0
- package/src/shared/components/Drawer.js +82 -0
- package/src/shared/components/EditConnectionModal.js +286 -0
- package/src/shared/components/Footer.js +132 -0
- package/src/shared/components/GitLabAuthModal.js +194 -0
- package/src/shared/components/Header.js +380 -0
- package/src/shared/components/HeaderLanguage.js +46 -0
- package/src/shared/components/HeaderMenu.js +126 -0
- package/src/shared/components/IFlowCookieModal.js +132 -0
- package/src/shared/components/Input.js +65 -0
- package/src/shared/components/KiroAuthModal.js +1171 -0
- package/src/shared/components/KiroOAuthWrapper.js +149 -0
- package/src/shared/components/KiroSocialOAuthModal.js +205 -0
- package/src/shared/components/LanguageSwitcher.js +190 -0
- package/src/shared/components/Loading.js +75 -0
- package/src/shared/components/ManualConfigModal.js +44 -0
- package/src/shared/components/McpMarketplaceModal.js +255 -0
- package/src/shared/components/Modal.js +146 -0
- package/src/shared/components/ModelSelectModal.js +537 -0
- package/src/shared/components/NineRemoteButton.js +23 -0
- package/src/shared/components/NineRemotePromoModal.js +99 -0
- package/src/shared/components/NoAuthProxyCard.js +86 -0
- package/src/shared/components/OAuthModal.js +682 -0
- package/src/shared/components/Pagination.js +150 -0
- package/src/shared/components/PricingModal.js +208 -0
- package/src/shared/components/ProviderIcon.js +63 -0
- package/src/shared/components/ProviderInfoCard.js +82 -0
- package/src/shared/components/RequestLogger.js +121 -0
- package/src/shared/components/SegmentedControl.js +48 -0
- package/src/shared/components/Select.js +67 -0
- package/src/shared/components/Sidebar.js +441 -0
- package/src/shared/components/ThemeProvider.js +15 -0
- package/src/shared/components/ThemeToggle.js +42 -0
- package/src/shared/components/Toggle.js +69 -0
- package/src/shared/components/Tooltip.js +25 -0
- package/src/shared/components/UsageStats.js +505 -0
- package/src/shared/components/index.js +46 -0
- package/src/shared/components/layouts/AuthLayout.js +29 -0
- package/src/shared/components/layouts/DashboardLayout.js +104 -0
- package/src/shared/components/layouts/index.js +4 -0
- package/src/shared/constants/cliTools.js +397 -0
- package/src/shared/constants/colors.js +77 -0
- package/src/shared/constants/config.js +99 -0
- package/src/shared/constants/coworkPlugins.js +75 -0
- package/src/shared/constants/index.js +4 -0
- package/src/shared/constants/locales.js +36 -0
- package/src/shared/constants/mitmToolHosts.js +12 -0
- package/src/shared/constants/models.js +38 -0
- package/src/shared/constants/pricing.js +303 -0
- package/src/shared/constants/providers.js +289 -0
- package/src/shared/constants/skills.js +78 -0
- package/src/shared/constants/ttsProviders.js +138 -0
- package/src/shared/hooks/index.js +2 -0
- package/src/shared/hooks/useCopyToClipboard.js +43 -0
- package/src/shared/hooks/useTheme.js +60 -0
- package/src/shared/services/bootstrap.js +12 -0
- package/src/shared/services/initializeApp.js +268 -0
- package/src/shared/utils/api.js +93 -0
- package/src/shared/utils/apiKey.js +98 -0
- package/src/shared/utils/clineAuth.js +37 -0
- package/src/shared/utils/cn.js +11 -0
- package/src/shared/utils/connectionStatus.js +162 -0
- package/src/shared/utils/index.js +40 -0
- package/src/shared/utils/machine.js +6 -0
- package/src/shared/utils/machineId.js +66 -0
- package/src/shared/utils/providerModelsFetcher.js +30 -0
- package/src/sse/handlers/chat.js +261 -0
- package/src/sse/handlers/embeddings.js +141 -0
- package/src/sse/handlers/fetch.js +213 -0
- package/src/sse/handlers/imageGeneration.js +142 -0
- package/src/sse/handlers/search.js +206 -0
- package/src/sse/handlers/stt.js +88 -0
- package/src/sse/handlers/tts.js +114 -0
- package/src/sse/services/auth.js +346 -0
- package/src/sse/services/codexGateway.js +215 -0
- package/src/sse/services/model.js +99 -0
- package/src/sse/services/tokenRefresh.js +319 -0
- package/src/sse/utils/logger.js +75 -0
- package/src/store/headerSearchStore.js +19 -0
- package/src/store/index.js +6 -0
- package/src/store/notificationStore.js +45 -0
- package/src/store/providerStore.js +55 -0
- package/src/store/settingsStore.js +51 -0
- package/src/store/themeStore.js +54 -0
- package/src/store/userStore.js +20 -0
- package/start.sh +4 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import Database from "better-sqlite3";
|
|
2
|
+
import { PRAGMA_SQL } from "../schema.js";
|
|
3
|
+
|
|
4
|
+
// Periodic checkpoint to keep WAL file small (avoid huge -wal/-shm growth)
|
|
5
|
+
const CHECKPOINT_INTERVAL_MS = 60 * 1000;
|
|
6
|
+
|
|
7
|
+
export function createBetterSqliteAdapter(filePath) {
|
|
8
|
+
const db = new Database(filePath);
|
|
9
|
+
db.exec(PRAGMA_SQL);
|
|
10
|
+
// Schema is created/synced by migrate.js after adapter init
|
|
11
|
+
|
|
12
|
+
const stmtCache = new Map();
|
|
13
|
+
|
|
14
|
+
function prepare(sql) {
|
|
15
|
+
let stmt = stmtCache.get(sql);
|
|
16
|
+
if (!stmt) {
|
|
17
|
+
stmt = db.prepare(sql);
|
|
18
|
+
stmtCache.set(sql, stmt);
|
|
19
|
+
}
|
|
20
|
+
return stmt;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Truncate WAL periodically so file stays small for backup/copy
|
|
24
|
+
const checkpointTimer = setInterval(() => {
|
|
25
|
+
try { db.pragma("wal_checkpoint(TRUNCATE)"); } catch {}
|
|
26
|
+
}, CHECKPOINT_INTERVAL_MS);
|
|
27
|
+
if (typeof checkpointTimer.unref === "function") checkpointTimer.unref();
|
|
28
|
+
|
|
29
|
+
function gracefulClose() {
|
|
30
|
+
try { db.pragma("wal_checkpoint(TRUNCATE)"); } catch {}
|
|
31
|
+
try { stmtCache.clear(); } catch {}
|
|
32
|
+
try { db.close(); } catch {}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Ensure WAL is flushed and -wal/-shm files removed on shutdown
|
|
36
|
+
const onShutdown = () => gracefulClose();
|
|
37
|
+
process.once("beforeExit", onShutdown);
|
|
38
|
+
process.once("SIGINT", () => { onShutdown(); process.exit(0); });
|
|
39
|
+
process.once("SIGTERM", () => { onShutdown(); process.exit(0); });
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
driver: "better-sqlite3",
|
|
43
|
+
run(sql, params = []) { return prepare(sql).run(params); },
|
|
44
|
+
get(sql, params = []) { return prepare(sql).get(params); },
|
|
45
|
+
all(sql, params = []) { return prepare(sql).all(params); },
|
|
46
|
+
exec(sql) { return db.exec(sql); },
|
|
47
|
+
transaction(fn) { return db.transaction(fn)(); },
|
|
48
|
+
checkpoint() { try { db.pragma("wal_checkpoint(TRUNCATE)"); } catch {} },
|
|
49
|
+
close() {
|
|
50
|
+
clearInterval(checkpointTimer);
|
|
51
|
+
gracefulClose();
|
|
52
|
+
},
|
|
53
|
+
raw: db,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Bun runtime adapter — uses built-in bun:sqlite (native, fastest under Bun).
|
|
2
|
+
// Loaded only when process.versions.bun is present.
|
|
3
|
+
import { PRAGMA_SQL } from "../schema.js";
|
|
4
|
+
|
|
5
|
+
const CHECKPOINT_INTERVAL_MS = 60 * 1000;
|
|
6
|
+
|
|
7
|
+
export async function createBunSqliteAdapter(filePath) {
|
|
8
|
+
// Dynamic import — only resolves under Bun runtime
|
|
9
|
+
const { Database } = await import("bun:sqlite");
|
|
10
|
+
const db = new Database(filePath, { create: true });
|
|
11
|
+
db.exec(PRAGMA_SQL);
|
|
12
|
+
|
|
13
|
+
const stmtCache = new Map();
|
|
14
|
+
function prepare(sql) {
|
|
15
|
+
let stmt = stmtCache.get(sql);
|
|
16
|
+
if (!stmt) {
|
|
17
|
+
stmt = db.prepare(sql);
|
|
18
|
+
stmtCache.set(sql, stmt);
|
|
19
|
+
}
|
|
20
|
+
return stmt;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const checkpointTimer = setInterval(() => {
|
|
24
|
+
try { db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); } catch {}
|
|
25
|
+
}, CHECKPOINT_INTERVAL_MS);
|
|
26
|
+
if (typeof checkpointTimer.unref === "function") checkpointTimer.unref();
|
|
27
|
+
|
|
28
|
+
function gracefulClose() {
|
|
29
|
+
try { db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); } catch {}
|
|
30
|
+
try { stmtCache.clear(); } catch {}
|
|
31
|
+
try { db.close(); } catch {}
|
|
32
|
+
}
|
|
33
|
+
const onShutdown = () => gracefulClose();
|
|
34
|
+
process.once("beforeExit", onShutdown);
|
|
35
|
+
process.once("SIGINT", () => { onShutdown(); process.exit(0); });
|
|
36
|
+
process.once("SIGTERM", () => { onShutdown(); process.exit(0); });
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
driver: "bun:sqlite",
|
|
40
|
+
run(sql, params = []) {
|
|
41
|
+
const r = prepare(sql).run(...params);
|
|
42
|
+
return { changes: Number(r.changes ?? 0), lastInsertRowid: Number(r.lastInsertRowid ?? 0) };
|
|
43
|
+
},
|
|
44
|
+
get(sql, params = []) {
|
|
45
|
+
return prepare(sql).get(...params);
|
|
46
|
+
},
|
|
47
|
+
all(sql, params = []) {
|
|
48
|
+
return prepare(sql).all(...params);
|
|
49
|
+
},
|
|
50
|
+
exec(sql) { return db.exec(sql); },
|
|
51
|
+
transaction(fn) {
|
|
52
|
+
// bun:sqlite has db.transaction() API (similar to better-sqlite3)
|
|
53
|
+
const tx = db.transaction(fn);
|
|
54
|
+
return tx();
|
|
55
|
+
},
|
|
56
|
+
checkpoint() { try { db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); } catch {} },
|
|
57
|
+
close() {
|
|
58
|
+
clearInterval(checkpointTimer);
|
|
59
|
+
gracefulClose();
|
|
60
|
+
},
|
|
61
|
+
raw: db,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Built-in node:sqlite adapter — available in Node >= 22.5.0.
|
|
2
|
+
// No native build, no npm install. API mirrors betterSqliteAdapter.
|
|
3
|
+
import { PRAGMA_SQL } from "../schema.js";
|
|
4
|
+
|
|
5
|
+
const CHECKPOINT_INTERVAL_MS = 60 * 1000;
|
|
6
|
+
|
|
7
|
+
export async function createNodeSqliteAdapter(filePath) {
|
|
8
|
+
// Suppress "ExperimentalWarning: SQLite is an experimental feature" from node:sqlite.
|
|
9
|
+
// Stable enough for production use as of Node 22.x (RC quality).
|
|
10
|
+
const origEmit = process.emit;
|
|
11
|
+
process.emit = function (name, data, ...rest) {
|
|
12
|
+
if (name === "warning" && data?.name === "ExperimentalWarning" && /SQLite/i.test(data.message || "")) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return origEmit.call(process, name, data, ...rest);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// Dynamic import — fails on Node < 22.5 → driver.js falls back to sql.js
|
|
19
|
+
const sqlite = await import("node:sqlite");
|
|
20
|
+
const Database = sqlite.DatabaseSync;
|
|
21
|
+
const db = new Database(filePath);
|
|
22
|
+
|
|
23
|
+
db.exec(PRAGMA_SQL);
|
|
24
|
+
|
|
25
|
+
const stmtCache = new Map();
|
|
26
|
+
function prepare(sql) {
|
|
27
|
+
let stmt = stmtCache.get(sql);
|
|
28
|
+
if (!stmt) {
|
|
29
|
+
stmt = db.prepare(sql);
|
|
30
|
+
stmtCache.set(sql, stmt);
|
|
31
|
+
}
|
|
32
|
+
return stmt;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Periodic WAL checkpoint to keep -wal/-shm small
|
|
36
|
+
const checkpointTimer = setInterval(() => {
|
|
37
|
+
try { db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); } catch {}
|
|
38
|
+
}, CHECKPOINT_INTERVAL_MS);
|
|
39
|
+
if (typeof checkpointTimer.unref === "function") checkpointTimer.unref();
|
|
40
|
+
|
|
41
|
+
function gracefulClose() {
|
|
42
|
+
try { db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); } catch {}
|
|
43
|
+
try { stmtCache.clear(); } catch {}
|
|
44
|
+
try { db.close(); } catch {}
|
|
45
|
+
}
|
|
46
|
+
const onShutdown = () => gracefulClose();
|
|
47
|
+
process.once("beforeExit", onShutdown);
|
|
48
|
+
process.once("SIGINT", () => { onShutdown(); process.exit(0); });
|
|
49
|
+
process.once("SIGTERM", () => { onShutdown(); process.exit(0); });
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
driver: "node:sqlite",
|
|
53
|
+
run(sql, params = []) {
|
|
54
|
+
const r = prepare(sql).run(...params);
|
|
55
|
+
return { changes: Number(r.changes ?? 0), lastInsertRowid: Number(r.lastInsertRowid ?? 0) };
|
|
56
|
+
},
|
|
57
|
+
get(sql, params = []) {
|
|
58
|
+
return prepare(sql).get(...params);
|
|
59
|
+
},
|
|
60
|
+
all(sql, params = []) {
|
|
61
|
+
return prepare(sql).all(...params);
|
|
62
|
+
},
|
|
63
|
+
exec(sql) { return db.exec(sql); },
|
|
64
|
+
transaction(fn) {
|
|
65
|
+
// node:sqlite has no transaction wrapper. Use SAVEPOINT for nested support.
|
|
66
|
+
const sp = `sp_${Math.random().toString(36).slice(2)}`;
|
|
67
|
+
db.exec(`SAVEPOINT ${sp}`);
|
|
68
|
+
try {
|
|
69
|
+
const r = fn();
|
|
70
|
+
db.exec(`RELEASE ${sp}`);
|
|
71
|
+
return r;
|
|
72
|
+
} catch (e) {
|
|
73
|
+
try { db.exec(`ROLLBACK TO ${sp}`); db.exec(`RELEASE ${sp}`); } catch {}
|
|
74
|
+
throw e;
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
checkpoint() { try { db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); } catch {} },
|
|
78
|
+
close() {
|
|
79
|
+
clearInterval(checkpointTimer);
|
|
80
|
+
gracefulClose();
|
|
81
|
+
},
|
|
82
|
+
raw: db,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import initSqlJs from "sql.js";
|
|
3
|
+
import { PRAGMA_SQL } from "../schema.js";
|
|
4
|
+
|
|
5
|
+
let SQL = null;
|
|
6
|
+
|
|
7
|
+
async function loadSql() {
|
|
8
|
+
if (SQL) return SQL;
|
|
9
|
+
SQL = await initSqlJs();
|
|
10
|
+
return SQL;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function createSqlJsAdapter(filePath) {
|
|
14
|
+
const SQLLib = await loadSql();
|
|
15
|
+
const buf = fs.existsSync(filePath) ? fs.readFileSync(filePath) : null;
|
|
16
|
+
const db = new SQLLib.Database(buf);
|
|
17
|
+
db.exec(PRAGMA_SQL);
|
|
18
|
+
// Schema is created/synced by migrate.js after adapter init
|
|
19
|
+
|
|
20
|
+
let dirty = false;
|
|
21
|
+
let saveTimer = null;
|
|
22
|
+
const SAVE_DEBOUNCE_MS = 100;
|
|
23
|
+
|
|
24
|
+
function persist() {
|
|
25
|
+
const data = db.export();
|
|
26
|
+
fs.writeFileSync(filePath, Buffer.from(data));
|
|
27
|
+
dirty = false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function scheduleSave() {
|
|
31
|
+
dirty = true;
|
|
32
|
+
if (saveTimer) clearTimeout(saveTimer);
|
|
33
|
+
saveTimer = setTimeout(() => {
|
|
34
|
+
saveTimer = null;
|
|
35
|
+
if (dirty) {
|
|
36
|
+
try { persist(); } catch (e) { console.error("[sqljs] save failed:", e); }
|
|
37
|
+
}
|
|
38
|
+
}, SAVE_DEBOUNCE_MS);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function paramsObj(params) {
|
|
42
|
+
if (!params || (Array.isArray(params) && params.length === 0)) return undefined;
|
|
43
|
+
return params;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function run(sql, params = []) {
|
|
47
|
+
const stmt = db.prepare(sql);
|
|
48
|
+
try {
|
|
49
|
+
stmt.bind(paramsObj(params));
|
|
50
|
+
stmt.step();
|
|
51
|
+
const changes = db.getRowsModified();
|
|
52
|
+
const lastInsertRowid = db.exec("SELECT last_insert_rowid() as id")[0]?.values?.[0]?.[0] ?? null;
|
|
53
|
+
scheduleSave();
|
|
54
|
+
return { changes, lastInsertRowid };
|
|
55
|
+
} finally {
|
|
56
|
+
stmt.free();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function get(sql, params = []) {
|
|
61
|
+
const stmt = db.prepare(sql);
|
|
62
|
+
try {
|
|
63
|
+
stmt.bind(paramsObj(params));
|
|
64
|
+
if (stmt.step()) return stmt.getAsObject();
|
|
65
|
+
return undefined;
|
|
66
|
+
} finally {
|
|
67
|
+
stmt.free();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function all(sql, params = []) {
|
|
72
|
+
const stmt = db.prepare(sql);
|
|
73
|
+
try {
|
|
74
|
+
stmt.bind(paramsObj(params));
|
|
75
|
+
const rows = [];
|
|
76
|
+
while (stmt.step()) rows.push(stmt.getAsObject());
|
|
77
|
+
return rows;
|
|
78
|
+
} finally {
|
|
79
|
+
stmt.free();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function exec(sql) {
|
|
84
|
+
db.exec(sql);
|
|
85
|
+
scheduleSave();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function transaction(fn) {
|
|
89
|
+
const sp = `sp_${Math.random().toString(36).slice(2)}`;
|
|
90
|
+
db.exec(`SAVEPOINT ${sp}`);
|
|
91
|
+
try {
|
|
92
|
+
const result = fn();
|
|
93
|
+
db.exec(`RELEASE ${sp}`);
|
|
94
|
+
scheduleSave();
|
|
95
|
+
return result;
|
|
96
|
+
} catch (e) {
|
|
97
|
+
try { db.exec(`ROLLBACK TO ${sp}`); db.exec(`RELEASE ${sp}`); } catch {}
|
|
98
|
+
throw e;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function close() {
|
|
103
|
+
if (saveTimer) clearTimeout(saveTimer);
|
|
104
|
+
if (dirty) persist();
|
|
105
|
+
db.close();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Flush on shutdown
|
|
109
|
+
const flush = () => { if (dirty) try { persist(); } catch {} };
|
|
110
|
+
process.on("beforeExit", flush);
|
|
111
|
+
process.on("SIGINT", flush);
|
|
112
|
+
process.on("SIGTERM", flush);
|
|
113
|
+
|
|
114
|
+
return { driver: "sql.js", run, get, all, exec, transaction, close, raw: db };
|
|
115
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { BACKUPS_DIR, ensureDirs } from "./paths.js";
|
|
4
|
+
import { timestampSlug, getAppVersion } from "./version.js";
|
|
5
|
+
|
|
6
|
+
const KEEP_BACKUPS = 5;
|
|
7
|
+
|
|
8
|
+
export function makeBackupDir(label) {
|
|
9
|
+
ensureDirs();
|
|
10
|
+
const ver = getAppVersion();
|
|
11
|
+
const slug = `${label}-${ver}-${timestampSlug()}`;
|
|
12
|
+
const dir = path.join(BACKUPS_DIR, slug);
|
|
13
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
14
|
+
return dir;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function backupFile(srcPath, destDir, destName = null) {
|
|
18
|
+
if (!fs.existsSync(srcPath)) return null;
|
|
19
|
+
const name = destName || path.basename(srcPath);
|
|
20
|
+
const dest = path.join(destDir, name);
|
|
21
|
+
fs.copyFileSync(srcPath, dest);
|
|
22
|
+
return dest;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function pruneOldBackups() {
|
|
26
|
+
if (!fs.existsSync(BACKUPS_DIR)) return;
|
|
27
|
+
const entries = fs.readdirSync(BACKUPS_DIR, { withFileTypes: true })
|
|
28
|
+
.filter((e) => e.isDirectory())
|
|
29
|
+
.map((e) => ({ name: e.name, full: path.join(BACKUPS_DIR, e.name), mtime: fs.statSync(path.join(BACKUPS_DIR, e.name)).mtimeMs }))
|
|
30
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
31
|
+
|
|
32
|
+
for (const old of entries.slice(KEEP_BACKUPS)) {
|
|
33
|
+
try { fs.rmSync(old.full, { recursive: true, force: true }); } catch {}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { ensureDirs, DATA_FILE } from "./paths.js";
|
|
2
|
+
|
|
3
|
+
// Use global to survive Next.js dev hot-reload (module state resets on reload)
|
|
4
|
+
if (!global._dbAdapter) global._dbAdapter = { instance: null, initPromise: null, logged: false };
|
|
5
|
+
const state = global._dbAdapter;
|
|
6
|
+
|
|
7
|
+
async function tryBunSqlite() {
|
|
8
|
+
// Bun runtime only — built-in, no install needed
|
|
9
|
+
if (!process.versions.bun) return null;
|
|
10
|
+
try {
|
|
11
|
+
const { createBunSqliteAdapter } = await import("./adapters/bunSqliteAdapter.js");
|
|
12
|
+
return await createBunSqliteAdapter(DATA_FILE);
|
|
13
|
+
} catch (e) {
|
|
14
|
+
console.warn(`[DB] bun:sqlite unavailable: ${e.message}`);
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function tryBetterSqlite() {
|
|
20
|
+
// Skip on Bun — better-sqlite3 native bindings unsupported
|
|
21
|
+
if (process.versions.bun) return null;
|
|
22
|
+
try {
|
|
23
|
+
const { createBetterSqliteAdapter } = await import("./adapters/betterSqliteAdapter.js");
|
|
24
|
+
return createBetterSqliteAdapter(DATA_FILE);
|
|
25
|
+
} catch (e) {
|
|
26
|
+
console.warn(`[DB] better-sqlite3 unavailable: ${e.message}`);
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function tryNodeSqlite() {
|
|
32
|
+
// Built-in since Node 22.5.0 — no install needed. Skip under Bun (no node:sqlite).
|
|
33
|
+
if (process.versions.bun) return null;
|
|
34
|
+
const [maj, min] = process.versions.node.split(".").map(Number);
|
|
35
|
+
if (maj < 22 || (maj === 22 && min < 5)) return null;
|
|
36
|
+
try {
|
|
37
|
+
const { createNodeSqliteAdapter } = await import("./adapters/nodeSqliteAdapter.js");
|
|
38
|
+
return await createNodeSqliteAdapter(DATA_FILE);
|
|
39
|
+
} catch (e) {
|
|
40
|
+
console.warn(`[DB] node:sqlite unavailable: ${e.message}`);
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function trySqlJs() {
|
|
46
|
+
try {
|
|
47
|
+
const { createSqlJsAdapter } = await import("./adapters/sqljsAdapter.js");
|
|
48
|
+
return await createSqlJsAdapter(DATA_FILE);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
console.warn(`[DB] sql.js unavailable: ${e.message}`);
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function initAdapter() {
|
|
56
|
+
ensureDirs();
|
|
57
|
+
// Order per runtime:
|
|
58
|
+
// Bun: bun:sqlite → sql.js
|
|
59
|
+
// Node: better-sqlite3 → node:sqlite (≥22.5) → sql.js
|
|
60
|
+
let adapter = await tryBunSqlite();
|
|
61
|
+
if (!adapter) adapter = await tryBetterSqlite();
|
|
62
|
+
if (!adapter) adapter = await tryNodeSqlite();
|
|
63
|
+
if (!adapter) adapter = await trySqlJs();
|
|
64
|
+
if (!adapter) throw new Error("[DB] No SQLite driver available (bun/better/node/sql.js all failed)");
|
|
65
|
+
|
|
66
|
+
if (!state.logged) {
|
|
67
|
+
console.log(`[DB] Driver: ${adapter.driver} | file: ${DATA_FILE}`);
|
|
68
|
+
state.logged = true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const { runMigrationOnce } = await import("./migrate.js");
|
|
72
|
+
await runMigrationOnce(adapter);
|
|
73
|
+
return adapter;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function getAdapter() {
|
|
77
|
+
if (state.instance) return state.instance;
|
|
78
|
+
if (!state.initPromise) state.initPromise = initAdapter().then((a) => { state.instance = a; return a; });
|
|
79
|
+
return state.initPromise;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function getAdapterSync() {
|
|
83
|
+
if (!state.instance) throw new Error("[DB] adapter not initialized — await getAdapter() first");
|
|
84
|
+
return state.instance;
|
|
85
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function parseJson(str, fallback = null) {
|
|
2
|
+
if (str == null) return fallback;
|
|
3
|
+
if (typeof str !== "string") return str;
|
|
4
|
+
try { return JSON.parse(str); } catch { return fallback; }
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function stringifyJson(value) {
|
|
8
|
+
return JSON.stringify(value ?? null);
|
|
9
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { getAdapter } from "../driver.js";
|
|
2
|
+
import { parseJson, stringifyJson } from "./jsonCol.js";
|
|
3
|
+
|
|
4
|
+
export function makeKv(scope) {
|
|
5
|
+
return {
|
|
6
|
+
async get(key, fallback = null) {
|
|
7
|
+
const db = await getAdapter();
|
|
8
|
+
const row = db.get(`SELECT value FROM kv WHERE scope = ? AND key = ?`, [scope, key]);
|
|
9
|
+
return row ? parseJson(row.value, fallback) : fallback;
|
|
10
|
+
},
|
|
11
|
+
async getAll() {
|
|
12
|
+
const db = await getAdapter();
|
|
13
|
+
const rows = db.all(`SELECT key, value FROM kv WHERE scope = ?`, [scope]);
|
|
14
|
+
const out = {};
|
|
15
|
+
for (const r of rows) out[r.key] = parseJson(r.value);
|
|
16
|
+
return out;
|
|
17
|
+
},
|
|
18
|
+
async set(key, value) {
|
|
19
|
+
const db = await getAdapter();
|
|
20
|
+
db.run(`INSERT INTO kv(scope, key, value) VALUES(?, ?, ?) ON CONFLICT(scope, key) DO UPDATE SET value = excluded.value`, [scope, key, stringifyJson(value)]);
|
|
21
|
+
},
|
|
22
|
+
async setMany(obj) {
|
|
23
|
+
const db = await getAdapter();
|
|
24
|
+
db.transaction(() => {
|
|
25
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
26
|
+
db.run(`INSERT INTO kv(scope, key, value) VALUES(?, ?, ?) ON CONFLICT(scope, key) DO UPDATE SET value = excluded.value`, [scope, k, stringifyJson(v)]);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
async remove(key) {
|
|
31
|
+
const db = await getAdapter();
|
|
32
|
+
db.run(`DELETE FROM kv WHERE scope = ? AND key = ?`, [scope, key]);
|
|
33
|
+
},
|
|
34
|
+
async clear() {
|
|
35
|
+
const db = await getAdapter();
|
|
36
|
+
db.run(`DELETE FROM kv WHERE scope = ?`, [scope]);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getAdapter } from "../driver.js";
|
|
2
|
+
|
|
3
|
+
export async function getMeta(key, fallback = null) {
|
|
4
|
+
const db = await getAdapter();
|
|
5
|
+
const row = db.get(`SELECT value FROM _meta WHERE key = ?`, [key]);
|
|
6
|
+
return row ? row.value : fallback;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export async function setMeta(key, value) {
|
|
10
|
+
const db = await getAdapter();
|
|
11
|
+
db.run(`INSERT INTO _meta(key, value) VALUES(?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value`, [key, String(value)]);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Sync versions for use during migration (adapter passed directly)
|
|
15
|
+
export function getMetaSync(adapter, key, fallback = null) {
|
|
16
|
+
const row = adapter.get(`SELECT value FROM _meta WHERE key = ?`, [key]);
|
|
17
|
+
return row ? row.value : fallback;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function setMetaSync(adapter, key, value) {
|
|
21
|
+
adapter.run(`INSERT INTO _meta(key, value) VALUES(?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value`, [key, String(value)]);
|
|
22
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Public API barrel — all DB functions
|
|
2
|
+
import { getAdapter } from "./driver.js";
|
|
3
|
+
import { stringifyJson, parseJson } from "./helpers/jsonCol.js";
|
|
4
|
+
|
|
5
|
+
// Settings
|
|
6
|
+
export {
|
|
7
|
+
getSettings, updateSettings, isCloudEnabled, getCloudUrl, exportSettings,
|
|
8
|
+
} from "./repos/settingsRepo.js";
|
|
9
|
+
|
|
10
|
+
// Provider connections
|
|
11
|
+
export {
|
|
12
|
+
getProviderConnections, getProviderConnectionById,
|
|
13
|
+
createProviderConnection, updateProviderConnection,
|
|
14
|
+
deleteProviderConnection, deleteProviderConnectionsByProvider,
|
|
15
|
+
reorderProviderConnections, cleanupProviderConnections,
|
|
16
|
+
} from "./repos/connectionsRepo.js";
|
|
17
|
+
|
|
18
|
+
// Provider nodes
|
|
19
|
+
export {
|
|
20
|
+
getProviderNodes, getProviderNodeById,
|
|
21
|
+
createProviderNode, updateProviderNode, deleteProviderNode,
|
|
22
|
+
} from "./repos/nodesRepo.js";
|
|
23
|
+
|
|
24
|
+
// Proxy pools
|
|
25
|
+
export {
|
|
26
|
+
getProxyPools, getProxyPoolById,
|
|
27
|
+
createProxyPool, updateProxyPool, deleteProxyPool,
|
|
28
|
+
} from "./repos/proxyPoolsRepo.js";
|
|
29
|
+
|
|
30
|
+
// API keys
|
|
31
|
+
export {
|
|
32
|
+
getApiKeys, getApiKeyById, createApiKey, updateApiKey, deleteApiKey, validateApiKey,
|
|
33
|
+
} from "./repos/apiKeysRepo.js";
|
|
34
|
+
|
|
35
|
+
// Combos
|
|
36
|
+
export {
|
|
37
|
+
getCombos, getComboById, getComboByName,
|
|
38
|
+
createCombo, updateCombo, deleteCombo,
|
|
39
|
+
} from "./repos/combosRepo.js";
|
|
40
|
+
|
|
41
|
+
// Aliases (model + custom + mitm)
|
|
42
|
+
export {
|
|
43
|
+
getModelAliases, setModelAlias, deleteModelAlias,
|
|
44
|
+
getCustomModels, addCustomModel, deleteCustomModel,
|
|
45
|
+
getMitmAlias, setMitmAliasAll,
|
|
46
|
+
} from "./repos/aliasRepo.js";
|
|
47
|
+
|
|
48
|
+
// Pricing
|
|
49
|
+
export {
|
|
50
|
+
getPricing, getPricingForModel, updatePricing, resetPricing, resetAllPricing,
|
|
51
|
+
} from "./repos/pricingRepo.js";
|
|
52
|
+
|
|
53
|
+
// Disabled models
|
|
54
|
+
export {
|
|
55
|
+
getDisabledModels, getDisabledByProvider, disableModels, enableModels,
|
|
56
|
+
} from "./repos/disabledModelsRepo.js";
|
|
57
|
+
|
|
58
|
+
// Usage
|
|
59
|
+
export {
|
|
60
|
+
statsEmitter, trackPendingRequest, getActiveRequests,
|
|
61
|
+
saveRequestUsage, getUsageHistory, getUsageStats, getChartData,
|
|
62
|
+
appendRequestLog, getRecentLogs,
|
|
63
|
+
} from "./repos/usageRepo.js";
|
|
64
|
+
|
|
65
|
+
// Request details
|
|
66
|
+
export {
|
|
67
|
+
saveRequestDetail, getRequestDetails, getRequestDetailById,
|
|
68
|
+
} from "./repos/requestDetailsRepo.js";
|
|
69
|
+
|
|
70
|
+
// Export/import full DB
|
|
71
|
+
export async function exportDb() {
|
|
72
|
+
const db = await getAdapter();
|
|
73
|
+
const { exportSettings } = await import("./repos/settingsRepo.js");
|
|
74
|
+
|
|
75
|
+
const out = {
|
|
76
|
+
settings: await exportSettings(),
|
|
77
|
+
providerConnections: db.all(`SELECT * FROM providerConnections`).map((r) => ({ ...parseJson(r.data, {}), id: r.id, provider: r.provider, authType: r.authType, name: r.name, email: r.email, priority: r.priority, isActive: r.isActive === 1, createdAt: r.createdAt, updatedAt: r.updatedAt })),
|
|
78
|
+
providerNodes: db.all(`SELECT * FROM providerNodes`).map((r) => ({ ...parseJson(r.data, {}), id: r.id, type: r.type, name: r.name, createdAt: r.createdAt, updatedAt: r.updatedAt })),
|
|
79
|
+
proxyPools: db.all(`SELECT * FROM proxyPools`).map((r) => ({ ...parseJson(r.data, {}), id: r.id, isActive: r.isActive === 1, testStatus: r.testStatus, createdAt: r.createdAt, updatedAt: r.updatedAt })),
|
|
80
|
+
apiKeys: db.all(`SELECT * FROM apiKeys`).map((r) => ({ id: r.id, key: r.key, name: r.name, machineId: r.machineId, isActive: r.isActive === 1, createdAt: r.createdAt })),
|
|
81
|
+
combos: db.all(`SELECT * FROM combos`).map((r) => ({ id: r.id, name: r.name, kind: r.kind, models: parseJson(r.models, []), createdAt: r.createdAt, updatedAt: r.updatedAt })),
|
|
82
|
+
modelAliases: {},
|
|
83
|
+
customModels: [],
|
|
84
|
+
mitmAlias: {},
|
|
85
|
+
pricing: {},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
for (const r of db.all(`SELECT key, value FROM kv WHERE scope = 'modelAliases'`)) out.modelAliases[r.key] = parseJson(r.value);
|
|
89
|
+
for (const r of db.all(`SELECT key, value FROM kv WHERE scope = 'customModels'`)) out.customModels.push(parseJson(r.value));
|
|
90
|
+
for (const r of db.all(`SELECT key, value FROM kv WHERE scope = 'mitmAlias'`)) out.mitmAlias[r.key] = parseJson(r.value);
|
|
91
|
+
for (const r of db.all(`SELECT key, value FROM kv WHERE scope = 'pricing'`)) out.pricing[r.key] = parseJson(r.value);
|
|
92
|
+
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function importDb(payload) {
|
|
97
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
98
|
+
throw new Error("Invalid database payload");
|
|
99
|
+
}
|
|
100
|
+
const db = await getAdapter();
|
|
101
|
+
|
|
102
|
+
db.transaction(() => {
|
|
103
|
+
// Wipe all tables (keep _meta)
|
|
104
|
+
db.run(`DELETE FROM settings`);
|
|
105
|
+
db.run(`DELETE FROM providerConnections`);
|
|
106
|
+
db.run(`DELETE FROM providerNodes`);
|
|
107
|
+
db.run(`DELETE FROM proxyPools`);
|
|
108
|
+
db.run(`DELETE FROM apiKeys`);
|
|
109
|
+
db.run(`DELETE FROM combos`);
|
|
110
|
+
db.run(`DELETE FROM kv WHERE scope IN ('modelAliases', 'customModels', 'mitmAlias', 'pricing')`);
|
|
111
|
+
|
|
112
|
+
// Settings
|
|
113
|
+
if (payload.settings) {
|
|
114
|
+
db.run(`INSERT INTO settings(id, data) VALUES(1, ?) ON CONFLICT(id) DO UPDATE SET data = excluded.data`, [stringifyJson(payload.settings)]);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
for (const c of payload.providerConnections || []) {
|
|
118
|
+
const { id, provider, authType, name, email, priority, isActive, createdAt, updatedAt, ...rest } = c;
|
|
119
|
+
db.run(
|
|
120
|
+
`INSERT OR REPLACE INTO providerConnections(id, provider, authType, name, email, priority, isActive, data, createdAt, updatedAt) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
121
|
+
[id, provider, authType || "oauth", name || null, email || null, priority || null, isActive === false ? 0 : 1, stringifyJson(rest), createdAt || new Date().toISOString(), updatedAt || new Date().toISOString()]
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
for (const n of payload.providerNodes || []) {
|
|
125
|
+
const { id, type, name, createdAt, updatedAt, ...rest } = n;
|
|
126
|
+
db.run(
|
|
127
|
+
`INSERT OR REPLACE INTO providerNodes(id, type, name, data, createdAt, updatedAt) VALUES(?, ?, ?, ?, ?, ?)`,
|
|
128
|
+
[id, type || null, name || null, stringifyJson(rest), createdAt || new Date().toISOString(), updatedAt || new Date().toISOString()]
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
for (const p of payload.proxyPools || []) {
|
|
132
|
+
const { id, isActive, testStatus, createdAt, updatedAt, ...rest } = p;
|
|
133
|
+
db.run(
|
|
134
|
+
`INSERT OR REPLACE INTO proxyPools(id, isActive, testStatus, data, createdAt, updatedAt) VALUES(?, ?, ?, ?, ?, ?)`,
|
|
135
|
+
[id, isActive === false ? 0 : 1, testStatus || "unknown", stringifyJson(rest), createdAt || new Date().toISOString(), updatedAt || new Date().toISOString()]
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
for (const k of payload.apiKeys || []) {
|
|
139
|
+
db.run(
|
|
140
|
+
`INSERT OR REPLACE INTO apiKeys(id, key, name, machineId, isActive, createdAt) VALUES(?, ?, ?, ?, ?, ?)`,
|
|
141
|
+
[k.id, k.key, k.name || null, k.machineId || null, k.isActive === false ? 0 : 1, k.createdAt || new Date().toISOString()]
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
for (const c of payload.combos || []) {
|
|
145
|
+
db.run(
|
|
146
|
+
`INSERT OR REPLACE INTO combos(id, name, kind, models, createdAt, updatedAt) VALUES(?, ?, ?, ?, ?, ?)`,
|
|
147
|
+
[c.id, c.name, c.kind || null, stringifyJson(c.models || []), c.createdAt || new Date().toISOString(), c.updatedAt || new Date().toISOString()]
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
for (const [a, m] of Object.entries(payload.modelAliases || {})) {
|
|
151
|
+
db.run(`INSERT OR REPLACE INTO kv(scope, key, value) VALUES('modelAliases', ?, ?)`, [a, stringifyJson(m)]);
|
|
152
|
+
}
|
|
153
|
+
for (const m of payload.customModels || []) {
|
|
154
|
+
const k = `${m.providerAlias}|${m.id}|${m.type || "llm"}`;
|
|
155
|
+
db.run(`INSERT OR REPLACE INTO kv(scope, key, value) VALUES('customModels', ?, ?)`, [k, stringifyJson(m)]);
|
|
156
|
+
}
|
|
157
|
+
for (const [tool, mappings] of Object.entries(payload.mitmAlias || {})) {
|
|
158
|
+
db.run(`INSERT OR REPLACE INTO kv(scope, key, value) VALUES('mitmAlias', ?, ?)`, [tool, stringifyJson(mappings || {})]);
|
|
159
|
+
}
|
|
160
|
+
for (const [provider, models] of Object.entries(payload.pricing || {})) {
|
|
161
|
+
db.run(`INSERT OR REPLACE INTO kv(scope, key, value) VALUES('pricing', ?, ?)`, [provider, stringifyJson(models || {})]);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
return await exportDb();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Eager init helper (optional)
|
|
169
|
+
export async function initDb() {
|
|
170
|
+
await getAdapter();
|
|
171
|
+
}
|