arisa 5.1.68 → 5.2.7
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/README.md +7 -4
- package/package.json +1 -1
- package/src/core/agent/agent-manager.js +46 -6
- package/src/core/agent/agent-session-lifecycle.js +80 -3
- package/src/core/agent/core-tools.js +1 -1
- package/src/core/agent/pi-auth-login.js +1 -1
- package/src/core/agent/pi-runtime.js +1 -1
- package/src/core/agent/runtime-context.js +1 -1
- package/src/core/agent/worker-heap-circuit-breaker.js +122 -0
- package/src/core/artifacts/artifact-store.js +1 -1
- package/src/core/capabilities/capability-service.js +1 -1
- package/src/core/config/config-defaults.js +30 -1
- package/src/core/config/config-store.js +1 -1
- package/src/core/conversation/session-seed-store.js +1 -1
- package/src/core/tasks/task-store.js +1 -1
- package/src/core/tools/daemon-client.js +180 -0
- package/src/core/tools/daemon-processes.js +19 -3
- package/src/core/tools/daemon-protocol.js +72 -0
- package/src/core/tools/daemon-runtime.js +13 -490
- package/src/core/tools/daemon-worker.js +310 -0
- package/src/core/tools/ipc-client.js +2 -2
- package/src/core/tools/memory-pressure.js +56 -0
- package/src/core/tools/official-tool-installer.js +1 -1
- package/src/core/tools/tool-config.js +1 -1
- package/src/core/tools/tool-process-output.js +100 -0
- package/src/core/tools/tool-process-runner.js +175 -0
- package/src/core/tools/tool-registry.js +99 -187
- package/src/core/tools/tool-resource-note-store.js +1 -1
- package/src/core/tools/tool-usage-store.js +1 -1
- package/src/core/tools/weighted-resource-governor.js +188 -38
- package/src/index.js +14 -2
- package/src/official-tools.lock.json +424 -50
- package/src/platform/paths.js +152 -0
- package/src/runtime/bootstrap-cli.js +121 -0
- package/src/runtime/bootstrap-config.js +97 -0
- package/src/runtime/bootstrap-telegram.js +325 -0
- package/src/runtime/bootstrap.js +6 -543
- package/src/runtime/doctor.js +6 -3
- package/src/runtime/flush.js +1 -1
- package/src/runtime/ipc/ipc-server.js +1 -1
- package/src/runtime/log-viewer.js +1 -1
- package/src/runtime/oom-protection.js +20 -0
- package/src/runtime/paths.js +3 -151
- package/src/runtime/restart-receipt.js +1 -1
- package/src/runtime/service-manager.js +1 -1
- package/src/runtime/service-supervisor.js +14 -0
- package/src/runtime/slave-cli.js +1 -1
- package/src/runtime/tool-process-supervisor.js +1 -1
- package/src/runtime/tui.js +200 -0
- package/src/runtime/update-manager.js +1 -1
- package/src/runtime/worker-recovery-report.js +142 -0
- package/src/transport/telegram/bot.js +42 -320
- package/src/transport/telegram/prompt-builders.js +8 -3
- package/src/transport/telegram/telegram-prompt-controller.js +346 -0
- package/src/transport/telegram/workspace-topic-store.js +1 -1
- package/test/agent-session-lifecycle.test.js +92 -0
- package/test/architecture-boundaries.test.js +29 -0
- package/test/bootstrap.test.js +65 -0
- package/test/daemon-process-invocation.test.js +27 -0
- package/test/daemon-runtime.test.js +36 -1
- package/test/doctor.test.js +22 -0
- package/test/memory-pressure.test.js +36 -0
- package/test/model-selection.test.js +11 -1
- package/test/official-tool-dependencies.test.js +1 -1
- package/test/official-tool-installer.test.js +18 -1
- package/test/oom-protection.test.js +32 -0
- package/test/paths.test.js +7 -0
- package/test/pi-compaction.test.js +9 -0
- package/test/service-manager.test.js +6 -1
- package/test/telegram-prompt-controller.test.js +81 -0
- package/test/telegram-text-artifact.test.js +30 -0
- package/test/tool-registry-run.test.js +108 -4
- package/test/tui.test.js +41 -0
- package/test/weighted-resource-governor.test.js +97 -5
- package/test/worker-heap-circuit-breaker.test.js +79 -0
- package/test/worker-recovery-report.test.js +69 -0
- package/test-fixtures/fake-daemon.js +5 -0
package/src/runtime/bootstrap.js
CHANGED
|
@@ -2,15 +2,12 @@ import crypto from "node:crypto";
|
|
|
2
2
|
import { readFile, writeFile } from "node:fs/promises";
|
|
3
3
|
import readline from "node:readline/promises";
|
|
4
4
|
import { stdin as input, stdout as output } from "node:process";
|
|
5
|
-
import { spawn } from "node:child_process";
|
|
6
5
|
import { Bot } from "grammy";
|
|
7
|
-
import { createPiOAuthLogin } from "../core/agent/pi-auth-login.js";
|
|
8
|
-
import { createPiRuntime, formatPiModelOption, hasProviderAuth, listPiProviders, listProviderModels, supportsProviderOAuth } from "../core/agent/pi-runtime.js";
|
|
9
|
-
import { applyConfigDefaults, telegramConfigDefaults } from "../core/config/config-defaults.js";
|
|
10
6
|
import { prepareConfigForSave } from "../core/config/config-store.js";
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
7
|
+
import { configFile, ensureArisaHome } from "../platform/paths.js";
|
|
8
|
+
import { buildBootstrapConfig, parseYesNo } from "./bootstrap-config.js";
|
|
9
|
+
import { collectCliBootstrapChoices, openExternal } from "./bootstrap-cli.js";
|
|
10
|
+
import { runTelegramBootstrap } from "./bootstrap-telegram.js";
|
|
14
11
|
|
|
15
12
|
const ARISA_BANNER = [
|
|
16
13
|
" █████╗ ██████╗ ██╗███████╗ █████╗ ",
|
|
@@ -30,544 +27,10 @@ async function exists(file) {
|
|
|
30
27
|
}
|
|
31
28
|
}
|
|
32
29
|
|
|
33
|
-
export function buildConfig({ telegramApiKey, telegramMaxChatIds, authorizedChatIds = [], chatMeta = {}, provider, model, piApiKey }) {
|
|
34
|
-
return applyConfigDefaults({
|
|
35
|
-
telegram: {
|
|
36
|
-
token: telegramApiKey,
|
|
37
|
-
maxChatIds: telegramMaxChatIds,
|
|
38
|
-
authorizedChatIds,
|
|
39
|
-
chatMeta
|
|
40
|
-
},
|
|
41
|
-
pi: {
|
|
42
|
-
provider,
|
|
43
|
-
model,
|
|
44
|
-
apiKey: piApiKey
|
|
45
|
-
},
|
|
46
|
-
createdAt: new Date().toISOString()
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function sortBootstrapProviders(providers) {
|
|
51
|
-
const preferredOrder = ["openai-codex"];
|
|
52
|
-
const positions = new Map(providers.map((provider, index) => [provider.provider, index]));
|
|
53
|
-
|
|
54
|
-
return [...providers].sort((a, b) => {
|
|
55
|
-
const aPref = preferredOrder.indexOf(a.provider);
|
|
56
|
-
const bPref = preferredOrder.indexOf(b.provider);
|
|
57
|
-
const aRank = aPref === -1 ? Number.MAX_SAFE_INTEGER : aPref;
|
|
58
|
-
const bRank = bPref === -1 ? Number.MAX_SAFE_INTEGER : bPref;
|
|
59
|
-
if (aRank !== bRank) return aRank - bRank;
|
|
60
|
-
return (positions.get(a.provider) || 0) - (positions.get(b.provider) || 0);
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function sortBootstrapModels(provider, models) {
|
|
65
|
-
const preferred = {
|
|
66
|
-
"openai-codex": ["gpt-5.5"]
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const priority = preferred[provider] || [];
|
|
70
|
-
const positions = new Map(models.map((model, index) => [model.id, index]));
|
|
71
|
-
|
|
72
|
-
return [...models].sort((a, b) => {
|
|
73
|
-
const aIndex = priority.indexOf(a.id);
|
|
74
|
-
const bIndex = priority.indexOf(b.id);
|
|
75
|
-
const aRank = aIndex === -1 ? Number.MAX_SAFE_INTEGER : aIndex;
|
|
76
|
-
const bRank = bIndex === -1 ? Number.MAX_SAFE_INTEGER : bIndex;
|
|
77
|
-
if (aRank !== bRank) return aRank - bRank;
|
|
78
|
-
return (positions.get(b.id) || 0) - (positions.get(a.id) || 0);
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
30
|
function createSetupToken() {
|
|
83
31
|
return crypto.randomBytes(18).toString("base64url");
|
|
84
32
|
}
|
|
85
33
|
|
|
86
|
-
function parsePositiveInteger(value, fallback = null) {
|
|
87
|
-
const number = Number(value);
|
|
88
|
-
if (!Number.isFinite(number) || number <= 0) return fallback;
|
|
89
|
-
return Math.floor(number);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function selectByIndex(items, value, fallbackIndex = 0) {
|
|
93
|
-
const index = parsePositiveInteger(value, fallbackIndex + 1) - 1;
|
|
94
|
-
return items[Math.max(0, Math.min(items.length - 1, index))];
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function parseYesNo(value, fallback = true) {
|
|
98
|
-
const text = String(value ?? "").trim().toLowerCase();
|
|
99
|
-
if (!text) return fallback;
|
|
100
|
-
if (["y", "yes", "s", "si", "sí"].includes(text)) return true;
|
|
101
|
-
if (["n", "no"].includes(text)) return false;
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function getIncomingChatMeta(ctx) {
|
|
106
|
-
return {
|
|
107
|
-
languageCode: ctx.from?.language_code || "",
|
|
108
|
-
username: ctx.from?.username || "",
|
|
109
|
-
firstName: ctx.from?.first_name || "",
|
|
110
|
-
lastName: ctx.from?.last_name || ""
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function formatProviderOption(item) {
|
|
115
|
-
const authLabel = item.authConfigured ? "auth configured" : item.supportsOAuth ? "login or API key" : "API key";
|
|
116
|
-
return `${item.provider} (${item.modelCount} models, ${authLabel})`;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function selectPiLoginOption(options = []) {
|
|
120
|
-
return options.find((option) => /device/i.test(`${option.id} ${option.label}`))
|
|
121
|
-
|| options.find((option) => /browser|oauth|web/i.test(`${option.id} ${option.label}`))
|
|
122
|
-
|| options[0]
|
|
123
|
-
|| null;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async function maybeOpenExternal(url) {
|
|
127
|
-
if (!url) return;
|
|
128
|
-
await new Promise((resolve) => {
|
|
129
|
-
let child;
|
|
130
|
-
if (process.platform === "darwin") {
|
|
131
|
-
child = spawn("open", [url], { stdio: "ignore" });
|
|
132
|
-
} else if (process.platform === "win32") {
|
|
133
|
-
child = spawn("cmd", ["/c", "start", "", url], { stdio: "ignore" });
|
|
134
|
-
} else {
|
|
135
|
-
child = spawn("xdg-open", [url], { stdio: "ignore" });
|
|
136
|
-
}
|
|
137
|
-
child.on("exit", () => resolve());
|
|
138
|
-
child.on("error", () => resolve());
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async function runInternalPiLogin(provider, { rl = null } = {}) {
|
|
143
|
-
const login = createPiOAuthLogin({
|
|
144
|
-
provider,
|
|
145
|
-
onSelect: async ({ message, options }) => {
|
|
146
|
-
const selected = selectPiLoginOption(options);
|
|
147
|
-
if (!selected) return undefined;
|
|
148
|
-
console.log(`${message}\nUsing: ${selected.label || selected.id}\n`);
|
|
149
|
-
return selected.id;
|
|
150
|
-
},
|
|
151
|
-
onAuth: async ({ url, instructions, controller }) => {
|
|
152
|
-
console.log(`${instructions || "Open this URL to continue authentication:"}\n${url}\n`);
|
|
153
|
-
await maybeOpenExternal(url);
|
|
154
|
-
if (controller.oauthProvider.usesCallbackServer && rl) {
|
|
155
|
-
const pasted = (await rl.question("Paste the redirect URL here if the browser does not return automatically, or press Enter to keep waiting: ")).trim();
|
|
156
|
-
if (pasted) controller.submitManualCode(pasted);
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
onDeviceCode: async ({ userCode, verificationUri }) => {
|
|
160
|
-
console.log(`Open this URL: ${verificationUri}`);
|
|
161
|
-
console.log(`Then enter code: ${userCode}\n`);
|
|
162
|
-
await maybeOpenExternal(verificationUri);
|
|
163
|
-
},
|
|
164
|
-
onPrompt: async ({ message }) => {
|
|
165
|
-
if (!rl) {
|
|
166
|
-
throw new Error(`Pi login for ${provider} requires interactive input: ${message}`);
|
|
167
|
-
}
|
|
168
|
-
return (await rl.question(`${message} `)).trim();
|
|
169
|
-
},
|
|
170
|
-
onProgress: (message) => {
|
|
171
|
-
console.log(message);
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
await login.promise;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async function collectCliBootstrapChoices({ telegramApiKey, rl, ask }) {
|
|
178
|
-
const telegramMaxChatIds = Number(await ask("Maximum authorized chat IDs", "1"));
|
|
179
|
-
|
|
180
|
-
const runtime = createPiRuntime();
|
|
181
|
-
const providers = sortBootstrapProviders(listPiProviders(runtime));
|
|
182
|
-
console.log("\nAvailable Pi providers:");
|
|
183
|
-
providers.forEach((item, index) => {
|
|
184
|
-
console.log(`${index + 1}. ${formatProviderOption(item)}`);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
const selectedProvider = selectByIndex(providers, await ask("Select Pi provider by number", "1"));
|
|
188
|
-
const models = sortBootstrapModels(selectedProvider.provider, listProviderModels(selectedProvider.provider, runtime));
|
|
189
|
-
console.log(`\nAvailable models for ${selectedProvider.provider}:`);
|
|
190
|
-
models.forEach((model, index) => {
|
|
191
|
-
console.log(`${index + 1}. ${formatPiModelOption(model)}`);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
const selectedModel = selectByIndex(models, await ask("Select Pi model by number", "1"));
|
|
195
|
-
const selectedAuthReady = hasProviderAuth(selectedProvider.provider, runtime);
|
|
196
|
-
const providerSupportsOAuth = supportsProviderOAuth(selectedProvider.provider, runtime);
|
|
197
|
-
console.log(`Selected model: ${selectedModel.provider}/${selectedModel.id}`);
|
|
198
|
-
console.log(`Existing Pi auth for ${selectedProvider.provider}: ${selectedAuthReady ? "yes" : "no"}`);
|
|
199
|
-
if (providerSupportsOAuth) {
|
|
200
|
-
console.log("Pi auth tip: leaving the API key empty will start Pi's internal login flow for this provider.");
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
let piApiKey = "";
|
|
204
|
-
while (true) {
|
|
205
|
-
piApiKey = (await rl.question(`Pi API key for ${selectedProvider.provider} (optional): `)).trim();
|
|
206
|
-
if (piApiKey) break;
|
|
207
|
-
if (hasProviderAuth(selectedProvider.provider, createPiRuntime())) break;
|
|
208
|
-
|
|
209
|
-
if (!providerSupportsOAuth) {
|
|
210
|
-
console.log(`No existing Pi auth found for ${selectedProvider.provider}. This provider requires an API key.`);
|
|
211
|
-
continue;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
console.log(`No existing Pi auth found for ${selectedProvider.provider}. Starting internal Pi login...`);
|
|
215
|
-
try {
|
|
216
|
-
await runInternalPiLogin(selectedProvider.provider, { rl });
|
|
217
|
-
} catch (error) {
|
|
218
|
-
console.log(`Internal Pi login failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (hasProviderAuth(selectedProvider.provider, createPiRuntime())) {
|
|
222
|
-
console.log(`Detected Pi auth for ${selectedProvider.provider}. Continuing bootstrap.`);
|
|
223
|
-
break;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
console.log(`Pi auth for ${selectedProvider.provider} is still missing after login.`);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
return {
|
|
230
|
-
config: buildConfig({
|
|
231
|
-
telegramApiKey,
|
|
232
|
-
telegramMaxChatIds,
|
|
233
|
-
provider: selectedProvider.provider,
|
|
234
|
-
model: selectedModel.id,
|
|
235
|
-
piApiKey
|
|
236
|
-
}),
|
|
237
|
-
startInBackground: false,
|
|
238
|
-
viaTelegram: false
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
async function runTelegramBootstrap({ telegramApiKey, setupToken, botInfo }) {
|
|
243
|
-
const bot = new Bot(telegramApiKey);
|
|
244
|
-
const runtime = createPiRuntime();
|
|
245
|
-
const providers = sortBootstrapProviders(listPiProviders(runtime));
|
|
246
|
-
let setupChatId = null;
|
|
247
|
-
let chatMeta = {};
|
|
248
|
-
let state = "await-start";
|
|
249
|
-
let telegramMaxChatIds = 1;
|
|
250
|
-
let selectedProvider = null;
|
|
251
|
-
let selectedModel = null;
|
|
252
|
-
let piApiKey = "";
|
|
253
|
-
let activeLogin = null;
|
|
254
|
-
let completed = false;
|
|
255
|
-
let resolveResult;
|
|
256
|
-
let rejectResult;
|
|
257
|
-
|
|
258
|
-
const resultPromise = new Promise((resolve, reject) => {
|
|
259
|
-
resolveResult = resolve;
|
|
260
|
-
rejectResult = reject;
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
const sendSetupMessage = async (text, extra = {}) => {
|
|
264
|
-
if (!setupChatId) return;
|
|
265
|
-
await bot.api.sendMessage(setupChatId, text, extra);
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
const showSetupPrompt = async (ctx, text, extra = {}) => {
|
|
269
|
-
const messageId = ctx?.callbackQuery?.message?.message_id;
|
|
270
|
-
if (messageId && ctx.chat?.id) {
|
|
271
|
-
try {
|
|
272
|
-
await ctx.api.editMessageText(ctx.chat.id, messageId, text, extra);
|
|
273
|
-
return;
|
|
274
|
-
} catch {}
|
|
275
|
-
}
|
|
276
|
-
await sendSetupMessage(text, extra);
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
const isSetupChat = (ctx) => setupChatId && ctx.chat?.id === setupChatId;
|
|
280
|
-
|
|
281
|
-
const complete = (startInBackground) => {
|
|
282
|
-
if (completed) return;
|
|
283
|
-
completed = true;
|
|
284
|
-
resolveResult({
|
|
285
|
-
config: buildConfig({
|
|
286
|
-
telegramApiKey,
|
|
287
|
-
telegramMaxChatIds,
|
|
288
|
-
authorizedChatIds: [setupChatId],
|
|
289
|
-
chatMeta: { [setupChatId]: chatMeta },
|
|
290
|
-
provider: selectedProvider.provider,
|
|
291
|
-
model: selectedModel.id,
|
|
292
|
-
piApiKey
|
|
293
|
-
}),
|
|
294
|
-
startInBackground,
|
|
295
|
-
viaTelegram: true
|
|
296
|
-
});
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
const askProvider = async (ctx = null, page = 0) => {
|
|
300
|
-
state = "provider";
|
|
301
|
-
await showSetupPrompt(ctx, "Select the Pi provider Arisa should use:", {
|
|
302
|
-
reply_markup: buildPagedInlineKeyboard("provider", providers.map((provider) => ({ text: formatProviderOption(provider) })), {
|
|
303
|
-
page,
|
|
304
|
-
pageSize: telegramConfigDefaults.modelPickerPageSize
|
|
305
|
-
})
|
|
306
|
-
});
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
const askModel = async (ctx = null, page = 0) => {
|
|
310
|
-
state = "model";
|
|
311
|
-
const models = sortBootstrapModels(selectedProvider.provider, listProviderModels(selectedProvider.provider, createPiRuntime()));
|
|
312
|
-
const keyboard = buildPagedInlineKeyboard("model", models.map((model) => ({ text: formatPiModelOption(model) })), {
|
|
313
|
-
page,
|
|
314
|
-
pageSize: telegramConfigDefaults.modelPickerPageSize
|
|
315
|
-
});
|
|
316
|
-
keyboard.inline_keyboard.push([{ text: "Back to providers", callback_data: "back:provider" }]);
|
|
317
|
-
await showSetupPrompt(ctx, `Select the model for ${selectedProvider.provider}:`, {
|
|
318
|
-
reply_markup: keyboard
|
|
319
|
-
});
|
|
320
|
-
};
|
|
321
|
-
|
|
322
|
-
const askBackground = async (ctx = null) => {
|
|
323
|
-
state = "background";
|
|
324
|
-
await showSetupPrompt(ctx, "Bootstrap complete. Keep Arisa running in background now?", {
|
|
325
|
-
reply_markup: {
|
|
326
|
-
inline_keyboard: [
|
|
327
|
-
[{ text: "Yes, start in background", callback_data: "background:yes" }],
|
|
328
|
-
[{ text: "No, continue in foreground", callback_data: "background:no" }]
|
|
329
|
-
]
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
const askApiKey = async (ctx = null) => {
|
|
335
|
-
state = "pi-api-key";
|
|
336
|
-
await showSetupPrompt(ctx, `Send the Pi API key for ${selectedProvider.provider}.`, {
|
|
337
|
-
reply_markup: { inline_keyboard: [] }
|
|
338
|
-
});
|
|
339
|
-
};
|
|
340
|
-
|
|
341
|
-
const askAuthMethod = async (ctx = null) => {
|
|
342
|
-
const providerRuntime = createPiRuntime();
|
|
343
|
-
const selectedAuthReady = hasProviderAuth(selectedProvider.provider, providerRuntime);
|
|
344
|
-
const providerSupportsOAuth = supportsProviderOAuth(selectedProvider.provider, providerRuntime);
|
|
345
|
-
const buttons = [];
|
|
346
|
-
|
|
347
|
-
if (selectedAuthReady) {
|
|
348
|
-
buttons.push([{ text: "Use existing Pi auth", callback_data: "auth:existing" }]);
|
|
349
|
-
}
|
|
350
|
-
if (providerSupportsOAuth) {
|
|
351
|
-
buttons.push([{ text: selectedAuthReady ? "Run Pi login again" : "Start Pi login", callback_data: "auth:login" }]);
|
|
352
|
-
}
|
|
353
|
-
buttons.push([{ text: "Enter API key", callback_data: "auth:key" }]);
|
|
354
|
-
buttons.push([{ text: "Back to models", callback_data: "back:model" }]);
|
|
355
|
-
|
|
356
|
-
state = "auth-method";
|
|
357
|
-
await showSetupPrompt(ctx, [
|
|
358
|
-
`Selected model: ${selectedProvider.provider}/${selectedModel.id}`,
|
|
359
|
-
`Existing Pi auth for ${selectedProvider.provider}: ${selectedAuthReady ? "yes" : "no"}`,
|
|
360
|
-
"Choose how Arisa should authenticate Pi."
|
|
361
|
-
].join("\n"), {
|
|
362
|
-
reply_markup: { inline_keyboard: buttons }
|
|
363
|
-
});
|
|
364
|
-
};
|
|
365
|
-
|
|
366
|
-
const finishPiLogin = async (login) => {
|
|
367
|
-
try {
|
|
368
|
-
await login.promise;
|
|
369
|
-
activeLogin = null;
|
|
370
|
-
if (hasProviderAuth(selectedProvider.provider, createPiRuntime())) {
|
|
371
|
-
await sendSetupMessage(`Detected Pi auth for ${selectedProvider.provider}.`);
|
|
372
|
-
await askBackground();
|
|
373
|
-
return;
|
|
374
|
-
}
|
|
375
|
-
await sendSetupMessage(`Pi auth for ${selectedProvider.provider} is still missing after login.`);
|
|
376
|
-
await askAuthMethod();
|
|
377
|
-
} catch (error) {
|
|
378
|
-
activeLogin = null;
|
|
379
|
-
await sendSetupMessage(`Pi login failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
380
|
-
await askAuthMethod();
|
|
381
|
-
}
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
const startPiLogin = async () => {
|
|
385
|
-
if (hasProviderAuth(selectedProvider.provider, createPiRuntime())) {
|
|
386
|
-
await sendSetupMessage(`Existing Pi auth for ${selectedProvider.provider} detected.`);
|
|
387
|
-
await askBackground();
|
|
388
|
-
return;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
state = "pi-login";
|
|
392
|
-
const login = createPiOAuthLogin({
|
|
393
|
-
provider: selectedProvider.provider,
|
|
394
|
-
onSelect: async ({ message, options }) => {
|
|
395
|
-
const selected = selectPiLoginOption(options);
|
|
396
|
-
if (!selected) return undefined;
|
|
397
|
-
await sendSetupMessage(`${message}\nUsing: ${selected.label || selected.id}`);
|
|
398
|
-
return selected.id;
|
|
399
|
-
},
|
|
400
|
-
onAuth: async ({ url, instructions }) => {
|
|
401
|
-
await sendSetupMessage([
|
|
402
|
-
instructions || "Open this URL to continue Pi authentication:",
|
|
403
|
-
url,
|
|
404
|
-
"After login, paste the full redirect URL back here."
|
|
405
|
-
].join("\n"));
|
|
406
|
-
},
|
|
407
|
-
onDeviceCode: async ({ userCode, verificationUri, expiresInSeconds }) => {
|
|
408
|
-
const { text, ...options } = buildDeviceCodeTelegramMessage({ userCode, verificationUri, expiresInSeconds });
|
|
409
|
-
await sendSetupMessage(text, options);
|
|
410
|
-
},
|
|
411
|
-
onPrompt: async ({ message, controller }) => {
|
|
412
|
-
await sendSetupMessage(`${message}\nReply here with the value.`);
|
|
413
|
-
return controller.waitForManualCode();
|
|
414
|
-
},
|
|
415
|
-
onProgress: (message) => {
|
|
416
|
-
if (message) console.log(`[bootstrap] Pi auth progress: ${message}`);
|
|
417
|
-
}
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
activeLogin = login;
|
|
421
|
-
finishPiLogin(login);
|
|
422
|
-
};
|
|
423
|
-
|
|
424
|
-
bot.catch((error) => {
|
|
425
|
-
console.error("Telegram setup bot error:", error);
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
bot.command("start", async (ctx) => {
|
|
429
|
-
if (String(ctx.match || "").trim() !== setupToken) {
|
|
430
|
-
await ctx.reply("Invalid setup link. Use the link shown in the Arisa bootstrap terminal.");
|
|
431
|
-
return;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
if (setupChatId && ctx.chat.id !== setupChatId) {
|
|
435
|
-
await ctx.reply("This setup session is already connected to another chat.");
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
setupChatId = ctx.chat.id;
|
|
440
|
-
chatMeta = getIncomingChatMeta(ctx);
|
|
441
|
-
await ctx.reply([
|
|
442
|
-
`Connected to @${botInfo.username}.`,
|
|
443
|
-
"This chat will be the only Telegram chat authorized during setup."
|
|
444
|
-
].join("\n"));
|
|
445
|
-
await askProvider();
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
bot.on("callback_query:data", async (ctx) => {
|
|
449
|
-
await ctx.answerCallbackQuery().catch(() => {});
|
|
450
|
-
if (!isSetupChat(ctx)) return;
|
|
451
|
-
|
|
452
|
-
const data = String(ctx.callbackQuery.data || "");
|
|
453
|
-
const [action, rawValue] = data.split(":");
|
|
454
|
-
|
|
455
|
-
if (action === "noop") return;
|
|
456
|
-
|
|
457
|
-
if (action === "back" && rawValue === "provider" && state === "model") {
|
|
458
|
-
selectedProvider = null;
|
|
459
|
-
selectedModel = null;
|
|
460
|
-
await askProvider(ctx);
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
if (action === "back" && rawValue === "model" && state === "auth-method") {
|
|
465
|
-
selectedModel = null;
|
|
466
|
-
await askModel(ctx);
|
|
467
|
-
return;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
if (action === "provider-page" && state === "provider") {
|
|
471
|
-
await askProvider(ctx, Number(rawValue));
|
|
472
|
-
return;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
if (action === "model-page" && state === "model") {
|
|
476
|
-
await askModel(ctx, Number(rawValue));
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
if (action === "provider" && state === "provider") {
|
|
481
|
-
selectedProvider = providers[Number(rawValue)];
|
|
482
|
-
if (!selectedProvider) return;
|
|
483
|
-
await askModel(ctx);
|
|
484
|
-
return;
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
if (action === "model" && state === "model") {
|
|
488
|
-
const models = sortBootstrapModels(selectedProvider.provider, listProviderModels(selectedProvider.provider, createPiRuntime()));
|
|
489
|
-
selectedModel = models[Number(rawValue)];
|
|
490
|
-
if (!selectedModel) return;
|
|
491
|
-
await askAuthMethod(ctx);
|
|
492
|
-
return;
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
if (action === "auth" && state === "auth-method") {
|
|
496
|
-
if (rawValue === "existing") {
|
|
497
|
-
if (hasProviderAuth(selectedProvider.provider, createPiRuntime())) {
|
|
498
|
-
await askBackground(ctx);
|
|
499
|
-
} else {
|
|
500
|
-
await sendSetupMessage(`No existing Pi auth found for ${selectedProvider.provider}.`);
|
|
501
|
-
await askAuthMethod(ctx);
|
|
502
|
-
}
|
|
503
|
-
return;
|
|
504
|
-
}
|
|
505
|
-
if (rawValue === "login") {
|
|
506
|
-
await startPiLogin();
|
|
507
|
-
return;
|
|
508
|
-
}
|
|
509
|
-
if (rawValue === "key") {
|
|
510
|
-
await askApiKey(ctx);
|
|
511
|
-
return;
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
if (action === "background" && state === "background") {
|
|
516
|
-
await sendSetupMessage(rawValue === "yes"
|
|
517
|
-
? "Saving config. Arisa will start in background now."
|
|
518
|
-
: "Saving config. Arisa will continue in foreground.");
|
|
519
|
-
complete(rawValue === "yes");
|
|
520
|
-
}
|
|
521
|
-
});
|
|
522
|
-
|
|
523
|
-
bot.on("message:text", async (ctx) => {
|
|
524
|
-
if (!isSetupChat(ctx)) return;
|
|
525
|
-
const text = String(ctx.message.text || "").trim();
|
|
526
|
-
if (!text || text.startsWith("/")) return;
|
|
527
|
-
|
|
528
|
-
if (state === "pi-api-key") {
|
|
529
|
-
piApiKey = text;
|
|
530
|
-
await askBackground();
|
|
531
|
-
return;
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
if (state === "pi-login" && activeLogin?.manualInputRequested) {
|
|
535
|
-
if (activeLogin.submitManualCode(text)) {
|
|
536
|
-
await ctx.reply("Got it. Finishing Pi login now...");
|
|
537
|
-
}
|
|
538
|
-
return;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
if (state === "background") {
|
|
542
|
-
const answer = parseYesNo(text, true);
|
|
543
|
-
if (answer === null) {
|
|
544
|
-
await ctx.reply("Please answer yes or no.");
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
await ctx.reply(answer
|
|
548
|
-
? "Saving config. Arisa will start in background now."
|
|
549
|
-
: "Saving config. Arisa will continue in foreground.");
|
|
550
|
-
complete(answer);
|
|
551
|
-
}
|
|
552
|
-
});
|
|
553
|
-
|
|
554
|
-
await bot.api.deleteWebhook({ drop_pending_updates: true });
|
|
555
|
-
console.log("Waiting for Telegram setup to complete...");
|
|
556
|
-
const polling = bot.start().then(() => {
|
|
557
|
-
if (!completed) throw new Error("Telegram setup bot stopped before bootstrap completed.");
|
|
558
|
-
});
|
|
559
|
-
|
|
560
|
-
try {
|
|
561
|
-
return await Promise.race([resultPromise, polling]);
|
|
562
|
-
} catch (error) {
|
|
563
|
-
rejectResult(error);
|
|
564
|
-
throw error;
|
|
565
|
-
} finally {
|
|
566
|
-
bot.stop();
|
|
567
|
-
await polling.catch(() => {});
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
|
|
571
34
|
export async function bootstrapIfNeeded({ force = false } = {}) {
|
|
572
35
|
await ensureArisaHome();
|
|
573
36
|
if (!force && await exists(configFile)) {
|
|
@@ -597,7 +60,7 @@ export async function bootstrapIfNeeded({ force = false } = {}) {
|
|
|
597
60
|
const setupToken = createSetupToken();
|
|
598
61
|
const setupLink = `https://t.me/${botInfo.username}?start=${setupToken}`;
|
|
599
62
|
console.log(`\nOpen this link to continue setup in Telegram:\n${setupLink}\n`);
|
|
600
|
-
await
|
|
63
|
+
await openExternal(setupLink);
|
|
601
64
|
result = await runTelegramBootstrap({ telegramApiKey, setupToken, botInfo });
|
|
602
65
|
} else {
|
|
603
66
|
result = await collectCliBootstrapChoices({ telegramApiKey, rl, ask });
|
|
@@ -615,4 +78,4 @@ export async function bootstrapIfNeeded({ force = false } = {}) {
|
|
|
615
78
|
}
|
|
616
79
|
}
|
|
617
80
|
|
|
618
|
-
export { configFile };
|
|
81
|
+
export { buildBootstrapConfig as buildConfig, configFile };
|
package/src/runtime/doctor.js
CHANGED
|
@@ -5,7 +5,7 @@ import process from "node:process";
|
|
|
5
5
|
import { promisify } from "node:util";
|
|
6
6
|
import { stopManagedDaemon, unregisterManagedDaemon } from "../core/tools/daemon-processes.js";
|
|
7
7
|
import { getServiceStatus, serviceEntryFile } from "./service-manager.js";
|
|
8
|
-
import { arisaHomeDir } from "
|
|
8
|
+
import { arisaHomeDir } from "../platform/paths.js";
|
|
9
9
|
import { renderTextReport, reportRow, wrapReportText } from "./report-format.js";
|
|
10
10
|
|
|
11
11
|
const execFileAsync = promisify(execFile);
|
|
@@ -314,7 +314,8 @@ export async function runDoctor({
|
|
|
314
314
|
unregisterDaemon = unregisterManagedDaemon,
|
|
315
315
|
inspectResources = inspectSystemResources,
|
|
316
316
|
inspectInfrastructure = null,
|
|
317
|
-
inspectToolDependencies = null
|
|
317
|
+
inspectToolDependencies = null,
|
|
318
|
+
supervisorPid = Number.parseInt(process.env.ARISA_SUPERVISOR_PID || "", 10)
|
|
318
319
|
}) {
|
|
319
320
|
assertDoctorPolicy(doctorPolicy);
|
|
320
321
|
const runtime = await agentManager.getRuntimeDiagnostic();
|
|
@@ -362,7 +363,9 @@ export async function runDoctor({
|
|
|
362
363
|
|
|
363
364
|
const processByPid = new Map(processes.map((record) => [record.pid, record]));
|
|
364
365
|
const currentService = await serviceStatus();
|
|
365
|
-
|
|
366
|
+
const belongsToCurrentService = currentService.pid === process.pid
|
|
367
|
+
|| (Number.isSafeInteger(supervisorPid) && currentService.pid === supervisorPid);
|
|
368
|
+
if (currentService.running && !belongsToCurrentService) {
|
|
366
369
|
const registered = processByPid.get(currentService.pid);
|
|
367
370
|
if (registered && isArisaServiceProcess(registered)) {
|
|
368
371
|
try {
|
package/src/runtime/flush.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import net from "node:net";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { chmod, mkdir, unlink } from "node:fs/promises";
|
|
4
|
-
import { arisaIpcSocketFile } from "
|
|
4
|
+
import { arisaIpcSocketFile } from "../../platform/paths.js";
|
|
5
5
|
|
|
6
6
|
function writeResponse(socket, response) {
|
|
7
7
|
socket.write(`${JSON.stringify(response)}\n`);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { open, readFile, stat } from "node:fs/promises";
|
|
2
2
|
import { cliLogConfig } from "../core/config/config-defaults.js";
|
|
3
|
-
import { ensureArisaHome, serviceLogFile } from "
|
|
3
|
+
import { ensureArisaHome, serviceLogFile } from "../platform/paths.js";
|
|
4
4
|
|
|
5
5
|
const readChunkSize = 64 * 1024;
|
|
6
6
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
|
|
3
|
+
const defaultCoreOomScoreAdjust = -900;
|
|
4
|
+
|
|
5
|
+
export async function protectCoreFromOom({
|
|
6
|
+
platform = process.platform,
|
|
7
|
+
score = defaultCoreOomScoreAdjust,
|
|
8
|
+
writeScore = (value) => writeFile("/proc/self/oom_score_adj", `${value}\n`, "utf8"),
|
|
9
|
+
logger
|
|
10
|
+
} = {}) {
|
|
11
|
+
if (platform !== "linux") return false;
|
|
12
|
+
try {
|
|
13
|
+
await writeScore(score);
|
|
14
|
+
logger?.log("service", `core OOM priority protected at ${score}`);
|
|
15
|
+
return true;
|
|
16
|
+
} catch (error) {
|
|
17
|
+
logger?.log("service", `core OOM priority could not be lowered: ${error?.code || error?.message || error}`);
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|