Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

12.1 KB
Raw Blame
import type { AccountScope } from '@platform/state/accountLifecycle';

import { createUuid } from '@platform/browser/randomUuid';
import { isAccountScopeCurrent } from '@platform/state/accountLifecycle';
import { getApiErrorMessage } from '@platform/transport/http';

import type { WorkflowLibraryWriteRefusedError, WorkflowRecordDTO } from './api';

import {
  createLibraryWorkflowRecord,
  getWorkflowRecordCategory,
  getLibraryWorkflowRecord,
  setLibraryWorkflowPublic,
  toWorkflowLibraryWriteRefusal,
  updateLibraryWorkflow,
} from './api';
import { invalidateWorkflowLibraryCache } from './libraryCache';

/**
 * The one way a project workflow reaches the library: no overlap per workflow, the captured request and reserved id
 * survive retryable failures, and an ambiguous answer is reconciled before resending.
 */

export type WorkflowPublicationDestination =
  | { kind: 'create'; name: string }
  | { kind: 'update'; libraryWorkflowId: string; expectedRevision: number };

export interface WorkflowPublicationRequest {
  owner: AccountScope;
  projectId: string;
  workflowId: string;
  /** The serialized document as it stood when the user confirmed; later edits are not part of this publication. */
  workflow: Record<string, unknown>;
  /** When true, create the template privately first and then request public visibility. */
  isPublic?: boolean;
  destination: WorkflowPublicationDestination;
}

export type WorkflowPublicationResult =
  | {
      status: 'published';
      kind: 'created' | 'updated';
      libraryWorkflowId: string;
      ownerUserId?: string;
      revision: number;
      name: string;
    }
  /** The template moved past the expected revision, or its revision was never known. */
  | { status: 'conflict'; libraryWorkflowId: string; currentRevision: number | null }
  /** The destination cannot be written: it is gone, someone else's, or bundled. Saving as new still works. */
  | { status: 'unavailable'; libraryWorkflowId: string; reason: 'missing' | 'forbidden' | 'bundled' }
  | { status: 'invalid'; message: string }
  /** The template exists privately, but the requested visibility change did not settle. */
  | {
      status: 'partial';
      libraryWorkflowId: string;
      message: string;
      retry: () => Promise<WorkflowPublicationResult>;
    }
  /** Nothing definitive came back; `retry` resends the same captured request under the same reserved id. */
  | { status: 'failed'; message: string; retry: () => Promise<WorkflowPublicationResult> }
  /** The account changed underneath the publication; nothing more will be reported. */
  | { status: 'cancelled' }
  | { status: 'busy' };

export interface WorkflowPublicationDeps {
  createRecord: typeof createLibraryWorkflowRecord;
  getRecord: typeof getLibraryWorkflowRecord;
  invalidateCache: typeof invalidateWorkflowLibraryCache;
  reserveId: () => string;
  setPublic: typeof setLibraryWorkflowPublic;
  updateRecord: typeof updateLibraryWorkflow;
}

const PRODUCTION_DEPS: WorkflowPublicationDeps = {
  createRecord: createLibraryWorkflowRecord,
  getRecord: getLibraryWorkflowRecord,
  invalidateCache: invalidateWorkflowLibraryCache,
  reserveId: createUuid,
  setPublic: setLibraryWorkflowPublic,
  updateRecord: updateLibraryWorkflow,
};

/** The fields the server keeps; anything else a payload carries is ignored on both sides. */
const CONTENT_KEYS = [
  'author',
  'contact',
  'description',
  'edges',
  'exposedFields',
  'form',
  'meta',
  'name',
  'nodes',
  'notes',
  'tags',
  'version',
] as const;

const canonicalize = (value: unknown): unknown => {
  if (Array.isArray(value)) {
    return value.map(canonicalize);
  }

  if (typeof value === 'object' && value !== null) {
    return Object.fromEntries(
      Object.keys(value)
        .sort()
        .map((key) => [key, canonicalize((value as Record<string, unknown>)[key])])
    );
  }

  return value;
};

