|
| 1 | +/** |
| 2 | + * Factory function to create builder functions with runtime dependency injection |
| 3 | + */ |
| 4 | + |
| 5 | +import type { |
| 6 | + BuilderFunctionsAndConstants, |
| 7 | + Cell, |
| 8 | + CreateCellFunction, |
| 9 | + JSONSchema, |
| 10 | +} from "./types.ts"; |
| 11 | +import { AuthSchema, ID, ID_FIELD, NAME, schema, TYPE, UI } from "./types.ts"; |
| 12 | +import { opaqueRef, stream } from "./opaque-ref.ts"; |
| 13 | +import { getTopFrame, recipe } from "./recipe.ts"; |
| 14 | +import { byRef, compute, derive, handler, lift, render } from "./module.ts"; |
| 15 | +import { |
| 16 | + compileAndRun, |
| 17 | + fetchData, |
| 18 | + ifElse, |
| 19 | + llm, |
| 20 | + navigateTo, |
| 21 | + str, |
| 22 | + streamData, |
| 23 | +} from "./built-in.ts"; |
| 24 | +import { getCellLinkOrThrow, type Runtime } from "@commontools/runner"; |
| 25 | +import { getRecipeEnvironment } from "./env.ts"; |
| 26 | + |
| 27 | +/** |
| 28 | + * Creates a set of builder functions with the given runtime |
| 29 | + * @param runtime - The runtime instance to use for cell creation |
| 30 | + * @returns An object containing all builder functions |
| 31 | + */ |
| 32 | +export const createBuilder = ( |
| 33 | + runtime: Runtime, |
| 34 | +): BuilderFunctionsAndConstants => { |
| 35 | + // Implementation of createCell moved from runner/harness |
| 36 | + const createCell: CreateCellFunction = function createCell<T = any>( |
| 37 | + schema?: JSONSchema, |
| 38 | + name?: string, |
| 39 | + value?: T, |
| 40 | + ): Cell<T> { |
| 41 | + const frame = getTopFrame(); |
| 42 | + // This is a rather hacky way to get the context, based on the |
| 43 | + // unsafe_binding pattern. Once we replace that mechanism, let's add nicer |
| 44 | + // abstractions for context here as well. |
| 45 | + const cellLink = frame?.unsafe_binding?.materialize([]); |
| 46 | + if (!frame || !frame.cause || !cellLink) { |
| 47 | + throw new Error( |
| 48 | + "Can't invoke createCell outside of a lifted function or handler", |
| 49 | + ); |
| 50 | + } |
| 51 | + if (!getCellLinkOrThrow) { |
| 52 | + throw new Error( |
| 53 | + "getCellLinkOrThrow function not provided to createBuilder", |
| 54 | + ); |
| 55 | + } |
| 56 | + const space = getCellLinkOrThrow(cellLink).cell.space; |
| 57 | + |
| 58 | + const cause = { parent: frame.cause } as Record<string, any>; |
| 59 | + if (name) cause.name = name; |
| 60 | + else cause.number = frame.generatedIdCounter++; |
| 61 | + |
| 62 | + // Cast to Cell<T> is necessary to cast to interface-only Cell type |
| 63 | + const cell = runtime.getCell<T>(space, cause, schema) as Cell<T>; |
| 64 | + |
| 65 | + if (value !== undefined) cell.set(value); |
| 66 | + |
| 67 | + return cell; |
| 68 | + } as CreateCellFunction; |
| 69 | + |
| 70 | + return { |
| 71 | + // Recipe creation |
| 72 | + recipe, |
| 73 | + |
| 74 | + // Module creation |
| 75 | + lift, |
| 76 | + handler, |
| 77 | + derive, |
| 78 | + compute, |
| 79 | + render, |
| 80 | + |
| 81 | + // Built-in modules |
| 82 | + str, |
| 83 | + ifElse, |
| 84 | + llm, |
| 85 | + fetchData, |
| 86 | + streamData, |
| 87 | + compileAndRun, |
| 88 | + navigateTo, |
| 89 | + |
| 90 | + // Cell creation |
| 91 | + createCell, |
| 92 | + cell: opaqueRef, |
| 93 | + stream, |
| 94 | + |
| 95 | + // Utility |
| 96 | + byRef, |
| 97 | + |
| 98 | + // Environment |
| 99 | + getRecipeEnvironment, |
| 100 | + |
| 101 | + // Constants |
| 102 | + ID, |
| 103 | + ID_FIELD, |
| 104 | + TYPE, |
| 105 | + NAME, |
| 106 | + UI, |
| 107 | + |
| 108 | + // Schema utilities |
| 109 | + schema, |
| 110 | + AuthSchema, |
| 111 | + }; |
| 112 | +}; |
0 commit comments