Skip to content

Commit 722a3a5

Browse files
committed
feat(session): add ts_before and breakpoint params to Session.messages API
Add optional parameters to Session.messages() for loading older messages: - ts_before: filter to messages created before this timestamp - breakpoint: stop at first compaction summary when true This is a foundational API enhancement that enables clients to implement pagination and history loading without breaking existing functionality.
1 parent 4abf804 commit 722a3a5

7 files changed

Lines changed: 104 additions & 343 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,13 +571,17 @@ export const SessionRoutes = lazy(() =>
571571
"query",
572572
z.object({
573573
limit: z.coerce.number().optional(),
574+
ts_before: z.coerce.number().optional(),
575+
breakpoint: z.coerce.boolean().optional(),
574576
}),
575577
),
576578
async (c) => {
577579
const query = c.req.valid("query")
578580
const messages = await Session.messages({
579581
sessionID: c.req.valid("param").sessionID,
580582
limit: query.limit,
583+
ts_before: query.ts_before,
584+
breakpoint: query.breakpoint,
581585
})
582586
return c.json(messages)
583587
},

packages/opencode/src/session/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,16 @@ export namespace Session {
317317
z.object({
318318
sessionID: Identifier.schema("session"),
319319
limit: z.number().optional(),
320+
ts_before: z.number().optional(),
321+
breakpoint: z.boolean().optional(),
320322
}),
321323
async (input) => {
322324
const result = [] as MessageV2.WithParts[]
323325
for await (const msg of MessageV2.stream(input.sessionID)) {
326+
if (input.ts_before && msg.info.time.created >= input.ts_before) continue
324327
if (input.limit && result.length >= input.limit) break
325328
result.push(msg)
329+
if (input.ts_before && input.breakpoint && msg.parts.some((p) => p.type === "compaction")) break
326330
}
327331
result.reverse()
328332
return result

packages/sdk/js/src/v2/gen/client/client.gen.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,10 @@ export const createClient = (config: Config = {}): Client => {
162162
case "arrayBuffer":
163163
case "blob":
164164
case "formData":
165+
case "json":
165166
case "text":
166167
data = await response[parseAs]()
167168
break
168-
case "json": {
169-
// Some servers return 200 with no Content-Length and empty body.
170-
// response.json() would throw; read as text and parse if non-empty.
171-
const text = await response.text()
172-
data = text ? JSON.parse(text) : {}
173-
break
174-
}
175169
case "stream":
176170
return opts.responseStyle === "data"
177171
? response.body
@@ -250,7 +244,6 @@ export const createClient = (config: Config = {}): Client => {
250244
}
251245
return request
252246
},
253-
serializedBody: getValidRequestBody(opts) as BodyInit | null | undefined,
254247
url,
255248
})
256249
}

packages/sdk/js/src/v2/gen/core/serverSentEvents.gen.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ export const createSseClient = <TData = unknown>({
151151
const { done, value } = await reader.read()
152152
if (done) break
153153
buffer += value
154-
// Normalize line endings: CRLF -> LF, then CR -> LF
155-
buffer = buffer.replace(/\r\n/g, "\n").replace(/\r/g, "\n")
156154

157155
const chunks = buffer.split("\n\n")
158156
buffer = chunks.pop() ?? ""

0 commit comments

Comments
 (0)