Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kody/rooms

Shared rooms where spawned agents and their spawner exchange messages, with push wakes and a blocking wait fallback.

FollowForkOpen app

Files

create.ts

43 lines · 2.0 KB · TypeScript
import { RoomError } from '../core/errors.ts'
import { buildPromptSection } from '../core/prompts.ts'
import { createRoom, parseName } from '../core/rooms.ts'
import { currentUserId, handle, kodyCtx, optionalString, params } from '../runtime.ts'

/**
 * Create a room. You become its creator member. Requires use access on
 * `@kody/rooms` (any Kody caller or package running with that access).
 *
 * @param input.name - Your name in the room (default "spawner").
 * @param input.purpose - Conversation topic shown to joiners (max 500 chars).
 * @param input.invites - One-time join codes to mint now (default 1; each expires in 24 h).
 * @param input.caps - Per-room cap overrides, e.g. `{ membersPerRoom: 5 }`.
 * @param input.wake - Optional push wake for you (same options as `watch`). Webhook `key`s are sealed and never returned.
 * @returns `{ status: 'ok', room, member: { agent_id, name, token }, invites: [{ code, expiresAt }], watch, promptSection }`.
 *   `member.token` and invite codes are shown once; store them as secrets.
 * @example
 * import create from 'kody:@kody/rooms/create'
 * const { room, invites } = await create({ purpose: 'Fix flaky auth test', invites: 2 })
 */
export default async function create(input: unknown) {
	return handle(async () => {
		const p = params(input, ['name', 'purpose', 'invites', 'caps', 'wake'])
		const creatorUserId = await currentUserId()
		if (!creatorUserId) throw new RoomError('unauthorized', 'Creating a room needs a signed-in Kody caller with use access on @kody/rooms.')
		const ctx = kodyCtx()
		const created = await createRoom(ctx, {
			creatorUserId,
			name: parseName(p.name ?? 'spawner'),
			purpose: optionalString(p.purpose, 'purpose'),
			caps: p.caps,
			invites: p.invites as number | undefined,
			via: 'cli',
			wake: p.wake,
			ownerTargets: true,
		})
		return {
			status: 'ok' as const,
			...created,
			promptSection: buildPromptSection({ roomId: created.room.id, appUrl: created.room.appUrl, purpose: created.room.purpose, viewUrl: created.room.viewUrl }),
		}
	})
}