mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
fix(core): broadcast wellknown updates globally and bound HTTP timeouts (#53821)
This commit is contained in:
committed by
GitHub
Unverified
parent
893d0f2726
commit
a13c73bb9e
@@ -60,11 +60,14 @@ export const Event = {
|
||||
Updated: Bus.ephemeral({ type: "wellknown.updated", schema: {} }),
|
||||
}
|
||||
|
||||
const requestTimeout = "15 seconds"
|
||||
|
||||
export const inspect = Effect.fn("WellKnown.inspect")(function* (origin: string) {
|
||||
const url = `${origin.replace(/\/+$/, "")}/.well-known/opencode`
|
||||
const http = HttpClient.filterStatusOk(yield* HttpClient.HttpClient)
|
||||
return yield* http.execute(HttpClientRequest.get(url).pipe(HttpClientRequest.acceptJson)).pipe(
|
||||
Effect.flatMap(HttpClientResponse.schemaBodyJson(Manifest)),
|
||||
Effect.timeout(requestTimeout),
|
||||
Effect.mapError((cause) => new Error(`Failed to load wellknown manifest from ${url}`, { cause })),
|
||||
)
|
||||
})
|
||||
@@ -92,6 +95,7 @@ const resolveEntry = Effect.fnUntraced(function* (entry: Entry, variables: Reado
|
||||
.execute(HttpClientRequest.get(url).pipe(HttpClientRequest.acceptJson, HttpClientRequest.setHeaders(headers)))
|
||||
.pipe(
|
||||
Effect.flatMap(HttpClientResponse.schemaBodyJson(Config)),
|
||||
Effect.timeout(requestTimeout),
|
||||
Effect.mapError((cause) => new Error(`Failed to load wellknown remote config from ${url}`, { cause })),
|
||||
)
|
||||
if (Schema.is(Config)(remote.config)) return [...configs, remote.config]
|
||||
@@ -155,7 +159,7 @@ const layer = Layer.effect(
|
||||
const changed = !isDeepStrictEqual(Ref.getUnsafe(cache), next)
|
||||
if (!changed) return false
|
||||
yield* Ref.set(cache, next)
|
||||
yield* bus.publish(Event.Updated, {})
|
||||
yield* bus.publish(Event.Updated, {}, { global: true })
|
||||
return true
|
||||
},
|
||||
(effect) => lock.withPermit(effect),
|
||||
@@ -174,7 +178,7 @@ const layer = Layer.effect(
|
||||
const origins = Schema.is(Sources)(sources) ? sources : []
|
||||
yield* kv.set(sourcesKey, Array.from(new Set([...origins, origin])))
|
||||
yield* Ref.update(cache, (current) => new Map(current).set(origin, entry))
|
||||
yield* bus.publish(Event.Updated, {})
|
||||
yield* bus.publish(Event.Updated, {}, { global: true })
|
||||
return entry
|
||||
},
|
||||
(effect, _value) => lock.withPermit(effect),
|
||||
@@ -195,7 +199,7 @@ const layer = Layer.effect(
|
||||
next.delete(origin)
|
||||
return next
|
||||
})
|
||||
yield* bus.publish(Event.Updated, {})
|
||||
yield* bus.publish(Event.Updated, {}, { global: true })
|
||||
},
|
||||
(effect, _value) => lock.withPermit(effect),
|
||||
),
|
||||
|
||||
@@ -4,7 +4,10 @@ import { FetchHttpClient } from "effect/http"
|
||||
import { KV } from "@opencode/core/kv"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
import { Bus } from "@opencode/core/bus"
|
||||
import { Location } from "@opencode/core/location"
|
||||
import { AbsolutePath } from "@opencode/core/schema"
|
||||
import { WellKnown } from "@opencode/core/wellknown"
|
||||
import { location } from "./fixture/location"
|
||||
import { testEffect } from "./lib/effect"
|
||||
|
||||
const it = testEffect(FetchHttpClient.layer)
|
||||
@@ -106,11 +109,18 @@ serviceIt.live("refreshes changed manifests", () =>
|
||||
yield* wellknown.add(server.url.origin)
|
||||
expect(yield* wellknown.refresh()).toBe(false)
|
||||
|
||||
const a = Location.Service.of(location({ directory: AbsolutePath.make("/fixture/a") }))
|
||||
const b = Location.Service.of(location({ directory: AbsolutePath.make("/fixture/b") }))
|
||||
const changed = yield* bus
|
||||
.subscribe(WellKnown.Event.Updated)
|
||||
.pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped({ startImmediately: true }))
|
||||
.pipe(
|
||||
Stream.take(1),
|
||||
Stream.runCollect,
|
||||
Effect.provideService(Location.Service, b),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
update()
|
||||
expect(yield* wellknown.refresh()).toBe(true)
|
||||
expect(yield* wellknown.refresh().pipe(Effect.provideService(Location.Service, a))).toBe(true)
|
||||
expect(yield* Fiber.join(changed)).toHaveLength(1)
|
||||
expect(wellknown.snapshot()[0]?.manifest.auth?.command).toEqual(["second"])
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user