netheriteai-code 0.1.0 → 0.2.0
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/package.json +2 -2
- package/src/ollama.js +35 -44
package/package.json
CHANGED
package/src/ollama.js
CHANGED
|
@@ -2,7 +2,7 @@ import { execFile } from "node:child_process";
|
|
|
2
2
|
import { promisify } from "node:util";
|
|
3
3
|
|
|
4
4
|
const execFileAsync = promisify(execFile);
|
|
5
|
-
const
|
|
5
|
+
const BASE_URL = process.env.NETHERITE_BASE_URL || "http://176.88.249.119:11434";
|
|
6
6
|
const PREFERRED_DEFAULT_MODELS = [
|
|
7
7
|
"glm-5:cloud",
|
|
8
8
|
];
|
|
@@ -18,25 +18,29 @@ function getThinkMode(model) {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
async function request(pathname, body, signal) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
try {
|
|
22
|
+
const response = await fetch(`${BASE_URL}${pathname}`, {
|
|
23
|
+
method: "POST",
|
|
24
|
+
headers: { "content-type": "application/json" },
|
|
25
|
+
body: JSON.stringify(body),
|
|
26
|
+
signal,
|
|
27
|
+
});
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
throw new Error("Server down");
|
|
31
|
+
}
|
|
31
32
|
|
|
32
|
-
|
|
33
|
+
return response;
|
|
34
|
+
} catch {
|
|
35
|
+
throw new Error("Server down");
|
|
36
|
+
}
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
export async function listModels() {
|
|
36
40
|
try {
|
|
37
|
-
const response = await fetch(`${
|
|
41
|
+
const response = await fetch(`${BASE_URL}/api/tags`);
|
|
38
42
|
if (!response.ok) {
|
|
39
|
-
throw new Error(
|
|
43
|
+
throw new Error("Server down");
|
|
40
44
|
}
|
|
41
45
|
const json = await response.json();
|
|
42
46
|
return (json.models || []).map((model) => ({
|
|
@@ -47,41 +51,28 @@ export async function listModels() {
|
|
|
47
51
|
details: model.details || {},
|
|
48
52
|
}));
|
|
49
53
|
} catch {
|
|
50
|
-
|
|
51
|
-
const { stdout } = await execFileAsync("ollama", ["list"], { encoding: "utf8" });
|
|
52
|
-
const lines = stdout.trim().split("\n").slice(1).filter(Boolean);
|
|
53
|
-
return lines.map((line) => {
|
|
54
|
-
const parts = line.trim().split(/\s{2,}/);
|
|
55
|
-
return {
|
|
56
|
-
name: parts[0] || "",
|
|
57
|
-
size: parts[2] || "",
|
|
58
|
-
modifiedAt: parts[3] || "",
|
|
59
|
-
digest: parts[1] || "",
|
|
60
|
-
details: {},
|
|
61
|
-
};
|
|
62
|
-
});
|
|
63
|
-
} catch {
|
|
64
|
-
throw new Error(
|
|
65
|
-
"Could not connect to NetheriteAI. Start it with `ollama serve`, or set OLLAMA_BASE_URL if it runs elsewhere.",
|
|
66
|
-
);
|
|
67
|
-
}
|
|
54
|
+
throw new Error("Server down");
|
|
68
55
|
}
|
|
69
56
|
}
|
|
70
57
|
|
|
71
58
|
export async function pickDefaultModel() {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
59
|
+
try {
|
|
60
|
+
const models = await listModels();
|
|
61
|
+
if (!models.length) {
|
|
62
|
+
throw new Error("Server down");
|
|
63
|
+
}
|
|
64
|
+
const preferredMatch = models.find((model) =>
|
|
65
|
+
PREFERRED_DEFAULT_MODELS.some((preferred) => preferred.toLowerCase() === model.name.toLowerCase()),
|
|
66
|
+
);
|
|
67
|
+
if (preferredMatch) {
|
|
68
|
+
return preferredMatch.name;
|
|
69
|
+
}
|
|
82
70
|
|
|
83
|
-
|
|
84
|
-
|
|
71
|
+
const familyMatch = models.find((model) => model.name.toLowerCase().startsWith("netheriteai"));
|
|
72
|
+
return familyMatch?.name || models[0].name;
|
|
73
|
+
} catch {
|
|
74
|
+
throw new Error("Server down");
|
|
75
|
+
}
|
|
85
76
|
}
|
|
86
77
|
|
|
87
78
|
export async function chat({ model, messages, tools, signal }) {
|
|
@@ -181,7 +172,7 @@ export async function chatStream({ model, messages, tools, onChunk, onReasoningC
|
|
|
181
172
|
|
|
182
173
|
const reader = response.body?.getReader();
|
|
183
174
|
if (!reader) {
|
|
184
|
-
throw new Error("
|
|
175
|
+
throw new Error("Server down");
|
|
185
176
|
}
|
|
186
177
|
|
|
187
178
|
const decoder = new TextDecoder();
|