bridgePermissionCallbacks.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import type { PermissionUpdate } from '../utils/permissions/PermissionUpdateSchema.js'
  2. type BridgePermissionResponse = {
  3. behavior: 'allow' | 'deny'
  4. updatedInput?: Record<string, unknown>
  5. updatedPermissions?: PermissionUpdate[]
  6. message?: string
  7. }
  8. type BridgePermissionCallbacks = {
  9. sendRequest(
  10. requestId: string,
  11. toolName: string,
  12. input: Record<string, unknown>,
  13. toolUseId: string,
  14. description: string,
  15. permissionSuggestions?: PermissionUpdate[],
  16. blockedPath?: string,
  17. ): void
  18. sendResponse(requestId: string, response: BridgePermissionResponse): void
  19. /** Cancel a pending control_request so the web app can dismiss its prompt. */
  20. cancelRequest(requestId: string): void
  21. onResponse(
  22. requestId: string,
  23. handler: (response: BridgePermissionResponse) => void,
  24. ): () => void // returns unsubscribe
  25. }
  26. /** Type predicate for validating a parsed control_response payload
  27. * as a BridgePermissionResponse. Checks the required `behavior`
  28. * discriminant rather than using an unsafe `as` cast. */
  29. function isBridgePermissionResponse(
  30. value: unknown,
  31. ): value is BridgePermissionResponse {
  32. if (!value || typeof value !== 'object') return false
  33. return (
  34. 'behavior' in value &&
  35. (value.behavior === 'allow' || value.behavior === 'deny')
  36. )
  37. }
  38. export { isBridgePermissionResponse }
  39. export type { BridgePermissionCallbacks, BridgePermissionResponse }