Skip to content

Commit 55ecb06

Browse files
authored
fix(httpapi): accept empty session create body (#24640)
1 parent dc6991e commit 55ecb06

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

packages/opencode/src/server/routes/instance/httpapi/session.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export const SessionApi = HttpApi.make("session")
203203
}),
204204
),
205205
HttpApiEndpoint.post("create", SessionPaths.create, {
206-
payload: Session.CreateInput,
206+
payload: [HttpApiSchema.NoContent, Session.CreateInput],
207207
success: Session.Info,
208208
}).annotateMerge(
209209
OpenApi.annotations({
@@ -513,7 +513,7 @@ export const sessionHandlers = Layer.unwrap(
513513
)
514514
})
515515

516-
const create = Effect.fn("SessionHttpApi.create")(function* (ctx: { payload: Session.CreateInput }) {
516+
const create = Effect.fn("SessionHttpApi.create")(function* (ctx: { payload?: Session.CreateInput }) {
517517
const instance = yield* InstanceState.context
518518
return yield* Effect.promise(() =>
519519
Instance.restore(instance, () =>
@@ -524,6 +524,22 @@ export const sessionHandlers = Layer.unwrap(
524524
)
525525
})
526526

527+
const createRaw = Effect.fn("SessionHttpApi.createRaw")(function* (ctx: {
528+
request: HttpServerRequest.HttpServerRequest
529+
}) {
530+
const body = yield* Effect.orDie(ctx.request.text)
531+
if (body.trim().length === 0) return yield* create({})
532+
533+
const json = yield* Effect.try({
534+
try: () => JSON.parse(body) as unknown,
535+
catch: () => new HttpApiError.BadRequest({}),
536+
})
537+
const payload = yield* Schema.decodeUnknownEffect(Session.CreateInput)(json).pipe(
538+
Effect.mapError(() => new HttpApiError.BadRequest({})),
539+
)
540+
return yield* create({ payload })
541+
})
542+
527543
const remove = Effect.fn("SessionHttpApi.remove")(function* (ctx: { params: { sessionID: SessionID } }) {
528544
const instance = yield* InstanceState.context
529545
yield* Effect.promise(() =>
@@ -894,7 +910,7 @@ export const sessionHandlers = Layer.unwrap(
894910
.handle("diff", diff)
895911
.handle("messages", messages)
896912
.handle("message", message)
897-
.handle("create", create)
913+
.handleRaw("create", createRaw)
898914
.handle("remove", remove)
899915
.handle("update", update)
900916
.handle("fork", fork)

packages/opencode/test/server/httpapi-session.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ describe("session HttpApi", () => {
151151
await using tmp = await tmpdir({ git: true, config: { formatter: false, lsp: false, share: "disabled" } })
152152
const headers = { "x-opencode-directory": tmp.path, "content-type": "application/json" }
153153

154+
const createdEmpty = await json<Session.Info>(
155+
await app().request(SessionPaths.create, {
156+
method: "POST",
157+
headers,
158+
}),
159+
)
160+
expect(createdEmpty.id).toBeTruthy()
161+
154162
const created = await json<Session.Info>(
155163
await app().request(SessionPaths.create, {
156164
method: "POST",

0 commit comments

Comments
 (0)