@workclaw/openclaw-workchat 1.0.0 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,84 +1,84 @@
1
- import {
2
- fireAndForgetHook,
3
- buildCanonicalSentMessageHookContext,
4
- toPluginMessageContext,
5
- toPluginMessageSentEvent,
6
- } from "openclaw/plugin-sdk/hook-runtime";
7
- import { getGlobalHookRunner } from "openclaw/plugin-sdk/plugin-runtime";
8
-
9
- import { logger } from "../util/logger.js";
10
-
11
- const CHANNEL_ID = "openclaw-workchat";
12
-
13
- /**
14
- * Run message_sending hook before sending.
15
- * Returns the (possibly modified) text content plus a cancelled flag.
16
- * Hook errors are caught and logged — sending proceeds regardless.
17
- */
18
- export async function applyWorkChatMessageSendingHook(params: {
19
- to: string;
20
- text: string;
21
- accountId?: string;
22
- mediaUrl?: string;
23
- }): Promise<{ cancelled: boolean; text: string }> {
24
- const hookRunner = getGlobalHookRunner();
25
- if (!hookRunner?.hasHooks("message_sending")) {
26
- return { cancelled: false, text: params.text };
27
- }
28
- try {
29
- const hookResult = await hookRunner.runMessageSending(
30
- {
31
- to: params.to,
32
- content: params.text,
33
- metadata: {
34
- channel: CHANNEL_ID,
35
- accountId: params.accountId,
36
- ...(params.mediaUrl ? { mediaUrls: [params.mediaUrl] } : {}),
37
- },
38
- },
39
- { channelId: CHANNEL_ID, accountId: params.accountId },
40
- );
41
- if (hookResult?.cancel) {
42
- return { cancelled: true, text: params.text };
43
- }
44
- return {
45
- cancelled: false,
46
- text: hookResult?.content ?? params.text,
47
- };
48
- } catch (err) {
49
- logger.warn(`message_sending hook error, proceeding with send: ${String(err)}`);
50
- return { cancelled: false, text: params.text };
51
- }
52
- }
53
-
54
- /**
55
- * Fire message_sent hook (fire-and-forget) after a send attempt.
56
- */
57
- export function emitWorkChatMessageSent(params: {
58
- to: string;
59
- content: string;
60
- success: boolean;
61
- error?: string;
62
- accountId?: string;
63
- }): void {
64
- const hookRunner = getGlobalHookRunner();
65
- if (!hookRunner?.hasHooks("message_sent")) return;
66
- const canonical = buildCanonicalSentMessageHookContext({
67
- to: params.to,
68
- content: params.content,
69
- success: params.success,
70
- error: params.error,
71
- channelId: CHANNEL_ID,
72
- accountId: params.accountId,
73
- conversationId: params.to,
74
- });
75
- fireAndForgetHook(
76
- Promise.resolve(
77
- hookRunner!.runMessageSent(
78
- toPluginMessageSentEvent(canonical),
79
- toPluginMessageContext(canonical),
80
- ),
81
- ),
82
- "workchat: message_sent plugin hook failed",
83
- );
84
- }
1
+ import {
2
+ fireAndForgetHook,
3
+ buildCanonicalSentMessageHookContext,
4
+ toPluginMessageContext,
5
+ toPluginMessageSentEvent,
6
+ } from "openclaw/plugin-sdk/hook-runtime";
7
+ import { getGlobalHookRunner } from "openclaw/plugin-sdk/plugin-runtime";
8
+
9
+ import { logger } from "../util/logger.js";
10
+
11
+ const CHANNEL_ID = "openclaw-workchat";
12
+
13
+ /**
14
+ * Run message_sending hook before sending.
15
+ * Returns the (possibly modified) text content plus a cancelled flag.
16
+ * Hook errors are caught and logged — sending proceeds regardless.
17
+ */
18
+ export async function applyWorkChatMessageSendingHook(params: {
19
+ to: string;
20
+ text: string;
21
+ accountId?: string;
22
+ mediaUrl?: string;
23
+ }): Promise<{ cancelled: boolean; text: string }> {
24
+ const hookRunner = getGlobalHookRunner();
25
+ if (!hookRunner?.hasHooks("message_sending")) {
26
+ return { cancelled: false, text: params.text };
27
+ }
28
+ try {
29
+ const hookResult = await hookRunner.runMessageSending(
30
+ {
31
+ to: params.to,
32
+ content: params.text,
33
+ metadata: {
34
+ channel: CHANNEL_ID,
35
+ accountId: params.accountId,
36
+ ...(params.mediaUrl ? { mediaUrls: [params.mediaUrl] } : {}),
37
+ },
38
+ },
39
+ { channelId: CHANNEL_ID, accountId: params.accountId },
40
+ );
41
+ if (hookResult?.cancel) {
42
+ return { cancelled: true, text: params.text };
43
+ }
44
+ return {
45
+ cancelled: false,
46
+ text: hookResult?.content ?? params.text,
47
+ };
48
+ } catch (err) {
49
+ logger.warn(`message_sending hook error, proceeding with send: ${String(err)}`);
50
+ return { cancelled: false, text: params.text };
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Fire message_sent hook (fire-and-forget) after a send attempt.
56
+ */
57
+ export function emitWorkChatMessageSent(params: {
58
+ to: string;
59
+ content: string;
60
+ success: boolean;
61
+ error?: string;
62
+ accountId?: string;
63
+ }): void {
64
+ const hookRunner = getGlobalHookRunner();
65
+ if (!hookRunner?.hasHooks("message_sent")) return;
66
+ const canonical = buildCanonicalSentMessageHookContext({
67
+ to: params.to,
68
+ content: params.content,
69
+ success: params.success,
70
+ error: params.error,
71
+ channelId: CHANNEL_ID,
72
+ accountId: params.accountId,
73
+ conversationId: params.to,
74
+ });
75
+ fireAndForgetHook(
76
+ Promise.resolve(
77
+ hookRunner!.runMessageSent(
78
+ toPluginMessageSentEvent(canonical),
79
+ toPluginMessageContext(canonical),
80
+ ),
81
+ ),
82
+ "workchat: message_sent plugin hook failed",
83
+ );
84
+ }