@symbo.ls/sdk 2.34.35 → 3.1.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 (187) hide show
  1. package/README.md +13 -315
  2. package/dist/cjs/config/environment.js +50 -124
  3. package/dist/cjs/index.js +22 -169
  4. package/dist/cjs/services/AIService.js +155 -0
  5. package/dist/cjs/services/AuthService.js +277 -751
  6. package/dist/cjs/services/BaseService.js +6 -158
  7. package/dist/cjs/services/BasedService.js +679 -0
  8. package/dist/cjs/services/SocketIOService.js +309 -0
  9. package/dist/cjs/services/SocketService.js +161 -0
  10. package/dist/cjs/services/SymstoryService.js +485 -0
  11. package/dist/cjs/services/index.js +16 -64
  12. package/dist/cjs/utils/basedQuerys.js +61 -0
  13. package/dist/cjs/utils/permission.js +4 -4
  14. package/dist/cjs/utils/services.js +80 -301
  15. package/dist/cjs/utils/symstoryClient.js +228 -0
  16. package/dist/cjs/utils/validation.js +3 -0
  17. package/dist/esm/config/environment.js +50 -124
  18. package/dist/esm/index.js +11282 -49789
  19. package/dist/esm/services/AIService.js +185 -0
  20. package/dist/esm/services/AuthService.js +358 -1506
  21. package/dist/esm/services/BaseService.js +6 -757
  22. package/dist/esm/services/BasedService.js +4651 -0
  23. package/dist/esm/services/SocketIOService.js +467 -0
  24. package/dist/esm/services/SocketService.js +191 -0
  25. package/dist/esm/services/SymstoryService.js +6849 -0
  26. package/dist/esm/services/index.js +11188 -49352
  27. package/dist/esm/utils/basedQuerys.js +43 -0
  28. package/dist/esm/utils/permission.js +4 -4
  29. package/dist/esm/utils/services.js +80 -301
  30. package/dist/esm/utils/symstoryClient.js +334 -0
  31. package/dist/esm/utils/validation.js +26 -93
  32. package/dist/node/config/environment.js +50 -124
  33. package/dist/node/index.js +26 -202
  34. package/dist/node/services/AIService.js +136 -0
  35. package/dist/node/services/AuthService.js +278 -751
  36. package/dist/node/services/BaseService.js +6 -148
  37. package/dist/node/services/BasedService.js +650 -0
  38. package/dist/node/services/SocketIOService.js +280 -0
  39. package/dist/node/services/SocketService.js +142 -0
  40. package/dist/node/services/SymstoryService.js +456 -0
  41. package/dist/node/services/index.js +16 -64
  42. package/dist/node/utils/basedQuerys.js +42 -0
  43. package/dist/node/utils/permission.js +4 -4
  44. package/dist/node/utils/services.js +80 -301
  45. package/dist/node/utils/symstoryClient.js +199 -0
  46. package/dist/node/utils/validation.js +3 -0
  47. package/package.json +21 -43
  48. package/src/config/environment.js +50 -126
  49. package/src/index.js +24 -208
  50. package/src/services/AIService.js +150 -0
  51. package/src/services/AuthService.js +298 -893
  52. package/src/services/BaseService.js +6 -166
  53. package/src/services/BasedService.js +722 -0
  54. package/src/services/SocketIOService.js +356 -0
  55. package/src/services/SocketService.js +168 -0
  56. package/src/services/SymstoryService.js +563 -0
  57. package/src/services/index.js +13 -80
  58. package/src/utils/basedQuerys.js +41 -0
  59. package/src/utils/permission.js +4 -4
  60. package/src/utils/services.js +83 -325
  61. package/src/utils/symstoryClient.js +218 -0
  62. package/src/utils/validation.js +3 -0
  63. package/dist/cjs/services/AdminService.js +0 -351
  64. package/dist/cjs/services/BranchService.js +0 -484
  65. package/dist/cjs/services/CollabService.js +0 -743
  66. package/dist/cjs/services/DnsService.js +0 -340
  67. package/dist/cjs/services/FeatureFlagService.js +0 -175
  68. package/dist/cjs/services/FileService.js +0 -201
  69. package/dist/cjs/services/IntegrationService.js +0 -538
  70. package/dist/cjs/services/MetricsService.js +0 -62
  71. package/dist/cjs/services/PaymentService.js +0 -271
  72. package/dist/cjs/services/PlanService.js +0 -426
  73. package/dist/cjs/services/ProjectService.js +0 -1207
  74. package/dist/cjs/services/PullRequestService.js +0 -503
  75. package/dist/cjs/services/ScreenshotService.js +0 -304
  76. package/dist/cjs/services/SubscriptionService.js +0 -396
  77. package/dist/cjs/services/TrackingService.js +0 -661
  78. package/dist/cjs/services/WaitlistService.js +0 -148
  79. package/dist/cjs/state/RootStateManager.js +0 -65
  80. package/dist/cjs/state/rootEventBus.js +0 -74
  81. package/dist/cjs/utils/CollabClient.js +0 -223
  82. package/dist/cjs/utils/TokenManager.js +0 -422
  83. package/dist/cjs/utils/changePreprocessor.js +0 -199
  84. package/dist/cjs/utils/jsonDiff.js +0 -145
  85. package/dist/cjs/utils/ordering.js +0 -309
  86. package/dist/esm/services/AdminService.js +0 -1132
  87. package/dist/esm/services/BranchService.js +0 -1265
  88. package/dist/esm/services/CollabService.js +0 -26838
  89. package/dist/esm/services/DnsService.js +0 -1121
  90. package/dist/esm/services/FeatureFlagService.js +0 -956
  91. package/dist/esm/services/FileService.js +0 -982
  92. package/dist/esm/services/IntegrationService.js +0 -1319
  93. package/dist/esm/services/MetricsService.js +0 -843
  94. package/dist/esm/services/PaymentService.js +0 -1052
  95. package/dist/esm/services/PlanService.js +0 -1207
  96. package/dist/esm/services/ProjectService.js +0 -2526
  97. package/dist/esm/services/PullRequestService.js +0 -1284
  98. package/dist/esm/services/ScreenshotService.js +0 -1085
  99. package/dist/esm/services/SubscriptionService.js +0 -1177
  100. package/dist/esm/services/TrackingService.js +0 -18343
  101. package/dist/esm/services/WaitlistService.js +0 -929
  102. package/dist/esm/state/RootStateManager.js +0 -90
  103. package/dist/esm/state/rootEventBus.js +0 -56
  104. package/dist/esm/utils/CollabClient.js +0 -18901
  105. package/dist/esm/utils/TokenManager.js +0 -408
  106. package/dist/esm/utils/changePreprocessor.js +0 -542
  107. package/dist/esm/utils/jsonDiff.js +0 -7011
  108. package/dist/esm/utils/ordering.js +0 -291
  109. package/dist/node/services/AdminService.js +0 -332
  110. package/dist/node/services/BranchService.js +0 -465
  111. package/dist/node/services/CollabService.js +0 -724
  112. package/dist/node/services/DnsService.js +0 -321
  113. package/dist/node/services/FeatureFlagService.js +0 -156
  114. package/dist/node/services/FileService.js +0 -182
  115. package/dist/node/services/IntegrationService.js +0 -519
  116. package/dist/node/services/MetricsService.js +0 -43
  117. package/dist/node/services/PaymentService.js +0 -252
  118. package/dist/node/services/PlanService.js +0 -407
  119. package/dist/node/services/ProjectService.js +0 -1188
  120. package/dist/node/services/PullRequestService.js +0 -484
  121. package/dist/node/services/ScreenshotService.js +0 -285
  122. package/dist/node/services/SubscriptionService.js +0 -377
  123. package/dist/node/services/TrackingService.js +0 -632
  124. package/dist/node/services/WaitlistService.js +0 -129
  125. package/dist/node/state/RootStateManager.js +0 -36
  126. package/dist/node/state/rootEventBus.js +0 -55
  127. package/dist/node/utils/CollabClient.js +0 -194
  128. package/dist/node/utils/TokenManager.js +0 -403
  129. package/dist/node/utils/changePreprocessor.js +0 -180
  130. package/dist/node/utils/jsonDiff.js +0 -116
  131. package/dist/node/utils/ordering.js +0 -290
  132. package/src/services/AdminService.js +0 -374
  133. package/src/services/BranchService.js +0 -536
  134. package/src/services/CollabService.js +0 -900
  135. package/src/services/DnsService.js +0 -366
  136. package/src/services/FeatureFlagService.js +0 -174
  137. package/src/services/FileService.js +0 -213
  138. package/src/services/IntegrationService.js +0 -548
  139. package/src/services/MetricsService.js +0 -40
  140. package/src/services/PaymentService.js +0 -287
  141. package/src/services/PlanService.js +0 -468
  142. package/src/services/ProjectService.js +0 -1366
  143. package/src/services/PullRequestService.js +0 -537
  144. package/src/services/ScreenshotService.js +0 -258
  145. package/src/services/SubscriptionService.js +0 -425
  146. package/src/services/TrackingService.js +0 -853
  147. package/src/services/WaitlistService.js +0 -130
  148. package/src/services/tests/BranchService/createBranch.test.js +0 -153
  149. package/src/services/tests/BranchService/deleteBranch.test.js +0 -173
  150. package/src/services/tests/BranchService/getBranchChanges.test.js +0 -146
  151. package/src/services/tests/BranchService/listBranches.test.js +0 -87
  152. package/src/services/tests/BranchService/mergeBranch.test.js +0 -210
  153. package/src/services/tests/BranchService/publishVersion.test.js +0 -183
  154. package/src/services/tests/BranchService/renameBranch.test.js +0 -240
  155. package/src/services/tests/BranchService/resetBranch.test.js +0 -152
  156. package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +0 -67
  157. package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +0 -75
  158. package/src/services/tests/FileService/createFileFormData.test.js +0 -74
  159. package/src/services/tests/FileService/getFileUrl.test.js +0 -69
  160. package/src/services/tests/FileService/updateProjectIcon.test.js +0 -109
  161. package/src/services/tests/FileService/uploadDocument.test.js +0 -36
  162. package/src/services/tests/FileService/uploadFile.test.js +0 -78
  163. package/src/services/tests/FileService/uploadFileWithValidation.test.js +0 -114
  164. package/src/services/tests/FileService/uploadImage.test.js +0 -36
  165. package/src/services/tests/FileService/uploadMultipleFiles.test.js +0 -111
  166. package/src/services/tests/FileService/validateFile.test.js +0 -63
  167. package/src/services/tests/PlanService/createPlan.test.js +0 -104
  168. package/src/services/tests/PlanService/createPlanWithValidation.test.js +0 -523
  169. package/src/services/tests/PlanService/deletePlan.test.js +0 -92
  170. package/src/services/tests/PlanService/getActivePlans.test.js +0 -123
  171. package/src/services/tests/PlanService/getAdminPlans.test.js +0 -84
  172. package/src/services/tests/PlanService/getPlan.test.js +0 -50
  173. package/src/services/tests/PlanService/getPlanByKey.test.js +0 -109
  174. package/src/services/tests/PlanService/getPlanWithValidation.test.js +0 -85
  175. package/src/services/tests/PlanService/getPlans.test.js +0 -53
  176. package/src/services/tests/PlanService/getPlansByPriceRange.test.js +0 -109
  177. package/src/services/tests/PlanService/getPlansWithValidation.test.js +0 -48
  178. package/src/services/tests/PlanService/initializePlans.test.js +0 -75
  179. package/src/services/tests/PlanService/updatePlan.test.js +0 -111
  180. package/src/services/tests/PlanService/updatePlanWithValidation.test.js +0 -556
  181. package/src/state/RootStateManager.js +0 -76
  182. package/src/state/rootEventBus.js +0 -67
  183. package/src/utils/CollabClient.js +0 -248
  184. package/src/utils/TokenManager.js +0 -479
  185. package/src/utils/changePreprocessor.js +0 -239
  186. package/src/utils/jsonDiff.js +0 -144
  187. package/src/utils/ordering.js +0 -271
