@engenty/wizards-runtime 0.9.8 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/apps/runtime/dist/app.js +18 -1
  2. package/apps/runtime/dist/auth/index.js +10 -1
  3. package/apps/runtime/dist/db/client.js +9 -1
  4. package/apps/runtime/dist/db/migrations/tenant/0022_sync.sql +247 -0
  5. package/apps/runtime/dist/db/migrations/tenant/meta/0022_snapshot.json +3792 -0
  6. package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +7 -0
  7. package/apps/runtime/dist/db/schema.js +60 -0
  8. package/apps/runtime/dist/docs/user-guide/cloud-sync.md +85 -0
  9. package/apps/runtime/dist/docs/user-guide/data.md +4 -4
  10. package/apps/runtime/dist/docs/user-guide/meta.json +1 -0
  11. package/apps/runtime/dist/docs/user-guide/test-and-share.md +4 -14
  12. package/apps/runtime/dist/files/blobs.js +13 -0
  13. package/apps/runtime/dist/index.js +7 -1
  14. package/apps/runtime/dist/plugins/jobs.js +7 -0
  15. package/apps/runtime/dist/routes/api.js +107 -0
  16. package/apps/runtime/dist/routes/studio.js +62 -0
  17. package/apps/runtime/dist/services/cloud.js +39 -5
  18. package/apps/runtime/dist/services/projects.js +11 -0
  19. package/apps/runtime/dist/sync/apply.js +39 -0
  20. package/apps/runtime/dist/sync/client.js +828 -0
  21. package/apps/runtime/dist/sync/hub.js +324 -0
  22. package/apps/runtime/dist/sync/log.js +74 -0
  23. package/apps/runtime/dist/sync/records.js +943 -0
  24. package/apps/runtime/dist/sync/state.js +17 -0
  25. package/apps/web/dist/assets/{AboutDialog-B_Ixp_Tb.js → AboutDialog-D1YHT0jc.js} +1 -1
  26. package/apps/web/dist/assets/{ChangelogDialog-BpEIbAZ8.js → ChangelogDialog-Bv02g8av.js} +1 -1
  27. package/apps/web/dist/assets/{EditorPage-CKHY4hn4.js → EditorPage-BThoZwqO.js} +4 -4
  28. package/apps/web/dist/assets/{FlowPreview-APXdRflR.js → FlowPreview-dc5C92kK.js} +1 -1
  29. package/apps/web/dist/assets/{Harness-BsGXMlLg.js → Harness-D0nFOIVx.js} +1 -1
  30. package/apps/web/dist/assets/{Marketplace-Dr-Qnb-F.js → Marketplace-Cnd8BF3q.js} +2 -2
  31. package/apps/web/dist/assets/{NewWizardChannels-0JrRdUFd.js → NewWizardChannels-i62Y_2DZ.js} +1 -1
  32. package/apps/web/dist/assets/{NewWizardPage-9igFL5O8.js → NewWizardPage-BcpRA1fx.js} +1 -1
  33. package/apps/web/dist/assets/{OnboardingPage-BcK9FUnU.js → OnboardingPage-BcMDEXNq.js} +1 -1
  34. package/apps/web/dist/assets/PreviewPane-GHcVV2Hu.js +4 -0
  35. package/apps/web/dist/assets/ProjectPage-TDywzUOd.js +1 -0
  36. package/apps/web/dist/assets/{PublicRunner-CZR1ziQ4.js → PublicRunner-B6JINLpR.js} +1 -1
  37. package/apps/web/dist/assets/{ReadOnly-BraZbEG_.js → ReadOnly-Mv5M2OV9.js} +1 -1
  38. package/apps/web/dist/assets/SettingsPage-B-F1dLgt.js +2 -0
  39. package/apps/web/dist/assets/{SetupPage-CvkE2aKx.js → SetupPage-CVdsdVBW.js} +1 -1
  40. package/apps/web/dist/assets/{SharePage-Dk7zC5Zo.js → SharePage-CBSRK0wF.js} +1 -1
  41. package/apps/web/dist/assets/changelog-BR1lxaWv.js +1 -0
  42. package/apps/web/dist/assets/{coins-391m1e4_.js → coins-BGZKYmu6.js} +1 -1
  43. package/apps/web/dist/assets/index-D3-kv1WF.js +14 -0
  44. package/apps/web/dist/assets/{phone-DvgrtyNO.js → phone-BzHhwehj.js} +1 -1
  45. package/apps/web/dist/assets/{runners-4OBelIgF.js → runners-C1-zIlry.js} +1 -1
  46. package/apps/web/dist/assets/{session-DvnGHbDz.js → session-B_JeZnJ0.js} +1 -1
  47. package/apps/web/dist/assets/{settings-menu-xK9A-gaP.js → settings-menu-BZRGhsIO.js} +1 -1
  48. package/apps/web/dist/assets/{theme-D3yDT4QT.js → theme-HcCfZDzS.js} +2 -2
  49. package/apps/web/dist/assets/{ui-BVQk2DtW.js → ui-BI1RPFWT.js} +1 -1
  50. package/apps/web/dist/assets/{upload-BgXbzG6C.js → upload-CwT8oMz0.js} +1 -1
  51. package/apps/web/dist/assets/{useMutation-DkwSQWoD.js → useMutation-byk81dDj.js} +1 -1
  52. package/apps/web/dist/assets/{volume-2-DHGoe3Cg.js → volume-2-B1Pz6Iwt.js} +1 -1
  53. package/apps/web/dist/assets/{wrench-Cf_aE3O0.js → wrench-CvzXRbWN.js} +1 -1
  54. package/apps/web/dist/index.html +7 -7
  55. package/apps/web/dist/mcp/flow.html +1 -1
  56. package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +6 -0
  57. package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +6 -0
  58. package/node_modules/@engenty/wizards-shared/dist/run.d.ts +6 -0
  59. package/node_modules/@engenty/wizards-shared/dist/run.js +12 -0
  60. package/node_modules/@engenty/wizards-shared/dist/sync-merge.d.ts +55 -0
  61. package/node_modules/@engenty/wizards-shared/dist/sync-merge.js +214 -0
  62. package/node_modules/@engenty/wizards-shared/dist/sync.d.ts +166 -0
  63. package/node_modules/@engenty/wizards-shared/dist/sync.js +40 -0
  64. package/node_modules/@engenty/wizards-shared/dist/text-merge.d.ts +14 -0
  65. package/node_modules/@engenty/wizards-shared/dist/text-merge.js +137 -0
  66. package/package.json +1 -1
  67. package/plugin/.claude-plugin/plugin.json +1 -1
  68. package/plugin/plugin.json +1 -1
  69. package/apps/web/dist/assets/PreviewPane-X4LBqA0D.js +0 -4
  70. package/apps/web/dist/assets/ProjectPage-CG0i-KhR.js +0 -1
  71. package/apps/web/dist/assets/SettingsPage-Bw4tdoV7.js +0 -2
  72. package/apps/web/dist/assets/changelog-DDXnAaHm.js +0 -1
  73. package/apps/web/dist/assets/index-BTUtot4r.js +0 -14
