mindroot 9.24.0__py3-none-any.whl → 10.0.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.

Potentially problematic release.


This version of mindroot might be problematic. Click here for more details.

mindroot/lib/chatlog.py CHANGED
@@ -281,7 +281,7 @@ async def find_child_logs_by_parent_id(parent_log_id: str) -> List[str]:
281
281
 
282
282
  def extract_delegate_task_log_ids(messages: List[Dict]) -> List[str]:
283
283
  """
284
- Extract log IDs from delegate_task commands in messages.
284
+ Extract log IDs from delegate_task and delegate_subtask commands in messages.
285
285
 
286
286
  Args:
287
287
  messages: List of chat messages
@@ -312,10 +312,15 @@ def extract_delegate_task_log_ids(messages: List[Dict]) -> List[str]:
312
312
  for key, value in cmd.items():
313
313
  if key == 'delegate_task' and 'log_id' in value:
314
314
  log_ids.append(value['log_id'])
315
+ elif key == 'delegate_subtask' and 'log_id' in value:
316
+ log_ids.append(value['log_id'])
315
317
  except (json.JSONDecodeError, TypeError, KeyError):
316
318
  # If not JSON, try regex to find log_ids in delegate_task commands
317
319
  matches = re.findall(r'"delegate_task"\s*:\s*{\s*"log_id"\s*:\s*"([^"]+)"', text)
318
320
  log_ids.extend(matches)
321
+ # Also check for delegate_subtask commands
322
+ matches = re.findall(r'"delegate_subtask"\s*:\s*{\s*"log_id"\s*:\s*"([^"]+)"', text)
323
+ log_ids.extend(matches)
319
324
 
320
325
  return log_ids
321
326
 
@@ -389,7 +394,119 @@ async def save_token_counts_to_cache(log_id: str, token_counts: Dict[str, int])
389
394
  async with aiofiles.open(cache_path, 'w') as f:
390
395
  await f.write(json.dumps(token_counts))
391
396
 
392
- async def count_tokens_for_log_id(log_id: str) -> Dict[str, int]:
397
+ async def build_token_hierarchy(log_id: str, user: str = None, visited: set = None) -> Dict:
398
+ """
399
+ Build a hierarchical token count structure for a log and its children.
400
+
401
+ Args:
402
+ log_id: The log ID to build hierarchy for
403
+ user: User for the log
404
+ visited: Set of already visited log IDs to prevent infinite recursion
405
+
406
+ Returns:
407
+ Dictionary with hierarchical structure containing:
408
+ - log_id: The log ID
409
+ - individual_counts: Token counts for this log only
410
+ - cumulative_counts: Token counts including all children
411
+ - children: List of child hierarchies
412
+ """
413
+ if visited is None:
414
+ visited = set()
415
+
416
+ if log_id in visited:
417
+ return None # Prevent infinite recursion
418
+
419
+ visited.add(log_id)
420
+
421
+ # Find the chatlog file
422
+ chatlog_path = await find_chatlog_file(log_id)
423
+ if not chatlog_path:
424
+ return None
425
+
426
+ # Load the chat log
427
+ async with aiofiles.open(chatlog_path, 'r') as f:
428
+ content = await f.read()
429
+ log_data = json.loads(content)
430
+
431
+ # Check if we have cached individual counts for this specific session
432
+ cached_individual = await get_cached_token_counts(log_id, chatlog_path)
433
+ if cached_individual and 'input_tokens_sequence' in cached_individual:
434
+ print(f"Using cached individual token counts for session {log_id}")
435
+ individual_counts = {
436
+ 'input_tokens_sequence': cached_individual['input_tokens_sequence'],
437
+ 'output_tokens_sequence': cached_individual['output_tokens_sequence'],
438
+ 'input_tokens_total': cached_individual['input_tokens_total']
439
+ }
440
+ else:
441
+ # Calculate individual counts for this session
442
+ if user is None:
443
+ try:
444
+ path_parts = chatlog_path.split(os.sep)
445
+ if len(path_parts) >= 4 and path_parts[-4] == 'chat':
446
+ user = path_parts[-3]
447
+ else:
448
+ user = "system"
449
+ except Exception:
450
+ user = "system"
451
+
452
+ temp_log = ChatLog(log_id=log_id, user=user, agent=log_data.get('agent', 'unknown'))
453
+ temp_log.messages = log_data.get('messages', [])
454
+
455
+ # Count tokens for this log only
456
+ individual_counts = temp_log.count_tokens()
457
+
458
+ # Cache the individual session counts
459
+ individual_cache_data = {
460
+ 'input_tokens_sequence': individual_counts['input_tokens_sequence'],
461
+ 'output_tokens_sequence': individual_counts['output_tokens_sequence'],
462
+ 'input_tokens_total': individual_counts['input_tokens_total']
463
+ }
464
+ await save_token_counts_to_cache(log_id, individual_cache_data)
465
+ print(f"Cached individual token counts for session {log_id}")
466
+
467
+ # Find all child log IDs
468
+ if user is None:
469
+ try:
470
+ path_parts = chatlog_path.split(os.sep)
471
+ if len(path_parts) >= 4 and path_parts[-4] == 'chat':
472
+ user = path_parts[-3]
473
+ else:
474
+ user = "system"
475
+ except Exception:
476
+ user = "system"
477
+
478
+ temp_log = ChatLog(log_id=log_id, user=user, agent=log_data.get('agent', 'unknown'))
479
+ temp_log.messages = log_data.get('messages', [])
480
+ delegated_log_ids = extract_delegate_task_log_ids(temp_log.messages)
481
+ child_logs_by_parent = await find_child_logs_by_parent_id(log_id)
482
+ all_child_log_ids = list(set(delegated_log_ids) | set(child_logs_by_parent))
483
+
484
+ # Build child hierarchies
485
+ children = []
486
+ cumulative_counts = {
487
+ 'input_tokens_sequence': individual_counts['input_tokens_sequence'],
488
+ 'output_tokens_sequence': individual_counts['output_tokens_sequence'],
489
+ 'input_tokens_total': individual_counts['input_tokens_total']
490
+ }
491
+
492
+ for child_id in all_child_log_ids:
493
+ child_hierarchy = await build_token_hierarchy(child_id, user, visited.copy())
494
+ if child_hierarchy:
495
+ children.append(child_hierarchy)
496
+ # Add child's cumulative counts to our cumulative counts
497
+ cumulative_counts['input_tokens_sequence'] += child_hierarchy['cumulative_counts']['input_tokens_sequence']
498
+ cumulative_counts['output_tokens_sequence'] += child_hierarchy['cumulative_counts']['output_tokens_sequence']
499
+ cumulative_counts['input_tokens_total'] += child_hierarchy['cumulative_counts']['input_tokens_total']
500
+
501
+ return {
502
+ 'log_id': log_id,
503
+ 'agent': log_data.get('agent', 'unknown'),
504
+ 'individual_counts': individual_counts,
505
+ 'cumulative_counts': cumulative_counts,
506
+ 'children': children
507
+ }
508
+
509
+ async def count_tokens_for_log_id(log_id: str, user: str = None, hierarchical: bool = False) -> Dict[str, int]:
393
510
  """
