@rebasepro/plugin-ai 0.12.1-canary.gf5f1d39 → 0.13.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 (33) hide show
  1. package/README.md +62 -18
  2. package/dist/api.d.ts +58 -30
  3. package/dist/components/AutofillReviewDialog.d.ts +16 -0
  4. package/dist/components/DataEnhancementControllerProvider.d.ts +2 -3
  5. package/dist/components/FormEnhanceAction.d.ts +1 -1
  6. package/dist/editor/useEditorAIController.d.ts +11 -2
  7. package/dist/index.es.js +601 -382
  8. package/dist/index.es.js.map +1 -1
  9. package/dist/types/data_enhancement_controller.d.ts +84 -28
  10. package/dist/useDataEnhancementPlugin.d.ts +10 -5
  11. package/package.json +24 -19
  12. package/src/api.ts +241 -174
  13. package/src/components/AutofillReviewDialog.tsx +209 -0
  14. package/src/components/DataEnhancementControllerProvider.tsx +203 -262
  15. package/src/components/FormEnhanceAction.tsx +154 -128
  16. package/src/editor/useEditorAIController.tsx +20 -33
  17. package/src/tests/AutofillReviewDialog.test.tsx +340 -0
  18. package/src/tests/api.test.ts +283 -0
  19. package/src/tests/properties.test.ts +420 -0
  20. package/src/tests/review.test.tsx +393 -0
  21. package/src/tests/useDataEnhancementPlugin.test.tsx +36 -15
  22. package/src/tests/useEditorAIController.test.ts +98 -0
  23. package/src/types/data_enhancement_controller.tsx +98 -31
  24. package/src/useDataEnhancementPlugin.tsx +13 -12
  25. package/dist/utils/diffStrings.d.ts +0 -7
  26. package/dist/utils/strings_counter.d.ts +0 -2
  27. package/dist/utils/suggestions.d.ts +0 -1
  28. package/src/tests/diffStrings.test.ts +0 -128
  29. package/src/tests/strings_counter.test.ts +0 -117
  30. package/src/tests/suggestions.test.ts +0 -53
  31. package/src/utils/diffStrings.ts +0 -70
  32. package/src/utils/strings_counter.ts +0 -22
  33. package/src/utils/suggestions.ts +0 -6
