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.

7.6 KB
Raw Blame
import type { LayersEnum, TopicCommentItem } from '@lobechat/types';

import { type PortalArtifact } from '@/types/artifact';

export enum ArtifactDisplayMode {
  Code = 'code',
  Preview = 'preview',
}

// ============== Portal View Stack Types ==============

export enum PortalViewType {
  Acceptance = 'acceptance',
  AcceptanceCheck = 'acceptanceCheck',
  AgentDetail = 'agentDetail',
  Artifact = 'artifact',
  Browser = 'browser',
  DashboardWidget = 'dashboardWidget',
  Document = 'document',
  FilePreview = 'filePreview',
  Goal = 'goal',
  GoalMetric = 'goalMetric',
  GoalNode = 'goalNode',
  GoalNorthStar = 'goalNorthStar',
  GoalReport = 'goalReport',
  GoalReportChapter = 'goalReportChapter',
  GroupThread = 'groupThread',
  Home = 'home',
  LocalFile = 'localFile',
  MemoryDetail = 'memoryDetail',
  MessageDetail = 'messageDetail',
  Notebook = 'notebook',
  TaskDetail = 'taskDetail',
  TaskResult = 'taskResult',
  Thread = 'thread',
  ToolUI = 'toolUI',
  Topic = 'topic',
  TopicComments = 'topicComments',
  TopicCommentThread = 'topicCommentThread',
  VerifyReport = 'verifyReport',
  VerifyResult = 'verifyResult',
}

/**
 * A file preview the opener has already resolved, for surfaces that cannot read
 * the file through the owner-scoped file store.
 */
export interface PortalFileSource {
  /** Display name; its extension also selects the renderer. */
  name: string;
  /** Publicly readable file URL. */
  url: string;
}

export interface PortalFile {
  chunkId?: string;
  chunkText?: string;
  fileId: string;
  /**
   * Render from this source instead of fetching the file item. An agent-share
   * visitor previews files the creator's run produced, which the visitor's
   * own file store cannot read.
   */
  source?: PortalFileSource;
}

export interface OpenLocalFileParams {
  allowExternalFilePreview?: boolean;
  deviceId?: string;
  filePath: string;
  /**
   * Present when the file lives in the topic's cloud sandbox instead of a local
   * or device filesystem: content is fetched live via the sandbox
   * `readLocalFile` tool scoped to this topic, read-only, and unavailable once
   * the sandbox is recycled.
   */
  sandboxTopicId?: string;
  workingDirectory: string;
}

export interface OpenLocalFileEntry extends OpenLocalFileParams {
  id: string;
  /** The topic the device file was opened from. */
  topicId?: string;
}

/**
 * Which header metric of the goal detail page a drill-down inspects. Every
 * value is derivable from the `goal.graph` snapshot the page already holds —
 * none of these views require server work.
 */
export type GoalMetricKind =
  'budget' | 'duration' | 'findings' | 'lifecycle' | 'liveness' | 'tasks';

export interface PortalTaskWorkspace {
  id: string;
  slug: string;
}

