@fleetless/contracts 13.3.0 → 15.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/CHANGELOG.md +304 -0
  2. package/artifacts/openapi.json +20156 -10513
  3. package/artifacts/routes.json +1429 -292
  4. package/artifacts/schema/access-warning.schema.json +51 -0
  5. package/artifacts/schema/action-config.schema.json +35 -0
  6. package/artifacts/schema/addon-catalogue-entry.schema.json +3 -1
  7. package/artifacts/schema/admin-plan-change-request.schema.json +26 -0
  8. package/artifacts/schema/app-access-response.schema.json +136 -0
  9. package/artifacts/schema/app-access.schema.json +80 -0
  10. package/artifacts/schema/app-deletion-summary.schema.json +15 -8
  11. package/artifacts/schema/app-invitation-list-response.schema.json +13 -6
  12. package/artifacts/schema/app-invitation.schema.json +5 -5
  13. package/artifacts/schema/app-list-response.schema.json +6 -6
  14. package/artifacts/schema/app-lock.schema.json +30 -0
  15. package/artifacts/schema/app-user-list-response.schema.json +13 -6
  16. package/artifacts/schema/app-user.schema.json +13 -6
  17. package/artifacts/schema/app.schema.json +6 -6
  18. package/artifacts/schema/asset-plan-limit-details.schema.json +3 -1
  19. package/artifacts/schema/auth-ok.schema.json +20 -6
  20. package/artifacts/schema/camera-list-response.schema.json +1 -1
  21. package/artifacts/schema/client-identity.schema.json +21 -6
  22. package/artifacts/schema/cloud-config.schema.json +359 -5
  23. package/artifacts/schema/collection-actor.schema.json +35 -0
  24. package/artifacts/schema/collection-definitions-response.schema.json +612 -0
  25. package/artifacts/schema/collection-event.schema.json +216 -0
  26. package/artifacts/schema/collection-export-query.schema.json +56 -0
  27. package/artifacts/schema/collection-filter.schema.json +9 -0
  28. package/artifacts/schema/collection-import-preview.schema.json +143 -0
  29. package/artifacts/schema/collection-import-request.schema.json +105 -0
  30. package/artifacts/schema/collection-import-result.schema.json +38 -0
  31. package/artifacts/schema/collection-list-response.schema.json +119 -0
  32. package/artifacts/schema/collection-record-get-query.schema.json +12 -0
  33. package/artifacts/schema/collection-record-history-query.schema.json +35 -0
  34. package/artifacts/schema/collection-record-history-response.schema.json +389 -0
  35. package/artifacts/schema/collection-record-list-query.schema.json +67 -0
  36. package/artifacts/schema/collection-record-list-response.schema.json +212 -0
  37. package/artifacts/schema/collection-record-restore-request.schema.json +14 -0
  38. package/artifacts/schema/collection-record-write.schema.json +31 -0
  39. package/artifacts/schema/collection-record.schema.json +180 -0
  40. package/artifacts/schema/collection-revision.schema.json +295 -0
  41. package/artifacts/schema/collection-subscribe-error.schema.json +31 -0
  42. package/artifacts/schema/collection-subscribe.schema.json +48 -0
  43. package/artifacts/schema/collection-subscribed.schema.json +22 -0
  44. package/artifacts/schema/collection-summary.schema.json +92 -0
  45. package/artifacts/schema/collection-unsubscribe.schema.json +22 -0
  46. package/artifacts/schema/collections-access-change.schema.json +34 -0
  47. package/artifacts/schema/collections-document.schema.json +732 -0
  48. package/artifacts/schema/collections-draft-response.schema.json +845 -0
  49. package/artifacts/schema/collections-effect.schema.json +384 -0
  50. package/artifacts/schema/collections-issue.schema.json +36 -0
  51. package/artifacts/schema/collections-publish-request.schema.json +18 -0
  52. package/artifacts/schema/collections-publish-response.schema.json +450 -0
  53. package/artifacts/schema/collections-purge-preview.schema.json +60 -0
  54. package/artifacts/schema/collections-purge-request.schema.json +27 -0
  55. package/artifacts/schema/collections-purge-response.schema.json +30 -0
  56. package/artifacts/schema/collections-restore-preview-query.schema.json +20 -0
  57. package/artifacts/schema/collections-restore-preview.schema.json +581 -0
  58. package/artifacts/schema/collections-restore-request.schema.json +27 -0
  59. package/artifacts/schema/collections-restore-response.schema.json +455 -0
  60. package/artifacts/schema/collections-schema-preview.schema.json +519 -0
  61. package/artifacts/schema/collections-schema-published.schema.json +22 -0
  62. package/artifacts/schema/collections-version-response.schema.json +869 -0
  63. package/artifacts/schema/collections-version-summary.schema.json +139 -0
  64. package/artifacts/schema/collections-versions-response.schema.json +167 -0
  65. package/artifacts/schema/config-draft-response.schema.json +359 -5
  66. package/artifacts/schema/config-version-response.schema.json +359 -5
  67. package/artifacts/schema/confirmation-required-details.schema.json +13 -0
  68. package/artifacts/schema/create-app-invitation-request.schema.json +4 -4
  69. package/artifacts/schema/create-app-user-request.schema.json +4 -4
  70. package/artifacts/schema/create-org-role-request.schema.json +33 -0
  71. package/artifacts/schema/dangling-reference.schema.json +130 -0
  72. package/artifacts/schema/datapoint-config.schema.json +35 -0
  73. package/artifacts/schema/datapoint-list-response.schema.json +2 -2
  74. package/artifacts/schema/effective-capability.schema.json +104 -0
  75. package/artifacts/schema/effective-rights-query.schema.json +19 -0
  76. package/artifacts/schema/effective-rights-response.schema.json +396 -0
  77. package/artifacts/schema/effective-robot-rights.schema.json +266 -0
  78. package/artifacts/schema/invoke-or-service-response.schema.json +1 -1
  79. package/artifacts/schema/invoke-response.schema.json +1 -1
  80. package/artifacts/schema/journal-intent-v2-body.schema.json +74 -0
  81. package/artifacts/schema/journal-intent-v2.schema.json +84 -0
  82. package/artifacts/schema/journal-replay-receipt-body.schema.json +1 -0
  83. package/artifacts/schema/journal-replay-receipt.schema.json +1 -0
  84. package/artifacts/schema/mcp-robot-datasheet.schema.json +2 -2
  85. package/artifacts/schema/org-plan.schema.json +29 -1
  86. package/artifacts/schema/org-quota-usage-counts.schema.json +18 -0
  87. package/artifacts/schema/org-quota-usage.schema.json +47 -1
  88. package/artifacts/schema/org-quotas.schema.json +29 -1
  89. package/artifacts/schema/org-role-list-response.schema.json +206 -0
  90. package/artifacts/schema/org-role.schema.json +59 -0
  91. package/artifacts/schema/patch-app-user-request.schema.json +4 -4
  92. package/artifacts/schema/patch-org-role-request.schema.json +38 -0
  93. package/artifacts/schema/plan-catalogue-entry.schema.json +29 -1
  94. package/artifacts/schema/plan-change-decision-status-response.schema.json +29 -1
  95. package/artifacts/schema/plan-limit-details.schema.json +3 -1
  96. package/artifacts/schema/plan-limits.schema.json +29 -1
  97. package/artifacts/schema/plan-overrides.schema.json +26 -0
  98. package/artifacts/schema/publish-config-response.schema.json +48 -1
  99. package/artifacts/schema/publisher-config.schema.json +35 -0
  100. package/artifacts/schema/put-collections-draft-request.schema.json +16 -0
  101. package/artifacts/schema/put-role-defaults-request.schema.json +127 -0
  102. package/artifacts/schema/record-changed-details.schema.json +16 -0
  103. package/artifacts/schema/record-referenced-details.schema.json +35 -0
  104. package/artifacts/schema/reference-invalid-details.schema.json +25 -0
  105. package/artifacts/schema/reference-place.schema.json +112 -0
  106. package/artifacts/schema/rename-slug-response.schema.json +4 -3
  107. package/artifacts/schema/required-missing-details.schema.json +19 -0
  108. package/artifacts/schema/robot-config-doc.schema.json +359 -5
  109. package/artifacts/schema/robot-deletion-summary.schema.json +33 -1
  110. package/artifacts/schema/role-collection-rights.schema.json +82 -0
  111. package/artifacts/schema/role-cycle-details.schema.json +18 -0
  112. package/artifacts/schema/role-defaults-response.schema.json +59 -0
  113. package/artifacts/schema/role-rename-preview.schema.json +147 -0
  114. package/artifacts/schema/service-config.schema.json +35 -0
  115. package/artifacts/schema/slug-usage-response.schema.json +4 -3
  116. package/artifacts/schema/update-app-request.schema.json +0 -12
  117. package/dist/app-users.d.ts +11 -8
  118. package/dist/app-users.js +18 -10
  119. package/dist/apps.d.ts +258 -71
  120. package/dist/apps.js +215 -151
  121. package/dist/audit.d.ts +35 -5
  122. package/dist/audit.js +40 -2
  123. package/dist/billing.d.ts +10 -10
  124. package/dist/client-auth.d.ts +5 -2
  125. package/dist/client-auth.js +11 -3
  126. package/dist/client-robots.d.ts +1 -1
  127. package/dist/client-robots.js +1 -1
  128. package/dist/collections-issues.d.ts +67 -0
  129. package/dist/collections-issues.js +340 -0
  130. package/dist/collections-records.d.ts +562 -0
  131. package/dist/collections-records.js +525 -0
  132. package/dist/collections-rights.d.ts +17 -0
  133. package/dist/collections-rights.js +41 -0
  134. package/dist/collections-schema.d.ts +1064 -0
  135. package/dist/collections-schema.js +386 -0
  136. package/dist/collections-templates.d.ts +55 -0
  137. package/dist/collections-templates.js +137 -0
  138. package/dist/collections-transfer.d.ts +101 -0
  139. package/dist/collections-transfer.js +206 -0
  140. package/dist/collections-values.d.ts +67 -0
  141. package/dist/collections-values.js +284 -0
  142. package/dist/collections.d.ts +568 -0
  143. package/dist/collections.js +332 -0
  144. package/dist/common.d.ts +1 -1
  145. package/dist/common.js +1 -1
  146. package/dist/config-issues.d.ts +1 -1
  147. package/dist/config-issues.js +1 -1
  148. package/dist/config.d.ts +78 -12
  149. package/dist/config.js +109 -15
  150. package/dist/errors.d.ts +63 -5
  151. package/dist/errors.js +152 -24
  152. package/dist/index.d.ts +40 -14
  153. package/dist/index.js +27 -7
  154. package/dist/journal-intents.d.ts +30 -0
  155. package/dist/journal-intents.js +3 -0
  156. package/dist/legal-acceptances.d.ts +2 -2
  157. package/dist/mcp.d.ts +5 -37
  158. package/dist/mcp.js +4 -14
  159. package/dist/org-roles.d.ts +186 -0
  160. package/dist/org-roles.js +199 -0
  161. package/dist/plans.d.ts +25 -7
  162. package/dist/plans.js +21 -1
  163. package/dist/protocol.d.ts +19 -1
  164. package/dist/realtime.d.ts +172 -3
  165. package/dist/realtime.js +164 -1
  166. package/dist/rest.d.ts +104 -53
  167. package/dist/rest.js +79 -55
  168. package/dist/rights.d.ts +197 -0
  169. package/dist/rights.js +396 -0
  170. package/dist/routes.d.ts +1 -1
  171. package/dist/routes.js +675 -150
  172. package/package.json +1 -1
  173. package/artifacts/schema/mcp-role-preview-response.schema.json +0 -134
  174. package/artifacts/schema/put-robot-details-request.schema.json +0 -41
  175. package/artifacts/schema/put-robot-details-response.schema.json +0 -43
  176. package/artifacts/schema/robot-details-doc.schema.json +0 -33
  177. package/artifacts/schema/role-delete-query.schema.json +0 -13
  178. package/artifacts/schema/role-in-use-details.schema.json +0 -28
  179. package/artifacts/schema/role-list-response.schema.json +0 -48
  180. package/artifacts/schema/role-permissions.schema.json +0 -61
  181. package/artifacts/schema/role-rename-request.schema.json +0 -16
  182. package/artifacts/schema/role.schema.json +0 -35
