/** * Per-project routing configuration storage. Routing questions and destination * mappings are data, not code, so they can be edited in the UI. */ import { loadJson, saveJson } from '../../lib/storage.ts' import { DEFAULT_CONFIDENCE_THRESHOLD, type RoutingConfig } from './types.ts' const ROUTING_KEY = 'factory:signals:routing:v1' type RoutingStore = { version: 1 configs: Record } function emptyStore(): RoutingStore { return { version: 1, configs: {} } } export function defaultRoutingConfig(projectId: string): RoutingConfig { return { projectId, questions: [ { key: 'actionability', label: 'Actionable', description: 'Is this signal something the factory should act on?', }, ], criteria: { create_work_item: { label: 'Create a work item', destination: 'board-card', promptTemplateId: null, }, wake_grok: { label: 'Wake Grok Bot', destination: 'grok-wake', promptTemplateId: null, }, ignore: { label: 'No action needed', destination: 'none', promptTemplateId: null, }, }, confidenceThreshold: DEFAULT_CONFIDENCE_THRESHOLD, enabled: true, } } export async function loadRoutingStore(): Promise { return loadJson(ROUTING_KEY, emptyStore()) } export async function getRoutingConfig(projectId: string): Promise { const store = await loadRoutingStore() return store.configs[projectId] ?? defaultRoutingConfig(projectId) } export async function saveRoutingConfig(config: RoutingConfig): Promise { const store = await loadRoutingStore() store.configs[config.projectId] = config await saveJson(ROUTING_KEY, store) } export async function listRoutingConfigs(): Promise { const store = await loadRoutingStore() return Object.values(store.configs) } export async function deleteRoutingConfig(projectId: string): Promise { const store = await loadRoutingStore() delete store.configs[projectId] await saveJson(ROUTING_KEY, store) }