Something went wrong. Try again.
remote mailbox into muse
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243#!/usr/bin/env nodeimport { randomUUID } from 'node:crypto';import { loadConfig, readSecret } from './config.ts';import { signToken } from './auth.ts';import type { ControllerConfig } from './types.ts';
async function main(): Promise<void> { const [configPath, action, target] = process.argv.slice(2); if (!configPath || !['publish', 'read', 'ack', 'cursor', 'get'].includes(action)) throw new Error('usage: orpheus-control CONFIG.json publish [UUID] < body.txt | read [LIMIT] | ack SEQUENCE | cursor | get UUID'); const config = loadConfig<ControllerConfig>(configPath); const url = new URL(config.server_url); if (url.protocol !== 'https:' && !(url.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname) && config.allow_insecure_http === true)) throw new Error('server_url must use HTTPS'); const token = signToken({ principal_id: config.principal_id, kid: config.kid, private_key: readSecret(config.private_key) }); let method: 'GET' | 'POST' = 'GET'; let route: string; let body: unknown; if (action === 'publish') { const id = target ?? randomUUID(); console.error(`message ID: ${id}`); let content = ''; for await (const chunk of process.stdin) content += chunk; method = 'POST'; route = '/v1/messages'; body = { id, body: content }; } else if (action === 'read') { route = `/v1/messages${target ? `?limit=${encodeURIComponent(target)}` : ''}`; } else if (action === 'ack') { if (!target || !/^(0|[1-9][0-9]*)$/.test(target) || !Number.isSafeInteger(Number(target))) throw new Error('ack needs a nonnegative sequence'); method = 'POST'; route = '/v1/cursor'; body = { sequence: Number(target) }; } else if (action === 'cursor') { route = '/v1/cursor'; } else { if (!target) throw new Error('get needs a UUID'); route = `/v1/messages/${encodeURIComponent(target)}`; } const response = await fetch(new URL(route, url), { method, headers: { Authorization: `Bearer ${token}`, ...(body !== undefined ? { 'Content-Type': 'application/json' } : {}) }, body: body === undefined ? undefined : JSON.stringify(body) }); console.log(JSON.stringify(await response.json(), null, 2)); if (!response.ok) process.exitCode = 1;}
main().catch(error => { console.error(error.message); process.exitCode = 1; });