File size: 1,777 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sessionApi from "../pages/Chat/sessionApi";

function mapIfKnown(raw: string, requireKnownInList: boolean): string {
  if (!raw) return "";
  const mapped = sessionApi.getBackendSessionId(raw);
  if (!mapped) return "";
  if (!requireKnownInList) return mapped;
  // Same readiness check as Chat bg-task hydration: mapping alone is not
  // enough when the id is still an unresolved local timestamp.
  const knownInList =
    mapped !== raw || sessionApi.getRealIdForSession(raw) != null;
  return knownInList ? mapped : "";
}

/**
 * Resolve a backend-compatible session_id for tool-call APIs.
 *
 * Align with Chat hydration / getSessionIdentity:
 * 1. explicit preferred id (tool card / caller)
 * 2. lastActiveChatId (intentional selection; do not prefer window)
 * 3. window.currentSessionId only when it still resolves in the session list
 *
 * Always map through sessionApi so local-timestamp library ids become the
 * coordinator's session_id.
 */
export function resolveBackendSessionId(preferred?: string | null): string {
  const preferredTrim = (preferred && preferred.trim()) || "";
  if (preferredTrim) {
    // Explicit caller id: map even if list membership is still catching up.
    return (
      mapIfKnown(preferredTrim, false) ||
      sessionApi.getBackendSessionId(preferredTrim)
    );
  }

  const fromActive = mapIfKnown(sessionApi.lastActiveChatId || "", true);
  if (fromActive) return fromActive;

  // Soft fallback while a brand-new chat is still joining the list.
  const activeRaw = sessionApi.lastActiveChatId || "";
  if (activeRaw) {
    return sessionApi.getBackendSessionId(activeRaw);
  }

  const windowSid =
    (window as unknown as { currentSessionId?: string }).currentSessionId ?? "";
  return mapIfKnown(windowSid, true);
}