Skip to content

Commit 68bb8ce

Browse files
committed
core: filter sessions at database level to improve session list loading performance
1 parent 306fc77 commit 68bb8ce

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ export const SessionRoutes = lazy(() =>
5353
),
5454
async (c) => {
5555
const query = c.req.valid("query")
56-
const term = query.search?.toLowerCase()
5756
const sessions: Session.Info[] = []
58-
for await (const session of Session.list()) {
59-
if (query.directory !== undefined && session.directory !== query.directory) continue
60-
if (query.roots && session.parentID) continue
61-
if (query.start !== undefined && session.time.updated < query.start) continue
62-
if (term !== undefined && !session.title.toLowerCase().includes(term)) continue
57+
for await (const session of Session.list({
58+
directory: query.directory,
59+
roots: query.roots,
60+
start: query.start,
61+
search: query.search,
62+
limit: query.limit,
63+
})) {
6364
sessions.push(session)
64-
if (query.limit !== undefined && sessions.length >= query.limit) break
6565
}
6666
return c.json(sessions)
6767
},

packages/opencode/src/session/index.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Flag } from "../flag/flag"
1010
import { Identifier } from "../id/id"
1111
import { Installation } from "../installation"
1212

13-
import { Database, NotFoundError, eq, and, or, like } from "../storage/db"
13+
import { Database, NotFoundError, eq, and, or, gte, isNull, desc, like } from "../storage/db"
1414
import { SessionTable, MessageTable, PartTable } from "./session.sql"
1515
import { Storage } from "@/storage/storage"
1616
import { Log } from "../util/log"
@@ -505,20 +505,38 @@ export namespace Session {
505505
},
506506
)
507507

508-
export function* list() {
508+
export function* list(input?: {
509+
directory?: string
510+
roots?: boolean
511+
start?: number
512+
search?: string
513+
limit?: number
514+
}) {
509515
const project = Instance.project
510-
// const rel = path.relative(Instance.worktree, Instance.directory)
511-
// const suffix = path.sep + rel
516+
const conditions = [eq(SessionTable.project_id, project.id)]
517+
518+
if (input?.directory) {
519+
conditions.push(eq(SessionTable.directory, input.directory))
520+
}
521+
if (input?.roots) {
522+
conditions.push(isNull(SessionTable.parent_id))
523+
}
524+
if (input?.start) {
525+
conditions.push(gte(SessionTable.time_updated, input.start))
526+
}
527+
if (input?.search) {
528+
conditions.push(like(SessionTable.title, `%${input.search}%`))
529+
}
530+
531+
const limit = input?.limit ?? 100
532+
512533
const rows = Database.use((db) =>
513534
db
514535
.select()
515536
.from(SessionTable)
516-
.where(
517-
and(
518-
eq(SessionTable.project_id, project.id),
519-
// or(eq(SessionTable.directory, Instance.directory), like(SessionTable.directory, `%${suffix}`)),
520-
),
521-
)
537+
.where(and(...conditions))
538+
.orderBy(desc(SessionTable.time_updated))
539+
.limit(limit)
522540
.all(),
523541
)
524542
for (const row of rows) {

0 commit comments

Comments
 (0)