The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
import { SECRET_REF_RE } from '@boxlite-ai/cloud-app-shared'
import { Hono } from 'hono'
import { addNote, forgetNote, listNotes, MAX_NOTE_CHARS } from '../services/notes'
import { z } from 'zod'
import type { Vars } from '../auth'
import { now } from '../db'
import type { Ctx } from '../ctx'
import { fail } from '../errors'
import { listFeedback } from '../feedback'
import { appSecretNames, deleteAppSecret, orgSecretNames, setAppSecret } from '../orgs'
import { envSummary } from '../views'
import { body, type C } from './util'
import { appProviders } from '../enduser/config'
export function adminSecretRoutes(ctx: Ctx) {
const r = new Hono()
/** Secrets belong to the authenticated platform organization. */
const orgParam = async (c: { req: { query: (k: string) => string | undefined } }): Promise<string> => {
const org = ctx.store.organizationId
if (!org) throw fail('forbidden', 'organization scope required')
return org
}
// Secret refs look like `org/openai`, so the name may contain slashes.
r.put('/secrets/:name{.+}', async (c) => {
const name = c.req.param('name')
if (!SECRET_REF_RE.test(name))
throw fail('invalid_request', 'secret names look like <scope>/<name>, e.g. org/openai')
const b = await body(c as unknown as C, z.object({ value: z.string().min(1) }))
const org = await orgParam(c)
await ctx.store.run(
'INSERT INTO org_secrets (org_id, name, value, updated_at) VALUES (?, ?, ?, ?) ON CONFLICT(org_id, name) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at',
org,
name,
b.value,
now(),
)
return c.json({ org, name, updated_at: now() })
})
r.get('/feedback', async (c) => c.json(await listFeedback(ctx))) // agents' reports about the platform, newest first
r.get('/secrets', async (c) => c.json(await orgSecretNames(ctx, await orgParam(c))))
r.delete('/secrets/:name{.+}', async (c) => {
await ctx.store.run('DELETE FROM org_secrets WHERE org_id = ? AND name = ?', await orgParam(c), c.req.param('name'))
return c.body(null, 204)
})
return r
}
/** Routes for an App selected within the authenticated organization. */
export function appRoutes(ctx: Ctx) {
const r = new Hono<{ Variables: Vars }>()
r.get('/', async (c) => {
const app = c.get('app')
const envs = await ctx.store.envs(app.id)
return c.json({
name: app.name,
created_at: app.created_at,
url: envs.find((env) => env.name === 'main')?.public_url ?? null,
envs: await Promise.all(envs.map((env) => envSummary(ctx, app, env))),
// What [auth].providers may list and actually work for this App right now (BoxLite's own
// OAuth apps plus the App's own, set in the console); `init` writes these.
sign_in_providers: await appProviders(ctx, app),
})
})
// ---- the App's own secrets (orgs.ts): values go in, never come back out ----
/** Names only: the App's own (`app/…`) and its organization's, each usable as a [secrets] reference. */
r.get('/secrets', async (c) => {
const app = c.get('app')
return c.json({ app: await appSecretNames(ctx, app.id), org: await orgSecretNames(ctx, app.org_id!) })
})
r.put('/secrets/:name{.+}', async (c) => {
const b = await body(c as unknown as C, z.object({ value: z.string() }))
return c.json(await setAppSecret(ctx, c.get('app'), c.req.param('name'), b.value, c.get('actor')))
})
r.delete('/secrets/:name{.+}', async (c) => {
await deleteAppSecret(ctx, c.get('app'), c.req.param('name'), c.get('actor'))
return c.body(null, 204)
})
// ---- notes for the next session (services/notes.ts) ----
r.get('/notes', async (c) =>
c.json(
await listNotes(ctx, c.get('app'), {
query: c.req.query('q') || undefined,
limit: Number(c.req.query('limit')) || undefined,
}),
),
)
r.post('/notes', async (c) => {
const b = await body(c as unknown as C, z.object({ text: z.string().max(MAX_NOTE_CHARS * 2) }))
return c.json(await addNote(ctx, c.get('app'), b.text, c.get('actor')), 201)
})
r.delete('/notes/:id{[0-9]+}', async (c) =>
c.json(await forgetNote(ctx, c.get('app'), Number(c.req.param('id')), c.get('actor'))),
)
r.get('/activity', async (c) => {
const app = c.get('app')
const limit = Math.min(Math.max(Number(c.req.query('limit') ?? 50) || 50, 1), 500)
const rows = await ctx.store.all<{
id: number
env: string | null
actor: string
action: string
detail: string | null
ts: string
}>(
'SELECT id, env, actor, action, detail, ts FROM activity WHERE app_id = ? ORDER BY id DESC LIMIT ?',
app.id,
limit,
)
return c.json(rows.map((r) => ({ ...r, detail: r.detail ? JSON.parse(r.detail) : null })))
})
return r
}