opencode-feishu 0.3.4 → 0.3.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 CHANGED
@@ -1,10 +1,12 @@
1
1
  import fs, { existsSync, readFileSync } from 'fs';
2
2
  import { join } from 'path';
3
3
  import { homedir } from 'os';
4
+ import https, { request } from 'https';
5
+ import { text } from 'stream/consumers';
6
+ import { HttpsProxyAgent } from 'https-proxy-agent';
4
7
  import crypto2 from 'crypto';
5
8
  import url from 'url';
6
9
  import http from 'http';
7
- import https from 'https';
8
10
  import http2 from 'http2';
9
11
  import util2 from 'util';
10
12
  import zlib from 'zlib';
@@ -12,7 +14,6 @@ import stream3, { Readable } from 'stream';
12
14
  import { EventEmitter } from 'events';
13
15
  import qs from 'querystring';
14
16
  import WebSocket from 'ws';
15
- import { HttpsProxyAgent } from 'https-proxy-agent';
16
17
 
17
18
  var __create = Object.create;
18
19
  var __defProp = Object.defineProperty;
@@ -18949,9 +18950,9 @@ var trackStream = (stream4, chunkSize, onProgress, onFinish) => {
18949
18950
  // node_modules/axios/lib/adapters/fetch.js
18950
18951
  var DEFAULT_CHUNK_SIZE = 64 * 1024;
18951
18952
  var { isFunction: isFunction2 } = utils_default;
18952
- var globalFetchAPI = (({ Request, Response }) => ({
18953
+ var globalFetchAPI = (({ Request, Response: Response2 }) => ({
18953
18954
  Request,
18954
- Response
18955
+ Response: Response2
18955
18956
  }))(utils_default.global);
18956
18957
  var {
18957
18958
  ReadableStream: ReadableStream2,
@@ -18968,10 +18969,10 @@ var factory = (env) => {
18968
18969
  env = utils_default.merge.call({
18969
18970
  skipUndefined: true
18970
18971
  }, globalFetchAPI, env);
18971
- const { fetch: envFetch, Request, Response } = env;
18972
+ const { fetch: envFetch, Request, Response: Response2 } = env;
18972
18973
  const isFetchSupported = envFetch ? isFunction2(envFetch) : typeof fetch === "function";
18973
18974
  const isRequestSupported = isFunction2(Request);
18974
- const isResponseSupported = isFunction2(Response);
18975
+ const isResponseSupported = isFunction2(Response2);
18975
18976
  if (!isFetchSupported) {
18976
18977
  return false;
18977
18978
  }
@@ -18989,7 +18990,7 @@ var factory = (env) => {
18989
18990
  }).headers.has("Content-Type");
18990
18991
  return duplexAccessed && !hasContentType;
18991
18992
  });
18992
- const supportsResponseStream = isResponseSupported && isReadableStreamSupported && test(() => utils_default.isReadableStream(new Response("").body));
18993
+ const supportsResponseStream = isResponseSupported && isReadableStreamSupported && test(() => utils_default.isReadableStream(new Response2("").body));
18993
18994
  const resolvers = {
18994
18995
  stream: supportsResponseStream && ((res) => res.body)
18995
18996
  };
@@ -19100,7 +19101,7 @@ var factory = (env) => {
19100
19101
  responseContentLength,
19101
19102
  progressEventReducer(asyncDecorator(onDownloadProgress), true)
19102
19103
  ) || [];
19103
- response = new Response(
19104
+ response = new Response2(
19104
19105
  trackStream(response.body, DEFAULT_CHUNK_SIZE, onProgress, () => {
19105
19106
  flush && flush();
19106
19107
  unsubscribe && unsubscribe();
@@ -19138,10 +19139,10 @@ var factory = (env) => {
19138
19139
  var seedCache = /* @__PURE__ */ new Map();
19139
19140
  var getFetch = (config) => {
19140
19141
  let env = config && config.env || {};
19141
- const { fetch: fetch2, Request, Response } = env;
19142
+ const { fetch: fetch2, Request, Response: Response2 } = env;
19142
19143
  const seeds = [
19143
19144
  Request,
19144
- Response,
19145
+ Response2,
19145
19146
  fetch2
19146
19147
  ];
19147
19148
  let len = seeds.length, i = len, seed, target, map = seedCache;
@@ -98802,15 +98803,15 @@ function startFeishuGateway(options) {
98802
98803
  hasContent: !!message.content
98803
98804
  });
98804
98805
  if (messageType !== "text" || !message.content) return;
98805
- let text;
98806
+ let text2;
98806
98807
  try {
98807
98808
  const parsed = JSON.parse(message.content);
98808
- text = (parsed.text ?? "").trim();
98809
+ text2 = (parsed.text ?? "").trim();
98809
98810
  } catch {
98810
98811
  return;
98811
98812
  }
98812
- text = text.replace(/@_user_\d+\s*/g, "").trim();
98813
- if (!text) return;
98813
+ text2 = text2.replace(/@_user_\d+\s*/g, "").trim();
98814
+ if (!text2) return;
98814
98815
  const chatType = message.chat_type === "group" ? "group" : "p2p";
98815
98816
  let shouldReply = true;
98816
98817
  if (chatType === "group") {
@@ -98828,7 +98829,7 @@ function startFeishuGateway(options) {
98828
98829
  chatId: String(chatId),
98829
98830
  messageId: messageId ?? "",
98830
98831
  messageType,
98831
- content: text,
98832
+ content: text2,
98832
98833
  chatType,
98833
98834
  senderId,
98834
98835
  rootId,
@@ -98840,7 +98841,7 @@ function startFeishuGateway(options) {
98840
98841
  messageId: messageId ?? "",
98841
98842
  chatType,
98842
98843
  shouldReply,
98843
- textPreview: text.slice(0, 80)
98844
+ textPreview: text2.slice(0, 80)
98844
98845
  });
98845
98846
  await onMessage(ctx);
98846
98847
  } catch (err) {
@@ -98894,7 +98895,7 @@ function startFeishuGateway(options) {
98894
98895
  }
98895
98896
 
98896
98897
  // src/feishu/sender.ts
98897
- async function sendTextMessage(client, chatId, text) {
98898
+ async function sendTextMessage(client, chatId, text2) {
98898
98899
  if (!chatId?.trim()) {
98899
98900
  return { ok: false, error: "No chat_id provided" };
98900
98901
  }
@@ -98904,7 +98905,7 @@ async function sendTextMessage(client, chatId, text) {
98904
98905
  data: {
98905
98906
  receive_id: chatId.trim(),
98906
98907
  msg_type: "text",
98907
- content: JSON.stringify({ text })
98908
+ content: JSON.stringify({ text: text2 })
98908
98909
  }
98909
98910
  });
98910
98911
  return { ok: true, messageId: res?.data?.message_id ?? "" };
@@ -98912,13 +98913,13 @@ async function sendTextMessage(client, chatId, text) {
98912
98913
  return { ok: false, error: err instanceof Error ? err.message : String(err) };
98913
98914
  }
98914
98915
  }
98915
- async function updateMessage(client, messageId, text) {
98916
+ async function updateMessage(client, messageId, text2) {
98916
98917
  try {
98917
98918
  await client.im.message.update({
98918
98919
  path: { message_id: messageId },
98919
98920
  data: {
98920
98921
  msg_type: "text",
98921
- content: JSON.stringify({ text })
98922
+ content: JSON.stringify({ text: text2 })
98922
98923
  }
98923
98924
  });
98924
98925
  return { ok: true, messageId };
@@ -99076,17 +99077,17 @@ async function handleChat(ctx, deps) {
99076
99077
  while (Date.now() - start < timeout) {
99077
99078
  await new Promise((r) => setTimeout(r, pollInterval));
99078
99079
  const { data: messages } = await client.session.messages({ path: { id: session.id }, query });
99079
- const text = extractLastAssistantText(messages ?? []);
99080
- if (text && text !== lastText) {
99081
- lastText = text;
99080
+ const text2 = extractLastAssistantText(messages ?? []);
99081
+ if (text2 && text2 !== lastText) {
99082
+ lastText = text2;
99082
99083
  sameCount = 0;
99083
99084
  if (placeholderId) {
99084
99085
  try {
99085
- await updateMessage(feishuClient, placeholderId, text);
99086
+ await updateMessage(feishuClient, placeholderId, text2);
99086
99087
  } catch {
99087
99088
  }
99088
99089
  }
99089
- } else if (text && text.length > 0) {
99090
+ } else if (text2 && text2.length > 0) {
99090
99091
  sameCount++;
99091
99092
  if (sameCount >= stablePolls) break;
99092
99093
  }
@@ -99106,15 +99107,15 @@ async function handleChat(ctx, deps) {
99106
99107
  unregisterPending(session.id);
99107
99108
  }
99108
99109
  }
99109
- async function replyOrUpdate(feishuClient, chatId, placeholderId, text) {
99110
+ async function replyOrUpdate(feishuClient, chatId, placeholderId, text2) {
99110
99111
  if (placeholderId) {
99111
99112
  try {
99112
- await updateMessage(feishuClient, placeholderId, text);
99113
+ await updateMessage(feishuClient, placeholderId, text2);
99113
99114
  } catch {
99114
- await sendTextMessage(feishuClient, chatId, text);
99115
+ await sendTextMessage(feishuClient, chatId, text2);
99115
99116
  }
99116
99117
  } else {
99117
- await sendTextMessage(feishuClient, chatId, text);
99118
+ await sendTextMessage(feishuClient, chatId, text2);
99118
99119
  }
99119
99120
  }
99120
99121
  function extractLastAssistantText(messages) {
@@ -99164,18 +99165,18 @@ async function fetchRecentMessages(client, chatId, maxMessages, log) {
99164
99165
  for (const item of items) {
99165
99166
  if (item.deleted) continue;
99166
99167
  if (item.msg_type !== "text" || !item.body?.content) continue;
99167
- let text;
99168
+ let text2;
99168
99169
  try {
99169
99170
  const parsed = JSON.parse(item.body.content);
99170
- text = (parsed.text ?? "").trim();
99171
+ text2 = (parsed.text ?? "").trim();
99171
99172
  } catch {
99172
99173
  continue;
99173
99174
  }
99174
- if (!text) continue;
99175
+ if (!text2) continue;
99175
99176
  result.push({
99176
99177
  senderType: item.sender?.sender_type ?? "unknown",
99177
99178
  senderId: item.sender?.id ?? "",
99178
- content: text,
99179
+ content: text2,
99179
99180
  createTime: item.create_time ?? ""
99180
99181
  });
99181
99182
  if (result.length >= maxMessages) break;
@@ -99244,7 +99245,9 @@ var FeishuPlugin = async (ctx) => {
99244
99245
  }
99245
99246
  let feishuRaw;
99246
99247
  try {
99247
- feishuRaw = JSON.parse(readFileSync(configPath, "utf-8"));
99248
+ feishuRaw = resolveEnvPlaceholders(
99249
+ JSON.parse(readFileSync(configPath, "utf-8"))
99250
+ );
99248
99251
  } catch (parseErr) {
99249
99252
  throw new Error(`\u98DE\u4E66\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1A${configPath} \u5FC5\u987B\u662F\u5408\u6CD5\u7684 JSON (${parseErr})`);
99250
99253
  }
@@ -99305,8 +99308,55 @@ var FeishuPlugin = async (ctx) => {
99305
99308
  };
99306
99309
  return hooks;
99307
99310
  };
99311
+ function resolveEnvPlaceholders(obj) {
99312
+ if (typeof obj === "string") {
99313
+ if (!obj.includes("${")) return obj;
99314
+ return obj.replace(/\$\{(\w+)\}/g, (_match, name) => {
99315
+ const val = process.env[name];
99316
+ if (val === void 0) {
99317
+ throw new Error(`\u73AF\u5883\u53D8\u91CF ${name} \u672A\u8BBE\u7F6E\uFF08\u914D\u7F6E\u503C\u5F15\u7528\u4E86 \${${name}}\uFF09`);
99318
+ }
99319
+ return val;
99320
+ });
99321
+ }
99322
+ if (Array.isArray(obj)) {
99323
+ return obj.map(resolveEnvPlaceholders);
99324
+ }
99325
+ if (obj !== null && typeof obj === "object") {
99326
+ const result = {};
99327
+ for (const [key, value] of Object.entries(obj)) {
99328
+ result[key] = resolveEnvPlaceholders(value);
99329
+ }
99330
+ return result;
99331
+ }
99332
+ return obj;
99333
+ }
99334
+ function proxyFetch(url2, init) {
99335
+ const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || process.env.ALL_PROXY || "";
99336
+ if (!proxyUrl) return fetch(url2, init);
99337
+ const parsed = new URL(url2);
99338
+ return new Promise((resolve, reject) => {
99339
+ const req = request(
99340
+ {
99341
+ hostname: parsed.hostname,
99342
+ path: parsed.pathname + parsed.search,
99343
+ method: init?.method ?? "GET",
99344
+ headers: init?.headers,
99345
+ agent: new HttpsProxyAgent(proxyUrl)
99346
+ },
99347
+ (res) => {
99348
+ text(res).then(
99349
+ (body) => resolve(new Response(body, { status: res.statusCode ?? 0 }))
99350
+ ).catch(reject);
99351
+ }
99352
+ );
99353
+ req.on("error", reject);
99354
+ if (init?.body) req.write(init.body);
99355
+ req.end();
99356
+ });
99357
+ }
99308
99358
  async function fetchBotOpenId(appId, appSecret, log) {
99309
- const tokenRes = await fetch("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", {
99359
+ const tokenRes = await proxyFetch("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", {
99310
99360
  method: "POST",
99311
99361
  headers: { "Content-Type": "application/json" },
99312
99362
  body: JSON.stringify({ app_id: appId, app_secret: appSecret })
@@ -99316,7 +99366,7 @@ async function fetchBotOpenId(appId, appSecret, log) {
99316
99366
  if (!token) {
99317
99367
  throw new Error("\u83B7\u53D6 tenant_access_token \u5931\u8D25\uFF0C\u65E0\u6CD5\u542F\u52A8\u7FA4\u804A @\u63D0\u53CA\u68C0\u6D4B");
99318
99368
  }
99319
- const botRes = await fetch("https://open.feishu.cn/open-apis/bot/v3/info", {
99369
+ const botRes = await proxyFetch("https://open.feishu.cn/open-apis/bot/v3/info", {
99320
99370
  method: "GET",
99321
99371
  headers: { Authorization: `Bearer ${token}` }
99322
99372
  });