@flowgram.ai/variable-core 0.5.6 → 1.0.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.mts CHANGED
@@ -358,9 +358,9 @@ declare abstract class ScopeChain {
358
358
  get onDispose(): Event<void>;
359
359
  }
360
360
 
361
- interface ASTNodeRegistry<JSON extends ASTNodeJSON = any, InjectOpts = any> {
361
+ interface ASTNodeRegistry<JSON extends ASTNodeJSON = any> {
362
362
  kind: string;
363
- new (params: CreateASTParams, injectOpts: InjectOpts): ASTNode<JSON>;
363
+ new (params: CreateASTParams, injectOpts: any): ASTNode<JSON>;
364
364
  }
365
365
  /**
366
366
  * An `ASTNode` represents a fundamental unit of variable information within the system's Abstract Syntax Tree.
@@ -375,14 +375,14 @@ interface ASTNodeRegistry<JSON extends ASTNodeJSON = any, InjectOpts = any> {
375
375
  * - **Reactive**: Changes in an ASTNode's value trigger events, enabling reactive programming patterns.
376
376
  * - **Serializable**: ASTNodes can be converted to and from a JSON format (ASTNodeJSON) for storage or transmission.
377
377
  */
378
- declare abstract class ASTNode<JSON extends ASTNodeJSON = any, InjectOpts = any> implements Disposable {
378
+ declare abstract class ASTNode<JSON extends ASTNodeJSON = any> implements Disposable {
379
379
  /**
380
380
  * @deprecated
381
381
  * Get the injected options for the ASTNode.
382
382
  *
383
383
  * Please use `@injectToAst(XXXService) declare xxxService: XXXService` to achieve external dependency injection.
384
384
  */
385
- readonly opts?: InjectOpts;
385
+ readonly opts?: any;
386
386
  /**
387
387
  * The unique identifier of the ASTNode, which is **immutable**.
388
388
  * - Immutable: Once assigned, the key cannot be changed.
@@ -429,7 +429,7 @@ declare abstract class ASTNode<JSON extends ASTNodeJSON = any, InjectOpts = any>
429
429
  /**
430
430
  * Child ASTNodes.
431
431
  */
432
- protected _children: Set<ASTNode<any, any>>;
432
+ protected _children: Set<ASTNode<any>>;
433
433
  /**
434
434
  * List of disposal handlers for the ASTNode.
435
435
  */
@@ -443,7 +443,7 @@ declare abstract class ASTNode<JSON extends ASTNodeJSON = any, InjectOpts = any>
443
443
  * @param createParams Necessary parameters for creating an ASTNode.
444
444
  * @param injectOptions Dependency injection for various modules.
445
445
  */
446
- constructor({ key, parent, scope }: CreateASTParams, opts?: InjectOpts);
446
+ constructor({ key, parent, scope }: CreateASTParams, opts?: any);
447
447
  /**
448
448
  * The type of the ASTNode.
449
449
  */
@@ -461,7 +461,7 @@ declare abstract class ASTNode<JSON extends ASTNodeJSON = any, InjectOpts = any>
461
461
  * Serializes the current ASTNode to ASTNodeJSON.
462
462
  * @returns
463
463
  */
464
- toJSON(): ASTNodeJSON;
464
+ abstract toJSON(): JSON;
465
465
  /**
466
466
  * Creates a child ASTNode.
467
467
  * @param json The AST JSON of the child ASTNode.
@@ -682,6 +682,9 @@ type DataInjector = () => Record<string, any>;
682
682
  * Register the AST node to the engine.
683
683
  */
684
684
  declare class ASTRegisters {
685
+ /**
686
+ * @deprecated Please use `@injectToAst(XXXService) declare xxxService: XXXService` to achieve external dependency injection.
687
+ */
685
688
  protected injectors: Map<ASTKindType, DataInjector>;
686
689
  protected astMap: Map<ASTKindType, ASTNodeRegistry>;
687
690
  /**
@@ -699,13 +702,16 @@ declare class ASTRegisters {
699
702
  * @param kind
700
703
  * @returns
701
704
  */
702
- getASTRegistryByKind(kind: ASTKindType): ASTNodeRegistry<any, any> | undefined;
705
+ getASTRegistryByKind(kind: ASTKindType): ASTNodeRegistry<any> | undefined;
703
706
  /**
704
707
  * Registers an AST node.
705
708
  * @param ASTNode
706
- * @param injector
707
709
  */
708
- registerAST(ASTNode: ASTNodeRegistry, injector?: DataInjector): void;
710
+ registerAST(ASTNode: ASTNodeRegistry,
711
+ /**
712
+ * @deprecated Please use `@injectToAst(XXXService) declare xxxService: XXXService` to achieve external dependency injection.
713
+ */
714
+ injector?: DataInjector): void;
709
715
  }
710
716
 
711
717
  /**
@@ -820,7 +826,10 @@ declare class ListNode extends ASTNode<ListNodeJSON> {
820
826
  * Serialize the `ListNode` to `ListNodeJSON`.
821
827
  * @returns The JSON representation of `ListNode`.
822
828
  */
823
- toJSON(): ASTNodeJSON;
829
+ toJSON(): {
830
+ kind: ASTKind;
831
+ list: any[];
832
+ };
824
833
  }
825
834
 
826
835
  /**
@@ -852,7 +861,10 @@ declare class MapNode extends ASTNode<MapNodeJSON> {
852
861
  * Serialize the `MapNode` to `MapNodeJSON`.
853
862
  * @returns The JSON representation of `MapNode`.
854
863
  */
855
- toJSON(): ASTNodeJSON;
864
+ toJSON(): {
865
+ kind: ASTKind;
866
+ map: [string, ASTNode<any>][];
867
+ };
856
868
  /**
857
869
  * Set a node in the map.
858
870
  * @param key The key of the node.
@@ -883,7 +895,7 @@ declare class MapNode extends ASTNode<MapNodeJSON> {
883
895
  *
884
896
  * All other types should extend this class.
885
897
  */
886
- declare abstract class BaseType<JSON extends ASTNodeJSON = any, InjectOpts = any> extends ASTNode<JSON, InjectOpts> {
898
+ declare abstract class BaseType<JSON extends ASTNodeJSON = any> extends ASTNode<JSON> {
887
899
  flags: number;
888
900
  /**
889
901
  * Check if the current type is equal to the target type.
@@ -899,11 +911,6 @@ declare abstract class BaseType<JSON extends ASTNodeJSON = any, InjectOpts = any
899
911
  * @returns The variable field if found, otherwise `undefined`.
900
912
  */
901
913
  getByKeyPath(keyPath?: string[]): BaseVariableField | undefined;
902
- /**
903
- * Serialize the node to a JSON object.
904
- * @returns The JSON representation of the node.
905
- */
906
- toJSON(): ASTNodeJSON;
907
914
  }
908
915
 
909
916
  /**
@@ -920,7 +927,7 @@ interface StringJSON {
920
927
  */
921
928
  format?: string;
922
929
  }
923
- declare class StringType extends BaseType {
930
+ declare class StringType extends BaseType<StringJSON> {
924
931
  flags: ASTNodeFlags;
925
932
  static kind: string;
926
933
  protected _format?: string;
@@ -934,6 +941,13 @@ declare class StringType extends BaseType {
934
941
  * @param json StringJSON representation of the `StringType`.
935
942
  */
936
943
  fromJSON(json?: StringJSON): void;
944
+ /**
945
+ * Serialize the `StringType` to `StringJSON`.
946
+ * @returns The JSON representation of `StringType`.
947
+ */
948
+ toJSON(): {
949
+ format: string | undefined;
950
+ };
937
951
  }
938
952
 
939
953
  /**
@@ -952,6 +966,7 @@ declare class IntegerType extends BaseType {
952
966
  * @param json The `IntegerJSON` to deserialize.
953
967
  */
954
968
  fromJSON(): void;
969
+ toJSON(): {};
955
970
  }
956
971
 
957
972
  /**
@@ -969,6 +984,7 @@ declare class BooleanType extends BaseType {
969
984
  * @param json The `BooleanJSON` to deserialize.
970
985
  */
971
986
  fromJSON(): void;
987
+ toJSON(): {};
972
988
  }
973
989
 
974
990
  /**
@@ -986,6 +1002,7 @@ declare class NumberType extends BaseType {
986
1002
  * @param json The `NumberJSON` to deserialize.
987
1003
  */
988
1004
  fromJSON(): void;
1005
+ toJSON(): {};
989
1006
  }
990
1007
 
991
1008
  /**
@@ -1043,7 +1060,10 @@ declare class ArrayType extends BaseType<ArrayJSON> {
1043
1060
  * Serialize the `ArrayType` to `ArrayJSON`
1044
1061
  * @returns The JSON representation of `ArrayType`.
1045
1062
  */
1046
- toJSON(): ASTNodeJSON;
1063
+ toJSON(): {
1064
+ kind: ASTKind;
1065
+ items: any;
1066
+ };
1047
1067
  }
