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.8 KB
Raw Blame
import { isGoalPrompt } from '@lobechat/builtin-tool-goal';

import type { HeteroOperationCapability } from '@/libs/trpc/utils/internalJwt';

/**
 * What a heterogeneous run's operation token may do.
 *
 * `goal:manage` lets the CLI create a goal the agent supervises from this
 * conversation (`lh goal create --conversation`), or bind an existing one to it
 * (`lh goal bind-topic`, which the `/goal` instructions route a named goal to). The token is exposed to the
 * always-installed CLI, so it follows who started the run, not what it says:
 * a turn the user sent (a chat message, or one they scheduled) gets it however
 * they word the ask, since users say "run a goal" as often as they type `/goal`.
 * Runs the server starts on its own — Task runs, goal planning and supervision
 * turns, background-result deliveries, cron, inbox and SCM wakeups — and
 * Channel or bot messages, whose sender may not be the owner, get it only from
 * an explicit `/goal`. That keeps a goal from spawning goals of its own. A goal
 * a user-sent run creates binds to that user's topic, where its tray shows it.
 *
 * `channel:read` and `channel:message` let a Channel dispatch read its own conversation
 * (`lh channel history|message`), message Agents in the requester's other Channels
 * (`lh channel list|send`) and people the requester can talk to one-on-one
 * (`lh channel people|dm`), as the requester, never as the device owner. A bot run whose
 * sender is the bot's owner (`botContext.isOwner`, e.g. the owner messaging their Agent on
 * Telegram) gets `channel:message` alone: it may list the owner's Channels and post a request
 * there as the owner. A turn the user sent in an ordinary chat gets the same, so asking an Agent
 * to "send tj a message" works there too. Neither has a Channel conversation to read; a bot
 * message from anyone else, a Task run and a server-started run get neither.
 *
 * `acceptance:evidence` lets a Task run file evidence into its own verification
 * round (`lh acceptance run result submit|ingest --operation`). That round is
 * owned by the operation's creator, so on a device logged in as another member
 * the token is the only identity that can reach it. Only a Task run has a
 * round to file into.
 *
 * `work:write` lets every run register the files and links it produced as Work
 * cards under its reply (`lh work create|update`). The Work lands in the run's
 * own topic; a share visitor's run included, whose Works stay fenced to that
 * visitor topic exactly like the ones its completion scan registers.
 *
 * Renewal copies the minted claims, so a run never gains either later.
 */
export const heteroOperationCapabilities = (
  prompt: unknown,
  options: {
    channel?: boolean;
    /** The run answers a bot message its owner sent (`botContext.isOwner`). */
    ownerBotTurn?: boolean;
    /**
     * A share visitor drives the run. It gets only the run's own callbacks:
     * a goal or acceptance round it created would belong to the creator.
     */
    shareVisitor?: boolean;
    taskId?: string;
    /** The run answers a message the user sent: a chat turn or one they scheduled. */
    userTurn?: boolean;
  } = {},
): HeteroOperationCapability[] => {
  if (options.shareVisitor)
    return ['hetero:ingest', 'hetero:finish', 'hetero:intervention:read', 'work:write'];

  const directUserTurn = !!options.userTurn && !options.channel && !options.taskId;
  return [
    'hetero:ingest',
    ...(options.channel
      ? (['channel:read', 'channel:message'] as const)
      : options.ownerBotTurn || directUserTurn
        ? (['channel:message'] as const)
        : []),
    'hetero:finish',
    'hetero:intervention:read',
    ...(isGoalPrompt(prompt) || directUserTurn ? (['goal:manage'] as const) : []),
    ...(options.taskId ? (['acceptance:evidence'] as const) : []),
    'work:write',
  ];
};