Restructure tree by role; rename mizan-runtime → mizan-base
packages/ flattens into: backends/ server protocol adapters (mizan-django, mizan-ts) frontends/ client kernel + framework adapters (mizan-base, mizan-react, mizan-vue, mizan-svelte) workers/ runtime workers (mizan-ssr) cores/ shared language-level primitives (empty for now; mizan-python forthcoming) The frontend kernel (was packages/mizan-runtime, now frontends/mizan-base) is renamed to reflect its role — it's the shared base that frontend adapters depend on directly. Reflects the substrate position that per-framework adapters wrap a single shared kernel; codegen targets the adapter, not the raw kernel. Path updates landed in: Makefile, two Gitea workflows, Dockerfile.test, four example/harness config files, .claude/settings.local.json, four docs (CLAUDE/ISSUES/ROADMAP/AFI_ARCHITECTURE), four codegen templates (stage1 + react/vue/svelte adapters), and three package.jsons (the mizan-base rename plus mizan-vue/svelte peerDeps). Generated files under examples/django-react-site/harness/src/api/ still reference @mizan/runtime — left as-is; they're regenerated artifacts and the harness is non-functional pending the React wrapper-layer codegen. Also folded in a pre-existing fix: the Gitea workflows had working-directory: react / django pointing at a layout that predates packages/, never updated. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
180
backends/mizan-django/generate/generator/lib/adapters/react.mjs
Normal file
180
backends/mizan-django/generate/generator/lib/adapters/react.mjs
Normal file
@@ -0,0 +1,180 @@
|
||||
/**
|
||||
* React Stage 2 — Generates hooks + context providers from Stage 1 output.
|
||||
*
|
||||
* Generated providers subscribe to the runtime kernel for state.
|
||||
* The kernel owns data, status, and error. React just renders.
|
||||
*/
|
||||
|
||||
function pascalCase(str) {
|
||||
return str.split(/[.\-_]/).map(p => p.charAt(0).toUpperCase() + p.slice(1)).join('')
|
||||
}
|
||||
|
||||
export function generateReactAdapter(schema) {
|
||||
const functions = schema['x-mizan-functions'] || []
|
||||
const contextGroups = schema['x-mizan-contexts'] || {}
|
||||
const namedContexts = Object.entries(contextGroups).filter(([n]) => n !== 'global')
|
||||
const globalContexts = functions.filter(fn => fn.isContext === 'global')
|
||||
const mutations = functions.filter(fn => !fn.isContext && !fn.isForm && fn.affects)
|
||||
const plainFns = functions.filter(fn => !fn.isContext && !fn.isForm && !fn.affects)
|
||||
|
||||
const lines = [
|
||||
"'use client'",
|
||||
'',
|
||||
'// AUTO-GENERATED by mizan — do not edit',
|
||||
'',
|
||||
"import { createContext, useContext, useState, useEffect, useCallback, useRef, useSyncExternalStore, type ReactNode } from 'react'",
|
||||
"import { registerContext, mizanFetch, mizanCall, type ContextState } from '@mizan/runtime'",
|
||||
'',
|
||||
]
|
||||
|
||||
// Import from Stage 1
|
||||
const stage1Imports = []
|
||||
for (const [ctxName] of Object.entries(contextGroups)) {
|
||||
const p = pascalCase(ctxName)
|
||||
stage1Imports.push(`fetch${p}Context`, `type ${p}ContextData`, `type ${p}ContextParams`)
|
||||
}
|
||||
for (const fn of [...mutations, ...plainFns]) {
|
||||
stage1Imports.push(`call${pascalCase(fn.camelName)}`)
|
||||
}
|
||||
if (stage1Imports.length > 0) {
|
||||
lines.push(`import { ${stage1Imports.join(', ')} } from '../index'`)
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
// ── Helper hook: subscribe to kernel state ──────────────────────────
|
||||
|
||||
lines.push('// Subscribe to kernel state via useSyncExternalStore')
|
||||
lines.push('function useContextState<T>(')
|
||||
lines.push(' name: string,')
|
||||
lines.push(' params: Record<string, any>,')
|
||||
lines.push(' fetchFn: () => Promise<T>,')
|
||||
lines.push(' initialData?: T,')
|
||||
lines.push('): ContextState<T> {')
|
||||
lines.push(' const ref = useRef<ReturnType<typeof registerContext> | null>(null)')
|
||||
lines.push('')
|
||||
lines.push(' if (!ref.current) {')
|
||||
lines.push(' ref.current = registerContext(name, params, fetchFn, initialData)')
|
||||
lines.push(' }')
|
||||
lines.push('')
|
||||
lines.push(' const handle = ref.current')
|
||||
lines.push('')
|
||||
lines.push(' // Fetch on mount if no data')
|
||||
lines.push(' useEffect(() => {')
|
||||
lines.push(" if (handle.getState().status === 'idle') handle.refetch()")
|
||||
lines.push(' return () => handle.unregister()')
|
||||
lines.push(' }, [handle])')
|
||||
lines.push('')
|
||||
lines.push(' return useSyncExternalStore(')
|
||||
lines.push(' handle.subscribe,')
|
||||
lines.push(' handle.getState,')
|
||||
lines.push(' handle.getState,')
|
||||
lines.push(' )')
|
||||
lines.push('}')
|
||||
lines.push('')
|
||||
|
||||
// ── Mutation hook helper ────────────────────────────────────────────
|
||||
|
||||
lines.push('// Mutation hook with loading/error state')
|
||||
lines.push('function useMutation<TArgs, TResult>(')
|
||||
lines.push(' callFn: (args: TArgs) => Promise<TResult>,')
|
||||
lines.push('): { mutate: (args: TArgs) => Promise<TResult>; isPending: boolean; error: Error | null } {')
|
||||
lines.push(' const [isPending, setIsPending] = useState(false)')
|
||||
lines.push(' const [error, setError] = useState<Error | null>(null)')
|
||||
lines.push('')
|
||||
lines.push(' const mutate = useCallback(async (args: TArgs) => {')
|
||||
lines.push(' setIsPending(true)')
|
||||
lines.push(' setError(null)')
|
||||
lines.push(' try {')
|
||||
lines.push(' const result = await callFn(args)')
|
||||
lines.push(' return result')
|
||||
lines.push(' } catch (e) {')
|
||||
lines.push(' setError(e as Error)')
|
||||
lines.push(' throw e')
|
||||
lines.push(' } finally {')
|
||||
lines.push(' setIsPending(false)')
|
||||
lines.push(' }')
|
||||
lines.push(' }, [callFn])')
|
||||
lines.push('')
|
||||
lines.push(' return { mutate, isPending, error }')
|
||||
lines.push('}')
|
||||
lines.push('')
|
||||
|
||||
// ── Context hooks ───────────────────────────────────────────────────
|
||||
|
||||
for (const [ctxName, ctxMeta] of Object.entries(contextGroups)) {
|
||||
const p = pascalCase(ctxName)
|
||||
const ctxFunctions = functions.filter(fn => fn.isContext === ctxName)
|
||||
const paramEntries = Object.entries(ctxMeta.params || {})
|
||||
|
||||
lines.push(`// ── ${p} Context ──`)
|
||||
lines.push('')
|
||||
|
||||
// Hook that returns the full kernel state
|
||||
if (paramEntries.length > 0) {
|
||||
lines.push(`export function use${p}Context(params: ${p}ContextParams): ContextState<${p}ContextData> {`)
|
||||
lines.push(` const ssrData = typeof window !== 'undefined' ? (window as any).__MIZAN_SSR_DATA__ : null`)
|
||||
lines.push(` return useContextState('${ctxName}', params, () => fetch${p}Context(params), ssrData)`)
|
||||
} else {
|
||||
lines.push(`export function use${p}Context(): ContextState<${p}ContextData> {`)
|
||||
lines.push(` const ssrData = typeof window !== 'undefined' ? (window as any).__MIZAN_SSR_DATA__ : null`)
|
||||
lines.push(` return useContextState('${ctxName}', {}, () => fetch${p}Context({} as any), ssrData)`)
|
||||
}
|
||||
lines.push('}')
|
||||
lines.push('')
|
||||
|
||||
// Convenience hooks for individual data fields
|
||||
for (const fn of ctxFunctions) {
|
||||
const hookPascal = pascalCase(fn.camelName)
|
||||
if (paramEntries.length > 0) {
|
||||
lines.push(`export function use${hookPascal}(params: ${p}ContextParams): ${fn.outputType} | null {`)
|
||||
lines.push(` const state = use${p}Context(params)`)
|
||||
} else {
|
||||
lines.push(`export function use${hookPascal}(): ${fn.outputType} | null {`)
|
||||
lines.push(` const state = use${p}Context()`)
|
||||
}
|
||||
lines.push(` return state.data?.${fn.name} ?? null`)
|
||||
lines.push('}')
|
||||
lines.push('')
|
||||
}
|
||||
}
|
||||
|
||||
// ── Mutation hooks (with loading/error) ──────────────────────────────
|
||||
|
||||
for (const fn of mutations) {
|
||||
const p = pascalCase(fn.camelName)
|
||||
if (fn.hasInput) {
|
||||
lines.push(`export function use${p}() {`)
|
||||
lines.push(` return useMutation<Parameters<typeof call${p}>[0], Awaited<ReturnType<typeof call${p}>>>(call${p})`)
|
||||
lines.push('}')
|
||||
} else {
|
||||
lines.push(`export function use${p}() {`)
|
||||
lines.push(` return useMutation<void, Awaited<ReturnType<typeof call${p}>>>(() => call${p}() as any)`)
|
||||
lines.push('}')
|
||||
}
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
// ── Plain function hooks ────────────────────────────────────────────
|
||||
|
||||
for (const fn of plainFns) {
|
||||
const p = pascalCase(fn.camelName)
|
||||
if (fn.hasInput) {
|
||||
lines.push(`export function use${p}() {`)
|
||||
lines.push(` return useMutation<Parameters<typeof call${p}>[0], Awaited<ReturnType<typeof call${p}>>>(call${p})`)
|
||||
lines.push('}')
|
||||
} else {
|
||||
lines.push(`export function use${p}() {`)
|
||||
lines.push(` return useMutation<void, Awaited<ReturnType<typeof call${p}>>>(() => call${p}() as any)`)
|
||||
lines.push('}')
|
||||
}
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
// ── Re-export runtime types ─────────────────────────────────────────
|
||||
|
||||
lines.push("export type { ContextState } from '@mizan/runtime'")
|
||||
lines.push("export { configure, initSession, MizanError } from '@mizan/runtime'")
|
||||
lines.push('')
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Svelte Stage 2 — Generates stores from Stage 1 output.
|
||||
*
|
||||
* Subscribes to the kernel for state. Returns readable stores.
|
||||
*/
|
||||
|
||||
function pascalCase(str) {
|
||||
return str.split(/[.\-_]/).map(p => p.charAt(0).toUpperCase() + p.slice(1)).join('')
|
||||
}
|
||||
|
||||
export function generateSvelteAdapter(schema) {
|
||||
const functions = schema['x-mizan-functions'] || []
|
||||
const contextGroups = schema['x-mizan-contexts'] || {}
|
||||
const mutations = functions.filter(fn => !fn.isContext && !fn.isForm && fn.affects)
|
||||
const plainFns = functions.filter(fn => !fn.isContext && !fn.isForm && !fn.affects)
|
||||
|
||||
const lines = [
|
||||
'// AUTO-GENERATED by mizan — do not edit',
|
||||
'',
|
||||
"import { readable, type Readable } from 'svelte/store'",
|
||||
"import { registerContext, type ContextState } from '@mizan/runtime'",
|
||||
'',
|
||||
]
|
||||
|
||||
const stage1Imports = []
|
||||
for (const [ctxName] of Object.entries(contextGroups)) {
|
||||
const p = pascalCase(ctxName)
|
||||
stage1Imports.push(`fetch${p}Context`, `type ${p}ContextData`, `type ${p}ContextParams`)
|
||||
}
|
||||
for (const fn of [...mutations, ...plainFns]) {
|
||||
stage1Imports.push(`call${pascalCase(fn.camelName)}`)
|
||||
}
|
||||
if (stage1Imports.length > 0) {
|
||||
lines.push(`import { ${stage1Imports.join(', ')} } from '../index'`)
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
for (const [ctxName, ctxMeta] of Object.entries(contextGroups)) {
|
||||
const p = pascalCase(ctxName)
|
||||
const ctxFunctions = functions.filter(fn => fn.isContext === ctxName)
|
||||
const paramEntries = Object.entries(ctxMeta.params || {})
|
||||
const paramsArg = paramEntries.length > 0 ? 'params' : '{} as any'
|
||||
|
||||
if (paramEntries.length > 0) {
|
||||
lines.push(`export function create${p}Context(params: ${p}ContextParams) {`)
|
||||
} else {
|
||||
lines.push(`export function create${p}Context() {`)
|
||||
}
|
||||
|
||||
// Use readable store backed by kernel subscription
|
||||
lines.push(` const store = readable<ContextState<${p}ContextData>>(`)
|
||||
lines.push(` { data: null, status: 'idle', error: null },`)
|
||||
lines.push(` (set) => {`)
|
||||
lines.push(` const handle = registerContext('${ctxName}', ${paramsArg}, () => fetch${p}Context(${paramsArg}))`)
|
||||
lines.push(` const unsub = handle.subscribe(() => set(handle.getState()))`)
|
||||
lines.push(` handle.refetch()`)
|
||||
lines.push(` return () => { unsub(); handle.unregister() }`)
|
||||
lines.push(` },`)
|
||||
lines.push(` )`)
|
||||
lines.push('')
|
||||
lines.push(` return store`)
|
||||
lines.push('}')
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
// Re-export mutations as-is from Stage 1
|
||||
for (const fn of [...mutations, ...plainFns]) {
|
||||
const p = pascalCase(fn.camelName)
|
||||
lines.push(`export { call${p} } from '../index'`)
|
||||
}
|
||||
lines.push('')
|
||||
|
||||
lines.push("export type { ContextState } from '@mizan/runtime'")
|
||||
lines.push("export { configure, initSession, MizanError } from '@mizan/runtime'")
|
||||
lines.push('')
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
104
backends/mizan-django/generate/generator/lib/adapters/vue.mjs
Normal file
104
backends/mizan-django/generate/generator/lib/adapters/vue.mjs
Normal file
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Vue Stage 2 — Generates composables from Stage 1 output.
|
||||
*
|
||||
* Subscribes to the kernel for state. Vue reactivity wraps kernel notifications.
|
||||
*/
|
||||
|
||||
function pascalCase(str) {
|
||||
return str.split(/[.\-_]/).map(p => p.charAt(0).toUpperCase() + p.slice(1)).join('')
|
||||
}
|
||||
|
||||
export function generateVueAdapter(schema) {
|
||||
const functions = schema['x-mizan-functions'] || []
|
||||
const contextGroups = schema['x-mizan-contexts'] || {}
|
||||
const mutations = functions.filter(fn => !fn.isContext && !fn.isForm && fn.affects)
|
||||
const plainFns = functions.filter(fn => !fn.isContext && !fn.isForm && !fn.affects)
|
||||
|
||||
const lines = [
|
||||
'// AUTO-GENERATED by mizan — do not edit',
|
||||
'',
|
||||
"import { ref, computed, onMounted, onUnmounted, onServerPrefetch, type ComputedRef } from 'vue'",
|
||||
"import { registerContext, type ContextState } from '@mizan/runtime'",
|
||||
'',
|
||||
]
|
||||
|
||||
const stage1Imports = []
|
||||
for (const [ctxName] of Object.entries(contextGroups)) {
|
||||
const p = pascalCase(ctxName)
|
||||
stage1Imports.push(`fetch${p}Context`, `type ${p}ContextData`, `type ${p}ContextParams`)
|
||||
}
|
||||
for (const fn of [...mutations, ...plainFns]) {
|
||||
stage1Imports.push(`call${pascalCase(fn.camelName)}`)
|
||||
}
|
||||
if (stage1Imports.length > 0) {
|
||||
lines.push(`import { ${stage1Imports.join(', ')} } from '../index'`)
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
for (const [ctxName, ctxMeta] of Object.entries(contextGroups)) {
|
||||
const p = pascalCase(ctxName)
|
||||
const ctxFunctions = functions.filter(fn => fn.isContext === ctxName)
|
||||
const paramEntries = Object.entries(ctxMeta.params || {})
|
||||
const paramsArg = paramEntries.length > 0 ? 'params' : '{} as any'
|
||||
|
||||
if (paramEntries.length > 0) {
|
||||
lines.push(`export function use${p}Context(params: ${p}ContextParams) {`)
|
||||
} else {
|
||||
lines.push(`export function use${p}Context() {`)
|
||||
}
|
||||
|
||||
lines.push(` const state = ref<ContextState<${p}ContextData>>({ data: null, status: 'idle', error: null })`)
|
||||
lines.push(` let handle: ReturnType<typeof registerContext> | null = null`)
|
||||
lines.push('')
|
||||
lines.push(` onMounted(() => {`)
|
||||
lines.push(` handle = registerContext('${ctxName}', ${paramsArg}, () => fetch${p}Context(${paramsArg}))`)
|
||||
lines.push(` handle.subscribe(() => { state.value = handle!.getState() })`)
|
||||
lines.push(` handle.refetch()`)
|
||||
lines.push(` })`)
|
||||
lines.push('')
|
||||
lines.push(` onServerPrefetch(async () => {`)
|
||||
lines.push(` handle = registerContext('${ctxName}', ${paramsArg}, () => fetch${p}Context(${paramsArg}))`)
|
||||
lines.push(` await handle.refetch()`)
|
||||
lines.push(` state.value = handle.getState()`)
|
||||
lines.push(` })`)
|
||||
lines.push('')
|
||||
lines.push(` onUnmounted(() => { handle?.unregister() })`)
|
||||
lines.push('')
|
||||
lines.push(` return {`)
|
||||
lines.push(` state,`)
|
||||
for (const fn of ctxFunctions) {
|
||||
lines.push(` ${fn.camelName}: computed(() => state.value.data?.${fn.name} ?? null) as ComputedRef<${fn.outputType} | null>,`)
|
||||
}
|
||||
lines.push(` loading: computed(() => state.value.status === 'loading'),`)
|
||||
lines.push(` error: computed(() => state.value.error),`)
|
||||
lines.push(` }`)
|
||||
lines.push('}')
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
for (const fn of [...mutations, ...plainFns]) {
|
||||
const p = pascalCase(fn.camelName)
|
||||
lines.push(`export function use${p}() {`)
|
||||
lines.push(` const isPending = ref(false)`)
|
||||
lines.push(` const error = ref<Error | null>(null)`)
|
||||
if (fn.hasInput) {
|
||||
lines.push(` async function mutate(args: Parameters<typeof call${p}>[0]) {`)
|
||||
} else {
|
||||
lines.push(` async function mutate() {`)
|
||||
}
|
||||
lines.push(` isPending.value = true; error.value = null`)
|
||||
lines.push(` try { return await call${p}(${fn.hasInput ? 'args' : ''}) }`)
|
||||
lines.push(` catch (e) { error.value = e as Error; throw e }`)
|
||||
lines.push(` finally { isPending.value = false }`)
|
||||
lines.push(` }`)
|
||||
lines.push(` return { mutate, isPending, error }`)
|
||||
lines.push('}')
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
lines.push("export type { ContextState } from '@mizan/runtime'")
|
||||
lines.push("export { configure, initSession, MizanError } from '@mizan/runtime'")
|
||||
lines.push('')
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
Reference in New Issue
Block a user