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

import {
  redactCreatorPrivateBlob,
  sanitizeVisitorError,
  toVisitorMessage,
} from '@/database/models/message';

import { stripFinalStateInEventData } from './StreamEventManager';

/**
 * Whether an operation's Gateway pushes go to a shared-agent visitor. A visitor
 * run is always fully redacted: creator model/provider/usage are stripped and
 * errors are projected to a classified type, with no per-share opt-out.
 *
 * `false` is the explicit "not a share run β€” push verbatim" marker, so a
 * missing/undefined value can stay reserved for "not resolved yet".
 */
export type GatewayVisitorRedaction = boolean;

/**
 * Whether `publishAgentRuntimeInit` is initializing a shared-agent visitor run.
 * The op EXECUTES as the creator, but `streamOwnerUserId` (set only for
 * share-visitor runs β€” see `AgentRuntimeService.createOperation`) registers
 * the Gateway WS channel under the *visitor's* id, so the visitor is the one
 * receiving this push over the wire.
 */
export const isShareVisitorInit = (initialState: any): boolean =>
  typeof initialState?.streamOwnerUserId === 'string' && initialState.streamOwnerUserId.length > 0;

/**
 * Read whether a run is a shared-agent visitor run off whatever the notifier
 * was handed.
 *
 * Two shapes reach here, deliberately handled by one function so the fast path
 * never disagrees with itself:
 *  - a runtime `AgentState`, whose `principal.actor.shareVisitor` is stamped once
 *    at operation creation (`AgentRuntimeService.createOperation`) and rides the
 *    state through to the terminal event (`publishAgentRuntimeEnd`);
 *  - an `AgentOperationMetadata` record, which is what
 *    `AgentRuntimeCoordinator.createAgentOperation` passes to
 *    `publishAgentRuntimeInit` β€” it carries the flattened `streamOwnerUserId`
 *    instead.
 */
export const resolveRedactionFromState = (state: any): GatewayVisitorRedaction => {
  const share = state?.principal?.actor?.shareVisitor as { visitorUserId?: string } | undefined;
  return !!share?.visitorUserId || isShareVisitorInit(state);
};

/**
 * The `agent_runtime_init` DTO pushed to the Gateway.
 *
 * Applied to EVERY run, not just shared-agent visitors. No consumer reads this
 * event's data: the web handler logs it and breaks, the CLI prints a fixed
 * line, and the gateway transport does not handle the type at all. What the raw
 * `initialState` would otherwise carry is the entire `AgentState` β€” the LLM
 * context in `messages` plus the tool-set maps that `stripStateForStream`
 * already treats as the size problem on `finalState`, and for a visitor the
 * creator's `agentConfig` / system prompt on top.
 *
 * `status` is kept because it is the one field the shape has ever needed.
 */
export const buildPublicInitEventData = (initialState: any): { status?: unknown } => ({
  status: initialState?.status,
});

/**
 * Public `agent_runtime_end` DTO pushed to the Gateway for shared-agent visitor
 * runs. `finalState` is the creator's full `AgentState`, none of which the
 * client reads off this event (`gatewayEventHandler.ts` only consumes `reason`
 * and `uiMessages`, both already-sanitized UI-facing values), so drop
 * `finalState` wholesale instead of trying to allowlist inside it.
 */
export const buildPublicEndEventData = <T extends { finalState?: unknown }>(
  data: T,
): Omit<T, 'finalState'> => {
  const { finalState: _finalState, ...rest } = data;
  return rest;
};

/**
 * Sanitize a `uiMessages` snapshot for a shared-agent visitor's WS channel.
 * `uiMessages` is the canonical `UIChatMessage[]` built from the creator's DB
 * rows (a creator-scoped query β€” unlike `shareChat.getMessages`, which already
 * goes through `MessageModel.queryForVisitor`). Every `step_start` and the
 * terminal `agent_runtime_end` push carry this snapshot, so without this step
 * the creator's joined `sender` identity would ride down the visitor's Gateway
 * WS channel even though `finalState` is already scrubbed. Reuses
 * `toVisitorMessage` β€” the same projection `shareChat.getMessages` applies β€” so
 * the two paths cannot drift apart.
 */
const sanitizeUiMessagesForVisitor = (uiMessages: unknown): unknown => {
  if (!Array.isArray(uiMessages)) return uiMessages;
  return (uiMessages as UIChatMessage[]).map((message) => toVisitorMessage(message));
};

