9router/src/lib/localDb.js

1215 lines
30 KiB
JavaScript
Raw Normal View History

2026-01-04 21:58:59 -05:00
import { Low } from "lowdb";
import { JSONFile } from "lowdb/node";
import { v4 as uuidv4 } from "uuid";
import path from "node:path";
import os from "node:os";
import fs from "node:fs";
import lockfile from "proper-lockfile";
const DEFAULT_MITM_ROUTER_BASE = "http://localhost:20128";
const isCloud = typeof caches !== 'undefined' || typeof caches === 'object';
// Get app name - fixed constant to avoid Windows path issues in standalone build
2026-01-04 21:58:59 -05:00
function getAppName() {
return "9router";
2026-01-04 21:58:59 -05:00
}
// Get user data directory based on platform
function getUserDataDir() {
if (isCloud) return "/tmp"; // Fallback for Workers
if (process.env.DATA_DIR) return process.env.DATA_DIR;
2026-01-04 21:58:59 -05:00
const platform = process.platform;
const homeDir = os.homedir();
const appName = getAppName();
2026-01-04 21:58:59 -05:00
if (platform === "win32") {
return path.join(process.env.APPDATA || path.join(homeDir, "AppData", "Roaming"), appName);
} else {
// macOS & Linux: ~/.{appName}
return path.join(homeDir, `.${appName}`);
}
}
// Data file path - stored in user home directory
const DATA_DIR = getUserDataDir();
const DB_FILE = isCloud ? null : path.join(DATA_DIR, "db.json");
2026-01-04 21:58:59 -05:00
// Ensure data directory exists
if (!isCloud && !fs.existsSync(DATA_DIR)) {
2026-01-04 21:58:59 -05:00
fs.mkdirSync(DATA_DIR, { recursive: true });
}
// Default data structure
const defaultData = {
providerConnections: [],
providerNodes: [],
proxyPools: [],
2026-01-04 21:58:59 -05:00
modelAliases: {},
mitmAlias: {},
2026-01-04 21:58:59 -05:00
combos: [],
apiKeys: [],
settings: {
cloudEnabled: false,
tunnelEnabled: false,
tunnelUrl: "",
2026-04-10 07:16:14 -04:00
tailscaleEnabled: false,
tailscaleUrl: "",
stickyRoundRobinLimit: 3,
providerStrategies: {},
comboStrategy: "fallback",
comboStrategies: {},
Feature/ai observability dashboard (#79) * feat: add AI request details feature with latency tracking Add comprehensive request history and debugging capability to the Usage dashboard: **Storage Layer** (usageDb.js): - Add saveRequestDetail() for storing full request/response details - Implement FIFO queue with 1000-record limit in request-details.json - Auto-sanitize sensitive headers (authorization, api-key, cookie, token) - Add getRequestDetails() with pagination and filtering support - Add getRequestDetailById() for single record lookup **Pipeline Integration** (chatCore.js): - Track request start time and calculate total latency - Record TTFT (Time To First Token) and total latency for all requests - Capture full request details (messages, model, parameters) - Save response content for non-streaming, mark streaming responses - Handle error cases with detailed error information - Async non-blocking saves to avoid impacting request performance **API Layer** (/api/usage/request-details): - GET endpoint with pagination (page, pageSize: 1-100) - Filter by provider, model, connectionId, status, date range - Returns { details: [...], pagination: {...} } format **UI Components**: - Drawer.js: Right slide-out panel with backdrop blur and ESC close - Pagination.js: Full pagination with page size selector (10/20/50) - RequestDetailsTab.js: Complete table view with filters and detail drawer **Dashboard Integration**: - Add "Details" tab to Usage page (4th tab after Overview/Logger/Limits) - Table columns: Timestamp, Model, Provider, Input Tokens, Output Tokens, Latency (TTFT/Total), Action - Provider filter dropdown (9 providers supported) - Date range filters (start/end datetime) - Click "Detail" button to view full request/response JSON in slide-out drawer **Features**: - Real-time latency monitoring (TTFT & Total) - Complete request/response inspection for debugging - Filterable and searchable request history - Responsive design with mobile-friendly filters - Data security with automatic header sanitization - Performance: async saves don't block request pipeline **Files Created/Modified**: - src/lib/usageDb.js (modified) - open-sse/handlers/chatCore.js (modified) - src/app/api/usage/request-details/route.js (new) - src/shared/components/Drawer.js (new) - src/shared/components/Pagination.js (new) - src/app/(dashboard)/dashboard/usage/components/RequestDetailsTab.js (new) - src/app/(dashboard)/dashboard/usage/page.js (modified) Closes: AI Observability Dashboard feature * feat: enhance request details with full config and streaming content capture Improve Request Details feature to capture comprehensive request parameters and actual streaming response content: **Request Configuration Enhancement** (chatCore.js): - Add extractRequestConfig() helper function to capture all request parameters - Include temperature controls: temperature, top_p, top_k - Include token limits: max_tokens, max_completion_tokens - Include thinking/reasoning modes: thinking, reasoning, enable_thinking - Include OpenAI parameters: presence_penalty, frequency_penalty, seed, stop, tools, tool_choice, response_format, n, logprobs, top_logprobs, logit_bias, user, parallel_tool_calls, prediction, store, metadata - Apply to all request types: non-streaming, streaming, and error cases **Streaming Content Capture** (chatCore.js & stream.js): - Add onStreamComplete callback mechanism to stream processors - Accumulate content from all formats: OpenAI, Claude, Gemini - Track content from delta.content, delta.reasoning_content, delta.text, delta.thinking, and Gemini content.parts - Save initial record with "[Streaming in progress...]" marker - Update record with actual content when stream completes - Include usage tokens when available from stream **Files Modified**: - open-sse/handlers/chatCore.js - extractRequestConfig() + streaming capture - open-sse/utils/stream.js - onStreamComplete callback + content accumulation **Benefits**: - View complete request configuration in Request Details (thinking mode, etc.) - See actual streaming response content instead of placeholder - Better debugging and observability for AI requests Refs: #request-details-enhancement * feat: separate thinking/reasoning content from response content Improve Request Details to display thinking process separately from final response: **Backend Changes**: - stream.js: Capture content and thinking separately in streaming mode - Add accumulatedThinking variable alongside accumulatedContent - Route delta.content to content, delta.reasoning_content to thinking - Support OpenAI (reasoning_content), Claude (thinking), Gemini (part.thought) - Update onStreamComplete callback to return { content, thinking } object - chatCore.js: Update response structure to include thinking field - Non-streaming: Extract thinking from reasoning_content field - Streaming: Receive { content, thinking } from stream callback - Error responses: Include thinking: null - Initial streaming save: Include thinking: null **Frontend Changes**: - RequestDetailsTab.js: Display thinking and content in separate sections - Add amber/yellow themed "Thinking Process" section with psychology icon - Show "Final Response" label when thinking is present - Use distinct visual styling for thinking (amber bg) vs content (gray bg) - Only show thinking section when thinking content exists **Benefits**: - Users can clearly see model's reasoning process vs final answer - Better debugging for models with thinking capabilities (Claude, o1, etc.) - Visual distinction makes it easy to identify thinking vs response Refs: #thinking-content-separation * fix: map Claude thinking to reasoning_content field Fix Claude thinking content to be properly captured as reasoning_content instead of regular content, enabling separate display in Request Details: **Changes**: - claude-to-openai.js: Use reasoning_content field for thinking blocks - thinking start: send { reasoning_content: "" } instead of { content: "```\n```" } - thinking delta: map to reasoning_content instead of content - thinking stop: send { reasoning_content: "" } instead of { content: "```\n```" } **Why This Matters**: - Previously Claude thinking was sent as `content` field, mixed with actual response - Now thinking uses `reasoning_content` field, matching OpenAI's o1 format - stream.js can now properly route thinking to accumulatedThinking variable - Request Details UI will show Claude thinking in separate "Thinking Process" section **Supported Thinking Formats**: - OpenAI: delta.reasoning_content → thinking - Claude: delta.thinking → reasoning_content (now fixed) - Gemini: part.thought === true → thinking Refs: #claude-thinking-fix * feat(observability): capture and display full 4-layer request chain Capture complete request/response chain in AI Request Details: - Add providerRequest field (translated request sent to provider) - Add providerResponse field (raw provider response, streaming indicator) - Update chatCore.js at all 5 saveRequestDetail() call sites - Reorganize UI into 4 collapsible sections with Material icons - Preserve backward compatibility for old records - Add distinct styling for streaming indicator * fix(observability): resolve React duplicate key warning in request details table - Use composite key (detail.id + index) to ensure unique keys - Prevents React warnings when database contains duplicate IDs from old ID generation * fix(observability): display actual content in streaming request details Change providerResponse field for streaming requests from placeholder "[Streaming - raw response not captured]" to actual final content. This improves debugging experience by showing the real AI response in the "Provider Response (Raw)" section instead of a confusing placeholder message. Files changed: - open-sse/handlers/chatCore.js: Save contentObj.content to providerResponse - src/app/.../RequestDetailsTab.js: Remove special handling for placeholder * refactor(observability): migrate request details to SQLite for improved concurrency - Replace LowDB JSON storage with better-sqlite3 - Enable WAL mode for true concurrent read/write support - Add 5 indexes to accelerate queries (timestamp, provider, model, connection_id, status) - Perform pagination at the database level to reduce memory footprint - Maintain 1000 record limit with automatic cleanup of old data - Ensure API compatibility via re-exports, requiring no caller changes Performance improvements: - Concurrent Writes: Lock-free WAL mode prevents data contention - Query Efficiency: Index-based searches replace full dataset loading - Data Integrity: Atomic operations prevent file corruption * fix(observability): resolve pagination statistics display issues - Fix issue where totalItems=0 showed 'Showing 1 to 0 of 0 results' - Hide pagination controls when totalItems=0 or totalPages<=1 - Standardize API response fields: pagination.total -> pagination.totalItems Before: Incorrect stats shown for empty data, and pager visible even for single-page results After: Stats hidden for empty data, pager hidden when navigation is unnecessary * feat(observability): display friendly provider names in request details - Add /api/usage/providers endpoint to dynamically fetch provider list with names - Replace hardcoded provider options with dynamic loading from database - Display friendly provider names instead of IDs in both table and detail drawer - Support custom provider nodes (e.g., OpenAI-compatible) with user-defined names - Add provider name caching to optimize performance * fix(observability): use INSERT OR REPLACE for request details to handle streaming updates * fix(observability): resolve zero-token display issue by ensuring streaming usage capture and fixing key mismatch * fix(observability): separate TTFT and total latency calculation for streaming requests * feat(observability): implement SQLite write queue and JSON size limits - Added in-memory buffer and batch writing for SQLite to prevent lock contention - Implemented with configurable 1MB limit to prevent DB bloat - Added dashboard UI for observability performance and data management settings - Integrated graceful shutdown handlers to prevent data loss * fix(observability): resolve ReferenceError by declaring dbInstance
2026-02-08 22:30:42 -05:00
requireLogin: true,
tunnelDashboardAccess: true,
observabilityEnabled: true,
Feature/ai observability dashboard (#79) * feat: add AI request details feature with latency tracking Add comprehensive request history and debugging capability to the Usage dashboard: **Storage Layer** (usageDb.js): - Add saveRequestDetail() for storing full request/response details - Implement FIFO queue with 1000-record limit in request-details.json - Auto-sanitize sensitive headers (authorization, api-key, cookie, token) - Add getRequestDetails() with pagination and filtering support - Add getRequestDetailById() for single record lookup **Pipeline Integration** (chatCore.js): - Track request start time and calculate total latency - Record TTFT (Time To First Token) and total latency for all requests - Capture full request details (messages, model, parameters) - Save response content for non-streaming, mark streaming responses - Handle error cases with detailed error information - Async non-blocking saves to avoid impacting request performance **API Layer** (/api/usage/request-details): - GET endpoint with pagination (page, pageSize: 1-100) - Filter by provider, model, connectionId, status, date range - Returns { details: [...], pagination: {...} } format **UI Components**: - Drawer.js: Right slide-out panel with backdrop blur and ESC close - Pagination.js: Full pagination with page size selector (10/20/50) - RequestDetailsTab.js: Complete table view with filters and detail drawer **Dashboard Integration**: - Add "Details" tab to Usage page (4th tab after Overview/Logger/Limits) - Table columns: Timestamp, Model, Provider, Input Tokens, Output Tokens, Latency (TTFT/Total), Action - Provider filter dropdown (9 providers supported) - Date range filters (start/end datetime) - Click "Detail" button to view full request/response JSON in slide-out drawer **Features**: - Real-time latency monitoring (TTFT & Total) - Complete request/response inspection for debugging - Filterable and searchable request history - Responsive design with mobile-friendly filters - Data security with automatic header sanitization - Performance: async saves don't block request pipeline **Files Created/Modified**: - src/lib/usageDb.js (modified) - open-sse/handlers/chatCore.js (modified) - src/app/api/usage/request-details/route.js (new) - src/shared/components/Drawer.js (new) - src/shared/components/Pagination.js (new) - src/app/(dashboard)/dashboard/usage/components/RequestDetailsTab.js (new) - src/app/(dashboard)/dashboard/usage/page.js (modified) Closes: AI Observability Dashboard feature * feat: enhance request details with full config and streaming content capture Improve Request Details feature to capture comprehensive request parameters and actual streaming response content: **Request Configuration Enhancement** (chatCore.js): - Add extractRequestConfig() helper function to capture all request parameters - Include temperature controls: temperature, top_p, top_k - Include token limits: max_tokens, max_completion_tokens - Include thinking/reasoning modes: thinking, reasoning, enable_thinking - Include OpenAI parameters: presence_penalty, frequency_penalty, seed, stop, tools, tool_choice, response_format, n, logprobs, top_logprobs, logit_bias, user, parallel_tool_calls, prediction, store, metadata - Apply to all request types: non-streaming, streaming, and error cases **Streaming Content Capture** (chatCore.js & stream.js): - Add onStreamComplete callback mechanism to stream processors - Accumulate content from all formats: OpenAI, Claude, Gemini - Track content from delta.content, delta.reasoning_content, delta.text, delta.thinking, and Gemini content.parts - Save initial record with "[Streaming in progress...]" marker - Update record with actual content when stream completes - Include usage tokens when available from stream **Files Modified**: - open-sse/handlers/chatCore.js - extractRequestConfig() + streaming capture - open-sse/utils/stream.js - onStreamComplete callback + content accumulation **Benefits**: - View complete request configuration in Request Details (thinking mode, etc.) - See actual streaming response content instead of placeholder - Better debugging and observability for AI requests Refs: #request-details-enhancement * feat: separate thinking/reasoning content from response content Improve Request Details to display thinking process separately from final response: **Backend Changes**: - stream.js: Capture content and thinking separately in streaming mode - Add accumulatedThinking variable alongside accumulatedContent - Route delta.content to content, delta.reasoning_content to thinking - Support OpenAI (reasoning_content), Claude (thinking), Gemini (part.thought) - Update onStreamComplete callback to return { content, thinking } object - chatCore.js: Update response structure to include thinking field - Non-streaming: Extract thinking from reasoning_content field - Streaming: Receive { content, thinking } from stream callback - Error responses: Include thinking: null - Initial streaming save: Include thinking: null **Frontend Changes**: - RequestDetailsTab.js: Display thinking and content in separate sections - Add amber/yellow themed "Thinking Process" section with psychology icon - Show "Final Response" label when thinking is present - Use distinct visual styling for thinking (amber bg) vs content (gray bg) - Only show thinking section when thinking content exists **Benefits**: - Users can clearly see model's reasoning process vs final answer - Better debugging for models with thinking capabilities (Claude, o1, etc.) - Visual distinction makes it easy to identify thinking vs response Refs: #thinking-content-separation * fix: map Claude thinking to reasoning_content field Fix Claude thinking content to be properly captured as reasoning_content instead of regular content, enabling separate display in Request Details: **Changes**: - claude-to-openai.js: Use reasoning_content field for thinking blocks - thinking start: send { reasoning_content: "" } instead of { content: "```\n```" } - thinking delta: map to reasoning_content instead of content - thinking stop: send { reasoning_content: "" } instead of { content: "```\n```" } **Why This Matters**: - Previously Claude thinking was sent as `content` field, mixed with actual response - Now thinking uses `reasoning_content` field, matching OpenAI's o1 format - stream.js can now properly route thinking to accumulatedThinking variable - Request Details UI will show Claude thinking in separate "Thinking Process" section **Supported Thinking Formats**: - OpenAI: delta.reasoning_content → thinking - Claude: delta.thinking → reasoning_content (now fixed) - Gemini: part.thought === true → thinking Refs: #claude-thinking-fix * feat(observability): capture and display full 4-layer request chain Capture complete request/response chain in AI Request Details: - Add providerRequest field (translated request sent to provider) - Add providerResponse field (raw provider response, streaming indicator) - Update chatCore.js at all 5 saveRequestDetail() call sites - Reorganize UI into 4 collapsible sections with Material icons - Preserve backward compatibility for old records - Add distinct styling for streaming indicator * fix(observability): resolve React duplicate key warning in request details table - Use composite key (detail.id + index) to ensure unique keys - Prevents React warnings when database contains duplicate IDs from old ID generation * fix(observability): display actual content in streaming request details Change providerResponse field for streaming requests from placeholder "[Streaming - raw response not captured]" to actual final content. This improves debugging experience by showing the real AI response in the "Provider Response (Raw)" section instead of a confusing placeholder message. Files changed: - open-sse/handlers/chatCore.js: Save contentObj.content to providerResponse - src/app/.../RequestDetailsTab.js: Remove special handling for placeholder * refactor(observability): migrate request details to SQLite for improved concurrency - Replace LowDB JSON storage with better-sqlite3 - Enable WAL mode for true concurrent read/write support - Add 5 indexes to accelerate queries (timestamp, provider, model, connection_id, status) - Perform pagination at the database level to reduce memory footprint - Maintain 1000 record limit with automatic cleanup of old data - Ensure API compatibility via re-exports, requiring no caller changes Performance improvements: - Concurrent Writes: Lock-free WAL mode prevents data contention - Query Efficiency: Index-based searches replace full dataset loading - Data Integrity: Atomic operations prevent file corruption * fix(observability): resolve pagination statistics display issues - Fix issue where totalItems=0 showed 'Showing 1 to 0 of 0 results' - Hide pagination controls when totalItems=0 or totalPages<=1 - Standardize API response fields: pagination.total -> pagination.totalItems Before: Incorrect stats shown for empty data, and pager visible even for single-page results After: Stats hidden for empty data, pager hidden when navigation is unnecessary * feat(observability): display friendly provider names in request details - Add /api/usage/providers endpoint to dynamically fetch provider list with names - Replace hardcoded provider options with dynamic loading from database - Display friendly provider names instead of IDs in both table and detail drawer - Support custom provider nodes (e.g., OpenAI-compatible) with user-defined names - Add provider name caching to optimize performance * fix(observability): use INSERT OR REPLACE for request details to handle streaming updates * fix(observability): resolve zero-token display issue by ensuring streaming usage capture and fixing key mismatch * fix(observability): separate TTFT and total latency calculation for streaming requests * feat(observability): implement SQLite write queue and JSON size limits - Added in-memory buffer and batch writing for SQLite to prevent lock contention - Implemented with configurable 1MB limit to prevent DB bloat - Added dashboard UI for observability performance and data management settings - Integrated graceful shutdown handlers to prevent data loss * fix(observability): resolve ReferenceError by declaring dbInstance
2026-02-08 22:30:42 -05:00
observabilityMaxRecords: 1000,
observabilityBatchSize: 20,
observabilityFlushIntervalMs: 5000,
observabilityMaxJsonSize: 1024,
outboundProxyEnabled: false,
outboundProxyUrl: "",
outboundNoProxy: "",
mitmRouterBaseUrl: DEFAULT_MITM_ROUTER_BASE,
},
pricing: {} // NEW: pricing configuration
2026-01-04 21:58:59 -05:00
};
// Seed db.json with defaults on first run so proper-lockfile never hits ENOENT
if (!isCloud && DB_FILE && !fs.existsSync(DB_FILE)) {
fs.writeFileSync(DB_FILE, JSON.stringify(defaultData, null, 2));
}
function cloneDefaultData() {
return {
providerConnections: [],
providerNodes: [],
proxyPools: [],
modelAliases: {},
mitmAlias: {},
combos: [],
apiKeys: [],
settings: {
cloudEnabled: false,
tunnelEnabled: false,
tunnelUrl: "",
2026-04-10 07:16:14 -04:00
tunnelProvider: "cloudflare",
stickyRoundRobinLimit: 3,
providerStrategies: {},
comboStrategy: "fallback",
comboStrategies: {},
requireLogin: true,
tunnelDashboardAccess: true,
observabilityEnabled: true,
Feature/ai observability dashboard (#79) * feat: add AI request details feature with latency tracking Add comprehensive request history and debugging capability to the Usage dashboard: **Storage Layer** (usageDb.js): - Add saveRequestDetail() for storing full request/response details - Implement FIFO queue with 1000-record limit in request-details.json - Auto-sanitize sensitive headers (authorization, api-key, cookie, token) - Add getRequestDetails() with pagination and filtering support - Add getRequestDetailById() for single record lookup **Pipeline Integration** (chatCore.js): - Track request start time and calculate total latency - Record TTFT (Time To First Token) and total latency for all requests - Capture full request details (messages, model, parameters) - Save response content for non-streaming, mark streaming responses - Handle error cases with detailed error information - Async non-blocking saves to avoid impacting request performance **API Layer** (/api/usage/request-details): - GET endpoint with pagination (page, pageSize: 1-100) - Filter by provider, model, connectionId, status, date range - Returns { details: [...], pagination: {...} } format **UI Components**: - Drawer.js: Right slide-out panel with backdrop blur and ESC close - Pagination.js: Full pagination with page size selector (10/20/50) - RequestDetailsTab.js: Complete table view with filters and detail drawer **Dashboard Integration**: - Add "Details" tab to Usage page (4th tab after Overview/Logger/Limits) - Table columns: Timestamp, Model, Provider, Input Tokens, Output Tokens, Latency (TTFT/Total), Action - Provider filter dropdown (9 providers supported) - Date range filters (start/end datetime) - Click "Detail" button to view full request/response JSON in slide-out drawer **Features**: - Real-time latency monitoring (TTFT & Total) - Complete request/response inspection for debugging - Filterable and searchable request history - Responsive design with mobile-friendly filters - Data security with automatic header sanitization - Performance: async saves don't block request pipeline **Files Created/Modified**: - src/lib/usageDb.js (modified) - open-sse/handlers/chatCore.js (modified) - src/app/api/usage/request-details/route.js (new) - src/shared/components/Drawer.js (new) - src/shared/components/Pagination.js (new) - src/app/(dashboard)/dashboard/usage/components/RequestDetailsTab.js (new) - src/app/(dashboard)/dashboard/usage/page.js (modified) Closes: AI Observability Dashboard feature * feat: enhance request details with full config and streaming content capture Improve Request Details feature to capture comprehensive request parameters and actual streaming response content: **Request Configuration Enhancement** (chatCore.js): - Add extractRequestConfig() helper function to capture all request parameters - Include temperature controls: temperature, top_p, top_k - Include token limits: max_tokens, max_completion_tokens - Include thinking/reasoning modes: thinking, reasoning, enable_thinking - Include OpenAI parameters: presence_penalty, frequency_penalty, seed, stop, tools, tool_choice, response_format, n, logprobs, top_logprobs, logit_bias, user, parallel_tool_calls, prediction, store, metadata - Apply to all request types: non-streaming, streaming, and error cases **Streaming Content Capture** (chatCore.js & stream.js): - Add onStreamComplete callback mechanism to stream processors - Accumulate content from all formats: OpenAI, Claude, Gemini - Track content from delta.content, delta.reasoning_content, delta.text, delta.thinking, and Gemini content.parts - Save initial record with "[Streaming in progress...]" marker - Update record with actual content when stream completes - Include usage tokens when available from stream **Files Modified**: - open-sse/handlers/chatCore.js - extractRequestConfig() + streaming capture - open-sse/utils/stream.js - onStreamComplete callback + content accumulation **Benefits**: - View complete request configuration in Request Details (thinking mode, etc.) - See actual streaming response content instead of placeholder - Better debugging and observability for AI requests Refs: #request-details-enhancement * feat: separate thinking/reasoning content from response content Improve Request Details to display thinking process separately from final response: **Backend Changes**: - stream.js: Capture content and thinking separately in streaming mode - Add accumulatedThinking variable alongside accumulatedContent - Route delta.content to content, delta.reasoning_content to thinking - Support OpenAI (reasoning_content), Claude (thinking), Gemini (part.thought) - Update onStreamComplete callback to return { content, thinking } object - chatCore.js: Update response structure to include thinking field - Non-streaming: Extract thinking from reasoning_content field - Streaming: Receive { content, thinking } from stream callback - Error responses: Include thinking: null - Initial streaming save: Include thinking: null **Frontend Changes**: - RequestDetailsTab.js: Display thinking and content in separate sections - Add amber/yellow themed "Thinking Process" section with psychology icon - Show "Final Response" label when thinking is present - Use distinct visual styling for thinking (amber bg) vs content (gray bg) - Only show thinking section when thinking content exists **Benefits**: - Users can clearly see model's reasoning process vs final answer - Better debugging for models with thinking capabilities (Claude, o1, etc.) - Visual distinction makes it easy to identify thinking vs response Refs: #thinking-content-separation * fix: map Claude thinking to reasoning_content field Fix Claude thinking content to be properly captured as reasoning_content instead of regular content, enabling separate display in Request Details: **Changes**: - claude-to-openai.js: Use reasoning_content field for thinking blocks - thinking start: send { reasoning_content: "" } instead of { content: "```\n```" } - thinking delta: map to reasoning_content instead of content - thinking stop: send { reasoning_content: "" } instead of { content: "```\n```" } **Why This Matters**: - Previously Claude thinking was sent as `content` field, mixed with actual response - Now thinking uses `reasoning_content` field, matching OpenAI's o1 format - stream.js can now properly route thinking to accumulatedThinking variable - Request Details UI will show Claude thinking in separate "Thinking Process" section **Supported Thinking Formats**: - OpenAI: delta.reasoning_content → thinking - Claude: delta.thinking → reasoning_content (now fixed) - Gemini: part.thought === true → thinking Refs: #claude-thinking-fix * feat(observability): capture and display full 4-layer request chain Capture complete request/response chain in AI Request Details: - Add providerRequest field (translated request sent to provider) - Add providerResponse field (raw provider response, streaming indicator) - Update chatCore.js at all 5 saveRequestDetail() call sites - Reorganize UI into 4 collapsible sections with Material icons - Preserve backward compatibility for old records - Add distinct styling for streaming indicator * fix(observability): resolve React duplicate key warning in request details table - Use composite key (detail.id + index) to ensure unique keys - Prevents React warnings when database contains duplicate IDs from old ID generation * fix(observability): display actual content in streaming request details Change providerResponse field for streaming requests from placeholder "[Streaming - raw response not captured]" to actual final content. This improves debugging experience by showing the real AI response in the "Provider Response (Raw)" section instead of a confusing placeholder message. Files changed: - open-sse/handlers/chatCore.js: Save contentObj.content to providerResponse - src/app/.../RequestDetailsTab.js: Remove special handling for placeholder * refactor(observability): migrate request details to SQLite for improved concurrency - Replace LowDB JSON storage with better-sqlite3 - Enable WAL mode for true concurrent read/write support - Add 5 indexes to accelerate queries (timestamp, provider, model, connection_id, status) - Perform pagination at the database level to reduce memory footprint - Maintain 1000 record limit with automatic cleanup of old data - Ensure API compatibility via re-exports, requiring no caller changes Performance improvements: - Concurrent Writes: Lock-free WAL mode prevents data contention - Query Efficiency: Index-based searches replace full dataset loading - Data Integrity: Atomic operations prevent file corruption * fix(observability): resolve pagination statistics display issues - Fix issue where totalItems=0 showed 'Showing 1 to 0 of 0 results' - Hide pagination controls when totalItems=0 or totalPages<=1 - Standardize API response fields: pagination.total -> pagination.totalItems Before: Incorrect stats shown for empty data, and pager visible even for single-page results After: Stats hidden for empty data, pager hidden when navigation is unnecessary * feat(observability): display friendly provider names in request details - Add /api/usage/providers endpoint to dynamically fetch provider list with names - Replace hardcoded provider options with dynamic loading from database - Display friendly provider names instead of IDs in both table and detail drawer - Support custom provider nodes (e.g., OpenAI-compatible) with user-defined names - Add provider name caching to optimize performance * fix(observability): use INSERT OR REPLACE for request details to handle streaming updates * fix(observability): resolve zero-token display issue by ensuring streaming usage capture and fixing key mismatch * fix(observability): separate TTFT and total latency calculation for streaming requests * feat(observability): implement SQLite write queue and JSON size limits - Added in-memory buffer and batch writing for SQLite to prevent lock contention - Implemented with configurable 1MB limit to prevent DB bloat - Added dashboard UI for observability performance and data management settings - Integrated graceful shutdown handlers to prevent data loss * fix(observability): resolve ReferenceError by declaring dbInstance
2026-02-08 22:30:42 -05:00
observabilityMaxRecords: 1000,
observabilityBatchSize: 20,
observabilityFlushIntervalMs: 5000,
observabilityMaxJsonSize: 1024,
outboundProxyEnabled: false,
outboundProxyUrl: "",
outboundNoProxy: "",
mitmRouterBaseUrl: DEFAULT_MITM_ROUTER_BASE,
},
pricing: {},
};
}
function ensureDbShape(data) {
const defaults = cloneDefaultData();
const next = data && typeof data === "object" ? data : {};
let changed = false;
for (const [key, defaultValue] of Object.entries(defaults)) {
if (next[key] === undefined || next[key] === null) {
next[key] = defaultValue;
changed = true;
continue;
}
if (
key === "settings" &&
(typeof next.settings !== "object" || Array.isArray(next.settings))
) {
next.settings = { ...defaultValue };
changed = true;
continue;
}
if (
key === "settings" &&
typeof next.settings === "object" &&
!Array.isArray(next.settings)
) {
for (const [settingKey, settingDefault] of Object.entries(defaultValue)) {
if (next.settings[settingKey] === undefined) {
// Backward-compat: if users previously saved a proxy URL,
// default to enabled so behavior doesn't silently change.
if (
settingKey === "outboundProxyEnabled" &&
typeof next.settings.outboundProxyUrl === "string" &&
next.settings.outboundProxyUrl.trim()
) {
next.settings.outboundProxyEnabled = true;
} else {
next.settings[settingKey] = settingDefault;
}
changed = true;
}
}
}
// Migrate existing API keys to have isActive
if (key === "apiKeys" && Array.isArray(next.apiKeys)) {
for (const apiKey of next.apiKeys) {
if (apiKey.isActive === undefined || apiKey.isActive === null) {
apiKey.isActive = true;
changed = true;
}
}
}
}
return { data: next, changed };
}
2026-01-04 21:58:59 -05:00
// Singleton instance
let dbInstance = null;
// Lock options for proper-lockfile (increased retries for multi-process robustness)
const LOCK_OPTIONS = {
retries: {
retries: 15,
minTimeout: 50,
maxTimeout: 3000,
},
stale: 10000, // Consider lock stale after 10s
};
// In-process mutex to serialize DB access within the same process
// This prevents ELOCKED when concurrent requests in the same process try to acquire the file lock
class LocalMutex {
constructor() {
this._queue = [];
this._locked = false;
}
async acquire() {
if (!this._locked) {
this._locked = true;
return () => this._release();
}
return new Promise((resolve) => {
this._queue.push(resolve);
}).then(() => () => this._release());
}
_release() {
const next = this._queue.shift();
if (next) {
next();
} else {
this._locked = false;
}
}
}
// Singleton local mutex for in-process serialization
const localMutex = new LocalMutex();
/**
* Safely read database with file locking
* Uses local mutex first to serialize within-process access, then file lock for cross-process
*/
async function safeRead(db) {
if (isCloud) {
await db.read();
return;
}
// Acquire local mutex first (in-process serialization)
const releaseLocal = await localMutex.acquire();
let release = null;
try {
// Acquire file lock (cross-process serialization)
release = await lockfile.lock(DB_FILE, LOCK_OPTIONS);
await db.read();
} catch (error) {
if (error.code === "ELOCKED") {
console.warn("[DB] File is locked, retrying read...");
throw error;
}
throw error;
} finally {
if (release) {
try {
await release();
} catch (err) {
// Ignore unlock errors
}
}
releaseLocal();
}
}
/**
* Safely write database with file locking
* Uses local mutex first to serialize within-process access, then file lock for cross-process
*/
async function safeWrite(db) {
if (isCloud) {
await db.write();
return;
}
// Acquire local mutex first (in-process serialization)
const releaseLocal = await localMutex.acquire();
let release = null;
try {
// Acquire file lock (cross-process serialization)
release = await lockfile.lock(DB_FILE, LOCK_OPTIONS);
await db.write();
} catch (error) {
if (error.code === "ELOCKED") {
console.warn("[DB] File is locked, retrying write...");
throw error;
}
throw error;
} finally {
if (release) {
try {
await release();
} catch (err) {
// Ignore unlock errors
}
}
releaseLocal();
}
}
2026-01-04 21:58:59 -05:00
/**
* Get database instance (singleton)
2026-01-04 21:58:59 -05:00
*/
export async function getDb() {
if (isCloud) {
// Return in-memory DB for Workers
if (!dbInstance) {
const data = cloneDefaultData();
dbInstance = new Low({ read: async () => { }, write: async () => { } }, data);
dbInstance.data = data;
}
return dbInstance;
}
if (!dbInstance) {
const adapter = new JSONFile(DB_FILE);
dbInstance = new Low(adapter, cloneDefaultData());
}
// Always read latest disk state to avoid stale singleton data across route workers.
try {
await safeRead(dbInstance);
} catch (error) {
if (error instanceof SyntaxError) {
console.warn('[DB] Corrupt JSON detected, resetting to defaults...');
dbInstance.data = cloneDefaultData();
await safeWrite(dbInstance);
} else {
throw error;
2026-01-04 21:58:59 -05:00
}
}
// Initialize/migrate missing keys for older DB schema versions.
if (!dbInstance.data) {
dbInstance.data = cloneDefaultData();
await safeWrite(dbInstance);
} else {
const { data, changed } = ensureDbShape(dbInstance.data);
dbInstance.data = data;
if (changed) {
await safeWrite(dbInstance);
}
}
return dbInstance;
2026-01-04 21:58:59 -05:00
}
// ============ Provider Connections ============
/**
* Get all provider connections
*/
export async function getProviderConnections(filter = {}) {
const db = await getDb();
let connections = db.data.providerConnections || [];
2026-01-04 21:58:59 -05:00
if (filter.provider) {
connections = connections.filter(c => c.provider === filter.provider);
}
if (filter.isActive !== undefined) {
connections = connections.filter(c => c.isActive === filter.isActive);
}
2026-01-04 21:58:59 -05:00
// Sort by priority (lower = higher priority)
connections.sort((a, b) => (a.priority || 999) - (b.priority || 999));
2026-01-04 21:58:59 -05:00
return connections;
}
// ============ Provider Nodes ============
/**
* Get provider nodes
*/
export async function getProviderNodes(filter = {}) {
const db = await getDb();
let nodes = db.data.providerNodes || [];
if (filter.type) {
nodes = nodes.filter((node) => node.type === filter.type);
}
return nodes;
}
/**
* Get provider node by ID
*/
export async function getProviderNodeById(id) {
const db = await getDb();
return db.data.providerNodes.find((node) => node.id === id) || null;
}
/**
* Create provider node
*/
export async function createProviderNode(data) {
const db = await getDb();
// Initialize providerNodes if undefined (backward compatibility)
if (!db.data.providerNodes) {
db.data.providerNodes = [];
}
const now = new Date().toISOString();
const node = {
id: data.id || uuidv4(),
type: data.type,
name: data.name,
prefix: data.prefix,
apiType: data.apiType,
baseUrl: data.baseUrl,
createdAt: now,
updatedAt: now,
};
db.data.providerNodes.push(node);
await safeWrite(db);
return node;
}
/**
* Update provider node
*/
export async function updateProviderNode(id, data) {
const db = await getDb();
if (!db.data.providerNodes) {
db.data.providerNodes = [];
}
const index = db.data.providerNodes.findIndex((node) => node.id === id);
if (index === -1) return null;
db.data.providerNodes[index] = {
...db.data.providerNodes[index],
...data,
updatedAt: new Date().toISOString(),
};
await safeWrite(db);
return db.data.providerNodes[index];
}
/**
* Delete provider node
*/
export async function deleteProviderNode(id) {
const db = await getDb();
if (!db.data.providerNodes) {
db.data.providerNodes = [];
}
const index = db.data.providerNodes.findIndex((node) => node.id === id);
if (index === -1) return null;
const [removed] = db.data.providerNodes.splice(index, 1);
await safeWrite(db);
return removed;
}
// ============ Proxy Pools ============
/**
* Get proxy pools
*/
export async function getProxyPools(filter = {}) {
const db = await getDb();
let pools = db.data.proxyPools || [];
if (filter.isActive !== undefined) {
pools = pools.filter((pool) => pool.isActive === filter.isActive);
}
if (filter.testStatus) {
pools = pools.filter((pool) => pool.testStatus === filter.testStatus);
}
return pools.sort((a, b) => new Date(b.updatedAt || 0) - new Date(a.updatedAt || 0));
}
/**
* Get proxy pool by ID
*/
export async function getProxyPoolById(id) {
const db = await getDb();
return (db.data.proxyPools || []).find((pool) => pool.id === id) || null;
}
/**
* Create proxy pool
*/
export async function createProxyPool(data) {
const db = await getDb();
if (!db.data.proxyPools) {
db.data.proxyPools = [];
}
const now = new Date().toISOString();
const pool = {
id: data.id || uuidv4(),
name: data.name,
proxyUrl: data.proxyUrl,
noProxy: data.noProxy || "",
isActive: data.isActive !== undefined ? data.isActive : true,
strictProxy: data.strictProxy === true,
testStatus: data.testStatus || "unknown",
lastTestedAt: data.lastTestedAt || null,
lastError: data.lastError || null,
createdAt: now,
updatedAt: now,
};
db.data.proxyPools.push(pool);
await safeWrite(db);
return pool;
}
/**
* Update proxy pool
*/
export async function updateProxyPool(id, data) {
const db = await getDb();
if (!db.data.proxyPools) {
db.data.proxyPools = [];
}
const index = db.data.proxyPools.findIndex((pool) => pool.id === id);
if (index === -1) return null;
db.data.proxyPools[index] = {
...db.data.proxyPools[index],
...data,
updatedAt: new Date().toISOString(),
};
await safeWrite(db);
return db.data.proxyPools[index];
}
/**
* Delete proxy pool
*/
export async function deleteProxyPool(id) {
const db = await getDb();
if (!db.data.proxyPools) {
db.data.proxyPools = [];
}
const index = db.data.proxyPools.findIndex((pool) => pool.id === id);
if (index === -1) return null;
const [removed] = db.data.proxyPools.splice(index, 1);
await safeWrite(db);
return removed;
}
/**
* Delete all provider connections by provider ID
*/
export async function deleteProviderConnectionsByProvider(providerId) {
const db = await getDb();
const beforeCount = db.data.providerConnections.length;
db.data.providerConnections = db.data.providerConnections.filter(
(connection) => connection.provider !== providerId
);
const deletedCount = beforeCount - db.data.providerConnections.length;
await safeWrite(db);
return deletedCount;
}
2026-01-04 21:58:59 -05:00
/**
* Get provider connection by ID
*/
export async function getProviderConnectionById(id) {
const db = await getDb();
return db.data.providerConnections.find(c => c.id === id) || null;
}
/**
* Create or update provider connection (upsert by provider + email/name)
*/
export async function createProviderConnection(data) {
const db = await getDb();
const now = new Date().toISOString();
2026-01-04 21:58:59 -05:00
// Check for existing connection with same provider and email (for OAuth)
// or same provider and name (for API key)
let existingIndex = -1;
if (data.authType === "oauth" && data.email) {
existingIndex = db.data.providerConnections.findIndex(
c => c.provider === data.provider && c.authType === "oauth" && c.email === data.email
);
} else if (data.authType === "apikey" && data.name) {
existingIndex = db.data.providerConnections.findIndex(
c => c.provider === data.provider && c.authType === "apikey" && c.name === data.name
);
}
2026-01-04 21:58:59 -05:00
// If exists, update instead of create
if (existingIndex !== -1) {
db.data.providerConnections[existingIndex] = {
...db.data.providerConnections[existingIndex],
...data,
updatedAt: now,
};
await safeWrite(db);
2026-01-04 21:58:59 -05:00
return db.data.providerConnections[existingIndex];
}
2026-01-04 21:58:59 -05:00
// Generate name for OAuth if not provided
let connectionName = data.name || null;
if (!connectionName && data.authType === "oauth") {
if (data.email) {
connectionName = data.email;
} else {
// Count existing connections for this provider to generate index
const existingCount = db.data.providerConnections.filter(
c => c.provider === data.provider
).length;
connectionName = `Account ${existingCount + 1}`;
}
}
// Auto-increment priority if not provided
let connectionPriority = data.priority;
if (!connectionPriority) {
const providerConnections = db.data.providerConnections.filter(
c => c.provider === data.provider
);
const maxPriority = providerConnections.reduce((max, c) => Math.max(max, c.priority || 0), 0);
connectionPriority = maxPriority + 1;
}
2026-01-04 21:58:59 -05:00
// Create new connection - only save fields with actual values
const connection = {
id: uuidv4(),
provider: data.provider,
authType: data.authType || "oauth",
name: connectionName,
priority: connectionPriority,
isActive: data.isActive !== undefined ? data.isActive : true,
createdAt: now,
updatedAt: now,
};
// Only add optional fields if they have values
const optionalFields = [
"displayName", "email", "globalPriority", "defaultModel",
"accessToken", "refreshToken", "expiresAt", "tokenType",
"scope", "idToken", "projectId", "apiKey", "testStatus",
"lastTested", "lastError", "lastErrorAt", "rateLimitedUntil", "expiresIn", "errorCode",
"consecutiveUseCount"
2026-01-04 21:58:59 -05:00
];
2026-01-04 21:58:59 -05:00
for (const field of optionalFields) {
if (data[field] !== undefined && data[field] !== null) {
connection[field] = data[field];
}
}
// Only add providerSpecificData if it has content
if (data.providerSpecificData && Object.keys(data.providerSpecificData).length > 0) {
connection.providerSpecificData = data.providerSpecificData;
}
2026-01-04 21:58:59 -05:00
db.data.providerConnections.push(connection);
await safeWrite(db);
// Reorder to ensure consistency
await reorderProviderConnections(data.provider);
2026-01-04 21:58:59 -05:00
return connection;
}
/**
* Update provider connection
*/
export async function updateProviderConnection(id, data) {
const db = await getDb();
const index = db.data.providerConnections.findIndex(c => c.id === id);
2026-01-04 21:58:59 -05:00
if (index === -1) return null;
const providerId = db.data.providerConnections[index].provider;
2026-01-04 21:58:59 -05:00
db.data.providerConnections[index] = {
...db.data.providerConnections[index],
...data,
updatedAt: new Date().toISOString(),
};
await safeWrite(db);
// Reorder if priority was changed
if (data.priority !== undefined) {
await reorderProviderConnections(providerId);
}
2026-01-04 21:58:59 -05:00
return db.data.providerConnections[index];
}
/**
* Delete provider connection
*/
export async function deleteProviderConnection(id) {
const db = await getDb();
const index = db.data.providerConnections.findIndex(c => c.id === id);
2026-01-04 21:58:59 -05:00
if (index === -1) return false;
const providerId = db.data.providerConnections[index].provider;
2026-01-04 21:58:59 -05:00
db.data.providerConnections.splice(index, 1);
await safeWrite(db);
// Reorder to fill gaps
await reorderProviderConnections(providerId);
2026-01-04 21:58:59 -05:00
return true;
}
/**
* Reorder provider connections to ensure unique, sequential priorities
*/
export async function reorderProviderConnections(providerId) {
const db = await getDb();
if (!db.data.providerConnections) return;
const providerConnections = db.data.providerConnections
.filter(c => c.provider === providerId)
.sort((a, b) => {
// Sort by priority first
const pDiff = (a.priority || 0) - (b.priority || 0);
if (pDiff !== 0) return pDiff;
// Use updatedAt as tie-breaker (newer first)
return new Date(b.updatedAt || 0) - new Date(a.updatedAt || 0);
});
// Re-assign sequential priorities
providerConnections.forEach((conn, index) => {
conn.priority = index + 1;
});
await safeWrite(db);
}
2026-01-04 21:58:59 -05:00
// ============ Model Aliases ============
/**
* Get all model aliases
*/
export async function getModelAliases() {
const db = await getDb();
return db.data.modelAliases || {};
}
/**
* Set model alias
*/
export async function setModelAlias(alias, model) {
const db = await getDb();
db.data.modelAliases[alias] = model;
await safeWrite(db);
2026-01-04 21:58:59 -05:00
}
/**
* Delete model alias
*/
export async function deleteModelAlias(alias) {
const db = await getDb();
delete db.data.modelAliases[alias];
await safeWrite(db);
2026-01-04 21:58:59 -05:00
}
// ============ MITM Alias ============
export async function getMitmAlias(toolName) {
const db = await getDb();
const all = db.data.mitmAlias || {};
if (toolName) return all[toolName] || {};
return all;
}
export async function setMitmAliasAll(toolName, mappings) {
const db = await getDb();
if (!db.data.mitmAlias) db.data.mitmAlias = {};
db.data.mitmAlias[toolName] = mappings || {};
await safeWrite(db);
}
2026-01-04 21:58:59 -05:00
// ============ Combos ============
/**
* Get all combos
*/
export async function getCombos() {
const db = await getDb();
return db.data.combos || [];
}
/**
* Get combo by ID
*/
export async function getComboById(id) {
const db = await getDb();
return (db.data.combos || []).find(c => c.id === id) || null;
}
/**
* Get combo by name
*/
export async function getComboByName(name) {
const db = await getDb();
return (db.data.combos || []).find(c => c.name === name) || null;
}
/**
* Create combo
*/
export async function createCombo(data) {
const db = await getDb();
if (!db.data.combos) db.data.combos = [];
2026-01-04 21:58:59 -05:00
const now = new Date().toISOString();
const combo = {
id: uuidv4(),
name: data.name,
models: data.models || [],
createdAt: now,
updatedAt: now,
};
2026-01-04 21:58:59 -05:00
db.data.combos.push(combo);
await safeWrite(db);
2026-01-04 21:58:59 -05:00
return combo;
}
/**
* Update combo
*/
export async function updateCombo(id, data) {
const db = await getDb();
if (!db.data.combos) db.data.combos = [];
2026-01-04 21:58:59 -05:00
const index = db.data.combos.findIndex(c => c.id === id);
if (index === -1) return null;
2026-01-04 21:58:59 -05:00
db.data.combos[index] = {
...db.data.combos[index],
...data,
updatedAt: new Date().toISOString(),
};
await safeWrite(db);
2026-01-04 21:58:59 -05:00
return db.data.combos[index];
}
/**
* Delete combo
*/
export async function deleteCombo(id) {
const db = await getDb();
if (!db.data.combos) return false;
2026-01-04 21:58:59 -05:00
const index = db.data.combos.findIndex(c => c.id === id);
if (index === -1) return false;
2026-01-04 21:58:59 -05:00
db.data.combos.splice(index, 1);
await safeWrite(db);
2026-01-04 21:58:59 -05:00
return true;
}
// ============ API Keys ============
/**
* Get all API keys
*/
export async function getApiKeys() {
const db = await getDb();
return db.data.apiKeys || [];
}
/**
* Generate short random key (8 chars)
*/
function generateShortKey() {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < 8; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
/**
* Create API key
* @param {string} name - Key name
* @param {string} machineId - MachineId (required)
*/
export async function createApiKey(name, machineId) {
if (!machineId) {
throw new Error("machineId is required");
}
2026-01-04 21:58:59 -05:00
const db = await getDb();
const now = new Date().toISOString();
2026-01-04 21:58:59 -05:00
// Always use new format: sk-{machineId}-{keyId}-{crc8}
const { generateApiKeyWithMachine } = await import("@/shared/utils/apiKey");
const result = generateApiKeyWithMachine(machineId);
2026-01-04 21:58:59 -05:00
const apiKey = {
id: uuidv4(),
name: name,
key: result.key,
machineId: machineId,
isActive: true,
2026-01-04 21:58:59 -05:00
createdAt: now,
};
2026-01-04 21:58:59 -05:00
db.data.apiKeys.push(apiKey);
await safeWrite(db);
2026-01-04 21:58:59 -05:00
return apiKey;
}
/**
* Delete API key
*/
export async function deleteApiKey(id) {
const db = await getDb();
const index = db.data.apiKeys.findIndex(k => k.id === id);
2026-01-04 21:58:59 -05:00
if (index === -1) return false;
2026-01-04 21:58:59 -05:00
db.data.apiKeys.splice(index, 1);
await safeWrite(db);
2026-01-04 21:58:59 -05:00
return true;
}
/**
* Get API key by ID
*/
export async function getApiKeyById(id) {
const db = await getDb();
return db.data.apiKeys.find(k => k.id === id) || null;
}
/**
* Update API key
*/
export async function updateApiKey(id, data) {
const db = await getDb();
const index = db.data.apiKeys.findIndex(k => k.id === id);
if (index === -1) return null;
db.data.apiKeys[index] = {
...db.data.apiKeys[index],
...data,
};
await safeWrite(db);
return db.data.apiKeys[index];
}
2026-01-04 21:58:59 -05:00
/**
* Validate API key
*/
export async function validateApiKey(key) {
const db = await getDb();
const found = db.data.apiKeys.find(k => k.key === key);
return found && found.isActive !== false;
2026-01-04 21:58:59 -05:00
}
// ============ Data Cleanup ============
/**
* Remove null/empty fields from all provider connections to reduce db size
*/
export async function cleanupProviderConnections() {
const db = await getDb();
const fieldsToCheck = [
"displayName", "email", "globalPriority", "defaultModel",
"accessToken", "refreshToken", "expiresAt", "tokenType",
"scope", "idToken", "projectId", "apiKey", "testStatus",
"lastTested", "lastError", "lastErrorAt", "rateLimitedUntil", "expiresIn",
"consecutiveUseCount"
2026-01-04 21:58:59 -05:00
];
let cleaned = 0;
for (const connection of db.data.providerConnections) {
for (const field of fieldsToCheck) {
if (connection[field] === null || connection[field] === undefined) {
delete connection[field];
cleaned++;
}
}
// Remove empty providerSpecificData
if (connection.providerSpecificData && Object.keys(connection.providerSpecificData).length === 0) {
delete connection.providerSpecificData;
cleaned++;
}
}
if (cleaned > 0) {
await safeWrite(db);
2026-01-04 21:58:59 -05:00
}
return cleaned;
}
// ============ Settings ============
/**
* Get settings
*/
export async function getSettings() {
const db = await getDb();
return db.data.settings || { cloudEnabled: false };
}
/**
* Update settings
*/
export async function updateSettings(updates) {
const db = await getDb();
db.data.settings = {
...db.data.settings,
...updates
};
await safeWrite(db);
2026-01-04 21:58:59 -05:00
return db.data.settings;
}
/**
* Export full database payload
*/
export async function exportDb() {
const db = await getDb();
return db.data || cloneDefaultData();
}
/**
* Import full database payload
*/
export async function importDb(payload) {
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
throw new Error("Invalid database payload");
}
const nextData = {
...cloneDefaultData(),
...payload,
settings: {
...cloneDefaultData().settings,
...(payload.settings && typeof payload.settings === "object" && !Array.isArray(payload.settings)
? payload.settings
: {}),
},
};
const { data: normalized } = ensureDbShape(nextData);
const db = await getDb();
db.data = normalized;
await safeWrite(db);
return db.data;
}
2026-01-04 21:58:59 -05:00
/**
* Check if cloud is enabled
*/
export async function isCloudEnabled() {
const settings = await getSettings();
return settings.cloudEnabled === true;
}
/**
* Get cloud URL (UI config > env > default)
*/
export async function getCloudUrl() {
const settings = await getSettings();
return settings.cloudUrl
|| process.env.CLOUD_URL
|| process.env.NEXT_PUBLIC_CLOUD_URL
|| "";
}
// ============ Pricing ============
/**
* Get pricing configuration
2026-04-04 03:40:49 -04:00
* Returns merged: PROVIDER_PRICING defaults + user overrides
*/
export async function getPricing() {
const db = await getDb();
const userPricing = db.data.pricing || {};
2026-04-04 03:40:49 -04:00
const { PROVIDER_PRICING } = await import("@/shared/constants/pricing.js");
2026-04-04 03:40:49 -04:00
// Deep merge PROVIDER_PRICING + user overrides
const merged = {};
2026-04-04 03:40:49 -04:00
for (const [provider, models] of Object.entries(PROVIDER_PRICING)) {
merged[provider] = { ...models };
if (userPricing[provider]) {
for (const [model, pricing] of Object.entries(userPricing[provider])) {
2026-04-04 03:40:49 -04:00
merged[provider][model] = merged[provider][model]
? { ...merged[provider][model], ...pricing }
: pricing;
}
}
}
2026-04-04 03:40:49 -04:00
// User-only providers not in PROVIDER_PRICING
for (const [provider, models] of Object.entries(userPricing)) {
2026-04-04 03:40:49 -04:00
if (!merged[provider]) {
merged[provider] = { ...models };
} else {
for (const [model, pricing] of Object.entries(models)) {
2026-04-04 03:40:49 -04:00
if (!merged[provider][model]) merged[provider][model] = pricing;
}
}
}
2026-04-04 03:40:49 -04:00
return merged;
}
/**
2026-04-04 03:40:49 -04:00
* Get pricing for a specific provider and model.
* Delegates to getPricingForModel in pricing.js which handles the full fallback chain:
* 1. PROVIDER_PRICING[provider][model] — provider-specific override
* 2. MODEL_PRICING[model] — canonical model price
* 3. PATTERN_PRICING — glob pattern match
*
* Also checks user DB overrides first.
*/
export async function getPricingForModel(provider, model) {
2026-04-04 03:40:49 -04:00
if (!model) return null;
2026-04-04 03:40:49 -04:00
const db = await getDb();
const userPricing = db.data.pricing || {};
2026-04-04 03:40:49 -04:00
// User override takes top priority
if (provider && userPricing[provider]?.[model]) {
return userPricing[provider][model];
}
2026-04-04 03:40:49 -04:00
// Delegate to constants fallback chain
const { getPricingForModel: resolve } = await import("@/shared/constants/pricing.js");
return resolve(provider, model);
}
/**
* Update pricing configuration
* @param {object} pricingData - New pricing data to merge
*/
export async function updatePricing(pricingData) {
const db = await getDb();
// Ensure pricing object exists
if (!db.data.pricing) {
db.data.pricing = {};
}
// Merge new pricing data
for (const [provider, models] of Object.entries(pricingData)) {
if (!db.data.pricing[provider]) {
db.data.pricing[provider] = {};
}
for (const [model, pricing] of Object.entries(models)) {
db.data.pricing[provider][model] = pricing;
}
}
await safeWrite(db);
return db.data.pricing;
}
/**
* Reset pricing to defaults for specific provider/model
* @param {string} provider - Provider ID
* @param {string} model - Model ID (optional, if not provided resets entire provider)
*/
export async function resetPricing(provider, model) {
const db = await getDb();
if (!db.data.pricing) {
db.data.pricing = {};
}
if (model) {
// Reset specific model
if (db.data.pricing[provider]) {
delete db.data.pricing[provider][model];
// Clean up empty provider objects
if (Object.keys(db.data.pricing[provider]).length === 0) {
delete db.data.pricing[provider];
}
}
} else {
// Reset entire provider
delete db.data.pricing[provider];
}
await safeWrite(db);
return db.data.pricing;
}
/**
* Reset all pricing to defaults
*/
export async function resetAllPricing() {
const db = await getDb();
db.data.pricing = {};
await safeWrite(db);
return db.data.pricing;
}