@flowgram.ai/form-materials 0.2.17 → 0.2.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowgram.ai/form-materials",
3
- "version": "0.2.17",
3
+ "version": "0.2.18",
4
4
  "homepage": "https://flowgram.ai/",
5
5
  "repository": "https://github.com/bytedance/flowgram.ai",
6
6
  "license": "MIT",
@@ -30,7 +30,9 @@
30
30
  "chalk": "^5.3.0",
31
31
  "inquirer": "^9.2.7",
32
32
  "immer": "~10.1.1",
33
- "@flowgram.ai/editor": "0.2.17"
33
+ "@coze-editor/editor": "0.1.0-alpha.8d7a30",
34
+ "@codemirror/view": "~6.38.0",
35
+ "@flowgram.ai/editor": "0.2.18"
34
36
  },
35
37
  "devDependencies": {
36
38
  "@types/lodash": "^4.14.137",
@@ -46,8 +48,8 @@
46
48
  "tsup": "^8.0.1",
47
49
  "typescript": "^5.0.4",
48
50
  "vitest": "^0.34.6",
49
- "@flowgram.ai/eslint-config": "0.2.17",
50
- "@flowgram.ai/ts-config": "0.2.17"
51
+ "@flowgram.ai/ts-config": "0.2.18",
52
+ "@flowgram.ai/eslint-config": "0.2.18"
51
53
  },