/** Whether a stored record holds the submitted content, ignoring key order and the record's own id. */
export const isWorkflowContentEquivalent = (
  submitted: Record<string, unknown>,
  stored: Record<string, unknown>
): boolean =>
  CONTENT_KEYS.every(
    (key) => JSON.stringify(canonicalize(submitted[key] ?? null)) === JSON.stringify(canonicalize(stored[key] ?? null))
  );

const toPublished = (
  kind: 'created' | 'updated',
  record: WorkflowRecordDTO,
  revision = record.revision
): Extract<WorkflowPublicationResult, { status: 'published' }> => ({
  kind,
  libraryWorkflowId: record.workflow_id,
  name: record.name,
  ...(getWorkflowRecordCategory(record) === 'user' ? { ownerUserId: record.user_id } : {}),
  revision,
  status: 'published',
});

const toRefusalResult = (
  refusal: WorkflowLibraryWriteRefusedError,
  libraryWorkflowId: string
): WorkflowPublicationResult => {
  switch (refusal.reason) {
    case 'revision-conflict':
      return { currentRevision: refusal.currentRevision, libraryWorkflowId, status: 'conflict' };
    case 'missing':
    case 'forbidden':
    case 'bundled':
      return { libraryWorkflowId, reason: refusal.reason, status: 'unavailable' };
    case 'id-conflict':
    case 'invalid':
      return { message: refusal.message, status: 'invalid' };
  }
};

export interface WorkflowPublicationController {
  publish(request: WorkflowPublicationRequest): Promise<WorkflowPublicationResult>;
  isPublishing(projectId: string, workflowId: string): boolean;
  /** Advances whenever the in-flight set changes; a stable snapshot for `useSyncExternalStore`. */
  getVersion(): number;
  subscribe(listener: () => void): () => void;
}

