lua-cli 2.2.8-alpha.2 → 2.3.0-alpha.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.
Files changed (193) hide show
  1. package/API_REFERENCE.md +1408 -0
  2. package/CLI_REFERENCE.md +818 -0
  3. package/GETTING_STARTED.md +1040 -0
  4. package/README.md +738 -424
  5. package/TEMPLATE_GUIDE.md +1398 -0
  6. package/dist/api/agent.api.service.d.ts +33 -6
  7. package/dist/api/agent.api.service.js +27 -0
  8. package/dist/api/auth.api.service.d.ts +31 -2
  9. package/dist/api/auth.api.service.js +29 -0
  10. package/dist/api/basket.api.service.d.ts +53 -11
  11. package/dist/api/basket.api.service.js +63 -14
  12. package/dist/api/chat.api.service.d.ts +15 -3
  13. package/dist/api/chat.api.service.js +12 -0
  14. package/dist/api/credentials.d.ts +24 -0
  15. package/dist/api/credentials.js +46 -0
  16. package/dist/api/custom.data.api.service.d.ts +45 -9
  17. package/dist/api/custom.data.api.service.js +43 -9
  18. package/dist/api/lazy-instances.d.ts +49 -0
  19. package/dist/api/lazy-instances.js +95 -0
  20. package/dist/api/order.api.service.d.ts +34 -4
  21. package/dist/api/order.api.service.js +41 -3
  22. package/dist/api/products.api.service.d.ts +39 -9
  23. package/dist/api/products.api.service.js +43 -5
  24. package/dist/api/skills.api.service.d.ts +49 -2
  25. package/dist/api/skills.api.service.js +47 -1
  26. package/dist/api/tool.api.service.d.ts +39 -1
  27. package/dist/api/tool.api.service.js +38 -0
  28. package/dist/api/user.data.api.service.d.ts +23 -1
  29. package/dist/api/user.data.api.service.js +22 -0
  30. package/dist/api-exports.d.ts +236 -5
  31. package/dist/api-exports.js +264 -81
  32. package/dist/cli/command-definitions.d.ts +30 -0
  33. package/dist/cli/command-definitions.js +71 -0
  34. package/dist/commands/agents.d.ts +20 -0
  35. package/dist/commands/agents.js +24 -2
  36. package/dist/commands/apiKey.d.ts +23 -0
  37. package/dist/commands/apiKey.js +23 -0
  38. package/dist/commands/compile.d.ts +24 -0
  39. package/dist/commands/compile.js +67 -759
  40. package/dist/commands/configure.d.ts +24 -0
  41. package/dist/commands/configure.js +31 -96
  42. package/dist/commands/deploy.d.ts +31 -19
  43. package/dist/commands/deploy.js +45 -74
  44. package/dist/commands/destroy.d.ts +27 -0
  45. package/dist/commands/destroy.js +27 -1
  46. package/dist/commands/dev.d.ts +25 -62
  47. package/dist/commands/dev.js +58 -878
  48. package/dist/commands/init.d.ts +27 -0
  49. package/dist/commands/init.js +98 -260
  50. package/dist/commands/push.d.ts +24 -21
  51. package/dist/commands/push.js +39 -92
  52. package/dist/commands/test.d.ts +26 -0
  53. package/dist/commands/test.js +41 -188
  54. package/dist/common/basket.instance.d.ts +54 -3
  55. package/dist/common/basket.instance.js +56 -3
  56. package/dist/common/data.entry.instance.d.ts +25 -2
  57. package/dist/common/data.entry.instance.js +24 -0
  58. package/dist/common/http.client.d.ts +51 -1
  59. package/dist/common/http.client.js +50 -0
  60. package/dist/common/order.instance.d.ts +22 -0
  61. package/dist/common/order.instance.js +31 -4
  62. package/dist/common/product.instance.d.ts +22 -1
  63. package/dist/common/product.instance.js +24 -6
  64. package/dist/common/product.pagination.instance.d.ts +22 -2
  65. package/dist/common/product.pagination.instance.js +22 -1
  66. package/dist/common/product.search.instance.d.ts +13 -3
  67. package/dist/common/product.search.instance.js +12 -1
  68. package/dist/common/user.instance.d.ts +27 -3
  69. package/dist/common/user.instance.js +28 -7
  70. package/dist/config/auth.constants.d.ts +11 -0
  71. package/dist/config/auth.constants.js +11 -0
  72. package/dist/config/compile.constants.d.ts +67 -0
  73. package/dist/config/compile.constants.js +99 -0
  74. package/dist/config/constants.d.ts +5 -0
  75. package/dist/config/constants.js +5 -0
  76. package/dist/config/dev.constants.d.ts +65 -0
  77. package/dist/config/dev.constants.js +79 -0
  78. package/dist/config/init.constants.d.ts +23 -0
  79. package/dist/config/init.constants.js +41 -0
  80. package/dist/index.d.ts +19 -3
  81. package/dist/index.js +28 -44
  82. package/dist/interfaces/admin.d.ts +56 -50
  83. package/dist/interfaces/admin.js +4 -0
  84. package/dist/interfaces/agent.d.ts +21 -0
  85. package/dist/interfaces/agent.js +4 -0
  86. package/dist/interfaces/baskets.d.ts +60 -0
  87. package/dist/interfaces/baskets.js +12 -0
  88. package/dist/interfaces/chat.d.ts +48 -4
  89. package/dist/interfaces/chat.js +4 -0
  90. package/dist/interfaces/common.d.ts +62 -0
  91. package/dist/interfaces/common.js +8 -0
  92. package/dist/interfaces/compile.d.ts +11 -0
  93. package/dist/interfaces/compile.js +4 -0
  94. package/dist/interfaces/custom.data.d.ts +49 -19
  95. package/dist/interfaces/custom.data.js +4 -0
  96. package/dist/interfaces/deploy.d.ts +29 -0
  97. package/dist/interfaces/deploy.js +4 -0
  98. package/dist/interfaces/dev.d.ts +53 -0
  99. package/dist/interfaces/dev.js +5 -0
  100. package/dist/interfaces/init.d.ts +60 -0
  101. package/dist/interfaces/init.js +4 -0
  102. package/dist/interfaces/orders.d.ts +37 -0
  103. package/dist/interfaces/orders.js +12 -0
  104. package/dist/interfaces/product.d.ts +38 -10
  105. package/dist/interfaces/product.js +4 -0
  106. package/dist/interfaces/push.d.ts +26 -0
  107. package/dist/interfaces/push.js +4 -0
  108. package/dist/interfaces/test.d.ts +36 -0
  109. package/dist/interfaces/test.js +4 -0
  110. package/dist/services/auth.d.ts +54 -99
  111. package/dist/services/auth.js +76 -12
  112. package/dist/types/api-contracts.d.ts +211 -0
  113. package/dist/types/api-contracts.js +8 -0
  114. package/dist/types/compile.types.d.ts +76 -0
  115. package/dist/types/compile.types.js +4 -0
  116. package/dist/types/index.d.ts +23 -121
  117. package/dist/types/index.js +25 -14
  118. package/dist/types/skill.d.ts +142 -0
  119. package/dist/{skill.js → types/skill.js} +66 -17
  120. package/dist/types/tool-validation.d.ts +34 -0
  121. package/dist/types/tool-validation.js +42 -0
  122. package/dist/utils/auth-flows.d.ts +26 -0
  123. package/dist/utils/auth-flows.js +141 -0
  124. package/dist/utils/bundling.d.ts +36 -0
  125. package/dist/utils/bundling.js +137 -0
  126. package/dist/utils/compile.d.ts +37 -0
  127. package/dist/utils/compile.js +242 -0
  128. package/dist/utils/deploy-api.d.ts +26 -0
  129. package/dist/utils/deploy-api.js +53 -0
  130. package/dist/utils/deploy-helpers.d.ts +46 -0
  131. package/dist/utils/deploy-helpers.js +86 -0
  132. package/dist/utils/deployment.d.ts +25 -0
  133. package/dist/utils/deployment.js +161 -0
  134. package/dist/utils/dev-api.d.ts +61 -0
  135. package/dist/utils/dev-api.js +262 -0
  136. package/dist/utils/dev-helpers.d.ts +46 -0
  137. package/dist/utils/dev-helpers.js +83 -0
  138. package/dist/utils/dev-server.d.ts +24 -0
  139. package/dist/utils/dev-server.js +555 -0
  140. package/dist/utils/dev-watcher.d.ts +31 -0
  141. package/dist/utils/dev-watcher.js +110 -0
  142. package/dist/utils/files.js +0 -5
  143. package/dist/utils/init-agent.d.ts +34 -0
  144. package/dist/utils/init-agent.js +129 -0
  145. package/dist/utils/init-helpers.d.ts +41 -0
  146. package/dist/utils/init-helpers.js +73 -0
  147. package/dist/utils/init-prompts.d.ts +47 -0
  148. package/dist/utils/init-prompts.js +168 -0
  149. package/dist/utils/push-api.d.ts +15 -0
  150. package/dist/utils/push-api.js +48 -0
  151. package/dist/utils/push-helpers.d.ts +38 -0
  152. package/dist/utils/push-helpers.js +84 -0
  153. package/dist/utils/sandbox-storage.d.ts +27 -0
  154. package/dist/utils/sandbox-storage.js +71 -0
  155. package/dist/utils/sandbox.js +78 -114
  156. package/dist/utils/skill-management.d.ts +14 -0
  157. package/dist/utils/skill-management.js +148 -0
  158. package/dist/utils/test-helpers.d.ts +40 -0
  159. package/dist/utils/test-helpers.js +92 -0
  160. package/dist/utils/test-prompts.d.ts +23 -0
  161. package/dist/utils/test-prompts.js +186 -0
  162. package/dist/utils/tool-detection.d.ts +18 -0
  163. package/dist/utils/tool-detection.js +110 -0
  164. package/dist/web/app.css +14 -9
  165. package/package.json +7 -4
  166. package/template/QUICKSTART.md +299 -144
  167. package/template/README.md +928 -349
  168. package/template/TOOL_EXAMPLES.md +655 -0
  169. package/template/package-lock.json +5 -5
  170. package/template/package.json +1 -1
  171. package/template/src/index.ts +147 -207
  172. package/template/src/tools/BasketTool.ts +128 -0
  173. package/template/src/tools/CustomDataTool.ts +7 -13
  174. package/template/src/tools/OrderTool.ts +54 -0
  175. package/template/src/tools/PaymentTool.ts +1 -1
  176. package/template/src/tools/ProductsTool.ts +56 -118
  177. package/template/src/tools/UserDataTool.ts +4 -26
  178. package/dist/common/config.d.ts +0 -5
  179. package/dist/common/config.js +0 -5
  180. package/dist/custom-data-api.d.ts +0 -72
  181. package/dist/custom-data-api.js +0 -174
  182. package/dist/product-api.d.ts +0 -189
  183. package/dist/product-api.js +0 -141
  184. package/dist/services/api.d.ts +0 -549
  185. package/dist/services/api.js +0 -596
  186. package/dist/skill.d.ts +0 -50
  187. package/dist/types.d.ts +0 -1
  188. package/dist/types.js +0 -2
  189. package/dist/user-data-api.d.ts +0 -39
  190. package/dist/user-data-api.js +0 -50
  191. package/template/API.md +0 -604
  192. package/template/DEVELOPER.md +0 -771
  193. package/template/lua.skill.yaml +0 -7
