2026-01-04 21:58:59 -05:00
|
|
|
import { Low } from "lowdb";
|
|
|
|
|
import { JSONFile } from "lowdb/node";
|
|
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2026-01-19 18:31:08 -05:00
|
|
|
import path from "node:path";
|
|
|
|
|
import os from "node:os";
|
|
|
|
|
import fs from "node:fs";
|
2026-01-11 09:45:01 -05:00
|
|
|
|
|
|
|
|
const isCloud = typeof caches !== 'undefined' || typeof caches === 'object';
|
|
|
|
|
|
2026-01-09 05:14:51 -05:00
|
|
|
// Get app name - fixed constant to avoid Windows path issues in standalone build
|
2026-01-04 21:58:59 -05:00
|
|
|
function getAppName() {
|
2026-01-09 05:14:51 -05:00
|
|
|
return "9router";
|
2026-01-04 21:58:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get user data directory based on platform
|
|
|
|
|
function getUserDataDir() {
|
2026-01-11 09:45:01 -05:00
|
|
|
if (isCloud) return "/tmp"; // Fallback for Workers
|
|
|
|
|
|
2026-01-19 05:15:11 -05:00
|
|
|
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-11 09:45:01 -05:00
|
|
|
|
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();
|
2026-01-11 09:45:01 -05:00
|
|
|
const DB_FILE = isCloud ? null : path.join(DATA_DIR, "db.json");
|
2026-01-04 21:58:59 -05:00
|
|
|
|
|
|
|
|
// Ensure data directory exists
|
2026-01-11 09:45:01 -05:00
|
|
|
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: [],
|
2026-02-02 07:45:12 -05:00
|
|
|
providerNodes: [],
|
2026-01-04 21:58:59 -05:00
|
|
|
modelAliases: {},
|
2026-02-08 04:28:13 -05:00
|
|
|
mitmAlias: {},
|
2026-01-04 21:58:59 -05:00
|
|
|
combos: [],
|
|
|
|
|
apiKeys: [],
|
|
|
|
|
settings: {
|
2026-01-06 15:35:21 -05:00
|
|
|
cloudEnabled: false,
|
2026-02-04 23:26:11 -05:00
|
|
|
stickyRoundRobinLimit: 3,
|
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,
|
|
|
|
|
observabilityMaxRecords: 1000,
|
|
|
|
|
observabilityBatchSize: 20,
|
|
|
|
|
observabilityFlushIntervalMs: 5000,
|
|
|
|
|
observabilityMaxJsonSize: 1024
|
2026-01-06 16:59:49 -05:00
|
|
|
},
|
|
|
|
|
pricing: {} // NEW: pricing configuration
|
2026-01-04 21:58:59 -05:00
|
|
|
};
|
|
|
|
|
|
2026-02-06 08:54:42 -05:00
|
|
|
function cloneDefaultData() {
|
|
|
|
|
return {
|
|
|
|
|
providerConnections: [],
|
|
|
|
|
providerNodes: [],
|
|
|
|
|
modelAliases: {},
|
2026-02-08 04:28:13 -05:00
|
|
|
mitmAlias: {},
|
2026-02-06 08:54:42 -05:00
|
|
|
combos: [],
|
|
|
|
|
apiKeys: [],
|
|
|
|
|
settings: {
|
|
|
|
|
cloudEnabled: false,
|
|
|
|
|
stickyRoundRobinLimit: 3,
|
|
|
|
|
requireLogin: 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
|
2026-02-06 08:54:42 -05:00
|
|
|
},
|
|
|
|
|
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) {
|
|
|
|
|
next.settings[settingKey] = settingDefault;
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { data: next, changed };
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
// Singleton instance
|
|
|
|
|
let dbInstance = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get database instance (singleton)
|
|
|
|
|
*/
|
|
|
|
|
export async function getDb() {
|
2026-01-11 09:45:01 -05:00
|
|
|
if (isCloud) {
|
|
|
|
|
// Return in-memory DB for Workers
|
|
|
|
|
if (!dbInstance) {
|
2026-02-06 08:54:42 -05:00
|
|
|
const data = cloneDefaultData();
|
|
|
|
|
dbInstance = new Low({ read: async () => {}, write: async () => {} }, data);
|
|
|
|
|
dbInstance.data = data;
|
2026-01-11 09:45:01 -05:00
|
|
|
}
|
|
|
|
|
return dbInstance;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
if (!dbInstance) {
|
|
|
|
|
const adapter = new JSONFile(DB_FILE);
|
2026-02-06 08:54:42 -05:00
|
|
|
dbInstance = new Low(adapter, cloneDefaultData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Always read latest disk state to avoid stale singleton data across route workers.
|
|
|
|
|
try {
|
|
|
|
|
await dbInstance.read();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof SyntaxError) {
|
|
|
|
|
console.warn('[DB] Corrupt JSON detected, resetting to defaults...');
|
|
|
|
|
dbInstance.data = cloneDefaultData();
|
|
|
|
|
await dbInstance.write();
|
|
|
|
|
} else {
|
|
|
|
|
throw error;
|
2026-01-04 21:58:59 -05:00
|
|
|
}
|
2026-02-06 08:54:42 -05:00
|
|
|
}
|
2026-01-11 09:45:01 -05:00
|
|
|
|
2026-02-06 08:54:42 -05:00
|
|
|
// Initialize/migrate missing keys for older DB schema versions.
|
|
|
|
|
if (!dbInstance.data) {
|
|
|
|
|
dbInstance.data = cloneDefaultData();
|
|
|
|
|
await dbInstance.write();
|
|
|
|
|
} else {
|
|
|
|
|
const { data, changed } = ensureDbShape(dbInstance.data);
|
|
|
|
|
dbInstance.data = data;
|
|
|
|
|
if (changed) {
|
2026-01-04 21:58:59 -05:00
|
|
|
await dbInstance.write();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-06 08:54:42 -05:00
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
return dbInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============ Provider Connections ============
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all provider connections
|
|
|
|
|
*/
|
|
|
|
|
export async function getProviderConnections(filter = {}) {
|
|
|
|
|
const db = await getDb();
|
|
|
|
|
let connections = db.data.providerConnections || [];
|
|
|
|
|
|
|
|
|
|
if (filter.provider) {
|
|
|
|
|
connections = connections.filter(c => c.provider === filter.provider);
|
|
|
|
|
}
|
|
|
|
|
if (filter.isActive !== undefined) {
|
|
|
|
|
connections = connections.filter(c => c.isActive === filter.isActive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort by priority (lower = higher priority)
|
|
|
|
|
connections.sort((a, b) => (a.priority || 999) - (b.priority || 999));
|
|
|
|
|
|
|
|
|
|
return connections;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 07:45:12 -05:00
|
|
|
// ============ 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();
|
2026-02-06 08:53:20 -05:00
|
|
|
|
|
|
|
|
// Initialize providerNodes if undefined (backward compatibility)
|
|
|
|
|
if (!db.data.providerNodes) {
|
|
|
|
|
db.data.providerNodes = [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 07:45:12 -05:00
|
|
|
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 db.write();
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update provider node
|
|
|
|
|
*/
|
|
|
|
|
export async function updateProviderNode(id, data) {
|
|
|
|
|
const db = await getDb();
|
2026-02-06 08:53:20 -05:00
|
|
|
if (!db.data.providerNodes) {
|
|
|
|
|
db.data.providerNodes = [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 07:45:12 -05:00
|
|
|
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 db.write();
|
|
|
|
|
|
|
|
|
|
return db.data.providerNodes[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete provider node
|
|
|
|
|
*/
|
|
|
|
|
export async function deleteProviderNode(id) {
|
|
|
|
|
const db = await getDb();
|
2026-02-06 08:53:20 -05:00
|
|
|
if (!db.data.providerNodes) {
|
|
|
|
|
db.data.providerNodes = [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 07:45:12 -05:00
|
|
|
const index = db.data.providerNodes.findIndex((node) => node.id === id);
|
|
|
|
|
|
|
|
|
|
if (index === -1) return null;
|
|
|
|
|
|
|
|
|
|
const [removed] = db.data.providerNodes.splice(index, 1);
|
|
|
|
|
await db.write();
|
|
|
|
|
|
|
|
|
|
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 db.write();
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If exists, update instead of create
|
|
|
|
|
if (existingIndex !== -1) {
|
|
|
|
|
db.data.providerConnections[existingIndex] = {
|
|
|
|
|
...db.data.providerConnections[existingIndex],
|
|
|
|
|
...data,
|
|
|
|
|
updatedAt: now,
|
|
|
|
|
};
|
|
|
|
|
await db.write();
|
|
|
|
|
return db.data.providerConnections[existingIndex];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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",
|
2026-01-06 15:35:21 -05:00
|
|
|
"lastTested", "lastError", "lastErrorAt", "rateLimitedUntil", "expiresIn", "errorCode",
|
|
|
|
|
"consecutiveUseCount"
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.data.providerConnections.push(connection);
|
|
|
|
|
await db.write();
|
2026-01-06 15:14:10 -05:00
|
|
|
|
|
|
|
|
// 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-06 15:14:10 -05:00
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
if (index === -1) return null;
|
2026-01-06 15:14:10 -05:00
|
|
|
|
|
|
|
|
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(),
|
|
|
|
|
};
|
2026-01-06 15:14:10 -05:00
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
await db.write();
|
2026-01-06 15:14:10 -05:00
|
|
|
|
|
|
|
|
// 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-06 15:14:10 -05:00
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
if (index === -1) return false;
|
2026-01-06 15:14:10 -05:00
|
|
|
|
|
|
|
|
const providerId = db.data.providerConnections[index].provider;
|
|
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
db.data.providerConnections.splice(index, 1);
|
|
|
|
|
await db.write();
|
2026-01-06 15:14:10 -05:00
|
|
|
|
|
|
|
|
// Reorder to fill gaps
|
|
|
|
|
await reorderProviderConnections(providerId);
|
|
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-06 15:14:10 -05:00
|
|
|
/**
|
|
|
|
|
* 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 db.write();
|
|
|
|
|
}
|
|
|
|
|
|
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 db.write();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete model alias
|
|
|
|
|
*/
|
|
|
|
|
export async function deleteModelAlias(alias) {
|
|
|
|
|
const db = await getDb();
|
|
|
|
|
delete db.data.modelAliases[alias];
|
|
|
|
|
await db.write();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 04:28:13 -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 db.write();
|
|
|
|
|
}
|
|
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
|
|
const now = new Date().toISOString();
|
|
|
|
|
const combo = {
|
|
|
|
|
id: uuidv4(),
|
|
|
|
|
name: data.name,
|
|
|
|
|
models: data.models || [],
|
|
|
|
|
createdAt: now,
|
|
|
|
|
updatedAt: now,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
db.data.combos.push(combo);
|
|
|
|
|
await db.write();
|
|
|
|
|
return combo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update combo
|
|
|
|
|
*/
|
|
|
|
|
export async function updateCombo(id, data) {
|
|
|
|
|
const db = await getDb();
|
|
|
|
|
if (!db.data.combos) db.data.combos = [];
|
|
|
|
|
|
|
|
|
|
const index = db.data.combos.findIndex(c => c.id === id);
|
|
|
|
|
if (index === -1) return null;
|
|
|
|
|
|
|
|
|
|
db.data.combos[index] = {
|
|
|
|
|
...db.data.combos[index],
|
|
|
|
|
...data,
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await db.write();
|
|
|
|
|
return db.data.combos[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete combo
|
|
|
|
|
*/
|
|
|
|
|
export async function deleteCombo(id) {
|
|
|
|
|
const db = await getDb();
|
|
|
|
|
if (!db.data.combos) return false;
|
|
|
|
|
|
|
|
|
|
const index = db.data.combos.findIndex(c => c.id === id);
|
|
|
|
|
if (index === -1) return false;
|
|
|
|
|
|
|
|
|
|
db.data.combos.splice(index, 1);
|
|
|
|
|
await db.write();
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const db = await getDb();
|
|
|
|
|
const now = new Date().toISOString();
|
|
|
|
|
|
|
|
|
|
// Always use new format: sk-{machineId}-{keyId}-{crc8}
|
|
|
|
|
const { generateApiKeyWithMachine } = await import("@/shared/utils/apiKey");
|
|
|
|
|
const result = generateApiKeyWithMachine(machineId);
|
|
|
|
|
|
|
|
|
|
const apiKey = {
|
|
|
|
|
id: uuidv4(),
|
|
|
|
|
name: name,
|
|
|
|
|
key: result.key,
|
|
|
|
|
machineId: machineId,
|
|
|
|
|
createdAt: now,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
db.data.apiKeys.push(apiKey);
|
|
|
|
|
await db.write();
|
|
|
|
|
|
|
|
|
|
return apiKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete API key
|
|
|
|
|
*/
|
|
|
|
|
export async function deleteApiKey(id) {
|
|
|
|
|
const db = await getDb();
|
|
|
|
|
const index = db.data.apiKeys.findIndex(k => k.id === id);
|
|
|
|
|
|
|
|
|
|
if (index === -1) return false;
|
|
|
|
|
|
|
|
|
|
db.data.apiKeys.splice(index, 1);
|
|
|
|
|
await db.write();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate API key
|
|
|
|
|
*/
|
|
|
|
|
export async function validateApiKey(key) {
|
|
|
|
|
const db = await getDb();
|
|
|
|
|
return db.data.apiKeys.some(k => k.key === key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============ 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",
|
2026-01-06 15:35:21 -05:00
|
|
|
"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 db.write();
|
|
|
|
|
}
|
|
|
|
|
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 db.write();
|
|
|
|
|
return db.data.settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if cloud is enabled
|
|
|
|
|
*/
|
|
|
|
|
export async function isCloudEnabled() {
|
|
|
|
|
const settings = await getSettings();
|
|
|
|
|
return settings.cloudEnabled === true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 23:27:41 -05:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
|| "";
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-06 16:59:49 -05:00
|
|
|
// ============ Pricing ============
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get pricing configuration
|
|
|
|
|
* Returns merged user pricing with defaults
|
|
|
|
|
*/
|
|
|
|
|
export async function getPricing() {
|
|
|
|
|
const db = await getDb();
|
|
|
|
|
const userPricing = db.data.pricing || {};
|
|
|
|
|
|
|
|
|
|
// Import default pricing
|
|
|
|
|
const { getDefaultPricing } = await import("@/shared/constants/pricing.js");
|
|
|
|
|
const defaultPricing = getDefaultPricing();
|
|
|
|
|
|
|
|
|
|
// Merge user pricing with defaults
|
|
|
|
|
// User pricing overrides defaults for specific provider/model combinations
|
|
|
|
|
const mergedPricing = {};
|
|
|
|
|
|
|
|
|
|
for (const [provider, models] of Object.entries(defaultPricing)) {
|
|
|
|
|
mergedPricing[provider] = { ...models };
|
|
|
|
|
|
|
|
|
|
// Apply user overrides if they exist
|
|
|
|
|
if (userPricing[provider]) {
|
|
|
|
|
for (const [model, pricing] of Object.entries(userPricing[provider])) {
|
|
|
|
|
if (mergedPricing[provider][model]) {
|
|
|
|
|
mergedPricing[provider][model] = { ...mergedPricing[provider][model], ...pricing };
|
|
|
|
|
} else {
|
|
|
|
|
mergedPricing[provider][model] = pricing;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add any user-only pricing entries
|
|
|
|
|
for (const [provider, models] of Object.entries(userPricing)) {
|
|
|
|
|
if (!mergedPricing[provider]) {
|
|
|
|
|
mergedPricing[provider] = { ...models };
|
|
|
|
|
} else {
|
|
|
|
|
for (const [model, pricing] of Object.entries(models)) {
|
|
|
|
|
if (!mergedPricing[provider][model]) {
|
|
|
|
|
mergedPricing[provider][model] = pricing;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mergedPricing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get pricing for a specific provider and model
|
|
|
|
|
*/
|
|
|
|
|
export async function getPricingForModel(provider, model) {
|
|
|
|
|
const pricing = await getPricing();
|
|
|
|
|
|
|
|
|
|
// Try direct lookup
|
2026-01-19 18:31:08 -05:00
|
|
|
if (pricing[provider]?.[model]) {
|
2026-01-06 16:59:49 -05:00
|
|
|
return pricing[provider][model];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try mapping provider ID to alias
|
|
|
|
|
// We need to duplicate the mapping here or import it
|
|
|
|
|
// Since we can't easily import from open-sse, we'll implement the mapping locally
|
|
|
|
|
const PROVIDER_ID_TO_ALIAS = {
|
|
|
|
|
claude: "cc",
|
|
|
|
|
codex: "cx",
|
|
|
|
|
"gemini-cli": "gc",
|
|
|
|
|
qwen: "qw",
|
|
|
|
|
iflow: "if",
|
|
|
|
|
antigravity: "ag",
|
|
|
|
|
github: "gh",
|
|
|
|
|
openai: "openai",
|
|
|
|
|
anthropic: "anthropic",
|
|
|
|
|
gemini: "gemini",
|
|
|
|
|
openrouter: "openrouter",
|
|
|
|
|
glm: "glm",
|
|
|
|
|
kimi: "kimi",
|
|
|
|
|
minimax: "minimax",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const alias = PROVIDER_ID_TO_ALIAS[provider];
|
|
|
|
|
if (alias && pricing[alias]) {
|
|
|
|
|
return pricing[alias][model] || null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 db.write();
|
|
|
|
|
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 db.write();
|
|
|
|
|
return db.data.pricing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset all pricing to defaults
|
|
|
|
|
*/
|
|
|
|
|
export async function resetAllPricing() {
|
|
|
|
|
const db = await getDb();
|
|
|
|
|
db.data.pricing = {};
|
|
|
|
|
await db.write();
|
|
|
|
|
return db.data.pricing;
|
|
|
|
|
}
|