Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

3.2 KB
Raw Blame
import { Hono } from 'hono';

import { getAllScopePermissions } from '@/utils/rbac';

import { routeDoc } from '../common/openapi';
import { zValidator } from '../common/validator';
import { AgentSignalController } from '../controllers/agent-signal.controller';
import { requireAuth } from '../middleware/auth';
import {
  requireAnyPermissionWithApiKeyScope,
  requireApiKeyScope,
} from '../middleware/permission-check';
import {
  AgentSignalEmissionSchema,
  AgentSignalReceiptListSchema,
  EmitSourceEventRequestSchema,
  ListReceiptsQuerySchema,
  TriggerSourceEventRequestSchema,
} from '../types/agent-signal.type';

/**
 * Agent Signal routes.
 *
 * Emitting is the "wake the agent" path, so it carries the RBAC gate the in-app
 * producer uses (`message:create`) *and* the delegated-key scopes the catalog
 * assigns to `agentSignal`: `agent:write` for the namespace, plus
 * `model:invoke` because both emissions enqueue workflows whose judges call a
 * model. Without them a `chat:write`-only key could spend model budget, which
 * the tRPC contract (`agentSignal.*` + `TRPC_PROCEDURE_EXTRA_SCOPES`) forbids.
 * Receipts are a read of the caller's own events, so they take `agent:read`.
 */
const AgentSignalRoutes = new Hono();

const signalWrite = requireAnyPermissionWithApiKeyScope(
  getAllScopePermissions('MESSAGE_CREATE'),
  'agent:write',
  'You do not have permission to emit agent signals',
);

/** Enqueueing a signal can start model-backed analysis, so it needs `model:invoke`. */
const signalRun = requireApiKeyScope('model:invoke');

/** Reading your own receipts is the namespace's read capability. */
const signalRead = requireApiKeyScope('agent:read');

/** POST /api/v1/signals/source-events β€” emit a client-side event. */
AgentSignalRoutes.post(
  '/source-events',
  routeDoc({
    data: AgentSignalEmissionSchema,
    // 404: the agent named in the payload is not usable by the caller.
    errors: [404],
    operationId: 'signals.emit',
    status: 202,
    summary: 'Emit an agent signal source event',
    tags: ['signals'],
  }),
  requireAuth,
  signalWrite,
  signalRun,
  zValidator('json', EmitSourceEventRequestSchema),
  async (c) => new AgentSignalController().emitSourceEvent(c),
);

/** POST /api/v1/signals/trigger β€” synthesise and enqueue a trigger event. */
AgentSignalRoutes.post(
  '/trigger',
  routeDoc({
    data: AgentSignalEmissionSchema,
    // 404: the target agent is not usable by the caller; 429: the hourly
    // manual-trigger budget is spent.
    errors: [404, 429],
    operationId: 'signals.trigger',
    status: 202,
    summary: 'Trigger a synthesized agent signal source event',
    tags: ['signals'],
  }),
  requireAuth,
  signalWrite,
  signalRun,
  zValidator('json', TriggerSourceEventRequestSchema),
  async (c) => new AgentSignalController().triggerSourceEvent(c),
);

/** GET /api/v1/signals/receipts β€” processed-event receipts for a topic. */
AgentSignalRoutes.get(
  '/receipts',
  routeDoc({
    data: AgentSignalReceiptListSchema,
    summary: 'List agent signal receipts for a topic',
    tags: ['signals'],
  }),
  requireAuth,
  signalRead,
  zValidator('query', ListReceiptsQuerySchema),
  async (c) => new AgentSignalController().listReceipts(c),
);

export default AgentSignalRoutes;