Skip to content
← Public packages

@kody/kody-factory

Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.

FollowFork

Files

config.ts

75 lines · 2.0 KB · TypeScript
/**
 * 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<string, RoutingConfig>
}

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<RoutingStore> {
	return loadJson<RoutingStore>(ROUTING_KEY, emptyStore())
}

export async function getRoutingConfig(projectId: string): Promise<RoutingConfig> {
	const store = await loadRoutingStore()
	return store.configs[projectId] ?? defaultRoutingConfig(projectId)
}

export async function saveRoutingConfig(config: RoutingConfig): Promise<void> {
	const store = await loadRoutingStore()
	store.configs[config.projectId] = config
	await saveJson(ROUTING_KEY, store)
}

export async function listRoutingConfigs(): Promise<RoutingConfig[]> {
	const store = await loadRoutingStore()
	return Object.values(store.configs)
}

export async function deleteRoutingConfig(projectId: string): Promise<void> {
	const store = await loadRoutingStore()
	delete store.configs[projectId]
	await saveJson(ROUTING_KEY, store)
}