export const createWorkflowPublicationController = (
  overrides: Partial<WorkflowPublicationDeps> = {}
): WorkflowPublicationController => {
  const deps = { ...PRODUCTION_DEPS, ...overrides };
  const inFlight = new Set<string>();
  const listeners = new Set<() => void>();
  let version = 0;
  const keyOf = (projectId: string, workflowId: string): string => `${projectId}\u0000${workflowId}`;
  const notify = (): void => {
    version += 1;
    listeners.forEach((listener) => listener());
  };

  const settle = async (
    request: WorkflowPublicationRequest,
    send: () => Promise<WorkflowPublicationResult>
  ): Promise<WorkflowPublicationResult> => {
    const key = keyOf(request.projectId, request.workflowId);

    if (inFlight.has(key)) {
      return { status: 'busy' };
    }

    inFlight.add(key);
    notify();

    try {
      if (!isAccountScopeCurrent(request.owner)) {
        return { status: 'cancelled' };
      }

      const result = await send();

      if (!isAccountScopeCurrent(request.owner)) {
        return { status: 'cancelled' };
      }

      if (result.status === 'published' || result.status === 'partial') {
        deps.invalidateCache(result.libraryWorkflowId);
      }

      return result;
    } finally {
      inFlight.delete(key);
      notify();
    }
  };

  /** A failure that is not a server refusal: the request may or may not have landed. */
  const toFailure = (
    error: unknown,
    request: WorkflowPublicationRequest,
    retry: () => Promise<WorkflowPublicationResult>
  ): WorkflowPublicationResult => {
    if (!isAccountScopeCurrent(request.owner)) {
      return { status: 'cancelled' };
    }

    return { message: getApiErrorMessage(error, 'The library did not answer.'), retry, status: 'failed' };
  };

  const create = (request: WorkflowPublicationRequest, name: string, reservedId: string) => {
    const retryVisibility = (
      libraryWorkflowId: string,
      creationRevision: number
    ): Promise<WorkflowPublicationResult> => {
      const attempt = (): Promise<WorkflowPublicationResult> =>
        settle(request, async () => {
          try {
            return toPublished(
              'created',
              await deps.setPublic(libraryWorkflowId, true, request.owner.signal),
              creationRevision
            );
          } catch (error) {
            if (!isAccountScopeCurrent(request.owner)) {
              return { status: 'cancelled' };
            }

            return {
              libraryWorkflowId,
              message: getApiErrorMessage(error, 'The library did not answer.'),
              retry: attempt,
              status: 'partial',
            };
          }
        });

      return attempt();
    };

    const attempt = (): Promise<WorkflowPublicationResult> =>
      settle(request, async () => {
        try {
          // The server matches a resend against the first send's record, so a lost response never makes a second one.
          const record = await deps.createRecord(
            { ...request.workflow, name },
            { reservedId, signal: request.owner.signal }
          );

          if (request.isPublic === true) {
            try {
              return toPublished(
                'created',
                await deps.setPublic(record.workflow_id, true, request.owner.signal),
                record.revision
              );
            } catch (error) {
              if (!isAccountScopeCurrent(request.owner)) {
                return { status: 'cancelled' };
              }

              return {
                libraryWorkflowId: record.workflow_id,
                message: getApiErrorMessage(error, 'The library did not answer.'),
                retry: () => retryVisibility(record.workflow_id, record.revision),
                status: 'partial',
              };
            }
          }

          return toPublished('created', record);
        } catch (error) {
          const refusal = toWorkflowLibraryWriteRefusal(error);

          return refusal ? toRefusalResult(refusal, reservedId) : toFailure(error, request, attempt);
        }
      });

    return attempt();
  };

  const update = (request: WorkflowPublicationRequest, libraryWorkflowId: string, expectedRevision: number) => {
    // The template keeps its own name: an update replaces its content, and the project workflow's name stays with the
    // project. The content is fixed by the first read so a resend carries exactly what was sent before.
    let content: Record<string, unknown> | null = null;

    const send = async (record: WorkflowRecordDTO): Promise<WorkflowPublicationResult> => {
      content ??= { ...request.workflow, name: record.name };

      try {
        const updated = await deps.updateRecord(libraryWorkflowId, content, {
          expectedRevision,
          signal: request.owner.signal,
        });

        return toPublished('updated', updated);
      } catch (error) {
        const refusal = toWorkflowLibraryWriteRefusal(error);

        return refusal ? toRefusalResult(refusal, libraryWorkflowId) : toFailure(error, request, attempt);
      }
    };

    /**
     * Every attempt starts from the live record; before a resend it tells whether the lost answer applied (revision
     * advanced by exactly one to this content), did not, or someone else moved the template.
     */
    const attempt = (): Promise<WorkflowPublicationResult> =>
      settle(request, async () => {
        let record: WorkflowRecordDTO;

        try {
          record = await deps.getRecord(libraryWorkflowId, request.owner.signal);
        } catch (error) {
          const refusal = toWorkflowLibraryWriteRefusal(error);

          return refusal ? toRefusalResult(refusal, libraryWorkflowId) : toFailure(error, request, attempt);
        }

        if (record.revision === expectedRevision) {
          return send(record);
        }

        if (
          content !== null &&
          record.revision === expectedRevision + 1 &&
          isWorkflowContentEquivalent(content, record.workflow)
        ) {
          return toPublished('updated', record);
        }

        return { currentRevision: record.revision, libraryWorkflowId, status: 'conflict' };
      });

    return attempt();
  };

  return {
    getVersion: () => version,
    isPublishing: (projectId, workflowId) => inFlight.has(keyOf(projectId, workflowId)),
    publish: (request) =>
      request.destination.kind === 'create'
        ? create(request, request.destination.name, deps.reserveId())
        : update(request, request.destination.libraryWorkflowId, request.destination.expectedRevision),
    subscribe: (listener) => {
      listeners.add(listener);
      return () => listeners.delete(listener);
    },
  };
};