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

runners.ts

71 lines · 3.1 KB · TypeScript
/**
 * Runner settings: which package runs each agent kind and how to link to its
 * sessions. The factory knows runner ids and events, not any one vendor's API:
 * it dispatches by `@kody/factory.agent.*.requested` events to the runner
 * package (`package`), which answers with `@kody/<package>.session.*` events.
 * The factory never holds a runner key.
 */
export type RunnerMode = 'events'

export type RunnerSetting = {
	label: string
	mode: RunnerMode
	/** Kody id of the runner package that emits `@kody/<package>.session.*` (e.g. `devin`). */
	package: string
	/** The runner package's exports, for people and agents wiring a runner by hand. */
	exports: { start: string; status: string; message: string; terminate: string }
	/** Session link when the runner gave none. `{id}` is the run id. Empty means no link. */
	sessionUrlTemplate: string
}

export type RunnerSettings = Record<string, RunnerSetting>

export function defaultRunnerSettings(): RunnerSettings {
	return {
		devin: {
			label: 'Devin',
			mode: 'events',
			package: 'devin',
			exports: {
				start: 'kody:@kody/devin/start',
				status: 'kody:@kody/devin/status',
				message: 'kody:@kody/devin/message',
				terminate: 'kody:@kody/devin/terminate',
			},
			sessionUrlTemplate: 'https://app.devin.ai/sessions/{id}',
		},
	}
}

const ID = /^[a-z][a-z0-9_-]{0,63}$/
const SPEC = /^kody:@[a-z0-9_-]+\/[a-z0-9_-]+(\/[A-Za-z0-9_./-]+)?$/

export function normalizeRunnerSettings(raw: unknown): RunnerSettings {
	const out = defaultRunnerSettings()
	if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return out
	for (const [id, value] of Object.entries(raw as Record<string, unknown>).slice(0, 20)) {
		if (!ID.test(id) || !value || typeof value !== 'object') continue
		const v = value as Partial<RunnerSetting>
		const base = out[id] ?? { label: id, mode: 'events' as const, package: id, exports: { start: '', status: '', message: '', terminate: '' }, sessionUrlTemplate: '' }
		const ex = (v.exports ?? {}) as Partial<RunnerSetting['exports']>
		const spec = (s: unknown, fb: string) => (typeof s === 'string' && (s === '' || SPEC.test(s)) ? s : fb)
		out[id] = {
			label: typeof v.label === 'string' && v.label.trim() ? v.label.trim().slice(0, 60) : base.label,
			// Events is the only dispatch mode; an older stored `builtin` reads as events.
			mode: 'events',
			package: typeof v.package === 'string' && ID.test(v.package) ? v.package : base.package,
			exports: { start: spec(ex.start, base.exports.start), status: spec(ex.status, base.exports.status), message: spec(ex.message, base.exports.message), terminate: spec(ex.terminate, base.exports.terminate) },
			sessionUrlTemplate:
				typeof v.sessionUrlTemplate === 'string' && (v.sessionUrlTemplate === '' || (/^https:\/\/[^\s]{3,200}$/.test(v.sessionUrlTemplate) && v.sessionUrlTemplate.includes('{id}')))
					? v.sessionUrlTemplate
					: base.sessionUrlTemplate,
		}
	}
	return out
}

/** Runner ids whose package may emit session events (by kody id). */
export function runnerForPackage(runners: RunnerSettings, kodyId: string): string | null {
	for (const [id, r] of Object.entries(runners)) if (r.package === kodyId) return id
	return null
}