Skip to content

Commit 5e7fbeb

Browse files
committed
Revert "fix(server): keep provider lists JSON-safe (anomalyco#26550)"
This reverts commit 6ebd710 as a fallback in case the toPublicInfo fix doesn't fully resolve anomalyco#26546 in production. Standalone revert so it can stack on top of anomalyco#26548 (Hono deletion revert) for a full rollback to v1.14.41 behavior, or merge alone if anomalyco#26550 specifically caused new regressions. DO NOT MERGE unless v1.14.43 produces fresh user reports. Keep this branch fresh until we confirm anomalyco#26550 holds in production.
1 parent 6ebd710 commit 5e7fbeb

5 files changed

Lines changed: 29 additions & 101 deletions

File tree

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -935,16 +935,6 @@ export const ConfigProvidersResult = Schema.Struct({
935935
}).pipe(withStatics((s) => ({ zod: zod(s) })))
936936
export type ConfigProvidersResult = Types.DeepMutable<Schema.Schema.Type<typeof ConfigProvidersResult>>
937937

938-
export function toPublicInfo(provider: Info): Info {
939-
return JSON.parse(
940-
JSON.stringify(provider, (_, value) => {
941-
if (typeof value === "function" || typeof value === "symbol" || value === undefined) return undefined
942-
if (typeof value === "bigint") return value.toString()
943-
return value
944-
}),
945-
)
946-
}
947-
948938
export function defaultModelIDs<T extends { models: Record<string, { id: string }> }>(providers: Record<string, T>) {
949939
return mapValues(providers, (item) => sort(Object.values(item.models))[0].id)
950940
}
@@ -1309,7 +1299,7 @@ const layer: Layer.Layer<
13091299
const options = yield* Effect.promise(() =>
13101300
plugin.auth!.loader!(
13111301
() => bridge.promise(auth.get(providerID).pipe(Effect.orDie)) as any,
1312-
toPublicInfo(database[plugin.auth!.provider]),
1302+
database[plugin.auth!.provider],
13131303
),
13141304
)
13151305
const opts = options ?? {}

