@spaceteams/weft 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Spaceteams
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,488 @@
1
+ //#region src/semantics/algebra.d.ts
2
+ type OpsDescriptor = {
3
+ readonly family: string;
4
+ readonly version: string;
5
+ };
6
+ type Equality<T> = {
7
+ eq(a: T, b: T): boolean;
8
+ };
9
+ type Order<T> = Equality<T> & {
10
+ compare(a: T, b: T): -1 | 0 | 1;
11
+ };
12
+ type Additive<T> = Equality<T> & {
13
+ zero(): T;
14
+ add(a: T, b: T): T;
15
+ sub(a: T, b: T): T;
16
+ };
17
+ type Scalable<T, S> = {
18
+ one(): S;
19
+ scale(value: T, factor: S): T;
20
+ };
21
+ type Divisible<T, R> = {
22
+ div(a: T, b: T): R;
23
+ };
24
+ declare const defaultOps: OpsDescriptor & Equality<unknown>;
25
+ declare const defaultNumberOps: OpsDescriptor & Order<number> & Additive<number> & Scalable<number, number> & Divisible<number, number>;
26
+ //#endregion
27
+ //#region src/snapshot/canonicalize.d.ts
28
+ type CanonicalJson = null | boolean | number | string | CanonicalJson[] | {
29
+ [key: string]: CanonicalJson;
30
+ };
31
+ //#endregion
32
+ //#region src/semantics/codec.d.ts
33
+ type Codec<T> = {
34
+ encode: (value: T) => CanonicalJson;
35
+ decode: (value: CanonicalJson) => T;
36
+ };
37
+ //#endregion
38
+ //#region src/semantics/formatter.d.ts
39
+ type Formatter<T, C = void> = {
40
+ format(value: T, context: C): string;
41
+ };
42
+ type Normalizer<T> = {
43
+ normalize(value: T): T;
44
+ };
45
+ //#endregion
46
+ //#region src/key.d.ts
47
+ type KeySemantics<T> = Equality<T> & Normalizer<T> & Codec<T>;
48
+ type KeyId = string;
49
+ type Key<T> = {
50
+ readonly id: KeyId;
51
+ readonly __kind: "key";
52
+ readonly __value?: T;
53
+ };
54
+ type AnyKey = Key<any>;
55
+ declare function key<T>(id: KeyId): Key<T>;
56
+ //#endregion
57
+ //#region src/facts.d.ts
58
+ type FactBag = Record<KeyId, unknown>;
59
+ //#endregion
60
+ //#region src/overlay/index.d.ts
61
+ type Overlay = Record<KeyId, unknown>;
62
+ type OverlayedFacts = {
63
+ readonly base: FactBag;
64
+ readonly overlay: Overlay;
65
+ readonly effective: FactBag;
66
+ };
67
+ //#endregion
68
+ //#region src/draft/draft-meta.d.ts
69
+ type DraftMeta = {
70
+ readonly createdBy?: string;
71
+ readonly createdAt?: string;
72
+ readonly label?: string;
73
+ };
74
+ //#endregion
75
+ //#region src/draft/index.d.ts
76
+ type DraftId = string;
77
+ type Draft = {
78
+ readonly draftId: DraftId;
79
+ readonly base: FactBag;
80
+ readonly overlay: Overlay;
81
+ readonly meta?: DraftMeta;
82
+ };
83
+ declare function createDraft(draftId: DraftId, base: FactBag, overlay: Overlay): Draft;
84
+ declare function isEmptyDraft(draft: Draft): boolean;
85
+ //#endregion
86
+ //#region src/input.d.ts
87
+ type Input<T> = {
88
+ readonly kind: "input";
89
+ readonly key: Key<T>;
90
+ };
91
+ //#endregion
92
+ //#region src/key-meta.d.ts
93
+ type KeyMeta = {
94
+ readonly label?: string;
95
+ readonly description?: string;
96
+ readonly group?: string;
97
+ readonly unit?: string;
98
+ readonly order?: number;
99
+ };
100
+ //#endregion
101
+ //#region src/rule/index.d.ts
102
+ type Resolver = <T>(key: Key<T>) => T;
103
+ type Rule<T> = {
104
+ readonly __kind: "rule";
105
+ readonly spec: Record<string, unknown>;
106
+ readonly target: Key<T>;
107
+ readonly deps: readonly AnyKey[];
108
+ readonly eval: (get: Resolver) => {
109
+ output: T;
110
+ detail?: Record<string, unknown>;
111
+ };
112
+ };
113
+ declare function rule<T>(def: {
114
+ target: Key<T>;
115
+ deps: readonly AnyKey[];
116
+ spec: Record<string, unknown>;
117
+ eval: (get: Resolver) => {
118
+ output: T;
119
+ detail?: Record<string, unknown>;
120
+ };
121
+ }): Rule<T>;
122
+ //#endregion
123
+ //#region src/rule/rule-meta.d.ts
124
+ type RuleMeta = {
125
+ readonly label?: string;
126
+ readonly description?: string;
127
+ };
128
+ //#endregion
129
+ //#region src/model/index.d.ts
130
+ type Model = {
131
+ readonly inputs: readonly Input<unknown>[];
132
+ readonly rules: readonly Rule<unknown>[];
133
+ readonly semantics: ReadonlyMap<KeyId, Partial<KeySemantics<unknown>>>;
134
+ readonly keyMeta: ReadonlyMap<KeyId, KeyMeta>;
135
+ readonly ruleMeta: ReadonlyMap<KeyId, RuleMeta>;
136
+ };
137
+ type CompiledModel = Model & {
138
+ readonly keys: ReadonlyMap<KeyId, AnyKey>;
139
+ readonly semantics: ReadonlyMap<KeyId, Partial<KeySemantics<unknown>>>;
140
+ readonly inputKeys: readonly KeyId[];
141
+ readonly orderedRuleTargets: readonly KeyId[];
142
+ readonly ruleByTarget: ReadonlyMap<KeyId, Rule<unknown>>;
143
+ readonly depsByTarget: ReadonlyMap<KeyId, readonly KeyId[]>;
144
+ readonly dependentsByKey: ReadonlyMap<KeyId, readonly KeyId[]>;
145
+ };
146
+ //#endregion
147
+ //#region src/evaluate/trace-step.d.ts
148
+ type TraceStep = {
149
+ readonly target: KeyId;
150
+ readonly keyMeta?: KeyMeta;
151
+ readonly deps: readonly KeyId[];
152
+ readonly ruleMeta?: RuleMeta;
153
+ readonly ruleSpec: Record<string, unknown>;
154
+ readonly inputs: Record<KeyId, unknown>;
155
+ readonly output: unknown;
156
+ readonly detail: Record<string, unknown>;
157
+ };
158
+ //#endregion
159
+ //#region src/evaluate/evaluation-result.d.ts
160
+ type MissingReason = {
161
+ kind: "missing-input";
162
+ key: KeyId;
163
+ } | {
164
+ kind: "rule-not-run";
165
+ key: KeyId;
166
+ because: KeyId[];
167
+ };
168
+ type EvaluationResult = {
169
+ readonly values: ReadonlyMap<KeyId, unknown>;
170
+ readonly missing: ReadonlyMap<KeyId, MissingReason>;
171
+ readonly order: readonly string[];
172
+ readonly trace: readonly TraceStep[];
173
+ };
174
+ //#endregion
175
+ //#region src/overlay/diff-results.d.ts
176
+ type ValueDelta = {
177
+ readonly key: KeyId;
178
+ readonly kind: "added";
179
+ readonly after: unknown;
180
+ } | {
181
+ readonly key: KeyId;
182
+ readonly kind: "removed";
183
+ readonly before: unknown;
184
+ } | {
185
+ readonly key: KeyId;
186
+ readonly kind: "changed";
187
+ readonly before: unknown;
188
+ readonly after: unknown;
189
+ };
190
+ declare function diffResults(model: CompiledModel, before: EvaluationResult, after: EvaluationResult): {
191
+ deltas: readonly ValueDelta[];
192
+ };
193
+ //#endregion
194
+ //#region src/evaluate/index.d.ts
195
+ type EvaluateMode = "strict" | "lenient";
196
+ declare const defaultEvaluateMode: EvaluateMode;
197
+ declare function evaluate(model: CompiledModel, provided: FactBag, mode?: EvaluateMode): EvaluationResult;
198
+ //#endregion
199
+ //#region src/overlay/evaluate-overlay.d.ts
200
+ type ValueOrigin = {
201
+ readonly kind: "base";
202
+ } | {
203
+ readonly kind: "overlay";
204
+ } | {
205
+ readonly kind: "derived";
206
+ };
207
+ type OriginMap = ReadonlyMap<KeyId, ValueOrigin>;
208
+ type OverlayEvaluationResult = EvaluationResult & {
209
+ readonly overlayedFacts: OverlayedFacts;
210
+ readonly origins: OriginMap;
211
+ };
212
+ declare function evaluateOverlay(model: CompiledModel, base: FactBag, overlay: Overlay, mode?: EvaluateMode): OverlayEvaluationResult;
213
+ //#endregion
214
+ //#region src/overlay/diff-group.d.ts
215
+ type DiffGroup = {
216
+ readonly label: string;
217
+ readonly deltas: readonly ValueDelta[];
218
+ };
219
+ type GroupedDiff = readonly DiffGroup[];
220
+ //#endregion
221
+ //#region src/overlay/explain-diff.d.ts
222
+ type ExplainedDependency = {
223
+ readonly key: KeyId;
224
+ readonly label?: string;
225
+ readonly kind?: string;
226
+ readonly changed: boolean;
227
+ };
228
+ type Change = {
229
+ readonly delta: ValueDelta;
230
+ readonly dependencies?: readonly ExplainedDependency[];
231
+ };
232
+ declare function explainDiffs(result: OverlayEvaluationResult, deltas: readonly ValueDelta[]): readonly Change[];
233
+ //#endregion
234
+ //#region src/draft/evaluate-draft.d.ts
235
+ type EvaluatedDraft = {
236
+ readonly draft: Draft;
237
+ readonly result: OverlayEvaluationResult;
238
+ readonly deltas: readonly ValueDelta[];
239
+ };
240
+ declare function evaluateDraft(model: CompiledModel, draft: Draft, mode?: EvaluateMode): EvaluatedDraft;
241
+ //#endregion
242
+ //#region src/draft/analyze-draft.d.ts
243
+ type ImpactAnalysis = {
244
+ readonly direct: readonly KeyId[];
245
+ readonly affected: readonly KeyId[];
246
+ readonly terminal: readonly KeyId[];
247
+ };
248
+ type DraftAnalysis = {
249
+ readonly evaluated: EvaluatedDraft;
250
+ readonly groupedDiffs: readonly DiffGroup[];
251
+ readonly changes: readonly Change[];
252
+ readonly impact: ImpactAnalysis;
253
+ readonly normalizationIssues: readonly NormalizationIssue[];
254
+ };
255
+ declare function analyzeDraft(model: CompiledModel, draft: Draft, mode?: EvaluateMode): DraftAnalysis;
256
+ type NormalizationIssue = {
257
+ level: "error" | "warning";
258
+ key: KeyId;
259
+ message: string;
260
+ };
261
+ type NormalizedDraft = {
262
+ draft: Draft;
263
+ issues: readonly NormalizationIssue[];
264
+ };
265
+ type AnalysisSnapshot = {
266
+ modelFingerprint: string;
267
+ baseFingerprint: string;
268
+ overlayFingerprint: string;
269
+ analysisFingerprint: string;
270
+ createdAt: string;
271
+ };
272
+ type FreezeDraftAnalysisInput = {
273
+ model: CompiledModel;
274
+ draft: Draft;
275
+ normalized: NormalizedDraft;
276
+ evaluated: EvaluatedDraft;
277
+ groupedDiffs: GroupedDiff;
278
+ changes: readonly Change[];
279
+ impact: ImpactAnalysis;
280
+ now: string;
281
+ };
282
+ declare function freezeDraftAnalysis(model: CompiledModel, analysis: DraftAnalysis): {
283
+ draftId: string;
284
+ snapshot: AnalysisSnapshot;
285
+ base: Record<string, unknown>;
286
+ overlay: Record<string, unknown>;
287
+ effective: Record<string, unknown>;
288
+ values: Record<string, CanonicalJson>;
289
+ deltas: ValueDelta[];
290
+ groupedDiffs: {
291
+ label: string;
292
+ deltas: ValueDelta[];
293
+ }[];
294
+ changes: {
295
+ delta: ValueDelta;
296
+ dependencies: readonly ExplainedDependency[] | undefined;
297
+ }[];
298
+ impact: ImpactAnalysis;
299
+ normalizationIssues: readonly NormalizationIssue[];
300
+ frozenAt: string;
301
+ };
302
+ //#endregion
303
+ //#region src/inspect/inspection-node.d.ts
304
+ type InspectionNode = {
305
+ key: KeyId;
306
+ kind: string;
307
+ label: string;
308
+ meta?: {
309
+ key?: KeyMeta;
310
+ rule?: RuleMeta;
311
+ };
312
+ structure?: {
313
+ ruleSpec?: Record<string, unknown>;
314
+ };
315
+ execution?: {
316
+ value?: unknown;
317
+ trace?: TraceStep;
318
+ };
319
+ change?: Change;
320
+ children: InspectionNode[];
321
+ };
322
+ //#endregion
323
+ //#region src/inspect/inspect-diff-target.d.ts
324
+ declare function inspectDiffTarget(model: CompiledModel, result: OverlayEvaluationResult, changes: readonly Change[], target: KeyId): InspectionNode;
325
+ //#endregion
326
+ //#region src/inspect/inspect-model-target.d.ts
327
+ declare function inspectModelTarget(model: CompiledModel, target: string): InspectionNode;
328
+ //#endregion
329
+ //#region src/inspect/inspect-trace-target.d.ts
330
+ declare function inspectTraceTarget(model: CompiledModel, trace: readonly TraceStep[], target: KeyId): InspectionNode;
331
+ //#endregion
332
+ //#region src/inspect/inspection-node-to-ascii.d.ts
333
+ type RenderOptions = {
334
+ showMeta: boolean;
335
+ showChange: boolean;
336
+ };
337
+ declare const inspectionNodeToAscii: (root: InspectionNode, options: RenderOptions) => string;
338
+ //#endregion
339
+ //#region src/model/compile-model.d.ts
340
+ type ModelIssue = {
341
+ level: "error" | "warn";
342
+ code: "DUPLICATE_INPUT" | "DUPLICATE_RULE_TARGET" | "INPUT_RULE_CONFLICT" | "MISSING_DEPENDENCY" | "CYCLE";
343
+ message: string;
344
+ keys?: readonly KeyId[];
345
+ };
346
+ type CompileResult = {
347
+ ok: true;
348
+ model: CompiledModel;
349
+ issues: readonly ModelIssue[];
350
+ } | {
351
+ ok: false;
352
+ issues: readonly ModelIssue[];
353
+ };
354
+ declare function compileModel(model: Model): CompileResult;
355
+ //#endregion
356
+ //#region src/model/create-model.d.ts
357
+ declare function createModel(): {
358
+ input<T>(k: Key<T>, meta?: KeyMeta, semantics?: Partial<KeySemantics<T>> | undefined): Key<T>;
359
+ rule<T>(r: Rule<T>, meta?: RuleMeta, semantics?: Partial<KeySemantics<T>> | undefined): Key<T>;
360
+ build(): Model;
361
+ };
362
+ //#endregion
363
+ //#region src/model/model.d.ts
364
+ declare function getDependencies(model: CompiledModel, key: AnyKey): readonly KeyId[];
365
+ declare function getDependents(model: CompiledModel, key: AnyKey): readonly KeyId[];
366
+ declare function getDeclaredKeys(model: CompiledModel): readonly KeyId[];
367
+ declare function upstreamOf(model: CompiledModel, key: AnyKey): readonly KeyId[];
368
+ declare function downstreamOf(model: CompiledModel, key: AnyKey): readonly KeyId[];
369
+ declare function sortKeysByModelOrder(model: CompiledModel, keys: Iterable<KeyId>): readonly string[];
370
+ //#endregion
371
+ //#region src/model/model-graph.d.ts
372
+ type ModelGraph = {
373
+ readonly nodes: readonly KeyId[];
374
+ readonly edges: readonly {
375
+ from: KeyId;
376
+ to: KeyId;
377
+ }[];
378
+ };
379
+ declare function toGraph(model: CompiledModel): ModelGraph;
380
+ declare function subgraph(model: CompiledModel, includedKeys: readonly KeyId[]): ModelGraph;
381
+ declare function upstreamGraphOf(model: CompiledModel, key: AnyKey, options?: {
382
+ includeTarget?: boolean;
383
+ }): ModelGraph;
384
+ declare function downstreamGraphOf(model: CompiledModel, key: AnyKey, options?: {
385
+ includeTarget?: boolean;
386
+ }): ModelGraph;
387
+ //#endregion
388
+ //#region src/value.d.ts
389
+ type Value<T> = {
390
+ readonly __kind: "value";
391
+ readonly value: T;
392
+ };
393
+ declare function value<T>(value: T): Value<T>;
394
+ //#endregion
395
+ //#region src/rule/operand.d.ts
396
+ type Operand<T> = Value<T> | Key<T>;
397
+ declare function resolveOperand<T>(operand: Operand<T>, get: (k: Key<T>) => T): T;
398
+ //#endregion
399
+ //#region src/rule/decision.d.ts
400
+ type EqualityPredicate<T> = {
401
+ source: Key<T>;
402
+ op: "eq";
403
+ right: Operand<T>;
404
+ } | {
405
+ source: Key<T>;
406
+ op: "in";
407
+ values: readonly Operand<T>[];
408
+ };
409
+ type OrderedPredicate<T> = {
410
+ source: Key<T>;
411
+ op: "gt" | "gte" | "lt" | "lte";
412
+ right: Operand<T>;
413
+ };
414
+ type Predicate<T> = EqualityPredicate<T> | OrderedPredicate<T>;
415
+ type RowId = string;
416
+ type DecisionRow<T, O, P extends Predicate<T> = Predicate<T>> = {
417
+ id: RowId;
418
+ when: readonly P[];
419
+ output: Operand<O>;
420
+ label?: string;
421
+ };
422
+ type DecisionTable<T, O, P extends Predicate<T> = Predicate<T>> = {
423
+ name: string;
424
+ rows: readonly DecisionRow<T, O, P>[];
425
+ default?: Operand<O>;
426
+ };
427
+ type RequiredOps<T, P extends Predicate<T>> = Extract<P, OrderedPredicate<T>> extends never ? Equality<T> : Order<T>;
428
+ type DecisionTableTraceDetail = {
429
+ op: "decision";
430
+ tableName: string;
431
+ matchedRowId?: RowId;
432
+ matchedRowLabel?: RowId;
433
+ usedDefault: boolean;
434
+ };
435
+ type DecisionTableSpec = {
436
+ op: "decision";
437
+ opsDescriptor: OpsDescriptor;
438
+ table: DecisionTable<unknown, unknown>;
439
+ };
440
+ declare function decision<T, O, P extends Predicate<T>>(ops: OpsDescriptor & RequiredOps<T, P>, target: Key<O>, table: DecisionTable<T, O, P>): Rule<O>;
441
+ //#endregion
442
+ //#region src/rule/projection.d.ts
443
+ type ProjectionSpec = {
444
+ op: "project";
445
+ source: KeyId;
446
+ field: string;
447
+ };
448
+ declare function projection<T extends Record<string, unknown>, K extends keyof T>(target: Key<T[K]>, source: Key<T>, field: K): Rule<T[K]>;
449
+ //#endregion
450
+ //#region src/rule/ratio.d.ts
451
+ type RatioTraceSpec = {
452
+ op: "ratio";
453
+ opsDescriptor: OpsDescriptor;
454
+ numerator: KeyId;
455
+ denominator: KeyId;
456
+ };
457
+ declare function ratio<T, R>(ops: OpsDescriptor & Divisible<T, R>, target: Key<R>, numerator: Key<T>, denominator: Key<T>): Rule<R>;
458
+ //#endregion
459
+ //#region src/rule/scale.d.ts
460
+ type ScaleTraceSpec = {
461
+ op: "scale";
462
+ opsDescriptor: OpsDescriptor;
463
+ input: KeyId;
464
+ factor: KeyId;
465
+ };
466
+ declare function scale<T, S>(ops: OpsDescriptor & Scalable<T, S>, target: Key<T>, input: Key<T>, factor: Key<S>): Rule<T>;
467
+ //#endregion
468
+ //#region src/rule/sum.d.ts
469
+ type SumTraceSpec = {
470
+ op: "sum";
471
+ opsDescriptor: OpsDescriptor;
472
+ deps: readonly KeyId[];
473
+ };
474
+ declare function sum<T>(ops: OpsDescriptor & Additive<T>, target: Key<T>, deps: readonly Key<T>[]): Rule<T>;
475
+ //#endregion
476
+ //#region src/rule/weighted-sum.d.ts
477
+ type WeightedSumSpec = {
478
+ op: "weighted-sum";
479
+ opsDescriptor: OpsDescriptor;
480
+ deps: readonly KeyId[];
481
+ weights: readonly unknown[];
482
+ };
483
+ declare function weightedSum<T, S>(ops: OpsDescriptor & Additive<T> & Scalable<T, S>, target: Key<T>, deps: readonly {
484
+ key: Key<T>;
485
+ weight: Operand<S>;
486
+ }[]): Rule<T>;
487
+ //#endregion
488
+ export { Additive, AnyKey, Change, CompileResult, CompiledModel, DecisionRow, DecisionTable, DecisionTableSpec, DecisionTableTraceDetail, Divisible, Draft, DraftAnalysis, DraftId, Equality, EqualityPredicate, EvaluateMode, EvaluatedDraft, EvaluationResult, ExplainedDependency, Formatter, FreezeDraftAnalysisInput, ImpactAnalysis, InspectionNode, Key, KeyId, KeySemantics, MissingReason, Model, ModelGraph, ModelIssue, Normalizer, Operand, OpsDescriptor, Order, OrderedPredicate, OriginMap, OverlayEvaluationResult, Predicate, ProjectionSpec, RatioTraceSpec, RequiredOps, Resolver, RowId, Rule, Scalable, ScaleTraceSpec, SumTraceSpec, Value, ValueDelta, ValueOrigin, WeightedSumSpec, analyzeDraft, compileModel, createDraft, createModel, decision, defaultEvaluateMode, defaultNumberOps, defaultOps, diffResults, downstreamGraphOf, downstreamOf, evaluate, evaluateDraft, evaluateOverlay, explainDiffs, freezeDraftAnalysis, getDeclaredKeys, getDependencies, getDependents, inspectDiffTarget, inspectModelTarget, inspectTraceTarget, inspectionNodeToAscii, isEmptyDraft, key, projection, ratio, resolveOperand, rule, scale, sortKeysByModelOrder, subgraph, sum, toGraph, upstreamGraphOf, upstreamOf, value, weightedSum };