2stacks Claude commited on
Commit
6b5afed
·
verified ·
1 Parent(s): d5e1565

Fix Tavily API key persistence in session state

Browse files

When users update the Tavily API key through the UI on HF Spaces, the agent now properly stores the key in session_state and recreates with both the OAuth token and the Tavily API key, ensuring MCP search tools are initialized correctly.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. my_ui.py +10 -6
my_ui.py CHANGED
@@ -22,19 +22,23 @@ class CustomGradioUI(GradioUI):
22
  self.allowed_file_types = allowed_file_types or [".pdf", ".docx", ".txt"]
23
  self.examples = examples or []
24
 
25
- def update_api_key(self, api_key, current_key, oauth_token: gr.OAuthToken | None = None):
26
  """Update the agent with a new API key."""
27
  if api_key and api_key != current_key:
28
  # Recreate the agent with the new API key
29
  self.agent = self.agent_factory(tavily_api_key=api_key, oauth_token=oauth_token)
30
- return api_key, gr.Markdown("✓ API key updated successfully", visible=True)
 
 
31
  elif not api_key and current_key:
32
  # Reset to default (env var)
33
  self.agent = self.agent_factory(oauth_token=oauth_token)
 
 
34
  return "", gr.Markdown(
35
  "API key cleared, using environment variable if set", visible=True
36
- )
37
- return current_key, gr.Markdown("", visible=False)
38
 
39
  def interact_with_agent_oauth(self, stored_messages, chatbot, session_state, oauth_token: gr.OAuthToken | None = None):
40
  """Wrapper for interact_with_agent that recreates agent with OAuth token."""
@@ -117,8 +121,8 @@ class CustomGradioUI(GradioUI):
117
  # Update agent when API key changes
118
  tavily_api_key_input.change(
119
  self.update_api_key,
120
- [tavily_api_key_input, current_api_key],
121
- [current_api_key, api_key_status],
122
  )
123
 
124
  gr.HTML(
 
22
  self.allowed_file_types = allowed_file_types or [".pdf", ".docx", ".txt"]
23
  self.examples = examples or []
24
 
25
+ def update_api_key(self, api_key, current_key, session_state, oauth_token: gr.OAuthToken | None = None):
26
  """Update the agent with a new API key."""
27
  if api_key and api_key != current_key:
28
  # Recreate the agent with the new API key
29
  self.agent = self.agent_factory(tavily_api_key=api_key, oauth_token=oauth_token)
30
+ # Store in session state for persistence
31
+ session_state["tavily_api_key"] = api_key
32
+ return api_key, gr.Markdown("✓ API key updated successfully", visible=True), session_state
33
  elif not api_key and current_key:
34
  # Reset to default (env var)
35
  self.agent = self.agent_factory(oauth_token=oauth_token)
36
+ # Clear from session state
37
+ session_state.pop("tavily_api_key", None)
38
  return "", gr.Markdown(
39
  "API key cleared, using environment variable if set", visible=True
40
+ ), session_state
41
+ return current_key, gr.Markdown("", visible=False), session_state
42
 
43
  def interact_with_agent_oauth(self, stored_messages, chatbot, session_state, oauth_token: gr.OAuthToken | None = None):
44
  """Wrapper for interact_with_agent that recreates agent with OAuth token."""
 
121
  # Update agent when API key changes
122
  tavily_api_key_input.change(
123
  self.update_api_key,
124
+ [tavily_api_key_input, current_api_key, session_state],
125
+ [current_api_key, api_key_status, session_state],
126
  )
127
 
128
  gr.HTML(