pairing.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import type { JsonValue } from '@shared/types'
  2. import { apiRequest } from './client'
  3. export type NodeScope =
  4. | 'agents.read'
  5. | 'history.read'
  6. | 'sessions.execute'
  7. | 'groups.read'
  8. | 'groups.execute'
  9. export interface PairingSession {
  10. protocolVersion: number
  11. pairingId: string
  12. nodeId: string
  13. fingerprint: string
  14. scopes: NodeScope[]
  15. expiresAt: number
  16. qrPayload: string
  17. }
  18. export interface PairedDevice {
  19. id: string
  20. name: string
  21. scopes: NodeScope[]
  22. createdAt: number
  23. lastUsedAt: number
  24. }
  25. export interface PairedDevicesResponse {
  26. nodeId: string
  27. fingerprint: string
  28. devices: PairedDevice[]
  29. }
  30. export async function createPairing(
  31. scopes: NodeScope[],
  32. ): Promise<PairingSession> {
  33. return apiRequest<PairingSession>('/api/app/pairings', {
  34. method: 'POST',
  35. body: { scopes } as unknown as JsonValue,
  36. })
  37. }
  38. export async function pairChildNode(qrPayload: string, name?: string): Promise<void> {
  39. await apiRequest('/api/app/nodes', {
  40. method: 'POST', body: { qrPayload, name: name || '' } as unknown as JsonValue,
  41. })
  42. }
  43. export async function pairingStatus(
  44. pairingId: string,
  45. ): Promise<{ state: 'pending' | 'claimed' | 'expired'; deviceID?: string }> {
  46. return apiRequest(`/api/app/pairings/${encodeURIComponent(pairingId)}`)
  47. }
  48. export async function pairedDevices(): Promise<PairedDevicesResponse> {
  49. return apiRequest('/api/app/paired-devices')
  50. }
  51. export async function revokePairedDevice(deviceId: string): Promise<void> {
  52. await apiRequest(`/api/app/paired-devices/${encodeURIComponent(deviceId)}`, {
  53. method: 'DELETE', body: {} as JsonValue,
  54. })
  55. }