@garrix82/reactgenie-dsl 1.0.1 → 1.0.2

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.
Files changed (104) hide show
  1. package/.env +10 -0
  2. package/.env.example +17 -0
  3. package/.github/workflows/publish.yml +20 -0
  4. package/package.json +1 -5
  5. package/dist/__test__/dsl-descriptor.test.d.ts +0 -1
  6. package/dist/__test__/dsl-descriptor.test.js +0 -27
  7. package/dist/__test__/dsl-descriptor.test.js.map +0 -1
  8. package/dist/__test__/example_descriptor.d.ts +0 -125
  9. package/dist/__test__/example_descriptor.js +0 -607
  10. package/dist/__test__/example_descriptor.js.map +0 -1
  11. package/dist/__test__/food_descriptor.state.json +0 -1
  12. package/dist/__test__/food_descriptor.test.d.ts +0 -74
  13. package/dist/__test__/food_descriptor.test.js +0 -205
  14. package/dist/__test__/food_descriptor.test.js.map +0 -1
  15. package/dist/__test__/nl-interpreter-provider-selection.test.d.ts +0 -1
  16. package/dist/__test__/nl-interpreter-provider-selection.test.js +0 -73
  17. package/dist/__test__/nl-interpreter-provider-selection.test.js.map +0 -1
  18. package/dist/__test__/nl-interpreter.test.d.ts +0 -1
  19. package/dist/__test__/nl-interpreter.test.js +0 -86
  20. package/dist/__test__/nl-interpreter.test.js.map +0 -1
  21. package/dist/decorators/__test__/decorators.test.d.ts +0 -1
  22. package/dist/decorators/__test__/decorators.test.js +0 -182
  23. package/dist/decorators/__test__/decorators.test.js.map +0 -1
  24. package/dist/decorators/__test__/inheritance-descriptor.test.d.ts +0 -1
  25. package/dist/decorators/__test__/inheritance-descriptor.test.js +0 -107
  26. package/dist/decorators/__test__/inheritance-descriptor.test.js.map +0 -1
  27. package/dist/dsl/__test__/dsl-interpreter.test.d.ts +0 -1
  28. package/dist/dsl/__test__/dsl-interpreter.test.js +0 -334
  29. package/dist/dsl/__test__/dsl-interpreter.test.js.map +0 -1
  30. package/dist/dsl/__test__/parser.gen.test.d.ts +0 -1
  31. package/dist/dsl/__test__/parser.gen.test.js +0 -283
  32. package/dist/dsl/__test__/parser.gen.test.js.map +0 -1
  33. package/dist/nl/__test__/context-aware-prompt.test.d.ts +0 -1
  34. package/dist/nl/__test__/context-aware-prompt.test.js +0 -247
  35. package/dist/nl/__test__/context-aware-prompt.test.js.map +0 -1
  36. package/dist/nl/__test__/context-selector.test.d.ts +0 -1
  37. package/dist/nl/__test__/context-selector.test.js +0 -20
  38. package/dist/nl/__test__/context-selector.test.js.map +0 -1
  39. package/dist/nl/__test__/nl-parser-groq-transport.test.d.ts +0 -1
  40. package/dist/nl/__test__/nl-parser-groq-transport.test.js +0 -87
  41. package/dist/nl/__test__/nl-parser-groq-transport.test.js.map +0 -1
  42. package/dist/nl/__test__/nl-parser-openai-parity.test.d.ts +0 -1
  43. package/dist/nl/__test__/nl-parser-openai-parity.test.js +0 -206
  44. package/dist/nl/__test__/nl-parser-openai-parity.test.js.map +0 -1
  45. package/dist/nl/__test__/nl-parser-openai-sampling.test.d.ts +0 -1
  46. package/dist/nl/__test__/nl-parser-openai-sampling.test.js +0 -44
  47. package/dist/nl/__test__/nl-parser-openai-sampling.test.js.map +0 -1
  48. package/dist/nl/__test__/nl-parser-openai-transport.test.d.ts +0 -1
  49. package/dist/nl/__test__/nl-parser-openai-transport.test.js +0 -55
  50. package/dist/nl/__test__/nl-parser-openai-transport.test.js.map +0 -1
  51. package/dist/nl/__test__/nl-parser-utils.test.d.ts +0 -1
  52. package/dist/nl/__test__/nl-parser-utils.test.js +0 -70
  53. package/dist/nl/__test__/nl-parser-utils.test.js.map +0 -1
  54. package/dist/nl/__test__/nl-parser.test.d.ts +0 -1
  55. package/dist/nl/__test__/nl-parser.test.js +0 -64
  56. package/dist/nl/__test__/nl-parser.test.js.map +0 -1
  57. package/dist/nl/__test__/parameter-tuning.test.d.ts +0 -1
  58. package/dist/nl/__test__/parameter-tuning.test.js +0 -95
  59. package/dist/nl/__test__/parameter-tuning.test.js.map +0 -1
  60. package/dist/nl/__test__/semantic-parsing-experiment.test.d.ts +0 -1
  61. package/dist/nl/__test__/semantic-parsing-experiment.test.js +0 -178
  62. package/dist/nl/__test__/semantic-parsing-experiment.test.js.map +0 -1
  63. package/dist/nl/llm-monitoring.test.d.ts +0 -5
  64. package/dist/nl/llm-monitoring.test.js +0 -101
  65. package/dist/nl/llm-monitoring.test.js.map +0 -1
  66. package/lib/__test__/dsl-descriptor.test.ts +0 -27
  67. package/lib/__test__/example_descriptor.ts +0 -762
  68. package/lib/__test__/food_descriptor.state.json +0 -1
  69. package/lib/__test__/food_descriptor.test.ts +0 -331
  70. package/lib/__test__/nl-interpreter-provider-selection.test.ts +0 -126
  71. package/lib/__test__/nl-interpreter.test.ts +0 -129
  72. package/lib/decorators/__test__/decorators.test.ts +0 -177
  73. package/lib/decorators/__test__/inheritance-descriptor.test.ts +0 -92
  74. package/lib/decorators/decorators.ts +0 -754
  75. package/lib/decorators/index.ts +0 -2
  76. package/lib/decorators/store.ts +0 -47
  77. package/lib/dsl/__test__/dsl-interpreter.test.ts +0 -453
  78. package/lib/dsl/__test__/parser.gen.test.ts +0 -296
  79. package/lib/dsl/dsl-interpreter.ts +0 -974
  80. package/lib/dsl/index.ts +0 -1
  81. package/lib/dsl/parser.gen.js +0 -1479
  82. package/lib/dsl/parser.pegjs +0 -130
  83. package/lib/dsl-descriptor.ts +0 -241
  84. package/lib/index.ts +0 -5
  85. package/lib/nl/__test__/context-aware-prompt.test.ts +0 -372
  86. package/lib/nl/__test__/context-selector.test.ts +0 -27
  87. package/lib/nl/__test__/nl-parser-groq-transport.test.ts +0 -139
  88. package/lib/nl/__test__/nl-parser-openai-parity.test.ts +0 -381
  89. package/lib/nl/__test__/nl-parser-openai-sampling.test.ts +0 -73
  90. package/lib/nl/__test__/nl-parser-openai-transport.test.ts +0 -79
  91. package/lib/nl/__test__/nl-parser-utils.test.ts +0 -98
  92. package/lib/nl/__test__/nl-parser.test.ts +0 -119
  93. package/lib/nl/__test__/parameter-tuning.test.ts +0 -137
  94. package/lib/nl/__test__/semantic-parsing-experiment.test.ts +0 -260
  95. package/lib/nl/context-selector.ts +0 -123
  96. package/lib/nl/index.ts +0 -19
  97. package/lib/nl/llm-monitoring.test.ts +0 -136
  98. package/lib/nl/llm-monitoring.ts +0 -339
  99. package/lib/nl/nl-parser-groq.ts +0 -510
  100. package/lib/nl/nl-parser-utils.ts +0 -310
  101. package/lib/nl/nl-parser.ts +0 -616
  102. package/lib/nl/prompt-gen.ts +0 -607
  103. package/lib/nl/prompt-res.ts +0 -207
  104. package/lib/nl-interpreter.ts +0 -262
