Skip to content

Commit 30721b8

Browse files
authored
fix(server): await providers before catalog reads (anomalyco#46066)
1 parent eb083cc commit 30721b8

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

‎packages/server/src/handlers/provider.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
import { Catalog } from "@opencode-ai/core/catalog"
2+
import { ProviderNotFoundError, ServiceUnavailableError } from "@opencode-ai/protocol/errors"
23
import { Effect } from "effect"
34
import { HttpApiBuilder } from "effect/unstable/httpapi"
45
import { Api } from "../api"
5-
import { ProviderNotFoundError } from "@opencode-ai/protocol/errors"
66
import { response } from "../location"
7+
import { pluginReadiness } from "./plugin-readiness"
8+
9+
const flushPlugins = pluginReadiness(
10+
() =>
11+
new ServiceUnavailableError({
12+
message: "Provider catalog initialization timed out",
13+
service: "provider.catalog",
14+
}),
15+
)
716

817
export const ProviderHandler = HttpApiBuilder.group(Api, "server.provider", (handlers) =>
918
Effect.gen(function* () {
1019
return handlers
1120
.handle(
1221
"provider.list",
1322
Effect.fn(function* () {
23+
yield* flushPlugins
1424
const catalog = yield* Catalog.Service
1525
return yield* response(catalog.provider.available())
1626
}),
1727
)
1828
.handle(
1929
"provider.get",
2030
Effect.fn(function* (ctx) {
31+
yield* flushPlugins
2132
const catalog = yield* Catalog.Service
2233
const provider = yield* catalog.provider.get(ctx.params.providerID)
2334
if (!provider)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import fs from "node:fs/promises"
2+
import path from "node:path"
3+
import { expect } from "bun:test"
4+
import { Effect } from "effect"
5+
import { tmpdir } from "../../core/test/fixture/tmpdir"
6+
import { it } from "../../core/test/lib/effect"
7+
import { startServer } from "./fixture/server"
8+
9+
it.live(
10+
"waits for plugin initialization on the first provider list request",
11+
() =>
12+
Effect.gen(function* () {
13+
const fixture = yield* configuredProvider("opencode-provider-list-endpoint-")
14+
const url = new URL("/api/provider", fixture.server.base)
15+
url.searchParams.set("location[directory]", fixture.path)
16+
const response = yield* Effect.promise(() => fetch(url, { headers: fixture.server.headers }))
17+
18+
expect(response.status).toBe(200)
19+
const body: unknown = yield* Effect.promise(() => response.json())
20+
if (!isRecord(body) || !Array.isArray(body["data"])) throw new Error("Expected a provider list response")
21+
expect(body["data"].some((provider) => isRecord(provider) && provider["id"] === "custom")).toBeTrue()
22+
}),
23+
15_000,
24+
)
25+
26+
it.live(
27+
"waits for plugin initialization on the first provider get request",
28+
() =>
29+
Effect.gen(function* () {
30+
const fixture = yield* configuredProvider("opencode-provider-get-endpoint-")
31+
const url = new URL("/api/provider/custom", fixture.server.base)
32+
url.searchParams.set("location[directory]", fixture.path)
33+
const response = yield* Effect.promise(() => fetch(url, { headers: fixture.server.headers }))
34+
35+
expect(response.status).toBe(200)
36+
const body: unknown = yield* Effect.promise(() => response.json())
37+
if (!isRecord(body) || !isRecord(body["data"])) throw new Error("Expected a provider response")
38+
expect(body["data"]["id"]).toBe("custom")
39+
}),
40+
15_000,
41+
)
42+
43+
const configuredProvider = Effect.fnUntraced(function* (prefix: string) {
44+
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir(prefix)))
45+
yield* Effect.promise(() =>
46+
fs.writeFile(
47+
path.join(tmp.path, "opencode.json"),
48+
JSON.stringify({
49+
providers: {
50+
custom: {
51+
package: "@opencode-ai/ai/providers/openai-compatible",
52+
settings: { apiKey: "secret" },
53+
models: { chat: {} },
54+
},
55+
},
56+
}),
57+
),
58+
)
59+
return { server: yield* startServer(tmp.path), path: tmp.path }
60+
})
61+
62+
function isRecord(value: unknown): value is Record<string, unknown> {
63+
return typeof value === "object" && value !== null && !Array.isArray(value)
64+
}

0 commit comments

Comments
 (0)