@@ -0,0 +1,386 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { z } from 'zod';
3
+ import { COLLECTION_LIMITS, collectionBinding, collectionKey, collectionsDocument } from './collections.js';
4
+ import { collectionsIssue } from './collections-issues.js';
5
+ import { collectionActor } from './collections-records.js';
6
+ /**
7
+ * The life of an app's schema on the wire: the draft a developer edits, the
8
+ * preview of what publishing it would do to existing data, publish, the
9
+ * versions that result, restoring one of them, and the owner's purge of old
10
+ * history.
11
+ *
12
+ * **One confirmation flow runs through all of it.** A change that would alter
13
+ * or remove data is never applied on the first request. The preview lists what
14
+ * would happen (`effects`) and a `token` that stands for exactly that list; the
15
+ * confirming request sends the token back as `confirm`. If the data changed in
16
+ * between, so that the effects differ, the token no longer matches and the
17
+ * answer is `409 confirmation_required` carrying the current preview, which the
18
+ * caller confirms again. A request that would change nothing needs no token,
19
+ * and its preview says so with `token: null`. Purge always needs one.
20
+ */
21
+ const timestamp = z.iso.datetime();
22
+ const count = z.number().int().min(0);
23
+ const schemaVersion = z.number().int().min(1);
24
+ /** The most records a `unique_discarded` effect lists; `total` stays exact. */
25
+ const UNIQUE_DISCARDED_LISTED = 1000;
26
+ const token = z.string().min(1);
27
+ const confirm = token.meta({
28
+ description: 'The `token` of the preview of exactly this request. Needed when the preview lists effects; a missing or outdated token is answered with `confirmation_required` and the current preview.',
29
+ });
30
+ /** SHA-256 as lowercase hex: 64 characters. */
31
+ const sha256 = z.string().regex(/^[0-9a-f]{64}$/);
32
+ /** A purge is always confirmed, since removing history cannot be undone; its preview has a token whatever it finds. */
33
+ const purgeConfirm = token.meta({
34
+ description: 'The `token` of the preview of exactly this purge. A purge is always confirmed: without a token, or with one that no longer holds, the answer is `confirmation_required` with the current preview.',
35
+ });
36
+ /**
37
+ * The length of `text` in bytes of UTF-8, counted without building the bytes. A
38
+ * half of a surrogate pair counts as the three bytes of the replacement
39
+ * character, as an encoder writes it.
40
+ */
41
+ function utf8Length(text) {
42
+ let bytes = 0;
43
+ for (let i = 0; i < text.length; i += 1) {
44
+ const unit = text.charCodeAt(i);
45
+ if (unit < 0x80)
46
+ bytes += 1;
47
+ else if (unit < 0x800)
48
+ bytes += 2;
49
+ else if (unit >= 0xd800 && unit <= 0xdbff && text.charCodeAt(i + 1) >= 0xdc00 && text.charCodeAt(i + 1) <= 0xdfff) {
50
+ bytes += 4;
51
+ i += 1;
52
+ }
53
+ else
54
+ bytes += 3;
55
+ }
56
+ return bytes;
57
+ }
58
+ const collection = collectionKey.meta({ description: 'The key of the collection.' });
59
+ const field = collectionKey.meta({ description: 'The key of the field.' });
60
+ /**
61
+ * One thing a publish or a restore does to data that already exists. Effects
62
+ * that would change nothing are not listed, so an empty list means the change
63
+ * touches no data. They are matched by key only: a renamed collection or field
64
+ * is a removal plus an addition.
65
+ */
66
+ export const collectionsEffect = z
67
+ .discriminatedUnion('kind', [
68
+ z
69
+ .object({
70
+ kind: z.literal('collection_removed'),
71
+ collection,
72
+ records: count.meta({ description: 'Records deleted with the collection.' }),
73
+ })
74
+ .strict()
75
+ .meta({
76
+ description: 'The collection is gone from the document, so its records are deleted. Their history is deleted 90 days after the publish or restore that removed the collection, also when it held no records any more by then; until then a restore of an earlier version with its data can bring the records back.',
77
+ }),
78
+ z
79
+ .object({
80
+ kind: z.literal('collection_recreated'),
81
+ collection,
82
+ records: count.meta({ description: 'Records deleted by the re-creation.' }),
83
+ from: collectionBinding,
84
+ to: collectionBinding,
85
+ })
86
+ .strict()
87
+ .meta({
88
+ description: 'The binding of the collection changed, which deletes its records and starts the collection again. The history of the deleted records is kept 90 days after the publish or restore, as for a removed collection.',
89
+ }),
90
+ z
91
+ .object({
92
+ kind: z.literal('records_created'),
93
+ collection,
94
+ count: count.meta({ description: 'Records created, one for each robot attached to the app.' }),
95
+ })
96
+ .strict()
97
+ .meta({ description: 'A collection with one record per robot got the records of the robots that have none.' }),
98
+ z
99
+ .object({
100
+ kind: z.literal('field_removed'),
101
+ collection,
102
+ field,
103
+ values: count.meta({ description: 'Values removed from records.' }),
104
+ })
105
+ .strict()
106
+ .meta({ description: 'The field is gone from the document, so its values are removed from the records.' }),
107
+ z
108
+ .object({
109
+ kind: z.literal('defaults_filled'),
110
+ collection,
111
+ field,
112
+ records: count.meta({ description: 'Records that receive the default.' }),
113
+ })
114
+ .strict()
115
+ .meta({ description: 'Records without a value get the default of a new or newly required field.' }),
116
+ z
117
+ .object({
118
+ kind: z.literal('values_converted'),
119
+ collection,
120
+ field,
121
+ converted: count.meta({ description: 'Values that became a value of the new type.' }),
122
+ discarded: count.meta({ description: 'Values that could not be converted and were removed.' }),
123
+ })
124
+ .strict()
125
+ .meta({ description: 'The type of the field changed: values are converted where the conversion rules allow it and removed where they do not.' }),
126
+ z
127
+ .object({
128
+ kind: z.literal('values_discarded'),
129
+ collection,
130
+ field,
131
+ reason: z.enum(['reference_target_changed', 'option_removed', 'constraint']).meta({
132
+ description: '`reference_target_changed`: the field points at another collection now. `option_removed`: a select option is gone. `constraint`: a limit such as `max_length`, `pattern`, `min`, `max`, `integer` or the JSON schema now excludes the value.',
133
+ }),
134
+ values: count.meta({ description: 'Values removed from records.' }),
135
+ })
136
+ .strict()
137
+ .meta({ description: 'Values that the changed field no longer accepts are removed.' }),
138
+ z
139
+ .object({
140
+ kind: z.literal('unique_discarded'),
141
+ collection,
142
+ field,
143
+ total: count.meta({ description: 'The exact number of values removed.' }),
144
+ records: z
145
+ .array(z
146
+ .object({
147
+ id: z.uuid(),
148
+ display: z.string().meta({ description: 'The record\'s display text.' }),
149
+ value: z.unknown().meta({ description: 'The value the record loses.' }),
150
+ })
151
+ .strict())
152
+ .max(UNIQUE_DISCARDED_LISTED)
153
+ .meta({ description: `The records that lose their value, at most ${UNIQUE_DISCARDED_LISTED}; \`total\` is the exact number.` }),
154
+ })
155
+ .strict()
156
+ .meta({
157
+ description: 'The field became unique, or unique across a wider scope, and records share a value: the record created first keeps it and the others lose it.',
158
+ }),
159
+ z
160
+ .object({
161
+ kind: z.literal('required_left_empty'),
162
+ collection,
163
+ field,
164
+ records: count.meta({ description: 'Records left without a value.' }),
165
+ })
166
+ .strict()
167
+ .meta({ description: 'The field is required now and these records have no value and no default to fill in; they stay updatable in their other fields.' }),
168
+ z
169
+ .object({
170
+ kind: z.literal('references_cleared'),
171
+ collection,
172
+ field,
173
+ values: count.meta({ description: 'References removed.' }),
174
+ })
175
+ .strict()
176
+ .meta({ description: 'References that pointed at records deleted by this change are cleared.' }),
177
+ ])
178
+ .meta({ description: 'One effect of a publish or restore on existing data. Effects that would change nothing are not listed.' });
179
+ /** A change to who may do what, shown with a preview as information; it needs no confirmation. */
180
+ export const collectionsAccessChange = z
181
+ .object({
182
+ collection,
183
+ action: z.enum(['read', 'create', 'update', 'delete']),
184
+ from: z.unknown().meta({ description: 'The access before, as written in the document. `null` means nobody.' }),
185
+ to: z.unknown().meta({ description: 'The access after, as written in the document. `null` means nobody.' }),
186
+ })
187
+ .meta({ description: 'A change of the roles that may do one thing with the records of a collection. Information only; it does not need confirming.' });
188
+ /** What publishing the draft would do. */
189
+ export const collectionsSchemaPreview = z
190
+ .object({
191
+ from_version: schemaVersion.nullable().meta({ description: 'The version that is published now. Null before the first publish.' }),
192
+ to_version: schemaVersion.meta({ description: 'The number the new version would get.' }),
193
+ valid: z.boolean().meta({ description: 'False when the draft has errors; it cannot be published and no effects are computed.' }),
194
+ issues: z.array(collectionsIssue).meta({ description: 'Everything found in the draft: errors that block publishing, warnings and information.' }),
195
+ effects: z.array(collectionsEffect).meta({ description: 'What would happen to existing data, computed against the data as it is now.' }),
196
+ access_changes: z.array(collectionsAccessChange),
197
+ source_sha256: sha256.meta({
198
+ description: 'The SHA-256 of the draft text this preview is for (UTF-8, lowercase hex). Send it as `source_sha256` when publishing to publish exactly this draft, also when there are no effects and so no token.',
199
+ }),
200
+ token: z.string().nullable().meta({
201
+ description: 'Stands for exactly this list of effects, on exactly this draft text and the version in force now: send it as `confirm` when publishing. If the draft was saved again, or the data or the version changed, the token no longer holds. Null when there are no effects, since publishing then needs no confirmation.',
202
+ }),
203
+ })
204
+ .meta({ description: 'The preview of publishing the draft: its problems, its effects on existing data and the token that confirms them.' });
205
+ /**
206
+ * The draft. `source` is the text the developer edits: the saved draft, else
207
+ * the text of the current version, else empty before anything was published.
208
+ * `updated_at: null` says no draft is saved and the text is the version's.
209
+ */
210
+ export const collectionsDraftResponse = z
211
+ .object({
212
+ source: z.string().meta({ description: 'The document as text. The saved draft; without one, the published version\'s text; before the first publish, empty.' }),
213
+ doc: collectionsDocument.nullable().meta({ description: 'The document the source parses to. Null when the source is empty.' }),
214
+ updated_at: timestamp.nullable().meta({ description: 'When the draft was saved. Null when no draft is saved.' }),
215
+ updated_by: collectionActor.nullable().meta({ description: 'Who saved the draft. Null when no draft is saved.' }),
216
+ issues: z.array(collectionsIssue).meta({ description: 'Warnings and information about the document. A draft is saved only when it has no errors.' }),
217
+ })
218
+ .meta({ description: 'The draft of the schema document, or the current version when there is no draft.' });
219
+ export const putCollectionsDraftRequest = z
220
+ .object({
221
+ // A character is at least one byte, so the cheap bound on characters holds for every source and rules out the long ones before they are measured.
222
+ source: z
223
+ .string()
224
+ .max(COLLECTION_LIMITS.schemaSourceBytes)
225
+ .refine((text) => utf8Length(text) <= COLLECTION_LIMITS.schemaSourceBytes, {
226
+ message: `The source is longer than ${COLLECTION_LIMITS.schemaSourceBytes.toLocaleString('en-US')} bytes of UTF-8.`,
227
+ })
228
+ .meta({
229
+ description: `The whole document as YAML or JSON text, at most ${COLLECTION_LIMITS.schemaSourceBytes.toLocaleString('en-US')} bytes of UTF-8, which is fewer characters when some are not plain ASCII. It replaces the draft and is refused with the problems found if it is not valid.`,
230
+ }),
231
+ })
232
+ .strict()
233
+ .meta({ description: 'Replaces the draft with a whole document.' });
234
+ export const collectionsPublishRequest = z
235
+ .object({
236
+ confirm: confirm.optional(),
237
+ source_sha256: sha256
238
+ .meta({
239
+ description: 'The `source_sha256` of the preview that was looked at. When it is sent and the saved draft is another text, because somebody saved it again since, nothing is published and the answer is `409 confirmation_required` with the current preview. Leave it out to publish whichever draft is saved; a token (`confirm`) already binds the draft it was made for.',
240
+ })
241
+ .optional(),
242
+ })
243
+ .strict()
244
+ .meta({
245
+ description: 'Publishes the draft. The body may be empty when the preview lists no effects; otherwise it carries the preview\'s token as `confirm`. Send `source_sha256` as well to be sure that the draft published is the one that was looked at.',
246
+ });
247
+ export const collectionsPublishResponse = z
248
+ .object({
249
+ version: schemaVersion,
250
+ published_at: timestamp,
251
+ effects: z.array(collectionsEffect).meta({ description: 'What the publish did to existing data.' }),
252
+ issues: z.array(collectionsIssue).meta({ description: 'Warnings and information about the published document.' }),
253
+ })
254
+ .meta({ description: 'The version that was published and the effects it had.' });
255
+ /** One published version. */
256
+ export const collectionsVersionSummary = z
257
+ .object({
258
+ version: schemaVersion,
259
+ published_at: timestamp,
260
+ published_by: collectionActor,
261
+ effect_counts: z.record(z.string(), count).meta({
262
+ description: 'For each kind of effect the publish had, how many records or values it touched, by effect `kind`. Empty when it had none.',
263
+ }),
264
+ restored_from: schemaVersion.nullable().meta({ description: 'The version this one restored. Null for an ordinary publish.' }),
265
+ restore_with_data: z.boolean().meta({
266
+ description: `Whether restoring this version can also put records back: its restore point is at most ${COLLECTION_LIMITS.restoreDays} days old, has not been purged, and it is not the current version.`,
267
+ }),
268
+ with_data: z.boolean().meta({
269
+ description: 'Whether this version came from a restore that also put records back. `false` for an ordinary publish and for a restore of the document alone.',
270
+ }),
271
+ reverted_totals: z
272
+ .object({
273
+ changes: count.meta({ description: 'Changes made since the restore point that were reverted.' }),
274
+ records_back: count.meta({ description: 'Deleted records that came back.' }),
275
+ records_deleted: count.meta({ description: 'Records created since the restore point that were deleted.' }),
276
+ robots_gone: count.meta({ description: 'Records of robots that were deleted since, which could not come back.' }),
277
+ })
278
+ .strict()
279
+ .nullable()
280
+ .meta({
281
+ description: 'What putting the records back did, in all the collections together, so that the history of versions can say what came back. Null unless `with_data`.',
282
+ }),
283
+ })
284
+ .meta({ description: 'A published version of the schema document.' });
285
+ export const collectionsVersionsResponse = z
286
+ .object({
287
+ current: schemaVersion.nullable().meta({ description: 'The version in force. Null before the first publish.' }),
288
+ versions: z.array(collectionsVersionSummary).meta({ description: 'Newest first.' }),
289
+ })
290
+ .meta({ description: 'The published versions of the schema document.' });
291
+ export const collectionsVersionResponse = collectionsVersionSummary
292
+ .extend({
293
+ source: z.string().meta({ description: 'The document as it was published, as text.' }),
294
+ doc: collectionsDocument,
295
+ })
296
+ .meta({ description: 'One published version with its document.' });
297
+ /**
298
+ * What restoring a version would do. `with_data` is what the caller asked for
299
+ * and the preview shows; `data_possible` and `data_reason` say whether data can
300
+ * come back at all.
301
+ */
302
+ export const collectionsRestorePreview = z
303
+ .object({
304
+ version: schemaVersion.meta({ description: 'The version that would be restored.' }),
305
+ with_data: z.boolean().meta({ description: 'Whether this preview is for a restore that also puts records back.' }),
306
+ data_possible: z.boolean().meta({ description: 'Whether records can be put back at all.' }),
307
+ data_reason: z.enum(['ok', 'expired', 'purged', 'current_version']).meta({
308
+ description: `\`ok\`: records can be put back. \`expired\`: the restore point is more than ${COLLECTION_LIMITS.restoreDays} days old. \`purged\`: an owner removed the history it needs. \`current_version\`: this version is already in force.`,
309
+ }),
310
+ touched: z.array(collection).meta({ description: 'The collections that differ between the current version and this one; only these can get records back.' }),
311
+ selected: z.array(collection).meta({ description: 'The touched collections whose records would be put back. The caller may deselect some.' }),
312
+ reverted: z
313
+ .array(z
314
+ .object({
315
+ collection,
316
+ changes: count.meta({ description: 'Changes made since the restore point that would be reverted.' }),
317
+ records_back: count.meta({ description: 'Deleted records that would come back.' }),
318
+ records_deleted: count.meta({ description: 'Records created since the restore point that would be deleted.' }),
319
+ })
320
+ .strict())
321
+ .meta({ description: 'What putting the records back would do, per selected collection.' }),
322
+ robots_gone: count.meta({ description: 'Records of robots that were deleted since cannot come back; this is how many.' }),
323
+ effects: z.array(collectionsEffect).meta({ description: 'The ordinary effects of publishing this version\'s document.' }),
324
+ access_changes: z.array(collectionsAccessChange),
325
+ issues: z.array(collectionsIssue),
326
+ token: z.string().nullable().meta({
327
+ description: 'Stands for exactly what this restore would do: send it as `confirm`. Null when the restore changes no data and needs no confirmation.',
328
+ }),
329
+ })
330
+ .meta({ description: 'The preview of restoring a version, with the token that confirms it.' });
331
+ /** The query string of a restore preview. */
332
+ export const collectionsRestorePreviewQuery = z
333
+ .object({
334
+ data: z.enum(['true', 'false']).meta({ description: '`true` also puts records back to their state at the version\'s restore point. Default `false`.' }).optional(),
335
+ collections: z
336
+ .string()
337
+ .min(1)
338
+ .meta({ description: 'The touched collections to put records back in, separated by commas. Default: all of them.' })
339
+ .optional(),
340
+ })
341
+ .strict();
342
+ export const collectionsRestoreRequest = z
343
+ .object({
344
+ data: z.boolean().meta({ description: 'Also put records back to their state at the restore point, in the touched collections. Default `false`.' }).optional(),
345
+ collections: z
346
+ .array(collectionKey)
347
+ .max(COLLECTION_LIMITS.collections)
348
+ .meta({ description: 'Limits putting records back to these touched collections. Default: all of them.' })
349
+ .optional(),
350
+ confirm: confirm.optional(),
351
+ })
352
+ .strict()
353
+ .meta({ description: 'Restores a version. Send the `data` and `collections` of the preview together with its token as `confirm`.' });
354
+ export const collectionsRestoreResponse = collectionsPublishResponse
355
+ .extend({ with_data: z.boolean().meta({ description: 'Whether records were put back as well.' }) })
356
+ .meta({ description: 'The version that the restore published and what it did.' });
357
+ /** The owner's purge of old history. Always confirmed with a token. */
358
+ export const collectionsPurgeRequest = z
359
+ .object({
360
+ before: timestamp.meta({
361
+ description: 'Revisions older than this UTC time are removed. The current values of records stay. It must lie in the past (`400 validation_error`, field `before`, rule `not_in_past`), and a date older than every revision is refused as well (rule `nothing_to_purge`).',
362
+ }),
363
+ collection: collectionKey.meta({ description: 'Purge only this collection. Default: the whole app.' }).optional(),
364
+ confirm: purgeConfirm.optional(),
365
+ })
366
+ .strict()
367
+ .meta({ description: 'Removes the history older than a date. The preview answers with a token; send it as `confirm` to purge.' });
368
+ export const collectionsPurgePreview = z
369
+ .object({
370
+ before: timestamp,
371
+ collection: collectionKey.nullable().meta({ description: 'The collection the purge is limited to. Null for the whole app.' }),
372
+ revisions: count.meta({ description: 'Revisions that would be removed.' }),
373
+ bytes: count.meta({ description: 'Their size in bytes.' }),
374
+ restore_points_lost: z.array(schemaVersion).meta({
375
+ description: 'Versions whose records could be put back today and could not once the history is gone, judged the way `restore_with_data` of the version list is. For the purge of one collection only the versions whose restore touches that collection are named.',
376
+ }),
377
+ token: token.meta({ description: 'Stands for exactly this purge: send it as `confirm` to carry it out.' }),
378
+ })
379
+ .meta({ description: 'What a purge would remove, with the token that confirms it.' });
380
+ export const collectionsPurgeResponse = z
381
+ .object({
382
+ revisions: count.meta({ description: 'Revisions removed.' }),
383
+ bytes: count.meta({ description: 'Their size in bytes.' }),
384
+ purged_at: timestamp,
385
+ })
386
+ .meta({ description: 'What a purge removed.' });
@@ -0,0 +1,55 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { z } from 'zod';
3
+ /**
4
+ * The named JSON Schemas a `json` field can use as its `schema`, and the
5
+ * parser of the `display` template a collection names its records with.
6
+ *
7
+ * Both are small enough to ship as data and code rather than as a service:
8
+ * the cloud checks values against the templates, the console and the SDK may
9
+ * one day recognise them (a map for a pose, a polygon editor), and an AI tool
10
+ * writing a document reads them here.
11
+ */
12
+ /** The names a `json` field accepts in `schema` besides an inline JSON Schema. */
13
+ export declare const collectionJsonTemplateName: z.ZodEnum<{
14
+ pose2d: "pose2d";
15
+ point2d: "point2d";
16
+ polygon2d: "polygon2d";
17
+ range1d: "range1d";
18
+ }>;
19
+ export type CollectionJsonTemplateName = z.infer<typeof collectionJsonTemplateName>;
20
+ /**
21
+ * Plain JSON Schemas of the shapes robots exchange most often. Units are fixed:
22
+ * metres and radians. Each template is a closed object with every property
23
+ * required, so a value is either complete or refused; nothing in them needs
24
+ * more than the core vocabulary, so any JSON Schema validator reads them
25
+ * (they are valid for draft 2020-12 and for earlier drafts alike).
26
+ *
27
+ * The objects are frozen: they are shared by every caller in the process, and
28
+ * a validator that cached one while a caller edited it would disagree with
29
+ * itself.
30
+ */
31
+ export declare const COLLECTION_JSON_TEMPLATES: Readonly<Record<CollectionJsonTemplateName, Readonly<Record<string, unknown>>>>;
32
+ /** One `{{...}}` in a display template: the text as written, and the key path it names. */
33
+ export type DisplayPlaceholder = {
34
+ /** The placeholder as it stands in the template, braces and inner spaces included. */
35
+ raw: string;
36
+ /** The keys it names: one for a field, two for a field of a referenced record. */
37
+ path: string[];
38
+ };
39
+ export type DisplayTemplateParse = {
40
+ ok: true;
41
+ placeholders: DisplayPlaceholder[];
42
+ } | {
43
+ ok: false;
44
+ message: string;
45
+ };
46
+ /**
47
+ * Reads the placeholders of a `display` template.
48
+ *
49
+ * A placeholder is `{{key}}` or `{{key.key}}`, with spaces allowed just inside
50
+ * the braces; everything else in the template is plain text, lone braces
51
+ * included. This only says whether the template is well formed and what it
52
+ * names. Whether each name exists is the document's question, because it
53
+ * depends on the collection's fields.
54
+ */
55
+ export declare function parseDisplayTemplate(template: string): DisplayTemplateParse;
@@ -0,0 +1,137 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { z } from 'zod';
3
+ /**
4
+ * The named JSON Schemas a `json` field can use as its `schema`, and the
5
+ * parser of the `display` template a collection names its records with.
6
+ *
7
+ * Both are small enough to ship as data and code rather than as a service:
8
+ * the cloud checks values against the templates, the console and the SDK may
9
+ * one day recognise them (a map for a pose, a polygon editor), and an AI tool
10
+ * writing a document reads them here.
11
+ */
12
+ /** The names a `json` field accepts in `schema` besides an inline JSON Schema. */
13
+ export const collectionJsonTemplateName = z
14
+ .enum(['pose2d', 'point2d', 'polygon2d', 'range1d'])
15
+ .meta({ description: 'The name of a built-in JSON template: `pose2d`, `point2d`, `polygon2d` or `range1d`.' });
16
+ function deepFreeze(value) {
17
+ if (value !== null && typeof value === 'object') {
18
+ for (const inner of Object.values(value))
19
+ deepFreeze(inner);
20
+ Object.freeze(value);
21
+ }
22
+ return value;
23
+ }
24
+ const metres = (what) => ({ type: 'number', description: `${what}, in metres.` });
25
+ const radians = (what) => ({ type: 'number', description: `${what}, in radians.` });
26
+ const frameId = { type: 'string', description: 'The name of the coordinate frame the numbers are given in.' };
27
+ /**
28
+ * Plain JSON Schemas of the shapes robots exchange most often. Units are fixed:
29
+ * metres and radians. Each template is a closed object with every property
30
+ * required, so a value is either complete or refused; nothing in them needs
31
+ * more than the core vocabulary, so any JSON Schema validator reads them
32
+ * (they are valid for draft 2020-12 and for earlier drafts alike).
33
+ *
34
+ * The objects are frozen: they are shared by every caller in the process, and
35
+ * a validator that cached one while a caller edited it would disagree with
36
+ * itself.
37
+ */
38
+ export const COLLECTION_JSON_TEMPLATES = deepFreeze({
39
+ pose2d: {
40
+ type: 'object',
41
+ description: 'A position and a heading in a plane.',
42
+ properties: { x: metres('Position along the x axis'), y: metres('Position along the y axis'), yaw: radians('Heading around the vertical axis'), frame_id: frameId },
43
+ required: ['x', 'y', 'yaw', 'frame_id'],
44
+ additionalProperties: false,
45
+ },
46
+ point2d: {
47
+ type: 'object',
48
+ description: 'A position in a plane.',
49
+ properties: { x: metres('Position along the x axis'), y: metres('Position along the y axis'), frame_id: frameId },
50
+ required: ['x', 'y', 'frame_id'],
51
+ additionalProperties: false,
52
+ },
53
+ polygon2d: {
54
+ type: 'object',
55
+ description: 'A closed outline in a plane, given by its corners in order.',
56
+ properties: {
57
+ points: {
58
+ type: 'array',
59
+ description: 'The corners, at least three.',
60
+ minItems: 3,
61
+ items: {
62
+ type: 'object',
63
+ properties: { x: metres('Position along the x axis'), y: metres('Position along the y axis') },
64
+ required: ['x', 'y'],
65
+ additionalProperties: false,
66
+ },
67
+ },
68
+ frame_id: frameId,
69
+ },
70
+ required: ['points', 'frame_id'],
71
+ additionalProperties: false,
72
+ },
73
+ range1d: {
74
+ type: 'object',
75
+ description: 'An interval along one axis.',
76
+ properties: { from: metres('Start of the interval'), to: metres('End of the interval') },
77
+ required: ['from', 'to'],
78
+ additionalProperties: false,
79
+ },
80
+ });
81
+ const PLACEHOLDER_KEY = /^[a-z][a-z0-9_]{0,62}$/;
82
+ /** A piece of the template for a message: a very long one is cut, so a message never repeats a megabyte back. */
83
+ const clip = (text) => (text.length > 80 ? `${text.slice(0, 80)}…` : text);
84
+ /**
85
+ * `text` without the spaces at both ends, in one pass over them. A pattern such
86
+ * as `/^ +| +$/g` starts again at every space of a long run and takes time
87
+ * quadratic in its length, which a template of a few hundred thousand spaces
88
+ * turns into seconds.
89
+ */
90
+ function trimSpaces(text) {
91
+ let start = 0;
92
+ let end = text.length;
93
+ while (start < end && text[start] === ' ')
94
+ start += 1;
95
+ while (end > start && text[end - 1] === ' ')
96
+ end -= 1;
97
+ return text.slice(start, end);
98
+ }
99
+ /**
100
+ * Reads the placeholders of a `display` template.
101
+ *
102
+ * A placeholder is `{{key}}` or `{{key.key}}`, with spaces allowed just inside
103
+ * the braces; everything else in the template is plain text, lone braces
104
+ * included. This only says whether the template is well formed and what it
105
+ * names. Whether each name exists is the document's question, because it
106
+ * depends on the collection's fields.
107
+ */
108
+ export function parseDisplayTemplate(template) {
109
+ const placeholders = [];
110
+ let from = 0;
111
+ for (;;) {
112
+ const open = template.indexOf('{{', from);
113
+ if (open === -1)
114
+ return { ok: true, placeholders };
115
+ const close = template.indexOf('}}', open + 2);
116
+ if (close === -1) {
117
+ return { ok: false, message: `The "{{" at position ${open + 1} is never closed with "}}".` };
118
+ }
119
+ const raw = template.slice(open, close + 2);
120
+ const inner = trimSpaces(template.slice(open + 2, close));
121
+ const path = inner.split('.');
122
+ if (path.length > 2) {
123
+ return { ok: false, message: `${clip(raw)} names ${path.length} keys. A placeholder is {{field}} or {{reference.field}}; a display follows a reference one step only.` };
124
+ }
125
+ const bad = path.find((key) => !PLACEHOLDER_KEY.test(key));
126
+ if (bad !== undefined) {
127
+ const message = inner === ''
128
+ ? `${clip(raw)} is empty. A placeholder names a field, like {{name}}.`
129
+ : bad === ''
130
+ ? `${clip(raw)} has an empty key next to a ".". A placeholder is {{field}} or {{reference.field}}.`
131
+ : `${clip(raw)} is not a placeholder: "${clip(bad)}" is not a key. Keys are lowercase letters, digits and underscores, starting with a letter.`;
132
+ return { ok: false, message };
133
+ }
134
+ placeholders.push({ raw, path });
135
+ from = close + 2;
136
+ }
137
+ }