Merge branch 'main' into fix/heartbeat-telegram-account-id
This commit is contained in:
@@ -2,11 +2,18 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
- Heartbeat: resolve Telegram account IDs from config-only tokens; cron tool accepts canonical `jobId` and legacy `id` for job actions. (#516) — thanks @YuriNachos
|
||||||
|
- Discord: stop provider when gateway reconnects are exhausted and surface errors. (#514) — thanks @joshp123
|
||||||
|
- Auto-reply: preserve block reply ordering with timeout fallback for streaming. (#503) — thanks @joshp123
|
||||||
|
- Auto-reply: avoid splitting outbound chunks inside parentheses. (#499) — thanks @philipp-spiess
|
||||||
|
- Status: show provider prefix in /status model display. (#506) — thanks @mcinteerj
|
||||||
|
- macOS: package ClawdbotKit resources and Swift 6.2 compatibility dylib to avoid launch/tool crashes. (#473) — thanks @gupsammy
|
||||||
- WhatsApp: group `/model list` output by provider for scannability. (#456) - thanks @mcinteerj
|
- WhatsApp: group `/model list` output by provider for scannability. (#456) - thanks @mcinteerj
|
||||||
- Hooks: allow per-hook model overrides for webhook/Gmail runs (e.g. GPT 5 Mini).
|
- Hooks: allow per-hook model overrides for webhook/Gmail runs (e.g. GPT 5 Mini).
|
||||||
- Control UI: logs tab opens at the newest entries (bottom).
|
- Control UI: logs tab opens at the newest entries (bottom).
|
||||||
- Control UI: add Docs link, remove chat composer divider, and add New session button.
|
- Control UI: add Docs link, remove chat composer divider, and add New session button.
|
||||||
- Telegram: retry long-polling conflicts with backoff to avoid fatal exits.
|
- Telegram: retry long-polling conflicts with backoff to avoid fatal exits.
|
||||||
|
- Telegram: fix grammY fetch type mismatch when injecting `fetch`. (#512) — thanks @YuriNachos
|
||||||
- Agent system prompt: avoid automatic self-updates unless explicitly requested.
|
- Agent system prompt: avoid automatic self-updates unless explicitly requested.
|
||||||
- Onboarding: tighten QuickStart hint copy for configuring later.
|
- Onboarding: tighten QuickStart hint copy for configuring later.
|
||||||
- Onboarding: avoid “token expired” for Codex CLI when expiry is heuristic.
|
- Onboarding: avoid “token expired” for Codex CLI when expiry is heuristic.
|
||||||
|
|||||||
@@ -1,5 +1,75 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public enum ClawdbotKitResources {
|
public enum ClawdbotKitResources {
|
||||||
public static let bundle: Bundle = .module
|
/// Resource bundle for ClawdbotKit.
|
||||||
|
///
|
||||||
|
/// Locates the SwiftPM-generated resource bundle, checking multiple locations:
|
||||||
|
/// 1. Inside Bundle.main (packaged apps)
|
||||||
|
/// 2. Bundle.module (SwiftPM development/tests)
|
||||||
|
/// 3. Falls back to Bundle.main if not found (resource lookups will return nil)
|
||||||
|
///
|
||||||
|
/// This avoids a fatal crash when Bundle.module can't locate its resources
|
||||||
|
/// in packaged .app bundles where the resource bundle path differs from
|
||||||
|
/// SwiftPM's expectations.
|
||||||
|
public static let bundle: Bundle = locateBundle()
|
||||||
|
|
||||||
|
private static let bundleName = "ClawdbotKit_ClawdbotKit"
|
||||||
|
|
||||||
|
private static func locateBundle() -> Bundle {
|
||||||
|
// 1. Check inside Bundle.main (packaged apps copy resources here)
|
||||||
|
if let mainResourceURL = Bundle.main.resourceURL {
|
||||||
|
let bundleURL = mainResourceURL.appendingPathComponent("\(bundleName).bundle")
|
||||||
|
if let bundle = Bundle(url: bundleURL) {
|
||||||
|
return bundle
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Check Bundle.main directly for embedded resources
|
||||||
|
if Bundle.main.url(forResource: "tool-display", withExtension: "json") != nil {
|
||||||
|
return Bundle.main
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Try Bundle.module (works in SwiftPM development/tests)
|
||||||
|
// Wrap in a function to defer the fatalError until actually called
|
||||||
|
if let moduleBundle = loadModuleBundleSafely() {
|
||||||
|
return moduleBundle
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Fallback: return Bundle.main (resource lookups will return nil gracefully)
|
||||||
|
return Bundle.main
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func loadModuleBundleSafely() -> Bundle? {
|
||||||
|
// Bundle.module is generated by SwiftPM and will fatalError if not found.
|
||||||
|
// We check likely locations manually to avoid the crash.
|
||||||
|
let candidates: [URL?] = [
|
||||||
|
Bundle.main.resourceURL,
|
||||||
|
Bundle.main.bundleURL,
|
||||||
|
Bundle(for: BundleLocator.self).resourceURL,
|
||||||
|
Bundle(for: BundleLocator.self).bundleURL,
|
||||||
|
]
|
||||||
|
|
||||||
|
for candidate in candidates {
|
||||||
|
guard let baseURL = candidate else { continue }
|
||||||
|
|
||||||
|
// Direct path
|
||||||
|
let directURL = baseURL.appendingPathComponent("\(bundleName).bundle")
|
||||||
|
if let bundle = Bundle(url: directURL) {
|
||||||
|
return bundle
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inside Resources/
|
||||||
|
let resourcesURL = baseURL
|
||||||
|
.appendingPathComponent("Resources")
|
||||||
|
.appendingPathComponent("\(bundleName).bundle")
|
||||||
|
if let bundle = Bundle(url: resourcesURL) {
|
||||||
|
return bundle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper class for bundle lookup via Bundle(for:)
|
||||||
|
private final class BundleLocator {}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import ClawdbotKit
|
||||||
|
import Foundation
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
@Suite struct ToolDisplayRegistryTests {
|
||||||
|
@Test func loadsToolDisplayConfigFromBundle() {
|
||||||
|
let url = ClawdbotKitResources.bundle.url(forResource: "tool-display", withExtension: "json")
|
||||||
|
#expect(url != nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func resolvesKnownToolFromConfig() {
|
||||||
|
let summary = ToolDisplayRegistry.resolve(name: "bash", args: nil)
|
||||||
|
#expect(summary.emoji == "🛠️")
|
||||||
|
#expect(summary.title == "Bash")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -221,6 +221,15 @@ if [ -d "$SPARKLE_FRAMEWORK_PRIMARY" ]; then
|
|||||||
chmod -R a+rX "$APP_ROOT/Contents/Frameworks/Sparkle.framework"
|
chmod -R a+rX "$APP_ROOT/Contents/Frameworks/Sparkle.framework"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "📦 Copying Swift 6.2 compatibility libraries"
|
||||||
|
SWIFT_COMPAT_LIB="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-6.2/macosx/libswiftCompatibilitySpan.dylib"
|
||||||
|
if [ -f "$SWIFT_COMPAT_LIB" ]; then
|
||||||
|
cp "$SWIFT_COMPAT_LIB" "$APP_ROOT/Contents/Frameworks/"
|
||||||
|
chmod +x "$APP_ROOT/Contents/Frameworks/libswiftCompatibilitySpan.dylib"
|
||||||
|
else
|
||||||
|
echo "WARN: Swift compatibility library not found at $SWIFT_COMPAT_LIB (continuing)" >&2
|
||||||
|
fi
|
||||||
|
|
||||||
echo "🖼 Copying app icon"
|
echo "🖼 Copying app icon"
|
||||||
cp "$ROOT_DIR/apps/macos/Sources/Clawdbot/Resources/Clawdbot.icns" "$APP_ROOT/Contents/Resources/Clawdbot.icns"
|
cp "$ROOT_DIR/apps/macos/Sources/Clawdbot/Resources/Clawdbot.icns" "$APP_ROOT/Contents/Resources/Clawdbot.icns"
|
||||||
|
|
||||||
@@ -228,6 +237,15 @@ echo "📦 Copying device model resources"
|
|||||||
rm -rf "$APP_ROOT/Contents/Resources/DeviceModels"
|
rm -rf "$APP_ROOT/Contents/Resources/DeviceModels"
|
||||||
cp -R "$ROOT_DIR/apps/macos/Sources/Clawdbot/Resources/DeviceModels" "$APP_ROOT/Contents/Resources/DeviceModels"
|
cp -R "$ROOT_DIR/apps/macos/Sources/Clawdbot/Resources/DeviceModels" "$APP_ROOT/Contents/Resources/DeviceModels"
|
||||||
|
|
||||||
|
echo "📦 Copying ClawdbotKit resources"
|
||||||
|
CLAWDBOTKIT_BUNDLE="$(build_path_for_arch "$PRIMARY_ARCH")/$BUILD_CONFIG/ClawdbotKit_ClawdbotKit.bundle"
|
||||||
|
if [ -d "$CLAWDBOTKIT_BUNDLE" ]; then
|
||||||
|
rm -rf "$APP_ROOT/Contents/Resources/ClawdbotKit_ClawdbotKit.bundle"
|
||||||
|
cp -R "$CLAWDBOTKIT_BUNDLE" "$APP_ROOT/Contents/Resources/ClawdbotKit_ClawdbotKit.bundle"
|
||||||
|
else
|
||||||
|
echo "WARN: ClawdbotKit resource bundle not found at $CLAWDBOTKIT_BUNDLE (continuing)" >&2
|
||||||
|
fi
|
||||||
|
|
||||||
RELAY_DIR="$APP_ROOT/Contents/Resources/Relay"
|
RELAY_DIR="$APP_ROOT/Contents/Resources/Relay"
|
||||||
|
|
||||||
if [[ "${SKIP_GATEWAY_PACKAGE:-0}" != "1" ]]; then
|
if [[ "${SKIP_GATEWAY_PACKAGE:-0}" != "1" ]]; then
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ describe("buildAgentSystemPrompt", () => {
|
|||||||
toolNames: ["gateway", "bash"],
|
toolNames: ["gateway", "bash"],
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(prompt).toContain("## ClaudeBot Self-Update");
|
expect(prompt).toContain("## Clawdbot Self-Update");
|
||||||
expect(prompt).toContain("config.apply");
|
expect(prompt).toContain("config.apply");
|
||||||
expect(prompt).toContain("update.run");
|
expect(prompt).toContain("update.run");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -19,9 +19,17 @@ describe("cron tool", () => {
|
|||||||
{ action: "update", jobId: "job-1", patch: { foo: "bar" } },
|
{ action: "update", jobId: "job-1", patch: { foo: "bar" } },
|
||||||
{ id: "job-1", patch: { foo: "bar" } },
|
{ id: "job-1", patch: { foo: "bar" } },
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
"update",
|
||||||
|
{ action: "update", id: "job-2", patch: { foo: "bar" } },
|
||||||
|
{ id: "job-2", patch: { foo: "bar" } },
|
||||||
|
],
|
||||||
["remove", { action: "remove", jobId: "job-1" }, { id: "job-1" }],
|
["remove", { action: "remove", jobId: "job-1" }, { id: "job-1" }],
|
||||||
|
["remove", { action: "remove", id: "job-2" }, { id: "job-2" }],
|
||||||
["run", { action: "run", jobId: "job-1" }, { id: "job-1" }],
|
["run", { action: "run", jobId: "job-1" }, { id: "job-1" }],
|
||||||
|
["run", { action: "run", id: "job-2" }, { id: "job-2" }],
|
||||||
["runs", { action: "runs", jobId: "job-1" }, { id: "job-1" }],
|
["runs", { action: "runs", jobId: "job-1" }, { id: "job-1" }],
|
||||||
|
["runs", { action: "runs", id: "job-2" }, { id: "job-2" }],
|
||||||
])("%s sends id to gateway", async (action, args, expectedParams) => {
|
])("%s sends id to gateway", async (action, args, expectedParams) => {
|
||||||
const tool = createCronTool();
|
const tool = createCronTool();
|
||||||
await tool.execute("call1", args);
|
await tool.execute("call1", args);
|
||||||
@@ -35,6 +43,20 @@ describe("cron tool", () => {
|
|||||||
expect(call.params).toEqual(expectedParams);
|
expect(call.params).toEqual(expectedParams);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("prefers jobId over id when both are provided", async () => {
|
||||||
|
const tool = createCronTool();
|
||||||
|
await tool.execute("call1", {
|
||||||
|
action: "run",
|
||||||
|
jobId: "job-primary",
|
||||||
|
id: "job-legacy",
|
||||||
|
});
|
||||||
|
|
||||||
|
const call = callGatewayMock.mock.calls[0]?.[0] as {
|
||||||
|
params?: unknown;
|
||||||
|
};
|
||||||
|
expect(call?.params).toEqual({ id: "job-primary" });
|
||||||
|
});
|
||||||
|
|
||||||
it("normalizes cron.add job payloads", async () => {
|
it("normalizes cron.add job payloads", async () => {
|
||||||
const tool = createCronTool();
|
const tool = createCronTool();
|
||||||
await tool.execute("call2", {
|
await tool.execute("call2", {
|
||||||
|
|||||||
@@ -47,7 +47,8 @@ const CronToolSchema = Type.Union([
|
|||||||
gatewayUrl: Type.Optional(Type.String()),
|
gatewayUrl: Type.Optional(Type.String()),
|
||||||
gatewayToken: Type.Optional(Type.String()),
|
gatewayToken: Type.Optional(Type.String()),
|
||||||
timeoutMs: Type.Optional(Type.Number()),
|
timeoutMs: Type.Optional(Type.Number()),
|
||||||
jobId: Type.String(),
|
jobId: Type.Optional(Type.String()),
|
||||||
|
id: Type.Optional(Type.String()),
|
||||||
patch: Type.Object({}, { additionalProperties: true }),
|
patch: Type.Object({}, { additionalProperties: true }),
|
||||||
}),
|
}),
|
||||||
Type.Object({
|
Type.Object({
|
||||||
@@ -55,21 +56,24 @@ const CronToolSchema = Type.Union([
|
|||||||
gatewayUrl: Type.Optional(Type.String()),
|
gatewayUrl: Type.Optional(Type.String()),
|
||||||
gatewayToken: Type.Optional(Type.String()),
|
gatewayToken: Type.Optional(Type.String()),
|
||||||
timeoutMs: Type.Optional(Type.Number()),
|
timeoutMs: Type.Optional(Type.Number()),
|
||||||
jobId: Type.String(),
|
jobId: Type.Optional(Type.String()),
|
||||||
|
id: Type.Optional(Type.String()),
|
||||||
}),
|
}),
|
||||||
Type.Object({
|
Type.Object({
|
||||||
action: Type.Literal("run"),
|
action: Type.Literal("run"),
|
||||||
gatewayUrl: Type.Optional(Type.String()),
|
gatewayUrl: Type.Optional(Type.String()),
|
||||||
gatewayToken: Type.Optional(Type.String()),
|
gatewayToken: Type.Optional(Type.String()),
|
||||||
timeoutMs: Type.Optional(Type.Number()),
|
timeoutMs: Type.Optional(Type.Number()),
|
||||||
jobId: Type.String(),
|
jobId: Type.Optional(Type.String()),
|
||||||
|
id: Type.Optional(Type.String()),
|
||||||
}),
|
}),
|
||||||
Type.Object({
|
Type.Object({
|
||||||
action: Type.Literal("runs"),
|
action: Type.Literal("runs"),
|
||||||
gatewayUrl: Type.Optional(Type.String()),
|
gatewayUrl: Type.Optional(Type.String()),
|
||||||
gatewayToken: Type.Optional(Type.String()),
|
gatewayToken: Type.Optional(Type.String()),
|
||||||
timeoutMs: Type.Optional(Type.Number()),
|
timeoutMs: Type.Optional(Type.Number()),
|
||||||
jobId: Type.String(),
|
jobId: Type.Optional(Type.String()),
|
||||||
|
id: Type.Optional(Type.String()),
|
||||||
}),
|
}),
|
||||||
Type.Object({
|
Type.Object({
|
||||||
action: Type.Literal("wake"),
|
action: Type.Literal("wake"),
|
||||||
@@ -88,7 +92,7 @@ export function createCronTool(): AnyAgentTool {
|
|||||||
label: "Cron",
|
label: "Cron",
|
||||||
name: "cron",
|
name: "cron",
|
||||||
description:
|
description:
|
||||||
"Manage Gateway cron jobs (status/list/add/update/remove/run/runs) and send wake events.",
|
"Manage Gateway cron jobs (status/list/add/update/remove/run/runs) and send wake events. Use `jobId` as the canonical identifier; `id` is accepted for compatibility.",
|
||||||
parameters: CronToolSchema,
|
parameters: CronToolSchema,
|
||||||
execute: async (_toolCallId, args) => {
|
execute: async (_toolCallId, args) => {
|
||||||
const params = args as Record<string, unknown>;
|
const params = args as Record<string, unknown>;
|
||||||
@@ -121,7 +125,13 @@ export function createCronTool(): AnyAgentTool {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
case "update": {
|
case "update": {
|
||||||
const id = readStringParam(params, "jobId", { required: true });
|
const id =
|
||||||
|
readStringParam(params, "jobId") ?? readStringParam(params, "id");
|
||||||
|
if (!id) {
|
||||||
|
throw new Error(
|
||||||
|
"jobId required (id accepted for backward compatibility)",
|
||||||
|
);
|
||||||
|
}
|
||||||
if (!params.patch || typeof params.patch !== "object") {
|
if (!params.patch || typeof params.patch !== "object") {
|
||||||
throw new Error("patch required");
|
throw new Error("patch required");
|
||||||
}
|
}
|
||||||
@@ -134,19 +144,37 @@ export function createCronTool(): AnyAgentTool {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
case "remove": {
|
case "remove": {
|
||||||
const id = readStringParam(params, "jobId", { required: true });
|
const id =
|
||||||
|
readStringParam(params, "jobId") ?? readStringParam(params, "id");
|
||||||
|
if (!id) {
|
||||||
|
throw new Error(
|
||||||
|
"jobId required (id accepted for backward compatibility)",
|
||||||
|
);
|
||||||
|
}
|
||||||
return jsonResult(
|
return jsonResult(
|
||||||
await callGatewayTool("cron.remove", gatewayOpts, { id }),
|
await callGatewayTool("cron.remove", gatewayOpts, { id }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
case "run": {
|
case "run": {
|
||||||
const id = readStringParam(params, "jobId", { required: true });
|
const id =
|
||||||
|
readStringParam(params, "jobId") ?? readStringParam(params, "id");
|
||||||
|
if (!id) {
|
||||||
|
throw new Error(
|
||||||
|
"jobId required (id accepted for backward compatibility)",
|
||||||
|
);
|
||||||
|
}
|
||||||
return jsonResult(
|
return jsonResult(
|
||||||
await callGatewayTool("cron.run", gatewayOpts, { id }),
|
await callGatewayTool("cron.run", gatewayOpts, { id }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
case "runs": {
|
case "runs": {
|
||||||
const id = readStringParam(params, "jobId", { required: true });
|
const id =
|
||||||
|
readStringParam(params, "jobId") ?? readStringParam(params, "id");
|
||||||
|
if (!id) {
|
||||||
|
throw new Error(
|
||||||
|
"jobId required (id accepted for backward compatibility)",
|
||||||
|
);
|
||||||
|
}
|
||||||
return jsonResult(
|
return jsonResult(
|
||||||
await callGatewayTool("cron.runs", gatewayOpts, { id }),
|
await callGatewayTool("cron.runs", gatewayOpts, { id }),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -67,6 +67,12 @@ describe("chunkText", () => {
|
|||||||
const chunks = chunkText(text, 10);
|
const chunks = chunkText(text, 10);
|
||||||
expect(chunks).toEqual(["Supercalif", "ragilistic", "expialidoc", "ious"]);
|
expect(chunks).toEqual(["Supercalif", "ragilistic", "expialidoc", "ious"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("keeps parenthetical phrases together", () => {
|
||||||
|
const text = "Heads up now (Though now I'm curious)ok";
|
||||||
|
const chunks = chunkText(text, 35);
|
||||||
|
expect(chunks).toEqual(["Heads up now", "(Though now I'm curious)ok"]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("resolveTextChunkLimit", () => {
|
describe("resolveTextChunkLimit", () => {
|
||||||
@@ -184,4 +190,29 @@ describe("chunkMarkdownText", () => {
|
|||||||
expect(nonFenceLines.join("\n").trim()).not.toBe("");
|
expect(nonFenceLines.join("\n").trim()).not.toBe("");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("keeps parenthetical phrases together", () => {
|
||||||
|
const text = "Heads up now (Though now I'm curious)ok";
|
||||||
|
const chunks = chunkMarkdownText(text, 35);
|
||||||
|
expect(chunks).toEqual(["Heads up now", "(Though now I'm curious)ok"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles nested parentheses", () => {
|
||||||
|
const text = "Hello (outer (inner) end) world";
|
||||||
|
const chunks = chunkMarkdownText(text, 26);
|
||||||
|
expect(chunks).toEqual(["Hello (outer (inner) end)", "world"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("hard-breaks when a parenthetical exceeds the limit", () => {
|
||||||
|
const text = `(${"a".repeat(80)})`;
|
||||||
|
const chunks = chunkMarkdownText(text, 20);
|
||||||
|
expect(chunks[0]?.length).toBe(20);
|
||||||
|
expect(chunks.join("")).toBe(text);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores unmatched closing parentheses", () => {
|
||||||
|
const text = "Hello) world (ok)";
|
||||||
|
const chunks = chunkMarkdownText(text, 12);
|
||||||
|
expect(chunks).toEqual(["Hello)", "world (ok)"]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -90,18 +90,27 @@ export function chunkText(text: string, limit: number): string[] {
|
|||||||
while (remaining.length > limit) {
|
while (remaining.length > limit) {
|
||||||
const window = remaining.slice(0, limit);
|
const window = remaining.slice(0, limit);
|
||||||
|
|
||||||
// 1) Prefer a newline break inside the window.
|
// 1) Prefer a newline break inside the window (outside parentheses).
|
||||||
let breakIdx = window.lastIndexOf("\n");
|
let lastNewline = -1;
|
||||||
|
let lastWhitespace = -1;
|
||||||
|
let depth = 0;
|
||||||
|
for (let i = 0; i < window.length; i++) {
|
||||||
|
const char = window[i];
|
||||||
|
if (char === "(") {
|
||||||
|
depth += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (char === ")" && depth > 0) {
|
||||||
|
depth -= 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (depth !== 0) continue;
|
||||||
|
if (char === "\n") lastNewline = i;
|
||||||
|
else if (/\s/.test(char)) lastWhitespace = i;
|
||||||
|
}
|
||||||
|
|
||||||
// 2) Otherwise prefer the last whitespace (word boundary) inside the window.
|
// 2) Otherwise prefer the last whitespace (word boundary) inside the window.
|
||||||
if (breakIdx <= 0) {
|
let breakIdx = lastNewline > 0 ? lastNewline : lastWhitespace;
|
||||||
for (let i = window.length - 1; i >= 0; i--) {
|
|
||||||
if (/\s/.test(window[i])) {
|
|
||||||
breakIdx = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3) Fallback: hard break exactly at the limit.
|
// 3) Fallback: hard break exactly at the limit.
|
||||||
if (breakIdx <= 0) breakIdx = limit;
|
if (breakIdx <= 0) breakIdx = limit;
|
||||||
@@ -234,15 +243,27 @@ function pickSafeBreakIndex(
|
|||||||
window: string,
|
window: string,
|
||||||
spans: ReturnType<typeof parseFenceSpans>,
|
spans: ReturnType<typeof parseFenceSpans>,
|
||||||
): number {
|
): number {
|
||||||
let newlineIdx = window.lastIndexOf("\n");
|
let lastNewline = -1;
|
||||||
while (newlineIdx > 0) {
|
let lastWhitespace = -1;
|
||||||
if (isSafeFenceBreak(spans, newlineIdx)) return newlineIdx;
|
let depth = 0;
|
||||||
newlineIdx = window.lastIndexOf("\n", newlineIdx - 1);
|
|
||||||
}
|
for (let i = 0; i < window.length; i++) {
|
||||||
|
if (!isSafeFenceBreak(spans, i)) continue;
|
||||||
for (let i = window.length - 1; i > 0; i--) {
|
const char = window[i];
|
||||||
if (/\s/.test(window[i]) && isSafeFenceBreak(spans, i)) return i;
|
if (char === "(") {
|
||||||
|
depth += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (char === ")" && depth > 0) {
|
||||||
|
depth -= 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (depth !== 0) continue;
|
||||||
|
if (char === "\n") lastNewline = i;
|
||||||
|
else if (/\s/.test(char)) lastWhitespace = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (lastNewline > 0) return lastNewline;
|
||||||
|
if (lastWhitespace > 0) return lastWhitespace;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,6 +103,61 @@ describe("block streaming", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("preserves block reply ordering when typing start is slow", async () => {
|
||||||
|
await withTempHome(async (home) => {
|
||||||
|
let releaseTyping: (() => void) | undefined;
|
||||||
|
const typingGate = new Promise<void>((resolve) => {
|
||||||
|
releaseTyping = resolve;
|
||||||
|
});
|
||||||
|
const onReplyStart = vi.fn(() => typingGate);
|
||||||
|
const seen: string[] = [];
|
||||||
|
const onBlockReply = vi.fn(async (payload) => {
|
||||||
|
seen.push(payload.text ?? "");
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.mocked(runEmbeddedPiAgent).mockImplementation(async (params) => {
|
||||||
|
void params.onBlockReply?.({ text: "first" });
|
||||||
|
void params.onBlockReply?.({ text: "second" });
|
||||||
|
return {
|
||||||
|
payloads: [{ text: "first" }, { text: "second" }],
|
||||||
|
meta: {
|
||||||
|
durationMs: 5,
|
||||||
|
agentMeta: { sessionId: "s", provider: "p", model: "m" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const replyPromise = getReplyFromConfig(
|
||||||
|
{
|
||||||
|
Body: "ping",
|
||||||
|
From: "+1004",
|
||||||
|
To: "+2000",
|
||||||
|
MessageSid: "msg-125",
|
||||||
|
Provider: "telegram",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onReplyStart,
|
||||||
|
onBlockReply,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
agent: {
|
||||||
|
model: "anthropic/claude-opus-4-5",
|
||||||
|
workspace: path.join(home, "clawd"),
|
||||||
|
},
|
||||||
|
telegram: { allowFrom: ["*"] },
|
||||||
|
session: { store: path.join(home, "sessions.json") },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitForCalls(() => onReplyStart.mock.calls.length, 1);
|
||||||
|
releaseTyping?.();
|
||||||
|
|
||||||
|
const res = await replyPromise;
|
||||||
|
expect(res).toBeUndefined();
|
||||||
|
expect(seen).toEqual(["first", "second"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("drops final payloads when block replies streamed", async () => {
|
it("drops final payloads when block replies streamed", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempHome(async (home) => {
|
||||||
const onBlockReply = vi.fn().mockResolvedValue(undefined);
|
const onBlockReply = vi.fn().mockResolvedValue(undefined);
|
||||||
@@ -143,4 +198,59 @@ describe("block streaming", () => {
|
|||||||
expect(onBlockReply).toHaveBeenCalledTimes(1);
|
expect(onBlockReply).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("falls back to final payloads when block reply send times out", async () => {
|
||||||
|
await withTempHome(async (home) => {
|
||||||
|
let sawAbort = false;
|
||||||
|
const onBlockReply = vi.fn((_, context) => {
|
||||||
|
return new Promise<void>((resolve) => {
|
||||||
|
context?.abortSignal?.addEventListener(
|
||||||
|
"abort",
|
||||||
|
() => {
|
||||||
|
sawAbort = true;
|
||||||
|
resolve();
|
||||||
|
},
|
||||||
|
{ once: true },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.mocked(runEmbeddedPiAgent).mockImplementation(async (params) => {
|
||||||
|
void params.onBlockReply?.({ text: "streamed" });
|
||||||
|
return {
|
||||||
|
payloads: [{ text: "final" }],
|
||||||
|
meta: {
|
||||||
|
durationMs: 5,
|
||||||
|
agentMeta: { sessionId: "s", provider: "p", model: "m" },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const replyPromise = getReplyFromConfig(
|
||||||
|
{
|
||||||
|
Body: "ping",
|
||||||
|
From: "+1004",
|
||||||
|
To: "+2000",
|
||||||
|
MessageSid: "msg-126",
|
||||||
|
Provider: "telegram",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onBlockReply,
|
||||||
|
blockReplyTimeoutMs: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
agent: {
|
||||||
|
model: "anthropic/claude-opus-4-5",
|
||||||
|
workspace: path.join(home, "clawd"),
|
||||||
|
},
|
||||||
|
telegram: { allowFrom: ["*"] },
|
||||||
|
session: { store: path.join(home, "sessions.json") },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const res = await replyPromise;
|
||||||
|
expect(res).toMatchObject({ text: "final" });
|
||||||
|
expect(sawAbort).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ import type { TypingController } from "./typing.js";
|
|||||||
import { createTypingSignaler } from "./typing-mode.js";
|
import { createTypingSignaler } from "./typing-mode.js";
|
||||||
|
|
||||||
const BUN_FETCH_SOCKET_ERROR_RE = /socket connection was closed unexpectedly/i;
|
const BUN_FETCH_SOCKET_ERROR_RE = /socket connection was closed unexpectedly/i;
|
||||||
|
const BLOCK_REPLY_SEND_TIMEOUT_MS = 15_000;
|
||||||
|
|
||||||
const isBunFetchSocketError = (message?: string) =>
|
const isBunFetchSocketError = (message?: string) =>
|
||||||
Boolean(message && BUN_FETCH_SOCKET_ERROR_RE.test(message));
|
Boolean(message && BUN_FETCH_SOCKET_ERROR_RE.test(message));
|
||||||
@@ -61,6 +62,23 @@ const formatBunFetchSocketError = (message: string) => {
|
|||||||
].join("\n");
|
].join("\n");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const withTimeout = async <T>(
|
||||||
|
promise: Promise<T>,
|
||||||
|
timeoutMs: number,
|
||||||
|
timeoutError: Error,
|
||||||
|
): Promise<T> => {
|
||||||
|
if (!timeoutMs || timeoutMs <= 0) return promise;
|
||||||
|
let timer: NodeJS.Timeout | undefined;
|
||||||
|
const timeoutPromise = new Promise<never>((_, reject) => {
|
||||||
|
timer = setTimeout(() => reject(timeoutError), timeoutMs);
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
return await Promise.race([promise, timeoutPromise]);
|
||||||
|
} finally {
|
||||||
|
if (timer) clearTimeout(timer);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export async function runReplyAgent(params: {
|
export async function runReplyAgent(params: {
|
||||||
commandBody: string;
|
commandBody: string;
|
||||||
followupRun: FollowupRun;
|
followupRun: FollowupRun;
|
||||||
@@ -144,7 +162,12 @@ export async function runReplyAgent(params: {
|
|||||||
const pendingStreamedPayloadKeys = new Set<string>();
|
const pendingStreamedPayloadKeys = new Set<string>();
|
||||||
const pendingBlockTasks = new Set<Promise<void>>();
|
const pendingBlockTasks = new Set<Promise<void>>();
|
||||||
const pendingToolTasks = new Set<Promise<void>>();
|
const pendingToolTasks = new Set<Promise<void>>();
|
||||||
|
let blockReplyChain: Promise<void> = Promise.resolve();
|
||||||
|
let blockReplyAborted = false;
|
||||||
|
let didLogBlockReplyAbort = false;
|
||||||
let didStreamBlockReply = false;
|
let didStreamBlockReply = false;
|
||||||
|
const blockReplyTimeoutMs =
|
||||||
|
opts?.blockReplyTimeoutMs ?? BLOCK_REPLY_SEND_TIMEOUT_MS;
|
||||||
const buildPayloadKey = (payload: ReplyPayload) => {
|
const buildPayloadKey = (payload: ReplyPayload) => {
|
||||||
const text = payload.text?.trim() ?? "";
|
const text = payload.text?.trim() ?? "";
|
||||||
const mediaList = payload.mediaUrls?.length
|
const mediaList = payload.mediaUrls?.length
|
||||||
@@ -367,16 +390,49 @@ export async function runReplyAgent(params: {
|
|||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (blockReplyAborted) return;
|
||||||
pendingStreamedPayloadKeys.add(payloadKey);
|
pendingStreamedPayloadKeys.add(payloadKey);
|
||||||
const task = (async () => {
|
void typingSignals
|
||||||
await typingSignals.signalTextDelta(taggedPayload.text);
|
.signalTextDelta(taggedPayload.text)
|
||||||
await opts.onBlockReply?.(blockPayload);
|
.catch((err) => {
|
||||||
})()
|
logVerbose(
|
||||||
.then(() => {
|
`block reply typing signal failed: ${String(err)}`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
const timeoutError = new Error(
|
||||||
|
`block reply delivery timed out after ${blockReplyTimeoutMs}ms`,
|
||||||
|
);
|
||||||
|
const abortController = new AbortController();
|
||||||
|
blockReplyChain = blockReplyChain
|
||||||
|
.then(async () => {
|
||||||
|
if (blockReplyAborted) return false;
|
||||||
|
await withTimeout(
|
||||||
|
opts.onBlockReply?.(blockPayload, {
|
||||||
|
abortSignal: abortController.signal,
|
||||||
|
timeoutMs: blockReplyTimeoutMs,
|
||||||
|
}) ?? Promise.resolve(),
|
||||||
|
blockReplyTimeoutMs,
|
||||||
|
timeoutError,
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
.then((didSend) => {
|
||||||
|
if (!didSend) return;
|
||||||
streamedPayloadKeys.add(payloadKey);
|
streamedPayloadKeys.add(payloadKey);
|
||||||
didStreamBlockReply = true;
|
didStreamBlockReply = true;
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
if (err === timeoutError) {
|
||||||
|
abortController.abort();
|
||||||
|
blockReplyAborted = true;
|
||||||
|
if (!didLogBlockReplyAbort) {
|
||||||
|
didLogBlockReplyAbort = true;
|
||||||
|
logVerbose(
|
||||||
|
`block reply delivery timed out after ${blockReplyTimeoutMs}ms; skipping remaining block replies to preserve ordering`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
logVerbose(
|
logVerbose(
|
||||||
`block reply delivery failed: ${String(err)}`,
|
`block reply delivery failed: ${String(err)}`,
|
||||||
);
|
);
|
||||||
@@ -384,6 +440,7 @@ export async function runReplyAgent(params: {
|
|||||||
.finally(() => {
|
.finally(() => {
|
||||||
pendingStreamedPayloadKeys.delete(payloadKey);
|
pendingStreamedPayloadKeys.delete(payloadKey);
|
||||||
});
|
});
|
||||||
|
const task = blockReplyChain;
|
||||||
pendingBlockTasks.add(task);
|
pendingBlockTasks.add(task);
|
||||||
void task.finally(() => pendingBlockTasks.delete(task));
|
void task.finally(() => pendingBlockTasks.delete(task));
|
||||||
}
|
}
|
||||||
@@ -546,10 +603,10 @@ export async function runReplyAgent(params: {
|
|||||||
})
|
})
|
||||||
.filter(isRenderablePayload);
|
.filter(isRenderablePayload);
|
||||||
|
|
||||||
// Drop final payloads if block streaming is enabled and we already streamed
|
// Drop final payloads only when block streaming succeeded end-to-end.
|
||||||
// block replies. Tool-sent duplicates are filtered below.
|
// If streaming aborted (e.g., timeout), fall back to final payloads.
|
||||||
const shouldDropFinalPayloads =
|
const shouldDropFinalPayloads =
|
||||||
blockStreamingEnabled && didStreamBlockReply;
|
blockStreamingEnabled && didStreamBlockReply && !blockReplyAborted;
|
||||||
const messagingToolSentTexts = runResult.messagingToolSentTexts ?? [];
|
const messagingToolSentTexts = runResult.messagingToolSentTexts ?? [];
|
||||||
const messagingToolSentTargets = runResult.messagingToolSentTargets ?? [];
|
const messagingToolSentTargets = runResult.messagingToolSentTargets ?? [];
|
||||||
const suppressMessagingToolReplies = shouldSuppressMessagingToolReplies({
|
const suppressMessagingToolReplies = shouldSuppressMessagingToolReplies({
|
||||||
|
|||||||
@@ -444,7 +444,7 @@ export async function handleCommands(params: {
|
|||||||
...cfg.agent,
|
...cfg.agent,
|
||||||
model: {
|
model: {
|
||||||
...cfg.agent?.model,
|
...cfg.agent?.model,
|
||||||
primary: model,
|
primary: `${provider}/${model}`,
|
||||||
},
|
},
|
||||||
contextTokens,
|
contextTokens,
|
||||||
thinkingDefault: cfg.agent?.thinkingDefault,
|
thinkingDefault: cfg.agent?.thinkingDefault,
|
||||||
|
|||||||
@@ -41,10 +41,14 @@ export async function dispatchReplyFromConfig(params: {
|
|||||||
* Note: Only called when shouldRouteToOriginating is true, so
|
* Note: Only called when shouldRouteToOriginating is true, so
|
||||||
* originatingChannel and originatingTo are guaranteed to be defined.
|
* originatingChannel and originatingTo are guaranteed to be defined.
|
||||||
*/
|
*/
|
||||||
const sendPayloadAsync = async (payload: ReplyPayload): Promise<void> => {
|
const sendPayloadAsync = async (
|
||||||
|
payload: ReplyPayload,
|
||||||
|
abortSignal?: AbortSignal,
|
||||||
|
): Promise<void> => {
|
||||||
// TypeScript doesn't narrow these from the shouldRouteToOriginating check,
|
// TypeScript doesn't narrow these from the shouldRouteToOriginating check,
|
||||||
// but they're guaranteed non-null when this function is called.
|
// but they're guaranteed non-null when this function is called.
|
||||||
if (!originatingChannel || !originatingTo) return;
|
if (!originatingChannel || !originatingTo) return;
|
||||||
|
if (abortSignal?.aborted) return;
|
||||||
const result = await routeReply({
|
const result = await routeReply({
|
||||||
payload,
|
payload,
|
||||||
channel: originatingChannel,
|
channel: originatingChannel,
|
||||||
@@ -52,6 +56,7 @@ export async function dispatchReplyFromConfig(params: {
|
|||||||
accountId: ctx.AccountId,
|
accountId: ctx.AccountId,
|
||||||
threadId: ctx.MessageThreadId,
|
threadId: ctx.MessageThreadId,
|
||||||
cfg,
|
cfg,
|
||||||
|
abortSignal,
|
||||||
});
|
});
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
logVerbose(
|
logVerbose(
|
||||||
@@ -73,10 +78,10 @@ export async function dispatchReplyFromConfig(params: {
|
|||||||
dispatcher.sendToolResult(payload);
|
dispatcher.sendToolResult(payload);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onBlockReply: (payload: ReplyPayload) => {
|
onBlockReply: (payload: ReplyPayload, context) => {
|
||||||
if (shouldRouteToOriginating) {
|
if (shouldRouteToOriginating) {
|
||||||
// Fire-and-forget for streaming block replies when routing.
|
// Await routed sends so upstream can enforce ordering/timeouts.
|
||||||
void sendPayloadAsync(payload);
|
return sendPayloadAsync(payload, context?.abortSignal);
|
||||||
} else {
|
} else {
|
||||||
// Synchronous dispatch to preserve callback timing.
|
// Synchronous dispatch to preserve callback timing.
|
||||||
dispatcher.sendBlockReply(payload);
|
dispatcher.sendBlockReply(payload);
|
||||||
|
|||||||
@@ -31,6 +31,22 @@ vi.mock("../../web/outbound.js", () => ({
|
|||||||
const { routeReply } = await import("./route-reply.js");
|
const { routeReply } = await import("./route-reply.js");
|
||||||
|
|
||||||
describe("routeReply", () => {
|
describe("routeReply", () => {
|
||||||
|
it("skips sends when abort signal is already aborted", async () => {
|
||||||
|
mocks.sendMessageSlack.mockClear();
|
||||||
|
const controller = new AbortController();
|
||||||
|
controller.abort();
|
||||||
|
const res = await routeReply({
|
||||||
|
payload: { text: "hi" },
|
||||||
|
channel: "slack",
|
||||||
|
to: "channel:C123",
|
||||||
|
cfg: {} as never,
|
||||||
|
abortSignal: controller.signal,
|
||||||
|
});
|
||||||
|
expect(res.ok).toBe(false);
|
||||||
|
expect(res.error).toContain("aborted");
|
||||||
|
expect(mocks.sendMessageSlack).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
it("no-ops on empty payload", async () => {
|
it("no-ops on empty payload", async () => {
|
||||||
mocks.sendMessageSlack.mockClear();
|
mocks.sendMessageSlack.mockClear();
|
||||||
const res = await routeReply({
|
const res = await routeReply({
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ export type RouteReplyParams = {
|
|||||||
threadId?: number;
|
threadId?: number;
|
||||||
/** Config for provider-specific settings. */
|
/** Config for provider-specific settings. */
|
||||||
cfg: ClawdbotConfig;
|
cfg: ClawdbotConfig;
|
||||||
|
/** Optional abort signal for cooperative cancellation. */
|
||||||
|
abortSignal?: AbortSignal;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type RouteReplyResult = {
|
export type RouteReplyResult = {
|
||||||
@@ -52,7 +54,7 @@ export type RouteReplyResult = {
|
|||||||
export async function routeReply(
|
export async function routeReply(
|
||||||
params: RouteReplyParams,
|
params: RouteReplyParams,
|
||||||
): Promise<RouteReplyResult> {
|
): Promise<RouteReplyResult> {
|
||||||
const { payload, channel, to, accountId, threadId } = params;
|
const { payload, channel, to, accountId, threadId, abortSignal } = params;
|
||||||
|
|
||||||
// Debug: `pnpm test src/auto-reply/reply/route-reply.test.ts`
|
// Debug: `pnpm test src/auto-reply/reply/route-reply.test.ts`
|
||||||
const text = payload.text ?? "";
|
const text = payload.text ?? "";
|
||||||
@@ -72,6 +74,9 @@ export async function routeReply(
|
|||||||
text: string;
|
text: string;
|
||||||
mediaUrl?: string;
|
mediaUrl?: string;
|
||||||
}): Promise<RouteReplyResult> => {
|
}): Promise<RouteReplyResult> => {
|
||||||
|
if (abortSignal?.aborted) {
|
||||||
|
return { ok: false, error: "Reply routing aborted" };
|
||||||
|
}
|
||||||
const { text, mediaUrl } = params;
|
const { text, mediaUrl } = params;
|
||||||
switch (channel) {
|
switch (channel) {
|
||||||
case "telegram": {
|
case "telegram": {
|
||||||
@@ -148,12 +153,18 @@ export async function routeReply(
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (abortSignal?.aborted) {
|
||||||
|
return { ok: false, error: "Reply routing aborted" };
|
||||||
|
}
|
||||||
if (mediaUrls.length === 0) {
|
if (mediaUrls.length === 0) {
|
||||||
return await sendOne({ text });
|
return await sendOne({ text });
|
||||||
}
|
}
|
||||||
|
|
||||||
let last: RouteReplyResult | undefined;
|
let last: RouteReplyResult | undefined;
|
||||||
for (let i = 0; i < mediaUrls.length; i++) {
|
for (let i = 0; i < mediaUrls.length; i++) {
|
||||||
|
if (abortSignal?.aborted) {
|
||||||
|
return { ok: false, error: "Reply routing aborted" };
|
||||||
|
}
|
||||||
const mediaUrl = mediaUrls[i];
|
const mediaUrl = mediaUrls[i];
|
||||||
const caption = i === 0 ? text : "";
|
const caption = i === 0 ? text : "";
|
||||||
last = await sendOne({ text: caption, mediaUrl });
|
last = await sendOne({ text: caption, mediaUrl });
|
||||||
|
|||||||
@@ -102,6 +102,18 @@ describe("buildStatusMessage", () => {
|
|||||||
expect(text).toContain("🧠 Model: openai/gpt-4.1-mini");
|
expect(text).toContain("🧠 Model: openai/gpt-4.1-mini");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("keeps provider prefix from configured model", () => {
|
||||||
|
const text = buildStatusMessage({
|
||||||
|
agent: {
|
||||||
|
model: "google-antigravity/claude-sonnet-4-5",
|
||||||
|
},
|
||||||
|
sessionScope: "per-sender",
|
||||||
|
queue: { mode: "collect", depth: 0 },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(text).toContain("🧠 Model: google-antigravity/claude-sonnet-4-5");
|
||||||
|
});
|
||||||
|
|
||||||
it("handles missing agent config gracefully", () => {
|
it("handles missing agent config gracefully", () => {
|
||||||
const text = buildStatusMessage({
|
const text = buildStatusMessage({
|
||||||
agent: {},
|
agent: {},
|
||||||
|
|||||||
@@ -1,14 +1,24 @@
|
|||||||
import type { TypingController } from "./reply/typing.js";
|
import type { TypingController } from "./reply/typing.js";
|
||||||
|
|
||||||
|
export type BlockReplyContext = {
|
||||||
|
abortSignal?: AbortSignal;
|
||||||
|
timeoutMs?: number;
|
||||||
|
};
|
||||||
|
|
||||||
export type GetReplyOptions = {
|
export type GetReplyOptions = {
|
||||||
onReplyStart?: () => Promise<void> | void;
|
onReplyStart?: () => Promise<void> | void;
|
||||||
onTypingController?: (typing: TypingController) => void;
|
onTypingController?: (typing: TypingController) => void;
|
||||||
isHeartbeat?: boolean;
|
isHeartbeat?: boolean;
|
||||||
onPartialReply?: (payload: ReplyPayload) => Promise<void> | void;
|
onPartialReply?: (payload: ReplyPayload) => Promise<void> | void;
|
||||||
onReasoningStream?: (payload: ReplyPayload) => Promise<void> | void;
|
onReasoningStream?: (payload: ReplyPayload) => Promise<void> | void;
|
||||||
onBlockReply?: (payload: ReplyPayload) => Promise<void> | void;
|
onBlockReply?: (
|
||||||
|
payload: ReplyPayload,
|
||||||
|
context?: BlockReplyContext,
|
||||||
|
) => Promise<void> | void;
|
||||||
onToolResult?: (payload: ReplyPayload) => Promise<void> | void;
|
onToolResult?: (payload: ReplyPayload) => Promise<void> | void;
|
||||||
disableBlockStreaming?: boolean;
|
disableBlockStreaming?: boolean;
|
||||||
|
/** Timeout for block reply delivery (ms). */
|
||||||
|
blockReplyTimeoutMs?: number;
|
||||||
/** If provided, only load these skills for this session (empty = no skills). */
|
/** If provided, only load these skills for this session (empty = no skills). */
|
||||||
skillFilter?: string[];
|
skillFilter?: string[];
|
||||||
};
|
};
|
||||||
|
|||||||
61
src/discord/monitor.gateway.test.ts
Normal file
61
src/discord/monitor.gateway.test.ts
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import { EventEmitter } from "node:events";
|
||||||
|
|
||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import { waitForDiscordGatewayStop } from "./monitor.gateway.js";
|
||||||
|
|
||||||
|
describe("waitForDiscordGatewayStop", () => {
|
||||||
|
it("resolves on abort and disconnects gateway", async () => {
|
||||||
|
const emitter = new EventEmitter();
|
||||||
|
const disconnect = vi.fn();
|
||||||
|
const abort = new AbortController();
|
||||||
|
|
||||||
|
const promise = waitForDiscordGatewayStop({
|
||||||
|
gateway: { emitter, disconnect },
|
||||||
|
abortSignal: abort.signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(emitter.listenerCount("error")).toBe(1);
|
||||||
|
abort.abort();
|
||||||
|
|
||||||
|
await expect(promise).resolves.toBeUndefined();
|
||||||
|
expect(disconnect).toHaveBeenCalledTimes(1);
|
||||||
|
expect(emitter.listenerCount("error")).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects on gateway error and disconnects", async () => {
|
||||||
|
const emitter = new EventEmitter();
|
||||||
|
const disconnect = vi.fn();
|
||||||
|
const onGatewayError = vi.fn();
|
||||||
|
const abort = new AbortController();
|
||||||
|
const err = new Error("boom");
|
||||||
|
|
||||||
|
const promise = waitForDiscordGatewayStop({
|
||||||
|
gateway: { emitter, disconnect },
|
||||||
|
abortSignal: abort.signal,
|
||||||
|
onGatewayError,
|
||||||
|
});
|
||||||
|
|
||||||
|
emitter.emit("error", err);
|
||||||
|
|
||||||
|
await expect(promise).rejects.toThrow("boom");
|
||||||
|
expect(onGatewayError).toHaveBeenCalledWith(err);
|
||||||
|
expect(disconnect).toHaveBeenCalledTimes(1);
|
||||||
|
expect(emitter.listenerCount("error")).toBe(0);
|
||||||
|
|
||||||
|
abort.abort();
|
||||||
|
expect(disconnect).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resolves on abort without a gateway", async () => {
|
||||||
|
const abort = new AbortController();
|
||||||
|
|
||||||
|
const promise = waitForDiscordGatewayStop({
|
||||||
|
abortSignal: abort.signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
abort.abort();
|
||||||
|
|
||||||
|
await expect(promise).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
63
src/discord/monitor.gateway.ts
Normal file
63
src/discord/monitor.gateway.ts
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import type { EventEmitter } from "node:events";
|
||||||
|
|
||||||
|
export type DiscordGatewayHandle = {
|
||||||
|
emitter?: Pick<EventEmitter, "on" | "removeListener">;
|
||||||
|
disconnect?: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function getDiscordGatewayEmitter(
|
||||||
|
gateway?: unknown,
|
||||||
|
): EventEmitter | undefined {
|
||||||
|
return (gateway as { emitter?: EventEmitter } | undefined)?.emitter;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function waitForDiscordGatewayStop(params: {
|
||||||
|
gateway?: DiscordGatewayHandle;
|
||||||
|
abortSignal?: AbortSignal;
|
||||||
|
onGatewayError?: (err: unknown) => void;
|
||||||
|
}): Promise<void> {
|
||||||
|
const { gateway, abortSignal, onGatewayError } = params;
|
||||||
|
const emitter = gateway?.emitter;
|
||||||
|
return await new Promise<void>((resolve, reject) => {
|
||||||
|
let settled = false;
|
||||||
|
const cleanup = () => {
|
||||||
|
abortSignal?.removeEventListener("abort", onAbort);
|
||||||
|
emitter?.removeListener("error", onGatewayErrorEvent);
|
||||||
|
};
|
||||||
|
const finishResolve = () => {
|
||||||
|
if (settled) return;
|
||||||
|
settled = true;
|
||||||
|
cleanup();
|
||||||
|
try {
|
||||||
|
gateway?.disconnect?.();
|
||||||
|
} finally {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const finishReject = (err: unknown) => {
|
||||||
|
if (settled) return;
|
||||||
|
settled = true;
|
||||||
|
cleanup();
|
||||||
|
try {
|
||||||
|
gateway?.disconnect?.();
|
||||||
|
} finally {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const onAbort = () => {
|
||||||
|
finishResolve();
|
||||||
|
};
|
||||||
|
const onGatewayErrorEvent = (err: unknown) => {
|
||||||
|
onGatewayError?.(err);
|
||||||
|
finishReject(err);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (abortSignal?.aborted) {
|
||||||
|
onAbort();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
abortSignal?.addEventListener("abort", onAbort, { once: true });
|
||||||
|
emitter?.on("error", onGatewayErrorEvent);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -61,6 +61,10 @@ import type { RuntimeEnv } from "../runtime.js";
|
|||||||
import { loadWebMedia } from "../web/media.js";
|
import { loadWebMedia } from "../web/media.js";
|
||||||
import { resolveDiscordAccount } from "./accounts.js";
|
import { resolveDiscordAccount } from "./accounts.js";
|
||||||
import { chunkDiscordText } from "./chunk.js";
|
import { chunkDiscordText } from "./chunk.js";
|
||||||
|
import {
|
||||||
|
getDiscordGatewayEmitter,
|
||||||
|
waitForDiscordGatewayStop,
|
||||||
|
} from "./monitor.gateway.js";
|
||||||
import { fetchDiscordApplicationId } from "./probe.js";
|
import { fetchDiscordApplicationId } from "./probe.js";
|
||||||
import { reactMessageDiscord, sendMessageDiscord } from "./send.js";
|
import { reactMessageDiscord, sendMessageDiscord } from "./send.js";
|
||||||
import { normalizeDiscordToken } from "./token.js";
|
import { normalizeDiscordToken } from "./token.js";
|
||||||
@@ -402,18 +406,19 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
|
|||||||
|
|
||||||
runtime.log?.(`logged in to discord${botUserId ? ` as ${botUserId}` : ""}`);
|
runtime.log?.(`logged in to discord${botUserId ? ` as ${botUserId}` : ""}`);
|
||||||
|
|
||||||
await new Promise<void>((resolve) => {
|
|
||||||
const onAbort = async () => {
|
|
||||||
try {
|
|
||||||
const gateway = client.getPlugin<GatewayPlugin>("gateway");
|
const gateway = client.getPlugin<GatewayPlugin>("gateway");
|
||||||
gateway?.disconnect();
|
const gatewayEmitter = getDiscordGatewayEmitter(gateway);
|
||||||
} finally {
|
await waitForDiscordGatewayStop({
|
||||||
resolve();
|
gateway: gateway
|
||||||
|
? {
|
||||||
|
emitter: gatewayEmitter,
|
||||||
|
disconnect: () => gateway.disconnect(),
|
||||||
}
|
}
|
||||||
};
|
: undefined,
|
||||||
opts.abortSignal?.addEventListener("abort", () => {
|
abortSignal: opts.abortSignal,
|
||||||
void onAbort();
|
onGatewayError: (err) => {
|
||||||
});
|
runtime.error?.(danger(`discord gateway error: ${String(err)}`));
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { ReactionType, ReactionTypeEmoji } from "@grammyjs/types";
|
import type { ReactionType, ReactionTypeEmoji } from "@grammyjs/types";
|
||||||
|
import type { ApiClientOptions } from "grammy";
|
||||||
import { Bot, InputFile } from "grammy";
|
import { Bot, InputFile } from "grammy";
|
||||||
import { loadConfig } from "../config/config.js";
|
import { loadConfig } from "../config/config.js";
|
||||||
import { formatErrorMessage } from "../infra/errors.js";
|
import { formatErrorMessage } from "../infra/errors.js";
|
||||||
@@ -113,10 +114,10 @@ export async function sendMessageTelegram(
|
|||||||
// Use provided api or create a new Bot instance. The nullish coalescing
|
// Use provided api or create a new Bot instance. The nullish coalescing
|
||||||
// operator ensures api is always defined (Bot.api is always non-null).
|
// operator ensures api is always defined (Bot.api is always non-null).
|
||||||
const fetchImpl = resolveTelegramFetch();
|
const fetchImpl = resolveTelegramFetch();
|
||||||
const api =
|
const client: ApiClientOptions | undefined = fetchImpl
|
||||||
opts.api ??
|
? { fetch: fetchImpl as unknown as ApiClientOptions["fetch"] }
|
||||||
new Bot(token, fetchImpl ? { client: { fetch: fetchImpl } } : undefined)
|
: undefined;
|
||||||
.api;
|
const api = opts.api ?? new Bot(token, client ? { client } : undefined).api;
|
||||||
const mediaUrl = opts.mediaUrl?.trim();
|
const mediaUrl = opts.mediaUrl?.trim();
|
||||||
|
|
||||||
// Build optional params for forum topics and reply threading.
|
// Build optional params for forum topics and reply threading.
|
||||||
@@ -271,10 +272,10 @@ export async function reactMessageTelegram(
|
|||||||
const chatId = normalizeChatId(String(chatIdInput));
|
const chatId = normalizeChatId(String(chatIdInput));
|
||||||
const messageId = normalizeMessageId(messageIdInput);
|
const messageId = normalizeMessageId(messageIdInput);
|
||||||
const fetchImpl = resolveTelegramFetch();
|
const fetchImpl = resolveTelegramFetch();
|
||||||
const api =
|
const client: ApiClientOptions | undefined = fetchImpl
|
||||||
opts.api ??
|
? { fetch: fetchImpl as unknown as ApiClientOptions["fetch"] }
|
||||||
new Bot(token, fetchImpl ? { client: { fetch: fetchImpl } } : undefined)
|
: undefined;
|
||||||
.api;
|
const api = opts.api ?? new Bot(token, client ? { client } : undefined).api;
|
||||||
const request = createTelegramRetryRunner({
|
const request = createTelegramRetryRunner({
|
||||||
retry: opts.retry,
|
retry: opts.retry,
|
||||||
configRetry: account.config.retry,
|
configRetry: account.config.retry,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import type { ApiClientOptions } from "grammy";
|
||||||
import { Bot } from "grammy";
|
import { Bot } from "grammy";
|
||||||
import { resolveTelegramFetch } from "./fetch.js";
|
import { resolveTelegramFetch } from "./fetch.js";
|
||||||
|
|
||||||
@@ -8,10 +9,10 @@ export async function setTelegramWebhook(opts: {
|
|||||||
dropPendingUpdates?: boolean;
|
dropPendingUpdates?: boolean;
|
||||||
}) {
|
}) {
|
||||||
const fetchImpl = resolveTelegramFetch();
|
const fetchImpl = resolveTelegramFetch();
|
||||||
const bot = new Bot(
|
const client: ApiClientOptions | undefined = fetchImpl
|
||||||
opts.token,
|
? { fetch: fetchImpl as unknown as ApiClientOptions["fetch"] }
|
||||||
fetchImpl ? { client: { fetch: fetchImpl } } : undefined,
|
: undefined;
|
||||||
);
|
const bot = new Bot(opts.token, client ? { client } : undefined);
|
||||||
await bot.api.setWebhook(opts.url, {
|
await bot.api.setWebhook(opts.url, {
|
||||||
secret_token: opts.secret,
|
secret_token: opts.secret,
|
||||||
drop_pending_updates: opts.dropPendingUpdates ?? false,
|
drop_pending_updates: opts.dropPendingUpdates ?? false,
|
||||||
@@ -20,9 +21,9 @@ export async function setTelegramWebhook(opts: {
|
|||||||
|
|
||||||
export async function deleteTelegramWebhook(opts: { token: string }) {
|
export async function deleteTelegramWebhook(opts: { token: string }) {
|
||||||
const fetchImpl = resolveTelegramFetch();
|
const fetchImpl = resolveTelegramFetch();
|
||||||
const bot = new Bot(
|
const client: ApiClientOptions | undefined = fetchImpl
|
||||||
opts.token,
|
? { fetch: fetchImpl as unknown as ApiClientOptions["fetch"] }
|
||||||
fetchImpl ? { client: { fetch: fetchImpl } } : undefined,
|
: undefined;
|
||||||
);
|
const bot = new Bot(opts.token, client ? { client } : undefined);
|
||||||
await bot.api.deleteWebhook();
|
await bot.api.deleteWebhook();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user