feat(copilot): refactor JWT token handling and improve metadata tracking

- Move JWT token from state to configurable context for better security and request isolation
- Add user_id parameter to agent service for enhanced metadata tracking
- Update checkpoint directory name from .gns3-copilot to gns3-copilot
- Implement context-aware JWT token management using ContextVar
- Improve tool node to extract JWT token from config instead of state
This commit is contained in:
YueGuobin committed 2026-03-04 13:36:16 +08:00
1 parent 5e9aac7514
commit b05e6a71b4
5 files changed
+43 -7

No files matched your search

@@ -214,12 +214,10 @@ def llm_call(state: dict, config: RunnableConfig | None = None):
llm_config=llm_config
)
# Store jwt_token in state for Tools to use when calling GNS3 API
return {
"messages": [model_with_tools.invoke(full_messages)],
"llm_calls": state.get("llm_calls", 0) + 1,
"topology_info": topology_info,
"jwt_token": jwt_token,
}
@@ -305,9 +303,18 @@ def generate_title(state: MessagesState, config: RunnableConfig | None = None) -
# Define tool node
def tool_node(state: dict):
def tool_node(state: dict, config: RunnableConfig | None = None):
"""Performs the tool call"""
# Extract jwt_token from config for tools to use
configurable = config.get("configurable", {}) if config else {}
jwt_token = configurable.get("jwt_token")
# Set jwt_token for connector_factory to access
if jwt_token:
from gns3server.agent.gns3_copilot.gns3_client import set_current_jwt_token
set_current_jwt_token(jwt_token)
result = []
for tool_call in state["messages"][-1].tool_calls:
tool = tools_by_name[tool_call["name"]]