warden-agent 0.0.0-stage → 0.4.1
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 +177 -2
- package/package.json +47 -4
- package/src/agent/catalog.mjs +85 -0
- package/src/agent/config.mjs +47 -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 +167 -0
- package/src/agent/models.mjs +77 -0
- package/src/agent/prompt.mjs +57 -0
- package/src/agent/providers.mjs +319 -0
- package/src/agent/session.mjs +53 -0
- package/src/agent/texttools.mjs +91 -0
- package/src/agent/tools.mjs +416 -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 +640 -0
- package/src/tui/render.mjs +130 -0
- package/src/version.mjs +4 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// The agent loop: send the conversation to the model, run the tools it asks for (with approvals), repeat until it answers.
|
|
2
|
+
import { chat, toolsUnsupported } from "./providers.mjs";
|
|
3
|
+
import { TOOLS, toolByName, needsApproval, describeCall } from "./tools.mjs";
|
|
4
|
+
import { systemPrompt } from "./prompt.mjs";
|
|
5
|
+
import { findModel, toolModeFor, setToolMode } from "./models.mjs";
|
|
6
|
+
|
|
7
|
+
const isAbort = (e) => e?.name === "AbortError" || /aborted/i.test(e?.message || "");
|
|
8
|
+
|
|
9
|
+
// Keep the conversation under a rough token budget by blanking the oldest tool outputs first.
|
|
10
|
+
export function trimContext(messages, budgetTokens) {
|
|
11
|
+
const size = () => messages.reduce((n, m) => n + (m.content?.length || 0) + JSON.stringify(m.toolCalls || "").length, 0) / 4;
|
|
12
|
+
if (size() <= budgetTokens) return 0;
|
|
13
|
+
let trimmed = 0;
|
|
14
|
+
for (let i = 0; i < messages.length - 6 && size() > budgetTokens; i++) {
|
|
15
|
+
const m = messages[i];
|
|
16
|
+
if (m.role === "tool" && m.content.length > 200 && !m.trimmed) { m.content = "[old tool output removed to save context]"; m.trimmed = true; trimmed++; }
|
|
17
|
+
}
|
|
18
|
+
return trimmed;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// File contents the model already wrote are on disk; keeping every full copy in the conversation makes each later
|
|
22
|
+
// request bigger and slower (and burns free-tier token limits). Older write_file / edit_file arguments are replaced
|
|
23
|
+
// by a short note; the model reads the file again when it needs it. The latest assistant message is left alone.
|
|
24
|
+
const BIG = 1500;
|
|
25
|
+
export function compactWrites(messages) {
|
|
26
|
+
let lastAssistant = -1;
|
|
27
|
+
for (let i = messages.length - 1; i >= 0; i--) if (messages[i].role === "assistant") { lastAssistant = i; break; }
|
|
28
|
+
let n = 0;
|
|
29
|
+
for (let i = 0; i < lastAssistant; i++) {
|
|
30
|
+
const m = messages[i];
|
|
31
|
+
if (m.role !== "assistant" || !m.toolCalls?.length) continue;
|
|
32
|
+
for (const c of m.toolCalls) {
|
|
33
|
+
const a = c.args;
|
|
34
|
+
if (!a || c.compacted || typeof a !== "object") continue;
|
|
35
|
+
if (c.name === "write_file" && String(a.content ?? "").length > BIG) {
|
|
36
|
+
const lines = String(a.content).split("\n").length;
|
|
37
|
+
c.args = { ...a, content: `[${lines} lines ${a.append ? "appended" : "written"} earlier; removed from the conversation to save space. Read ${a.path} to see the current contents.]` };
|
|
38
|
+
c.compacted = true; n++;
|
|
39
|
+
} else if (c.name === "edit_file" && String(a.old_string ?? "").length + String(a.new_string ?? "").length > BIG * 2) {
|
|
40
|
+
c.args = { ...a, old_string: `[${String(a.old_string).split("\n").length} lines, removed to save space]`, new_string: `[${String(a.new_string).split("\n").length} lines, removed to save space; read ${a.path} for the current contents]` };
|
|
41
|
+
c.compacted = true; n++;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return n;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const TRUNCATED_ARGS = (name, limit) =>
|
|
49
|
+
`Your reply hit the output limit (${limit} tokens) while you were still writing the arguments for ${name}, so the call was cut off and nothing was done.` +
|
|
50
|
+
(name === "write_file" ? " Write big files in parts: call write_file with the first part (under about 300 lines), then write_file with append=true for each further part." : " Send a shorter call, splitting the work into several smaller calls.");
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Runs one user message to completion.
|
|
54
|
+
* ui: { onAssistantStart, onText, onReasoning, onAssistantEnd, onToolStart, onToolOutput, onToolEnd, confirm, onRetry, onNotice,
|
|
55
|
+
* takeQueued, onQueuedDelivered }
|
|
56
|
+
* takeQueued(): text the user typed while the model was working (or null). It is handed to the model between steps,
|
|
57
|
+
* so the user can steer without waiting for the whole task to finish.
|
|
58
|
+
* perm: { mode, allowed: Set<toolName> }
|
|
59
|
+
*/
|
|
60
|
+
export async function runTurn({ session, userText, cfg, ui, perm, signal, cwd = session.cwd }) {
|
|
61
|
+
session.push({ role: "user", content: userText });
|
|
62
|
+
const tools = TOOLS.map(({ name, description, parameters }) => ({ name, description, parameters }));
|
|
63
|
+
const limit = cfg.maxOutputTokens || 8192;
|
|
64
|
+
|
|
65
|
+
for (let step = 1; ; step++) {
|
|
66
|
+
if (step > cfg.maxSteps) {
|
|
67
|
+
ui.onNotice?.(`Stopped after ${cfg.maxSteps} model calls for one message. Say "continue" to keep going.`);
|
|
68
|
+
return { stopped: "max_steps" };
|
|
69
|
+
}
|
|
70
|
+
compactWrites(session.messages);
|
|
71
|
+
// a saved model may have a smaller context window than the global budget
|
|
72
|
+
const saved = findModel(cfg, session.model);
|
|
73
|
+
const budget = saved?.ctx ? Math.min(cfg.contextTokens, Math.floor(saved.ctx * 0.7)) : cfg.contextTokens;
|
|
74
|
+
const cut = trimContext(session.messages, budget);
|
|
75
|
+
if (cut) ui.onNotice?.(`Trimmed ${cut} old tool output(s) to stay within the context budget.`);
|
|
76
|
+
|
|
77
|
+
let res, partial = "";
|
|
78
|
+
session.toolModes ||= {};
|
|
79
|
+
const toolMode = session.toolModes[session.model] || toolModeFor(cfg, session.model);
|
|
80
|
+
ui.onAssistantStart?.();
|
|
81
|
+
try {
|
|
82
|
+
res = await chat({
|
|
83
|
+
spec: session.model, system: systemPrompt({ cwd, mode: perm.mode }), messages: session.messages, tools,
|
|
84
|
+
cfg, signal, maxOutputTokens: limit, toolMode, timeouts: cfg,
|
|
85
|
+
onText: (d) => { partial += d; ui.onText?.(d); }, onReasoning: (d) => ui.onReasoning?.(d), onToolDelta: (n, a) => ui.onToolDelta?.(n, a), onRetry: ui.onRetry,
|
|
86
|
+
});
|
|
87
|
+
} catch (e) {
|
|
88
|
+
ui.onAssistantEnd?.(partial);
|
|
89
|
+
if (signal?.aborted || (isAbort(e) && !e.timeout)) {
|
|
90
|
+
session.push({ role: "assistant", content: (partial ? partial + "\n" : "") + "[interrupted by the user]" });
|
|
91
|
+
return { interrupted: true };
|
|
92
|
+
}
|
|
93
|
+
// This model rejects native tool calling: switch it to the text format and try the same step again.
|
|
94
|
+
if (toolMode === "native" && !partial && toolsUnsupported(e)) {
|
|
95
|
+
setToolMode(cfg, session.model, "text"); // remembered if it is a saved model
|
|
96
|
+
session.toolModes[session.model] = "text"; // and for the rest of this conversation either way
|
|
97
|
+
ui.onNotice?.(`${session.model} does not accept native tool calls; switching it to Warden's text tool format.`);
|
|
98
|
+
step--;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
// keep the conversation valid for the next message: the user turn stays; what streamed so far is kept as context
|
|
102
|
+
if (partial) session.push({ role: "assistant", content: partial + "\n[reply cut off: " + String(e.message).slice(0, 120) + "]" });
|
|
103
|
+
throw e;
|
|
104
|
+
}
|
|
105
|
+
ui.onAssistantEnd?.(res.text);
|
|
106
|
+
session.usage.calls++;
|
|
107
|
+
if (res.usage) { session.usage.input += res.usage.input; session.usage.output += res.usage.output; }
|
|
108
|
+
const cutOff = /^(length|max_tokens|MAX_TOKENS)$/.test(res.stopReason || "");
|
|
109
|
+
session.push({ role: "assistant", content: res.text, ...(res.toolCalls.length ? { toolCalls: res.toolCalls } : {}) });
|
|
110
|
+
|
|
111
|
+
if (!res.toolCalls.length) {
|
|
112
|
+
const queued = ui.takeQueued?.();
|
|
113
|
+
if (queued) { // the user said something while this step ran: keep going with it instead of ending the turn
|
|
114
|
+
session.push({ role: "user", content: queued });
|
|
115
|
+
ui.onQueuedDelivered?.(queued);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (!res.text.trim()) {
|
|
119
|
+
ui.onNotice?.(cutOff
|
|
120
|
+
? `The model used its whole output limit (${limit} tokens) without writing a reply, often by thinking too long. Ask again, ask for something smaller, or raise "maxOutputTokens" in settings.`
|
|
121
|
+
: `The model sent an empty reply${res.stopReason ? ` (finish reason: ${res.stopReason})` : ""}. Ask again or rephrase; if it keeps happening, try another model with /model.`);
|
|
122
|
+
return { done: true, text: "", empty: true, stopReason: res.stopReason };
|
|
123
|
+
}
|
|
124
|
+
if (cutOff) ui.onNotice?.(`The reply hit the output limit (${limit} tokens) and was cut off. Say "continue" for the rest.`);
|
|
125
|
+
return { done: true, text: res.text, stopReason: res.stopReason };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Every tool call must get a result, even when interrupted, or the next request is invalid.
|
|
129
|
+
for (const call of res.toolCalls) {
|
|
130
|
+
const tool = toolByName(call.name);
|
|
131
|
+
const result = (content, isError = true) => session.push({ role: "tool", toolCallId: call.id, name: call.name, content, ...(isError ? { isError: true } : {}) });
|
|
132
|
+
if (signal?.aborted) { result("Interrupted by the user before this ran."); continue; }
|
|
133
|
+
if (call.args?.__invalid_json !== undefined) {
|
|
134
|
+
const truncated = cutOff || call.args.__truncated;
|
|
135
|
+
result(truncated ? TRUNCATED_ARGS(tool?.name || "the tool call", limit) : "Your tool arguments were not valid JSON. Send them again as a JSON object.");
|
|
136
|
+
ui.onToolEnd?.(call, tool, { error: truncated ? "cut off at the output limit; the model will retry in smaller parts" : "invalid arguments" });
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (!tool) { result(`Unknown tool "${call.name}". Available: ${TOOLS.map((t) => t.name).join(", ")}`); ui.onToolEnd?.(call, null, { error: "unknown tool" }); continue; }
|
|
140
|
+
|
|
141
|
+
ui.onToolStart?.(call, tool);
|
|
142
|
+
if (needsApproval(tool, call.args, { cwd, mode: perm.mode, allowed: perm.allowed })) {
|
|
143
|
+
const answer = await ui.confirm(call, tool, describeCall(tool, call.args, cwd));
|
|
144
|
+
if (answer === "always") perm.allowed.add(tool.name);
|
|
145
|
+
else if (answer !== "yes") {
|
|
146
|
+
const why = typeof answer === "object" && answer.reason ? ` Reason: ${answer.reason}` : "";
|
|
147
|
+
result(`User denied this ${tool.name} call.${why}`);
|
|
148
|
+
ui.onToolEnd?.(call, tool, { denied: true });
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
ui.onToolApproved?.(call, tool);
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
const out = await tool.run(call.args || {}, { cwd, signal, onOutput: (s) => ui.onToolOutput?.(call, s) });
|
|
155
|
+
result(out.content, !!out.isError);
|
|
156
|
+
ui.onToolEnd?.(call, tool, out);
|
|
157
|
+
} catch (e) {
|
|
158
|
+
result(`Error: ${e.message}`);
|
|
159
|
+
ui.onToolEnd?.(call, tool, { error: e.message });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (signal?.aborted) return { interrupted: true };
|
|
163
|
+
// anything the user typed meanwhile goes to the model now, before its next step
|
|
164
|
+
const queued = ui.takeQueued?.();
|
|
165
|
+
if (queued) { session.push({ role: "user", content: queued }); ui.onQueuedDelivered?.(queued); }
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -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,57 @@
|
|
|
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. When the user asks for a change to something you already made, read the file and make small edit_file changes; do not rewrite the whole file to change a few lines.
|
|
38
|
+
- Write large files (more than about 300 lines) in parts: write_file with the first part, then write_file with append=true for the rest. A single huge tool call can be cut off by the output limit.
|
|
39
|
+
- File contents you wrote earlier may be removed from the conversation to save space. Read a file again before editing it.
|
|
40
|
+
- 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.
|
|
41
|
+
- Use bash for commands (tests, builds, git, package managers). Do not use it to read or write files.
|
|
42
|
+
- Never run anything that keeps running (dev servers, \`npm run dev\`, \`npx serve\`, \`python -m http.server\`, file watchers) without background=true: it would block until it times out. Start it with background=true, check it with the job tool, and tell the user the address. To show a static page, you can also just tell the user which file to open in a browser.
|
|
43
|
+
- The user may send new messages while you work. They arrive as user messages between your steps: read them and follow the latest instructions.
|
|
44
|
+
- 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.
|
|
45
|
+
- If the user denies a tool call, do not retry it the same way; adapt or ask.
|
|
46
|
+
- Do not run destructive commands (deleting data, force-pushing, rewriting git history) unless the user explicitly asked.
|
|
47
|
+
- Keep replies short and direct. Use Markdown sparingly; code in fenced blocks. Refer to code as path:line.
|
|
48
|
+
- When the task is done, stop and give a brief summary of what changed.
|
|
49
|
+
|
|
50
|
+
Environment:
|
|
51
|
+
- Working directory (project root): ${cwd}
|
|
52
|
+
- OS: ${os.platform()} ${os.release()}; shell for the bash tool: ${detectShell().name}
|
|
53
|
+
- Date: ${new Date().toISOString().slice(0, 10)}
|
|
54
|
+
- ${gitInfo(cwd)}
|
|
55
|
+
- Permission mode: ${mode} (the user approves edits and commands that need it; you will get "User denied" if they refuse)
|
|
56
|
+
${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` : ""}`;
|
|
57
|
+
}
|
|
@@ -0,0 +1,319 @@
|
|
|
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") {
|
|
73
|
+
// a message typed while tools ran (or after a failed request) follows another user turn: merge them
|
|
74
|
+
const last = out.at(-1);
|
|
75
|
+
if (last?.role === "user") last.content.push({ type: "text", text: m.content });
|
|
76
|
+
else out.push({ role: "user", content: [{ type: "text", text: m.content }] });
|
|
77
|
+
} else if (m.role === "assistant") {
|
|
78
|
+
const content = [];
|
|
79
|
+
if (m.content) content.push({ type: "text", text: m.content });
|
|
80
|
+
for (const c of m.toolCalls || []) content.push({ type: "tool_use", id: c.id, name: c.name, input: c.args ?? {} });
|
|
81
|
+
out.push({ role: "assistant", content: content.length ? content : [{ type: "text", text: "(no output)" }] });
|
|
82
|
+
} else if (m.role === "tool") {
|
|
83
|
+
const block = { type: "tool_result", tool_use_id: m.toolCallId, content: m.content };
|
|
84
|
+
const last = out.at(-1);
|
|
85
|
+
// consecutive tool results belong in one user message
|
|
86
|
+
if (last?.role === "user" && last.content.every((b) => b.type === "tool_result")) last.content.push(block);
|
|
87
|
+
else out.push({ role: "user", content: [block] });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const toolsOpenAI = (tools) => tools.map((t) => ({ type: "function", function: { name: t.name, description: t.description, parameters: t.parameters } }));
|
|
94
|
+
const toolsAnthropic = (tools) => tools.map((t) => ({ name: t.name, description: t.description, input_schema: t.parameters }));
|
|
95
|
+
|
|
96
|
+
// ---- HTTP with retries (free tiers rate-limit a lot) -----------------------
|
|
97
|
+
|
|
98
|
+
const sleep = (ms, signal) => new Promise((res, rej) => {
|
|
99
|
+
const t = setTimeout(res, ms);
|
|
100
|
+
signal?.addEventListener("abort", () => { clearTimeout(t); rej(new DOMException("aborted", "AbortError")); }, { once: true });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// ---- timeouts ------------------------------------------------------------------
|
|
104
|
+
// A model request must never hang forever. Three limits, all in seconds and changeable in settings:
|
|
105
|
+
// requestTimeoutSec no answer at all (not even headers) after this long: give up on the attempt (retried once)
|
|
106
|
+
// idleTimeoutSec a streaming reply goes completely silent this long: stop
|
|
107
|
+
// stallTimeoutSec only keep-alive pings, no text / reasoning / tool calls, this long: stop
|
|
108
|
+
export const TIMEOUT_DEFAULTS = { requestTimeoutSec: 180, idleTimeoutSec: 120, stallTimeoutSec: 300 };
|
|
109
|
+
|
|
110
|
+
export class ModelTimeoutError extends Error {
|
|
111
|
+
constructor(message) { super(message); this.name = "ModelTimeoutError"; this.timeout = true; }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function timeoutsFrom(cfg = {}) {
|
|
115
|
+
const sec = (k) => { const v = Number(cfg?.[k]); return (Number.isFinite(v) && v > 0 ? v : TIMEOUT_DEFAULTS[k]) * 1000; };
|
|
116
|
+
return { request: sec("requestTimeoutSec"), idle: sec("idleTimeoutSec"), stall: sec("stallTimeoutSec") };
|
|
117
|
+
}
|
|
118
|
+
const secs = (ms) => (ms % 1000 ? (ms / 1000).toFixed(1) : String(ms / 1000));
|
|
119
|
+
|
|
120
|
+
// Reads a fetch body, calling onLine for each line, and stops with ModelTimeoutError when the stream goes quiet.
|
|
121
|
+
// progress() is called by the parser when real content arrives (text, reasoning or tool calls).
|
|
122
|
+
async function readStream(r, { signal, idleMs, stallMs, onLine, label }) {
|
|
123
|
+
const reader = r.body.getReader();
|
|
124
|
+
const dec = new TextDecoder();
|
|
125
|
+
let buf = "", fired = null;
|
|
126
|
+
const stop = (why) => { if (!fired) { fired = why; reader.cancel().catch(() => {}); } };
|
|
127
|
+
let idle = setTimeout(() => stop("idle"), idleMs);
|
|
128
|
+
let stall = setTimeout(() => stop("stall"), stallMs);
|
|
129
|
+
const progress = () => { clearTimeout(stall); stall = setTimeout(() => stop("stall"), stallMs); };
|
|
130
|
+
try {
|
|
131
|
+
for (;;) {
|
|
132
|
+
const { done, value } = await reader.read();
|
|
133
|
+
if (done) break;
|
|
134
|
+
clearTimeout(idle); idle = setTimeout(() => stop("idle"), idleMs);
|
|
135
|
+
buf += dec.decode(value, { stream: true });
|
|
136
|
+
let i;
|
|
137
|
+
while ((i = buf.indexOf("\n")) >= 0) {
|
|
138
|
+
const line = buf.slice(0, i).trim(); buf = buf.slice(i + 1);
|
|
139
|
+
onLine(line, progress);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (buf.trim()) onLine(buf.trim(), progress);
|
|
143
|
+
} catch (e) {
|
|
144
|
+
if (!fired || signal?.aborted) { reader.cancel().catch(() => {}); throw e; }
|
|
145
|
+
} finally {
|
|
146
|
+
clearTimeout(idle); clearTimeout(stall);
|
|
147
|
+
}
|
|
148
|
+
if (fired === "idle") throw new ModelTimeoutError(`${label} stopped sending anything for ${secs(idleMs)}s, so Warden stopped waiting. Send your message again, or switch models with /model.`);
|
|
149
|
+
if (fired === "stall") throw new ModelTimeoutError(`${label} kept the connection open for ${secs(stallMs)}s without sending any reply (it may be overloaded), so Warden stopped waiting. Try again, or switch models with /model.`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function postWithRetry(url, headers, body, { signal, onRetry, retries = 4, timeoutMs = TIMEOUT_DEFAULTS.requestTimeoutSec * 1000, label = "The model" }) {
|
|
153
|
+
let timeoutRetries = 1;
|
|
154
|
+
for (let attempt = 0; ; attempt++) {
|
|
155
|
+
// each attempt gets its own deadline for the response to start; the user's interrupt still applies throughout
|
|
156
|
+
const deadline = new AbortController();
|
|
157
|
+
const timer = setTimeout(() => deadline.abort(), timeoutMs);
|
|
158
|
+
let r;
|
|
159
|
+
try {
|
|
160
|
+
r = await fetch(url, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body), signal: signal ? AbortSignal.any([signal, deadline.signal]) : deadline.signal });
|
|
161
|
+
} catch (e) {
|
|
162
|
+
if (signal?.aborted || !deadline.signal.aborted) throw e;
|
|
163
|
+
if (timeoutRetries-- > 0) {
|
|
164
|
+
onRetry?.({ status: "no response", waitMs: 0, attempt: attempt + 1, message: `no response after ${secs(timeoutMs)}s` });
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
throw new ModelTimeoutError(`${label} did not respond within ${secs(timeoutMs)}s (tried twice). It may be overloaded: try again, or switch models with /model.`);
|
|
168
|
+
} finally {
|
|
169
|
+
clearTimeout(timer);
|
|
170
|
+
}
|
|
171
|
+
if (r.ok) return r;
|
|
172
|
+
const text = await r.text().catch(() => "");
|
|
173
|
+
let msg = text, hintedDelay = null;
|
|
174
|
+
try {
|
|
175
|
+
const j = JSON.parse(text);
|
|
176
|
+
const err = Array.isArray(j) ? j[0]?.error : j.error;
|
|
177
|
+
msg = err?.message || j.message || text;
|
|
178
|
+
// Google puts the wait time in error.details[].retryDelay, e.g. "33s"
|
|
179
|
+
const rd = (err?.details || []).find((d) => d?.retryDelay)?.retryDelay;
|
|
180
|
+
if (rd) hintedDelay = parseFloat(rd) * 1000;
|
|
181
|
+
} catch {}
|
|
182
|
+
const ra = Number(r.headers.get("retry-after"));
|
|
183
|
+
if (Number.isFinite(ra) && ra > 0) hintedDelay ??= ra * 1000;
|
|
184
|
+
const fail = (extra = "") => { const e = new Error(`${r.status} ${String(msg).replace(/\s+/g, " ").slice(0, 400)}${extra}`); e.status = r.status; return e; };
|
|
185
|
+
const retryable = r.status === 429 || r.status >= 500;
|
|
186
|
+
if (!retryable || attempt >= retries) throw fail();
|
|
187
|
+
// A long or daily quota is not worth waiting for: say so now instead of retrying.
|
|
188
|
+
if (r.status === 429 && (hintedDelay > 60_000 || (/per ?day|daily|PerDay/i.test(msg) && !hintedDelay))) {
|
|
189
|
+
const e = fail(" [quota used up for now; switch model with /model or try again later]");
|
|
190
|
+
e.quota = true;
|
|
191
|
+
throw e;
|
|
192
|
+
}
|
|
193
|
+
const wait = hintedDelay != null ? Math.min(Math.max(hintedDelay, 1000), 60_000) : Math.min(2000 * 2 ** attempt, 30_000);
|
|
194
|
+
onRetry?.({ status: r.status, waitMs: wait, attempt: attempt + 1, message: String(msg).slice(0, 160) });
|
|
195
|
+
await sleep(wait, signal);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ---- streaming chat --------------------------------------------------------
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* One model call. Streams text through onText. Returns { text, toolCalls, usage, stopReason }.
|
|
203
|
+
*/
|
|
204
|
+
export async function chat({ spec, system, messages, tools, cfg = {}, signal, onText = () => {}, onReasoning = () => {}, onToolDelta = () => {}, onRetry, maxOutputTokens = 8192, env = process.env, toolMode = "native", timeouts }) {
|
|
205
|
+
const { provider, model } = parseModelSpec(spec);
|
|
206
|
+
const p = resolveProvider(provider, cfg);
|
|
207
|
+
const key = keyFor(provider, p, env);
|
|
208
|
+
const t = timeoutsFrom(timeouts || cfg);
|
|
209
|
+
const label = `The model (${spec})`;
|
|
210
|
+
const common = { p, key, model, signal, onRetry, maxOutputTokens, t, label, onReasoning, onToolDelta };
|
|
211
|
+
const send = (o) => (p.api === "anthropic" ? anthropicChat({ ...common, ...o }) : openaiChat({ ...common, ...o }));
|
|
212
|
+
|
|
213
|
+
if (toolMode === "text" && tools?.length) {
|
|
214
|
+
// the model writes <tool_call> blocks; hide them from the stream and parse them afterwards
|
|
215
|
+
const filter = new ToolTagFilter(onText);
|
|
216
|
+
let hidden = "";
|
|
217
|
+
const res = await send({ system: system + textToolsPrompt(tools), messages: toTextProtocol(messages), tools: null, onText: (d) => {
|
|
218
|
+
filter.write(d);
|
|
219
|
+
if (filter.inside) { hidden += d; try { onToolDelta(hidden.match(/"name"s*:s*"([w-]+)"/)?.[1] || "", hidden); } catch {} } else hidden = "";
|
|
220
|
+
} });
|
|
221
|
+
filter.end();
|
|
222
|
+
const parsed = parseTextToolCalls(res.text);
|
|
223
|
+
return { ...res, text: parsed.text, toolCalls: parsed.toolCalls };
|
|
224
|
+
}
|
|
225
|
+
if (toolMode === "none" || !tools?.length) {
|
|
226
|
+
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.";
|
|
227
|
+
return send({ system: toolMode === "none" ? system + note : system, messages: toTextProtocol(messages), tools: null, onText });
|
|
228
|
+
}
|
|
229
|
+
return send({ system, messages, tools, onText });
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async function openaiChat({ p, key, model, system, messages, tools, signal, onText, onReasoning, onToolDelta, onRetry, maxOutputTokens, t, label }) {
|
|
233
|
+
const body = { model, messages: toOpenAI(system, messages), stream: true, [p.maxTokensParam || "max_tokens"]: maxOutputTokens };
|
|
234
|
+
if (tools?.length) body.tools = toolsOpenAI(tools);
|
|
235
|
+
if (p.streamUsage !== false && !p.local) body.stream_options = { include_usage: true };
|
|
236
|
+
const headers = key ? { authorization: `Bearer ${key}` } : {};
|
|
237
|
+
const post = () => postWithRetry(`${p.baseUrl}/chat/completions`, headers, body, { signal, onRetry, timeoutMs: t.request, label });
|
|
238
|
+
let r;
|
|
239
|
+
try {
|
|
240
|
+
r = await post();
|
|
241
|
+
} catch (e) {
|
|
242
|
+
// some compatible servers reject stream_options; retry once without it
|
|
243
|
+
if (e.status === 400 && body.stream_options && /stream_options|include_usage|unknown/i.test(e.message)) {
|
|
244
|
+
delete body.stream_options;
|
|
245
|
+
r = await post();
|
|
246
|
+
} else throw e;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
let text = "", stopReason = null, usage = null;
|
|
250
|
+
const calls = new Map(); // index -> {id, name, args}
|
|
251
|
+
const handle = (data, progress) => {
|
|
252
|
+
if (data === "[DONE]") return;
|
|
253
|
+
let j; try { j = JSON.parse(data); } catch { return; }
|
|
254
|
+
if (j.error) { const e = new Error(j.error.message || JSON.stringify(j.error)); e.status = Number(j.error.code) || undefined; throw e; }
|
|
255
|
+
if (j.usage) usage = { input: j.usage.prompt_tokens ?? 0, output: j.usage.completion_tokens ?? 0 };
|
|
256
|
+
const ch = j.choices?.[0];
|
|
257
|
+
if (!ch) return;
|
|
258
|
+
const d = ch.delta || ch.message || {};
|
|
259
|
+
// reasoning models stream their thinking separately (field name differs by server); show activity, keep it out of the reply
|
|
260
|
+
const thought = d.reasoning_content ?? d.reasoning ?? d.thinking;
|
|
261
|
+
if (typeof thought === "string" && thought) { progress(); onReasoning(thought); }
|
|
262
|
+
if (typeof d.content === "string" && d.content) { progress(); text += d.content; onText(d.content); }
|
|
263
|
+
for (const tc of d.tool_calls || []) {
|
|
264
|
+
progress();
|
|
265
|
+
const idx = tc.index ?? calls.size;
|
|
266
|
+
const cur = calls.get(idx) || { id: "", name: "", args: "" };
|
|
267
|
+
if (tc.id) cur.id = tc.id;
|
|
268
|
+
if (tc.function?.name) cur.name += tc.function.name;
|
|
269
|
+
if (tc.function?.arguments) cur.args += typeof tc.function.arguments === "string" ? tc.function.arguments : JSON.stringify(tc.function.arguments);
|
|
270
|
+
calls.set(idx, cur);
|
|
271
|
+
try { onToolDelta(cur.name, cur.args); } catch {}
|
|
272
|
+
}
|
|
273
|
+
if (ch.finish_reason) stopReason = ch.finish_reason;
|
|
274
|
+
};
|
|
275
|
+
// lines starting with ":" are keep-alive comments (OpenRouter sends them while a model is queued): not progress
|
|
276
|
+
await readStream(r, { signal, idleMs: t.idle, stallMs: t.stall, label, onLine: (line, progress) => { if (line.startsWith("data:")) handle(line.slice(5).trim(), progress); } });
|
|
277
|
+
|
|
278
|
+
const toolCalls = [...calls.entries()].sort((a, b) => a[0] - b[0]).map(([i, c], n) => {
|
|
279
|
+
let args = {};
|
|
280
|
+
try { args = c.args ? JSON.parse(c.args) : {}; } catch { args = { __invalid_json: c.args }; }
|
|
281
|
+
return { id: c.id || `call_${Date.now().toString(36)}_${n}`, name: c.name, args };
|
|
282
|
+
}).filter((c) => c.name);
|
|
283
|
+
return { text, toolCalls, usage, stopReason };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async function anthropicChat({ p, key, model, system, messages, tools, signal, onText, onRetry, maxOutputTokens, t, label }) {
|
|
287
|
+
const body = { model, system, max_tokens: maxOutputTokens, messages: toAnthropic(messages) };
|
|
288
|
+
if (tools?.length) body.tools = toolsAnthropic(tools);
|
|
289
|
+
// not streamed: the whole reply arrives at once, so allow the request time plus the stall time for it
|
|
290
|
+
const r = await postWithRetry(`${p.baseUrl}/messages`, { "x-api-key": key, "anthropic-version": "2023-06-01" }, body, { signal, onRetry, timeoutMs: t.request + t.stall, label });
|
|
291
|
+
const j = await r.json();
|
|
292
|
+
let text = "";
|
|
293
|
+
const toolCalls = [];
|
|
294
|
+
for (const b of j.content || []) {
|
|
295
|
+
if (b.type === "text") text += b.text;
|
|
296
|
+
if (b.type === "tool_use") toolCalls.push({ id: b.id, name: b.name, args: b.input || {} });
|
|
297
|
+
}
|
|
298
|
+
if (text) onText(text);
|
|
299
|
+
return { text, toolCalls, usage: j.usage ? { input: j.usage.input_tokens ?? 0, output: j.usage.output_tokens ?? 0 } : null, stopReason: j.stop_reason };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Models a provider offers: [{ id, ctx, free, tools }] (null = unknown). Uses the source's public list when it has one.
|
|
303
|
+
export async function listModels(name, cfg = {}, env = process.env) {
|
|
304
|
+
const p = resolveProvider(name, cfg);
|
|
305
|
+
let key = null;
|
|
306
|
+
try { key = keyFor(name, p, env); } catch (e) { if (!p.publicList) throw e; }
|
|
307
|
+
const fetchJson = async (url) => {
|
|
308
|
+
const r = await fetch(url, { headers: key ? { authorization: `Bearer ${key}` } : {}, signal: AbortSignal.timeout(20000) });
|
|
309
|
+
if (!r.ok) { const e = new Error(`${r.status} ${(await r.text()).replace(/\s+/g, " ").slice(0, 200)}`); e.status = r.status; throw e; }
|
|
310
|
+
return r.json();
|
|
311
|
+
};
|
|
312
|
+
let list;
|
|
313
|
+
if (p.list) list = await p.list(fetchJson);
|
|
314
|
+
else if (p.api === "openai") {
|
|
315
|
+
const j = await fetchJson(`${p.baseUrl}/models`);
|
|
316
|
+
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 }));
|
|
317
|
+
} else throw new Error(`listing models is not supported for ${name}; type the model id instead`);
|
|
318
|
+
return list.filter((m) => m.id).sort((a, b) => a.id.localeCompare(b.id));
|
|
319
|
+
}
|