diff --git a/packages/core/src/session/store.ts b/packages/core/src/session/store.ts index 94e13d2717d..4eb1ee2e838 100644 --- a/packages/core/src/session/store.ts +++ b/packages/core/src/session/store.ts @@ -1,6 +1,6 @@ export * as SessionStore from "./store.js" -import { and, asc, desc, eq, gt, isNotNull, isNull, like, lt, notInArray, or, sql, type SQL } from "drizzle-orm" +import { and, asc, desc, eq, gt, isNotNull, isNull, lt, notInArray, or, sql, type SQL } from "drizzle-orm" import { Context, Effect, Layer, Schema } from "effect" import { Project } from "@opencode/schema/project" import { Workspace } from "@opencode/schema/workspace" @@ -159,7 +159,7 @@ const layer = Layer.effect( if (input.workspaceID) conditions.push(eq(SessionTable.workspace_id, input.workspaceID)) if ("project" in input) conditions.push(eq(SessionTable.project_id, input.project)) if ("project" in input && input.subpath !== undefined) conditions.push(eq(SessionTable.path, input.subpath)) - if (input.search) conditions.push(like(SessionTable.title, `%${input.search}%`)) + if (input.search) conditions.push(sql`${SessionTable.title} GLOB ${titlePattern(input.search)}`) if (input.parentID !== undefined) conditions.push( input.parentID === null ? isNull(SessionTable.parent_id) : eq(SessionTable.parent_id, input.parentID), @@ -299,3 +299,20 @@ const layer = Layer.effect( ) export const node = makeGlobalNode({ service: Service, layer, deps: [Database.node] }) + +// SQLite's LIKE folds case for ASCII only, and none of our drivers (bun, node, Durable Objects) ships ICU or lets us +// register a lowercase function everywhere. GLOB is case-sensitive but supports character classes, so each cased +// letter becomes a class of its single-character case forms ("т" -> "[тТ]"). The LIKE wildcards % and _ keep their +// meaning, and GLOB metacharacters are wrapped in a class so they stay literal as they were under LIKE. +function titlePattern(search: string) { + const chars = Array.from(search, (char) => { + if (char === "%") return "*" + if (char === "_") return "?" + const cases = new Set( + [char, char.toLowerCase(), char.toUpperCase()].filter((form) => Array.from(form).length === 1), + ) + if (cases.size > 1) return `[${[...cases].join("")}]` + return "*?[".includes(char) ? `[${char}]` : char + }) + return `*${chars.join("")}*` +} diff --git a/packages/core/test/session-store.test.ts b/packages/core/test/session-store.test.ts index 84ab081e8e5..a69b0851e0b 100644 --- a/packages/core/test/session-store.test.ts +++ b/packages/core/test/session-store.test.ts @@ -36,7 +36,7 @@ const partRows = Config.testLayer([ }), ]) -const seedSessions = (rows: { id: string; updated: number }[]) => +const seedSessions = (rows: { id: string; updated: number; title?: string }[]) => Effect.gen(function* () { const database = yield* Database.Service const bus = yield* Bus.Service @@ -58,7 +58,7 @@ const seedSessions = (rows: { id: string; updated: number }[]) => aggregateID: sessionID, seq: 1, type: Bus.versionedType(SessionEvent.Renamed.type, 1), - data: { sessionID, title: row.id }, + data: { sessionID, title: row.title ?? row.id }, }) }), ) @@ -166,6 +166,32 @@ describe("SessionStore", () => { }), ) + it.effect("searches titles case-insensitively beyond ASCII and keeps LIKE wildcard semantics", () => + Effect.gen(function* () { + yield* seedSessions([ + { id: "ses_cyrillic", updated: 10, title: "Тестова сесия" }, + { id: "ses_ascii", updated: 20, title: "OpenCode notes" }, + { id: "ses_percent", updated: 30, title: "100% done" }, + { id: "ses_underscore", updated: 40, title: "file_name [draft]" }, + ]) + const store = yield* SessionStore.Service + const search = (text: string) => + store.list({ search: text }).pipe(Effect.map((sessions) => sessions.map((session) => String(session.id)))) + + expect(yield* search("тестова")).toEqual(["ses_cyrillic"]) + expect(yield* search("ТЕСТОВА СЕСИЯ")).toEqual(["ses_cyrillic"]) + expect(yield* search("opencode")).toEqual(["ses_ascii"]) + expect(yield* search("OPENCODE")).toEqual(["ses_ascii"]) + expect(yield* search("тест%сия")).toEqual(["ses_cyrillic"]) + expect(yield* search("OP_NCODE")).toEqual(["ses_ascii"]) + expect(yield* search("100%")).toEqual(["ses_percent"]) + expect(yield* search("file_name")).toEqual(["ses_underscore"]) + expect(yield* search("[DRAFT]")).toEqual(["ses_underscore"]) + expect(yield* search("*")).toEqual([]) + expect(yield* search("?")).toEqual([]) + }), + ) + it.effect("pages messages by durable sequence, not timestamp or ID, and scopes cursor lookup", () => Effect.gen(function* () { const sessionID = Session.ID.make("ses_messages")