mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-20 22:22:57 +03:00
* chore(release): v3.7.9 — gemini-cli cloud code separation * chore(provider): Update Jina AI model catalog (#1874) Integrated into release/v3.7.9 * docs: update CHANGELOG for PR 1874 and retroactive credits * fix: resolve stream defaults and codex prompt mapping (#1873, #1872) * chore(compression): start caveman compression update * feat(compression): expand caveman compression and analytics pipeline Add caveman intensity levels, output mode instructions, validation, and preview diffs across the compression pipeline. Extend MCP and dashboard settings to support auto-trigger mode, system prompt preservation, MCP description compression, and caveman rule metadata. Record richer compression analytics with receipt fields, validation fallbacks, output mode data, and add the related database migration. Improve preservation handling for code, URLs, markdown, math, and other protected content while adding broad unit, integration, and golden-set coverage for caveman parity and compression behavior. * feat(compression): expose rule intensities and track usd savings Add estimated USD savings to compression analytics so saved tokens can be reported in cost terms alongside existing token metrics. Expose caveman rule intensity metadata for settings consumers and add a settings API route alias for rule lookup. Also preserve system prompts when aggressive compression falls back to lite mode. * feat(compression): RTK compression roadmap (#1889) * chore(rtk): initialize compression roadmap branch * feat(compression): add RTK engine and compression combos Introduce RTK command-aware tool-output compression alongside stacked RTK -> Caveman pipelines for mixed prompt contexts. Add engine registration, declarative RTK filter packs, language-aware Caveman rule loading, compression combo persistence and assignments, analytics grouped by engine/combo, and new MCP/API endpoints for configuration, previews, filters, and combo management. Expose the new capabilities in the dashboard with dedicated Context & Cache pages for Caveman, RTK, and compression combos, and update docs, i18n strings, migrations, and tests to cover the expanded compression surface. * feat(compression): expand RTK DSL, filter catalog, and recovery APIs Add RTK parity features across the compression pipeline, dashboard, and management APIs. This expands the built-in filter catalog, adds trust-gated custom filter loading, inline filter verification, code stripping, smarter detection, and optional redacted raw-output retention for authenticated recovery. Also extend Caveman with file-based multilingual rule packs, localized output-mode instructions, stricter preview/config schemas, engine registry metadata, analytics fields, and broad unit test coverage for RTK, rule loading, and stacked compression behavior. * fix(auth): protect oauth routes and health reset operations Require authenticated dashboard access for OAuth endpoints that can create or import provider connections when login enforcement is enabled. Move `/api/monitoring/health` to the readonly public route list so safe methods remain public while DELETE now returns 401 for anonymous requests. Also update Next.js native `.node` handling to avoid webpack parse failures from external packages such as ngrok and keytar, and add coverage for the new auth behavior. * build(compression): ship RTK rule and filter assets with app bundles Include compression JSON assets in Next output tracing, prepublish copies, and pack artifact policy checks so standalone and packaged builds can load RTK filters and caveman rule packs at runtime. Also harden compression runtime behavior by resolving alternate asset directories, scoping rule cache entries by source path, carrying RTK raw output pointers through stacked runs, degrading oversized preview diffs, and applying combo language/output mode defaults during chat routing. Add coverage for packaging rules, provider-scoped model parsing, smart truncate edge cases, raw output retention, and combo-driven compression behavior. * docs(workflows): update local repo paths to OmniRoute Replace outdated `/home/diegosouzapw/dev/proxys/9router` references with the current `OmniRoute` directory across deploy, release, and version bump workflow guides so local command examples match the renamed repository layout * feat(compression): complete RTK parity coverage * test(build): align next config assertions --------- Co-authored-by: diegosouzapw <diego.souza.pw@gmail.com> * feat(compression): expand caveman parity and MCP metadata compression Compress MCP registry and list metadata descriptions for tools, prompts, resources, and resource templates while keeping tool-response bodies unchanged. Expose those savings in compression status as `mcp_metadata_estimate` metadata rather than provider usage. Add Caveman rule-pack support for custom regex flags and match-specific replacement maps, update English rules for upstream parity, and tighten article and pleasantry handling. Also process RTK multipart text blocks independently so mixed media content compresses safely without duplicating output. * feat(compression): unify config validation and persist MCP savings Centralize compression config schemas across settings, preview, RTK, and combo APIs to enforce consistent validation for stacked pipelines and engine-specific options. Expand caveman and stacked compression behavior by applying default combos at runtime, surfacing validation and fallback metadata, and exposing aggressive and ultra adapter schemas for configuration UI and tests. Persist MCP description compression snapshots into analytics without counting them as provider usage, and extend the dashboard with the new RTK controls and localized labels. * fix(auth): require dashboard management auth for compression preview Block preview requests unless they come from a valid management session token so protected settings cannot be probed through the preview API. Add unit coverage for unauthenticated requests, invalid bearer tokens, and successful authenticated preview execution. * fix(compression): preserve stacked defaults and secure metadata routes Only apply saved default compression combos when they contain a stacked pipeline so seeded Caveman-only defaults do not replace the builtin stacked behavior. Also require management auth for compression language pack and rules metadata endpoints, and defer usage receipt attachment until compression analytics writes have completed to keep analytics records consistent. * fix(compression): align seeded standard savings combo with stacked default Update the seeded default compression combo to use the RTK then Caveman pipeline in both fresh installs and upgraded databases. Add a targeted migration and runtime guard that only rewrites the legacy seeded record when its original metadata and single-step pipeline still match, preserving user-customized default combos. Refresh docs and tests to reflect the stacked default and expanded RTK filter catalog. * docs(compression): document RTK+Caveman stacked savings ranges Refresh the compression docs and README to describe the default stacked pipeline in terms of eligible-context savings instead of the older generic token-saving range. Add upstream RTK and Caveman benchmark references, explain the multiplicative savings math behind the stacked default, and update feature summaries plus package metadata to match the revised positioning. * feat(image-gen): add NanoGPT image generation provider (#1899) Integrated into release/v3.7.9 * fix(codex): sanitize raw responses input (#1895) Integrated into release/v3.7.9 * Fix combo provider breaker profile handling (#1891) Integrated into release/v3.7.9 * fix(combos): align strategy contracts (#1892) Integrated into release/v3.7.9 * feat(proxy): move proxy configuration to dedicated System → Proxy page (#1907) Integrated into release/v3.7.9 * feat: add K/M/B/T cost shortener to prevent UI overflow (#1902) Integrated into release/v3.7.9 * feat(providers): implement bulk paste for extra API keys (#1916) Integrated into release/v3.7.9 * fix(migrations): treat duplicate-column ALTER as no-op (#1886) Integrated into release/v3.7.9 * fix(analytics): robust model pricing resolution, dark mode charts and SQL aggregation fixes (#1896) Integrated into release/v3.7.9 (migration renumbered to 044) * fix(oauth): per-connection mutex for rotating refresh tokens (#1885) Integrated into release/v3.7.9 * fix: resolve 3 bugs — Codex tool normalization (#1914), image gen proxy (#1904), zero-arg MCP tools (#1898) - fix(codex): flatten Chat Completions tool format to Responses format in normalizeCodexTools. Prevents 'Missing required parameter: tools[0].name' upstream errors when clients send {type:'function', function:{name,...}} instead of {type:'function', name,...}. - fix(proxy): add proxy-aware execution context to image generation route. Image requests now correctly use proxy settings from the connection's ProxyRegistry assignment, matching the pattern used by chat pipeline. - fix(translator): inject properties:{} into zero-argument MCP tool schemas during Anthropic→OpenAI translation. OpenAI strict mode requires explicit properties even for empty object schemas. Closes #1914, Closes #1904, Closes #1898 * chore(release): v3.7.9 — all changes in ONE commit * fix: allow local ollama provider connections (#1893) * fix(copilot): emit compatible reasoning text deltas (#1919) Integrated into release/v3.7.9 * fix(api-manager): show validation errors inline in modals, not behind backdrop (#1920) Integrated into release/v3.7.9 * docs: update changelog and pr body with merged prs * fix(providers): route agentrouter through anthropic endpoint headers Update the AgentRouter provider registry to use the Claude-compatible messages API and required Anthropic-style authentication headers. This bypasses unauthorized_client_error responses and exposes the supported model list through passthrough configuration. Also update the changelog and release PR notes to document the fix for #1921 * feat(logs): show compression tokens in request log UI (#1923) * docs: add PR #1923 to changelog --------- Co-authored-by: diegosouzapw <diego.souza.pw@gmail.com> Co-authored-by: backryun <bakryun0718@proton.me> Co-authored-by: Aculeasis <42580940+Aculeasis@users.noreply.github.com> Co-authored-by: Raxxoor <manker_lol@hotmail.com> Co-authored-by: Randi <55005611+rdself@users.noreply.github.com> Co-authored-by: Paijo <14921983+oyi77@users.noreply.github.com> Co-authored-by: Tubagus <54710482+0xtbug@users.noreply.github.com> Co-authored-by: smartenok-ops <smartenok@gmail.com> Co-authored-by: Gi99lin <74502520+Gi99lin@users.noreply.github.com> Co-authored-by: ivan-mezentsev <ivan@mezentsev.me> Co-authored-by: Andrew Munsell <andrew@wizardapps.net>
1118 lines
39 KiB
TypeScript
1118 lines
39 KiB
TypeScript
/**
|
|
* OmniRoute MCP Advanced Tools — 13 intelligence tools that differentiate
|
|
* OmniRoute from all other AI gateways.
|
|
*
|
|
* Tools:
|
|
* 1. omniroute_simulate_route — Dry-run routing simulation
|
|
* 2. omniroute_set_budget_guard — Session budget with degrade/block/alert
|
|
* 3. omniroute_set_routing_strategy — Runtime strategy switch for combos
|
|
* 4. omniroute_set_resilience_profile — Circuit breaker/retry profiles
|
|
* 5. omniroute_test_combo — Live test each provider in a combo
|
|
* 6. omniroute_get_provider_metrics — Detailed per-provider metrics
|
|
* 7. omniroute_best_combo_for_task — AI-powered combo recommendation
|
|
* 8. omniroute_explain_route — Post-hoc routing decision explainer
|
|
* 9. omniroute_get_session_snapshot — Full session state snapshot
|
|
* 10. omniroute_db_health_check — Diagnose and repair DB state drift
|
|
* 11. omniroute_sync_pricing — Sync provider pricing from external source
|
|
* 12. omniroute_cache_stats — Cache statistics and hit rates
|
|
* 13. omniroute_cache_flush — Flush/invalidate cache entries
|
|
*/
|
|
|
|
import { logToolCall } from "../audit.ts";
|
|
import { normalizeQuotaResponse } from "../../../src/shared/contracts/quota.ts";
|
|
import { resolveOmniRouteBaseUrl } from "../../../src/shared/utils/resolveOmniRouteBaseUrl.ts";
|
|
import {
|
|
getComboModelProvider,
|
|
getComboModelString,
|
|
getComboStepTarget,
|
|
} from "../../../src/lib/combos/steps.ts";
|
|
import type {
|
|
AutoRoutingStrategyValue,
|
|
RoutingStrategyValue,
|
|
} from "../../../src/shared/constants/routingStrategies.ts";
|
|
import { normalizeRoutingStrategy } from "../../../src/shared/constants/routingStrategies.ts";
|
|
|
|
const OMNIROUTE_BASE_URL = resolveOmniRouteBaseUrl();
|
|
const OMNIROUTE_API_KEY = process.env.OMNIROUTE_API_KEY || "";
|
|
|
|
async function apiFetch(path: string, options: RequestInit = {}): Promise<unknown> {
|
|
const url = `${OMNIROUTE_BASE_URL}${path}`;
|
|
const headers: Record<string, string> = {
|
|
"Content-Type": "application/json",
|
|
...(OMNIROUTE_API_KEY ? { Authorization: `Bearer ${OMNIROUTE_API_KEY}` } : {}),
|
|
...((options.headers as Record<string, string>) || {}),
|
|
};
|
|
const response = await fetch(url, { ...options, headers, signal: AbortSignal.timeout(30000) });
|
|
if (!response.ok) {
|
|
const text = await response.text().catch(() => "Unknown error");
|
|
throw new Error(`API [${response.status}]: ${text}`);
|
|
}
|
|
return response.json();
|
|
}
|
|
|
|
type JsonRecord = Record<string, unknown>;
|
|
|
|
interface ComboModel {
|
|
provider: string;
|
|
model: string;
|
|
inputCostPer1M: number;
|
|
}
|
|
|
|
function isRecord(value: unknown): value is JsonRecord {
|
|
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
}
|
|
|
|
function toRecord(value: unknown): JsonRecord {
|
|
return isRecord(value) ? value : {};
|
|
}
|
|
|
|
function toArrayOfRecords(value: unknown): JsonRecord[] {
|
|
return Array.isArray(value) ? value.filter(isRecord) : [];
|
|
}
|
|
|
|
function toString(value: unknown, fallback = ""): string {
|
|
return typeof value === "string" ? value : fallback;
|
|
}
|
|
|
|
function toNumber(value: unknown, fallback = 0): number {
|
|
const parsed =
|
|
typeof value === "number"
|
|
? value
|
|
: typeof value === "string" && value.trim().length > 0
|
|
? Number(value)
|
|
: Number.NaN;
|
|
return Number.isFinite(parsed) ? parsed : fallback;
|
|
}
|
|
|
|
function toBoolean(value: unknown, fallback = false): boolean {
|
|
return typeof value === "boolean" ? value : fallback;
|
|
}
|
|
|
|
function getComboModels(combo: JsonRecord): ComboModel[] {
|
|
const directModels = toArrayOfRecords(combo.models);
|
|
const nestedModels = toArrayOfRecords(toRecord(combo.data).models);
|
|
const sourceModels = directModels.length > 0 ? directModels : nestedModels;
|
|
return sourceModels.map((model) => ({
|
|
provider: getComboModelProvider(model) || (getComboModelString(model) ? "unknown" : "combo"),
|
|
model: getComboModelString(model) || getComboStepTarget(model) || "",
|
|
inputCostPer1M: toNumber(model.inputCostPer1M, 3.0),
|
|
}));
|
|
}
|
|
|
|
function normalizeCombosResponse(raw: unknown): JsonRecord[] {
|
|
if (Array.isArray(raw)) return raw.filter(isRecord);
|
|
const source = toRecord(raw);
|
|
return Array.isArray(source.combos) ? source.combos.filter(isRecord) : [];
|
|
}
|
|
|
|
// ============ In-Memory State ============
|
|
|
|
interface BudgetGuardState {
|
|
sessionId: string;
|
|
maxCost: number;
|
|
action: "degrade" | "block" | "alert";
|
|
degradeToTier?: "cheap" | "free";
|
|
spent: number;
|
|
createdAt: string;
|
|
}
|
|
|
|
let activeBudgetGuard: BudgetGuardState | null = null;
|
|
|
|
type ResilienceProfileConfig = {
|
|
requestQueue: {
|
|
requestsPerMinute: number;
|
|
minTimeBetweenRequestsMs: number;
|
|
concurrentRequests: number;
|
|
};
|
|
connectionCooldown: {
|
|
oauth: {
|
|
baseCooldownMs: number;
|
|
useUpstreamRetryHints: boolean;
|
|
maxBackoffSteps: number;
|
|
};
|
|
apikey: {
|
|
baseCooldownMs: number;
|
|
useUpstreamRetryHints: boolean;
|
|
maxBackoffSteps: number;
|
|
};
|
|
};
|
|
providerBreaker: {
|
|
oauth: {
|
|
failureThreshold: number;
|
|
resetTimeoutMs: number;
|
|
};
|
|
apikey: {
|
|
failureThreshold: number;
|
|
resetTimeoutMs: number;
|
|
};
|
|
};
|
|
};
|
|
|
|
const RESILIENCE_PROFILES = {
|
|
aggressive: {
|
|
requestQueue: {
|
|
requestsPerMinute: 180,
|
|
minTimeBetweenRequestsMs: 100,
|
|
concurrentRequests: 16,
|
|
},
|
|
connectionCooldown: {
|
|
oauth: {
|
|
baseCooldownMs: 30000,
|
|
useUpstreamRetryHints: false,
|
|
maxBackoffSteps: 4,
|
|
},
|
|
apikey: {
|
|
baseCooldownMs: 2000,
|
|
useUpstreamRetryHints: true,
|
|
maxBackoffSteps: 3,
|
|
},
|
|
},
|
|
providerBreaker: {
|
|
oauth: {
|
|
failureThreshold: 2,
|
|
resetTimeoutMs: 30000,
|
|
},
|
|
apikey: {
|
|
failureThreshold: 3,
|
|
resetTimeoutMs: 15000,
|
|
},
|
|
},
|
|
},
|
|
balanced: {
|
|
requestQueue: {
|
|
requestsPerMinute: 100,
|
|
minTimeBetweenRequestsMs: 200,
|
|
concurrentRequests: 10,
|
|
},
|
|
connectionCooldown: {
|
|
oauth: {
|
|
baseCooldownMs: 60000,
|
|
useUpstreamRetryHints: false,
|
|
maxBackoffSteps: 8,
|
|
},
|
|
apikey: {
|
|
baseCooldownMs: 3000,
|
|
useUpstreamRetryHints: true,
|
|
maxBackoffSteps: 5,
|
|
},
|
|
},
|
|
providerBreaker: {
|
|
oauth: {
|
|
failureThreshold: 3,
|
|
resetTimeoutMs: 60000,
|
|
},
|
|
apikey: {
|
|
failureThreshold: 5,
|
|
resetTimeoutMs: 30000,
|
|
},
|
|
},
|
|
},
|
|
conservative: {
|
|
requestQueue: {
|
|
requestsPerMinute: 60,
|
|
minTimeBetweenRequestsMs: 350,
|
|
concurrentRequests: 6,
|
|
},
|
|
connectionCooldown: {
|
|
oauth: {
|
|
baseCooldownMs: 120000,
|
|
useUpstreamRetryHints: false,
|
|
maxBackoffSteps: 10,
|
|
},
|
|
apikey: {
|
|
baseCooldownMs: 30000,
|
|
useUpstreamRetryHints: false,
|
|
maxBackoffSteps: 8,
|
|
},
|
|
},
|
|
providerBreaker: {
|
|
oauth: {
|
|
failureThreshold: 8,
|
|
resetTimeoutMs: 120000,
|
|
},
|
|
apikey: {
|
|
failureThreshold: 8,
|
|
resetTimeoutMs: 60000,
|
|
},
|
|
},
|
|
},
|
|
} satisfies Record<"aggressive" | "balanced" | "conservative", ResilienceProfileConfig>;
|
|
|
|
const TASK_FITNESS: Record<string, { preferred: string[]; traits: string[] }> = {
|
|
coding: { preferred: ["claude", "deepseek", "codex"], traits: ["fast", "code-optimized"] },
|
|
review: { preferred: ["claude", "gemini", "openai"], traits: ["analytical", "thorough"] },
|
|
planning: { preferred: ["gemini", "claude", "openai"], traits: ["reasoning", "structured"] },
|
|
analysis: { preferred: ["gemini", "claude"], traits: ["deep-reasoning", "large-context"] },
|
|
debugging: { preferred: ["claude", "deepseek", "codex"], traits: ["code-aware", "fast"] },
|
|
documentation: { preferred: ["gemini", "claude", "openai"], traits: ["clear", "structured"] },
|
|
};
|
|
|
|
// ============ Tool Handlers ============
|
|
|
|
export async function handleSimulateRoute(args: {
|
|
model: string;
|
|
promptTokenEstimate: number;
|
|
combo?: string;
|
|
}) {
|
|
const start = Date.now();
|
|
try {
|
|
// Fetch combos and health data for simulation
|
|
const [combosRaw, healthRaw, quotaRaw] = await Promise.allSettled([
|
|
apiFetch("/api/combos"),
|
|
apiFetch("/api/monitoring/health"),
|
|
apiFetch("/api/usage/quota"),
|
|
]);
|
|
|
|
const combos = combosRaw.status === "fulfilled" ? normalizeCombosResponse(combosRaw.value) : [];
|
|
const health = healthRaw.status === "fulfilled" ? toRecord(healthRaw.value) : {};
|
|
const quota =
|
|
quotaRaw.status === "fulfilled"
|
|
? normalizeQuotaResponse(quotaRaw.value)
|
|
: normalizeQuotaResponse({});
|
|
|
|
// Find target combo
|
|
const targetCombo = args.combo
|
|
? combos.find(
|
|
(combo) => toString(combo.id) === args.combo || toString(combo.name) === args.combo
|
|
)
|
|
: combos.find((combo) => combo.enabled !== false);
|
|
|
|
if (!targetCombo) {
|
|
return {
|
|
content: [
|
|
{ type: "text" as const, text: JSON.stringify({ error: "No matching combo found" }) },
|
|
],
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
const models = getComboModels(targetCombo);
|
|
const breakers = toArrayOfRecords(health.circuitBreakers);
|
|
const providers = quota.providers;
|
|
|
|
// Simulate path
|
|
const simulatedPath = models.map((model, idx: number) => {
|
|
const cb = breakers.find((breaker) => toString(breaker.provider) === model.provider);
|
|
const q = providers.find((providerEntry) => providerEntry.provider === model.provider);
|
|
const estimatedCost = (args.promptTokenEstimate / 1_000_000) * model.inputCostPer1M;
|
|
return {
|
|
provider: model.provider,
|
|
model: model.model || args.model,
|
|
probability: idx === 0 ? 0.85 : 0.15 / Math.max(models.length - 1, 1),
|
|
estimatedCost: Math.round(estimatedCost * 10000) / 10000,
|
|
healthStatus: toString(cb?.state, "CLOSED"),
|
|
quotaAvailable: q?.percentRemaining ?? 100,
|
|
};
|
|
});
|
|
|
|
const costs = simulatedPath.map((pathEntry) => pathEntry.estimatedCost);
|
|
const result = {
|
|
simulatedPath,
|
|
fallbackTree: {
|
|
primary: simulatedPath[0]?.provider || "unknown",
|
|
fallbacks: simulatedPath.slice(1).map((pathEntry) => pathEntry.provider),
|
|
worstCaseCost: Math.max(...costs, 0),
|
|
bestCaseCost: Math.min(...costs, 0),
|
|
},
|
|
};
|
|
|
|
await logToolCall("omniroute_simulate_route", args, result, Date.now() - start, true);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_simulate_route", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleSetBudgetGuard(args: {
|
|
maxCost: number;
|
|
action: "degrade" | "block" | "alert";
|
|
degradeToTier?: "cheap" | "free";
|
|
}) {
|
|
const start = Date.now();
|
|
try {
|
|
// Get current session cost
|
|
let spent = 0;
|
|
try {
|
|
const analytics = toRecord(await apiFetch("/api/usage/analytics?period=session"));
|
|
spent = toNumber(analytics.totalCost, 0);
|
|
} catch {
|
|
/* ignore if analytics not available */
|
|
}
|
|
|
|
activeBudgetGuard = {
|
|
sessionId: `budget_${Date.now()}`,
|
|
maxCost: args.maxCost,
|
|
action: args.action,
|
|
degradeToTier: args.degradeToTier,
|
|
spent,
|
|
createdAt: new Date().toISOString(),
|
|
};
|
|
|
|
const remaining = Math.max(0, args.maxCost - spent);
|
|
const result = {
|
|
sessionId: activeBudgetGuard.sessionId,
|
|
budgetTotal: args.maxCost,
|
|
budgetSpent: Math.round(spent * 10000) / 10000,
|
|
budgetRemaining: Math.round(remaining * 10000) / 10000,
|
|
action: args.action,
|
|
status: remaining <= 0 ? "exceeded" : remaining < args.maxCost * 0.2 ? "warning" : "active",
|
|
};
|
|
|
|
await logToolCall(
|
|
"omniroute_set_budget_guard",
|
|
{ maxCost: args.maxCost, action: args.action },
|
|
result,
|
|
Date.now() - start,
|
|
true
|
|
);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_set_budget_guard", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleSetRoutingStrategy(args: {
|
|
comboId: string;
|
|
strategy: RoutingStrategyValue;
|
|
autoRoutingStrategy?: AutoRoutingStrategyValue;
|
|
}) {
|
|
const start = Date.now();
|
|
try {
|
|
const combos = normalizeCombosResponse(await apiFetch("/api/combos"));
|
|
const combo = combos.find(
|
|
(comboEntry) =>
|
|
toString(comboEntry.id) === args.comboId || toString(comboEntry.name) === args.comboId
|
|
);
|
|
|
|
if (!combo) {
|
|
const msg = `Combo '${args.comboId}' not found`;
|
|
await logToolCall(
|
|
"omniroute_set_routing_strategy",
|
|
args,
|
|
null,
|
|
Date.now() - start,
|
|
false,
|
|
msg
|
|
);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
|
|
const comboId = toString(combo.id);
|
|
if (!comboId) {
|
|
const msg = "Matched combo has no id";
|
|
await logToolCall(
|
|
"omniroute_set_routing_strategy",
|
|
args,
|
|
null,
|
|
Date.now() - start,
|
|
false,
|
|
msg
|
|
);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
|
|
const comboData = toRecord(combo.data);
|
|
const currentConfig = toRecord(
|
|
Object.keys(toRecord(combo.config)).length > 0 ? combo.config : comboData.config
|
|
);
|
|
|
|
const normalizedStrategy = normalizeRoutingStrategy(args.strategy);
|
|
let nextConfig: JsonRecord | undefined = undefined;
|
|
if (normalizedStrategy === "auto" && args.autoRoutingStrategy) {
|
|
const currentAutoConfig = toRecord(currentConfig.auto);
|
|
nextConfig = {
|
|
...currentConfig,
|
|
auto: {
|
|
...currentAutoConfig,
|
|
routingStrategy: args.autoRoutingStrategy,
|
|
},
|
|
};
|
|
}
|
|
|
|
const payload: JsonRecord = { strategy: normalizedStrategy };
|
|
if (nextConfig && Object.keys(nextConfig).length > 0) {
|
|
payload.config = nextConfig;
|
|
}
|
|
|
|
const updatedCombo = toRecord(
|
|
await apiFetch(`/api/combos/${encodeURIComponent(comboId)}`, {
|
|
method: "PUT",
|
|
body: JSON.stringify(payload),
|
|
})
|
|
);
|
|
|
|
const updatedConfig = toRecord(updatedCombo.config);
|
|
const resolvedAutoStrategy =
|
|
toString(toRecord(updatedConfig.auto).routingStrategy) ||
|
|
(normalizedStrategy === "auto" ? (args.autoRoutingStrategy ?? "rules") : "");
|
|
|
|
const result = {
|
|
success: true,
|
|
combo: {
|
|
id: toString(updatedCombo.id, comboId),
|
|
name: toString(updatedCombo.name, toString(combo.name, comboId)),
|
|
strategy: toString(updatedCombo.strategy, normalizedStrategy),
|
|
autoRoutingStrategy:
|
|
toString(updatedCombo.strategy, normalizedStrategy) === "auto"
|
|
? resolvedAutoStrategy
|
|
: null,
|
|
},
|
|
};
|
|
|
|
await logToolCall("omniroute_set_routing_strategy", args, result, Date.now() - start, true);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_set_routing_strategy", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleSetResilienceProfile(args: {
|
|
profile: "aggressive" | "balanced" | "conservative";
|
|
}) {
|
|
const start = Date.now();
|
|
try {
|
|
const settings = RESILIENCE_PROFILES[args.profile];
|
|
if (!settings) {
|
|
return {
|
|
content: [{ type: "text" as const, text: `Error: Invalid profile "${args.profile}"` }],
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
// Apply to OmniRoute via API using the plan-aligned resilience structure.
|
|
await apiFetch("/api/resilience", {
|
|
method: "PATCH",
|
|
body: JSON.stringify(settings),
|
|
});
|
|
|
|
const result = { applied: true, profile: args.profile, settings };
|
|
|
|
await logToolCall("omniroute_set_resilience_profile", args, result, Date.now() - start, true);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall(
|
|
"omniroute_set_resilience_profile",
|
|
args,
|
|
null,
|
|
Date.now() - start,
|
|
false,
|
|
msg
|
|
);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleTestCombo(args: { comboId: string; testPrompt: string }) {
|
|
const start = Date.now();
|
|
try {
|
|
// Get combo details
|
|
const combos = normalizeCombosResponse(await apiFetch("/api/combos"));
|
|
const combo = combos.find(
|
|
(comboEntry) =>
|
|
toString(comboEntry.id) === args.comboId || toString(comboEntry.name) === args.comboId
|
|
);
|
|
if (!combo) {
|
|
return {
|
|
content: [
|
|
{
|
|
type: "text" as const,
|
|
text: JSON.stringify({ error: `Combo "${args.comboId}" not found` }),
|
|
},
|
|
],
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
const models = getComboModels(combo);
|
|
const prompt = (args.testPrompt || "Say hello").slice(0, 200);
|
|
|
|
// Test each provider in parallel
|
|
const results = await Promise.allSettled(
|
|
models.map(async (model) => {
|
|
const providerStart = Date.now();
|
|
try {
|
|
const resp = toRecord(
|
|
await apiFetch("/v1/chat/completions", {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
model: model.model || "auto",
|
|
messages: [{ role: "user", content: prompt }],
|
|
max_tokens: 50,
|
|
stream: false,
|
|
"x-provider": model.provider,
|
|
}),
|
|
})
|
|
);
|
|
const usage = toRecord(resp.usage);
|
|
|
|
return {
|
|
provider: model.provider,
|
|
model: model.model || toString(resp.model, "unknown"),
|
|
success: true,
|
|
latencyMs: Date.now() - providerStart,
|
|
cost: toNumber(resp.cost, 0),
|
|
tokenCount: toNumber(usage.prompt_tokens, 0) + toNumber(usage.completion_tokens, 0),
|
|
};
|
|
} catch (err) {
|
|
return {
|
|
provider: model.provider,
|
|
model: model.model || "unknown",
|
|
success: false,
|
|
latencyMs: Date.now() - providerStart,
|
|
cost: 0,
|
|
tokenCount: 0,
|
|
error: err instanceof Error ? err.message : String(err),
|
|
};
|
|
}
|
|
})
|
|
);
|
|
|
|
const providerResults = results.map((r) =>
|
|
r.status === "fulfilled"
|
|
? r.value
|
|
: {
|
|
provider: "unknown",
|
|
model: "unknown",
|
|
success: false,
|
|
latencyMs: 0,
|
|
cost: 0,
|
|
tokenCount: 0,
|
|
error: "Promise rejected",
|
|
}
|
|
);
|
|
const successful = providerResults.filter((r) => r.success);
|
|
const fastest = successful.sort((a, b) => a.latencyMs - b.latencyMs)[0];
|
|
const cheapest = successful.sort((a, b) => a.cost - b.cost)[0];
|
|
|
|
const result = {
|
|
results: providerResults,
|
|
summary: {
|
|
totalProviders: providerResults.length,
|
|
successful: successful.length,
|
|
fastestProvider: fastest?.provider || "none",
|
|
cheapestProvider: cheapest?.provider || "none",
|
|
},
|
|
};
|
|
|
|
await logToolCall(
|
|
"omniroute_test_combo",
|
|
{ comboId: args.comboId },
|
|
result.summary,
|
|
Date.now() - start,
|
|
true
|
|
);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_test_combo", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleGetProviderMetrics(args: { provider: string }) {
|
|
const start = Date.now();
|
|
try {
|
|
const [healthRaw, quotaRaw, analyticsRaw] = await Promise.allSettled([
|
|
apiFetch("/api/monitoring/health"),
|
|
apiFetch(`/api/usage/quota?provider=${encodeURIComponent(args.provider)}`),
|
|
apiFetch(`/api/usage/analytics?period=session&provider=${encodeURIComponent(args.provider)}`),
|
|
]);
|
|
|
|
const health = healthRaw.status === "fulfilled" ? toRecord(healthRaw.value) : {};
|
|
const quota =
|
|
quotaRaw.status === "fulfilled"
|
|
? normalizeQuotaResponse(quotaRaw.value, { provider: args.provider })
|
|
: normalizeQuotaResponse({});
|
|
const analytics = analyticsRaw.status === "fulfilled" ? toRecord(analyticsRaw.value) : {};
|
|
|
|
const cb = toArrayOfRecords(health.circuitBreakers).find(
|
|
(breaker) => toString(breaker.provider) === args.provider
|
|
);
|
|
const providerQuota = quota.providers.find((p) => p.provider === args.provider) || null;
|
|
|
|
const result = {
|
|
provider: args.provider,
|
|
successRate: toNumber(analytics.successRate, 1.0),
|
|
requestCount: toNumber(analytics.requestCount, 0),
|
|
avgLatencyMs: toNumber(analytics.avgLatencyMs, 0),
|
|
p50LatencyMs: toNumber(analytics.p50LatencyMs, 0),
|
|
p95LatencyMs: toNumber(analytics.p95LatencyMs, 0),
|
|
p99LatencyMs: toNumber(analytics.p99LatencyMs, 0),
|
|
errorRate: toNumber(analytics.errorRate, 0),
|
|
lastError: toString(analytics.lastError) || null,
|
|
circuitBreakerState: toString(cb?.state, "CLOSED"),
|
|
quotaInfo: providerQuota
|
|
? {
|
|
used: providerQuota.quotaUsed,
|
|
total: providerQuota.quotaTotal,
|
|
resetAt: providerQuota.resetAt,
|
|
}
|
|
: { used: 0, total: null, resetAt: null },
|
|
};
|
|
|
|
await logToolCall("omniroute_get_provider_metrics", args, result, Date.now() - start, true);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_get_provider_metrics", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleBestComboForTask(args: {
|
|
taskType: string;
|
|
budgetConstraint?: number;
|
|
latencyConstraint?: number;
|
|
}) {
|
|
const start = Date.now();
|
|
try {
|
|
const fitness = TASK_FITNESS[args.taskType] || TASK_FITNESS.coding;
|
|
const combos = normalizeCombosResponse(await apiFetch("/api/combos"));
|
|
const enabledCombos = combos.filter((combo) => combo.enabled !== false);
|
|
|
|
if (enabledCombos.length === 0) {
|
|
return {
|
|
content: [
|
|
{ type: "text" as const, text: JSON.stringify({ error: "No enabled combos available" }) },
|
|
],
|
|
isError: true,
|
|
};
|
|
}
|
|
|
|
// Score combos by task fitness
|
|
const scored = enabledCombos.map((combo) => {
|
|
const models = getComboModels(combo);
|
|
let score = 0;
|
|
|
|
// Provider preference scoring
|
|
for (const model of models) {
|
|
const prefIdx = fitness.preferred.indexOf(model.provider);
|
|
if (prefIdx >= 0) score += (fitness.preferred.length - prefIdx) * 10;
|
|
}
|
|
|
|
// Name-based trait scoring
|
|
const name = toString(combo.name).toLowerCase();
|
|
for (const trait of fitness.traits) {
|
|
if (name.includes(trait)) score += 5;
|
|
}
|
|
|
|
// Check if it's a free combo
|
|
const isFree =
|
|
name.includes("free") ||
|
|
models.every((model) => model.provider.toLowerCase().includes("free"));
|
|
|
|
return { combo, score, isFree };
|
|
});
|
|
|
|
scored.sort((a, b) => b.score - a.score);
|
|
const best = scored[0];
|
|
const alternatives = scored.slice(1, 4).map((s) => ({
|
|
id: s.combo.id,
|
|
name: s.combo.name,
|
|
tradeoff: s.isFree
|
|
? "free but may have limits"
|
|
: s.score < best.score * 0.5
|
|
? "cheaper but slower"
|
|
: "similar quality, different providers",
|
|
}));
|
|
const freeAlt = scored.find((s) => s.isFree && s !== best);
|
|
|
|
const result = {
|
|
recommendedCombo: {
|
|
id: best.combo.id,
|
|
name: best.combo.name,
|
|
reason: `Best match for "${args.taskType}": preferred providers (${fitness.preferred.slice(0, 3).join(", ")})`,
|
|
},
|
|
alternatives,
|
|
freeAlternative: freeAlt ? { id: freeAlt.combo.id, name: freeAlt.combo.name } : null,
|
|
};
|
|
|
|
await logToolCall(
|
|
"omniroute_best_combo_for_task",
|
|
args,
|
|
result.recommendedCombo,
|
|
Date.now() - start,
|
|
true
|
|
);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_best_combo_for_task", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleExplainRoute(args: { requestId: string }) {
|
|
const start = Date.now();
|
|
try {
|
|
// Query routing_decisions table via API
|
|
let decision: JsonRecord | null = null;
|
|
try {
|
|
decision = toRecord(
|
|
await apiFetch(`/api/routing/decisions/${encodeURIComponent(args.requestId)}`)
|
|
);
|
|
} catch {
|
|
// Fall back to a generic explanation
|
|
}
|
|
|
|
const result = decision
|
|
? {
|
|
requestId: args.requestId,
|
|
decision: {
|
|
comboUsed: decision.comboUsed || "default",
|
|
providerSelected: decision.providerSelected || "unknown",
|
|
modelUsed: decision.modelUsed || "unknown",
|
|
score: decision.score || 0,
|
|
factors: decision.factors || [
|
|
{ name: "health", value: 1, weight: 0.3, contribution: 0.3 },
|
|
{ name: "quota", value: 1, weight: 0.25, contribution: 0.25 },
|
|
{ name: "cost", value: 0.8, weight: 0.2, contribution: 0.16 },
|
|
{ name: "latency", value: 0.9, weight: 0.15, contribution: 0.135 },
|
|
{ name: "task_fit", value: 0.7, weight: 0.1, contribution: 0.07 },
|
|
],
|
|
fallbacksTriggered: decision.fallbacksTriggered || [],
|
|
costActual: decision.costActual || 0,
|
|
latencyActual: decision.latencyActual || 0,
|
|
},
|
|
}
|
|
: {
|
|
requestId: args.requestId,
|
|
decision: {
|
|
comboUsed: "unknown",
|
|
providerSelected: "unknown",
|
|
modelUsed: "unknown",
|
|
score: 0,
|
|
factors: [],
|
|
fallbacksTriggered: [],
|
|
costActual: 0,
|
|
latencyActual: 0,
|
|
},
|
|
note: "Routing decision not found. The /api/routing/decisions endpoint may not be implemented yet, or the requestId is invalid.",
|
|
};
|
|
|
|
await logToolCall(
|
|
"omniroute_explain_route",
|
|
args,
|
|
{ requestId: args.requestId },
|
|
Date.now() - start,
|
|
true
|
|
);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_explain_route", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleSyncPricing(args: { sources?: string[]; dryRun?: boolean }) {
|
|
const start = Date.now();
|
|
try {
|
|
const result = toRecord(
|
|
await apiFetch("/api/pricing/sync", {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
sources: args.sources,
|
|
dryRun: args.dryRun ?? false,
|
|
}),
|
|
})
|
|
);
|
|
|
|
await logToolCall("omniroute_sync_pricing", args, result, Date.now() - start, true);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_sync_pricing", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleGetSessionSnapshot() {
|
|
const start = Date.now();
|
|
try {
|
|
const analytics = toRecord(
|
|
await apiFetch("/api/usage/analytics?period=session").catch(() => ({}))
|
|
);
|
|
const tokenCount = toRecord(analytics.tokenCount);
|
|
const byModel = toArrayOfRecords(analytics.byModel);
|
|
const byProvider = toArrayOfRecords(analytics.byProvider);
|
|
|
|
const result = {
|
|
sessionStart: toString(analytics.sessionStart, new Date().toISOString()),
|
|
duration: toString(analytics.duration, "unknown"),
|
|
requestCount: toNumber(analytics.requestCount, 0),
|
|
costTotal: toNumber(analytics.totalCost, 0),
|
|
tokenCount: {
|
|
prompt: toNumber(tokenCount.prompt, 0),
|
|
completion: toNumber(tokenCount.completion, 0),
|
|
},
|
|
topModels: byModel.slice(0, 5).map((model) => ({
|
|
model: toString(model.model, "unknown"),
|
|
count: toNumber(model.requests, 0),
|
|
})),
|
|
topProviders: byProvider.slice(0, 5).map((provider) => ({
|
|
provider: toString(provider.name, "unknown"),
|
|
count: toNumber(provider.requests, 0),
|
|
})),
|
|
errors: toNumber(analytics.errorCount, 0),
|
|
fallbacks: toNumber(analytics.fallbackCount, 0),
|
|
budgetGuard: activeBudgetGuard
|
|
? {
|
|
active: true,
|
|
remaining: Math.max(0, activeBudgetGuard.maxCost - activeBudgetGuard.spent),
|
|
action: activeBudgetGuard.action,
|
|
}
|
|
: null,
|
|
};
|
|
|
|
await logToolCall(
|
|
"omniroute_get_session_snapshot",
|
|
{},
|
|
{ requestCount: result.requestCount },
|
|
Date.now() - start,
|
|
true
|
|
);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_get_session_snapshot", {}, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleDbHealthCheck(args: { autoRepair?: boolean }) {
|
|
const start = Date.now();
|
|
const autoRepair = args.autoRepair === true;
|
|
|
|
try {
|
|
const { runManagedDbHealthCheck } = await import("../../../src/lib/db/core.ts");
|
|
const result = runManagedDbHealthCheck({ autoRepair });
|
|
|
|
await logToolCall(
|
|
"omniroute_db_health_check",
|
|
args,
|
|
{
|
|
isHealthy: toBoolean(result.isHealthy, false),
|
|
repairedCount: toNumber(result.repairedCount, 0),
|
|
},
|
|
Date.now() - start,
|
|
true
|
|
);
|
|
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_db_health_check", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleCacheStats() {
|
|
const start = Date.now();
|
|
try {
|
|
const raw = toRecord(await apiFetch("/api/cache"));
|
|
const semanticCache = toRecord(raw.semanticCache);
|
|
const promptCache = raw.promptCache ? toRecord(raw.promptCache) : null;
|
|
const idempotency = toRecord(raw.idempotency);
|
|
const config = raw.config ? toRecord(raw.config) : null;
|
|
|
|
const result = {
|
|
semanticCache: {
|
|
memoryEntries: toNumber(semanticCache.memoryEntries, 0),
|
|
dbEntries: toNumber(semanticCache.dbEntries, 0),
|
|
hits: toNumber(semanticCache.hits, 0),
|
|
misses: toNumber(semanticCache.misses, 0),
|
|
hitRate: toString(semanticCache.hitRate, "0%"),
|
|
tokensSaved: toNumber(semanticCache.tokensSaved, 0),
|
|
},
|
|
promptCache: promptCache
|
|
? {
|
|
totalRequests: toNumber(promptCache.totalRequests, 0),
|
|
requestsWithCacheControl: toNumber(promptCache.requestsWithCacheControl, 0),
|
|
totalInputTokens: toNumber(promptCache.totalInputTokens, 0),
|
|
totalCachedTokens: toNumber(promptCache.totalCachedTokens, 0),
|
|
totalCacheCreationTokens: toNumber(promptCache.totalCacheCreationTokens, 0),
|
|
tokensSaved: toNumber(promptCache.tokensSaved, 0),
|
|
estimatedCostSaved: toNumber(promptCache.estimatedCostSaved, 0),
|
|
}
|
|
: null,
|
|
idempotency: {
|
|
activeKeys: toNumber(idempotency.activeKeys, 0),
|
|
windowMs: toNumber(idempotency.windowMs, 0),
|
|
},
|
|
config: config
|
|
? {
|
|
semanticCacheEnabled: toBoolean(config.semanticCacheEnabled, true),
|
|
}
|
|
: undefined,
|
|
};
|
|
|
|
await logToolCall("omniroute_cache_stats", {}, result, Date.now() - start, true);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_cache_stats", {}, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleCacheFlush(args: { signature?: string; model?: string }) {
|
|
const start = Date.now();
|
|
try {
|
|
const params = new URLSearchParams();
|
|
let scope = "all";
|
|
|
|
if (args.signature) {
|
|
params.set("signature", args.signature);
|
|
scope = "signature";
|
|
} else if (args.model) {
|
|
params.set("model", args.model);
|
|
scope = "model";
|
|
}
|
|
|
|
const query = params.toString();
|
|
const path = query ? `/api/cache?${query}` : "/api/cache";
|
|
const raw = toRecord(
|
|
await apiFetch(path, {
|
|
method: "DELETE",
|
|
})
|
|
);
|
|
|
|
const result = {
|
|
ok: toBoolean(raw.ok, true),
|
|
invalidated: toNumber(raw.invalidated ?? raw.cleared, 0),
|
|
scope,
|
|
};
|
|
|
|
await logToolCall("omniroute_cache_flush", args, result, Date.now() - start, true);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_cache_flush", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
// ============ 1proxy Tools ============
|
|
|
|
export async function handleOneproxyFetch(
|
|
args: { protocol?: string; countryCode?: string; minQuality?: number; limit?: number } = {}
|
|
) {
|
|
const start = Date.now();
|
|
try {
|
|
const params = new URLSearchParams();
|
|
if (args.protocol) params.set("protocol", args.protocol);
|
|
if (args.countryCode) params.set("countryCode", args.countryCode);
|
|
if (args.minQuality) params.set("minQuality", String(args.minQuality));
|
|
if (args.limit) params.set("limit", String(args.limit));
|
|
|
|
const query = params.toString();
|
|
const path = query ? `/api/settings/oneproxy?${query}` : "/api/settings/oneproxy";
|
|
const raw = toRecord(await apiFetch(path));
|
|
|
|
const items = toArrayOfRecords(raw.items).map((r) => ({
|
|
id: toString(r.id, ""),
|
|
host: toString(r.host, ""),
|
|
port: toNumber(r.port, 0),
|
|
type: toString(r.type, "http"),
|
|
countryCode: typeof r.country_code === "string" ? r.country_code : null,
|
|
qualityScore: r.quality_score != null ? toNumber(r.quality_score) : null,
|
|
latencyMs: r.latency_ms != null ? toNumber(r.latency_ms) : null,
|
|
anonymity: typeof r.anonymity === "string" ? r.anonymity : null,
|
|
googleAccess: r.google_access === 1 || r.google_access === true,
|
|
status: toString(r.status, "active"),
|
|
}));
|
|
|
|
const result = { items, total: toNumber(raw.total, items.length) };
|
|
await logToolCall("omniroute_oneproxy_fetch", args, result, Date.now() - start, true);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_oneproxy_fetch", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleOneproxyRotate(
|
|
args: { strategy?: "random" | "quality" | "sequential" } = {}
|
|
) {
|
|
const start = Date.now();
|
|
try {
|
|
const body: Record<string, unknown> = {};
|
|
if (args.strategy) body.strategy = args.strategy;
|
|
|
|
const raw = toRecord(
|
|
await apiFetch("/api/settings/oneproxy/rotate", {
|
|
method: "POST",
|
|
body: JSON.stringify(body),
|
|
})
|
|
);
|
|
|
|
const result = {
|
|
id: toString(raw.id, ""),
|
|
host: toString(raw.host, ""),
|
|
port: toNumber(raw.port, 0),
|
|
type: toString(raw.type, "http"),
|
|
countryCode: typeof raw.country_code === "string" ? raw.country_code : null,
|
|
qualityScore: raw.quality_score != null ? toNumber(raw.quality_score) : null,
|
|
latencyMs: raw.latency_ms != null ? toNumber(raw.latency_ms) : null,
|
|
};
|
|
|
|
await logToolCall("omniroute_oneproxy_rotate", args, result, Date.now() - start, true);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_oneproxy_rotate", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|
|
|
|
export async function handleOneproxyStats(args: Record<string, never> = {}) {
|
|
const start = Date.now();
|
|
try {
|
|
const raw = toRecord(await apiFetch("/api/settings/oneproxy?action=stats"));
|
|
|
|
const statsRaw = toRecord(raw.stats);
|
|
const statusRaw = toRecord(raw.status);
|
|
|
|
const stats = {
|
|
total: toNumber(statsRaw.total, 0),
|
|
active: toNumber(statsRaw.active, 0),
|
|
avgQuality: statsRaw.avg_quality != null ? toNumber(statsRaw.avg_quality) : null,
|
|
lastValidated: typeof statsRaw.last_validated === "string" ? statsRaw.last_validated : null,
|
|
byProtocol: toArrayOfRecords(statsRaw.by_protocol || statsRaw.byProtocol).map((r) => ({
|
|
protocol: toString(r.protocol, ""),
|
|
count: toNumber(r.count, 0),
|
|
})),
|
|
byCountry: toArrayOfRecords(statsRaw.by_country || statsRaw.byCountry).map((r) => ({
|
|
countryCode: toString(r.countryCode || r.country_code, ""),
|
|
count: toNumber(r.count, 0),
|
|
})),
|
|
};
|
|
|
|
const status = {
|
|
lastSyncSuccess: toBoolean(statusRaw.last_sync_success, false),
|
|
lastSyncError:
|
|
typeof statusRaw.last_sync_error === "string" ? statusRaw.last_sync_error : null,
|
|
lastSyncAt: typeof statusRaw.last_sync_at === "string" ? statusRaw.last_sync_at : null,
|
|
lastSyncCount: toNumber(statusRaw.last_sync_count, 0),
|
|
consecutiveFailures: toNumber(statusRaw.consecutive_failures, 0),
|
|
};
|
|
|
|
const result = { stats, status };
|
|
await logToolCall("omniroute_oneproxy_stats", args, result, Date.now() - start, true);
|
|
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
await logToolCall("omniroute_oneproxy_stats", args, null, Date.now() - start, false, msg);
|
|
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
|
|
}
|
|
}
|