Skip to content
← Public packages

@kody/mailroom

Offline-first PWA for your Kody mailbox: sent and received threads, copy-prompt for agents, no send.

src/prompt.ts

37 lines · 1.2 KB · TypeScript
import { getMailMessage } from './mail.ts'
import { buildMessagePrompt, buildThreadPrompt } from './prompt-text.ts'
import { getThread } from './thread.ts'

export { buildMessagePrompt, buildThreadPrompt, textFromMessage } from './prompt-text.ts'

/**
 * Build a copy-paste prompt for a mailbox thread or a single stored message.
 *
 * @example
 * import buildPrompt from 'kody:@kody/mailroom/prompt'
 *
 * const { prompt } = await buildPrompt({ threadId: '…' })
 */
export default async function buildPrompt(input: {
	threadId?: string
	messageId?: string
}): Promise<{ prompt: string; kind: 'thread' | 'message' }> {
	const messageId = String(input.messageId || '').trim()
	if (messageId) {
		const message = await getMailMessage(messageId)
		if (!message) throw new Error('Message not found.')
		return { prompt: buildMessagePrompt(message), kind: 'message' }
	}
	const threadId = String(input.threadId || '').trim()
	if (!threadId) throw new Error('threadId or messageId is required.')
	const thread = await getThread({ threadId })
	if (!thread) throw new Error('Thread not found.')
	return {
		prompt: buildThreadPrompt({
			id: thread.id,
			subject: thread.subject,
			messages: thread.messages,
		}),
		kind: 'thread',
	}
}