@cloudbase/agent-adapter-wx 1.0.1-alpha.25-1 → 1.0.1-alpha.25-2
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/dist/index.d.mts +7 -7
- package/dist/index.d.ts +7 -7
- package/dist/index.js +97 -33
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +95 -31
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Request, Response } from 'express';
|
|
2
2
|
import { AbstractAgent, AgentConfig, BaseEvent, RunAgentInput } from '@ag-ui/client';
|
|
3
3
|
import { Observable } from 'rxjs';
|
|
4
|
-
import { WxSendMessageInput } from '@cloudbase/aiagent-framework';
|
|
4
|
+
import { WxSendMessageInput, aitools } from '@cloudbase/aiagent-framework';
|
|
5
5
|
import { Logger } from '@cloudbase/agent-shared';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -236,8 +236,8 @@ interface WeChatReplyMessage {
|
|
|
236
236
|
createTime: number;
|
|
237
237
|
msgType: string;
|
|
238
238
|
content: string;
|
|
239
|
+
msgId: string;
|
|
239
240
|
openKfId?: string;
|
|
240
|
-
msgId?: string;
|
|
241
241
|
}
|
|
242
242
|
/**
|
|
243
243
|
* Format reply message for WeChat
|
|
@@ -465,13 +465,13 @@ interface IWxRunAgentInput extends RunAgentInput {
|
|
|
465
465
|
declare class WeChatAgent extends AbstractAgent {
|
|
466
466
|
private wrappedAgent;
|
|
467
467
|
private wechatSender?;
|
|
468
|
-
private aitools?;
|
|
469
468
|
private wechatConfig;
|
|
470
469
|
private _historyManager;
|
|
471
470
|
private messageFormatter;
|
|
472
471
|
private eventFilter;
|
|
473
472
|
private messageBuffer;
|
|
474
473
|
private recommendQuestions?;
|
|
474
|
+
aitools?: aitools.AITools;
|
|
475
475
|
constructor(config: WeChatAgentConfig);
|
|
476
476
|
/**
|
|
477
477
|
* Last assembled reply content after run() completes
|
|
@@ -568,15 +568,15 @@ interface AgentCreatorResult {
|
|
|
568
568
|
toAGUIAgent(): AbstractAgent;
|
|
569
569
|
};
|
|
570
570
|
cleanup?: () => void;
|
|
571
|
-
aitools?: {
|
|
572
|
-
getWxMediaContent: (botId: string, triggerSrc: string, mediaId: string) => Promise<string | null>;
|
|
573
|
-
};
|
|
574
571
|
}
|
|
575
572
|
/**
|
|
576
573
|
* Agent creator function type
|
|
577
574
|
*/
|
|
578
575
|
type AgentCreator = (params: {
|
|
579
576
|
request: Request;
|
|
577
|
+
options?: {
|
|
578
|
+
agentId: string;
|
|
579
|
+
};
|
|
580
580
|
}) => AgentCreatorResult | Promise<AgentCreatorResult>;
|
|
581
581
|
/**
|
|
582
582
|
* Handler options
|
|
@@ -589,7 +589,7 @@ interface WxMessageHandlerOptions {
|
|
|
589
589
|
* Directly using sendMessageAGUI.handler from @cloudbase/agent-server
|
|
590
590
|
* Aligned with chat_wx_v2.service.ts logic
|
|
591
591
|
*
|
|
592
|
-
* @param createAgent - Function to create agent instance (returns { agent, cleanup
|
|
592
|
+
* @param createAgent - Function to create agent instance (returns { agent, cleanup?})
|
|
593
593
|
* @param options - Handler options
|
|
594
594
|
* @returns Express route handler
|
|
595
595
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Request, Response } from 'express';
|
|
2
2
|
import { AbstractAgent, AgentConfig, BaseEvent, RunAgentInput } from '@ag-ui/client';
|
|
3
3
|
import { Observable } from 'rxjs';
|
|
4
|
-
import { WxSendMessageInput } from '@cloudbase/aiagent-framework';
|
|
4
|
+
import { WxSendMessageInput, aitools } from '@cloudbase/aiagent-framework';
|
|
5
5
|
import { Logger } from '@cloudbase/agent-shared';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -236,8 +236,8 @@ interface WeChatReplyMessage {
|
|
|
236
236
|
createTime: number;
|
|
237
237
|
msgType: string;
|
|
238
238
|
content: string;
|
|
239
|
+
msgId: string;
|
|
239
240
|
openKfId?: string;
|
|
240
|
-
msgId?: string;
|
|
241
241
|
}
|
|
242
242
|
/**
|
|
243
243
|
* Format reply message for WeChat
|
|
@@ -465,13 +465,13 @@ interface IWxRunAgentInput extends RunAgentInput {
|
|
|
465
465
|
declare class WeChatAgent extends AbstractAgent {
|
|
466
466
|
private wrappedAgent;
|
|
467
467
|
private wechatSender?;
|
|
468
|
-
private aitools?;
|
|
469
468
|
private wechatConfig;
|
|
470
469
|
private _historyManager;
|
|
471
470
|
private messageFormatter;
|
|
472
471
|
private eventFilter;
|
|
473
472
|
private messageBuffer;
|
|
474
473
|
private recommendQuestions?;
|
|
474
|
+
aitools?: aitools.AITools;
|
|
475
475
|
constructor(config: WeChatAgentConfig);
|
|
476
476
|
/**
|
|
477
477
|
* Last assembled reply content after run() completes
|
|
@@ -568,15 +568,15 @@ interface AgentCreatorResult {
|
|
|
568
568
|
toAGUIAgent(): AbstractAgent;
|
|
569
569
|
};
|
|
570
570
|
cleanup?: () => void;
|
|
571
|
-
aitools?: {
|
|
572
|
-
getWxMediaContent: (botId: string, triggerSrc: string, mediaId: string) => Promise<string | null>;
|
|
573
|
-
};
|
|
574
571
|
}
|
|
575
572
|
/**
|
|
576
573
|
* Agent creator function type
|
|
577
574
|
*/
|
|
578
575
|
type AgentCreator = (params: {
|
|
579
576
|
request: Request;
|
|
577
|
+
options?: {
|
|
578
|
+
agentId: string;
|
|
579
|
+
};
|
|
580
580
|
}) => AgentCreatorResult | Promise<AgentCreatorResult>;
|
|
581
581
|
/**
|
|
582
582
|
* Handler options
|
|
@@ -589,7 +589,7 @@ interface WxMessageHandlerOptions {
|
|
|
589
589
|
* Directly using sendMessageAGUI.handler from @cloudbase/agent-server
|
|
590
590
|
* Aligned with chat_wx_v2.service.ts logic
|
|
591
591
|
*
|
|
592
|
-
* @param createAgent - Function to create agent instance (returns { agent, cleanup
|
|
592
|
+
* @param createAgent - Function to create agent instance (returns { agent, cleanup?})
|
|
593
593
|
* @param options - Handler options
|
|
594
594
|
* @returns Express route handler
|
|
595
595
|
*/
|
package/dist/index.js
CHANGED
|
@@ -189,7 +189,8 @@ function formatWeChatReplyMessage(callbackData, triggerSrc, content) {
|
|
|
189
189
|
openKfId: callbackData.openKfId,
|
|
190
190
|
createTime,
|
|
191
191
|
msgType: "text",
|
|
192
|
-
content: finalContent
|
|
192
|
+
content: finalContent,
|
|
193
|
+
msgId: callbackData.msgId
|
|
193
194
|
};
|
|
194
195
|
}
|
|
195
196
|
return {
|
|
@@ -197,7 +198,8 @@ function formatWeChatReplyMessage(callbackData, triggerSrc, content) {
|
|
|
197
198
|
fromUserName: callbackData.toUserName || callbackData.ToUserName,
|
|
198
199
|
createTime,
|
|
199
200
|
msgType: "text",
|
|
200
|
-
content: finalContent
|
|
201
|
+
content: finalContent,
|
|
202
|
+
msgId: callbackData.msgId
|
|
201
203
|
};
|
|
202
204
|
}
|
|
203
205
|
function dealMsgData(originMsg, triggerSrc, wxVerify, botId) {
|
|
@@ -277,20 +279,57 @@ var import_agent_server = require("@cloudbase/agent-server");
|
|
|
277
279
|
var import_agent_shared = require("@cloudbase/agent-shared");
|
|
278
280
|
var import_client = require("@ag-ui/client");
|
|
279
281
|
var import_agent_shared2 = require("@cloudbase/agent-shared");
|
|
282
|
+
var import_aiagent_framework = require("@cloudbase/aiagent-framework");
|
|
283
|
+
function getServiceNameFromHostname(hostname) {
|
|
284
|
+
const parts = hostname.split("-");
|
|
285
|
+
let versionIdx = -1;
|
|
286
|
+
for (let i = parts.length - 1; i >= 0; i--) {
|
|
287
|
+
if (/^\d+$/.test(parts[i])) {
|
|
288
|
+
versionIdx = i;
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return versionIdx > 0 ? parts.slice(0, versionIdx).join("-") : hostname;
|
|
293
|
+
}
|
|
294
|
+
function getBotId(url, fallback = "agent-id") {
|
|
295
|
+
if (process.env.SCF_FUNCTIONNAME) {
|
|
296
|
+
return process.env.SCF_FUNCTIONNAME;
|
|
297
|
+
}
|
|
298
|
+
if (url) {
|
|
299
|
+
const parsedBotId = import_aiagent_framework.utils.parseBotId(url);
|
|
300
|
+
if (parsedBotId) {
|
|
301
|
+
return parsedBotId;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (process.env.HOSTNAME) {
|
|
305
|
+
return getServiceNameFromHostname(process.env.HOSTNAME);
|
|
306
|
+
}
|
|
307
|
+
return fallback;
|
|
308
|
+
}
|
|
280
309
|
function createWxMessageHandler(createAgent, options) {
|
|
281
310
|
const { logger: parentLogger = import_agent_shared.noopLogger } = options ?? {};
|
|
282
311
|
const adapterLogger = parentLogger.child?.({ component: "sendWXMessageAGUI" }) ?? parentLogger;
|
|
283
312
|
return async (req, res) => {
|
|
284
313
|
let cleanup = null;
|
|
314
|
+
let agent = void 0;
|
|
315
|
+
const { callbackData } = import_aiagent_framework.utils.transformKeysToCamelCase(req.body);
|
|
316
|
+
const { triggerSrc, wxVerify } = import_aiagent_framework.utils.parseWxEnvParam({
|
|
317
|
+
httpContext: {
|
|
318
|
+
headers: req.headers
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
const wxSendmessageInput = {
|
|
322
|
+
callbackData,
|
|
323
|
+
triggerSrc,
|
|
324
|
+
wxVerify
|
|
325
|
+
};
|
|
326
|
+
const requestId = (0, import_uuid.v4)();
|
|
327
|
+
const logger = adapterLogger.child?.({ requestId }) ?? adapterLogger;
|
|
328
|
+
const botId = getBotId(
|
|
329
|
+
`${req.protocol}://${req.get("host")}${req.originalUrl}`,
|
|
330
|
+
"agent-wx-send-message"
|
|
331
|
+
);
|
|
285
332
|
try {
|
|
286
|
-
const {
|
|
287
|
-
callbackData,
|
|
288
|
-
triggerSrc,
|
|
289
|
-
wxVerify = false,
|
|
290
|
-
botId = "default"
|
|
291
|
-
} = req.body;
|
|
292
|
-
const requestId = (0, import_uuid.v4)();
|
|
293
|
-
const logger = adapterLogger.child?.({ requestId }) ?? adapterLogger;
|
|
294
333
|
if (!callbackData || !triggerSrc) {
|
|
295
334
|
return res.status(400).json({
|
|
296
335
|
error: "Bad Request",
|
|
@@ -298,9 +337,11 @@ function createWxMessageHandler(createAgent, options) {
|
|
|
298
337
|
});
|
|
299
338
|
}
|
|
300
339
|
logger.info?.("[WX] Received message:", { triggerSrc, wxVerify, botId });
|
|
301
|
-
const { agent: unknownAgent, cleanup: agentCleanup } = await Promise.resolve(
|
|
340
|
+
const { agent: unknownAgent, cleanup: agentCleanup } = await Promise.resolve(
|
|
341
|
+
createAgent({ request: req, options: { agentId: botId } })
|
|
342
|
+
);
|
|
302
343
|
cleanup = agentCleanup ?? null;
|
|
303
|
-
|
|
344
|
+
agent = "toAGUIAgent" in unknownAgent ? unknownAgent.toAGUIAgent() : unknownAgent;
|
|
304
345
|
const { msgData, replyMsgData } = dealMsgData(
|
|
305
346
|
callbackData,
|
|
306
347
|
triggerSrc,
|
|
@@ -321,17 +362,12 @@ function createWxMessageHandler(createAgent, options) {
|
|
|
321
362
|
);
|
|
322
363
|
const voiceHandler = async (vBotId, vTriggerSrc, mediaId) => {
|
|
323
364
|
try {
|
|
324
|
-
const
|
|
365
|
+
const result = await agent?.aitools?.getWxMediaContent(
|
|
325
366
|
vBotId,
|
|
326
367
|
vTriggerSrc,
|
|
327
368
|
mediaId
|
|
328
369
|
);
|
|
329
|
-
|
|
330
|
-
return {
|
|
331
|
-
content: content2
|
|
332
|
-
};
|
|
333
|
-
}
|
|
334
|
-
return null;
|
|
370
|
+
return result || null;
|
|
335
371
|
} catch {
|
|
336
372
|
return null;
|
|
337
373
|
}
|
|
@@ -388,7 +424,7 @@ function createWxMessageHandler(createAgent, options) {
|
|
|
388
424
|
tools: [],
|
|
389
425
|
context: [],
|
|
390
426
|
forwardedProps: {
|
|
391
|
-
wxSendmessageInput
|
|
427
|
+
wxSendmessageInput,
|
|
392
428
|
// Pass full msgData for history storage
|
|
393
429
|
msgData,
|
|
394
430
|
// Pass replay info with replyMsgData for assistant history
|
|
@@ -416,10 +452,34 @@ function createWxMessageHandler(createAgent, options) {
|
|
|
416
452
|
formatWeChatReplyMessage(callbackData, triggerSrc, finalContent)
|
|
417
453
|
);
|
|
418
454
|
} catch (error) {
|
|
419
|
-
|
|
455
|
+
logger.error?.("[WX] Error processing message:", error);
|
|
456
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
457
|
+
try {
|
|
458
|
+
if (agent?.aitools?.sendWxClientMessage) {
|
|
459
|
+
const data = formatWeChatReplyMessage(
|
|
460
|
+
callbackData,
|
|
461
|
+
triggerSrc,
|
|
462
|
+
message
|
|
463
|
+
);
|
|
464
|
+
await agent.aitools.sendWxClientMessage(
|
|
465
|
+
agent.agentId || botId,
|
|
466
|
+
triggerSrc,
|
|
467
|
+
{
|
|
468
|
+
msgType: data.msgType,
|
|
469
|
+
touser: data.toUserName,
|
|
470
|
+
text: {
|
|
471
|
+
content: data.content
|
|
472
|
+
},
|
|
473
|
+
openKfId: data.openKfId,
|
|
474
|
+
msgId: data.msgId
|
|
475
|
+
}
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
} catch (e) {
|
|
479
|
+
}
|
|
420
480
|
return res.status(500).json({
|
|
421
481
|
error: "Internal Server Error",
|
|
422
|
-
message
|
|
482
|
+
message
|
|
423
483
|
});
|
|
424
484
|
} finally {
|
|
425
485
|
if (cleanup) cleanup();
|
|
@@ -822,9 +882,9 @@ var WeChatHistoryManager = class _WeChatHistoryManager {
|
|
|
822
882
|
var getHistoryManager = WeChatHistoryManager.getInstance;
|
|
823
883
|
|
|
824
884
|
// src/agent.ts
|
|
825
|
-
var
|
|
885
|
+
var import_aiagent_framework2 = require("@cloudbase/aiagent-framework");
|
|
826
886
|
var import_crypto = require("crypto");
|
|
827
|
-
var AITOOLS =
|
|
887
|
+
var AITOOLS = import_aiagent_framework2.aitools.AITools;
|
|
828
888
|
var WeChatAgent = class extends import_client2.AbstractAgent {
|
|
829
889
|
constructor(config) {
|
|
830
890
|
super({
|
|
@@ -1118,15 +1178,19 @@ var WeChatAgent = class extends import_client2.AbstractAgent {
|
|
|
1118
1178
|
return;
|
|
1119
1179
|
}
|
|
1120
1180
|
const triggerSrc = wxSendmessageInput.triggerSrc || this.wechatConfig.platform;
|
|
1121
|
-
await this.aitools.sendWxClientMessage(
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1181
|
+
await this.aitools.sendWxClientMessage(
|
|
1182
|
+
this.agentId || "agent",
|
|
1183
|
+
triggerSrc,
|
|
1184
|
+
{
|
|
1185
|
+
msgType: toWxMsgData.msgType,
|
|
1186
|
+
touser: toWxMsgData.toUserName,
|
|
1187
|
+
text: {
|
|
1188
|
+
content: toWxMsgData.content
|
|
1189
|
+
},
|
|
1190
|
+
openKfId: toWxMsgData.openKfId,
|
|
1191
|
+
msgId: toWxMsgData.msgId || ""
|
|
1192
|
+
}
|
|
1193
|
+
);
|
|
1130
1194
|
}
|
|
1131
1195
|
/**
|
|
1132
1196
|
* Format reply message for WeChat HTTP response (sync mode)
|