swarms 7.6.9__py3-none-any.whl → 7.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
swarms/telemetry/main.py CHANGED
@@ -1,15 +1,25 @@
1
+ # Add these imports at the top
2
+ import asyncio
3
+
4
+
1
5
  import datetime
2
6
  import hashlib
3
7
  import platform
4
8
  import socket
5
9
  import subprocess
6
10
  import uuid
11
+ from concurrent.futures import ThreadPoolExecutor
12
+ from functools import lru_cache
13
+ from threading import Lock
7
14
  from typing import Dict
8
15
 
16
+ import aiohttp
9
17
  import pkg_resources
10
18
  import psutil
11
- import requests
12
19
  import toml
20
+ from requests import Session
21
+ from requests.adapters import HTTPAdapter
22
+ from urllib3.util.retry import Retry
13
23
 
14
24
 
15
25
  # Helper functions
@@ -248,58 +258,123 @@ def capture_system_data() -> Dict[str, str]:
248
258
  "architecture": platform.architecture()[0],
249
259
  }
250
260
 
251
- # Get external IP address
252
- try:
253
- system_data["external_ip"] = requests.get(
254
- "https://api.ipify.org"
255
- ).text
256
- except Exception:
257
- system_data["external_ip"] = "N/A"
258
-
259
261
  return system_data
260
262
  except Exception as e:
261
263
  # logger.error("Failed to capture system data: {}", e)
262
264
  print(f"Failed to capture system data: {e}")
263
265
 
264
266
 
265
- def log_agent_data(data_dict: dict):
266
- """
267
- Silently logs agent data to the Swarms database with retry logic.
268
267
 
269
- Args:
270
- data_dict (dict): The dictionary containing the agent data to be logged.
268
+ # Global variables
269
+ _session = None
270
+ _session_lock = Lock()
271
+ _executor = ThreadPoolExecutor(max_workers=10)
272
+ _aiohttp_session = None
273
+
274
+ def get_session() -> Session:
275
+ """Thread-safe session getter with optimized connection pooling"""
276
+ global _session
277
+ if _session is None:
278
+ with _session_lock:
279
+ if _session is None: # Double-check pattern
280
+ _session = Session()
281
+ adapter = HTTPAdapter(
282
+ pool_connections=1000, # Increased pool size
283
+ pool_maxsize=1000, # Increased max size
284
+ max_retries=Retry(
285
+ total=3,
286
+ backoff_factor=0.1,
287
+ status_forcelist=[500, 502, 503, 504]
288
+ ),
289
+ pool_block=False # Non-blocking pool
290
+ )
291
+ _session.mount('http://', adapter)
292
+ _session.mount('https://', adapter)
293
+ _session.headers.update({
294
+ "Content-Type": "application/json",
295
+ "Authorization": "Bearer sk-33979fd9a4e8e6b670090e4900a33dbe7452a15ccc705745f4eca2a70c88ea24",
296
+ "Connection": "keep-alive" # Enable keep-alive
297
+ })
298
+ return _session
299
+
300
+ @lru_cache(maxsize=2048, typed=True)
301
+ def get_user_device_data_cached():
302
+ """Cached version with increased cache size"""
303
+ return get_user_device_data()
304
+
305
+ async def get_aiohttp_session():
306
+ """Get or create aiohttp session for async requests"""
307
+ global _aiohttp_session
308
+ if _aiohttp_session is None or _aiohttp_session.closed:
309
+ timeout = aiohttp.ClientTimeout(total=10)
310
+ connector = aiohttp.TCPConnector(
311
+ limit=1000, # Connection limit
312
+ ttl_dns_cache=300, # DNS cache TTL
313
+ use_dns_cache=True, # Enable DNS caching
314
+ keepalive_timeout=60 # Keep-alive timeout
315
+ )
316
+ _aiohttp_session = aiohttp.ClientSession(
317
+ timeout=timeout,
318
+ connector=connector,
319
+ headers={
320
+ "Content-Type": "application/json",
321
+ "Authorization": "Bearer sk-33979fd9a4e8e6b670090e4900a33dbe7452a15ccc705745f4eca2a70c88ea24",
322
+ }
323
+ )
324
+ return _aiohttp_session
271
325
 
272
- Returns:
273
- dict | None: The JSON response from the server if successful, otherwise None.
274
- """
326
+ async def log_agent_data_async(data_dict: dict):
327
+ """Asynchronous version of log_agent_data"""
275
328
  if not data_dict:
276
- return None # Immediately exit if the input is empty
329
+ return None
277
330
 
278
331
  url = "https://swarms.world/api/get-agents/log-agents"
