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

devin.ts

66 lines · 2.2 KB · TypeScript
/**
 * Cleanup automations that run on Devin (general-audit, docs-freshness) start
 * their sessions by event: `@kody/factory.agent.start.requested` with runner
 * `devin`, answered by the runner package (`@kody/devin`). The factory holds no
 * Devin key; the session id arrives later on `@kody/devin.session.started`.
 */
import { requestAgentStart } from '../agent-events/events-runner.ts'
import { loadSettings } from '../lib/settings.ts'
import { SELF_KODY_ID, type AutomationId, type DevinMode } from './shared.ts'

export const CLEANUP_RUNNER = 'devin'

export type DevinSessionRequest = {
	topic: '@kody/factory.agent.start.requested'
	runner: typeof CLEANUP_RUNNER
	title: string
	tags: string[]
	repos: string[]
	prompt: string
	/** Sent as `mode`; the Devin runner passes it on as `devin_mode`. */
	mode: DevinMode
	maxAcuLimit?: number
}

export function buildDevinSessionRequest(input: {
	automation: AutomationId
	repo: string
	prompt: string
	devinMode: DevinMode
	maxAcuLimit: number | null
}): DevinSessionRequest {
	return {
		topic: '@kody/factory.agent.start.requested',
		runner: CLEANUP_RUNNER,
		title: `${input.automation} ${input.repo}`.slice(0, 80),
		// kody-factory keeps the session on the runner's watch list, so its updates come back as events.
		tags: ['kody-factory', SELF_KODY_ID, input.automation],
		repos: [input.repo],
		prompt: input.prompt,
		mode: input.devinMode,
		...(typeof input.maxAcuLimit === 'number' && input.maxAcuLimit > 0 ? { maxAcuLimit: input.maxAcuLimit } : {}),
	}
}

/** The Devin runner is listed in factory settings (it is by default). */
export async function devinRunnerConfigured() {
	try {
		return Boolean((await loadSettings()).runners[CLEANUP_RUNNER])
	} catch {
		return false
	}
}

/** Request the session. `sessionId` is the pending run id until the runner reports the real one. */
export async function launchDevinSession(request: DevinSessionRequest) {
	const { runId, requestId } = await requestAgentStart({
		runner: request.runner,
		prompt: request.prompt,
		title: request.title,
		tags: request.tags,
		repos: request.repos,
		mode: request.mode,
		maxAcuLimit: request.maxAcuLimit,
	})
	return { sessionId: runId as string | null, requestId, url: null as string | null, devinMode: request.mode as string | null }
}