managarten/packages/website-blocks/src/registry.ts
Till JS 54a12ffd5c feat(webapp): wire isParallelSafe in Companion chat + Mission runner
Enables the M1 parallel-reads optimisation on the webapp side. Both
consumers of runPlannerLoop pass an isParallelSafe predicate derived
from the tool catalog:

  isParallelSafe: (name) =>
    AI_TOOL_CATALOG_BY_NAME.get(name)?.defaultPolicy === 'auto'

Auto-policy tools (list_tasks, get_habits, nutrition_summary, …) run
via Promise.all in batches of 10 when the LLM fans them out in one
round. Propose-policy tools — which surface to the user as Proposal
cards — stay sequential so intent ordering in the inbox is preserved
and pre-execute guardrails can reason about prior-step state.

Tests: 31 existing companion + mission tests pass unchanged; the
parallel path is exercised via the new loop.test.ts cases shipped
with the M1 commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-23 14:11:24 +02:00

68 lines
2.3 KiB
TypeScript

import type { BlockSpec } from './types';
import { heroBlockSpec } from './hero';
import { richTextBlockSpec } from './richText';
import { spacerBlockSpec } from './spacer';
/**
* The block registry — single source of truth for every block type the
* website builder knows about. Editor insert palette, renderer, inspector,
* schema validation, and future AI tools all consume this map.
*
* Adding a new block = create a folder under `src/{type}/`, export a
* `BlockSpec` from its index, and list it here.
*/
export const BLOCK_SPECS: readonly BlockSpec<unknown>[] = [
heroBlockSpec,
richTextBlockSpec,
spacerBlockSpec,
] as unknown as readonly BlockSpec<unknown>[];
const BY_TYPE: Record<string, BlockSpec<unknown>> = (() => {
const map: Record<string, BlockSpec<unknown>> = {};
for (const spec of BLOCK_SPECS) {
if (map[spec.type]) {
throw new Error(`[website-blocks] duplicate block type "${spec.type}"`);
}
map[spec.type] = spec as BlockSpec<unknown>;
}
return map;
})();
export function getBlockSpec(type: string): BlockSpec<unknown> | undefined {
return BY_TYPE[type];
}
export function requireBlockSpec(type: string): BlockSpec<unknown> {
const spec = BY_TYPE[type];
if (!spec) throw new Error(`[website-blocks] unknown block type "${type}"`);
return spec;
}
export function getAllBlockSpecs(): readonly BlockSpec<unknown>[] {
return BLOCK_SPECS;
}
/**
* Validate props against a block type's schema. Returns the parsed props
* (with defaults applied) on success, or throws with the Zod error.
*/
export function validateBlockProps(type: string, props: unknown): unknown {
const spec = requireBlockSpec(type);
return spec.schema.parse(props);
}
/**
* Safe-validate: returns `{ success, data, error }` without throwing.
* Used at boundaries (submit endpoint, snapshot builder) where we want
* to collect all errors rather than fail on the first one.
*/
export function safeValidateBlockProps(
type: string,
props: unknown
): { success: true; data: unknown } | { success: false; error: unknown } {
const spec = getBlockSpec(type);
if (!spec) return { success: false, error: new Error(`Unknown block type "${type}"`) };
const parsed = spec.schema.safeParse(props);
if (parsed.success) return { success: true, data: parsed.data };
return { success: false, error: parsed.error };
}