1048
1068
 
1049
1069
  /**
@@ -1098,7 +1118,11 @@ declare class MapType extends BaseType<MapJSON> {
1098
1118
  * Serialize the node to a JSON object.
1099
1119
  * @returns The JSON representation of the node.
1100
1120
  */
1101
- toJSON(): ASTNodeJSON;
1121
+ toJSON(): {
1122
+ kind: ASTKind;
1123
+ keyType: any;
1124
+ valueType: any;
1125
+ };
1102
1126
  }
1103
1127
 
1104
1128
  /**
@@ -1109,9 +1133,7 @@ declare class MapType extends BaseType<MapJSON> {
1109
1133
  /**
1110
1134
  * ASTNodeJSON representation of the `Property`.
1111
1135
  */
1112
- type PropertyJSON<VariableMeta = any> = BaseVariableFieldJSON<VariableMeta> & {
1113
- key: string;
1114
- };
1136
+ type PropertyJSON<VariableMeta = any> = BaseVariableFieldJSON<VariableMeta>;
1115
1137
  /**
1116
1138
  * `Property` is a variable field that represents a property of a `ObjectType`.
1117
1139
  */
@@ -1119,11 +1141,6 @@ declare class Property<VariableMeta = any> extends BaseVariableField<VariableMet
1119
1141
  static kind: string;
