mprattoy commited on
Commit
560812a
·
1 Parent(s): df2b1f9

DB updated

Browse files
Files changed (2) hide show
  1. database.py +24 -12
  2. gradio_app.py +76 -26
database.py CHANGED
@@ -165,11 +165,10 @@ def generate_chatbot_data(user_id, workspace_id, chatbot_id, chatbot_name, model
165
  },
166
  'mcp': mcp_config,
167
  'systemPrompt': system_prompt,
168
- 'label': '',
169
- 'iconPath': '',
170
  'errorText': '',
 
171
  'validationRegex': '',
172
- 'switchButtonEntities': [
173
  {
174
  'title': 'Back Button',
175
  'value': True,
@@ -181,7 +180,7 @@ def generate_chatbot_data(user_id, workspace_id, chatbot_id, chatbot_name, model
181
  'isDisabled': False
182
  }
183
  ],
184
- 'optionEntities': []
185
  }
186
 
187
  return {
@@ -194,12 +193,25 @@ def generate_chatbot_data(user_id, workspace_id, chatbot_id, chatbot_name, model
194
  'value': 'asset/image/female_profile.png'
195
  },
196
  'branding': True,
 
 
197
  'position': 'bottomRight',
198
  'primaryColor': 4287203839,
199
  'themeColor': 4294967295,
200
  'welcomeMessage': True
201
  },
202
  'id': chatbot_id,
 
 
 
 
 
 
 
 
 
 
 
203
  'shareScript': generate_chatbot_share_script(user_id, workspace_id, chatbot_id)
204
  }
205
 
@@ -269,17 +281,17 @@ def create_agent(user_id, agent_name, model_name, mcp_github_enabled, github_tok
269
  # Path: g_users/{user_id}/workspaces/{DEFAULT_WORKSPACE_ID}/chatbots/{chatbot_id}
270
  chatbot_data['timestamp'] = firestore.SERVER_TIMESTAMP
271
  db.collection('g_users').document(user_id).collection('workspaces').document(
272
- DEFAULT_WORKSPACE_ID).collection('chatbots').document(chatbot_id).set(chatbot_data)
273
  else:
274
  mock_agents[agent_id] = agent_data
275
 
276
  # Store chatbot in mock workspace
277
  for user_email, user_data in mock_users.items():
278
  if user_data['user_id'] == user_id:
279
- if 'chatbots' not in user_data['workspaces'][DEFAULT_WORKSPACE_ID]:
280
- user_data['workspaces'][DEFAULT_WORKSPACE_ID]['chatbots'] = {}
281
  chatbot_data['timestamp'] = datetime.now()
282
- user_data['workspaces'][DEFAULT_WORKSPACE_ID]['chatbots'][chatbot_id] = chatbot_data
283
  break
284
 
285
  success_msg = f"✅ Agent '{agent_name}' created successfully!"
@@ -302,13 +314,13 @@ def get_user_agents(user_id):
302
  # Fetch chatbots from the workspace subcollection
303
  # Path: g_users/{user_id}/workspaces/{DEFAULT_WORKSPACE_ID}/chatbots
304
  chatbots = db.collection('g_users').document(user_id).collection('workspaces').document(
305
- DEFAULT_WORKSPACE_ID).collection('chatbots').get()
306
  agent_list = []
307
  for chatbot in chatbots:
308
  data = chatbot.to_dict()
309
  chatbot_id = data.get('id', chatbot.id)
310
  chatbot_name = data.get('chatbotName', 'Unnamed Agent')
311
- agent_url = f"https://curious-learner-chatbot.web.app/#/chats?clientId={user_id}&workspaceId={DEFAULT_WORKSPACE_ID}&chatbotId={chatbot_id}"
312
  agent_list.append(f"• {chatbot_name}\n {agent_url}")
313
  return agent_list
314
  else:
@@ -317,10 +329,10 @@ def get_user_agents(user_id):
317
  if user_data['user_id'] == user_id:
318
  workspaces = user_data.get('workspaces', {})
319
  default_workspace = workspaces.get(DEFAULT_WORKSPACE_ID, {})
320
- chatbots = default_workspace.get('chatbots', {})
321
  for chatbot_id, data in chatbots.items():
322
  chatbot_name = data.get('chatbotName', 'Unnamed Agent')
323
- agent_url = f"https://curious-learner-chatbot.web.app/#/chats?clientId={user_id}&workspaceId={DEFAULT_WORKSPACE_ID}&chatbotId={chatbot_id}"
324
  agent_list.append(f"• {chatbot_name}\n {agent_url}")
325
  break
326
  return agent_list
 
165
  },
