Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
44cc4d3
initial step of refactoring builder API to be created by a factory + …
seefeldb Jun 6, 2025
8c4f7fa
deno.lock change, not sure this is right
seefeldb Jun 6, 2025
2a712d9
let's just go back to `any` here, this is wonky
seefeldb Jun 6, 2025
2ac3fe2
builder/interface.ts and builder/schema-to-ts.ts are now together ent…
seefeldb Jun 6, 2025
27de921
lots of types fixes for previous change
seefeldb Jun 6, 2025
d8837ba
more type fixes
seefeldb Jun 6, 2025
14b319b
remove this syntax for now
seefeldb Jun 6, 2025
82ca353
remove extraneous parameter for createBuilder
seefeldb Jun 6, 2025
9c614de
add back ability to set schema with cell/opaqueRef
seefeldb Jun 7, 2025
f2fe26a
fix more tests
seefeldb Jun 7, 2025
79ef937
export opaqueRef directly still, for internal use outside of recipes
seefeldb Jun 7, 2025
6074063
partially fix test
seefeldb Jun 7, 2025
c630bc1
import type with `type` annotation
seefeldb Jun 9, 2025
6f97b4d
interface is now truly just the public interface. users should only i…
seefeldb Jun 9, 2025
d6398e3
separated imports in other places
seefeldb Jun 9, 2025
4d9e789
fix misaligned exports
seefeldb Jun 9, 2025
f11897e
removed Mutable from interface
seefeldb Jun 9, 2025
255a1cb
change all recipes to import from /interface.
seefeldb Jun 9, 2025
70139fe
redo use of SchemaWithoutCell in the right places, this fixed the tests
seefeldb Jun 9, 2025
0206774
need Mutable<> after all, so duplicating here to keep interface.ts an…
seefeldb Jun 9, 2025
fe6edc7
recipe also needs SchemaWithoutCell
seefeldb Jun 9, 2025
65c14ca
and recipe needs that also for the recipe function!
seefeldb Jun 9, 2025
1300446
fix recipes
seefeldb Jun 9, 2025
2a226d6
move getRecipeEnvironment to public environment
seefeldb Jun 9, 2025
5c4f4b7
revert to original deno.lock
seefeldb Jun 9, 2025
2bb1e50
also exports constants via builder!
seefeldb Jun 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
interface is now truly just the public interface. users should only i…
…mport one or the other
  • Loading branch information
seefeldb committed Jun 9, 2025
commit 6f97b4d0c7e048dbce5b5d8eb8d5031ac066bf1b
19 changes: 7 additions & 12 deletions packages/builder/src/built-in.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { createNodeFactory, lift } from "./module.ts";
import type { JSONSchema, NodeFactory, Opaque, OpaqueRef } from "./types.ts";
import type {
Cell,
JSONSchema,
NodeFactory,
Opaque,
OpaqueRef,
} from "./types.ts";
import type { Schema } from "./schema-to-ts.ts";

// Cell interface for global declaration (avoiding circular dependency)
interface Cell<T = any> {
get(): T;
set(value: T): void;
send(value: T): void;
update(values: Partial<T>): void;
push(...value: T extends (infer U)[] ? U[] : never): void;
equals(other: Cell<any>): boolean;
key<K extends keyof T>(valueKey: K): Cell<T[K]>;
}

export interface BuiltInLLMParams {
messages?: string[];
model?: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/builder/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
Cell,
CreateCellFunction,
JSONSchema,
} from "./interface.ts";
} from "./types.ts";
import { opaqueRef, stream } from "./opaque-ref.ts";
import { getTopFrame, recipe } from "./recipe.ts";
import { byRef, compute, derive, handler, lift, render } from "./module.ts";
Expand Down
2 changes: 1 addition & 1 deletion packages/builder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Export the factory function
export { createBuilder } from "./factory.ts";
export type { BuilderFunctions, BuilderRuntime } from "./interface.ts";
export type { BuilderFunctions, BuilderRuntime } from "./types.ts";

