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.

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

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

import { routeDoc } from '../common/openapi';
import { zValidator } from '../common/validator';
import { ImController } from '../controllers/im.controller';
import { requireAuth } from '../middleware/auth';
import { requireAnyPermission, requireApiKeyScope } from '../middleware/permission-check';
import {
  ImReadRequestSchema,
  ImReadResultSchema,
  ImSendRequestSchema,
  ImSendResultSchema,
  ImSyncQuerySchema,
  ImSyncResultSchema,
  ImTopicParamSchema,
} from '../types/im.type';

/**
 * IM channel routes β€” the asynchronous, whole-message conversation surface for
 * messaging-style clients. No token streaming: `send` acknowledges, the agent
 * works in the background, `sync` long-polls for whole replies, typing and read
 * receipts.
 */
const ImRoutes = new Hono();

const messageRead = requireAnyPermission(
  getAllScopePermissions('MESSAGE_READ'),
  'You do not have permission to read messages',
);
const messageWrite = requireAnyPermission(
  getAllScopePermissions('MESSAGE_CREATE'),
  'You do not have permission to send messages',
);

/**
 * POST /api/v1/im/messages β€” send a message; the agent replies asynchronously.
 * Starts an agent run, so restricted keys need the same trio as `/responses`.
 */
ImRoutes.post(
  '/messages',
  routeDoc({
    data: ImSendResultSchema,
    errors: [404, 409],
    operationId: 'im.send',
    summary: 'Send a message to an agent; the reply arrives asynchronously via sync',
    tags: ['im'],
  }),
  requireAuth,
  messageWrite,
  requireApiKeyScope('model:invoke'),
  requireApiKeyScope('chat:write'),
  requireApiKeyScope('agent:write'),
  zValidator('json', ImSendRequestSchema),
  async (c) => new ImController().send(c),
);

/** GET /api/v1/im/topics/:topicId/sync β€” whole new messages, typing and read state (long-poll). */
ImRoutes.get(
  '/topics/:topicId/sync',
  routeDoc({
    data: ImSyncResultSchema,
    errors: [404],
    operationId: 'im.sync',
    summary: 'Long-poll a conversation for whole new messages, typing and read receipts',
    tags: ['im'],
  }),
  requireAuth,
  messageRead,
  requireApiKeyScope('chat:read'),
  zValidator('param', ImTopicParamSchema),
  zValidator('query', ImSyncQuerySchema),
  async (c) => new ImController().sync(c),
);

/** POST /api/v1/im/topics/:topicId/read β€” move the user's read cursor forward. */
ImRoutes.post(
  '/topics/:topicId/read',
  routeDoc({
    data: ImReadResultSchema,
    errors: [404],
    operationId: 'im.markRead',
    summary: "Mark the agent's messages read up to a message",
    tags: ['im'],
  }),
  requireAuth,
  messageRead,
  requireApiKeyScope('chat:write'),
  zValidator('param', ImTopicParamSchema),
  zValidator('json', ImReadRequestSchema),
  async (c) => new ImController().markRead(c),
);

export default ImRoutes;