@voidwire/lore 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/prismis.ts +55 -9
- package/package.json +1 -1
package/lib/prismis.ts
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
* Prismis API integration
|
|
3
3
|
*
|
|
4
4
|
* Queries prismis daemon REST API for semantic search across content.
|
|
5
|
-
*
|
|
5
|
+
* Config priority:
|
|
6
|
+
* 1. ~/.config/lore/config.toml [remote] section
|
|
7
|
+
* 2. ~/.config/prismis/config.toml [api] section (local daemon fallback)
|
|
6
8
|
*/
|
|
7
9
|
|
|
8
10
|
import { readFileSync, existsSync } from "fs";
|
|
@@ -18,6 +20,12 @@ export interface PrismisSearchResult {
|
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
const DEFAULT_PORT = 8989;
|
|
23
|
+
const LORE_CONFIG_PATH = join(
|
|
24
|
+
process.env.HOME ?? "",
|
|
25
|
+
".config",
|
|
26
|
+
"lore",
|
|
27
|
+
"config.toml",
|
|
28
|
+
);
|
|
21
29
|
const PRISMIS_CONFIG_PATH = join(
|
|
22
30
|
process.env.HOME ?? "",
|
|
23
31
|
".config",
|
|
@@ -27,10 +35,11 @@ const PRISMIS_CONFIG_PATH = join(
|
|
|
27
35
|
|
|
28
36
|
export interface PrismisSearchOptions {
|
|
29
37
|
limit?: number;
|
|
38
|
+
source?: string;
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
interface PrismisConfig {
|
|
33
|
-
|
|
42
|
+
url: string;
|
|
34
43
|
apiKey: string;
|
|
35
44
|
}
|
|
36
45
|
|
|
@@ -55,19 +64,41 @@ interface PrismisResponse {
|
|
|
55
64
|
}
|
|
56
65
|
|
|
57
66
|
/**
|
|
58
|
-
*
|
|
67
|
+
* Try to read [remote] section from lore config
|
|
59
68
|
*/
|
|
60
|
-
function
|
|
69
|
+
function readLoreRemoteConfig(): PrismisConfig | null {
|
|
70
|
+
if (!existsSync(LORE_CONFIG_PATH)) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const content = readFileSync(LORE_CONFIG_PATH, "utf-8");
|
|
75
|
+
|
|
76
|
+
const urlMatch = content.match(/\[remote\][^[]*url\s*=\s*"([^"]+)"/s);
|
|
77
|
+
const keyMatch = content.match(/\[remote\][^[]*key\s*=\s*"([^"]+)"/s);
|
|
78
|
+
|
|
79
|
+
if (urlMatch && keyMatch) {
|
|
80
|
+
return {
|
|
81
|
+
url: urlMatch[1],
|
|
82
|
+
apiKey: keyMatch[1],
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Read prismis config from local prismis config.toml
|
|
91
|
+
*/
|
|
92
|
+
function readLocalPrismisConfig(): PrismisConfig {
|
|
61
93
|
if (!existsSync(PRISMIS_CONFIG_PATH)) {
|
|
62
94
|
throw new Error(
|
|
63
|
-
`Prismis config not found
|
|
64
|
-
|
|
95
|
+
`Prismis config not found. Add [remote] to ~/.config/lore/config.toml ` +
|
|
96
|
+
`or install prismis locally.`,
|
|
65
97
|
);
|
|
66
98
|
}
|
|
67
99
|
|
|
68
100
|
const content = readFileSync(PRISMIS_CONFIG_PATH, "utf-8");
|
|
69
101
|
|
|
70
|
-
// Parse [api] section
|
|
71
102
|
const keyMatch = content.match(/\[api\][^[]*key\s*=\s*"([^"]+)"/);
|
|
72
103
|
if (!keyMatch) {
|
|
73
104
|
throw new Error(
|
|
@@ -84,11 +115,22 @@ function readPrismisConfig(): PrismisConfig {
|
|
|
84
115
|
}
|
|
85
116
|
|
|
86
117
|
return {
|
|
87
|
-
host
|
|
118
|
+
url: `http://${host}:${DEFAULT_PORT}`,
|
|
88
119
|
apiKey: keyMatch[1],
|
|
89
120
|
};
|
|
90
121
|
}
|
|
91
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Read prismis config - tries lore [remote] first, falls back to local prismis
|
|
125
|
+
*/
|
|
126
|
+
function readPrismisConfig(): PrismisConfig {
|
|
127
|
+
const remoteConfig = readLoreRemoteConfig();
|
|
128
|
+
if (remoteConfig) {
|
|
129
|
+
return remoteConfig;
|
|
130
|
+
}
|
|
131
|
+
return readLocalPrismisConfig();
|
|
132
|
+
}
|
|
133
|
+
|
|
92
134
|
/**
|
|
93
135
|
* Check if prismis daemon is running
|
|
94
136
|
*/
|
|
@@ -122,7 +164,7 @@ export async function searchPrismis(
|
|
|
122
164
|
): Promise<PrismisSearchResult[]> {
|
|
123
165
|
// Read config
|
|
124
166
|
const config = readPrismisConfig();
|
|
125
|
-
const apiBase =
|
|
167
|
+
const apiBase = config.url;
|
|
126
168
|
|
|
127
169
|
// Check daemon is running
|
|
128
170
|
await checkPrismisDaemon(apiBase);
|
|
@@ -134,6 +176,10 @@ export async function searchPrismis(
|
|
|
134
176
|
compact: "true",
|
|
135
177
|
});
|
|
136
178
|
|
|
179
|
+
if (options.source) {
|
|
180
|
+
params.set("source", options.source);
|
|
181
|
+
}
|
|
182
|
+
|
|
137
183
|
const response = await fetch(`${apiBase}/api/search?${params}`, {
|
|
138
184
|
headers: {
|
|
139
185
|
"X-API-Key": config.apiKey,
|