@@ -1,743 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var CollabService_exports = {};
19
- __export(CollabService_exports, {
20
- CollabService: () => CollabService
21
- });
22
- module.exports = __toCommonJS(CollabService_exports);
23
- var import_BaseService = require("./BaseService.js");
24
- var import_CollabClient = require("../utils/CollabClient.js");
25
- var import_RootStateManager = require("../state/RootStateManager.js");
26
- var import_rootEventBus = require("../state/rootEventBus.js");
27
- var import_validation = require("../utils/validation.js");
28
- var import_utils = require("@domql/utils");
29
- var import_changePreprocessor = require("../utils/changePreprocessor.js");
30
- const FUNCTION_META_KEYS = ["node", "__ref", "__element", "parent", "parse"];
31
- function stringifyFunctionsForTransport(value, seen = /* @__PURE__ */ new WeakMap()) {
32
- if (value === null || typeof value !== "object") {
33
- return typeof value === "function" ? value.toString() : value;
34
- }
35
- if (seen.has(value)) {
36
- return seen.get(value);
37
- }
38
- const clone = Array.isArray(value) ? [] : {};
39
- seen.set(value, clone);
40
- if (Array.isArray(value)) {
41
- for (let i = 0; i < value.length; i++) {
42
- clone[i] = stringifyFunctionsForTransport(value[i], seen);
43
- }
44
- return clone;
45
- }
46
- const keys = Object.keys(value);
47
- for (let i = 0; i < keys.length; i++) {
48
- const key = keys[i];
49
- if (!FUNCTION_META_KEYS.includes(key)) {
50
- clone[key] = stringifyFunctionsForTransport(value[key], seen);
51
- }
52
- }
53
- return clone;
54
- }
55
- class CollabService extends import_BaseService.BaseService {
56
- constructor(config) {
57
- super(config);
58
- this._client = null;
59
- this._stateManager = null;
60
- this._connected = false;
61
- this._connecting = false;
62
- this._connectPromise = null;
63
- this._connectionMeta = null;
64
- this._pendingConnectReject = null;
65
- this._undoStack = [];
66
- this._redoStack = [];
67
- this._isUndoRedo = false;
68
- this._pendingOps = [];
69
- this._onSocketConnect = this._onSocketConnect.bind(this);
70
- this._onSocketDisconnect = this._onSocketDisconnect.bind(this);
71
- this._onSocketError = this._onSocketError.bind(this);
72
- }
73
- init({ context }) {
74
- super.init({ context });
75
- if (context == null ? void 0 : context.state) {
76
- try {
77
- this._stateManager = new import_RootStateManager.RootStateManager(context.state);
78
- } catch (err) {
79
- this._setError(err);
80
- throw err;
81
- }
82
- }
83
- this._setReady();
84
- }
85
- /**
86
- * Overridden to re-initialise the state manager once the root state becomes
87
- * available via a subsequent SDK `updateContext()` call.
88
- */
89
- updateContext(context = {}) {
90
- super.updateContext(context);
91
- if (context.state) {
92
- this._stateManager = new import_RootStateManager.RootStateManager(context.state);
93
- }
94
- }
95
- /**
96
- * Ensure that the state manager exists. This is called right before any
97
- * operation that requires access to the root state (e.g. `connect()`).
98
- * Throws an explicit error if the root state is still missing so that the
99
- * caller can react accordingly.
100
- */
101
- _ensureStateManager() {
102
- var _a, _b;
103
- if (!this._stateManager) {
104
- if (!((_a = this._context) == null ? void 0 : _a.state)) {
105
- throw new Error("[CollabService] Cannot operate without root state");
106
- }
107
- this._stateManager = new import_RootStateManager.RootStateManager(this._context.state);
108
- }
109
- const root = (_b = this._stateManager) == null ? void 0 : _b.root;
110
- if (root && !root.__element) {
111
- root.__element = {
112
- /**
113
- * Very small subset of the DOMQL `call` API that we rely on inside the
114
- * CollabService for browser notifications and data helpers.
115
- * In a Node.js test context we simply log or return fallbacks.
116
- */
117
- call: (method, ...args) => {
118
- switch (method) {
119
- case "openNotification": {
120
- const [payload = {}] = args;
121
- const { type = "info", title = "", message = "" } = payload;
122
- const logger = type === "error" ? console.error : console.log;
123
- logger(`[Notification] ${title}${message ? ` \u2013 ${message}` : ""}`);
124
- return;
125
- }
126
- case "deepStringifyFunctions": {
127
- return (0, import_utils.deepStringifyFunctions)(...args);
128
- }
129
- default:
130
- return {};
131
- }
132
- }
133
- };
134
- }
135
- }
136
- /* ---------- Connection Management ---------- */
137
- async connect(options = {}) {
138
- if (this._connectPromise) {
139
- return this._connectPromise;
140
- }
141
- this._connectPromise = (async () => {
142
- var _a;
143
- this._connecting = true;
144
- this._connected = false;
145
- this._ensureStateManager();
146
- const mergedOptions = {
147
- ...this._context,
148
- ...options
149
- };
150
- let { authToken: jwt } = mergedOptions;
151
- const { projectId, branch = "main", pro } = mergedOptions;
152
- if (!jwt && this._tokenManager) {
153
- try {
154
- jwt = await this._tokenManager.ensureValidToken();
155
- } catch (error) {
156
- console.warn(
157
- "[CollabService] Failed to obtain auth token from token manager",
158
- error
159
- );
160
- }
161
- if (!jwt && typeof this._tokenManager.getAccessToken === "function") {
162
- jwt = this._tokenManager.getAccessToken();
163
- }
164
- }
165
- if (!jwt) {
166
- throw new Error("[CollabService] Cannot connect without auth token");
167
- }
168
- this._context = {
169
- ...this._context,
170
- authToken: jwt,
171
- projectId,
172
- branch,
173
- pro
174
- };
175
- if (!projectId) {
176
- const state = (_a = this._stateManager) == null ? void 0 : _a.root;
177
- const el = state.__element;
178
- el.call("openNotification", {
179
- type: "error",
180
- title: "projectId is required",
181
- message: "projectId is required for CollabService connection"
182
- });
183
- throw new Error("projectId is required for CollabService connection");
184
- }
185
- if (this._client) {
186
- await this.disconnect();
187
- }
188
- this._client = new import_CollabClient.CollabClient({
189
- jwt,
190
- projectId,
191
- branch,
192
- live: Boolean(pro)
193
- });
194
- const { socket } = this._client;
195
- try {
196
- await new Promise((resolve, reject) => {
197
- if (!socket) {
198
- reject(new Error("[CollabService] Socket instance missing"));
199
- return;
200
- }
201
- if (socket.connected) {
202
- resolve();
203
- return;
204
- }
205
- const cleanup = () => {
206
- socket.off("connect", handleConnect);
207
- socket.off("connect_error", handleError);
208
- socket.off("error", handleError);
209
- socket.off("disconnect", handleDisconnect);
210
- if (this._pendingConnectReject === handleError) {
211
- this._pendingConnectReject = null;
212
- }
213
- };
214
- const handleConnect = () => {
215
- cleanup();
216
- resolve();
217
- };
218
- const handleError = (error) => {
219
- cleanup();
220
- reject(
221
- error instanceof Error ? error : new Error(String(error || "Unknown connection error"))
222
- );
223
- };
224
- const handleDisconnect = (reason) => {
225
- handleError(
226
- reason instanceof Error ? reason : new Error(
227
- `[CollabService] Socket disconnected before connect: ${reason || "unknown"}`
228
- )
229
- );
230
- };
231
- this._pendingConnectReject = handleError;
232
- socket.once("connect", handleConnect);
233
- socket.once("connect_error", handleError);
234
- socket.once("error", handleError);
235
- socket.once("disconnect", handleDisconnect);
236
- });
237
- } catch (error) {
238
- socket == null ? void 0 : socket.disconnect();
239
- this._client = null;
240
- this._connectionMeta = null;
241
- throw error;
242
- }
243
- this._attachSocketLifecycleListeners();
244
- if (socket == null ? void 0 : socket.connected) {
245
- this._onSocketConnect();
246
- }
247
- socket == null ? void 0 : socket.on("ops", ({ changes }) => {
248
- console.log(`ops event`);
249
- this._stateManager.applyChanges(changes, { fromSocket: true });
250
- });
251
- socket == null ? void 0 : socket.on("commit", ({ version }) => {
252
- if (version) {
253
- this._stateManager.setVersion(version);
254
- }
255
- import_rootEventBus.rootBus.emit("checkpoint:done", { version, origin: "auto" });
256
- });
257
- socket == null ? void 0 : socket.on("clients", this._handleClientsEvent.bind(this));
258
- socket == null ? void 0 : socket.on("bundle:done", this._handleBundleDoneEvent.bind(this));
259
- socket == null ? void 0 : socket.on("bundle:error", this._handleBundleErrorEvent.bind(this));
260
- if (this._pendingOps.length) {
261
- console.log(
262
- `[CollabService] Flushing ${this._pendingOps.length} offline operation batch(es)`
263
- );
264
- this._pendingOps.forEach(
265
- ({ changes, granularChanges, orders, options: opOptions }) => {
266
- const { message } = opOptions || {};
267
- const ts = Date.now();
268
- const payload = {
269
- changes,
270
- granularChanges,
271
- orders,
272
- ts
273
- };
274
- if (message) {
275
- payload.message = message;
276
- }
277
- this.socket.emit("ops", payload);
278
- }
279
- );
280
- this._pendingOps.length = 0;
281
- }
282
- await this._client.ready;
283
- this._connectionMeta = {
284
- projectId,
285
- branch,
286
- live: Boolean(pro)
287
- };
288
- return this.getConnectionInfo();
289
- })();
290
- try {
291
- return await this._connectPromise;
292
- } finally {
293
- this._connecting = false;
294
- this._connectPromise = null;
295
- }
296
- }
297
- disconnect() {
298
- var _a;
299
- if ((_a = this._client) == null ? void 0 : _a.socket) {
300
- if (this._pendingConnectReject) {
301
- this._pendingConnectReject(
302
- new Error("[CollabService] Connection attempt aborted")
303
- );
304
- this._pendingConnectReject = null;
305
- }
306
- this._detachSocketLifecycleListeners();
307
- if (typeof this._client.dispose === "function") {
308
- this._client.dispose();
309
- } else {
310
- this._client.socket.disconnect();
311
- }
312
- }
313
- this._client = null;
314
- this._connected = false;
315
- this._connecting = false;
316
- this._connectionMeta = null;
317
- this._pendingConnectReject = null;
318
- console.log("[CollabService] Disconnected");
319
- }
320
- isConnected() {
321
- var _a, _b;
322
- return Boolean(this._connected && ((_b = (_a = this._client) == null ? void 0 : _a.socket) == null ? void 0 : _b.connected));
323
- }
324
- getConnectionInfo() {
325
- var _a, _b, _c;
326
- return {
327
- connected: this.isConnected(),
328
- connecting: this._connecting,
329
- projectId: ((_a = this._connectionMeta) == null ? void 0 : _a.projectId) ?? null,
330
- branch: ((_b = this._connectionMeta) == null ? void 0 : _b.branch) ?? null,
331
- live: ((_c = this._connectionMeta) == null ? void 0 : _c.live) ?? null,
332
- pendingOps: this._pendingOps.length,
333
- undoStackSize: this.getUndoStackSize(),
334
- redoStackSize: this.getRedoStackSize()
335
- };
336
- }
337
- /* convenient shortcuts */
338
- get ydoc() {
339
- var _a;
340
- return (_a = this._client) == null ? void 0 : _a.ydoc;
341
- }
342
- get socket() {
343
- var _a;
344
- return (_a = this._client) == null ? void 0 : _a.socket;
345
- }
346
- toggleLive(f) {
347
- var _a;
348
- (_a = this._client) == null ? void 0 : _a.toggleLive(f);
349
- }
350
- sendCursor(d) {
351
- var _a;
352
- (_a = this._client) == null ? void 0 : _a.sendCursor(d);
353
- }
354
- sendPresence(d) {
355
- var _a;
356
- (_a = this._client) == null ? void 0 : _a.sendPresence(d);
357
- }
358
- /* ---------- data helpers ---------- */
359
- updateData(tuples, options = {}) {
360
- var _a, _b;
361
- this._ensureStateManager();
362
- const { isUndo = false, isRedo = false } = options;
363
- if (!isUndo && !isRedo && !this._isUndoRedo) {
364
- this._trackForUndo(tuples, options);
365
- }
366
- const root = (_a = this._stateManager) == null ? void 0 : _a.root;
367
- const { granularChanges: processedTuples, orders } = (0, import_changePreprocessor.preprocessChanges)(
368
- root,
369
- tuples,
370
- options
371
- );
372
- if (options.append && options.append.length) {
373
- processedTuples.push(...options.append);
374
- }
375
- this._stateManager.applyChanges(tuples, { ...options });
376
- const stringifiedGranularTuples = stringifyFunctionsForTransport(processedTuples);
377
- const stringifiedTuples = stringifyFunctionsForTransport(tuples);
378
- const { message } = options;
379
- if (!this.isConnected()) {
380
- console.warn("[CollabService] Not connected, queuing real-time update");
381
- this._pendingOps.push({
382
- changes: stringifiedTuples,
383
- granularChanges: stringifiedGranularTuples,
384
- orders,
385
- options
386
- });
387
- return;
388
- }
389
- if ((_b = this.socket) == null ? void 0 : _b.connected) {
390
- const ts = Date.now();
391
- const payload = {
392
- changes: stringifiedTuples,
393
- granularChanges: stringifiedGranularTuples,
394
- orders,
395
- ts
396
- };
397
- if (message) {
398
- payload.message = message;
399
- }
400
- this.socket.emit("ops", payload);
401
- }
402
- return { success: true };
403
- }
404
- _trackForUndo(tuples, options) {
405
- var _a;
406
- const undoOperations = tuples.map((tuple) => {
407
- const [action, path] = tuple;
408
- const currentValue = this._getValueAtPath(path);
409
- if (action === "delete") {
410
- return ["update", path, currentValue];
411
- }
412
- if (typeof currentValue !== "undefined") {
413
- return ["update", path, currentValue];
414
- }
415
- return ["delete", path];
416
- });
417
- this._undoStack.push({
418
- operations: undoOperations,
419
- originalOperations: tuples,
420
- options,
421
- timestamp: Date.now()
422
- });
423
- this._redoStack.length = 0;
424
- const maxUndoSteps = ((_a = this._options) == null ? void 0 : _a.maxUndoSteps) || 50;
425
- if (this._undoStack.length > maxUndoSteps) {
426
- this._undoStack.shift();
427
- }
428
- }
429
- _getValueAtPath(path) {
430
- var _a;
431
- const state = (_a = this._stateManager) == null ? void 0 : _a.root;
432
- if (!state || !state.getByPath) {
433
- return null;
434
- }
435
- try {
436
- return state.getByPath(path);
437
- } catch (error) {
438
- console.warn("[CollabService] Could not get value at path:", path, error);
439
- return null;
440
- }
441
- }
442
- undo() {
443
- var _a;
444
- if (!this._undoStack.length) {
445
- const state = (_a = this._stateManager) == null ? void 0 : _a.root;
446
- const el = state.__element;
447
- el.call("openNotification", {
448
- type: "error",
449
- title: "Nothing to undo"
450
- });
451
- throw new Error("Nothing to undo");
452
- }
453
- if (!this.isConnected()) {
454
- console.warn("[CollabService] Not connected, cannot undo");
455
- return;
456
- }
457
- const undoItem = this._undoStack.pop();
458
- const { operations, originalOperations, options } = undoItem;
459
- this._redoStack.push({
460
- operations: originalOperations,
461
- originalOperations: operations,
462
- options,
463
- timestamp: Date.now()
464
- });
465
- this._isUndoRedo = true;
466
- try {
467
- this.updateData(operations, {
468
- ...options,
469
- isUndo: true,
470
- message: `Undo: ${options.message || "operation"}`
471
- });
472
- } finally {
473
- this._isUndoRedo = false;
474
- }
475
- return operations;
476
- }
477
- redo() {
478
- var _a;
479
- if (!this._redoStack.length) {
480
- const state = (_a = this._stateManager) == null ? void 0 : _a.root;
481
- const el = state.__element;
482
- el.call("openNotification", {
483
- type: "error",
484
- title: "Nothing to redo"
485
- });
486
- throw new Error("Nothing to redo");
487
- }
488
- if (!this.isConnected()) {
489
- console.warn("[CollabService] Not connected, cannot redo");
490
- return;
491
- }
492
- const redoItem = this._redoStack.pop();
493
- const { operations, originalOperations, options } = redoItem;
494
- this._undoStack.push({
495
- operations: originalOperations,
496
- originalOperations: operations,
497
- options,
498
- timestamp: Date.now()
499
- });
500
- this._isUndoRedo = true;
501
- try {
502
- this.updateData(operations, {
503
- ...options,
504
- isRedo: true,
505
- message: `Redo: ${options.message || "operation"}`
506
- });
507
- } finally {
508
- this._isUndoRedo = false;
509
- }
510
- return operations;
511
- }
512
- /* ---------- Undo/Redo State ---------- */
513
- canUndo() {
514
- return this._undoStack.length > 0;
515
- }
516
- canRedo() {
517
- return this._redoStack.length > 0;
518
- }
519
- getUndoStackSize() {
520
- return this._undoStack.length;
521
- }
522
- getRedoStackSize() {
523
- return this._redoStack.length;
524
- }
525
- clearUndoHistory() {
526
- this._undoStack.length = 0;
527
- this._redoStack.length = 0;
528
- }
529
- addItem(type, data, opts = {}) {
530
- try {
531
- import_validation.validateParams.type(type);
532
- import_validation.validateParams.data(data, type);
533
- const { value, ...schema } = data;
534
- const tuples = [
535
- ["update", [type, data.key], value],
536
- ["update", ["schema", type, data.key], schema || {}]
537
- ];
538
- const updatedOpts = { ...opts, skipComponentsChangedEvent: true };
539
- return this.updateData(tuples, updatedOpts);
540
- } catch (error) {
541
- throw new Error(`Failed to add item: ${error.message}`, { cause: error });
542
- }
543
- }
544
- addMultipleItems(items, opts = {}) {
545
- var _a;
546
- const tuples = [];
547
- try {
548
- items.forEach(([type, data]) => {
549
- import_validation.validateParams.type(type);
550
- import_validation.validateParams.data(data, type);
551
- const { value, ...schema } = data;
552
- tuples.push(
553
- ["update", [type, data.key], value],
554
- ["update", ["schema", type, data.key], schema]
555
- );
556
- });
557
- this.updateData([...tuples, ...opts.append || []], {
558
- message: `Created ${tuples.length} items`,
559
- ...opts
560
- });
561
- return tuples;
562
- } catch (error) {
563
- const state = (_a = this._stateManager) == null ? void 0 : _a.root;
564
- const el = state.__element;
565
- el.call("openNotification", {
566
- type: "error",
567
- title: "Failed to add item",
568
- message: error.message
569
- });
570
- throw new Error(`Failed to add item: ${error.message}`, { cause: error });
571
- }
572
- }
573
- updateItem(type, data, opts = {}) {
574
- var _a;
575
- try {
576
- import_validation.validateParams.type(type);
577
- import_validation.validateParams.data(data, type);
578
- const { value, ...schema } = data;
579
- const tuples = [
580
- ["update", [type, data.key], value],
581
- ["update", ["schema", type, data.key], schema]
582
- ];
583
- return this.updateData(tuples, opts);
584
- } catch (error) {
585
- const state = (_a = this._stateManager) == null ? void 0 : _a.root;
586
- const el = state.__element;
587
- el.call("openNotification", {
588
- type: "error",
589
- title: "Failed to update item",
590
- message: error.message
591
- });
592
- throw new Error(`Failed to update item: ${error.message}`, {
593
- cause: error
594
- });
595
- }
596
- }
597
- deleteItem(type, key, opts = {}) {
598
- var _a;
599
- try {
600
- import_validation.validateParams.type(type);
601
- import_validation.validateParams.key(key, type);
602
- const tuples = [
603
- ["delete", [type, key]],
604
- ["delete", ["schema", type, key]],
605
- ...opts.append || []
606
- ];
607
- return this.updateData(tuples, {
608
- message: `Deleted ${key} from ${type}`,
609
- ...opts
610
- });
611
- } catch (error) {
612
- const state = (_a = this._stateManager) == null ? void 0 : _a.root;
613
- const el = state.__element;
614
- el.call("openNotification", {
615
- type: "error",
616
- title: "Failed to delete item",
617
- message: error.message
618
- });
619
- throw new Error(`Failed to delete item: ${error.message}`, {
620
- cause: error
621
- });
622
- }
623
- }
624
- /* ---------- socket event helpers ---------- */
625
- /**
626
- * Handle "clients" or "presence" events coming from the collab socket.
627
- * The backend sends the full `clients` object which we directly patch to
628
- * the root state, mimicking the legacy SocketService behaviour so that
629
- * existing UI components keep working unmodified.
630
- */
631
- _handleClientsEvent(data = {}) {
632
- var _a;
633
- const root = (_a = this._stateManager) == null ? void 0 : _a.root;
634
- if (root && typeof root.replace === "function") {
635
- root.clients = data;
636
- }
637
- import_rootEventBus.rootBus.emit("clients:updated", data);
638
- }
639
- /* ---------- Dependency bundling events ---------- */
640
- _handleBundleDoneEvent({
641
- project,
642
- ticket,
643
- dependencies = {},
644
- schema = {}
645
- } = {}) {
646
- var _a;
647
- console.info("[CollabService] Bundle done", { project, ticket });
648
- try {
649
- this._ensureStateManager();
650
- const { dependencies: schemaDependencies = {} } = schema || {};
651
- const tuples = [
652
- ["update", ["dependencies"], dependencies],
653
- ["update", ["schema", "dependencies"], schemaDependencies]
654
- ];
655
- this._stateManager.applyChanges(tuples, {
656
- fromSocket: true,
657
- preventFetchDeps: true,
658
- preventUpdate: ["Iframe"]
659
- });
660
- } catch (err) {
661
- console.error("[CollabService] Failed to update deps after bundle", err);
662
- }
663
- const root = (_a = this._stateManager) == null ? void 0 : _a.root;
664
- const el = root == null ? void 0 : root.__element;
665
- if (el == null ? void 0 : el.call) {
666
- el.call("openNotification", {
667
- type: "success",
668
- title: "Dependencies ready",
669
- message: `Project ${project} dependencies have been bundled successfully.`
670
- });
671
- }
672
- import_rootEventBus.rootBus.emit("bundle:done", { project, ticket });
673
- }
674
- _handleBundleErrorEvent({ project, ticket, error } = {}) {
675
- var _a;
676
- console.error("[CollabService] Bundle error", { project, ticket, error });
677
- const root = (_a = this._stateManager) == null ? void 0 : _a.root;
678
- const el = root == null ? void 0 : root.__element;
679
- if (el == null ? void 0 : el.call) {
680
- el.call("openNotification", {
681
- type: "error",
682
- title: "Dependency bundle failed",
683
- message: error || `An error occurred while bundling dependencies for project ${project}.`
684
- });
685
- }
686
- import_rootEventBus.rootBus.emit("bundle:error", { project, ticket, error });
687
- }
688
- /* ---------- Manual checkpoint ---------- */
689
- _attachSocketLifecycleListeners() {
690
- var _a;
691
- const socket = (_a = this._client) == null ? void 0 : _a.socket;
692
- if (!socket) {
693
- return;
694
- }
695
- socket.on("connect", this._onSocketConnect);
696
- socket.on("disconnect", this._onSocketDisconnect);
697
- socket.on("connect_error", this._onSocketError);
698
- }
699
- _detachSocketLifecycleListeners() {
700
- var _a;
701
- const socket = (_a = this._client) == null ? void 0 : _a.socket;
702
- if (!socket) {
703
- return;
704
- }
705
- socket.off("connect", this._onSocketConnect);
706
- socket.off("disconnect", this._onSocketDisconnect);
707
- socket.off("connect_error", this._onSocketError);
708
- }
709
- _onSocketConnect() {
710
- this._connected = true;
711
- }
712
- _onSocketDisconnect(reason) {
713
- this._connected = false;
714
- if (reason && reason !== "io client disconnect") {
715
- console.warn("[CollabService] Socket disconnected", reason);
716
- }
717
- }
718
- _onSocketError(error) {
719
- console.warn("[CollabService] Socket connection error", error);
720
- }
721
- /**
722
- * Manually request a checkpoint / commit of buffered operations on the server.
723
- * Resolves with the new version number once the backend confirms via the
724
- * regular "commit" event.
725
- */
726
- checkpoint() {
727
- if (!this.isConnected()) {
728
- console.warn("[CollabService] Not connected, cannot request checkpoint");
729
- return Promise.reject(new Error("Not connected"));
730
- }
731
- return new Promise((resolve) => {
732
- var _a, _b;
733
- const handler = ({ version }) => {
734
- var _a2;
735
- (_a2 = this.socket) == null ? void 0 : _a2.off("commit", handler);
736
- import_rootEventBus.rootBus.emit("checkpoint:done", { version, origin: "manual" });
737
- resolve(version);
738
- };
739
- (_a = this.socket) == null ? void 0 : _a.once("commit", handler);
740
- (_b = this.socket) == null ? void 0 : _b.emit("checkpoint");
741
- });
742
- }
743
- }