1120
1142
  }
1121
1143
 
1122
- /**
1123
- * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
1124
- * SPDX-License-Identifier: MIT
1125
- */
1126
-
1127
1144
  /**
1128
1145
  * ASTNodeJSON representation of `ObjectType`
1129
1146
  */
@@ -1165,7 +1182,9 @@ declare class ObjectType extends BaseType<ObjectJSON> {
1165
1182
  * Serialize the `ObjectType` to `ObjectJSON`.
1166
1183
  * @returns The JSON representation of `ObjectType`.
1167
1184
  */
1168
- toJSON(): ASTNodeJSON;
1185
+ toJSON(): {
1186
+ properties: BaseVariableFieldJSON<any>[];
1187
+ };
1169
1188
  /**
1170
1189
  * Get a variable field by key path.
1171
1190
  * @param keyPath The key path to search for.
@@ -1233,6 +1252,9 @@ declare class CustomType extends BaseType<CustomTypeJSON> {
1233
1252
  * @returns `true` if the types are equal, `false` otherwise.
1234
1253
  */
1235
1254
  isTypeEqual(targetTypeJSONOrKind?: ASTNodeJSONOrKind): boolean;
1255
+ toJSON(): {
1256
+ typeName: string;
1257
+ };
1236
1258
  }
1237
1259
 
1238
1260
  /**
@@ -1246,7 +1268,7 @@ type ExpressionRefs = (BaseVariableField | undefined)[];
1246
1268
  *
1247
1269
  * All other expressions should extend this class.
1248
1270
  */
