Skip to content

Commit 2a36236

Browse files
authored
fix(opencode): normalize upgrade endpoint (anomalyco#44686)
1 parent 105b398 commit 2a36236

5 files changed

Lines changed: 51 additions & 54 deletions

File tree

‎packages/opencode/src/server/routes/instance/httpapi/groups/global.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { InstanceDisposed } from "@/server/event"
55
import "@opencode-ai/core/account"
66
import "@/server/event"
77
import { Schema } from "effect"
8-
import { HttpApi, HttpApiEndpoint, HttpApiError, HttpApiGroup, HttpApiSchema, OpenApi } from "effect/unstable/httpapi"
8+
import { HttpApi, HttpApiEndpoint, HttpApiError, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
9+
import semver from "semver"
910
import { described } from "./metadata"
1011

1112
const GlobalHealth = Schema.Struct({
@@ -48,7 +49,9 @@ const GlobalEventSchema = Schema.Struct({
4849
}).annotate({ identifier: "GlobalEvent" })
4950

5051
export const GlobalUpgradeInput = Schema.Struct({
51-
target: Schema.optional(Schema.String),
52+
target: Schema.String.check(
53+
Schema.makeFilter((value) => (semver.valid(value) === null ? "Expected a semantic version" : undefined)),
54+
),
5255
})
5356

5457
const GlobalUpgradeResult = Schema.Union([
@@ -121,14 +124,14 @@ export const GlobalApi = HttpApi.make("global").add(
121124
}),
122125
),
123126
HttpApiEndpoint.post("upgrade", GlobalPaths.upgrade, {
124-
payload: [HttpApiSchema.NoContent, GlobalUpgradeInput],
127+
payload: GlobalUpgradeInput,
125128
success: described(GlobalUpgradeResult, "Upgrade result"),
126129
error: HttpApiError.BadRequest,
127130
}).annotateMerge(
128131
OpenApi.annotations({
129132
identifier: "global.upgrade",
130133
summary: "Upgrade opencode",
131-
description: "Upgrade opencode to the specified version or latest if not specified.",
134+
description: "Upgrade opencode to the specified version.",
132135
}),
133136
),
134137
)

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

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { EventV2 } from "@opencode-ai/core/event"
55
import { Installation } from "@/installation"
66
import { disposeAllInstancesAndEmitGlobalDisposed } from "@/server/global-lifecycle"
77
import { InstallationVersion } from "@opencode-ai/core/installation/version"
8-
import { Effect, Queue, Schema } from "effect"
8+
import { Effect, Queue } from "effect"
99
import * as Stream from "effect/Stream"
10-
import { HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
10+
import { HttpServerResponse } from "effect/unstable/http"
1111
import { HttpApiBuilder } from "effect/unstable/httpapi"
1212
import * as Sse from "effect/unstable/encoding/Sse"
1313
import { RootHttpApi } from "../api"
@@ -22,14 +22,6 @@ function eventData(data: unknown): Sse.Event {
2222
}
2323
}
2424

25-
function parseBody(body: string) {
26-
try {
27-
return JSON.parse(body || "{}") as unknown
28-
} catch {
29-
return undefined
30-
}
31-
}
32-
3325
function eventResponse() {
3426
return Effect.gen(function* () {
3527
yield* Effect.logInfo("global event connected")
@@ -97,52 +89,30 @@ export const globalHandlers = HttpApiBuilder.group(RootHttpApi, "global", (handl
9789
const upgrade = Effect.fn("GlobalHttpApi.upgrade")(function* (ctx: { payload: typeof GlobalUpgradeInput.Type }) {
9890
const method = yield* installation.method()
9991
if (method === "unknown") {
100-
return {
101-
status: 400,
102-
body: { success: false as const, error: "Unknown installation method" },
103-
}
92+
return HttpServerResponse.jsonUnsafe(
93+
{ success: false as const, error: "Unknown installation method" },
94+
{ status: 400 },
95+
)
10496
}
105-
const target = ctx.payload.target || (yield* installation.latest(method))
97+
const target = ctx.payload.target
10698
const result = yield* installation.upgrade(method, target).pipe(
107-
Effect.as({ status: 200, body: { success: true as const, version: target } }),
99+
Effect.as({ success: true as const, version: target }),
108100
Effect.catch((err) =>
109101
Effect.succeed({
110-
status: 500,
111-
body: {
112-
success: false as const,
113-
error: err instanceof Error ? err.message : String(err),
114-
},
102+
success: false as const,
103+
error: err instanceof Error ? err.message : String(err),
115104
}),
116105
),
117106
)
118-
if (!result.body.success) return result
107+
if (!result.success) return HttpServerResponse.jsonUnsafe(result, { status: 500 })
119108
GlobalBus.emit("event", {
120109
directory: "global",
121110
payload: {
122111
type: Installation.Event.Updated.type,
123112
properties: { version: target },
124113
},
125114
})
126-
return result
127-
})
128-
129-
const upgradeRaw = Effect.fn("GlobalHttpApi.upgradeRaw")(function* (ctx: {
130-
request: HttpServerRequest.HttpServerRequest
131-
}) {
132-
const body = yield* Effect.orDie(ctx.request.text)
133-
const json = parseBody(body)
134-
if (json === undefined) {
135-
return HttpServerResponse.jsonUnsafe({ success: false, error: "Invalid request body" }, { status: 400 })
136-
}
137-
const payload = yield* Schema.decodeUnknownEffect(GlobalUpgradeInput)(json).pipe(
138-
Effect.map((payload) => ({ valid: true as const, payload })),
139-
Effect.catch(() => Effect.succeed({ valid: false as const })),
140-
)
141-
if (!payload.valid) {
142-
return HttpServerResponse.jsonUnsafe({ success: false, error: "Invalid request body" }, { status: 400 })
143-
}
144-
const result = yield* upgrade({ payload: payload.payload })
145-
return HttpServerResponse.jsonUnsafe(result.body, { status: result.status })
115+
return HttpServerResponse.jsonUnsafe(result)
146116
})
147117

148118
return handlers
@@ -151,6 +121,6 @@ export const globalHandlers = HttpApiBuilder.group(RootHttpApi, "global", (handl
151121
.handle("configGet", configGet)
152122
.handle("configUpdate", configUpdate)
153123
.handle("dispose", dispose)
154-
.handleRaw("upgrade", upgradeRaw)
124+
.handle("upgrade", upgrade)
155125
}),
156126
)

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,48 @@ const apiLayer = HttpRouter.serve(
4343
const it = testEffect(apiLayer)
4444

4545
describe("global HttpApi", () => {
46-
it.live("upgrades to latest when the request body is omitted", () =>
46+
it.live("upgrades to the requested version", () =>
4747
Effect.gen(function* () {
48-
const response = yield* HttpClient.post(GlobalPaths.upgrade)
48+
const response = yield* HttpClientRequest.post(GlobalPaths.upgrade).pipe(
49+
HttpClientRequest.bodyJsonUnsafe({ target: "9.9.9" }),
50+
HttpClient.execute,
51+
)
4952

5053
expect(response.status).toBe(200)
5154
expect(yield* response.json).toEqual({ success: true, version: "9.9.9" })
5255
}),
5356
)
5457

55-
it.live("rejects malformed upgrade payloads", () =>
58+
it.live("rejects invalid upgrade payloads", () =>
59+
Effect.gen(function* () {
60+
const response = yield* HttpClientRequest.post(GlobalPaths.upgrade).pipe(
61+
HttpClientRequest.bodyJsonUnsafe({ target: 1 }),
62+
HttpClient.execute,
63+
)
64+
65+
expect(response.status).toBe(400)
66+
}),
67+
)
68+
69+
it.live("rejects invalid upgrade target versions", () =>
5670
Effect.gen(function* () {
5771
const response = yield* HttpClientRequest.post(GlobalPaths.upgrade).pipe(
58-
HttpClientRequest.setBody(HttpBody.text("{", "application/json")),
72+
HttpClientRequest.bodyJsonUnsafe({ target: "latest" }),
5973
HttpClient.execute,
6074
)
6175

6276
expect(response.status).toBe(400)
63-
expect(yield* response.json).toEqual({ success: false, error: "Invalid request body" })
77+
}),
78+
)
79+
80+
it.live("rejects unsupported upgrade content types", () =>
81+
Effect.gen(function* () {
82+
const response = yield* HttpClientRequest.post(GlobalPaths.upgrade).pipe(
83+
HttpClientRequest.setBody(HttpBody.text('{"target":"1.0.0"}', "text/plain")),
84+
HttpClient.execute,
85+
)
86+
87+
expect(response.status).toBe(415)
6488
}),
6589
)
6690
})

‎packages/sdk/js/src/v2/gen/sdk.gen.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ export class Global extends HeyApiClient {
13551355
/**
13561356
* Upgrade opencode
13571357
*
1358-
* Upgrade opencode to the specified version or latest if not specified.
1358+
* Upgrade opencode to the specified version.
13591359
*/
13601360
public upgrade<ThrowOnError extends boolean = false>(
13611361
parameters?: {

‎packages/sdk/js/src/v2/gen/types.gen.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7353,7 +7353,7 @@ export type GlobalDisposeResponse = GlobalDisposeResponses[keyof GlobalDisposeRe
73537353

73547354
export type GlobalUpgradeData = {
73557355
body?: {
7356-
target?: string
7356+
target: string
73577357
}
73587358
path?: never
73597359
query?: never

0 commit comments

Comments
 (0)