From 1dd5d8e70dd9b04b5d4df4fa09251332e551181d Mon Sep 17 00:00:00 2001
From: Alex Freska <alex.freska@gmail.com>
Date: Tue, 26 Nov 2024 11:17:33 -0500
Subject: [PATCH] refactor(renterd-libs): move autopilot routes

---
 .changeset/selfish-queens-fold.md      |  7 +++++
 .changeset/ten-lies-draw.md            |  7 +++++
 apps/renterd/contexts/hosts/dataset.ts |  2 +-
 libs/renterd-js/README.md              |  1 -
 libs/renterd-js/src/autopilot.ts       | 20 ++----------
 libs/renterd-js/src/bus.ts             | 17 +++++++++++
 libs/renterd-js/src/example.ts         |  1 -
 libs/renterd-react/src/autopilot.ts    | 42 ++------------------------
 libs/renterd-react/src/bus.ts          | 37 +++++++++++++++++++++++
 libs/renterd-types/src/autopilot.ts    |  3 +-
 libs/renterd-types/src/types.ts        |  4 +--
 11 files changed, 76 insertions(+), 65 deletions(-)
 create mode 100644 .changeset/selfish-queens-fold.md
 create mode 100644 .changeset/ten-lies-draw.md

diff --git a/.changeset/selfish-queens-fold.md b/.changeset/selfish-queens-fold.md
new file mode 100644
index 000000000..47c9b85c1
--- /dev/null
+++ b/.changeset/selfish-queens-fold.md
@@ -0,0 +1,7 @@
+---
+'@siafoundation/renterd-js': minor
+'@siafoundation/renterd-react': minor
+'@siafoundation/renterd-types': minor
+---
+
+The host checks field is now just HostAutopilotChecks.
diff --git a/.changeset/ten-lies-draw.md b/.changeset/ten-lies-draw.md
new file mode 100644
index 000000000..e105a2397
--- /dev/null
+++ b/.changeset/ten-lies-draw.md
@@ -0,0 +1,7 @@
+---
+'@siafoundation/renterd-js': minor
+'@siafoundation/renterd-react': minor
+'@siafoundation/renterd-types': minor
+---
+
+Autopilot config routes moved to the bus.
diff --git a/apps/renterd/contexts/hosts/dataset.ts b/apps/renterd/contexts/hosts/dataset.ts
index 2132efa96..26ca7c430 100644
--- a/apps/renterd/contexts/hosts/dataset.ts
+++ b/apps/renterd/contexts/hosts/dataset.ts
@@ -42,7 +42,7 @@ export function useDataset({
           blocklist: block,
           isAllowlistActive,
         }),
-        ...getAutopilotFields(host.checks?.autopilot),
+        ...getAutopilotFields(host.checks),
         location: sch?.location,
         countryCode: sch?.country_code,
         // selectable
