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.1 KB
Raw Blame
import type { BuiltinRestrictedManifestResolver, LobeChatPluginApi } from '@lobechat/types';

import { agentShareSystemPrompt } from './agentShareSystemRole';
import { AgentDocumentsManifest } from './manifest';
import { AGENT_SHARE_DOCUMENT_API_NAMES, AgentDocumentsApiName } from './types';

const matchesExactApiSet = (apiNames: readonly string[], expected: ReadonlySet<string>) => {
  const actual = new Set(apiNames);

  return actual.size === expected.size && [...actual].every((apiName) => expected.has(apiName));
};

const isSupportedAgentShareApiSet = (apiNames: readonly string[]) =>
  apiNames.length > 0 && apiNames.every((apiName) => AGENT_SHARE_DOCUMENT_API_NAMES.has(apiName));

const getApi = (name: string): LobeChatPluginApi => {
  const api = AgentDocumentsManifest.api.find((item) => item.name === name);

  if (!api) throw new Error(`Agent Documents manifest is missing ${name}`);

  return api;
};

const createDocumentApi: LobeChatPluginApi = {
  ...getApi(AgentDocumentsApiName.createDocument),
  description: 'Create a document in the current shared-agent topic.',
  parameters: {
    properties: {
      content: getApi(AgentDocumentsApiName.createDocument).parameters.properties.content,
      title: getApi(AgentDocumentsApiName.createDocument).parameters.properties.title,
    },
    required: ['title', 'content'],
    type: 'object',
  },
};

const listDocumentsApi: LobeChatPluginApi = {
  ...getApi(AgentDocumentsApiName.listDocuments),
  description: 'List documents created during the current shared-agent topic.',
  parameters: {
    properties: {},
    required: [],
    type: 'object',
  },
};

const agentShareApiOverrides = new Map<string, LobeChatPluginApi>([
  [AgentDocumentsApiName.createDocument, createDocumentApi],
  [AgentDocumentsApiName.listDocuments, listDocumentsApi],
]);

/**
 * Owns the temporary Agent Share projection of Documents capabilities.
 *
 * Keeping the semantic projection in this package prevents Share orchestration
 * from learning Documents-specific arguments and makes a future common tool
 * finalization pipeline a registry wiring change rather than another rewrite.
 */
export const resolveAgentDocumentsRestrictedManifest: BuiltinRestrictedManifestResolver = ({
  allowedApiNames,
  restriction,
}) => {
  if (restriction !== 'agentShare' || !isSupportedAgentShareApiSet(allowedApiNames)) {
    return undefined;
  }

  const allowedApiNameSet = new Set(allowedApiNames);
  const hasFullAgentShareApiSet = matchesExactApiSet(
    allowedApiNames,
    AGENT_SHARE_DOCUMENT_API_NAMES,
  );

  return {
    ...AgentDocumentsManifest,
    api: AgentDocumentsManifest.api
      .filter((api) => allowedApiNameSet.has(api.name))
      .map((api) => agentShareApiOverrides.get(api.name) ?? api),
    meta: {
      ...AgentDocumentsManifest.meta,
      description: hasFullAgentShareApiSet
        ? 'Create, list, read, edit, and rename documents isolated to the current shared-agent topic.'
        : 'Use documents isolated to the current shared-agent topic.',
    },
    systemRole: hasFullAgentShareApiSet ? agentShareSystemPrompt : undefined,
  };
};