166
  'mcp': mcp_config,
167
  'systemPrompt': system_prompt,
 
 
168
  'errorText': '',
169
+ 'replayType': 'multiple',
170
  'validationRegex': '',
171
+ 'switchButtonModels': [
172
  {
173
  'title': 'Back Button',
174
  'value': True,
 
180
  'isDisabled': False
181
  }
182
  ],
183
+ 'optionModels': []
184
  }
185
 
186
  return {
 
193
  'value': 'asset/image/female_profile.png'
194
  },
195
  'branding': True,
196
+ 'dynamicTheme': '{}',
197
+ 'isDynamicTheme': False,
198
  'position': 'bottomRight',
199
  'primaryColor': 4287203839,
200
  'themeColor': 4294967295,
201
  'welcomeMessage': True
202
  },
203
  'id': chatbot_id,
204
+ "notificationModel": {
205
+ "sendTo": [
206
+ "afmjoaa@gmail.com",
207
+ "mprattoy@gmail.com"
208
+ ],
209
+ "subjectHeader": "New Response from Curious Learner",
210
+ "sendIncompleteResponse": True,
211
+ "leadInfo": "notificationCount",
212
+ "emailNotification": True
213
+ },
214
+ 'responseCount': 0,
215
  'shareScript': generate_chatbot_share_script(user_id, workspace_id, chatbot_id)
216
  }
217
 
 
281
  # Path: g_users/{user_id}/workspaces/{DEFAULT_WORKSPACE_ID}/chatbots/{chatbot_id}
282
  chatbot_data['timestamp'] = firestore.SERVER_TIMESTAMP
283
  db.collection('g_users').document(user_id).collection('workspaces').document(
284
+ DEFAULT_WORKSPACE_ID).collection('chats').document(chatbot_id).set(chatbot_data)
285
  else:
286
  mock_agents[agent_id] = agent_data
287
 
288
  # Store chatbot in mock workspace
289
  for user_email, user_data in mock_users.items():
290
  if user_data['user_id'] == user_id:
291
+ if 'chats' not in user_data['workspaces'][DEFAULT_WORKSPACE_ID]:
292
+ user_data['workspaces'][DEFAULT_WORKSPACE_ID]['chats'] = {}
293
  chatbot_data['timestamp'] = datetime.now()
294
+ user_data['workspaces'][DEFAULT_WORKSPACE_ID]['chats'][chatbot_id] = chatbot_data
295
  break
296
 
297
  success_msg = f"✅ Agent '{agent_name}' created successfully!"
 
314
  # Fetch chatbots from the workspace subcollection
315
  # Path: g_users/{user_id}/workspaces/{DEFAULT_WORKSPACE_ID}/chatbots
316
  chatbots = db.collection('g_users').document(user_id).collection('workspaces').document(
317
+ DEFAULT_WORKSPACE_ID).collection('chats').get()
318
  agent_list = []
319
  for chatbot in chatbots:
320
  data = chatbot.to_dict()
321
  chatbot_id = data.get('id', chatbot.id)
322
  chatbot_name = data.get('chatbotName', 'Unnamed Agent')
323
+ agent_url = f"https://curious-learner-chatbot.web.app/#/individual_chat_screen?clientId={user_id}&workspaceId={DEFAULT_WORKSPACE_ID}&chatbotId={chatbot_id}"
324
  agent_list.append(f"• {chatbot_name}\n {agent_url}")
325
  return agent_list
326
  else:
 
329
  if user_data['user_id'] == user_id:
