mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-27 12:56:01 -06:00
refactor(hooks): remove unused message event guards (#129515)
Amp-Thread-ID: https://ampcode.com/threads/T-01a037b7-8aa3-72eb-95f7-517a508319f6 Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
committed by
GitHub
parent
063f8ccb10
commit
f5990fc124
@@ -9,8 +9,6 @@ import {
|
||||
getRegisteredEventKeys,
|
||||
isAgentBootstrapEvent,
|
||||
isGatewayStartupEvent,
|
||||
isMessageReceivedEvent,
|
||||
isMessageSentEvent,
|
||||
registerInternalHook,
|
||||
setInternalHooksEnabled,
|
||||
triggerInternalHook,
|
||||
@@ -251,151 +249,6 @@ describe("hooks", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("isMessageReceivedEvent", () => {
|
||||
it.each([
|
||||
{
|
||||
name: "returns true for message:received events with expected context",
|
||||
event: createInternalHookEvent("message", "received", "test-session", {
|
||||
from: "+1234567890",
|
||||
content: "Hello world",
|
||||
channelId: "whatsapp",
|
||||
conversationId: "chat-123",
|
||||
timestamp: Date.now(),
|
||||
} satisfies MessageReceivedHookContext),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "returns false for message:sent events",
|
||||
event: createInternalHookEvent("message", "sent", "test-session", {
|
||||
to: "+1234567890",
|
||||
content: "Hello world",
|
||||
success: true,
|
||||
channelId: "whatsapp",
|
||||
} satisfies MessageSentHookContext),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "returns false when content is missing",
|
||||
event: createInternalHookEvent("message", "received", "test-session", {
|
||||
from: "+1234567890",
|
||||
channelId: "whatsapp",
|
||||
}),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "returns false when content is not a string",
|
||||
event: createInternalHookEvent("message", "received", "test-session", {
|
||||
from: "+1234567890",
|
||||
content: 123,
|
||||
channelId: "whatsapp",
|
||||
}),
|
||||
expected: false,
|
||||
},
|
||||
] satisfies Array<{
|
||||
name: string;
|
||||
event: ReturnType<typeof createInternalHookEvent>;
|
||||
expected: boolean;
|
||||
}>)("$name", ({ event, expected }) => {
|
||||
expect(isMessageReceivedEvent(event)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isMessageSentEvent", () => {
|
||||
it.each([
|
||||
{
|
||||
name: "returns true for message:sent events with expected context",
|
||||
event: createInternalHookEvent("message", "sent", "test-session", {
|
||||
to: "+1234567890",
|
||||
content: "Hello world",
|
||||
success: true,
|
||||
channelId: "telegram",
|
||||
conversationId: "chat-456",
|
||||
messageId: "msg-789",
|
||||
} satisfies MessageSentHookContext),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "returns true when success is false (error case)",
|
||||
event: createInternalHookEvent("message", "sent", "test-session", {
|
||||
to: "+1234567890",
|
||||
content: "Hello world",
|
||||
success: false,
|
||||
error: "Network error",
|
||||
channelId: "whatsapp",
|
||||
} satisfies MessageSentHookContext),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "returns false for message:received events",
|
||||
event: createInternalHookEvent("message", "received", "test-session", {
|
||||
from: "+1234567890",
|
||||
content: "Hello world",
|
||||
channelId: "whatsapp",
|
||||
} satisfies MessageReceivedHookContext),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "returns false when content is missing",
|
||||
event: createInternalHookEvent("message", "sent", "test-session", {
|
||||
to: "+1234567890",
|
||||
success: true,
|
||||
channelId: "telegram",
|
||||
}),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "returns false when content is not a string",
|
||||
event: createInternalHookEvent("message", "sent", "test-session", {
|
||||
to: "+1234567890",
|
||||
content: false,
|
||||
success: true,
|
||||
channelId: "telegram",
|
||||
}),
|
||||
expected: false,
|
||||
},
|
||||
] satisfies Array<{
|
||||
name: string;
|
||||
event: ReturnType<typeof createInternalHookEvent>;
|
||||
expected: boolean;
|
||||
}>)("$name", ({ event, expected }) => {
|
||||
expect(isMessageSentEvent(event)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("message type-guard shared negatives", () => {
|
||||
it("returns false for non-message and missing-context shapes", () => {
|
||||
const cases = [
|
||||
{
|
||||
match: isMessageReceivedEvent,
|
||||
},
|
||||
{
|
||||
match: isMessageSentEvent,
|
||||
},
|
||||
] as const;
|
||||
const nonMessageEvent = createInternalHookEvent("command", "new", "test-session");
|
||||
const missingReceivedContext = createInternalHookEvent(
|
||||
"message",
|
||||
"received",
|
||||
"test-session",
|
||||
{
|
||||
from: "+1234567890",
|
||||
// missing channelId
|
||||
},
|
||||
);
|
||||
const missingSentContext = createInternalHookEvent("message", "sent", "test-session", {
|
||||
to: "+1234567890",
|
||||
channelId: "whatsapp",
|
||||
// missing success
|
||||
});
|
||||
|
||||
for (const { match } of cases) {
|
||||
expect(match(nonMessageEvent)).toBe(false);
|
||||
}
|
||||
expect(isMessageReceivedEvent(missingReceivedContext)).toBe(false);
|
||||
expect(isMessageSentEvent(missingSentContext)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("message hooks", () => {
|
||||
it("should trigger message:received handlers", async () => {
|
||||
const handler = vi.fn();
|
||||
|
||||
@@ -82,12 +82,6 @@ export type MessageReceivedHookContext = {
|
||||
metadata?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export type MessageReceivedHookEvent = InternalHookEvent & {
|
||||
type: "message";
|
||||
action: "received";
|
||||
context: MessageReceivedHookContext;
|
||||
};
|
||||
|
||||
export type MessageSentHookContext = {
|
||||
/** Recipient identifier */
|
||||
to: string;
|
||||
@@ -111,12 +105,6 @@ export type MessageSentHookContext = {
|
||||
groupId?: string;
|
||||
};
|
||||
|
||||
export type MessageSentHookEvent = InternalHookEvent & {
|
||||
type: "message";
|
||||
action: "sent";
|
||||
context: MessageSentHookContext;
|
||||
};
|
||||
|
||||
type MessageEnrichedBodyHookContext = {
|
||||
/** Sender identifier (e.g., phone number, user ID) */
|
||||
from?: string;
|
||||
@@ -161,12 +149,6 @@ export type MessageTranscribedHookContext = MessageEnrichedBodyHookContext & {
|
||||
transcript: string;
|
||||
};
|
||||
|
||||
export type MessageTranscribedHookEvent = InternalHookEvent & {
|
||||
type: "message";
|
||||
action: "transcribed";
|
||||
context: MessageTranscribedHookContext;
|
||||
};
|
||||
|
||||
export type MessagePreprocessedHookContext = MessageEnrichedBodyHookContext & {
|
||||
/** Transcribed audio text, if the message contained audio */
|
||||
transcript?: string;
|
||||
@@ -176,12 +158,6 @@ export type MessagePreprocessedHookContext = MessageEnrichedBodyHookContext & {
|
||||
groupId?: string;
|
||||
};
|
||||
|
||||
export type MessagePreprocessedHookEvent = InternalHookEvent & {
|
||||
type: "message";
|
||||
action: "preprocessed";
|
||||
context: MessagePreprocessedHookContext;
|
||||
};
|
||||
|
||||
export type SessionPatchHookContext = {
|
||||
sessionEntry: SessionEntry;
|
||||
patch: SessionsPatchParams;
|
||||
@@ -383,13 +359,6 @@ function hasStringContextField<T extends Record<string, unknown>>(
|
||||
return typeof context[key] === "string";
|
||||
}
|
||||
|
||||
function hasBooleanContextField<T extends Record<string, unknown>>(
|
||||
context: Partial<T>,
|
||||
key: keyof T,
|
||||
): boolean {
|
||||
return typeof context[key] === "boolean";
|
||||
}
|
||||
|
||||
export function isAgentBootstrapEvent(event: InternalHookEvent): event is AgentBootstrapHookEvent {
|
||||
if (!isHookEventTypeAndAction(event, "agent", "bootstrap")) {
|
||||
return false;
|
||||
@@ -411,67 +380,6 @@ export function isGatewayStartupEvent(event: InternalHookEvent): event is Gatewa
|
||||
return Boolean(getHookContext<GatewayStartupHookContext>(event));
|
||||
}
|
||||
|
||||
export function isMessageReceivedEvent(
|
||||
event: InternalHookEvent,
|
||||
): event is MessageReceivedHookEvent {
|
||||
if (!isHookEventTypeAndAction(event, "message", "received")) {
|
||||
return false;
|
||||
}
|
||||
const context = getHookContext<MessageReceivedHookContext>(event);
|
||||
if (!context) {
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
hasStringContextField(context, "from") &&
|
||||
hasStringContextField(context, "content") &&
|
||||
hasStringContextField(context, "channelId")
|
||||
);
|
||||
}
|
||||
|
||||
export function isMessageSentEvent(event: InternalHookEvent): event is MessageSentHookEvent {
|
||||
if (!isHookEventTypeAndAction(event, "message", "sent")) {
|
||||
return false;
|
||||
}
|
||||
const context = getHookContext<MessageSentHookContext>(event);
|
||||
if (!context) {
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
hasStringContextField(context, "to") &&
|
||||
hasStringContextField(context, "content") &&
|
||||
hasStringContextField(context, "channelId") &&
|
||||
hasBooleanContextField(context, "success")
|
||||
);
|
||||
}
|
||||
|
||||
export function isMessageTranscribedEvent(
|
||||
event: InternalHookEvent,
|
||||
): event is MessageTranscribedHookEvent {
|
||||
if (!isHookEventTypeAndAction(event, "message", "transcribed")) {
|
||||
return false;
|
||||
}
|
||||
const context = getHookContext<MessageTranscribedHookContext>(event);
|
||||
if (!context) {
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
hasStringContextField(context, "transcript") && hasStringContextField(context, "channelId")
|
||||
);
|
||||
}
|
||||
|
||||
export function isMessagePreprocessedEvent(
|
||||
event: InternalHookEvent,
|
||||
): event is MessagePreprocessedHookEvent {
|
||||
if (!isHookEventTypeAndAction(event, "message", "preprocessed")) {
|
||||
return false;
|
||||
}
|
||||
const context = getHookContext<MessagePreprocessedHookContext>(event);
|
||||
if (!context) {
|
||||
return false;
|
||||
}
|
||||
return hasStringContextField(context, "channelId");
|
||||
}
|
||||
|
||||
export function isSessionPatchEvent(event: InternalHookEvent): event is SessionPatchHookEvent {
|
||||
if (!isHookEventTypeAndAction(event, "session", "patch")) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user