@@ -27,6 +27,12 @@ export declare const FEATURES: {
27
27
  readonly label: "Account in a local install";
28
28
  readonly hint: "Linking, Settings → Account, the offer to sign in.";
29
29
  };
30
+ readonly twoWaySync: {
31
+ readonly kind: "release";
32
+ readonly default: true;
33
+ readonly label: "Two-way sync with the cloud";
34
+ readonly hint: "A local install with an account keeps its spaces in step with the organization in the cloud, both ways: wizards, knowledge, data and files.";
35
+ };
30
36
  readonly unsetConnectors: {
31
37
  readonly kind: "release";
32
38
  readonly default: false;
@@ -19,6 +19,12 @@ export const FEATURES = {
19
19
  label: "Account in a local install",
20
20
  hint: "Linking, Settings → Account, the offer to sign in.",
21
21
  },
22
+ twoWaySync: {
23
+ kind: "release",
24
+ default: true,
25
+ label: "Two-way sync with the cloud",
26
+ hint: "A local install with an account keeps its spaces in step with the organization in the cloud, both ways: wizards, knowledge, data and files.",
27
+ },
22
28
  unsetConnectors: {
23
29
  kind: "release",
24
30
  default: false,
@@ -428,6 +428,12 @@ declare const NOTES_EN: {
428
428
  };
429
429
  readonly accountSignedOut: "The account is signed out. Sign in again.";
430
430
  readonly cloudNotReachedRight: "The cloud can’t be reached right now.";
431
+ readonly syncUpdateHere: "The cloud runs a newer version than this computer. Update engenty wizards, then your changes arrive in the cloud again.";
432
+ readonly syncUpdateCloud: "This computer runs a newer version than the cloud. Your changes arrive there once the cloud is updated.";
433
+ readonly syncSpaceNotInCloud: "This space isn’t in the cloud.";
434
+ readonly syncOtherSpace: "In the cloud this belongs to another space.";
435
+ readonly syncChangeRefused: "The cloud doesn’t take this change: {detail}";
436
+ readonly syncFileMissing: "A file of this change hasn’t arrived in the cloud yet. It’s sent again.";
431
437
  readonly publishWizardFirst: "Publish the wizard first.";
432
438
  readonly wizardJsonUnreadable: "This isn’t a wizard file: its wizard.json can’t be read.";
433
439
  readonly wizardFileComesNewer: "This wizard file comes from a newer version, or it’s damaged. Update engenty wizards or export the wizard again.";
@@ -257,6 +257,12 @@ const NOTES_EN = {
257
257
  },
258
258
  accountSignedOut: "The account is signed out. Sign in again.",
259
259
  cloudNotReachedRight: "The cloud can’t be reached right now.",
260
+ syncUpdateHere: "The cloud runs a newer version than this computer. Update engenty wizards, then your changes arrive in the cloud again.",
261
+ syncUpdateCloud: "This computer runs a newer version than the cloud. Your changes arrive there once the cloud is updated.",
262
+ syncSpaceNotInCloud: "This space isn’t in the cloud.",
263
+ syncOtherSpace: "In the cloud this belongs to another space.",
264
+ syncChangeRefused: "The cloud doesn’t take this change: {detail}",
265
+ syncFileMissing: "A file of this change hasn’t arrived in the cloud yet. It’s sent again.",
260
266
  publishWizardFirst: "Publish the wizard first.",
261
267
  wizardJsonUnreadable: "This isn’t a wizard file: its wizard.json can’t be read.",
262
268
  wizardFileComesNewer: "This wizard file comes from a newer version, or it’s damaged. Update engenty wizards or export the wizard again.",
@@ -560,6 +566,12 @@ const NOTES_DE = {
560
566
  },
561
567
  accountSignedOut: "Das Konto ist abgemeldet. Melde dich neu an.",
562
568
  cloudNotReachedRight: "Die Cloud ist gerade nicht erreichbar.",
569
+ syncUpdateHere: "Die Cloud hat eine neuere Version als dieser Computer. Aktualisiere engenty wizards, dann kommen deine Änderungen wieder in der Cloud an.",
570
+ syncUpdateCloud: "Dieser Computer hat eine neuere Version als die Cloud. Deine Änderungen kommen dort an, sobald die Cloud aktualisiert ist.",
571
+ syncSpaceNotInCloud: "Diesen Space gibt es in der Cloud nicht.",
572
+ syncOtherSpace: "In der Cloud gehört das zu einem anderen Space.",
573
+ syncChangeRefused: "Diese Änderung nimmt die Cloud nicht an: {detail}",
574
+ syncFileMissing: "Eine Datei dieser Änderung ist noch nicht in der Cloud angekommen. Sie wird noch einmal gesendet.",
563
575
  publishWizardFirst: "Veröffentliche den Wizard zuerst.",
564
576
  wizardJsonUnreadable: "Das ist keine Wizard-Datei: Die wizard.json darin lässt sich nicht lesen.",
565
577
  wizardFileComesNewer: "Diese Wizard-Datei stammt aus einer neueren Version oder ist beschädigt. Aktualisiere engenty wizards oder exportiere den Wizard neu.",
@@ -0,0 +1,55 @@
1
+ /**
2
+ * A three-way merge of JSON, for what syncs between an install and the cloud
3
+ * (docs/plans/tenant-sync.md): a wizard's definition, a page, a row, the space's details.
4
+ * Objects merge key by key; a list of items that each carry an `id` merges item by item, its
5
+ * order taken from the side that changed it; text that both sides changed merges line by line.
6
+ * Everything else is one value: changed on one side, that side's; changed on both to different
7
+ * values, a conflict, named by where it is. Unlike `mergeDrafts` (merge.ts), which lets the
8
+ * studio's side win, this one says what it could not settle.
9
+ */
10
+ /** A step on the way to a value: a key of an object, or the item of a list with that `id`. */
11
+ export type MergePathPart = string | {
12
+ id: string;
13
+ };
14
+ export type MergePath = MergePathPart[];
15
+ export interface MergeConflict {
16
+ path: MergePath;
17
+ /**
18
+ * `value`: both changed it differently. `removed`: one side removed an item the other changed
19
+ * (the removed side's value is `undefined`). `order`: both reordered a list differently; the
20
+ * values are the lists of ids.
21
+ */
22
+ kind: "value" | "removed" | "order";
23
+ base: unknown;
24
+ local: unknown;
25
+ remote: unknown;
26
+ }
27
+ export interface JsonMerge<T> {
28
+ /** The merge; at a conflict it holds the local side. */
29
+ value: T;
30
+ conflicts: MergeConflict[];
31
+ }
32
+ export declare function mergeJson<T>(base: T, local: T, remote: T): JsonMerge<T>;
33
+ /** What the person chose for one conflict: a side, or a value of their own. */
34
+ export type MergeChoice = {
35
+ take: "local" | "remote";
36
+ } | {
37
+ value: unknown;
38
+ };
39
+ /** The value at `path`, or `undefined`. */
40
+ export declare function valueAt(root: unknown, path: MergePath): unknown;
41
+ /**
42
+ * `root` with `value` at `path` (`undefined` removes it). An item that is not in its list goes
43
+ * where it stands in `hint`, the side it came from.
44
+ */
45
+ export declare function withValueAt(root: unknown, path: MergePath, value: unknown, hint?: unknown): unknown;
46
+ /**
47
+ * The merge with each conflict settled as chosen, in order. A conflict without a choice keeps
48
+ * the local side, which the merge already holds.
49
+ */
50
+ export declare function settleConflicts<T>(merged: T, conflicts: MergeConflict[], choices: (MergeChoice | null | undefined)[], sides: {
51
+ local: T;
52
+ remote: T;
53
+ }): T;
54
+ /** A path as people read it, given the names of its items: `Steps › “File the report” › title`. */
55
+ export declare function pathLabel(path: MergePath, root: unknown, nameOf?: (item: unknown) => string | null): string;
@@ -0,0 +1,214 @@
1
+ import { mergeText } from "./text-merge.js";
2
+ const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
3
+ const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
4
+ const idOf = (v) => (isObject(v) && typeof v.id === "string" ? v.id : null);
5
+ const keyed = (v) => Array.isArray(v) && v.every((x) => idOf(x) !== null) && new Set(v.map(idOf)).size === v.length;
6
+ const multiline = (...values) => values.every((v) => typeof v === "string") && values.some((v) => v.includes("\n"));
7
+ export function mergeJson(base, local, remote) {
8
+ const conflicts = [];
9
+ const value = merge(base, local, remote, [], conflicts);
10
+ return { value, conflicts };
11
+ }
12
+ function merge(base, local, remote, path, conflicts) {
13
+ if (same(local, remote)) {
14
+ return local;
15
+ }
16
+ if (same(base, local)) {
17
+ return remote;
18
+ }
19
+ if (same(base, remote)) {
20
+ return local;
21
+ }
22
+ if (isObject(local) && isObject(remote)) {
23
+ const b = isObject(base) ? base : {};
24
+ const out = {};
25
+ // The local side's key order, then keys only the remote side has.
26
+ for (const key of new Set([...Object.keys(local), ...Object.keys(remote), ...Object.keys(b)])) {
27
+ const v = merge(b[key], local[key], remote[key], [...path, key], conflicts);
28
+ if (v !== undefined) {
29
+ out[key] = v;
30
+ }
31
+ }
32
+ return out;
33
+ }
34
+ if (keyed(local) && keyed(remote) && (base === undefined || keyed(base))) {
35
+ return mergeList(base ?? [], local, remote, path, conflicts);
36
+ }
37
+ if (multiline(base ?? "", local, remote)) {
38
+ const text = mergeText(base ?? "", local, remote);
39
+ if (text.clean) {
40
+ return text.text;
41
+ }
42
+ }
43
+ conflicts.push({ path, kind: "value", base, local, remote });
44
+ return local;
45
+ }
46
+ function mergeList(base, local, remote, path, conflicts) {
47
+ const by = (list) => new Map(list.map((x) => [idOf(x), x]));
48
+ const [B, L, R] = [by(base), by(local), by(remote)];
49
+ const items = new Map();
50
+ for (const id of new Set([...L.keys(), ...R.keys(), ...B.keys()])) {
51
+ const [b, l, r] = [B.get(id), L.get(id), R.get(id)];
52
+ const at = [...path, { id }];
53
+ if (b !== undefined && (l === undefined || r === undefined)) {
54
+ // Removed on one side: gone, unless the other side changed it meanwhile.
55
+ const kept = l ?? r;
56
+ if (kept !== undefined && !same(kept, b)) {
57
+ conflicts.push({ path: at, kind: "removed", base: b, local: l, remote: r });
58
+ if (l !== undefined) {
59
+ items.set(id, l);
60
+ }
61
+ }
62
+ continue;
63
+ }
64
+ const v = merge(b, l, r, at, conflicts);
65
+ if (v !== undefined) {
66
+ items.set(id, v);
67
+ }
68
+ }
69
+ // The order: of the items all three have, the side that moved them wins.
70
+ const common = (list) => list.map((x) => idOf(x)).filter((id) => B.has(id) && L.has(id) && R.has(id));
71
+ const [ob, ol, or] = [common(base), common(local), common(remote)];
72
+ const localMoved = !same(ob, ol);
73
+ const remoteMoved = !same(ob, or);
74
+ if (localMoved && remoteMoved && !same(ol, or)) {
75
+ conflicts.push({ path, kind: "order", base: ob, local: ol, remote: or });
76
+ }
77
+ const order = (remoteMoved && !localMoved ? remote : local).map((x) => idOf(x));
78
+ // Items the chosen order lacks: after the item they follow on their own side, and after what
79
+ // the other side added there (the local side's additions come first).
80
+ for (const side of [local, remote]) {
81
+ const own = new Set(side.map((x) => idOf(x)));
82
+ side.forEach((x, i) => {
83
+ const id = idOf(x);
84
+ if (order.includes(id)) {
85
+ return;
86
+ }
87
+ const before = side
88
+ .slice(0, i)
89
+ .reverse()
90
+ .find((p) => order.includes(idOf(p)));
91
+ let at = before ? order.indexOf(idOf(before)) + 1 : 0;
92
+ while (at < order.length && !B.has(order[at]) && !own.has(order[at])) {
93
+ at++;
94
+ }
95
+ order.splice(at, 0, id);
96
+ });
97
+ }
98
+ return order.filter((id) => items.has(id)).map((id) => items.get(id));
99
+ }
100
+ /** The value at `path`, or `undefined`. */
101
+ export function valueAt(root, path) {
102
+ let at = root;
103
+ for (const part of path) {
104
+ if (typeof part === "string") {
105
+ at = isObject(at) ? at[part] : undefined;
106
+ }
107
+ else {
108
+ at = Array.isArray(at) ? at.find((x) => idOf(x) === part.id) : undefined;
109
+ }
110
+ }
111
+ return at;
112
+ }
113
+ /**
114
+ * `root` with `value` at `path` (`undefined` removes it). An item that is not in its list goes
115
+ * where it stands in `hint`, the side it came from.
116
+ */
117
+ export function withValueAt(root, path, value, hint) {
118
+ if (!path.length) {
119
+ return value;
120
+ }
121
+ const [part, ...rest] = path;
122
+ if (typeof part === "string") {
123
+ const obj = isObject(root) ? { ...root } : {};
124
+ const next = withValueAt(obj[part], rest, value, valueAt(hint, [part]));
125
+ if (next === undefined) {
126
+ delete obj[part];
127
+ }
128
+ else {
129
+ obj[part] = next;
130
+ }
131
+ return obj;
132
+ }
133
+ const list = Array.isArray(root) ? [...root] : [];
134
+ const at = list.findIndex((x) => idOf(x) === part.id);
135
+ const hintList = Array.isArray(hint) ? hint : [];
136
+ const next = withValueAt(at === -1 ? undefined : list[at], rest, value, hintList.find((x) => idOf(x) === part.id));
137
+ if (at !== -1) {
138
+ if (next === undefined) {
139
+ list.splice(at, 1);
140
+ }
141
+ else {
142
+ list[at] = next;
143
+ }
144
+ return list;
145
+ }
146
+ if (next === undefined) {
147
+ return list;
148
+ }
149
+ // Where it stands on its own side: after the item it follows there.
150
+ const i = hintList.findIndex((x) => idOf(x) === part.id);
151
+ const before = hintList
152
+ .slice(0, Math.max(0, i))
153
+ .reverse()
154
+ .map(idOf)
155
+ .find((id) => list.some((x) => idOf(x) === id));
156
+ list.splice(before ? list.findIndex((x) => idOf(x) === before) + 1 : 0, 0, next);
157
+ return list;
158
+ }
159
+ /** A list in the order of `ids`; items not named keep their place after the one they follow. */
160
+ function reordered(list, ids) {
161
+ if (!Array.isArray(list)) {
162
+ return list;
163
+ }
164
+ const named = ids.map((id) => list.find((x) => idOf(x) === id)).filter((x) => x !== undefined);
165
+ const rest = list.filter((x) => !ids.includes(idOf(x) ?? ""));
166
+ const out = [...named];
167
+ for (const x of rest) {
168
+ const i = list.indexOf(x);
169
+ const before = list
170
+ .slice(0, i)
171
+ .reverse()
172
+ .find((p) => out.includes(p));
173
+ out.splice(before ? out.indexOf(before) + 1 : 0, 0, x);
174
+ }
175
+ return out;
176
+ }
177
+ /**
178
+ * The merge with each conflict settled as chosen, in order. A conflict without a choice keeps
179
+ * the local side, which the merge already holds.
180
+ */
181
+ export function settleConflicts(merged, conflicts, choices, sides) {
182
+ let out = merged;
183
+ conflicts.forEach((c, i) => {
184
+ const choice = choices[i];
185
+ if (!choice) {
186
+ return;
187
+ }
188
+ const pick = "take" in choice ? (choice.take === "local" ? c.local : c.remote) : choice.value;
189
+ const from = "take" in choice ? (choice.take === "local" ? sides.local : sides.remote) : merged;
190
+ if (c.kind === "order") {
191
+ out = withValueAt(out, c.path, reordered(valueAt(out, c.path), pick));
192
+ return;
193
+ }
194
+ out = withValueAt(out, c.path, pick, from);
195
+ });
196
+ return out;
197
+ }
198
+ /** A path as people read it, given the names of its items: `Steps › “File the report” › title`. */
199
+ export function pathLabel(path, root, nameOf) {
200
+ const parts = [];
201
+ let at = root;
202
+ for (const part of path) {
203
+ if (typeof part === "string") {
204
+ parts.push(part);
205
+ at = isObject(at) ? at[part] : undefined;
206
+ }
207
+ else {
208
+ at = Array.isArray(at) ? at.find((x) => idOf(x) === part.id) : undefined;
209
+ const name = (at !== undefined && nameOf?.(at)) || part.id;
210
+ parts.push(`“${name}”`);
211
+ }
212
+ }
213
+ return parts.join(" › ");
214
+ }
@@ -0,0 +1,166 @@
1
+ import { z } from "zod";
2
+ import type { MergeConflict } from "./sync-merge.js";
3
+ /**
4
+ * The two-way sync of a space between a local install and its organization in the cloud
5
+ * (docs/plans/tenant-sync.md, docs/manage-contract.md "Two-way sync of a space"). The cloud is
6
+ * the hub: it numbers every change (`seq`); an install pushes what it changed on top of the
7
+ * version it last had of each item, and pulls what changed after the last number it saw.
8
+ */
9
+ /**
10
+ * What syncs, one row each: the space's details, a wizard (its draft and workspace together),
11
+ * a translation, a message of a wizard's build chat, a page, a table, a row, a category (with
12
+ * its values), a file, an imported connector. Runs, chats, the store and the index do not.
13
+ */
14
+ export declare const SYNC_KINDS: readonly ["space", "wizard", "translation", "message", "page", "table", "row", "category", "file", "connector"];
15
+ export type SyncKind = (typeof SYNC_KINDS)[number];
16
+ /** One item as it travels: its fields, never the tenant's id; `null` = deleted. */
17
+ export type SyncRecord = Record<string, unknown>;
18
+ /** An item that changed in the cloud, as a pull hands it out. */
19
+ export interface SyncChange {
20
+ kind: SyncKind;
21
+ item: string;
22
+ /** The cloud's number of its last change. */
23
+ seq: number;
24
+ record: SyncRecord | null;
25
+ }
26
+ /** Items carry ids as installs make them (nanoid), or `<id>:<part>` for a wizard's translations and a space's connectors. */
27
+ export declare const syncItemId: z.ZodString;
28
+ export declare const pushItemSchema: z.ZodObject<{
29
+ kind: z.ZodEnum<{
30
+ category: "category";
31
+ connector: "connector";
32
+ file: "file";
33
+ message: "message";
34
+ page: "page";
35
+ row: "row";
36
+ space: "space";
37
+ table: "table";
38
+ translation: "translation";
39
+ wizard: "wizard";
40
+ }>;
41
+ item: z.ZodString;
42
+ base: z.ZodNumber;
43
+ record: z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
44
+ }, z.core.$strip>;
45
+ export type PushItem = z.infer<typeof pushItemSchema>;
46
+ export declare const pushSchema: z.ZodObject<{
47
+ schema: z.ZodString;
48
+ items: z.ZodArray<z.ZodObject<{
49
+ kind: z.ZodEnum<{
50
+ category: "category";
51
+ connector: "connector";
52
+ file: "file";
53
+ message: "message";
54
+ page: "page";
55
+ row: "row";
56
+ space: "space";
57
+ table: "table";
58
+ translation: "translation";
59
+ wizard: "wizard";
60
+ }>;
61
+ item: z.ZodString;
62
+ base: z.ZodNumber;
63
+ record: z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
64
+ }, z.core.$strip>>;
65
+ }, z.core.$strip>;
66
+ export type PushInput = z.infer<typeof pushSchema>;
67
+ export type PushResult = {
68
+ kind: SyncKind;
69
+ item: string;
70
+ status: "taken";
71
+ seq: number;
72
+ }
73
+ /** The cloud's version changed after `base`: here it is, to merge with. */
74
+ | {
75
+ kind: SyncKind;
76
+ item: string;
77
+ status: "conflict";
78
+ seq: number;
79
+ record: SyncRecord | null;
80
+ }
81
+ /** Not taken, for a reason only the person can mend (a role, a limit, an id another item has). */
82
+ | {
83
+ kind: SyncKind;
84
+ item: string;
85
+ status: "refused";
86
+ reason: string;
87
+ message: string;
88
+ };
89
+ export interface PushAnswer {
90
+ results: PushResult[];
91
+ /** The cloud's last number after the push. */
92
+ seq: number;
93
+ }
94
+ export interface PullAnswer {
95
+ changes: SyncChange[];
96
+ /** Ask again from here. */
97
+ next: number;
98
+ /** More changes wait after `next`. */
99
+ more: boolean;
100
+ }
101
+ /** The organization as the sync sees it: its schema, how many spaces it may have, its spaces. */
102
+ export interface SyncOverview {
103
+ tenantId: string;
104
+ schema: string;
105
+ role: string;
106
+ limits: {
107
+ projects: number | null;
108
+ };
109
+ spaces: {
110
+ id: string;
111
+ name: string;
112
+ seq: number;
113
+ }[];
114
+ }
115
+ export declare const blobHash: z.ZodString;
116
+ export declare const missingBlobsSchema: z.ZodObject<{
117
+ hashes: z.ZodArray<z.ZodString>;
118
+ }, z.core.$strip>;
119
+ /** Where a space stands with the cloud, for the studio. */
120
+ export interface SpaceSyncState {
121
+ spaceId: string;
122
+ /** This space syncs both ways. */
123
+ on: boolean;
124
+ /** When the last push or pull arrived. */
125
+ syncedAt: string | null;
126
+ /** Changes made here that have not arrived in the cloud yet. */
127
+ waiting: number;
128
+ conflicts: number;
129
+ /** Merges made by themselves since they were last seen. */
130
+ merges: number;
131
+ /** Why the last try did not arrive; null when it did. */
132
+ error: {
133
+ message: string;
134
+ reason?: string;
135
+ at: string;
136
+ } | null;
137
+ }
138
+ /** An item where both sides changed the same thing: what each has, and where they differ. */
139
+ export interface SyncConflictView {
140
+ id: string;
141
+ spaceId: string;
142
+ kind: SyncKind;
143
+ item: string;
144
+ /** The item's name: a wizard's title, a page's title, a row's first cell. */
145
+ title: string;
146
+ /** Deleted on one side. */
147
+ deleted: "local" | "remote" | null;
148
+ conflicts: (MergeConflict & {
149
+ label: string;
150
+ })[];
151
+ at: string;
152
+ }
153
+ /** A merge the sync made by itself, for the notice on the item. */
154
+ export interface SyncMergeView {
155
+ id: string;
156
+ spaceId: string;
157
+ kind: SyncKind;
158
+ item: string;
159
+ title: string;
160
+ /** What each side changed, as labels of the places. */
161
+ local: string[];
162
+ remote: string[];
163
+ at: string;
164
+ /** The versions before the merge are kept, so it can be undone. */
165
+ undoable: boolean;
166
+ }
@@ -0,0 +1,40 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * The two-way sync of a space between a local install and its organization in the cloud
4
+ * (docs/plans/tenant-sync.md, docs/manage-contract.md "Two-way sync of a space"). The cloud is
5
+ * the hub: it numbers every change (`seq`); an install pushes what it changed on top of the
6
+ * version it last had of each item, and pulls what changed after the last number it saw.
7
+ */
8
+ /**
9
+ * What syncs, one row each: the space's details, a wizard (its draft and workspace together),
10
+ * a translation, a message of a wizard's build chat, a page, a table, a row, a category (with
11
+ * its values), a file, an imported connector. Runs, chats, the store and the index do not.
12
+ */
13
+ export const SYNC_KINDS = [
14
+ "space",
15
+ "wizard",
16
+ "translation",
17
+ "message",
18
+ "page",
19
+ "table",
20
+ "row",
21
+ "category",
22
+ "file",
23
+ "connector",
24
+ ];
25
+ /** Items carry ids as installs make them (nanoid), or `<id>:<part>` for a wizard's translations and a space's connectors. */
26
+ export const syncItemId = z.string().regex(/^[A-Za-z0-9_-]{1,64}(:[A-Za-z0-9_.-]{1,64})?$/);
27
+ export const pushItemSchema = z.object({
28
+ kind: z.enum(SYNC_KINDS),
29
+ item: syncItemId,
30
+ /** The cloud's number of the version this change was made on; 0 for an item new to the cloud. */
31
+ base: z.number().int().min(0),
32
+ record: z.record(z.string(), z.unknown()).nullable(),
33
+ });
34
+ export const pushSchema = z.object({
35
+ /** The tenant schema the install writes, the last migration's tag: both sides must have the same. */
36
+ schema: z.string().min(1).max(100),
37
+ items: z.array(pushItemSchema).max(500),
38
+ });
39
+ export const blobHash = z.string().regex(/^[a-f0-9]{64}$/);
40
+ export const missingBlobsSchema = z.object({ hashes: z.array(blobHash).max(2000) });
@@ -0,0 +1,14 @@
1
+ /**
2
+ * A classic three-way merge of text, line by line (diff3): what one side changed and the other
3
+ * left as it was is taken; where both changed the same lines differently, the merge is not
4
+ * clean. For prose and code (a page's Markdown, a widget's file), not for JSON: a wizard merges
5
+ * by its structure (sync-merge.ts).
6
+ */
7
+ export interface TextMerge {
8
+ /** The merged text; where it is not clean, the local side's lines stand in the conflict's place. */
9
+ text: string;
10
+ clean: boolean;
11
+ /** How many places both sides changed differently. */
12
+ conflicts: number;
13
+ }
14
+ export declare function mergeText(base: string, local: string, remote: string): TextMerge;