@flowgram.ai/form-materials 0.1.0-alpha.10

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 (100) hide show
  1. package/bin/index.ts +99 -0
  2. package/bin/materials.ts +156 -0
  3. package/bin/project.ts +91 -0
  4. package/dist/esm/index.js +3303 -0
  5. package/dist/esm/index.js.map +1 -0
  6. package/dist/index.d.mts +495 -0
  7. package/dist/index.d.ts +495 -0
  8. package/dist/index.js +3299 -0
  9. package/dist/index.js.map +1 -0
  10. package/package.json +75 -0
  11. package/src/components/batch-outputs/config.json +12 -0
  12. package/src/components/batch-outputs/index.tsx +61 -0
  13. package/src/components/batch-outputs/styles.tsx +19 -0
  14. package/src/components/batch-outputs/types.ts +22 -0
  15. package/src/components/batch-outputs/use-list.ts +86 -0
  16. package/src/components/batch-variable-selector/config.json +5 -0
  17. package/src/components/batch-variable-selector/index.tsx +24 -0
  18. package/src/components/code-editor/config.json +9 -0
  19. package/src/components/code-editor/index.tsx +74 -0
  20. package/src/components/code-editor/language-features.ts +24 -0
  21. package/src/components/code-editor/theme/dark.ts +119 -0
  22. package/src/components/code-editor/theme/index.ts +12 -0
  23. package/src/components/code-editor/theme/light.ts +119 -0
  24. package/src/components/code-editor/utils.ts +20 -0
  25. package/src/components/condition-row/config.json +5 -0
  26. package/src/components/condition-row/constants.ts +128 -0
  27. package/src/components/condition-row/hooks/useOp.tsx +50 -0
  28. package/src/components/condition-row/hooks/useRule.ts +31 -0
  29. package/src/components/condition-row/index.tsx +81 -0
  30. package/src/components/condition-row/styles.tsx +30 -0
  31. package/src/components/condition-row/types.ts +42 -0
  32. package/src/components/constant-input/config.json +6 -0
  33. package/src/components/constant-input/index.tsx +86 -0
  34. package/src/components/constant-input/types.ts +23 -0
  35. package/src/components/dynamic-value-input/config.json +5 -0
  36. package/src/components/dynamic-value-input/index.tsx +92 -0
  37. package/src/components/dynamic-value-input/styles.tsx +26 -0
  38. package/src/components/index.ts +18 -0
  39. package/src/components/json-editor-with-variables/config.json +13 -0
  40. package/src/components/json-editor-with-variables/extensions/variable-tag.tsx +173 -0
  41. package/src/components/json-editor-with-variables/extensions/variable-tree.tsx +83 -0
  42. package/src/components/json-editor-with-variables/index.tsx +19 -0
  43. package/src/components/json-editor-with-variables/styles.tsx +44 -0
  44. package/src/components/json-schema-editor/components/blur-input.tsx +27 -0
  45. package/src/components/json-schema-editor/config.json +13 -0
  46. package/src/components/json-schema-editor/default-value.tsx +135 -0
  47. package/src/components/json-schema-editor/hooks.tsx +166 -0
  48. package/src/components/json-schema-editor/index.tsx +267 -0
  49. package/src/components/json-schema-editor/styles.tsx +240 -0
  50. package/src/components/json-schema-editor/types.ts +26 -0
  51. package/src/components/json-schema-editor/utils.ts +29 -0
  52. package/src/components/prompt-editor/config.json +9 -0
  53. package/src/components/prompt-editor/extensions/jinja.tsx +58 -0
  54. package/src/components/prompt-editor/extensions/language-support.tsx +19 -0
  55. package/src/components/prompt-editor/extensions/markdown.tsx +75 -0
  56. package/src/components/prompt-editor/index.tsx +68 -0
  57. package/src/components/prompt-editor/styles.tsx +18 -0
  58. package/src/components/prompt-editor/types.tsx +18 -0
  59. package/src/components/prompt-editor-with-inputs/config.json +13 -0
  60. package/src/components/prompt-editor-with-inputs/extensions/inputs-tree.tsx +82 -0
  61. package/src/components/prompt-editor-with-inputs/index.tsx +22 -0
  62. package/src/components/prompt-editor-with-inputs/inputs-picker.tsx +100 -0
  63. package/src/components/prompt-editor-with-variables/config.json +13 -0
  64. package/src/components/prompt-editor-with-variables/extensions/variable-tag.tsx +179 -0
  65. package/src/components/prompt-editor-with-variables/extensions/variable-tree.tsx +83 -0
  66. package/src/components/prompt-editor-with-variables/index.tsx +19 -0
  67. package/src/components/prompt-editor-with-variables/styles.tsx +44 -0
  68. package/src/components/type-selector/config.json +5 -0
  69. package/src/components/type-selector/constants.tsx +364 -0
  70. package/src/components/type-selector/index.tsx +62 -0
  71. package/src/components/variable-selector/config.json +5 -0
  72. package/src/components/variable-selector/index.tsx +116 -0
  73. package/src/components/variable-selector/styles.tsx +59 -0
  74. package/src/components/variable-selector/use-variable-tree.tsx +103 -0
  75. package/src/effects/auto-rename-ref/config.json +5 -0
  76. package/src/effects/auto-rename-ref/index.ts +109 -0
  77. package/src/effects/index.ts +10 -0
  78. package/src/effects/provide-batch-input/config.json +5 -0
  79. package/src/effects/provide-batch-input/index.ts +43 -0
  80. package/src/effects/provide-batch-outputs/config.json +5 -0
  81. package/src/effects/provide-batch-outputs/index.ts +38 -0
  82. package/src/effects/provide-json-schema-outputs/config.json +8 -0
  83. package/src/effects/provide-json-schema-outputs/index.ts +28 -0
  84. package/src/effects/sync-variable-title/config.json +5 -0
  85. package/src/effects/sync-variable-title/index.ts +28 -0
  86. package/src/form-plugins/batch-outputs-plugin/config.json +7 -0
  87. package/src/form-plugins/batch-outputs-plugin/index.ts +104 -0
  88. package/src/form-plugins/index.ts +6 -0
  89. package/src/index.ts +10 -0
  90. package/src/typings/flow-value/config.json +5 -0
  91. package/src/typings/flow-value/index.ts +32 -0
  92. package/src/typings/index.ts +7 -0
  93. package/src/typings/json-schema/config.json +5 -0
  94. package/src/typings/json-schema/index.ts +36 -0
  95. package/src/utils/format-legacy-refs/config.json +5 -0
  96. package/src/utils/format-legacy-refs/index.ts +158 -0
  97. package/src/utils/format-legacy-refs/readme.md +38 -0
  98. package/src/utils/index.ts +7 -0
  99. package/src/utils/json-schema/config.json +5 -0
  100. package/src/utils/json-schema/index.ts +180 -0
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { isObject } from 'lodash';
7
+
8
+ interface LegacyFlowRefValueSchema {
9
+ type: 'ref';
10
+ content: string;
11
+ }
12
+
13
+ interface NewFlowRefValueSchema {
14
+ type: 'ref';
15
+ content: string[];
16
+ }
17
+
18
+ /**
19
+ * In flowgram 0.2.0, for introducing Loop variable functionality,
20
+ * the FlowRefValueSchema type definition is updated:
21
+ *
22
+ * interface LegacyFlowRefValueSchema {
23
+ * type: 'ref';
24
+ * content: string;
25
+ * }
26
+ *
27
+ * interface NewFlowRefValueSchema {
28
+ * type: 'ref';
29
+ * content: string[];
30
+ * }
31
+ *
32
+ *
33
+ * For making sure backend json will not be changed, we provide format legacy ref utils for updating the formData
34
+ *
35
+ * How to use:
36
+ *
37
+ * 1. Call formatLegacyRefOnSubmit on the formData before submitting
38
+ * 2. Call formatLegacyRefOnInit on the formData after submitting
39
+ *
40
+ * Example:
41
+ * import { formatLegacyRefOnSubmit, formatLegacyRefOnInit } from '@flowgram.ai/form-materials';
42
+ * formMeta: {
43
+ * formatOnSubmit: (data) => formatLegacyRefOnSubmit(data),
44
+ * formatOnInit: (data) => formatLegacyRefOnInit(data),
45
+ * }
46
+ */
47
+ export function formatLegacyRefOnSubmit(value: any): any {
48
+ if (isObject(value)) {
49
+ if (isLegacyFlowRefValueSchema(value)) {
50
+ return formatLegacyRefToNewRef(value);
51
+ }
52
+
53
+ return Object.fromEntries(
54
+ Object.entries(value).map(([key, value]: [string, any]) => [
55
+ key,
56
+ formatLegacyRefOnSubmit(value),
57
+ ])
58
+ );
59
+ }
60
+
61
+ if (Array.isArray(value)) {
62
+ return value.map(formatLegacyRefOnSubmit);
63
+ }
64
+
65
+ return value;
66
+ }
67
+
68
+ /**
69
+ * In flowgram 0.2.0, for introducing Loop variable functionality,
70
+ * the FlowRefValueSchema type definition is updated:
71
+ *
72
+ * interface LegacyFlowRefValueSchema {
73
+ * type: 'ref';
74
+ * content: string;
75
+ * }
76
+ *
77
+ * interface NewFlowRefValueSchema {
78
+ * type: 'ref';
79
+ * content: string[];
80
+ * }
81
+ *
82
+ *
83
+ * For making sure backend json will not be changed, we provide format legacy ref utils for updating the formData
84
+ *
85
+ * How to use:
86
+ *
87
+ * 1. Call formatLegacyRefOnSubmit on the formData before submitting
88
+ * 2. Call formatLegacyRefOnInit on the formData after submitting
89
+ *
90
+ * Example:
91
+ * import { formatLegacyRefOnSubmit, formatLegacyRefOnInit } from '@flowgram.ai/form-materials';
92
+ *
93
+ * formMeta: {
94
+ * formatOnSubmit: (data) => formatLegacyRefOnSubmit(data),
95
+ * formatOnInit: (data) => formatLegacyRefOnInit(data),
96
+ * }
97
+ */
98
+ export function formatLegacyRefOnInit(value: any): any {
99
+ if (isObject(value)) {
100
+ if (isNewFlowRefValueSchema(value)) {
101
+ return formatNewRefToLegacyRef(value);
102
+ }
103
+
104
+ return Object.fromEntries(
105
+ Object.entries(value).map(([key, value]: [string, any]) => [
106
+ key,
107
+ formatLegacyRefOnInit(value),
108
+ ])
109
+ );
110
+ }
111
+
112
+ if (Array.isArray(value)) {
113
+ return value.map(formatLegacyRefOnInit);
114
+ }
115
+
116
+ return value;
117
+ }
118
+
119
+ export function isLegacyFlowRefValueSchema(value: any): value is LegacyFlowRefValueSchema {
120
+ return (
121
+ isObject(value) &&
122
+ Object.keys(value).length === 2 &&
123
+ (value as any).type === 'ref' &&
124
+ typeof (value as any).content === 'string'
125
+ );
126
+ }
127
+
128
+ export function isNewFlowRefValueSchema(value: any): value is NewFlowRefValueSchema {
129
+ return (
130
+ isObject(value) &&
131
+ Object.keys(value).length === 2 &&
132
+ (value as any).type === 'ref' &&
133
+ Array.isArray((value as any).content)
134
+ );
135
+ }
136
+
137
+ export function formatLegacyRefToNewRef(value: LegacyFlowRefValueSchema) {
138
+ const keyPath = value.content.split('.');
139
+
140
+ if (keyPath[1] === 'outputs') {
141
+ return {
142
+ type: 'ref',
143
+ content: [`${keyPath[0]}.${keyPath[1]}`, ...(keyPath.length > 2 ? keyPath.slice(2) : [])],
144
+ };
145
+ }
146
+
147
+ return {
148
+ type: 'ref',
149
+ content: keyPath,
150
+ };
151
+ }
152
+
153
+ export function formatNewRefToLegacyRef(value: NewFlowRefValueSchema) {
154
+ return {
155
+ type: 'ref',
156
+ content: value.content.join('.'),
157
+ };
158
+ }
@@ -0,0 +1,38 @@
1
+ # Notice
2
+
3
+ In `@flowgram.ai/form-materials@0.2.0`, for introducing loop-related materials,
4
+
5
+ The FlowRefValueSchema type definition is updated:
6
+
7
+ ```typescript
8
+ interface LegacyFlowRefValueSchema {
9
+ type: 'ref';
10
+ content: string;
11
+ }
12
+
13
+ interface NewFlowRefValueSchema {
14
+ type: 'ref';
15
+ content: string[];
16
+ }
17
+ ```
18
+
19
+
20
+
21
+ For making sure backend json will not be changed in your application, we provide `format-legacy-ref` utils for upgrading
22
+
23
+
24
+ How to use:
25
+
26
+ 1. Call formatLegacyRefOnSubmit on the formData before submitting
27
+ 2. Call formatLegacyRefOnInit on the formData after submitting
28
+
29
+ Example:
30
+
31
+ ```typescript
32
+ import { formatLegacyRefOnSubmit, formatLegacyRefOnInit } from '@flowgram.ai/form-materials';
33
+
34
+ formMeta: {
35
+ formatOnSubmit: (data) => formatLegacyRefOnSubmit(data),
36
+ formatOnInit: (data) => formatLegacyRefOnInit(data),
37
+ }
38
+ ```
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ export * from './format-legacy-refs';
7
+ export * from './json-schema';
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "json-schema",
3
+ "depMaterials": ["typings/json-schema"],
4
+ "depPackages": ["lodash"]
5
+ }
@@ -0,0 +1,180 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { get } from 'lodash';
7
+ import { ASTFactory, ASTKind, ASTMatch, ASTNode, ASTNodeJSON, BaseType } from '@flowgram.ai/editor';
8
+
9
+ import { IJsonSchema } from '../../typings/json-schema';
10
+
11
+ export namespace JsonSchemaUtils {
12
+ /**
13
+ * Converts a JSON schema to an Abstract Syntax Tree (AST) representation.
14
+ * This function recursively processes the JSON schema and creates corresponding AST nodes.
15
+ *
16
+ * For more information on JSON Schema, refer to the official documentation:
17
+ * https://json-schema.org/
18
+ *
19
+ * @param jsonSchema - The JSON schema to convert.
20
+ * @returns An AST node representing the JSON schema, or undefined if the schema type is not recognized.
21
+ */
22
+ export function schemaToAST(jsonSchema: IJsonSchema): ASTNodeJSON | undefined {
23
+ const { type, extra } = jsonSchema || {};
24
+ const { weak = false } = extra || {};
25
+
26
+ if (!type) {
27
+ return undefined;
28
+ }
29
+
30
+ switch (type) {
31
+ case 'object':
32
+ if (weak) {
33
+ return { kind: ASTKind.Object, weak: true };
34
+ }
35
+ return ASTFactory.createObject({
36
+ properties: Object.entries(jsonSchema.properties || {})
37
+ /**
38
+ * Sorts the properties of a JSON schema based on the 'extra.index' field.
39
+ * If the 'extra.index' field is not present, the property will be treated as having an index of 0.
40
+ */
41
+ .sort((a, b) => (get(a?.[1], 'extra.index') || 0) - (get(b?.[1], 'extra.index') || 0))
42
+ .map(([key, _property]) => ({
43
+ key,
44
+ type: schemaToAST(_property),
45
+ meta: {
46
+ title: _property.title,
47
+ description: _property.description,
48
+ },
49
+ })),
50
+ });
51
+ case 'array':
52
+ if (weak) {
53
+ return { kind: ASTKind.Array, weak: true };
54
+ }
55
+ return ASTFactory.createArray({
56
+ items: schemaToAST(jsonSchema.items!),
57
+ });
58
+ case 'map':
59
+ if (weak) {
60
+ return { kind: ASTKind.Map, weak: true };
61
+ }
62
+ return ASTFactory.createMap({
63
+ valueType: schemaToAST(jsonSchema.additionalProperties!),
64
+ });
65
+ case 'string':
66
+ return ASTFactory.createString();
67
+ case 'number':
68
+ return ASTFactory.createNumber();
69
+ case 'boolean':
70
+ return ASTFactory.createBoolean();
71
+ case 'integer':
72
+ return ASTFactory.createInteger();
73
+
74
+ default:
75
+ // If the type is not recognized, return CustomType
76
+ return ASTFactory.createCustomType({ typeName: type });
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Convert AST To JSON Schema
82
+ * @param typeAST
83
+ * @returns
84
+ */
85
+ export function astToSchema(
86
+ typeAST: ASTNode,
87
+ options?: { drilldown?: boolean }
88
+ ): IJsonSchema | undefined {
89
+ const { drilldown = true } = options || {};
90
+
91
+ if (ASTMatch.isString(typeAST)) {
92
+ return {
93
+ type: 'string',
94
+ };
95
+ }
96
+
97
+ if (ASTMatch.isBoolean(typeAST)) {
98
+ return {
99
+ type: 'boolean',
100
+ };
101
+ }
102
+
103
+ if (ASTMatch.isNumber(typeAST)) {
104
+ return {
105
+ type: 'number',
106
+ };
107
+ }
108
+
109
+ if (ASTMatch.isInteger(typeAST)) {
110
+ return {
111
+ type: 'integer',
112
+ };
113
+ }
114
+
115
+ if (ASTMatch.isObject(typeAST)) {
116
+ return {
117
+ type: 'object',
118
+ properties: drilldown
119
+ ? Object.fromEntries(
120
+ typeAST.properties.map((property) => {
121
+ const schema = astToSchema(property.type);
122
+
123
+ if (property.meta?.title && schema) {
124
+ schema.title = property.meta.title;
125
+ }
126
+ if (property.meta?.description && schema) {
127
+ schema.description = property.meta.description;
128
+ }
129
+
130
+ return [property.key, schema!];
131
+ })
132
+ )
133
+ : {},
134
+ };
135
+ }
136
+
137
+ if (ASTMatch.isArray(typeAST)) {
138
+ return {
139
+ type: 'array',
140
+ items: drilldown ? astToSchema(typeAST.items) : undefined,
141
+ };
142
+ }
143
+
144
+ if (ASTMatch.isMap(typeAST)) {
145
+ return {
146
+ type: 'map',
147
+ items: drilldown ? astToSchema(typeAST.valueType) : undefined,
148
+ };
149
+ }
150
+
151
+ if (ASTMatch.isCustomType(typeAST)) {
152
+ return {
153
+ type: typeAST.typeName,
154
+ };
155
+ }
156
+
157
+ return undefined;
158
+ }
159
+
160
+ /**
161
+ * Check if the AST type is match the JSON Schema
162
+ * @param typeAST
163
+ * @param schema
164
+ * @returns
165
+ */
166
+ export function isASTMatchSchema(
167
+ typeAST: BaseType,
168
+ schema: IJsonSchema | IJsonSchema[]
169
+ ): boolean {
170
+ if (Array.isArray(schema)) {
171
+ return typeAST.isTypeEqual(
172
+ ASTFactory.createUnion({
173
+ types: schema.map((_schema) => schemaToAST(_schema)!).filter(Boolean),
174
+ })
175
+ );
176
+ }
177
+
178
+ return typeAST.isTypeEqual(schemaToAST(schema));
179
+ }
180
+ }