Merge branch 'main' into fix/issue-3256-voice-call-intro-context

This commit is contained in:
hcl 2026-01-29 22:17:48 +08:00 committed by GitHub
commit 6486b3e5b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 311 additions and 55 deletions

View File

@ -73,6 +73,7 @@ Status: beta.
- **BREAKING:** Gateway auth mode "none" is removed; gateway now requires token/password (Tailscale Serve identity still allowed). - **BREAKING:** Gateway auth mode "none" is removed; gateway now requires token/password (Tailscale Serve identity still allowed).
### Fixes ### Fixes
- Telegram: avoid silent empty replies by tracking normalization skips before fallback. (#3796)
- Mentions: honor mentionPatterns even when explicit mentions are present. (#3303) Thanks @HirokiKobayashi-R. - Mentions: honor mentionPatterns even when explicit mentions are present. (#3303) Thanks @HirokiKobayashi-R.
- Discord: restore username directory lookup in target resolution. (#3131) Thanks @bonald. - Discord: restore username directory lookup in target resolution. (#3131) Thanks @bonald.
- Agents: align MiniMax base URL test expectation with default provider config. (#3131) Thanks @bonald. - Agents: align MiniMax base URL test expectation with default provider config. (#3131) Thanks @bonald.

View File

@ -1,3 +1,4 @@
import fs from "node:fs";
import path from "node:path"; import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { afterEach, beforeEach, describe, expect, it } from "vitest";
@ -8,6 +9,24 @@ import { buildDockerExecArgs } from "./bash-tools.shared.js";
import { sanitizeBinaryOutput } from "./shell-utils.js"; import { sanitizeBinaryOutput } from "./shell-utils.js";
const isWin = process.platform === "win32"; const isWin = process.platform === "win32";
const resolveShellFromPath = (name: string) => {
const envPath = process.env.PATH ?? "";
if (!envPath) return undefined;
const entries = envPath.split(path.delimiter).filter(Boolean);
for (const entry of entries) {
const candidate = path.join(entry, name);
try {
fs.accessSync(candidate, fs.constants.X_OK);
return candidate;
} catch {
// ignore missing or non-executable entries
}
}
return undefined;
};
const defaultShell = isWin
? undefined
: process.env.CLAWDBOT_TEST_SHELL || resolveShellFromPath("bash") || process.env.SHELL || "sh";
// PowerShell: Start-Sleep for delays, ; for command separation, $null for null device // PowerShell: Start-Sleep for delays, ; for command separation, $null for null device
const shortDelayCmd = isWin ? "Start-Sleep -Milliseconds 50" : "sleep 0.05"; const shortDelayCmd = isWin ? "Start-Sleep -Milliseconds 50" : "sleep 0.05";
const yieldDelayCmd = isWin ? "Start-Sleep -Milliseconds 200" : "sleep 0.2"; const yieldDelayCmd = isWin ? "Start-Sleep -Milliseconds 200" : "sleep 0.2";
@ -52,7 +71,7 @@ describe("exec tool backgrounding", () => {
const originalShell = process.env.SHELL; const originalShell = process.env.SHELL;
beforeEach(() => { beforeEach(() => {
if (!isWin) process.env.SHELL = "/bin/bash"; if (!isWin && defaultShell) process.env.SHELL = defaultShell;
}); });
afterEach(() => { afterEach(() => {
@ -282,7 +301,7 @@ describe("exec PATH handling", () => {
const originalShell = process.env.SHELL; const originalShell = process.env.SHELL;
beforeEach(() => { beforeEach(() => {
if (!isWin) process.env.SHELL = "/bin/bash"; if (!isWin && defaultShell) process.env.SHELL = defaultShell;
}); });
afterEach(() => { afterEach(() => {

View File

@ -35,8 +35,8 @@ function isAlive(pid: number): boolean {
function releaseAllLocksSync(): void { function releaseAllLocksSync(): void {
for (const [sessionFile, held] of HELD_LOCKS) { for (const [sessionFile, held] of HELD_LOCKS) {
try { try {
if (typeof held.handle.fd === "number") { if (typeof held.handle.close === "function") {
fsSync.closeSync(held.handle.fd); void held.handle.close().catch(() => {});
} }
} catch { } catch {
// Ignore errors during cleanup - best effort // Ignore errors during cleanup - best effort

View File

@ -1,10 +1,9 @@
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import * as ssrf from "../../infra/net/ssrf.js";
const lookupMock = vi.fn(); const lookupMock = vi.fn();
const resolvePinnedHostname = ssrf.resolvePinnedHostname;
vi.mock("node:dns/promises", () => ({
lookup: lookupMock,
}));
function makeHeaders(map: Record<string, string>): { get: (key: string) => string | null } { function makeHeaders(map: Record<string, string>): { get: (key: string) => string | null } {
return { return {
@ -33,6 +32,12 @@ function textResponse(body: string): Response {
describe("web_fetch SSRF protection", () => { describe("web_fetch SSRF protection", () => {
const priorFetch = global.fetch; const priorFetch = global.fetch;
beforeEach(() => {
vi.spyOn(ssrf, "resolvePinnedHostname").mockImplementation((hostname) =>
resolvePinnedHostname(hostname, lookupMock),
);
});
afterEach(() => { afterEach(() => {
// @ts-expect-error restore // @ts-expect-error restore
global.fetch = priorFetch; global.fetch = priorFetch;

View File

@ -1,5 +1,6 @@
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import * as ssrf from "../../infra/net/ssrf.js";
import { createWebFetchTool } from "./web-tools.js"; import { createWebFetchTool } from "./web-tools.js";
type MockResponse = { type MockResponse = {
@ -73,6 +74,18 @@ function requestUrl(input: RequestInfo): string {
describe("web_fetch extraction fallbacks", () => { describe("web_fetch extraction fallbacks", () => {
const priorFetch = global.fetch; const priorFetch = global.fetch;
beforeEach(() => {
vi.spyOn(ssrf, "resolvePinnedHostname").mockImplementation(async (hostname) => {
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
const addresses = ["93.184.216.34", "93.184.216.35"];
return {
hostname: normalized,
addresses,
lookup: ssrf.createPinnedLookup({ hostname: normalized, addresses }),
};
});
});
afterEach(() => { afterEach(() => {
// @ts-expect-error restore // @ts-expect-error restore
global.fetch = priorFetch; global.fetch = priorFetch;

View File

@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { SILENT_REPLY_TOKEN } from "../tokens.js";
import { normalizeReplyPayload } from "./normalize-reply.js"; import { normalizeReplyPayload } from "./normalize-reply.js";
// Keep channelData-only payloads so channel-specific replies survive normalization. // Keep channelData-only payloads so channel-specific replies survive normalization.
@ -19,4 +20,30 @@ describe("normalizeReplyPayload", () => {
expect(normalized?.text).toBeUndefined(); expect(normalized?.text).toBeUndefined();
expect(normalized?.channelData).toEqual(payload.channelData); expect(normalized?.channelData).toEqual(payload.channelData);
}); });
it("records silent skips", () => {
const reasons: string[] = [];
const normalized = normalizeReplyPayload(
{ text: SILENT_REPLY_TOKEN },
{
onSkip: (reason) => reasons.push(reason),
},
);
expect(normalized).toBeNull();
expect(reasons).toEqual(["silent"]);
});
it("records empty skips", () => {
const reasons: string[] = [];
const normalized = normalizeReplyPayload(
{ text: " " },
{
onSkip: (reason) => reasons.push(reason),
},
);
expect(normalized).toBeNull();
expect(reasons).toEqual(["empty"]);
});
}); });

View File

@ -8,6 +8,8 @@ import {
} from "./response-prefix-template.js"; } from "./response-prefix-template.js";
import { hasLineDirectives, parseLineDirectives } from "./line-directives.js"; import { hasLineDirectives, parseLineDirectives } from "./line-directives.js";
export type NormalizeReplySkipReason = "empty" | "silent" | "heartbeat";
export type NormalizeReplyOptions = { export type NormalizeReplyOptions = {
responsePrefix?: string; responsePrefix?: string;
/** Context for template variable interpolation in responsePrefix */ /** Context for template variable interpolation in responsePrefix */
@ -15,6 +17,7 @@ export type NormalizeReplyOptions = {
onHeartbeatStrip?: () => void; onHeartbeatStrip?: () => void;
stripHeartbeat?: boolean; stripHeartbeat?: boolean;
silentToken?: string; silentToken?: string;
onSkip?: (reason: NormalizeReplySkipReason) => void;
}; };
export function normalizeReplyPayload( export function normalizeReplyPayload(
@ -26,12 +29,18 @@ export function normalizeReplyPayload(
payload.channelData && Object.keys(payload.channelData).length > 0, payload.channelData && Object.keys(payload.channelData).length > 0,
); );
const trimmed = payload.text?.trim() ?? ""; const trimmed = payload.text?.trim() ?? "";
if (!trimmed && !hasMedia && !hasChannelData) return null; if (!trimmed && !hasMedia && !hasChannelData) {
opts.onSkip?.("empty");
return null;
}
const silentToken = opts.silentToken ?? SILENT_REPLY_TOKEN; const silentToken = opts.silentToken ?? SILENT_REPLY_TOKEN;
let text = payload.text ?? undefined; let text = payload.text ?? undefined;
if (text && isSilentReplyText(text, silentToken)) { if (text && isSilentReplyText(text, silentToken)) {
if (!hasMedia && !hasChannelData) return null; if (!hasMedia && !hasChannelData) {
opts.onSkip?.("silent");
return null;
}
text = ""; text = "";
} }
if (text && !trimmed) { if (text && !trimmed) {
@ -43,14 +52,20 @@ export function normalizeReplyPayload(
if (shouldStripHeartbeat && text?.includes(HEARTBEAT_TOKEN)) { if (shouldStripHeartbeat && text?.includes(HEARTBEAT_TOKEN)) {
const stripped = stripHeartbeatToken(text, { mode: "message" }); const stripped = stripHeartbeatToken(text, { mode: "message" });
if (stripped.didStrip) opts.onHeartbeatStrip?.(); if (stripped.didStrip) opts.onHeartbeatStrip?.();
if (stripped.shouldSkip && !hasMedia && !hasChannelData) return null; if (stripped.shouldSkip && !hasMedia && !hasChannelData) {
opts.onSkip?.("heartbeat");
return null;
}
text = stripped.text; text = stripped.text;
} }
if (text) { if (text) {
text = sanitizeUserFacingText(text); text = sanitizeUserFacingText(text);
} }
if (!text?.trim() && !hasMedia && !hasChannelData) return null; if (!text?.trim() && !hasMedia && !hasChannelData) {
opts.onSkip?.("empty");
return null;
}
// Parse LINE-specific directives from text (quick_replies, location, confirm, buttons) // Parse LINE-specific directives from text (quick_replies, location, confirm, buttons)
let enrichedPayload: ReplyPayload = { ...payload, text }; let enrichedPayload: ReplyPayload = { ...payload, text };

View File

@ -1,6 +1,6 @@
import type { HumanDelayConfig } from "../../config/types.js"; import type { HumanDelayConfig } from "../../config/types.js";
import type { GetReplyOptions, ReplyPayload } from "../types.js"; import type { GetReplyOptions, ReplyPayload } from "../types.js";
import { normalizeReplyPayload } from "./normalize-reply.js"; import { normalizeReplyPayload, type NormalizeReplySkipReason } from "./normalize-reply.js";
import type { ResponsePrefixContext } from "./response-prefix-template.js"; import type { ResponsePrefixContext } from "./response-prefix-template.js";
import type { TypingController } from "./typing.js"; import type { TypingController } from "./typing.js";
@ -8,6 +8,11 @@ export type ReplyDispatchKind = "tool" | "block" | "final";
type ReplyDispatchErrorHandler = (err: unknown, info: { kind: ReplyDispatchKind }) => void; type ReplyDispatchErrorHandler = (err: unknown, info: { kind: ReplyDispatchKind }) => void;
type ReplyDispatchSkipHandler = (
payload: ReplyPayload,
info: { kind: ReplyDispatchKind; reason: NormalizeReplySkipReason },
) => void;
type ReplyDispatchDeliverer = ( type ReplyDispatchDeliverer = (
payload: ReplyPayload, payload: ReplyPayload,
info: { kind: ReplyDispatchKind }, info: { kind: ReplyDispatchKind },
@ -42,6 +47,8 @@ export type ReplyDispatcherOptions = {
onHeartbeatStrip?: () => void; onHeartbeatStrip?: () => void;
onIdle?: () => void; onIdle?: () => void;
onError?: ReplyDispatchErrorHandler; onError?: ReplyDispatchErrorHandler;
// AIDEV-NOTE: onSkip lets channels detect silent/empty drops (e.g. Telegram empty-response fallback).
onSkip?: ReplyDispatchSkipHandler;
/** Human-like delay between block replies for natural rhythm. */ /** Human-like delay between block replies for natural rhythm. */
humanDelay?: HumanDelayConfig; humanDelay?: HumanDelayConfig;
}; };
@ -65,15 +72,16 @@ export type ReplyDispatcher = {
getQueuedCounts: () => Record<ReplyDispatchKind, number>; getQueuedCounts: () => Record<ReplyDispatchKind, number>;
}; };
type NormalizeReplyPayloadInternalOptions = Pick<
ReplyDispatcherOptions,
"responsePrefix" | "responsePrefixContext" | "responsePrefixContextProvider" | "onHeartbeatStrip"
> & {
onSkip?: (reason: NormalizeReplySkipReason) => void;
};
function normalizeReplyPayloadInternal( function normalizeReplyPayloadInternal(
payload: ReplyPayload, payload: ReplyPayload,
opts: Pick< opts: NormalizeReplyPayloadInternalOptions,
ReplyDispatcherOptions,
| "responsePrefix"
| "responsePrefixContext"
| "responsePrefixContextProvider"
| "onHeartbeatStrip"
>,
): ReplyPayload | null { ): ReplyPayload | null {
// Prefer dynamic context provider over static context // Prefer dynamic context provider over static context
const prefixContext = opts.responsePrefixContextProvider?.() ?? opts.responsePrefixContext; const prefixContext = opts.responsePrefixContextProvider?.() ?? opts.responsePrefixContext;
@ -82,6 +90,7 @@ function normalizeReplyPayloadInternal(
responsePrefix: opts.responsePrefix, responsePrefix: opts.responsePrefix,
responsePrefixContext: prefixContext, responsePrefixContext: prefixContext,
onHeartbeatStrip: opts.onHeartbeatStrip, onHeartbeatStrip: opts.onHeartbeatStrip,
onSkip: opts.onSkip,
}); });
} }
@ -99,7 +108,13 @@ export function createReplyDispatcher(options: ReplyDispatcherOptions): ReplyDis
}; };
const enqueue = (kind: ReplyDispatchKind, payload: ReplyPayload) => { const enqueue = (kind: ReplyDispatchKind, payload: ReplyPayload) => {
const normalized = normalizeReplyPayloadInternal(payload, options); const normalized = normalizeReplyPayloadInternal(payload, {
responsePrefix: options.responsePrefix,
responsePrefixContext: options.responsePrefixContext,
responsePrefixContextProvider: options.responsePrefixContextProvider,
onHeartbeatStrip: options.onHeartbeatStrip,
onSkip: (reason) => options.onSkip?.(payload, { kind, reason }),
});
if (!normalized) return false; if (!normalized) return false;
queuedCounts[kind] += 1; queuedCounts[kind] += 1;
pending += 1; pending += 1;

View File

@ -202,6 +202,16 @@ describe("canvas host", () => {
it("serves the gateway-hosted A2UI scaffold", async () => { it("serves the gateway-hosted A2UI scaffold", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "moltbot-canvas-")); const dir = await fs.mkdtemp(path.join(os.tmpdir(), "moltbot-canvas-"));
const a2uiRoot = path.resolve(process.cwd(), "src/canvas-host/a2ui");
const bundlePath = path.join(a2uiRoot, "a2ui.bundle.js");
let createdBundle = false;
try {
await fs.stat(bundlePath);
} catch {
await fs.writeFile(bundlePath, "window.moltbotA2UI = {};", "utf8");
createdBundle = true;
}
const server = await startCanvasHost({ const server = await startCanvasHost({
runtime: defaultRuntime, runtime: defaultRuntime,
@ -226,6 +236,9 @@ describe("canvas host", () => {
expect(js).toContain("moltbotA2UI"); expect(js).toContain("moltbotA2UI");
} finally { } finally {
await server.close(); await server.close();
if (createdBundle) {
await fs.rm(bundlePath, { force: true });
}
await fs.rm(dir, { recursive: true, force: true }); await fs.rm(dir, { recursive: true, force: true });
} }
}); });

View File

@ -550,10 +550,11 @@ describe("applyMediaUnderstanding", () => {
it("escapes XML special characters in filenames to prevent injection", async () => { it("escapes XML special characters in filenames to prevent injection", async () => {
const { applyMediaUnderstanding } = await loadApply(); const { applyMediaUnderstanding } = await loadApply();
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "moltbot-media-")); const dir = await fs.mkdtemp(path.join(os.tmpdir(), "moltbot-media-"));
// Create file with XML special characters in the name (what filesystem allows) // Use & in filename — valid on all platforms (including Windows, which
// forbids < and > in NTFS filenames) and still requires XML escaping.
// Note: The sanitizeFilename in store.ts would strip most dangerous chars, // Note: The sanitizeFilename in store.ts would strip most dangerous chars,
// but we test that even if some slip through, they get escaped in output // but we test that even if some slip through, they get escaped in output
const filePath = path.join(dir, "file<test>.txt"); const filePath = path.join(dir, "file&test.txt");
await fs.writeFile(filePath, "safe content"); await fs.writeFile(filePath, "safe content");
const ctx: MsgContext = { const ctx: MsgContext = {
@ -575,10 +576,9 @@ describe("applyMediaUnderstanding", () => {
expect(result.appliedFile).toBe(true); expect(result.appliedFile).toBe(true);
// Verify XML special chars are escaped in the output // Verify XML special chars are escaped in the output
expect(ctx.Body).toContain("&lt;"); expect(ctx.Body).toContain("&amp;");
expect(ctx.Body).toContain("&gt;"); // The name attribute should contain the escaped form, not a raw unescaped &
// The raw < and > should not appear unescaped in the name attribute expect(ctx.Body).toMatch(/name="file&amp;test\.txt"/);
expect(ctx.Body).not.toMatch(/name="[^"]*<[^"]*"/);
}); });
it("normalizes MIME types to prevent attribute injection", async () => { it("normalizes MIME types to prevent attribute injection", async () => {

View File

@ -21,6 +21,8 @@ import { createTelegramDraftStream } from "./draft-stream.js";
import { cacheSticker, describeStickerImage } from "./sticker-cache.js"; import { cacheSticker, describeStickerImage } from "./sticker-cache.js";
import { resolveAgentDir } from "../agents/agent-scope.js"; import { resolveAgentDir } from "../agents/agent-scope.js";
const EMPTY_RESPONSE_FALLBACK = "No response generated. Please try again.";
async function resolveStickerVisionSupport(cfg, agentId) { async function resolveStickerVisionSupport(cfg, agentId) {
try { try {
const catalog = await loadModelCatalog({ config: cfg }); const catalog = await loadModelCatalog({ config: cfg });
@ -198,6 +200,15 @@ export const dispatchTelegramMessage = async ({
} }
} }
const replyQuoteText =
ctxPayload.ReplyToIsQuote && ctxPayload.ReplyToBody
? ctxPayload.ReplyToBody.trim() || undefined
: undefined;
const deliveryState = {
delivered: false,
skippedNonSilent: 0,
};
const { queuedFinal } = await dispatchReplyWithBufferedBlockDispatcher({ const { queuedFinal } = await dispatchReplyWithBufferedBlockDispatcher({
ctx: ctxPayload, ctx: ctxPayload,
cfg, cfg,
@ -209,12 +220,7 @@ export const dispatchTelegramMessage = async ({
await flushDraft(); await flushDraft();
draftStream?.stop(); draftStream?.stop();
} }
const result = await deliverReplies({
const replyQuoteText =
ctxPayload.ReplyToIsQuote && ctxPayload.ReplyToBody
? ctxPayload.ReplyToBody.trim() || undefined
: undefined;
await deliverReplies({
replies: [payload], replies: [payload],
chatId: String(chatId), chatId: String(chatId),
token: opts.token, token: opts.token,
@ -229,6 +235,12 @@ export const dispatchTelegramMessage = async ({
linkPreview: telegramCfg.linkPreview, linkPreview: telegramCfg.linkPreview,
replyQuoteText, replyQuoteText,
}); });
if (result.delivered) {
deliveryState.delivered = true;
}
},
onSkip: (_payload, info) => {
if (info.reason !== "silent") deliveryState.skippedNonSilent += 1;
}, },
onError: (err, info) => { onError: (err, info) => {
runtime.error?.(danger(`telegram ${info.kind} reply failed: ${String(err)}`)); runtime.error?.(danger(`telegram ${info.kind} reply failed: ${String(err)}`));
@ -260,7 +272,27 @@ export const dispatchTelegramMessage = async ({
}, },
}); });
draftStream?.stop(); draftStream?.stop();
if (!queuedFinal) { let sentFallback = false;
if (!deliveryState.delivered && deliveryState.skippedNonSilent > 0) {
const result = await deliverReplies({
replies: [{ text: EMPTY_RESPONSE_FALLBACK }],
chatId: String(chatId),
token: opts.token,
runtime,
bot,
replyToMode,
textLimit,
messageThreadId: resolvedThreadId,
tableMode,
chunkMode,
linkPreview: telegramCfg.linkPreview,
replyQuoteText,
});
sentFallback = result.delivered;
}
const hasFinalResponse = queuedFinal || sentFallback;
if (!hasFinalResponse) {
if (isGroup && historyKey) { if (isGroup && historyKey) {
clearHistoryEntriesIfEnabled({ historyMap: groupHistories, historyKey, limit: historyLimit }); clearHistoryEntriesIfEnabled({ historyMap: groupHistories, historyKey, limit: historyLimit });
} }

View File

@ -50,6 +50,8 @@ import {
import { firstDefined, isSenderAllowed, normalizeAllowFromWithStore } from "./bot-access.js"; import { firstDefined, isSenderAllowed, normalizeAllowFromWithStore } from "./bot-access.js";
import { readTelegramAllowFromStore } from "./pairing-store.js"; import { readTelegramAllowFromStore } from "./pairing-store.js";
const EMPTY_RESPONSE_FALLBACK = "No response generated. Please try again.";
type TelegramNativeCommandContext = Context & { match?: string }; type TelegramNativeCommandContext = Context & { match?: string };
type TelegramCommandAuthResult = { type TelegramCommandAuthResult = {
@ -483,13 +485,18 @@ export const registerTelegramNativeCommands = ({
: undefined; : undefined;
const chunkMode = resolveChunkMode(cfg, "telegram", route.accountId); const chunkMode = resolveChunkMode(cfg, "telegram", route.accountId);
const deliveryState = {
delivered: false,
skippedNonSilent: 0,
};
await dispatchReplyWithBufferedBlockDispatcher({ await dispatchReplyWithBufferedBlockDispatcher({
ctx: ctxPayload, ctx: ctxPayload,
cfg, cfg,
dispatcherOptions: { dispatcherOptions: {
responsePrefix: resolveEffectiveMessagesConfig(cfg, route.agentId).responsePrefix, responsePrefix: resolveEffectiveMessagesConfig(cfg, route.agentId).responsePrefix,
deliver: async (payload) => { deliver: async (payload, _info) => {
await deliverReplies({ const result = await deliverReplies({
replies: [payload], replies: [payload],
chatId: String(chatId), chatId: String(chatId),
token: opts.token, token: opts.token,
@ -502,6 +509,12 @@ export const registerTelegramNativeCommands = ({
chunkMode, chunkMode,
linkPreview: telegramCfg.linkPreview, linkPreview: telegramCfg.linkPreview,
}); });
if (result.delivered) {
deliveryState.delivered = true;
}
},
onSkip: (_payload, info) => {
if (info.reason !== "silent") deliveryState.skippedNonSilent += 1;
}, },
onError: (err, info) => { onError: (err, info) => {
runtime.error?.(danger(`telegram slash ${info.kind} reply failed: ${String(err)}`)); runtime.error?.(danger(`telegram slash ${info.kind} reply failed: ${String(err)}`));
@ -512,6 +525,21 @@ export const registerTelegramNativeCommands = ({
disableBlockStreaming, disableBlockStreaming,
}, },
}); });
if (!deliveryState.delivered && deliveryState.skippedNonSilent > 0) {
await deliverReplies({
replies: [{ text: EMPTY_RESPONSE_FALLBACK }],
chatId: String(chatId),
token: opts.token,
runtime,
bot,
replyToMode,
textLimit,
messageThreadId: threadIdForSend,
tableMode,
chunkMode,
linkPreview: telegramCfg.linkPreview,
});
}
}); });
} }

View File

@ -44,7 +44,7 @@ export async function deliverReplies(params: {
linkPreview?: boolean; linkPreview?: boolean;
/** Optional quote text for Telegram reply_parameters. */ /** Optional quote text for Telegram reply_parameters. */
replyQuoteText?: string; replyQuoteText?: string;
}) { }): Promise<{ delivered: boolean }> {
const { const {
replies, replies,
chatId, chatId,
@ -58,6 +58,10 @@ export async function deliverReplies(params: {
} = params; } = params;
const chunkMode = params.chunkMode ?? "length"; const chunkMode = params.chunkMode ?? "length";
let hasReplied = false; let hasReplied = false;
let hasDelivered = false;
const markDelivered = () => {
hasDelivered = true;
};
const chunkText = (markdown: string) => { const chunkText = (markdown: string) => {
const markdownChunks = const markdownChunks =
chunkMode === "newline" chunkMode === "newline"
@ -114,6 +118,7 @@ export async function deliverReplies(params: {
linkPreview, linkPreview,
replyMarkup: shouldAttachButtons ? replyMarkup : undefined, replyMarkup: shouldAttachButtons ? replyMarkup : undefined,
}); });
markDelivered();
if (replyToId && !hasReplied) { if (replyToId && !hasReplied) {
hasReplied = true; hasReplied = true;
} }
@ -165,18 +170,21 @@ export async function deliverReplies(params: {
runtime, runtime,
fn: () => bot.api.sendAnimation(chatId, file, { ...mediaParams }), fn: () => bot.api.sendAnimation(chatId, file, { ...mediaParams }),
}); });
markDelivered();
} else if (kind === "image") { } else if (kind === "image") {
await withTelegramApiErrorLogging({ await withTelegramApiErrorLogging({
operation: "sendPhoto", operation: "sendPhoto",
runtime, runtime,
fn: () => bot.api.sendPhoto(chatId, file, { ...mediaParams }), fn: () => bot.api.sendPhoto(chatId, file, { ...mediaParams }),
}); });
markDelivered();
} else if (kind === "video") { } else if (kind === "video") {
await withTelegramApiErrorLogging({ await withTelegramApiErrorLogging({
operation: "sendVideo", operation: "sendVideo",
runtime, runtime,
fn: () => bot.api.sendVideo(chatId, file, { ...mediaParams }), fn: () => bot.api.sendVideo(chatId, file, { ...mediaParams }),
}); });
markDelivered();
} else if (kind === "audio") { } else if (kind === "audio") {
const { useVoice } = resolveTelegramVoiceSend({ const { useVoice } = resolveTelegramVoiceSend({
wantsVoice: reply.audioAsVoice === true, // default false (backward compatible) wantsVoice: reply.audioAsVoice === true, // default false (backward compatible)
@ -195,6 +203,7 @@ export async function deliverReplies(params: {
shouldLog: (err) => !isVoiceMessagesForbidden(err), shouldLog: (err) => !isVoiceMessagesForbidden(err),
fn: () => bot.api.sendVoice(chatId, file, { ...mediaParams }), fn: () => bot.api.sendVoice(chatId, file, { ...mediaParams }),
}); });
markDelivered();
} catch (voiceErr) { } catch (voiceErr) {
// Fall back to text if voice messages are forbidden in this chat. // Fall back to text if voice messages are forbidden in this chat.
// This happens when the recipient has Telegram Premium privacy settings // This happens when the recipient has Telegram Premium privacy settings
@ -221,6 +230,7 @@ export async function deliverReplies(params: {
replyMarkup, replyMarkup,
replyQuoteText, replyQuoteText,
}); });
markDelivered();
// Skip this media item; continue with next. // Skip this media item; continue with next.
continue; continue;
} }
@ -233,6 +243,7 @@ export async function deliverReplies(params: {
runtime, runtime,
fn: () => bot.api.sendAudio(chatId, file, { ...mediaParams }), fn: () => bot.api.sendAudio(chatId, file, { ...mediaParams }),
}); });
markDelivered();
} }
} else { } else {
await withTelegramApiErrorLogging({ await withTelegramApiErrorLogging({
@ -240,6 +251,7 @@ export async function deliverReplies(params: {
runtime, runtime,
fn: () => bot.api.sendDocument(chatId, file, { ...mediaParams }), fn: () => bot.api.sendDocument(chatId, file, { ...mediaParams }),
}); });
markDelivered();
} }
if (replyToId && !hasReplied) { if (replyToId && !hasReplied) {
hasReplied = true; hasReplied = true;
@ -260,6 +272,7 @@ export async function deliverReplies(params: {
linkPreview, linkPreview,
replyMarkup: i === 0 ? replyMarkup : undefined, replyMarkup: i === 0 ? replyMarkup : undefined,
}); });
markDelivered();
if (replyToId && !hasReplied) { if (replyToId && !hasReplied) {
hasReplied = true; hasReplied = true;
} }
@ -268,6 +281,8 @@ export async function deliverReplies(params: {
} }
} }
} }
return { delivered: hasDelivered };
} }
export async function resolveMedia( export async function resolveMedia(

View File

@ -21,6 +21,7 @@ type ChatHost = {
basePath: string; basePath: string;
hello: GatewayHelloOk | null; hello: GatewayHelloOk | null;
chatAvatarUrl: string | null; chatAvatarUrl: string | null;
refreshSessionsAfterChat: boolean;
}; };
export function isChatBusy(host: ChatHost) { export function isChatBusy(host: ChatHost) {
@ -41,6 +42,14 @@ export function isChatStopCommand(text: string) {
); );
} }
function isChatResetCommand(text: string) {
const trimmed = text.trim();
if (!trimmed) return false;
const normalized = trimmed.toLowerCase();
if (normalized === "/new" || normalized === "/reset") return true;
return normalized.startsWith("/new ") || normalized.startsWith("/reset ");
}
export async function handleAbortChat(host: ChatHost) { export async function handleAbortChat(host: ChatHost) {
if (!host.connected) return; if (!host.connected) return;
host.chatMessage = ""; host.chatMessage = "";
@ -71,6 +80,7 @@ async function sendChatMessageNow(
attachments?: ChatAttachment[]; attachments?: ChatAttachment[];
previousAttachments?: ChatAttachment[]; previousAttachments?: ChatAttachment[];
restoreAttachments?: boolean; restoreAttachments?: boolean;
refreshSessions?: boolean;
}, },
) { ) {
resetToolStream(host as unknown as Parameters<typeof resetToolStream>[0]); resetToolStream(host as unknown as Parameters<typeof resetToolStream>[0]);
@ -94,6 +104,9 @@ async function sendChatMessageNow(
if (ok && !host.chatRunId) { if (ok && !host.chatRunId) {
void flushChatQueue(host); void flushChatQueue(host);
} }
if (ok && opts?.refreshSessions) {
host.refreshSessionsAfterChat = true;
}
return ok; return ok;
} }
@ -132,6 +145,7 @@ export async function handleSendChat(
return; return;
} }
const refreshSessions = isChatResetCommand(message);
if (messageOverride == null) { if (messageOverride == null) {
host.chatMessage = ""; host.chatMessage = "";
// Clear attachments when sending // Clear attachments when sending
@ -149,13 +163,14 @@ export async function handleSendChat(
attachments: hasAttachments ? attachmentsToSend : undefined, attachments: hasAttachments ? attachmentsToSend : undefined,
previousAttachments: messageOverride == null ? attachments : undefined, previousAttachments: messageOverride == null ? attachments : undefined,
restoreAttachments: Boolean(messageOverride && opts?.restoreDraft), restoreAttachments: Boolean(messageOverride && opts?.restoreDraft),
refreshSessions,
}); });
} }
export async function refreshChat(host: ChatHost) { export async function refreshChat(host: ChatHost) {
await Promise.all([ await Promise.all([
loadChatHistory(host as unknown as MoltbotApp), loadChatHistory(host as unknown as MoltbotApp),
loadSessions(host as unknown as MoltbotApp), loadSessions(host as unknown as MoltbotApp, { activeMinutes: 0 }),
refreshChatAvatar(host), refreshChatAvatar(host),
]); ]);
scheduleChatScroll(host as unknown as Parameters<typeof scheduleChatScroll>[0], true); scheduleChatScroll(host as unknown as Parameters<typeof scheduleChatScroll>[0], true);

View File

@ -26,6 +26,7 @@ import {
import type { MoltbotApp } from "./app"; import type { MoltbotApp } from "./app";
import type { ExecApprovalRequest } from "./controllers/exec-approval"; import type { ExecApprovalRequest } from "./controllers/exec-approval";
import { loadAssistantIdentity } from "./controllers/assistant-identity"; import { loadAssistantIdentity } from "./controllers/assistant-identity";
import { loadSessions } from "./controllers/sessions";
type GatewayHost = { type GatewayHost = {
settings: UiSettings; settings: UiSettings;
@ -50,6 +51,7 @@ type GatewayHost = {
assistantAgentId: string | null; assistantAgentId: string | null;
sessionKey: string; sessionKey: string;
chatRunId: string | null; chatRunId: string | null;
refreshSessionsAfterChat: boolean;
execApprovalQueue: ExecApprovalRequest[]; execApprovalQueue: ExecApprovalRequest[];
execApprovalError: string | null; execApprovalError: string | null;
}; };
@ -194,6 +196,12 @@ function handleGatewayEventUnsafe(host: GatewayHost, evt: GatewayEventFrame) {
void flushChatQueueForEvent( void flushChatQueueForEvent(
host as unknown as Parameters<typeof flushChatQueueForEvent>[0], host as unknown as Parameters<typeof flushChatQueueForEvent>[0],
); );
if (host.refreshSessionsAfterChat) {
host.refreshSessionsAfterChat = false;
if (state === "final") {
void loadSessions(host as unknown as MoltbotApp, { activeMinutes: 0 });
}
}
} }
if (state === "final") void loadChatHistory(host as unknown as MoltbotApp); if (state === "final") void loadChatHistory(host as unknown as MoltbotApp);
return; return;

View File

@ -35,6 +35,9 @@ type LifecycleHost = {
export function handleConnected(host: LifecycleHost) { export function handleConnected(host: LifecycleHost) {
host.basePath = inferBasePath(); host.basePath = inferBasePath();
applySettingsFromUrl(
host as unknown as Parameters<typeof applySettingsFromUrl>[0],
);
syncTabWithLocation( syncTabWithLocation(
host as unknown as Parameters<typeof syncTabWithLocation>[0], host as unknown as Parameters<typeof syncTabWithLocation>[0],
true, true,
@ -46,9 +49,6 @@ export function handleConnected(host: LifecycleHost) {
host as unknown as Parameters<typeof attachThemeListener>[0], host as unknown as Parameters<typeof attachThemeListener>[0],
); );
window.addEventListener("popstate", host.popStateHandler); window.addEventListener("popstate", host.popStateHandler);
applySettingsFromUrl(
host as unknown as Parameters<typeof applySettingsFromUrl>[0],
);
connectGateway(host as unknown as Parameters<typeof connectGateway>[0]); connectGateway(host as unknown as Parameters<typeof connectGateway>[0]);
startNodesPolling(host as unknown as Parameters<typeof startNodesPolling>[0]); startNodesPolling(host as unknown as Parameters<typeof startNodesPolling>[0]);
if (host.tab === "logs") { if (host.tab === "logs") {

View File

@ -5,6 +5,7 @@ import type { AppViewState } from "./app-view-state";
import { iconForTab, pathForTab, titleForTab, type Tab } from "./navigation"; import { iconForTab, pathForTab, titleForTab, type Tab } from "./navigation";
import { icons } from "./icons"; import { icons } from "./icons";
import { loadChatHistory } from "./controllers/chat"; import { loadChatHistory } from "./controllers/chat";
import { refreshChat } from "./app-chat";
import { syncUrlWithSessionKey } from "./app-settings"; import { syncUrlWithSessionKey } from "./app-settings";
import type { SessionsListResult } from "./types"; import type { SessionsListResult } from "./types";
import type { ThemeMode } from "./theme"; import type { ThemeMode } from "./theme";
@ -39,7 +40,12 @@ export function renderTab(state: AppViewState, tab: Tab) {
} }
export function renderChatControls(state: AppViewState) { export function renderChatControls(state: AppViewState) {
const sessionOptions = resolveSessionOptions(state.sessionKey, state.sessionsResult); const mainSessionKey = resolveMainSessionKey(state.hello, state.sessionsResult);
const sessionOptions = resolveSessionOptions(
state.sessionKey,
state.sessionsResult,
mainSessionKey,
);
const disableThinkingToggle = state.onboarding; const disableThinkingToggle = state.onboarding;
const disableFocusToggle = state.onboarding; const disableFocusToggle = state.onboarding;
const showThinking = state.onboarding ? false : state.settings.chatShowThinking; const showThinking = state.onboarding ? false : state.settings.chatShowThinking;
@ -87,9 +93,9 @@ export function renderChatControls(state: AppViewState) {
?disabled=${state.chatLoading || !state.connected} ?disabled=${state.chatLoading || !state.connected}
@click=${() => { @click=${() => {
state.resetToolStream(); state.resetToolStream();
void loadChatHistory(state); void refreshChat(state as unknown as Parameters<typeof refreshChat>[0]);
}} }}
title="Refresh chat history" title="Refresh chat data"
> >
${refreshIcon} ${refreshIcon}
</button> </button>
@ -132,15 +138,47 @@ export function renderChatControls(state: AppViewState) {
`; `;
} }
function resolveSessionOptions(sessionKey: string, sessions: SessionsListResult | null) { type SessionDefaultsSnapshot = {
mainSessionKey?: string;
mainKey?: string;
};
function resolveMainSessionKey(
hello: AppViewState["hello"],
sessions: SessionsListResult | null,
): string | null {
const snapshot = hello?.snapshot as { sessionDefaults?: SessionDefaultsSnapshot } | undefined;
const mainSessionKey = snapshot?.sessionDefaults?.mainSessionKey?.trim();
if (mainSessionKey) return mainSessionKey;
const mainKey = snapshot?.sessionDefaults?.mainKey?.trim();
if (mainKey) return mainKey;
if (sessions?.sessions?.some((row) => row.key === "main")) return "main";
return null;
}
function resolveSessionOptions(
sessionKey: string,
sessions: SessionsListResult | null,
mainSessionKey?: string | null,
) {
const seen = new Set<string>(); const seen = new Set<string>();
const options: Array<{ key: string; displayName?: string }> = []; const options: Array<{ key: string; displayName?: string }> = [];
const resolvedMain =
mainSessionKey && sessions?.sessions?.find((s) => s.key === mainSessionKey);
const resolvedCurrent = sessions?.sessions?.find((s) => s.key === sessionKey); const resolvedCurrent = sessions?.sessions?.find((s) => s.key === sessionKey);
// Add current session key first // Add main session key first
if (mainSessionKey) {
seen.add(mainSessionKey);
options.push({ key: mainSessionKey, displayName: resolvedMain?.displayName });
}
// Add current session key next
if (!seen.has(sessionKey)) {
seen.add(sessionKey); seen.add(sessionKey);
options.push({ key: sessionKey, displayName: resolvedCurrent?.displayName }); options.push({ key: sessionKey, displayName: resolvedCurrent?.displayName });
}
// Add sessions from the result // Add sessions from the result
if (sessions?.sessions) { if (sessions?.sessions) {

View File

@ -258,6 +258,7 @@ export class MoltbotApp extends LitElement {
private logsScrollFrame: number | null = null; private logsScrollFrame: number | null = null;
private toolStreamById = new Map<string, ToolStreamEntry>(); private toolStreamById = new Map<string, ToolStreamEntry>();
private toolStreamOrder: string[] = []; private toolStreamOrder: string[] = [];
refreshSessionsAfterChat = false;
basePath = ""; basePath = "";
private popStateHandler = () => private popStateHandler = () =>
onPopStateInternal( onPopStateInternal(

View File

@ -14,18 +14,29 @@ export type SessionsState = {
sessionsIncludeUnknown: boolean; sessionsIncludeUnknown: boolean;
}; };
export async function loadSessions(state: SessionsState) { export async function loadSessions(
state: SessionsState,
overrides?: {
activeMinutes?: number;
limit?: number;
includeGlobal?: boolean;
includeUnknown?: boolean;
},
) {
if (!state.client || !state.connected) return; if (!state.client || !state.connected) return;
if (state.sessionsLoading) return; if (state.sessionsLoading) return;
state.sessionsLoading = true; state.sessionsLoading = true;
state.sessionsError = null; state.sessionsError = null;
try { try {
const includeGlobal = overrides?.includeGlobal ?? state.sessionsIncludeGlobal;
const includeUnknown = overrides?.includeUnknown ?? state.sessionsIncludeUnknown;
const activeMinutes =
overrides?.activeMinutes ?? toNumber(state.sessionsFilterActive, 0);
const limit = overrides?.limit ?? toNumber(state.sessionsFilterLimit, 0);
const params: Record<string, unknown> = { const params: Record<string, unknown> = {
includeGlobal: state.sessionsIncludeGlobal, includeGlobal,
includeUnknown: state.sessionsIncludeUnknown, includeUnknown,
}; };
const activeMinutes = toNumber(state.sessionsFilterActive, 0);
const limit = toNumber(state.sessionsFilterLimit, 0);
if (activeMinutes > 0) params.activeMinutes = activeMinutes; if (activeMinutes > 0) params.activeMinutes = activeMinutes;
if (limit > 0) params.limit = limit; if (limit > 0) params.limit = limit;
const res = (await state.client.request("sessions.list", params)) as const res = (await state.client.request("sessions.list", params)) as