@buz-extensions/buz 1.0.0-beta.13 → 1.0.0-beta.15
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.
- package/index.ts +10 -6
- package/package.json +1 -1
- package/src/inbound.ts +78 -41
package/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { ChannelPlugin } from "openclaw/plugin-sdk/line";
|
|
1
|
+
import type { ChannelPlugin, OpenClawPluginApi } from "openclaw/plugin-sdk/line";
|
|
3
2
|
import { emptyPluginConfigSchema, buildChannelConfigSchema } from "openclaw/plugin-sdk/line";
|
|
4
3
|
import { z } from "zod";
|
|
5
4
|
|
|
@@ -179,11 +178,16 @@ export const buzChannelPlugin = {
|
|
|
179
178
|
},
|
|
180
179
|
} as any;
|
|
181
180
|
|
|
182
|
-
|
|
181
|
+
const plugin = {
|
|
183
182
|
id: "buz",
|
|
184
183
|
name: "buz Plugin",
|
|
185
184
|
description: "Connects OpenClaw to buz",
|
|
186
|
-
plugin: buzChannelPlugin as ChannelPlugin,
|
|
187
185
|
configSchema: emptyPluginConfigSchema(),
|
|
188
|
-
|
|
189
|
-
|
|
186
|
+
register(api: OpenClawPluginApi) {
|
|
187
|
+
setBuzRuntime(api.runtime);
|
|
188
|
+
api.registerChannel({ plugin: buzChannelPlugin as ChannelPlugin });
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export { setBuzRuntime, getBuzRuntime, buzChannelPlugin };
|
|
193
|
+
export default plugin;
|
package/package.json
CHANGED
package/src/inbound.ts
CHANGED
|
@@ -14,7 +14,13 @@ function resolveDefaultAgentIdCompat(cfg: any): string {
|
|
|
14
14
|
return firstAgentId || "default";
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
function resolveStorePath(cfg: any): string {
|
|
17
|
+
function resolveStorePath(cfg: any, agentId: string): string {
|
|
18
|
+
const core = getBuzRuntime();
|
|
19
|
+
const resolver = core?.channel?.session?.resolveStorePath;
|
|
20
|
+
if (typeof resolver === "function") {
|
|
21
|
+
return resolver(cfg?.session?.store, { agentId });
|
|
22
|
+
}
|
|
23
|
+
|
|
18
24
|
const configuredPath = cfg?.session?.storePath || ".openclaw/sessions";
|
|
19
25
|
if (configuredPath.startsWith("/") || configuredPath.startsWith("~")) {
|
|
20
26
|
return configuredPath.replace(/^~/, homedir());
|
|
@@ -22,6 +28,27 @@ function resolveStorePath(cfg: any): string {
|
|
|
22
28
|
return resolve(homedir(), configuredPath);
|
|
23
29
|
}
|
|
24
30
|
|
|
31
|
+
function buildSessionKey(params: {
|
|
32
|
+
core: any;
|
|
33
|
+
agentId: string;
|
|
34
|
+
accountId: string;
|
|
35
|
+
isGroup: boolean;
|
|
36
|
+
conversationId: string;
|
|
37
|
+
}) {
|
|
38
|
+
const { core, agentId, accountId, isGroup, conversationId } = params;
|
|
39
|
+
const buildAgentSessionKey = core?.channel?.routing?.buildAgentSessionKey;
|
|
40
|
+
if (typeof buildAgentSessionKey === "function") {
|
|
41
|
+
return buildAgentSessionKey({
|
|
42
|
+
agentId,
|
|
43
|
+
channel: "buz",
|
|
44
|
+
accountId,
|
|
45
|
+
chatType: isGroup ? "group" : "direct",
|
|
46
|
+
conversationId,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return `agent:${agentId}:buz:${accountId}:${isGroup ? "group" : "direct"}:${conversationId}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
25
52
|
export async function handleInboundMessage(ctx: any, inboundMsg: any) {
|
|
26
53
|
console.log("[buz inbound] =========================================");
|
|
27
54
|
console.log("[buz inbound] handleInboundMessage called");
|
|
@@ -30,69 +57,79 @@ export async function handleInboundMessage(ctx: any, inboundMsg: any) {
|
|
|
30
57
|
|
|
31
58
|
const accountId = ctx.account.accountId;
|
|
32
59
|
const cfg = ctx.cfg;
|
|
60
|
+
const core = getBuzRuntime().core;
|
|
33
61
|
const agentId = resolveDefaultAgentIdCompat(cfg);
|
|
34
62
|
|
|
35
63
|
console.log("[buz inbound] accountId:", accountId);
|
|
36
64
|
console.log("[buz inbound] agentId:", agentId);
|
|
37
65
|
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
66
|
+
const isGroup = inboundMsg.chat_type === "group";
|
|
67
|
+
const senderId = String(inboundMsg.sender_id || "").trim();
|
|
68
|
+
const senderName = String(inboundMsg.sender_name || inboundMsg.sender_id || "").trim() || undefined;
|
|
69
|
+
const conversationId = String(isGroup ? inboundMsg.group_id : inboundMsg.sender_id || "").trim();
|
|
70
|
+
const rawBody = String(inboundMsg.content_text || "");
|
|
71
|
+
const messageSid = String(inboundMsg.message_id || Date.now().toString());
|
|
42
72
|
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
73
|
+
const fromTarget = isGroup ? `buz:group:${conversationId}:${senderId}` : `buz:${senderId}`;
|
|
74
|
+
const toTarget = isGroup ? `buz:group:${conversationId}` : `buz:${senderId}`;
|
|
75
|
+
const sessionKey = buildSessionKey({
|
|
76
|
+
core,
|
|
77
|
+
agentId,
|
|
78
|
+
accountId,
|
|
79
|
+
isGroup,
|
|
80
|
+
conversationId,
|
|
81
|
+
});
|
|
50
82
|
|
|
51
83
|
console.log("[buz inbound] fromTarget:", fromTarget);
|
|
52
84
|
console.log("[buz inbound] toTarget:", toTarget);
|
|
53
85
|
console.log("[buz inbound] conversationId:", conversationId);
|
|
86
|
+
console.log("[buz inbound] sessionKey:", sessionKey);
|
|
54
87
|
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
88
|
+
const finalizeInboundContext = core?.channel?.reply?.finalizeInboundContext;
|
|
89
|
+
const baseCtxPayload = {
|
|
90
|
+
MessageSid: messageSid,
|
|
91
|
+
MessageSids: [messageSid],
|
|
92
|
+
SessionKey: sessionKey,
|
|
59
93
|
ConversationId: conversationId,
|
|
60
94
|
From: fromTarget,
|
|
61
95
|
To: toTarget,
|
|
62
|
-
Body:
|
|
63
|
-
BodyForAgent:
|
|
64
|
-
BodyForCommands:
|
|
96
|
+
Body: rawBody,
|
|
97
|
+
BodyForAgent: rawBody,
|
|
98
|
+
BodyForCommands: rawBody,
|
|
65
99
|
Channel: "buz",
|
|
66
|
-
|
|
100
|
+
Provider: "buz",
|
|
101
|
+
Surface: "buz",
|
|
102
|
+
OriginatingChannel: "buz",
|
|
103
|
+
OriginatingTo: toTarget,
|
|
104
|
+
AccountId: accountId,
|
|
105
|
+
ChatType: isGroup ? "group" : "direct",
|
|
106
|
+
SenderName: senderName,
|
|
107
|
+
SenderId: senderId || undefined,
|
|
67
108
|
};
|
|
109
|
+
const ctxPayload =
|
|
110
|
+
typeof finalizeInboundContext === "function"
|
|
111
|
+
? finalizeInboundContext(baseCtxPayload)
|
|
112
|
+
: baseCtxPayload;
|
|
68
113
|
|
|
69
114
|
console.log("[buz inbound] ctxPayload:", JSON.stringify(ctxPayload, null, 2));
|
|
70
115
|
|
|
71
|
-
const storePath = resolveStorePath(cfg);
|
|
116
|
+
const storePath = resolveStorePath(cfg, agentId);
|
|
72
117
|
console.log("[buz inbound] storePath:", storePath);
|
|
73
|
-
console.log("[buz inbound]
|
|
118
|
+
console.log("[buz inbound] dispatching via recordInboundSessionAndDispatchReply...");
|
|
74
119
|
|
|
75
120
|
try {
|
|
76
|
-
console.log("[buz inbound] dispatching via recordInboundSessionAndDispatchReply...");
|
|
77
|
-
|
|
78
|
-
// Get core from runtime
|
|
79
|
-
const core = getBuzRuntime().core;
|
|
80
|
-
|
|
81
121
|
await recordInboundSessionAndDispatchReply({
|
|
82
|
-
|
|
83
|
-
dispatchReplyWithBufferedBlockDispatcher: core.channel.reply.dispatchReplyWithBufferedBlockDispatcher,
|
|
84
|
-
storePath,
|
|
85
|
-
ctxPayload,
|
|
86
|
-
agentId,
|
|
122
|
+
cfg,
|
|
87
123
|
channel: "buz",
|
|
88
124
|
accountId,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
125
|
+
agentId,
|
|
126
|
+
routeSessionKey: sessionKey,
|
|
127
|
+
storePath,
|
|
128
|
+
ctxPayload,
|
|
129
|
+
recordInboundSession: core.channel.session.recordInboundSession,
|
|
130
|
+
dispatchReplyWithBufferedBlockDispatcher: core.channel.reply.dispatchReplyWithBufferedBlockDispatcher,
|
|
131
|
+
deliver: async (payload: any) => {
|
|
132
|
+
console.log("[buz inbound] deliver called text:", payload?.text?.substring?.(0, 50));
|
|
96
133
|
if (!payload?.text) {
|
|
97
134
|
return;
|
|
98
135
|
}
|
|
@@ -100,7 +137,7 @@ export async function handleInboundMessage(ctx: any, inboundMsg: any) {
|
|
|
100
137
|
to: toTarget,
|
|
101
138
|
text: payload.text,
|
|
102
139
|
accountId,
|
|
103
|
-
replyToId:
|
|
140
|
+
replyToId: messageSid,
|
|
104
141
|
});
|
|
105
142
|
console.log("[buz inbound] reply sent successfully via gRPC");
|
|
106
143
|
},
|
|
@@ -114,7 +151,7 @@ export async function handleInboundMessage(ctx: any, inboundMsg: any) {
|
|
|
114
151
|
disableBlockStreaming: true,
|
|
115
152
|
},
|
|
116
153
|
});
|
|
117
|
-
|
|
154
|
+
|
|
118
155
|
console.log("[buz inbound] message dispatched successfully");
|
|
119
156
|
ctx.log?.info?.(
|
|
120
157
|
`[${accountId}] Successfully dispatched inbound message from ${inboundMsg.sender_id}`,
|