@almadar/core 4.6.0 → 4.7.1

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.
@@ -1,52 +1,8 @@
1
- import { T as TraitReference, a as TraitEventContract, E as EntityField, b as EntityPersistence, c as EntityRow, O as OrbitalDefinition, d as OrbitalSchema, e as Trait, f as Entity, P as Page } from './schema-CLFPg1xW.js';
2
- export { C as ComposeBehaviorsInput, a as ComposeBehaviorsResult, E as EventWiringEntry, L as LayoutStrategy, b as applyEventWiring, c as composeBehaviors, d as detectLayoutStrategy } from './compose-behaviors-Bp32KayC.js';
1
+ import { T as TraitEventContract, E as Entity, S as SExpr, a as TraitEventListener, b as EntityField, c as EntityPersistence, d as EntityRow, U as UseDeclaration, e as EntityRef, f as TraitRef, P as PageRef, O as OrbitalDefinition, g as OrbitalSchema, h as Trait, i as Page, j as PageRefObject, k as TraitReference } from './schema-aGK1-NnG.js';
2
+ export { C as ComposeBehaviorsInput, a as ComposeBehaviorsResult, E as EventWiringEntry, L as LayoutStrategy, b as applyEventWiring, c as composeBehaviors, d as detectLayoutStrategy } from './compose-behaviors-BCURTbGX.js';
3
3
  import 'zod';
4
4
  import '@almadar/patterns';
5
5
 
6
- /**
7
- * Explode Behavior Composition
8
- *
9
- * Human-authoring primitive (Phase 5.3 of Almadar_Agent_Behaviors_Plan).
10
- *
11
- * Reads a behavior's compiled `.orb` from the sibling `@almadar/std`
12
- * registry on disk and returns the flat list of `TraitReference` objects
13
- * it's composed from — each with `from`, `ref`, `linkedEntity`, and any
14
- * call-site overrides. **Not exposed to the agent or LLM.** The helper
15
- * exists for developers designing new molecules or debugging compositions.
16
- *
17
- * The function is Node-only. It walks the filesystem under
18
- * `packages/almadar-std/behaviors/registry/{atoms,molecules,organisms}/`
19
- * relative to this source file and will throw a descriptive error if the
20
- * behavior cannot be resolved.
21
- *
22
- * When the target is a leaf atom (no `uses:` / composition), the function
23
- * returns a single synthetic `TraitReference` pointing at the atom's own
24
- * inline trait so consumers always get something useful.
25
- *
26
- * @packageDocumentation
27
- */
28
-
29
- /**
30
- * Flatten a behavior's `.orb` composition into the list of
31
- * `TraitReference` objects it's built from.
32
- *
33
- * For composed behaviors (molecules, organisms) this returns every
34
- * `TraitReference` entry from every orbital's `traits:` array, carrying
35
- * the call-site `ref`, backfilled `from` (looked up against the orbital's
36
- * `uses:` imports when omitted), `linkedEntity`, `events`, `effects`, and
37
- * any other override fields declared at the call site.
38
- *
39
- * For leaf atoms (no composition, only an inline trait) the function
40
- * returns a single synthetic `TraitReference` pointing at the atom's own
41
- * trait, so consumers always get a non-empty array they can iterate.
42
- *
43
- * @param behaviorName - Registry name of the behavior (e.g. `"std-cart"`,
44
- * `"std-browse"`).
45
- * @returns Flat list of every `TraitReference` the behavior is composed of.
46
- * @throws Error when the behavior cannot be found in any registry level.
47
- */
48
- declare function explodeBehaviorComposition(behaviorName: string): TraitReference[];
49
-
50
6
  /**
51
7
  * Orbital Builders
52
8
  *
@@ -96,6 +52,146 @@ declare function makePage(opts: MakePageOpts): Page;
96
52
  * Build an OrbitalDefinition from its three components.
97
53
  */
98
54
  declare function makeOrbital(name: string, entity: Entity, traits: Trait[], pages: Page[]): OrbitalDefinition;
