@genart-dev/core 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { ParamDef, ColorDef, RendererType, SketchState, CanvasSpec, SketchDefinition } from '@genart-dev/format';
2
2
  export * from '@genart-dev/format';
3
+ import { ResolvedComponent } from '@genart-dev/components';
4
+ export { COMPONENT_REGISTRY, ComponentCategory, ComponentEntry, RendererTarget, ResolvedComponent, resolveComponents } from '@genart-dev/components';
3
5
 
4
6
  /** A design knowledge skill definition. */
5
7
  interface SkillDefinition {
@@ -75,8 +77,9 @@ interface RendererAdapter {
75
77
  readonly algorithmLanguage: "javascript" | "glsl" | "typescript";
76
78
  /** Validate algorithm source without executing it. */
77
79
  validate(algorithm: string): ValidationResult;
78
- /** Compile an algorithm string into a runnable form. */
79
- compile(algorithm: string): Promise<CompiledAlgorithm>;
80
+ /** Compile an algorithm string into a runnable form.
81
+ * @param components — Pre-resolved component source to prepend (from resolveComponents). */
82
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
80
83
  /** Create a live sketch instance from compiled algorithm + state. */
81
84
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
82
85
  /** Render a single frame offscreen (for capture/export). */
@@ -126,7 +129,7 @@ declare class P5RendererAdapter implements RendererAdapter {
126
129
  readonly displayName = "p5.js";
127
130
  readonly algorithmLanguage: "javascript";
128
131
  validate(algorithm: string): ValidationResult;
129
- compile(algorithm: string): Promise<CompiledAlgorithm>;
132
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
130
133
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
131
134
  renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
132
135
  generateStandaloneHTML(sketch: SketchDefinition): string;
@@ -145,7 +148,7 @@ declare class Canvas2DRendererAdapter implements RendererAdapter {
145
148
  readonly displayName = "Canvas 2D";
146
149
  readonly algorithmLanguage: "javascript";
147
150
  validate(algorithm: string): ValidationResult;
148
- compile(algorithm: string): Promise<CompiledAlgorithm>;
151
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
149
152
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
150
153
  renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
151
154
  generateStandaloneHTML(sketch: SketchDefinition): string;
@@ -164,7 +167,7 @@ declare class ThreeRendererAdapter implements RendererAdapter {
164
167
  readonly displayName = "Three.js";
165
168
  readonly algorithmLanguage: "javascript";
166
169
  validate(algorithm: string): ValidationResult;
167
- compile(algorithm: string): Promise<CompiledAlgorithm>;
170
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
168
171
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
169
172
  renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
170
173
  generateStandaloneHTML(sketch: SketchDefinition): string;
@@ -188,7 +191,7 @@ declare class GLSLRendererAdapter implements RendererAdapter {
188
191
  readonly displayName = "GLSL Shader";
189
192
  readonly algorithmLanguage: "glsl";
190
193
  validate(algorithm: string): ValidationResult;
191
- compile(algorithm: string): Promise<CompiledAlgorithm>;
194
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
192
195
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
193
196
  renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
194
197
  generateStandaloneHTML(sketch: SketchDefinition): string;
@@ -208,7 +211,7 @@ declare class SVGRendererAdapter implements RendererAdapter {
208
211
  readonly displayName = "SVG";
209
212
  readonly algorithmLanguage: "javascript";
210
213
  validate(algorithm: string): ValidationResult;
211
- compile(algorithm: string): Promise<CompiledAlgorithm>;
214
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
212
215
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
213
216
  renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, _options?: CaptureOptions): Promise<Uint8Array | Blob>;
214
217
  generateStandaloneHTML(sketch: SketchDefinition): string;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { ParamDef, ColorDef, RendererType, SketchState, CanvasSpec, SketchDefinition } from '@genart-dev/format';
2
2
  export * from '@genart-dev/format';
3
+ import { ResolvedComponent } from '@genart-dev/components';
4
+ export { COMPONENT_REGISTRY, ComponentCategory, ComponentEntry, RendererTarget, ResolvedComponent, resolveComponents } from '@genart-dev/components';
3
5
 
4
6
  /** A design knowledge skill definition. */
5
7
  interface SkillDefinition {
@@ -75,8 +77,9 @@ interface RendererAdapter {
75
77
  readonly algorithmLanguage: "javascript" | "glsl" | "typescript";
76
78
  /** Validate algorithm source without executing it. */
77
79
  validate(algorithm: string): ValidationResult;
78
- /** Compile an algorithm string into a runnable form. */
79
- compile(algorithm: string): Promise<CompiledAlgorithm>;
80
+ /** Compile an algorithm string into a runnable form.
81
+ * @param components — Pre-resolved component source to prepend (from resolveComponents). */
82
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
80
83
  /** Create a live sketch instance from compiled algorithm + state. */
81
84
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
82
85
  /** Render a single frame offscreen (for capture/export). */
@@ -126,7 +129,7 @@ declare class P5RendererAdapter implements RendererAdapter {
126
129
  readonly displayName = "p5.js";
127
130
  readonly algorithmLanguage: "javascript";
128
131
  validate(algorithm: string): ValidationResult;
129
- compile(algorithm: string): Promise<CompiledAlgorithm>;
132
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
130
133
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
131
134
  renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
132
135
  generateStandaloneHTML(sketch: SketchDefinition): string;
@@ -145,7 +148,7 @@ declare class Canvas2DRendererAdapter implements RendererAdapter {
145
148
  readonly displayName = "Canvas 2D";
146
149
  readonly algorithmLanguage: "javascript";
147
150
  validate(algorithm: string): ValidationResult;
148
- compile(algorithm: string): Promise<CompiledAlgorithm>;
151
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
149
152
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
150
153
  renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
151
154
  generateStandaloneHTML(sketch: SketchDefinition): string;
@@ -164,7 +167,7 @@ declare class ThreeRendererAdapter implements RendererAdapter {
164
167
  readonly displayName = "Three.js";
165
168
  readonly algorithmLanguage: "javascript";
166
169
  validate(algorithm: string): ValidationResult;
167
- compile(algorithm: string): Promise<CompiledAlgorithm>;
170
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
168
171
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
169
172
  renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
170
173
  generateStandaloneHTML(sketch: SketchDefinition): string;
@@ -188,7 +191,7 @@ declare class GLSLRendererAdapter implements RendererAdapter {
188
191
  readonly displayName = "GLSL Shader";
189
192
  readonly algorithmLanguage: "glsl";
190
193
  validate(algorithm: string): ValidationResult;
191
- compile(algorithm: string): Promise<CompiledAlgorithm>;
194
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
192
195
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
193
196
  renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
194
197
  generateStandaloneHTML(sketch: SketchDefinition): string;
@@ -208,7 +211,7 @@ declare class SVGRendererAdapter implements RendererAdapter {
208
211
  readonly displayName = "SVG";
209
212
  readonly algorithmLanguage: "javascript";
210
213
  validate(algorithm: string): ValidationResult;
211
- compile(algorithm: string): Promise<CompiledAlgorithm>;
214
+ compile(algorithm: string, components?: ResolvedComponent[]): Promise<CompiledAlgorithm>;
212
215
  createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
213
216
  renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, _options?: CaptureOptions): Promise<Uint8Array | Blob>;
214
217
  generateStandaloneHTML(sketch: SketchDefinition): string;
package/dist/index.js CHANGED
@@ -1,5 +1,24 @@
1
1
  // src/index.ts
2
2
  export * from "@genart-dev/format";
3
+ import {
4
+ COMPONENT_REGISTRY,
5
+ resolveComponents
6
+ } from "@genart-dev/components";
7
+
8
+ // src/sketch/adapters/component-utils.ts
9
+ function extractComponentCode(components) {
10
+ if (!components) return "";
11
+ const blocks = [];
12
+ for (const [name, value] of Object.entries(components)) {
13
+ if (typeof value === "string") continue;
14
+ if (value.code) {
15
+ const ver = value.version ? ` v${value.version}` : "";
16
+ blocks.push(`// --- ${name}${ver} ---
17
+ ${value.code}`);
18
+ }
19
+ }
20
+ return blocks.join("\n\n");
21
+ }
3
22
 
4
23
  // src/sketch/adapters/p5.ts
5
24
  var P5_CDN_VERSION = "1.11.3";
@@ -25,15 +44,20 @@ var P5RendererAdapter = class {
25
44
  }
26
45
  return { valid: errors.length === 0, errors };
27
46
  }
28
- async compile(algorithm) {
47
+ async compile(algorithm, components) {
29
48
  const validation = this.validate(algorithm);
30
49
  if (!validation.valid) {
31
50
  throw new Error(
32
51
  `p5 compilation failed: ${validation.errors.join("; ")}`
33
52
  );
34
53
  }
54
+ const componentCode = components?.map(
55
+ (c) => `// --- ${c.name} v${c.version} ---
56
+ ${c.code}`
57
+ ).join("\n\n") ?? "";
35
58
  const wrappedSource = `
36
59
  return (function() {
60
+ ${componentCode}
37
61
  ${algorithm}
38
62
  return sketch;
39
63
  })();
@@ -187,6 +211,7 @@ var P5RendererAdapter = class {
187
211
  state.PARAMS = state.params;
188
212
  state.COLORS = ${colorsJson};
189
213
 
214
+ ${extractComponentCode(sketch.components)}
190
215
  ${sketch.algorithm}
191
216
 
192
217
  new p5(function(p) {
@@ -242,15 +267,20 @@ var Canvas2DRendererAdapter = class {
242
267
  }
243
268
  return { valid: errors.length === 0, errors };
244
269
  }
245
- async compile(algorithm) {
270
+ async compile(algorithm, components) {
246
271
  const validation = this.validate(algorithm);
247
272
  if (!validation.valid) {
248
273
  throw new Error(
249
274
  `Canvas 2D compilation failed: ${validation.errors.join("; ")}`
250
275
  );
251
276
  }
277
+ const componentCode = components?.map(
278
+ (c) => `// --- ${c.name} v${c.version} ---
279
+ ${c.code}`
280
+ ).join("\n\n") ?? "";
252
281
  const wrappedSource = `
253
282
  return (function() {
283
+ ${componentCode}
254
284
  ${algorithm}
255
285
  return sketch;
256
286
  })();
@@ -410,6 +440,7 @@ var Canvas2DRendererAdapter = class {
410
440
  const state = ${stateJson};
411
441
  state.canvas = { width: ${width}, height: ${height}, pixelDensity: ${pixelDensity} };
412
442
 
443
+ ${extractComponentCode(sketch.components)}
413
444
  ${sketch.algorithm}
414
445
 
415
446
  const canvas = document.getElementById('canvas');
@@ -469,15 +500,20 @@ var ThreeRendererAdapter = class {
469
500
  }
470
501
  return { valid: true, errors: [] };
471
502
  }
472
- async compile(algorithm) {
503
+ async compile(algorithm, components) {
473
504
  const validation = this.validate(algorithm);
474
505
  if (!validation.valid) {
475
506
  throw new Error(
476
507
  `Three.js compilation failed: ${validation.errors.join("; ")}`
477
508
  );
478
509
  }
510
+ const componentCode = components?.map(
511
+ (c) => `// --- ${c.name} v${c.version} ---
512
+ ${c.code}`
513
+ ).join("\n\n") ?? "";
479
514
  const wrappedSource = `
480
515
  return (function() {
516
+ ${componentCode}
481
517
  ${algorithm}
482
518
  return sketch;
483
519
  })();
@@ -620,6 +656,7 @@ var ThreeRendererAdapter = class {
620
656
  const state = ${stateJson};
621
657
  state.canvas = { width: ${width}, height: ${height}, pixelDensity: ${pixelDensity} };
622
658
 
659
+ ${extractComponentCode(sketch.components)}
623
660
  ${sketch.algorithm}
624
661
 
625
662
  sketch(THREE, state, document.getElementById('canvas-container'));
@@ -724,6 +761,41 @@ function extractUniforms(source) {
724
761
  }
725
762
  return { params, colors };
726
763
  }
764
+ function injectGLSLSource(algorithm, codeToInject) {
765
+ if (!codeToInject) return algorithm;
766
+ const trimmed = algorithm.trimStart();
767
+ const versionMatch = trimmed.match(/^(#version\s+\S+[^\n]*\n)/);
768
+ const versionLine = versionMatch?.[1] ?? "";
769
+ let shaderBody = versionMatch ? trimmed.slice(versionLine.length) : trimmed;
770
+ const precisionLines = [];
771
+ let precisionMatch;
772
+ while ((precisionMatch = shaderBody.match(/^\s*(precision\s+\w+\s+\w+;\s*\n)/)) !== null) {
773
+ precisionLines.push(precisionMatch[1]);
774
+ shaderBody = shaderBody.slice(precisionMatch[0].length);
775
+ }
776
+ return versionLine + precisionLines.join("") + "\n" + codeToInject + "\n\n" + shaderBody;
777
+ }
778
+ function injectGLSLComponents(algorithm, components) {
779
+ if (!components || components.length === 0) return algorithm;
780
+ const componentCode = components.map(
781
+ (c) => `// --- ${c.name} v${c.version} ---
782
+ ${c.code}`
783
+ ).join("\n\n");
784
+ return injectGLSLSource(algorithm, componentCode);
785
+ }
786
+ function extractGLSLComponentCode(components) {
787
+ if (!components) return "";
788
+ const blocks = [];
789
+ for (const [name, value] of Object.entries(components)) {
790
+ if (typeof value === "string") continue;
791
+ if (value.code) {
792
+ const ver = value.version ? ` v${value.version}` : "";
793
+ blocks.push(`// --- ${name}${ver} ---
794
+ ${value.code}`);
795
+ }
796
+ }
797
+ return blocks.join("\n\n");
798
+ }
727
799
  var GLSLRendererAdapter = class {
728
800
  type = "glsl";
729
801
  displayName = "GLSL Shader";
@@ -747,16 +819,17 @@ var GLSLRendererAdapter = class {
747
819
  }
748
820
  return { valid: errors.length === 0, errors };
749
821
  }
750
- async compile(algorithm) {
822
+ async compile(algorithm, components) {
751
823
  const validation = this.validate(algorithm);
752
824
  if (!validation.valid) {
753
825
  throw new Error(
754
826
  `GLSL compilation failed: ${validation.errors.join("; ")}`
755
827
  );
756
828
  }
757
- const uniforms = extractUniforms(algorithm);
829
+ const fragmentSource = injectGLSLComponents(algorithm, components);
830
+ const uniforms = extractUniforms(fragmentSource);
758
831
  const compiled = {
759
- fragmentSource: algorithm,
832
+ fragmentSource,
760
833
  vertexSource: FULLSCREEN_QUAD_VERTEX,
761
834
  uniformNames: uniforms
762
835
  };
@@ -1002,7 +1075,9 @@ var GLSLRendererAdapter = class {
1002
1075
  const { width, height } = sketch.canvas;
1003
1076
  const pixelDensity = sketch.canvas.pixelDensity ?? 1;
1004
1077
  const stateJson = JSON.stringify(sketch.state, null, 2);
1005
- const uniforms = extractUniforms(sketch.algorithm);
1078
+ const glslComponentCode = extractGLSLComponentCode(sketch.components);
1079
+ const fullAlgorithm = glslComponentCode ? injectGLSLSource(sketch.algorithm, glslComponentCode) : sketch.algorithm;
1080
+ const uniforms = extractUniforms(fullAlgorithm);
1006
1081
  const paramBindings = uniforms.params.map((u) => {
1007
1082
  const key = u.substring(2);
1008
1083
  return ` { const loc = gl.getUniformLocation(program, "${u}"); if (loc && state.params["${key}"] !== undefined) gl.uniform1f(loc, state.params["${key}"]); }`;
@@ -1036,7 +1111,7 @@ var GLSLRendererAdapter = class {
1036
1111
  const vertSrc = \`${FULLSCREEN_QUAD_VERTEX}\`;
1037
1112
 
1038
1113
  // Fragment shader
1039
- const fragSrc = \`${sketch.algorithm.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$/g, "\\$")}\`;
1114
+ const fragSrc = \`${fullAlgorithm.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$/g, "\\$")}\`;
1040
1115
 
1041
1116
  function createShader(type, src) {
1042
1117
  const s = gl.createShader(type);
@@ -1133,15 +1208,20 @@ var SVGRendererAdapter = class {
1133
1208
  }
1134
1209
  return { valid: true, errors: [] };
1135
1210
  }
1136
- async compile(algorithm) {
1211
+ async compile(algorithm, components) {
1137
1212
  const validation = this.validate(algorithm);
1138
1213
  if (!validation.valid) {
1139
1214
  throw new Error(
1140
1215
  `SVG compilation failed: ${validation.errors.join("; ")}`
1141
1216
  );
1142
1217
  }
1218
+ const componentCode = components?.map(
1219
+ (c) => `// --- ${c.name} v${c.version} ---
1220
+ ${c.code}`
1221
+ ).join("\n\n") ?? "";
1143
1222
  const wrappedSource = `
1144
1223
  return (function() {
1224
+ ${componentCode}
1145
1225
  ${algorithm}
1146
1226
  return sketch;
1147
1227
  })();
@@ -1271,6 +1351,7 @@ var SVGRendererAdapter = class {
1271
1351
  const state = ${stateJson};
1272
1352
  state.canvas = { width: ${width}, height: ${height} };
1273
1353
 
1354
+ ${extractComponentCode(sketch.components)}
1274
1355
  ${sketch.algorithm}
1275
1356
 
1276
1357
  const module = sketch(state);
@@ -1804,6 +1885,7 @@ function createDefaultSkillRegistry() {
1804
1885
  }
1805
1886
  export {
1806
1887
  COLOR_SKILLS,
1888
+ COMPONENT_REGISTRY,
1807
1889
  COMPOSITION_SKILLS,
1808
1890
  Canvas2DRendererAdapter,
1809
1891
  GLSLRendererAdapter,
@@ -1814,6 +1896,7 @@ export {
1814
1896
  ThreeRendererAdapter,
1815
1897
  createDefaultRegistry,
1816
1898
  createDefaultSkillRegistry,
1817
- hexToVec3
1899
+ hexToVec3,
1900
+ resolveComponents
1818
1901
  };
1819
1902
  //# sourceMappingURL=index.js.map