@@ -1,6 +1,6 @@
1
- import { env, LuaTool } from "lua-cli/skill";
1
+ import { LuaTool, Products } from "lua-cli";
2
2
  import { z } from "zod";
3
- import { product, BasketStatus, OrderStatus, CreateOrderRequest } from 'lua-cli/product-api';
3
+ import { BasketStatus, OrderStatus } from 'lua-cli';
4
4
  import { v4 as uuidv4 } from 'uuid';
5
5
  export class SearchProductsTool implements LuaTool {
6
6
  name = "search_products";
@@ -12,7 +12,7 @@ export class SearchProductsTool implements LuaTool {
12
12
  constructor() {}
13
13
 
14
14
  async execute(input: z.infer<typeof this.inputSchema>) {
15
- return await product.data.search(input.query);
15
+ return await Products.search(input.query);
16
16
  }
17
17
  }
18
18
 
@@ -25,7 +25,7 @@ export class GetAllProductsTool implements LuaTool {
25
25
  });
26
26
 
27
27
  async execute(input: z.infer<typeof this.inputSchema>) {
28
- return product.data.get(input.page, input.limit);
28
+ return await Products.get(input.page, input.limit);
29
29
  }
30
30
  }
31
31
 
@@ -41,7 +41,7 @@ export class CreateProductTool implements LuaTool {
41
41
  });