330
  workspaces = user_data.get('workspaces', {})
331
  default_workspace = workspaces.get(DEFAULT_WORKSPACE_ID, {})
332
+ chatbots = default_workspace.get('chats', {})
333
  for chatbot_id, data in chatbots.items():
334
  chatbot_name = data.get('chatbotName', 'Unnamed Agent')
335
+ agent_url = f"https://curious-learner-chatbot.web.app/#/individual_chat_screen?clientId={user_id}&workspaceId={DEFAULT_WORKSPACE_ID}&chatbotId={chatbot_id}"
336
  agent_list.append(f"• {chatbot_name}\n {agent_url}")
337
  break
338
  return agent_list
gradio_app.py CHANGED
@@ -29,7 +29,7 @@ def get_chatbot_iframe(user_id):
29
  return f'''
30
  <iframe
31
  src="{chatbot_url}"
32
- style="width: 100%; height: 100%; border: none;"
33
  allow="clipboard-write; clipboard-read"
34
  ></iframe>
35
  '''
@@ -93,13 +93,17 @@ def get_username(session_id):
93
 
94
  def create_agent(session_id, agent_name, model_name, mcp_github_enabled, github_token, system_prompt):
95
  """Create a new React agent"""
 
96
  if not session_id or session_id not in user_sessions:
 
97
  return "Please login first!", None
98
 
99
  user_id = user_sessions[session_id]
 
100
  success, message, chatbot_id = db_create_agent(
101
  user_id, agent_name, model_name, mcp_github_enabled, github_token, system_prompt
102
  )
 
103
 
104
  return message, chatbot_id if success else None
105
 
@@ -232,8 +236,8 @@ custom_css = """
232
  position: fixed !important;
233
  bottom: 20px !important;
234
  right: 20px !important;
235
- width: 380px !important;
236
- height: 550px !important;
237
  border: none !important;
238
  border-radius: 16px !important;
239
  overflow: hidden !important;
@@ -244,7 +248,25 @@ custom_css = """
244
 
245
  #chatbot-webview iframe {
246
  width: 100% !important;
247
- height: 100% !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  border: none !important;
249
  }
250
 
@@ -317,12 +339,6 @@ with gr.Blocks(title="React Agent Builder") as app:
317
  # Logout button at the bottom of dashboard, inside the container
318
  logout_btn_dashboard = gr.Button("🚪 Logout", size="sm", variant="secondary", elem_classes="logout-btn")
319
 
320
- # Floating chatbot webview - outside the main container
321
- chatbot_webview = gr.HTML(
322
- value="",
323
- elem_id="chatbot-webview"
324
- )
325
-
326
  with gr.Tab("Create Agent", id="create_agent", visible=False) as create_agent_tab:
327
  with gr.Column(elem_classes="form-container"):
328
  # Back button at the top
@@ -378,9 +394,20 @@ with gr.Blocks(title="React Agent Builder") as app:
378
  create_msg = gr.Textbox(label="Status", interactive=False, lines=2, scale=1)
379
  agent_url_output = gr.Textbox(label="Agent URL", interactive=False, lines=3, scale=1)
380
 
 
 
 
 
381
  # Logout button at the bottom of create agent form
382
  logout_btn_create = gr.Button("🚪 Logout", size="sm", variant="secondary", elem_classes="logout-btn")
383
 
 
 
 
 
 
 
 
384
 
385
  # Event handlers
386
  def handle_login(email, password):
@@ -408,7 +435,7 @@ with gr.Blocks(title="React Agent Builder") as app:
408
  session_state: sess_id,
409
  user_id_state: user_id,
410
  agents_list: agents_text,
411
- chatbot_webview: chatbot_html,
412
  tabs: gr.update(selected="dashboard")
413
  }
414
  else:
@@ -439,7 +466,7 @@ with gr.Blocks(title="React Agent Builder") as app:
439
  login_email: "",
440
  login_password: "",
441
  login_msg: "",
442
- chatbot_webview: "",
443
  tabs: gr.update(selected="login")
444
  }
445
 
