@simonfestl/husky-cli 1.25.2 → 1.25.3
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/dist/commands/brain.js +4 -1
- package/dist/lib/biz/api-brain.js +39 -7
- package/package.json +1 -1
package/dist/commands/brain.js
CHANGED
|
@@ -7,7 +7,10 @@ function toDate(value) {
|
|
|
7
7
|
return value instanceof Date ? value : new Date(value);
|
|
8
8
|
}
|
|
9
9
|
function createBrain(agentId, agentType, options) {
|
|
10
|
-
|
|
10
|
+
// Use API if:
|
|
11
|
+
// 1. Explicitly requested via --use-api
|
|
12
|
+
// 2. shouldUseApi() returns true (no Qdrant configured or session token available)
|
|
13
|
+
if (options?.useApi || shouldUseApi()) {
|
|
11
14
|
return new ApiBrain({
|
|
12
15
|
agentId,
|
|
13
16
|
agentType: isValidAgentType(agentType) ? agentType : undefined,
|
|
@@ -2,20 +2,45 @@ import { getConfig } from "../../commands/config.js";
|
|
|
2
2
|
import { canAccessKnowledgeBase as checkKbAccess } from "../permissions-cache.js";
|
|
3
3
|
async function apiRequest(path, options = {}) {
|
|
4
4
|
const config = getConfig();
|
|
5
|
-
if (!config.apiUrl
|
|
6
|
-
throw new Error("API not configured. Run: husky config set api-url <url>
|
|
5
|
+
if (!config.apiUrl) {
|
|
6
|
+
throw new Error("API not configured. Run: husky config set api-url <url>");
|
|
7
|
+
}
|
|
8
|
+
// Prefer session token (JWT), fall back to API key for backwards compatibility
|
|
9
|
+
const headers = {
|
|
10
|
+
"Content-Type": "application/json",
|
|
11
|
+
};
|
|
12
|
+
if (config.sessionToken) {
|
|
13
|
+
// Check if session is expired
|
|
14
|
+
if (config.sessionExpiresAt) {
|
|
15
|
+
const expiresAt = new Date(config.sessionExpiresAt);
|
|
16
|
+
if (expiresAt < new Date()) {
|
|
17
|
+
throw new Error("Session expired. Run: husky auth login --agent <name>");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
headers["Authorization"] = `Bearer ${config.sessionToken}`;
|
|
21
|
+
}
|
|
22
|
+
else if (config.apiKey) {
|
|
23
|
+
// Legacy fallback - will be rejected by new API but kept for error message
|
|
24
|
+
headers["x-api-key"] = config.apiKey;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
throw new Error("Not authenticated. Run: husky auth login --agent <name>");
|
|
7
28
|
}
|
|
8
29
|
const url = new URL(`/api/brain${path}`, config.apiUrl);
|
|
9
30
|
const res = await fetch(url.toString(), {
|
|
10
31
|
method: options.method || "POST",
|
|
11
|
-
headers
|
|
12
|
-
"x-api-key": config.apiKey,
|
|
13
|
-
"Content-Type": "application/json",
|
|
14
|
-
},
|
|
32
|
+
headers,
|
|
15
33
|
body: options.body ? JSON.stringify(options.body) : undefined,
|
|
16
34
|
});
|
|
17
35
|
if (!res.ok) {
|
|
18
36
|
const error = await res.json().catch(() => ({ error: res.statusText }));
|
|
37
|
+
if (res.status === 401) {
|
|
38
|
+
// Check if it's the new API rejecting API key auth
|
|
39
|
+
if (error.upgrade) {
|
|
40
|
+
throw new Error("Session required. Run: husky auth login --agent <name>");
|
|
41
|
+
}
|
|
42
|
+
throw new Error(error.message || "Authentication failed");
|
|
43
|
+
}
|
|
19
44
|
if (res.status === 403) {
|
|
20
45
|
throw new Error(`Access denied: ${error.error || 'Permission denied to this resource'}`);
|
|
21
46
|
}
|
|
@@ -191,6 +216,13 @@ export class ApiBrain {
|
|
|
191
216
|
}
|
|
192
217
|
export function shouldUseApi() {
|
|
193
218
|
const config = getConfig();
|
|
194
|
-
|
|
219
|
+
// Use API if:
|
|
220
|
+
// 1. No Qdrant URL configured (can't use direct access)
|
|
221
|
+
// 2. Or session token is available (preferred auth method)
|
|
222
|
+
if (!config.qdrantUrl) {
|
|
223
|
+
return Boolean(config.apiUrl);
|
|
224
|
+
}
|
|
225
|
+
// If Qdrant is configured, still prefer API if session token exists
|
|
226
|
+
return Boolean(config.apiUrl && config.sessionToken);
|
|
195
227
|
}
|
|
196
228
|
export default ApiBrain;
|