@@ -10,7 +10,7 @@ import { Flag } from "../flag/flag"
1010import { Identifier } from "../id/id"
1111import { 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"
1414import { SessionTable , MessageTable , PartTable } from "./session.sql"
1515import { Storage } from "@/storage/storage"
1616import { 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