@@ -448,11 +475,13 @@ with gr.Blocks(title="React Agent Builder") as app:
448
  return {
449
  dashboard_tab: gr.update(visible=False),
450
  create_agent_tab: gr.update(visible=True),
 
451
  tabs: gr.update(selected="create_agent")
452
  }
453
 
454
 
455
- def switch_back_to_dashboard():
 
456
  return {
457
  dashboard_tab: gr.update(visible=True),
458
  create_agent_tab: gr.update(visible=False),
@@ -462,6 +491,8 @@ with gr.Blocks(title="React Agent Builder") as app:
462
  system_prompt_input: "",
463
  create_msg: "",
464
  agent_url_output: "",
 
 
465
  tabs: gr.update(selected="dashboard")
466
  }
467
 
@@ -472,22 +503,26 @@ with gr.Blocks(title="React Agent Builder") as app:
472
 
473
 
474
  def handle_agent_creation(sess_id, user_id, name, model, mcp_enabled, github_token, prompt):
 
475
  msg, chatbot_id = create_agent(sess_id, name, model, mcp_enabled, github_token, prompt)
 
476
 
477
- # Build the agent URL locally if creation was successful
478
  agent_url = ""
 
479
  if chatbot_id and user_id:
480
- agent_url = f"https://curious-learner-chatbot.web.app/#/chats?clientId={user_id}&workspaceId={DEFAULT_WORKSPACE_ID}&chatbotId={chatbot_id}"
 
 
 
 
 
481
 
482
- # Also refresh the agents list and webview after creation
483
  agent_list = db_get_user_agents(user_id) if user_id else []
484
  updated_agents = "\n\n".join(agent_list) if agent_list else "No agents created yet."
485
- return {
486
- create_msg: msg,
487
- agent_url_output: agent_url,
488
- agents_list: updated_agents,
489
- chatbot_webview: get_chatbot_iframe(user_id)
490
- }
491
 
492
 
493
  # Refresh agents list function
@@ -499,7 +534,16 @@ with gr.Blocks(title="React Agent Builder") as app:
499
  return "\n\n".join(agent_list) if agent_list else "No agents created yet."
500
 
501
 
502
- # Login event - chain refresh after login
 
 
 
 
 
 
 
 
 
503
  login_btn.click(
504
  fn=handle_login,
505
  inputs=[login_email, login_password],
@@ -509,6 +553,10 @@ with gr.Blocks(title="React Agent Builder") as app:
509
  fn=refresh_agents_list,
510
  inputs=[user_id_state],
511
  outputs=[agents_list]
 
 
 
 
512
  )
513
 
514
  # Logout events (both buttons do the same thing)
@@ -535,14 +583,16 @@ with gr.Blocks(title="React Agent Builder") as app:
535
  # Create agent button
536
  create_agent_btn.click(
537
  fn=switch_to_create_agent,
538
- outputs=[dashboard_tab, create_agent_tab, tabs]
539
  )
540
 
541
  # Back button
542
  back_btn.click(
543
  fn=switch_back_to_dashboard,
 
544
  outputs=[dashboard_tab, create_agent_tab, agent_name_input, mcp_github,
545
- github_token_input, system_prompt_input, create_msg, agent_url_output, tabs]
 
546
  )
547
 
548
  # MCP GitHub checkbox toggle
@@ -557,7 +607,7 @@ with gr.Blocks(title="React Agent Builder") as app:
557
  fn=handle_agent_creation,
558
  inputs=[session_state, user_id_state, agent_name_input, model_dropdown, mcp_github,
559
  github_token_input, system_prompt_input],
560
- outputs=[create_msg, agent_url_output, agents_list, chatbot_webview]
561
  )
562
 
563
  # Refresh agents list button
 
29
  return f'''
30
  <iframe
31
  src="{chatbot_url}"
32
+ style="width: 100%; height: 600px; border: none;"
33
  allow="clipboard-write; clipboard-read"
34
  ></iframe>
35
  '''
 
93
 
94
  def create_agent(session_id, agent_name, model_name, mcp_github_enabled, github_token, system_prompt):
