v0.2.0
This commit is contained in:
+49
-29
@@ -1,5 +1,3 @@
|
||||
// api-wrappers/lmstudio.js
|
||||
|
||||
const http = require("node:http");
|
||||
const Repetition = require("./repetition");
|
||||
|
||||
@@ -18,40 +16,47 @@ function instanceMatches(instance, configured) {
|
||||
.some(value => typeof value === "string" && value === configured);
|
||||
}
|
||||
|
||||
async function modelIsAvailable(model) {
|
||||
const listResponse = await fetch(`http://${BASE_HOST}:${BASE_PORT}/api/v1/models`);
|
||||
if (!listResponse.ok) throw new Error(`LM Studio model list returned ${listResponse.status}: ${await listResponse.text()}`);
|
||||
|
||||
const list = await listResponse.json();
|
||||
const models = Array.isArray(list.models) ? list.models : [];
|
||||
const installed = models.find(item => item.key === model);
|
||||
const loadedInstance = models
|
||||
.flatMap(item => item.loaded_instances || [])
|
||||
.find(instance => instanceMatches(instance, model));
|
||||
|
||||
if (loadedInstance) return true;
|
||||
if (installed?.loaded_instances?.length) return true;
|
||||
if (installed) return false;
|
||||
|
||||
const openAiList = await fetch(`http://${BASE_HOST}:${BASE_PORT}/v1/models`);
|
||||
if (openAiList.ok) {
|
||||
const loaded = await openAiList.json();
|
||||
if (loaded.data?.some(item => item.id === model)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async function warmUp(settings) {
|
||||
try {
|
||||
const listResponse = await fetch(`http://${BASE_HOST}:${BASE_PORT}/api/v1/models`);
|
||||
if (!listResponse.ok) throw new Error(`LM Studio model list returned ${listResponse.status}: ${await listResponse.text()}`);
|
||||
const list = await listResponse.json();
|
||||
const models = Array.isArray(list.models) ? list.models : [];
|
||||
const installed = models.find(item => item.key === settings.model);
|
||||
const loadedInstance = models.flatMap(item => item.loaded_instances || []).find(instance => instanceMatches(instance, settings.model));
|
||||
if (await modelIsAvailable(settings.model)) return;
|
||||
|
||||
if (loadedInstance || installed?.loaded_instances?.length) return;
|
||||
|
||||
if (!installed) {
|
||||
const openAiList = await fetch(`http://${BASE_HOST}:${BASE_PORT}/v1/models`);
|
||||
if (openAiList.ok) {
|
||||
const loaded = await openAiList.json();
|
||||
if (loaded.data?.some(item => item.id === settings.model)) return;
|
||||
}
|
||||
}
|
||||
|
||||
const modelKey = installed?.key || settings.model;
|
||||
const response = await fetch(`http://${BASE_HOST}:${BASE_PORT}/api/v1/models/load`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ model: modelKey })
|
||||
body: JSON.stringify({ model: settings.model })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(`Model warmup failed: LM Studio returned ${response.status}: ${await response.text()}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!response.ok) throw new Error(`LM Studio returned ${response.status}: ${await response.text()}`);
|
||||
await response.json();
|
||||
} catch (error) {
|
||||
console.error(`Model warmup failed: ${error.message}`);
|
||||
const failure = new Error(`Model warmup failed for '${settings.model}': ${error.message}`);
|
||||
failure.retryable = false;
|
||||
failure.cause = error;
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,11 +101,24 @@ function pageDiagnostic(page) {
|
||||
return {
|
||||
responseId: page.responseId || null,
|
||||
status: page.status || null,
|
||||
model: page.model || null,
|
||||
incompleteDetails: page.incompleteDetails || null,
|
||||
usage: page.usage || null
|
||||
};
|
||||
}
|
||||
|
||||
function assertResponseModel(page, settings) {
|
||||
if (typeof page.model !== "string" || !page.model) return;
|
||||
if (page.model === settings.model) return;
|
||||
|
||||
const error = new Error(`LM Studio response model mismatch: requested '${settings.model}', received '${page.model}'.`);
|
||||
error.code = "PERRY_MODEL_MISMATCH";
|
||||
error.retryable = false;
|
||||
error.expectedModel = settings.model;
|
||||
error.actualModel = page.model;
|
||||
throw error;
|
||||
}
|
||||
|
||||
function sendPage(conversation, settings, options = {}) {
|
||||
const body = {
|
||||
model: settings.model,
|
||||
@@ -109,7 +127,6 @@ function sendPage(conversation, settings, options = {}) {
|
||||
};
|
||||
|
||||
if (conversation.systemPrompt) body.instructions = conversation.systemPrompt;
|
||||
if (settings.reasoning && settings.reasoning !== "off") body.reasoning = { effort: settings.reasoning };
|
||||
if (options.previousResponseId) body.previous_response_id = options.previousResponseId;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -263,6 +280,7 @@ function sendPage(conversation, settings, options = {}) {
|
||||
content: content || fallback.content,
|
||||
status: finalEventType === "response.incomplete" ? "incomplete" : finalResponse?.status || "completed",
|
||||
responseId: responseId || finalResponse?.id || null,
|
||||
model: typeof finalResponse?.model === "string" ? finalResponse.model : null,
|
||||
incompleteDetails: finalResponse?.incomplete_details || null,
|
||||
usage: finalResponse?.usage || null
|
||||
};
|
||||
@@ -306,6 +324,7 @@ async function send(conversation, settings, options = {}) {
|
||||
continuationInput
|
||||
});
|
||||
|
||||
assertResponseModel(page, settings);
|
||||
lastReply = page;
|
||||
pages.push(pageDiagnostic(page));
|
||||
if (page.reasoning) reasoning.push(page.reasoning);
|
||||
@@ -316,7 +335,8 @@ async function send(conversation, settings, options = {}) {
|
||||
if (reasoning.length > 0) reply.reasoning = reasoning.join("");
|
||||
if (page.responseId) reply.responseId = page.responseId;
|
||||
reply.provider = {
|
||||
name: "lmstudio",
|
||||
name: "lm-studio",
|
||||
model: page.model || null,
|
||||
continuationPages: pages.length - 1,
|
||||
pages
|
||||
};
|
||||
@@ -334,7 +354,7 @@ async function send(conversation, settings, options = {}) {
|
||||
const error = new Error(`LM Studio response remained incomplete after ${MAX_INCOMPLETE_CONTINUATIONS} automatic continuations.`);
|
||||
error.retryable = false;
|
||||
error.partial = Boolean(content.length || reasoning.length);
|
||||
error.provider = { name: "lmstudio", continuationPages: pages.length - 1, pages };
|
||||
error.provider = { name: "lm-studio", continuationPages: pages.length - 1, pages };
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user