@searchfe/openclaw-baiduapp 0.1.7-beta.2 → 0.1.7
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.js +43 -38
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4444,7 +4444,6 @@ async function dispatchBaiduAppMessage(params) {
|
|
|
4444
4444
|
cfg: safeCfg,
|
|
4445
4445
|
dispatcherOptions: {
|
|
4446
4446
|
deliver: async (payload) => {
|
|
4447
|
-
console.log("DELIVER123", payload);
|
|
4448
4447
|
const rawText = payload.text ?? "";
|
|
4449
4448
|
if (!rawText.trim()) {
|
|
4450
4449
|
logger2.debug("deliver callback: empty text, skipping");
|
|
@@ -4484,6 +4483,7 @@ var streams = /* @__PURE__ */ new Map();
|
|
|
4484
4483
|
var msgidToStreamId = /* @__PURE__ */ new Map();
|
|
4485
4484
|
var STREAM_TTL_MS = 10 * 60 * 1e3;
|
|
4486
4485
|
var STREAM_MAX_BYTES = 512e3;
|
|
4486
|
+
var MAX_MESSAGE_BYTES = 2048;
|
|
4487
4487
|
function normalizeWebhookPath(raw) {
|
|
4488
4488
|
const trimmed = raw.trim();
|
|
4489
4489
|
if (!trimmed) {
|
|
@@ -4516,6 +4516,26 @@ function truncateUtf8Bytes(text, maxBytes) {
|
|
|
4516
4516
|
const slice = buf.subarray(buf.length - maxBytes);
|
|
4517
4517
|
return slice.toString("utf8");
|
|
4518
4518
|
}
|
|
4519
|
+
function splitMessageByBytes(text, maxBytes = MAX_MESSAGE_BYTES) {
|
|
4520
|
+
const result = [];
|
|
4521
|
+
let current = "";
|
|
4522
|
+
let currentBytes = 0;
|
|
4523
|
+
for (const char of text) {
|
|
4524
|
+
const charBytes = Buffer.byteLength(char, "utf8");
|
|
4525
|
+
if (currentBytes + charBytes > maxBytes && current.length > 0) {
|
|
4526
|
+
result.push(current);
|
|
4527
|
+
current = char;
|
|
4528
|
+
currentBytes = charBytes;
|
|
4529
|
+
} else {
|
|
4530
|
+
current += char;
|
|
4531
|
+
currentBytes += charBytes;
|
|
4532
|
+
}
|
|
4533
|
+
}
|
|
4534
|
+
if (current.length > 0) {
|
|
4535
|
+
result.push(current);
|
|
4536
|
+
}
|
|
4537
|
+
return result;
|
|
4538
|
+
}
|
|
4519
4539
|
function jsonOk(res, body) {
|
|
4520
4540
|
res.statusCode = 200;
|
|
4521
4541
|
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
@@ -4938,8 +4958,6 @@ async function handleBaiduAppWebhookRequest(req, res) {
|
|
|
4938
4958
|
finished: false,
|
|
4939
4959
|
content: ""
|
|
4940
4960
|
});
|
|
4941
|
-
let nextChunkKey = 0;
|
|
4942
|
-
let activeSendQueue = Promise.resolve();
|
|
4943
4961
|
logger2.info(`stream created: streamId=${streamId} msgid=${msgid ?? "none"}`);
|
|
4944
4962
|
const core = tryGetBaiduAppRuntime();
|
|
4945
4963
|
if (core) {
|
|
@@ -4950,7 +4968,6 @@ async function handleBaiduAppWebhookRequest(req, res) {
|
|
|
4950
4968
|
logger2.info(`agent dispatch started: streamId=${streamId} canSendActive=${target.account.canSendActive}`);
|
|
4951
4969
|
const hooks = {
|
|
4952
4970
|
onChunk: (text) => {
|
|
4953
|
-
console.log("CHUNK123", text);
|
|
4954
4971
|
const current = streams.get(streamId);
|
|
4955
4972
|
if (!current) {
|
|
4956
4973
|
return;
|
|
@@ -4960,32 +4977,6 @@ async function handleBaiduAppWebhookRequest(req, res) {
|
|
|
4960
4977
|
`chunk received: streamId=${streamId} chunkLen=${text.length} totalLen=${current.content.length}`
|
|
4961
4978
|
);
|
|
4962
4979
|
target.statusSink?.({ lastOutboundAt: Date.now() });
|
|
4963
|
-
if (!target.account.canSendActive) {
|
|
4964
|
-
return;
|
|
4965
|
-
}
|
|
4966
|
-
const chunkKey = nextChunkKey;
|
|
4967
|
-
nextChunkKey += 1;
|
|
4968
|
-
activeSendQueue = activeSendQueue.then(async () => {
|
|
4969
|
-
try {
|
|
4970
|
-
await sendBaiduAppMessage(target.account, text, {
|
|
4971
|
-
msgid,
|
|
4972
|
-
streamId,
|
|
4973
|
-
chunkKey
|
|
4974
|
-
});
|
|
4975
|
-
logger2.debug(
|
|
4976
|
-
`active send chunk ${chunkKey + 1} sent: streamId=${streamId} chunkLen=${text.length}`
|
|
4977
|
-
);
|
|
4978
|
-
} catch (err) {
|
|
4979
|
-
const latest = streams.get(streamId);
|
|
4980
|
-
if (latest) {
|
|
4981
|
-
latest.error = err instanceof Error ? err.message : String(err);
|
|
4982
|
-
latest.updatedAt = Date.now();
|
|
4983
|
-
}
|
|
4984
|
-
logger2.error(
|
|
4985
|
-
`active send chunk failed: streamId=${streamId} chunkKey=${chunkKey} error=${String(err)}`
|
|
4986
|
-
);
|
|
4987
|
-
}
|
|
4988
|
-
});
|
|
4989
4980
|
},
|
|
4990
4981
|
onError: (err) => {
|
|
4991
4982
|
const current = streams.get(streamId);
|
|
@@ -5007,7 +4998,6 @@ async function handleBaiduAppWebhookRequest(req, res) {
|
|
|
5007
4998
|
log: target.runtime.log,
|
|
5008
4999
|
error: target.runtime.error
|
|
5009
5000
|
}).then(async () => {
|
|
5010
|
-
await activeSendQueue;
|
|
5011
5001
|
const current = streams.get(streamId);
|
|
5012
5002
|
if (current) {
|
|
5013
5003
|
current.finished = true;
|
|
@@ -5021,13 +5011,28 @@ async function handleBaiduAppWebhookRequest(req, res) {
|
|
|
5021
5011
|
`active send skipped: appKey/appSecret not configured for account ${target.account.accountId}`
|
|
5022
5012
|
);
|
|
5023
5013
|
} else if (!contentLen) {
|
|
5024
|
-
logger2.warn(
|
|
5025
|
-
|
|
5026
|
-
|
|
5027
|
-
|
|
5028
|
-
|
|
5029
|
-
|
|
5030
|
-
|
|
5014
|
+
logger2.warn(`active send skipped: agent produced no content`);
|
|
5015
|
+
}
|
|
5016
|
+
if (target.account.canSendActive && current.content.trim()) {
|
|
5017
|
+
try {
|
|
5018
|
+
const chunks = splitMessageByBytes(current.content, MAX_MESSAGE_BYTES);
|
|
5019
|
+
logger2.info(
|
|
5020
|
+
`[REPLY-MODE:ACTIVE-SEND] active send starting: streamId=${streamId} chunks=${chunks.length} contentLen=${contentLen}`
|
|
5021
|
+
);
|
|
5022
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
5023
|
+
await sendBaiduAppMessage(target.account, chunks[i], {
|
|
5024
|
+
msgid,
|
|
5025
|
+
streamId,
|
|
5026
|
+
chunkKey: i
|
|
5027
|
+
});
|
|
5028
|
+
logger2.debug(`active send chunk ${i + 1}/${chunks.length} sent: streamId=${streamId}`);
|
|
5029
|
+
}
|
|
5030
|
+
logger2.info(
|
|
5031
|
+
`[REPLY-MODE:ACTIVE-SEND] active send complete: streamId=${streamId} chunks=${chunks.length}`
|
|
5032
|
+
);
|
|
5033
|
+
} catch (err) {
|
|
5034
|
+
logger2.error(`active send failed: streamId=${streamId} error=${String(err)}`);
|
|
5035
|
+
}
|
|
5031
5036
|
}
|
|
5032
5037
|
}
|
|
5033
5038
|
}).catch((err) => {
|