aai-lark-bridge 0.2.31 → 0.2.33
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 +165 -32
- 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.33",
|
|
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",
|
|
@@ -7617,7 +7617,7 @@ async function submitClaudeLogin(cfg, sub, deps = {}) {
|
|
|
7617
7617
|
}
|
|
7618
7618
|
|
|
7619
7619
|
// src/bridge/bot/channel.ts
|
|
7620
|
-
import { dirname as dirname19, join as
|
|
7620
|
+
import { dirname as dirname19, join as join28 } from "path";
|
|
7621
7621
|
|
|
7622
7622
|
// src/bridge/agent/capability.ts
|
|
7623
7623
|
function claudeCapability(profile2) {
|
|
@@ -7874,7 +7874,7 @@ function isDispatchRoot(dir, rootMessageId) {
|
|
|
7874
7874
|
}
|
|
7875
7875
|
function dispatchRoute(dir, m) {
|
|
7876
7876
|
if (m.threadId) return isDispatchRoot(dir, m.rootId) ? "thread" : void 0;
|
|
7877
|
-
return isDispatchMarker(m.content) ? "start" : void 0;
|
|
7877
|
+
return isDispatchMarker(m.content) || isDispatchRoot(dir, m.messageId) ? "start" : void 0;
|
|
7878
7878
|
}
|
|
7879
7879
|
var LOCK_WAIT_MS = 2e3;
|
|
7880
7880
|
var LOCK_STEP_MS = 50;
|
|
@@ -12667,6 +12667,19 @@ async function lookupMessageThreadId(channel, messageId) {
|
|
|
12667
12667
|
return void 0;
|
|
12668
12668
|
}
|
|
12669
12669
|
}
|
|
12670
|
+
async function lookupMessageRoot(channel, messageId) {
|
|
12671
|
+
try {
|
|
12672
|
+
const [item] = await channel.fetchRawMessage(messageId);
|
|
12673
|
+
const raw = item;
|
|
12674
|
+
return { threadId: raw?.thread_id, rootId: raw?.root_id };
|
|
12675
|
+
} catch (err) {
|
|
12676
|
+
log.warn("thread", "root-lookup-failed", {
|
|
12677
|
+
messageId,
|
|
12678
|
+
err: err instanceof Error ? err.message : String(err)
|
|
12679
|
+
});
|
|
12680
|
+
return {};
|
|
12681
|
+
}
|
|
12682
|
+
}
|
|
12670
12683
|
|
|
12671
12684
|
// src/bridge/card/handoff.ts
|
|
12672
12685
|
import { createHash as createHash4 } from "crypto";
|
|
@@ -12734,7 +12747,7 @@ async function handleCardAction(deps) {
|
|
|
12734
12747
|
const chatId = deps.evt.chatId;
|
|
12735
12748
|
const raw = deps.evt.raw;
|
|
12736
12749
|
const formValue = raw?.action?.form_value;
|
|
12737
|
-
const { scope, threadId, mode } = await resolveScope(deps);
|
|
12750
|
+
const { scope, threadId, rootId, mode } = await resolveScope(deps);
|
|
12738
12751
|
const accessDecision = mode === "p2p" ? canUseDm(deps.controls.profileConfig, deps.controls, operatorId) : canUseGroup(deps.controls.profileConfig, deps.controls, chatId, operatorId);
|
|
12739
12752
|
if (!accessDecision.ok) {
|
|
12740
12753
|
log.info("cardAction", "skip-not-allowed-user", {
|
|
@@ -12753,7 +12766,7 @@ async function handleCardAction(deps) {
|
|
|
12753
12766
|
return;
|
|
12754
12767
|
}
|
|
12755
12768
|
log.info("cardAction", "cmd", { cmd, scope });
|
|
12756
|
-
const msg = makeFakeMsg(deps.evt, threadId);
|
|
12769
|
+
const msg = makeFakeMsg(deps.evt, threadId, rootId);
|
|
12757
12770
|
const ctx = {
|
|
12758
12771
|
channel: deps.channel,
|
|
12759
12772
|
msg,
|
|
@@ -12791,7 +12804,7 @@ async function handleCardAction(deps) {
|
|
|
12791
12804
|
}
|
|
12792
12805
|
if (BRIDGE_CALLBACK_MARKER in payload) {
|
|
12793
12806
|
if (!verifyBridgeToken(deps, payload, scope, "agent_callback")) return;
|
|
12794
|
-
forwardToAgent(deps, payload, formValue, scope, threadId, mode);
|
|
12807
|
+
forwardToAgent(deps, payload, formValue, scope, threadId, mode, rootId);
|
|
12795
12808
|
return;
|
|
12796
12809
|
}
|
|
12797
12810
|
return;
|
|
@@ -12821,6 +12834,13 @@ async function resolveScope(deps) {
|
|
|
12821
12834
|
const chatId = deps.evt.chatId;
|
|
12822
12835
|
const mode = await deps.chatModeCache.resolve(deps.channel, chatId);
|
|
12823
12836
|
if (mode !== "topic") {
|
|
12837
|
+
const bindingsDir = deps.controls.profileConfig.preferences.taskBindings?.dir;
|
|
12838
|
+
if (mode === "p2p" && bindingsDir) {
|
|
12839
|
+
const { threadId: threadId2, rootId } = await lookupMessageRoot(deps.channel, deps.evt.messageId);
|
|
12840
|
+
if (rootId && isDispatchRoot(bindingsDir, rootId)) {
|
|
12841
|
+
return { scope: `${chatId}:${rootId}`, threadId: threadId2, rootId, mode };
|
|
12842
|
+
}
|
|
12843
|
+
}
|
|
12824
12844
|
return { scope: chatId, threadId: void 0, mode };
|
|
12825
12845
|
}
|
|
12826
12846
|
const threadId = await lookupMessageThreadId(deps.channel, deps.evt.messageId);
|
|
@@ -12829,7 +12849,7 @@ async function resolveScope(deps) {
|
|
|
12829
12849
|
}
|
|
12830
12850
|
return { scope: `${chatId}:${threadId}`, threadId, mode };
|
|
12831
12851
|
}
|
|
12832
|
-
function forwardToAgent(deps, payload, formValue, scope, threadId, mode) {
|
|
12852
|
+
function forwardToAgent(deps, payload, formValue, scope, threadId, mode, rootId) {
|
|
12833
12853
|
const {
|
|
12834
12854
|
[BRIDGE_CALLBACK_MARKER]: _marker,
|
|
12835
12855
|
bridge_token: _token,
|
|
@@ -12845,6 +12865,7 @@ function forwardToAgent(deps, payload, formValue, scope, threadId, mode) {
|
|
|
12845
12865
|
chatId: deps.evt.chatId,
|
|
12846
12866
|
chatType: mode === "p2p" ? "p2p" : "group",
|
|
12847
12867
|
threadId,
|
|
12868
|
+
...rootId ? { rootId } : {},
|
|
12848
12869
|
senderId: deps.evt.operator.openId,
|
|
12849
12870
|
senderName: deps.evt.operator.name,
|
|
12850
12871
|
content: `[card-click] ${JSON.stringify(merged)}`,
|
|
@@ -12892,12 +12913,13 @@ function composeArgs(sub, payload) {
|
|
|
12892
12913
|
const arg = typeof payload.arg === "string" && payload.arg || typeof payload.name === "string" && payload.name || "";
|
|
12893
12914
|
return arg ? `${sub} ${arg}` : sub;
|
|
12894
12915
|
}
|
|
12895
|
-
function makeFakeMsg(evt, threadId) {
|
|
12916
|
+
function makeFakeMsg(evt, threadId, rootId) {
|
|
12896
12917
|
return {
|
|
12897
12918
|
messageId: evt.messageId,
|
|
12898
12919
|
chatId: evt.chatId,
|
|
12899
12920
|
chatType: "p2p",
|
|
12900
12921
|
threadId,
|
|
12922
|
+
...rootId ? { rootId } : {},
|
|
12901
12923
|
senderId: evt.operator.openId,
|
|
12902
12924
|
senderName: evt.operator.name,
|
|
12903
12925
|
content: "",
|
|
@@ -14812,9 +14834,100 @@ function runningWorkers(scopes, chatId) {
|
|
|
14812
14834
|
return scopes.filter((s) => s.startsWith(prefix)).length;
|
|
14813
14835
|
}
|
|
14814
14836
|
|
|
14815
|
-
// src/bridge/bot/
|
|
14816
|
-
import {
|
|
14837
|
+
// src/bridge/bot/spawn-inbox.ts
|
|
14838
|
+
import { existsSync as existsSync9, mkdirSync as mkdirSync6, readFileSync as readFileSync5, readdirSync as readdirSync2, rmSync as rmSync3 } from "fs";
|
|
14817
14839
|
import { join as join26 } from "path";
|
|
14840
|
+
var SPAWN_DIR = "spawn";
|
|
14841
|
+
var SPAWN_POLL_MS = 1500;
|
|
14842
|
+
var RECENT_TTL_MS = 15 * 6e4;
|
|
14843
|
+
var RECENT_MAX = 200;
|
|
14844
|
+
var STALE_FILE_MS = 10 * 6e4;
|
|
14845
|
+
var RecentMessages = class {
|
|
14846
|
+
constructor(now = Date.now) {
|
|
14847
|
+
this.now = now;
|
|
14848
|
+
}
|
|
14849
|
+
now;
|
|
14850
|
+
map = /* @__PURE__ */ new Map();
|
|
14851
|
+
set(msg) {
|
|
14852
|
+
this.map.delete(msg.messageId);
|
|
14853
|
+
this.map.set(msg.messageId, { msg, at: this.now() });
|
|
14854
|
+
while (this.map.size > RECENT_MAX) this.map.delete(this.map.keys().next().value);
|
|
14855
|
+
}
|
|
14856
|
+
get(chatId, messageId) {
|
|
14857
|
+
const hit = this.map.get(messageId);
|
|
14858
|
+
if (!hit || this.now() - hit.at > RECENT_TTL_MS || hit.msg.chatId !== chatId) return void 0;
|
|
14859
|
+
return hit.msg;
|
|
14860
|
+
}
|
|
14861
|
+
};
|
|
14862
|
+
function processSpawnFiles(o) {
|
|
14863
|
+
const folder = join26(o.dir, SPAWN_DIR);
|
|
14864
|
+
if (!existsSync9(folder)) return [];
|
|
14865
|
+
const now = (o.now ?? Date.now)();
|
|
14866
|
+
const out = [];
|
|
14867
|
+
for (const name of readdirSync2(folder)) {
|
|
14868
|
+
if (!name.endsWith(".json")) continue;
|
|
14869
|
+
const file = join26(folder, name);
|
|
14870
|
+
let req;
|
|
14871
|
+
try {
|
|
14872
|
+
req = JSON.parse(readFileSync5(file, "utf8"));
|
|
14873
|
+
} catch {
|
|
14874
|
+
rmSync3(file, { force: true });
|
|
14875
|
+
out.push("bad-file");
|
|
14876
|
+
continue;
|
|
14877
|
+
}
|
|
14878
|
+
rmSync3(file, { force: true });
|
|
14879
|
+
const chatId = req.chat_id;
|
|
14880
|
+
const messageId = req.message_id;
|
|
14881
|
+
if (!chatId || !messageId || req.at && now - Date.parse(req.at) > STALE_FILE_MS) {
|
|
14882
|
+
out.push("bad-file");
|
|
14883
|
+
continue;
|
|
14884
|
+
}
|
|
14885
|
+
const msg = o.recent.get(chatId, messageId);
|
|
14886
|
+
if (!msg) {
|
|
14887
|
+
log.warn("spawn", "unknown-message", { chatId, messageId });
|
|
14888
|
+
out.push("unknown-message");
|
|
14889
|
+
continue;
|
|
14890
|
+
}
|
|
14891
|
+
const scope = `${chatId}:${messageId}`;
|
|
14892
|
+
if (o.isRoot(messageId) || o.activeScopes().includes(scope)) {
|
|
14893
|
+
out.push("already");
|
|
14894
|
+
continue;
|
|
14895
|
+
}
|
|
14896
|
+
const running = o.activeScopes().filter((s) => s.startsWith(`${chatId}:`)).length;
|
|
14897
|
+
if (running >= o.limit) {
|
|
14898
|
+
log.info("spawn", "worker-limit", { scope, limit: o.limit });
|
|
14899
|
+
o.reject(msg, "limit");
|
|
14900
|
+
out.push("limit");
|
|
14901
|
+
continue;
|
|
14902
|
+
}
|
|
14903
|
+
try {
|
|
14904
|
+
o.register(messageId, chatId);
|
|
14905
|
+
o.push(scope, msg);
|
|
14906
|
+
log.info("spawn", "spawned", { scope });
|
|
14907
|
+
out.push("spawned");
|
|
14908
|
+
} catch (err) {
|
|
14909
|
+
log.fail("spawn", err, { scope });
|
|
14910
|
+
out.push("bad-file");
|
|
14911
|
+
}
|
|
14912
|
+
}
|
|
14913
|
+
return out;
|
|
14914
|
+
}
|
|
14915
|
+
function startSpawnInbox(o, intervalMs = SPAWN_POLL_MS) {
|
|
14916
|
+
mkdirSync6(join26(o.dir, SPAWN_DIR), { recursive: true });
|
|
14917
|
+
const timer = setInterval(() => {
|
|
14918
|
+
try {
|
|
14919
|
+
processSpawnFiles(o);
|
|
14920
|
+
} catch (err) {
|
|
14921
|
+
log.fail("spawn", err);
|
|
14922
|
+
}
|
|
14923
|
+
}, intervalMs);
|
|
14924
|
+
timer.unref?.();
|
|
14925
|
+
return { stop: () => clearInterval(timer) };
|
|
14926
|
+
}
|
|
14927
|
+
|
|
14928
|
+
// src/bridge/bot/worker-report.ts
|
|
14929
|
+
import { appendFileSync as appendFileSync2, mkdirSync as mkdirSync7 } from "fs";
|
|
14930
|
+
import { join as join27 } from "path";
|
|
14818
14931
|
var REPORT_MARKER = "[worker-report]";
|
|
14819
14932
|
var SUMMARY_LINES = 4;
|
|
14820
14933
|
var clip = (s, n) => s.length > n ? `${s.slice(0, n)}\u2026` : s;
|
|
@@ -14860,9 +14973,9 @@ function reportMessage(r, senderId, senderName) {
|
|
|
14860
14973
|
}
|
|
14861
14974
|
function appendWorkerLog(dir, r) {
|
|
14862
14975
|
if (!/^[A-Za-z0-9_-]+$/.test(r.chatId)) return;
|
|
14863
|
-
const folder =
|
|
14864
|
-
|
|
14865
|
-
appendFileSync2(
|
|
14976
|
+
const folder = join27(dir, "workers");
|
|
14977
|
+
mkdirSync7(folder, { recursive: true });
|
|
14978
|
+
appendFileSync2(join27(folder, `${r.chatId}.jsonl`), `${JSON.stringify(r)}
|
|
14866
14979
|
`);
|
|
14867
14980
|
}
|
|
14868
14981
|
async function observeWorkerRun(i) {
|
|
@@ -15656,7 +15769,7 @@ async function startChannel(deps) {
|
|
|
15656
15769
|
const pool = new ProcessPool(() => getMaxConcurrentRuns(controls.cfg));
|
|
15657
15770
|
const executor = new RunExecutor({ agent, pool, activeRuns });
|
|
15658
15771
|
const appSecret = await resolveAppSecret(cfg, deps.appPaths);
|
|
15659
|
-
const callbackNonceStore = deps.appPaths?.mediaDir ? new CallbackNonceStore(
|
|
15772
|
+
const callbackNonceStore = deps.appPaths?.mediaDir ? new CallbackNonceStore(join28(dirname19(deps.appPaths.mediaDir), "callback-nonces.json")) : void 0;
|
|
15660
15773
|
await callbackNonceStore?.load();
|
|
15661
15774
|
const callbackAuth = callbackNonceStore ? new CallbackAuth({
|
|
15662
15775
|
keys: [{ version: 1, secret: appSecret }],
|
|
@@ -15670,6 +15783,7 @@ async function startChannel(deps) {
|
|
|
15670
15783
|
appSecret
|
|
15671
15784
|
});
|
|
15672
15785
|
const threadModeOverrideWarnedChats = /* @__PURE__ */ new Set();
|
|
15786
|
+
const recentDm = new RecentMessages();
|
|
15673
15787
|
const logThreadModeOverride = ({ chatId, resolvedMode, threadId }) => {
|
|
15674
15788
|
const fields = { chatId, cachedMode: resolvedMode, threadId };
|
|
15675
15789
|
if (threadModeOverrideWarnedChats.has(chatId)) {
|
|
@@ -15793,6 +15907,7 @@ async function startChannel(deps) {
|
|
|
15793
15907
|
workspaces,
|
|
15794
15908
|
activeRuns,
|
|
15795
15909
|
pending,
|
|
15910
|
+
recent: recentDm,
|
|
15796
15911
|
msg,
|
|
15797
15912
|
controls,
|
|
15798
15913
|
chatModeCache,
|
|
@@ -15928,6 +16043,22 @@ async function startChannel(deps) {
|
|
|
15928
16043
|
await ownerRefresh.start();
|
|
15929
16044
|
const knownChatsRefresh = startKnownChatsRefreshTimer(channel, controls);
|
|
15930
16045
|
armed.push(() => knownChatsRefresh.stop());
|
|
16046
|
+
const spawnDir = controls.profileConfig.preferences.taskBindings?.dir;
|
|
16047
|
+
const spawnInbox = spawnDir ? startSpawnInbox({
|
|
16048
|
+
dir: spawnDir,
|
|
16049
|
+
recent: recentDm,
|
|
16050
|
+
activeScopes: () => activeRuns.scopes(),
|
|
16051
|
+
limit: MAX_DM_WORKERS,
|
|
16052
|
+
isRoot: (id) => isDispatchRoot(spawnDir, id),
|
|
16053
|
+
register: (id, chatId) => registerDispatchRoot(spawnDir, id, chatId),
|
|
16054
|
+
push: (scope, m) => {
|
|
16055
|
+
pending.push(scope, m);
|
|
16056
|
+
},
|
|
16057
|
+
reject: (m) => {
|
|
16058
|
+
void channel.send(m.chatId, { markdown: `\u4F60\u540C\u65F6\u5728\u8DD1\u7684\u4EFB\u52A1\u5DF2\u7ECF\u6709 ${MAX_DM_WORKERS} \u4E2A\uFF0C\u7B49\u5176\u4E2D\u4E00\u4E2A\u7ED3\u675F\u540E\u518D\u53D1\u8FD9\u6761\u3002` }, { replyTo: m.messageId }).catch((err) => log.warn("spawn", "limit-reply-failed", { err: String(err) }));
|
|
16059
|
+
}
|
|
16060
|
+
}) : void 0;
|
|
16061
|
+
if (spawnInbox) armed.push(() => spawnInbox.stop());
|
|
15931
16062
|
const identity = channel.botIdentity;
|
|
15932
16063
|
if (identity?.openId) {
|
|
15933
16064
|
agent.setBotIdentity?.({
|
|
@@ -15958,6 +16089,7 @@ async function startChannel(deps) {
|
|
|
15958
16089
|
unregisterChannel(controls.profile, channelToken);
|
|
15959
16090
|
ownerRefresh.stop();
|
|
15960
16091
|
knownChatsRefresh.stop();
|
|
16092
|
+
spawnInbox?.stop();
|
|
15961
16093
|
keepalive.stop();
|
|
15962
16094
|
meetingManager?.dispose();
|
|
15963
16095
|
controls.meeting = void 0;
|
|
@@ -16081,6 +16213,7 @@ async function intakeMessage(deps) {
|
|
|
16081
16213
|
}
|
|
16082
16214
|
}
|
|
16083
16215
|
const emsg = threadId === msg.threadId ? msg : { ...msg, threadId };
|
|
16216
|
+
if (msg.chatType === "p2p") deps.recent.set(emsg);
|
|
16084
16217
|
const chatMode = threadId ? "topic" : resolvedMode;
|
|
16085
16218
|
if (threadId && resolvedMode !== "topic") {
|
|
16086
16219
|
chatModeCache.invalidate(msg.chatId);
|
|
@@ -16369,7 +16502,7 @@ async function runAgentBatchInner(deps) {
|
|
|
16369
16502
|
channel.botIdentity,
|
|
16370
16503
|
extraInstructions
|
|
16371
16504
|
);
|
|
16372
|
-
const dispatchStart = firstMsg.chatType === "p2p" && !threadId && Boolean(controls.profileConfig.preferences.taskBindings) && isDispatchMarker(firstMsg.content);
|
|
16505
|
+
const dispatchStart = firstMsg.chatType === "p2p" && !threadId && Boolean(controls.profileConfig.preferences.taskBindings) && (isDispatchMarker(firstMsg.content) || isDispatchRoot(controls.profileConfig.preferences.taskBindings.dir, firstMsg.messageId));
|
|
16373
16506
|
const sendOpts = {
|
|
16374
16507
|
replyTo: dispatchStart ? firstMsg.messageId : lastMsg.messageId,
|
|
16375
16508
|
...mode === "topic" && threadId || dispatchStart ? { replyInThread: true } : {}
|
|
@@ -16416,7 +16549,7 @@ async function runAgentBatchInner(deps) {
|
|
|
16416
16549
|
now: Date.now(),
|
|
16417
16550
|
stopGraceMs: getAgentStopGraceMs(controls.cfg),
|
|
16418
16551
|
...binding && taskPlan?.ok ? { task: { binding, maxBudgetUsd: taskPlan.maxBudgetUsd } } : {},
|
|
16419
|
-
...deps.userRead?.extraEnv || deps.claudeEnv || firstMsg.chatType === "p2p" ? { extraEnv: { ...deps.claudeEnv, ...deps.userRead?.extraEnv, ...firstMsg.chatType === "p2p" ? { SDLC_DM_CHAT: chatId } : {} } } : {},
|
|
16552
|
+
...deps.userRead?.extraEnv || deps.claudeEnv || firstMsg.chatType === "p2p" ? { extraEnv: { ...deps.claudeEnv, ...deps.userRead?.extraEnv, ...firstMsg.chatType === "p2p" ? { SDLC_DM_CHAT: chatId } : {}, ...firstMsg.chatType === "p2p" && scope === chatId ? { SDLC_DM_MSG: lastMsg.messageId } : {} } } : {},
|
|
16420
16553
|
observability: {
|
|
16421
16554
|
profile: controls.profile,
|
|
16422
16555
|
agent: capability.agentId,
|
|
@@ -19575,8 +19708,8 @@ function openBrowser(url) {
|
|
|
19575
19708
|
|
|
19576
19709
|
// src/bridge/cli/commands/user-gateway.ts
|
|
19577
19710
|
import { spawnSync as spawnSync5 } from "child_process";
|
|
19578
|
-
import { existsSync as
|
|
19579
|
-
import { join as
|
|
19711
|
+
import { existsSync as existsSync11 } from "fs";
|
|
19712
|
+
import { join as join30 } from "path";
|
|
19580
19713
|
|
|
19581
19714
|
// src/bridge/user-gateway/db-query.ts
|
|
19582
19715
|
import { spawn as spawn3 } from "child_process";
|
|
@@ -19634,13 +19767,13 @@ function toResult(r) {
|
|
|
19634
19767
|
|
|
19635
19768
|
// src/bridge/user-gateway/gateway.ts
|
|
19636
19769
|
import { randomBytes as randomBytes10 } from "crypto";
|
|
19637
|
-
import { existsSync as
|
|
19770
|
+
import { existsSync as existsSync10 } from "fs";
|
|
19638
19771
|
import { mkdir as mkdir19, rm as rm14, writeFile as writeFile9 } from "fs/promises";
|
|
19639
|
-
import { join as
|
|
19772
|
+
import { join as join29 } from "path";
|
|
19640
19773
|
|
|
19641
19774
|
// src/bridge/user-gateway/app-token.ts
|
|
19642
19775
|
import { spawn as spawn4 } from "child_process";
|
|
19643
|
-
import { readFileSync as
|
|
19776
|
+
import { readFileSync as readFileSync6 } from "fs";
|
|
19644
19777
|
var APP_TOKEN_REFRESH_BEFORE_MS = 5 * 6e4;
|
|
19645
19778
|
var APP_TOKEN_FAIL_BACKOFF_MS = 6e4;
|
|
19646
19779
|
function createAppTokenCache(fetcher, now = Date.now) {
|
|
@@ -19685,7 +19818,7 @@ function brandOfTenant(tenant) {
|
|
|
19685
19818
|
}
|
|
19686
19819
|
function sourceBrand(sourceConfigFile) {
|
|
19687
19820
|
try {
|
|
19688
|
-
return brandOfTenant(JSON.parse(
|
|
19821
|
+
return brandOfTenant(JSON.parse(readFileSync6(sourceConfigFile, "utf8")).accounts?.app?.tenant);
|
|
19689
19822
|
} catch {
|
|
19690
19823
|
return "lark";
|
|
19691
19824
|
}
|
|
@@ -19722,7 +19855,7 @@ function run(command, args, input, env, timeoutMs) {
|
|
|
19722
19855
|
}
|
|
19723
19856
|
function realAppTokenFetcher(sourceConfigFile) {
|
|
19724
19857
|
return async () => {
|
|
19725
|
-
const cfg = JSON.parse(
|
|
19858
|
+
const cfg = JSON.parse(readFileSync6(sourceConfigFile, "utf8"));
|
|
19726
19859
|
const app = cfg.accounts?.app;
|
|
19727
19860
|
if (!app?.id) throw new Error("source \u914D\u7F6E\u91CC\u6CA1\u6709\u5E94\u7528 id");
|
|
19728
19861
|
let cred;
|
|
@@ -19878,10 +20011,10 @@ function createGateway(opts) {
|
|
|
19878
20011
|
const used = /* @__PURE__ */ new Map();
|
|
19879
20012
|
const inFlight = /* @__PURE__ */ new Set();
|
|
19880
20013
|
const pendingAuth = /* @__PURE__ */ new Map();
|
|
19881
|
-
const tokenRoot =
|
|
19882
|
-
const userDir = (openId) =>
|
|
19883
|
-
const authorizedMark = (openId) =>
|
|
19884
|
-
const isAuthorized = (openId) =>
|
|
20014
|
+
const tokenRoot = join29(opts.stateDir, "user-tokens", opts.appId);
|
|
20015
|
+
const userDir = (openId) => join29(tokenRoot, openId);
|
|
20016
|
+
const authorizedMark = (openId) => join29(userDir(openId), ".authorized");
|
|
20017
|
+
const isAuthorized = (openId) => existsSync10(authorizedMark(openId));
|
|
19885
20018
|
const brand = sourceBrand(opts.larkSourceConfig);
|
|
19886
20019
|
function env(dir) {
|
|
19887
20020
|
return {
|
|
@@ -19892,11 +20025,11 @@ function createGateway(opts) {
|
|
|
19892
20025
|
LARKSUITE_CLI_NO_SKILLS_NOTIFIER: "1"
|
|
19893
20026
|
};
|
|
19894
20027
|
}
|
|
19895
|
-
const appDir =
|
|
20028
|
+
const appDir = join29(opts.stateDir, "app");
|
|
19896
20029
|
const bound2 = /* @__PURE__ */ new Set();
|
|
19897
20030
|
async function ensureBound(dir, identity) {
|
|
19898
20031
|
if (bound2.has(dir)) return;
|
|
19899
|
-
if (!
|
|
20032
|
+
if (!existsSync10(join29(dir, "lark-channel", "config.json"))) {
|
|
19900
20033
|
await mkdir19(dir, { recursive: true, mode: 448 });
|
|
19901
20034
|
const r = await opts.exec(["config", "bind", "--source", "lark-channel", "--identity", identity], env(dir), 3e4);
|
|
19902
20035
|
if (r.code !== 0) throw new Error(`lark-cli \u7ED1\u5B9A\u5931\u8D25\uFF1A${larkError(r).detail}`);
|
|
@@ -20195,7 +20328,7 @@ function createGateway(opts) {
|
|
|
20195
20328
|
if (typeof v === "string") return { status: v };
|
|
20196
20329
|
if (v.text?.trim() !== REVOKE_TEXT2) return { status: "not_revoke" };
|
|
20197
20330
|
for (const [h, x] of handles) if (x.openId === v.openId) handles.delete(h);
|
|
20198
|
-
const had =
|
|
20331
|
+
const had = existsSync10(userDir(v.openId));
|
|
20199
20332
|
await rm14(userDir(v.openId), { recursive: true, force: true });
|
|
20200
20333
|
bound2.delete(userDir(v.openId));
|
|
20201
20334
|
log.info("user-gateway", "auth.revoked", { sender: tail6(v.openId), messageId, had });
|
|
@@ -20442,10 +20575,10 @@ function fetchToken(cmd) {
|
|
|
20442
20575
|
}
|
|
20443
20576
|
async function runUserGateway(opts) {
|
|
20444
20577
|
if (!/^cli_[A-Za-z0-9]+$/.test(opts.appId)) throw new Error("--app-id \u683C\u5F0F\u4E0D\u5BF9");
|
|
20445
|
-
if (!
|
|
20578
|
+
if (!existsSync11(opts.larkSourceConfig)) throw new Error(`\u627E\u4E0D\u5230 lark-cli source \u914D\u7F6E\uFF1A${opts.larkSourceConfig}`);
|
|
20446
20579
|
const port = Number(opts.port ?? 8788);
|
|
20447
20580
|
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("--port \u4E0D\u5BF9");
|
|
20448
|
-
configureLogger({ logsDir:
|
|
20581
|
+
configureLogger({ logsDir: join30(opts.stateDir, "logs") });
|
|
20449
20582
|
const gateway = createGateway({
|
|
20450
20583
|
appId: opts.appId,
|
|
20451
20584
|
stateDir: opts.stateDir,
|