55
+ /**
56
+ * Options for {@link makeTraitRef}.
57
+ */
58
+ interface MakeTraitRefOpts {
59
+ /**
60
+ * Optional registry path disambiguator that pairs with {@link ref}
61
+ * (see {@link TraitReference.from}).
62
+ */
63
+ from?: string;
64
+ /** Trait reference string, e.g. "Browse.traits.BrowseItemBrowse". */
65
+ ref: string;
66
+ /** Rename the inlined trait at the call site. */
67
+ name?: string;
68
+ /** Rebind the trait to a different linkedEntity. */
69
+ linkedEntity?: string;
70
+ /** Per-key rename map, e.g. `{ OPEN: "ADD_ITEM" }`. */
71
+ events?: Record<string, string>;
72
+ /**
73
+ * Per-event SExpression effect replacement. Keys are POST-rename event
74
+ * names. See {@link TraitReference.effects} for the full contract.
75
+ */
76
+ effects?: Record<string, SExpr[]>;
77
+ /** Replace the imported trait's `listens` array entirely. */
78
+ listens?: TraitEventListener[];
79
+ /** Set every emit's scope. */
80
+ emitsScope?: 'internal' | 'external';
81
+ /**
82
+ * Nested config overrides. Matches the real {@link TraitReference.config}
83
+ * shape: `Record<string, Record<string, unknown>>` (the outer key is the
84
+ * config field name, the inner record is its value shape).
85
+ */
86
+ config?: Record<string, Record<string, unknown>>;
87
+ }
88
+ /**
89
+ * Build a {@link TraitReference} from options.
90
+ *
91
+ * Pass-through factory: copies only the fields that are actually provided,
92
+ * so optionals stay absent (no `key: undefined` slots) and the emitted
93
+ * object matches the inliner's expectation that "present = override".
94
+ */
95
+ declare function makeTraitRef(opts: MakeTraitRefOpts): TraitReference;
96
+ /**
97
+ * Options for {@link makePageRef}.
98
+ */
99
+ interface MakePageRefOpts {
100
+ /**
101
+ * Optional registry path disambiguator that pairs with {@link ref}
102
+ * (see {@link PageRefObject.from}).
103
+ */
104
+ from?: string;
105
+ /** Page reference string, e.g. "Browse.pages.BrowseItemPage". */
106
+ ref: string;
107
+ /** URL path override. */
108
+ path?: string;
109
+ /** Rebind the page's primary entity. */
110
+ linkedEntity?: string;
111
+ /** Replace the page's trait list. */
112
+ traits?: TraitRef[];
113
+ }
114
+ /**
115
+ * Build a {@link PageRefObject} from options. Pass-through factory — omits
116
+ * optional keys that are `undefined`.
117
+ */
118
+ declare function makePageRef(opts: MakePageRefOpts): PageRefObject;
119
+ /**
120
+ * Options for {@link makeOrbitalWithUses}.
121
+ */
122
+ interface MakeOrbitalWithUsesOpts {
123
+ /** Orbital name. */
124
+ name: string;
125
+ /**
126
+ * Per-orbital `uses:` header entries (see CLAUDE.md "uses: lives inside
127
+ * the orbital").
128
+ */
129
+ uses: UseDeclaration[];
130
+ /** Entity (inline or reference form). */
131
+ entity: EntityRef;
132
+ /** Trait references. */
133
+ traits: TraitRef[];
134
+ /** Optional page references (omitted entirely when not provided). */
135
+ pages?: PageRef[];
136
+ }
137
+ /**
138
+ * Build an {@link OrbitalDefinition} with the `uses:` header set. Follows
139
+ * the convention that `uses:` lives on the orbital (not the schema).
140
+ *
141
+ * When `pages` is omitted, the result has no `pages` property (matches the
142
+ * existing {@link OrbitalDefinition.pages} optionality in descriptors that
143
+ * carry trait-only atoms).
144
+ */
145
+ declare function makeOrbitalWithUses(opts: MakeOrbitalWithUsesOpts): OrbitalDefinition;
146
+ /**
147
+ * Options for {@link makeAtomOrbital}.
148
+ *
149
+ * Overrides for the single trait reference. Mirrors the {@link MakeTraitRefOpts}
150
+ * subset that is meaningful at the atom-wrapping call site.
151
+ */
152
+ interface MakeAtomOrbitalTraitOverrides {
153
+ name?: string;
154
+ events?: Record<string, string>;
155
+ effects?: Record<string, SExpr[]>;
156
+ listens?: TraitEventListener[];
157
+ emitsScope?: 'internal' | 'external';
158
+ config?: Record<string, Record<string, unknown>>;
159
+ }
160
+ /**
161
+ * Options for {@link makeAtomOrbital}.
162
+ */
163
+ interface MakeAtomOrbitalOpts {
164
+ /** Orbital name, e.g. `${entityName}Orbital`. */
165
+ name: string;
166
+ /** Atom registry path, e.g. `std/behaviors/atoms/std-browse`. */
167
+ atomPath: string;
168
+ /** Import alias (PascalCase), e.g. `Browse`. */
169
+ alias: string;
170
+ /** Entity definition to attach to the orbital. */
171
+ entity: Entity;
172
+ /** Trait reference string, e.g. `Browse.traits.BrowseItemBrowse`. */
173
+ traitRef: string;
174
+ /** Optional trait-level overrides applied at the call site. */
175
+ traitOverrides?: MakeAtomOrbitalTraitOverrides;
176
+ /** Optional page reference string, e.g. `Browse.pages.BrowseItemPage`. */
177
+ pageRef?: string;
178
+ /** Optional page-level overrides applied at the call site. */
179
+ pageOverrides?: {
180
+ path?: string;
181
+ };
182
+ }
183
+ /**
184
+ * Build a single-atom {@link OrbitalDefinition}.
185
+ *
186
+ * The common atom shape: one entity + one trait reference + (optionally) one
187
+ * page reference, all under a single `uses:` import. Wraps
188
+ * {@link makeTraitRef}, {@link makePageRef}, and {@link makeOrbitalWithUses}.
189
+ *
190
+ * The trait reference is linkedEntity-bound to `entity.name` by default so
191
+ * that the inliner's entity-substitution pass rewrites every `["ref", X]`
192
+ * and `@X.path` reference inside the atom.
193
+ */
194
+ declare function makeAtomOrbital(opts: MakeAtomOrbitalOpts): OrbitalDefinition;
99
195
  /**
100
196
  * Wrap one or more OrbitalDefinitions into an OrbitalSchema.
101
197
  * Every .orb file should be a full OrbitalSchema — this is the builder for that.
@@ -147,4 +243,4 @@ declare function compose(orbitals: (OrbitalDefinition | OrbitalSchema)[], pages:
147
243
  */