394
511
  Count tokens for a chat log identified by log_id, including any delegated tasks.
395
512
 
@@ -397,13 +514,32 @@ async def count_tokens_for_log_id(log_id: str) -> Dict[str, int]:
397
514
  log_id: The log ID to count tokens for
398
515
 
399
516
  Returns:
400
- Dictionary with token counts or None if log not found
517
+ Dictionary with token counts or None if log not found.
518
+ If hierarchical=True, includes 'hierarchy' key with tree structure.
519
+ If hierarchical=False (default), returns flat structure for backwards compatibility.
401
520
  """
402
521
  # Find the chatlog file
403
522
  chatlog_path = await find_chatlog_file(log_id)
404
523
  if not chatlog_path:
405
524
  return None
406
525
 
526
+ # If hierarchical structure is requested, build and return it
527
+ if hierarchical:
528
+ # Check cache first for hierarchical data
529
+ cached_counts = await get_cached_token_counts(log_id, chatlog_path)
530
+ if cached_counts and 'hierarchy' in cached_counts:
531
+ print(f"Using cached hierarchical token counts for {log_id}")
532
+ return cached_counts
533
+
534
+ print(f"Calculating hierarchical token counts for {log_id}")
535
+ hierarchy = await build_token_hierarchy(log_id, user)
536
+ if hierarchy:
537
+ result = {'hierarchy': hierarchy}
538
+ # Save hierarchical data to cache
539
+ await save_token_counts_to_cache(log_id, result)
540
+ return result
541
+ return None
542
+
407
543
  # Check cache first
408
544
  cached_counts = await get_cached_token_counts(log_id, chatlog_path)
409
545
  if cached_counts:
@@ -421,7 +557,21 @@ async def count_tokens_for_log_id(log_id: str) -> Dict[str, int]:
421
557
  parent_log_id = log_data.get('parent_log_id')
422
558
 
423
559
  # Create a temporary ChatLog instance to count tokens
424
- temp_log = ChatLog(log_id=log_id, user="system", agent=log_data.get('agent', 'unknown'))
560
+ # Use provided user or try to determine from chatlog path or fallback to "system"
561
+ if user is None:
562
+ # Try to extract user from chatlog path: data/chat/{user}/{agent}/chatlog_{log_id}.json
563
+ try:
564
+ path_parts = chatlog_path.split(os.sep)
565
+ if len(path_parts) >= 4 and path_parts[-4] == 'chat':
566
+ extracted_user = path_parts[-3] # User is third from the end
567
+ user = extracted_user
568
+ print(f"Extracted user '{user}' from chatlog path: {chatlog_path}")
569
+ else:
570
+ user = "system" # Default fallback
571
+ except Exception as e:
572
+ print(f"Error extracting user from path {chatlog_path}: {e}")
573
+ user = "system" # Default fallback
574
+ temp_log = ChatLog(log_id=log_id, user=user, agent=log_data.get('agent', 'unknown'))
425
575
  temp_log.messages = log_data.get('messages', [])
426
576
 
427
577
  # Count tokens for this log
@@ -449,7 +599,7 @@ async def count_tokens_for_log_id(log_id: str) -> Dict[str, int]:
449
599
 
450
600
  # Recursively count tokens for all child tasks
451
601
  for child_id in all_child_log_ids:
452
- delegated_counts = await count_tokens_for_log_id(child_id)
602
+ delegated_counts = await count_tokens_for_log_id(child_id, user=user)
453
603
  if delegated_counts:
454
604
  combined_counts['input_tokens_sequence'] += delegated_counts['input_tokens_sequence']
455
605
  combined_counts['output_tokens_sequence'] += delegated_counts['output_tokens_sequence']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mindroot
3
- Version: 9.24.0
3
+ Version: 10.0.0
4
4
  Summary: MindRoot AI Agent Framework
5
5
  Requires-Python: >=3.9
6
6
  License-File: LICENSE
@@ -489,9 +489,9 @@ mindroot/coreplugins/chat/static/js/auth.js,sha256=HbzBIz2yGdWQggVGzEsIzNG272T7x
489
489
  mindroot/coreplugins/chat/static/js/authfetch.js,sha256=a05Vj4pt6Qnu4WdF7jsismVijubUXZuR6ndWeYBOtZY,526
490
490
  mindroot/coreplugins/chat/static/js/base.js,sha256=AtaHATjxpQNQ7Lv8RUlV0n3icrpmC2dmnkAdSI_v714,1285
491
491
  mindroot/coreplugins/chat/static/js/chat-history.js,sha256=FuJyEu0jcNomJeCbRT45Z_kcS5LVZdRbnC6kcH-Eyd8,6501
492
- mindroot/coreplugins/chat/static/js/chat.js,sha256=Qe0LA36uUGad1lTTpAdlEmkTwCY4btdFk_q0lafoPVA,14785
492
+ mindroot/coreplugins/chat/static/js/chat.js,sha256=PEQlgtSQB779Rk1IgD-8NdZ9eZT8dlQW8R8iapq9onM,15074
493
493
  mindroot/coreplugins/chat/static/js/chat.js.diff,sha256=QdlbO_WiNXUWmvbwM5x_bC5WMFdzsXrXaNtmubPtnhE,8339
494
- mindroot/coreplugins/chat/static/js/chatform.js,sha256=Y_LFGA6QBcqI8BAQ2hog4fBkLcnQDAfZQO3zIfO2tpw,17825
494
+ mindroot/coreplugins/chat/static/js/chatform.js,sha256=0tMTfeBTVLxiuTVS8tLD67UYVMrL1H_sa-0hAkm3EqA,17832
495
495
  mindroot/coreplugins/chat/static/js/chatmessage.js,sha256=qoRb6XWKzzbMOIowIt3admWO8FaT2VqWGhN_anQlGSQ,1272
496
496
  mindroot/coreplugins/chat/static/js/cmdprefixes.js,sha256=Gip5WQXYb_VFK51OYlNtxT1fKUATKLsuVUpaGBKBwig,1022
497
497
  mindroot/coreplugins/chat/static/js/code-copy-button.js,sha256=-owTkgvP9Ibdd12yyFJcvAeN2jfqf0ETUtXgNuHnWrg,5890
@@ -890,7 +890,12 @@ mindroot/coreplugins/chat_avatar/__init__.py,sha256=MsSFjiLMLJZ7QhUPpVBWKiyDnCzr
890
890
  mindroot/coreplugins/chat_avatar/inject/chat.jinja2,sha256=TDSSt_SdOOW4EJMQK7fA_L2W5GNbDICRmXyqSsw0wuE,1093
891
891
  mindroot/coreplugins/check_list/__init__.py,sha256=SaaGvnpz37xRM7DjGWBz5CD27Jh2UVdPLGoVUAFrUSY,77
892
892
  mindroot/coreplugins/check_list/checklist.md,sha256=C8zeBhcA6R_ts2m-PzhIQOs0pg-C8xcFUmXjfHZO3d0,3908
893
- mindroot/coreplugins/check_list/mod.py,sha256=WuhrvkERbXOrNrB3R-zcOxZirNH4FGP0k6jao8hzsrE,11102
893
+ mindroot/coreplugins/check_list/helpers.py,sha256=C1wZEXF4dAfQGzXbRcOddEpXOt-jY1WfFN2bLUy9Y9U,7395
894
+ mindroot/coreplugins/check_list/mod.py,sha256=0qQTSFUudgnnjMM1-IiqfI6KNCt40T-AKUJekEunRso,24052
895
+ mindroot/coreplugins/check_list/mod.py.backup,sha256=ApezrvpRpJRvXlApVM1KaEfENwrjjO6Yo8C6vz4_TDE,12715
896
+ mindroot/coreplugins/check_list/mod.py.backup2,sha256=oFHtGmUGxB-5P-WD7O2qoldGpjPIqN0rmYIc6DFwreM,17429
897
+ mindroot/coreplugins/check_list/mod.py.backup3,sha256=oFHtGmUGxB-5P-WD7O2qoldGpjPIqN0rmYIc6DFwreM,17429
898
+ mindroot/coreplugins/check_list/plugin_info.json,sha256=DGX0ThPGszK-p-vrIMsNnlC57AucfJsJWQOiMInD0NY,390
894
899
  mindroot/coreplugins/check_list/inject/admin.jinja2,sha256=wt7LCyfY2jEiN7qGecAvIb_Sn8yvXGFI4hK20ZuwjSA,407
895
900
  mindroot/coreplugins/check_list/override/system.jinja2,sha256=nvak8X3APCKqNtFstSFqfxiU1-oTcq6XNZER19xiW1Q,208
896
901
  mindroot/coreplugins/check_list/static/js/checklist-help.js,sha256=xwjtXMae4gyHeetqTWZ36sSRQHuGPoqEdVtfk7I2m24,8214
@@ -2219,9 +2224,10 @@ mindroot/docs/source/mr_agent_expert_instr.rst,sha256=SWz277SscvWbpGnxtbacMTyQ7L
2219
2224
  mindroot/lib/__init__.py,sha256=388n_hMskU0TnZ4xT10US_kFkya-EPBjWcv7AZf_HOk,74
2220
2225
  mindroot/lib/buchatlog.py,sha256=LJZc3ksKgJcStltmHrrwNLaON3EDzhOKVAWj0Wl22wk,5861
2221
2226
  mindroot/lib/buchatlog2.py,sha256=Va9FteBWePEjWD9OZcw-OtQfEb-IoCVGTmJeMRaX9is,13729
2227
+ mindroot/lib/buchatlog3.py,sha256=SAvcK2m_CW0Jw8p1pqnbrTexcx24PotrsJTqvQ_D290,24573
2222
2228
  mindroot/lib/butemplates.py,sha256=gfHGPTOjvoEenXsR7xokNuqMjOAPuC2DawheH1Ae4bU,12196
2223
2229
  mindroot/lib/chatcontext.py,sha256=CXk-pX-7RG3NiRFsAZWERWxnuFJOHH7FHtOLm-kGRXE,12437
2224
- mindroot/lib/chatlog.py,sha256=QbUNPgCnr9KhreQt4A_kXNICy1-JsVjpLKYt6URYhaQ,19390
2230
+ mindroot/lib/chatlog.py,sha256=JuUffRUhs966d7MhE_xt8iSviZCULSRpwCtvnpjNd4Y,26139
2225
2231
  mindroot/lib/chatlog_optimized.py,sha256=rL7KBP-V4_cGgMLihxPm3HoKcjFEyA1uEtPtqvkOa3A,20011
2226
2232
  mindroot/lib/json_escape.py,sha256=5cAmAdNbnYX2uyfQcnse2fFtNI0CdB-AfZ23RwaDm-k,884
2227
2233
  mindroot/lib/model_selector.py,sha256=Wz-8NZoiclmnhLeCNnI3WCuKFmjsO5HE4bK5F8GpZzU,1397
@@ -2275,9 +2281,9 @@ mindroot/protocols/services/stream_chat.py,sha256=fMnPfwaB5fdNMBLTEg8BXKAGvrELKH
2275
2281
  mindroot/registry/__init__.py,sha256=40Xy9bmPHsgdIrOzbtBGzf4XMqXVi9P8oZTJhn0r654,151
2276
2282
  mindroot/registry/component_manager.py,sha256=WZFNPg4SNvpqsM5NFiC2DpgmrJQCyR9cNhrCBpp30Qk,995
2277
2283
  mindroot/registry/data_access.py,sha256=81In5TwETpaqnnY1_-tBQM7rfWvUxZUZkG7lEelRUfU,5321
2278
- mindroot-9.24.0.dist-info/licenses/LICENSE,sha256=8plAmZh8y9ccuuqFFz4kp7G-cO_qsPgAOoHNvabSB4U,1070
2279
- mindroot-9.24.0.dist-info/METADATA,sha256=q4JCmi8RGEMUxvpjdZOUM_nlr4yDxfgVRVJa8iBEobM,1035
2280
- mindroot-9.24.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
2281
- mindroot-9.24.0.dist-info/entry_points.txt,sha256=0bpyjMccLttx6VcjDp6zfJPN0Kk0rffor6IdIbP0j4c,50
2282
- mindroot-9.24.0.dist-info/top_level.txt,sha256=gwKm7DmNjhdrCJTYCrxa9Szne4lLpCtrEBltfsX-Mm8,9
2283
- mindroot-9.24.0.dist-info/RECORD,,
2284
+ mindroot-10.0.0.dist-info/licenses/LICENSE,sha256=8plAmZh8y9ccuuqFFz4kp7G-cO_qsPgAOoHNvabSB4U,1070
2285
+ mindroot-10.0.0.dist-info/METADATA,sha256=2vDWtBX3R98u5X5FXEgU3luyiNFtn_MbEggRRai4MnQ,1035
2286
+ mindroot-10.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
2287
+ mindroot-10.0.0.dist-info/entry_points.txt,sha256=0bpyjMccLttx6VcjDp6zfJPN0Kk0rffor6IdIbP0j4c,50
2288
+ mindroot-10.0.0.dist-info/top_level.txt,sha256=gwKm7DmNjhdrCJTYCrxa9Szne4lLpCtrEBltfsX-Mm8,9
2289
+ mindroot-10.0.0.dist-info/RECORD,,