@cydm/pie 4.3.0 → 4.3.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.
|
@@ -447,10 +447,10 @@ function resolveConfig(cwd, dir) {
|
|
|
447
447
|
for (const file of configPaths(cwd, dir)) {
|
|
448
448
|
const layer = readLayer(file);
|
|
449
449
|
if (Object.keys(layer).length) present = true;
|
|
450
|
-
const legacy = ["chatId", "receiveId", "openId", "larkCli", "allowUsers"].some((key) => key in layer);
|
|
450
|
+
const legacy = ["chatId", "receiveId", "openId", "larkCli", "allowUsers", "groupAccess"].some((key) => key in layer);
|
|
451
451
|
if (legacy && !layer.provider) {
|
|
452
452
|
const previous = providers.feishu ?? {};
|
|
453
|
-
providers.feishu = { ...previous, ...Object.fromEntries(["chatId", "openId", "larkCli", "allowUsers"].filter((key) => key in layer).map((key) => [key, layer[key]])), ...layer.receiveId && !layer.chatId ? { chatId: layer.receiveId } : {} };
|
|
453
|
+
providers.feishu = { ...previous, ...Object.fromEntries(["chatId", "openId", "larkCli", "allowUsers", "groupAccess"].filter((key) => key in layer).map((key) => [key, layer[key]])), ...layer.receiveId && !layer.chatId ? { chatId: layer.receiveId } : {} };
|
|
454
454
|
merged.provider = "feishu";
|
|
455
455
|
}
|
|
456
456
|
if (isRecord(layer.providers)) {
|
|
@@ -1079,39 +1079,43 @@ function findOwnerInstance(sessionId) {
|
|
|
1079
1079
|
} catch {
|
|
1080
1080
|
return void 0;
|
|
1081
1081
|
}
|
|
1082
|
+
const candidates = [];
|
|
1082
1083
|
for (const name of entries.slice(0, 256)) {
|
|
1083
1084
|
if (!name.endsWith(".json")) continue;
|
|
1084
1085
|
try {
|
|
1085
1086
|
const parsed = JSON.parse(fs5.readFileSync(path5.join(discoveryDir(), name), "utf8"));
|
|
1086
1087
|
if (parsed.instanceId === name.slice(0, -".json".length) && parsed.sessionId === sessionId && parsed.port && typeof parsed.secret === "string" && parsed.protocol === IPC_PROTOCOL_VERSION && pidAlive4(parsed.pid ?? 0)) {
|
|
1087
|
-
|
|
1088
|
+
candidates.push({ file: parsed, startedAt: typeof parsed.startedAt === "number" ? parsed.startedAt : 0 });
|
|
1088
1089
|
}
|
|
1089
1090
|
} catch {
|
|
1090
1091
|
}
|
|
1091
1092
|
}
|
|
1092
|
-
return void 0;
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
return createHmac("sha256", "").update(JSON.stringify(payload)).digest("hex");
|
|
1093
|
+
if (!candidates.length) return void 0;
|
|
1094
|
+
candidates.sort((a, b) => b.startedAt - a.startedAt);
|
|
1095
|
+
return candidates[0].file;
|
|
1096
1096
|
}
|
|
1097
|
-
|
|
1097
|
+
var SYNTHETIC_SENDER = (() => {
|
|
1098
|
+
const hex = randomUUID3().replace(/-/g, "");
|
|
1099
|
+
return `pie-${hex.slice(0, 12)}`;
|
|
1100
|
+
})();
|
|
1101
|
+
function deliverOnce(port, secret, text, targetSessionId, targetInstanceId) {
|
|
1098
1102
|
return new Promise((resolve4) => {
|
|
1099
1103
|
const id = randomUUID3();
|
|
1100
1104
|
const envelope = {
|
|
1101
1105
|
v: IPC_PROTOCOL_VERSION,
|
|
1102
1106
|
id,
|
|
1103
|
-
from:
|
|
1104
|
-
to:
|
|
1107
|
+
from: SYNTHETIC_SENDER,
|
|
1108
|
+
to: targetInstanceId,
|
|
1105
1109
|
kind: "message",
|
|
1106
1110
|
createdAt: Date.now(),
|
|
1107
1111
|
text,
|
|
1108
|
-
sessionId:
|
|
1112
|
+
sessionId: targetSessionId,
|
|
1109
1113
|
waitMs: 0
|
|
1110
1114
|
};
|
|
1111
1115
|
const payload = [envelope.v, envelope.id, envelope.from, envelope.to, envelope.kind, envelope.createdAt, envelope.sessionId ?? null, envelope.text ?? null, envelope.replyTo ?? null, envelope.waitMs ?? null, envelope.notifyCompletion ?? null, envelope.control ?? null, envelope.userAssigned ?? null, envelope.subscriberSession ?? null];
|
|
1112
1116
|
const frame = JSON.stringify({ ...envelope, auth: createHmac("sha256", secret).update(JSON.stringify(payload)).digest("hex") });
|
|
1113
|
-
void sign;
|
|
1114
1117
|
const socket = new net.Socket();
|
|
1118
|
+
let buffer = "";
|
|
1115
1119
|
let settled = false;
|
|
1116
1120
|
const finish = (ok) => {
|
|
1117
1121
|
if (settled) return;
|
|
@@ -1122,10 +1126,34 @@ function deliverOnce(port, secret, text) {
|
|
|
1122
1126
|
};
|
|
1123
1127
|
const timer = setTimeout(() => finish(false), 5e3);
|
|
1124
1128
|
socket.once("error", () => finish(false));
|
|
1129
|
+
socket.once("end", () => finish(false));
|
|
1130
|
+
socket.once("close", (hadError) => {
|
|
1131
|
+
if (!hadError) finish(false);
|
|
1132
|
+
});
|
|
1125
1133
|
socket.once("connect", () => {
|
|
1126
1134
|
socket.write(`${frame}
|
|
1127
1135
|
`);
|
|
1128
|
-
|
|
1136
|
+
});
|
|
1137
|
+
socket.on("data", (chunk) => {
|
|
1138
|
+
buffer += chunk.toString("utf8");
|
|
1139
|
+
let lineEnd = buffer.indexOf("\n");
|
|
1140
|
+
if (lineEnd < 0) return;
|
|
1141
|
+
while (lineEnd >= 0) {
|
|
1142
|
+
const line = buffer.slice(0, lineEnd);
|
|
1143
|
+
buffer = buffer.slice(lineEnd + 1);
|
|
1144
|
+
let reply = null;
|
|
1145
|
+
try {
|
|
1146
|
+
reply = JSON.parse(line);
|
|
1147
|
+
} catch {
|
|
1148
|
+
reply = null;
|
|
1149
|
+
}
|
|
1150
|
+
if (reply && (reply.replyTo === id || reply.id === `${id}:r`) && (reply.kind === "result" || reply.kind === "ack")) {
|
|
1151
|
+
const text2 = typeof reply.text === "string" ? reply.text : "";
|
|
1152
|
+
finish(!/rejected|denied/i.test(text2));
|
|
1153
|
+
return;
|
|
1154
|
+
}
|
|
1155
|
+
lineEnd = buffer.indexOf("\n");
|
|
1156
|
+
}
|
|
1129
1157
|
});
|
|
1130
1158
|
try {
|
|
1131
1159
|
socket.connect(port, "127.0.0.1");
|
|
@@ -1137,7 +1165,7 @@ function deliverOnce(port, secret, text) {
|
|
|
1137
1165
|
async function dispatchEventToSession(sessionId, eventText) {
|
|
1138
1166
|
const owner = findOwnerInstance(sessionId);
|
|
1139
1167
|
if (!owner?.port || !owner.secret) return void 0;
|
|
1140
|
-
const ok = await deliverOnce(owner.port, owner.secret, eventText);
|
|
1168
|
+
const ok = await deliverOnce(owner.port, owner.secret, eventText, owner.sessionId ?? "", owner.instanceId);
|
|
1141
1169
|
return ok ? owner.instanceId : void 0;
|
|
1142
1170
|
}
|
|
1143
1171
|
|
|
@@ -1198,10 +1226,17 @@ function loadPersistedAnchor(sessionId, identity) {
|
|
|
1198
1226
|
try {
|
|
1199
1227
|
const raw = fs7.readFileSync(anchorPath(sessionId), "utf8");
|
|
1200
1228
|
const parsed = JSON.parse(raw);
|
|
1201
|
-
if (typeof parsed.anchor?.messageId === "string" && parsed.anchor.messageId && typeof parsed.anchor.at === "number" && parsed.provider === identity.provider &&
|
|
1229
|
+
if (typeof parsed.anchor?.messageId === "string" && parsed.anchor.messageId && typeof parsed.anchor.at === "number" && parsed.provider === identity.provider && // v2 group-home (11-12): the home is the APP'S WHOLE PRESENCE, so an
|
|
1230
|
+
// anchor may live in ANY room — a group chat id differs from the
|
|
1231
|
+
// configured DM target and must NOT invalidate the anchor (the old
|
|
1232
|
+
// equality here silently forked a group-anchored session back to the
|
|
1233
|
+
// DM root on restart). The provider check stays: a re-bound app is a
|
|
1234
|
+
// different home entirely.
|
|
1235
|
+
(parsed.targetId === identity.targetId || typeof parsed.anchor.home === "string")) {
|
|
1202
1236
|
return {
|
|
1203
1237
|
messageId: parsed.anchor.messageId,
|
|
1204
1238
|
...typeof parsed.anchor.threadId === "string" && parsed.anchor.threadId ? { threadId: parsed.anchor.threadId } : {},
|
|
1239
|
+
...typeof parsed.anchor.home === "string" && parsed.anchor.home ? { home: parsed.anchor.home } : {},
|
|
1205
1240
|
at: parsed.anchor.at
|
|
1206
1241
|
};
|
|
1207
1242
|
}
|
|
@@ -1357,11 +1392,12 @@ function createRuntime(ctx, cfg, connection, liveEnabled, persistEnabled) {
|
|
|
1357
1392
|
const anchor = replyAnchor.get(sessionId);
|
|
1358
1393
|
return anchor ? { messageId: anchor.messageId, threadId: anchor.threadId } : { threadId: null };
|
|
1359
1394
|
}
|
|
1360
|
-
function persistSessionAnchor(sessionId, messageId, threadId) {
|
|
1395
|
+
function persistSessionAnchor(sessionId, messageId, threadId, home) {
|
|
1361
1396
|
replyAnchor.set(sessionId, { messageId, threadId, at: Date.now() });
|
|
1362
1397
|
savePersistedAnchor(sessionId, anchorIdentity, {
|
|
1363
1398
|
messageId,
|
|
1364
1399
|
...threadId ? { threadId } : {},
|
|
1400
|
+
...home ? { home } : {},
|
|
1365
1401
|
at: Date.now()
|
|
1366
1402
|
});
|
|
1367
1403
|
}
|
|
@@ -1732,7 +1768,7 @@ ${pending.caption || IM_INPUT_FALLBACK_CAPTION}` : pending.caption).trim();
|
|
|
1732
1768
|
}
|
|
1733
1769
|
if (opener) registerRoute(opener, sessionId);
|
|
1734
1770
|
}
|
|
1735
|
-
persistSessionAnchor(sessionId, sent.messageId, threadId);
|
|
1771
|
+
persistSessionAnchor(sessionId, sent.messageId, threadId, cfg.targetId);
|
|
1736
1772
|
if (threadId) {
|
|
1737
1773
|
claimThreadOwnership({ provider: cfg.provider, threadId }, sessionId);
|
|
1738
1774
|
}
|
|
@@ -1927,24 +1963,37 @@ ${pending.caption || IM_INPUT_FALLBACK_CAPTION}` : pending.caption).trim();
|
|
|
1927
1963
|
}
|
|
1928
1964
|
async function dispatchToOwner(event, meta) {
|
|
1929
1965
|
let ownerSession;
|
|
1966
|
+
let unknown = false;
|
|
1930
1967
|
if (meta.threadId) {
|
|
1931
|
-
|
|
1968
|
+
const t = threadOwner({ provider: cfg.provider, threadId: meta.threadId });
|
|
1969
|
+
if (t.corrupt || t.unavailable) unknown = true;
|
|
1970
|
+
ownerSession = t.sessionId;
|
|
1932
1971
|
}
|
|
1933
|
-
if (!ownerSession && meta.replyTo) {
|
|
1972
|
+
if (!ownerSession && !unknown && meta.replyTo) {
|
|
1934
1973
|
ownerSession = sessionRootOwner(cfg.provider, meta.replyTo)?.sessionId;
|
|
1935
1974
|
}
|
|
1936
|
-
if (
|
|
1975
|
+
if (unknown) return { kind: "unknown-owner" };
|
|
1976
|
+
if (!ownerSession) return { kind: "no-owner" };
|
|
1977
|
+
if (ownerSession === currentSessionId) return { kind: "no-owner" };
|
|
1937
1978
|
const payload = `${DISPATCH_PREFIX} ${JSON.stringify({ v: 1, binding: bindingKey(), event })}`;
|
|
1938
1979
|
try {
|
|
1939
|
-
|
|
1980
|
+
const delivered = await dispatchEventToSession(ownerSession, payload);
|
|
1981
|
+
return delivered ? { kind: "dispatched", owner: delivered } : { kind: "delivery-failed", owner: ownerSession };
|
|
1940
1982
|
} catch {
|
|
1941
|
-
return
|
|
1983
|
+
return { kind: "delivery-failed", owner: ownerSession };
|
|
1942
1984
|
}
|
|
1943
1985
|
}
|
|
1944
1986
|
async function handleInboundEventInner(event, retry) {
|
|
1945
|
-
const
|
|
1946
|
-
if (
|
|
1947
|
-
|
|
1987
|
+
const isGroup = event.chatType === "group";
|
|
1988
|
+
if (isGroup) {
|
|
1989
|
+
if (cfg.groupAccess === "off") return;
|
|
1990
|
+
if (cfg.groupAccess === "allowUsers" && (!event.sender_id || !cfg.allowUsers?.includes(event.sender_id)))
|
|
1991
|
+
return;
|
|
1992
|
+
} else {
|
|
1993
|
+
const matchesTarget = cfg.targetKind === "user" ? event.sender_id === cfg.targetId && (event.chatType === void 0 || event.chatType === "p2p") : event.chat_id === cfg.targetId || event.chatType === "p2p";
|
|
1994
|
+
if (!matchesTarget) return;
|
|
1995
|
+
}
|
|
1996
|
+
if (!isGroup && cfg.allowUsers && (!event.sender_id || !cfg.allowUsers.includes(event.sender_id)))
|
|
1948
1997
|
return;
|
|
1949
1998
|
const messageId = event.message_id;
|
|
1950
1999
|
if (!messageId) return;
|
|
@@ -2024,9 +2073,38 @@ ${pending.caption || IM_INPUT_FALLBACK_CAPTION}` : pending.caption).trim();
|
|
|
2024
2073
|
return;
|
|
2025
2074
|
}
|
|
2026
2075
|
if (consumerLock) {
|
|
2027
|
-
const
|
|
2028
|
-
if (dispatched) {
|
|
2029
|
-
ctx.log(`im-forward: event ${messageId} dispatched to owner ${
|
|
2076
|
+
const outcome = await dispatchToOwner(event, meta);
|
|
2077
|
+
if (outcome.kind === "dispatched") {
|
|
2078
|
+
ctx.log(`im-forward: event ${messageId} dispatched to owner ${outcome.owner}`);
|
|
2079
|
+
return;
|
|
2080
|
+
}
|
|
2081
|
+
if (outcome.kind === "delivery-failed") {
|
|
2082
|
+
ctx.log(`im-forward: dispatch to ${outcome.owner} failed for ${messageId}; dropping`);
|
|
2083
|
+
return;
|
|
2084
|
+
}
|
|
2085
|
+
if (outcome.kind === "unknown-owner") {
|
|
2086
|
+
ctx.log(`im-forward: thread ownership unreadable for ${messageId}; dropping (fail closed)`);
|
|
2087
|
+
return;
|
|
2088
|
+
}
|
|
2089
|
+
if (isConcierge && event.chatType === "group" && meta.threadId) {
|
|
2090
|
+
if (!scope.isCurrent()) {
|
|
2091
|
+
releaseInvalidatedEvent();
|
|
2092
|
+
return;
|
|
2093
|
+
}
|
|
2094
|
+
const claim = claimThreadOwnership({ provider: cfg.provider, threadId: meta.threadId }, sessionId);
|
|
2095
|
+
if (!claim.claimed) {
|
|
2096
|
+
ctx.log(`im-forward: unowned-topic takeover lost the claim (${claim.reason}); ${messageId} dropped`);
|
|
2097
|
+
return;
|
|
2098
|
+
}
|
|
2099
|
+
ctx.log(`im-forward: unowned group topic ${meta.threadId} taken by concierge (claimed)`);
|
|
2100
|
+
await acceptRoutedMessage(
|
|
2101
|
+
{ messageId: "", threadId: meta.threadId, sessionId, sentAt: Date.now() },
|
|
2102
|
+
event,
|
|
2103
|
+
meta,
|
|
2104
|
+
scope,
|
|
2105
|
+
address,
|
|
2106
|
+
sessionId
|
|
2107
|
+
);
|
|
2030
2108
|
return;
|
|
2031
2109
|
}
|
|
2032
2110
|
}
|
|
@@ -2083,7 +2161,10 @@ ${caption || IM_INPUT_FALLBACK_CAPTION}`;
|
|
|
2083
2161
|
persistSessionAnchor(
|
|
2084
2162
|
route.sessionId,
|
|
2085
2163
|
messageId,
|
|
2086
|
-
route.threadId ?? meta.threadId ?? null
|
|
2164
|
+
route.threadId ?? meta.threadId ?? null,
|
|
2165
|
+
// v2 group-home: the room the user spoke in IS the anchor's home —
|
|
2166
|
+
// a group topic keeps living in the group across restarts.
|
|
2167
|
+
event.chat_id
|
|
2087
2168
|
);
|
|
2088
2169
|
claimSessionRoot(cfg.provider, messageId, route.sessionId);
|
|
2089
2170
|
if (route.threadId ?? meta.threadId) {
|
|
@@ -2208,6 +2289,7 @@ ${caption || IM_INPUT_FALLBACK_CAPTION}`;
|
|
|
2208
2289
|
}
|
|
2209
2290
|
async function resolveRouteByThreadRoot(threadId) {
|
|
2210
2291
|
const messages = await fetchThreadMessages(threadId);
|
|
2292
|
+
if (messages === "forbidden") return { notOurs: true };
|
|
2211
2293
|
if (!messages) return { transient: true };
|
|
2212
2294
|
const rootId = messages[0];
|
|
2213
2295
|
if (!rootId) return { notOurs: true };
|
|
@@ -2234,12 +2316,43 @@ ${caption || IM_INPUT_FALLBACK_CAPTION}`;
|
|
|
2234
2316
|
});
|
|
2235
2317
|
}
|
|
2236
2318
|
let consumerLock;
|
|
2319
|
+
let resolvedBindingKey;
|
|
2237
2320
|
function bindingKey() {
|
|
2238
|
-
return `${cfg.provider}:${cfg.targetId}`;
|
|
2321
|
+
return resolvedBindingKey ?? `${cfg.provider}:${cfg.appId ?? (cfg.profile ? `profile:${cfg.profile}` : cfg.targetId)}`;
|
|
2322
|
+
}
|
|
2323
|
+
function resolveBindingKeySync() {
|
|
2324
|
+
if (resolvedBindingKey) return resolvedBindingKey;
|
|
2325
|
+
if (cfg.appId || !connection.canonicalAppId) {
|
|
2326
|
+
resolvedBindingKey = `${cfg.provider}:${cfg.appId ?? (cfg.profile ? `profile:${cfg.profile}` : cfg.targetId)}`;
|
|
2327
|
+
}
|
|
2328
|
+
return resolvedBindingKey ?? bindingKey();
|
|
2329
|
+
}
|
|
2330
|
+
async function resolveBindingKey() {
|
|
2331
|
+
if (resolvedBindingKey) return resolvedBindingKey;
|
|
2332
|
+
if (cfg.appId || !connection.canonicalAppId) return resolveBindingKeySync();
|
|
2333
|
+
const canonical = await connection.canonicalAppId().catch(() => void 0);
|
|
2334
|
+
if (canonical === void 0) return void 0;
|
|
2335
|
+
resolvedBindingKey = `${cfg.provider}:${canonical || (cfg.profile ? `profile:${cfg.profile}` : cfg.targetId)}`;
|
|
2336
|
+
return resolvedBindingKey;
|
|
2337
|
+
}
|
|
2338
|
+
let consumerStartupInFlight = false;
|
|
2339
|
+
function needsCanonicalResolution() {
|
|
2340
|
+
return !resolvedBindingKey && !cfg.appId && !!cfg.profile && connection.canonicalAppId !== void 0;
|
|
2239
2341
|
}
|
|
2240
|
-
function
|
|
2342
|
+
function acquireConsumerSlotSync() {
|
|
2241
2343
|
if (consumerLock) return true;
|
|
2242
|
-
|
|
2344
|
+
if (needsCanonicalResolution()) return false;
|
|
2345
|
+
return acquireClaimedSlot(resolveBindingKeySync());
|
|
2346
|
+
}
|
|
2347
|
+
async function acquireConsumerSlot() {
|
|
2348
|
+
if (consumerLock) return true;
|
|
2349
|
+
if (!needsCanonicalResolution()) return acquireClaimedSlot(resolveBindingKeySync());
|
|
2350
|
+
const resolved = await resolveBindingKey();
|
|
2351
|
+
return acquireClaimedSlot(resolved);
|
|
2352
|
+
}
|
|
2353
|
+
function acquireClaimedSlot(binding) {
|
|
2354
|
+
if (consumerLock) return true;
|
|
2355
|
+
const claim = tryClaimConsumer(binding, `pie-${currentSessionId ?? "unknown"}`, () => {
|
|
2243
2356
|
ctx.log("im-forward: event-hub slot lost to another consumer; retiring ours");
|
|
2244
2357
|
consumerLock = void 0;
|
|
2245
2358
|
if (consumer || consumerRecovering) {
|
|
@@ -2258,11 +2371,38 @@ ${caption || IM_INPUT_FALLBACK_CAPTION}`;
|
|
|
2258
2371
|
consumerLock = claim;
|
|
2259
2372
|
return true;
|
|
2260
2373
|
}
|
|
2261
|
-
function spawnConsumer() {
|
|
2374
|
+
async function spawnConsumer() {
|
|
2262
2375
|
restartTimer = void 0;
|
|
2263
2376
|
restartDueAt = void 0;
|
|
2264
2377
|
if (terminated || suspended || consumer || consumerRecovering) return;
|
|
2265
|
-
if (
|
|
2378
|
+
if (needsCanonicalResolution()) {
|
|
2379
|
+
if (consumerStartupInFlight) return;
|
|
2380
|
+
consumerStartupInFlight = true;
|
|
2381
|
+
let key2;
|
|
2382
|
+
try {
|
|
2383
|
+
key2 = await resolveBindingKey();
|
|
2384
|
+
if (terminated || suspended || consumer || consumerRecovering) return;
|
|
2385
|
+
} finally {
|
|
2386
|
+
consumerStartupInFlight = false;
|
|
2387
|
+
}
|
|
2388
|
+
if (!key2) {
|
|
2389
|
+
restartTimer = setTimeout(spawnConsumer, 15e3);
|
|
2390
|
+
restartTimer.unref?.();
|
|
2391
|
+
restartDueAt = Date.now() + 15e3;
|
|
2392
|
+
return;
|
|
2393
|
+
}
|
|
2394
|
+
if (!acquireClaimedSlot(key2)) {
|
|
2395
|
+
restartTimer = setTimeout(spawnConsumer, 15e3);
|
|
2396
|
+
restartTimer.unref?.();
|
|
2397
|
+
restartDueAt = Date.now() + 15e3;
|
|
2398
|
+
return;
|
|
2399
|
+
}
|
|
2400
|
+
} else if (!acquireClaimedSlot(resolveBindingKeySync())) {
|
|
2401
|
+
restartTimer = setTimeout(spawnConsumer, 15e3);
|
|
2402
|
+
restartTimer.unref?.();
|
|
2403
|
+
restartDueAt = Date.now() + 15e3;
|
|
2404
|
+
return;
|
|
2405
|
+
}
|
|
2266
2406
|
const gen = {
|
|
2267
2407
|
handle: null,
|
|
2268
2408
|
installed: false,
|
|
@@ -3408,13 +3548,24 @@ function normalizeEvent(line) {
|
|
|
3408
3548
|
if (!isRecord(raw) || raw.type !== "im.message.receive_v1" || typeof raw.event_id !== "string" || typeof raw.chat_id !== "string") return null;
|
|
3409
3549
|
const id = raw.message_id ?? raw.id;
|
|
3410
3550
|
if (typeof id !== "string") return null;
|
|
3551
|
+
const mentions = Array.isArray(raw.mentions) ? raw.mentions.filter((m) => isRecord(m)).map((m) => ({ id: typeof m.id === "string" ? m.id : void 0, name: typeof m.name === "string" ? m.name : void 0 })) : void 0;
|
|
3552
|
+
let text = extractInboundText(raw.content).replace(/\[Image:\s*img_[A-Za-z0-9_-]+\]/g, "").trim();
|
|
3553
|
+
const chatType = raw.chat_type === void 0 ? void 0 : typeof raw.chat_type === "string" ? raw.chat_type : "invalid";
|
|
3554
|
+
if (chatType === "group" && mentions?.length) {
|
|
3555
|
+
const first = mentions[0];
|
|
3556
|
+
if (first?.name) {
|
|
3557
|
+
const esc = first.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
3558
|
+
text = text.replace(new RegExp(`^(?:[^\\n]*\\n)?@${esc}\\s*`), "").trim();
|
|
3559
|
+
}
|
|
3560
|
+
}
|
|
3411
3561
|
return {
|
|
3412
3562
|
event_id: raw.event_id,
|
|
3413
3563
|
message_id: id,
|
|
3414
3564
|
chat_id: raw.chat_id,
|
|
3415
3565
|
sender_id: typeof raw.sender_id === "string" ? raw.sender_id : void 0,
|
|
3416
|
-
chatType
|
|
3417
|
-
|
|
3566
|
+
chatType,
|
|
3567
|
+
mentions: mentions?.length ? mentions : void 0,
|
|
3568
|
+
text,
|
|
3418
3569
|
images: extractImageKeys(raw.content)
|
|
3419
3570
|
};
|
|
3420
3571
|
}
|
|
@@ -3431,8 +3582,29 @@ function connect(binary, profile, targetKind = "chat") {
|
|
|
3431
3582
|
const result = await native(binary, profile ? [...args, "--profile", profile] : args, 3e4, void 0, { signal: lifetime.signal });
|
|
3432
3583
|
return result.error ? void 0 : parseLarkCliEnvelope(result.stdout).data;
|
|
3433
3584
|
};
|
|
3585
|
+
let canonicalAppIdCache = null;
|
|
3586
|
+
let canonicalResolved = false;
|
|
3434
3587
|
return {
|
|
3435
3588
|
sourcePrefix: "[Feishu]",
|
|
3589
|
+
async canonicalAppId() {
|
|
3590
|
+
if (canonicalResolved) return canonicalAppIdCache || void 0;
|
|
3591
|
+
if (!profile) {
|
|
3592
|
+
canonicalResolved = true;
|
|
3593
|
+
return void 0;
|
|
3594
|
+
}
|
|
3595
|
+
try {
|
|
3596
|
+
const result = await native(binary, ["profile", "list"], 1e4, void 0, { signal: lifetime.signal });
|
|
3597
|
+
const profiles = result.error ? null : parseProfiles(result.stdout);
|
|
3598
|
+
if (!profiles) return void 0;
|
|
3599
|
+
const appId = profiles.find((item) => item.name === profile)?.appId;
|
|
3600
|
+
if (!appId) return void 0;
|
|
3601
|
+
canonicalAppIdCache = appId;
|
|
3602
|
+
canonicalResolved = true;
|
|
3603
|
+
return canonicalAppIdCache;
|
|
3604
|
+
} catch {
|
|
3605
|
+
return void 0;
|
|
3606
|
+
}
|
|
3607
|
+
},
|
|
3436
3608
|
async dispose() {
|
|
3437
3609
|
lifetime.abort();
|
|
3438
3610
|
await Promise.allSettled([...active]);
|
|
@@ -3461,7 +3633,13 @@ function connect(binary, profile, targetKind = "chat") {
|
|
|
3461
3633
|
} : null;
|
|
3462
3634
|
},
|
|
3463
3635
|
async threadMessages(id) {
|
|
3464
|
-
const
|
|
3636
|
+
const args = ["im", "+threads-messages-list", "--as", "bot", "--thread", id];
|
|
3637
|
+
const result = await native(binary, profile ? [...args, "--profile", profile] : args, 3e4, void 0, { signal: lifetime.signal });
|
|
3638
|
+
if (result.error) {
|
|
3639
|
+
if (/230027|user_unauthorized|authorization/i.test(result.error)) return "forbidden";
|
|
3640
|
+
return null;
|
|
3641
|
+
}
|
|
3642
|
+
const data = parseLarkCliEnvelope(result.stdout).data;
|
|
3465
3643
|
return Array.isArray(data?.messages) ? data.messages.flatMap((message) => isRecord(message) && typeof message.message_id === "string" ? [message.message_id] : []) : null;
|
|
3466
3644
|
},
|
|
3467
3645
|
async downloadImage(messageId, image, index) {
|
|
@@ -3548,7 +3726,8 @@ var feishuProvider = {
|
|
|
3548
3726
|
if (!targetKind) return null;
|
|
3549
3727
|
const allowed = Array.isArray(raw.allowUsers) ? raw.allowUsers.filter((id) => typeof id === "string" && !!id.trim()) : void 0;
|
|
3550
3728
|
const appId = typeof raw.larkCliAppId === "string" && raw.larkCliAppId.trim() ? raw.larkCliAppId.trim() : void 0;
|
|
3551
|
-
|
|
3729
|
+
const groupAccess = raw.groupAccess === "allowUsers" || raw.groupAccess === "members" || raw.groupAccess === "off" ? raw.groupAccess : void 0;
|
|
3730
|
+
return { ...raw, targetId: targetKind === "user" ? openId : chatId, targetKind, allowUsers: allowed?.length ? allowed : void 0, ...appId ? { appId } : {}, ...groupAccess ? { groupAccess } : {} };
|
|
3552
3731
|
},
|
|
3553
3732
|
connect(settings, cwd) {
|
|
3554
3733
|
const binary = resolveLaunchSpec(typeof settings.larkCli === "string" ? settings.larkCli : void 0, cwd);
|
|
@@ -3650,7 +3829,7 @@ function initializeImForward(ctx, providers = implementedProviders) {
|
|
|
3650
3829
|
return () => {
|
|
3651
3830
|
};
|
|
3652
3831
|
};
|
|
3653
|
-
createRuntime(bridge, { enabled: config.enabled, provider: config.provider, label: provider.label, targetId: settings.targetId, targetKind: settings.targetKind, allowUsers: settings.allowUsers, retryBackoffMs: config.retryBackoffMs, ...settings.appId ? { appId: settings.appId } : {}, ...caps.unattended ? { unattended: true } : {} }, connection, () => liveEnabled(config), (next) => {
|
|
3832
|
+
createRuntime(bridge, { enabled: config.enabled, provider: config.provider, label: provider.label, targetId: settings.targetId, targetKind: settings.targetKind, allowUsers: settings.allowUsers, retryBackoffMs: config.retryBackoffMs, ...settings.appId ? { appId: settings.appId } : {}, ...typeof settings.larkCliProfile === "string" && settings.larkCliProfile.trim() ? { profile: settings.larkCliProfile.trim() } : {}, ...settings.groupAccess ? { groupAccess: settings.groupAccess } : {}, ...caps.unattended ? { unattended: true } : {} }, connection, () => liveEnabled(config), (next) => {
|
|
3654
3833
|
const latest = readLiveConfig(ctx.cwd, dir);
|
|
3655
3834
|
if (latest.problems.length || !latest.config || configurationIdentity(latest.config) !== configurationIdentity(config)) throw new Error("Configuration changed; setup required");
|
|
3656
3835
|
writeConfig(ctx.cwd, dir, { enabled: next });
|