148
244
  declare function pipe(orbitals: (OrbitalDefinition | OrbitalSchema)[], events: TraitEventContract[], appName?: string): OrbitalSchema;
149
245
 
150
- export { type ComposeConnection, type ComposePage, type MakeEntityOpts, type MakePageOpts, compose, connect, ensureIdField, explodeBehaviorComposition, extractTrait, makeEntity, makeOrbital, makePage, makeSchema, mergeOrbitals, pipe, plural, wire };
246
+ export { type ComposeConnection, type ComposePage, type MakeAtomOrbitalOpts, type MakeAtomOrbitalTraitOverrides, type MakeEntityOpts, type MakeOrbitalWithUsesOpts, type MakePageOpts, type MakePageRefOpts, type MakeTraitRefOpts, compose, connect, ensureIdField, extractTrait, makeAtomOrbital, makeEntity, makeOrbital, makeOrbitalWithUses, makePage, makePageRef, makeSchema, makeTraitRef, mergeOrbitals, pipe, plural, wire };
package/dist/builders.js CHANGED
@@ -1,7 +1,4 @@
1
1
  import { z } from 'zod';
2
- import { readFileSync } from 'fs';
3
- import { resolve, dirname } from 'path';
4
- import { fileURLToPath } from 'url';
5
2
 
6
3
  // src/types/orbital.ts
7
4
  var FieldTypeSchema = z.enum([
@@ -1041,104 +1038,6 @@ function composeBehaviors(input) {
1041
1038
  }
1042
1039
  };
1043
1040
  }
