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.

6.5 KB
Raw Blame
import type {
  CreateAssistantMessageOptions,
  MessageTransport,
  QueryMessagesInput,
  QueryMessagesOptions,
  RuntimeMessageRef,
  RuntimeToolMessage,
  UpdateToolMessageInput,
} from '@lobechat/agent-runtime';
import { omitToolWorkRegistration } from '@lobechat/agent-runtime';
import { parse } from '@lobechat/conversation-flow';
import type { CreateMessageParams, UIChatMessage, UpdateMessageParams } from '@lobechat/types';

import { type MessageModel } from '@/database/models/message';

import {
  createConversationParentMissingError,
  isMidOperationReferenceMissingError,
} from '../messagePersistErrors';
import { unwrapPgError } from '../pgError';

/**
 * Server {@link MessageTransport} adapter — delegates to `MessageModel` (DB).
 */
export class ServerMessageTransport implements MessageTransport {
  constructor(
    private readonly messageModel: MessageModel,
    private readonly options: {
      postProcessUrl?: (
        path: string | null,
        file: { fileType: string; id?: string | null },
      ) => Promise<string>;
    } = {},
  ) {}

  /**
   * Reuses the first persisted placeholder when an at-least-once step delivery
   * retries after message creation but before the runtime state is committed.
   */
  async createAssistantMessage(
    params: CreateMessageParams,
    options?: CreateAssistantMessageOptions,
  ): Promise<RuntimeMessageRef> {
    const idempotencyKey = options?.idempotencyKey;
    const createParams = idempotencyKey ? { ...params, clientId: idempotencyKey } : params;

    try {
      return await this.messageModel.create(createParams);
    } catch (error) {
      const pgError = unwrapPgError(error);
      const isIdempotencyConflict =
        idempotencyKey &&
        pgError?.code === '23505' &&
        pgError.constraint === 'message_client_id_user_unique';

      if (!isIdempotencyConflict) throw error;

      const existingMessage = await this.messageModel.findByClientId(idempotencyKey);
      if (!existingMessage) throw error;

      return {
        agentId: existingMessage.agentId,
        createdAt: existingMessage.createdAt,
        groupId: existingMessage.groupId,
        id: existingMessage.id,
        model: existingMessage.model,
        parentId: existingMessage.parentId,
        provider: existingMessage.provider,
        role: existingMessage.role,
        threadId: existingMessage.threadId,
        topicId: existingMessage.topicId,
      };
    }
  }

  async createToolMessage(params: CreateMessageParams): Promise<RuntimeMessageRef> {
    try {
      return await this.messageModel.create({
        ...params,
        ...(params.pluginIntervention && {
          pluginIntervention: { ...params.pluginIntervention, runtimeType: 'server' },
        }),
      });
    } catch (error) {
      if (typeof params.parentId === 'string' && isMidOperationReferenceMissingError(error)) {
        throw createConversationParentMissingError(params.parentId, error);
      }
      throw error;
    }
  }

  async deleteMessage(id: string): Promise<void> {
    // Runtime cleanup path (orphaned placeholders / approval teardown). Agent-share
    // visitor turns run under the creator's identity, so the runtime must be able to
    // delete rows the creator-facing delete guard fails closed on.
    await this.messageModel.deleteMessage(id, { includeShareVisitor: true });
  }

  async findToolMessageIdByToolCallId(
    toolCallId: string,
    parentMessageId: string,
  ): Promise<string | undefined> {
    // Indexed on `message_plugins_tool_call_id_idx`; the model already uses this
    // lookup for card updates, so the abort path adds no new access pattern.
    const id = await this.messageModel.findToolMessageIdByToolCallId(toolCallId, parentMessageId);
    return id ?? undefined;
  }

  async findById(id: string): Promise<RuntimeMessageRef | undefined> {
    const message = await this.messageModel.findById(id);
    return message
      ? {
          agentId: message.agentId,
          content: message.content,
          createdAt: message.createdAt,
          groupId: message.groupId,
          id: message.id,
          metadata: message.metadata,
          model: message.model,
          parentId: message.parentId,
          provider: message.provider,
          role: message.role,
          threadId: message.threadId,
          topicId: message.topicId,
        }
      : undefined;
  }

  async getToolMessage(id: string): Promise<RuntimeToolMessage | undefined> {
    const [message, plugin] = await Promise.all([
      this.messageModel.findById(id),
      this.messageModel.findMessagePlugin(id),
    ]);
    if (!message || message.role !== 'tool' || !plugin) return undefined;
    return {
      content: message.content ?? '',
      id: message.id,
      metadata: message.metadata ?? undefined,
      parentId: message.parentId,
      pluginError: plugin.error,
      pluginIntervention: plugin.intervention,
      pluginState: plugin.state,
      role: 'tool',
      tool_call_id: plugin.toolCallId,
    };
  }

  async query(
    params?: QueryMessagesInput,
    options?: QueryMessagesOptions,
  ): Promise<UIChatMessage[]> {
    const messages = (
      await this.messageModel.query(params, {
        // Executors only ever read the operation's own, already-authorized topic.
        // An agent-share visitor run executes under the CREATOR's identity, so
        // without this opt-in `query()`'s creator-facing agent-share exclusion
        // would blank the visitor's conversation mid-run (see `tool.ts`, which
        // re-reads `state.messages` from the DB after every tool call).
        allowShareVisitor: true,
        postProcessUrl: options?.resolveAssetUrls ? this.options.postProcessUrl : undefined,
      })
    ).map(omitToolWorkRegistration);

    if (!options?.flatten) return messages;

    const { flatList } = parse(messages, undefined, { threadId: params?.threadId });
    return flatList;
  }

  async update(id: string, params: Partial<UpdateMessageParams>): Promise<void> {
    await this.messageModel.update(id, params);
  }

  async updatePluginState(id: string, state: Record<string, any>): Promise<void> {
    await this.messageModel.updatePluginState(id, state);
  }

  async updateToolIntervention(id: string, intervention: Record<string, any>): Promise<void> {
    await this.messageModel.updateMessagePlugin(id, { intervention } as any);
  }

  async updateToolMessage(id: string, params: UpdateToolMessageInput): Promise<void> {
    const result = await this.messageModel.updateToolMessage(id, params);
    if (!result.success) {
      throw new Error('Failed to persist tool result');
    }
  }
}