Просмотр исходного кода

refactor(config): use effectful HttpApi group builder

Build the config providers HttpApi handler with an effectful group callback so the provider service is resolved once at layer construction time and the endpoint handler closes over the resulting service access.
Kit Langton 3 дней назад
Родитель
Сommit
2a118b5214
1 измененных файлов с 20 добавлено и 13 удалено
  1. 20 13
      packages/opencode/src/server/instance/httpapi/config.ts

+ 20 - 13
packages/opencode/src/server/instance/httpapi/config.ts

@@ -134,20 +134,27 @@ const Api = HttpApi.make("config")
     }),
   )
 
-const providers = Effect.fn("ConfigHttpApi.providers")(function* () {
-  const svc = yield* Provider.Service
-  const all = mapValues(yield* svc.list(), (item) => item)
-  return Schema.decodeUnknownSync(Providers)(
-    JSON.parse(
-      JSON.stringify({
-        providers: Object.values(all),
-        default: mapValues(all, (item) => Provider.sort(Object.values(item.models))[0].id),
-      }),
-    ),
-  )
-})
+const ConfigLive = HttpApiBuilder.group(
+  Api,
+  "config",
+  Effect.fn("ConfigHttpApi.handlers")(function* (handlers) {
+    const svc = yield* Provider.Service
+
+    const providers = Effect.fn("ConfigHttpApi.providers")(function* () {
+      const all = mapValues(yield* svc.list(), (item) => item)
+      return Schema.decodeUnknownSync(Providers)(
+        JSON.parse(
+          JSON.stringify({
+            providers: Object.values(all),
+            default: mapValues(all, (item) => Provider.sort(Object.values(item.models))[0].id),
+          }),
+        ),
+      )
+    })
 
-const ConfigLive = HttpApiBuilder.group(Api, "config", (handlers) => handlers.handle("providers", providers))
+    return handlers.handle("providers", providers)
+  }),
+).pipe(Layer.provide(Provider.defaultLayer))
 
 const web = lazy(() =>
   HttpRouter.toWebHandler(