1044
- function registryRoot() {
1045
- const here = dirname(fileURLToPath(import.meta.url));
1046
- return resolve(here, "..", "..", "..", "almadar-std", "behaviors", "registry");
1047
- }
1048
- var LEVELS = ["atoms", "molecules", "organisms"];
1049
- function resolveBehavior(behaviorName) {
1050
- const root = registryRoot();
1051
- const searched = [];
1052
- for (const level of LEVELS) {
1053
- const path = resolve(root, level, `${behaviorName}.orb`);
1054
- searched.push(path);
1055
- let source;
1056
- try {
1057
- source = readFileSync(path, "utf8");
1058
- } catch {
1059
- continue;
1060
- }
1061
- let parsed;
1062
- try {
1063
- parsed = JSON.parse(source);
1064
- } catch (err) {
1065
- const message = err instanceof Error ? err.message : String(err);
1066
- throw new Error(
1067
- `explodeBehaviorComposition: failed to parse ${path} as JSON: ${message}`
1068
- );
1069
- }
1070
- return { level, path, schema: parsed };
1071
- }
1072
- throw new Error(
1073
- `explodeBehaviorComposition: behavior "${behaviorName}" not found. Searched:
1074
- - ${searched.join("\n - ")}`
1075
- );
1076
- }
1077
- function isTraitReferenceObject(entry) {
1078
- return typeof entry === "object" && entry !== null && "ref" in entry && typeof entry.ref === "string" && !isInlineTrait(entry);
1079
- }
1080
- function aliasToFromMap(uses) {
1081
- const map = /* @__PURE__ */ new Map();
1082
- if (!uses) return map;
1083
- for (const use of uses) {
1084
- map.set(use.as, use.from);
1085
- }
1086
- return map;
1087
- }
1088
- function extractAlias(ref) {
1089
- const match = ref.match(/^([A-Z][a-zA-Z0-9]*)\.traits\.[A-Z][a-zA-Z0-9]*$/);
1090
- return match ? match[1] : null;
1091
- }
1092
- function findAtomInlineTrait(orbital) {
1093
- for (const entry of orbital.traits) {
1094
- if (typeof entry === "object" && entry !== null && isInlineTrait(entry)) {
1095
- return entry;
1096
- }
1097
- }
1098
- return void 0;
1099
- }
1100
- function syntheticLeafReference(behaviorName, orbital, trait) {
1101
- const base = behaviorName.replace(/^std-/, "");
1102
- const alias = base.split("-").filter((segment) => segment.length > 0).map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1)).join("") || "Behavior";
1103
- const entityName = typeof orbital.entity === "string" ? orbital.entity : "name" in orbital.entity && typeof orbital.entity.name === "string" ? orbital.entity.name : void 0;
1104
- const reference = {
1105
- ref: `${alias}.traits.${trait.name}`,
1106
- from: `std/behaviors/${behaviorName}`
1107
- };
1108
- if (trait.linkedEntity !== void 0) {
1109
- reference.linkedEntity = trait.linkedEntity;
1110
- } else if (entityName !== void 0) {
1111
- reference.linkedEntity = entityName;
1112
- }
1113
- return reference;
1114
- }
1115
- function explodeBehaviorComposition(behaviorName) {
1116
- const resolved = resolveBehavior(behaviorName);
1117
- const orbitals = Array.isArray(resolved.schema.orbitals) ? resolved.schema.orbitals : [];
1118
- const references = [];
1119
- for (const orbital of orbitals) {
1120
- const aliasMap = aliasToFromMap(orbital.uses);
1121
- for (const entry of orbital.traits ?? []) {
1122
- if (!isTraitReferenceObject(entry)) continue;
1123
- const copy = { ...entry };
1124
- if (copy.from === void 0) {
1125
- const alias = extractAlias(copy.ref);
1126
- const fromPath = alias !== null ? aliasMap.get(alias) : void 0;
1127
- if (fromPath !== void 0) {
1128
- copy.from = fromPath;
1129
- }
1130
- }
1131
- references.push(copy);
1132
- }
1133
- }
1134
- if (references.length > 0) return references;
1135
- const firstOrbital = orbitals[0];
1136
- const inlineTrait = firstOrbital ? findAtomInlineTrait(firstOrbital) : void 0;
1137
- if (firstOrbital && inlineTrait) {
1138
- return [syntheticLeafReference(behaviorName, firstOrbital, inlineTrait)];
1139
- }
1140
- return [];
1141
- }
1142
1041
 
1143
1042
  // src/builders.ts
