Kidizzz commited on
Commit
a87cdef
·
verified ·
1 Parent(s): 6d03ba4

Upload pages/index.js with huggingface_hub

Browse files
Files changed (1) hide show
  1. pages/index.js +202 -0
pages/index.js ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useState, useCallback, useRef, useEffect } from 'react'
2
+ import Header from '../components/Header'
3
+ import NodeCanvas from '../components/NodeCanvas'
4
+ import NodeEditor from '../components/NodeEditor'
5
+ import ChatPanel from '../components/ChatPanel'
6
+ import ConnectionModal from '../components/ConnectionModal'
7
+
8
+ const initialNodes = [
9
+ {
10
+ id: 'ceo-1',
11
+ type: 'ceo',
12
+ name: 'CEO Agent',
13
+ model: 'claude-3-opus',
14
+ apiKey: '',
15
+ brief: 'You are the CEO agent. Your role is to coordinate communication between all agents, delegate tasks, and ensure efficient workflow. Summarize and relay information between agents.',
16
+ position: { x: 400, y: 150 },
17
+ connections: [],
18
+ },
19
+ ]
20
+
21
+ const modelOptions = {
22
+ claude: ['claude-3-opus', 'claude-3-sonnet', 'claude-3-haiku'],
23
+ gemini: ['gemini-pro', 'gemini-pro-vision'],
24
+ openai: ['gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo'],
25
+ llama: ['llama-2-70b', 'llama-2-13b', 'llama-2-7b'],
26
+ mistral: ['mistral-large', 'mistral-medium', 'mistral-small'],
27
+ }
28
+
29
+ export default function Home() {
30
+ const [nodes, setNodes] = useState(initialNodes)
31
+ const [connections, setConnections] = useState([])
32
+ const [selectedNode, setSelectedNode] = useState(null)
33
+ const [editingNode, setEditingNode] = useState(null)
34
+ const [showChat, setShowChat] = useState(false)
35
+ const [chatMessages, setChatMessages] = useState([])
36
+ const [connectionModal, setConnectionModal] = useState(null)
37
+ const [draggedNode, setDraggedNode] = useState(null)
38
+ const [dragOffset, setDragOffset] = useState({ x: 0, y: 0 })
39
+
40
+ const handleAddNode = useCallback(() => {
41
+ const newNode = {
42
+ id: `node-${Date.now()}`,
43
+ type: 'agent',
44
+ name: `Agent ${nodes.length}`,
45
+ model: 'gpt-4-turbo',
46
+ apiKey: '',
47
+ brief: 'You are a helpful AI agent. Follow instructions from the CEO and collaborate with other agents.',
48
+ position: { x: 100 + Math.random() * 300, y: 100 + Math.random() * 300 },
49
+ connections: [],
50
+ }
51
+ setNodes(prev => [...prev, newNode])
52
+ }, [nodes.length])
53
+
54
+ const handleDeleteNode = useCallback((nodeId) => {
55
+ setNodes(prev => prev.filter(n => n.id !== nodeId))
56
+ setConnections(prev => prev.filter(c => c.from !== nodeId && c.to !== nodeId))
57
+ if (selectedNode?.id === nodeId) setSelectedNode(null)
58
+ if (editingNode?.id === nodeId) setEditingNode(null)
59
+ }, [selectedNode, editingNode])
60
+
61
+ const handleUpdateNode = useCallback((nodeId, updates) => {
62
+ setNodes(prev => prev.map(n => n.id === nodeId ? { ...n, ...updates } : n))
63
+ }, [])
64
+
65
+ const handleAddConnection = useCallback((fromId, toId) => {
66
+ if (fromId === toId) return
67
+ const exists = connections.some(c => c.from === fromId && c.to === toId)
68
+ if (!exists) {
69
+ setConnections(prev => [...prev, { id: `conn-${Date.now()}`, from: fromId, to: toId, active: false }])
70
+ }
71
+ }, [connections])
72
+
73
+ const handleDeleteConnection = useCallback((connectionId) => {
74
+ setConnections(prev => prev.filter(c => c.id !== connectionId))
75
+ }, [])
76
+
77
+ const handleNodeDragStart = useCallback((e, node) => {
78
+ const rect = e.currentTarget.getBoundingClientRect()
79
+ setDragOffset({
80
+ x: e.clientX - rect.left,
81
+ y: e.clientY - rect.top
82
+ })
83
+ setDraggedNode(node)
84
+ }, [])
85
+
86
+ const handleNodeDrag = useCallback((e) => {
87
+ if (!draggedNode) return
88
+ const canvas = document.getElementById('node-canvas')
89
+ const canvasRect = canvas.getBoundingClientRect()
90
+ const newX = e.clientX - canvasRect.left - dragOffset.x
91
+ const newY = e.clientY - canvasRect.top - dragOffset.y
92
+
93
+ handleUpdateNode(draggedNode.id, {
94
+ position: { x: Math.max(0, newX), y: Math.max(0, newY) }
95
+ })
96
+ }, [draggedNode, dragOffset, handleUpdateNode])
97
+
98
+ const handleNodeDragEnd = useCallback(() => {
99
+ setDraggedNode(null)
100
+ }, [])
101
+
102
+ useEffect(() => {
103
+ if (draggedNode) {
104
+ window.addEventListener('mousemove', handleNodeDrag)
105
+ window.addEventListener('mouseup', handleNodeDragEnd)
106
+ return () => {
107
+ window.removeEventListener('mousemove', handleNodeDrag)
108
+ window.removeEventListener('mouseup', handleNodeDragEnd)
109
+ }
110
+ }
111
+ }, [draggedNode, handleNodeDrag, handleNodeDragEnd])
112
+
113
+ const handleSendMessage = useCallback(async (message, targetNodeId) => {
114
+ const newMessage = {
115
+ id: `msg-${Date.now()}`,
116
+ sender: 'user',
117
+ content: message,
118
+ targetNode: targetNodeId,
119
+ timestamp: new Date().toISOString(),
120
+ }
121
+ setChatMessages(prev => [...prev, newMessage])
122
+
123
+ // Simulate agent response
124
+ setTimeout(() => {
125
+ const targetNode = nodes.find(n => n.id === targetNodeId)
126
+ const responseMessage = {
127
+ id: `msg-${Date.now()}-response`,
128
+ sender: targetNodeId,
129
+ senderName: targetNode?.name || 'Agent',
130
+ content: `[${targetNode?.name || 'Agent'}] Received your message: "${message}". Processing based on my brief: "${targetNode?.brief?.substring(0, 100)}..."`,
131
+ timestamp: new Date().toISOString(),
132
+ }
133
+ setChatMessages(prev => [...prev, responseMessage])
134
+ }, 1000)
135
+ }, [nodes])
136
+
137
+ return (
138
+ <div className="min-h-screen flex flex-col">
139
+ <Header
140
+ onAddNode={handleAddNode}
141
+ onToggleChat={() => setShowChat(!showChat)}
142
+ showChat={showChat}
143
+ nodeCount={nodes.length}
144
+ connectionCount={connections.length}
145
+ />
146
+
147
+ <div className="flex-1 flex overflow-hidden">
148
+ <div className="flex-1 relative">
149
+ <NodeCanvas
150
+ nodes={nodes}
151
+ connections={connections}
152
+ selectedNode={selectedNode}
153
+ onSelectNode={setSelectedNode}
154
+ onEditNode={setEditingNode}
155
+ onDeleteNode={handleDeleteNode}
156
+ onAddConnection={(fromId) => setConnectionModal({ from: fromId })}
157
+ onNodeDragStart={handleNodeDragStart}
158
+ />
159
+ </div>
160
+
161
+ {showChat && (
162
+ <ChatPanel
163
+ nodes={nodes}
164
+ messages={chatMessages}
165
+ onSendMessage={handleSendMessage}
166
+ onClose={() => setShowChat(false)}
167
+ />
168
+ )}
169
+ </div>
170
+
171
+ {editingNode && (
172
+ <NodeEditor
173
+ node={editingNode}
174
+ modelOptions={modelOptions}
175
+ onSave={(updates) => {
176
+ handleUpdateNode(editingNode.id, updates)
177
+ setEditingNode(null)
178
+
179
+ onClose={() => setEditingNode(null)}
180
+ onDelete={() => {
181
+ handleDeleteNode(editingNode.id)
182
+ setEditingNode(null)
183
+
184
+ />
185
+ )}
186
+
187
+ {connectionModal && (
188
+ <ConnectionModal
189
+ fromNode={nodes.find(n => n.id === connectionModal.from)}
190
+ nodes={nodes.filter(n => n.id !== connectionModal.from)}
191
+ existingConnections={connections.filter(c => c.from === connectionModal.from)}
192
+ onConnect={(toId) => {
193
+ handleAddConnection(connectionModal.from, toId)
194
+ setConnectionModal(null)
195
+
196
+ onDisconnect={handleDeleteConnection}
197
+ onClose={() => setConnectionModal(null)}
198
+ />
199
+ )}
200
+ </div>
201
+ )
202
+ }