// Internal functions and exports needed by other packages
export {
Expand Down
61 changes: 7 additions & 54 deletions packages/builder/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* Public interface for the builder package.
* This module exports only the types and functions that are part of the public API.
* Public interface for the builder package. This module exports only the types
* and functions that are part of the public API.
*
* Import these types via `types.ts` for internal code.
*
* Other packages should either import from `@commontools/builder` or
* `@commontools/builder/interface`, but not both.
*/

import type { Mutable } from "@commontools/utils/types";
Expand Down Expand Up @@ -343,36 +348,6 @@ export type CellFunction = <T>(value?: T, schema?: JSONSchema) => OpaqueRef<T>;
export type StreamFunction = <T>(initial?: T) => OpaqueRef<T>;
export type ByRefFunction = <T, R>(ref: string) => ModuleFactory<T, R>;

// Builder functions interface
export interface BuilderFunctions {
// Recipe creation
recipe: RecipeFunction;

// Module creation
lift: LiftFunction;
handler: HandlerFunction;
derive: DeriveFunction;
compute: ComputeFunction;
render: RenderFunction;

// Built-in modules
str: StrFunction;
ifElse: IfElseFunction;
llm: LLMFunction;
fetchData: FetchDataFunction;
streamData: StreamDataFunction;
compileAndRun: CompileAndRunFunction;
navigateTo: NavigateToFunction;

// Cell creation
createCell: CreateCellFunction;
cell: CellFunction;
stream: StreamFunction;

// Utility
byRef: ByRefFunction;
}

// Re-export all function types as values for destructuring imports
// These will be implemented by the factory
export declare const recipe: RecipeFunction;
Expand All @@ -393,25 +368,3 @@ export declare const createCell: CreateCellFunction;
export declare const cell: CellFunction;
export declare const stream: StreamFunction;
export declare const byRef: ByRefFunction;

// Runtime interface needed by createCell
export interface BuilderRuntime {
getCell<T>(
space: string,
cause: any,
schema?: JSONSchema,
log?: any,
): Cell<T>;
getCell<S extends JSONSchema = JSONSchema>(
space: string,
cause: any,
schema: S,
log?: any,
): Cell<Schema<S>>;
}

// Factory function to create builder with runtime
export type CreateBuilder = (
runtime: BuilderRuntime,
getCellLinkOrThrow?: (value: any) => any,
) => BuilderFunctions;
77 changes: 77 additions & 0 deletions packages/builder/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
import { isObject } from "@commontools/utils/types";

import type {
ByRefFunction,
Cell,
CellFunction,
CompileAndRunFunction,
ComputeFunction,
CreateCellFunction,
DeriveFunction,
FetchDataFunction,
HandlerFunction,
IfElseFunction,
JSONSchema,
JSONValue,
LiftFunction,
LLMFunction,
Module,
NavigateToFunction,
Opaque,
OpaqueRef,
Recipe,
RecipeFunction,
RenderFunction,
Schema,
StreamDataFunction,
StreamFunction,
StrFunction,
} from "./interface.ts";

export { AuthSchema, ID, ID_FIELD, NAME, TYPE, UI } from "./interface.ts";
export type {
Cell,
CreateCellFunction,
Handler,
HandlerFactory,
JSONObject,
Expand All @@ -27,6 +48,10 @@ export type {
toJSON,
} from "./interface.ts";

// Augment the public interface with the internal OpaqueRefMethods interface.
// Deliberately repeating the original interface to catch any inconsistencies:
// This here then reflects the entire interface the internal implementation
// implements.
declare module "./interface.ts" {
interface OpaqueRefMethods<T> {
get(): OpaqueRef<T>;
Expand Down Expand Up @@ -219,3 +244,55 @@ export function markAsStatic(value: unknown): unknown {
(value as any)[isStaticMarker] = true;
return value;
}

// Builder functions interface
export interface BuilderFunctions {
// Recipe creation
recipe: RecipeFunction;

// Module creation
lift: LiftFunction;
handler: HandlerFunction;
derive: DeriveFunction;
compute: ComputeFunction;
render: RenderFunction;

// Built-in modules
str: StrFunction;
ifElse: IfElseFunction;
llm: LLMFunction;
fetchData: FetchDataFunction;
streamData: StreamDataFunction;
compileAndRun: CompileAndRunFunction;
navigateTo: NavigateToFunction;

// Cell creation
createCell: CreateCellFunction;
cell: CellFunction;
stream: StreamFunction;

// Utility
byRef: ByRefFunction;
}

// Runtime interface needed by createCell
export interface BuilderRuntime {
getCell<T>(
space: string,
cause: any,
schema?: JSONSchema,
log?: any,
): Cell<T>;
getCell<S extends JSONSchema = JSONSchema>(
space: string,
cause: any,
schema: S,
log?: any,
): Cell<Schema<S>>;
}

// Factory function to create builder with runtime
export type CreateBuilder = (
runtime: BuilderRuntime,
getCellLinkOrThrow?: (value: any) => any,
) => BuilderFunctions;