diff --git a/libs/renterd-js/README.md b/libs/renterd-js/README.md
index 5bd2e8983..13feb95e7 100644
--- a/libs/renterd-js/README.md
+++ b/libs/renterd-js/README.md
@@ -53,7 +53,6 @@ export async function example() {
 
   const hosts = await bus.hosts({
     data: {
-      autopilotID: 'autopilot',
       filterMode: 'allowed',
       usabilityMode: 'usable',
       addressContains: 'example.com',
diff --git a/libs/renterd-js/src/autopilot.ts b/libs/renterd-js/src/autopilot.ts
index 49f4077d9..74c42612d 100644
--- a/libs/renterd-js/src/autopilot.ts
+++ b/libs/renterd-js/src/autopilot.ts
@@ -2,21 +2,15 @@ import {
   AutopilotConfigEvaluateParams,
   AutopilotConfigEvaluatePayload,
   AutopilotConfigEvaluateResponse,
-  AutopilotConfigParams,
-  AutopilotConfigPayload,
-  AutopilotConfigResponse,
-  AutopilotConfigUpdateParams,
-  AutopilotConfigUpdatePayload,
-  AutopilotConfigUpdateResponse,
   AutopilotStateParams,
   AutopilotStatePayload,
   AutopilotStateResponse,
   AutopilotTriggerParams,
   AutopilotTriggerPayload,
   AutopilotTriggerResponse,
-  autopilotConfigRoute,
   autopilotStateRoute,
   autopilotTriggerRoute,
+  autopilotConfigEvaluateRoute,
 } from '@siafoundation/renterd-types'
 import { buildRequestHandler, initAxios } from '@siafoundation/request'
 
@@ -35,21 +29,11 @@ export function Autopilot({
       AutopilotStatePayload,
       AutopilotStateResponse
     >(axios, 'get', autopilotStateRoute),
-    config: buildRequestHandler<
-      AutopilotConfigParams,
-      AutopilotConfigPayload,
-      AutopilotConfigResponse
-    >(axios, 'get', autopilotConfigRoute),
-    configUpdate: buildRequestHandler<
-      AutopilotConfigUpdateParams,
-      AutopilotConfigUpdatePayload,
-      AutopilotConfigUpdateResponse
-    >(axios, 'put', autopilotConfigRoute),
     configEvaluate: buildRequestHandler<
       AutopilotConfigEvaluateParams,
       AutopilotConfigEvaluatePayload,
       AutopilotConfigEvaluateResponse
-    >(axios, 'post', autopilotConfigRoute),
+    >(axios, 'post', autopilotConfigEvaluateRoute),
     trigger: buildRequestHandler<
       AutopilotTriggerParams,
       AutopilotTriggerPayload,
diff --git a/libs/renterd-js/src/bus.ts b/libs/renterd-js/src/bus.ts
index 9da3ec8fb..6c8e1f0a2 100644
--- a/libs/renterd-js/src/bus.ts
+++ b/libs/renterd-js/src/bus.ts
@@ -248,6 +248,13 @@ import {
   HostScanPayload,
   HostScanResponse,
   busHostHostKeyScanRoute,
+  AutopilotConfigParams,
+  AutopilotConfigPayload,
+  AutopilotConfigResponse,
+  AutopilotConfigUpdateParams,
+  AutopilotConfigUpdatePayload,
+  AutopilotConfigUpdateResponse,
+  busAutopilotRoute,
 } from '@siafoundation/renterd-types'
 import { buildRequestHandler, initAxios } from '@siafoundation/request'
 
@@ -580,5 +587,15 @@ export function Bus({ api, password }: { api: string; password?: string }) {
       MultipartUploadAddPartPayload,
       MultipartUploadAddPartResponse
     >(axios, 'post', busMultipartPartRoute),
+    autopilotConfig: buildRequestHandler<
+      AutopilotConfigParams,
+      AutopilotConfigPayload,
+      AutopilotConfigResponse
+    >(axios, 'get', busAutopilotRoute),
+    autopilotConfigUpdate: buildRequestHandler<
+      AutopilotConfigUpdateParams,
+      AutopilotConfigUpdatePayload,
+      AutopilotConfigUpdateResponse
+    >(axios, 'put', busAutopilotRoute),
   }
 }
diff --git a/libs/renterd-js/src/example.ts b/libs/renterd-js/src/example.ts
index 019bc3cbc..9b3339138 100644
--- a/libs/renterd-js/src/example.ts
+++ b/libs/renterd-js/src/example.ts
@@ -42,7 +42,6 @@ export async function example() {
 
   const hosts = await bus.hosts({
     data: {
-      autopilotID: 'autopilot-id',
       filterMode: 'allowed',
       usabilityMode: 'usable',
       addressContains: 'example.com',
diff --git a/libs/renterd-react/src/autopilot.ts b/libs/renterd-react/src/autopilot.ts
index bdccf73e7..eaab9c51b 100644
--- a/libs/renterd-react/src/autopilot.ts
+++ b/libs/renterd-react/src/autopilot.ts
@@ -1,19 +1,12 @@
 import {
   useGetSwr,
   usePostSwr,
-  usePutFunc,
   HookArgsSwr,
   HookArgsCallback,
   HookArgsWithPayloadSwr,
-  delay,
   usePostFunc,
 } from '@siafoundation/react-core'
 import {
-  AutopilotConfigParams,
-  AutopilotConfigResponse,
-  AutopilotConfigUpdateParams,
-  AutopilotConfigUpdatePayload,
-  AutopilotConfigUpdateResponse,
   AutopilotConfigEvaluateParams,
   AutopilotConfigEvaluatePayload,
   AutopilotConfigEvaluateResponse,
@@ -22,9 +15,9 @@ import {
   AutopilotTriggerParams,
   AutopilotTriggerPayload,
   AutopilotTriggerResponse,
-  autopilotConfigRoute,
   autopilotStateRoute,
   autopilotTriggerRoute,
+  autopilotConfigEvaluateRoute,
 } from '@siafoundation/renterd-types'
 
 export function useAutopilotState(
@@ -36,37 +29,6 @@ export function useAutopilotState(
   })
 }
 
-export function useAutopilotConfig(
-  args?: HookArgsSwr<AutopilotConfigParams, AutopilotConfigResponse>
-) {
-  return useGetSwr({
-    ...args,
-    route: autopilotConfigRoute,
-  })
-}
-
-export function useAutopilotConfigUpdate(
-  args?: HookArgsCallback<
-    AutopilotConfigUpdateParams,
-    AutopilotConfigUpdatePayload,
-    AutopilotConfigUpdateResponse
-  >
-) {
-  return usePutFunc(
-    { ...args, route: autopilotConfigRoute },
-    async (mutate) => {
-      mutate((key) => key === autopilotConfigRoute)
-      // Might need a delay before revalidating status which returns whether
-      // or not autopilot is configured.
-      const func = async () => {
-        await delay(1000)
-        mutate((key) => key === autopilotStateRoute)
-      }
-      func()
-    }
-  )
-}
-
 export function useAutopilotConfigEvaluate(
   args: HookArgsWithPayloadSwr<
     AutopilotConfigEvaluateParams,
@@ -74,7 +36,7 @@ export function useAutopilotConfigEvaluate(
     AutopilotConfigEvaluateResponse
   >
 ) {
-  return usePostSwr({ ...args, route: autopilotConfigRoute })
+  return usePostSwr({ ...args, route: autopilotConfigEvaluateRoute })
 }
 
 export function useAutopilotTrigger(
diff --git a/libs/renterd-react/src/bus.ts b/libs/renterd-react/src/bus.ts
index d86407343..086849eec 100644
--- a/libs/renterd-react/src/bus.ts
+++ b/libs/renterd-react/src/bus.ts
@@ -21,6 +21,12 @@ import {
   AlertsDismissResponse,
   AlertsParams,
   AlertsResponse,
+  AutopilotConfigParams,
+  AutopilotConfigResponse,
+  AutopilotConfigUpdateParams,
+  AutopilotConfigUpdatePayload,
+  AutopilotConfigUpdateResponse,
+  busAutopilotRoute,
   BucketCreateParams,
   BucketCreatePayload,
   BucketCreateResponse,
@@ -241,6 +247,7 @@ import {
   HostScanResponse,
   busHostHostKeyScanRoute,
   Host,
+  autopilotStateRoute,
 } from '@siafoundation/renterd-types'
 
 // state
@@ -1007,3 +1014,33 @@ export function useMultipartUploadAddPart(
     }
   )
 }
+
+// autopilot
+
+export function useAutopilotConfig(
+  args?: HookArgsSwr<AutopilotConfigParams, AutopilotConfigResponse>
+) {
+  return useGetSwr({
+    ...args,
+    route: busAutopilotRoute,
+  })
+}
+
+export function useAutopilotConfigUpdate(
+  args?: HookArgsCallback<
+    AutopilotConfigUpdateParams,
+    AutopilotConfigUpdatePayload,
+    AutopilotConfigUpdateResponse
+  >
+) {
+  return usePutFunc({ ...args, route: busAutopilotRoute }, async (mutate) => {
+    mutate((key) => key === busAutopilotRoute)
+    // Might need a delay before revalidating status which returns whether
+    // or not autopilot is configured.
+    const func = async () => {
+      await delay(1000)
+      mutate((key) => key === autopilotStateRoute)
+    }
+    func()
+  })
+}
diff --git a/libs/renterd-types/src/autopilot.ts b/libs/renterd-types/src/autopilot.ts
index de0da424e..8ab9766e0 100644
--- a/libs/renterd-types/src/autopilot.ts
+++ b/libs/renterd-types/src/autopilot.ts
@@ -2,8 +2,9 @@ import { BuildState } from './bus'
 import { AutopilotConfig, SettingsGouging, SettingsRedundancy } from './types'
 
 export const autopilotStateRoute = '/autopilot/state'
-export const autopilotConfigRoute = '/autopilot/config'
+export const autopilotConfigEvaluateRoute = '/autopilot/config/evaluate'
 export const autopilotTriggerRoute = '/autopilot/trigger'
+export const busAutopilotRoute = '/bus/autopilot'
 
 type AutopilotStatus = {
   configured: boolean
diff --git a/libs/renterd-types/src/types.ts b/libs/renterd-types/src/types.ts
index 247bdb08d..37632a612 100644
--- a/libs/renterd-types/src/types.ts
+++ b/libs/renterd-types/src/types.ts
@@ -184,9 +184,7 @@ export type Host = {
   storedData: number
   resolvedAddresses?: string[]
   subnets?: string[]
-  checks?: {
-    autopilot: HostAutopilotChecks
-  }
+  checks?: HostAutopilotChecks
 }
 
 export type HostAutopilotChecks = {