feat: add persisted manual pi backend mode controls

This commit is contained in:
William Valentin
2026-02-23 23:06:40 -08:00
parent 3e5e82c76e
commit 4188c68130
14 changed files with 360 additions and 10 deletions
+115 -6
View File
@@ -31,6 +31,8 @@ import { dirname, resolve } from 'path';
import { loadCouncilScaffoldSafe } from '../councils/scaffold.js';
import { buildCouncilPreflightReport, shouldRunCouncilPreflight } from '../councils/preflight.js';
export type BackendRuntimeMode = 'config_default' | 'force_native' | 'force_pi_embedded';
function buildProviderConfigMap(config: Config): Partial<Record<ModelProvider, ModelConfig>> {
const providerConfigs: Partial<Record<ModelProvider, ModelConfig>> = {};
const modelConfigs: ModelConfig[] = [
@@ -333,6 +335,8 @@ export function createMessageRouter(deps: {
skillInstaller?: SkillInstaller;
externalBackends?: Partial<Record<ExternalBackendName, ExternalBackend>>;
defaultName?: ExternalBackendName;
getBackendMode?: () => BackendRuntimeMode;
setBackendMode?: (mode: BackendRuntimeMode) => void;
}): {
handler: (msg: InboundMessage, reply: (response: OutboundMessage) => Promise<void>) => Promise<void>;
agents: Map<string, { orchestrator: AgentOrchestrator; collector: OutboundAttachmentCollector }>;
@@ -342,6 +346,59 @@ export function createMessageRouter(deps: {
const talkModeUntil = new Map<string, number>();
const activeRuns = new Map<string, AgentOrchestrator>();
function getBackendMode(): BackendRuntimeMode {
return deps.getBackendMode?.() ?? 'config_default';
}
function getConfiguredOrFallbackDefaultBackend(): ExternalBackendName | 'native' {
return deps.defaultName ?? 'native';
}
function getEffectiveDefaultBackend(): ExternalBackendName | 'native' {
const mode = getBackendMode();
if (mode === 'force_native') {
return 'native';
}
if (mode === 'force_pi_embedded') {
return 'pi_embedded';
}
return getConfiguredOrFallbackDefaultBackend();
}
function resolveRoutableBackend(
requestedBackend: ExternalBackendName | 'native' | undefined,
): ExternalBackendName | 'native' {
if (!requestedBackend || requestedBackend === 'native') {
return 'native';
}
return deps.externalBackends?.[requestedBackend] ? requestedBackend : 'native';
}
function applyBackendModeOverride(
requestedBackend: ExternalBackendName | 'native' | undefined,
): ExternalBackendName | 'native' | undefined {
if (requestedBackend !== 'pi_embedded') {
return requestedBackend;
}
if (getBackendMode() === 'force_native') {
return 'native';
}
return requestedBackend;
}
function formatBackendStatusLine(activeTier: string): string {
const mode = getBackendMode();
const configuredDefault = getConfiguredOrFallbackDefaultBackend();
const effectiveDefault = resolveRoutableBackend(getEffectiveDefaultBackend());
const availableExternal = Object.keys(deps.externalBackends ?? {}).sort().join(', ') || 'none';
return [
`Flynn is running. Active model tier: ${activeTier}. Backend: ${effectiveDefault}`,
`Backend mode: ${mode}`,
`Configured default: ${configuredDefault}`,
`Available external backends: ${availableExternal}`,
].join('\n');
}
async function maybeBuildTtsAttachment(responseText: string, channel: string) {
if (!isTtsEnabledForChannel(deps.config, channel)) {
return undefined;
@@ -759,11 +816,7 @@ export function createMessageRouter(deps: {
rawInput: commandInput,
services: {
getStatus: () => {
const requestedBackend = agentConfig?.backend ?? deps.defaultName;
const backend = requestedBackend && requestedBackend !== 'native' && deps.externalBackends?.[requestedBackend]
? requestedBackend
: 'native';
return `Flynn is running. Active model tier: ${agent.getModelTier()}. Backend: ${backend}`;
return formatBackendStatusLine(agent.getModelTier());
},
getTools: () => {
const names = new Set(deps.toolRegistry.list().map((tool: Tool) => tool.name));
@@ -1143,6 +1196,62 @@ export function createMessageRouter(deps: {
return `Session transferred to ${destinationLabel}`;
},
backendCommand: (inputRaw: string) => {
const normalized = inputRaw.trim().toLowerCase();
if (!normalized || normalized === 'status' || normalized === 'show') {
return formatBackendStatusLine(agent.getModelTier());
}
if (!deps.setBackendMode) {
return 'Backend mode control is not available in this runtime.';
}
if (
normalized === 'activate pi'
|| normalized === 'activate pi_embedded'
|| normalized === 'activate pi-embedded'
) {
deps.setBackendMode('force_pi_embedded');
return [
'Pi embedded backend activated globally.',
formatBackendStatusLine(agent.getModelTier()),
].join('\n\n');
}
if (
normalized === 'deactivate pi'
|| normalized === 'deactivate pi_embedded'
|| normalized === 'deactivate pi-embedded'
) {
deps.setBackendMode('force_native');
return [
'Pi embedded backend deactivated globally. Native is now forced for Pi-routed turns.',
formatBackendStatusLine(agent.getModelTier()),
].join('\n\n');
}
if (
normalized === 'use config'
|| normalized === 'reset'
|| normalized === 'auto'
|| normalized === 'config'
) {
deps.setBackendMode('config_default');
return [
'Backend mode reset to config default.',
formatBackendStatusLine(agent.getModelTier()),
].join('\n\n');
}
return [
'Usage:',
'/backend status',
'/backend activate pi',
'/backend deactivate pi',
'/backend use config',
].join('\n');
},
getApprovals: () => {
if (!deps.hookEngine) {
return 'Approval gates are not enabled in this runtime.';
@@ -1409,7 +1518,7 @@ export function createMessageRouter(deps: {
// If native audio IS supported, we pass attachments through unchanged —
// buildUserMessage() in the agent will create native audio content parts
const requestedBackend = agentConfig?.backend ?? deps.defaultName;
const requestedBackend = applyBackendModeOverride(agentConfig?.backend ?? getEffectiveDefaultBackend());
const forceNativeForCapabilityQuery = shouldForceNativeForCapabilityQuery(messageText);
const hasAttachmentsForExternalBackend = Boolean(attachments && attachments.length > 0);
const selectedBackend = requestedBackend && requestedBackend !== 'native'