@flowgram.ai/form-materials 0.2.17 → 0.2.19

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.19",
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.19"
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/eslint-config": "0.2.19",
52
+ "@flowgram.ai/ts-config": "0.2.19"
51
53
  },
52
54
  "peerDependencies": {
53
55
  "react": ">=16.8",
@@ -65,7 +65,12 @@ export function ConditionRow({ style, value, onChange, readonly }: PropTypes) {
65
65
  onChange={(v) => onChange({ ...value, right: v })}
66
66
  />
67
67
  ) : (
68
- <Input size="small" disabled value={opConfig?.rightDisplay || 'Empty'} />
68
+ <Input
69
+ size="small"
70
+ disabled
71
+ style={{ pointerEvents: 'none' }}
72
+ value={opConfig?.rightDisplay || 'Empty'}
73
+ />
69
74
  )}
70
75
  </UIRight>
71
76
  </UIValues>
@@ -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,12 @@
1
+ {
2
+ "name": "prompt-editor",
3
+ "depMaterials": [
4
+ "variable-selector"
5
+ ],
6
+ "depPackages": [
7
+ "@coze-editor/editor@0.1.0-alpha.8d7a30",
8
+ "@codemirror/view",
9
+ "styled-components",
10
+ "@douyinfe/semi-ui"
11
+ ]
12
+ }
@@ -0,0 +1,173 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React, { useLayoutEffect } from 'react';
7
+
8
+ import { createRoot, Root } from 'react-dom/client';
9
+ import { isEqual, last } from 'lodash';
10
+ import {
11
+ BaseVariableField,
12
+ Disposable,
13
+ DisposableCollection,
14
+ Scope,
15
+ useCurrentScope,
16
+ } from '@flowgram.ai/editor';
17
+ import { Popover } from '@douyinfe/semi-ui';
18
+ import { IconIssueStroked } from '@douyinfe/semi-icons';
19
+ import { useInjector } from '@coze-editor/editor/react';
20
+ import {
21
+ Decoration,
22
+ DecorationSet,
23
+ EditorView,
24
+ MatchDecorator,
25
+ ViewPlugin,
26
+ WidgetType,
27
+ } from '@codemirror/view';
28
+
29
+ import { UIPopoverContent, UIRootTitle, UITag, UIVarName } from '../styles';
30
+
31
+ class VariableTagWidget extends WidgetType {
32
+ keyPath?: string[];
33
+
34
+ toDispose = new DisposableCollection();
35
+
36
+ scope: Scope;
37
+
38
+ root: Root;
39
+
40
+ constructor({ keyPath, scope }: { keyPath?: string[]; scope: Scope }) {
41
+ super();
42
+
43
+ this.keyPath = keyPath;
44
+ this.scope = scope;
45
+ }
46
+
47
+ renderIcon = (icon: string | JSX.Element) => {
48
+ if (typeof icon === 'string') {
49
+ return <img style={{ marginRight: 8 }} width={12} height={12} src={icon} />;
50
+ }
51
+
52
+ return icon;
53
+ };
54
+
55
+ renderVariable(v?: BaseVariableField) {
56
+ if (!v) {
57
+ this.root.render(
58
+ <UITag prefixIcon={<IconIssueStroked />} color="amber">
59
+ Unknown
60
+ </UITag>
61
+ );
62
+ return;
63
+ }
64
+
65
+ const rootField = last(v.parentFields);
66
+
67
+ const rootTitle = (
68
+ <UIRootTitle>{rootField?.meta.title ? `${rootField.meta.title} -` : ''}</UIRootTitle>
69
+ );
70
+ const rootIcon = this.renderIcon(rootField?.meta.icon);
71
+
72
+ this.root.render(
73
+ <Popover
74
+ content={
75
+ <UIPopoverContent>
76
+ {rootIcon}
77
+ {rootTitle}
78
+ <UIVarName>{v?.keyPath.slice(1).join('.')}</UIVarName>
79
+ </UIPopoverContent>
80
+ }
81
+ >
82
+ <UITag prefixIcon={rootIcon}>
83
+ {rootTitle}
84
+ <UIVarName>{v?.key}</UIVarName>
85
+ </UITag>
86
+ </Popover>
87
+ );
88
+ }
89
+
90
+ toDOM(view: EditorView): HTMLElement {
91
+ const dom = document.createElement('span');
92
+
93
+ this.root = createRoot(dom);
94
+
95
+ this.toDispose.push(
96
+ Disposable.create(() => {
97
+ this.root.unmount();
98
+ })
99
+ );
100
+
101
+ this.toDispose.push(
102
+ this.scope.available.trackByKeyPath(
103
+ this.keyPath,
104
+ (v) => {
105
+ this.renderVariable(v);
106
+ },
107
+ { triggerOnInit: false }
108
+ )
109
+ );
110
+
111
+ this.renderVariable(this.scope.available.getByKeyPath(this.keyPath));
112
+
113
+ return dom;
114
+ }
115
+
116
+ eq(other: VariableTagWidget) {
117
+ return isEqual(this.keyPath, other.keyPath);
118
+ }
119
+
120
+ ignoreEvent(): boolean {
121
+ return false;
122
+ }
123
+
124
+ destroy(dom: HTMLElement): void {
125
+ this.toDispose.dispose();
126
+ }
127
+ }
128
+
129
+ export function VariableTagInject() {
130
+ const injector = useInjector();
131
+
132
+ const scope = useCurrentScope();
133
+
134
+ // 基于 {{var}} 的正则进行匹配,匹配后进行自定义渲染
135
+ useLayoutEffect(() => {
136
+ const atMatcher = new MatchDecorator({
137
+ regexp: /\{\{([^\}]+)\}\}/g,
138
+ decoration: (match) =>
139
+ Decoration.replace({
140
+ widget: new VariableTagWidget({
141
+ keyPath: match[1]?.split('.') ?? [],
142
+ scope,
143
+ }),
144
+ }),
145
+ });
146
+
147
+ return injector.inject([
148
+ ViewPlugin.fromClass(
149
+ class {
150
+ decorations: DecorationSet;
151
+
152
+ constructor(private view: EditorView) {
153
+ this.decorations = atMatcher.createDeco(view);
154
+ }
155
+
156
+ update() {
157
+ this.decorations = atMatcher.createDeco(this.view);
158
+ }
159
+ },
160
+ {
161
+ decorations: (p) => p.decorations,
162
+ provide(p) {
163
+ return EditorView.atomicRanges.of(
164
+ (view) => view.plugin(p)?.decorations ?? Decoration.none
165
+ );
166
+ },
167
+ }
168
+ ),
169
+ ]);
170
+ }, [injector]);
171
+
172
+ return null;
173
+ }
@@ -0,0 +1,83 @@
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
+ export function VariableTree() {
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
+ }
@@ -0,0 +1,19 @@
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 { VariableTree } from './extensions/variable-tree';
9
+ import { VariableTagInject } from './extensions/variable-tag';
10
+ import { PromptEditor, PromptEditorPropsType } from '../prompt-editor';
11
+
12
+ export function PromptEditorWithVariables(props: PromptEditorPropsType) {
13
+ return (
14
+ <PromptEditor {...props}>
15
+ <VariableTree />
16
+ <VariableTagInject />
17
+ </PromptEditor>
18
+ );
19
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import styled from 'styled-components';
7
+ import { Tag } from '@douyinfe/semi-ui';
8
+
9
+ export const UIRootTitle = styled.div`
10
+ margin-right: 4px;
11
+ min-width: 20px;
12
+ overflow: hidden;
13
+ text-overflow: ellipsis;
14
+ white-space: nowrap;
15
+ color: var(--semi-color-text-2);
16
+ `;
17
+
18
+ export const UIVarName = styled.div`
19
+ overflow: hidden;
20
+ text-overflow: ellipsis;
21
+ white-space: nowrap;
22
+ `;
23
+
24
+ export const UITag = styled(Tag)`
25
+ display: inline-flex;
26
+ align-items: center;
27
+ justify-content: flex-start;
28
+ max-width: 300px;
29
+
30
+ & .semi-tag-content-center {
31
+ justify-content: flex-start;
32
+ }
33
+
34
+ &.semi-tag {
35
+ margin: 0 5px;
36
+ }
37
+ `;
38
+
39
+ export const UIPopoverContent = styled.div`
40
+ padding: 10px;
41
+ display: inline-flex;
42
+ align-items: center;
43
+ justify-content: flex-start;
44
+ `;
@@ -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;