LocalWebAgent / js /agent.js
ereniko's picture
v0.5.0: more local model options and a model picker
40a5fbc unverified
Raw History Blame Contribute Delete
8 kB
// The agent harness: an LFM3 tool-calling loop over the Linux VM.
import { parseAssistant, shQuote } from "./toolcalls.js";
import { TOOLS, systemPrompt } from "./prompts.js";
export { TOOLS, systemPrompt };
// Sampling for agent steps; tuned with tools/loop-test.mjs (see README).
export const SAMPLING = { temperature: 0.1, topK: 50, repetitionPenalty: 1.1 };
const MAX_TOOL_OUTPUT = 4000;
export function truncateOutput(s, max = MAX_TOOL_OUTPUT) {
if (s.length <= max) return s;
const head = Math.floor(max * 0.35);
const tail = max - head;
return `${s.slice(0, head)}\n... [${s.length - max} characters omitted] ...\n${s.slice(-tail)}`;
}
export class Agent {
/**
* @param {{llm: import('./llm.js').LLM, vm: import('./vm.js').VM, settings: object, ui: object}} deps
* ui hooks: startTurn(), onToken(view), onToolStart(call) -> handle, onToolEnd(handle, result),
* confirmTool(call) -> Promise<boolean>, onStats(stats), onPrefill(done,total)
*/
constructor({ llm, vm, settings, ui }) {
this.llm = llm;
this.vm = vm;
this.settings = settings;
this.ui = ui;
this.history = [];
this.running = false;
this.stopRequested = false;
this.lastPromptTokens = 0;
}
clear() {
this.history = [];
this.llm.resetCache();
}
stop() {
this.stopRequested = true;
this.llm.stop();
if (this.vm.agentBusy) this.vm.interrupt();
}
async run(userText) {
if (this.running) throw new Error("Agent is already running");
this.running = true;
this.stopRequested = false;
this.history.push({ role: "user", content: userText });
try {
for (let step = 1; step <= this.settings.maxSteps; step++) {
if (this.stopRequested) break;
this.#compact();
const thinking = this.settings.thinking && this.llm.supportsThinking !== false;
const view = this.ui.startTurn(step);
let raw = "";
const res = await this.llm.generate(
[{ role: "system", content: systemPrompt() }, ...this.history],
TOOLS,
{ thinking, maxNewTokens: this.settings.maxNewTokens, thinkingBudget: this.settings.thinkingBudget, ...SAMPLING },
(ev) => {
if (ev.type === "token" || ev.type === "replace") {
raw = ev.type === "replace" ? ev.text : raw + ev.text;
view.update(parseAssistant(raw, { thinkingOpen: thinking }));
} else if (ev.type === "prefill") view.prefill(ev.done, ev.total);
},
);
this.lastPromptTokens = res.promptTokens + res.genTokens;
this.ui.onStats(res);
const text = res.text;
const parsed = parseAssistant(text, { thinkingOpen: thinking });
view.update(parsed, true);
if (res.thinkingCut) view.note(res.thinkingCut === "loop" ? "The model's reasoning started repeating, so it was cut short." : "The model's reasoning hit its length limit, so it was cut short.");
if (res.stopped) {
this.history.push({ role: "assistant", content: "<think></think>" + (parsed.content || "(stopped by the user)") });
view.note("Stopped.");
break;
}
// Store exactly what was generated (plus the "<think>" the prompt ended with),
// so the next prompt re-tokenizes to the same prefix and the KV cache is reused.
// Some chat templates end the prompt with "<think>" (LFM3), others let the model
// write it (Qwen3); don't double it.
const prefix = /^\s*<think>/.test(text) ? "" : thinking ? "<think>" : "<think></think>";
this.history.push({ role: "assistant", content: prefix + text });
const calls = parsed.calls.filter((c) => c.complete || !res.hitLimit);
if (!calls.length) {
if (res.hitLimit) view.note("The model hit its output limit for one step.");
break; // final answer
}
for (const call of calls) {
if (this.stopRequested) break;
const result = await this.#runTool(call);
this.history.push({ role: "tool", content: result });
}
if (step === this.settings.maxSteps) view.note(`Reached the step limit (${this.settings.maxSteps}). Send a message to continue.`);
}
} finally {
this.running = false;
}
}
async #runTool(call) {
const handle = this.ui.onToolStart(call);
const a = call.arguments;
let result;
try {
if (this.settings.confirmCommands && !(await this.ui.confirmTool(call, handle))) {
result = { text: "The user declined to run this.", exitCode: null, declined: true };
} else if (call.name === "run_command") {
if (!a.command?.trim()) throw new Error("missing 'command'");
const timeoutSec = Math.min(600, Math.max(5, parseInt(a.timeout, 10) || this.settings.commandTimeoutSec));
const r = await this.vm.exec(a.command, { timeoutSec });
result = { text: formatExec(r, this.#maxToolChars(), timeoutSec), exitCode: r.exitCode, output: r.output, timedOut: r.timedOut };
} else if (call.name === "write_file") {
if (!a.path) throw new Error("missing 'path'");
let content = a.content ?? "";
if (content && !content.endsWith("\n")) content += "\n"; // the call format strips it
const r = await this.vm.writeFile(a.path, content);
result = { text: formatExec(r, this.#maxToolChars()), exitCode: r.exitCode, output: r.output };
} else if (call.name === "read_file") {
if (!a.path) throw new Error("missing 'path'");
const s = Math.max(1, parseInt(a.start_line, 10) || 1);
const e = Math.max(s, parseInt(a.end_line, 10) || s + 199);
const r = await this.vm.exec(`nl -ba -w4 -s' ' -- ${shQuote(a.path)} | sed -n '${s},${e}p'`);
result = { text: formatExec(r, this.#maxToolChars()), exitCode: r.exitCode, output: r.output };
} else {
result = { text: `Unknown tool "${call.name}". Available tools: run_command, write_file, read_file.`, exitCode: null };
}
} catch (err) {
result = { text: `Tool error: ${err.message}`, exitCode: null, error: true };
}
this.ui.onToolEnd(handle, result);
return result.text;
}
// One tool result may use up to ~15% of the context budget (at least 4000 characters).
#maxToolChars() {
return Math.max(MAX_TOOL_OUTPUT, Math.round(this.settings.contextBudgetTokens * 3.2 * 0.15));
}
// Keep the prompt within budget: shorten old tool outputs first, then drop old turns.
#compact() {
const budget = this.settings.contextBudgetTokens;
const est = () => this.history.reduce((n, m) => n + Math.ceil(m.content.length / 3.2), 900);
if (est() <= budget) return;
const toolIdx = this.history.map((m, i) => (m.role === "tool" ? i : -1)).filter((i) => i >= 0);
const keep = Math.max(300, Math.round(this.#maxToolChars() / 10));
for (const i of toolIdx.slice(0, -2)) {
const c = this.history[i].content;
if (c.length > keep + 100) this.history[i] = { role: "tool", content: c.slice(0, keep) + "\n... [older output truncated to save context]" };
if (est() <= budget) return;
}
// Drop whole old exchanges (keep the latest user message and everything after it).
const lastUser = this.history.findLastIndex((m) => m.role === "user");
while (est() > budget && lastUser > 0) {
const nextUser = this.history.findIndex((m, i) => i > 0 && m.role === "user");
if (nextUser <= 0 || nextUser >= this.history.length) break;
this.history.splice(0, nextUser);
if (this.history.findLastIndex((m) => m.role === "user") === 0) break;
}
}
}
function formatExec(r, maxChars, timeoutSec) {
if (r.busy) return r.output;
const out = r.output?.length ? truncateOutput(r.output, maxChars) : "(no output)";
let head = r.exitCode === null ? "exit code: unknown" : `exit code: ${r.exitCode}`;
if (r.timedOut) head += ` (interrupted after ${timeoutSec}s timeout)`;
return `${head}\n${out}`;
}