@searchfe/openclaw-baiduapp 0.1.0-beta.3 → 0.1.0-beta.5

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.ts CHANGED
@@ -10,6 +10,7 @@ interface BaiduAppAccountConfig {
10
10
  receiveId?: string;
11
11
  appKey?: string;
12
12
  appSecret?: string;
13
+ apiBase?: string;
13
14
  welcomeText?: string;
14
15
  dmPolicy?: BaiduAppDmPolicy;
15
16
  allowFrom?: string[];
@@ -28,6 +29,7 @@ interface ResolvedBaiduAppAccount {
28
29
  receiveId: string;
29
30
  appKey?: string;
30
31
  appSecret?: string;
32
+ apiBase: string;
31
33
  canSendActive: boolean;
32
34
  config: BaiduAppAccountConfig;
33
35
  }
@@ -137,6 +139,9 @@ declare const baiduAppPlugin: {
137
139
  appSecret: {
138
140
  type: string;
139
141
  };
142
+ apiBase: {
143
+ type: string;
144
+ };
140
145
  welcomeText: {
141
146
  type: string;
142
147
  };
@@ -183,6 +188,9 @@ declare const baiduAppPlugin: {
183
188
  appSecret: {
184
189
  type: string;
185
190
  };
191
+ apiBase: {
192
+ type: string;
193
+ };
186
194
  welcomeText: {
187
195
  type: string;
188
196
  };
package/dist/index.js CHANGED
@@ -4048,6 +4048,7 @@ var NEVER = INVALID;
4048
4048
 
4049
4049
  // src/config.ts
4050
4050
  var DEFAULT_ACCOUNT_ID = "default";
4051
+ var DEFAULT_API_BASE = "https://claw.baidu.com";
4051
4052
  var BaiduAppAccountSchema = external_exports.object({
4052
4053
  name: external_exports.string().optional(),
4053
4054
  enabled: external_exports.boolean().optional(),
@@ -4057,6 +4058,7 @@ var BaiduAppAccountSchema = external_exports.object({
4057
4058
  receiveId: external_exports.string().optional(),
4058
4059
  appKey: external_exports.string().optional(),
4059
4060
  appSecret: external_exports.string().optional(),
4061
+ apiBase: external_exports.string().optional(),
4060
4062
  welcomeText: external_exports.string().optional(),
4061
4063
  dmPolicy: external_exports.enum(["open", "pairing", "allowlist", "disabled"]).optional(),
4062
4064
  allowFrom: external_exports.array(external_exports.string()).optional()
@@ -4078,6 +4080,7 @@ var BaiduAppConfigJsonSchema = {
4078
4080
  receiveId: { type: "string" },
4079
4081
  appKey: { type: "string" },
4080
4082
  appSecret: { type: "string" },
4083
+ apiBase: { type: "string" },
4081
4084
  welcomeText: { type: "string" },
4082
4085
  dmPolicy: { type: "string", enum: ["open", "pairing", "allowlist", "disabled"] },
4083
4086
  allowFrom: { type: "array", items: { type: "string" } },
@@ -4096,6 +4099,7 @@ var BaiduAppConfigJsonSchema = {
4096
4099
  receiveId: { type: "string" },
4097
4100
  appKey: { type: "string" },
4098
4101
  appSecret: { type: "string" },
4102
+ apiBase: { type: "string" },
4099
4103
  welcomeText: { type: "string" },
4100
4104
  dmPolicy: { type: "string", enum: ["open", "pairing", "allowlist", "disabled"] },
4101
4105
  allowFrom: { type: "array", items: { type: "string" } }
@@ -4160,6 +4164,8 @@ function resolveBaiduAppAccount(params) {
4160
4164
  const appSecret = merged.appSecret?.trim() || (isDefaultAccount ? process.env.BAIDU_APP_SECRET?.trim() : void 0) || void 0;
4161
4165
  const configured = Boolean(token && encodingAESKey);
4162
4166
  const canSendActive = Boolean(appKey && appSecret);
4167
+ const rawApiBase = merged.apiBase?.trim() || (isDefaultAccount ? process.env.BAIDU_API_BASE?.trim() : void 0) || void 0;
4168
+ const apiBase = (rawApiBase || DEFAULT_API_BASE).replace(/\/+$/, "");
4163
4169
  return {
4164
4170
  accountId,
4165
4171
  name: merged.name?.trim() || void 0,
@@ -4170,6 +4176,7 @@ function resolveBaiduAppAccount(params) {
4170
4176
  receiveId,
4171
4177
  appKey,
4172
4178
  appSecret,
4179
+ apiBase,
4173
4180
  canSendActive,
4174
4181
  config: merged
4175
4182
  };
@@ -4215,9 +4222,6 @@ function checkDmPolicy(params) {
4215
4222
  }
4216
4223
 
4217
4224
  // src/api.ts
4218
- var BAIDU_API_BASE = process.env.BAIDU_API_BASE?.replace(/\/+$/, "") || "https://openapi.baidu.com";
4219
- var TOKEN_URL = `${BAIDU_API_BASE}/auth/token`;
4220
- var SEND_MESSAGE_URL = `${BAIDU_API_BASE}/chat/openclaw/callback`;
4221
4225
  var accessTokenCache = /* @__PURE__ */ new Map();
4222
4226
  var ACCESS_TOKEN_TTL_MS = 7200 * 1e3 - 5 * 60 * 1e3;
4223
4227
  async function getAccessToken(account) {
@@ -4229,7 +4233,8 @@ async function getAccessToken(account) {
4229
4233
  if (cached && Date.now() < cached.expiresAt) {
4230
4234
  return cached.token;
4231
4235
  }
4232
- const url = `${TOKEN_URL}?appKey=${encodeURIComponent(account.appKey)}&appSecret=${encodeURIComponent(account.appSecret)}`;
4236
+ const tokenUrl = `${account.apiBase}/auth/token`;
4237
+ const url = `${tokenUrl}?appKey=${encodeURIComponent(account.appKey)}&appSecret=${encodeURIComponent(account.appSecret)}`;
4233
4238
  const resp = await fetch(url);
4234
4239
  const text = await resp.text();
4235
4240
  if (!text) {
@@ -4274,7 +4279,8 @@ async function sendBaiduAppMessage(account, target, message) {
4274
4279
  text: { content: message },
4275
4280
  touser: target.userId
4276
4281
  };
4277
- const resp = await fetch(`${SEND_MESSAGE_URL}?access_token=${encodeURIComponent(token)}`, {
4282
+ const sendMessageUrl = `${account.apiBase}/chat/openclaw/callback`;
4283
+ const resp = await fetch(`${sendMessageUrl}?access_token=${encodeURIComponent(token)}`, {
4278
4284
  method: "POST",
4279
4285
  body: JSON.stringify(payload),
4280
4286
  headers: { "Content-Type": "application/json" }
@@ -4621,8 +4627,13 @@ function splitMessageByBytes(text, maxBytes = MAX_MESSAGE_BYTES) {
4621
4627
  }
4622
4628
  function jsonOk(res, body) {
4623
4629
  res.statusCode = 200;
4624
- res.setHeader("Content-Type", "text/plain; charset=utf-8");
4625
- res.end(JSON.stringify(body));
4630
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
4631
+ res.end(JSON.stringify({ status: 0, data: body }));
4632
+ }
4633
+ function jsonError(res, message, statusCode = 200) {
4634
+ res.statusCode = statusCode;
4635
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
4636
+ res.end(JSON.stringify({ status: -1, message }));
4626
4637
  }
4627
4638
  async function readRawBody(req, maxBytes) {
4628
4639
  const chunks = [];
@@ -4830,8 +4841,7 @@ async function handleBaiduAppWebhookRequest(req, res) {
4830
4841
  if (req.method === "GET") {
4831
4842
  const echostr = query.get("echostr") ?? "";
4832
4843
  if (!timestamp || !nonce || !signature || !echostr) {
4833
- res.statusCode = 400;
4834
- res.end("missing query params");
4844
+ jsonError(res, "missing query params", 400);
4835
4845
  return true;
4836
4846
  }
4837
4847
  const signatureMatched2 = targets.filter((candidate) => {
@@ -4847,14 +4857,12 @@ async function handleBaiduAppWebhookRequest(req, res) {
4847
4857
  });
4848
4858
  });
4849
4859
  if (signatureMatched2.length === 0) {
4850
- res.statusCode = 401;
4851
- res.end("unauthorized");
4860
+ jsonError(res, "unauthorized");
4852
4861
  return true;
4853
4862
  }
4854
4863
  const decryptable2 = signatureMatched2.filter((candidate) => Boolean(candidate.account.encodingAESKey));
4855
4864
  if (decryptable2.length === 0) {
4856
- res.statusCode = 401;
4857
- res.end("unauthorized");
4865
+ jsonError(res, "unauthorized");
4858
4866
  return true;
4859
4867
  }
4860
4868
  const decryptedCandidates2 = decryptBaiduAppCandidates({
@@ -4862,31 +4870,24 @@ async function handleBaiduAppWebhookRequest(req, res) {
4862
4870
  encrypt: echostr
4863
4871
  });
4864
4872
  if (decryptedCandidates2.length === 0) {
4865
- res.statusCode = 400;
4866
- res.end("decrypt failed");
4873
+ jsonError(res, "decrypt failed");
4867
4874
  return true;
4868
4875
  }
4869
4876
  const selected2 = selectDecryptedTarget({ candidates: decryptedCandidates2, logger });
4870
- res.statusCode = 200;
4871
- res.setHeader("Content-Type", "text/plain; charset=utf-8");
4872
- res.end(selected2.plaintext);
4877
+ jsonOk(res, selected2.plaintext);
4873
4878
  return true;
4874
4879
  }
4875
4880
  if (req.method !== "POST") {
4876
- res.statusCode = 405;
4877
- res.setHeader("Allow", "GET, POST");
4878
- res.end("Method Not Allowed");
4881
+ jsonError(res, "Method Not Allowed", 405);
4879
4882
  return true;
4880
4883
  }
4881
4884
  if (!timestamp || !nonce || !signature) {
4882
- res.statusCode = 400;
4883
- res.end("missing query params");
4885
+ jsonError(res, "missing query params");
4884
4886
  return true;
4885
4887
  }
4886
4888
  const body = await readRawBody(req, 1024 * 1024);
4887
4889
  if (!body.ok || !body.raw) {
4888
- res.statusCode = body.error === "payload too large" ? 413 : 400;
4889
- res.end(body.error ?? "invalid payload");
4890
+ jsonError(res, body.error ?? "invalid payload");
4890
4891
  return true;
4891
4892
  }
4892
4893
  const rawBody = body.raw;
@@ -4908,14 +4909,12 @@ async function handleBaiduAppWebhookRequest(req, res) {
4908
4909
  logger.info(`inbound json parsed: hasEncrypt=${Boolean(encrypt)}`);
4909
4910
  } catch {
4910
4911
  logger.warn(`inbound payload parse failed: not valid xml or json`);
4911
- res.statusCode = 400;
4912
- res.end("invalid payload format");
4912
+ jsonError(res, "invalid payload format");
4913
4913
  return true;
4914
4914
  }
4915
4915
  }
4916
4916
  if (!encrypt) {
4917
- res.statusCode = 400;
4918
- res.end("missing encrypt");
4917
+ jsonError(res, "missing encrypt");
4919
4918
  return true;
4920
4919
  }
4921
4920
  const signatureMatched = targets.filter((candidate) => {
@@ -4932,16 +4931,14 @@ async function handleBaiduAppWebhookRequest(req, res) {
4932
4931
  });
4933
4932
  if (signatureMatched.length === 0) {
4934
4933
  logger.warn(`signature verification failed: checked ${targets.length} account(s), none matched`);
4935
- res.statusCode = 401;
4936
- res.end("unauthorized");
4934
+ jsonError(res, "unauthorized");
4937
4935
  return true;
4938
4936
  }
4939
4937
  logger.debug(`signature verified: ${signatureMatched.length} account(s) matched`);
4940
4938
  const decryptable = signatureMatched.filter((candidate) => Boolean(candidate.account.encodingAESKey));
4941
4939
  if (decryptable.length === 0) {
4942
4940
  logger.warn(`no account has encodingAESKey configured`);
4943
- res.statusCode = 500;
4944
- res.end("baidu-app not configured");
4941
+ jsonError(res, "baidu-app not configured");
4945
4942
  return true;
4946
4943
  }
4947
4944
  const decryptedCandidates = decryptBaiduAppCandidates({
@@ -4950,16 +4947,14 @@ async function handleBaiduAppWebhookRequest(req, res) {
4950
4947
  });
4951
4948
  if (decryptedCandidates.length === 0) {
4952
4949
  logger.warn(`decrypt failed for all ${decryptable.length} candidate account(s)`);
4953
- res.statusCode = 400;
4954
- res.end("decrypt failed");
4950
+ jsonError(res, "decrypt failed");
4955
4951
  return true;
4956
4952
  }
4957
4953
  const selected = selectDecryptedTarget({ candidates: decryptedCandidates, logger });
4958
4954
  const target = selected.target;
4959
4955
  if (!target.account.configured || !target.account.token || !target.account.encodingAESKey) {
4960
4956
  logger.warn(`selected account ${target.account.accountId} not fully configured`);
4961
- res.statusCode = 500;
4962
- res.end("baidu-app not configured");
4957
+ jsonError(res, "baidu-app not configured");
4963
4958
  return true;
4964
4959
  }
4965
4960
  const msg = selected.msg;