aai-lark-bridge 0.2.6 → 0.2.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/cli.js +96 -7
- package/dist/cli.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -4,7 +4,7 @@ import { Command } from "commander";
|
|
|
4
4
|
// package.json
|
|
5
5
|
var package_default = {
|
|
6
6
|
name: "aai-lark-bridge",
|
|
7
|
-
version: "0.2.
|
|
7
|
+
version: "0.2.7",
|
|
8
8
|
description: "Lark AI Native Bridge - Lark\u57FA\u7840\u8BBE\u65BD\u5C42\u548CCLI\u96C6\u6210",
|
|
9
9
|
type: "module",
|
|
10
10
|
main: "./dist/index.js",
|
|
@@ -7150,6 +7150,8 @@ import {
|
|
|
7150
7150
|
mkdirSync as mkdirSync3,
|
|
7151
7151
|
readFileSync as readFileSync3,
|
|
7152
7152
|
realpathSync,
|
|
7153
|
+
renameSync as renameSync2,
|
|
7154
|
+
rmSync as rmSync2,
|
|
7153
7155
|
statSync,
|
|
7154
7156
|
unlinkSync as unlinkSync2,
|
|
7155
7157
|
writeFileSync as writeFileSync3
|
|
@@ -7244,6 +7246,85 @@ function resolveBinding(rawConfig, chatId, senderOpenId) {
|
|
|
7244
7246
|
}
|
|
7245
7247
|
};
|
|
7246
7248
|
}
|
|
7249
|
+
function taskOfMessage(dir, messageId) {
|
|
7250
|
+
if (!messageId || !safeName(messageId)) return void 0;
|
|
7251
|
+
try {
|
|
7252
|
+
const v = JSON.parse(readFileSync3(join19(dir, "messages", `${messageId}.json`), "utf8"));
|
|
7253
|
+
return typeof v.task_id === "string" && v.task_id !== "" ? v.task_id : void 0;
|
|
7254
|
+
} catch {
|
|
7255
|
+
return void 0;
|
|
7256
|
+
}
|
|
7257
|
+
}
|
|
7258
|
+
var LOCK_WAIT_MS = 2e3;
|
|
7259
|
+
var LOCK_STEP_MS = 50;
|
|
7260
|
+
var LOCK_STALE_MS = 1e4;
|
|
7261
|
+
var sleepSync = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
7262
|
+
function withChatLock(dir, chatId, fn) {
|
|
7263
|
+
const lock5 = join19(dir, "chats", `${chatId}.lock`);
|
|
7264
|
+
const until = Date.now() + LOCK_WAIT_MS;
|
|
7265
|
+
for (; ; ) {
|
|
7266
|
+
try {
|
|
7267
|
+
mkdirSync3(lock5);
|
|
7268
|
+
break;
|
|
7269
|
+
} catch (err) {
|
|
7270
|
+
if (err.code !== "EEXIST") throw err;
|
|
7271
|
+
try {
|
|
7272
|
+
if (Date.now() - statSync(lock5).mtimeMs > LOCK_STALE_MS) {
|
|
7273
|
+
rmSync2(lock5, { recursive: true, force: true });
|
|
7274
|
+
continue;
|
|
7275
|
+
}
|
|
7276
|
+
} catch {
|
|
7277
|
+
continue;
|
|
7278
|
+
}
|
|
7279
|
+
if (Date.now() >= until) throw new Error(`binding locked for over ${LOCK_WAIT_MS} ms`);
|
|
7280
|
+
sleepSync(LOCK_STEP_MS);
|
|
7281
|
+
}
|
|
7282
|
+
}
|
|
7283
|
+
try {
|
|
7284
|
+
return fn();
|
|
7285
|
+
} finally {
|
|
7286
|
+
rmSync2(lock5, { recursive: true, force: true });
|
|
7287
|
+
}
|
|
7288
|
+
}
|
|
7289
|
+
function switchByReply(rawConfig, chatId, senderOpenId, messageIds, now = /* @__PURE__ */ new Date()) {
|
|
7290
|
+
const cfg = taskBindingsConfig(rawConfig);
|
|
7291
|
+
if (!cfg || !safeName(chatId)) return void 0;
|
|
7292
|
+
const replied = messageIds.find((id) => taskOfMessage(cfg.dir, id));
|
|
7293
|
+
const taskId = taskOfMessage(cfg.dir, replied);
|
|
7294
|
+
if (!replied || !taskId) return void 0;
|
|
7295
|
+
const file = join19(cfg.dir, "chats", `${chatId}.json`);
|
|
7296
|
+
if (!existsSync6(file)) return void 0;
|
|
7297
|
+
const moved = withChatLock(cfg.dir, chatId, () => {
|
|
7298
|
+
const doc = JSON.parse(readFileSync3(file, "utf8"));
|
|
7299
|
+
if (doc.version !== BINDING_VERSION || doc.chat_id !== chatId || doc.user_open_id !== senderOpenId) return void 0;
|
|
7300
|
+
if (doc.active?.task_id === taskId) return void 0;
|
|
7301
|
+
const queue = Array.isArray(doc.queue) ? doc.queue : [];
|
|
7302
|
+
const i = queue.findIndex((x) => x?.task_id === taskId);
|
|
7303
|
+
if (i < 0) return void 0;
|
|
7304
|
+
const item = queue.splice(i, 1)[0];
|
|
7305
|
+
const from = doc.active ?? null;
|
|
7306
|
+
if (from) queue.unshift(from);
|
|
7307
|
+
const next = { ...doc, active: item, queue, updated_at: now.toISOString() };
|
|
7308
|
+
const tmp = `${file}.new-${process.pid}`;
|
|
7309
|
+
writeFileSync3(tmp, JSON.stringify(next, null, 2), { mode: 384 });
|
|
7310
|
+
renameSync2(tmp, file);
|
|
7311
|
+
return { from: from?.task_id ?? null, to: taskId };
|
|
7312
|
+
});
|
|
7313
|
+
if (!moved) return void 0;
|
|
7314
|
+
const event = { type: "switched", chat_id: chatId, task_id: moved.to, from_task_id: moved.from, message_id: replied, at: now.toISOString() };
|
|
7315
|
+
let eventWritten = false;
|
|
7316
|
+
try {
|
|
7317
|
+
const eventsDir = join19(cfg.dir, "events");
|
|
7318
|
+
mkdirSync3(eventsDir, { recursive: true });
|
|
7319
|
+
const tmp = join19(eventsDir, `.${randomBytes4(6).toString("hex")}.tmp`);
|
|
7320
|
+
writeFileSync3(tmp, JSON.stringify(event));
|
|
7321
|
+
renameSync2(tmp, join19(eventsDir, `switch-${now.getTime()}-${randomBytes4(4).toString("hex")}.json`));
|
|
7322
|
+
eventWritten = true;
|
|
7323
|
+
} catch {
|
|
7324
|
+
eventWritten = false;
|
|
7325
|
+
}
|
|
7326
|
+
return { ...moved, eventWritten };
|
|
7327
|
+
}
|
|
7247
7328
|
function ledgerFile(dir, ledgerKey) {
|
|
7248
7329
|
return join19(dir, "spend", `${ledgerKey}.jsonl`);
|
|
7249
7330
|
}
|
|
@@ -11963,7 +12044,7 @@ async function lookupMessageThreadId(channel, messageId) {
|
|
|
11963
12044
|
|
|
11964
12045
|
// src/bridge/card/handoff.ts
|
|
11965
12046
|
import { createHash as createHash4 } from "crypto";
|
|
11966
|
-
import { existsSync as existsSync7, mkdirSync as mkdirSync4, renameSync as
|
|
12047
|
+
import { existsSync as existsSync7, mkdirSync as mkdirSync4, renameSync as renameSync3, writeFileSync as writeFileSync4 } from "fs";
|
|
11967
12048
|
import { isAbsolute as isAbsolute4, join as join22 } from "path";
|
|
11968
12049
|
function handoffDirs(raw) {
|
|
11969
12050
|
if (!raw || typeof raw !== "object") return {};
|
|
@@ -12006,7 +12087,7 @@ function tryHandoff(evt, dirs) {
|
|
|
12006
12087
|
received_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
12007
12088
|
})
|
|
12008
12089
|
);
|
|
12009
|
-
|
|
12090
|
+
renameSync3(tmp, file);
|
|
12010
12091
|
log.info("cardAction", "handoff", { kind, file: name });
|
|
12011
12092
|
} catch (err) {
|
|
12012
12093
|
log.fail("cardAction", err, { kind, step: "handoff" });
|
|
@@ -13715,7 +13796,7 @@ function preview(text) {
|
|
|
13715
13796
|
|
|
13716
13797
|
// src/bridge/bot/doc-confirmation.ts
|
|
13717
13798
|
import { createHash as createHash7 } from "crypto";
|
|
13718
|
-
import { mkdirSync as mkdirSync5, realpathSync as realpathSync2, renameSync as
|
|
13799
|
+
import { mkdirSync as mkdirSync5, realpathSync as realpathSync2, renameSync as renameSync4, writeFileSync as writeFileSync5 } from "fs";
|
|
13719
13800
|
import { isAbsolute as isAbsolute5, join as join24, sep as sep3 } from "path";
|
|
13720
13801
|
import { spawn } from "child_process";
|
|
13721
13802
|
var waking = /* @__PURE__ */ new Set();
|
|
@@ -13736,7 +13817,7 @@ function enqueueDocConfirmation(workspace, event, { selfReply = false } = {}) {
|
|
|
13736
13817
|
const target = join24(inbox, key + ".json");
|
|
13737
13818
|
const tmp = target + `.new-${process.pid}`;
|
|
13738
13819
|
writeFileSync5(tmp, JSON.stringify(hint), { mode: 384 });
|
|
13739
|
-
|
|
13820
|
+
renameSync4(tmp, target);
|
|
13740
13821
|
if (!waking.has(root)) {
|
|
13741
13822
|
waking.add(root);
|
|
13742
13823
|
try {
|
|
@@ -15133,7 +15214,7 @@ async function intakeMessage(deps) {
|
|
|
15133
15214
|
);
|
|
15134
15215
|
return;
|
|
15135
15216
|
}
|
|
15136
|
-
const binding = emsg.chatType === "p2p" ? dmBinding(controls, emsg, scope) : void 0;
|
|
15217
|
+
const binding = emsg.chatType === "p2p" ? dmBinding(controls, emsg, scope, { switchByReply: true }) : void 0;
|
|
15137
15218
|
if (binding?.kind === "invalid") {
|
|
15138
15219
|
await channel.send(emsg.chatId, { markdown: BINDING_INVALID_REPLY }, { replyTo: emsg.messageId }).catch(
|
|
15139
15220
|
(err) => log.warn("binding", "invalid-reply-failed", { err: String(err) })
|
|
@@ -15172,9 +15253,17 @@ async function intakeMessage(deps) {
|
|
|
15172
15253
|
const size = pending.push(scope, emsg);
|
|
15173
15254
|
log.info("intake", "queued", { scope, queueSize: size, debounceMs: DEBOUNCE_MS });
|
|
15174
15255
|
}
|
|
15175
|
-
function dmBinding(controls, msg, scope) {
|
|
15256
|
+
function dmBinding(controls, msg, scope, opts = {}) {
|
|
15176
15257
|
const cfg = controls.profileConfig.preferences.taskBindings;
|
|
15177
15258
|
if (!cfg) return void 0;
|
|
15259
|
+
if (opts.switchByReply) {
|
|
15260
|
+
try {
|
|
15261
|
+
const moved = switchByReply(cfg, msg.chatId, msg.senderId, [msg.replyToMessageId, msg.rootId]);
|
|
15262
|
+
if (moved) log.info("binding", "switched-by-reply", { scope, from: moved.from, to: moved.to, eventWritten: moved.eventWritten });
|
|
15263
|
+
} catch (err) {
|
|
15264
|
+
log.warn("binding", "switch-by-reply-failed", { scope, reason: err instanceof Error ? err.message : String(err) });
|
|
15265
|
+
}
|
|
15266
|
+
}
|
|
15178
15267
|
let result;
|
|
15179
15268
|
try {
|
|
15180
15269
|
result = resolveBinding(cfg, msg.chatId, msg.senderId);
|