95
  """Create a new React agent"""
96
+ print(f"[DEBUG] create_agent: session_id={session_id}, agent_name={agent_name}")
97
  if not session_id or session_id not in user_sessions:
98
+ print(f"[DEBUG] create_agent: session not found")
99
  return "Please login first!", None
100
 
101
  user_id = user_sessions[session_id]
102
+ print(f"[DEBUG] create_agent: user_id={user_id}")
103
  success, message, chatbot_id = db_create_agent(
104
  user_id, agent_name, model_name, mcp_github_enabled, github_token, system_prompt
105
  )
106
+ print(f"[DEBUG] create_agent: success={success}, chatbot_id={chatbot_id}")
107
 
108
  return message, chatbot_id if success else None
109
 
 
236
  position: fixed !important;
237
  bottom: 20px !important;
238
  right: 20px !important;
239
+ width: 400px !important;
240
+ height: 600px !important;
241
  border: none !important;
242
  border-radius: 16px !important;
243
  overflow: hidden !important;
 
248
 
249
  #chatbot-webview iframe {
250
  width: 100% !important;
251
+ height: 600px !important;
252
+ border: none !important;
253
+ }
254
+
255
+ /* Agent preview styling */
256
+ #agent-preview {
257
+ width: 100% !important;
258
+ min-height: 500px !important;
259
+ height: 500px !important;
260
+ border: 2px solid #e5e7eb;
261
+ border-radius: 12px;
262
+ overflow: hidden;
263
+ margin: 15px 0;
264
+ background: #f9fafb;
265
+ }
266
+
267
+ #agent-preview iframe {
268
+ width: 100% !important;
269
+ height: 500px !important;
270
  border: none !important;
271
  }
272
 
 
339
  # Logout button at the bottom of dashboard, inside the container
340
  logout_btn_dashboard = gr.Button("🚪 Logout", size="sm", variant="secondary", elem_classes="logout-btn")
341
 
 
 
 
 
 
 
342
  with gr.Tab("Create Agent", id="create_agent", visible=False) as create_agent_tab:
343
  with gr.Column(elem_classes="form-container"):
344
  # Back button at the top
 
394
  create_msg = gr.Textbox(label="Status", interactive=False, lines=2, scale=1)
395
  agent_url_output = gr.Textbox(label="Agent URL", interactive=False, lines=3, scale=1)
396
 
397
+ # Agent preview iframe - shows after successful creation
398
+ agent_preview_label = gr.Markdown("### 🤖 Agent Preview")
399
+ agent_preview = gr.HTML(value="", elem_id="agent-preview")
400
+
401
  # Logout button at the bottom of create agent form
402
  logout_btn_create = gr.Button("🚪 Logout", size="sm", variant="secondary", elem_classes="logout-btn")
403
 
404
+ # Floating chatbot webview - outside tabs so it's always available
405
+ chatbot_webview = gr.HTML(
406
+ value="",
407
+ elem_id="chatbot-webview",
408
+ visible=False
409
+ )
410
+
411
 
412
  # Event handlers
413
  def handle_login(email, password):
 
435
  session_state: sess_id,
436
  user_id_state: user_id,
437
  agents_list: agents_text,
438
+ chatbot_webview: gr.update(value=chatbot_html, visible=True),
439
  tabs: gr.update(selected="dashboard")
440
  }
441
  else:
 
466
  login_email: "",
467
  login_password: "",
468
  login_msg: "",
469
+ chatbot_webview: gr.update(value="", visible=False),
470
  tabs: gr.update(selected="login")
471
  }
472
 
 
475
  return {
476
  dashboard_tab: gr.update(visible=False),
477
  create_agent_tab: gr.update(visible=True),
478
+ chatbot_webview: gr.update(visible=False),
479
  tabs: gr.update(selected="create_agent")
480
  }
481
 
482
 