1249
- declare abstract class BaseExpression<JSON extends ASTNodeJSON = any, InjectOpts = any> extends ASTNode<JSON, InjectOpts> {
1271
+ declare abstract class BaseExpression<JSON extends ASTNodeJSON = any> extends ASTNode<JSON> {
1250
1272
  flags: ASTNodeFlags;
1251
1273
  /**
1252
1274
  * Get the global variable table, which is used to access referenced variables.
@@ -1284,7 +1306,7 @@ declare abstract class BaseExpression<JSON extends ASTNodeJSON = any, InjectOpts
1284
1306
  * An observable that emits the referenced variable fields when they change.
1285
1307
  */
1286
1308
  refs$: Observable<ExpressionRefs>;
1287
- constructor(params: CreateASTParams, opts?: InjectOpts);
1309
+ constructor(params: CreateASTParams, opts?: any);
1288
1310
  }
1289
1311
 
1290
1312
  /**
@@ -1310,7 +1332,7 @@ declare class EnumerateExpression extends BaseExpression<EnumerateExpressionJSON
1310
1332
  /**
1311
1333
  * The expression to be enumerated.
1312
1334
  */
1313
- get enumerateFor(): BaseExpression<any, any> | undefined;
1335
+ get enumerateFor(): BaseExpression<any> | undefined;
1314
1336
  /**
1315
1337
  * The return type of the expression.
1316
1338
  */
@@ -1329,7 +1351,10 @@ declare class EnumerateExpression extends BaseExpression<EnumerateExpressionJSON
1329
1351
  * Serialize the `EnumerateExpression` to `EnumerateExpressionJSON`.
1330
1352
  * @returns The JSON representation of `EnumerateExpression`.
1331
1353
  */
1332
- toJSON(): ASTNodeJSON;
1354
+ toJSON(): {
1355
+ kind: ASTKind;
1356
+ enumerateFor: any;
1357
+ };
1333
1358
  }
1334
1359
 
1335
1360
  /**
@@ -1356,6 +1381,7 @@ interface KeyPathExpressionJSON$1 {
1356
1381
  declare class KeyPathExpression<CustomPathJSON extends ASTNodeJSON = KeyPathExpressionJSON$1> extends BaseExpression<CustomPathJSON> {
1357
1382
  static kind: string;
1358
1383
  protected _keyPath: string[];
1384
+ protected _rawPathJson: CustomPathJSON;
1359
1385
  /**
1360
1386
  * The key path of the variable.
1361
1387
  */
@@ -1374,7 +1400,7 @@ declare class KeyPathExpression<CustomPathJSON extends ASTNodeJSON = KeyPathExpr
1374
1400
  /**
1375
1401
  * The return type of the expression.
1376
1402
  */
1377
- get returnType(): BaseType<any, any>;
1403
+ get returnType(): BaseType<any>;
1378
1404
  /**
1379
1405
  * Parse the business-defined path expression into a key path.
1380
1406
  *
@@ -1394,13 +1420,12 @@ declare class KeyPathExpression<CustomPathJSON extends ASTNodeJSON = KeyPathExpr
1394
1420
  * @returns The JSON representation of the return type.
1395
1421
  */
1396
1422
  getReturnTypeJSONByRef(_ref: BaseVariableField | undefined): ASTNodeJSON | undefined;
1397
- protected prevRefTypeHash: string | undefined;
1398
1423
  constructor(params: CreateASTParams, opts: any);
1399
1424
  /**
1400
1425
  * Serialize the `KeyPathExpression` to `KeyPathExpressionJSON`.
1401
1426
  * @returns The JSON representation of `KeyPathExpression`.
1402
1427
  */
1403
- toJSON(): ASTNodeJSON;
1428
+ toJSON(): CustomPathJSON;
1404
1429
  }
1405
1430
 
