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-launch.ts

104 lines · 2.9 KB · TypeScript
import {
	buildDevinSessionRequest,
	devinRunnerConfigured,
	launchDevinSession,
} from './devin.ts'
import { recordLaunch } from './storage-port.ts'
import type { SpawnOrPreviewInput, SpawnOrPreviewResult } from './run-core.ts'

/**
 * Devin launcher (general-audit and docs-freshness): same skip / dryRun / record
 * semantics as the Cursor path. Live runs request the session by event (the
 * runner package starts it); dryRun returns the exact request without sending it.
 */
export async function launchOrPreviewDevin(input: SpawnOrPreviewInput): Promise<SpawnOrPreviewResult> {
	const spawnedAt = new Date().toISOString()
	const launcher = 'devin' as const
	const request = buildDevinSessionRequest({
		automation: input.automation,
		repo: input.repo,
		prompt: input.prompt,
		devinMode: input.devinMode,
		maxAcuLimit: input.maxAcuLimit,
	})
	const record = (fields: { agent_id?: string | null; agent_url?: string | null; dry_run: number; skipped: string | null }) =>
		recordLaunch({
			automation: input.automation,
			repo: input.repo,
			branch: input.branch,
			head_sha: input.headSha,
			agent_id: fields.agent_id ?? null,
			agent_url: fields.agent_url ?? null,
			spawned_at: spawnedAt,
			dry_run: fields.dry_run,
			skipped: fields.skipped,
		})

	if (input.skipped) {
		if (!input.dryRun) await record({ dry_run: 0, skipped: input.skipped })
		return {
			ok: true as const,
			automation: input.automation,
			launcher,
			repo: input.repo,
			branch: input.branch,
			dryRun: input.dryRun,
			skipped: input.skipped,
			promptChars: input.prompt.length,
			devinMode: input.devinMode,
			wouldSpawn: false,
		}
	}

	if (input.dryRun) {
		await record({ dry_run: 1, skipped: null })
		return {
			ok: true as const,
			automation: input.automation,
			launcher,
			repo: input.repo,
			branch: input.branch,
			dryRun: true,
			skipped: null,
			prompt: input.prompt,
			promptChars: input.prompt.length,
			devinMode: input.devinMode,
			devinRequest: request,
			devinRunnerConfigured: await devinRunnerConfigured(),
			wouldSpawn: true,
		}
	}

	try {
		const session = await launchDevinSession(request)
		await record({ agent_id: session.sessionId, agent_url: session.url, dry_run: 0, skipped: null })
		return {
			ok: true as const,
			automation: input.automation,
			launcher,
			repo: input.repo,
			branch: input.branch,
			dryRun: false,
			skipped: null,
			spawned: true,
			agentId: session.sessionId,
			agentUrl: session.url,
			devinMode: session.devinMode ?? input.devinMode,
			promptChars: input.prompt.length,
		}
	} catch (error) {
		const message = error instanceof Error ? error.message : String(error)
		await record({ dry_run: 0, skipped: `spawn-failed:${message.slice(0, 180)}` })
		return {
			ok: false as const,
			automation: input.automation,
			launcher,
			repo: input.repo,
			branch: input.branch,
			dryRun: false,
			error: message,
			promptChars: input.prompt.length,
			devinMode: input.devinMode,
		}
	}
}