@@ -0,0 +1,420 @@
1
+ import { getSimplifiedProperties } from "../utils/properties";
2
+ import { Properties } from "@rebasepro/types";
3
+
4
+ /**
5
+ * The schema serializer.
6
+ *
7
+ * This is the file that decides *what the model is asked to fill*. The service
8
+ * builds its JSON schema from whatever comes out of here, so a key this
9
+ * function drops is a field autofill will never write, silently — no error, no
10
+ * warning, just a form that stays half empty on exactly the collections that
11
+ * are most tedious to fill by hand.
12
+ *
13
+ * It also has to agree with `flatMapEntityValues` about paths. Properties are
14
+ * keyed on dotted paths (`seo.title`) and values are flattened onto the same
15
+ * ones; if the two disagree, the model is told a field exists and shown no
16
+ * value for it, and cheerfully overwrites what the operator already wrote.
17
+ *
18
+ * `getFieldId` is mocked to something deterministic — the real one resolves
19
+ * against the admin field registry, which is not what is under test here.
20
+ */
21
+ jest.mock("@rebasepro/admin", () => ({
22
+ getFieldId: (property: { type?: string; admin?: { markdown?: boolean; multiline?: boolean } }) => {
23
+ if (!property?.type) return undefined;
24
+ if (property.type === "string" && property.admin?.markdown) return "markdown";
25
+ if (property.type === "string" && property.admin?.multiline) return "multiline";
26
+ if (property.type === "string") return "text_field";
27
+ if (property.type === "number") return "number_field";
28
+ if (property.type === "boolean") return "switch";
29
+ if (property.type === "date") return "date_time";
30
+ if (property.type === "reference") return "reference";
31
+ return undefined;
32
+ }
33
+ }));
34
+
35
+ describe("getSimplifiedProperties — flat properties", () => {
36
+
37
+ it("keys each scalar on its own name", () => {
38
+ const properties = {
39
+ title: { name: "Title",
40
+ type: "string" },
41
+ stock: { name: "Stock",
42
+ type: "number" }
43
+ } as unknown as Properties;
44
+
45
+ const out = getSimplifiedProperties(properties, {});
46
+
47
+ expect(Object.keys(out).sort()).toEqual(["stock", "title"]);
48
+ expect(out.title).toMatchObject({ name: "Title",
49
+ type: "string",
50
+ fieldConfigId: "text_field" });
51
+ });
52
+
53
+ it("carries the description through, because it is what the model is steered by", () => {
54
+ const properties = {
55
+ summary: { name: "Summary",
56
+ type: "string",
57
+ description: "One sentence, no marketing" }
58
+ } as unknown as Properties;
59
+
60
+ expect(getSimplifiedProperties(properties, {}).summary.description).toBe("One sentence, no marketing");
61
+ });
62
+
63
+ it("marks disabled and read-only fields, so the service can refuse to fill them", () => {
64
+ const properties = {
65
+ slug: { name: "Slug",
66
+ type: "string",
67
+ admin: { disabled: true } },
68
+ computed: { name: "Computed",
69
+ type: "string",
70
+ admin: { readOnly: true } },
71
+ normal: { name: "Normal",
72
+ type: "string" }
73
+ } as unknown as Properties;
74
+
75
+ const out = getSimplifiedProperties(properties, {});
76
+ expect(out.slug.disabled).toBe(true);
77
+ expect(out.computed.disabled).toBe(true);
78
+ expect(out.normal.disabled).toBe(false);
79
+ });
80
+
81
+ it("flattens enum values to the ids the model must choose between", () => {
82
+ const asArray = {
83
+ status: { name: "Status",
84
+ type: "string",
85
+ enum: [{ id: "draft",
86
+ label: "Draft" }, { id: "live",
87
+ label: "Live" }] }
88
+ } as unknown as Properties;
89
+ expect(getSimplifiedProperties(asArray, {}).status.enum).toEqual(["draft", "live"]);
90
+
91
+ const asObject = {
92
+ status: { name: "Status",
93
+ type: "string",
94
+ enum: { draft: "Draft",
95
+ live: "Live" } }
96
+ } as unknown as Properties;
97
+ expect(getSimplifiedProperties(asObject, {}).status.enum).toEqual(["draft", "live"]);
98
+ });
99
+
100
+ it("distinguishes markdown and multiline, which change how a suggestion is joined", () => {
101
+ const properties = {
102
+ body: { name: "Body",
103
+ type: "string",
104
+ admin: { markdown: true } },
105
+ notes: { name: "Notes",
106
+ type: "string",
107
+ admin: { multiline: true } }
108
+ } as unknown as Properties;
109
+
110
+ const out = getSimplifiedProperties(properties, {});
111
+ expect(out.body.fieldConfigId).toBe("markdown");
112
+ expect(out.notes.fieldConfigId).toBe("multiline");
113
+ });
114
+
115
+ it("skips a property builder, which is a function with no schema to read", () => {
116
+ const properties = {
117
+ title: { name: "Title",
118
+ type: "string" },
119
+ derived: () => ({ name: "Derived",
120
+ type: "string" })
121
+ } as unknown as Properties;
122
+
123
+ expect(Object.keys(getSimplifiedProperties(properties, {}))).toEqual(["title"]);
124
+ });
125
+
126
+ it("drops a property whose type has no field, rather than throwing", () => {
127
+ const properties = {
128
+ title: { name: "Title",
129
+ type: "string" },
130
+ weird: { name: "Weird",
131
+ type: "geopoint" }
132
+ } as unknown as Properties;
133
+
134
+ expect(Object.keys(getSimplifiedProperties(properties, {}))).toEqual(["title"]);
135
+ });
136
+
137
+ it("returns nothing for an absent property map", () => {
138
+ expect(getSimplifiedProperties(undefined as unknown as Properties, {})).toEqual({});
139
+ });
140
+ });
141
+
142
+ describe("getSimplifiedProperties — nested maps", () => {
143
+
144
+ it("keys children on the dotted path the values are flattened onto", () => {
145
+ // The contract with `flatMapEntityValues`. If these disagree the model
146
+ // is told about `seo.title` and shown a value for `seo`, so it treats
147
+ // every existing value as absent.
148
+ const properties = {
149
+ seo: {
150
+ name: "SEO",
151
+ type: "map",
152
+ properties: {
153
+ title: { name: "SEO title",
154
+ type: "string" },
155
+ description: { name: "SEO description",
156
+ type: "string" }
157
+ }
158
+ }
159
+ } as unknown as Properties;
160
+
161
+ const out = getSimplifiedProperties(properties, {});
162
+
163
+ expect(Object.keys(out).sort()).toEqual(["seo", "seo.description", "seo.title"]);
164
+ expect(out["seo.title"]).toMatchObject({ name: "SEO title",
165
+ fieldConfigId: "text_field" });
166
+ // The container itself is described too, so the model knows the grouping.
167
+ expect(out.seo).toMatchObject({ type: "map",
168
+ fieldConfigId: "group" });
169
+ });
170
+
171
+ it("nests two levels deep", () => {
172
+ const properties = {
173
+ meta: {
174
+ name: "Meta",
175
+ type: "map",
176
+ properties: {
177
+ seo: {
178
+ name: "SEO",
179
+ type: "map",
180
+ properties: { title: { name: "Title",
181
+ type: "string" } }
182
+ }
183
+ }
184
+ }
185
+ } as unknown as Properties;
186
+
187
+ const out = getSimplifiedProperties(properties, {});
188
+ expect(out["meta.seo.title"]).toMatchObject({ name: "Title" });
189
+ });
190
+
191
+ it("omits a map that declares no properties", () => {
192
+ const properties = {
193
+ title: { name: "Title",
194
+ type: "string" },
195
+ blob: { name: "Blob",
196
+ type: "map" }
197
+ } as unknown as Properties;
198
+
199
+ expect(Object.keys(getSimplifiedProperties(properties, {}))).toEqual(["title"]);
200
+ });
201
+
202
+ it("omits a map whose children are all unfillable", () => {
203
+ const properties = {
204
+ refs: {
205
+ name: "Refs",
206
+ type: "map",
207
+ properties: { point: { name: "Point",
208
+ type: "geopoint" } }
209
+ }
210
+ } as unknown as Properties;
211
+
212
+ expect(getSimplifiedProperties(properties, {})).toEqual({});
213
+ });
214
+ });
215
+
216
+ describe("getSimplifiedProperties — arrays", () => {
217
+
218
+ it("describes an array of scalars by its element type", () => {
219
+ // What lets the service offer `{type: "array", items: {type: "string"}}`
220
+ // for a tags field rather than skipping it.
221
+ const properties = {
222
+ tags: { name: "Tags",
223
+ type: "array",
224
+ of: { name: "Tag",
225
+ type: "string" } }
226
+ } as unknown as Properties;
227
+
228
+ const out = getSimplifiedProperties(properties, {});
229
+ expect(out.tags).toMatchObject({ type: "array",
230
+ fieldConfigId: "repeat" });
231
+ expect(out.tags.of).toMatchObject({ type: "string",
232
+ fieldConfigId: "text_field" });
233
+ });
234
+
235
+ it("carries the element's enum, so an array of choices stays constrained", () => {
236
+ const properties = {
237
+ categories: {
238
+ name: "Categories",
239
+ type: "array",
240
+ of: { name: "Category",
241
+ type: "string",
242
+ enum: [{ id: "a",
243
+ label: "A" }, { id: "b",
244
+ label: "B" }] }
245
+ }
246
+ } as unknown as Properties;
247
+
248
+ expect(getSimplifiedProperties(properties, {}).categories.of?.enum).toEqual(["a", "b"]);
249
+ });
250
+
251
+ it("marks a disabled array so the service leaves it alone", () => {
252
+ const properties = {
253
+ tags: { name: "Tags",
254
+ type: "array",
255
+ admin: { readOnly: true },
256
+ of: { name: "Tag",
257
+ type: "string" } }
258
+ } as unknown as Properties;
259
+
260
+ expect(getSimplifiedProperties(properties, {}).tags.disabled).toBe(true);
261
+ });
262
+
263
+ it("omits an array with neither `of` nor `oneOf`", () => {
264
+ const properties = {
265
+ title: { name: "Title",
266
+ type: "string" },
267
+ mystery: { name: "Mystery",
268
+ type: "array" }
269
+ } as unknown as Properties;
270
+
271
+ expect(Object.keys(getSimplifiedProperties(properties, {}))).toEqual(["title"]);
272
+ });
273
+ });
274
+
275
+ describe("getSimplifiedProperties — oneOf blocks", () => {
276
+
277
+ const blockProperties = {
278
+ content: {
279
+ name: "Content",
280
+ type: "array",
281
+ oneOf: {
282
+ typeField: "type",
283
+ valueField: "value",
284
+ properties: {
285
+ text: { name: "Text",
286
+ type: "string",
287
+ admin: { markdown: true } },
288
+ count: { name: "Count",
289
+ type: "number" }
290
+ }
291
+ }
292
+ }
293
+ } as unknown as Properties;
294
+
295
+ it("describes the available block types when the field is empty", () => {
296
+ const out = getSimplifiedProperties(blockProperties, {});
297
+ expect(out.content).toMatchObject({ type: "array",
298
+ fieldConfigId: "block" });
299
+ expect(Object.keys(out.content.oneOf!.properties).sort()).toEqual(["count", "text"]);
300
+ expect(out.content.oneOf!.typeField).toBe("type");
301
+ expect(out.content.oneOf!.valueField).toBe("value");
302
+ });
303
+
304
+ it("expands the blocks that already exist, keyed by index", () => {
305
+ const values = {
306
+ content: [
307
+ { type: "text",
308
+ value: "Hello" },
309
+ { type: "count",
310
+ value: 3 }
311
+ ]
312
+ };
313
+
314
+ const out = getSimplifiedProperties(blockProperties, values);
315
+
316
+ expect(out["content.0.value"]).toMatchObject({ name: "Text",
317
+ fieldConfigId: "markdown" });
318
+ expect(out["content.1.value"]).toMatchObject({ name: "Count",
319
+ fieldConfigId: "number_field" });
320
+ });
321
+
322
+ it("skips a block whose declared type is not in the schema", () => {
323
+ // Data can outlive a schema change. An unknown block must not take the
324
+ // whole collection's serialization down with it.
325
+ const values = { content: [{ type: "removed_block",
326
+ value: "x" }] };
327
+ const out = getSimplifiedProperties(blockProperties, values);
328
+ expect(out.content).toBeDefined();
329
+ expect(Object.keys(out).filter((k) => k.startsWith("content."))).toEqual([]);
330
+ });
331
+
332
+ it("skips a null entry in the block list", () => {
333
+ const values = { content: [null, { type: "text",
334
+ value: "Hi" }] };
335
+ const out = getSimplifiedProperties(blockProperties, values);
336
+ expect(out["content.1.value"]).toBeDefined();
337
+ });
338
+
339
+ it("defaults the type and value field names when the schema omits them", () => {
340
+ const properties = {
341
+ content: {
342
+ name: "Content",
343
+ type: "array",
344
+ oneOf: { properties: { text: { name: "Text",
345
+ type: "string" } } }
346
+ }
347
+ } as unknown as Properties;
348
+
349
+ const out = getSimplifiedProperties(properties, { content: [{ type: "text",
350
+ value: "Hi" }] });
351
+ expect(out["content.0.value"]).toMatchObject({ name: "Text" });
352
+ });
353
+ });
354
+
355
+ describe("getSimplifiedProperties — the paths it produces", () => {
356
+
357
+ it("agrees with the flattened value paths for every shape at once", () => {
358
+ // The single assertion that catches a drift between this function and
359
+ // `flatMapEntityValues`: every leaf key the model is told about must be
360
+ // one the values can actually be keyed on.
361
+ const properties = {
362
+ title: { name: "Title",
363
+ type: "string" },
364
+ seo: {
365
+ name: "SEO",
366
+ type: "map",
367
+ properties: { title: { name: "SEO title",
368
+ type: "string" } }
369
+ },
370
+ tags: { name: "Tags",
371
+ type: "array",
372
+ of: { name: "Tag",
373
+ type: "string" } }
374
+ } as unknown as Properties;
375
+
376
+ const out = getSimplifiedProperties(properties, { title: "T",
377
+ seo: { title: "S" } });
378
+
379
+ expect(Object.keys(out).sort()).toEqual(["seo", "seo.title", "tags", "title"]);
380
+ for (const key of Object.keys(out)) {
381
+ expect(key).toMatch(/^[A-Za-z0-9_]+(\.[A-Za-z0-9_]+)*$/);
382
+ }
383
+ });
384
+ });
385
+
386
+ describe("getSimplifiedProperties — a known wart", () => {
387
+
388
+ it("emits the block's type discriminator as a bare string, not a property", () => {
389
+ // Pinned rather than fixed, because it is pre-existing and harmless in
390
+ // effect — but it *is* malformed: the return type says
391
+ // `Record<string, InputProperty>` and this entry is the raw string
392
+ // `"text"`. It reaches the service, which skips it via an explicit
393
+ // `typeof property !== "object"` guard in `planFill` (see the matching
394
+ // test in saas/backend/functions/ai.test.ts).
395
+ //
396
+ // If that guard is ever removed, `property.disabled` and `property.type`
397
+ // read `undefined` off a string and the key is dropped anyway — so the
398
+ // worst case is noise on the wire rather than a bad schema. Fixing it
399
+ // means deciding whether the model benefits from knowing a block's type,
400
+ // which is a behaviour change, not a cleanup.
401
+ const properties = {
402
+ content: {
403
+ name: "Content",
404
+ type: "array",
405
+ oneOf: { properties: { text: { name: "Text",
406
+ type: "string" } } }
407
+ }
408
+ } as unknown as Properties;
409
+
410
+ const out = getSimplifiedProperties(properties, { content: [{ type: "text",
411
+ value: "Hi" }] });
412
+
413
+ expect(Object.keys(out)).toContain("content.0.type");
414
+ expect(typeof out["content.0.type"]).toBe("string");
415
+ expect(out["content.0.type"] as unknown).toBe("text");
416
+ // The entry that carries real schema is the value one, and it is correct.
417
+ expect(out["content.0.value"]).toMatchObject({ name: "Text",
418
+ type: "string" });
419
+ });
420
+ });