1406
1431
  /**
@@ -1424,6 +1449,7 @@ interface KeyPathExpressionJSON {
1424
1449
  declare class LegacyKeyPathExpression<CustomPathJSON extends ASTNodeJSON = KeyPathExpressionJSON> extends BaseExpression<CustomPathJSON> {
1425
1450
  static kind: string;
1426
1451
  protected _keyPath: string[];
1452
+ protected _rawPathJson: CustomPathJSON;
1427
1453
  /**
1428
1454
  * The key path of the variable.
1429
1455
  */
@@ -1455,7 +1481,7 @@ declare class LegacyKeyPathExpression<CustomPathJSON extends ASTNodeJSON = KeyPa
1455
1481
  * Serialize the `KeyPathExpression` to `KeyPathExpressionJSON`.
1456
1482
  * @returns The JSON representation of `KeyPathExpression`.
1457
1483
  */
1458
- toJSON(): ASTNodeJSON;
1484
+ toJSON(): CustomPathJSON;
1459
1485
  }
1460
1486
 
1461
1487
  /**
@@ -1482,7 +1508,7 @@ declare class WrapArrayExpression extends BaseExpression<WrapArrayExpressionJSON
1482
1508
  /**
1483
1509
  * The expression to be wrapped.
1484
1510
  */
1485
- get wrapFor(): BaseExpression<any, any> | undefined;
1511
+ get wrapFor(): BaseExpression<any> | undefined;
1486
1512
  /**
1487
1513
  * The return type of the expression.
1488
1514
  */
@@ -1505,20 +1531,23 @@ declare class WrapArrayExpression extends BaseExpression<WrapArrayExpressionJSON
1505
1531
  * Serialize the `WrapArrayExpression` to `WrapArrayExpressionJSON`.
1506
1532
  * @returns The JSON representation of `WrapArrayExpression`.
1507
1533
  */
1508
- toJSON(): ASTNodeJSON;
1534
+ toJSON(): {
1535
+ kind: ASTKind;
1536
+ wrapFor: any;
1537
+ };
1509
1538
  protected init(): void;
1510
1539
  }
1511
1540
 
1512
1541
  /**
1513
1542
  * ASTNodeJSON representation of `BaseVariableField`
1514
1543
  */
1515
- type BaseVariableFieldJSON<VariableMeta = any> = {
1544
+ interface BaseVariableFieldJSON<VariableMeta = any> extends ASTNodeJSON {
1516
1545
  /**
1517
1546
  * key of the variable field
1518
1547
  * - For `VariableDeclaration`, the key should be global unique.
1519
1548
  * - For `Property`, the key is the property name.
1520
1549
  */
1521
- key?: Identifier;
1550
+ key: Identifier;
1522
1551
  /**
1523
1552
  * type of the variable field, similar to js code:
1524
1553
  * `const v: string`
@@ -1535,7 +1564,7 @@ type BaseVariableFieldJSON<VariableMeta = any> = {
1535
1564
  * meta data of the variable field, you cans store information like `title`, `icon`, etc.
1536
1565
  */
1537
1566
  meta?: VariableMeta;
1538
- };
1567
+ }
1539
1568
  /**
1540
1569
  * Variable Field abstract class, which is the base class for `VariableDeclaration` and `Property`
1541
1570
  *
@@ -1579,7 +1608,7 @@ declare abstract class BaseVariableField<VariableMeta = any> extends ASTNode<Bas
1579
1608
  * Deserialize the `BaseVariableFieldJSON` to the `BaseVariableField`.
1580
1609
  * @param json ASTJSON representation of `BaseVariableField`
1581
1610
  */
1582
- fromJSON({ type, initializer, meta }: BaseVariableFieldJSON<VariableMeta>): void;
1611
+ fromJSON({ type, initializer, meta }: Omit<BaseVariableFieldJSON<VariableMeta>, 'key'>): void;
1583
1612
  /**
1584
1613
  * Update the type of the variable field
1585
1614
  * @param type type ASTJSON representation of Type
@@ -1612,9 +1641,7 @@ declare abstract class BaseVariableField<VariableMeta = any> extends ASTNode<Bas
1612
1641
  * Serialize the variable field to JSON
1613
1642
  * @returns ASTNodeJSON representation of `BaseVariableField`
1614
1643
  */