@@ -1,754 +0,0 @@
1
- import "reflect-metadata";
2
- import {
3
- ClassDescriptor,
4
- FieldDescriptor,
5
- FuncDescriptor,
6
- DataClass,
7
- ParamDescriptor,
8
- HelperClass,
9
- HelperClassGetterSetter, GenieObject,
10
- } from "../dsl-descriptor";
11
- import { Store } from "redux";
12
- import { configureStore } from "@reduxjs/toolkit";
13
- import {
14
- genieDispatch,
15
- objects,
16
- setSharedState,
17
- setSharedStore,
18
- sharedState,
19
- storeReducer,
20
- } from "./store";
21
-
22
- export type int = number;
23
- export type float = number;
24
-
25
- export const AllGenieObjects: { [key: string]: any } = {};
26
-
27
- export type GenieClassModifier = (target: any) => void;
28
- export type GenieFunctionModifier = (
29
- target: any,
30
- functionKey: string,
31
- isStatic: boolean
32
- ) => void;
33
- export type GeniePropertyModifier = (
34
- target: any,
35
- propertyKey: string,
36
- isStatic: boolean
37
- ) => void;
38
-
39
- let genieClassModifier: GenieClassModifier = undefined;
40
- let genieFunctionModifier: GenieFunctionModifier = undefined;
41
- let geniePropertyModifier: GeniePropertyModifier = undefined;
42
-
43
- let genieInitialized = false;
44
-
45
- function hasOwn(target: any, key: string): boolean {
46
- return Object.prototype.hasOwnProperty.call(target, key);
47
- }
48
-
49
- function uniqueFuncDescriptors(descriptors: FuncDescriptor[]): FuncDescriptor[] {
50
- const unique: FuncDescriptor[] = [];
51
- descriptors.forEach((descriptor) => {
52
- if (!unique.some((candidate) => candidate.isSame(descriptor))) {
53
- unique.push(descriptor);
54
- }
55
- });
56
- return unique;
57
- }
58
-
59
- function uniqueFieldDescriptors(descriptors: FieldDescriptor[]): FieldDescriptor[] {
60
- const unique: FieldDescriptor[] = [];
61
- descriptors.forEach((descriptor) => {
62
- if (!unique.some((candidate) => candidate.isSame(descriptor))) {
63
- unique.push(descriptor);
64
- }
65
- });
66
- return unique;
67
- }
68
-
69
- export function initGenie({
70
- initGenieClassModifier,
71
- initGenieFunctionModifier,
72
- initGeniePropertyModifier,
73
- }: {
74
- initGenieClassModifier?: GenieClassModifier;
75
- initGenieFunctionModifier?: GenieFunctionModifier;
76
- initGeniePropertyModifier?: GeniePropertyModifier;
77
- } = {}): Store {
78
- if (initGenieClassModifier) {
79
- genieClassModifier = initGenieClassModifier;
80
- }
81
- if (initGenieFunctionModifier) {
82
- genieFunctionModifier = initGenieFunctionModifier;
83
- }
84
- if (initGeniePropertyModifier) {
85
- geniePropertyModifier = initGeniePropertyModifier;
86
- }
87
- let genieStore = configureStore({ reducer: storeReducer });
88
- setSharedStore(genieStore);
89
- // call modifiers for existing classes
90
- for (let className in AllGenieObjects) {
91
- const target = AllGenieObjects[className];
92
- if (genieClassModifier) {
93
- genieClassModifier(target);
94
- }
95
- // for all properties
96
- for (let propertyDescriptor of target.ClassDescriptor.fields) {
97
- if (geniePropertyModifier) {
98
- geniePropertyModifier(
99
- target,
100
- propertyDescriptor.field,
101
- propertyDescriptor.isStatic
102
- );
103
- }
104
- }
105
- // for all functions
106
- for (let functionDescriptor of target.ClassDescriptor.functions) {
107
- if (genieFunctionModifier) {
108
- genieFunctionModifier(
109
- target,
110
- functionDescriptor.func_name,
111
- functionDescriptor.isStatic
112
- );
113
- }
114
- }
115
- }
116
- // call setup for existing classes
117
- for (let className in AllGenieObjects) {
118
- const target = AllGenieObjects[className];
119
- target.setup();
120
- }
121
- genieInitialized = true;
122
- return genieStore;
123
- }
124
-
125
- export type LazyType<T> = T;
126
-
127
- function getJsonByPath(json: any, path: (string | number)[]): any {
128
- let current = json;
129
- for (let pathElement of path) {
130
- current = current[pathElement];
131
- }
132
- return current;
133
- }
134
-
135
- function setJsonByPath(json: any, path: (string | number)[], value: any): any {
136
- if (path.length === 0) {
137
- return value;
138
- } else {
139
- if (Array.isArray(json)) {
140
- const array = json as any[];
141
- const index = path[0] as number;
142
- return [
143
- ...array.slice(0, index),
144
- setJsonByPath(array[index], path.slice(1), value),
145
- ...array.slice(index + 1),
146
- ];
147
- } else {
148
- return {
149
- ...json,
150
- [path[0]]: setJsonByPath(json[path[0]], path.slice(1), value),
151
- };
152
- }
153
- }
154
- }
155
-
156
- class ObservableArray<T> extends Array<T> {
157
- private callback: () => void;
158
-
159
- constructor(callback: (array: ObservableArray<T>) => void, ...items: T[]) {
160
- super(...items);
161
- this.callback = () => callback(this);
162
- }
163
-
164
- push(...items: T[]): number {
165
- const result = super.push(...items);
166
- this.callback();
167
- return result;
168
- }
169
-
170
- pop(): T {
171
- const result = super.pop();
172
- this.callback();
173
- return result;
174
- }
175
-
176
- splice(start: number, deleteCount?: number, ...items: T[]): T[] {
177
- const result = super.splice(start, deleteCount, ...items);
178
- this.callback();
179
- return result;
180
- }
181
-
182
- shift(): T {
183
- const result = super.shift();
184
- this.callback();
185
- return result;
186
- }
187
-
188
- unshift(...items: T[]): number {
189
- const result = super.unshift(...items);
190
- this.callback();
191
- return result;
192
- }
193
-
194
- set(index: number, item: T): void {
195
- this[index] = item;
196
- this.callback();
197
- }
198
-
199
- map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[] {
200
- return [...super.map(callbackfn, thisArg)];
201
- }
202
- }
203
-
204
- const serializeField =
205
- (
206
- generateGetterSetter: (path: (string | number)[]) => HelperClassGetterSetter
207
- ) =>
208
- (value: any, path: (string | number)[] = []) => {
209
- // if field is array
210
- if (Array.isArray(value)) {
211
- return value.map((item, index) => {
212
- return serializeField(generateGetterSetter)(item, path.concat(index));
213
- });
214
- } else if (
215
- value != null &&
216
- value.constructor.__genieObjectType == "DataClass"
217
- ) {
218
- return {
219
- __genieObjectType: "DataClass",
220
- __genieObjectClass: value.constructor.name,
221
- __genieObjectKey: value[value.genieKey],
222
- };
223
- } else if (
224
- value != null &&
225
- value.constructor.__genieObjectType == "HelperClass"
226
- ) {
227
- (value as HelperClass).localStoreGetterSetter =
228
- generateGetterSetter(path);
229
- let serializedValue = (value as HelperClass).localStore;
230
- if (serializedValue == null) {
231
- throw new Error(
232
- "HelperClass is not initialized.\n" +
233
- "Please make sure to use CreateObject() to create HelperClass instance."
234
- );
235
- }
236
- serializedValue["__genieObjectType"] = "HelperClass";
237
- serializedValue["__genieObjectClass"] = value.constructor.name;
238
-
239
- // console.log(typeof serializedValue["__genieObjectClass"]);
240
- return serializedValue;
241
- }
242
- return value;
243
- };
244
-
245
- const deserializeField =
246
- (
247
- generateGetterSetter: (path: (string | number)[]) => HelperClassGetterSetter
248
- ) =>
249
- (value: any, path: (string | number)[] = []) => {
250
- // if field is array
251
- if (Array.isArray(value)) {
252
- return new ObservableArray(
253
- (newArray) => {
254
- generateGetterSetter(path)[1](
255
- serializeField(generateGetterSetter)(newArray, path)
256
- );
257
- },
258
- ...value.map((item, index) => {
259
- return deserializeField(generateGetterSetter)(
260
- item,
261
- path.concat(index)
262
- );
263
- })
264
- );
265
- } else if (value != null && value.__genieObjectType == "DataClass") {
266
- return objects[value.__genieObjectClass][value.__genieObjectKey];
267
- } else if (value != null && value.__genieObjectType == "HelperClass") {
268
- let obj = AllGenieObjects[value.__genieObjectClass] as (typeof HelperClass);
269
- let deserializedValue = obj.CreateObject(value);
270
- deserializedValue.localStoreGetterSetter = generateGetterSetter(path);
271
- return deserializedValue;
272
- }
273
- return value;
274
- };
275
-
276
- export function GenieClass(comment: string = "") {
277
- return function (target: any) {
278
- // console.debug("GenieClass decorator called on " + target.name);
279
-
280
- target.prototype.comment = comment;
281
- if (!hasOwn(target, "ClassDescriptor")) {
282
- const inheritedClass = Object.getPrototypeOf(target);
283
- const inheritedDescriptor = inheritedClass?.ClassDescriptor as
284
- | ClassDescriptor<GenieObject>
285
- | undefined;
286
- const inheritedFunctions = inheritedDescriptor
287
- ? Array.from(inheritedDescriptor.functions)
288
- : [];
289
- const inheritedFields = inheritedDescriptor
290
- ? Array.from(inheritedDescriptor.fields)
291
- : [];
292
- const ownFunctions = hasOwn(target, "__class_descriptor_functions")
293
- ? target.__class_descriptor_functions
294
- : [];
295
- const ownFields = hasOwn(target, "__class_descriptor_properties")
296
- ? target.__class_descriptor_properties
297
- : [];
298
-
299
- target.ClassDescriptor = new ClassDescriptor(
300
- target.name,
301
- uniqueFuncDescriptors([...inheritedFunctions, ...ownFunctions]),
302
- uniqueFieldDescriptors([...inheritedFields, ...ownFields]),
303
- target,
304
- comment
305
- );
306
- }
307
-
308
- // if target extends DataClass
309
- if (target.prototype instanceof DataClass) {
310
- target._createObject = function (...args: any[]) {
311
- const obj = new target(...args);
312
-
313
- const generateGetterSetter = function (
314
- path: (string | number)[]
315
- ): HelperClassGetterSetter {
316
- return [
317
- () => getJsonByPath(sharedState["OBJECT"][target.name][obj[keyField]], path),
318
- (value) => {
319
- genieDispatch(() => {
320
- setSharedState({
321
- ...sharedState,
322
- ["OBJECT"]: {
323
- ...sharedState["OBJECT"],
324
- [target.name]: {
325
- ...sharedState["OBJECT"][target.name],
326
- [obj[keyField]]: setJsonByPath(
327
- sharedState["OBJECT"][target.name][obj[keyField]],
328
- path,
329
- value
330
- ),
331
- },
332
- }
333
- });
334
- });
335
- },
336
- ];
337
- };
338
-
339
- // find all fields not starting with _
340
- let allFields = Object.getOwnPropertyNames(obj).filter((name) => name[0] !== "_");
341
- // filter out functions
342
- allFields = allFields.filter((field) => {
343
- return typeof obj[field] !== "function";
344
- });
345
- let keyField = obj.genieKey;
346
- // check if keyField is in allFields
347
- if (allFields.indexOf(keyField) === -1) {
348
- throw new Error(
349
- "keyField " + keyField + " not found in class " + target.name
350
- );
351
- }
352
- // check if keyField exists
353
- if (obj[keyField]=== null || obj[keyField]=== undefined) {
354
- throw new Error(
355
- "keyField " +
356
- keyField +
357
- " not found in object " +
358
- obj.constructor.name +
359
- "\n" +
360
- "Did you have @GenieKey on the key field?\n" +
361
- "Did set the key field in the constructor?"
362
- );
363
- }
364
- // save data to store
365
- genieDispatch(() => {
366
- let objectState = {};
367
- if (sharedState["OBJECT"][target.name]) {
368
- objectState = {
369
- ...sharedState["OBJECT"][target.name],
370
- };
371
- }
372
- // overwrite existing object
373
- // if (sharedState[target.name][obj[keyField]]) {
374
- // throw new Error("object with key " + obj[keyField] + " already exists in store");
375
- // }
376
- objectState[obj[keyField]] = {};
377
- // save all fields
378
- allFields.forEach((field) => {
379
- // console.log(field, obj[field])
380
- objectState[obj[keyField]][field] = serializeField(
381
- generateGetterSetter
382
- )(obj[field], [field]);
383
- });
384
- setSharedState({
385
- ...sharedState,
386
- ["OBJECT"]: {
387
- ...sharedState["OBJECT"],
388
- [target.name]: objectState,
389
- }
390
- });
391
- });
392
- // replace fields with getters and setters
393
- allFields.forEach((field) => {
394
- if (field == keyField) {
395
- return;
396
- }
397
- Object.defineProperty(obj, field, {
398
- get: function () {
399
- let value = sharedState["OBJECT"][target.name][obj[keyField]][field];
400
- return deserializeField(generateGetterSetter)(value, [field]);
401
- },
402
- set: function (value) {
403
- genieDispatch(() => {
404
- setSharedState({
405
- ...sharedState,
406
- ["OBJECT"]: {
407
- ...sharedState["OBJECT"],
408
- [target.name]: {
409
- ...sharedState["OBJECT"][target.name],
410
- [obj[keyField]]: {
411
- ...sharedState["OBJECT"][target.name][obj[keyField]],
412
- [field]: serializeField(generateGetterSetter)(value, [
413
- field,
414
- ]),
415
- },
416
- },
417
- },
418
- });
419
- });
420
- },
421
- });
422
- });
423
- if (!objects[target.name]) {
424
- objects[target.name] = {};
425
- }
426
- objects[target.name][obj[keyField]] = obj;
427
- obj.__getState = function () {
428
- return {
429
- "object": sharedState["OBJECT"][target.name][obj[keyField]],
430
- "class": sharedState["CLASS"][target.name]
431
- }
432
- }
433
- return obj;
434
- };
435
-
436
- // add a static method to get all objects of this type
437
- target.GetObject = function (key: {}) {
438
- const classObjects = objects[target.name];
439
- if (!classObjects) {
440
- return undefined;
441
- }
442
- return classObjects[key[target.prototype.genieKey]];
443
- };
444
-
445
- target.DeleteObject = function (key: {}) {
446
- genieDispatch(() => {
447
- const classObjects = objects[target.name];
448
- if (!classObjects) {
449
- return undefined;
450
- }
451
- let obj = classObjects[key[target.prototype.genieKey]];
452
- if (!obj) {
453
- return undefined;
454
- }
455
- obj.__deleted = true;
456
- return obj;
457
- })
458
- }
459
- } else if (target.prototype instanceof HelperClass) {
460
- target._createObject = function (...args: any[]){
461
- const obj = new target(...args);
462
-
463
- const generateGetterSetter = function (
464
- path: (string | number)[]
465
- ): HelperClassGetterSetter {
466
- return [
467
- () =>
468
- genieDispatch(() => {
469
- if (obj.localStoreGetterSetter != null) {
470
- obj.localStore = obj.localStoreGetterSetter[0]();
471
- }
472
- return getJsonByPath(obj.localStore, path);
473
- })
474
- ,
475
- (value) => {
476
- genieDispatch(() => {
477
- if (obj.localStoreGetterSetter != null) {
478
- obj.localStore = obj.localStoreGetterSetter[0]();
479
- }
480
- setJsonByPath(obj.localStore, path, value);
481
- if (obj.localStoreGetterSetter != null) {
482
- obj.localStoreGetterSetter[1](obj.localStore);
483
- }
484
- });
485
- },
486
- ];
487
- };
488
-
489
- // find all fields
490
- let allFields = Object.getOwnPropertyNames(obj).filter((name) => name[0] !== "_");
491
- // filter out functions
492
- allFields = allFields.filter((field) => {
493
- // console.log(field);
494
- return typeof obj[field] !== "function" && field !=="localStore" && field !=="localStoreGetterSetter";
495
- });
496
-
497
- (obj as HelperClass).localStore = {};
498
- // save all fields
499
- allFields.forEach((field) => {
500
- (obj as HelperClass).localStore[field] = serializeField(
501
- generateGetterSetter
502
- )(obj[field], [field]);
503
- });
504
- // console.log(obj.localStore)
505
-
506
- let targetObj = obj as HelperClass;//??
507
- // replace fields with getters and setters
508
- // console.log(allFields);
509
- allFields.forEach((field) => {
510
- // get field type
511
- Object.defineProperty(obj, field, {
512
- get: function () {
513
- if (targetObj.localStoreGetterSetter != null) {
514
- targetObj.localStore = targetObj.localStoreGetterSetter[0]();
515
- }
516
- let value = targetObj.localStore[field];
517
- return deserializeField(generateGetterSetter)(value, [field]);
518
- },
519
- set: function (value) {
520
- let serializedValue = serializeField(generateGetterSetter)(
521
- value,
522
- [field]
523
- );
524
- if (targetObj.localStoreGetterSetter != null) {
525
- targetObj.localStore = targetObj.localStoreGetterSetter[0]();
526
- }
527
- targetObj.localStore = {
528
- ...targetObj.localStore,
529
- [field]: serializedValue,
530
- };
531
- if (targetObj.localStoreGetterSetter != null) {
532
- targetObj.localStoreGetterSetter[1](targetObj.localStore);
533
- }
534
-
535
- },
536
- });
537
- });
538
- return obj;
539
- };
540
- }
541
-
542
- if (target.prototype instanceof DataClass) {
543
- // append method `current()` to the class
544
- /**
545
- * Retrieves all instances of GenieObject in this context
546
- * @returns {any} the GenieObject
547
- */
548
- function All() {
549
- let allObjects = [];
550
- const classObjects = objects[target.name];
551
- if (!classObjects) {
552
- return allObjects;
553
- }
554
- for (let key in classObjects) {
555
- if (classObjects[key].__deleted) {
556
- continue;
557
- }
558
- allObjects.push(classObjects[key]);
559
- }
560
- return allObjects;
561
- }
562
-
563
- if (target.All == DataClass.All) {
564
- target.All = All;
565
- // append additional function descriptor to class descriptor
566
- target.ClassDescriptor.functions.add(
567
- new FuncDescriptor(
568
- "All",
569
- [],
570
- target.ClassDescriptor.className + "[]",
571
- true
572
- )
573
- );
574
- }
575
- }
576
-
577
- if (genieClassModifier) {
578
- genieClassModifier(target);
579
- }
580
-
581
- AllGenieObjects[target.name] = target;
582
- if (genieInitialized) {
583
- (target as (typeof GenieObject)).setup();
584
- }
585
- return target;
586
- };
587
- }
588
-
589
- function classToName(object: any): string {
590
- if (object.constructor.name === "Object") {
591
- if (object["type"]) {
592
- if (object["type"].name !== "Array") {
593
- throw new Error("non-array type not supported");
594
- }
595
- if (object["elementType"]) {
596
- if (object["elementType"].name) {
597
- return object["elementType"].name + "[]";
598
- } else {
599
- return object["elementType"] + "[]";
600
- }
601
- } else {
602
- throw new Error("array elementType missing");
603
- }
604
- }
605
- } else if (object.constructor.name === "String") {
606
- return object;
607
- } else if (object === String) {
608
- return "string";
609
- } else if (object === Boolean) {
610
- return "bool";
611
- } else if (object.hasOwnProperty("name")) {
612
- return object.name;
613
- }
614
- throw new Error("classToName not implemented for " + object);
615
- }
616
-
617
- export function GenieFunction(comment: string = "") {
618
- return function (target: any, propertyKey: string) {
619
- const isStaticMeta = Reflect.getMetadata(
620
- "design:is_static",
621
- target,
622
- propertyKey
623
- );
624
- const isStatic = isStaticMeta === true;
625
- const targetClass = isStatic ? target : target.constructor;
626
- if (!hasOwn(targetClass, "ClassDescriptor")) {
627
- // console.debug(
628
- // "GenieFunction decorator called on " +
629
- // target.constructor.name +
630
- // "." +
631
- // propertyKey
632
- // );
633
- const paramTypes = Reflect.getMetadata(
634
- "design:paramtypes",
635
- target,
636
- propertyKey
637
- );
638
- const returnType = Reflect.getMetadata(
639
- "design:returntype",
640
- target,
641
- propertyKey
642
- );
643
- const destructuringParamTypes = Reflect.getMetadata(
644
- "design:destructuringparamtypes",
645
- target,
646
- propertyKey
647
- );
648
-
649
- const destructingParamValues = Reflect.getMetadata(
650
- "design:destructuringparamvalues",
651
- target,
652
- propertyKey
653
- );
654
-
655
- const funcName = propertyKey;
656
-
657
- if (returnType === undefined) {
658
- throw new Error(
659
- `Genie functions must have a return type: ${target.constructor.name}.${propertyKey}`
660
- );
661
- }
662
- const returnTypeStr = classToName(returnType);
663
-
664
- let parameters = [];
665
-
666
- if (paramTypes.length > 1) {
667
- throw new Error(
668
- "Genie functions can only have one destructuring parameter"
669
- );
670
- }
671
- if (paramTypes.length == 1) {
672
- // get the parameter names
673
- if (destructuringParamTypes.length !== 1 || destructingParamValues.length !== 1) {
674
- throw new Error(
675
- "deconstructed parameters should be the same number as the number of parameters"
676
- );
677
- }
678
- const paramTypeObj = destructuringParamTypes[0];
679
- const paramValueObj = destructingParamValues[0];
680
- parameters = Object.keys(paramTypeObj).map((paramName) => {
681
- const paramType = classToName(paramTypeObj[paramName].type);
682
- let paramValue = paramValueObj[paramName];
683
- if (paramValue === undefined || paramValue === "undefined") {
684
- paramValue = null;
685
- }
686
- const paramOptional = paramTypeObj[paramName].optional;
687
- return new ParamDescriptor(paramName, paramType, !paramOptional, paramValue);
688
- });
689
- }
690
-
691
- const funcDescriptor = new FuncDescriptor(
692
- funcName,
693
- parameters,
694
- returnTypeStr,
695
- isStatic,
696
- comment
697
- );
698
-
699
- if (!hasOwn(targetClass, "__class_descriptor_functions")) {
700
- targetClass.__class_descriptor_functions = [];
701
- }
702
- // console.debug(`pushing function descriptor ${funcDescriptor}`);
703
- targetClass.__class_descriptor_functions.push(funcDescriptor);
704
- if (genieFunctionModifier) {
705
- genieFunctionModifier(target, propertyKey, isStatic);
706
- }
707
- }
708
- };
709
- }
710
-
711
- export function GenieKey(target: any, propertyKey: string) {
712
- // console.debug(
713
- // "GenieKey decorator called on " +
714
- // target.constructor.name +
715
- // "." +
716
- // propertyKey
717
- // );
718
- target.genieKey = propertyKey;
719
- }
720
-
721
- export function GenieProperty(comment: string = "") {
722
- return function (target: any, propertyKey: string) {
723
- // console.debug(
724
- // "GenieProperty decorator called on " +
725
- // target.constructor.name +
726
- // "." +
727
- // propertyKey
728
- // );
729
- const typeObj = Reflect.getMetadata("design:type", target, propertyKey);
730
- const isStaticMeta = Reflect.getMetadata(
731
- "design:is_static",
732
- target,
733
- propertyKey
734
- );
735
- const isStatic = isStaticMeta === true;
736
- const type = classToName(typeObj);
737
-
738
- const targetClass = isStatic ? target : target.constructor;
739
- if (!hasOwn(targetClass, "__class_descriptor_properties")) {
740
- targetClass.__class_descriptor_properties = [];
741
- }
742
- const propertyDescriptor = new FieldDescriptor(
743
- propertyKey,
744
- type,
745
- isStatic,
746
- comment
747
- );
748
- // console.debug(`pushing property descriptor: ${propertyDescriptor}`);
749
- targetClass.__class_descriptor_properties.push(propertyDescriptor);
750
- if (geniePropertyModifier) {
751
- geniePropertyModifier(target, propertyKey, isStatic);
752
- }
753
- };
754
- }