@rebasepro/plugin-ai 0.0.1-canary.4829d6e
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.
- package/LICENSE +22 -0
- package/README.md +81 -0
- package/dist/api.d.ts +34 -0
- package/dist/components/DataEnhancementControllerProvider.d.ts +14 -0
- package/dist/components/FormEnhanceAction.d.ts +3 -0
- package/dist/editor/useEditorAIController.d.ts +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.es.js +741 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +772 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/types/data_enhancement_controller.d.ts +71 -0
- package/dist/useDataEnhancementPlugin.d.ts +29 -0
- package/dist/utils/diffStrings.d.ts +7 -0
- package/dist/utils/properties.d.ts +3 -0
- package/dist/utils/strings_counter.d.ts +2 -0
- package/dist/utils/suggestions.d.ts +1 -0
- package/dist/utils/values.d.ts +1 -0
- package/package.json +99 -0
- package/src/api.ts +198 -0
- package/src/components/DataEnhancementControllerProvider.tsx +342 -0
- package/src/components/FormEnhanceAction.tsx +277 -0
- package/src/editor/useEditorAIController.tsx +37 -0
- package/src/index.ts +9 -0
- package/src/tests/diffStrings.test.ts +128 -0
- package/src/tests/strings_counter.test.ts +117 -0
- package/src/tests/suggestions.test.ts +53 -0
- package/src/tests/useDataEnhancementPlugin.test.tsx +51 -0
- package/src/tests/values.test.ts +87 -0
- package/src/types/data_enhancement_controller.tsx +75 -0
- package/src/useDataEnhancementPlugin.tsx +66 -0
- package/src/utils/diffStrings.ts +70 -0
- package/src/utils/properties.ts +168 -0
- package/src/utils/strings_counter.ts +22 -0
- package/src/utils/suggestions.ts +6 -0
- package/src/utils/values.ts +12 -0
- package/src/vite-env.d.ts +1 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { getFieldId } from "@rebasepro/admin";
|
|
2
|
+
import { EnumValues, Properties, Property } from "@rebasepro/types";
|
|
3
|
+
import { isPropertyBuilder } from "@rebasepro/common";
|
|
4
|
+
import { InputProperty } from "../types/data_enhancement_controller";
|
|
5
|
+
import { getValueInPath } from "@rebasepro/utils";
|
|
6
|
+
|
|
7
|
+
export function getSimplifiedProperties<M extends Record<string, any>>(properties: Properties, values: M, path = ""): Record<string, InputProperty> {
|
|
8
|
+
if (!properties) return {};
|
|
9
|
+
return Object.entries(properties)
|
|
10
|
+
.map(([key, property]) => {
|
|
11
|
+
if (isPropertyBuilder(property)) return {};
|
|
12
|
+
const fullKey = path ? `${path}.${key}` : key;
|
|
13
|
+
const valueInPath = getValueInPath(values, fullKey);
|
|
14
|
+
return getSimplifiedProperty(property, fullKey, valueInPath)
|
|
15
|
+
})
|
|
16
|
+
.reduce((a, b) => ({ ...a,
|
|
17
|
+
...b }), {});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getSimpleProperty(property: Property): InputProperty {
|
|
21
|
+
const fieldId = getFieldId(property);
|
|
22
|
+
if (!fieldId) {
|
|
23
|
+
console.error("No fieldId found for property", property);
|
|
24
|
+
throw new Error("Field id not found");
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
name: property.name,
|
|
28
|
+
description: property.description,
|
|
29
|
+
type: property.type,
|
|
30
|
+
fieldConfigId: fieldId,
|
|
31
|
+
enum: "enum" in property && property.enum
|
|
32
|
+
? getSimpleEnumValues(property.enum)
|
|
33
|
+
: undefined,
|
|
34
|
+
disabled: Boolean(property.ui?.disabled || property.ui?.readOnly)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function getSimplifiedProperty(property: Property, path: string, value?: unknown): Record<string, InputProperty> {
|
|
39
|
+
if (isPropertyBuilder(property)) return {};
|
|
40
|
+
if (property.type === "array") {
|
|
41
|
+
|
|
42
|
+
if (property.of && !Array.isArray(property.of) && !isPropertyBuilder(property.of)) {
|
|
43
|
+
const arrayParentProperty: InputProperty = {
|
|
44
|
+
name: property.name,
|
|
45
|
+
description: property.description,
|
|
46
|
+
type: property.type,
|
|
47
|
+
fieldConfigId: "repeat",
|
|
48
|
+
disabled: Boolean(property.ui?.disabled || property.ui?.readOnly),
|
|
49
|
+
of: getSimpleProperty(property.of as Property)
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const result = { [path]: arrayParentProperty };
|
|
53
|
+
// if (Array.isArray(value)) {
|
|
54
|
+
// result = {
|
|
55
|
+
// ...result,
|
|
56
|
+
// ...value
|
|
57
|
+
// .map((v, i) => getSimplifiedProperty(property.of, `${path}.${i}`, v))
|
|
58
|
+
// .reduce((a, b) => ({ ...a, ...b }), {})
|
|
59
|
+
// };
|
|
60
|
+
// }
|
|
61
|
+
//
|
|
62
|
+
// const existingValuesCount = Array.isArray(value) ? value.length : 0;
|
|
63
|
+
//
|
|
64
|
+
// const newValuesCount = property.of && !isPropertyBuilder<any, any>(property.of) && (property.of as Property).type === "map" ? 1 : 3;
|
|
65
|
+
// result = {
|
|
66
|
+
// ...result,
|
|
67
|
+
// // ...Array.from(Array(newValuesCount))
|
|
68
|
+
// // .map((v, i) => getSimplifiedProperty(property.of, `${path}.${i + existingValuesCount}`, v))
|
|
69
|
+
// // .reduce((a, b) => ({ ...a, ...b }), {})
|
|
70
|
+
// }
|
|
71
|
+
|
|
72
|
+
return result;
|
|
73
|
+
} else if (property.oneOf) {
|
|
74
|
+
|
|
75
|
+
const arrayParentProperty: InputProperty = {
|
|
76
|
+
name: property.name,
|
|
77
|
+
description: property.description,
|
|
78
|
+
type: property.type,
|
|
79
|
+
fieldConfigId: "block",
|
|
80
|
+
disabled: Boolean(property.ui?.disabled || property.ui?.readOnly),
|
|
81
|
+
oneOf: {
|
|
82
|
+
typeField: property.oneOf.typeField,
|
|
83
|
+
valueField: property.oneOf.valueField,
|
|
84
|
+
properties: Object.entries(property.oneOf.properties)
|
|
85
|
+
.map(([key, prop]) => ({ [key]: getSimpleProperty(prop) }))
|
|
86
|
+
.reduce((a, b) => ({ ...a,
|
|
87
|
+
...b }), {})
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
if (!Array.isArray(value)) {
|
|
92
|
+
return { [path]: arrayParentProperty };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return value.map((v, i) => {
|
|
96
|
+
if (v == null) return {};
|
|
97
|
+
const typeKey = property.oneOf!.typeField ?? "type";
|
|
98
|
+
const oneOfType = v[typeKey];
|
|
99
|
+
const valueKey = property.oneOf!.valueField ?? "value";
|
|
100
|
+
const oneOfValue = v[valueKey];
|
|
101
|
+
const childProperty = property.oneOf!.properties[oneOfType];
|
|
102
|
+
if (childProperty === undefined) {
|
|
103
|
+
console.error(`No property found for type ${oneOfType}`, property.oneOf!.properties);
|
|
104
|
+
return {};
|
|
105
|
+
}
|
|
106
|
+
const simplifiedProperty = getSimplifiedProperty(childProperty, `${path}.${i}.${valueKey}`, oneOfValue);
|
|
107
|
+
return {
|
|
108
|
+
[`${path}.${i}.${typeKey}`]: oneOfType,
|
|
109
|
+
...simplifiedProperty
|
|
110
|
+
};
|
|
111
|
+
}).reduce((a, b) => ({ ...a,
|
|
112
|
+
...b }), { [path]: arrayParentProperty });
|
|
113
|
+
}
|
|
114
|
+
} else if (property.type === "map") {
|
|
115
|
+
if (property.properties) {
|
|
116
|
+
const mapProperties: Record<string, InputProperty> = Object.entries(property.properties)
|
|
117
|
+
.map(([key, childProperty]) => {
|
|
118
|
+
const childValue = value && typeof value === "object" ? (value as Record<string, unknown>)[key] : undefined;
|
|
119
|
+
return getSimplifiedProperty(childProperty, key, childValue);
|
|
120
|
+
})
|
|
121
|
+
.map(o => attachPathToKeys(o, path))
|
|
122
|
+
.reduce((a, b) => ({ ...a,
|
|
123
|
+
...b }), {});
|
|
124
|
+
|
|
125
|
+
if (Object.keys(mapProperties).length === 0) return {};
|
|
126
|
+
const mapParentProperty: InputProperty = {
|
|
127
|
+
name: property.name,
|
|
128
|
+
description: property.description,
|
|
129
|
+
type: property.type,
|
|
130
|
+
fieldConfigId: "group",
|
|
131
|
+
disabled: Boolean(property.ui?.disabled || property.ui?.readOnly)
|
|
132
|
+
};
|
|
133
|
+
return {
|
|
134
|
+
[path]: mapParentProperty,
|
|
135
|
+
...mapProperties
|
|
136
|
+
} as Record<string, InputProperty>;
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
const fieldId = getFieldId(property);
|
|
140
|
+
if (!fieldId) {
|
|
141
|
+
console.warn(`No fieldId found for property ${path} with type ${property.type}`);
|
|
142
|
+
return {};
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
[path]: getSimpleProperty(property)
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
return {};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// attach a path to every key in an object
|
|
152
|
+
function attachPathToKeys(obj: Record<string, InputProperty>, path = ""): Record<string, InputProperty> {
|
|
153
|
+
return Object.entries(obj)
|
|
154
|
+
.map(([key, value]) => {
|
|
155
|
+
const fullKey = path ? `${path}.${key}` : key;
|
|
156
|
+
return { [fullKey]: value };
|
|
157
|
+
})
|
|
158
|
+
.reduce((a, b) => ({ ...a,
|
|
159
|
+
...b }), {});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function getSimpleEnumValues(enumValues: EnumValues): string[] {
|
|
163
|
+
if (Array.isArray(enumValues))
|
|
164
|
+
return enumValues.map(v => String(v.id));
|
|
165
|
+
if (typeof enumValues === "object")
|
|
166
|
+
return Object.keys(enumValues);
|
|
167
|
+
throw Error("getSimpleEnumValues: Invalid enumValues");
|
|
168
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { EntityValues, Properties, Property } from "@rebasepro/types";
|
|
2
|
+
|
|
3
|
+
export function countStringCharacters(values: EntityValues<any>, properties: Properties) {
|
|
4
|
+
let count = 0;
|
|
5
|
+
|
|
6
|
+
for (const key in values) {
|
|
7
|
+
const value = values[key];
|
|
8
|
+
const property: Property = properties[key];
|
|
9
|
+
|
|
10
|
+
if (property && !property.ui?.disabled) {
|
|
11
|
+
if (property.type === "string" || property.type === "number") {
|
|
12
|
+
count += String(value).length;
|
|
13
|
+
} else if (property.type === "array" && Array.isArray(value) && property.of && !Array.isArray(property.of) && property.of.type === "string") {
|
|
14
|
+
count += (value as string[]).reduce((acc, curr) => acc + (curr?.length ?? 0), 0);
|
|
15
|
+
} else if (property.type === "map" && property.properties && typeof value === "object") {
|
|
16
|
+
count += countStringCharacters(value, property.properties);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return count;
|
|
22
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export function getAppendableSuggestion(suggestion: string | number | undefined, value: unknown): string | undefined {
|
|
2
|
+
const suggestionIncludesValue = typeof suggestion === "string" && typeof value === "string" && suggestion.toLowerCase().trim().startsWith(value.toLowerCase().trim());
|
|
3
|
+
return (typeof value === "string" && suggestionIncludesValue)
|
|
4
|
+
? suggestion.substring(suggestion.toLowerCase().trim().indexOf(value.toLowerCase().trim()) + value.trim().length)
|
|
5
|
+
: undefined;
|
|
6
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function flatMapEntityValues<M extends object>(values: M, path = ""): object {
|
|
2
|
+
if (!values) return {};
|
|
3
|
+
return Object.entries(values).flatMap(([key, value]) => {
|
|
4
|
+
const currentPath = path ? `${path}.${key}` : key;
|
|
5
|
+
if (typeof value === "object") {
|
|
6
|
+
return flatMapEntityValues(value, currentPath);
|
|
7
|
+
} else {
|
|
8
|
+
return { [currentPath]: value };
|
|
9
|
+
}
|
|
10
|
+
}).reduce((acc, curr) => ({ ...acc,
|
|
11
|
+
...curr }), {})
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|