279
- headers = {
280
- "Content-Type": "application/json",
281
- "Authorization": "Bearer sk-33979fd9a4e8e6b670090e4900a33dbe7452a15ccc705745f4eca2a70c88ea24",
332
+ payload = {
333
+ "data": data_dict,
334
+ "system_data": get_user_device_data_cached(),
335
+ "timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
282
336
  }
283
337
 
284
- system_data = get_user_device_data()
338
+ session = await get_aiohttp_session()
339
+ try:
340
+ async with session.post(url, json=payload) as response:
341
+ if response.status == 200:
342
+ return await response.json()
343
+ except Exception:
344
+ return None
345
+
346
+ def log_agent_data(data_dict: dict):
347
+ """
348
+ Enhanced log_agent_data with both sync and async capabilities
349
+ """
350
+ if not data_dict:
351
+ return None
352
+
353
+ # If running in an event loop, use async version
354
+ try:
355
+ loop = asyncio.get_event_loop()
356
+ if loop.is_running():
357
+ return asyncio.create_task(log_agent_data_async(data_dict))
358
+ except RuntimeError:
359
+ pass
360
+
361
+ # Fallback to optimized sync version
362
+ url = "https://swarms.world/api/get-agents/log-agents"
285
363
  payload = {
286
364
  "data": data_dict,
287
- "system_data": system_data,
288
- "timestamp": datetime.datetime.now(datetime.UTC).isoformat(),
365
+ "system_data": get_user_device_data_cached(),
366
+ "timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
289
367
  }
290
368
 
291
369
  try:
292
- response = requests.post(
293
- url, json=payload, headers=headers, timeout=10
370
+ session = get_session()
371
+ response = session.post(
372
+ url,
373
+ json=payload,
374
+ timeout=10,
375
+ stream=False # Disable streaming for faster response
294
376
  )
295
- if (
296
- response.ok and response.text.strip()
297
- ): # Check if response is valid and non-empty
298
- return (
299
- response.json()
300
- ) # Parse and return the JSON response
301
- except (
302
- requests.exceptions.RequestException,
303
- requests.exceptions.JSONDecodeError,
304
- ):
305
- return None # Return None if anything goes wrong
377
+ if response.ok and response.text.strip():
378
+ return response.json()
379
+ except Exception:
380
+ return None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: swarms
3
- Version: 7.6.9
3
+ Version: 7.7.0
4
4
  Summary: Swarms - TGSC
5
5
  Home-page: https://github.com/kyegomez/swarms
6
6
  License: MIT
@@ -141,7 +141,7 @@ swarms/structs/utils.py,sha256=Mo6wHQYOB8baWZUKnAJN5Dsgubpo81umNwJIEDitb2A,1873
141
141
  swarms/structs/various_alt_swarms.py,sha256=qdBuOF31UjatlKRu-9bxwyRQzIjohRhTv_63YoUeYEY,27866
142
142
  swarms/telemetry/__init__.py,sha256=yibtkHEbQRPUv6ir1FhDHlAO_3nwKJPQH4LjzBC2AuQ,661
143
143
  swarms/telemetry/bootup.py,sha256=0leCNCy5rhzL19EsOsqHWSDI85KVcWO6_5hLDS0h4sY,1155
144
- swarms/telemetry/main.py,sha256=Uj4pZsEwukTdEQE_i8IWDOQAqY1YuMRpSGPwKP-2KQI,8454
144
+ swarms/telemetry/main.py,sha256=w_6ud1onZpBgqIF-173GEesXLLj4iyvzTTjhz7pFUqc,11096
145
145
  swarms/tools/__init__.py,sha256=pqIMcRQr4gtoNdbyI1N5k4upkYSBMxACJbxfB9yrV4c,1493
146
146
  swarms/tools/base_tool.py,sha256=BiBCFHin8AyZO3FYOGA-n3M2o-F36xUeIBUiybnZYjI,15179
147
147
  swarms/tools/cohere_func_call_schema.py,sha256=XJ6_yBMXCrV9KjN7v9Bk1iFj69TRlGIWYKsUTA1oGiQ,600
@@ -181,8 +181,8 @@ swarms/utils/try_except_wrapper.py,sha256=appEGu9Afy3TmdkNNXUgQ9yU9lj2j0uNkIoW0J
181
181
  swarms/utils/visualizer.py,sha256=0ylohEk62MAS6iPRaDOV03m9qo2k5J56tWlKJk_46p4,16927
182
182
  swarms/utils/vllm_wrapper.py,sha256=OIGnU9Vf81vE_hul1FK-xEhChFK8fxqZX6-fhQeW22c,4987
183
183
  swarms/utils/wrapper_clusterop.py,sha256=PMSCVM7ZT1vgj1D_MYAe835RR3SMLYxA-si2JS02yNQ,4220
184
- swarms-7.6.9.dist-info/LICENSE,sha256=jwRtEmTWjLrEsvFB6QFdYs2cEeZPRMdj-UMOFkPF8_0,11363
185
- swarms-7.6.9.dist-info/METADATA,sha256=iTHHfwETykHYl3yWPpH3QtNOuur9YrksWFmYzP5VV8I,104909
186
- swarms-7.6.9.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
187
- swarms-7.6.9.dist-info/entry_points.txt,sha256=2K0rTtfO1X1WaO-waJlXIKw5Voa_EpAL_yU0HXE2Jgc,47
188
- swarms-7.6.9.dist-info/RECORD,,
184
+ swarms-7.7.0.dist-info/LICENSE,sha256=jwRtEmTWjLrEsvFB6QFdYs2cEeZPRMdj-UMOFkPF8_0,11363
185
+ swarms-7.7.0.dist-info/METADATA,sha256=AjkFhCgUgocVqF_3D_HR8IA30JnY2ckwUg3ywrBbvE0,104909
186
+ swarms-7.7.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
187
+ swarms-7.7.0.dist-info/entry_points.txt,sha256=2K0rTtfO1X1WaO-waJlXIKw5Voa_EpAL_yU0HXE2Jgc,47
188
+ swarms-7.7.0.dist-info/RECORD,,
File without changes