chore: import upstream snapshot with attribution
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:20:55 +08:00
commit d25d482dc2
13754 changed files with 4996608 additions and 0 deletions
@@ -0,0 +1,60 @@
import { useInfiniteQuery } from '@tanstack/react-query'
import { requestJson } from '@/lib/api/client/request'
import {
type BackgroundWorkItem,
type GetWorkspaceBackgroundWorkResponse,
getWorkspaceBackgroundWorkContract,
} from '@/lib/api/contracts/workspace-fork'
export const backgroundWorkKeys = {
all: ['backgroundWork'] as const,
// 'infinite' segments the key from the pre-pagination plain-query era: the data shape
// under the old key was an array, and an infinite query reading such a cache entry
// renders as empty. A shape change must always re-key.
lists: () => [...backgroundWorkKeys.all, 'list', 'infinite'] as const,
list: (workspaceId?: string) => [...backgroundWorkKeys.lists(), workspaceId ?? ''] as const,
}
export const BACKGROUND_WORK_STALE_TIME = 5_000
/** Page size for the fork Activity feed, matching the audit log's. */
const BACKGROUND_WORK_PAGE_SIZE = '50'
async function fetchWorkspaceBackgroundWork(
workspaceId: string,
cursor?: string,
signal?: AbortSignal
): Promise<GetWorkspaceBackgroundWorkResponse> {
return requestJson(getWorkspaceBackgroundWorkContract, {
params: { id: workspaceId },
query: { cursor, limit: BACKGROUND_WORK_PAGE_SIZE },
signal,
})
}
const isActive = (item: BackgroundWorkItem) =>
item.status === 'pending' || item.status === 'processing'
/**
* Durable "background work in progress" status for a workspace (fork content copy +
* any deployment side-effects), keyset-paginated like the enterprise audit log
* (`getNextPageParam` from the page's `nextCursor`). Poll-first per the best-practice
* for long jobs: the status survives a reload (it's a DB row), and we only keep
* polling while something is still running, then stop - the poll refetches every
* loaded page sequentially with fresh cursors, so pagination stays consistent.
* Refetch on focus catches changes after the tab was away.
*/
export function useWorkspaceBackgroundWork(workspaceId?: string) {
return useInfiniteQuery({
queryKey: backgroundWorkKeys.list(workspaceId),
queryFn: ({ pageParam, signal }) =>
fetchWorkspaceBackgroundWork(workspaceId as string, pageParam, signal),
initialPageParam: undefined as string | undefined,
getNextPageParam: (lastPage) => lastPage.nextCursor,
enabled: Boolean(workspaceId),
staleTime: BACKGROUND_WORK_STALE_TIME,
refetchInterval: (query) =>
(query.state.data?.pages ?? []).some((page) => page.items.some(isActive)) ? 5_000 : false,
refetchOnWindowFocus: true,
})
}
@@ -0,0 +1,40 @@
import { useQuery } from '@tanstack/react-query'
import { requestJson } from '@/lib/api/client/request'
import { getForkAvailabilityContract } from '@/lib/api/contracts/workspace-fork'
export const forkAvailabilityKeys = {
all: ['fork-availability'] as const,
details: () => [...forkAvailabilityKeys.all, 'detail'] as const,
detail: (workspaceId?: string) => [...forkAvailabilityKeys.details(), workspaceId ?? ''] as const,
}
/** Availability flips only on plan changes or flag rollouts - cache generously. */
const FORK_AVAILABILITY_STALE_TIME = 5 * 60 * 1000
interface ForkingAvailability {
available: boolean
/** The lookup is still in flight - callers that gate a whole page wait on this. */
isLoading: boolean
}
/**
* Server-evaluated fork availability for the workspace: the verdict of the exact gate
* every fork route enforces (env/plan + the `workspace-forking` AppConfig rollout
* flag), served by the availability route. Used to hide the Forks settings tab and
* the fork context-menu entries; the server gate remains the security boundary.
*/
export function useForkingAvailability(workspaceId?: string): ForkingAvailability {
const { data, isLoading } = useQuery({
queryKey: forkAvailabilityKeys.detail(workspaceId),
queryFn: ({ signal }) =>
requestJson(getForkAvailabilityContract, { params: { id: workspaceId as string }, signal }),
enabled: Boolean(workspaceId),
staleTime: FORK_AVAILABILITY_STALE_TIME,
})
return { available: data?.available ?? false, isLoading }
}
/** Boolean shorthand for surfaces that only show/hide fork entry points. */
export function useForkingAvailable(workspaceId?: string): boolean {
return useForkingAvailability(workspaceId).available
}
@@ -0,0 +1,203 @@
import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { requestJson } from '@/lib/api/client/request'
import {
type ForkWorkspaceBody,
forkWorkspaceContract,
getForkDiffContract,
getForkLineageContract,
getForkMappingContract,
getForkResourcesContract,
type PromoteForkBody,
promoteForkContract,
type RollbackForkBody,
rollbackForkContract,
type UnlinkForkBody,
type UpdateForkMappingBody,
unlinkForkContract,
updateForkMappingContract,
} from '@/lib/api/contracts/workspace-fork'
import type { WorkspacesResponse } from '@/lib/api/contracts/workspaces'
import { backgroundWorkKeys } from '@/ee/workspace-forking/hooks/background-work'
import { deploymentKeys } from '@/hooks/queries/deployments'
import { workspaceKeys } from '@/hooks/queries/workspace'
export type ForkDirection = 'push' | 'pull'
export const forkKeys = {
all: ['workspace-fork'] as const,
lineages: () => [...forkKeys.all, 'lineage'] as const,
lineage: (workspaceId?: string) => [...forkKeys.lineages(), workspaceId ?? ''] as const,
mappings: () => [...forkKeys.all, 'mapping'] as const,
mapping: (workspaceId?: string, otherWorkspaceId?: string, direction?: ForkDirection) =>
[...forkKeys.mappings(), workspaceId ?? '', otherWorkspaceId ?? '', direction ?? ''] as const,
diffs: () => [...forkKeys.all, 'diff'] as const,
diff: (workspaceId?: string, otherWorkspaceId?: string, direction?: ForkDirection) =>
[...forkKeys.diffs(), workspaceId ?? '', otherWorkspaceId ?? '', direction ?? ''] as const,
resourcesAll: () => [...forkKeys.all, 'resources'] as const,
resources: (workspaceId?: string) => [...forkKeys.resourcesAll(), workspaceId ?? ''] as const,
}
export const WORKSPACE_FORK_RESOURCES_STALE_TIME = 30 * 1000
export const WORKSPACE_FORK_LINEAGE_STALE_TIME = 30 * 1000
export const WORKSPACE_FORK_MAPPING_STALE_TIME = 15 * 1000
export const WORKSPACE_FORK_DIFF_STALE_TIME = 10 * 1000
export function useForkResources(workspaceId?: string, enabled = true) {
return useQuery({
queryKey: forkKeys.resources(workspaceId),
queryFn: ({ signal }) =>
requestJson(getForkResourcesContract, { params: { id: workspaceId as string }, signal }),
enabled: Boolean(workspaceId) && enabled,
staleTime: WORKSPACE_FORK_RESOURCES_STALE_TIME,
})
}
export function useForkLineage(workspaceId?: string, enabled = true) {
return useQuery({
queryKey: forkKeys.lineage(workspaceId),
queryFn: ({ signal }) =>
requestJson(getForkLineageContract, { params: { id: workspaceId as string }, signal }),
enabled: Boolean(workspaceId) && enabled,
staleTime: WORKSPACE_FORK_LINEAGE_STALE_TIME,
placeholderData: keepPreviousData,
})
}
export function useForkWorkspace() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (vars: { workspaceId: string; body: ForkWorkspaceBody }) =>
requestJson(forkWorkspaceContract, { params: { id: vars.workspaceId }, body: vars.body }),
onSuccess: (data) => {
// Merge the new fork into the active list cache before invalidation so the
// immediate navigation into it can't race a stale list and trip the
// not-in-workspaces redirect (mirrors useCreateWorkspace).
const newWorkspace = data.workspace
queryClient.setQueryData<WorkspacesResponse>(workspaceKeys.list('active'), (previous) => {
if (!previous) {
return { workspaces: [newWorkspace], lastActiveWorkspaceId: null, creationPolicy: null }
}
if (previous.workspaces.some((w) => w.id === newWorkspace.id)) {
return previous
}
return { ...previous, workspaces: [newWorkspace, ...previous.workspaces] }
})
queryClient.invalidateQueries({ queryKey: workspaceKeys.lists() })
queryClient.invalidateQueries({ queryKey: workspaceKeys.adminLists() })
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: forkKeys.lineages() })
queryClient.invalidateQueries({ queryKey: backgroundWorkKeys.lists() })
},
})
}
export function useForkMapping(args: {
workspaceId?: string
otherWorkspaceId?: string
direction: ForkDirection
enabled?: boolean
}) {
return useQuery({
queryKey: forkKeys.mapping(args.workspaceId, args.otherWorkspaceId, args.direction),
queryFn: ({ signal }) =>
requestJson(getForkMappingContract, {
params: { id: args.workspaceId as string },
query: { otherWorkspaceId: args.otherWorkspaceId as string, direction: args.direction },
signal,
}),
enabled: Boolean(args.workspaceId && args.otherWorkspaceId) && (args.enabled ?? true),
staleTime: WORKSPACE_FORK_MAPPING_STALE_TIME,
placeholderData: keepPreviousData,
})
}
export function useUpdateForkMapping() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (vars: { workspaceId: string; body: UpdateForkMappingBody }) =>
requestJson(updateForkMappingContract, { params: { id: vars.workspaceId }, body: vars.body }),
onSettled: () => {
queryClient.invalidateQueries({ queryKey: forkKeys.mappings() })
queryClient.invalidateQueries({ queryKey: forkKeys.diffs() })
},
})
}
export function useForkDiff(args: {
workspaceId?: string
otherWorkspaceId?: string
direction: ForkDirection
enabled?: boolean
}) {
return useQuery({
queryKey: forkKeys.diff(args.workspaceId, args.otherWorkspaceId, args.direction),
queryFn: ({ signal }) =>
requestJson(getForkDiffContract, {
params: { id: args.workspaceId as string },
query: { otherWorkspaceId: args.otherWorkspaceId as string, direction: args.direction },
signal,
}),
enabled: Boolean(args.workspaceId && args.otherWorkspaceId) && (args.enabled ?? true),
staleTime: WORKSPACE_FORK_DIFF_STALE_TIME,
placeholderData: keepPreviousData,
})
}
export function usePromoteFork() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (vars: { workspaceId: string; body: PromoteForkBody }) =>
requestJson(promoteForkContract, { params: { id: vars.workspaceId }, body: vars.body }),
onSettled: () => {
// A sync changes lineage (undoable run), mappings, and the diff - not the
// workspace's copyable resource inventory, so leave `resources` cached.
queryClient.invalidateQueries({ queryKey: forkKeys.lineages() })
queryClient.invalidateQueries({ queryKey: forkKeys.mappings() })
queryClient.invalidateQueries({ queryKey: forkKeys.diffs() })
queryClient.invalidateQueries({ queryKey: backgroundWorkKeys.lists() })
// A sync rewrites the target workflows' drafts AND redeploys them. The promote
// result doesn't expose the affected ids, so invalidate all deployment caches:
// otherwise a target workflow whose deployed state was already cached compares its
// fresh draft against the stale (pre-sync) deployed snapshot and falsely shows
// "Update" instead of "Live".
queryClient.invalidateQueries({ queryKey: deploymentKeys.all })
},
})
}
export function useUnlinkFork() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (vars: { workspaceId: string; body: UnlinkForkBody }) =>
requestJson(unlinkForkContract, { params: { id: vars.workspaceId }, body: vars.body }),
onSettled: () => {
// Unlink dissolves the edge: lineage loses the row, and the edge's mappings/diff
// no longer exist. Workflows and deployments are untouched.
queryClient.invalidateQueries({ queryKey: forkKeys.lineages() })
queryClient.invalidateQueries({ queryKey: forkKeys.mappings() })
queryClient.invalidateQueries({ queryKey: forkKeys.diffs() })
queryClient.invalidateQueries({ queryKey: backgroundWorkKeys.lists() })
},
})
}
export function useRollbackFork() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (vars: { workspaceId: string; body: RollbackForkBody }) =>
requestJson(rollbackForkContract, { params: { id: vars.workspaceId }, body: vars.body }),
onSettled: () => {
// Rollback changes lineage, mappings, and the diff - not the copyable resource
// inventory, so leave `resources` cached (mirrors usePromoteFork).
queryClient.invalidateQueries({ queryKey: forkKeys.lineages() })
queryClient.invalidateQueries({ queryKey: forkKeys.mappings() })
queryClient.invalidateQueries({ queryKey: forkKeys.diffs() })
queryClient.invalidateQueries({ queryKey: backgroundWorkKeys.lists() })
// Rollback restores the target workflows' drafts + reactivates a prior deployment,
// so the cached deployed snapshots are stale - refresh them so change detection
// doesn't falsely show "Update" (mirrors usePromoteFork).
queryClient.invalidateQueries({ queryKey: deploymentKeys.all })
},
})
}