42
42
 
43
43
  async execute(input: z.infer<typeof this.inputSchema>) {
44
- return product.data.create({...input.product, id: uuidv4()});
44
+ return Products.create({...input.product, id: uuidv4()});
45
45
  }
46
46
  }
47
47
 
@@ -58,133 +58,71 @@ export class UpdateProductTool implements LuaTool {
58
58
  });
59
59
 
60
60
  async execute(input: z.infer<typeof this.inputSchema>) {
61
- return product.data.update({...input.product, id: uuidv4()}, input.product.id);
62
- }
63
- }
64
-
65
- export class DeleteProductTool implements LuaTool {
66
- name = "delete_product";
67
- description = "Delete an existing product";
68
- inputSchema = z.object({
69
- id: z.string()
70
- });
71
-
72
- async execute(input: z.infer<typeof this.inputSchema>) {
73
- return product.data.delete(input.id);
61
+ return Products.update({...input.product, id: uuidv4()}, input.product.id);
74
62
  }
75
63
  }
76
64
 
77
- export class CreateBasketTool implements LuaTool {
78
- name = "create_basket";
79
- description = "Create a new basket";
65
+ export class GetProductByIdTool implements LuaTool {
66
+ name = "get_product_by_id";
67
+ description = "Get a product by its unique identifier";
80
68
  inputSchema = z.object({
81
- basket: z.object({
82
- currency: z.string()
83
- })
84
- });
85
-
86
- async execute(input: z.infer<typeof this.inputSchema>) {
87
- return product.basket.create(input.basket);
88
- }
89
- }
90
-
91
- export class GetBasketsTool implements LuaTool {
92
- name = "get_baskets";
93
- description = "Get all baskets";
94
- inputSchema = z.object({
95
- status: z.enum(['active', 'checked_out', 'abandoned', 'expired']).optional()
96
- });
97
- async execute(input: z.infer<typeof this.inputSchema>) {
98
- return product.basket.get(input.status as BasketStatus);
99
- }
100
- }
101
-
102
- export class AddItemToBasketTool implements LuaTool {
103
- name = "add_item_to_basket";
104
- description = "Add an item to a basket";
105
- inputSchema = z.object({
106
- basketId: z.string(),
107
- item: z.object({
108
- id: z.string(),
109
- price: z.number(),
110
- quantity: z.number()
111
- })
112
- });
113
- async execute(input: z.infer<typeof this.inputSchema>) {
114
- return product.basket.addItem(input.basketId, input.item);
115
- }
116
- }
117
-
118
- export class RemoveItemFromBasketTool implements LuaTool {
119
- name = "remove_item_from_basket";
120
- description = "Remove an item from a basket";
121
- inputSchema = z.object({
122
- basketId: z.string(),
123
- itemId: z.string()
69
+ id: z.string()
124
70
  });
125
- async execute(input: z.infer<typeof this.inputSchema>) {
126
- return product.basket.removeItem(input.basketId, input.itemId);
127
- }
128
- }
129
71
 
130
- export class ClearBasketTool implements LuaTool {
131
- name = "clear_basket";
132
- description = "Clear a basket";
133
- inputSchema = z.object({
134
- basketId: z.string()
135
- });
136
72
  async execute(input: z.infer<typeof this.inputSchema>) {
137
- return product.basket.clear(input.basketId);
73
+ return Products.getById(input.id);
138
74
  }
139
75
  }
140
-
141
- export class UpdateBasketStatusTool implements LuaTool {
142
- name = "update_basket_status";
143
- description = "Update the status of a basket";
76
+
77
+ export class DeleteProductTool implements LuaTool {
78
+ name = "delete_product";
79
+ description = "Delete an existing product";
144
80
  inputSchema = z.object({
145
- basketId: z.string(),
146
- status: z.enum(['active', 'checked_out', 'abandoned', 'expired'])
81
+ id: z.string()
147
82
  });
148
- async execute(input: z.infer<typeof this.inputSchema>) {
149
- return product.basket.updateStatus(input.basketId, input.status as BasketStatus);
150
- }
151
- }
152
83
 
153
- export class CreateOrderTool implements LuaTool {
154
- name = "create_order";
155
- description = "Create a new order";
156
- inputSchema = z.object({
157
- order: z.object({
158
- basketId: z.string(),
159
- data: z.object({
160
- storeId: z.string(),
161
- }).optional()
162
- })
163
- });
164
84
  async execute(input: z.infer<typeof this.inputSchema>) {
165
- return product.order.create(input.order as CreateOrderRequest);
85
+ const product = await Products.getById(input.id);
86
+ return product.delete();
166
87
  }
167
88
  }
168
89
 
169
- export class UpdateOrderStatusTool implements LuaTool {
170
- name = "update_order_status";
171
- description = "Update the status of an order";
172
- inputSchema = z.object({
173
- orderId: z.string(),
174
- status: z.enum(['pending', 'confirmed', 'fulfilled', 'cancelled'])
175
- });
176
- async execute(input: z.infer<typeof this.inputSchema>) {
177
- return product.order.updateStatus(input.orderId, input.status as OrderStatus);
178
- }
179
- }
180
90
 
181
- export class GetUserOrdersTool implements LuaTool {
182
- name = "get_user_orders";
183
- description = "Get all orders for a user";
184
- inputSchema = z.object({
185
- userId: z.string()
186
- });
187
- async execute(input: z.infer<typeof this.inputSchema>) {
188
- return product.order.get(input.userId);
189
- }
190
- }
91
+ // export class CreateOrderTool implements LuaTool {
92
+ // name = "create_order";
93
+ // description = "Create a new order";
94
+ // inputSchema = z.object({
95
+ // order: z.object({
96
+ // basketId: z.string(),
97
+ // data: z.object({
98
+ // storeId: z.string(),
99
+ // }).optional()
100
+ // })
101
+ // });
102
+ // async execute(input: z.infer<typeof this.inputSchema>) {
103
+ // return product.order.create(input.order as CreateOrderRequest);
104
+ // }
105
+ // }
106
+
107
+ // export class UpdateOrderStatusTool implements LuaTool {
108
+ // name = "update_order_status";
109
+ // description = "Update the status of an order";
110
+ // inputSchema = z.object({
111
+ // orderId: z.string(),
112
+ // status: z.enum(['pending', 'confirmed', 'fulfilled', 'cancelled'])
113
+ // });
114
+ // async execute(input: z.infer<typeof this.inputSchema>) {
115
+ // return product.order.updateStatus(input.orderId, input.status as OrderStatus);
116
+ // }
117
+ // }
118
+
119
+ // export class GetUserOrdersTool implements LuaTool {
120
+ // name = "get_user_orders";
121
+ // description = "Get all orders for a user";
122
+ // inputSchema = z.object({
123
+ // userId: z.string()
124
+ // });
125
+ // async execute(input: z.infer<typeof this.inputSchema>) {
126
+ // return product.order.get(input.userId);
127
+ // }
128
+ // }
@@ -1,4 +1,4 @@
1
- import { Lua, LuaTool } from "lua-cli";
1
+ import { LuaTool, User } from "lua-cli";
2
2
  import { z } from "zod";
3
3
 
4
4
 
@@ -10,30 +10,7 @@ export class GetUserDataTool implements LuaTool {
10
10
  constructor() {}
11
11
 
12
12
  async execute(input: z.infer<typeof this.inputSchema>) {
13
- return new Lua.User().get();
14
- }
15
- }
16
-
17
-
18
- export class CreateUserDataTool implements LuaTool {
19
- name = "create_user_data";
20
- description = "Get the user data for a given user id";
21
- inputSchema = z.object({
22
- data: z.object({
23
- name: z.string(),
24
- age: z.number()
25
- })
26
- });
27
-
28
- constructor() {}
29
-
30
- async execute(input: z.infer<typeof this.inputSchema>) {
31
- console.log('createUserData tool', input);
32
- const userData = await new Lua.User().get();
33
- if (userData.data) {
34
- return new Lua.User().update(input.data);
35
- }
36
- return new Lua.User().create(input.data);
13
+ return User.get();
37
14
  }
38
15
  }
39
16
 
@@ -50,6 +27,7 @@ export class UpdateUserDataTool implements LuaTool {
50
27
  constructor() {}
51
28
 
52
29
  async execute(input: z.infer<typeof this.inputSchema>) {
53
- return new Lua.User().update(input.data);
30
+ const user = await User.get(); //get instance of user
31
+ return await user.update(input.data);
54
32
  }
55
33
  }
@@ -1,5 +0,0 @@
1
- export declare const BASE_URLS: {
2
- readonly API: "http://localhost:3022";
3
- readonly AUTH: "https://auth.heylua.ai";
4
- readonly CHAT: "http://localhost:3022";
5
- };
@@ -1,5 +0,0 @@
1
- export const BASE_URLS = {
2
- API: 'http://localhost:3022',
3
- AUTH: 'https://auth.heylua.ai',
4
- CHAT: 'http://localhost:3022'
5
- };
@@ -1,72 +0,0 @@
1
- export interface CustomDataEntry {
2
- id: string;
3
- data: any;
4
- createdAt: number;
5
- updatedAt: number;
6
- searchText?: string;
7
- }
8
- export interface CreateCustomDataRequest {
9
- data: any;
10
- searchText?: string;
11
- }
12
- export interface CreateCustomDataResponse {
13
- id: string;
14
- data: any;
15
- createdAt: number;
16
- updatedAt: number;
17
- searchText?: string;
18
- }
19
- export interface GetCustomDataResponse {
20
- data: CustomDataEntry[];
21
- pagination: {
22
- currentPage: number;
23
- totalPages: number;
24
- totalCount: number;
25
- limit: number;
26
- hasNextPage: boolean;
27
- hasPrevPage: boolean;
28
- };
29
- }
30
- export interface UpdateCustomDataRequest {
31
- data: any;
32
- searchText?: string;
33
- }
34
- export interface UpdateCustomDataResponse {
35
- status: string;
36
- message: string;
37
- }
38
- export interface SearchCustomDataResponse {
39
- data: Array<CustomDataEntry & {
40
- score: number;
41
- }>;
42
- count: number;
43
- }
44
- export interface DeleteCustomDataResponse {
45
- status: string;
46
- message: string;
47
- }
48
- export declare class CustomDataAPI {
49
- private collections;
50
- constructor();
51
- private generateId;
52
- private getCollection;
53
- create(collectionName: string, data: CreateCustomDataRequest): Promise<CreateCustomDataResponse>;
54
- get(collectionName: string, filter?: any, page?: number, limit?: number): Promise<GetCustomDataResponse>;
55
- getEntry(collectionName: string, entryId: string): Promise<CustomDataEntry | null>;
56
- update(collectionName: string, entryId: string, data: UpdateCustomDataRequest): Promise<UpdateCustomDataResponse>;
57
- search(collectionName: string, searchText: string, limit?: number, scoreThreshold?: number): Promise<SearchCustomDataResponse>;
58
- delete(collectionName: string, entryId: string): Promise<DeleteCustomDataResponse>;
59
- private matchesFilter;
60
- private calculateSimilarity;
61
- getAllCollections(): Map<string, CustomDataEntry[]>;
62
- clearCollection(collectionName: string): void;
63
- clearAllCollections(): void;
64
- }
65
- export declare const customData: {
66
- create: (collectionName: string, data: CreateCustomDataRequest) => Promise<CreateCustomDataResponse>;
67
- get: (collectionName: string, filter?: any, page?: number, limit?: number) => Promise<GetCustomDataResponse>;
68
- getEntry: (collectionName: string, entryId: string) => Promise<CustomDataEntry | null>;
69
- update: (collectionName: string, entryId: string, data: UpdateCustomDataRequest) => Promise<UpdateCustomDataResponse>;
70
- search: (collectionName: string, searchText: string, limit?: number, scoreThreshold?: number) => Promise<SearchCustomDataResponse>;
71
- delete: (collectionName: string, entryId: string) => Promise<DeleteCustomDataResponse>;
72
- };
@@ -1,174 +0,0 @@
1
- export class CustomDataAPI {
2
- constructor() {
3
- // Store data by collection name
4
- this.collections = new Map();
5
- }
6
- generateId() {
7
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
8
- const r = Math.random() * 16 | 0;
9
- const v = c == 'x' ? r : (r & 0x3 | 0x8);
10
- return v.toString(16);
11
- });
12
- }
13
- getCollection(collectionName) {
14
- if (!this.collections.has(collectionName)) {
15
- this.collections.set(collectionName, []);
16
- }
17
- return this.collections.get(collectionName);
18
- }
19
- async create(collectionName, data) {
20
- const collection = this.getCollection(collectionName);
21
- const now = Date.now();
22
- const entry = {
23
- id: this.generateId(),
24
- data: data.data,
25
- createdAt: now,
26
- updatedAt: now,
27
- searchText: data.searchText
28
- };
29
- collection.push(entry);
30
- return {
31
- id: entry.id,
32
- data: entry.data,
33
- createdAt: entry.createdAt,
34
- updatedAt: entry.updatedAt,
35
- searchText: entry.searchText
36
- };
37
- }
38
- async get(collectionName, filter, page = 1, limit = 10) {
39
- const collection = this.getCollection(collectionName);
40
- let filteredData = [...collection];
41
- // Apply filter if provided
42
- if (filter) {
43
- filteredData = collection.filter(entry => {
44
- return this.matchesFilter(entry.data, filter);
45
- });
46
- }
47
- // Apply pagination
48
- const startIndex = (page - 1) * limit;
49
- const endIndex = startIndex + limit;
50
- const paginatedData = filteredData.slice(startIndex, endIndex);
51
- const totalCount = filteredData.length;
52
- const totalPages = Math.ceil(totalCount / limit);
53
- return {
54
- data: paginatedData,
55
- pagination: {
56
- currentPage: page,
57
- totalPages: totalPages,
58
- totalCount: totalCount,
59
- limit: limit,
60
- hasNextPage: page < totalPages,
61
- hasPrevPage: page > 1
62
- }
63
- };
64
- }
65
- async getEntry(collectionName, entryId) {
66
- const collection = this.getCollection(collectionName);
67
- return collection.find(entry => entry.id === entryId) || null;
68
- }
69
- async update(collectionName, entryId, data) {
70
- const collection = this.getCollection(collectionName);
71
- const entryIndex = collection.findIndex(entry => entry.id === entryId);
72
- if (entryIndex === -1) {
73
- return {
74
- status: 'error',
75
- message: 'Custom data entry not found'
76
- };
77
- }
78
- const entry = collection[entryIndex];
79
- entry.data = { ...entry.data, ...data.data };
80
- entry.updatedAt = Date.now();
81
- if (data.searchText !== undefined) {
82
- entry.searchText = data.searchText;
83
- }
84
- return {
85
- status: 'success',
86
- message: 'Custom data entry updated'
87
- };
88
- }
89
- async search(collectionName, searchText, limit = 10, scoreThreshold = 0.6) {
90
- const collection = this.getCollection(collectionName);
91
- const results = [];
92
- for (const entry of collection) {
93
- if (entry.searchText) {
94
- const score = this.calculateSimilarity(searchText.toLowerCase(), entry.searchText.toLowerCase());
95
- if (score >= scoreThreshold) {
96
- results.push({ ...entry, score });
97
- }
98
- }
99
- }
100
- // Sort by score descending
101
- results.sort((a, b) => b.score - a.score);
102
- // Apply limit
103
- const limitedResults = results.slice(0, limit);
104
- return {
105
- data: limitedResults,
106
- count: limitedResults.length
107
- };
108
- }
109
- async delete(collectionName, entryId) {
110
- const collection = this.getCollection(collectionName);
111
- const initialLength = collection.length;
112
- const updatedCollection = collection.filter(entry => entry.id !== entryId);
113
- this.collections.set(collectionName, updatedCollection);
114
- if (updatedCollection.length < initialLength) {
115
- return {
116
- status: 'success',
117
- message: 'Custom data entry deleted'
118
- };
119
- }
120
- else {
121
- return {
122
- status: 'error',
123
- message: 'Custom data entry not found'
124
- };
125
- }
126
- }
127
- // Helper method to check if an object matches a filter
128
- matchesFilter(obj, filter) {
129
- for (const key in filter) {
130
- if (filter.hasOwnProperty(key)) {
131
- if (typeof filter[key] === 'object' && filter[key] !== null) {
132
- if (typeof obj[key] !== 'object' || !this.matchesFilter(obj[key], filter[key])) {
133
- return false;
134
- }
135
- }
136
- else {
137
- if (obj[key] !== filter[key]) {
138
- return false;
139
- }
140
- }
141
- }
142
- }
143
- return true;
144
- }
145
- // Simple similarity calculation (Jaccard similarity for words)
146
- calculateSimilarity(str1, str2) {
147
- const words1 = new Set(str1.split(/\s+/));
148
- const words2 = new Set(str2.split(/\s+/));
149
- const intersection = new Set([...words1].filter(x => words2.has(x)));
150
- const union = new Set([...words1, ...words2]);
151
- return intersection.size / union.size;
152
- }
153
- // Method to get all collections (for debugging/testing)
154
- getAllCollections() {
155
- return this.collections;
156
- }
157
- // Method to clear a collection (for testing)
158
- clearCollection(collectionName) {
159
- this.collections.set(collectionName, []);
160
- }
161
- // Method to clear all collections (for testing)
162
- clearAllCollections() {
163
- this.collections.clear();
164
- }
165
- }
166
- const customDataAPI = new CustomDataAPI();
167
- export const customData = {
168
- create: (collectionName, data) => customDataAPI.create(collectionName, data),
169
- get: (collectionName, filter, page, limit) => customDataAPI.get(collectionName, filter, page, limit),
170
- getEntry: (collectionName, entryId) => customDataAPI.getEntry(collectionName, entryId),
171
- update: (collectionName, entryId, data) => customDataAPI.update(collectionName, entryId, data),
172
- search: (collectionName, searchText, limit, scoreThreshold) => customDataAPI.search(collectionName, searchText, limit, scoreThreshold),
173
- delete: (collectionName, entryId) => customDataAPI.delete(collectionName, entryId),
174
- };