2026-06-04 06:57:43 +00:00
import { SessionV2 } from "@opencode-ai/core/session"
2026-05-31 01:08:38 +00:00
import { SessionMessage } from "@opencode-ai/core/session/message"
2026-05-03 02:09:48 +00:00
import { Schema } from "effect"
2026-05-20 17:53:23 +00:00
import { HttpApiEndpoint , HttpApiGroup , OpenApi } from "effect/unstable/httpapi"
2026-06-07 03:27:28 +00:00
import { InvalidCursorError , SessionNotFoundError , UnknownError } from "../errors"
import { SessionLocationMiddleware } from "../middleware/session-location"
2026-05-10 01:12:34 +00:00
2026-06-07 03:27:28 +00:00
export const SessionMessagesQuery = Schema . Struct ( {
2026-05-10 01:12:34 +00:00
limit : Schema.optional (
Schema . NumberFromString . check ( Schema . isInt ( ) , Schema . isGreaterThanOrEqualTo ( 1 ) , Schema . isLessThanOrEqualTo ( 200 ) ) ,
) . annotate ( {
description : "Maximum number of messages to return. When omitted, the endpoint returns its default page size." ,
} ) ,
order : Schema.optional ( Schema . Union ( [ Schema . Literal ( "asc" ) , Schema . Literal ( "desc" ) ] ) ) . annotate ( {
description : "Message order for the first page. Use desc for newest first or asc for oldest first." ,
} ) ,
cursor : Schema.optional (
Schema . String . annotate ( {
description :
"Opaque pagination cursor returned as cursor.previous or cursor.next in the previous response. Do not combine with order." ,
} ) ,
) ,
2026-06-07 03:27:28 +00:00
} ) . annotate ( { identifier : "SessionMessagesQuery" } )
2026-05-03 02:09:48 +00:00
2026-06-07 03:27:28 +00:00
export const MessageGroup = HttpApiGroup . make ( "server.message" )
2026-05-03 02:09:48 +00:00
. add (
2026-06-07 03:27:28 +00:00
HttpApiEndpoint . get ( "session.messages" , "/api/session/:sessionID/message" , {
2026-06-04 06:57:43 +00:00
params : { sessionID : SessionV2.ID } ,
2026-06-07 03:27:28 +00:00
query : SessionMessagesQuery ,
2026-05-03 02:09:48 +00:00
success : Schema.Struct ( {
2026-06-04 06:57:43 +00:00
data : Schema.Array ( SessionMessage . Message ) ,
2026-05-03 02:09:48 +00:00
cursor : Schema.Struct ( {
previous : Schema.String.pipe ( Schema . optional ) ,
next : Schema.String.pipe ( Schema . optional ) ,
} ) ,
2026-06-07 03:27:28 +00:00
} ) . annotate ( { identifier : "SessionMessagesResponse" } ) ,
2026-05-21 11:00:04 +00:00
error : [ InvalidCursorError , SessionNotFoundError , UnknownError ] ,
2026-06-07 03:27:28 +00:00
} )
. middleware ( SessionLocationMiddleware )
. annotateMerge (
OpenApi . annotations ( {
identifier : "v2.session.messages" ,
summary : "Get session messages" ,
description :
"Retrieve projected messages for a session. Items keep the requested order across pages; use cursor.next or cursor.previous to move through the ordered timeline." ,
} ) ,
) ,
2026-05-03 02:09:48 +00:00
)
. annotateMerge (
OpenApi . annotations ( {
2026-06-07 03:27:28 +00:00
title : "messages" ,
description : "Experimental message routes." ,
2026-05-03 02:09:48 +00:00
} ) ,
)