Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ee24cfb58e | ||
|
|
ea7791f2b9 | ||
|
|
64be2b2cd1 | ||
|
|
dd2400fb2a | ||
|
|
5d001cb953 |
No files matched your search
+2
-1
@@ -9,7 +9,6 @@ Docs: https://docs.clawd.bot
|
|||||||
- Lobster: allow workflow file args via `argsJson` in the plugin tool. https://docs.clawd.bot/tools/lobster
|
- Lobster: allow workflow file args via `argsJson` in the plugin tool. https://docs.clawd.bot/tools/lobster
|
||||||
- Agents: add identity avatar config support and Control UI avatar rendering. (#1329, #1424) Thanks @dlauer.
|
- Agents: add identity avatar config support and Control UI avatar rendering. (#1329, #1424) Thanks @dlauer.
|
||||||
- Memory: prevent CLI hangs by deferring vector probes, adding sqlite-vec/embedding timeouts, and showing sync progress early.
|
- Memory: prevent CLI hangs by deferring vector probes, adding sqlite-vec/embedding timeouts, and showing sync progress early.
|
||||||
- BlueBubbles: add `asVoice` support for MP3/CAF voice memos in sendAttachment. (#1477) Thanks @Nicell.
|
|
||||||
- Docs: add troubleshooting entry for gateway.mode blocking gateway start. https://docs.clawd.bot/gateway/troubleshooting
|
- Docs: add troubleshooting entry for gateway.mode blocking gateway start. https://docs.clawd.bot/gateway/troubleshooting
|
||||||
- Docs: add /model allowlist troubleshooting note. (#1405)
|
- Docs: add /model allowlist troubleshooting note. (#1405)
|
||||||
- Docs: add per-message Gmail search example for gog. (#1220) Thanks @mbelinky.
|
- Docs: add per-message Gmail search example for gog. (#1220) Thanks @mbelinky.
|
||||||
@@ -25,6 +24,7 @@ Docs: https://docs.clawd.bot
|
|||||||
### Fixes
|
### Fixes
|
||||||
- BlueBubbles: stop typing indicator on idle/no-reply. (#1439) Thanks @Nicell.
|
- BlueBubbles: stop typing indicator on idle/no-reply. (#1439) Thanks @Nicell.
|
||||||
- Message tool: keep path/filePath as-is for send; hydrate buffers only for sendAttachment. (#1444) Thanks @hopyky.
|
- Message tool: keep path/filePath as-is for send; hydrate buffers only for sendAttachment. (#1444) Thanks @hopyky.
|
||||||
|
- CLI: handle Windows runtime exe argv and pnpm build shims more reliably. (#1480) Thanks @Takhoffman.
|
||||||
- Auto-reply: only report a model switch when session state is available. (#1465) Thanks @robbyczgw-cla.
|
- Auto-reply: only report a model switch when session state is available. (#1465) Thanks @robbyczgw-cla.
|
||||||
- Control UI: resolve local avatar URLs with basePath across injection + identity RPC. (#1457) Thanks @dlauer.
|
- Control UI: resolve local avatar URLs with basePath across injection + identity RPC. (#1457) Thanks @dlauer.
|
||||||
- Agents: surface concrete API error details instead of generic AI service errors.
|
- Agents: surface concrete API error details instead of generic AI service errors.
|
||||||
@@ -38,6 +38,7 @@ Docs: https://docs.clawd.bot
|
|||||||
- Auth: skip auth profiles in cooldown during initial selection and rotation. (#1316) Thanks @odrobnik.
|
- Auth: skip auth profiles in cooldown during initial selection and rotation. (#1316) Thanks @odrobnik.
|
||||||
- Agents/TUI: honor user-pinned auth profiles during cooldown and preserve search picker ranking. (#1432) Thanks @tobiasbischoff.
|
- Agents/TUI: honor user-pinned auth profiles during cooldown and preserve search picker ranking. (#1432) Thanks @tobiasbischoff.
|
||||||
- Docs: fix gog auth services example to include docs scope. (#1454) Thanks @zerone0x.
|
- Docs: fix gog auth services example to include docs scope. (#1454) Thanks @zerone0x.
|
||||||
|
- Slack: read thread replies for message reads when threadId is provided (replies-only). (#1450) Thanks @rodrigouroz.
|
||||||
- macOS: prefer linked channels in gateway summary to avoid false “not linked” status.
|
- macOS: prefer linked channels in gateway summary to avoid false “not linked” status.
|
||||||
- Providers: improve GitHub Copilot integration (enterprise support, base URL, and auth flow alignment).
|
- Providers: improve GitHub Copilot integration (enterprise support, base URL, and auth flow alignment).
|
||||||
|
|
||||||
|
|||||||
@@ -147,8 +147,7 @@ Available actions:
|
|||||||
- **addParticipant**: Add someone to a group (`chatGuid`, `address`)
|
- **addParticipant**: Add someone to a group (`chatGuid`, `address`)
|
||||||
- **removeParticipant**: Remove someone from a group (`chatGuid`, `address`)
|
- **removeParticipant**: Remove someone from a group (`chatGuid`, `address`)
|
||||||
- **leaveGroup**: Leave a group chat (`chatGuid`)
|
- **leaveGroup**: Leave a group chat (`chatGuid`)
|
||||||
- **sendAttachment**: Send media/files (`to`, `buffer`, `filename`, `asVoice`)
|
- **sendAttachment**: Send media/files (`to`, `buffer`, `filename`)
|
||||||
- Voice memos: set `asVoice: true` with **MP3** or **CAF** audio to send as an iMessage voice message. BlueBubbles converts MP3 → CAF when sending voice memos.
|
|
||||||
|
|
||||||
### Message IDs (short vs full)
|
### Message IDs (short vs full)
|
||||||
Clawdbot may surface *short* message IDs (e.g., `1`, `2`) to save tokens.
|
Clawdbot may surface *short* message IDs (e.g., `1`, `2`) to save tokens.
|
||||||
|
|||||||
@@ -521,42 +521,6 @@ describe("bluebubblesMessageActions", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("passes asVoice through sendAttachment", async () => {
|
|
||||||
const { sendBlueBubblesAttachment } = await import("./attachments.js");
|
|
||||||
|
|
||||||
const cfg: ClawdbotConfig = {
|
|
||||||
channels: {
|
|
||||||
bluebubbles: {
|
|
||||||
serverUrl: "http://localhost:1234",
|
|
||||||
password: "test-password",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const base64Buffer = Buffer.from("voice").toString("base64");
|
|
||||||
|
|
||||||
await bluebubblesMessageActions.handleAction({
|
|
||||||
action: "sendAttachment",
|
|
||||||
params: {
|
|
||||||
to: "+15551234567",
|
|
||||||
filename: "voice.mp3",
|
|
||||||
buffer: base64Buffer,
|
|
||||||
contentType: "audio/mpeg",
|
|
||||||
asVoice: true,
|
|
||||||
},
|
|
||||||
cfg,
|
|
||||||
accountId: null,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(sendBlueBubblesAttachment).toHaveBeenCalledWith(
|
|
||||||
expect.objectContaining({
|
|
||||||
filename: "voice.mp3",
|
|
||||||
contentType: "audio/mpeg",
|
|
||||||
asVoice: true,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("throws when buffer is missing for setGroupIcon", async () => {
|
it("throws when buffer is missing for setGroupIcon", async () => {
|
||||||
const cfg: ClawdbotConfig = {
|
const cfg: ClawdbotConfig = {
|
||||||
channels: {
|
channels: {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import {
|
|||||||
BLUEBUBBLES_ACTIONS,
|
BLUEBUBBLES_ACTIONS,
|
||||||
createActionGate,
|
createActionGate,
|
||||||
jsonResult,
|
jsonResult,
|
||||||
|
readBooleanParam,
|
||||||
readNumberParam,
|
readNumberParam,
|
||||||
readReactionParams,
|
readReactionParams,
|
||||||
readStringParam,
|
readStringParam,
|
||||||
@@ -50,17 +51,6 @@ function readMessageText(params: Record<string, unknown>): string | undefined {
|
|||||||
return readStringParam(params, "text") ?? readStringParam(params, "message");
|
return readStringParam(params, "text") ?? readStringParam(params, "message");
|
||||||
}
|
}
|
||||||
|
|
||||||
function readBooleanParam(params: Record<string, unknown>, key: string): boolean | undefined {
|
|
||||||
const raw = params[key];
|
|
||||||
if (typeof raw === "boolean") return raw;
|
|
||||||
if (typeof raw === "string") {
|
|
||||||
const trimmed = raw.trim().toLowerCase();
|
|
||||||
if (trimmed === "true") return true;
|
|
||||||
if (trimmed === "false") return false;
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Supported action names for BlueBubbles */
|
/** Supported action names for BlueBubbles */
|
||||||
const SUPPORTED_ACTIONS = new Set<ChannelMessageActionName>(BLUEBUBBLES_ACTION_NAMES);
|
const SUPPORTED_ACTIONS = new Set<ChannelMessageActionName>(BLUEBUBBLES_ACTION_NAMES);
|
||||||
|
|
||||||
@@ -366,7 +356,6 @@ export const bluebubblesMessageActions: ChannelMessageActionAdapter = {
|
|||||||
const caption = readStringParam(params, "caption");
|
const caption = readStringParam(params, "caption");
|
||||||
const contentType =
|
const contentType =
|
||||||
readStringParam(params, "contentType") ?? readStringParam(params, "mimeType");
|
readStringParam(params, "contentType") ?? readStringParam(params, "mimeType");
|
||||||
const asVoice = readBooleanParam(params, "asVoice");
|
|
||||||
|
|
||||||
// Buffer can come from params.buffer (base64) or params.path (file path)
|
// Buffer can come from params.buffer (base64) or params.path (file path)
|
||||||
const base64Buffer = readStringParam(params, "buffer");
|
const base64Buffer = readStringParam(params, "buffer");
|
||||||
@@ -391,7 +380,6 @@ export const bluebubblesMessageActions: ChannelMessageActionAdapter = {
|
|||||||
filename,
|
filename,
|
||||||
contentType: contentType ?? undefined,
|
contentType: contentType ?? undefined,
|
||||||
caption: caption ?? undefined,
|
caption: caption ?? undefined,
|
||||||
asVoice: asVoice ?? undefined,
|
|
||||||
opts,
|
opts,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
|
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
|
||||||
|
|
||||||
import { downloadBlueBubblesAttachment, sendBlueBubblesAttachment } from "./attachments.js";
|
import { downloadBlueBubblesAttachment } from "./attachments.js";
|
||||||
import type { BlueBubblesAttachment } from "./types.js";
|
import type { BlueBubblesAttachment } from "./types.js";
|
||||||
|
|
||||||
vi.mock("./accounts.js", () => ({
|
vi.mock("./accounts.js", () => ({
|
||||||
@@ -238,109 +238,3 @@ describe("downloadBlueBubblesAttachment", () => {
|
|||||||
expect(result.buffer).toEqual(new Uint8Array([1]));
|
expect(result.buffer).toEqual(new Uint8Array([1]));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("sendBlueBubblesAttachment", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
vi.stubGlobal("fetch", mockFetch);
|
|
||||||
mockFetch.mockReset();
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
vi.unstubAllGlobals();
|
|
||||||
});
|
|
||||||
|
|
||||||
function decodeBody(body: Uint8Array) {
|
|
||||||
return Buffer.from(body).toString("utf8");
|
|
||||||
}
|
|
||||||
|
|
||||||
it("marks voice memos when asVoice is true and mp3 is provided", async () => {
|
|
||||||
mockFetch.mockResolvedValueOnce({
|
|
||||||
ok: true,
|
|
||||||
text: () => Promise.resolve(JSON.stringify({ messageId: "msg-1" })),
|
|
||||||
});
|
|
||||||
|
|
||||||
await sendBlueBubblesAttachment({
|
|
||||||
to: "chat_guid:iMessage;-;+15551234567",
|
|
||||||
buffer: new Uint8Array([1, 2, 3]),
|
|
||||||
filename: "voice.mp3",
|
|
||||||
contentType: "audio/mpeg",
|
|
||||||
asVoice: true,
|
|
||||||
opts: { serverUrl: "http://localhost:1234", password: "test" },
|
|
||||||
});
|
|
||||||
|
|
||||||
const body = mockFetch.mock.calls[0][1]?.body as Uint8Array;
|
|
||||||
const bodyText = decodeBody(body);
|
|
||||||
expect(bodyText).toContain('name="isAudioMessage"');
|
|
||||||
expect(bodyText).toContain("true");
|
|
||||||
expect(bodyText).toContain('filename="voice.mp3"');
|
|
||||||
});
|
|
||||||
|
|
||||||
it("normalizes mp3 filenames for voice memos", async () => {
|
|
||||||
mockFetch.mockResolvedValueOnce({
|
|
||||||
ok: true,
|
|
||||||
text: () => Promise.resolve(JSON.stringify({ messageId: "msg-2" })),
|
|
||||||
});
|
|
||||||
|
|
||||||
await sendBlueBubblesAttachment({
|
|
||||||
to: "chat_guid:iMessage;-;+15551234567",
|
|
||||||
buffer: new Uint8Array([1, 2, 3]),
|
|
||||||
filename: "voice",
|
|
||||||
contentType: "audio/mpeg",
|
|
||||||
asVoice: true,
|
|
||||||
opts: { serverUrl: "http://localhost:1234", password: "test" },
|
|
||||||
});
|
|
||||||
|
|
||||||
const body = mockFetch.mock.calls[0][1]?.body as Uint8Array;
|
|
||||||
const bodyText = decodeBody(body);
|
|
||||||
expect(bodyText).toContain('filename="voice.mp3"');
|
|
||||||
expect(bodyText).toContain('name="voice.mp3"');
|
|
||||||
});
|
|
||||||
|
|
||||||
it("throws when asVoice is true but media is not audio", async () => {
|
|
||||||
await expect(
|
|
||||||
sendBlueBubblesAttachment({
|
|
||||||
to: "chat_guid:iMessage;-;+15551234567",
|
|
||||||
buffer: new Uint8Array([1, 2, 3]),
|
|
||||||
filename: "image.png",
|
|
||||||
contentType: "image/png",
|
|
||||||
asVoice: true,
|
|
||||||
opts: { serverUrl: "http://localhost:1234", password: "test" },
|
|
||||||
}),
|
|
||||||
).rejects.toThrow("voice messages require audio");
|
|
||||||
expect(mockFetch).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("throws when asVoice is true but audio is not mp3 or caf", async () => {
|
|
||||||
await expect(
|
|
||||||
sendBlueBubblesAttachment({
|
|
||||||
to: "chat_guid:iMessage;-;+15551234567",
|
|
||||||
buffer: new Uint8Array([1, 2, 3]),
|
|
||||||
filename: "voice.wav",
|
|
||||||
contentType: "audio/wav",
|
|
||||||
asVoice: true,
|
|
||||||
opts: { serverUrl: "http://localhost:1234", password: "test" },
|
|
||||||
}),
|
|
||||||
).rejects.toThrow("require mp3 or caf");
|
|
||||||
expect(mockFetch).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("sanitizes filenames before sending", async () => {
|
|
||||||
mockFetch.mockResolvedValueOnce({
|
|
||||||
ok: true,
|
|
||||||
text: () => Promise.resolve(JSON.stringify({ messageId: "msg-3" })),
|
|
||||||
});
|
|
||||||
|
|
||||||
await sendBlueBubblesAttachment({
|
|
||||||
to: "chat_guid:iMessage;-;+15551234567",
|
|
||||||
buffer: new Uint8Array([1, 2, 3]),
|
|
||||||
filename: "../evil.mp3",
|
|
||||||
contentType: "audio/mpeg",
|
|
||||||
opts: { serverUrl: "http://localhost:1234", password: "test" },
|
|
||||||
});
|
|
||||||
|
|
||||||
const body = mockFetch.mock.calls[0][1]?.body as Uint8Array;
|
|
||||||
const bodyText = decodeBody(body);
|
|
||||||
expect(bodyText).toContain('filename="evil.mp3"');
|
|
||||||
expect(bodyText).toContain('name="evil.mp3"');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import crypto from "node:crypto";
|
import crypto from "node:crypto";
|
||||||
import path from "node:path";
|
|
||||||
import type { ClawdbotConfig } from "clawdbot/plugin-sdk";
|
import type { ClawdbotConfig } from "clawdbot/plugin-sdk";
|
||||||
import { resolveBlueBubblesAccount } from "./accounts.js";
|
import { resolveBlueBubblesAccount } from "./accounts.js";
|
||||||
import { resolveChatGuidForTarget } from "./send.js";
|
import { resolveChatGuidForTarget } from "./send.js";
|
||||||
@@ -20,30 +19,6 @@ export type BlueBubblesAttachmentOpts = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const DEFAULT_ATTACHMENT_MAX_BYTES = 8 * 1024 * 1024;
|
const DEFAULT_ATTACHMENT_MAX_BYTES = 8 * 1024 * 1024;
|
||||||
const AUDIO_MIME_MP3 = new Set(["audio/mpeg", "audio/mp3"]);
|
|
||||||
const AUDIO_MIME_CAF = new Set(["audio/x-caf", "audio/caf"]);
|
|
||||||
|
|
||||||
function sanitizeFilename(input: string | undefined, fallback: string): string {
|
|
||||||
const trimmed = input?.trim() ?? "";
|
|
||||||
const base = trimmed ? path.basename(trimmed) : "";
|
|
||||||
return base || fallback;
|
|
||||||
}
|
|
||||||
|
|
||||||
function ensureExtension(filename: string, extension: string, fallbackBase: string): string {
|
|
||||||
const currentExt = path.extname(filename);
|
|
||||||
if (currentExt.toLowerCase() === extension) return filename;
|
|
||||||
const base = currentExt ? filename.slice(0, -currentExt.length) : filename;
|
|
||||||
return `${base || fallbackBase}${extension}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveVoiceInfo(filename: string, contentType?: string) {
|
|
||||||
const normalizedType = contentType?.trim().toLowerCase();
|
|
||||||
const extension = path.extname(filename).toLowerCase();
|
|
||||||
const isMp3 = extension === ".mp3" || (normalizedType ? AUDIO_MIME_MP3.has(normalizedType) : false);
|
|
||||||
const isCaf = extension === ".caf" || (normalizedType ? AUDIO_MIME_CAF.has(normalizedType) : false);
|
|
||||||
const isAudio = isMp3 || isCaf || Boolean(normalizedType?.startsWith("audio/"));
|
|
||||||
return { isAudio, isMp3, isCaf };
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveAccount(params: BlueBubblesAttachmentOpts) {
|
function resolveAccount(params: BlueBubblesAttachmentOpts) {
|
||||||
const account = resolveBlueBubblesAccount({
|
const account = resolveBlueBubblesAccount({
|
||||||
@@ -129,7 +104,6 @@ function extractMessageId(payload: unknown): string {
|
|||||||
/**
|
/**
|
||||||
* Send an attachment via BlueBubbles API.
|
* Send an attachment via BlueBubbles API.
|
||||||
* Supports sending media files (images, videos, audio, documents) to a chat.
|
* Supports sending media files (images, videos, audio, documents) to a chat.
|
||||||
* When asVoice is true, expects MP3/CAF audio and marks it as an iMessage voice memo.
|
|
||||||
*/
|
*/
|
||||||
export async function sendBlueBubblesAttachment(params: {
|
export async function sendBlueBubblesAttachment(params: {
|
||||||
to: string;
|
to: string;
|
||||||
@@ -139,37 +113,12 @@ export async function sendBlueBubblesAttachment(params: {
|
|||||||
caption?: string;
|
caption?: string;
|
||||||
replyToMessageGuid?: string;
|
replyToMessageGuid?: string;
|
||||||
replyToPartIndex?: number;
|
replyToPartIndex?: number;
|
||||||
asVoice?: boolean;
|
|
||||||
opts?: BlueBubblesAttachmentOpts;
|
opts?: BlueBubblesAttachmentOpts;
|
||||||
}): Promise<SendBlueBubblesAttachmentResult> {
|
}): Promise<SendBlueBubblesAttachmentResult> {
|
||||||
const { to, caption, replyToMessageGuid, replyToPartIndex, asVoice, opts = {} } = params;
|
const { to, buffer, filename, contentType, caption, replyToMessageGuid, replyToPartIndex, opts = {} } =
|
||||||
let { buffer, filename, contentType } = params;
|
params;
|
||||||
const wantsVoice = asVoice === true;
|
|
||||||
const fallbackName = wantsVoice ? "Audio Message" : "attachment";
|
|
||||||
filename = sanitizeFilename(filename, fallbackName);
|
|
||||||
contentType = contentType?.trim() || undefined;
|
|
||||||
const { baseUrl, password } = resolveAccount(opts);
|
const { baseUrl, password } = resolveAccount(opts);
|
||||||
|
|
||||||
// Validate voice memo format when requested (BlueBubbles converts MP3 -> CAF when isAudioMessage).
|
|
||||||
const isAudioMessage = wantsVoice;
|
|
||||||
if (isAudioMessage) {
|
|
||||||
const voiceInfo = resolveVoiceInfo(filename, contentType);
|
|
||||||
if (!voiceInfo.isAudio) {
|
|
||||||
throw new Error("BlueBubbles voice messages require audio media (mp3 or caf).");
|
|
||||||
}
|
|
||||||
if (voiceInfo.isMp3) {
|
|
||||||
filename = ensureExtension(filename, ".mp3", fallbackName);
|
|
||||||
contentType = contentType ?? "audio/mpeg";
|
|
||||||
} else if (voiceInfo.isCaf) {
|
|
||||||
filename = ensureExtension(filename, ".caf", fallbackName);
|
|
||||||
contentType = contentType ?? "audio/x-caf";
|
|
||||||
} else {
|
|
||||||
throw new Error(
|
|
||||||
"BlueBubbles voice messages require mp3 or caf audio (convert before sending).",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const target = resolveSendTarget(to);
|
const target = resolveSendTarget(to);
|
||||||
const chatGuid = await resolveChatGuidForTarget({
|
const chatGuid = await resolveChatGuidForTarget({
|
||||||
baseUrl,
|
baseUrl,
|
||||||
@@ -221,11 +170,6 @@ export async function sendBlueBubblesAttachment(params: {
|
|||||||
addField("tempGuid", `temp-${Date.now()}-${crypto.randomUUID().slice(0, 8)}`);
|
addField("tempGuid", `temp-${Date.now()}-${crypto.randomUUID().slice(0, 8)}`);
|
||||||
addField("method", "private-api");
|
addField("method", "private-api");
|
||||||
|
|
||||||
// Add isAudioMessage flag for voice memos
|
|
||||||
if (isAudioMessage) {
|
|
||||||
addField("isAudioMessage", "true");
|
|
||||||
}
|
|
||||||
|
|
||||||
const trimmedReplyTo = replyToMessageGuid?.trim();
|
const trimmedReplyTo = replyToMessageGuid?.trim();
|
||||||
if (trimmedReplyTo) {
|
if (trimmedReplyTo) {
|
||||||
addField("selectedMessageGuid", trimmedReplyTo);
|
addField("selectedMessageGuid", trimmedReplyTo);
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ export async function sendBlueBubblesMedia(params: {
|
|||||||
caption?: string;
|
caption?: string;
|
||||||
replyToId?: string | null;
|
replyToId?: string | null;
|
||||||
accountId?: string;
|
accountId?: string;
|
||||||
asVoice?: boolean;
|
|
||||||
}) {
|
}) {
|
||||||
const {
|
const {
|
||||||
cfg,
|
cfg,
|
||||||
@@ -72,7 +71,6 @@ export async function sendBlueBubblesMedia(params: {
|
|||||||
caption,
|
caption,
|
||||||
replyToId,
|
replyToId,
|
||||||
accountId,
|
accountId,
|
||||||
asVoice,
|
|
||||||
} = params;
|
} = params;
|
||||||
const core = getBlueBubblesRuntime();
|
const core = getBlueBubblesRuntime();
|
||||||
const maxBytes = resolveChannelMediaMaxBytes({
|
const maxBytes = resolveChannelMediaMaxBytes({
|
||||||
@@ -148,7 +146,6 @@ export async function sendBlueBubblesMedia(params: {
|
|||||||
filename: resolvedFilename ?? "attachment",
|
filename: resolvedFilename ?? "attachment",
|
||||||
contentType: resolvedContentType ?? undefined,
|
contentType: resolvedContentType ?? undefined,
|
||||||
replyToMessageGuid,
|
replyToMessageGuid,
|
||||||
asVoice,
|
|
||||||
opts: {
|
opts: {
|
||||||
cfg,
|
cfg,
|
||||||
accountId,
|
accountId,
|
||||||
|
|||||||
@@ -115,7 +115,12 @@ if (!shouldBuild()) {
|
|||||||
runNode();
|
runNode();
|
||||||
} else {
|
} else {
|
||||||
logRunner("Building TypeScript (dist is stale).");
|
logRunner("Building TypeScript (dist is stale).");
|
||||||
const build = spawn("pnpm", ["exec", compiler, ...projectArgs], {
|
const pnpmArgs = ["exec", compiler, ...projectArgs];
|
||||||
|
// On Windows, pnpm is a .cmd shim, so use cmd.exe for reliable resolution.
|
||||||
|
const buildCmd = process.platform === "win32" ? "cmd.exe" : "pnpm";
|
||||||
|
const buildArgs =
|
||||||
|
process.platform === "win32" ? ["/d", "/s", "/c", "pnpm", ...pnpmArgs] : pnpmArgs;
|
||||||
|
const build = spawn(buildCmd, buildArgs, {
|
||||||
cwd,
|
cwd,
|
||||||
env,
|
env,
|
||||||
stdio: "inherit",
|
stdio: "inherit",
|
||||||
|
|||||||
@@ -357,6 +357,20 @@ describe("handleSlackAction", () => {
|
|||||||
expect(payload.messages[0].timestampUtc).toBe(new Date(expectedMs).toISOString());
|
expect(payload.messages[0].timestampUtc).toBe(new Date(expectedMs).toISOString());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("passes threadId through to readSlackMessages", async () => {
|
||||||
|
const cfg = { channels: { slack: { botToken: "tok" } } } as ClawdbotConfig;
|
||||||
|
readSlackMessages.mockClear();
|
||||||
|
readSlackMessages.mockResolvedValueOnce({ messages: [], hasMore: false });
|
||||||
|
|
||||||
|
await handleSlackAction(
|
||||||
|
{ action: "readMessages", channelId: "C1", threadId: "12345.6789" },
|
||||||
|
cfg,
|
||||||
|
);
|
||||||
|
|
||||||
|
const [, opts] = readSlackMessages.mock.calls[0] ?? [];
|
||||||
|
expect(opts?.threadId).toBe("12345.6789");
|
||||||
|
});
|
||||||
|
|
||||||
it("adds normalized timestamps to pin payloads", async () => {
|
it("adds normalized timestamps to pin payloads", async () => {
|
||||||
const cfg = { channels: { slack: { botToken: "tok" } } } as ClawdbotConfig;
|
const cfg = { channels: { slack: { botToken: "tok" } } } as ClawdbotConfig;
|
||||||
listSlackPins.mockResolvedValueOnce([
|
listSlackPins.mockResolvedValueOnce([
|
||||||
|
|||||||
@@ -214,11 +214,13 @@ export async function handleSlackAction(
|
|||||||
typeof limitRaw === "number" && Number.isFinite(limitRaw) ? limitRaw : undefined;
|
typeof limitRaw === "number" && Number.isFinite(limitRaw) ? limitRaw : undefined;
|
||||||
const before = readStringParam(params, "before");
|
const before = readStringParam(params, "before");
|
||||||
const after = readStringParam(params, "after");
|
const after = readStringParam(params, "after");
|
||||||
|
const threadId = readStringParam(params, "threadId");
|
||||||
const result = await readSlackMessages(channelId, {
|
const result = await readSlackMessages(channelId, {
|
||||||
...readOpts,
|
...readOpts,
|
||||||
limit,
|
limit,
|
||||||
before: before ?? undefined,
|
before: before ?? undefined,
|
||||||
after: after ?? undefined,
|
after: after ?? undefined,
|
||||||
|
threadId: threadId ?? undefined,
|
||||||
});
|
});
|
||||||
const messages = result.messages.map((message) =>
|
const messages = result.messages.map((message) =>
|
||||||
withNormalizedTimestamp(
|
withNormalizedTimestamp(
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import type { ClawdbotConfig } from "../../config/config.js";
|
||||||
|
import { createSlackActions } from "./slack.actions.js";
|
||||||
|
|
||||||
|
const handleSlackAction = vi.fn(async () => ({ details: { ok: true } }));
|
||||||
|
|
||||||
|
vi.mock("../../agents/tools/slack-actions.js", () => ({
|
||||||
|
handleSlackAction: (...args: unknown[]) => handleSlackAction(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe("slack actions adapter", () => {
|
||||||
|
it("forwards threadId for read", async () => {
|
||||||
|
const cfg = { channels: { slack: { botToken: "tok" } } } as ClawdbotConfig;
|
||||||
|
const actions = createSlackActions("slack");
|
||||||
|
|
||||||
|
await actions.handleAction?.({
|
||||||
|
channel: "slack",
|
||||||
|
action: "read",
|
||||||
|
cfg,
|
||||||
|
params: {
|
||||||
|
channelId: "C1",
|
||||||
|
threadId: "171234.567",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const [params] = handleSlackAction.mock.calls[0] ?? [];
|
||||||
|
expect(params).toMatchObject({
|
||||||
|
action: "readMessages",
|
||||||
|
channelId: "C1",
|
||||||
|
threadId: "171234.567",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -133,6 +133,7 @@ export function createSlackActions(providerId: string): ChannelMessageActionAdap
|
|||||||
limit,
|
limit,
|
||||||
before: readStringParam(params, "before"),
|
before: readStringParam(params, "before"),
|
||||||
after: readStringParam(params, "after"),
|
after: readStringParam(params, "after"),
|
||||||
|
threadId: readStringParam(params, "threadId"),
|
||||||
accountId: accountId ?? undefined,
|
accountId: accountId ?? undefined,
|
||||||
},
|
},
|
||||||
cfg,
|
cfg,
|
||||||
|
|||||||
@@ -78,6 +78,16 @@ describe("argv helpers", () => {
|
|||||||
});
|
});
|
||||||
expect(nodeArgv).toEqual(["node", "clawdbot", "status"]);
|
expect(nodeArgv).toEqual(["node", "clawdbot", "status"]);
|
||||||
|
|
||||||
|
const nodeExeArgv = buildParseArgv({
|
||||||
|
programName: "clawdbot",
|
||||||
|
rawArgs: ["C:\\\\Program Files\\\\nodejs\\\\Node.EXE", "clawdbot", "status"],
|
||||||
|
});
|
||||||
|
expect(nodeExeArgv).toEqual([
|
||||||
|
"C:\\\\Program Files\\\\nodejs\\\\Node.EXE",
|
||||||
|
"clawdbot",
|
||||||
|
"status",
|
||||||
|
]);
|
||||||
|
|
||||||
const directArgv = buildParseArgv({
|
const directArgv = buildParseArgv({
|
||||||
programName: "clawdbot",
|
programName: "clawdbot",
|
||||||
rawArgs: ["clawdbot", "status"],
|
rawArgs: ["clawdbot", "status"],
|
||||||
@@ -89,6 +99,12 @@ describe("argv helpers", () => {
|
|||||||
rawArgs: ["bun", "src/entry.ts", "status"],
|
rawArgs: ["bun", "src/entry.ts", "status"],
|
||||||
});
|
});
|
||||||
expect(bunArgv).toEqual(["bun", "src/entry.ts", "status"]);
|
expect(bunArgv).toEqual(["bun", "src/entry.ts", "status"]);
|
||||||
|
|
||||||
|
const bunExeArgv = buildParseArgv({
|
||||||
|
programName: "clawdbot",
|
||||||
|
rawArgs: ["C:\\\\bun\\\\bun.exe", "src\\\\entry.ts", "status"],
|
||||||
|
});
|
||||||
|
expect(bunExeArgv).toEqual(["C:\\\\bun\\\\bun.exe", "src\\\\entry.ts", "status"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("builds parse argv from fallback args", () => {
|
it("builds parse argv from fallback args", () => {
|
||||||
|
|||||||
+7
-2
@@ -94,9 +94,14 @@ export function buildParseArgv(params: {
|
|||||||
: baseArgv[0]?.endsWith("clawdbot")
|
: baseArgv[0]?.endsWith("clawdbot")
|
||||||
? baseArgv.slice(1)
|
? baseArgv.slice(1)
|
||||||
: baseArgv;
|
: baseArgv;
|
||||||
const executable = normalizedArgv[0]?.split(/[/\\]/).pop() ?? "";
|
const executable = (normalizedArgv[0]?.split(/[/\\]/).pop() ?? "").toLowerCase();
|
||||||
|
// Normalize Windows `.exe` runtimes (node.exe/bun.exe) for runtime detection.
|
||||||
const looksLikeNode =
|
const looksLikeNode =
|
||||||
normalizedArgv.length >= 2 && (executable === "node" || executable === "bun");
|
normalizedArgv.length >= 2 &&
|
||||||
|
(executable === "node" ||
|
||||||
|
executable === "node.exe" ||
|
||||||
|
executable === "bun" ||
|
||||||
|
executable === "bun.exe");
|
||||||
if (looksLikeNode) return normalizedArgv;
|
if (looksLikeNode) return normalizedArgv;
|
||||||
return ["node", programName || "clawdbot", ...normalizedArgv];
|
return ["node", programName || "clawdbot", ...normalizedArgv];
|
||||||
}
|
}
|
||||||
|
|||||||
+333
-430
@@ -49,469 +49,372 @@ function parseSseDataLines(text: string): string[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("OpenAI-compatible HTTP API (e2e)", () => {
|
describe("OpenAI-compatible HTTP API (e2e)", () => {
|
||||||
it("is disabled by default (requires config)", { timeout: 120_000 }, async () => {
|
it("rejects when disabled (default + config)", { timeout: 120_000 }, async () => {
|
||||||
const port = await getFreePort();
|
{
|
||||||
const server = await startServerWithDefaultConfig(port);
|
const port = await getFreePort();
|
||||||
try {
|
const server = await startServerWithDefaultConfig(port);
|
||||||
const res = await postChatCompletions(port, {
|
try {
|
||||||
model: "clawdbot",
|
const res = await postChatCompletions(port, {
|
||||||
messages: [{ role: "user", content: "hi" }],
|
model: "clawdbot",
|
||||||
|
messages: [{ role: "user", content: "hi" }],
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
} finally {
|
||||||
|
await server.close({ reason: "test done" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const port = await getFreePort();
|
||||||
|
const server = await startServer(port, {
|
||||||
|
openAiChatCompletionsEnabled: false,
|
||||||
});
|
});
|
||||||
expect(res.status).toBe(404);
|
try {
|
||||||
} finally {
|
const res = await postChatCompletions(port, {
|
||||||
await server.close({ reason: "test done" });
|
model: "clawdbot",
|
||||||
|
messages: [{ role: "user", content: "hi" }],
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
} finally {
|
||||||
|
await server.close({ reason: "test done" });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("can be disabled via config (404)", async () => {
|
it("handles request validation and routing", async () => {
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port, {
|
|
||||||
openAiChatCompletionsEnabled: false,
|
|
||||||
});
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(port, {
|
|
||||||
model: "clawdbot",
|
|
||||||
messages: [{ role: "user", content: "hi" }],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(404);
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("rejects non-POST", async () => {
|
|
||||||
const port = await getFreePort();
|
const port = await getFreePort();
|
||||||
const server = await startServer(port);
|
const server = await startServer(port);
|
||||||
|
const mockAgentOnce = (payloads: Array<{ text: string }>) => {
|
||||||
|
agentCommand.mockReset();
|
||||||
|
agentCommand.mockResolvedValueOnce({ payloads } as never);
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
|
{
|
||||||
method: "GET",
|
const res = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
|
||||||
headers: { authorization: "Bearer secret" },
|
method: "GET",
|
||||||
});
|
headers: { authorization: "Bearer secret" },
|
||||||
expect(res.status).toBe(405);
|
});
|
||||||
} finally {
|
expect(res.status).toBe(405);
|
||||||
await server.close({ reason: "test done" });
|
await res.text();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
it("rejects missing auth", async () => {
|
{
|
||||||
const port = await getFreePort();
|
const res = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
|
||||||
const server = await startServer(port);
|
method: "POST",
|
||||||
try {
|
headers: { "content-type": "application/json" },
|
||||||
const res = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
|
body: JSON.stringify({ messages: [{ role: "user", content: "hi" }] }),
|
||||||
method: "POST",
|
});
|
||||||
headers: { "content-type": "application/json" },
|
expect(res.status).toBe(401);
|
||||||
body: JSON.stringify({ messages: [{ role: "user", content: "hi" }] }),
|
await res.text();
|
||||||
});
|
}
|
||||||
expect(res.status).toBe(401);
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("routes to a specific agent via header", async () => {
|
{
|
||||||
agentCommand.mockResolvedValueOnce({
|
mockAgentOnce([{ text: "hello" }]);
|
||||||
payloads: [{ text: "hello" }],
|
const res = await postChatCompletions(
|
||||||
} as never);
|
port,
|
||||||
|
{ model: "clawdbot", messages: [{ role: "user", content: "hi" }] },
|
||||||
|
{ "x-clawdbot-agent-id": "beta" },
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
const port = await getFreePort();
|
expect(agentCommand).toHaveBeenCalledTimes(1);
|
||||||
const server = await startServer(port);
|
const [opts] = agentCommand.mock.calls[0] ?? [];
|
||||||
try {
|
expect((opts as { sessionKey?: string } | undefined)?.sessionKey ?? "").toMatch(
|
||||||
const res = await postChatCompletions(
|
/^agent:beta:/,
|
||||||
port,
|
);
|
||||||
{ model: "clawdbot", messages: [{ role: "user", content: "hi" }] },
|
await res.text();
|
||||||
{ "x-clawdbot-agent-id": "beta" },
|
}
|
||||||
);
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
|
|
||||||
expect(agentCommand).toHaveBeenCalledTimes(1);
|
{
|
||||||
const [opts] = agentCommand.mock.calls[0] ?? [];
|
mockAgentOnce([{ text: "hello" }]);
|
||||||
expect((opts as { sessionKey?: string } | undefined)?.sessionKey ?? "").toMatch(
|
const res = await postChatCompletions(port, {
|
||||||
/^agent:beta:/,
|
|
||||||
);
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("routes to a specific agent via model (no custom headers)", async () => {
|
|
||||||
agentCommand.mockResolvedValueOnce({
|
|
||||||
payloads: [{ text: "hello" }],
|
|
||||||
} as never);
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(port, {
|
|
||||||
model: "clawdbot:beta",
|
|
||||||
messages: [{ role: "user", content: "hi" }],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
|
|
||||||
expect(agentCommand).toHaveBeenCalledTimes(1);
|
|
||||||
const [opts] = agentCommand.mock.calls[0] ?? [];
|
|
||||||
expect((opts as { sessionKey?: string } | undefined)?.sessionKey ?? "").toMatch(
|
|
||||||
/^agent:beta:/,
|
|
||||||
);
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("prefers explicit header agent over model agent", async () => {
|
|
||||||
agentCommand.mockResolvedValueOnce({
|
|
||||||
payloads: [{ text: "hello" }],
|
|
||||||
} as never);
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(
|
|
||||||
port,
|
|
||||||
{
|
|
||||||
model: "clawdbot:beta",
|
model: "clawdbot:beta",
|
||||||
messages: [{ role: "user", content: "hi" }],
|
messages: [{ role: "user", content: "hi" }],
|
||||||
},
|
});
|
||||||
{ "x-clawdbot-agent-id": "alpha" },
|
expect(res.status).toBe(200);
|
||||||
);
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
|
|
||||||
expect(agentCommand).toHaveBeenCalledTimes(1);
|
expect(agentCommand).toHaveBeenCalledTimes(1);
|
||||||
const [opts] = agentCommand.mock.calls[0] ?? [];
|
const [opts] = agentCommand.mock.calls[0] ?? [];
|
||||||
expect((opts as { sessionKey?: string } | undefined)?.sessionKey ?? "").toMatch(
|
expect((opts as { sessionKey?: string } | undefined)?.sessionKey ?? "").toMatch(
|
||||||
/^agent:alpha:/,
|
/^agent:beta:/,
|
||||||
);
|
);
|
||||||
} finally {
|
await res.text();
|
||||||
await server.close({ reason: "test done" });
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("honors x-clawdbot-session-key override", async () => {
|
{
|
||||||
agentCommand.mockResolvedValueOnce({
|
mockAgentOnce([{ text: "hello" }]);
|
||||||
payloads: [{ text: "hello" }],
|
const res = await postChatCompletions(
|
||||||
} as never);
|
port,
|
||||||
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(
|
|
||||||
port,
|
|
||||||
{ model: "clawdbot", messages: [{ role: "user", content: "hi" }] },
|
|
||||||
{
|
|
||||||
"x-clawdbot-agent-id": "beta",
|
|
||||||
"x-clawdbot-session-key": "agent:beta:openai:custom",
|
|
||||||
},
|
|
||||||
);
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
|
|
||||||
const [opts] = agentCommand.mock.calls[0] ?? [];
|
|
||||||
expect((opts as { sessionKey?: string } | undefined)?.sessionKey).toBe(
|
|
||||||
"agent:beta:openai:custom",
|
|
||||||
);
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("uses OpenAI user for a stable session key", async () => {
|
|
||||||
agentCommand.mockResolvedValueOnce({
|
|
||||||
payloads: [{ text: "hello" }],
|
|
||||||
} as never);
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(port, {
|
|
||||||
user: "alice",
|
|
||||||
model: "clawdbot",
|
|
||||||
messages: [{ role: "user", content: "hi" }],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
|
|
||||||
const [opts] = agentCommand.mock.calls[0] ?? [];
|
|
||||||
expect((opts as { sessionKey?: string } | undefined)?.sessionKey ?? "").toContain(
|
|
||||||
"openai-user:alice",
|
|
||||||
);
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("extracts user message text from array content", async () => {
|
|
||||||
agentCommand.mockResolvedValueOnce({
|
|
||||||
payloads: [{ text: "hello" }],
|
|
||||||
} as never);
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(port, {
|
|
||||||
model: "clawdbot",
|
|
||||||
messages: [
|
|
||||||
{
|
{
|
||||||
role: "user",
|
model: "clawdbot:beta",
|
||||||
content: [
|
messages: [{ role: "user", content: "hi" }],
|
||||||
{ type: "text", text: "hello" },
|
|
||||||
{ type: "input_text", text: "world" },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
],
|
{ "x-clawdbot-agent-id": "alpha" },
|
||||||
});
|
);
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
const [opts] = agentCommand.mock.calls[0] ?? [];
|
expect(agentCommand).toHaveBeenCalledTimes(1);
|
||||||
expect((opts as { message?: string } | undefined)?.message).toBe("hello\nworld");
|
const [opts] = agentCommand.mock.calls[0] ?? [];
|
||||||
|
expect((opts as { sessionKey?: string } | undefined)?.sessionKey ?? "").toMatch(
|
||||||
|
/^agent:alpha:/,
|
||||||
|
);
|
||||||
|
await res.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
mockAgentOnce([{ text: "hello" }]);
|
||||||
|
const res = await postChatCompletions(
|
||||||
|
port,
|
||||||
|
{ model: "clawdbot", messages: [{ role: "user", content: "hi" }] },
|
||||||
|
{
|
||||||
|
"x-clawdbot-agent-id": "beta",
|
||||||
|
"x-clawdbot-session-key": "agent:beta:openai:custom",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
|
const [opts] = agentCommand.mock.calls[0] ?? [];
|
||||||
|
expect((opts as { sessionKey?: string } | undefined)?.sessionKey).toBe(
|
||||||
|
"agent:beta:openai:custom",
|
||||||
|
);
|
||||||
|
await res.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
mockAgentOnce([{ text: "hello" }]);
|
||||||
|
const res = await postChatCompletions(port, {
|
||||||
|
user: "alice",
|
||||||
|
model: "clawdbot",
|
||||||
|
messages: [{ role: "user", content: "hi" }],
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
|
const [opts] = agentCommand.mock.calls[0] ?? [];
|
||||||
|
expect((opts as { sessionKey?: string } | undefined)?.sessionKey ?? "").toContain(
|
||||||
|
"openai-user:alice",
|
||||||
|
);
|
||||||
|
await res.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
mockAgentOnce([{ text: "hello" }]);
|
||||||
|
const res = await postChatCompletions(port, {
|
||||||
|
model: "clawdbot",
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: [
|
||||||
|
{ type: "text", text: "hello" },
|
||||||
|
{ type: "input_text", text: "world" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
|
const [opts] = agentCommand.mock.calls[0] ?? [];
|
||||||
|
expect((opts as { message?: string } | undefined)?.message).toBe("hello\nworld");
|
||||||
|
await res.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
mockAgentOnce([{ text: "I am Claude" }]);
|
||||||
|
const res = await postChatCompletions(port, {
|
||||||
|
model: "clawdbot",
|
||||||
|
messages: [
|
||||||
|
{ role: "system", content: "You are a helpful assistant." },
|
||||||
|
{ role: "user", content: "Hello, who are you?" },
|
||||||
|
{ role: "assistant", content: "I am Claude." },
|
||||||
|
{ role: "user", content: "What did I just ask you?" },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
|
const [opts] = agentCommand.mock.calls[0] ?? [];
|
||||||
|
const message = (opts as { message?: string } | undefined)?.message ?? "";
|
||||||
|
expect(message).toContain(HISTORY_CONTEXT_MARKER);
|
||||||
|
expect(message).toContain("User: Hello, who are you?");
|
||||||
|
expect(message).toContain("Assistant: I am Claude.");
|
||||||
|
expect(message).toContain(CURRENT_MESSAGE_MARKER);
|
||||||
|
expect(message).toContain("User: What did I just ask you?");
|
||||||
|
await res.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
mockAgentOnce([{ text: "hello" }]);
|
||||||
|
const res = await postChatCompletions(port, {
|
||||||
|
model: "clawdbot",
|
||||||
|
messages: [
|
||||||
|
{ role: "system", content: "You are a helpful assistant." },
|
||||||
|
{ role: "user", content: "Hello" },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
|
const [opts] = agentCommand.mock.calls[0] ?? [];
|
||||||
|
const message = (opts as { message?: string } | undefined)?.message ?? "";
|
||||||
|
expect(message).not.toContain(HISTORY_CONTEXT_MARKER);
|
||||||
|
expect(message).not.toContain(CURRENT_MESSAGE_MARKER);
|
||||||
|
expect(message).toBe("Hello");
|
||||||
|
await res.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
mockAgentOnce([{ text: "hello" }]);
|
||||||
|
const res = await postChatCompletions(port, {
|
||||||
|
model: "clawdbot",
|
||||||
|
messages: [
|
||||||
|
{ role: "developer", content: "You are a helpful assistant." },
|
||||||
|
{ role: "user", content: "Hello" },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
|
const [opts] = agentCommand.mock.calls[0] ?? [];
|
||||||
|
const extraSystemPrompt =
|
||||||
|
(opts as { extraSystemPrompt?: string } | undefined)?.extraSystemPrompt ?? "";
|
||||||
|
expect(extraSystemPrompt).toBe("You are a helpful assistant.");
|
||||||
|
await res.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
mockAgentOnce([{ text: "ok" }]);
|
||||||
|
const res = await postChatCompletions(port, {
|
||||||
|
model: "clawdbot",
|
||||||
|
messages: [
|
||||||
|
{ role: "system", content: "You are a helpful assistant." },
|
||||||
|
{ role: "user", content: "What's the weather?" },
|
||||||
|
{ role: "assistant", content: "Checking the weather." },
|
||||||
|
{ role: "tool", content: "Sunny, 70F." },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
|
const [opts] = agentCommand.mock.calls[0] ?? [];
|
||||||
|
const message = (opts as { message?: string } | undefined)?.message ?? "";
|
||||||
|
expect(message).toContain(HISTORY_CONTEXT_MARKER);
|
||||||
|
expect(message).toContain("User: What's the weather?");
|
||||||
|
expect(message).toContain("Assistant: Checking the weather.");
|
||||||
|
expect(message).toContain(CURRENT_MESSAGE_MARKER);
|
||||||
|
expect(message).toContain("Tool: Sunny, 70F.");
|
||||||
|
await res.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
mockAgentOnce([{ text: "hello" }]);
|
||||||
|
const res = await postChatCompletions(port, {
|
||||||
|
stream: false,
|
||||||
|
model: "clawdbot",
|
||||||
|
messages: [{ role: "user", content: "hi" }],
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
const json = (await res.json()) as Record<string, unknown>;
|
||||||
|
expect(json.object).toBe("chat.completion");
|
||||||
|
expect(Array.isArray(json.choices)).toBe(true);
|
||||||
|
const choice0 = (json.choices as Array<Record<string, unknown>>)[0] ?? {};
|
||||||
|
const msg = (choice0.message as Record<string, unknown> | undefined) ?? {};
|
||||||
|
expect(msg.role).toBe("assistant");
|
||||||
|
expect(msg.content).toBe("hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const res = await postChatCompletions(port, {
|
||||||
|
model: "clawdbot",
|
||||||
|
messages: [{ role: "system", content: "yo" }],
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
const missingUserJson = (await res.json()) as Record<string, unknown>;
|
||||||
|
expect((missingUserJson.error as Record<string, unknown> | undefined)?.type).toBe(
|
||||||
|
"invalid_request_error",
|
||||||
|
);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
await server.close({ reason: "test done" });
|
await server.close({ reason: "test done" });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("includes conversation history when multiple messages are provided", async () => {
|
it("streams SSE chunks when stream=true", async () => {
|
||||||
agentCommand.mockResolvedValueOnce({
|
|
||||||
payloads: [{ text: "I am Claude" }],
|
|
||||||
} as never);
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
const port = await getFreePort();
|
||||||
const server = await startServer(port);
|
const server = await startServer(port);
|
||||||
try {
|
try {
|
||||||
const res = await postChatCompletions(port, {
|
{
|
||||||
model: "clawdbot",
|
agentCommand.mockReset();
|
||||||
messages: [
|
agentCommand.mockImplementationOnce(async (opts: unknown) => {
|
||||||
{ role: "system", content: "You are a helpful assistant." },
|
const runId = (opts as { runId?: string } | undefined)?.runId ?? "";
|
||||||
{ role: "user", content: "Hello, who are you?" },
|
emitAgentEvent({ runId, stream: "assistant", data: { delta: "he" } });
|
||||||
{ role: "assistant", content: "I am Claude." },
|
emitAgentEvent({ runId, stream: "assistant", data: { delta: "llo" } });
|
||||||
{ role: "user", content: "What did I just ask you?" },
|
return { payloads: [{ text: "hello" }] } as never;
|
||||||
],
|
});
|
||||||
});
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
|
|
||||||
const [opts] = agentCommand.mock.calls[0] ?? [];
|
const res = await postChatCompletions(port, {
|
||||||
const message = (opts as { message?: string } | undefined)?.message ?? "";
|
stream: true,
|
||||||
expect(message).toContain(HISTORY_CONTEXT_MARKER);
|
model: "clawdbot",
|
||||||
expect(message).toContain("User: Hello, who are you?");
|
messages: [{ role: "user", content: "hi" }],
|
||||||
expect(message).toContain("Assistant: I am Claude.");
|
});
|
||||||
expect(message).toContain(CURRENT_MESSAGE_MARKER);
|
expect(res.status).toBe(200);
|
||||||
expect(message).toContain("User: What did I just ask you?");
|
expect(res.headers.get("content-type") ?? "").toContain("text/event-stream");
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does not include history markers for single message", async () => {
|
const text = await res.text();
|
||||||
agentCommand.mockResolvedValueOnce({
|
const data = parseSseDataLines(text);
|
||||||
payloads: [{ text: "hello" }],
|
expect(data[data.length - 1]).toBe("[DONE]");
|
||||||
} as never);
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
const jsonChunks = data
|
||||||
const server = await startServer(port);
|
.filter((d) => d !== "[DONE]")
|
||||||
try {
|
.map((d) => JSON.parse(d) as Record<string, unknown>);
|
||||||
const res = await postChatCompletions(port, {
|
expect(jsonChunks.some((c) => c.object === "chat.completion.chunk")).toBe(true);
|
||||||
model: "clawdbot",
|
const allContent = jsonChunks
|
||||||
messages: [
|
.flatMap((c) => (c.choices as Array<Record<string, unknown>> | undefined) ?? [])
|
||||||
{ role: "system", content: "You are a helpful assistant." },
|
.map((choice) => (choice.delta as Record<string, unknown> | undefined)?.content)
|
||||||
{ role: "user", content: "Hello" },
|
.filter((v): v is string => typeof v === "string")
|
||||||
],
|
.join("");
|
||||||
});
|
expect(allContent).toBe("hello");
|
||||||
expect(res.status).toBe(200);
|
}
|
||||||
|
|
||||||
const [opts] = agentCommand.mock.calls[0] ?? [];
|
{
|
||||||
const message = (opts as { message?: string } | undefined)?.message ?? "";
|
agentCommand.mockReset();
|
||||||
expect(message).not.toContain(HISTORY_CONTEXT_MARKER);
|
agentCommand.mockImplementationOnce(async (opts: unknown) => {
|
||||||
expect(message).not.toContain(CURRENT_MESSAGE_MARKER);
|
const runId = (opts as { runId?: string } | undefined)?.runId ?? "";
|
||||||
expect(message).toBe("Hello");
|
emitAgentEvent({ runId, stream: "assistant", data: { delta: "hi" } });
|
||||||
} finally {
|
emitAgentEvent({ runId, stream: "assistant", data: { delta: "hi" } });
|
||||||
await server.close({ reason: "test done" });
|
return { payloads: [{ text: "hihi" }] } as never;
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it("treats developer role same as system role", async () => {
|
const repeatedRes = await postChatCompletions(port, {
|
||||||
agentCommand.mockResolvedValueOnce({
|
stream: true,
|
||||||
payloads: [{ text: "hello" }],
|
model: "clawdbot",
|
||||||
} as never);
|
messages: [{ role: "user", content: "hi" }],
|
||||||
|
});
|
||||||
|
expect(repeatedRes.status).toBe(200);
|
||||||
|
const repeatedText = await repeatedRes.text();
|
||||||
|
const repeatedData = parseSseDataLines(repeatedText);
|
||||||
|
const repeatedChunks = repeatedData
|
||||||
|
.filter((d) => d !== "[DONE]")
|
||||||
|
.map((d) => JSON.parse(d) as Record<string, unknown>);
|
||||||
|
const repeatedContent = repeatedChunks
|
||||||
|
.flatMap((c) => (c.choices as Array<Record<string, unknown>> | undefined) ?? [])
|
||||||
|
.map((choice) => (choice.delta as Record<string, unknown> | undefined)?.content)
|
||||||
|
.filter((v): v is string => typeof v === "string")
|
||||||
|
.join("");
|
||||||
|
expect(repeatedContent).toBe("hihi");
|
||||||
|
}
|
||||||
|
|
||||||
const port = await getFreePort();
|
{
|
||||||
const server = await startServer(port);
|
agentCommand.mockReset();
|
||||||
try {
|
agentCommand.mockResolvedValueOnce({
|
||||||
const res = await postChatCompletions(port, {
|
payloads: [{ text: "hello" }],
|
||||||
model: "clawdbot",
|
} as never);
|
||||||
messages: [
|
|
||||||
{ role: "developer", content: "You are a helpful assistant." },
|
|
||||||
{ role: "user", content: "Hello" },
|
|
||||||
],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
|
|
||||||
const [opts] = agentCommand.mock.calls[0] ?? [];
|
const fallbackRes = await postChatCompletions(port, {
|
||||||
const extraSystemPrompt =
|
stream: true,
|
||||||
(opts as { extraSystemPrompt?: string } | undefined)?.extraSystemPrompt ?? "";
|
model: "clawdbot",
|
||||||
expect(extraSystemPrompt).toBe("You are a helpful assistant.");
|
messages: [{ role: "user", content: "hi" }],
|
||||||
} finally {
|
});
|
||||||
await server.close({ reason: "test done" });
|
expect(fallbackRes.status).toBe(200);
|
||||||
}
|
const fallbackText = await fallbackRes.text();
|
||||||
});
|
expect(fallbackText).toContain("[DONE]");
|
||||||
|
expect(fallbackText).toContain("hello");
|
||||||
it("includes tool output when it is the latest message", async () => {
|
}
|
||||||
agentCommand.mockResolvedValueOnce({
|
|
||||||
payloads: [{ text: "ok" }],
|
|
||||||
} as never);
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(port, {
|
|
||||||
model: "clawdbot",
|
|
||||||
messages: [
|
|
||||||
{ role: "system", content: "You are a helpful assistant." },
|
|
||||||
{ role: "user", content: "What's the weather?" },
|
|
||||||
{ role: "assistant", content: "Checking the weather." },
|
|
||||||
{ role: "tool", content: "Sunny, 70F." },
|
|
||||||
],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
|
|
||||||
const [opts] = agentCommand.mock.calls[0] ?? [];
|
|
||||||
const message = (opts as { message?: string } | undefined)?.message ?? "";
|
|
||||||
expect(message).toContain(HISTORY_CONTEXT_MARKER);
|
|
||||||
expect(message).toContain("User: What's the weather?");
|
|
||||||
expect(message).toContain("Assistant: Checking the weather.");
|
|
||||||
expect(message).toContain(CURRENT_MESSAGE_MARKER);
|
|
||||||
expect(message).toContain("Tool: Sunny, 70F.");
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns a non-streaming OpenAI chat.completion response", async () => {
|
|
||||||
agentCommand.mockResolvedValueOnce({
|
|
||||||
payloads: [{ text: "hello" }],
|
|
||||||
} as never);
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(port, {
|
|
||||||
stream: false,
|
|
||||||
model: "clawdbot",
|
|
||||||
messages: [{ role: "user", content: "hi" }],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
const json = (await res.json()) as Record<string, unknown>;
|
|
||||||
expect(json.object).toBe("chat.completion");
|
|
||||||
expect(Array.isArray(json.choices)).toBe(true);
|
|
||||||
const choice0 = (json.choices as Array<Record<string, unknown>>)[0] ?? {};
|
|
||||||
const msg = (choice0.message as Record<string, unknown> | undefined) ?? {};
|
|
||||||
expect(msg.role).toBe("assistant");
|
|
||||||
expect(msg.content).toBe("hello");
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("requires a user message", async () => {
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(port, {
|
|
||||||
model: "clawdbot",
|
|
||||||
messages: [{ role: "system", content: "yo" }],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
const json = (await res.json()) as Record<string, unknown>;
|
|
||||||
expect((json.error as Record<string, unknown> | undefined)?.type).toBe(
|
|
||||||
"invalid_request_error",
|
|
||||||
);
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("streams SSE chunks when stream=true (delta events)", async () => {
|
|
||||||
agentCommand.mockImplementationOnce(async (opts: unknown) => {
|
|
||||||
const runId = (opts as { runId?: string } | undefined)?.runId ?? "";
|
|
||||||
emitAgentEvent({ runId, stream: "assistant", data: { delta: "he" } });
|
|
||||||
emitAgentEvent({ runId, stream: "assistant", data: { delta: "llo" } });
|
|
||||||
return { payloads: [{ text: "hello" }] } as never;
|
|
||||||
});
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(port, {
|
|
||||||
stream: true,
|
|
||||||
model: "clawdbot",
|
|
||||||
messages: [{ role: "user", content: "hi" }],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
expect(res.headers.get("content-type") ?? "").toContain("text/event-stream");
|
|
||||||
|
|
||||||
const text = await res.text();
|
|
||||||
const data = parseSseDataLines(text);
|
|
||||||
expect(data[data.length - 1]).toBe("[DONE]");
|
|
||||||
|
|
||||||
const jsonChunks = data
|
|
||||||
.filter((d) => d !== "[DONE]")
|
|
||||||
.map((d) => JSON.parse(d) as Record<string, unknown>);
|
|
||||||
expect(jsonChunks.some((c) => c.object === "chat.completion.chunk")).toBe(true);
|
|
||||||
const allContent = jsonChunks
|
|
||||||
.flatMap((c) => (c.choices as Array<Record<string, unknown>> | undefined) ?? [])
|
|
||||||
.map((choice) => (choice.delta as Record<string, unknown> | undefined)?.content)
|
|
||||||
.filter((v): v is string => typeof v === "string")
|
|
||||||
.join("");
|
|
||||||
expect(allContent).toBe("hello");
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("preserves repeated identical deltas when streaming SSE", async () => {
|
|
||||||
agentCommand.mockImplementationOnce(async (opts: unknown) => {
|
|
||||||
const runId = (opts as { runId?: string } | undefined)?.runId ?? "";
|
|
||||||
emitAgentEvent({ runId, stream: "assistant", data: { delta: "hi" } });
|
|
||||||
emitAgentEvent({ runId, stream: "assistant", data: { delta: "hi" } });
|
|
||||||
return { payloads: [{ text: "hihi" }] } as never;
|
|
||||||
});
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(port, {
|
|
||||||
stream: true,
|
|
||||||
model: "clawdbot",
|
|
||||||
messages: [{ role: "user", content: "hi" }],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
const text = await res.text();
|
|
||||||
const data = parseSseDataLines(text);
|
|
||||||
const jsonChunks = data
|
|
||||||
.filter((d) => d !== "[DONE]")
|
|
||||||
.map((d) => JSON.parse(d) as Record<string, unknown>);
|
|
||||||
const allContent = jsonChunks
|
|
||||||
.flatMap((c) => (c.choices as Array<Record<string, unknown>> | undefined) ?? [])
|
|
||||||
.map((choice) => (choice.delta as Record<string, unknown> | undefined)?.content)
|
|
||||||
.filter((v): v is string => typeof v === "string")
|
|
||||||
.join("");
|
|
||||||
expect(allContent).toBe("hihi");
|
|
||||||
} finally {
|
|
||||||
await server.close({ reason: "test done" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("streams SSE chunks when stream=true (fallback when no deltas)", async () => {
|
|
||||||
agentCommand.mockResolvedValueOnce({
|
|
||||||
payloads: [{ text: "hello" }],
|
|
||||||
} as never);
|
|
||||||
|
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startServer(port);
|
|
||||||
try {
|
|
||||||
const res = await postChatCompletions(port, {
|
|
||||||
stream: true,
|
|
||||||
model: "clawdbot",
|
|
||||||
messages: [{ role: "user", content: "hi" }],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
const text = await res.text();
|
|
||||||
expect(text).toContain("[DONE]");
|
|
||||||
expect(text).toContain("hello");
|
|
||||||
} finally {
|
} finally {
|
||||||
await server.close({ reason: "test done" });
|
await server.close({ reason: "test done" });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,95 +1,106 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterEach, describe, expect, it } from "vitest";
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||||
|
import { WebSocket } from "ws";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
connectOk,
|
connectOk,
|
||||||
|
getFreePort,
|
||||||
installGatewayTestHooks,
|
installGatewayTestHooks,
|
||||||
onceMessage,
|
onceMessage,
|
||||||
startServerWithClient,
|
startGatewayServer,
|
||||||
} from "./test-helpers.js";
|
} from "./test-helpers.js";
|
||||||
|
|
||||||
installGatewayTestHooks();
|
installGatewayTestHooks({ scope: "suite" });
|
||||||
|
|
||||||
const servers: Array<Awaited<ReturnType<typeof startServerWithClient>>> = [];
|
let server: Awaited<ReturnType<typeof startGatewayServer>>;
|
||||||
|
let port = 0;
|
||||||
|
let previousToken: string | undefined;
|
||||||
|
|
||||||
afterEach(async () => {
|
beforeAll(async () => {
|
||||||
for (const { server, ws } of servers) {
|
previousToken = process.env.CLAWDBOT_GATEWAY_TOKEN;
|
||||||
try {
|
delete process.env.CLAWDBOT_GATEWAY_TOKEN;
|
||||||
ws.close();
|
port = await getFreePort();
|
||||||
await server.close();
|
server = await startGatewayServer(port);
|
||||||
} catch {
|
|
||||||
/* ignore */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
servers.length = 0;
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await server.close();
|
||||||
|
if (previousToken === undefined) delete process.env.CLAWDBOT_GATEWAY_TOKEN;
|
||||||
|
else process.env.CLAWDBOT_GATEWAY_TOKEN = previousToken;
|
||||||
|
});
|
||||||
|
|
||||||
|
const openClient = async () => {
|
||||||
|
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
|
||||||
|
await new Promise<void>((resolve) => ws.once("open", resolve));
|
||||||
|
await connectOk(ws);
|
||||||
|
return ws;
|
||||||
|
};
|
||||||
|
|
||||||
describe("gateway config.apply", () => {
|
describe("gateway config.apply", () => {
|
||||||
it("writes config, stores sentinel, and schedules restart", async () => {
|
it("writes config, stores sentinel, and schedules restart", async () => {
|
||||||
const result = await startServerWithClient();
|
const ws = await openClient();
|
||||||
servers.push(result);
|
|
||||||
const { ws } = result;
|
|
||||||
await connectOk(ws);
|
|
||||||
|
|
||||||
const id = "req-1";
|
|
||||||
ws.send(
|
|
||||||
JSON.stringify({
|
|
||||||
type: "req",
|
|
||||||
id,
|
|
||||||
method: "config.apply",
|
|
||||||
params: {
|
|
||||||
raw: '{ "agents": { "list": [{ "id": "main", "workspace": "~/clawd" }] } }',
|
|
||||||
sessionKey: "agent:main:whatsapp:dm:+15555550123",
|
|
||||||
restartDelayMs: 0,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
const res = await onceMessage<{ ok: boolean; payload?: unknown }>(
|
|
||||||
ws,
|
|
||||||
(o) => o.type === "res" && o.id === id,
|
|
||||||
);
|
|
||||||
expect(res.ok).toBe(true);
|
|
||||||
|
|
||||||
// Verify sentinel file was created (restart was scheduled)
|
|
||||||
const sentinelPath = path.join(os.homedir(), ".clawdbot", "restart-sentinel.json");
|
|
||||||
|
|
||||||
// Wait for file to be written
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const raw = await fs.readFile(sentinelPath, "utf-8");
|
const id = "req-1";
|
||||||
const parsed = JSON.parse(raw) as { payload?: { kind?: string } };
|
ws.send(
|
||||||
expect(parsed.payload?.kind).toBe("config-apply");
|
JSON.stringify({
|
||||||
} catch {
|
type: "req",
|
||||||
// File may not exist if signal delivery is mocked, verify response was ok instead
|
id,
|
||||||
|
method: "config.apply",
|
||||||
|
params: {
|
||||||
|
raw: '{ "agents": { "list": [{ "id": "main", "workspace": "~/clawd" }] } }',
|
||||||
|
sessionKey: "agent:main:whatsapp:dm:+15555550123",
|
||||||
|
restartDelayMs: 0,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const res = await onceMessage<{ ok: boolean; payload?: unknown }>(
|
||||||
|
ws,
|
||||||
|
(o) => o.type === "res" && o.id === id,
|
||||||
|
);
|
||||||
expect(res.ok).toBe(true);
|
expect(res.ok).toBe(true);
|
||||||
|
|
||||||
|
// Verify sentinel file was created (restart was scheduled)
|
||||||
|
const sentinelPath = path.join(os.homedir(), ".clawdbot", "restart-sentinel.json");
|
||||||
|
|
||||||
|
// Wait for file to be written
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||||
|
|
||||||
|
try {
|
||||||
|
const raw = await fs.readFile(sentinelPath, "utf-8");
|
||||||
|
const parsed = JSON.parse(raw) as { payload?: { kind?: string } };
|
||||||
|
expect(parsed.payload?.kind).toBe("config-apply");
|
||||||
|
} catch {
|
||||||
|
// File may not exist if signal delivery is mocked, verify response was ok instead
|
||||||
|
expect(res.ok).toBe(true);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
ws.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("rejects invalid raw config", async () => {
|
it("rejects invalid raw config", async () => {
|
||||||
const result = await startServerWithClient();
|
const ws = await openClient();
|
||||||
servers.push(result);
|
try {
|
||||||
const { ws } = result;
|
const id = "req-2";
|
||||||
await connectOk(ws);
|
ws.send(
|
||||||
|
JSON.stringify({
|
||||||
const id = "req-2";
|
type: "req",
|
||||||
ws.send(
|
id,
|
||||||
JSON.stringify({
|
method: "config.apply",
|
||||||
type: "req",
|
params: {
|
||||||
id,
|
raw: "{",
|
||||||
method: "config.apply",
|
},
|
||||||
params: {
|
}),
|
||||||
raw: "{",
|
);
|
||||||
},
|
const res = await onceMessage<{ ok: boolean; error?: unknown }>(
|
||||||
}),
|
ws,
|
||||||
);
|
(o) => o.type === "res" && o.id === id,
|
||||||
const res = await onceMessage<{ ok: boolean; error?: unknown }>(
|
);
|
||||||
ws,
|
expect(res.ok).toBe(false);
|
||||||
(o) => o.type === "res" && o.id === id,
|
} finally {
|
||||||
);
|
ws.close();
|
||||||
expect(res.ok).toBe(false);
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -12,7 +12,7 @@ import {
|
|||||||
waitForSystemEvent,
|
waitForSystemEvent,
|
||||||
} from "./test-helpers.js";
|
} from "./test-helpers.js";
|
||||||
|
|
||||||
installGatewayTestHooks();
|
installGatewayTestHooks({ scope: "suite" });
|
||||||
|
|
||||||
async function yieldToEventLoop() {
|
async function yieldToEventLoop() {
|
||||||
// Avoid relying on timers (fake timers can leak between tests).
|
// Avoid relying on timers (fake timers can leak between tests).
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { describe, expect, test } from "vitest";
|
import { afterAll, beforeAll, describe, expect, test } from "vitest";
|
||||||
import { WebSocket } from "ws";
|
import { WebSocket } from "ws";
|
||||||
import { emitAgentEvent } from "../infra/agent-events.js";
|
import { emitAgentEvent } from "../infra/agent-events.js";
|
||||||
import {
|
import {
|
||||||
@@ -21,12 +21,35 @@ import {
|
|||||||
} from "./test-helpers.js";
|
} from "./test-helpers.js";
|
||||||
import { buildDeviceAuthPayload } from "./device-auth.js";
|
import { buildDeviceAuthPayload } from "./device-auth.js";
|
||||||
|
|
||||||
installGatewayTestHooks();
|
installGatewayTestHooks({ scope: "suite" });
|
||||||
|
|
||||||
|
let server: Awaited<ReturnType<typeof startGatewayServer>>;
|
||||||
|
let port = 0;
|
||||||
|
let previousToken: string | undefined;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
previousToken = process.env.CLAWDBOT_GATEWAY_TOKEN;
|
||||||
|
delete process.env.CLAWDBOT_GATEWAY_TOKEN;
|
||||||
|
port = await getFreePort();
|
||||||
|
server = await startGatewayServer(port);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await server.close();
|
||||||
|
if (previousToken === undefined) delete process.env.CLAWDBOT_GATEWAY_TOKEN;
|
||||||
|
else process.env.CLAWDBOT_GATEWAY_TOKEN = previousToken;
|
||||||
|
});
|
||||||
|
|
||||||
|
const openClient = async (opts?: Parameters<typeof connectOk>[1]) => {
|
||||||
|
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
|
||||||
|
await new Promise<void>((resolve) => ws.once("open", resolve));
|
||||||
|
await connectOk(ws, opts);
|
||||||
|
return ws;
|
||||||
|
};
|
||||||
|
|
||||||
describe("gateway server health/presence", () => {
|
describe("gateway server health/presence", () => {
|
||||||
test("connect + health + presence + status succeed", { timeout: 60_000 }, async () => {
|
test("connect + health + presence + status succeed", { timeout: 60_000 }, async () => {
|
||||||
const { server, ws } = await startServerWithClient();
|
const ws = await openClient();
|
||||||
await connectOk(ws);
|
|
||||||
|
|
||||||
const healthP = onceMessage(ws, (o) => o.type === "res" && o.id === "health1");
|
const healthP = onceMessage(ws, (o) => o.type === "res" && o.id === "health1");
|
||||||
const statusP = onceMessage(ws, (o) => o.type === "res" && o.id === "status1");
|
const statusP = onceMessage(ws, (o) => o.type === "res" && o.id === "status1");
|
||||||
@@ -51,7 +74,6 @@ describe("gateway server health/presence", () => {
|
|||||||
expect(Array.isArray(presence.payload)).toBe(true);
|
expect(Array.isArray(presence.payload)).toBe(true);
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("broadcasts heartbeat events and serves last-heartbeat", async () => {
|
test("broadcasts heartbeat events and serves last-heartbeat", async () => {
|
||||||
@@ -76,8 +98,7 @@ describe("gateway server health/presence", () => {
|
|||||||
payload?: unknown;
|
payload?: unknown;
|
||||||
};
|
};
|
||||||
|
|
||||||
const { server, ws } = await startServerWithClient();
|
const ws = await openClient();
|
||||||
await connectOk(ws);
|
|
||||||
|
|
||||||
const waitHeartbeat = onceMessage<EventFrame>(
|
const waitHeartbeat = onceMessage<EventFrame>(
|
||||||
ws,
|
ws,
|
||||||
@@ -117,12 +138,10 @@ describe("gateway server health/presence", () => {
|
|||||||
expect((toggle.payload as { enabled?: boolean } | undefined)?.enabled).toBe(false);
|
expect((toggle.payload as { enabled?: boolean } | undefined)?.enabled).toBe(false);
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("presence events carry seq + stateVersion", { timeout: 8000 }, async () => {
|
test("presence events carry seq + stateVersion", { timeout: 8000 }, async () => {
|
||||||
const { server, ws } = await startServerWithClient();
|
const ws = await openClient();
|
||||||
await connectOk(ws);
|
|
||||||
|
|
||||||
const presenceEventP = onceMessage(ws, (o) => o.type === "event" && o.event === "presence");
|
const presenceEventP = onceMessage(ws, (o) => o.type === "event" && o.event === "presence");
|
||||||
ws.send(
|
ws.send(
|
||||||
@@ -140,12 +159,10 @@ describe("gateway server health/presence", () => {
|
|||||||
expect(Array.isArray(evt.payload?.presence)).toBe(true);
|
expect(Array.isArray(evt.payload?.presence)).toBe(true);
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("agent events stream with seq", { timeout: 8000 }, async () => {
|
test("agent events stream with seq", { timeout: 8000 }, async () => {
|
||||||
const { server, ws } = await startServerWithClient();
|
const ws = await openClient();
|
||||||
await connectOk(ws);
|
|
||||||
|
|
||||||
const runId = randomUUID();
|
const runId = randomUUID();
|
||||||
const evtPromise = onceMessage(
|
const evtPromise = onceMessage(
|
||||||
@@ -163,7 +180,6 @@ describe("gateway server health/presence", () => {
|
|||||||
expect(evt.payload.data.msg).toBe("hi");
|
expect(evt.payload.data.msg).toBe("hi");
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("shutdown event is broadcast on close", { timeout: 8000 }, async () => {
|
test("shutdown event is broadcast on close", { timeout: 8000 }, async () => {
|
||||||
@@ -177,16 +193,7 @@ describe("gateway server health/presence", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("presence broadcast reaches multiple clients", { timeout: 8000 }, async () => {
|
test("presence broadcast reaches multiple clients", { timeout: 8000 }, async () => {
|
||||||
const port = await getFreePort();
|
const clients = await Promise.all([openClient(), openClient(), openClient()]);
|
||||||
const server = await startGatewayServer(port);
|
|
||||||
const mkClient = async () => {
|
|
||||||
const c = new WebSocket(`ws://127.0.0.1:${port}`);
|
|
||||||
await new Promise<void>((resolve) => c.once("open", resolve));
|
|
||||||
await connectOk(c);
|
|
||||||
return c;
|
|
||||||
};
|
|
||||||
|
|
||||||
const clients = await Promise.all([mkClient(), mkClient(), mkClient()]);
|
|
||||||
const waits = clients.map((c) =>
|
const waits = clients.map((c) =>
|
||||||
onceMessage(c, (o) => o.type === "event" && o.event === "presence"),
|
onceMessage(c, (o) => o.type === "event" && o.event === "presence"),
|
||||||
);
|
);
|
||||||
@@ -204,7 +211,6 @@ describe("gateway server health/presence", () => {
|
|||||||
expect(typeof evt.seq).toBe("number");
|
expect(typeof evt.seq).toBe("number");
|
||||||
}
|
}
|
||||||
for (const c of clients) c.close();
|
for (const c of clients) c.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("presence includes client fingerprint", async () => {
|
test("presence includes client fingerprint", async () => {
|
||||||
@@ -222,8 +228,7 @@ describe("gateway server health/presence", () => {
|
|||||||
signedAtMs,
|
signedAtMs,
|
||||||
token: null,
|
token: null,
|
||||||
});
|
});
|
||||||
const { server, ws } = await startServerWithClient();
|
const ws = await openClient({
|
||||||
await connectOk(ws, {
|
|
||||||
role,
|
role,
|
||||||
scopes,
|
scopes,
|
||||||
client: {
|
client: {
|
||||||
@@ -264,13 +269,11 @@ describe("gateway server health/presence", () => {
|
|||||||
expect(clientEntry?.modelIdentifier).toBe("iPad16,6");
|
expect(clientEntry?.modelIdentifier).toBe("iPad16,6");
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("cli connections are not tracked as instances", async () => {
|
test("cli connections are not tracked as instances", async () => {
|
||||||
const { server, ws } = await startServerWithClient();
|
|
||||||
const cliId = `cli-${randomUUID()}`;
|
const cliId = `cli-${randomUUID()}`;
|
||||||
await connectOk(ws, {
|
const ws = await openClient({
|
||||||
client: {
|
client: {
|
||||||
id: GATEWAY_CLIENT_NAMES.CLI,
|
id: GATEWAY_CLIENT_NAMES.CLI,
|
||||||
version: "dev",
|
version: "dev",
|
||||||
@@ -294,6 +297,5 @@ describe("gateway server health/presence", () => {
|
|||||||
expect(entries.some((e) => e.instanceId === cliId)).toBe(false);
|
expect(entries.some((e) => e.instanceId === cliId)).toBe(false);
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1,9 +1,21 @@
|
|||||||
import { test } from "vitest";
|
import { afterAll, beforeAll, test } from "vitest";
|
||||||
import WebSocket from "ws";
|
import WebSocket from "ws";
|
||||||
|
|
||||||
import { PROTOCOL_VERSION } from "./protocol/index.js";
|
import { PROTOCOL_VERSION } from "./protocol/index.js";
|
||||||
import { getFreePort, onceMessage, startGatewayServer } from "./test-helpers.server.js";
|
import { getFreePort, onceMessage, startGatewayServer } from "./test-helpers.server.js";
|
||||||
|
|
||||||
|
let server: Awaited<ReturnType<typeof startGatewayServer>>;
|
||||||
|
let port = 0;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
port = await getFreePort();
|
||||||
|
server = await startGatewayServer(port);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await server.close();
|
||||||
|
});
|
||||||
|
|
||||||
function connectReq(
|
function connectReq(
|
||||||
ws: WebSocket,
|
ws: WebSocket,
|
||||||
params: { clientId: string; platform: string; token?: string; password?: string },
|
params: { clientId: string; platform: string; token?: string; password?: string },
|
||||||
@@ -43,8 +55,6 @@ function connectReq(
|
|||||||
}
|
}
|
||||||
|
|
||||||
test("accepts clawdbot-ios as a valid gateway client id", async () => {
|
test("accepts clawdbot-ios as a valid gateway client id", async () => {
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startGatewayServer(port);
|
|
||||||
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
|
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
|
||||||
await new Promise<void>((resolve) => ws.once("open", resolve));
|
await new Promise<void>((resolve) => ws.once("open", resolve));
|
||||||
|
|
||||||
@@ -61,12 +71,9 @@ test("accepts clawdbot-ios as a valid gateway client id", async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("accepts clawdbot-android as a valid gateway client id", async () => {
|
test("accepts clawdbot-android as a valid gateway client id", async () => {
|
||||||
const port = await getFreePort();
|
|
||||||
const server = await startGatewayServer(port);
|
|
||||||
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
|
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
|
||||||
await new Promise<void>((resolve) => ws.once("open", resolve));
|
await new Promise<void>((resolve) => ws.once("open", resolve));
|
||||||
|
|
||||||
@@ -83,5 +90,4 @@ test("accepts clawdbot-android as a valid gateway client id", async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
import { afterAll, beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
|
||||||
|
import { WebSocket } from "ws";
|
||||||
import {
|
import {
|
||||||
connectOk,
|
connectOk,
|
||||||
embeddedRunMock,
|
embeddedRunMock,
|
||||||
|
getFreePort,
|
||||||
installGatewayTestHooks,
|
installGatewayTestHooks,
|
||||||
piSdkMock,
|
piSdkMock,
|
||||||
rpcReq,
|
rpcReq,
|
||||||
startServerWithClient,
|
startGatewayServer,
|
||||||
testState,
|
testState,
|
||||||
writeSessionStore,
|
writeSessionStore,
|
||||||
} from "./test-helpers.js";
|
} from "./test-helpers.js";
|
||||||
@@ -39,7 +41,31 @@ vi.mock("../auto-reply/reply/abort.js", async () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
installGatewayTestHooks();
|
installGatewayTestHooks({ scope: "suite" });
|
||||||
|
|
||||||
|
let server: Awaited<ReturnType<typeof startGatewayServer>>;
|
||||||
|
let port = 0;
|
||||||
|
let previousToken: string | undefined;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
previousToken = process.env.CLAWDBOT_GATEWAY_TOKEN;
|
||||||
|
delete process.env.CLAWDBOT_GATEWAY_TOKEN;
|
||||||
|
port = await getFreePort();
|
||||||
|
server = await startGatewayServer(port);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await server.close();
|
||||||
|
if (previousToken === undefined) delete process.env.CLAWDBOT_GATEWAY_TOKEN;
|
||||||
|
else process.env.CLAWDBOT_GATEWAY_TOKEN = previousToken;
|
||||||
|
});
|
||||||
|
|
||||||
|
const openClient = async (opts?: Parameters<typeof connectOk>[1]) => {
|
||||||
|
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
|
||||||
|
await new Promise<void>((resolve) => ws.once("open", resolve));
|
||||||
|
const hello = await connectOk(ws, opts);
|
||||||
|
return { ws, hello };
|
||||||
|
};
|
||||||
|
|
||||||
describe("gateway server sessions", () => {
|
describe("gateway server sessions", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -98,8 +124,7 @@ describe("gateway server sessions", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const { server, ws } = await startServerWithClient();
|
const { ws, hello } = await openClient();
|
||||||
const hello = await connectOk(ws);
|
|
||||||
expect((hello as unknown as { features?: { methods?: string[] } }).features?.methods).toEqual(
|
expect((hello as unknown as { features?: { methods?: string[] } }).features?.methods).toEqual(
|
||||||
expect.arrayContaining([
|
expect.arrayContaining([
|
||||||
"sessions.list",
|
"sessions.list",
|
||||||
@@ -336,7 +361,6 @@ describe("gateway server sessions", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("sessions.preview returns transcript previews", async () => {
|
test("sessions.preview returns transcript previews", async () => {
|
||||||
@@ -365,8 +389,7 @@ describe("gateway server sessions", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const { server, ws } = await startServerWithClient();
|
const { ws } = await openClient();
|
||||||
await connectOk(ws);
|
|
||||||
const preview = await rpcReq<{
|
const preview = await rpcReq<{
|
||||||
previews: Array<{
|
previews: Array<{
|
||||||
key: string;
|
key: string;
|
||||||
@@ -383,7 +406,6 @@ describe("gateway server sessions", () => {
|
|||||||
expect(entry?.items[1]?.text).toContain("call weather");
|
expect(entry?.items[1]?.text).toContain("call weather");
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("sessions.delete rejects main and aborts active runs", async () => {
|
test("sessions.delete rejects main and aborts active runs", async () => {
|
||||||
@@ -415,8 +437,7 @@ describe("gateway server sessions", () => {
|
|||||||
embeddedRunMock.activeIds.add("sess-active");
|
embeddedRunMock.activeIds.add("sess-active");
|
||||||
embeddedRunMock.waitResults.set("sess-active", true);
|
embeddedRunMock.waitResults.set("sess-active", true);
|
||||||
|
|
||||||
const { server, ws } = await startServerWithClient();
|
const { ws } = await openClient();
|
||||||
await connectOk(ws);
|
|
||||||
|
|
||||||
const mainDelete = await rpcReq(ws, "sessions.delete", { key: "main" });
|
const mainDelete = await rpcReq(ws, "sessions.delete", { key: "main" });
|
||||||
expect(mainDelete.ok).toBe(false);
|
expect(mainDelete.ok).toBe(false);
|
||||||
@@ -439,6 +460,5 @@ describe("gateway server sessions", () => {
|
|||||||
expect(embeddedRunMock.waitCalls).toEqual(["sess-active"]);
|
expect(embeddedRunMock.waitCalls).toEqual(["sess-active"]);
|
||||||
|
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -3,7 +3,7 @@ import { type AddressInfo, createServer } from "node:net";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
import { afterEach, beforeEach, expect, vi } from "vitest";
|
import { afterAll, afterEach, beforeAll, beforeEach, expect, vi } from "vitest";
|
||||||
import { WebSocket } from "ws";
|
import { WebSocket } from "ws";
|
||||||
|
|
||||||
import { resolveMainSessionKeyFromConfig, type SessionEntry } from "../config/sessions.js";
|
import { resolveMainSessionKeyFromConfig, type SessionEntry } from "../config/sessions.js";
|
||||||
@@ -75,67 +75,79 @@ export async function writeSessionStore(params: {
|
|||||||
await fs.writeFile(storePath, JSON.stringify(store, null, 2), "utf-8");
|
await fs.writeFile(storePath, JSON.stringify(store, null, 2), "utf-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function installGatewayTestHooks() {
|
async function setupGatewayTestHome() {
|
||||||
beforeEach(async () => {
|
previousHome = process.env.HOME;
|
||||||
// Some tests intentionally use fake timers; ensure they don't leak into gateway suites.
|
previousUserProfile = process.env.USERPROFILE;
|
||||||
vi.useRealTimers();
|
previousStateDir = process.env.CLAWDBOT_STATE_DIR;
|
||||||
setLoggerOverride({ level: "silent", consoleLevel: "silent" });
|
previousConfigPath = process.env.CLAWDBOT_CONFIG_PATH;
|
||||||
previousHome = process.env.HOME;
|
previousSkipBrowserControl = process.env.CLAWDBOT_SKIP_BROWSER_CONTROL_SERVER;
|
||||||
previousUserProfile = process.env.USERPROFILE;
|
previousSkipGmailWatcher = process.env.CLAWDBOT_SKIP_GMAIL_WATCHER;
|
||||||
previousStateDir = process.env.CLAWDBOT_STATE_DIR;
|
previousSkipCanvasHost = process.env.CLAWDBOT_SKIP_CANVAS_HOST;
|
||||||
previousConfigPath = process.env.CLAWDBOT_CONFIG_PATH;
|
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-gateway-home-"));
|
||||||
previousSkipBrowserControl = process.env.CLAWDBOT_SKIP_BROWSER_CONTROL_SERVER;
|
process.env.HOME = tempHome;
|
||||||
previousSkipGmailWatcher = process.env.CLAWDBOT_SKIP_GMAIL_WATCHER;
|
process.env.USERPROFILE = tempHome;
|
||||||
previousSkipCanvasHost = process.env.CLAWDBOT_SKIP_CANVAS_HOST;
|
process.env.CLAWDBOT_STATE_DIR = path.join(tempHome, ".clawdbot");
|
||||||
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-gateway-home-"));
|
delete process.env.CLAWDBOT_CONFIG_PATH;
|
||||||
process.env.HOME = tempHome;
|
}
|
||||||
process.env.USERPROFILE = tempHome;
|
|
||||||
process.env.CLAWDBOT_STATE_DIR = path.join(tempHome, ".clawdbot");
|
|
||||||
delete process.env.CLAWDBOT_CONFIG_PATH;
|
|
||||||
process.env.CLAWDBOT_SKIP_BROWSER_CONTROL_SERVER = "1";
|
|
||||||
process.env.CLAWDBOT_SKIP_GMAIL_WATCHER = "1";
|
|
||||||
process.env.CLAWDBOT_SKIP_CANVAS_HOST = "1";
|
|
||||||
tempConfigRoot = path.join(tempHome, ".clawdbot-test");
|
|
||||||
setTestConfigRoot(tempConfigRoot);
|
|
||||||
sessionStoreSaveDelayMs.value = 0;
|
|
||||||
testTailnetIPv4.value = undefined;
|
|
||||||
testState.gatewayBind = undefined;
|
|
||||||
testState.gatewayAuth = undefined;
|
|
||||||
testState.gatewayControlUi = undefined;
|
|
||||||
testState.hooksConfig = undefined;
|
|
||||||
testState.canvasHostPort = undefined;
|
|
||||||
testState.legacyIssues = [];
|
|
||||||
testState.legacyParsed = {};
|
|
||||||
testState.migrationConfig = null;
|
|
||||||
testState.migrationChanges = [];
|
|
||||||
testState.cronEnabled = false;
|
|
||||||
testState.cronStorePath = undefined;
|
|
||||||
testState.sessionConfig = undefined;
|
|
||||||
testState.sessionStorePath = undefined;
|
|
||||||
testState.agentConfig = undefined;
|
|
||||||
testState.agentsConfig = undefined;
|
|
||||||
testState.bindingsConfig = undefined;
|
|
||||||
testState.channelsConfig = undefined;
|
|
||||||
testState.allowFrom = undefined;
|
|
||||||
testIsNixMode.value = false;
|
|
||||||
cronIsolatedRun.mockClear();
|
|
||||||
agentCommand.mockClear();
|
|
||||||
embeddedRunMock.activeIds.clear();
|
|
||||||
embeddedRunMock.abortCalls = [];
|
|
||||||
embeddedRunMock.waitCalls = [];
|
|
||||||
embeddedRunMock.waitResults.clear();
|
|
||||||
drainSystemEvents(resolveMainSessionKeyFromConfig());
|
|
||||||
resetAgentRunContextForTest();
|
|
||||||
const mod = await serverModulePromise;
|
|
||||||
mod.__resetModelCatalogCacheForTest();
|
|
||||||
piSdkMock.enabled = false;
|
|
||||||
piSdkMock.discoverCalls = 0;
|
|
||||||
piSdkMock.models = [];
|
|
||||||
}, 60_000);
|
|
||||||
|
|
||||||
afterEach(async () => {
|
function applyGatewaySkipEnv() {
|
||||||
vi.useRealTimers();
|
process.env.CLAWDBOT_SKIP_BROWSER_CONTROL_SERVER = "1";
|
||||||
resetLogger();
|
process.env.CLAWDBOT_SKIP_GMAIL_WATCHER = "1";
|
||||||
|
process.env.CLAWDBOT_SKIP_CANVAS_HOST = "1";
|
||||||
|
}
|
||||||
|
|
||||||
|
async function resetGatewayTestState(options: { uniqueConfigRoot: boolean }) {
|
||||||
|
// Some tests intentionally use fake timers; ensure they don't leak into gateway suites.
|
||||||
|
vi.useRealTimers();
|
||||||
|
setLoggerOverride({ level: "silent", consoleLevel: "silent" });
|
||||||
|
if (!tempHome) {
|
||||||
|
throw new Error("resetGatewayTestState called before temp home was initialized");
|
||||||
|
}
|
||||||
|
applyGatewaySkipEnv();
|
||||||
|
tempConfigRoot = options.uniqueConfigRoot
|
||||||
|
? await fs.mkdtemp(path.join(tempHome, "clawdbot-test-"))
|
||||||
|
: path.join(tempHome, ".clawdbot-test");
|
||||||
|
setTestConfigRoot(tempConfigRoot);
|
||||||
|
sessionStoreSaveDelayMs.value = 0;
|
||||||
|
testTailnetIPv4.value = undefined;
|
||||||
|
testState.gatewayBind = undefined;
|
||||||
|
testState.gatewayAuth = undefined;
|
||||||
|
testState.gatewayControlUi = undefined;
|
||||||
|
testState.hooksConfig = undefined;
|
||||||
|
testState.canvasHostPort = undefined;
|
||||||
|
testState.legacyIssues = [];
|
||||||
|
testState.legacyParsed = {};
|
||||||
|
testState.migrationConfig = null;
|
||||||
|
testState.migrationChanges = [];
|
||||||
|
testState.cronEnabled = false;
|
||||||
|
testState.cronStorePath = undefined;
|
||||||
|
testState.sessionConfig = undefined;
|
||||||
|
testState.sessionStorePath = undefined;
|
||||||
|
testState.agentConfig = undefined;
|
||||||
|
testState.agentsConfig = undefined;
|
||||||
|
testState.bindingsConfig = undefined;
|
||||||
|
testState.channelsConfig = undefined;
|
||||||
|
testState.allowFrom = undefined;
|
||||||
|
testIsNixMode.value = false;
|
||||||
|
cronIsolatedRun.mockClear();
|
||||||
|
agentCommand.mockClear();
|
||||||
|
embeddedRunMock.activeIds.clear();
|
||||||
|
embeddedRunMock.abortCalls = [];
|
||||||
|
embeddedRunMock.waitCalls = [];
|
||||||
|
embeddedRunMock.waitResults.clear();
|
||||||
|
drainSystemEvents(resolveMainSessionKeyFromConfig());
|
||||||
|
resetAgentRunContextForTest();
|
||||||
|
const mod = await serverModulePromise;
|
||||||
|
mod.__resetModelCatalogCacheForTest();
|
||||||
|
piSdkMock.enabled = false;
|
||||||
|
piSdkMock.discoverCalls = 0;
|
||||||
|
piSdkMock.models = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function cleanupGatewayTestHome(options: { restoreEnv: boolean }) {
|
||||||
|
vi.useRealTimers();
|
||||||
|
resetLogger();
|
||||||
|
if (options.restoreEnv) {
|
||||||
if (previousHome === undefined) delete process.env.HOME;
|
if (previousHome === undefined) delete process.env.HOME;
|
||||||
else process.env.HOME = previousHome;
|
else process.env.HOME = previousHome;
|
||||||
if (previousUserProfile === undefined) delete process.env.USERPROFILE;
|
if (previousUserProfile === undefined) delete process.env.USERPROFILE;
|
||||||
@@ -151,16 +163,45 @@ export function installGatewayTestHooks() {
|
|||||||
else process.env.CLAWDBOT_SKIP_GMAIL_WATCHER = previousSkipGmailWatcher;
|
else process.env.CLAWDBOT_SKIP_GMAIL_WATCHER = previousSkipGmailWatcher;
|
||||||
if (previousSkipCanvasHost === undefined) delete process.env.CLAWDBOT_SKIP_CANVAS_HOST;
|
if (previousSkipCanvasHost === undefined) delete process.env.CLAWDBOT_SKIP_CANVAS_HOST;
|
||||||
else process.env.CLAWDBOT_SKIP_CANVAS_HOST = previousSkipCanvasHost;
|
else process.env.CLAWDBOT_SKIP_CANVAS_HOST = previousSkipCanvasHost;
|
||||||
if (tempHome) {
|
}
|
||||||
await fs.rm(tempHome, {
|
if (options.restoreEnv && tempHome) {
|
||||||
recursive: true,
|
await fs.rm(tempHome, {
|
||||||
force: true,
|
recursive: true,
|
||||||
maxRetries: 20,
|
force: true,
|
||||||
retryDelay: 25,
|
maxRetries: 20,
|
||||||
});
|
retryDelay: 25,
|
||||||
tempHome = undefined;
|
});
|
||||||
}
|
tempHome = undefined;
|
||||||
tempConfigRoot = undefined;
|
}
|
||||||
|
tempConfigRoot = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function installGatewayTestHooks(options?: { scope?: "test" | "suite" }) {
|
||||||
|
const scope = options?.scope ?? "test";
|
||||||
|
if (scope === "suite") {
|
||||||
|
beforeAll(async () => {
|
||||||
|
await setupGatewayTestHome();
|
||||||
|
await resetGatewayTestState({ uniqueConfigRoot: true });
|
||||||
|
});
|
||||||
|
beforeEach(async () => {
|
||||||
|
await resetGatewayTestState({ uniqueConfigRoot: true });
|
||||||
|
}, 60_000);
|
||||||
|
afterEach(async () => {
|
||||||
|
await cleanupGatewayTestHome({ restoreEnv: false });
|
||||||
|
});
|
||||||
|
afterAll(async () => {
|
||||||
|
await cleanupGatewayTestHome({ restoreEnv: true });
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await setupGatewayTestHome();
|
||||||
|
await resetGatewayTestState({ uniqueConfigRoot: false });
|
||||||
|
}, 60_000);
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await cleanupGatewayTestHome({ restoreEnv: true });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import type { WebClient } from "@slack/web-api";
|
||||||
|
|
||||||
|
import { readSlackMessages } from "./actions.js";
|
||||||
|
|
||||||
|
function createClient() {
|
||||||
|
return {
|
||||||
|
conversations: {
|
||||||
|
replies: vi.fn(async () => ({ messages: [], has_more: false })),
|
||||||
|
history: vi.fn(async () => ({ messages: [], has_more: false })),
|
||||||
|
},
|
||||||
|
} as unknown as WebClient & {
|
||||||
|
conversations: {
|
||||||
|
replies: ReturnType<typeof vi.fn>;
|
||||||
|
history: ReturnType<typeof vi.fn>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("readSlackMessages", () => {
|
||||||
|
it("uses conversations.replies and drops the parent message", async () => {
|
||||||
|
const client = createClient();
|
||||||
|
client.conversations.replies.mockResolvedValueOnce({
|
||||||
|
messages: [{ ts: "171234.567" }, { ts: "171234.890" }, { ts: "171235.000" }],
|
||||||
|
has_more: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await readSlackMessages("C1", {
|
||||||
|
client,
|
||||||
|
threadId: "171234.567",
|
||||||
|
token: "xoxb-test",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(client.conversations.replies).toHaveBeenCalledWith({
|
||||||
|
channel: "C1",
|
||||||
|
ts: "171234.567",
|
||||||
|
limit: undefined,
|
||||||
|
latest: undefined,
|
||||||
|
oldest: undefined,
|
||||||
|
});
|
||||||
|
expect(client.conversations.history).not.toHaveBeenCalled();
|
||||||
|
expect(result.messages.map((message) => message.ts)).toEqual(["171234.890", "171235.000"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses conversations.history when threadId is missing", async () => {
|
||||||
|
const client = createClient();
|
||||||
|
client.conversations.history.mockResolvedValueOnce({
|
||||||
|
messages: [{ ts: "1" }],
|
||||||
|
has_more: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await readSlackMessages("C1", {
|
||||||
|
client,
|
||||||
|
limit: 20,
|
||||||
|
token: "xoxb-test",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(client.conversations.history).toHaveBeenCalledWith({
|
||||||
|
channel: "C1",
|
||||||
|
limit: 20,
|
||||||
|
latest: undefined,
|
||||||
|
oldest: undefined,
|
||||||
|
});
|
||||||
|
expect(client.conversations.replies).not.toHaveBeenCalled();
|
||||||
|
expect(result.messages.map((message) => message.ts)).toEqual(["1"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -186,9 +186,29 @@ export async function readSlackMessages(
|
|||||||
limit?: number;
|
limit?: number;
|
||||||
before?: string;
|
before?: string;
|
||||||
after?: string;
|
after?: string;
|
||||||
|
threadId?: string;
|
||||||
} = {},
|
} = {},
|
||||||
): Promise<{ messages: SlackMessageSummary[]; hasMore: boolean }> {
|
): Promise<{ messages: SlackMessageSummary[]; hasMore: boolean }> {
|
||||||
const client = await getClient(opts);
|
const client = await getClient(opts);
|
||||||
|
|
||||||
|
// Use conversations.replies for thread messages, conversations.history for channel messages.
|
||||||
|
if (opts.threadId) {
|
||||||
|
const result = await client.conversations.replies({
|
||||||
|
channel: channelId,
|
||||||
|
ts: opts.threadId,
|
||||||
|
limit: opts.limit,
|
||||||
|
latest: opts.before,
|
||||||
|
oldest: opts.after,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
// conversations.replies includes the parent message; drop it for replies-only reads.
|
||||||
|
messages: (result.messages ?? []).filter(
|
||||||
|
(message) => (message as SlackMessageSummary)?.ts !== opts.threadId,
|
||||||
|
) as SlackMessageSummary[],
|
||||||
|
hasMore: Boolean(result.has_more),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const result = await client.conversations.history({
|
const result = await client.conversations.history({
|
||||||
channel: channelId,
|
channel: channelId,
|
||||||
limit: opts.limit,
|
limit: opts.limit,
|
||||||
|
|||||||
Reference in new issue
Block a user