/** * 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/.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/.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 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).slice(0, 20)) { if (!ID.test(id) || !value || typeof value !== 'object') continue const v = value as Partial const base = out[id] ?? { label: id, mode: 'events' as const, package: id, exports: { start: '', status: '', message: '', terminate: '' }, sessionUrlTemplate: '' } const ex = (v.exports ?? {}) as Partial 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 }