Skip to content
← Public packages

@kody/agent-mail

AgentMail inboxes for agents: create/list inboxes, messages, threads, and Svix webhook ingress.

FollowFork

Files

list-rules.ts

49 lines · 1.9 KB · TypeScript
import { agentMailRequest, clean } from './core.ts'
import { listPath, slimEntry, type ListSelectorInput } from './lists.ts'

export type ListRulesInput = ListSelectorInput & {
	limit?: number
	pageToken?: string
}

/**
 * List AgentMail allow/block list entries (native AgentMail Lists).
 * Read-only; uses GET /v0/lists/{direction}/{type}, or the pod / inbox scoped
 * variant when podId / inboxId is passed. AgentMail enforces these lists at
 * receive/send time, so this package does not store or re-check rules.
 *
 * @param input.type - `allow` or `block` (required)
 * @param input.direction - `receive` (default), `reply`, or `send`
 * @param input.inboxId - Optional inbox scope
 * @param input.podId - Optional pod scope
 * @param input.limit - Max entries to return
 * @param input.pageToken - Pagination cursor from a prior response
 * @returns `{ scope, scopeId, direction, type, count, entries, nextPageToken }`
 *
 * @example
 * import listRules from 'kody:@kody/agent-mail/list-rules'
 * const { entries } = await listRules({ type: 'allow', inboxId: 'prime@agentmail.to' })
 */
export default async function listRules(input: ListRulesInput) {
	const target = listPath(input)
	const query: Record<string, unknown> = {}
	if (input.limit !== undefined && input.limit !== null) query.limit = Number(input.limit)
	if (clean(input.pageToken)) query.page_token = clean(input.pageToken)

	const result = (await agentMailRequest({
		path: target.path,
		query,
		label: 'AgentMail list ' + target.direction + ' ' + target.type + ' entries',
	})) as { entries?: unknown[]; count?: number; next_page_token?: string }

	const entries = Array.isArray(result?.entries) ? result.entries.map(slimEntry) : []
	return {
		scope: target.scope,
		scopeId: target.scopeId,
		direction: target.direction,
		type: target.type,
		count: result?.count ?? entries.length,
		entries,
		nextPageToken: result?.next_page_token ?? null,
	}
}