52
54
  "peerDependencies": {
53
55
  "react": ">=16.8",
@@ -50,6 +50,7 @@ export function DynamicValueInput({
50
50
  // Display Variable Or Delete
51
51
  return (
52
52
  <VariableSelector
53
+ style={{ width: '100%' }}
53
54
  value={value?.content}
54
55
  onChange={(_v) => onChange(_v ? { type: 'ref', content: _v } : undefined)}
55
56
  includeSchema={includeSchema}
@@ -13,6 +13,8 @@ export const UIContainer = styled.div`
13
13
 
14
14
  export const UIMain = styled.div`
15
15
  flex-grow: 1;
16
+ overflow: hidden;
17
+ min-width: 0;
16
18
 
17
19
  & .semi-tree-select,
18
20
  & .semi-input-number,
@@ -11,3 +11,5 @@ export * from './constant-input';
11
11
  export * from './dynamic-value-input';
12
12
  export * from './condition-row';
13
13
  export * from './batch-outputs';
14
+ export * from './prompt-editor';
15
+ export * from './prompt-editor-with-variables';
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "prompt-editor",
3
+ "depMaterials": [],
4
+ "depPackages": [
5
+ "@coze-editor/editor@0.1.0-alpha.8d7a30",
6
+ "@codemirror/view",
7
+ "styled-components"
8
+ ]
9
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { useLayoutEffect } from 'react';
7
+
8
+ import { useInjector } from '@coze-editor/editor/react';
9
+ import { astDecorator } from '@coze-editor/editor';
10
+ import { EditorView } from '@codemirror/view';
11
+
12
+ function JinjaHighlight() {
13
+ const injector = useInjector();
14
+
15
+ useLayoutEffect(
16
+ () =>
17
+ injector.inject([
18
+ astDecorator.whole.of((cursor) => {
19
+ if (cursor.name === 'JinjaStatementStart' || cursor.name === 'JinjaStatementEnd') {
20
+ return {
21
+ type: 'className',
22
+ className: 'jinja-statement-bracket',
23
+ };
24
+ }
25
+
26
+ if (cursor.name === 'JinjaComment') {
27
+ return {
28
+ type: 'className',
29
+ className: 'jinja-comment',
30
+ };
31
+ }
32
+
33
+ if (cursor.name === 'JinjaExpression') {
34
+ return {
35
+ type: 'className',
36
+ className: 'jinja-expression',
37
+ };
38
+ }
39
+ }),
40
+ EditorView.theme({
41
+ '.jinja-statement-bracket': {
42
+ color: '#D1009D',
43
+ },
44
+ '.jinja-comment': {
45
+ color: '#0607094D',
46
+ },
47
+ '.jinja-expression': {
48
+ color: '#4E40E5',
49
+ },
50
+ }),
51
+ ]),
52
+ [injector]
53
+ );
54
+
55
+ return null;
56
+ }
57
+
58
+ export default JinjaHighlight;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { useLayoutEffect } from 'react';
7
+
8
+ import { useInjector } from '@coze-editor/editor/react';
9
+ import { languageSupport } from '@coze-editor/editor/preset-prompt';
10
+
11
+ function LanguageSupport() {
12
+ const injector = useInjector();
13
+
14
+ useLayoutEffect(() => injector.inject([languageSupport]), [injector]);
15
+
16
+ return null;
17
+ }
18
+
19
+ export default LanguageSupport;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { useLayoutEffect } from 'react';
7
+
8
+ import { useInjector } from '@coze-editor/editor/react';
9
+ import { astDecorator } from '@coze-editor/editor';
10
+ import { EditorView } from '@codemirror/view';
11
+
12
+ function MarkdownHighlight() {
13
+ const injector = useInjector();
14
+
15
+ useLayoutEffect(
16
+ () =>
17
+ injector.inject([
18
+ astDecorator.whole.of((cursor) => {
19
+ // # heading
20
+ if (cursor.name.startsWith('ATXHeading')) {
21
+ return {
22
+ type: 'className',
23
+ className: 'heading',
24
+ };
25
+ }
26
+
27
+ // *italic*
28
+ if (cursor.name === 'Emphasis') {
29
+ return {
30
+ type: 'className',
31
+ className: 'emphasis',
32
+ };
33
+ }
34
+
35
+ // **bold**
36
+ if (cursor.name === 'StrongEmphasis') {
37
+ return {
38
+ type: 'className',
39
+ className: 'strong-emphasis',
40
+ };
41
+ }
42
+
43
+ // -
44
+ // 1.
45
+ // >
46
+ if (cursor.name === 'ListMark' || cursor.name === 'QuoteMark') {
47
+ return {
48
+ type: 'className',
49
+ className: 'mark',
50
+ };
51
+ }
52
+ }),
53
+ EditorView.theme({
54
+ '.heading': {
55
+ color: '#00818C',
56
+ fontWeight: 'bold',
57
+ },
58
+ '.emphasis': {
59
+ fontStyle: 'italic',
60
+ },
61
+ '.strong-emphasis': {
62
+ fontWeight: 'bold',
63
+ },
64
+ '.mark': {
65
+ color: '#4E40E5',
66
+ },
67
+ }),
68
+ ]),
69
+ [injector]
70
+ );
71
+
72
+ return null;
73
+ }
74
+
75
+ export default MarkdownHighlight;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React from 'react';
7
+
8
+ import { Renderer, EditorProvider } from '@coze-editor/editor/react';
9
+ import preset from '@coze-editor/editor/preset-prompt';
10
+
11
+ import { PropsType } from './types';
12
+ import { UIContainer } from './styles';
13
+ import MarkdownHighlight from './extensions/markdown';
14
+ import LanguageSupport from './extensions/language-support';
15
+ import JinjaHighlight from './extensions/jinja';
16
+
17
+ export type PromptEditorPropsType = PropsType;
18
+
19
+ export function PromptEditor(props: PropsType) {
20
+ const { value, onChange, readonly, style, hasError, children } = props || {};
21
+
22
+ return (
23
+ <UIContainer $hasError={hasError} style={style}>
24
+ <EditorProvider>
25
+ <Renderer
26
+ plugins={preset}
27
+ defaultValue={String(value?.content)}
28
+ options={{
29
+ readOnly: readonly,
30
+ editable: !readonly,
31
+ }}
32
+ onChange={(e) => {
33
+ onChange({ type: 'template', content: e.value });
34
+ }}
35
+ />
36
+ <MarkdownHighlight />
37
+ <LanguageSupport />
38
+ <JinjaHighlight />
39
+ {children}
40
+ </EditorProvider>
41
+ </UIContainer>
42
+ );
43
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import styled, { css } from 'styled-components';
7
+
8
+ export const UIContainer = styled.div<{ $hasError?: boolean }>`
9
+ background-color: var(--semi-color-fill-0);
10
+ padding-left: 10px;
11
+ padding-right: 6px;
12
+
13
+ ${({ $hasError }) =>
14
+ $hasError &&
15
+ css`
16
+ border: 1px solid var(--semi-color-danger-6);
17
+ `}
18
+ `;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React from 'react';
7
+
8
+ import { IFlowTemplateValue } from '../../typings';
9
+
10
+ export type PropsType = React.PropsWithChildren<{
11
+ value?: IFlowTemplateValue;
12
+ onChange: (value?: IFlowTemplateValue) => void;
13
+ readonly?: boolean;
14
+ hasError?: boolean;
15
+ style?: React.CSSProperties;
16
+ }>;
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "prompt-editor",
3
+ "depMaterials": [],
4
+ "depPackages": [
5
+ "@coze-editor/editor@0.1.0-alpha.8d7a30",
6
+ "@codemirror/view",
7
+ "styled-components",
8
+ "@douyinfe/semi-ui"
9
+ ]
10
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React, { useEffect, useState } from 'react';
7
+
8
+ import { Popover, Tree } from '@douyinfe/semi-ui';
9
+ import {
10
+ Mention,
11
+ MentionOpenChangeEvent,
12
+ getCurrentMentionReplaceRange,
13
+ useEditor,
14
+ PositionMirror,
15
+ } from '@coze-editor/editor/react';
16
+ import { EditorAPI } from '@coze-editor/editor/preset-prompt';
17
+
18
+ import { useVariableTree } from '../../variable-selector';
19
+
20
+ function Variable() {
21
+ const [posKey, setPosKey] = useState('');
22
+ const [visible, setVisible] = useState(false);
23
+ const [position, setPosition] = useState(-1);
24
+ const editor = useEditor<EditorAPI>();
25
+
26
+ function insert(variablePath: string) {
27
+ const range = getCurrentMentionReplaceRange(editor.$view.state);
28
+
29
+ if (!range) {
30
+ return;
31
+ }
32
+
33
+ editor.replaceText({
34
+ ...range,
35
+ text: '{{' + variablePath + '}}',
36
+ });
37
+
38
+ setVisible(false);
39
+ }
40
+
41
+ function handleOpenChange(e: MentionOpenChangeEvent) {
42
+ setPosition(e.state.selection.main.head);
43
+ setVisible(e.value);
44
+ }
45
+
46
+ useEffect(() => {
47
+ if (!editor) {
48
+ return;
49
+ }
50
+ }, [editor, visible]);
51
+
52
+ const treeData = useVariableTree({});
53
+
54
+ return (
55
+ <>
56
+ <Mention triggerCharacters={['{', '{}']} onOpenChange={handleOpenChange} />
57
+
58
+ <Popover
59
+ visible={visible}
60
+ trigger="custom"
61
+ position="topLeft"
62
+ rePosKey={posKey}
63
+ content={
64
+ <div style={{ width: 300 }}>
65
+ <Tree
66
+ treeData={treeData}
67
+ onSelect={(v) => {
68
+ insert(v);
69
+ }}
70
+ />
71
+ </div>
72
+ }
73
+ >
74
+ {/* PositionMirror allows the Popover to appear at the specified cursor position */}
75
+ <PositionMirror
76
+ position={position}
77
+ // When Doc scroll, update position
78
+ onChange={() => setPosKey(String(Math.random()))}
79
+ />
80
+ </Popover>
81
+ </>
82
+ );
83
+ }
84
+
85
+ export default Variable;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React from 'react';
7
+
8
+ import Variable from './extensions/variable';
9
+ import { PromptEditor, PromptEditorPropsType } from '../prompt-editor';
10
+
11
+ export function PromptEditorWithVariables(props: PromptEditorPropsType) {
12
+ return (
13
+ <PromptEditor {...props}>
14
+ <Variable />
15
+ </PromptEditor>
16
+ );
17
+ }
@@ -11,7 +11,7 @@ import { IconChevronDownStroked, IconIssueStroked } from '@douyinfe/semi-icons';
11
11
 
12
12
  import { IJsonSchema } from '../../typings/json-schema';
13
13
  import { useVariableTree } from './use-variable-tree';
14
- import { UIRootTitle, UITag, UITreeSelect } from './styles';
14
+ import { UIRootTitle, UITag, UITreeSelect, UIVarName } from './styles';
15
15
 
16
16
  interface PropTypes {
17
17
  value?: string[];
@@ -102,7 +102,7 @@ export const VariableSelector = ({
102
102
  <UIRootTitle>
103
103
  {_option.rootMeta?.title ? `${_option.rootMeta?.title} -` : null}
104
104
  </UIRootTitle>
105
- {_option.label}
105
+ <UIVarName>{_option.label}</UIVarName>
106
106
  </UITag>
107
107
  );
108
108
  }}
@@ -6,11 +6,22 @@
6
6
  import styled from 'styled-components';
7
7
  import { Tag, TreeSelect } from '@douyinfe/semi-ui';
8
8
 
9
- export const UIRootTitle = styled.span`
9
+ export const UIRootTitle = styled.div`
10
10
  margin-right: 4px;
11
+ min-width: 20px;
12
+ overflow: hidden;
13
+ text-overflow: ellipsis;
14
+ white-space: nowrap;
11
15
  color: var(--semi-color-text-2);
12
16
  `;
13
17
 
18
+ export const UIVarName = styled.div`
19
+ overflow: hidden;
20
+ text-overflow: ellipsis;
21
+ white-space: nowrap;
22
+ min-width: 50%;
23
+ `;
24
+
14
25
  export const UITag = styled(Tag)`
15
26
  width: 100%;
16
27
  display: flex;
@@ -42,7 +42,10 @@ export namespace JsonSchemaUtils {
42
42
  .map(([key, _property]) => ({
43
43
  key,
44
44
  type: schemaToAST(_property),
45
- meta: { description: _property.description },
45
+ meta: {
46
+ title: _property.title,
47
+ description: _property.description,
48
+ },
46
49
  })),
47
50
  });
48
51
  case 'array':
@@ -114,7 +117,18 @@ export namespace JsonSchemaUtils {
114
117
  type: 'object',
115
118
  properties: drilldown
116
119
  ? Object.fromEntries(
117
- typeAST.properties.map((property) => [property.key, astToSchema(property.type)!])
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
+ })
118
132
  )
119
133
  : {},
120
134
  };