1144
1043
  function ensureIdField(fields) {
@@ -1181,6 +1080,64 @@ function makePage(opts) {
1181
1080
  function makeOrbital(name, entity, traits, pages) {
1182
1081
  return { name, entity, traits, pages };
1183
1082
  }
1083
+ function makeTraitRef(opts) {
1084
+ const ref = { ref: opts.ref };
1085
+ if (opts.from !== void 0) ref.from = opts.from;
1086
+ if (opts.name !== void 0) ref.name = opts.name;
1087
+ if (opts.linkedEntity !== void 0) ref.linkedEntity = opts.linkedEntity;
1088
+ if (opts.events !== void 0) ref.events = opts.events;
1089
+ if (opts.effects !== void 0) ref.effects = opts.effects;
1090
+ if (opts.listens !== void 0) ref.listens = opts.listens;
1091
+ if (opts.emitsScope !== void 0) ref.emitsScope = opts.emitsScope;
1092
+ if (opts.config !== void 0) ref.config = opts.config;
1093
+ return ref;
1094
+ }
1095
+ function makePageRef(opts) {
1096
+ const ref = { ref: opts.ref };
1097
+ if (opts.from !== void 0) ref.from = opts.from;
1098
+ if (opts.path !== void 0) ref.path = opts.path;
1099
+ if (opts.linkedEntity !== void 0) ref.linkedEntity = opts.linkedEntity;
1100
+ if (opts.traits !== void 0) ref.traits = opts.traits;
1101
+ return ref;
1102
+ }
1103
+ function makeOrbitalWithUses(opts) {
1104
+ const orbital = {
1105
+ name: opts.name,
1106
+ uses: opts.uses,
1107
+ entity: opts.entity,
1108
+ traits: opts.traits,
1109
+ pages: opts.pages ?? []
1110
+ };
1111
+ return orbital;
1112
+ }
1113
+ function makeAtomOrbital(opts) {
1114
+ const traitRef = makeTraitRef({
1115
+ from: opts.atomPath,
1116
+ ref: opts.traitRef,
1117
+ linkedEntity: opts.entity.name,
1118
+ ...opts.traitOverrides?.name !== void 0 ? { name: opts.traitOverrides.name } : {},
1119
+ ...opts.traitOverrides?.events !== void 0 ? { events: opts.traitOverrides.events } : {},
1120
+ ...opts.traitOverrides?.effects !== void 0 ? { effects: opts.traitOverrides.effects } : {},
1121
+ ...opts.traitOverrides?.listens !== void 0 ? { listens: opts.traitOverrides.listens } : {},
1122
+ ...opts.traitOverrides?.emitsScope !== void 0 ? { emitsScope: opts.traitOverrides.emitsScope } : {},
1123
+ ...opts.traitOverrides?.config !== void 0 ? { config: opts.traitOverrides.config } : {}
1124
+ });
1125
+ const pages = opts.pageRef ? [
1126
+ makePageRef({
1127
+ from: opts.atomPath,
1128
+ ref: opts.pageRef,
1129
+ linkedEntity: opts.entity.name,
1130
+ ...opts.pageOverrides?.path !== void 0 ? { path: opts.pageOverrides.path } : {}
1131
+ })
1132
+ ] : void 0;
1133
+ return makeOrbitalWithUses({
1134
+ name: opts.name,
1135
+ uses: [{ from: opts.atomPath, as: opts.alias }],
1136
+ entity: opts.entity,
1137
+ traits: [traitRef],
1138
+ ...pages !== void 0 ? { pages } : {}
1139
+ });
1140
+ }
1184
1141
  function makeSchema(name, ...definitions) {
1185
1142
  return { name, version: "1.0.0", orbitals: definitions };
1186
1143
  }
@@ -1328,6 +1285,6 @@ function pipe(orbitals, events, appName) {
1328
1285
  };
1329
1286
  }
1330
1287
 
1331
- export { applyEventWiring, compose, composeBehaviors, connect, detectLayoutStrategy, ensureIdField, explodeBehaviorComposition, extractTrait, makeEntity, makeOrbital, makePage, makeSchema, mergeOrbitals, pipe, plural, wire };
1288
+ export { applyEventWiring, compose, composeBehaviors, connect, detectLayoutStrategy, ensureIdField, extractTrait, makeAtomOrbital, makeEntity, makeOrbital, makeOrbitalWithUses, makePage, makePageRef, makeSchema, makeTraitRef, mergeOrbitals, pipe, plural, wire };
1332
1289
  //# sourceMappingURL=builders.js.map
1333
1290
  //# sourceMappingURL=builders.js.map