Skip to content

Commit

Permalink
Merge pull request #45 from tiwater/revert-44-38-import-custom-agent
Browse files Browse the repository at this point in the history
Revert 44 38 import custom agent
  • Loading branch information
hughlv authored Jan 3, 2024
2 parents d90e7f4 + 1aa22ae commit 14b26fc
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 29 deletions.
26 changes: 16 additions & 10 deletions ui/src/app/[locale]/components/Flow.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import ReactFlow, {
Node,
Edge,
Background,
Controls,
applyNodeChanges,
applyEdgeChanges,
EdgeChange,
NodeChange,
SelectionMode,
useStoreApi,
useReactFlow,
addEdge,
ConnectionLineType,
} from 'reactflow';
import 'reactflow/dist/style.css';
import { nodeTypes, initialEdges, initialNodes } from '../utils/flow';
Expand All @@ -25,8 +31,8 @@ const Autoflow = ({ flowId }: any) => {
const { flow, updateFlow, isUpdating, isLoading, isError } = useFlow(flowId);

const [mode, setMode] = useState<'main' | 'flow' | 'json' | 'python'>('flow');
const [nodes, setNodes] = useState<any[]>(initialNodes);
const [edges, setEdges] = useState<any[]>(initialEdges);
const [nodes, setNodes] = useState<Node[]>(initialNodes);
const [edges, setEdges] = useState<Edge[]>(initialEdges);
const [isDirty, setIsDirty] = useState<boolean>(false);
const flowParent = useRef<HTMLDivElement>(null);
const { fitView, screenToFlowPosition, toObject } = useReactFlow();
Expand All @@ -37,7 +43,7 @@ const Autoflow = ({ flowId }: any) => {
// https://github.com/xyflow/xyflow/issues/3243
const store = useStoreApi();
if (process.env.NODE_ENV === 'development') {
store.getState().onError = (code: any, message: any) => {
store.getState().onError = (code, message) => {
if (code === '002') {
return;
}
Expand Down Expand Up @@ -78,7 +84,7 @@ const Autoflow = ({ flowId }: any) => {
}, [flow, nodes, edges, isDirty, flowId, toObject, debouncedUpdateFlow]);

const onNodesChange = useCallback(
(changes: any[]) => {
(changes: NodeChange[]) => {
if (
!initialLoad.current &&
changes.some(change => change.type !== 'select')
Expand All @@ -90,7 +96,7 @@ const Autoflow = ({ flowId }: any) => {
[setNodes]
);
const onEdgesChange = useCallback(
(changes: any[]) => {
(changes: EdgeChange[]) => {
if (
!initialLoad.current &&
changes.some(change => change.type !== 'select')
Expand Down Expand Up @@ -144,7 +150,7 @@ const Autoflow = ({ flowId }: any) => {
// Generate a unique node ID
const newId = genId();

const newNode = {
const newNode: Node = {
id: newId,
type: data.type,
position,
Expand All @@ -155,7 +161,7 @@ const Autoflow = ({ flowId }: any) => {
// Add the new node to the list of nodes in state
// And clean the previous selections
setNodes(nds =>
nds.map(nd => ({ ...nd, selected: false })).concat(newNode)
nds.map(nd => ({ ...nd, selected: false } as Node)).concat(newNode)
);
},
// Specify dependencies for useCallback
Expand All @@ -181,7 +187,7 @@ const Autoflow = ({ flowId }: any) => {
const randInt = (max: number) =>
Math.floor(Math.random() * Math.floor(max));

const newNode = {
const newNode: Node = {
id: newId,
type,
position: { x: 150 + randInt(100), y: 50 + randInt(50) },
Expand Down Expand Up @@ -231,13 +237,13 @@ const Autoflow = ({ flowId }: any) => {
onEdgesChange={onEdgesChange}
nodeTypes={nodeTypes}
onConnect={onConnect}
connectionLineType={'smoothstep'}
connectionLineType={ConnectionLineType.SmoothStep}
connectionLineStyle={{ strokeWidth: 2, stroke: 'lightgreen' }}
onDragOver={onDragOver}
onDrop={onDrop}
panOnScroll
selectionOnDrag
selectionMode={'partial'}
selectionMode={SelectionMode.Partial}
fitView
fitViewOptions={{ maxZoom: 1 }}
proOptions={{ hideAttribution: true }}
Expand Down
8 changes: 6 additions & 2 deletions ui/src/app/[locale]/components/node/Assistant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import clsx from 'clsx';
import React, { memo, useEffect, useState } from 'react';
import { FaEye, FaMeta } from 'react-icons/fa6';
import { GoGear } from 'react-icons/go';
import { Handle, useReactFlow } from 'reactflow';
import { Handle, Position, useReactFlow } from 'reactflow';
import { IconType } from 'react-icons';
import Toolbar from './Toolbar';
import { TbBrandOpenai } from 'react-icons/tb';
Expand Down Expand Up @@ -175,7 +175,11 @@ function AssistantNode({ id, data, selected }: any) {
)}
</div>

<Handle type="source" position={'right'} className="w-16 !bg-green-600" />
<Handle
type="source"
position={Position.Right}
className="w-16 !bg-green-600"
/>

<AssistantConfig
show={showOptions}
Expand Down
8 changes: 6 additions & 2 deletions ui/src/app/[locale]/components/node/CustomConversable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import EditButton from '@/components/EditButton';
import EditableText from '@/components/EditableText';
import clsx from 'clsx';
import { IoExtensionPuzzleSharp } from 'react-icons/io5';
import { Handle, useReactFlow } from 'reactflow';
import { Handle, Position, useReactFlow } from 'reactflow';
import { setNodeData } from '../../utils/flow';
import Toolbar from './Toolbar';
import { useState } from 'react';
Expand Down Expand Up @@ -75,7 +75,11 @@ const CustomConversable = ({ id, data, selected }: any) => {
</div>
</div>

<Handle type="source" position={'right'} className="w-16 !bg-green-600" />
<Handle
type="source"
position={Position.Right}
className="w-16 !bg-green-600"
/>
</div>
);
};
Expand Down
14 changes: 11 additions & 3 deletions ui/src/app/[locale]/components/node/GroupChat.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import clsx from 'clsx';
import { Handle, useReactFlow } from 'reactflow';
import { Handle, Position, useReactFlow } from 'reactflow';
import { getNodeLabel, setNodeData } from '../../utils/flow';
import { FaUserGroup } from 'react-icons/fa6';
import Toolbar from './Toolbar';
Expand Down Expand Up @@ -55,8 +55,16 @@ const GroupChatManager = ({ id, selected, data }: any) => {
/>
</div>
</div>
<Handle type="target" position={'left'} className="w-16 !bg-primary" />
<Handle type="source" position={'right'} className="w-16 !bg-primary" />
<Handle
type="target"
position={Position.Left}
className="w-16 !bg-primary"
/>
<Handle
type="source"
position={Position.Right}
className="w-16 !bg-primary"
/>
</div>
);
};
Expand Down
4 changes: 2 additions & 2 deletions ui/src/app/[locale]/components/node/Toolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import clsx from 'clsx';
import { useTranslations } from 'next-intl';
import { GoTrash } from 'react-icons/go';
import { NodeToolbar, useReactFlow } from 'reactflow';
import { Position, NodeToolbar, useReactFlow } from 'reactflow';

const Toolbar = ({
nodeId,
Expand All @@ -26,7 +26,7 @@ const Toolbar = ({
return (
<NodeToolbar
isVisible={selected}
position={'top'}
position={Position.Top}
align="end"
className={clsx(
className,
Expand Down
14 changes: 11 additions & 3 deletions ui/src/app/[locale]/components/node/UserProxy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import clsx from 'clsx';
import { RiChatSmile2Fill } from 'react-icons/ri';
import { Handle, useReactFlow } from 'reactflow';
import { Handle, Position, useReactFlow } from 'reactflow';
import { getNodeLabel, setNodeData } from '../../utils/flow';
import Toolbar from './Toolbar';
import { useState } from 'react';
Expand Down Expand Up @@ -126,8 +126,16 @@ const UserProxyAgent = ({ id, selected, data }: any) => {
</button>
</div>

<Handle type="target" position={'left'} className="w-16 !bg-primary" />
<Handle type="source" position={'right'} className="w-16 !bg-primary" />
<Handle
type="target"
position={Position.Left}
className="w-16 !bg-primary"
/>
<Handle
type="source"
position={Position.Right}
className="w-16 !bg-primary"
/>
<UserConfig
show={showOptions}
nodeId={id}
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/[locale]/gallery/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const FlowViewer = ({ template, className }: any) => {
// https://github.com/xyflow/xyflow/issues/3243
const store = useStoreApi();
if (process.env.NODE_ENV === 'development') {
store.getState().onError = (code: any, message: any) => {
store.getState().onError = (code, message) => {
if (code === '002') {
return;
}
Expand Down
11 changes: 6 additions & 5 deletions ui/src/app/[locale]/utils/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
RiUser4Line,
RiUserSearchLine,
} from 'react-icons/ri';
import { Edge, Node, ReactFlowInstance } from 'reactflow';
import { genId } from '@/utils/id';
import CustomConversable from '../components/node/CustomConversable';
import { ComponentType } from 'react';
Expand Down Expand Up @@ -145,7 +146,7 @@ export const getNodeLabel = (label: string, tNodeMeta: any) => {

// ---------------------

export const initialNodes: any[] = [
export const initialNodes: Node[] = [
{
id: '1',
type: 'assistant',
Expand Down Expand Up @@ -198,7 +199,7 @@ export const initialNodes: any[] = [
},
];

export const initialEdges: any[] = [
export const initialEdges: Edge[] = [
{
id: '1-3',
source: '1',
Expand All @@ -212,7 +213,7 @@ export const initialEdges: any[] = [
];

export const setNodeData = (
instance: any,
instance: ReactFlowInstance,
id: string,
dataset: { [key: string]: any },
scope: string = ''
Expand All @@ -233,7 +234,7 @@ export const setNodeData = (
);
};

export const getFlowName = (nodes: any[]) => {
export const getFlowName = (nodes: Node[]) => {
const configNode = nodes.find((node: any) => node.type === 'config');
let name = 'flow-unknown';
if (configNode && configNode?.data.flow_name) {
Expand All @@ -244,7 +245,7 @@ export const getFlowName = (nodes: any[]) => {
return name;
};

export const getFlowDescription = (nodes: any[]) => {
export const getFlowDescription = (nodes: Node[]) => {
const configNode = nodes.find((node: any) => node.type === 'config');
let description = '';
if (configNode && configNode?.data.flow_description) {
Expand Down
4 changes: 3 additions & 1 deletion ui/src/utils/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Edge, Node } from 'reactflow';

export enum AgentTypes {
user_proxy = 'UserProxyAgent',
assistant = 'AssistantAgent',
Expand Down Expand Up @@ -67,7 +69,7 @@ const importDict: Record<string, string> = {
};

export const genEntry = (
data: { nodes: any[]; edges: any[] },
data: { nodes: Node[]; edges: Edge[] },
message: string
) => {
const { nodes, edges } = data;
Expand Down

0 comments on commit 14b26fc

Please sign in to comment.