/**
 * Sanitize a live `type: 'error'` Gateway stream event for a shared-agent
 * visitor. `ServerStreamSink.publishError` builds this event's `data` from
 * `formatErrorEventData`, which β€” like `formatErrorForState` β€” copies the raw
 * upstream `body` (provider, budget, upstream diagnostic) straight onto the
 * payload. The client (`gatewayEventHandler.ts`'s `case 'error'`) immediately
 * overlays it onto the visible message, so it reaches the visitor's screen
 * live, during the run, before any DB row (and therefore `toVisitorMessage`) is
 * ever involved. Reuses {@link sanitizeVisitorError}'s classification so the
 * live and reloaded-history projections cannot drift apart, reshaped into
 * `formatErrorEventData`'s flatter `{ error, errorType, phase }` wire shape
 * instead of `ChatMessageError`'s `{ body, message, type }`.
 */
const sanitizeErrorEventDataForVisitor = (data: unknown): unknown => {
  if (!data || typeof data !== 'object') return data;
  const record = data as { error?: unknown; errorType?: unknown; phase?: unknown };

  const safe = sanitizeVisitorError(
    typeof record.errorType === 'string' || typeof record.errorType === 'number'
      ? ({
          message: typeof record.error === 'string' ? record.error : undefined,
          type: record.errorType,
        } as ChatMessageError)
      : undefined,
  );

  return {
    ...(safe?.message === undefined ? {} : { error: safe.message }),
    ...(safe?.type === undefined ? {} : { errorType: safe.type }),
    ...(record.phase === undefined ? {} : { phase: record.phase }),
  };
};

/**
 * Chokepoint applied to every event `GatewayStreamNotifier` pushes to the
 * Gateway WS channel β€” not just `agent_runtime_init` / `agent_runtime_end`.
 *
 * For a share-visitor run:
 * 1. `finalState` is dropped wholesale (same DTO shape as
 *    {@link buildPublicEndEventData}) β€” `step_complete`, published via the
 *    generic `publishStreamEvent`, has no per-event DTO builder, so this
 *    chokepoint is its only sanitization point.
 * 2. A live `type: 'error'` event is routed through
 *    {@link sanitizeErrorEventDataForVisitor} β€” a SEPARATE leak from
 *    `uiMessages`/`finalState`, since it carries `formatErrorEventData`'s raw
 *    `{ body, error, errorType }` shape with no `finalState`/`uiMessages` key
 *    for the other checks to even look at.
 * 3. {@link redactCreatorPrivateBlob} runs over the WHOLE remaining payload.
 *    Every other event type for a share run β€” `stream_start` (`{ model,
 *    provider }`), `tool_end` (a tool's own free-form `result.state` blob, e.g.
 *    `{ model, provider, usage }`), and `step_complete`'s `subagent_progress`
 *    phase (`{ model, totalCost, totalInputTokens, … }` as SIBLINGS of `phase`)
 *    β€” carries creator model/provider/usage/cost shapes. Rather than enumerate
 *    and denylist each event type (which silently stops covering the next
 *    producer that lands a new field), the recursive key-set strip is applied
 *    at any nesting depth, so a future event type or tool cannot reintroduce
 *    this leak by construction. `error` is excluded from this generic pass
 *    because it needs PROJECTION rather than key-name redaction.
 * 4. `uiMessages` additionally goes through {@link toVisitorMessage}'s full
 *    field allowlist, which the private-blob key strip alone does not cover.
 *
 * Normal step_complete events omit finalState unless the run opts in. Clients reconcile
 * messages through message_patch/uiMessages and do not consume runtime state.
 * Other events in a normal run fall back to the generic
 * {@link stripFinalStateInEventData} (messages / tool-set fields only),
 * matching the Redis xadd chokepoint.
 */
export const sanitizeGatewayEventData = (
  data: unknown,
  redaction: GatewayVisitorRedaction,
  eventType?: unknown,
): unknown => {
  if (!data || typeof data !== 'object') return data;
  const record = data as Record<string, unknown>;

  if (!redaction) return stripFinalStateInEventData(data, eventType);

  const withoutFinalState: Record<string, unknown> =
    'finalState' in record
      ? (() => {
          const { finalState: _finalState, ...rest } = record;
          return rest;
        })()
      : record;

  if (eventType === 'error') return sanitizeErrorEventDataForVisitor(withoutFinalState);

  const redacted = redactCreatorPrivateBlob(withoutFinalState);

  if (!('uiMessages' in withoutFinalState)) return redacted;

  return {
    ...redacted,
    uiMessages: sanitizeUiMessagesForVisitor(withoutFinalState.uiMessages),
  };
};