v0.2.0
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const Conversation = require("./conversation");
|
||||
const lmStudio = require("./lmstudio");
|
||||
const cleanText = require("./clean-text");
|
||||
|
||||
const MAX_TRANSPORT_ATTEMPTS = 10;
|
||||
const MAX_CONTINUATIONS = 10;
|
||||
@@ -9,16 +10,32 @@ const CONTINUE_PROMPT = [
|
||||
"Keep investigating as deeply as necessary. When you have actually finished, provide the final deliverable required by the original prompt."
|
||||
].join("\n");
|
||||
|
||||
module.exports = utils => {
|
||||
const settings = utils.getSettings("llm");
|
||||
const client = String(settings.client || "lm-studio").toLowerCase();
|
||||
if (client !== "lm-studio" && client !== "lmstudio") throw new Error(`Unsupported LLM client: ${settings.client}`);
|
||||
const llm = lmStudio;
|
||||
let warmupPromise = null;
|
||||
function providerFor(name) {
|
||||
switch (String(name || "").toLowerCase()) {
|
||||
case "lm-studio":
|
||||
return lmStudio;
|
||||
default:
|
||||
throw new Error(`Unsupported LLM provider: ${name}`);
|
||||
}
|
||||
}
|
||||
|
||||
function validateSettings(settings) {
|
||||
if (!settings || typeof settings !== "object") throw new Error("Perry settings are required.");
|
||||
if (typeof settings.provider !== "string" || !settings.provider.trim()) throw new Error("Perry setting 'provider' is required.");
|
||||
if (typeof settings.model !== "string" || !settings.model.trim()) throw new Error("Perry setting 'model' is required.");
|
||||
|
||||
return {
|
||||
provider: settings.provider.trim().toLowerCase(),
|
||||
model: settings.model
|
||||
};
|
||||
}
|
||||
|
||||
function createPerry(settings) {
|
||||
const configured = validateSettings(settings);
|
||||
const llm = providerFor(configured.provider);
|
||||
|
||||
function warmUp() {
|
||||
if (!warmupPromise) warmupPromise = llm.warmUp(settings);
|
||||
return warmupPromise;
|
||||
return llm.warmUp(configured);
|
||||
}
|
||||
|
||||
function prepare(name, systemPrompt, segments = []) {
|
||||
@@ -27,23 +44,6 @@ module.exports = utils => {
|
||||
return { name: name || "anonymous", conversation };
|
||||
}
|
||||
|
||||
function liveOutput() {
|
||||
let channel = null;
|
||||
let wrote = false;
|
||||
function switchTo(next) {
|
||||
if (channel === next) return;
|
||||
if (wrote) process.stdout.write("\n");
|
||||
process.stdout.write(` --- ${next} ---\n`);
|
||||
channel = next;
|
||||
wrote = true;
|
||||
}
|
||||
return {
|
||||
reasoning(text) { switchTo("reasoning"); process.stdout.write(text); },
|
||||
content(text) { switchTo("response"); process.stdout.write(text); },
|
||||
end() { if (wrote) process.stdout.write("\n"); }
|
||||
};
|
||||
}
|
||||
|
||||
function addFallbackContinuation(conversation, reply) {
|
||||
if (reply.reasoning?.trim()) {
|
||||
conversation.add({
|
||||
@@ -59,29 +59,23 @@ module.exports = utils => {
|
||||
}
|
||||
|
||||
async function askConversation(agent, conversation, options = {}) {
|
||||
await warmUp();
|
||||
const callerOwnsOutput = typeof options.onReasoning === "function" || typeof options.onContent === "function";
|
||||
const reasoning = [];
|
||||
let previousResponseId = null;
|
||||
let useStatefulContinuation = false;
|
||||
|
||||
for (let continuation = 0; continuation <= MAX_CONTINUATIONS; continuation++) {
|
||||
for (let attempt = 1; attempt <= MAX_TRANSPORT_ATTEMPTS; attempt++) {
|
||||
const output = !callerOwnsOutput && options.live !== false ? liveOutput() : null;
|
||||
const sendOptions = {
|
||||
...options,
|
||||
onReasoning: options.onReasoning || output?.reasoning,
|
||||
onContent: options.onContent || output?.content
|
||||
};
|
||||
const sendOptions = { ...options };
|
||||
delete sendOptions.live;
|
||||
|
||||
if (useStatefulContinuation && previousResponseId) {
|
||||
sendOptions.previousResponseId = previousResponseId;
|
||||
sendOptions.continuationInput = CONTINUE_PROMPT;
|
||||
}
|
||||
|
||||
try {
|
||||
const reply = await llm.send(conversation, settings, sendOptions);
|
||||
output?.end();
|
||||
await warmUp();
|
||||
const reply = await llm.send(conversation, configured, sendOptions);
|
||||
if (reply.reasoning?.trim()) reasoning.push(reply.reasoning.trim());
|
||||
if (reply.content?.trim()) {
|
||||
delete reply.responseId;
|
||||
@@ -91,8 +85,7 @@ module.exports = utils => {
|
||||
if (continuation >= MAX_CONTINUATIONS) {
|
||||
throw Object.assign(new Error(`LLM returned no final response content for ${agent.name} after ${MAX_CONTINUATIONS} continuations.`), { retryable: false });
|
||||
}
|
||||
console.error();
|
||||
console.error(`LLM returned unfinished reasoning with no final response for ${agent.name}; continuing (${continuation + 1}/${MAX_CONTINUATIONS}).`);
|
||||
|
||||
if (reply.responseId) {
|
||||
previousResponseId = reply.responseId;
|
||||
useStatefulContinuation = true;
|
||||
@@ -103,15 +96,9 @@ module.exports = utils => {
|
||||
}
|
||||
break;
|
||||
} catch (error) {
|
||||
output?.end();
|
||||
const retryable = error.retryable !== false;
|
||||
console.error();
|
||||
console.error(`LLM request failed for ${agent.name} (attempt ${attempt}/${MAX_TRANSPORT_ATTEMPTS}): ${error.message}`);
|
||||
if (error.cause) console.error(` Cause: ${error.cause.code || error.cause.message || String(error.cause)}`);
|
||||
if (error.partial) console.error(" Stream ended after partial output; retry will restart this response.");
|
||||
if (!retryable || attempt === MAX_TRANSPORT_ATTEMPTS) throw error;
|
||||
const delayMs = Math.min(attempt, 5) * 1000;
|
||||
console.error(` Retrying in ${delayMs / 1000} second${delayMs === 1000 ? "" : "s"}...`);
|
||||
await new Promise(resolve => setTimeout(resolve, delayMs));
|
||||
}
|
||||
}
|
||||
@@ -133,7 +120,12 @@ module.exports = utils => {
|
||||
return askConversation(agent, conversation, options);
|
||||
}
|
||||
|
||||
function model() { return settings.model; }
|
||||
function model() {
|
||||
return configured.model;
|
||||
}
|
||||
|
||||
return { warmUp, prepare, ask, followUp, model };
|
||||
};
|
||||
}
|
||||
|
||||
createPerry.cleanText = cleanText;
|
||||
module.exports = createPerry;
|
||||
|
||||
Reference in New Issue
Block a user