483
+ def switch_back_to_dashboard(user_id):
484
+ chatbot_html = get_chatbot_iframe(user_id) if user_id else ""
485
  return {
486
  dashboard_tab: gr.update(visible=True),
487
  create_agent_tab: gr.update(visible=False),
 
491
  system_prompt_input: "",
492
  create_msg: "",
493
  agent_url_output: "",
494
+ agent_preview: "",
495
+ chatbot_webview: gr.update(value=chatbot_html, visible=True),
496
  tabs: gr.update(selected="dashboard")
497
  }
498
 
 
503
 
504
 
505
  def handle_agent_creation(sess_id, user_id, name, model, mcp_enabled, github_token, prompt):
506
+ print(f"[DEBUG] handle_agent_creation: sess_id={sess_id}, user_id={user_id}, name={name}")
507
  msg, chatbot_id = create_agent(sess_id, name, model, mcp_enabled, github_token, prompt)
508
+ print(f"[DEBUG] handle_agent_creation: msg={msg}, chatbot_id={chatbot_id}")
509
 
510
+ # Build the individual agent URL locally if creation was successful
511
  agent_url = ""
512
+ agent_iframe = ""
513
  if chatbot_id and user_id:
514
+ agent_url = f"https://curious-learner-chatbot.web.app/#/individual_chat_screen?clientId={user_id}&workspaceId={DEFAULT_WORKSPACE_ID}&chatbotId={chatbot_id}"
515
+ print(f"[DEBUG] handle_agent_creation: agent_url={agent_url}")
516
+ agent_iframe = f'''<iframe src="{agent_url}" style="width: 100%; height: 500px; border: none;" allow="clipboard-write; clipboard-read"></iframe>'''
517
+ print(f"[DEBUG] handle_agent_creation: iframe built, length={len(agent_iframe)}")
518
+ else:
519
+ print(f"[DEBUG] handle_agent_creation: chatbot_id or user_id is None/empty")
520
 
521
+ # Also refresh the agents list after creation
522
  agent_list = db_get_user_agents(user_id) if user_id else []
523
  updated_agents = "\n\n".join(agent_list) if agent_list else "No agents created yet."
524
+
525
+ return msg, agent_url, agent_iframe, updated_agents
 
 
 
 
526
 
527
 
528
  # Refresh agents list function
 
534
  return "\n\n".join(agent_list) if agent_list else "No agents created yet."
535
 
536
 
537
+ # Load chatbot webview function
538
+ def load_chatbot_webview(user_id):
539
+ """Load chatbot webview for user"""
540
+ if not user_id:
541
+ return gr.update(value="", visible=False)
542
+ chatbot_html = get_chatbot_iframe(user_id)
543
+ return gr.update(value=chatbot_html, visible=True)
544
+
545
+
546
+ # Login event - chain refresh and webview load after login
547
  login_btn.click(
548
  fn=handle_login,
549
  inputs=[login_email, login_password],
 
553
  fn=refresh_agents_list,
554
  inputs=[user_id_state],
555
  outputs=[agents_list]
556
+ ).then(
557
+ fn=load_chatbot_webview,
558
+ inputs=[user_id_state],
559
+ outputs=[chatbot_webview]
560
  )
561
 
562
  # Logout events (both buttons do the same thing)
 
583
  # Create agent button
584
  create_agent_btn.click(
585
  fn=switch_to_create_agent,
586
+ outputs=[dashboard_tab, create_agent_tab, chatbot_webview, tabs]
587
  )
588
 
589
  # Back button
590
  back_btn.click(
591
  fn=switch_back_to_dashboard,
592
+ inputs=[user_id_state],
593
  outputs=[dashboard_tab, create_agent_tab, agent_name_input, mcp_github,
594
+ github_token_input, system_prompt_input, create_msg, agent_url_output, agent_preview, chatbot_webview,
595
+ tabs]
596
  )
597
 
598
  # MCP GitHub checkbox toggle
 
607
  fn=handle_agent_creation,
608
  inputs=[session_state, user_id_state, agent_name_input, model_dropdown, mcp_github,
609
  github_token_input, system_prompt_input],
610
+ outputs=[create_msg, agent_url_output, agent_preview, agents_list]
611
  )
612
 
613
  # Refresh agents list button