MCPcopy Create free account
hub / github.com/nlweb-ai/NLWeb / handle_query

Method handle_query

AskAgent/python/webserver/a2a_wrapper.py:125–198  ·  view source on GitHub ↗

Handle a query from an agent using NLWebHandler Args: content: Query content with query, site, generate_mode etc. query_params: URL query parameters to pass through from_agent: ID of the requesting agent Returns: Query result

(self, content: dict[str, Any], query_params: dict,
                          from_agent: str)

Source from the content-addressed store, hash-verified

123 await send_chunk(json.dumps(response).encode('utf-8'), end_response=True)
124
125 async def handle_query(self, content: dict[str, Any], query_params: dict,
126 from_agent: str) -> dict[str, Any]:
127 """
128 Handle a query from an agent using NLWebHandler
129
130 Args:
131 content: Query content with query, site, generate_mode etc.
132 query_params: URL query parameters to pass through
133 from_agent: ID of the requesting agent
134
135 Returns:
136 Query results in A2A format
137 """
138 query = content.get("query", "")
139 sites = content.get("site", content.get("sites", []))
140 generate_mode = content.get("generate_mode", "list")
141
142 # Update query params with A2A content
143 query_params["query"] = [query] if query else []
144 if sites:
145 query_params["site"] = sites if isinstance(sites, list) else [sites]
146 query_params["generate_mode"] = [generate_mode]
147
148 logger.info(f"Processing query from {from_agent}: {query}")
149
150 # Collect response
151 response_chunks = []
152
153 class ChunkCollector:
154 async def write_stream(self, data, end_response=False):
155 if isinstance(data, dict):
156 chunk = json.dumps(data)
157 elif isinstance(data, bytes):
158 chunk = data.decode('utf-8')
159 else:
160 chunk = str(data)
161 response_chunks.append(chunk)
162
163 collector = ChunkCollector()
164
165 # Process query with NLWebHandler
166 try:
167 handler = NLWebHandler(query_params, collector)
168 await asyncio.wait_for(handler.runQuery(), timeout=30.0)
169
170 # Combine chunks into response
171 full_response = ''.join(response_chunks)
172
173 # Try to parse as JSON for structured response
174 try:
175 response_data = json.loads(full_response)
176 except Exception:
177 response_data = {"text": full_response}
178
179 return {
180 "query": query,
181 "content": response_data,
182 "status": "success"

Callers 1

handle_messageMethod · 0.95

Calls 7

runQueryMethod · 0.95
NLWebHandlerClass · 0.90
ChunkCollectorClass · 0.85
getMethod · 0.80
infoMethod · 0.45
warningMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected