Skip to content

Commit

Permalink
feat: create blank canvas infiniflow#918 (infiniflow#1356)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

feat: create blank canvas infiniflow#918

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
  • Loading branch information
cike8899 authored Jul 3, 2024
1 parent 845c9a0 commit 2a0722f
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 6 deletions.
File renamed without changes.
24 changes: 24 additions & 0 deletions web/src/assets/svg/nothing.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions web/src/hooks/flow-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,59 @@ import flowService from '@/services/flow-service';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { message } from 'antd';
import { useParams } from 'umi';
import { v4 as uuid } from 'uuid';

export const EmptyDsl = {
graph: {
nodes: [
{
id: 'Begin',
type: 'beginNode',
position: {
x: 50,
y: 200,
},
data: {
label: 'Begin',
name: 'begin',
},
sourcePosition: 'left',
targetPosition: 'right',
},
],
edges: [],
},
components: {
begin: {
obj: {
component_name: 'Begin',
params: {},
},
downstream: ['Answer:China'], // other edge target is downstream, edge source is current node id
upstream: [], // edge source is upstream, edge target is current node id
},
},
messages: [],
reference: [],
history: [],
path: [],
answer: [],
};

export const useFetchFlowTemplates = (): ResponseType<IFlowTemplate[]> => {
const { data } = useQuery({
queryKey: ['fetchFlowTemplates'],
initialData: [],
queryFn: async () => {
const { data } = await flowService.listTemplates();
if (Array.isArray(data?.data)) {
data.data.unshift({
id: uuid(),
title: 'Blank',
description: 'Create from nothing',
dsl: EmptyDsl,
});
}

return data;
},
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/flow/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export const useValidateConnection = () => {
// restricted lines cannot be connected successfully.
const isValidConnection = useCallback(
(connection: Connection) => {
// limit there to be only one line between two nodes
// limit the connection between two nodes to only one connection line in one direction
const hasLine = edges.some(
(x) => x.source === connection.source && x.target === connection.target,
);
Expand Down
75 changes: 75 additions & 0 deletions web/src/pages/flow/interpreter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"edges": [
{
"id": "c87c7805-8cf0-4cd4-b45b-152031811020",
"label": "",
"source": "begin",
"target": "answer:0"
},
{
"id": "e30320bb-601b-4885-acb3-79becdc49f08",
"label": "",
"source": "generate:0",
"target": "answer:0"
},
{
"id": "83927e42-739a-402a-9f75-a88d4fab37ed",
"label": "",
"source": "answer:0",
"target": "generate:0"
}
],
"nodes": [
{
"id": "begin",
"type": "beginNode",
"position": {
"x": 0,
"y": 0
},
"data": {
"label": "Begin",
"name": "FruityPianosSend",
"form": {
"prologue": "Hi there! Please enter the text you want to translate in format like: 'text you want to translate' => target language. For an example: 您好! => English"
}
},
"sourcePosition": "left",
"targetPosition": "right"
},
{
"id": "answer:0",
"type": "ragNode",
"position": {
"x": 0,
"y": 0
},
"data": {
"label": "Answer",
"name": "YummyBoatsFlow",
"form": {}
},
"sourcePosition": "left",
"targetPosition": "right"
},
{
"id": "generate:0",
"type": "ragNode",
"position": {
"x": 0,
"y": 0
},
"data": {
"label": "Generate",
"name": "SwiftTramsDrop",
"form": {
"llm_id": "deepseek-chat",
"prompt": "You are an professional interpreter.\n- Role: an professional interpreter.\n- Input format: content need to be translated => target language. \n- Answer format: => translated content in target language. \n- Examples:\n - user: 您好! => English. assistant: => How are you doing!\n - user: You look good today. => Japanese. assistant: => 今日は調子がいいですね 。\n",
"temperature": 0.5
}
},
"sourcePosition": "left",
"targetPosition": "right"
}
]
}
10 changes: 7 additions & 3 deletions web/src/pages/flow/list/create-flow-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ReactComponent as NothingIcon } from '@/assets/svg/nothing.svg';
import { IModalManagerChildrenProps } from '@/components/modal-manager';
import { useTranslate } from '@/hooks/commonHooks';
import { useFetchFlowTemplates } from '@/hooks/flow-hooks';
import { useSelectItem } from '@/hooks/logicHooks';
import { UserOutlined } from '@ant-design/icons';
import {
Avatar,
Card,
Expand Down Expand Up @@ -79,7 +79,7 @@ const CreateFlowModal = ({
<Input />
</Form.Item>
</Form>
<Title level={5}>Choose from templates</Title>
<Title level={5}>Create from templates</Title>
<Flex vertical gap={16}>
{list?.map((x) => (
<Card
Expand All @@ -90,7 +90,11 @@ const CreateFlowModal = ({
onClick={handleItemClick(x.id)}
>
<Space size={'middle'}>
<Avatar size={40} icon={<UserOutlined />} src={x.avatar} />
{x.avatar ? (
<Avatar size={40} icon={<NothingIcon />} src={x.avatar} />
) : (
<NothingIcon width={40} height={30} />
)}
<b>{x.title}</b>
</Space>
<p>{x.description}</p>
Expand Down
4 changes: 3 additions & 1 deletion web/src/pages/flow/list/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { useNavigate } from 'umi';
// import dslJson from '../../../../../dls.json';
// import customerServiceBase from '../../../../../graph/test/dsl_examples/customer_service.json';
// import customerService from '../customer_service.json';
// import interpreterBase from '../../../../../graph/test/dsl_examples/interpreter.json';
// import interpreter from '../interpreter.json';

export const useFetchDataOnMount = () => {
const { data, loading } = useFetchFlowList();
Expand Down Expand Up @@ -41,7 +43,7 @@ export const useSaveFlow = () => {
title,
dsl,
// dsl: dslJson,
// dsl: { ...customerServiceBase, graph: customerService },
// dsl: { ...interpreterBase, graph: interpreter },
});

if (ret?.retcode === 0) {
Expand Down
3 changes: 2 additions & 1 deletion web/src/pages/flow/message-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const MessageForm = ({ onValuesChange, form }: IOperatorForm) => {
]}
noStyle
>
<Input
<Input.TextArea
rows={4}
placeholder={t('messagePlaceholder')}
style={{ width: '80%' }}
/>
Expand Down
19 changes: 19 additions & 0 deletions web/src/pages/flow/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import path from 'path';
import customer_service from '../../../../graph/test/dsl_examples/customer_service.json';
import headhunter_zh from '../../../../graph/test/dsl_examples/headhunter_zh.json';
import interpreter from '../../../../graph/test/dsl_examples/interpreter.json';
import { dsl } from './mock';
import { buildNodesAndEdgesFromDSLComponents } from './utils';

Expand Down Expand Up @@ -68,3 +69,21 @@ test('build nodes and edges from customer_service dsl', () => {
}
expect(nodes.length).toEqual(12);
});

test('build nodes and edges from interpreter dsl', () => {
const { edges, nodes } = buildNodesAndEdgesFromDSLComponents(
interpreter.components,
);
console.info('node length', nodes.length);
console.info('edge length', edges.length);
try {
fs.writeFileSync(
path.join(__dirname, 'interpreter.json'),
JSON.stringify({ edges, nodes }, null, 4),
);
console.log('JSON data is saved.');
} catch (error) {
console.warn(error);
}
expect(nodes.length).toEqual(12);
});

0 comments on commit 2a0722f

Please sign in to comment.