humanRelay.ts 848 B

1234567891011121314151617181920212223242526
  1. // Callback mapping of human relay response.
  2. const humanRelayCallbacks = new Map<string, (response: string | undefined) => void>()
  3. /**
  4. * Register a callback function for human relay response.
  5. * @param requestId
  6. * @param callback
  7. */
  8. export const registerHumanRelayCallback = (requestId: string, callback: (response: string | undefined) => void) =>
  9. humanRelayCallbacks.set(requestId, callback)
  10. export const unregisterHumanRelayCallback = (requestId: string) => humanRelayCallbacks.delete(requestId)
  11. export const handleHumanRelayResponse = (response: { requestId: string; text?: string; cancelled?: boolean }) => {
  12. const callback = humanRelayCallbacks.get(response.requestId)
  13. if (callback) {
  14. if (response.cancelled) {
  15. callback(undefined)
  16. } else {
  17. callback(response.text)
  18. }
  19. humanRelayCallbacks.delete(response.requestId)
  20. }
  21. }