Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

16.0 KB
Raw Blame
import type {
  CreateCreatorJobRequest,
  CreatorActionRequest,
  CreatorActionResponse,
  CreatorAgentApproval,
  CreatorAgentApprovalDecisionRequest,
  CreatorAgentHistoryResponse,
  CreatorAgentSteerRequest,
  CreatorAgentTimelineResponse,
  CreatorAgentTurn,
  CreatorAgentTurnRequest,
  CreatorArtifactImportRequest,
  CreatorArtifactImportResponse,
  CreatorEventEnvelope,
  CreatorJob,
  CreatorJobListResponse,
  CreatorClientIssueReportRequest,
  CreatorIssueListResponse,
  OpenCreatorIssue,
  CreatorPresetListResponse,
  CreatorPreflightResponse,
  CreatorStageRun,
  CreatorSubtitlePreviewRequest,
  CreatorSubtitleRenderRequest,
  CreatorSubtitleRenderBundle,
  CreatorSourceUploadResponse,
  CreatorTemplateListResponse,
  CreatorVisualAssetCatalogResponse,
  CreatorVisualAssetKind
} from '@opencreator/protocol';

type ClientLike = {
  get(path: string, options?: { signal?: AbortSignal }): Promise<unknown>;
  post(path: string, body?: unknown): Promise<unknown>;
  delete(path: string): Promise<unknown>;
  postBinary?(path: string, body: BodyInit, contentType?: string): Promise<unknown>;
  rawGet?(path: string, options?: { signal?: AbortSignal }): Promise<Response>;
};

const CREATOR_SOURCE_UPLOAD_CONTENT_TYPE =
  'application/vnd.opencreator.creator-source';
const CREATOR_REFERENCE_IMAGE_CONTENT_TYPE =
  'application/vnd.opencreator.creator-reference-image';
const CREATOR_DOCUMENT_UPLOAD_CONTENT_TYPE =
  'application/vnd.opencreator.creator-document';

export type CreatorJobControlResponse = {
  job: CreatorJob;
  stage: CreatorStageRun;
  control: 'canceling' | 'canceled' | 'resumed';
};

export type CreatorDownloadControl = 'pause' | 'resume' | 'cancel';
export type CreatorDownloadControlResponse = {
  job: CreatorJob;
  stage: CreatorStageRun;
  control: 'pausing' | 'paused' | 'canceling' | 'canceled' | 'resumed';
};

