const history = [];
const MAX_TASKS = 3;
const RESULT = 2000;
const HUB_EXAMPLE = ["run llama 3.2 1b from Hugging Face", "llama 3.2 1b"];
function routingPrompt(catalog, request, done) {
let out = "Turn the request into one tesserax command, as JSON.\nTools:\n";
out += "- reply: answer in text. Use it for questions, chat and anything else.\n";
for (const t of catalog.tools) {
const args = [];
if (t.source) args.push("model or hub");
for (const k of Object.keys((t.args && t.args.properties) || {})) args.push(k);
out += `- ${t.name} (${args.join(", ")}): ${t.about}\n`;
}
out += "Models:\n";
for (const m of catalog.models) {
out += `- ${m.name} (${m.kind}): ${m.about || ""}\n`;
}
for (const f of catalog.families || []) {
out += `- ${f.family} (also ${(f.aliases || []).join(", ")})\n`;
}
out += "model is a name from this list. For any other model, hub holds the words\n";
out += "the request uses for it, and tesserax finds it on Hugging Face.\n";
out += "A request for a kind of model, such as an image model, takes model.\n";
out += "knobs is optional. Set a knob only when the request asks for it:\n";
for (const k of catalog.knobs || []) {
out += `- ${k.name}: ${k.about}\n`;
}
out += 'Request: what is the capital of France?\n{"tool":"reply"}\n';
for (const t of catalog.tools) {
out += `Request: ${t.example.request}\n${JSON.stringify(t.example.pick)}\n`;
}
const serve = catalog.tools.find((t) => t.kind === "generate");
if (serve) {
out += `Request: ${HUB_EXAMPLE[0]}\n${JSON.stringify({ tool: serve.name, hub: HUB_EXAMPLE[1] })}\n`;
const base = serve.example && serve.example.pick;
if (base && typeof base.model === "string") {
const profiled = Object.assign({}, base, { knobs: { profile: "1" } });
out += `Request: profile the ${base.model} boot\n${JSON.stringify(profiled)}\n`;
}
}
out += `Request: ${request}\n`;
if (done.length === 0) return out;
out += "Commands done so far for this request:\n";
for (const d of done) out += `- ${JSON.stringify(d.pick)}\n`;
return out + "Give the next command the request still needs, or reply when all are done.\n";
}
function pickCommand(catalog, request, done) {
if (!catalog.guided) return { tool: "reply" };
const content = routingPrompt(catalog, request, done);
const raw = host("generate", {
messages: [{ role: "user", content }],
format: catalog.pick,
step: "picking a command...",
done: "picked a command",
});
try {
const parsed = JSON.parse(String(raw).trim());
return parsed && typeof parsed === "object" ? parsed : { tool: "reply" };
} catch {
return { tool: "reply" };
}
}
function trimResult(value) {
const text = JSON.stringify(value);
return text.length > RESULT ? text.slice(0, RESULT) + "…" : text;
}
function runRequest(request) {
const catalog = host("tasks", null);
const byName = new Map(catalog.tools.map((t) => [t.name, t]));
const mark = history.length;
history.push({ role: "user", content: request });
const done = [];
try {
while (done.length < MAX_TASKS) {
const chosen = pickCommand(catalog, request, done);
const key = JSON.stringify(chosen);
if (chosen.tool === "reply" || done.some((d) => d.key === key)) break;
const tool = byName.get(chosen.tool);
if (tool && tool.stays) {
const ran = host("replace", chosen);
if (ran === null && done.length === 0) history.length = mark;
return;
}
const result = host("run", chosen);
if (result === null) {
if (done.length === 0) {
history.length = mark;
return;
}
break;
}
const text = trimResult(result);
done.push({ key, pick: chosen, result: text });
const id = `call_${history.length}`;
const args = Object.assign({}, chosen);
delete args.tool;
const call = { id, type: "function", function: { name: chosen.tool, arguments: args } };
history.push({ role: "assistant", content: null, tool_calls: [call] });
history.push({ role: "tool", content: text, tool_call_id: id, name: chosen.tool });
}
if (done.length > 0) {
const noun = done.length === 1 ? "that task" : "those tasks";
history.push({ role: "user", content: `Tell me the result of ${noun} in a sentence or two.` });
}
const answer = host("generate", { messages: history, show: true });
history.push({ role: "assistant", content: String(answer).trim() });
} catch (e) {
history.length = mark;
throw e;
}
}
export function turn({ request }) {
runRequest(request);
}
export function oneshot({ request }) {
history.length = 0;
runRequest(request);
}