arisa 4.0.3 → 4.0.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arisa",
3
- "version": "4.0.3",
3
+ "version": "4.0.5",
4
4
  "description": "Telegram + Pi Agent modular assistant",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -23,6 +23,25 @@ function requiresProviderAuth(model) {
23
23
  return !isLocalBaseUrl(model?.baseUrl);
24
24
  }
25
25
 
26
+ async function promptAndThrowOnAssistantError(session, prompt) {
27
+ let assistantErrorMessage = "";
28
+ const unsubscribe = session.subscribe((event) => {
29
+ if (event.type === "message_end" && event.message?.stopReason === "error") {
30
+ assistantErrorMessage = event.message.errorMessage || "assistant message ended with error";
31
+ }
32
+ });
33
+
34
+ try {
35
+ await session.prompt(prompt);
36
+ } finally {
37
+ unsubscribe();
38
+ }
39
+
40
+ if (assistantErrorMessage) {
41
+ throw new Error(assistantErrorMessage);
42
+ }
43
+ }
44
+
26
45
  function mimeMatches(pattern, mimeType = "") {
27
46
  if (!pattern || !mimeType) return false;
28
47
  if (pattern === mimeType) return true;
@@ -72,6 +91,10 @@ export class AgentManager {
72
91
  this.pendingNewSessions.add(sessionKey);
73
92
  }
74
93
 
94
+ clearSessionCache(chatId) {
95
+ this.sessions.delete(String(chatId));
96
+ }
97
+
75
98
  createSessionManager(chatId) {
76
99
  const sessionKey = String(chatId);
77
100
  const sessionDir = getChatPiSessionsDir(sessionKey);
@@ -103,7 +126,7 @@ export class AgentManager {
103
126
  model,
104
127
  sessionManager: SessionManager.inMemory(),
105
128
  });
106
- await withTimeout(session.prompt("Reply with exactly: OK"), {
129
+ await withTimeout(promptAndThrowOnAssistantError(session, "Reply with exactly: OK"), {
107
130
  timeoutMs: piValidationTimeoutMs,
108
131
  label: "Pi validation prompt"
109
132
  });
@@ -3,6 +3,7 @@ import { ArtifactStore } from "../core/artifacts/artifact-store.js";
3
3
  import { ToolRegistry } from "../core/tools/tool-registry.js";
4
4
  import { TaskStore } from "../core/tasks/task-store.js";
5
5
  import { AgentManager } from "../core/agent/agent-manager.js";
6
+ import { getErrorMessage, getPiAuthIssue } from "../core/agent/auth-flow.js";
6
7
  import { createTelegramBot } from "../transport/telegram/bot.js";
7
8
  import { createToolProcessSupervisor } from "./tool-process-supervisor.js";
8
9
 
@@ -64,16 +65,22 @@ export async function createApp({ logger, runtimeOverrides, webhookUrl, setHttpR
64
65
  return {
65
66
  async start() {
66
67
  logger?.log("app", `validating Pi model ${config.pi.provider}/${config.pi.model}`);
68
+ let skipAgentStartupPrompts = false;
67
69
  try {
68
70
  await agentManager.validatePiAgent();
69
71
  } catch (error) {
72
+ const issue = getPiAuthIssue(error);
73
+ if (!issue) {
74
+ throw error;
75
+ }
76
+ skipAgentStartupPrompts = true;
77
+ logger?.error("app", `Pi auth validation failed; starting Telegram in auth recovery mode: ${getErrorMessage(error)}`);
70
78
  await bot.notifyPiAuthIssue?.(error);
71
- throw error;
72
79
  }
73
80
  await toolProcessSupervisor.start();
74
81
  logger?.log("app", "starting Telegram bot");
75
82
  try {
76
- await bot.start();
83
+ await bot.start({ skipAgentStartupPrompts });
77
84
  } catch (error) {
78
85
  await toolProcessSupervisor.stop();
79
86
  throw error;
@@ -532,6 +532,7 @@ export async function createTelegramBot({ config, artifactStore, toolRegistry, t
532
532
  await withTyping(ctx, async () => {
533
533
  try {
534
534
  await agentManager.validatePiAgent();
535
+ agentManager.clearSessionCache(ctx.chat.id);
535
536
  await ctx.reply(buildPiAuthTelegramMessage({ config, verified: true }));
536
537
  } catch (error) {
537
538
  const issue = getPiAuthIssue(error) || { kind: "validation-failed", message: getErrorMessage(error) };
@@ -561,26 +562,30 @@ export async function createTelegramBot({ config, artifactStore, toolRegistry, t
561
562
  });
562
563
 
563
564
  return {
564
- async start() {
565
+ async start({ skipAgentStartupPrompts = false } = {}) {
565
566
  config.telegram.chatMeta ||= {};
566
- for (const chatId of config.telegram.authorizedChatIds || []) {
567
- try {
568
- logger?.log("telegram", `generating startup message for chat ${chatId}`);
569
- const chatMeta = config.telegram.chatMeta[chatId] || {};
570
- const welcomePrompt = [
571
- "System event: Arisa has just started.",
572
- `chatId: ${chatId}`,
573
- `preferredTelegramLanguageCode: ${chatMeta.languageCode || "unknown"}`,
574
- chatMeta.username ? `username: ${chatMeta.username}` : null,
575
- chatMeta.firstName ? `firstName: ${chatMeta.firstName}` : null,
576
- "Send a short welcome-back message for Telegram.",
577
- "Keep it brief, warm, and natural.",
578
- "Use the user's Telegram language when possible.",
579
- "Do not mention internal implementation details."
580
- ].filter(Boolean).join("\n");
581
- await enqueuePrompt({ chatId, prompt: welcomePrompt, label: "startup message" });
582
- } catch (error) {
583
- logger?.log("telegram", `startup message failed for chat ${chatId}: ${error instanceof Error ? error.message : String(error)}`);
567
+ if (skipAgentStartupPrompts) {
568
+ logger?.log("telegram", "skipping agent startup messages because Pi auth needs attention");
569
+ } else {
570
+ for (const chatId of config.telegram.authorizedChatIds || []) {
571
+ try {
572
+ logger?.log("telegram", `generating startup message for chat ${chatId}`);
573
+ const chatMeta = config.telegram.chatMeta[chatId] || {};
574
+ const welcomePrompt = [
575
+ "System event: Arisa has just started.",
576
+ `chatId: ${chatId}`,
577
+ `preferredTelegramLanguageCode: ${chatMeta.languageCode || "unknown"}`,
578
+ chatMeta.username ? `username: ${chatMeta.username}` : null,
579
+ chatMeta.firstName ? `firstName: ${chatMeta.firstName}` : null,
580
+ "Send a short welcome-back message for Telegram.",
581
+ "Keep it brief, warm, and natural.",
582
+ "Use the user's Telegram language when possible.",
583
+ "Do not mention internal implementation details."
584
+ ].filter(Boolean).join("\n");
585
+ await enqueuePrompt({ chatId, prompt: welcomePrompt, label: "startup message" });
586
+ } catch (error) {
587
+ logger?.log("telegram", `startup message failed for chat ${chatId}: ${error instanceof Error ? error.message : String(error)}`);
588
+ }
584
589
  }
585
590
  }
586
591
  await bot.api.setMyCommands([