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

grok-wake.ts

37 lines · 1.6 KB · TypeScript
/**
 * Grok Bot wake destination. The factory holds no wake credentials: it asks
 * `@kody/wake-relay` to wake the bot through `@kody/factory.wake.requested`.
 * The relay knows each bot's URL and key, and answers delivered or failed.
 */
import type { DispatchResult, PromptContext } from './types.ts'
import { getTemplate, renderTemplate } from './templates.ts'
import { routeWake } from '../../events/wake-transport.ts'

export async function dispatchGrokWake(
	ctx: PromptContext,
	opts: { templateId?: string | null; bot?: string; dryRun?: boolean },
): Promise<DispatchResult> {
	const botName = (opts.bot ?? 'patch').trim().toLowerCase()
	if (!/^[a-z][a-z0-9_-]{0,31}$/.test(botName)) {
		return { destination: 'grok-wake', ok: false, detail: `Invalid bot name "${opts.bot ?? ''}"` }
	}

	const templateId = opts.templateId ?? 'default-grok-wake'
	const template = await getTemplate(templateId)
	const prompt = template ? renderTemplate(template.template, ctx) : `Signal: ${ctx.title}\n\n${ctx.body}`

	if (opts.dryRun) {
		return { destination: 'grok-wake', ok: true, detail: 'dry run: would ask @kody/wake-relay to wake ' + botName }
	}

	const r = await routeWake({
		source: 'signal',
		bot: botName,
		prompt,
		reason: `signal ${ctx.source} routed to grok-wake: ${ctx.title}`,
		ref: ctx.signalId || ctx.sourceId,
	})
	return r.ok
		? { destination: 'grok-wake', ok: true, detail: `Wake for ${botName} handed to @kody/wake-relay (${r.requestId}); a wake alert is raised if it is not delivered` }
		: { destination: 'grok-wake', ok: false, detail: `Could not request the wake: ${r.detail ?? 'unknown error'}` }
}