feat(server): turn the LiteLLM proxy off by default (P1.1)
Every sidecar start outside the desktop app looked for Python, probed
each interpreter for litellm and, when it found one, started a LiteLLM
child. The community edition must not need Python, so LiteLLM is now
opt-in: startService starts it only when config.json contains
"litellm": { "enabled": true } (WaggleConfig.isLiteLLMEnabled). Otherwise
there is no Python lookup, no LiteLLM health probe and no child process,
and the built-in provider proxy serves every request, as it already did
in the desktop app. --skip-litellm and WAGGLE_SKIP_LITELLM=1 still force
it off.
The lookup for a bundled resources/python/python.exe is removed: no
build ships one. When LiteLLM is enabled it runs on the user's own
Python from PATH.
This commit is contained in:
@@ -50,6 +50,13 @@ interface ConfigData {
|
|||||||
threshold?: number;
|
threshold?: number;
|
||||||
topK?: number;
|
topK?: number;
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* Optional LiteLLM proxy. Off unless `enabled` is true; it needs the user's
|
||||||
|
* own Python with litellm installed. The built-in provider proxy is the default.
|
||||||
|
*/
|
||||||
|
litellm?: {
|
||||||
|
enabled?: boolean;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Resolved MCP tool-retrieval config (all fields present). */
|
/** Resolved MCP tool-retrieval config (all fields present). */
|
||||||
@@ -213,6 +220,12 @@ export class WaggleConfig {
|
|||||||
this.data.cli = { ...(this.data.cli ?? {}), allowlist: next };
|
this.data.cli = { ...(this.data.cli ?? {}), allowlist: next };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- LiteLLM proxy (opt-in) ---
|
||||||
|
|
||||||
|
isLiteLLMEnabled(): boolean {
|
||||||
|
return this.data.litellm?.enabled === true;
|
||||||
|
}
|
||||||
|
|
||||||
// --- Telemetry (M2-7) ---
|
// --- Telemetry (M2-7) ---
|
||||||
|
|
||||||
getTelemetryEnabled(): boolean {
|
getTelemetryEnabled(): boolean {
|
||||||
|
|||||||
@@ -120,6 +120,28 @@ describe('WaggleConfig', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('LiteLLM opt-in', () => {
|
||||||
|
it('is off by default and when not explicitly enabled', () => {
|
||||||
|
const configDir = makeTempDir();
|
||||||
|
expect(new WaggleConfig(configDir).isLiteLLMEnabled()).toBe(false);
|
||||||
|
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(configDir, 'config.json'),
|
||||||
|
JSON.stringify({ defaultModel: 'claude-sonnet-4-6', providers: {}, litellm: {} }),
|
||||||
|
);
|
||||||
|
expect(new WaggleConfig(configDir).isLiteLLMEnabled()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('is on when config.json sets litellm.enabled to true', () => {
|
||||||
|
const configDir = makeTempDir();
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(configDir, 'config.json'),
|
||||||
|
JSON.stringify({ defaultModel: 'claude-sonnet-4-6', providers: {}, litellm: { enabled: true } }),
|
||||||
|
);
|
||||||
|
expect(new WaggleConfig(configDir).isLiteLLMEnabled()).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('governed CLI config', () => {
|
describe('governed CLI config', () => {
|
||||||
it('normalizes, deduplicates, and persists the CLI allowlist', () => {
|
it('normalizes, deduplicates, and persists the CLI allowlist', () => {
|
||||||
const configDir = makeTempDir();
|
const configDir = makeTempDir();
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import { spawnSync, type ChildProcess } from 'node:child_process';
|
import { spawnSync, type ChildProcess } from 'node:child_process';
|
||||||
import { existsSync, openSync } from 'node:fs';
|
import { openSync } from 'node:fs';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
|
||||||
import { spawnSidecarOwnedProcess } from '@waggle/agent';
|
import { spawnSidecarOwnedProcess } from '@waggle/agent';
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
|
||||||
const __dirname = path.dirname(__filename);
|
|
||||||
|
|
||||||
export interface LiteLLMStatus {
|
export interface LiteLLMStatus {
|
||||||
status: 'running' | 'started' | 'timeout' | 'error';
|
status: 'running' | 'started' | 'timeout' | 'error';
|
||||||
port: number;
|
port: number;
|
||||||
@@ -79,32 +75,6 @@ async function stopOwnedLiteLLMProcess(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Look for a bundled Python executable in the app's resources directory.
|
|
||||||
* On an installed Tauri app the layout is:
|
|
||||||
* {exe_dir}/resources/python/python.exe
|
|
||||||
* During development we also check relative to this source file:
|
|
||||||
* app/src-tauri/resources/python/python.exe
|
|
||||||
*
|
|
||||||
* Returns the absolute path if found, otherwise null (falls back to system PATH).
|
|
||||||
*/
|
|
||||||
export function getBundledPythonPath(): string | null {
|
|
||||||
// Installed app: next to the running executable
|
|
||||||
const exeDir = path.dirname(process.execPath);
|
|
||||||
const installedPath = path.join(exeDir, 'resources', 'python', 'python.exe');
|
|
||||||
if (existsSync(installedPath)) {
|
|
||||||
return installedPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Development: relative to this file → ../../app/src-tauri/resources
|
|
||||||
const devPath = path.resolve(__dirname, '..', '..', '..', 'app', 'src-tauri', 'resources', 'python', 'python.exe');
|
|
||||||
if (existsSync(devPath)) {
|
|
||||||
return devPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function selectLiteLLMPython(
|
export function selectLiteLLMPython(
|
||||||
candidates: readonly string[],
|
candidates: readonly string[],
|
||||||
supportsLiteLLM: (pythonBin: string) => boolean,
|
supportsLiteLLM: (pythonBin: string) => boolean,
|
||||||
@@ -169,13 +139,9 @@ function canImportLiteLLM(pythonBin: string): boolean {
|
|||||||
return result.status === 0 && !result.error;
|
return result.status === 0 && !result.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The user's own Python with litellm installed; Waggle does not ship one. */
|
||||||
export function resolveLiteLLMPython(): string | null {
|
export function resolveLiteLLMPython(): string | null {
|
||||||
const bundledPython = getBundledPythonPath();
|
return selectLiteLLMPython(discoverSystemPythonPaths(), canImportLiteLLM);
|
||||||
const candidates = [
|
|
||||||
...(bundledPython ? [bundledPython] : []),
|
|
||||||
...discoverSystemPythonPaths(),
|
|
||||||
];
|
|
||||||
return selectLiteLLMPython(candidates, canImportLiteLLM);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkHealth(port: number): Promise<boolean> {
|
async function checkHealth(port: number): Promise<boolean> {
|
||||||
@@ -205,7 +171,7 @@ export async function getLiteLLMStatus(port?: number): Promise<LiteLLMStatus> {
|
|||||||
/**
|
/**
|
||||||
* Start LiteLLM proxy. If already running, returns immediately.
|
* Start LiteLLM proxy. If already running, returns immediately.
|
||||||
* Otherwise spawns `python -m litellm.proxy.proxy_cli --port {port}` and polls health.
|
* Otherwise spawns `python -m litellm.proxy.proxy_cli --port {port}` and polls health.
|
||||||
* Prefers a compatible bundled Python, then probes every Python on system PATH.
|
* Probes every Python on the system PATH for one that can import LiteLLM.
|
||||||
*/
|
*/
|
||||||
export async function startLiteLLM(port?: number, configPath?: string): Promise<LiteLLMStatus> {
|
export async function startLiteLLM(port?: number, configPath?: string): Promise<LiteLLMStatus> {
|
||||||
const p = port ?? DEFAULT_PORT;
|
const p = port ?? DEFAULT_PORT;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import net from 'node:net';
|
|||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
import type { FastifyInstance } from 'fastify';
|
import type { FastifyInstance } from 'fastify';
|
||||||
import { needsMigration, migrateToMultiMind, MindDB, VaultStore } from '@waggle/core';
|
import { needsMigration, migrateToMultiMind, MindDB, VaultStore, WaggleConfig } from '@waggle/core';
|
||||||
import { buildLocalServer } from './index.js';
|
import { buildLocalServer } from './index.js';
|
||||||
import type { LlmHealthStatus } from './index.js';
|
import type { LlmHealthStatus } from './index.js';
|
||||||
import { startLiteLLM, stopLiteLLM, type LiteLLMStatus } from './lifecycle.js';
|
import { startLiteLLM, stopLiteLLM, type LiteLLMStatus } from './lifecycle.js';
|
||||||
@@ -175,7 +175,7 @@ function getConfiguredProviderIds(dataDir: string, server?: FastifyInstance): st
|
|||||||
* 1. Resolves/creates dataDir (option > WAGGLE_DATA_DIR > ~/.waggle)
|
* 1. Resolves/creates dataDir (option > WAGGLE_DATA_DIR > ~/.waggle)
|
||||||
* 2. Runs migration if needed (default.mind -> personal.mind)
|
* 2. Runs migration if needed (default.mind -> personal.mind)
|
||||||
* 3. Creates personal.mind if fresh install
|
* 3. Creates personal.mind if fresh install
|
||||||
* 4. Starts LiteLLM proxy (unless skipped)
|
* 4. Starts the LiteLLM proxy when config.json enables it and it is not skipped
|
||||||
* 5. Builds & starts local Fastify server
|
* 5. Builds & starts local Fastify server
|
||||||
* 6. Registers graceful shutdown handlers
|
* 6. Registers graceful shutdown handlers
|
||||||
*
|
*
|
||||||
@@ -256,12 +256,20 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
|||||||
mind.close();
|
mind.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Hydrate provider credentials before the LiteLLM child snapshots env,
|
// 4. LiteLLM is optional and runs on the user's own Python, so it starts
|
||||||
// then generate its concrete model catalog from provider APIs.
|
// only when config.json enables it ("litellm": { "enabled": true }). The
|
||||||
emit({ phase: 'litellm', message: skipLiteLLM ? 'Skipping LiteLLM proxy...' : 'Starting LiteLLM proxy...', progress: 0.5 });
|
// built-in provider proxy serves every request otherwise. When it does
|
||||||
|
// start, hydrate provider credentials before the child snapshots env, then
|
||||||
|
// generate its concrete model catalog from provider APIs.
|
||||||
|
const useLiteLLM = !skipLiteLLM && new WaggleConfig(dataDir).isLiteLLMEnabled();
|
||||||
|
emit({ phase: 'litellm', message: useLiteLLM ? 'Starting LiteLLM proxy...' : 'Skipping LiteLLM proxy...', progress: 0.5 });
|
||||||
let litellm: LiteLLMStatus;
|
let litellm: LiteLLMStatus;
|
||||||
if (skipLiteLLM) {
|
if (!useLiteLLM) {
|
||||||
litellm = { status: 'error', port: litellmPort, error: 'Skipped' };
|
litellm = {
|
||||||
|
status: 'error',
|
||||||
|
port: litellmPort,
|
||||||
|
error: skipLiteLLM ? 'Skipped' : 'Not enabled in config.json',
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
const startupVault = new VaultStore(dataDir);
|
const startupVault = new VaultStore(dataDir);
|
||||||
migrateLegacyProviderKeysToVault(dataDir, startupVault);
|
migrateLegacyProviderKeysToVault(dataDir, startupVault);
|
||||||
@@ -275,7 +283,7 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
|||||||
const managedLiteLLMUrl = `http://localhost:${litellmPort}`;
|
const managedLiteLLMUrl = `http://localhost:${litellmPort}`;
|
||||||
let selfProxyUrl = `http://127.0.0.1:${port}/v1`;
|
let selfProxyUrl = `http://127.0.0.1:${port}/v1`;
|
||||||
let litellmReachable = false;
|
let litellmReachable = false;
|
||||||
if (!skipLiteLLM) {
|
if (useLiteLLM) {
|
||||||
try {
|
try {
|
||||||
const healthRes = await fetch(`${managedLiteLLMUrl}/health/liveliness`, {
|
const healthRes = await fetch(`${managedLiteLLMUrl}/health/liveliness`, {
|
||||||
signal: AbortSignal.timeout(2000),
|
signal: AbortSignal.timeout(2000),
|
||||||
@@ -294,7 +302,7 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
|||||||
port,
|
port,
|
||||||
instanceId: allowDesktopPortFallback ? desktopInstanceId : undefined,
|
instanceId: allowDesktopPortFallback ? desktopInstanceId : undefined,
|
||||||
litellmUrl: litellmReachable ? managedLiteLLMUrl : selfProxyUrl,
|
litellmUrl: litellmReachable ? managedLiteLLMUrl : selfProxyUrl,
|
||||||
manageLiteLLM: !skipLiteLLM,
|
manageLiteLLM: useLiteLLM,
|
||||||
managedLiteLLMPort: litellmPort,
|
managedLiteLLMPort: litellmPort,
|
||||||
useBuiltInProxy: !litellmReachable,
|
useBuiltInProxy: !litellmReachable,
|
||||||
startOfflineManagerOnListen: false,
|
startOfflineManagerOnListen: false,
|
||||||
@@ -305,7 +313,7 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
|||||||
process.off('SIGTERM', shutdown);
|
process.off('SIGTERM', shutdown);
|
||||||
process.off('SIGINT', shutdown);
|
process.off('SIGINT', shutdown);
|
||||||
await server.close();
|
await server.close();
|
||||||
if (!skipLiteLLM) {
|
if (useLiteLLM) {
|
||||||
await stopLiteLLM();
|
await stopLiteLLM();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -325,7 +333,7 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
|||||||
const bindHost = allowDesktopPortFallback ? '127.0.0.1' : resolveBindHost();
|
const bindHost = allowDesktopPortFallback ? '127.0.0.1' : resolveBindHost();
|
||||||
const cleanupListenFailure = async (): Promise<void> => {
|
const cleanupListenFailure = async (): Promise<void> => {
|
||||||
try { await server.close(); } catch { /* preserve the listen error */ }
|
try { await server.close(); } catch { /* preserve the listen error */ }
|
||||||
if (!skipLiteLLM) await stopLiteLLM().catch(() => undefined);
|
if (useLiteLLM) await stopLiteLLM().catch(() => undefined);
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
await server.listen({ port, host: bindHost });
|
await server.listen({ port, host: bindHost });
|
||||||
@@ -351,7 +359,7 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
|||||||
const listeningAddress = server.server.address();
|
const listeningAddress = server.server.address();
|
||||||
if (!listeningAddress || typeof listeningAddress === 'string') {
|
if (!listeningAddress || typeof listeningAddress === 'string') {
|
||||||
await server.close();
|
await server.close();
|
||||||
if (!skipLiteLLM) await stopLiteLLM().catch(() => undefined);
|
if (useLiteLLM) await stopLiteLLM().catch(() => undefined);
|
||||||
throw new Error('Unable to resolve the Waggle service listen port');
|
throw new Error('Unable to resolve the Waggle service listen port');
|
||||||
}
|
}
|
||||||
const actualPort = listeningAddress.port;
|
const actualPort = listeningAddress.port;
|
||||||
@@ -403,7 +411,9 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (litellm.status !== 'running' && litellm.status !== 'started') {
|
if (!useLiteLLM) {
|
||||||
|
log.info('LiteLLM is off, using built-in provider proxy');
|
||||||
|
} else if (litellm.status !== 'running' && litellm.status !== 'started') {
|
||||||
log.info(`LiteLLM unavailable (${litellm.status}), using built-in provider proxy`);
|
log.info(`LiteLLM unavailable (${litellm.status}), using built-in provider proxy`);
|
||||||
} else {
|
} else {
|
||||||
log.info(`LiteLLM not reachable, using built-in provider proxy`);
|
log.info(`LiteLLM not reachable, using built-in provider proxy`);
|
||||||
|
|||||||
@@ -315,6 +315,59 @@ describe('Agent Service', () => {
|
|||||||
}]);
|
}]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('leaves LiteLLM off and uses the built-in proxy unless config.json enables it', async () => {
|
||||||
|
const dataDir = makeTmpDir();
|
||||||
|
tmpDirs.push(dataDir);
|
||||||
|
const port = randomPort();
|
||||||
|
const litellmPort = randomPort();
|
||||||
|
|
||||||
|
clearProviderEnv();
|
||||||
|
vi.stubEnv('OPENAI_API_KEY', 'openai-solo-test-key');
|
||||||
|
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation(
|
||||||
|
async () => new Response('{}', { status: 503 }),
|
||||||
|
);
|
||||||
|
|
||||||
|
const { server, litellm } = await startService({ dataDir, port, litellmPort });
|
||||||
|
cleanups.push(async () => { await server.close(); });
|
||||||
|
|
||||||
|
expect(litellm).toEqual({ status: 'error', port: litellmPort, error: 'Not enabled in config.json' });
|
||||||
|
expect(server.localConfig.manageLiteLLM).toBe(false);
|
||||||
|
expect(server.localConfig.useBuiltInProxy).toBe(true);
|
||||||
|
expect(server.localConfig.litellmUrl).toBe(`http://127.0.0.1:${port}/v1`);
|
||||||
|
expect(server.agentState.llmProvider.provider).toBe('anthropic-proxy');
|
||||||
|
expect(fetchSpy.mock.calls.some(([input]) => (
|
||||||
|
String(input) === `http://localhost:${litellmPort}/health/liveliness`
|
||||||
|
))).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('manages LiteLLM when config.json enables it', async () => {
|
||||||
|
const dataDir = makeTmpDir();
|
||||||
|
tmpDirs.push(dataDir);
|
||||||
|
const port = randomPort();
|
||||||
|
const litellmPort = randomPort();
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(dataDir, 'config.json'),
|
||||||
|
JSON.stringify({ defaultModel: 'claude-sonnet-4-6', providers: {}, litellm: { enabled: true } }),
|
||||||
|
);
|
||||||
|
|
||||||
|
// No provider key: the runtime catalog is empty, so no Python is spawned.
|
||||||
|
clearProviderEnv();
|
||||||
|
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation(
|
||||||
|
async () => new Response('{}', { status: 503 }),
|
||||||
|
);
|
||||||
|
|
||||||
|
const { server, litellm } = await startService({ dataDir, port, litellmPort });
|
||||||
|
cleanups.push(async () => { await server.close(); });
|
||||||
|
|
||||||
|
expect(litellm).toEqual({ status: 'error', port: litellmPort, error: 'No provider models available' });
|
||||||
|
expect(server.localConfig.manageLiteLLM).toBe(true);
|
||||||
|
expect(server.localConfig.managedLiteLLMPort).toBe(litellmPort);
|
||||||
|
expect(server.localConfig.useBuiltInProxy).toBe(true);
|
||||||
|
expect(fetchSpy.mock.calls.some(([input]) => (
|
||||||
|
String(input) === `http://localhost:${litellmPort}/health/liveliness`
|
||||||
|
))).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
it('atomically falls back from an occupied desktop port and routes spawned agents to it', async () => {
|
it('atomically falls back from an occupied desktop port and routes spawned agents to it', async () => {
|
||||||
const base = makeTmpDir();
|
const base = makeTmpDir();
|
||||||
tmpDirs.push(base);
|
tmpDirs.push(base);
|
||||||
|
|||||||
Reference in New Issue
Block a user