export type PortalViewData =
  | { type: PortalViewType.Home }
  | { goalId: string; type: PortalViewType.Goal }
  | { goalId: string; metric: GoalMetricKind; type: PortalViewType.GoalMetric }
  | { goalId: string; nodeId: string; type: PortalViewType.GoalNode }
  /** One declared north-star clause of a goal, keyed by its metric series key. */
  | { goalId: string; metricKey: string; type: PortalViewType.GoalNorthStar }
  | { goalId: string; type: PortalViewType.GoalReport }
  | { chapterIndex: number; goalId: string; type: PortalViewType.GoalReportChapter }
  | { acceptanceId: string; type: PortalViewType.Acceptance }
  | { acceptanceId: string; checkId: string; type: PortalViewType.AcceptanceCheck }
  | { agentId: string; type: PortalViewType.AgentDetail }
  | { artifact: PortalArtifact; type: PortalViewType.Artifact }
  /**
   * The desktop side browser, for hosts without the working sidebar. `sessionId`
   * names the retained guest page; `request` navigates it once and is then dropped,
   * so returning to the view keeps wherever the user browsed to.
   */
  | {
      request?: { nonce: number; url: string };
      sessionId: string;
      type: PortalViewType.Browser;
    }
  | { runId?: string; type: PortalViewType.DashboardWidget; widgetId: string }
  | { agentDocumentId?: string; documentId: string; type: PortalViewType.Document }
  | { type: PortalViewType.Notebook }
  | {
      file: PortalFile;
      /**
       * The isolated conversation Topic whose message opened this preview. A
       * floating host (the Task run drawer) shows its own previews in place
       * instead of in a page portal it would cover.
       */
      sourceTopicId?: string;
      type: PortalViewType.FilePreview;
    }
  | { type: PortalViewType.LocalFile }
  /** `id` is the layer-specific row id (e.g. a preference id), not the base memory id. */
  | { id: string; layer: LayersEnum; type: PortalViewType.MemoryDetail }
  | { messageId: string; type: PortalViewType.MessageDetail }
  | {
      identifier: string;
      messageId: string;
      params?: Record<string, any>;
      type: PortalViewType.ToolUI;
    }
  | { startMessageId?: string; threadId?: string; type: PortalViewType.Thread }
  | { topicId: string; type: PortalViewType.Topic }
  | { agentId: string; type: PortalViewType.GroupThread }
  | {
      taskId: string;
      type: PortalViewType.TaskDetail;
      /**
       * Set when the task lives in a workspace other than the active one. The
       * panel then reads it through that workspace's scope, read-only, instead
       * of the active-scope task store.
       */
      workspace?: PortalTaskWorkspace;
    }
  | { taskId: string; type: PortalViewType.TaskResult }
  | {
      focusCommentId?: string;
      initialReplyCount?: number;
      initialRoot?: TopicCommentItem;
      rootCommentId: string;
      topicId: string;
      type: PortalViewType.TopicCommentThread;
    }
  | { messageId?: string; topicId: string; type: PortalViewType.TopicComments }
  | { runId: string; type: PortalViewType.VerifyReport }
  | { checkItemId: string; operationId: string; type: PortalViewType.VerifyResult };

// ============== Portal State ==============

export interface ChatPortalState {
  /** Composite id of the currently active local-file tab; undefined when no tabs open. */
  activeLocalFileId?: string;

  /** Active local-file tab id keyed by project/root working directory. */
  activeLocalFileIdsByScope: Record<string, string>;

  /** Path of the currently active tab; kept for legacy consumers that only need display/open path. */
  activeLocalFilePath?: string;

  /** Version a canvas tab is held at; a canvas absent here follows its latest version. */
  canvasTabVersions: Record<string, number>;

  /** Unsaved edit buffers keyed by file path. Presence implies the file is dirty. */
  dirtyLocalFileContents: Record<string, string>;

  // Legacy fields (kept for backward compatibility during migration)
  // TODO: Remove after Phase 3 migration complete
  /** Open file tabs in the LocalFile portal. */
  openLocalFiles: OpenLocalFileEntry[];
  /** @deprecated Use portalStack instead */
  portalArtifact?: PortalArtifact;
  portalArtifactDisplayMode: ArtifactDisplayMode;
  /** @deprecated Use portalStack instead */
  portalDocumentId?: string;

  /** @deprecated Use portalStack instead */
  portalFile?: PortalFile;
  /** @deprecated Use portalStack instead */
  portalMessageDetail?: string;
  portalStack: PortalViewData[];
  /** @deprecated Use portalStack instead */
  portalThreadId?: string;
  /** @deprecated Use portalStack instead */
  portalToolMessage?: { id: string; identifier: string };
  /** @deprecated Use portalStack instead */
  showNotebook?: boolean;
  showPortal: boolean;
}

export const initialChatPortalState: ChatPortalState = {
  activeLocalFileIdsByScope: {},
  canvasTabVersions: {},
  dirtyLocalFileContents: {},
  openLocalFiles: [],
  portalArtifactDisplayMode: ArtifactDisplayMode.Preview,
  portalStack: [],
  showPortal: false,
};