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.

4.1 KB
Raw Blame
import {
  type ContextEngineeringResult,
  type ContextSnapshot,
  runContextEngineering,
} from '@lobechat/mecha';

import { type ServerMessagesEngineParams } from './types';

/**
 * Shape the server's per-step gathering into the host-agnostic context
 * snapshot. The assembly itself (engine parameters, placeholder generators,
 * page-agent gating) lives in `@lobechat/mecha` and is shared with the client.
 */
export const toContextSnapshot = ({
  additionalContexts,
  additionalVariables,
  agentAccountContext,
  agentBuilderContext,
  agentDocumentChanges,
  agentDocuments,
  agentGroup,
  agentIdentity,
  agentManagementContext,
  attachmentPreviewFileIds,
  botPlatformContext,
  capabilities,
  connectorOwnershipNote,
  discordContext,
  documentReferences,
  enableAgentMode,
  enableExpertise,
  enableMcpAppContext,
  enableHistoryCount,
  enableStaleToolResultTrim,
  evalContext,
  expertise,
  forceFinish,
  formatHistorySummary,
  groupAgentBuilderContext,
  historyCount,
  historySummary,
  initialContext,
  inputTemplate,
  knowledge,
  messages = [],
  model,
  modelDisplayName,
  modelKnowledgeCutoff,
  onboardingContext,
  planTodo,
  projectInstructions,
  provider,
  revokedDocumentReads,
  skillsConfig,
  systemRole,
  toolDiscoveryConfig,
  toolsConfig,
  topicReferences,
  userMemory,
  userTimezone,
  workspaceContext,
}: ServerMessagesEngineParams): ContextSnapshot => ({
  agent: {
    documentChanges: agentDocumentChanges,
    documents: agentDocuments,
    enableHistoryCount,
    enableStaleToolResultTrim,
    historyCount,
    identity: agentIdentity,
    inputTemplate,
    knowledge: { fileContents: knowledge?.fileContents, knowledgeBases: knowledge?.knowledgeBases },
    revokedDocumentReads,
    systemRole,
  },
  // Server-side file access URLs resolve to stable file-proxy URLs in production.
  fileContext: { enabled: true, includeFileUrl: true, previewFileIds: attachmentPreviewFileIds },
  model: {
    capabilities: {
      isCanUseAudio: capabilities?.isCanUseAudio,
      isCanUseFC: capabilities?.isCanUseFC,
      isCanUseVideo: capabilities?.isCanUseVideo,
      isCanUseVision: capabilities?.isCanUseVision,
    },
    displayName: modelDisplayName,
    knowledgeCutoff: modelKnowledgeCutoff,
    model,
    provider,
  },
  run: {
    additionalContexts,
    enableAgentMode,
    enableExpertise,
    enableMcpAppContext,
    expertise,
    forceFinish,
    formatHistorySummary,
    historySummary,
    initialContext,
    messages,
  },
  step: {
    agentAccountContext,
    agentBuilderContext,
    agentManagementContext,
    documentReferences,
    groupAgentBuilderContext,
    onboardingContext,
    planTodo,
    topicReferences,
    workspaceContext,
  },
  tools: {
    disabledToolIdentifiers: toolsConfig?.disabledToolIdentifiers,
    enabledSkills: skillsConfig?.enabledSkills,
    enabledToolIds: toolsConfig?.tools,
    manifests: toolsConfig?.manifests,
    toolDiscoveryConfig,
  },
  variables: additionalVariables,
  world: {
    botPlatformContext,
    connectorOwnershipNote,
    discordContext,
    evalContext,
    group: agentGroup,
    projectInstructions,
    userMemory: userMemory?.memories
      ? { enabled: true, fetchedAt: userMemory.fetchedAt, memories: userMemory.memories }
      : undefined,
    userTimezone,
  },
});

/**
 * Server-side messages engine function.
 *
 * Unlike the frontend version, it receives all data as parameters instead of
 * fetching from stores, shapes them into a {@link ContextSnapshot} and runs the
 * shared context engineering core.
 *
 * Returns the pipeline metadata alongside the messages: processors record
 * per-request decisions there (trim stats, cache-warmth gates, truncation
 * counts), and the agent-runtime trace recorder persists them per step.
 */
export const serverMessagesEngine = async (
  params: ServerMessagesEngineParams,
): Promise<ContextEngineeringResult> => runContextEngineering(toContextSnapshot(params));

// Re-export types
export type {
  BotPlatformContext,
  EvalContext,
  ServerKnowledgeConfig,
  ServerMessagesEngineParams,
  ServerModelCapabilities,
  ServerToolsConfig,
  ServerUserMemoryConfig,
} from './types';