replBridgeHandle.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { updateSessionBridgeId } from '../utils/concurrentSessions.js'
  2. import type { ReplBridgeHandle } from './replBridge.js'
  3. import { toCompatSessionId } from './sessionIdCompat.js'
  4. /**
  5. * Global pointer to the active REPL bridge handle, so callers outside
  6. * useReplBridge's React tree (tools, slash commands) can invoke handle methods
  7. * like subscribePR. Same one-bridge-per-process justification as bridgeDebug.ts
  8. * — the handle's closure captures the sessionId and getAccessToken that created
  9. * the session, and re-deriving those independently (BriefTool/upload.ts pattern)
  10. * risks staging/prod token divergence.
  11. *
  12. * Set from useReplBridge.tsx when init completes; cleared on teardown.
  13. */
  14. let handle: ReplBridgeHandle | null = null
  15. export function setReplBridgeHandle(h: ReplBridgeHandle | null): void {
  16. handle = h
  17. // Publish (or clear) our bridge session ID in the session record so other
  18. // local peers can dedup us out of their bridge list — local is preferred.
  19. void updateSessionBridgeId(getSelfBridgeCompatId() ?? null).catch(() => {})
  20. }
  21. export function getReplBridgeHandle(): ReplBridgeHandle | null {
  22. return handle
  23. }
  24. /**
  25. * Our own bridge session ID in the session_* compat format the API returns
  26. * in /v1/sessions responses — or undefined if bridge isn't connected.
  27. */
  28. export function getSelfBridgeCompatId(): string | undefined {
  29. const h = getReplBridgeHandle()
  30. return h ? toCompatSessionId(h.bridgeSessionId) : undefined
  31. }