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.4 KB
Raw Blame
import { ARTIFACT_TAG } from '@lobechat/const';
import { escapeXmlAttr } from '@lobechat/prompts';

import { WORKSPACE_HTML_ARTIFACT_INLINE_MAX_BYTES } from './limits';
import { hostedPath, packWorkspaceHtmlDocument } from './packWorkspaceHtmlDocument';
import type {
  WorkspaceHtmlArtifactFile,
  WorkspaceHtmlArtifactPublishInput,
  WorkspaceHtmlArtifactPublishResult,
} from './workspaceHtmlArtifact';

export const wrapWorkspaceHtmlArtifact = ({
  html,
  identifier,
  title,
}: {
  html: string;
  identifier: string;
  title: string;
}): string =>
  `<${ARTIFACT_TAG} identifier="${escapeXmlAttr(identifier)}" type="text/html" title="${escapeXmlAttr(title)}">\n${html}\n</${ARTIFACT_TAG}>`;

export interface PublishWorkspaceHtmlSiteParams {
  artifactIdentifier: string;
  files: WorkspaceHtmlArtifactFile[];
  html: string;
  onPhase?: WorkspaceHtmlArtifactPublishInput['onUploadPhase'];
  onProgress?: WorkspaceHtmlArtifactPublishInput['onUploadProgress'];
  requestedSlug?: string;
  signal?: AbortSignal;
  title?: string;
  topicId: string;
}

export const publishWorkspaceHtmlArtifact = async (
  input: WorkspaceHtmlArtifactPublishInput,
  deps: {
    createMessage: (params: {
      agentId: string;
      content: string;
      role: 'assistant';
      topicId: string;
    }) => Promise<{ id: string }>;
    publishArtifact: (params: {
      artifactIdentifier: string;
      messageId: string;
      requestedSlug?: string;
      topicId: string;
    }) => Promise<{ id?: string; latestRevisionNumber?: number; publicUrl?: string }>;
    publishSite?: (
      params: PublishWorkspaceHtmlSiteParams,
    ) => Promise<{ id?: string; latestRevisionNumber?: number; publicUrl?: string }>;
  },
): Promise<WorkspaceHtmlArtifactPublishResult> => {
  const packed =
    input.packed ??
    (() => {
      const fresh = packWorkspaceHtmlDocument({
        entryPath: input.entryPath,
        files: input.files,
      });
      if (fresh.unresolvedHrefs.length > 0) {
        throw new Error('unresolved-local-assets');
      }
      return fresh;
    })();

  if (
    packed.sidecars.length > 0 ||
    new TextEncoder().encode(packed.html).byteLength > WORKSPACE_HTML_ARTIFACT_INLINE_MAX_BYTES
  ) {
    if (!deps.publishSite) {
      throw new Error('unavailable');
    }

    const deployment = await deps.publishSite({
      artifactIdentifier: input.identifier,
      files: packed.sidecars.map((file) => ({
        ...file,
        path: hostedPath(file.path),
      })),
      html: packed.html,
      onPhase: input.onUploadPhase,
      onProgress: input.onUploadProgress,
      requestedSlug: input.title,
      signal: input.signal,
      title: input.title,
      topicId: input.topicId,
    });

    return {
      id: deployment.id,
      publicUrl: deployment.publicUrl,
      revision: deployment.latestRevisionNumber,
    };
  }

  if (!input.agentId) {
    throw new Error('unavailable');
  }

  const message = await deps.createMessage({
    agentId: input.agentId,
    content: wrapWorkspaceHtmlArtifact({
      html: packed.html,
      identifier: input.identifier,
      title: input.title,
    }),
    role: 'assistant',
    topicId: input.topicId,
  });

  const deployment = await deps.publishArtifact({
    artifactIdentifier: input.identifier,
    messageId: message.id,
    requestedSlug: input.title,
    topicId: input.topicId,
  });

  return {
    id: deployment.id,
    publicUrl: deployment.publicUrl,
    revision: deployment.latestRevisionNumber,
  };
};