aai-lark-bridge 0.2.6 → 0.2.8
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 +108 -12
- 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.8",
|
|
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
|
|
@@ -7165,6 +7167,7 @@ function budgetStopReply(title, spentUsd, budgetUsd) {
|
|
|
7165
7167
|
return `\u4EFB\u52A1\u300A${title}\u300B\u7684\u5BF9\u8BDD\u5DF2\u82B1 ${usd(spentUsd)} \u7F8E\u5143\uFF0C\u5230\u4E0A\u9650 ${usd(budgetUsd)} \u7F8E\u5143\uFF0C\u5148\u505C\u4E0B\uFF1B\u8BF7\u770B\u9884\u7B97\u5361\u7247`;
|
|
7166
7168
|
}
|
|
7167
7169
|
function taskFooterLine(binding) {
|
|
7170
|
+
if (binding.viaThread) return `\u672C\u8BDD\u9898\u4EFB\u52A1\uFF1A\u300A${binding.task.title}\u300B`;
|
|
7168
7171
|
return `\u5F53\u524D\u4EFB\u52A1\uFF1A\u300A${binding.task.title}\u300B\uFF0C\u6392\u961F ${binding.queued} \u6761`;
|
|
7169
7172
|
}
|
|
7170
7173
|
var SAFE_NAME = /^[A-Za-z0-9_.:@+-]{1,200}$/;
|
|
@@ -7189,7 +7192,7 @@ function isTaskItem(v) {
|
|
|
7189
7192
|
const t = v;
|
|
7190
7193
|
return typeof t.task_id === "string" && t.task_id !== "" && typeof t.title === "string" && typeof t.cwd === "string" && isAbsolute2(t.cwd) && typeof t.system_prompt_file === "string" && isAbsolute2(t.system_prompt_file) && (t.first_context === void 0 || typeof t.first_context === "string") && typeof t.ledger_key === "string" && safeName(t.ledger_key);
|
|
7191
7194
|
}
|
|
7192
|
-
function resolveBinding(rawConfig, chatId, senderOpenId) {
|
|
7195
|
+
function resolveBinding(rawConfig, chatId, senderOpenId, opts = {}) {
|
|
7193
7196
|
const cfg = taskBindingsConfig(rawConfig);
|
|
7194
7197
|
if (!cfg || !safeName(chatId)) return { kind: "none" };
|
|
7195
7198
|
const file = join19(cfg.dir, "chats", `${chatId}.json`);
|
|
@@ -7204,9 +7207,13 @@ function resolveBinding(rawConfig, chatId, senderOpenId) {
|
|
|
7204
7207
|
const doc = parsed;
|
|
7205
7208
|
if (doc.version !== BINDING_VERSION) return { kind: "invalid", reason: "version" };
|
|
7206
7209
|
if (doc.chat_id !== chatId) return { kind: "invalid", reason: "chat-id" };
|
|
7207
|
-
|
|
7210
|
+
const threadTask = opts.threadRootId ? taskOfMessage(cfg.dir, opts.threadRootId) : void 0;
|
|
7211
|
+
const queue = Array.isArray(doc.queue) ? doc.queue : [];
|
|
7212
|
+
const inThread = threadTask ? [doc.active, ...queue].find((x) => x && typeof x === "object" && x.task_id === threadTask) : void 0;
|
|
7213
|
+
if (threadTask && !inThread) return { kind: "none" };
|
|
7214
|
+
if (!inThread && (doc.active === null || doc.active === void 0)) return { kind: "none" };
|
|
7208
7215
|
if (doc.user_open_id !== senderOpenId) return { kind: "invalid", reason: "user" };
|
|
7209
|
-
const task = doc.active;
|
|
7216
|
+
const task = inThread ?? doc.active;
|
|
7210
7217
|
if (!isTaskItem(task)) return { kind: "invalid", reason: "task-shape" };
|
|
7211
7218
|
if (typeof task.budget_usd !== "number" || !Number.isFinite(task.budget_usd) || task.budget_usd <= 0) {
|
|
7212
7219
|
return { kind: "invalid", reason: "budget" };
|
|
@@ -7240,10 +7247,90 @@ function resolveBinding(rawConfig, chatId, senderOpenId) {
|
|
|
7240
7247
|
cwdRealpath: cwdReal,
|
|
7241
7248
|
settingsFile,
|
|
7242
7249
|
systemPromptFile: promptReal,
|
|
7243
|
-
queued:
|
|
7250
|
+
queued: queue.length,
|
|
7251
|
+
...inThread ? { viaThread: true } : {}
|
|
7244
7252
|
}
|
|
7245
7253
|
};
|
|
7246
7254
|
}
|
|
7255
|
+
function taskOfMessage(dir, messageId) {
|
|
7256
|
+
if (!messageId || !safeName(messageId)) return void 0;
|
|
7257
|
+
try {
|
|
7258
|
+
const v = JSON.parse(readFileSync3(join19(dir, "messages", `${messageId}.json`), "utf8"));
|
|
7259
|
+
return typeof v.task_id === "string" && v.task_id !== "" ? v.task_id : void 0;
|
|
7260
|
+
} catch {
|
|
7261
|
+
return void 0;
|
|
7262
|
+
}
|
|
7263
|
+
}
|
|
7264
|
+
var LOCK_WAIT_MS = 2e3;
|
|
7265
|
+
var LOCK_STEP_MS = 50;
|
|
7266
|
+
var LOCK_STALE_MS = 1e4;
|
|
7267
|
+
var sleepSync = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
7268
|
+
function withChatLock(dir, chatId, fn) {
|
|
7269
|
+
const lock5 = join19(dir, "chats", `${chatId}.lock`);
|
|
7270
|
+
const until = Date.now() + LOCK_WAIT_MS;
|
|
7271
|
+
for (; ; ) {
|
|
7272
|
+
try {
|
|
7273
|
+
mkdirSync3(lock5);
|
|
7274
|
+
break;
|
|
7275
|
+
} catch (err) {
|
|
7276
|
+
if (err.code !== "EEXIST") throw err;
|
|
7277
|
+
try {
|
|
7278
|
+
if (Date.now() - statSync(lock5).mtimeMs > LOCK_STALE_MS) {
|
|
7279
|
+
rmSync2(lock5, { recursive: true, force: true });
|
|
7280
|
+
continue;
|
|
7281
|
+
}
|
|
7282
|
+
} catch {
|
|
7283
|
+
continue;
|
|
7284
|
+
}
|
|
7285
|
+
if (Date.now() >= until) throw new Error(`binding locked for over ${LOCK_WAIT_MS} ms`);
|
|
7286
|
+
sleepSync(LOCK_STEP_MS);
|
|
7287
|
+
}
|
|
7288
|
+
}
|
|
7289
|
+
try {
|
|
7290
|
+
return fn();
|
|
7291
|
+
} finally {
|
|
7292
|
+
rmSync2(lock5, { recursive: true, force: true });
|
|
7293
|
+
}
|
|
7294
|
+
}
|
|
7295
|
+
function switchByReply(rawConfig, chatId, senderOpenId, messageIds, now = /* @__PURE__ */ new Date()) {
|
|
7296
|
+
const cfg = taskBindingsConfig(rawConfig);
|
|
7297
|
+
if (!cfg || !safeName(chatId)) return void 0;
|
|
7298
|
+
const replied = messageIds.find((id) => taskOfMessage(cfg.dir, id));
|
|
7299
|
+
const taskId = taskOfMessage(cfg.dir, replied);
|
|
7300
|
+
if (!replied || !taskId) return void 0;
|
|
7301
|
+
const file = join19(cfg.dir, "chats", `${chatId}.json`);
|
|
7302
|
+
if (!existsSync6(file)) return void 0;
|
|
7303
|
+
const moved = withChatLock(cfg.dir, chatId, () => {
|
|
7304
|
+
const doc = JSON.parse(readFileSync3(file, "utf8"));
|
|
7305
|
+
if (doc.version !== BINDING_VERSION || doc.chat_id !== chatId || doc.user_open_id !== senderOpenId) return void 0;
|
|
7306
|
+
if (doc.active?.task_id === taskId) return void 0;
|
|
7307
|
+
const queue = Array.isArray(doc.queue) ? doc.queue : [];
|
|
7308
|
+
const i = queue.findIndex((x) => x?.task_id === taskId);
|
|
7309
|
+
if (i < 0) return void 0;
|
|
7310
|
+
const item = queue.splice(i, 1)[0];
|
|
7311
|
+
const from = doc.active ?? null;
|
|
7312
|
+
if (from) queue.unshift(from);
|
|
7313
|
+
const next = { ...doc, active: item, queue, updated_at: now.toISOString() };
|
|
7314
|
+
const tmp = `${file}.new-${process.pid}`;
|
|
7315
|
+
writeFileSync3(tmp, JSON.stringify(next, null, 2), { mode: 384 });
|
|
7316
|
+
renameSync2(tmp, file);
|
|
7317
|
+
return { from: from?.task_id ?? null, to: taskId };
|
|
7318
|
+
});
|
|
7319
|
+
if (!moved) return void 0;
|
|
7320
|
+
const event = { type: "switched", chat_id: chatId, task_id: moved.to, from_task_id: moved.from, message_id: replied, at: now.toISOString() };
|
|
7321
|
+
let eventWritten = false;
|
|
7322
|
+
try {
|
|
7323
|
+
const eventsDir = join19(cfg.dir, "events");
|
|
7324
|
+
mkdirSync3(eventsDir, { recursive: true });
|
|
7325
|
+
const tmp = join19(eventsDir, `.${randomBytes4(6).toString("hex")}.tmp`);
|
|
7326
|
+
writeFileSync3(tmp, JSON.stringify(event));
|
|
7327
|
+
renameSync2(tmp, join19(eventsDir, `switch-${now.getTime()}-${randomBytes4(4).toString("hex")}.json`));
|
|
7328
|
+
eventWritten = true;
|
|
7329
|
+
} catch {
|
|
7330
|
+
eventWritten = false;
|
|
7331
|
+
}
|
|
7332
|
+
return { ...moved, eventWritten };
|
|
7333
|
+
}
|
|
7247
7334
|
function ledgerFile(dir, ledgerKey) {
|
|
7248
7335
|
return join19(dir, "spend", `${ledgerKey}.jsonl`);
|
|
7249
7336
|
}
|
|
@@ -11963,7 +12050,7 @@ async function lookupMessageThreadId(channel, messageId) {
|
|
|
11963
12050
|
|
|
11964
12051
|
// src/bridge/card/handoff.ts
|
|
11965
12052
|
import { createHash as createHash4 } from "crypto";
|
|
11966
|
-
import { existsSync as existsSync7, mkdirSync as mkdirSync4, renameSync as
|
|
12053
|
+
import { existsSync as existsSync7, mkdirSync as mkdirSync4, renameSync as renameSync3, writeFileSync as writeFileSync4 } from "fs";
|
|
11967
12054
|
import { isAbsolute as isAbsolute4, join as join22 } from "path";
|
|
11968
12055
|
function handoffDirs(raw) {
|
|
11969
12056
|
if (!raw || typeof raw !== "object") return {};
|
|
@@ -12006,7 +12093,7 @@ function tryHandoff(evt, dirs) {
|
|
|
12006
12093
|
received_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
12007
12094
|
})
|
|
12008
12095
|
);
|
|
12009
|
-
|
|
12096
|
+
renameSync3(tmp, file);
|
|
12010
12097
|
log.info("cardAction", "handoff", { kind, file: name });
|
|
12011
12098
|
} catch (err) {
|
|
12012
12099
|
log.fail("cardAction", err, { kind, step: "handoff" });
|
|
@@ -13715,7 +13802,7 @@ function preview(text) {
|
|
|
13715
13802
|
|
|
13716
13803
|
// src/bridge/bot/doc-confirmation.ts
|
|
13717
13804
|
import { createHash as createHash7 } from "crypto";
|
|
13718
|
-
import { mkdirSync as mkdirSync5, realpathSync as realpathSync2, renameSync as
|
|
13805
|
+
import { mkdirSync as mkdirSync5, realpathSync as realpathSync2, renameSync as renameSync4, writeFileSync as writeFileSync5 } from "fs";
|
|
13719
13806
|
import { isAbsolute as isAbsolute5, join as join24, sep as sep3 } from "path";
|
|
13720
13807
|
import { spawn } from "child_process";
|
|
13721
13808
|
var waking = /* @__PURE__ */ new Set();
|
|
@@ -13736,7 +13823,7 @@ function enqueueDocConfirmation(workspace, event, { selfReply = false } = {}) {
|
|
|
13736
13823
|
const target = join24(inbox, key + ".json");
|
|
13737
13824
|
const tmp = target + `.new-${process.pid}`;
|
|
13738
13825
|
writeFileSync5(tmp, JSON.stringify(hint), { mode: 384 });
|
|
13739
|
-
|
|
13826
|
+
renameSync4(tmp, target);
|
|
13740
13827
|
if (!waking.has(root)) {
|
|
13741
13828
|
waking.add(root);
|
|
13742
13829
|
try {
|
|
@@ -15133,7 +15220,7 @@ async function intakeMessage(deps) {
|
|
|
15133
15220
|
);
|
|
15134
15221
|
return;
|
|
15135
15222
|
}
|
|
15136
|
-
const binding = emsg.chatType === "p2p" ? dmBinding(controls, emsg, scope) : void 0;
|
|
15223
|
+
const binding = emsg.chatType === "p2p" ? dmBinding(controls, emsg, scope, { switchByReply: true }) : void 0;
|
|
15137
15224
|
if (binding?.kind === "invalid") {
|
|
15138
15225
|
await channel.send(emsg.chatId, { markdown: BINDING_INVALID_REPLY }, { replyTo: emsg.messageId }).catch(
|
|
15139
15226
|
(err) => log.warn("binding", "invalid-reply-failed", { err: String(err) })
|
|
@@ -15172,12 +15259,21 @@ async function intakeMessage(deps) {
|
|
|
15172
15259
|
const size = pending.push(scope, emsg);
|
|
15173
15260
|
log.info("intake", "queued", { scope, queueSize: size, debounceMs: DEBOUNCE_MS });
|
|
15174
15261
|
}
|
|
15175
|
-
function dmBinding(controls, msg, scope) {
|
|
15262
|
+
function dmBinding(controls, msg, scope, opts = {}) {
|
|
15176
15263
|
const cfg = controls.profileConfig.preferences.taskBindings;
|
|
15177
15264
|
if (!cfg) return void 0;
|
|
15265
|
+
const threadRootId = msg.threadId ? msg.rootId : void 0;
|
|
15266
|
+
if (opts.switchByReply && !threadRootId) {
|
|
15267
|
+
try {
|
|
15268
|
+
const moved = switchByReply(cfg, msg.chatId, msg.senderId, [msg.replyToMessageId, msg.rootId]);
|
|
15269
|
+
if (moved) log.info("binding", "switched-by-reply", { scope, from: moved.from, to: moved.to, eventWritten: moved.eventWritten });
|
|
15270
|
+
} catch (err) {
|
|
15271
|
+
log.warn("binding", "switch-by-reply-failed", { scope, reason: err instanceof Error ? err.message : String(err) });
|
|
15272
|
+
}
|
|
15273
|
+
}
|
|
15178
15274
|
let result;
|
|
15179
15275
|
try {
|
|
15180
|
-
result = resolveBinding(cfg, msg.chatId, msg.senderId);
|
|
15276
|
+
result = resolveBinding(cfg, msg.chatId, msg.senderId, { threadRootId });
|
|
15181
15277
|
} catch (err) {
|
|
15182
15278
|
result = { kind: "invalid", reason: err instanceof Error ? err.message : String(err) };
|
|
15183
15279
|
}
|