opencode-feishu 0.3.6 → 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 +60 -35
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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(
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
98806
|
+
let text2;
|
|
98806
98807
|
try {
|
|
98807
98808
|
const parsed = JSON.parse(message.content);
|
|
98808
|
-
|
|
98809
|
+
text2 = (parsed.text ?? "").trim();
|
|
98809
98810
|
} catch {
|
|
98810
98811
|
return;
|
|
98811
98812
|
}
|
|
98812
|
-
|
|
98813
|
-
if (!
|
|
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:
|
|
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:
|
|
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,
|
|
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,
|
|
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
|
|
99080
|
-
if (
|
|
99081
|
-
lastText =
|
|
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,
|
|
99086
|
+
await updateMessage(feishuClient, placeholderId, text2);
|
|
99086
99087
|
} catch {
|
|
99087
99088
|
}
|
|
99088
99089
|
}
|
|
99089
|
-
} else if (
|
|
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,
|
|
99110
|
+
async function replyOrUpdate(feishuClient, chatId, placeholderId, text2) {
|
|
99110
99111
|
if (placeholderId) {
|
|
99111
99112
|
try {
|
|
99112
|
-
await updateMessage(feishuClient, placeholderId,
|
|
99113
|
+
await updateMessage(feishuClient, placeholderId, text2);
|
|
99113
99114
|
} catch {
|
|
99114
|
-
await sendTextMessage(feishuClient, chatId,
|
|
99115
|
+
await sendTextMessage(feishuClient, chatId, text2);
|
|
99115
99116
|
}
|
|
99116
99117
|
} else {
|
|
99117
|
-
await sendTextMessage(feishuClient, chatId,
|
|
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
|
|
99168
|
+
let text2;
|
|
99168
99169
|
try {
|
|
99169
99170
|
const parsed = JSON.parse(item.body.content);
|
|
99170
|
-
|
|
99171
|
+
text2 = (parsed.text ?? "").trim();
|
|
99171
99172
|
} catch {
|
|
99172
99173
|
continue;
|
|
99173
99174
|
}
|
|
99174
|
-
if (!
|
|
99175
|
+
if (!text2) continue;
|
|
99175
99176
|
result.push({
|
|
99176
99177
|
senderType: item.sender?.sender_type ?? "unknown",
|
|
99177
99178
|
senderId: item.sender?.id ?? "",
|
|
99178
|
-
content:
|
|
99179
|
+
content: text2,
|
|
99179
99180
|
createTime: item.create_time ?? ""
|
|
99180
99181
|
});
|
|
99181
99182
|
if (result.length >= maxMessages) break;
|
|
@@ -99330,8 +99331,32 @@ function resolveEnvPlaceholders(obj) {
|
|
|
99330
99331
|
}
|
|
99331
99332
|
return obj;
|
|
99332
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
|
+
}
|
|
99333
99358
|
async function fetchBotOpenId(appId, appSecret, log) {
|
|
99334
|
-
const tokenRes = await
|
|
99359
|
+
const tokenRes = await proxyFetch("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", {
|
|
99335
99360
|
method: "POST",
|
|
99336
99361
|
headers: { "Content-Type": "application/json" },
|
|
99337
99362
|
body: JSON.stringify({ app_id: appId, app_secret: appSecret })
|
|
@@ -99341,7 +99366,7 @@ async function fetchBotOpenId(appId, appSecret, log) {
|
|
|
99341
99366
|
if (!token) {
|
|
99342
99367
|
throw new Error("\u83B7\u53D6 tenant_access_token \u5931\u8D25\uFF0C\u65E0\u6CD5\u542F\u52A8\u7FA4\u804A @\u63D0\u53CA\u68C0\u6D4B");
|
|
99343
99368
|
}
|
|
99344
|
-
const botRes = await
|
|
99369
|
+
const botRes = await proxyFetch("https://open.feishu.cn/open-apis/bot/v3/info", {
|
|
99345
99370
|
method: "GET",
|
|
99346
99371
|
headers: { Authorization: `Bearer ${token}` }
|
|
99347
99372
|
});
|