2026-06-25 02:15:31 +00:00
import { Session } from "@opencode-ai/schema/session"
import { SessionMessage } from "@opencode-ai/schema/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-07-01 17:53:09 +00:00
import { InvalidCursorError , SessionNotFoundError , UnknownError } from "../errors.js"
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-25 02:15:31 +00:00
params : { sessionID : Session.ID } ,
2026-06-07 03:27:28 +00:00
query : SessionMessagesQuery ,
2026-05-03 02:09:48 +00:00
success : Schema.Struct ( {
2026-07-08 02:10:11 +00:00
data : Schema.Array ( SessionMessage . Info ) ,
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-25 02:15:31 +00:00
} ) . annotateMerge (
OpenApi . annotations ( {
2026-07-07 23:56:47 +00:00
identifier : "v2.message.list" ,
2026-06-25 02:15:31 +00:00
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-07-06 05:29:08 +00:00
title : "session" ,
2026-06-07 03:27:28 +00:00
description : "Experimental message routes." ,
2026-05-03 02:09:48 +00:00
} ) ,
)