piroboros 0.0.0-stage → 0.2.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/CHANGELOG.md +43 -0
- package/LICENSE +21 -0
- package/NOTICE +39 -0
- package/README.md +140 -2
- package/agents/astrelia.md +31 -0
- package/agents/avaritia.md +31 -0
- package/agents/elaria.md +31 -0
- package/agents/nytheria.md +32 -0
- package/agents/raskreia.md +32 -0
- package/agents/samaela.md +40 -0
- package/agents/vaelia.md +35 -0
- package/agents/vesperia.md +45 -0
- package/bin/piroboros.mjs +17 -0
- package/dist/auth.js +105 -0
- package/dist/base.js +19 -0
- package/dist/chat/budget.js +87 -0
- package/dist/chat/compactloop.js +63 -0
- package/dist/chat/compactor.js +530 -0
- package/dist/chat/extension.js +36 -0
- package/dist/chat/hook.js +40 -0
- package/dist/chat/log.js +317 -0
- package/dist/chat/outbound.js +114 -0
- package/dist/chat/plan.js +64 -0
- package/dist/chat/prompt.js +115 -0
- package/dist/chat/status.js +37 -0
- package/dist/chat/stuck.js +83 -0
- package/dist/chat/tools.js +163 -0
- package/dist/chat/tree.js +73 -0
- package/dist/chat/turn.js +305 -0
- package/dist/chat/view.js +163 -0
- package/dist/conclave/agents.js +113 -0
- package/dist/conclave/pipeline.js +715 -0
- package/dist/conclave/report.js +43 -0
- package/dist/config.js +614 -0
- package/dist/durable.js +177 -0
- package/dist/guard.js +346 -0
- package/dist/lock.js +56 -0
- package/dist/main.js +348 -0
- package/dist/memory/akashic/api.js +121 -0
- package/dist/memory/akashic/confusables.js +41 -0
- package/dist/memory/akashic/scanner.js +67 -0
- package/dist/memory/akashic/snapshot.js +31 -0
- package/dist/memory/akashic/store.js +1254 -0
- package/dist/memory/akashic/tools.js +168 -0
- package/dist/memory/akashic/vault.js +984 -0
- package/dist/memory/host.js +124 -0
- package/dist/memory/mnemosia/adapters/akashic-memories.js +180 -0
- package/dist/memory/mnemosia/adapters/contract.js +219 -0
- package/dist/memory/mnemosia/agents.js +387 -0
- package/dist/memory/mnemosia/brief.js +890 -0
- package/dist/memory/mnemosia/claims.js +609 -0
- package/dist/memory/mnemosia/conflicts.js +769 -0
- package/dist/memory/mnemosia/escalation.js +257 -0
- package/dist/memory/mnemosia/feedback.js +108 -0
- package/dist/memory/mnemosia/filters.js +248 -0
- package/dist/memory/mnemosia/intent.js +221 -0
- package/dist/memory/mnemosia/open.js +34 -0
- package/dist/memory/mnemosia/orchestrator.js +926 -0
- package/dist/memory/mnemosia/plan.js +266 -0
- package/dist/memory/mnemosia/policy.js +426 -0
- package/dist/memory/mnemosia/progressive.js +550 -0
- package/dist/memory/mnemosia/provenance.js +458 -0
- package/dist/memory/mnemosia/qualifiers.js +428 -0
- package/dist/memory/mnemosia/rank.js +525 -0
- package/dist/memory/mnemosia/relations.js +156 -0
- package/dist/memory/mnemosia/requirements.js +138 -0
- package/dist/memory/mnemosia/rerank.js +253 -0
- package/dist/memory/mnemosia/retention.js +174 -0
- package/dist/memory/mnemosia/rollback.js +337 -0
- package/dist/memory/mnemosia/select.js +607 -0
- package/dist/memory/mnemosia/similarity.js +164 -0
- package/dist/memory/mnemosia/situation.js +251 -0
- package/dist/memory/mnemosia/task.js +418 -0
- package/dist/memory/mnemosia/temporal.js +136 -0
- package/dist/memory/mnemosia/tokens.js +66 -0
- package/dist/memory/mnemosia/tools.js +160 -0
- package/dist/memory/mnemosia/trace.js +306 -0
- package/dist/memory/mnemosia/types.js +298 -0
- package/dist/models.js +360 -0
- package/dist/paths.js +103 -0
- package/dist/redact.js +79 -0
- package/dist/runtime.js +378 -0
- package/dist/search-format.js +102 -0
- package/dist/search.js +351 -0
- package/dist/setup.js +366 -0
- package/dist/shell.js +110 -0
- package/dist/skills.js +311 -0
- package/dist/telemetry.js +84 -0
- package/dist/tui/app.js +1011 -0
- package/dist/tui/commands.js +121 -0
- package/dist/tui/keys.js +83 -0
- package/dist/tui/pane.js +387 -0
- package/dist/tui/panel.js +60 -0
- package/dist/tui/picker.js +25 -0
- package/dist/tui/sanitize.js +59 -0
- package/dist/tui/settings.js +198 -0
- package/dist/tui/setup.js +256 -0
- package/dist/tui/theme.js +26 -0
- package/dist/tui/thinking/parse.js +1094 -0
- package/dist/tui/thinking/select.js +86 -0
- package/dist/tui/thinking/state.js +137 -0
- package/dist/tui/thinking/types.js +3 -0
- package/dist/tui/thinking/view.js +74 -0
- package/dist/tui/viewport.js +208 -0
- package/dist/undo.js +258 -0
- package/dist/web.js +933 -0
- package/docs/conclave.md +21 -0
- package/docs/configuration.md +48 -0
- package/docs/development.md +25 -0
- package/docs/interface.md +33 -0
- package/docs/memory.md +11 -0
- package/docs/privacy.md +13 -0
- package/docs/tools.md +20 -0
- package/docs/web.md +38 -0
- package/models.json.example +34 -0
- package/package.json +76 -4
- package/settings.json.example +29 -0
- package/skills/conclave/SKILL.md +76 -0
package/dist/setup.js
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { BASH_HINT, findBash } from "./shell.js";
|
|
2
|
+
import { probeSearch } from "./search.js";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { THINKING_STEP_MODES, ensureHome, homePaths, mergeSettings, recordIntegrity, splitModelRef, unsetSettings, ConfigError } from "./config.js";
|
|
5
|
+
import { MODELS_JSON_EXAMPLE, apiKeyEnvName, contextCheck, hasAuth, loginAndStore, modelChoices, providerChoices, } from "./models.js";
|
|
6
|
+
import { safeAuthUrl } from "./tui/sanitize.js";
|
|
7
|
+
import { SEARCH_KEYS, isUsableKey, searchKey } from "./web.js";
|
|
8
|
+
// Guided setup (D24). The flow is a sequence of steps that talk to the user only through SetupIO, so the
|
|
9
|
+
// Ink layer and the tests drive the same code. Nothing is written to settings.json until the summary step
|
|
10
|
+
// is confirmed, and then in one merged write. Credentials stored by a completed login stay if the user cancels.
|
|
11
|
+
export const EXIT_SETUP_REQUIREMENT = 2;
|
|
12
|
+
export class SetupCancelled extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super("setup cancelled");
|
|
15
|
+
this.name = "SetupCancelled";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export class SetupError extends Error {
|
|
19
|
+
exitCode = EXIT_SETUP_REQUIREMENT;
|
|
20
|
+
constructor(message) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "SetupError";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const MIN_NODE = [26, 5, 0];
|
|
26
|
+
export function nodeAtLeast(version, min = MIN_NODE) {
|
|
27
|
+
const [major = 0, minor = 0, patch = 0] = version.replace(/^v/u, "").split(".").map(Number);
|
|
28
|
+
if (major !== min[0])
|
|
29
|
+
return major > min[0];
|
|
30
|
+
if (minor !== min[1])
|
|
31
|
+
return minor > min[1];
|
|
32
|
+
return patch >= min[2];
|
|
33
|
+
}
|
|
34
|
+
function programExists(name) {
|
|
35
|
+
if (name === "bash")
|
|
36
|
+
return findBash() !== undefined;
|
|
37
|
+
const result = spawnSync(name, ["--version"], { stdio: "ignore", windowsHide: true });
|
|
38
|
+
return result.error === undefined;
|
|
39
|
+
}
|
|
40
|
+
export const isLoopbackHost = (host) => host === "localhost" || host === "::1" || host === "[::1]" || /^127\.\d+\.\d+\.\d+$/u.test(host);
|
|
41
|
+
function hostOf(baseUrl) {
|
|
42
|
+
if (baseUrl === undefined)
|
|
43
|
+
return undefined;
|
|
44
|
+
try {
|
|
45
|
+
return new URL(baseUrl).hostname;
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Step 1. Hard checks stop setup with exit code 2; soft checks only warn. */
|
|
52
|
+
export async function checkRequirements(env, io) {
|
|
53
|
+
const version = env.nodeVersion ?? process.versions.node;
|
|
54
|
+
if (!nodeAtLeast(version))
|
|
55
|
+
throw new SetupError(`Node ${MIN_NODE.join(".")} or newer is required (found ${version}).`);
|
|
56
|
+
try {
|
|
57
|
+
ensureHome(env.home, env.env);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
throw new SetupError(error instanceof ConfigError ? error.message : `the home directory is not writable: ${error.message}`);
|
|
61
|
+
}
|
|
62
|
+
const has = env.hasProgram ?? programExists;
|
|
63
|
+
if (!has("bash"))
|
|
64
|
+
throw new SetupError(`bash was not found. piroboros runs every shell command through bash (Git Bash on Windows). ${BASH_HINT}`);
|
|
65
|
+
io.notify("info", `Data directory: ${env.home}`);
|
|
66
|
+
if (!has("git"))
|
|
67
|
+
io.notify("warn", "git was not found. The memory vault needs it.");
|
|
68
|
+
if (!has("rg"))
|
|
69
|
+
io.notify("warn", "rg (ripgrep) was not found. Exact search needs it, and file search falls back to it when the fff library cannot load.");
|
|
70
|
+
// Soft: without fff the find and grep tools use ripgrep, and without both they return an error.
|
|
71
|
+
const fff = await (env.fffStatus ?? defaultFffStatus)();
|
|
72
|
+
if (fff === undefined)
|
|
73
|
+
io.notify("info", "File search: fff is available.");
|
|
74
|
+
else
|
|
75
|
+
io.notify("warn", `File search: fff is unavailable (${fff}). find and grep use ripgrep${has("rg") ? "" : ", which is also missing, so they will return an error"}.`);
|
|
76
|
+
}
|
|
77
|
+
/** The reason fff cannot be used, or undefined when it can. */
|
|
78
|
+
async function defaultFffStatus() {
|
|
79
|
+
const status = await probeSearch();
|
|
80
|
+
return status.fff.ok ? undefined : status.fff.reason;
|
|
81
|
+
}
|
|
82
|
+
/** Adapts SetupIO to the interaction object pi-ai login flows expect. */
|
|
83
|
+
function interactionFor(io, signal) {
|
|
84
|
+
return {
|
|
85
|
+
signal,
|
|
86
|
+
async prompt(prompt) {
|
|
87
|
+
const aborted = prompt.signal ?? signal;
|
|
88
|
+
switch (prompt.type) {
|
|
89
|
+
case "select":
|
|
90
|
+
return io.choose(prompt.message, prompt.options.map((option) => ({ id: option.id, label: option.description ? `${option.label} (${option.description})` : option.label })));
|
|
91
|
+
case "secret":
|
|
92
|
+
return io.secret(prompt.message, aborted);
|
|
93
|
+
case "text":
|
|
94
|
+
case "manual_code":
|
|
95
|
+
return io.text(prompt.message, aborted, prompt.placeholder);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
notify(event) {
|
|
99
|
+
switch (event.type) {
|
|
100
|
+
case "auth_url": {
|
|
101
|
+
const href = safeAuthUrl(event.url);
|
|
102
|
+
if (href === undefined) {
|
|
103
|
+
io.notify("error", "The sign-in address was refused because it is not an https address. Nothing was opened.");
|
|
104
|
+
throw new Error("the provider returned an unsafe sign-in address");
|
|
105
|
+
}
|
|
106
|
+
io.openUrl(href, event.instructions);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
case "device_code":
|
|
110
|
+
io.notify("info", `Open ${event.verificationUri} and enter the code ${event.userCode}.`);
|
|
111
|
+
return;
|
|
112
|
+
case "info":
|
|
113
|
+
case "progress":
|
|
114
|
+
io.notify("info", event.message);
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export function entriesFor(models) {
|
|
120
|
+
const out = [];
|
|
121
|
+
for (const provider of providerChoices(models))
|
|
122
|
+
for (const method of provider.methods)
|
|
123
|
+
out.push({ id: `${provider.id}|${method.type}`, label: method.label, provider, method });
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
const MODELS_JSON = "models-json";
|
|
127
|
+
/** Show where models.json goes and an example, then wait until the user asks for a reload (or goes back). */
|
|
128
|
+
async function addCustomProvider(env, io) {
|
|
129
|
+
const path = homePaths(env.home).models;
|
|
130
|
+
io.notify("info", `Create ${path} with the providers you want, for example:\n${MODELS_JSON_EXAMPLE}\nThe API key is read from the environment variable <ID>_API_KEY (for example my-llm becomes MY_LLM_API_KEY, with every other character replaced by an underscore), or you can enter it here when you pick the provider.`);
|
|
131
|
+
for (;;) {
|
|
132
|
+
const choice = await io.choose("models.json", [{ id: "reload", label: "I saved the file, reload it" }, { id: "back", label: "Back" }], "reload");
|
|
133
|
+
if (choice === "back")
|
|
134
|
+
return;
|
|
135
|
+
const result = env.custom?.reload() ?? { ok: false, error: "custom providers are not available in this run" };
|
|
136
|
+
if (!result.ok) {
|
|
137
|
+
io.notify("error", result.error);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
recordIntegrity(env.home, ["models"]);
|
|
141
|
+
io.notify("info", result.ids.length === 0 ? `${path} lists no providers.` : `Loaded: ${result.ids.map((id) => `${id} (${apiKeyEnvName(id)})`).join(", ")}`);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Sign in to one provider method and check that the credential resolves. Returns false after telling the user why not.
|
|
147
|
+
* An existing credential counts as signed in unless `force` asks for a new login. SetupCancelled passes through.
|
|
148
|
+
*/
|
|
149
|
+
export async function signIn(env, io, entry, signal, options = {}) {
|
|
150
|
+
if (options.force !== true && (await hasAuth(env.models, entry.provider.id)))
|
|
151
|
+
return true;
|
|
152
|
+
if (!entry.method.interactive) {
|
|
153
|
+
io.notify("error", `${entry.provider.name} has no sign-in step. Set its API key in the environment and try again.`);
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
await loginAndStore(env.models, env.store, entry.provider.id, entry.method.type, interactionFor(io, signal), env.deviceId);
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
if (error instanceof SetupCancelled)
|
|
161
|
+
throw error;
|
|
162
|
+
io.notify("error", `Sign-in failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
if (!(await hasAuth(env.models, entry.provider.id))) {
|
|
166
|
+
io.notify("error", "Sign-in finished but the credential does not resolve. Try again.");
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Pick a provider and model, signing in when the provider does not already resolve. Returns the model ref.
|
|
173
|
+
* `keep` offers the current value on a re-run. `skipLabel` renames the skip option, for callers where it means a reset.
|
|
174
|
+
*/
|
|
175
|
+
export async function pickModel(env, io, role, title, keep, allowSkip, signal, skipLabel = "Skip") {
|
|
176
|
+
for (;;) {
|
|
177
|
+
const entries = entriesFor(env.models);
|
|
178
|
+
const options = [];
|
|
179
|
+
if (keep !== undefined)
|
|
180
|
+
options.push({ id: "keep", label: `Keep ${keep}` });
|
|
181
|
+
if (allowSkip)
|
|
182
|
+
options.push({ id: "skip", label: skipLabel });
|
|
183
|
+
const ready = new Set();
|
|
184
|
+
for (const entry of entries) {
|
|
185
|
+
const resolved = await hasAuth(env.models, entry.provider.id);
|
|
186
|
+
if (resolved)
|
|
187
|
+
ready.add(entry.provider.id);
|
|
188
|
+
options.push({ id: entry.id, label: `${entry.label}${resolved ? " [ready]" : ""}` });
|
|
189
|
+
}
|
|
190
|
+
options.push({ id: MODELS_JSON, label: "Add a custom provider (create models.json)" });
|
|
191
|
+
const defaultId = keep === undefined ? undefined : "keep";
|
|
192
|
+
const picked = await io.choose(title, options, defaultId);
|
|
193
|
+
if (picked === "keep")
|
|
194
|
+
return "keep";
|
|
195
|
+
if (picked === "skip")
|
|
196
|
+
return "skip";
|
|
197
|
+
if (picked === MODELS_JSON) {
|
|
198
|
+
await addCustomProvider(env, io);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
const entry = entries.find((candidate) => candidate.id === picked);
|
|
202
|
+
if (!(await signIn(env, io, entry, signal)))
|
|
203
|
+
continue;
|
|
204
|
+
const choices = modelChoices(env.models, entry.provider.id, role);
|
|
205
|
+
if (choices.length === 0) {
|
|
206
|
+
io.notify("error", `${entry.provider.name} lists no models.`);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
const modelOptions = choices.map((choice) => ({ id: choice.ref, label: choice.model.name ?? choice.ref, ...(choice.check.ok ? {} : { disabled: choice.check.reason }) }));
|
|
210
|
+
const firstOk = choices.find((choice) => choice.check.ok)?.ref;
|
|
211
|
+
for (;;) {
|
|
212
|
+
const model = await io.choose(`Model for ${entry.provider.name}`, modelOptions, firstOk);
|
|
213
|
+
const chosen = choices.find((choice) => choice.ref === model);
|
|
214
|
+
if (chosen !== undefined && chosen.check.ok)
|
|
215
|
+
return chosen.ref;
|
|
216
|
+
io.notify("error", chosen?.check.reason ?? "That model cannot be used.");
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
export function resolveBaseHost(models, ref) {
|
|
221
|
+
const { provider, modelId } = splitModelRef(ref);
|
|
222
|
+
return hostOf(models.getModel(provider, modelId)?.baseUrl);
|
|
223
|
+
}
|
|
224
|
+
/** True when a key for the search provider is stored or set in its environment variable. */
|
|
225
|
+
export const hasSearchKey = (env, provider) => searchKey(provider, homePaths(env.home).auth, env.env ?? process.env) !== undefined;
|
|
226
|
+
/**
|
|
227
|
+
* Ask for a web search key and store it in auth.json; it is never printed. A key that is already stored or set is kept
|
|
228
|
+
* unless `force` asks for a new one. Returns true when a key is in place afterwards.
|
|
229
|
+
*/
|
|
230
|
+
export async function webKeyStep(env, io, provider, signal, options = {}) {
|
|
231
|
+
const names = SEARCH_KEYS[provider];
|
|
232
|
+
io.notify("info", `Queries are sent to ${names.label}, which may keep them under its own terms. You use your own ${names.label} key and accept its terms.`);
|
|
233
|
+
if (options.force !== true && hasSearchKey(env, provider)) {
|
|
234
|
+
io.notify("info", `A ${names.label} key is already stored or set in ${names.env}; it is kept.`);
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
for (;;) {
|
|
238
|
+
const key = (await io.secret(`${names.label} API key`, signal)).trim();
|
|
239
|
+
if (isUsableKey(key)) {
|
|
240
|
+
await env.store.modify(names.auth, async () => ({ type: "api_key", key }));
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
io.notify("error", "That is not a usable key (8 to 512 printable characters, no spaces). Try again.");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/** The optional web search step. Returns the chosen provider, or undefined for off. */
|
|
247
|
+
export async function pickWebSearch(env, io, signal) {
|
|
248
|
+
const picked = await io.choose("Web search for the Vaelia seat (optional)", [
|
|
249
|
+
{ id: "off", label: "Off" },
|
|
250
|
+
{ id: "tinyfish", label: "TinyFish (recommended, free tier)" },
|
|
251
|
+
{ id: "exa", label: "Exa (paid per search, monthly free credit)" },
|
|
252
|
+
], "off");
|
|
253
|
+
if (picked !== "tinyfish" && picked !== "exa")
|
|
254
|
+
return undefined;
|
|
255
|
+
await webKeyStep(env, io, picked, signal);
|
|
256
|
+
return picked;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* /login: pick a provider (or web-tinyfish, web-exa), pick the method when the provider has an OAuth and an API key
|
|
260
|
+
* method, then sign in. `target` skips the provider pick. A provider that already resolves is signed in again only after
|
|
261
|
+
* the user agrees, or when `force` is set. Never runs the wizard.
|
|
262
|
+
*/
|
|
263
|
+
export async function runLogin(env, plain, signal, options = {}) {
|
|
264
|
+
// The notices of a flow disappear when it ends, so the last error is kept for the result.
|
|
265
|
+
let lastError = "";
|
|
266
|
+
const io = { ...plain, notify: (kind, message) => { if (kind === "error")
|
|
267
|
+
lastError = message; plain.notify(kind, message); } };
|
|
268
|
+
const failed = (what) => ({ status: "failed", message: lastError === "" ? what : `${what}: ${lastError}` });
|
|
269
|
+
const providers = providerChoices(env.models);
|
|
270
|
+
let target = options.target;
|
|
271
|
+
if (target === undefined) {
|
|
272
|
+
const choices = [];
|
|
273
|
+
for (const provider of providers)
|
|
274
|
+
choices.push({ id: provider.id, label: `${provider.name}${(await hasAuth(env.models, provider.id)) ? " [ready]" : ""}` });
|
|
275
|
+
for (const name of ["tinyfish", "exa"])
|
|
276
|
+
choices.push({ id: `web-${name}`, label: `${SEARCH_KEYS[name].label} web search key${hasSearchKey(env, name) ? " [ready]" : ""}` });
|
|
277
|
+
target = await io.choose("Sign in to", choices);
|
|
278
|
+
}
|
|
279
|
+
const web = /^web-(tinyfish|exa)$/u.exec(target);
|
|
280
|
+
if (web !== null) {
|
|
281
|
+
const provider = web[1];
|
|
282
|
+
const label = SEARCH_KEYS[provider].label;
|
|
283
|
+
const force = options.force === true || (hasSearchKey(env, provider) && (await io.confirm(`A ${label} key is already stored or set. Enter a new one?`, false)));
|
|
284
|
+
if (hasSearchKey(env, provider) && !force)
|
|
285
|
+
return { status: "unchanged", message: `${label} key kept` };
|
|
286
|
+
return (await webKeyStep(env, io, provider, signal, { force })) ? { status: "web-key", message: `${label} key stored` } : failed(`${label} key not stored`);
|
|
287
|
+
}
|
|
288
|
+
const provider = providers.find((candidate) => candidate.id === target);
|
|
289
|
+
if (provider === undefined)
|
|
290
|
+
return failed(`unknown provider ${target}; use /login to see the list, or /reload after editing models.json`);
|
|
291
|
+
let method = provider.methods[0];
|
|
292
|
+
if (provider.methods.length > 1) {
|
|
293
|
+
const pickedMethod = await io.choose(`How do you want to sign in to ${provider.name}?`, provider.methods.map((candidate) => ({ id: candidate.type, label: candidate.label })));
|
|
294
|
+
method = provider.methods.find((candidate) => candidate.type === pickedMethod) ?? method;
|
|
295
|
+
}
|
|
296
|
+
const entry = { id: `${provider.id}|${method.type}`, label: method.label, provider, method };
|
|
297
|
+
const force = options.force === true || ((await hasAuth(env.models, provider.id)) && (await io.confirm(`${provider.name} already has a credential. Sign in again?`, false)));
|
|
298
|
+
if (!force && (await hasAuth(env.models, provider.id)))
|
|
299
|
+
return { status: "unchanged", message: `${provider.name} kept as it is` };
|
|
300
|
+
return (await signIn(env, io, entry, signal, { force })) ? { status: "signed-in", message: `signed in to ${provider.name}` } : failed(`sign-in to ${provider.name} did not finish`);
|
|
301
|
+
}
|
|
302
|
+
/** Run the wizard. Throws SetupError for a missing hard requirement; returns `cancelled` and writes nothing on cancel. */
|
|
303
|
+
export async function runSetup(env, io, signal = new AbortController().signal) {
|
|
304
|
+
try {
|
|
305
|
+
// Step 1
|
|
306
|
+
await checkRequirements(env, io);
|
|
307
|
+
if (!(await io.confirm("Continue with setup?", true)))
|
|
308
|
+
throw new SetupCancelled();
|
|
309
|
+
// Step 2: compactor
|
|
310
|
+
let compactor;
|
|
311
|
+
const existingCompactor = env.existing.compactor?.model;
|
|
312
|
+
const compactorPick = await pickModel(env, io, "compactor", "Compactor model (summarizes old chat in the background)", existingCompactor, true, signal);
|
|
313
|
+
if (compactorPick === "keep")
|
|
314
|
+
compactor = undefined;
|
|
315
|
+
else if (compactorPick === "skip") {
|
|
316
|
+
io.notify("warn", "compactor: not configured");
|
|
317
|
+
compactor = undefined;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
const host = resolveBaseHost(env.models, compactorPick);
|
|
321
|
+
let allow = false;
|
|
322
|
+
if (host !== undefined && !isLoopbackHost(host))
|
|
323
|
+
allow = await io.confirm(`The compactor will send chat text to ${host}. Allow it?`, false);
|
|
324
|
+
compactor = allow || (host !== undefined && isLoopbackHost(host)) ? { enabled: true, model: compactorPick, allowThirdParty: allow } : undefined;
|
|
325
|
+
if (compactor === undefined)
|
|
326
|
+
io.notify("warn", existingCompactor === undefined ? "compactor: not configured" : `compactor: kept ${existingCompactor}`);
|
|
327
|
+
}
|
|
328
|
+
// Step 3: main
|
|
329
|
+
const mainPick = await pickModel(env, io, "main", "Main model", env.existing.main, false, signal);
|
|
330
|
+
const main = mainPick === "keep" ? env.existing.main : mainPick;
|
|
331
|
+
// Step 4: optional values (Enter accepts the default)
|
|
332
|
+
const defaultMember = env.existing.conclave?.defaultModel ?? main;
|
|
333
|
+
const memberOptions = [{ id: "main", label: `Same as main (${main})` }];
|
|
334
|
+
if (defaultMember !== main)
|
|
335
|
+
memberOptions.push({ id: "current", label: `Keep ${defaultMember}` });
|
|
336
|
+
const memberPick = await io.choose("Conclave default model", memberOptions, defaultMember === main ? "main" : "current");
|
|
337
|
+
const conclaveDefault = memberPick === "current" ? defaultMember : main;
|
|
338
|
+
const stepsPick = await io.choose("Thinking display", THINKING_STEP_MODES.map((mode) => ({ id: mode, label: mode })), env.existing.tui?.thinkingSteps ?? "summary");
|
|
339
|
+
// Step 5: optional web search for the Vaelia seat. Off changes nothing; a provider turns web access on.
|
|
340
|
+
const webProvider = await pickWebSearch(env, io, signal);
|
|
341
|
+
// Step 6: summary and one write
|
|
342
|
+
const patch = { main, tui: { thinkingSteps: stepsPick } };
|
|
343
|
+
if (webProvider !== undefined)
|
|
344
|
+
patch.web = { enabled: true, search: { provider: webProvider } };
|
|
345
|
+
if (compactor !== undefined)
|
|
346
|
+
patch.compactor = compactor;
|
|
347
|
+
if (conclaveDefault !== main)
|
|
348
|
+
patch.conclave = { defaultModel: conclaveDefault };
|
|
349
|
+
const lines = [`main: ${main}`, `compactor: ${compactor?.model ?? env.existing.compactor?.model ?? "not configured"}`, `conclave default: ${conclaveDefault}`, `thinking display: ${stepsPick}`, ...(webProvider === undefined ? [] : [`web search: ${webProvider}`])];
|
|
350
|
+
io.notify("info", lines.join("\n"));
|
|
351
|
+
if (!(await io.confirm("Write these settings?", true)))
|
|
352
|
+
throw new SetupCancelled();
|
|
353
|
+
let settings = mergeSettings(env.home, patch);
|
|
354
|
+
if (conclaveDefault === main && settings.conclave?.defaultModel !== undefined)
|
|
355
|
+
settings = unsetSettings(env.home, "conclave.defaultModel");
|
|
356
|
+
return { status: "done", settings };
|
|
357
|
+
}
|
|
358
|
+
catch (error) {
|
|
359
|
+
if (error instanceof SetupCancelled)
|
|
360
|
+
return { status: "cancelled" };
|
|
361
|
+
throw error;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
/** Used by --print and the TUI when no main model is set. */
|
|
365
|
+
export const SETUP_HINT = 'run "piroboros setup" first';
|
|
366
|
+
export { contextCheck };
|
package/dist/shell.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { delimiter, join } from "node:path";
|
|
3
|
+
// The shell is bash on every platform. On Windows that is Git Bash. The WSL launcher in System32 is never used.
|
|
4
|
+
export const BASH_HINT = "Install Git for Windows (https://git-scm.com/download/win), which includes Git Bash. On Linux and macOS install bash from your package manager.";
|
|
5
|
+
const isWslLauncher = (path) => /[\\/](?:system32|sysnative)[\\/]bash\.exe$/iu.test(path);
|
|
6
|
+
/** The bash executable to run commands with, or undefined when none is installed. */
|
|
7
|
+
export function findBash(env = process.env, platform = process.platform) {
|
|
8
|
+
if (platform !== "win32")
|
|
9
|
+
return existsSync("/bin/bash") ? "/bin/bash" : (env.PATH ?? "").split(delimiter).map((dir) => join(dir, "bash")).find((p) => existsSync(p));
|
|
10
|
+
const roots = [env.ProgramFiles, env["ProgramFiles(x86)"], env.LOCALAPPDATA && join(env.LOCALAPPDATA, "Programs")];
|
|
11
|
+
for (const root of roots) {
|
|
12
|
+
if (!root)
|
|
13
|
+
continue;
|
|
14
|
+
for (const rel of [join("Git", "bin", "bash.exe"), join("Git", "usr", "bin", "bash.exe")]) {
|
|
15
|
+
const candidate = join(root, rel);
|
|
16
|
+
if (existsSync(candidate))
|
|
17
|
+
return candidate;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return (env.PATH ?? env.Path ?? "").split(delimiter).filter(Boolean).map((dir) => join(dir, "bash.exe")).find((p) => !isWslLauncher(p) && existsSync(p));
|
|
21
|
+
}
|
|
22
|
+
const SGR = /\u001b\[[0-9;:]*m/gu;
|
|
23
|
+
const PARTIAL_ESCAPE = /\u001b(?:\[[0-9;:]*)?$/u;
|
|
24
|
+
/**
|
|
25
|
+
* Removes color codes (ESC [ ... m) from streamed text. A code split across two chunks is held back until it
|
|
26
|
+
* completes, per stream. Every other escape passes through and is shown as visible text by the terminal layer.
|
|
27
|
+
*/
|
|
28
|
+
export function sgrStripper() {
|
|
29
|
+
const carry = new Map();
|
|
30
|
+
return {
|
|
31
|
+
push(text, stream) {
|
|
32
|
+
let joined = (carry.get(stream) ?? "") + text;
|
|
33
|
+
carry.delete(stream);
|
|
34
|
+
joined = joined.replace(SGR, "");
|
|
35
|
+
const partial = PARTIAL_ESCAPE.exec(joined);
|
|
36
|
+
if (partial !== null) {
|
|
37
|
+
carry.set(stream, partial[0]);
|
|
38
|
+
joined = joined.slice(0, partial.index);
|
|
39
|
+
}
|
|
40
|
+
return joined;
|
|
41
|
+
},
|
|
42
|
+
flush(stream) {
|
|
43
|
+
const rest = carry.get(stream) ?? "";
|
|
44
|
+
carry.delete(stream);
|
|
45
|
+
return rest;
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/* --- output clipping (H2) ------------------------------------------------------------------------------------ */
|
|
50
|
+
export const CLIP_HEAD = 12_000;
|
|
51
|
+
export const CLIP_TAIL = 18_000;
|
|
52
|
+
/** Keeps the first CLIP_HEAD and the last CLIP_TAIL characters of a stream, and counts the rest. */
|
|
53
|
+
export class HeadTail {
|
|
54
|
+
head = "";
|
|
55
|
+
tail = "";
|
|
56
|
+
total = 0;
|
|
57
|
+
push(text) {
|
|
58
|
+
this.total += text.length;
|
|
59
|
+
if (this.head.length < CLIP_HEAD) {
|
|
60
|
+
const take = Math.min(text.length, CLIP_HEAD - this.head.length);
|
|
61
|
+
this.head += text.slice(0, take);
|
|
62
|
+
text = text.slice(take);
|
|
63
|
+
}
|
|
64
|
+
this.tail += text;
|
|
65
|
+
if (this.tail.length > CLIP_TAIL * 2)
|
|
66
|
+
this.tail = this.tail.slice(-CLIP_TAIL);
|
|
67
|
+
}
|
|
68
|
+
/** The text to show and how many characters it leaves out. */
|
|
69
|
+
result() {
|
|
70
|
+
let tail = this.tail.length > CLIP_TAIL ? this.tail.slice(-CLIP_TAIL) : this.tail;
|
|
71
|
+
const low = tail.charCodeAt(0);
|
|
72
|
+
if (this.total - this.head.length - tail.length > 0 && low >= 0xdc00 && low <= 0xdfff)
|
|
73
|
+
tail = tail.slice(1);
|
|
74
|
+
if (this.total - this.head.length - tail.length <= 0)
|
|
75
|
+
return { text: this.head + tail, omitted: 0 };
|
|
76
|
+
const end = this.head.charCodeAt(this.head.length - 1);
|
|
77
|
+
const head = end >= 0xd800 && end <= 0xdbff ? this.head.slice(0, -1) : this.head; // never cut a surrogate pair
|
|
78
|
+
const omitted = this.total - head.length - tail.length;
|
|
79
|
+
return { text: `${head}\n[${omitted} characters omitted from the middle of the output]\n${tail}`, omitted };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const CLIP_NOTE = `Output beyond ${CLIP_HEAD + CLIP_TAIL} characters is cut to its first ${CLIP_HEAD} and last ${CLIP_TAIL} characters with a marker in between. Nothing is saved to a file.`;
|
|
83
|
+
/**
|
|
84
|
+
* Wrap a shell tool so oversized output keeps its head and its tail. The tool's own tail retention would keep only
|
|
85
|
+
* the end, and its spill file is switched off in ShellEnv. A failed command keeps its output and its error.
|
|
86
|
+
*/
|
|
87
|
+
export function clipShellOutput(tool) {
|
|
88
|
+
const decoder = new TextDecoder("utf-8");
|
|
89
|
+
return {
|
|
90
|
+
...tool,
|
|
91
|
+
description: tool.description.replace(/ Output is truncated.*?temp file\./u, ` ${CLIP_NOTE}`),
|
|
92
|
+
outputLimits: { maxBytes: 4 * (CLIP_HEAD + CLIP_TAIL), maxLines: CLIP_HEAD + CLIP_TAIL, retain: "tail" },
|
|
93
|
+
async execute(args, api, context) {
|
|
94
|
+
const clip = new HeadTail();
|
|
95
|
+
// outputWindow is cleared so the environment never skips text the clip has not seen.
|
|
96
|
+
const inner = { ...api, outputWindow: undefined, output: (chunk, skipped) => { clip.push(typeof chunk === "string" ? chunk : decoder.decode(chunk, { stream: true })); api.output(chunk, skipped); } };
|
|
97
|
+
try {
|
|
98
|
+
const result = await tool.execute(args, inner, context);
|
|
99
|
+
const shown = clip.result();
|
|
100
|
+
return shown.omitted > 0 ? { ...result, output: [{ type: "text", text: shown.text }] } : result;
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
const shown = clip.result();
|
|
104
|
+
if (shown.omitted <= 0 || context.abortSignal?.aborted)
|
|
105
|
+
throw error;
|
|
106
|
+
return { output: [{ type: "text", text: shown.text }], isError: true, diagnostics: [{ severity: "error", message: error instanceof Error ? error.message : String(error) }] };
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|