warden-agent 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +166 -2
- package/package.json +47 -4
- package/src/agent/catalog.mjs +85 -0
- package/src/agent/config.mjs +44 -0
- package/src/agent/credentials.mjs +38 -0
- package/src/agent/doctor.mjs +75 -0
- package/src/agent/headless.mjs +35 -0
- package/src/agent/loop.mjs +105 -0
- package/src/agent/models.mjs +77 -0
- package/src/agent/prompt.mjs +53 -0
- package/src/agent/providers.mjs +244 -0
- package/src/agent/session.mjs +53 -0
- package/src/agent/texttools.mjs +85 -0
- package/src/agent/tools.mjs +226 -0
- package/src/agent/update.mjs +62 -0
- package/src/cli.mjs +71 -0
- package/src/supervisor/arena/arena.mjs +148 -0
- package/src/supervisor/arena/harnesses.mjs +112 -0
- package/src/supervisor/arena/judge.mjs +76 -0
- package/src/supervisor/arena/subjects.mjs +64 -0
- package/src/supervisor/capture.mjs +30 -0
- package/src/supervisor/cli.mjs +223 -0
- package/src/supervisor/config.mjs +33 -0
- package/src/supervisor/daemon.mjs +187 -0
- package/src/supervisor/dashboard/index.html +222 -0
- package/src/supervisor/db.mjs +86 -0
- package/src/supervisor/providers.mjs +120 -0
- package/src/supervisor/redact.mjs +37 -0
- package/src/supervisor/review.mjs +111 -0
- package/src/tui/app.mjs +500 -0
- package/src/tui/render.mjs +108 -0
- package/src/version.mjs +4 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Models you added with /add, saved in ~/.warden/config.json under agent.models:
|
|
2
|
+
// [{ name: "nemotron-free", spec: "openrouter/nvidia/nemotron-3.5-lightning:free", ctx: 262144, tools: "native" | "text" | "none" | null }]
|
|
3
|
+
// tools = how this model gets to use Warden's tools: its own tool calling, the text fallback, or chat only.
|
|
4
|
+
import { saveAgentConfig } from "./config.mjs";
|
|
5
|
+
import { chat, toolsUnsupported } from "./providers.mjs";
|
|
6
|
+
|
|
7
|
+
export const savedModels = (cfg) => (Array.isArray(cfg.models) ? cfg.models : []);
|
|
8
|
+
|
|
9
|
+
export function findModel(cfg, nameOrSpec) {
|
|
10
|
+
return savedModels(cfg).find((m) => m.name === nameOrSpec) || savedModels(cfg).find((m) => m.spec === nameOrSpec) || null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// "nemotron-free" -> its spec; "provider/model" passes through.
|
|
14
|
+
export function resolveModelInput(cfg, input) {
|
|
15
|
+
const s = String(input || "").trim();
|
|
16
|
+
const hit = savedModels(cfg).find((m) => m.name === s);
|
|
17
|
+
if (hit) return hit.spec;
|
|
18
|
+
if (s.includes("/")) return s;
|
|
19
|
+
throw new Error(`no saved model called "${s}". /model lists them; /add adds one; or use provider/model`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function persist(cfg, models) { cfg.models = models; saveAgentConfig({ models }); }
|
|
23
|
+
|
|
24
|
+
export function addModel(cfg, entry) {
|
|
25
|
+
const models = savedModels(cfg).filter((m) => m.name !== entry.name && m.spec !== entry.spec);
|
|
26
|
+
models.push(entry);
|
|
27
|
+
persist(cfg, models);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function removeModel(cfg, name) {
|
|
31
|
+
const before = savedModels(cfg);
|
|
32
|
+
const after = before.filter((m) => m.name !== name);
|
|
33
|
+
if (after.length === before.length) return false;
|
|
34
|
+
persist(cfg, after);
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function removeModelsOf(cfg, provider) {
|
|
39
|
+
const after = savedModels(cfg).filter((m) => !m.spec.startsWith(provider + "/"));
|
|
40
|
+
const n = savedModels(cfg).length - after.length;
|
|
41
|
+
if (n) persist(cfg, after);
|
|
42
|
+
return n;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function toolModeFor(cfg, spec) { return findModel(cfg, spec)?.tools || "native"; }
|
|
46
|
+
|
|
47
|
+
export function setToolMode(cfg, spec, mode) {
|
|
48
|
+
const models = savedModels(cfg).map((m) => (m.spec === spec ? { ...m, tools: mode } : m));
|
|
49
|
+
if (!models.some((m) => m.spec === spec)) return; // only remembered for saved models
|
|
50
|
+
persist(cfg, models);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Short readable default name from a model id: "nvidia/nemotron-3.5-lightning:free" -> "nemotron-3.5-lightning-free"
|
|
54
|
+
export function suggestName(id) {
|
|
55
|
+
const last = String(id).split("/").pop();
|
|
56
|
+
return last.replace(/:free$/, "-free").replace(/[^\w.-]+/g, "-").toLowerCase().slice(0, 40);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const PING = [{ name: "ping", description: "Health check. Call it whenever you are asked to.", parameters: { type: "object", properties: { message: { type: "string" } }, required: ["message"] } }];
|
|
60
|
+
const ASK = [{ role: "user", content: "Call the ping tool with message \"hello\". Do not reply with text." }];
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Works out how a model can use tools. Returns { mode: "native" | "text" | "none", detail }.
|
|
64
|
+
* Throws for errors unrelated to tools (bad key, unknown model, no credits) so the caller can show them.
|
|
65
|
+
*/
|
|
66
|
+
export async function probeToolSupport(spec, cfg, { signal, onRetry } = {}) {
|
|
67
|
+
const base = { spec, cfg, signal, onRetry, system: "You are a test harness. Follow the instruction exactly.", maxOutputTokens: 300 };
|
|
68
|
+
try {
|
|
69
|
+
const r = await chat({ ...base, messages: ASK, tools: PING, toolMode: "native" });
|
|
70
|
+
if (r.toolCalls.some((c) => c.name === "ping")) return { mode: "native", detail: "uses its own tool calling" };
|
|
71
|
+
} catch (e) {
|
|
72
|
+
if (!toolsUnsupported(e)) throw e;
|
|
73
|
+
}
|
|
74
|
+
const r2 = await chat({ ...base, messages: ASK, tools: PING, toolMode: "text" });
|
|
75
|
+
if (r2.toolCalls.some((c) => c.name === "ping")) return { mode: "text", detail: "no native tool calling; Warden's text format works" };
|
|
76
|
+
return { mode: "none", detail: "could not call tools either way; chat only (it cannot read or edit files)" };
|
|
77
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// System prompt: who the agent is, how to use the tools, and facts about this machine and project.
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { execFileSync } from "node:child_process";
|
|
6
|
+
import { detectShell } from "./tools.mjs";
|
|
7
|
+
|
|
8
|
+
const PROJECT_FILES = ["WARDEN.md", "AGENTS.md", "CLAUDE.md"];
|
|
9
|
+
|
|
10
|
+
function gitInfo(cwd) {
|
|
11
|
+
try {
|
|
12
|
+
const opts = { cwd, encoding: "utf8", timeout: 3000, stdio: ["ignore", "pipe", "ignore"], windowsHide: true };
|
|
13
|
+
const branch = execFileSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], opts).trim();
|
|
14
|
+
const status = execFileSync("git", ["status", "--porcelain"], opts).trim().split("\n").filter(Boolean);
|
|
15
|
+
return `git repo, branch ${branch}, ${status.length} changed file(s)${status.length ? ":\n" + status.slice(0, 20).join("\n") : ""}`;
|
|
16
|
+
} catch { return "not a git repository"; }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function projectInstructions(cwd) {
|
|
20
|
+
for (const f of PROJECT_FILES) {
|
|
21
|
+
const p = path.join(cwd, f);
|
|
22
|
+
if (fs.existsSync(p)) {
|
|
23
|
+
const t = fs.readFileSync(p, "utf8");
|
|
24
|
+
return { file: f, text: t.length > 20000 ? t.slice(0, 20000) + "\n[truncated]" : t };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function systemPrompt({ cwd, mode }) {
|
|
31
|
+
const proj = projectInstructions(cwd);
|
|
32
|
+
return `You are Warden, a coding agent running in the user's terminal. You help with software engineering tasks in the project at the working directory: reading and changing code, running commands and tests, and explaining what you find.
|
|
33
|
+
|
|
34
|
+
How to work:
|
|
35
|
+
- Use the tools to look before you act. Read a file before editing it. Search with glob/grep instead of guessing paths.
|
|
36
|
+
- Make the smallest change that does what was asked. Do not refactor, rename or add features nobody asked for.
|
|
37
|
+
- Prefer edit_file for changes to existing files; use write_file only for new files or full rewrites.
|
|
38
|
+
- After changing code, run the relevant tests or a quick check with bash when one exists, and say what you ran and what happened. If you did not verify something, say so plainly.
|
|
39
|
+
- Use bash for commands (tests, builds, git, package managers). Do not use it to read or write files.
|
|
40
|
+
- Never claim you did something you did not do. If a tool fails, read the error and adjust; do not repeat the same failing call.
|
|
41
|
+
- If the user denies a tool call, do not retry it the same way; adapt or ask.
|
|
42
|
+
- Do not run destructive commands (deleting data, force-pushing, rewriting git history) unless the user explicitly asked.
|
|
43
|
+
- Keep replies short and direct. Use Markdown sparingly; code in fenced blocks. Refer to code as path:line.
|
|
44
|
+
- When the task is done, stop and give a brief summary of what changed.
|
|
45
|
+
|
|
46
|
+
Environment:
|
|
47
|
+
- Working directory (project root): ${cwd}
|
|
48
|
+
- OS: ${os.platform()} ${os.release()}; shell for the bash tool: ${detectShell().name}
|
|
49
|
+
- Date: ${new Date().toISOString().slice(0, 10)}
|
|
50
|
+
- ${gitInfo(cwd)}
|
|
51
|
+
- Permission mode: ${mode} (the user approves edits and commands that need it; you will get "User denied" if they refuse)
|
|
52
|
+
${proj ? `\nProject instructions from ${proj.file} (written by the project's authors; follow them unless they conflict with the user):\n<project_instructions>\n${proj.text}\n</project_instructions>\n` : ""}`;
|
|
53
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// Model providers for the Warden agent. Two wire protocols cover almost everything:
|
|
2
|
+
// "openai" Chat Completions with streaming + tool calls. Used by OpenAI, Gemini (Google's OpenAI-compatible
|
|
3
|
+
// endpoint), OpenRouter, Groq, Ollama, LM Studio, and any other compatible server.
|
|
4
|
+
// "anthropic" Messages API (non-streaming here) with tool use.
|
|
5
|
+
//
|
|
6
|
+
// Internal message format (provider-neutral):
|
|
7
|
+
// { role: "user", content: string }
|
|
8
|
+
// { role: "assistant", content: string, toolCalls?: [{ id, name, args }] }
|
|
9
|
+
// { role: "tool", toolCallId, name, content: string }
|
|
10
|
+
|
|
11
|
+
import { CATALOG } from "./catalog.mjs";
|
|
12
|
+
import { savedKey } from "./credentials.mjs";
|
|
13
|
+
import { textToolsPrompt, toTextProtocol, parseTextToolCalls, ToolTagFilter } from "./texttools.mjs";
|
|
14
|
+
|
|
15
|
+
export const PROVIDERS = CATALOG;
|
|
16
|
+
|
|
17
|
+
// "gemini/gemini-2.5-flash" -> { provider: "gemini", model: "gemini-2.5-flash" }. Model ids may contain "/" (openrouter).
|
|
18
|
+
export function parseModelSpec(spec) {
|
|
19
|
+
const i = String(spec || "").indexOf("/");
|
|
20
|
+
if (i <= 0) throw new Error(`model must look like provider/model, e.g. gemini/gemini-2.5-flash (got "${spec}")`);
|
|
21
|
+
return { provider: spec.slice(0, i), model: spec.slice(i + 1) };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function resolveProvider(name, cfg = {}) {
|
|
25
|
+
const p = { ...PROVIDERS[name], ...(cfg.providers?.[name] || {}) };
|
|
26
|
+
if (!p.api || !p.baseUrl) throw new Error(`unknown provider "${name}". Built in: ${Object.keys(PROVIDERS).join(", ")}. Add others under "providers" in ~/.warden/config.json`);
|
|
27
|
+
return p;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Environment variables win; then a key saved with /add; local servers need none.
|
|
31
|
+
export function keyFor(name, p, env = process.env) {
|
|
32
|
+
for (const v of p.keyEnv || []) if (env[v]) return env[v];
|
|
33
|
+
const saved = savedKey(name);
|
|
34
|
+
if (saved) return saved;
|
|
35
|
+
if (!p.keyEnv?.length) return null;
|
|
36
|
+
throw new Error(`no API key for ${name}: run /add and pick ${p.label || name}, or set ${p.keyEnv.join(" or ")}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function keyStatus(name, cfg = {}, env = process.env) {
|
|
40
|
+
const p = resolveProvider(name, cfg);
|
|
41
|
+
const v = (p.keyEnv || []).find((k) => env[k]);
|
|
42
|
+
if (v) return `key from ${v}`;
|
|
43
|
+
if (savedKey(name)) return "key saved by /add";
|
|
44
|
+
if (!p.keyEnv?.length) return "no key needed";
|
|
45
|
+
return `missing key (${p.keyEnv.join(" or ")})`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// A provider that rejects the tools parameter answers 400/404/422 and usually names tools/functions in the error.
|
|
49
|
+
export const toolsUnsupported = (e) => [400, 404, 422].includes(e?.status) && /tool|function/i.test(e?.message || "");
|
|
50
|
+
|
|
51
|
+
// ---- conversion ------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
export function toOpenAI(system, messages) {
|
|
54
|
+
const out = [{ role: "system", content: system }];
|
|
55
|
+
for (const m of messages) {
|
|
56
|
+
if (m.role === "user") out.push({ role: "user", content: m.content });
|
|
57
|
+
else if (m.role === "assistant") {
|
|
58
|
+
const msg = { role: "assistant", content: m.content || "" };
|
|
59
|
+
if (m.toolCalls?.length) {
|
|
60
|
+
msg.tool_calls = m.toolCalls.map((c) => ({ id: c.id, type: "function", function: { name: c.name, arguments: JSON.stringify(c.args ?? {}) } }));
|
|
61
|
+
if (!m.content) msg.content = null;
|
|
62
|
+
}
|
|
63
|
+
out.push(msg);
|
|
64
|
+
} else if (m.role === "tool") out.push({ role: "tool", tool_call_id: m.toolCallId, content: m.content });
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function toAnthropic(messages) {
|
|
70
|
+
const out = [];
|
|
71
|
+
for (const m of messages) {
|
|
72
|
+
if (m.role === "user") out.push({ role: "user", content: [{ type: "text", text: m.content }] });
|
|
73
|
+
else if (m.role === "assistant") {
|
|
74
|
+
const content = [];
|
|
75
|
+
if (m.content) content.push({ type: "text", text: m.content });
|
|
76
|
+
for (const c of m.toolCalls || []) content.push({ type: "tool_use", id: c.id, name: c.name, input: c.args ?? {} });
|
|
77
|
+
out.push({ role: "assistant", content: content.length ? content : [{ type: "text", text: "(no output)" }] });
|
|
78
|
+
} else if (m.role === "tool") {
|
|
79
|
+
const block = { type: "tool_result", tool_use_id: m.toolCallId, content: m.content };
|
|
80
|
+
const last = out.at(-1);
|
|
81
|
+
// consecutive tool results belong in one user message
|
|
82
|
+
if (last?.role === "user" && last.content.every((b) => b.type === "tool_result")) last.content.push(block);
|
|
83
|
+
else out.push({ role: "user", content: [block] });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const toolsOpenAI = (tools) => tools.map((t) => ({ type: "function", function: { name: t.name, description: t.description, parameters: t.parameters } }));
|
|
90
|
+
const toolsAnthropic = (tools) => tools.map((t) => ({ name: t.name, description: t.description, input_schema: t.parameters }));
|
|
91
|
+
|
|
92
|
+
// ---- HTTP with retries (free tiers rate-limit a lot) -----------------------
|
|
93
|
+
|
|
94
|
+
const sleep = (ms, signal) => new Promise((res, rej) => {
|
|
95
|
+
const t = setTimeout(res, ms);
|
|
96
|
+
signal?.addEventListener("abort", () => { clearTimeout(t); rej(new DOMException("aborted", "AbortError")); }, { once: true });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
async function postWithRetry(url, headers, body, { signal, onRetry, retries = 4 }) {
|
|
100
|
+
for (let attempt = 0; ; attempt++) {
|
|
101
|
+
const r = await fetch(url, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body), signal });
|
|
102
|
+
if (r.ok) return r;
|
|
103
|
+
const text = await r.text().catch(() => "");
|
|
104
|
+
let msg = text, hintedDelay = null;
|
|
105
|
+
try {
|
|
106
|
+
const j = JSON.parse(text);
|
|
107
|
+
const err = Array.isArray(j) ? j[0]?.error : j.error;
|
|
108
|
+
msg = err?.message || j.message || text;
|
|
109
|
+
// Google puts the wait time in error.details[].retryDelay, e.g. "33s"
|
|
110
|
+
const rd = (err?.details || []).find((d) => d?.retryDelay)?.retryDelay;
|
|
111
|
+
if (rd) hintedDelay = parseFloat(rd) * 1000;
|
|
112
|
+
} catch {}
|
|
113
|
+
const ra = Number(r.headers.get("retry-after"));
|
|
114
|
+
if (Number.isFinite(ra) && ra > 0) hintedDelay ??= ra * 1000;
|
|
115
|
+
const fail = (extra = "") => { const e = new Error(`${r.status} ${String(msg).replace(/\s+/g, " ").slice(0, 400)}${extra}`); e.status = r.status; return e; };
|
|
116
|
+
const retryable = r.status === 429 || r.status >= 500;
|
|
117
|
+
if (!retryable || attempt >= retries) throw fail();
|
|
118
|
+
// A long or daily quota is not worth waiting for: say so now instead of retrying.
|
|
119
|
+
if (r.status === 429 && (hintedDelay > 60_000 || (/per ?day|daily|PerDay/i.test(msg) && !hintedDelay))) {
|
|
120
|
+
const e = fail(" [quota used up for now; switch model with /model or try again later]");
|
|
121
|
+
e.quota = true;
|
|
122
|
+
throw e;
|
|
123
|
+
}
|
|
124
|
+
const wait = hintedDelay != null ? Math.min(Math.max(hintedDelay, 1000), 60_000) : Math.min(2000 * 2 ** attempt, 30_000);
|
|
125
|
+
onRetry?.({ status: r.status, waitMs: wait, attempt: attempt + 1, message: String(msg).slice(0, 160) });
|
|
126
|
+
await sleep(wait, signal);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ---- streaming chat --------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* One model call. Streams text through onText. Returns { text, toolCalls, usage, stopReason }.
|
|
134
|
+
*/
|
|
135
|
+
export async function chat({ spec, system, messages, tools, cfg = {}, signal, onText = () => {}, onRetry, maxOutputTokens = 8192, env = process.env, toolMode = "native" }) {
|
|
136
|
+
const { provider, model } = parseModelSpec(spec);
|
|
137
|
+
const p = resolveProvider(provider, cfg);
|
|
138
|
+
const key = keyFor(provider, p, env);
|
|
139
|
+
const send = (o) => (p.api === "anthropic" ? anthropicChat({ p, key, model, signal, onRetry, maxOutputTokens, ...o }) : openaiChat({ p, key, model, signal, onRetry, maxOutputTokens, ...o }));
|
|
140
|
+
|
|
141
|
+
if (toolMode === "text" && tools?.length) {
|
|
142
|
+
// the model writes <tool_call> blocks; hide them from the stream and parse them afterwards
|
|
143
|
+
const filter = new ToolTagFilter(onText);
|
|
144
|
+
const res = await send({ system: system + textToolsPrompt(tools), messages: toTextProtocol(messages), tools: null, onText: (d) => filter.write(d) });
|
|
145
|
+
filter.end();
|
|
146
|
+
const parsed = parseTextToolCalls(res.text);
|
|
147
|
+
return { ...res, text: parsed.text, toolCalls: parsed.toolCalls };
|
|
148
|
+
}
|
|
149
|
+
if (toolMode === "none" || !tools?.length) {
|
|
150
|
+
const note = "\n\nNote: this model is in chat-only mode. You cannot read files or run commands; answer from the conversation and ask the user to paste what you need.";
|
|
151
|
+
return send({ system: toolMode === "none" ? system + note : system, messages: toTextProtocol(messages), tools: null, onText });
|
|
152
|
+
}
|
|
153
|
+
return send({ system, messages, tools, onText });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function openaiChat({ p, key, model, system, messages, tools, signal, onText, onRetry, maxOutputTokens }) {
|
|
157
|
+
const body = { model, messages: toOpenAI(system, messages), stream: true, [p.maxTokensParam || "max_tokens"]: maxOutputTokens };
|
|
158
|
+
if (tools?.length) body.tools = toolsOpenAI(tools);
|
|
159
|
+
if (p.streamUsage !== false && !p.local) body.stream_options = { include_usage: true };
|
|
160
|
+
const headers = key ? { authorization: `Bearer ${key}` } : {};
|
|
161
|
+
let r;
|
|
162
|
+
try {
|
|
163
|
+
r = await postWithRetry(`${p.baseUrl}/chat/completions`, headers, body, { signal, onRetry });
|
|
164
|
+
} catch (e) {
|
|
165
|
+
// some compatible servers reject stream_options; retry once without it
|
|
166
|
+
if (e.status === 400 && body.stream_options && /stream_options|include_usage|unknown/i.test(e.message)) {
|
|
167
|
+
delete body.stream_options;
|
|
168
|
+
r = await postWithRetry(`${p.baseUrl}/chat/completions`, headers, body, { signal, onRetry });
|
|
169
|
+
} else throw e;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let text = "", stopReason = null, usage = null, buf = "";
|
|
173
|
+
const calls = new Map(); // index -> {id, name, args}
|
|
174
|
+
const dec = new TextDecoder();
|
|
175
|
+
const handle = (data) => {
|
|
176
|
+
if (data === "[DONE]") return;
|
|
177
|
+
let j; try { j = JSON.parse(data); } catch { return; }
|
|
178
|
+
if (j.error) throw new Error(j.error.message || JSON.stringify(j.error));
|
|
179
|
+
if (j.usage) usage = { input: j.usage.prompt_tokens ?? 0, output: j.usage.completion_tokens ?? 0 };
|
|
180
|
+
const ch = j.choices?.[0];
|
|
181
|
+
if (!ch) return;
|
|
182
|
+
const d = ch.delta || ch.message || {};
|
|
183
|
+
if (typeof d.content === "string" && d.content) { text += d.content; onText(d.content); }
|
|
184
|
+
for (const tc of d.tool_calls || []) {
|
|
185
|
+
const idx = tc.index ?? calls.size;
|
|
186
|
+
const cur = calls.get(idx) || { id: "", name: "", args: "" };
|
|
187
|
+
if (tc.id) cur.id = tc.id;
|
|
188
|
+
if (tc.function?.name) cur.name += tc.function.name;
|
|
189
|
+
if (tc.function?.arguments) cur.args += typeof tc.function.arguments === "string" ? tc.function.arguments : JSON.stringify(tc.function.arguments);
|
|
190
|
+
calls.set(idx, cur);
|
|
191
|
+
}
|
|
192
|
+
if (ch.finish_reason) stopReason = ch.finish_reason;
|
|
193
|
+
};
|
|
194
|
+
for await (const chunk of r.body) {
|
|
195
|
+
buf += dec.decode(chunk, { stream: true });
|
|
196
|
+
let i;
|
|
197
|
+
while ((i = buf.indexOf("\n")) >= 0) {
|
|
198
|
+
const line = buf.slice(0, i).trim(); buf = buf.slice(i + 1);
|
|
199
|
+
if (line.startsWith("data:")) handle(line.slice(5).trim());
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (buf.trim().startsWith("data:")) handle(buf.trim().slice(5).trim());
|
|
203
|
+
|
|
204
|
+
const toolCalls = [...calls.entries()].sort((a, b) => a[0] - b[0]).map(([i, c], n) => {
|
|
205
|
+
let args = {};
|
|
206
|
+
try { args = c.args ? JSON.parse(c.args) : {}; } catch { args = { __invalid_json: c.args }; }
|
|
207
|
+
return { id: c.id || `call_${Date.now().toString(36)}_${n}`, name: c.name, args };
|
|
208
|
+
}).filter((c) => c.name);
|
|
209
|
+
return { text, toolCalls, usage, stopReason };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function anthropicChat({ p, key, model, system, messages, tools, signal, onText, onRetry, maxOutputTokens }) {
|
|
213
|
+
const body = { model, system, max_tokens: maxOutputTokens, messages: toAnthropic(messages) };
|
|
214
|
+
if (tools?.length) body.tools = toolsAnthropic(tools);
|
|
215
|
+
const r = await postWithRetry(`${p.baseUrl}/messages`, { "x-api-key": key, "anthropic-version": "2023-06-01" }, body, { signal, onRetry });
|
|
216
|
+
const j = await r.json();
|
|
217
|
+
let text = "";
|
|
218
|
+
const toolCalls = [];
|
|
219
|
+
for (const b of j.content || []) {
|
|
220
|
+
if (b.type === "text") text += b.text;
|
|
221
|
+
if (b.type === "tool_use") toolCalls.push({ id: b.id, name: b.name, args: b.input || {} });
|
|
222
|
+
}
|
|
223
|
+
if (text) onText(text);
|
|
224
|
+
return { text, toolCalls, usage: j.usage ? { input: j.usage.input_tokens ?? 0, output: j.usage.output_tokens ?? 0 } : null, stopReason: j.stop_reason };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Models a provider offers: [{ id, ctx, free, tools }] (null = unknown). Uses the source's public list when it has one.
|
|
228
|
+
export async function listModels(name, cfg = {}, env = process.env) {
|
|
229
|
+
const p = resolveProvider(name, cfg);
|
|
230
|
+
let key = null;
|
|
231
|
+
try { key = keyFor(name, p, env); } catch (e) { if (!p.publicList) throw e; }
|
|
232
|
+
const fetchJson = async (url) => {
|
|
233
|
+
const r = await fetch(url, { headers: key ? { authorization: `Bearer ${key}` } : {}, signal: AbortSignal.timeout(20000) });
|
|
234
|
+
if (!r.ok) { const e = new Error(`${r.status} ${(await r.text()).replace(/\s+/g, " ").slice(0, 200)}`); e.status = r.status; throw e; }
|
|
235
|
+
return r.json();
|
|
236
|
+
};
|
|
237
|
+
let list;
|
|
238
|
+
if (p.list) list = await p.list(fetchJson);
|
|
239
|
+
else if (p.api === "openai") {
|
|
240
|
+
const j = await fetchJson(`${p.baseUrl}/models`);
|
|
241
|
+
list = (j.data || j.models || []).map((m) => ({ id: String(m.id || m.name || "").replace(/^models\//, ""), ctx: m.context_length ?? m.context_window ?? null, free: null, tools: null }));
|
|
242
|
+
} else throw new Error(`listing models is not supported for ${name}; type the model id instead`);
|
|
243
|
+
return list.filter((m) => m.id).sort((a, b) => a.id.localeCompare(b.id));
|
|
244
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Sessions: one JSONL file per conversation in ~/.warden/sessions. Line 1 is metadata, then one message per line.
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import crypto from "node:crypto";
|
|
5
|
+
import { SESSIONS_DIR } from "./config.mjs";
|
|
6
|
+
|
|
7
|
+
export class Session {
|
|
8
|
+
constructor({ cwd, model, id, messages = [], title = null, created = Date.now() }) {
|
|
9
|
+
this.id = id || new Date().toISOString().replace(/[-:T]/g, "").slice(0, 14) + "-" + crypto.randomBytes(3).toString("hex");
|
|
10
|
+
this.cwd = cwd; this.model = model; this.messages = messages; this.title = title; this.created = created;
|
|
11
|
+
this.usage = { input: 0, output: 0, calls: 0 };
|
|
12
|
+
this.file = path.join(SESSIONS_DIR, this.id + ".jsonl");
|
|
13
|
+
this.persisted = messages.length;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
push(msg) { this.messages.push(msg); if (!this.title && msg.role === "user") this.title = msg.content.slice(0, 80); this.save(); }
|
|
17
|
+
|
|
18
|
+
save() {
|
|
19
|
+
try {
|
|
20
|
+
fs.mkdirSync(SESSIONS_DIR, { recursive: true });
|
|
21
|
+
if (!fs.existsSync(this.file) || this.persisted === 0) {
|
|
22
|
+
fs.writeFileSync(this.file, JSON.stringify({ meta: { id: this.id, cwd: this.cwd, model: this.model, created: this.created, title: this.title } }) + "\n");
|
|
23
|
+
this.persisted = 0;
|
|
24
|
+
}
|
|
25
|
+
const fresh = this.messages.slice(this.persisted);
|
|
26
|
+
if (fresh.length) fs.appendFileSync(this.file, fresh.map((m) => JSON.stringify(m)).join("\n") + "\n");
|
|
27
|
+
this.persisted = this.messages.length;
|
|
28
|
+
} catch { /* saving is best effort; never break the conversation */ }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static list(limit = 20) {
|
|
32
|
+
let files = [];
|
|
33
|
+
try { files = fs.readdirSync(SESSIONS_DIR).filter((f) => f.endsWith(".jsonl")); } catch { return []; }
|
|
34
|
+
return files.map((f) => {
|
|
35
|
+
const p = path.join(SESSIONS_DIR, f);
|
|
36
|
+
try {
|
|
37
|
+
const lines = fs.readFileSync(p, "utf8").split("\n").filter(Boolean);
|
|
38
|
+
const meta = JSON.parse(lines[0]).meta || {};
|
|
39
|
+
const firstUser = lines.slice(1).map((l) => { try { return JSON.parse(l); } catch { return null; } }).find((m) => m?.role === "user");
|
|
40
|
+
return { ...meta, title: meta.title || firstUser?.content?.slice(0, 80) || "(empty)", messages: lines.length - 1, mtime: fs.statSync(p).mtimeMs, file: p };
|
|
41
|
+
} catch { return null; }
|
|
42
|
+
}).filter(Boolean).sort((a, b) => b.mtime - a.mtime).slice(0, limit);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static load(idOrPrefix) {
|
|
46
|
+
const hit = Session.list(1000).filter((s) => s.id.startsWith(idOrPrefix));
|
|
47
|
+
if (hit.length !== 1) throw new Error(hit.length ? `"${idOrPrefix}" matches ${hit.length} sessions` : `no session "${idOrPrefix}"`);
|
|
48
|
+
const lines = fs.readFileSync(hit[0].file, "utf8").split("\n").filter(Boolean);
|
|
49
|
+
const meta = JSON.parse(lines[0]).meta;
|
|
50
|
+
const messages = lines.slice(1).map((l) => JSON.parse(l));
|
|
51
|
+
return new Session({ ...meta, messages });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Tool calling for models that do not support it natively: the model writes calls as tagged JSON in its reply,
|
|
2
|
+
// <tool_call>{"name": "read_file", "arguments": {"path": "calc.py"}}</tool_call>
|
|
3
|
+
// Warden parses them, runs them with the usual approvals, and sends results back as <tool_result> blocks.
|
|
4
|
+
|
|
5
|
+
export function textToolsPrompt(tools) {
|
|
6
|
+
const list = tools.map((t) => `- ${t.name}: ${t.description}\n arguments (JSON schema): ${JSON.stringify(t.parameters)}`).join("\n");
|
|
7
|
+
return `
|
|
8
|
+
|
|
9
|
+
# Tools
|
|
10
|
+
You can use tools by writing a tool call in your reply, exactly in this format, and then STOP and wait for the result:
|
|
11
|
+
|
|
12
|
+
<tool_call>
|
|
13
|
+
{"name": "TOOL_NAME", "arguments": { ... }}
|
|
14
|
+
</tool_call>
|
|
15
|
+
|
|
16
|
+
Rules: the content between the tags must be valid JSON. You may write a short sentence before a tool call. You may make several calls in one reply. After you stop, the results come back in <tool_result> blocks. When you need no more tools, answer normally without any <tool_call>.
|
|
17
|
+
|
|
18
|
+
Available tools:
|
|
19
|
+
${list}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Converts the neutral conversation into plain user/assistant turns for a model without native tools.
|
|
23
|
+
export function toTextProtocol(messages) {
|
|
24
|
+
const out = [];
|
|
25
|
+
const push = (role, content) => {
|
|
26
|
+
const last = out.at(-1);
|
|
27
|
+
if (last && last.role === role) last.content += "\n\n" + content; // keep strict user/assistant alternation
|
|
28
|
+
else out.push({ role, content });
|
|
29
|
+
};
|
|
30
|
+
for (const m of messages) {
|
|
31
|
+
if (m.role === "user") push("user", m.content);
|
|
32
|
+
else if (m.role === "assistant") {
|
|
33
|
+
const calls = (m.toolCalls || []).map((c) => `<tool_call>\n${JSON.stringify({ name: c.name, arguments: c.args ?? {} })}\n</tool_call>`).join("\n");
|
|
34
|
+
push("assistant", [m.content, calls].filter(Boolean).join("\n") || "(no output)");
|
|
35
|
+
} else if (m.role === "tool") push("user", `<tool_result name="${m.name}">\n${m.content}\n</tool_result>`);
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Pulls tool calls out of a reply. Returns { text (without the tags), toolCalls }.
|
|
41
|
+
export function parseTextToolCalls(reply, idPrefix = "tc") {
|
|
42
|
+
const toolCalls = [];
|
|
43
|
+
const re = /<tool_call>\s*([\s\S]*?)\s*<\/tool_call>/g;
|
|
44
|
+
let m, n = 0;
|
|
45
|
+
while ((m = re.exec(reply))) {
|
|
46
|
+
let body = m[1].trim().replace(/^```(?:json)?\s*/i, "").replace(/\s*```$/, "");
|
|
47
|
+
let obj = null;
|
|
48
|
+
try { obj = JSON.parse(body); } catch {
|
|
49
|
+
// be forgiving about trailing commas and single quotes, which small models produce
|
|
50
|
+
try { obj = JSON.parse(body.replace(/,\s*([}\]])/g, "$1")); } catch { obj = null; }
|
|
51
|
+
}
|
|
52
|
+
const name = obj?.name || obj?.tool || obj?.function;
|
|
53
|
+
const args = obj?.arguments ?? obj?.args ?? obj?.parameters ?? {};
|
|
54
|
+
toolCalls.push(name ? { id: `${idPrefix}_${Date.now().toString(36)}_${n++}`, name: String(name), args: typeof args === "string" ? safeJson(args) : args }
|
|
55
|
+
: { id: `${idPrefix}_${Date.now().toString(36)}_${n++}`, name: "__invalid__", args: { __invalid_json: body } });
|
|
56
|
+
}
|
|
57
|
+
const text = reply.replace(re, "").replace(/<tool_call>[\s\S]*$/, "").replace(/\n{3,}/g, "\n\n").trim();
|
|
58
|
+
return { text, toolCalls };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function safeJson(s) { try { return JSON.parse(s); } catch { return { __invalid_json: s }; } }
|
|
62
|
+
|
|
63
|
+
// Streaming filter: hides <tool_call>…</tool_call> from what the user sees while the reply streams in.
|
|
64
|
+
export class ToolTagFilter {
|
|
65
|
+
constructor(emit) { this.emit = emit; this.buf = ""; this.inside = false; }
|
|
66
|
+
write(chunk) {
|
|
67
|
+
this.buf += chunk;
|
|
68
|
+
for (;;) {
|
|
69
|
+
if (this.inside) {
|
|
70
|
+
const end = this.buf.indexOf("</tool_call>");
|
|
71
|
+
if (end < 0) { this.buf = this.buf.slice(-12); return; }
|
|
72
|
+
this.buf = this.buf.slice(end + 12); this.inside = false;
|
|
73
|
+
} else {
|
|
74
|
+
const start = this.buf.indexOf("<tool_call>");
|
|
75
|
+
if (start >= 0) { if (start) this.emit(this.buf.slice(0, start)); this.buf = this.buf.slice(start + 11); this.inside = true; continue; }
|
|
76
|
+
// keep a possible partial "<tool_call" at the end
|
|
77
|
+
const lt = this.buf.lastIndexOf("<");
|
|
78
|
+
if (lt >= 0 && "<tool_call>".startsWith(this.buf.slice(lt))) { if (lt) this.emit(this.buf.slice(0, lt)); this.buf = this.buf.slice(lt); return; }
|
|
79
|
+
if (this.buf) this.emit(this.buf);
|
|
80
|
+
this.buf = ""; return;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
end() { if (!this.inside && this.buf) this.emit(this.buf); this.buf = ""; }
|
|
85
|
+
}
|