export function createCreatorService(client: ClientLike) {
  const lastEventIdByJob = new Map<string, string>();
  const consumedEventIds = new Map<string, Set<string>>();
  return {
    openDownloadThumbnail(jobId: string, probeArtifactId: string): Promise<Response> {
      if (client.rawGet === undefined) throw new Error('Video thumbnail transport is unavailable');
      return client.rawGet(`/creator/jobs/${encodeURIComponent(jobId)}/artifacts/${encodeURIComponent(probeArtifactId)}/thumbnail`);
    },
    openSubtitlePreview(input: CreatorSubtitlePreviewRequest, options?: { signal?: AbortSignal }): Promise<Response> {
      if (client.rawGet === undefined) throw new Error('Subtitle preview transport is unavailable');
      return client.rawGet(`/creator/subtitle-preview?${new URLSearchParams({ options: JSON.stringify(input) })}`, options);
    },
    prepareSubtitleRender(input: CreatorSubtitleRenderRequest): Promise<CreatorSubtitleRenderBundle> {
      return client.post('/creator/subtitle-render/prepare', input) as Promise<CreatorSubtitleRenderBundle>;
    },
    openSubtitleFont(id: string): Promise<Response> {
      if (!client.rawGet) throw new Error('Subtitle font transport is unavailable');
      return client.rawGet(`/creator/subtitle-render/fonts/${encodeURIComponent(id)}`);
    },
    listTemplates(): Promise<CreatorTemplateListResponse> {
      return client.get('/creator/templates') as Promise<CreatorTemplateListResponse>;
    },
    listPresets(locale: string): Promise<CreatorPresetListResponse> {
      const presetLocale = locale === 'zh-CN' ? 'zh-CN' : 'en-US';
      return client.get(
        `/creator/presets?locale=${encodeURIComponent(presetLocale)}`
      ) as Promise<CreatorPresetListResponse>;
    },
    listVisualAssets(
      templateId: string,
      kind?: CreatorVisualAssetKind
    ): Promise<CreatorVisualAssetCatalogResponse> {
      const query = new URLSearchParams({ templateId });
      if (kind !== undefined) query.set('kind', kind);
      return client.get(
        `/creator/visual-assets?${query.toString()}`
      ) as Promise<CreatorVisualAssetCatalogResponse>;
    },
    openVisualAssetPreview(assetId: string, revision: number): Promise<Response> {
      if (client.rawGet === undefined) {
        throw new Error('Creator visual asset preview transport is unavailable');
      }
      return client.rawGet(
        `/creator/visual-assets/${encodeURIComponent(assetId)}/revisions/${revision}/preview`
      );
    },
    createJob(request: CreateCreatorJobRequest): Promise<{ job: CreatorJob }> {
      return client.post('/creator/jobs', request) as Promise<{ job: CreatorJob }>;
    },
    listJobs(projectId?: string): Promise<CreatorJobListResponse> {
      return client.get(projectId === undefined
        ? '/creator/jobs'
        : `/creator/jobs?projectId=${encodeURIComponent(projectId)}`) as Promise<CreatorJobListResponse>;
    },
    getJob(jobId: string, options?: { signal?: AbortSignal }): Promise<{ job: CreatorJob }> {
      return client.get(`/creator/jobs/${encodeURIComponent(jobId)}`, options) as Promise<{ job: CreatorJob }>;
    },
    preflight(jobId: string, stageId: string, options?: { inputResultVersion?: number }): Promise<CreatorPreflightResponse> {
      const version = options?.inputResultVersion;
      const query = version === undefined ? '' : `&inputResultVersion=${encodeURIComponent(version)}`;
      return client.get(`/creator/jobs/${encodeURIComponent(jobId)}/preflight?stageId=${encodeURIComponent(stageId)}${query}`) as Promise<CreatorPreflightResponse>;
    },
    deleteJob(jobId: string, options: { deleteFiles?: boolean } = {}): Promise<void> {
      const query = options.deleteFiles === true ? '?deleteFiles=true' : '';
      return client.delete(`/creator/jobs/${encodeURIComponent(jobId)}${query}`) as Promise<void>;
    },
    uploadSourceVideo(jobId: string, input: {
      file: File;
      expectedRevision: number;
      repairIssueId?: string;
      resolutionAttemptId?: string;
    }): Promise<CreatorSourceUploadResponse> {
      if (client.postBinary === undefined) {
        return Promise.reject(new Error('Creator source upload transport is unavailable'));
      }
      const query = new URLSearchParams({
        expectedRevision: String(input.expectedRevision),
        fileName: input.file.name,
        mime: input.file.type || 'application/octet-stream',
        lastModified: String(input.file.lastModified)
      });
      if (input.repairIssueId !== undefined) query.set('repairIssueId', input.repairIssueId);
      if (input.resolutionAttemptId !== undefined) query.set('resolutionAttemptId', input.resolutionAttemptId);
      return client.postBinary(
        `/creator/jobs/${encodeURIComponent(jobId)}/source-video?${query.toString()}`,
        input.file,
        CREATOR_SOURCE_UPLOAD_CONTENT_TYPE
      ) as Promise<CreatorSourceUploadResponse>;
    },
    uploadReferenceImage(jobId: string, input: {
      file: File;
      expectedRevision: number;
    }): Promise<CreatorSourceUploadResponse> {
      if (client.postBinary === undefined) {
        return Promise.reject(new Error('Creator reference upload transport is unavailable'));
      }
      const query = new URLSearchParams({
        expectedRevision: String(input.expectedRevision),
        fileName: input.file.name,
        mime: input.file.type || 'application/octet-stream',
        lastModified: String(input.file.lastModified)
      });
      return client.postBinary(
        `/creator/jobs/${encodeURIComponent(jobId)}/reference-image?${query.toString()}`,
        input.file,
        CREATOR_REFERENCE_IMAGE_CONTENT_TYPE
      ) as Promise<CreatorSourceUploadResponse>;
    },
    uploadVideoReference(file: File): Promise<{ assetId: string; expiresAt: string }> {
      if (!client.postBinary) return Promise.reject(new Error('Video reference upload transport is unavailable'));
      return client.postBinary(`/video-generation/assets?${new URLSearchParams({ mime: file.type })}`, file, 'application/vnd.opencreator.video-reference') as Promise<{ assetId: string; expiresAt: string }>;
    },
    uploadArticleImage(jobId: string, input: {
      file: File;
      expectedRevision: number;
    }): Promise<CreatorSourceUploadResponse> {
      if (client.postBinary === undefined) {
        return Promise.reject(new Error('Creator article image upload transport is unavailable'));
      }
      const query = new URLSearchParams({
        expectedRevision: String(input.expectedRevision),
        fileName: input.file.name,
        mime: input.file.type || 'application/octet-stream',
        lastModified: String(input.file.lastModified)
      });
      return client.postBinary(
        `/creator/jobs/${encodeURIComponent(jobId)}/article-image?${query.toString()}`,
        input.file,
        CREATOR_REFERENCE_IMAGE_CONTENT_TYPE
      ) as Promise<CreatorSourceUploadResponse>;
    },
    uploadSourceDocument(jobId: string, input: {
      file: File;
      expectedRevision: number;
    }): Promise<CreatorSourceUploadResponse> {
      if (client.postBinary === undefined) {
        return Promise.reject(new Error('Creator document upload transport is unavailable'));
      }
      const query = new URLSearchParams({
        expectedRevision: String(input.expectedRevision),
        fileName: input.file.name,
        mime: input.file.type || 'application/octet-stream',
        lastModified: String(input.file.lastModified)
      });
      return client.postBinary(
        `/creator/jobs/${encodeURIComponent(jobId)}/source-document?${query.toString()}`,
        input.file,
        CREATOR_DOCUMENT_UPLOAD_CONTENT_TYPE
      ) as Promise<CreatorSourceUploadResponse>;
    },
    importArtifact(
      jobId: string,
      request: CreatorArtifactImportRequest
    ): Promise<CreatorArtifactImportResponse> {
      return client.post(
        `/creator/jobs/${encodeURIComponent(jobId)}/import-artifact`,
        request
      ) as Promise<CreatorArtifactImportResponse>;
    },
    openProjectCover(jobId: string): Promise<Response> {
      if (client.rawGet === undefined) throw new Error('Creator project cover transport is unavailable');
      return client.rawGet(`/creator/jobs/${encodeURIComponent(jobId)}/cover`);
    },
    openArtifact(jobId: string, artifactId: string): Promise<Response> {
      if (client.rawGet === undefined) throw new Error('Creator artifact transport is unavailable');
      return client.rawGet(
        `/creator/jobs/${encodeURIComponent(jobId)}/artifacts/${encodeURIComponent(artifactId)}/content`
      );
    },
    applyAction(jobId: string, request: CreatorActionRequest): Promise<CreatorActionResponse> {
      return client.post(`/creator/jobs/${encodeURIComponent(jobId)}/actions`, request) as Promise<CreatorActionResponse>;
    },
    listIssues(jobId: string, cursor?: string): Promise<CreatorIssueListResponse> {
      const query = cursor === undefined ? '' : `?cursor=${encodeURIComponent(cursor)}`;
      return client.get(
        `/creator/jobs/${encodeURIComponent(jobId)}/issues${query}`
      ) as Promise<CreatorIssueListResponse>;
    },
    reportClientIssue(
      jobId: string,
      request: CreatorClientIssueReportRequest
    ): Promise<{ clientIssueId: string; issue: OpenCreatorIssue }> {
      return client.post(
        `/creator/jobs/${encodeURIComponent(jobId)}/issues/report`,
        request
      ) as Promise<{ clientIssueId: string; issue: OpenCreatorIssue }>;
    },
    controlDownload(jobId: string, stageRunId: string, action: CreatorDownloadControl): Promise<CreatorDownloadControlResponse> {
      return client.post(`/creator/jobs/${encodeURIComponent(jobId)}/downloads/${encodeURIComponent(stageRunId)}/${action}`) as Promise<CreatorDownloadControlResponse>;
    },
    cancelJob(jobId: string): Promise<CreatorJobControlResponse> {
      return client.post(
        `/creator/jobs/${encodeURIComponent(jobId)}/cancel`
      ) as Promise<CreatorJobControlResponse>;
    },
    resumeJob(jobId: string): Promise<CreatorJobControlResponse> {
      return client.post(
        `/creator/jobs/${encodeURIComponent(jobId)}/resume`
      ) as Promise<CreatorJobControlResponse>;
    },
    runAgentTurn(jobId: string, request: CreatorAgentTurnRequest): Promise<{ turn: CreatorAgentTurn; action?: CreatorActionResponse }> {
      return client.post(`/creator/jobs/${encodeURIComponent(jobId)}/agent-turns`, request) as Promise<{ turn: CreatorAgentTurn; action?: CreatorActionResponse }>;
    },
    startAgentTurn(jobId: string, request: CreatorAgentTurnRequest): Promise<{ turn: CreatorAgentTurn; action?: CreatorActionResponse }> {
      return client.post(`/creator/jobs/${encodeURIComponent(jobId)}/agent-turns`, request) as Promise<{ turn: CreatorAgentTurn; action?: CreatorActionResponse }>;
    },
    steerAgentTurn(jobId: string, request: CreatorAgentSteerRequest): Promise<{ turn: CreatorAgentTurn }> {
      return client.post(`/creator/jobs/${encodeURIComponent(jobId)}/agent-steer`, request) as Promise<{ turn: CreatorAgentTurn }>;
    },
    interruptAgentTurn(jobId: string): Promise<{ interrupted: true }> {
      return client.post(`/creator/jobs/${encodeURIComponent(jobId)}/agent-interrupt`) as Promise<{ interrupted: true }>;
    },
    respondAgentApproval(
      jobId: string,
      approvalId: string,
      request: CreatorAgentApprovalDecisionRequest
    ): Promise<{ approval: CreatorAgentApproval }> {
      return client.post(
        `/creator/jobs/${encodeURIComponent(jobId)}/agent-approvals/${encodeURIComponent(approvalId)}`,
        request
      ) as Promise<{ approval: CreatorAgentApproval }>;
    },
    getAgentHistory(jobId: string): Promise<CreatorAgentHistoryResponse> {
      return client.get(`/creator/jobs/${encodeURIComponent(jobId)}/agent-history`) as Promise<CreatorAgentHistoryResponse>;
    },
    getAgentTimeline(jobId: string): Promise<CreatorAgentTimelineResponse> {
      return client.get(`/creator/jobs/${encodeURIComponent(jobId)}/agent-timeline`) as Promise<CreatorAgentTimelineResponse>;
    },
    subscribeJobEvents(
      jobId: string,
      onEvent: (event: CreatorEventEnvelope) => void,
      onDisconnect: (error?: unknown) => void
    ): { close(): void } {
      const controller = new AbortController();
      void (async () => {
        if (client.rawGet === undefined) return;
        const cursor = lastEventIdByJob.get(jobId);
        const response = await client.rawGet(
          `/creator/jobs/${encodeURIComponent(jobId)}/events${cursor === undefined
            ? ''
            : `?cursor=${encodeURIComponent(cursor)}`}`,
          { signal: controller.signal }
        );
        const reader = response.body?.getReader();
        if (reader === undefined) throw new Error('Creator event stream is unavailable');
        const decoder = new TextDecoder();
        let buffered = '';
        while (!controller.signal.aborted) {
          const item = await reader.read();
          if (item.done) break;
          buffered += decoder.decode(item.value, { stream: true });
          let boundary = buffered.indexOf('\n\n');
          while (boundary >= 0) {
            const frame = buffered.slice(0, boundary);
            buffered = buffered.slice(boundary + 2);
            const event = parseCreatorSseFrame(frame);
            if (event !== undefined) {
              const consumed = consumedEventIds.get(jobId) ?? new Set<string>();
              consumedEventIds.set(jobId, consumed);
              lastEventIdByJob.set(jobId, event.id);
              if (!consumed.has(event.id)) {
                consumed.add(event.id);
                onEvent(event);
              }
            }
            boundary = buffered.indexOf('\n\n');
          }
        }
        if (!controller.signal.aborted) onDisconnect();
      })().catch(error => {
        if (!controller.signal.aborted) onDisconnect(error);
      });
      return { close: () => controller.abort() };
    }
  };
}

export type CreatorWebService = ReturnType<typeof createCreatorService>;

function parseCreatorSseFrame(frame: string): CreatorEventEnvelope | undefined {
  const lines = frame.split('\n');
  const id = lines.find(line => line.startsWith('id: '))?.slice(4).trim();
  const data = lines.find(line => line.startsWith('data: '))?.slice(6);
  if (id === undefined || data === undefined) return undefined;
  const parsed = JSON.parse(data) as CreatorEventEnvelope;
  return parsed.id === id ? parsed : undefined;
}