Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
refactor: use portos-ai-toolkit for provider status tracking
Adam Eivy2b6c89a38fb5
diff --git a/.changelog/v0.10.x.md b/.changelog/v0.10.x.mdindex f93b4e14c9..e2736b9288 100644--- a/.changelog/v0.10.x.md+++ b/.changelog/v0.10.x.md@@ -123,6 +123,7 @@ This release introduces a structured changelog documentation system for creating- **Manual Recovery**: "Recover" button allows manually marking a provider as available when you know the limit has reset- **Socket.IO Events**: Real-time `provider:status:changed` events notify UI of provider status changes- **API Endpoints**: New `/api/providers/status`, `/api/providers/:id/status`, and `/api/providers/:id/status/recover` endpoints for status management+- **AI Toolkit Integration**: Core rate limit detection, error categorization, and provider status tracking now built into `portos-ai-toolkit` v0.2.0 - PortOS wraps the toolkit service for backwards compatibility### CoS Learning Data Reset- **Reset Skipped Task Types**: Added ability to reset learning data for auto-paused task types that have been stuck due to historical failures (e.g., task types with 5% success after 200+ attempts from a now-fixed bug)diff --git a/package.json b/package.jsonindex 31e60fa3fe..0d0ba55a10 100644--- a/package.json+++ b/package.json@@ -28,6 +28,6 @@},"dependencies": {"express": "^5.2.1",- "portos-ai-toolkit": "github:atomantic/portos-ai-toolkit#v0.1.3"+ "portos-ai-toolkit": "github:atomantic/portos-ai-toolkit#v0.2.0"}}diff --git a/server/package.json b/server/package.jsonindex e6d0d5a825..7ad2441f84 100644--- a/server/package.json+++ b/server/package.json@@ -12,7 +12,7 @@},"dependencies": {"cors": "^2.8.5",- "portos-ai-toolkit": "^0.1.0",+ "portos-ai-toolkit": "github:atomantic/portos-ai-toolkit#v0.2.0","express": "^4.21.2","pm2": "^5.4.3","socket.io": "^4.8.3",diff --git a/server/services/providerStatus.js b/server/services/providerStatus.jsindex f3c3081ab6..a26c95e551 100644--- a/server/services/providerStatus.js+++ b/server/services/providerStatus.js@@ -1,98 +1,37 @@/*** Provider Status Service*- * Tracks provider availability status, usage limits, and provides- * fallback provider selection when the primary provider is unavailable.+ * Thin wrapper around portos-ai-toolkit's createProviderStatusService+ * that provides backwards-compatible exports for PortOS.*/-import { EventEmitter } from 'events';-import { readFile, writeFile, mkdir } from 'fs/promises';-import { existsSync } from 'fs';+import { createProviderStatusService } from 'portos-ai-toolkit/server';import { join, dirname } from 'path';import { fileURLToPath } from 'url';const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);-const STATUS_FILE = join(__dirname, '../../data/provider-status.json');--// Event emitter for status changes-export const providerStatusEvents = new EventEmitter();--// In-memory status cache-let statusCache = {- providers: {},- lastUpdated: null-};--/**- * Provider status structure:- * {- * providerId: {- * available: boolean,- * reason: string, // 'ok' | 'usage-limit' | 'rate-limit' | 'auth-error' | 'network-error'- * message: string, // Human-readable status message- * waitTime: string, // Extracted wait time (e.g., "1 day 1 hour 33 minutes")- * unavailableSince: string, // ISO timestamp when became unavailable- * estimatedRecovery: string,// ISO timestamp when expected to recover- * failureCount: number, // Consecutive failures- * lastChecked: string // ISO timestamp of last status check- * }- * }- */--/**- * Load status from disk- */-async function loadStatus() {- if (!existsSync(STATUS_FILE)) {- return { providers: {}, lastUpdated: null };+const DATA_DIR = join(__dirname, '../../data');++// Create the provider status service from ai-toolkit+const providerStatusService = createProviderStatusService({+ dataDir: DATA_DIR,+ statusFile: 'provider-status.json',+ defaultFallbackPriority: ['claude-code', 'codex', 'lmstudio', 'local-lm-studio', 'ollama', 'gemini-cli'],+ onStatusChange: (eventData) => {+ // Re-emit on the exported events emitter for backwards compatibility+ providerStatusEvents.emit('status:changed', eventData);}- const content = await readFile(STATUS_FILE, 'utf-8');- return JSON.parse(content);-}+});-/**- * Save status to disk- */-async function saveStatus(status) {- const dir = dirname(STATUS_FILE);- if (!existsSync(dir)) {- await mkdir(dir, { recursive: true });- }- status.lastUpdated = new Date().toISOString();- await writeFile(STATUS_FILE, JSON.stringify(status, null, 2));- statusCache = status;-}+// Export the events emitter for Socket.IO integration+export const providerStatusEvents = providerStatusService.events;/*** Initialize status cache*/export async function initProviderStatus() {- statusCache = await loadStatus().catch(() => ({ providers: {}, lastUpdated: null }));-- // Clean up stale statuses (older than 24 hours)- const now = Date.now();- let changed = false;- for (const [providerId, status] of Object.entries(statusCache.providers)) {- if (status.estimatedRecovery) {- const recoveryTime = new Date(status.estimatedRecovery).getTime();- if (now > recoveryTime) {- // Recovery time passed, mark as available- statusCache.providers[providerId] = {- available: true,- reason: 'ok',- message: 'Provider available',- lastChecked: new Date().toISOString()- };- changed = true;- }- }- }-- if (changed) {- await saveStatus(statusCache);- }-+ await providerStatusService.init();console.log('📊 Provider status service initialized');}@@ -100,143 +39,46 @@ export async function initProviderStatus() {* Get status for a specific provider*/export function getProviderStatus(providerId) {- return statusCache.providers[providerId] || {- available: true,- reason: 'ok',- message: 'Provider available',- lastChecked: new Date().toISOString()- };+ return providerStatusService.getStatus(providerId);}/*** Get all provider statuses*/export function getAllProviderStatuses() {- return { ...statusCache };+ return providerStatusService.getAllStatuses();}/*** Check if a provider is available*/export function isProviderAvailable(providerId) {- const status = getProviderStatus(providerId);- return status.available;-}--/**- * Parse wait time string to milliseconds- * e.g., "1 day 1 hour 33 minutes" -> 91980000- */-function parseWaitTime(waitTimeStr) {- if (!waitTimeStr) return null;-- let totalMs = 0;- const dayMatch = waitTimeStr.match(/(\d+)\s*day/i);- const hourMatch = waitTimeStr.match(/(\d+)\s*hour/i);- const minMatch = waitTimeStr.match(/(\d+)\s*min/i);- const secMatch = waitTimeStr.match(/(\d+)\s*sec/i);-- if (dayMatch) totalMs += parseInt(dayMatch[1]) * 24 * 60 * 60 * 1000;- if (hourMatch) totalMs += parseInt(hourMatch[1]) * 60 * 60 * 1000;- if (minMatch) totalMs += parseInt(minMatch[1]) * 60 * 1000;- if (secMatch) totalMs += parseInt(secMatch[1]) * 1000;-- return totalMs || null;+ return providerStatusService.isAvailable(providerId);}/*** Mark a provider as unavailable due to usage limit*/export async function markProviderUsageLimit(providerId, errorInfo) {- const now = new Date();- const waitTimeMs = parseWaitTime(errorInfo.waitTime);- const estimatedRecovery = waitTimeMs- ? new Date(now.getTime() + waitTimeMs).toISOString()- : new Date(now.getTime() + 24 * 60 * 60 * 1000).toISOString(); // Default: 24 hours-- const previousStatus = statusCache.providers[providerId];- const failureCount = (previousStatus?.failureCount || 0) + 1;-- statusCache.providers[providerId] = {- available: false,- reason: 'usage-limit',- message: errorInfo.message || 'Usage limit exceeded',- waitTime: errorInfo.waitTime || null,- unavailableSince: now.toISOString(),- estimatedRecovery,- failureCount,- lastChecked: now.toISOString()- };-- await saveStatus(statusCache);-- // Emit event for UI updates- providerStatusEvents.emit('status:changed', {- providerId,- status: statusCache.providers[providerId],- type: 'usage-limit'- });-- console.log(`⚠️ Provider ${providerId} marked unavailable: usage limit (retry after ${errorInfo.waitTime || '24h'})`);-- return statusCache.providers[providerId];+ const status = await providerStatusService.markUsageLimit(providerId, errorInfo);+ console.log(`⚠️ Provider ${providerId} marked unavailable: usage limit (retry after ${errorInfo?.waitTime || '24h'})`);+ return status;}/*** Mark a provider as unavailable due to rate limiting (temporary)*/export async function markProviderRateLimited(providerId) {- const now = new Date();- // Rate limits are usually short - default to 5 minutes- const estimatedRecovery = new Date(now.getTime() + 5 * 60 * 1000).toISOString();-- const previousStatus = statusCache.providers[providerId];- const failureCount = (previousStatus?.failureCount || 0) + 1;-- statusCache.providers[providerId] = {- available: false,- reason: 'rate-limit',- message: 'Rate limit exceeded - temporary',- unavailableSince: now.toISOString(),- estimatedRecovery,- failureCount,- lastChecked: now.toISOString()- };-- await saveStatus(statusCache);-- providerStatusEvents.emit('status:changed', {- providerId,- status: statusCache.providers[providerId],- type: 'rate-limit'- });-- return statusCache.providers[providerId];+ return providerStatusService.markRateLimited(providerId);}/*** Mark a provider as available (recovered)*/export async function markProviderAvailable(providerId) {- statusCache.providers[providerId] = {- available: true,- reason: 'ok',- message: 'Provider available',- failureCount: 0,- lastChecked: new Date().toISOString()- };-- await saveStatus(statusCache);-- providerStatusEvents.emit('status:changed', {- providerId,- status: statusCache.providers[providerId],- type: 'recovered'- });-+ const status = await providerStatusService.markAvailable(providerId);console.log(`✅ Provider ${providerId} marked available`);-- return statusCache.providers[providerId];+ return status;}/**@@ -248,61 +90,16 @@ export async function markProviderAvailable(providerId) {* 2. Provider-level fallback (provider.fallbackProvider)* 3. System default priority list*/-export async function getFallbackProvider(primaryProviderId, providers, taskFallbackId = null) {- // Define fallback priority order- const fallbackPriority = ['claude-code', 'codex', 'lmstudio', 'local-lm-studio', 'ollama', 'gemini-cli'];-- // 1. Check task-level fallback first (highest priority)- if (taskFallbackId && taskFallbackId !== primaryProviderId) {- const taskFallback = providers[taskFallbackId];- if (taskFallback?.enabled && isProviderAvailable(taskFallback.id)) {- return { provider: taskFallback, source: 'task' };- }- }-- // 2. Check provider's configured fallback- const primaryProvider = providers[primaryProviderId];- if (primaryProvider?.fallbackProvider) {- const configuredFallback = providers[primaryProvider.fallbackProvider];- if (configuredFallback?.enabled && isProviderAvailable(configuredFallback.id)) {- return { provider: configuredFallback, source: 'provider' };- }- }-- // 3. Try fallback priority list- for (const providerId of fallbackPriority) {- if (providerId === primaryProviderId) continue;-- const provider = providers[providerId];- if (provider?.enabled && isProviderAvailable(providerId)) {- return { provider, source: 'system' };- }- }-- return null;+export function getFallbackProvider(primaryProviderId, providers, taskFallbackId = null) {+ return providerStatusService.getFallbackProvider(primaryProviderId, providers, taskFallbackId);}/*** Get human-readable time until provider recovery*/export function getTimeUntilRecovery(providerId) {- const status = getProviderStatus(providerId);- if (status.available || !status.estimatedRecovery) return null;-- const now = Date.now();- const recoveryTime = new Date(status.estimatedRecovery).getTime();- const remainingMs = recoveryTime - now;-- if (remainingMs <= 0) return 'any moment';-- const days = Math.floor(remainingMs / (24 * 60 * 60 * 1000));- const hours = Math.floor((remainingMs % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000));- const minutes = Math.floor((remainingMs % (60 * 60 * 1000)) / (60 * 1000));-- const parts = [];- if (days > 0) parts.push(`${days}d`);- if (hours > 0) parts.push(`${hours}h`);- if (minutes > 0) parts.push(`${minutes}m`);-- return parts.join(' ') || '< 1m';+ return providerStatusService.getTimeUntilRecovery(providerId);}++// Export the underlying service for direct access if needed+export { providerStatusService };