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:
@@ -1,13 +1,9 @@
|
||||
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 path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { spawnSidecarOwnedProcess } from '@waggle/agent';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
export interface LiteLLMStatus {
|
||||
status: 'running' | 'started' | 'timeout' | 'error';
|
||||
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(
|
||||
candidates: readonly string[],
|
||||
supportsLiteLLM: (pythonBin: string) => boolean,
|
||||
@@ -169,13 +139,9 @@ function canImportLiteLLM(pythonBin: string): boolean {
|
||||
return result.status === 0 && !result.error;
|
||||
}
|
||||
|
||||
/** The user's own Python with litellm installed; Waggle does not ship one. */
|
||||
export function resolveLiteLLMPython(): string | null {
|
||||
const bundledPython = getBundledPythonPath();
|
||||
const candidates = [
|
||||
...(bundledPython ? [bundledPython] : []),
|
||||
...discoverSystemPythonPaths(),
|
||||
];
|
||||
return selectLiteLLMPython(candidates, canImportLiteLLM);
|
||||
return selectLiteLLMPython(discoverSystemPythonPaths(), canImportLiteLLM);
|
||||
}
|
||||
|
||||
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.
|
||||
* 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> {
|
||||
const p = port ?? DEFAULT_PORT;
|
||||
|
||||
@@ -4,7 +4,7 @@ import net from 'node:net';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
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 type { LlmHealthStatus } from './index.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)
|
||||
* 2. Runs migration if needed (default.mind -> personal.mind)
|
||||
* 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
|
||||
* 6. Registers graceful shutdown handlers
|
||||
*
|
||||
@@ -256,12 +256,20 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
||||
mind.close();
|
||||
}
|
||||
|
||||
// 4. Hydrate provider credentials before the LiteLLM child snapshots env,
|
||||
// then generate its concrete model catalog from provider APIs.
|
||||
emit({ phase: 'litellm', message: skipLiteLLM ? 'Skipping LiteLLM proxy...' : 'Starting LiteLLM proxy...', progress: 0.5 });
|
||||
// 4. LiteLLM is optional and runs on the user's own Python, so it starts
|
||||
// only when config.json enables it ("litellm": { "enabled": true }). The
|
||||
// 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;
|
||||
if (skipLiteLLM) {
|
||||
litellm = { status: 'error', port: litellmPort, error: 'Skipped' };
|
||||
if (!useLiteLLM) {
|
||||
litellm = {
|
||||
status: 'error',
|
||||
port: litellmPort,
|
||||
error: skipLiteLLM ? 'Skipped' : 'Not enabled in config.json',
|
||||
};
|
||||
} else {
|
||||
const startupVault = new VaultStore(dataDir);
|
||||
migrateLegacyProviderKeysToVault(dataDir, startupVault);
|
||||
@@ -275,7 +283,7 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
||||
const managedLiteLLMUrl = `http://localhost:${litellmPort}`;
|
||||
let selfProxyUrl = `http://127.0.0.1:${port}/v1`;
|
||||
let litellmReachable = false;
|
||||
if (!skipLiteLLM) {
|
||||
if (useLiteLLM) {
|
||||
try {
|
||||
const healthRes = await fetch(`${managedLiteLLMUrl}/health/liveliness`, {
|
||||
signal: AbortSignal.timeout(2000),
|
||||
@@ -294,7 +302,7 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
||||
port,
|
||||
instanceId: allowDesktopPortFallback ? desktopInstanceId : undefined,
|
||||
litellmUrl: litellmReachable ? managedLiteLLMUrl : selfProxyUrl,
|
||||
manageLiteLLM: !skipLiteLLM,
|
||||
manageLiteLLM: useLiteLLM,
|
||||
managedLiteLLMPort: litellmPort,
|
||||
useBuiltInProxy: !litellmReachable,
|
||||
startOfflineManagerOnListen: false,
|
||||
@@ -305,7 +313,7 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
||||
process.off('SIGTERM', shutdown);
|
||||
process.off('SIGINT', shutdown);
|
||||
await server.close();
|
||||
if (!skipLiteLLM) {
|
||||
if (useLiteLLM) {
|
||||
await stopLiteLLM();
|
||||
}
|
||||
};
|
||||
@@ -325,7 +333,7 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
||||
const bindHost = allowDesktopPortFallback ? '127.0.0.1' : resolveBindHost();
|
||||
const cleanupListenFailure = async (): Promise<void> => {
|
||||
try { await server.close(); } catch { /* preserve the listen error */ }
|
||||
if (!skipLiteLLM) await stopLiteLLM().catch(() => undefined);
|
||||
if (useLiteLLM) await stopLiteLLM().catch(() => undefined);
|
||||
};
|
||||
try {
|
||||
await server.listen({ port, host: bindHost });
|
||||
@@ -351,7 +359,7 @@ export async function startService(options?: ServiceOptions): Promise<ServiceRes
|
||||
const listeningAddress = server.server.address();
|
||||
if (!listeningAddress || typeof listeningAddress === 'string') {
|
||||
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');
|
||||
}
|
||||
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`);
|
||||
} else {
|
||||
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 () => {
|
||||
const base = makeTmpDir();
|
||||
tmpDirs.push(base);
|
||||
|
||||
Reference in New Issue
Block a user