Skip to content
← Public packages

@cameronpak/raycast-kodys-pouch

Your Kody Skills and Tools, one command away.

src/pouch.ts

66 lines · 2.0 KB · TypeScript
import getPackage from './get-package'
import getSkill from './get-skill'
import listCapabilities from './list-capabilities'
import listPackages from './list-packages'
import listSkills from './list-skills'

export type PouchInput =
	| { operation: 'list-packages' }
	| { operation: 'get-package'; packageId?: string; kodyId?: string }
	| { operation: 'list-capabilities' }
	| { operation: 'list-skills' }
	| { operation: 'get-skill'; id: string; path?: string }

function stringField(
	input: Record<string, unknown>,
	key: string,
): string | undefined {
	const value = input[key]
	if (value === undefined) return undefined
	if (typeof value !== 'string') throw new Error(`${key} must be a string.`)
	return value
}

/**
 * Serve the read-only operations used by Kody's Pouch through one webhook.
 * Use this from the Raycast extension so one installer-owned URL can load the
 * inventory and fetch Skill Contents on demand.
 *
 * @param input - One supported Pouch operation and its operation-specific fields.
 * @returns The selected operation's existing response payload.
 *
 * @example
 * import pouch from 'kody:@cameronpak/raycast-kodys-pouch/pouch'
 *
 * const skills = await pouch({ operation: 'list-skills' })
 */
export default async function pouch(input: PouchInput) {
	if (input === null || typeof input !== 'object' || Array.isArray(input)) {
		throw new Error('Pass a Pouch operation object.')
	}

	const request = input as unknown as Record<string, unknown>
	switch (request.operation) {
		case 'list-packages':
			return await listPackages()
		case 'get-package':
			return await getPackage({
				packageId: stringField(request, 'packageId'),
				kodyId: stringField(request, 'kodyId'),
			})
		case 'list-capabilities':
			return await listCapabilities()
		case 'list-skills':
			return await listSkills()
		case 'get-skill': {
			const id = stringField(request, 'id')
			if (!id) throw new Error('Pass id.')
			return await getSkill({
				id,
				path: stringField(request, 'path'),
			})
		}
		default:
			throw new Error('Unknown Pouch operation.')
	}
}