Skip to content
← Public packages

@cameronpak/box

Full Box Public API v1 client: lifecycle, prompts, files, commands, snapshots, environments, webhooks, desktop, hosting, and account.

src/fork-box.ts

39 lines · 1.5 KB · TypeScript
import { assertBoxId, boxRequest, failure, type Result } from './client.ts'
import type { BoxSize } from './types.ts'
import type { BoxActionOutput } from './stop-box.ts'

export type ForkBoxInput = {
	/** The source box to copy. The source is never modified. */
	boxId: string
	/** Replaces the env the fork would inherit from the source box. */
	env?: Record<string, string>
	noEnv?: boolean
	/** Machine size for the fork. Omit to inherit the source size. */
	type?: BoxSize
	/** Optionally pin the fork to a different named environment. */
	environment?: string
	/** Auto-stop for the fork. Defaults to 3600. The fork does not inherit the source TTL. */
	ttlSeconds?: number | null
}

/**
 * Fork a box into a new box. The returned id is the new fork, not the source.
 * Use this to build a template box once and branch it per task.
 */
export default async function forkBox(input: ForkBoxInput): Promise<Result<BoxActionOutput>> {
	const invalid = assertBoxId(input?.boxId)
	if (invalid) return failure<BoxActionOutput>(invalid)

	const body: Record<string, unknown> = {}
	if (input.env !== undefined) body.env = input.env
	if (input.noEnv !== undefined) body.noEnv = input.noEnv
	if (input.type !== undefined) body.type = input.type
	if (input.environment !== undefined) body.environment = input.environment
	if ('ttlSeconds' in input) body.ttlSeconds = input.ttlSeconds

	return boxRequest<BoxActionOutput>({
		path: '/boxes/' + input.boxId + '/fork',
		method: 'POST',
		body,
	})
}