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.

8.9 KB
Raw Blame
import { AgentAccountManifest } from '@lobechat/builtin-tool-agent-account';
import { AttachmentsManifest } from '@lobechat/builtin-tool-attachments';
import { AuvManifest } from '@lobechat/builtin-tool-auv';
import { BrowserManifest } from '@lobechat/builtin-tool-browser';
import { CloudSandboxManifest } from '@lobechat/builtin-tool-cloud-sandbox';
import { ImageGenerationManifest } from '@lobechat/builtin-tool-image-generation';
import { KnowledgeBaseManifest } from '@lobechat/builtin-tool-knowledge-base';
import { LocalSystemManifest } from '@lobechat/builtin-tool-local-system';
import { McpEventsManifest } from '@lobechat/builtin-tool-mcp-events';
import { MemoryManifest } from '@lobechat/builtin-tool-memory';
import { MessageManifest } from '@lobechat/builtin-tool-message';
import { RemoteDeviceManifest } from '@lobechat/builtin-tool-remote-device';
import { VideoGenerationManifest } from '@lobechat/builtin-tool-video-generation';
import { WebBrowsingManifest } from '@lobechat/builtin-tool-web-browsing';
import {
  alwaysOnToolIds,
  chatModeAllowedToolIds,
  defaultToolIds,
  groupSupervisorToolIds,
  runtimeManagedToolIds,
} from '@lobechat/builtin-tools';
import { executionTargetToRuntimeMode, resolveToolMode } from '@lobechat/types';

import {
  DEVICE_TOOL_IDENTIFIERS,
  type DeviceToolWallParams,
  REMOTE_DEVICE_TOOL_IDENTIFIERS,
} from './deviceTools';
import type { ResolvedToolRules, ToolRuleRequest } from './types';

const enableAll = (ids: readonly string[]) => Object.fromEntries(ids.map((id) => [id, true]));

/** The wall parameters for a run's device facts; no device means no walls. */
export const deviceToolWallParams = (request: ToolRuleRequest): DeviceToolWallParams | undefined =>
  request.deviceAccess
    ? {
        canUseDevice: request.deviceAccess.canUseDevice,
        deviceLocked: request.deviceAccess.deviceLocked,
        disableLocalSystem: request.disableLocalSystem,
        // Only a gateway device can report what it supports.
        supportedDeviceTools: request.device ? (request.device.supportedTools ?? []) : undefined,
      }
    : undefined;

/**
 * Decide which tools a run may enable, which it gets by default, and which
 * must not exist in its manifest pool. The mode (chat / custom / agent) and
 * every per-tool rule live here for both hosts; hosts only assemble the facts
 * and feed the result to the tools engine's enable checker.
 */
