Spaces:
Running
Running
File size: 7,996 Bytes
a8f5a2f 40a5fbc a8f5a2f 40a5fbc a8f5a2f ff322b9 a8f5a2f ff322b9 a8f5a2f ff322b9 a8f5a2f ff322b9 a8f5a2f ff322b9 a8f5a2f ff322b9 a8f5a2f ff322b9 a8f5a2f ff322b9 a8f5a2f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | // 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}`;
}
|