Files
wehub-resource-sync d25d482dc2
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
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

58 lines
2.2 KiB
TypeScript

import { AsyncLocalStorage } from 'node:async_hooks'
import type { WorkflowInputField } from '@/lib/workflows/input-format'
import { buildCustomBlockConfig, type CustomBlockRow } from '@/blocks/custom/build-config'
import { registerBlockOverlayResolver } from '@/blocks/custom/overlay'
import type { BlockConfig, BlockIcon } from '@/blocks/types'
/** A row for the overlay, optionally carrying live-derived Start input fields. */
type CustomBlockOverlayRow = CustomBlockRow & {
inputFields?: WorkflowInputField[]
/** When `false`, the block resolves but is hidden from the palette (disabled). */
enabled?: boolean
}
/**
* Server-side custom-block overlay. Resolves `custom_block_*` types during
* serialization + execution from a per-request, per-org map held in
* AsyncLocalStorage — keeping the `@/blocks/registry` accessors synchronous while
* isolating concurrent requests across different organizations.
*/
/** Icon is never rendered server-side (the serializer ignores it). */
const PLACEHOLDER_ICON: BlockIcon = () => null as never
const store = new AsyncLocalStorage<Map<string, BlockConfig>>()
registerBlockOverlayResolver({
get: (type) => store.getStore()?.get(type),
all: () => [...(store.getStore()?.values() ?? [])],
})
/**
* Run `fn` with the given org's custom blocks resolvable via `getBlock`/
* `getAllBlocks`. Wrap every execution serializer entry point (execute route,
* trigger.dev task, scheduled/webhook runs) at the org-context boundary so a
* workflow containing a custom block can serialize and execute.
*
* Execution passes bare rows: `inputMapping` is schema-agnostic, so no per-field
* editors are needed. Agent-facing callers (`get_blocks_metadata`, `edit_workflow`)
* pass rows carrying `inputFields` so `getBlock` exposes the real input sub-blocks —
* matching what the VFS block files show — instead of an empty schema.
*/
export function withCustomBlockOverlay<T>(
rows: CustomBlockOverlayRow[],
fn: () => Promise<T>
): Promise<T> {
const map = new Map<string, BlockConfig>()
for (const row of rows) {
map.set(
row.type,
buildCustomBlockConfig(row, row.inputFields ?? [], {
icon: PLACEHOLDER_ICON,
hideFromToolbar: row.enabled === false,
})
)
}
return store.run(map, fn)
}