about summary refs log tree commit diff
path: root/index.odin
blob: eb6e3f4550abd25f4f7f3139c62aca3af0b0349c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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 <store>.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)
}