|
25 | 25 | from datetime import datetime |
26 | 26 | from pathlib import Path |
27 | 27 |
|
| 28 | +__all__ = [ |
| 29 | + "search_global_storage", |
| 30 | + "search_legacy_workspaces", |
| 31 | + "search_cli_sessions", |
| 32 | + "rank_results", |
| 33 | +] |
28 | 34 | from models import Bubble, Composer, ParseWarningCollector, SchemaError |
29 | 35 | from services.workspace_db import ( |
30 | 36 | build_composer_id_to_workspace_id, |
@@ -120,7 +126,6 @@ def _find_match( |
120 | 126 |
|
121 | 127 |
|
122 | 128 | def _build_ws_id_to_name( |
123 | | - workspace_path: str, |
124 | 129 | workspace_entries: list[dict], |
125 | 130 | ) -> dict[str, str]: |
126 | 131 | """Map workspace folder IDs to human-readable display names. |
@@ -204,7 +209,7 @@ def search_global_storage( |
204 | 209 | results: list[dict] = [] |
205 | 210 | try: |
206 | 211 | workspace_entries = collect_workspace_entries(workspace_path) |
207 | | - ws_id_to_name = _build_ws_id_to_name(workspace_path, workspace_entries) |
| 212 | + ws_id_to_name = _build_ws_id_to_name(workspace_entries) |
208 | 213 | composer_id_to_ws = build_composer_id_to_workspace_id( |
209 | 214 | workspace_path, workspace_entries |
210 | 215 | ) |
@@ -435,7 +440,7 @@ def search_legacy_workspaces( |
435 | 440 | "workspaceFolder": workspace_folder, |
436 | 441 | "chatId": tab.get("tabId"), |
437 | 442 | "chatTitle": ct or f"Chat {(tab.get('tabId') or '')[:8]}", |
438 | | - "timestamp": tab.get("lastSendTime") or datetime.now().isoformat(), |
| 443 | + "timestamp": tab.get("lastSendTime") or 0, |
439 | 444 | "matchingText": matching_text, |
440 | 445 | "type": "chat", |
441 | 446 | }) |
@@ -554,14 +559,17 @@ def search_cli_sessions( |
554 | 559 | def rank_results(results: list[dict]) -> list[dict]: |
555 | 560 | """Sort *results* by timestamp descending. |
556 | 561 |
|
557 | | - Handles both integer epoch-ms timestamps and ISO 8601 strings so the |
558 | | - three source types (composer, chat, cli_agent) sort together correctly. |
| 562 | + All three source types use epoch-millisecond integers, except |
| 563 | + ``search_legacy_workspaces`` which may emit ISO 8601 strings for the |
| 564 | + ``lastSendTime`` field. ISO strings are converted to epoch-ms so |
| 565 | + cross-source comparisons are made in the same unit. |
559 | 566 | """ |
560 | 567 | def _ts(r: dict) -> float: |
561 | 568 | t = r.get("timestamp", 0) |
562 | 569 | if isinstance(t, str): |
563 | 570 | try: |
564 | | - return datetime.fromisoformat(t.replace("Z", "+00:00")).timestamp() |
| 571 | + # .timestamp() → epoch-seconds; ×1000 → epoch-ms to match ints |
| 572 | + return datetime.fromisoformat(t.replace("Z", "+00:00")).timestamp() * 1000 |
565 | 573 | except Exception: |
566 | 574 | return 0.0 |
567 | 575 | return float(t) if t else 0.0 |
|
0 commit comments