export const resolveToolRules = (request: ToolRuleRequest): ResolvedToolRules => {
  const { agent, device, deviceAccess, executionTarget, model } = request;
  const pinnedPluginIds = agent.plugins ?? [];
  const toolMode = resolveToolMode(agent.chatConfig);
  const runtimeMode = executionTargetToRuntimeMode(executionTarget);
  // Device tools only exist for device-capable targets: `none` means no
  // device, and `sandbox` and devices are mutually exclusive.
  const deviceCapable =
    executionTarget === 'local' ||
    executionTarget === 'device' ||
    (executionTarget === 'auto' && !!device);
  const deviceLocked = deviceAccess?.deviceLocked ?? false;

  const searchMode = agent.chatConfig?.searchMode ?? 'auto';
  const isSearchEnabled = request.useApplicationBuiltinSearchTool ?? searchMode !== 'off';
  const kbEnabled = request.hasEnabledKnowledgeBases ?? false;
  // Oversized attachments are sent as previews that can be paged with `readAttachment`; only that
  // read-only tool is enabled, not the knowledge-base tool with its search and write APIs. The
  // preview names it only when it survives into the final tool set (see `MessagesEngine`).
  const attachmentsEnabled = request.hasOversizedFiles ?? false;
  const memoryEnabled = request.memoryEnabled ?? false;
  // Image generation is never auto-injected: the user opts in by pinning the
  // tool, and a model with native image output never gets the fallback.
  const imageGenerationEnabled =
    model.canUseFC &&
    !model.hasImageOutput &&
    pinnedPluginIds.includes(ImageGenerationManifest.identifier);
  // Video generation follows the same opt-in contract as image generation.
  const videoGenerationEnabled =
    model.canUseFC && pinnedPluginIds.includes(VideoGenerationManifest.identifier);
  // Local tools need a `local` target that can actually reach a machine.
  const localToolsEnabled =
    !request.disableLocalSystem && runtimeMode === 'local' && request.localExecutionReady;

  // Chat mode: a strict outer whitelist. No always-on tools, no runtime
  // injection, no activator — each entry still passes its own gate.
  const chatModeRules = {
    [AttachmentsManifest.identifier]: attachmentsEnabled,
    [ImageGenerationManifest.identifier]: imageGenerationEnabled,
    [VideoGenerationManifest.identifier]: videoGenerationEnabled,
    [KnowledgeBaseManifest.identifier]: kbEnabled,
    [MemoryManifest.identifier]: memoryEnabled,
    [WebBrowsingManifest.identifier]: isSearchEnabled,
  };
  // Custom mode: the tool set is EXACTLY the agent's declared plugins, for
  // focused builtin sub-agents that configure themselves precisely.
  const customModeRules = enableAll(pinnedPluginIds);
  const agentModeRules: Record<string, boolean> = {
    ...enableAll(request.runtimePluginIds ?? []),
    ...enableAll(pinnedPluginIds),
    ...enableAll(alwaysOnToolIds),
    // System rules may override the user's selection for specific tools.
    // Auto mode lets the model pick the sandbox or the routed device per
    // call, so the dedicated sandbox tool is offered there too.
    [AttachmentsManifest.identifier]: attachmentsEnabled,
    [CloudSandboxManifest.identifier]:
      runtimeMode === 'cloud' || executionTarget === 'auto' || !!request.pageScope,
    [KnowledgeBaseManifest.identifier]: kbEnabled,
    [LocalSystemManifest.identifier]: localToolsEnabled,
    // The in-app browser drives the same machine as local-system.
    [BrowserManifest.identifier]: runtimeMode === 'local' && request.localExecutionReady,
    [MemoryManifest.identifier]: memoryEnabled,
    ...(request.isBotConversation && { [MessageManifest.identifier]: true }),
    // The identity/inbox state is context, not a tool; this tool is the
    // on-demand action half and only costs a schema when the agent owns an
    // account. Replaces the always-on mailbox slot the channel design paid.
    [AgentAccountManifest.identifier]: request.hasIdentityAccount ?? false,
    [McpEventsManifest.identifier]: request.hasEventConnector ?? false,
    ...(request.isGroupSupervisor && enableAll(groupSupervisorToolIds)),
    // The device picker: only for device-capable targets behind a gateway,
    // and only while the run still has a device decision to make. The wall
    // below is the authoritative gate; this rule is defense in depth.
    [RemoteDeviceManifest.identifier]: deviceCapable && !!device && !deviceLocked,
    [WebBrowsingManifest.identifier]: isSearchEnabled,
  };

  // Walls follow the policy and the plan, with or without a gateway; the
  // Computer Use opt-in is a gateway device's own report.
  const excludedIdentifiers = new Set(request.disabledPluginIds ?? []);
  // The flag and connector capability are authoritative, including explicit
  // activation and custom-mode pinning. Browser runtimes omit this server fact.
  if (!request.hasEventConnector) excludedIdentifiers.add(McpEventsManifest.identifier);
  if (device && !device.supportedTools?.includes(AuvManifest.identifier))
    excludedIdentifiers.add(AuvManifest.identifier);
  if (toolMode === 'agent') {
    // A runtime capability grants discovery. Its functions are loaded only after activation.
    for (const id of runtimeManagedToolIds) {
      // A device can be selected later in this operation. Keep its local executor
      // available for the step that binds it.
      if (
        id === LocalSystemManifest.identifier &&
        deviceCapable &&
        device &&
        !request.disableLocalSystem
      )
        continue;
      if (!agentModeRules[id]) excludedIdentifiers.add(id);
    }
  }
  if (deviceAccess) {
    if (!deviceAccess.canUseDevice) {
      for (const identifier of DEVICE_TOOL_IDENTIFIERS) excludedIdentifiers.add(identifier);
    } else if (deviceLocked) {
      for (const identifier of REMOTE_DEVICE_TOOL_IDENTIFIERS) excludedIdentifiers.add(identifier);
    }
  }

  return {
    allowExplicitActivation: toolMode === 'agent',
    defaultToolIds:
      toolMode === 'custom'
        ? [...pinnedPluginIds]
        : toolMode === 'chat'
          ? [...chatModeAllowedToolIds]
          : [
              ...defaultToolIds,
              ...(request.hasIdentityAccount ? [AgentAccountManifest.identifier] : []),
              ...(request.hasEventConnector ? [McpEventsManifest.identifier] : []),
              ...(request.isGroupSupervisor ? groupSupervisorToolIds : []),
            ],
    deviceCapable,
    deviceLocked,
    excludedIdentifiers,
    rules:
      toolMode === 'custom'
        ? customModeRules
        : toolMode === 'chat'
          ? chatModeRules
          : agentModeRules,
    runtimeMode,
    toolMode,
  };
};