message.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { SessionV2 } from "@opencode-ai/core/session"
  2. import { SessionMessage } from "@opencode-ai/core/session/message"
  3. import { Schema } from "effect"
  4. import { HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
  5. import { InvalidCursorError, SessionNotFoundError, UnknownError } from "../errors"
  6. import { SessionLocationMiddleware } from "../middleware/session-location"
  7. export const SessionMessagesQuery = Schema.Struct({
  8. limit: Schema.optional(
  9. Schema.NumberFromString.check(Schema.isInt(), Schema.isGreaterThanOrEqualTo(1), Schema.isLessThanOrEqualTo(200)),
  10. ).annotate({
  11. description: "Maximum number of messages to return. When omitted, the endpoint returns its default page size.",
  12. }),
  13. order: Schema.optional(Schema.Union([Schema.Literal("asc"), Schema.Literal("desc")])).annotate({
  14. description: "Message order for the first page. Use desc for newest first or asc for oldest first.",
  15. }),
  16. cursor: Schema.optional(
  17. Schema.String.annotate({
  18. description:
  19. "Opaque pagination cursor returned as cursor.previous or cursor.next in the previous response. Do not combine with order.",
  20. }),
  21. ),
  22. }).annotate({ identifier: "SessionMessagesQuery" })
  23. export const MessageGroup = HttpApiGroup.make("server.message")
  24. .add(
  25. HttpApiEndpoint.get("session.messages", "/api/session/:sessionID/message", {
  26. params: { sessionID: SessionV2.ID },
  27. query: SessionMessagesQuery,
  28. success: Schema.Struct({
  29. data: Schema.Array(SessionMessage.Message),
  30. cursor: Schema.Struct({
  31. previous: Schema.String.pipe(Schema.optional),
  32. next: Schema.String.pipe(Schema.optional),
  33. }),
  34. }).annotate({ identifier: "SessionMessagesResponse" }),
  35. error: [InvalidCursorError, SessionNotFoundError, UnknownError],
  36. })
  37. .middleware(SessionLocationMiddleware)
  38. .annotateMerge(
  39. OpenApi.annotations({
  40. identifier: "v2.session.messages",
  41. summary: "Get session messages",
  42. description:
  43. "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.",
  44. }),
  45. ),
  46. )
  47. .annotateMerge(
  48. OpenApi.annotations({
  49. title: "messages",
  50. description: "Experimental message routes.",
  51. }),
  52. )