package main // The topic index: a derived Xapian index over the store, which lets a // reader enter by concept ("topic:pve") or by repository and type rather than // by symbol name. // // SQLite stays the source of truth. This is a subprocess because libxapian is // C++-only: a call into it from Odin means a hand-written C shim, while // guile-xapian already has one, tested. So the boundary is a script path in // and JSON out -- see scripts/index.scm, which owns every Xapian call. // // ai-memory --index DIR update-index [--rebuild] // ai-memory --index DIR search 'topic:pve repo:genenetwork4' // // Where the pieces are: // --index PATH or $AI_MEMORY_INDEX the Xapian directory // --index-script PATH or $AI_MEMORY_INDEX_SCRIPT scripts/index.scm // $GUILE or "guile" the interpreter import "core:fmt" import "core:os" import "core:strings" // Set by the global options. INDEX_PATH: string INDEX_SCRIPT: string // The script defaults to $AI_MEMORY_INDEX_SCRIPT, then to an installed copy // beside the binary (share/ai-memory), then to the working tree's own copy -- // so a developer running ./bin/ai-memory from the repository needs no setup. index_script :: proc() -> string { if len(INDEX_SCRIPT) > 0 { return INDEX_SCRIPT } if env := os.get_env("AI_MEMORY_INDEX_SCRIPT", context.temp_allocator); len(env) > 0 { return env } if dir, err := os.get_executable_directory(context.temp_allocator); err == nil { installed := strings.concatenate({dir, "/../share/ai-memory/index.scm"}, context.temp_allocator) if os.exists(installed) { return installed } } return "scripts/index.scm" } guile_command :: proc() -> string { if env := os.get_env("GUILE", context.temp_allocator); len(env) > 0 { return env } return "guile" } // Run the index script and hand back its streams. `--no-auto-compile' keeps // the first call from writing a cache into the user's home directory, which // matters because the script runs from a read-only store path when installed. run_index_script :: proc(args: []string) -> (stdout: []byte, stderr: []byte, ok: bool) { script := index_script() if !os.exists(script) { fmt.eprintfln("ai-memory: no index script at %q (set $AI_MEMORY_INDEX_SCRIPT)", script) return nil, nil, false } command := make([dynamic]string, 0, len(args) + 5, context.temp_allocator) append(&command, guile_command(), "--no-auto-compile", "-s", script) for arg in args { append(&command, arg) } state, out, err_out, perr := os.process_exec({command = command[:]}, context.temp_allocator) if perr != nil { fmt.eprintfln("ai-memory: could not run %q: %v", guile_command(), perr) return nil, nil, false } if !state.success { fmt.eprintfln("ai-memory: the index script failed") if len(err_out) > 0 { fmt.eprintfln("%s", string(err_out)) } return out, err_out, false } return out, err_out, true } // `--store' is the SQLite path the store already resolved: the index is over // *this* store, so the caller never names it twice. cmd_update_index :: proc(rebuild: bool) -> bool { db := db_path() if !os.exists(db) { fmt.eprintfln("ai-memory: no store at %s -- run `init' first", db) return false } args := make([dynamic]string, 0, 8, context.temp_allocator) append(&args, "update", "--store", db, "--index", index_path()) if rebuild { append(&args, "--rebuild") } _, stderr, ok := run_index_script(args[:]) if ok && len(stderr) > 0 { // The script's progress note is informational: pass it through. fmt.eprintfln("%s", string(stderr)) } return ok } // Search through the index instead of the store's own FTS. The script's JSON // is printed verbatim: it is the reply format, and re-encoding it here would // only add a parser that can disagree with the writer. cmd_search_index :: proc(words: []string, limit: int) -> bool { args := make([dynamic]string, 0, 8, context.temp_allocator) append(&args, "search", "--index", index_path()) if limit > 0 { append(&args, "--limit", fmt.aprintf("%d", limit, allocator = context.temp_allocator)) } for word in words { append(&args, word) } stdout, _, ok := run_index_script(args[:]) if ok { fmt.printfln("%s", strings.trim_space(string(stdout))) } return ok } // The index directory: --index, else $AI_MEMORY_INDEX, else .d/index // so that a store and its derived index sit together and both are gitignored. // Whether the caller asked for the index: an explicit --index, or the // environment variable that stands in for it. (The default directory is // *not* a request: without one of these, `search' keeps its SQLite // behaviour, so nothing changes for a store that has no index.) index_requested :: proc() -> bool { if len(INDEX_PATH) > 0 { return true } return len(os.get_env("AI_MEMORY_INDEX", context.temp_allocator)) > 0 } index_path :: proc() -> string { if len(INDEX_PATH) > 0 { return INDEX_PATH } if env := os.get_env("AI_MEMORY_INDEX", context.temp_allocator); len(env) > 0 { return env } return strings.concatenate({db_path(), ".d/index"}, context.temp_allocator) }