diff --git a/packages/opencode/src/server/routes/instance/httpapi/handlers/v2/message.ts b/packages/opencode/src/server/routes/instance/httpapi/handlers/v2/message.ts index c4aae9ce40..c88ffea480 100644 --- a/packages/opencode/src/server/routes/instance/httpapi/handlers/v2/message.ts +++ b/packages/opencode/src/server/routes/instance/httpapi/handlers/v2/message.ts @@ -42,21 +42,23 @@ export const messageHandlers = HttpApiBuilder.group(InstanceHttpApi, "v2.message catch: () => new InvalidCursorError({ message: "Invalid cursor" }), }) const order = decoded?.order ?? ctx.query.order ?? "desc" - const messages = yield* session.messages({ - sessionID: ctx.params.sessionID, - limit: ctx.query.limit ?? DefaultMessagesLimit, - order, - cursor: decoded ? { id: decoded.id, time: decoded.time, direction: decoded.direction } : undefined, - }).pipe( - Effect.catchTag("Session.NotFoundError", (error) => - Effect.fail( - new SessionNotFoundError({ - sessionID: error.sessionID, - message: `Session not found: ${error.sessionID}`, - }), + const messages = yield* session + .messages({ + sessionID: ctx.params.sessionID, + limit: ctx.query.limit ?? DefaultMessagesLimit, + order, + cursor: decoded ? { id: decoded.id, time: decoded.time, direction: decoded.direction } : undefined, + }) + .pipe( + Effect.catchTag("Session.NotFoundError", (error) => + Effect.fail( + new SessionNotFoundError({ + sessionID: error.sessionID, + message: `Session not found: ${error.sessionID}`, + }), + ), ), - ), - ) + ) const first = messages[0] const last = messages.at(-1) return { diff --git a/packages/opencode/src/server/routes/instance/httpapi/handlers/v2/session.ts b/packages/opencode/src/server/routes/instance/httpapi/handlers/v2/session.ts index 34ed4ed984..1fa99ac5bc 100644 --- a/packages/opencode/src/server/routes/instance/httpapi/handlers/v2/session.ts +++ b/packages/opencode/src/server/routes/instance/httpapi/handlers/v2/session.ts @@ -133,11 +133,60 @@ export const sessionHandlers = HttpApiBuilder.group(InstanceHttpApi, "v2.session .handle( "prompt", Effect.fn(function* (ctx) { - return yield* session.prompt({ - sessionID: ctx.params.sessionID, - prompt: ctx.payload.prompt, - delivery: ctx.payload.delivery ?? SessionV2.DefaultDelivery, - }).pipe( + return yield* session + .prompt({ + sessionID: ctx.params.sessionID, + prompt: ctx.payload.prompt, + delivery: ctx.payload.delivery ?? SessionV2.DefaultDelivery, + }) + .pipe( + Effect.catchTag("Session.NotFoundError", (error) => + Effect.fail( + new SessionNotFoundError({ + sessionID: error.sessionID, + message: `Session not found: ${error.sessionID}`, + }), + ), + ), + ) + }), + ) + .handle( + "compact", + Effect.fn(function* (ctx) { + yield* session.compact(ctx.params.sessionID).pipe( + Effect.catchTag("Session.NotFoundError", (error) => + Effect.fail( + new SessionNotFoundError({ + sessionID: error.sessionID, + message: `Session not found: ${error.sessionID}`, + }), + ), + ), + ) + return HttpApiSchema.NoContent.make() + }), + ) + .handle( + "wait", + Effect.fn(function* (ctx) { + yield* session.wait(ctx.params.sessionID).pipe( + Effect.catchTag("Session.NotFoundError", (error) => + Effect.fail( + new SessionNotFoundError({ + sessionID: error.sessionID, + message: `Session not found: ${error.sessionID}`, + }), + ), + ), + ) + return HttpApiSchema.NoContent.make() + }), + ) + .handle( + "context", + Effect.fn(function* (ctx) { + return yield* session.context(ctx.params.sessionID).pipe( Effect.catchTag("Session.NotFoundError", (error) => Effect.fail( new SessionNotFoundError({ @@ -149,58 +198,5 @@ export const sessionHandlers = HttpApiBuilder.group(InstanceHttpApi, "v2.session ) }), ) - .handle( - "compact", - Effect.fn(function* (ctx) { - yield* session - .compact(ctx.params.sessionID) - .pipe( - Effect.catchTag("Session.NotFoundError", (error) => - Effect.fail( - new SessionNotFoundError({ - sessionID: error.sessionID, - message: `Session not found: ${error.sessionID}`, - }), - ), - ), - ) - return HttpApiSchema.NoContent.make() - }), - ) - .handle( - "wait", - Effect.fn(function* (ctx) { - yield* session - .wait(ctx.params.sessionID) - .pipe( - Effect.catchTag("Session.NotFoundError", (error) => - Effect.fail( - new SessionNotFoundError({ - sessionID: error.sessionID, - message: `Session not found: ${error.sessionID}`, - }), - ), - ), - ) - return HttpApiSchema.NoContent.make() - }), - ) - .handle( - "context", - Effect.fn(function* (ctx) { - return yield* session - .context(ctx.params.sessionID) - .pipe( - Effect.catchTag("Session.NotFoundError", (error) => - Effect.fail( - new SessionNotFoundError({ - sessionID: error.sessionID, - message: `Session not found: ${error.sessionID}`, - }), - ), - ), - ) - }), - ) }), ) diff --git a/packages/sdk/js/src/v2/gen/types.gen.ts b/packages/sdk/js/src/v2/gen/types.gen.ts index 8ae5f89e56..498d158e86 100644 --- a/packages/sdk/js/src/v2/gen/types.gen.ts +++ b/packages/sdk/js/src/v2/gen/types.gen.ts @@ -1823,6 +1823,12 @@ export type UnauthorizedError = { message: string } +export type SessionNotFoundError = { + _tag: "SessionNotFoundError" + sessionID: string + message: string +} + export type V2SessionMessagesResponse = { items: Array cursor: { @@ -7174,6 +7180,10 @@ export type V2SessionPromptErrors = { * UnauthorizedError */ 401: UnauthorizedError + /** + * SessionNotFoundError + */ + 404: SessionNotFoundError } export type V2SessionPromptError = V2SessionPromptErrors[keyof V2SessionPromptErrors] @@ -7208,6 +7218,10 @@ export type V2SessionCompactErrors = { * UnauthorizedError */ 401: UnauthorizedError + /** + * SessionNotFoundError + */ + 404: SessionNotFoundError } export type V2SessionCompactError = V2SessionCompactErrors[keyof V2SessionCompactErrors] @@ -7242,6 +7256,10 @@ export type V2SessionWaitErrors = { * UnauthorizedError */ 401: UnauthorizedError + /** + * SessionNotFoundError + */ + 404: SessionNotFoundError } export type V2SessionWaitError = V2SessionWaitErrors[keyof V2SessionWaitErrors] @@ -7276,6 +7294,10 @@ export type V2SessionContextErrors = { * UnauthorizedError */ 401: UnauthorizedError + /** + * SessionNotFoundError + */ + 404: SessionNotFoundError } export type V2SessionContextError = V2SessionContextErrors[keyof V2SessionContextErrors] @@ -7316,6 +7338,10 @@ export type V2SessionMessagesErrors = { * UnauthorizedError */ 401: UnauthorizedError + /** + * SessionNotFoundError + */ + 404: SessionNotFoundError } export type V2SessionMessagesError = V2SessionMessagesErrors[keyof V2SessionMessagesErrors] diff --git a/packages/sdk/openapi.json b/packages/sdk/openapi.json index 3020c0e002..17cd256d6e 100644 --- a/packages/sdk/openapi.json +++ b/packages/sdk/openapi.json @@ -8217,6 +8217,16 @@ } } } + }, + "404": { + "description": "SessionNotFoundError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SessionNotFoundError" + } + } + } } }, "description": "Create a v2 session message and queue it for the agent loop.", @@ -8303,6 +8313,16 @@ } } } + }, + "404": { + "description": "SessionNotFoundError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SessionNotFoundError" + } + } + } } }, "description": "Compact a v2 session conversation.", @@ -8370,6 +8390,16 @@ } } } + }, + "404": { + "description": "SessionNotFoundError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SessionNotFoundError" + } + } + } } }, "description": "Wait for a v2 session agent loop to become idle.", @@ -8447,6 +8477,16 @@ } } } + }, + "404": { + "description": "SessionNotFoundError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SessionNotFoundError" + } + } + } } }, "description": "Retrieve the active context messages for a v2 session (all messages after the last compaction).", @@ -8554,6 +8594,16 @@ } } } + }, + "404": { + "description": "SessionNotFoundError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SessionNotFoundError" + } + } + } } }, "description": "Retrieve projected v2 messages for a session. Items keep the requested order across pages; use cursor.next or cursor.previous to move through the ordered timeline.", @@ -15618,6 +15668,23 @@ "required": ["_tag", "message"], "additionalProperties": false }, + "SessionNotFoundError": { + "type": "object", + "properties": { + "_tag": { + "type": "string", + "enum": ["SessionNotFoundError"] + }, + "sessionID": { + "type": "string" + }, + "message": { + "type": "string" + } + }, + "required": ["_tag", "sessionID", "message"], + "additionalProperties": false + }, "V2SessionMessagesResponse": { "type": "object", "properties": {