1615
- toJSON(): BaseVariableFieldJSON<VariableMeta> & {
1616
- kind: string;
1617
- };
1644
+ toJSON(): BaseVariableFieldJSON<VariableMeta>;
1618
1645
  }
1619
1646
 
1620
1647
  /**
@@ -1645,12 +1672,17 @@ declare class VariableDeclaration<VariableMeta = any> extends BaseVariableField<
1645
1672
  /**
1646
1673
  * Deserialize the `VariableDeclarationJSON` to the `VariableDeclaration`.
1647
1674
  */
1648
- fromJSON({ order, ...rest }: VariableDeclarationJSON<VariableMeta>): void;
1675
+ fromJSON({ order, ...rest }: Omit<VariableDeclarationJSON<VariableMeta>, 'key'>): void;
1649
1676
  /**
1650
1677
  * Update the sorting order of the variable declaration.
1651
1678
  * @param order Variable sorting order. Default is 0.
1652
1679
  */
1653
1680
  updateOrder(order?: number): void;
1681
+ /**
1682
+ * Serialize the `VariableDeclaration` to `VariableDeclarationJSON`.
1683
+ * @returns The JSON representation of `VariableDeclaration`.
1684
+ */
1685
+ toJSON(): VariableDeclarationJSON<VariableMeta>;
1654
1686
  }
1655
1687
 
1656
1688
  /**
@@ -1694,7 +1726,10 @@ declare class VariableDeclarationList extends ASTNode<VariableDeclarationListJSO
1694
1726
  * Serialize the `VariableDeclarationList` to the `VariableDeclarationListJSON`.
1695
1727
  * @returns ASTJSON representation of `VariableDeclarationList`
1696
1728
  */
1697
- toJSON(): ASTNodeJSON;
1729
+ toJSON(): {
1730
+ kind: ASTKind;
1731
+ declarations: VariableDeclarationJSON<any>[];
1732
+ };
1698
1733
  }
1699
1734
 
1700
1735
  /**
@@ -1772,12 +1807,12 @@ declare namespace ASTFactory {
1772
1807
  * Creates a `VariableDeclaration` node.
1773
1808
  */
1774
1809
  const createVariableDeclaration: <VariableMeta = any>(json: VariableDeclarationJSON<VariableMeta>) => {
1775
- key?: Identifier;
1810
+ key: Identifier;
1776
1811
  type?: ASTNodeJSONOrKind;
1777
1812
  initializer?: ASTNodeJSON;
1778
1813
  meta?: VariableMeta | undefined;
1814
+ kind: ASTKindType;
1779
1815
  order?: number;
1780
- kind: ASTKind;
1781
1816
  };
1782
1817
  /**
1783
1818
  * Creates a `Property` node.
@@ -1787,7 +1822,7 @@ declare namespace ASTFactory {
1787
1822
  type?: ASTNodeJSONOrKind;
1788
1823
  initializer?: ASTNodeJSON;
1789
1824
  meta?: VariableMeta | undefined;
1790
- kind: ASTKind;
1825
+ kind: ASTKindType;
1791
1826
  };
1792
1827
  /**
1793
1828
  * Creates a `VariableDeclarationList` node.
@@ -2152,12 +2187,12 @@ declare const ScopeProvider: (props: React.PropsWithChildren<{
2152
2187
  * useCurrentScope returns the scope provided by ScopeProvider.
2153
2188
  * @returns
2154
2189
  */
2155
- declare const useCurrentScope: (params?: {
2190
+ declare const useCurrentScope: <Strict extends boolean = false>(params?: {
2156
2191
  /**
2157
2192
  * whether to throw error when no scope in ScopeProvider is found
2158
2193
  */
2159
- strict?: boolean;
2160
- }) => Scope;
2194
+ strict: Strict;
2195
+ }) => Strict extends true ? Scope : Scope | undefined;
2161
2196
 
2162
2197
  /**
2163
2198
  * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates