@arkheia/mcp-server 1.2.1 → 1.2.4
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/CLAUDE_MD_TEMPLATE.md +33 -0
- package/README.md +99 -31
- package/dist/egress.js +91 -0
- package/dist/index.js +272 -0
- package/dist/memory.js +192 -0
- package/dist/providers.js +329 -0
- package/dist/proxy-client.js +243 -0
- package/dist/tool-registry.js +90 -0
- package/package.json +27 -26
- package/scripts/setup-claude.js +169 -0
- package/scripts/setup.js +22 -181
- package/bin/arkheia-mcp.js +0 -207
- package/python/mcp_server/__init__.py +0 -0
- package/python/requirements.txt +0 -4
package/dist/memory.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.store_entity = store_entity;
|
|
40
|
+
exports.retrieve_entities = retrieve_entities;
|
|
41
|
+
exports.store_relation = store_relation;
|
|
42
|
+
const sql_js_1 = __importDefault(require("sql.js"));
|
|
43
|
+
const path = __importStar(require("path"));
|
|
44
|
+
const fs = __importStar(require("fs"));
|
|
45
|
+
const crypto = __importStar(require("crypto"));
|
|
46
|
+
const os = __importStar(require("os"));
|
|
47
|
+
const logger = {
|
|
48
|
+
// stdout carries the MCP protocol; log lines go to stderr only.
|
|
49
|
+
info: (m) => { process.stderr.write(`${m}\n`); },
|
|
50
|
+
warn: (m) => { process.stderr.write(`${m}\n`); },
|
|
51
|
+
error: (m) => { process.stderr.write(`${m}\n`); },
|
|
52
|
+
};
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
// DB setup — sql.js (pure JS, no native module)
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
function _db_path() {
|
|
57
|
+
return process.env.MEMORY_DB_PATH || path.join(os.homedir(), '.arkheia', 'memory.db');
|
|
58
|
+
}
|
|
59
|
+
let _db = null;
|
|
60
|
+
let _dbReady = null;
|
|
61
|
+
function _getDb() {
|
|
62
|
+
if (_dbReady)
|
|
63
|
+
return _dbReady;
|
|
64
|
+
_dbReady = (async () => {
|
|
65
|
+
const SQL = await (0, sql_js_1.default)();
|
|
66
|
+
const dbPath = _db_path();
|
|
67
|
+
const dir = path.dirname(dbPath);
|
|
68
|
+
if (!fs.existsSync(dir)) {
|
|
69
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
70
|
+
}
|
|
71
|
+
// Load existing DB or create new
|
|
72
|
+
if (fs.existsSync(dbPath)) {
|
|
73
|
+
const buffer = fs.readFileSync(dbPath);
|
|
74
|
+
_db = new SQL.Database(buffer);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
_db = new SQL.Database();
|
|
78
|
+
}
|
|
79
|
+
// Init schema
|
|
80
|
+
_db.run(`
|
|
81
|
+
CREATE TABLE IF NOT EXISTS entities (
|
|
82
|
+
entity_id TEXT PRIMARY KEY,
|
|
83
|
+
name TEXT NOT NULL,
|
|
84
|
+
entity_type TEXT NOT NULL,
|
|
85
|
+
created_at TEXT NOT NULL
|
|
86
|
+
);
|
|
87
|
+
CREATE TABLE IF NOT EXISTS observations (
|
|
88
|
+
obs_id TEXT PRIMARY KEY,
|
|
89
|
+
entity_id TEXT NOT NULL REFERENCES entities(entity_id),
|
|
90
|
+
content TEXT NOT NULL,
|
|
91
|
+
created_at TEXT NOT NULL
|
|
92
|
+
);
|
|
93
|
+
CREATE TABLE IF NOT EXISTS relations (
|
|
94
|
+
rel_id TEXT PRIMARY KEY,
|
|
95
|
+
from_entity TEXT NOT NULL,
|
|
96
|
+
relation_type TEXT NOT NULL,
|
|
97
|
+
to_entity TEXT NOT NULL,
|
|
98
|
+
created_at TEXT NOT NULL
|
|
99
|
+
);
|
|
100
|
+
`);
|
|
101
|
+
_save(_db);
|
|
102
|
+
return _db;
|
|
103
|
+
})();
|
|
104
|
+
return _dbReady;
|
|
105
|
+
}
|
|
106
|
+
function _save(db) {
|
|
107
|
+
const data = db.export();
|
|
108
|
+
const buffer = Buffer.from(data);
|
|
109
|
+
fs.writeFileSync(_db_path(), buffer);
|
|
110
|
+
}
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
112
|
+
// Public functions
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
async function store_entity(name, entity_type, observations) {
|
|
115
|
+
const db = await _getDb();
|
|
116
|
+
const now = new Date().toISOString();
|
|
117
|
+
// Upsert entity — look up by name+type
|
|
118
|
+
let entity_id;
|
|
119
|
+
const existing = db.exec("SELECT entity_id FROM entities WHERE name = ? AND entity_type = ?", [name, entity_type]);
|
|
120
|
+
if (existing.length > 0 && existing[0].values.length > 0) {
|
|
121
|
+
entity_id = existing[0].values[0][0];
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
entity_id = crypto.randomUUID();
|
|
125
|
+
db.run("INSERT INTO entities (entity_id, name, entity_type, created_at) VALUES (?, ?, ?, ?)", [entity_id, name, entity_type, now]);
|
|
126
|
+
}
|
|
127
|
+
// Fetch existing observation contents to deduplicate
|
|
128
|
+
const existingObs = db.exec("SELECT content FROM observations WHERE entity_id = ?", [entity_id]);
|
|
129
|
+
const existingContentSet = new Set();
|
|
130
|
+
if (existingObs.length > 0) {
|
|
131
|
+
for (const row of existingObs[0].values) {
|
|
132
|
+
existingContentSet.add(row[0]);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
let added = 0;
|
|
136
|
+
for (const content of observations) {
|
|
137
|
+
if (!existingContentSet.has(content)) {
|
|
138
|
+
db.run("INSERT INTO observations (obs_id, entity_id, content, created_at) VALUES (?, ?, ?, ?)", [crypto.randomUUID(), entity_id, content, now]);
|
|
139
|
+
existingContentSet.add(content);
|
|
140
|
+
added++;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const countResult = db.exec("SELECT COUNT(*) AS n FROM observations WHERE entity_id = ?", [entity_id]);
|
|
144
|
+
const totalObservations = countResult.length > 0 ? countResult[0].values[0][0] : 0;
|
|
145
|
+
_save(db);
|
|
146
|
+
return {
|
|
147
|
+
entity_id,
|
|
148
|
+
name,
|
|
149
|
+
entity_type,
|
|
150
|
+
observations_added: added,
|
|
151
|
+
total_observations: totalObservations,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
async function retrieve_entities(query, entity_type = undefined, limit = 10) {
|
|
155
|
+
const db = await _getDb();
|
|
156
|
+
const pattern = `%${query}%`;
|
|
157
|
+
let rows;
|
|
158
|
+
if (entity_type) {
|
|
159
|
+
const result = db.exec("SELECT entity_id, name, entity_type, created_at FROM entities WHERE name LIKE ? AND entity_type = ?", [pattern, entity_type]);
|
|
160
|
+
rows = result.length > 0 ? result[0].values.map(r => ({ entity_id: r[0], name: r[1], entity_type: r[2], created_at: r[3] })) : [];
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
const result = db.exec("SELECT entity_id, name, entity_type, created_at FROM entities WHERE name LIKE ?", [pattern]);
|
|
164
|
+
rows = result.length > 0 ? result[0].values.map(r => ({ entity_id: r[0], name: r[1], entity_type: r[2], created_at: r[3] })) : [];
|
|
165
|
+
}
|
|
166
|
+
const total = rows.length;
|
|
167
|
+
rows = rows.slice(0, Math.min(limit, 50));
|
|
168
|
+
const entities = [];
|
|
169
|
+
for (const row of rows) {
|
|
170
|
+
const obsResult = db.exec("SELECT content, created_at FROM observations WHERE entity_id = ? ORDER BY created_at", [row.entity_id]);
|
|
171
|
+
const obs = obsResult.length > 0 ? obsResult[0].values.map(o => ({ content: o[0], created_at: o[1] })) : [];
|
|
172
|
+
const relResult = db.exec("SELECT relation_type, to_entity FROM relations WHERE from_entity = ? ORDER BY created_at", [row.name]);
|
|
173
|
+
const rels = relResult.length > 0 ? relResult[0].values.map(r => ({ relation_type: r[0], to_entity: r[1] })) : [];
|
|
174
|
+
entities.push({
|
|
175
|
+
entity_id: row.entity_id,
|
|
176
|
+
name: row.name,
|
|
177
|
+
entity_type: row.entity_type,
|
|
178
|
+
created_at: row.created_at,
|
|
179
|
+
observations: obs,
|
|
180
|
+
relations: rels,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return { entities, total };
|
|
184
|
+
}
|
|
185
|
+
async function store_relation(from_entity, relation_type, to_entity) {
|
|
186
|
+
const db = await _getDb();
|
|
187
|
+
const rel_id = crypto.randomUUID();
|
|
188
|
+
const now = new Date().toISOString();
|
|
189
|
+
db.run("INSERT INTO relations (rel_id, from_entity, relation_type, to_entity, created_at) VALUES (?, ?, ?, ?, ?)", [rel_id, from_entity, relation_type, to_entity, now]);
|
|
190
|
+
_save(db);
|
|
191
|
+
return { rel_id, from_entity, relation_type, to_entity };
|
|
192
|
+
}
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.call_grok = call_grok;
|
|
37
|
+
exports.call_gemini = call_gemini;
|
|
38
|
+
exports.call_together = call_together;
|
|
39
|
+
exports.call_ollama = call_ollama;
|
|
40
|
+
const crypto = __importStar(require("crypto"));
|
|
41
|
+
// Node.js native fetch doesn't have a global logger, use console
|
|
42
|
+
const logger = {
|
|
43
|
+
// stdout carries the MCP protocol; log lines go to stderr only.
|
|
44
|
+
info: (m) => { process.stderr.write(`${m}\n`); },
|
|
45
|
+
warn: (m) => { process.stderr.write(`${m}\n`); },
|
|
46
|
+
error: (m) => { process.stderr.write(`${m}\n`); },
|
|
47
|
+
};
|
|
48
|
+
const _DEFAULT_TIMEOUT = 60 * 1000; // 60 seconds in milliseconds
|
|
49
|
+
const _OLLAMA_TIMEOUT = 120 * 1000; // 120 seconds in milliseconds
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// Internal helpers
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
function _prompt_hash(prompt) {
|
|
54
|
+
return crypto.createHash('sha256').update(prompt).digest('hex');
|
|
55
|
+
}
|
|
56
|
+
function _err_response(model, prompt, error) {
|
|
57
|
+
return {
|
|
58
|
+
response: `[provider_error: ${error}]`,
|
|
59
|
+
model: model,
|
|
60
|
+
prompt_hash: _prompt_hash(prompt),
|
|
61
|
+
error: error,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
// Grok (xAI) — OpenAI-compatible /v1/chat/completions
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
async function call_grok(prompt, model = "grok-4-fast-non-reasoning", kwargs = {}) {
|
|
68
|
+
/**
|
|
69
|
+
* Call xAI Grok chat completions API.
|
|
70
|
+
*
|
|
71
|
+
* Returns: {response, model, prompt_hash, error}
|
|
72
|
+
*/
|
|
73
|
+
const api_key = process.env.XAI_API_KEY;
|
|
74
|
+
if (!api_key) {
|
|
75
|
+
return _err_response(model, prompt, "XAI_API_KEY not set");
|
|
76
|
+
}
|
|
77
|
+
const controller = new AbortController();
|
|
78
|
+
const timeoutId = setTimeout(() => controller.abort(), _DEFAULT_TIMEOUT);
|
|
79
|
+
try {
|
|
80
|
+
const resp = await fetch("https://api.x.ai/v1/chat/completions", {
|
|
81
|
+
method: "POST",
|
|
82
|
+
headers: {
|
|
83
|
+
"Authorization": `Bearer ${api_key}`,
|
|
84
|
+
"Content-Type": "application/json",
|
|
85
|
+
},
|
|
86
|
+
body: JSON.stringify({
|
|
87
|
+
model: model,
|
|
88
|
+
messages: [{ role: "user", content: prompt }],
|
|
89
|
+
...kwargs,
|
|
90
|
+
}),
|
|
91
|
+
redirect: "error", // provider keys are never re-sent to wherever a redirect points
|
|
92
|
+
signal: controller.signal,
|
|
93
|
+
});
|
|
94
|
+
clearTimeout(timeoutId);
|
|
95
|
+
if (!resp.ok) {
|
|
96
|
+
logger.error(`call_grok: HTTP ${resp.status} for model=${model}`);
|
|
97
|
+
return _err_response(model, prompt, `http_${resp.status}`);
|
|
98
|
+
}
|
|
99
|
+
const data = await resp.json();
|
|
100
|
+
const response_text = data.choices?.[0]?.message?.content;
|
|
101
|
+
if (typeof response_text !== 'string') {
|
|
102
|
+
throw new Error("Invalid response format from Grok API");
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
response: response_text,
|
|
106
|
+
model: model,
|
|
107
|
+
prompt_hash: _prompt_hash(prompt),
|
|
108
|
+
usage: data.usage || {},
|
|
109
|
+
error: null,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
clearTimeout(timeoutId);
|
|
114
|
+
if (e.name === 'AbortError') {
|
|
115
|
+
logger.error(`call_grok: request timed out for model=${model}`);
|
|
116
|
+
return _err_response(model, prompt, "timeout");
|
|
117
|
+
}
|
|
118
|
+
else if (e.name === 'TypeError' && e.message.includes('fetch failed')) {
|
|
119
|
+
logger.error(`call_grok: network error for model=${model}: ${e.message}`);
|
|
120
|
+
return _err_response(model, prompt, "network_error");
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
logger.error(`call_grok: unexpected error: ${e.message}`);
|
|
124
|
+
return _err_response(model, prompt, e.message);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
// Gemini (Google) — generateContent REST API
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
async function call_gemini(prompt, model = "gemini-2.5-flash", max_output_tokens = 1000, kwargs = {}) {
|
|
132
|
+
/**
|
|
133
|
+
* Call Google Gemini generateContent API.
|
|
134
|
+
*
|
|
135
|
+
* Note: gemini-2.5-flash and -pro are thinking models — they need
|
|
136
|
+
* max_output_tokens >= 1000 to produce content after thinking tokens.
|
|
137
|
+
*
|
|
138
|
+
* Returns: {response, model, prompt_hash, error}
|
|
139
|
+
*/
|
|
140
|
+
const api_key = process.env.GOOGLE_API_KEY;
|
|
141
|
+
if (!api_key) {
|
|
142
|
+
return _err_response(model, prompt, "GOOGLE_API_KEY not set");
|
|
143
|
+
}
|
|
144
|
+
const url = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;
|
|
145
|
+
const controller = new AbortController();
|
|
146
|
+
const timeoutId = setTimeout(() => controller.abort(), _DEFAULT_TIMEOUT);
|
|
147
|
+
try {
|
|
148
|
+
const resp = await fetch(`${url}?key=${api_key}`, {
|
|
149
|
+
method: "POST",
|
|
150
|
+
headers: {
|
|
151
|
+
"Content-Type": "application/json",
|
|
152
|
+
},
|
|
153
|
+
body: JSON.stringify({
|
|
154
|
+
contents: [{ parts: [{ text: prompt }] }],
|
|
155
|
+
generationConfig: {
|
|
156
|
+
maxOutputTokens: max_output_tokens,
|
|
157
|
+
...kwargs,
|
|
158
|
+
},
|
|
159
|
+
}),
|
|
160
|
+
redirect: "error", // provider keys are never re-sent to wherever a redirect points
|
|
161
|
+
signal: controller.signal,
|
|
162
|
+
});
|
|
163
|
+
clearTimeout(timeoutId);
|
|
164
|
+
if (!resp.ok) {
|
|
165
|
+
logger.error(`call_gemini: HTTP ${resp.status} for model=${model}`);
|
|
166
|
+
return _err_response(model, prompt, `http_${resp.status}`);
|
|
167
|
+
}
|
|
168
|
+
const data = await resp.json();
|
|
169
|
+
const response_text = data.candidates?.[0]?.content?.parts?.[0]?.text;
|
|
170
|
+
if (typeof response_text !== 'string') {
|
|
171
|
+
throw new Error("Invalid response format from Gemini API");
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
response: response_text,
|
|
175
|
+
model: model,
|
|
176
|
+
prompt_hash: _prompt_hash(prompt),
|
|
177
|
+
usage: data.usageMetadata || {},
|
|
178
|
+
error: null,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
catch (e) {
|
|
182
|
+
clearTimeout(timeoutId);
|
|
183
|
+
if (e.name === 'AbortError') {
|
|
184
|
+
logger.error(`call_gemini: request timed out for model=${model}`);
|
|
185
|
+
return _err_response(model, prompt, "timeout");
|
|
186
|
+
}
|
|
187
|
+
else if (e.name === 'TypeError' && e.message.includes('fetch failed')) {
|
|
188
|
+
logger.error(`call_gemini: network error for model=${model}: ${e.message}`);
|
|
189
|
+
return _err_response(model, prompt, "network_error");
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
logger.error(`call_gemini: unexpected error: ${e.message}`);
|
|
193
|
+
return _err_response(model, prompt, e.message);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// ---------------------------------------------------------------------------
|
|
198
|
+
// Together AI — OpenAI-compatible, cloud inference
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
async function call_together(prompt, model = "moonshotai/Kimi-K2.5", max_tokens = 2048, kwargs = {}) {
|
|
201
|
+
/**
|
|
202
|
+
* Call Together AI chat completions API (OpenAI-compatible).
|
|
203
|
+
*
|
|
204
|
+
* Default model is Kimi K2.5 — a thinking model that consumes
|
|
205
|
+
* 100-500 tokens internally before producing output, so max_tokens
|
|
206
|
+
* must be >= 2048 to reliably get a response.
|
|
207
|
+
*
|
|
208
|
+
* Returns: {response, model, prompt_hash, usage, error}
|
|
209
|
+
*/
|
|
210
|
+
const api_key = process.env.TOGETHER_API_KEY;
|
|
211
|
+
if (!api_key) {
|
|
212
|
+
return _err_response(model, prompt, "TOGETHER_API_KEY not set");
|
|
213
|
+
}
|
|
214
|
+
const controller = new AbortController();
|
|
215
|
+
const timeoutId = setTimeout(() => controller.abort(), _DEFAULT_TIMEOUT);
|
|
216
|
+
try {
|
|
217
|
+
const resp = await fetch("https://api.together.xyz/v1/chat/completions", {
|
|
218
|
+
method: "POST",
|
|
219
|
+
headers: {
|
|
220
|
+
"Authorization": `Bearer ${api_key}`,
|
|
221
|
+
"Content-Type": "application/json",
|
|
222
|
+
},
|
|
223
|
+
body: JSON.stringify({
|
|
224
|
+
model: model,
|
|
225
|
+
max_tokens: max_tokens,
|
|
226
|
+
messages: [{ role: "user", content: prompt }],
|
|
227
|
+
...kwargs,
|
|
228
|
+
}),
|
|
229
|
+
redirect: "error", // provider keys are never re-sent to wherever a redirect points
|
|
230
|
+
signal: controller.signal,
|
|
231
|
+
});
|
|
232
|
+
clearTimeout(timeoutId);
|
|
233
|
+
if (!resp.ok) {
|
|
234
|
+
logger.error(`call_together: HTTP ${resp.status} for model=${model}`);
|
|
235
|
+
return _err_response(model, prompt, `http_${resp.status}`);
|
|
236
|
+
}
|
|
237
|
+
const data = await resp.json();
|
|
238
|
+
const response_text = data.choices?.[0]?.message?.content;
|
|
239
|
+
if (typeof response_text !== 'string') {
|
|
240
|
+
throw new Error("Invalid response format from Together AI API");
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
response: response_text,
|
|
244
|
+
model: model,
|
|
245
|
+
prompt_hash: _prompt_hash(prompt),
|
|
246
|
+
usage: data.usage || {},
|
|
247
|
+
error: null,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
catch (e) {
|
|
251
|
+
clearTimeout(timeoutId);
|
|
252
|
+
if (e.name === 'AbortError') {
|
|
253
|
+
logger.error(`call_together: request timed out for model=${model}`);
|
|
254
|
+
return _err_response(model, prompt, "timeout");
|
|
255
|
+
}
|
|
256
|
+
else if (e.name === 'TypeError' && e.message.includes('fetch failed')) {
|
|
257
|
+
logger.error(`call_together: network error for model=${model}: ${e.message}`);
|
|
258
|
+
return _err_response(model, prompt, "network_error");
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
logger.error(`call_together: unexpected error: ${e.message}`);
|
|
262
|
+
return _err_response(model, prompt, e.message);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
// ---------------------------------------------------------------------------
|
|
267
|
+
// Ollama — local inference, no network egress
|
|
268
|
+
// ---------------------------------------------------------------------------
|
|
269
|
+
async function call_ollama(prompt, model = "phi4:14b", kwargs = {}) {
|
|
270
|
+
/**
|
|
271
|
+
* Call local Ollama model via /api/generate (non-streaming).
|
|
272
|
+
*
|
|
273
|
+
* OLLAMA_BASE_URL defaults to http://localhost:11434.
|
|
274
|
+
* No network egress — local eval only.
|
|
275
|
+
*
|
|
276
|
+
* Returns: {response, model, prompt_hash, eval_count, error}
|
|
277
|
+
*/
|
|
278
|
+
const base_url = process.env.OLLAMA_BASE_URL || "http://localhost:11434";
|
|
279
|
+
const controller = new AbortController();
|
|
280
|
+
const timeoutId = setTimeout(() => controller.abort(), _OLLAMA_TIMEOUT);
|
|
281
|
+
try {
|
|
282
|
+
const resp = await fetch(`${base_url}/api/generate`, {
|
|
283
|
+
method: "POST",
|
|
284
|
+
headers: {
|
|
285
|
+
"Content-Type": "application/json",
|
|
286
|
+
},
|
|
287
|
+
body: JSON.stringify({
|
|
288
|
+
model: model,
|
|
289
|
+
prompt: prompt,
|
|
290
|
+
stream: false,
|
|
291
|
+
...kwargs,
|
|
292
|
+
}),
|
|
293
|
+
redirect: "error", // provider keys are never re-sent to wherever a redirect points
|
|
294
|
+
signal: controller.signal,
|
|
295
|
+
});
|
|
296
|
+
clearTimeout(timeoutId);
|
|
297
|
+
if (!resp.ok) {
|
|
298
|
+
logger.error(`call_ollama: HTTP ${resp.status} for model=${model}`);
|
|
299
|
+
return _err_response(model, prompt, `http_${resp.status}`);
|
|
300
|
+
}
|
|
301
|
+
const data = await resp.json();
|
|
302
|
+
const response_text = data.response;
|
|
303
|
+
if (typeof response_text !== 'string') {
|
|
304
|
+
throw new Error("Invalid response format from Ollama API");
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
response: response_text,
|
|
308
|
+
model: model,
|
|
309
|
+
prompt_hash: _prompt_hash(prompt),
|
|
310
|
+
eval_count: data.eval_count,
|
|
311
|
+
error: null,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
catch (e) {
|
|
315
|
+
clearTimeout(timeoutId);
|
|
316
|
+
if (e.name === 'AbortError') {
|
|
317
|
+
logger.error(`call_ollama: request timed out for model=${model}`);
|
|
318
|
+
return _err_response(model, prompt, "timeout");
|
|
319
|
+
}
|
|
320
|
+
else if (e.name === 'TypeError' && e.message.includes('fetch failed')) {
|
|
321
|
+
logger.error(`call_ollama: cannot connect to Ollama at ${base_url}`);
|
|
322
|
+
return _err_response(model, prompt, "ollama_unavailable");
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
logger.error(`call_ollama: unexpected error: ${e.message}`);
|
|
326
|
+
return _err_response(model, prompt, e.message);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|