‎packages/opencode/src/server/routes/instance/httpapi/handlers/config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const configHandlers = HttpApiBuilder.group(InstanceHttpApi, "config", (h
2424
const providers = Effect.fn("ConfigHttpApi.providers")(function* () {
2525
const providers = yield* providerSvc.list()
2626
return {
27-
providers: Object.values(providers).map(Provider.toPublicInfo),
27+
providers: Object.values(providers),
2828
default: Provider.defaultModelIDs(providers),
2929
}
3030
})

‎packages/opencode/src/server/routes/instance/httpapi/handlers/provider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const providerHandlers = HttpApiBuilder.group(InstanceHttpApi, "provider"
3030
connected,
3131
)
3232
return {
33-
all: Object.values(providers).map(Provider.toPublicInfo),
33+
all: Object.values(providers),
3434
default: Provider.defaultModelIDs(providers),
3535
connected: Object.keys(connected),
3636
}

‎packages/opencode/src/server/routes/instance/httpapi/middleware/error.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,39 @@ import { NamedError } from "@opencode-ai/core/util/error"
66
import * as Log from "@opencode-ai/core/util/log"
77
import { Cause, Effect } from "effect"
88
import { HttpRouter, HttpServerError, HttpServerRespondable, HttpServerResponse } from "effect/unstable/http"
9+
import { HttpApiError } from "effect/unstable/httpapi"
10+
import { HttpApiSchemaError } from "effect/unstable/httpapi/HttpApiError"
911

1012
const log = Log.create({ service: "server" })
1113

14+
function badRequestResponse() {
15+
return HttpServerResponse.jsonUnsafe(
16+
{
17+
data: {},
18+
errors: [],
19+
success: false,
20+
},
21+
{ status: 400 },
22+
)
23+
}
24+
25+
function normalizeEmptyBadRequest(response: HttpServerResponse.HttpServerResponse) {
26+
if (response.status !== 400 || response.body._tag !== "Empty") return response
27+
return badRequestResponse()
28+
}
29+
1230
// Keep typed HttpApi failures on their declared error path; this boundary only replaces defect-only empty 500s.
1331
export const errorLayer = HttpRouter.middleware<{ handles: unknown }>()((effect) =>
1432
effect.pipe(
33+
Effect.catch((error) => {
34+
if (error instanceof HttpApiError.BadRequest) return Effect.succeed(badRequestResponse())
35+
return Effect.fail(error)
36+
}),
37+
Effect.map(normalizeEmptyBadRequest),
1538
Effect.catchCause((cause) => {
39+
const schemaError = cause.reasons.filter(Cause.isDieReason).find((reason) => HttpApiSchemaError.is(reason.defect))
40+
if (schemaError) return Effect.succeed(badRequestResponse())
41+
1642
const defect = cause.reasons.filter(Cause.isDieReason).find((reason) => {
1743
if (HttpServerResponse.isHttpServerResponse(reason.defect)) return false
1844
if (HttpServerError.isHttpServerError(reason.defect)) return false

‎packages/opencode/test/server/httpapi-provider.test.ts‎

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ function app() {
2121
return Server.Default().app
2222
}
2323

24-
function providerListHasFetch(list: unknown) {
25-
if (!Array.isArray(list)) return false
26-
return list.some((item: unknown) => {
27-
if (typeof item !== "object" || item === null || !("id" in item) || !("options" in item)) return false
28-
if (item.id !== "google") return false
29-
if (typeof item.options !== "object" || item.options === null) return false
30-
return "fetch" in item.options
31-
})
32-
}
33-
34-
function hasProviderWithFetch(input: unknown, key: "all" | "providers") {
35-
if (typeof input !== "object" || input === null) return false
36-
if (key === "all") return "all" in input && providerListHasFetch(input.all)
37-
return "providers" in input && providerListHasFetch(input.providers)
38-
}
39-
4024
function requestAuthorize(input: {
4125
app: ReturnType<typeof app>
4226
providerID: string
@@ -92,39 +76,6 @@ function writeProviderAuthPlugin(dir: string) {
9276
})
9377
}
9478

95-
function writeFunctionOptionsPlugin(dir: string) {
96-
return Effect.gen(function* () {
97-
const fs = yield* FileSystem.FileSystem
98-
const path = yield* Path.Path
99-
100-
yield* fs.makeDirectory(path.join(dir, ".opencode", "plugin"), { recursive: true })
101-
yield* fs.writeFileString(
102-
path.join(dir, ".opencode", "plugin", "provider-function-options.ts"),
103-
[
104-
"export default {",
105-
' id: "test.provider-function-options",',
106-
" server: async () => ({",
107-
" auth: {",
108-
' provider: "google",',
109-
" loader: async (_getAuth, provider) => {",
110-
" for (const model of Object.values(provider.models ?? {})) {",
111-
" model.cost = { input: 0, output: 0 }",
112-
" }",
113-
" return {",
114-
' apiKey: "",',
115-
" fetch: async (input, init) => fetch(input, init),",
116-
" }",
117-
" },",
118-
" methods: [{ type: 'api', label: 'API key' }],",
119-
" },",
120-
" }),",
121-
"}",
122-
"",
123-
].join("\n"),
124-
)
125-
})
126-
}
127-
12879
function withProviderProject<A, E, R>(self: (dir: string) => Effect.Effect<A, E, R>) {
12980
return Effect.gen(function* () {
13081
const fs = yield* FileSystem.FileSystem
@@ -185,43 +136,4 @@ describe("provider HttpApi", () => {
185136
}),
186137
),
187138
)
188-
189-
it.live("serves provider lists when auth loaders add runtime fetch options", () =>
190-
Effect.gen(function* () {
191-
const fs = yield* FileSystem.FileSystem
192-
const path = yield* Path.Path
193-
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "opencode-test-" })
194-
const previous = process.env.OPENCODE_AUTH_CONTENT
195-
196-
yield* fs.writeFileString(
197-
path.join(dir, "opencode.json"),
198-
JSON.stringify({ $schema: "https://opencode.ai/config.json", formatter: false, lsp: false }),
199-
)
200-
yield* writeFunctionOptionsPlugin(dir)
201-
yield* Effect.sync(() => {
202-
process.env.OPENCODE_AUTH_CONTENT = JSON.stringify({
203-
google: { type: "oauth", refresh: "dummy", access: "dummy", expires: 9999999999999 },
204-
})
205-
})
206-
yield* Effect.addFinalizer(() =>
207-
Effect.sync(() => {
208-
if (previous === undefined) delete process.env.OPENCODE_AUTH_CONTENT
209-
if (previous !== undefined) process.env.OPENCODE_AUTH_CONTENT = previous
210-
}),
211-
)
212-
const headers = { "x-opencode-directory": dir }
213-
const providerResponse = yield* Effect.promise(() => Promise.resolve(app().request("/provider", { headers })))
214-
const configResponse = yield* Effect.promise(() =>
215-
Promise.resolve(app().request("/config/providers", { headers })),
216-
)
217-
218-
expect(providerResponse.status).toBe(200)
219-
expect(configResponse.status).toBe(200)
220-
221-
const providerBody = yield* Effect.promise(() => providerResponse.json())
222-
const configBody = yield* Effect.promise(() => configResponse.json())
223-
expect(hasProviderWithFetch(providerBody, "all")).toBe(false)
224-
expect(hasProviderWithFetch(configBody, "providers")).toBe(false)
225-
}),
226-
)
227139
})

0 commit comments

Comments
 (0)