engine7 7.1.121 → 7.1.122
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.mjs +174 -138
- package/dist/engine-startup.mjs +330 -148
- package/dist/main.mjs +330 -148
- package/dist/mem7-worker.mjs +15 -2
- package/package.json +1 -1
- package/templates/config.template.json +1 -1
package/dist/engine-startup.mjs
CHANGED
|
@@ -3873,7 +3873,7 @@ var init_read_budget = __esm({
|
|
|
3873
3873
|
check(depth = 0) {
|
|
3874
3874
|
if (this.failure) throw this.failure;
|
|
3875
3875
|
const elapsed = this.elapsedMs + (this.started === void 0 ? 0 : this.now() - this.started);
|
|
3876
|
-
const reason = depth > (this.limits.maxDepth ?? 256) ? "depth" : ++this.steps > (this.limits.maxSteps ??
|
|
3876
|
+
const reason = depth > (this.limits.maxDepth ?? 256) ? "depth" : ++this.steps > (this.limits.maxSteps ?? 25e4) ? "work" : elapsed >= (this.limits.maxMs ?? 4e3) ? "time" : void 0;
|
|
3877
3877
|
if (reason) {
|
|
3878
3878
|
this.failure = new MemoryReadBudgetExceededError(reason);
|
|
3879
3879
|
throw this.failure;
|
|
@@ -7366,6 +7366,11 @@ CREATE TABLE memory_state_outbox (
|
|
|
7366
7366
|
payload_json TEXT NOT NULL, created_at TEXT NOT NULL,
|
|
7367
7367
|
PRIMARY KEY(scope_id,event_seq), FOREIGN KEY(scope_id) REFERENCES memory_scopes(scope_id)
|
|
7368
7368
|
);
|
|
7369
|
+
-- 1013\uFF1A\u6309 (scope_id,kind) \u53D6 MAX(aggregate_version) \u7684\u805A\u5408\u7248\u672C\u67E5\u8BE2\uFF0C\u9760 PK \u53EA\u80FD\u547D\u4E2D scope_id\uFF0C
|
|
7370
|
+
-- \u4E4B\u540E\u8981\u626B\u8BE5 scope \u4E0B\u5168\u90E8 outbox \u884C\u7B97 MAX\u3002\u8BE5\u67E5\u8BE2\u5728\u5355\u6B21 buildMemoryContext \u91CC\u88AB\u8C03\u7528\u6570\u767E\u6B21
|
|
7371
|
+
-- \uFF08\u59B9\u59B9 2015 \u884C\u5E93 \u2248 140 \u4E07\u884C\u8BBF\u95EE\uFF09\uFF0C\u5B9E\u6D4B\u628A\u5F53\u524D\u4E0A\u4E0B\u6587\u88C5\u914D\u4ECE 719ms \u63A8\u5230 2617ms\uFF0C\u76F4\u63A5\u51FB\u7A7F
|
|
7372
|
+
-- turnReadBudgetMs \u9ED8\u8BA4 1500ms \u2192 \u5168\u8F6E\u964D\u7EA7 \u2192 \u5360\u4F4D\u7B26\u4E8B\u6545\u3002\u8986\u76D6\u7D22\u5F15\u8BA9 MAX \u9000\u5316\u6210\u7D22\u5F15\u672B\u9879\u5B9A\u4F4D\u3002
|
|
7373
|
+
CREATE INDEX IF NOT EXISTS idx_outbox_scope_kind_version ON memory_state_outbox(scope_id,kind,aggregate_version);
|
|
7369
7374
|
`;
|
|
7370
7375
|
}
|
|
7371
7376
|
});
|
|
@@ -10996,6 +11001,14 @@ function migrateMem7(db2, initializeLegacySchema) {
|
|
|
10996
11001
|
version: 29,
|
|
10997
11002
|
name: "idempotent-source-backed-chat-people-updates",
|
|
10998
11003
|
apply: (database) => database.exec(SOCIAL_WRITE_SCHEMA)
|
|
11004
|
+
}, {
|
|
11005
|
+
version: 30,
|
|
11006
|
+
name: "indexed-outbox-aggregate-revisions",
|
|
11007
|
+
// 存量库补索引:聚合版本查询此前要扫该 scope 下全部 outbox 行算 MAX,
|
|
11008
|
+
// 实测妹妹 182MB 库 buildMemoryContext 2617ms → 719ms(读预算默认 1500ms 本来必破)。
|
|
11009
|
+
apply: (database) => database.exec(
|
|
11010
|
+
"CREATE INDEX IF NOT EXISTS idx_outbox_scope_kind_version ON memory_state_outbox(scope_id,kind,aggregate_version)"
|
|
11011
|
+
)
|
|
10999
11012
|
}]);
|
|
11000
11013
|
} finally {
|
|
11001
11014
|
if (foreignKeys) db2.exec("PRAGMA foreign_keys=ON");
|
|
@@ -11021,7 +11034,7 @@ var init_migrations = __esm({
|
|
|
11021
11034
|
init_relationship_cognition();
|
|
11022
11035
|
init_relationship_notices();
|
|
11023
11036
|
init_relationship_history();
|
|
11024
|
-
MEM7_SCHEMA_VERSION =
|
|
11037
|
+
MEM7_SCHEMA_VERSION = 30;
|
|
11025
11038
|
}
|
|
11026
11039
|
});
|
|
11027
11040
|
|
|
@@ -14787,6 +14800,130 @@ var init_executor2 = __esm({
|
|
|
14787
14800
|
}
|
|
14788
14801
|
});
|
|
14789
14802
|
|
|
14803
|
+
// src/mem7/assistant-context.ts
|
|
14804
|
+
function emittedWithheldPlaceholder(text) {
|
|
14805
|
+
return typeof text === "string" && text.trim() === withheldSourceBody;
|
|
14806
|
+
}
|
|
14807
|
+
function withholdSource(message) {
|
|
14808
|
+
message.content = withheldSourceBody;
|
|
14809
|
+
delete message.memoryOriginalText;
|
|
14810
|
+
delete message.reasoning_content;
|
|
14811
|
+
delete message.thinking_signature;
|
|
14812
|
+
delete message.responses_reasoning;
|
|
14813
|
+
}
|
|
14814
|
+
function degradeUncheckedMemory(messages) {
|
|
14815
|
+
const safeMessages = [];
|
|
14816
|
+
for (const message of messages) {
|
|
14817
|
+
if (isAttachmentMessage(message)) {
|
|
14818
|
+
if (message.attachment.type !== "relevant_memories") safeMessages.push(message);
|
|
14819
|
+
continue;
|
|
14820
|
+
}
|
|
14821
|
+
safeMessages.push(message);
|
|
14822
|
+
}
|
|
14823
|
+
return safeMessages;
|
|
14824
|
+
}
|
|
14825
|
+
function memoryContextRefs(store, scope, value) {
|
|
14826
|
+
if (!value || typeof value !== "object") return [];
|
|
14827
|
+
const input = value;
|
|
14828
|
+
if (input.scopeId !== void 0 && input.scopeId !== scopeId(scope)) return [];
|
|
14829
|
+
const refs = /* @__PURE__ */ new Map();
|
|
14830
|
+
const add = (candidate) => {
|
|
14831
|
+
checkMemoryReadBudget();
|
|
14832
|
+
if (!candidate || typeof candidate.sourceId !== "string") return;
|
|
14833
|
+
const source = store.sources.get(scope, candidate.sourceId);
|
|
14834
|
+
if (!source || !store.socialSources.retained(scope, source.sourceId)) return;
|
|
14835
|
+
if (candidate.scopeId !== void 0 && candidate.scopeId !== scopeId(scope)) return;
|
|
14836
|
+
if (candidate.sourceVersion !== void 0 && candidate.sourceVersion !== source.sourceVersion) return;
|
|
14837
|
+
const ref = { scopeId: scopeId(scope), sourceId: source.sourceId, sourceVersion: source.sourceVersion };
|
|
14838
|
+
if (candidate.relationshipId !== void 0 || candidate.semanticVersion !== void 0) {
|
|
14839
|
+
if (typeof candidate.relationshipId !== "string" || !Number.isSafeInteger(candidate.semanticVersion) || candidate.semanticVersion < 1) return;
|
|
14840
|
+
ref.relationshipId = candidate.relationshipId;
|
|
14841
|
+
ref.semanticVersion = candidate.semanticVersion;
|
|
14842
|
+
}
|
|
14843
|
+
if (candidate.purpose !== void 0) {
|
|
14844
|
+
if (!["evidence", "context", "historical"].includes(candidate.purpose)) return;
|
|
14845
|
+
ref.purpose = candidate.purpose;
|
|
14846
|
+
}
|
|
14847
|
+
if (store.socialSources.referenceRetained(scope, ref)) refs.set(canonicalJson(ref), ref);
|
|
14848
|
+
};
|
|
14849
|
+
for (const list2 of [input.contextRefs, input.relationships?.contextRefs, input.citations, input.pendingSources]) {
|
|
14850
|
+
if (Array.isArray(list2)) for (const ref of list2) add(ref);
|
|
14851
|
+
}
|
|
14852
|
+
const relationships = Array.isArray(input.relationships) ? input.relationships : input.relationships?.relationships;
|
|
14853
|
+
if (Array.isArray(relationships)) for (const relationship of relationships) {
|
|
14854
|
+
if (!relationship || typeof relationship.relationshipId !== "string" || !Number.isSafeInteger(relationship.semanticVersion)) continue;
|
|
14855
|
+
for (const list2 of [relationship.evidenceSources, relationship.basisSources]) {
|
|
14856
|
+
if (Array.isArray(list2)) for (const source of list2) add({
|
|
14857
|
+
sourceId: source.sourceId,
|
|
14858
|
+
sourceVersion: source.sourceVersion,
|
|
14859
|
+
relationshipId: relationship.relationshipId,
|
|
14860
|
+
semanticVersion: relationship.semanticVersion,
|
|
14861
|
+
purpose: relationship.current === false ? "historical" : "context"
|
|
14862
|
+
});
|
|
14863
|
+
}
|
|
14864
|
+
}
|
|
14865
|
+
for (const list2 of [input.historicalEvidence, input.episodes, input.entries]) {
|
|
14866
|
+
if (Array.isArray(list2)) {
|
|
14867
|
+
for (const item of list2) if (Array.isArray(item.sourceIds)) for (const sourceId of item.sourceIds) add({ sourceId });
|
|
14868
|
+
}
|
|
14869
|
+
}
|
|
14870
|
+
if (typeof input.sourceId === "string" && Number.isSafeInteger(input.sourceVersion)) add(input);
|
|
14871
|
+
return [...refs.values()];
|
|
14872
|
+
}
|
|
14873
|
+
function assistantContextRefs(store, scope, messages) {
|
|
14874
|
+
const refs = /* @__PURE__ */ new Map();
|
|
14875
|
+
const add = (ref) => refs.set(canonicalJson(ref), ref);
|
|
14876
|
+
for (const message of messages) {
|
|
14877
|
+
checkMemoryReadBudget();
|
|
14878
|
+
if (isAttachmentMessage(message)) {
|
|
14879
|
+
if (message.attachment.type !== "relevant_memories") continue;
|
|
14880
|
+
for (const memory of message.attachment.memories) {
|
|
14881
|
+
if (memory.mem7 && scopeId(memory.mem7.scope) === scopeId(scope)) {
|
|
14882
|
+
for (const sourceId of memory.mem7.sourceIds) {
|
|
14883
|
+
const source = store.sources.get(scope, sourceId);
|
|
14884
|
+
if (source && store.socialSources.retained(scope, sourceId)) add({ scopeId: scopeId(scope), sourceId, sourceVersion: source.sourceVersion });
|
|
14885
|
+
}
|
|
14886
|
+
}
|
|
14887
|
+
if (memory.path.startsWith("mem7-context://")) {
|
|
14888
|
+
let value;
|
|
14889
|
+
try {
|
|
14890
|
+
value = JSON.parse(memory.content);
|
|
14891
|
+
} catch (error) {
|
|
14892
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
14893
|
+
continue;
|
|
14894
|
+
}
|
|
14895
|
+
for (const ref of memoryContextRefs(store, scope, value)) add(ref);
|
|
14896
|
+
}
|
|
14897
|
+
}
|
|
14898
|
+
} else if (message.content !== withheldSourceBody && message.memoryOriginKey && message.memoryScope && scopeId(message.memoryScope) === scopeId(scope)) {
|
|
14899
|
+
const source = store.sources.getByOrigin(scope, message.memoryOriginKey, message.memorySourceVersion ?? 1);
|
|
14900
|
+
if (source && source.role === message.role && store.socialSources.retained(scope, source.sourceId))
|
|
14901
|
+
add({ scopeId: scopeId(scope), sourceId: source.sourceId, sourceVersion: source.sourceVersion });
|
|
14902
|
+
}
|
|
14903
|
+
}
|
|
14904
|
+
return [...refs.values()];
|
|
14905
|
+
}
|
|
14906
|
+
function scrubUnavailableSourceMessages(store, scope, messages) {
|
|
14907
|
+
for (const message of messages) {
|
|
14908
|
+
checkMemoryReadBudget();
|
|
14909
|
+
if (isAttachmentMessage(message) || !message.memoryOriginKey || !message.memoryScope || scopeId(message.memoryScope) !== scopeId(scope) || !store.sources.hasOrigin(scope, message.memoryOriginKey)) continue;
|
|
14910
|
+
const source = store.sources.getByOrigin(scope, message.memoryOriginKey, message.memorySourceVersion ?? 1);
|
|
14911
|
+
if (source && store.socialSources.retained(scope, source.sourceId)) continue;
|
|
14912
|
+
withholdSource(message);
|
|
14913
|
+
}
|
|
14914
|
+
}
|
|
14915
|
+
var withheldSourceBody;
|
|
14916
|
+
var init_assistant_context = __esm({
|
|
14917
|
+
"src/mem7/assistant-context.ts"() {
|
|
14918
|
+
"use strict";
|
|
14919
|
+
init_types();
|
|
14920
|
+
init_scope();
|
|
14921
|
+
init_temporal();
|
|
14922
|
+
init_read_budget();
|
|
14923
|
+
withheldSourceBody = "[\u5BF9\u5E94\u8BB0\u5FC6\u6765\u6E90\u672C\u8F6E\u672A\u901A\u8FC7\u6821\u9A8C\uFF0C\u539F\u6587\u6682\u4E0D\u63D0\u4F9B\u3002]";
|
|
14924
|
+
}
|
|
14925
|
+
});
|
|
14926
|
+
|
|
14790
14927
|
// src/memory/memdir/memoryAge.ts
|
|
14791
14928
|
function memoryAgeDays(mtimeMs) {
|
|
14792
14929
|
return Math.max(0, Math.floor((Date.now() - mtimeMs) / 864e5));
|
|
@@ -17208,6 +17345,7 @@ var init_query = __esm({
|
|
|
17208
17345
|
"use strict";
|
|
17209
17346
|
init_executor2();
|
|
17210
17347
|
init_host_turn();
|
|
17348
|
+
init_assistant_context();
|
|
17211
17349
|
init_external_effect();
|
|
17212
17350
|
init_attachments();
|
|
17213
17351
|
init_context_dump();
|
|
@@ -17658,6 +17796,10 @@ ${perTurnSystemDynamic}` : deferredHint || perTurnSystemDynamic;
|
|
|
17658
17796
|
return;
|
|
17659
17797
|
}
|
|
17660
17798
|
if (toolCalls.length === 0) {
|
|
17799
|
+
if (emittedWithheldPlaceholder(textContent)) {
|
|
17800
|
+
console.warn(`${this.logPrefix} Turn ${turnCount}: \u6A21\u578B\u8F93\u51FA\u7CFB\u7EDF\u5360\u4F4D\u7B26\uFF0C\u6309\u7A7A\u54CD\u5E94\u91CD\u8BD5\uFF08\u4E0D\u5165\u5386\u53F2\u3001\u4E0D\u51FA\u7AD9\uFF09`);
|
|
17801
|
+
textContent = "";
|
|
17802
|
+
}
|
|
17661
17803
|
if (!textContent.trim()) {
|
|
17662
17804
|
const thinkingAteBudget = turnThinking.length > 0;
|
|
17663
17805
|
console.log(`${this.logPrefix} Turn ${turnCount}: API returned empty (no text, no tool_call).${thinkingAteBudget ? ` ${turnThinking.length}ch thinking \u5403\u5149\u8F93\u51FA\u9884\u7B97 \u2192 retry with thinking off` : " Retrying..."} messages=${currentMessages.length}`);
|
|
@@ -42529,130 +42671,8 @@ var NoteRateLimiter = class {
|
|
|
42529
42671
|
|
|
42530
42672
|
// src/tools/memory-bridge.ts
|
|
42531
42673
|
init_task_manager();
|
|
42674
|
+
init_assistant_context();
|
|
42532
42675
|
import { join as join28 } from "node:path";
|
|
42533
|
-
|
|
42534
|
-
// src/mem7/assistant-context.ts
|
|
42535
|
-
init_types();
|
|
42536
|
-
init_scope();
|
|
42537
|
-
init_temporal();
|
|
42538
|
-
init_read_budget();
|
|
42539
|
-
var withheldSourceBody = "[\u5BF9\u5E94\u8BB0\u5FC6\u6765\u6E90\u672C\u8F6E\u672A\u901A\u8FC7\u6821\u9A8C\uFF0C\u539F\u6587\u6682\u4E0D\u63D0\u4F9B\u3002]";
|
|
42540
|
-
function withholdSource(message) {
|
|
42541
|
-
message.content = withheldSourceBody;
|
|
42542
|
-
delete message.memoryOriginalText;
|
|
42543
|
-
delete message.reasoning_content;
|
|
42544
|
-
delete message.thinking_signature;
|
|
42545
|
-
delete message.responses_reasoning;
|
|
42546
|
-
}
|
|
42547
|
-
function withholdUncheckedMemory(messages, currentMessage) {
|
|
42548
|
-
const safeMessages = [];
|
|
42549
|
-
for (const message of messages) {
|
|
42550
|
-
if (isAttachmentMessage(message)) {
|
|
42551
|
-
if (message.attachment.type !== "relevant_memories") safeMessages.push(message);
|
|
42552
|
-
continue;
|
|
42553
|
-
}
|
|
42554
|
-
if (!message.memoryOriginKey || message === currentMessage) {
|
|
42555
|
-
safeMessages.push(message);
|
|
42556
|
-
continue;
|
|
42557
|
-
}
|
|
42558
|
-
const safe = { ...message };
|
|
42559
|
-
withholdSource(safe);
|
|
42560
|
-
safeMessages.push(safe);
|
|
42561
|
-
}
|
|
42562
|
-
return safeMessages;
|
|
42563
|
-
}
|
|
42564
|
-
function memoryContextRefs(store, scope, value) {
|
|
42565
|
-
if (!value || typeof value !== "object") return [];
|
|
42566
|
-
const input = value;
|
|
42567
|
-
if (input.scopeId !== void 0 && input.scopeId !== scopeId(scope)) return [];
|
|
42568
|
-
const refs = /* @__PURE__ */ new Map();
|
|
42569
|
-
const add = (candidate) => {
|
|
42570
|
-
checkMemoryReadBudget();
|
|
42571
|
-
if (!candidate || typeof candidate.sourceId !== "string") return;
|
|
42572
|
-
const source = store.sources.get(scope, candidate.sourceId);
|
|
42573
|
-
if (!source || !store.socialSources.retained(scope, source.sourceId)) return;
|
|
42574
|
-
if (candidate.scopeId !== void 0 && candidate.scopeId !== scopeId(scope)) return;
|
|
42575
|
-
if (candidate.sourceVersion !== void 0 && candidate.sourceVersion !== source.sourceVersion) return;
|
|
42576
|
-
const ref = { scopeId: scopeId(scope), sourceId: source.sourceId, sourceVersion: source.sourceVersion };
|
|
42577
|
-
if (candidate.relationshipId !== void 0 || candidate.semanticVersion !== void 0) {
|
|
42578
|
-
if (typeof candidate.relationshipId !== "string" || !Number.isSafeInteger(candidate.semanticVersion) || candidate.semanticVersion < 1) return;
|
|
42579
|
-
ref.relationshipId = candidate.relationshipId;
|
|
42580
|
-
ref.semanticVersion = candidate.semanticVersion;
|
|
42581
|
-
}
|
|
42582
|
-
if (candidate.purpose !== void 0) {
|
|
42583
|
-
if (!["evidence", "context", "historical"].includes(candidate.purpose)) return;
|
|
42584
|
-
ref.purpose = candidate.purpose;
|
|
42585
|
-
}
|
|
42586
|
-
if (store.socialSources.referenceRetained(scope, ref)) refs.set(canonicalJson(ref), ref);
|
|
42587
|
-
};
|
|
42588
|
-
for (const list2 of [input.contextRefs, input.relationships?.contextRefs, input.citations, input.pendingSources]) {
|
|
42589
|
-
if (Array.isArray(list2)) for (const ref of list2) add(ref);
|
|
42590
|
-
}
|
|
42591
|
-
const relationships = Array.isArray(input.relationships) ? input.relationships : input.relationships?.relationships;
|
|
42592
|
-
if (Array.isArray(relationships)) for (const relationship of relationships) {
|
|
42593
|
-
if (!relationship || typeof relationship.relationshipId !== "string" || !Number.isSafeInteger(relationship.semanticVersion)) continue;
|
|
42594
|
-
for (const list2 of [relationship.evidenceSources, relationship.basisSources]) {
|
|
42595
|
-
if (Array.isArray(list2)) for (const source of list2) add({
|
|
42596
|
-
sourceId: source.sourceId,
|
|
42597
|
-
sourceVersion: source.sourceVersion,
|
|
42598
|
-
relationshipId: relationship.relationshipId,
|
|
42599
|
-
semanticVersion: relationship.semanticVersion,
|
|
42600
|
-
purpose: relationship.current === false ? "historical" : "context"
|
|
42601
|
-
});
|
|
42602
|
-
}
|
|
42603
|
-
}
|
|
42604
|
-
for (const list2 of [input.historicalEvidence, input.episodes, input.entries]) {
|
|
42605
|
-
if (Array.isArray(list2)) {
|
|
42606
|
-
for (const item of list2) if (Array.isArray(item.sourceIds)) for (const sourceId of item.sourceIds) add({ sourceId });
|
|
42607
|
-
}
|
|
42608
|
-
}
|
|
42609
|
-
if (typeof input.sourceId === "string" && Number.isSafeInteger(input.sourceVersion)) add(input);
|
|
42610
|
-
return [...refs.values()];
|
|
42611
|
-
}
|
|
42612
|
-
function assistantContextRefs(store, scope, messages) {
|
|
42613
|
-
const refs = /* @__PURE__ */ new Map();
|
|
42614
|
-
const add = (ref) => refs.set(canonicalJson(ref), ref);
|
|
42615
|
-
for (const message of messages) {
|
|
42616
|
-
checkMemoryReadBudget();
|
|
42617
|
-
if (isAttachmentMessage(message)) {
|
|
42618
|
-
if (message.attachment.type !== "relevant_memories") continue;
|
|
42619
|
-
for (const memory of message.attachment.memories) {
|
|
42620
|
-
if (memory.mem7 && scopeId(memory.mem7.scope) === scopeId(scope)) {
|
|
42621
|
-
for (const sourceId of memory.mem7.sourceIds) {
|
|
42622
|
-
const source = store.sources.get(scope, sourceId);
|
|
42623
|
-
if (source && store.socialSources.retained(scope, sourceId)) add({ scopeId: scopeId(scope), sourceId, sourceVersion: source.sourceVersion });
|
|
42624
|
-
}
|
|
42625
|
-
}
|
|
42626
|
-
if (memory.path.startsWith("mem7-context://")) {
|
|
42627
|
-
let value;
|
|
42628
|
-
try {
|
|
42629
|
-
value = JSON.parse(memory.content);
|
|
42630
|
-
} catch (error) {
|
|
42631
|
-
if (!(error instanceof SyntaxError)) throw error;
|
|
42632
|
-
continue;
|
|
42633
|
-
}
|
|
42634
|
-
for (const ref of memoryContextRefs(store, scope, value)) add(ref);
|
|
42635
|
-
}
|
|
42636
|
-
}
|
|
42637
|
-
} else if (message.content !== withheldSourceBody && message.memoryOriginKey && message.memoryScope && scopeId(message.memoryScope) === scopeId(scope)) {
|
|
42638
|
-
const source = store.sources.getByOrigin(scope, message.memoryOriginKey, message.memorySourceVersion ?? 1);
|
|
42639
|
-
if (source && source.role === message.role && store.socialSources.retained(scope, source.sourceId))
|
|
42640
|
-
add({ scopeId: scopeId(scope), sourceId: source.sourceId, sourceVersion: source.sourceVersion });
|
|
42641
|
-
}
|
|
42642
|
-
}
|
|
42643
|
-
return [...refs.values()];
|
|
42644
|
-
}
|
|
42645
|
-
function scrubUnavailableSourceMessages(store, scope, messages) {
|
|
42646
|
-
for (const message of messages) {
|
|
42647
|
-
checkMemoryReadBudget();
|
|
42648
|
-
if (isAttachmentMessage(message) || !message.memoryOriginKey || !message.memoryScope || scopeId(message.memoryScope) !== scopeId(scope) || !store.sources.hasOrigin(scope, message.memoryOriginKey)) continue;
|
|
42649
|
-
const source = store.sources.getByOrigin(scope, message.memoryOriginKey, message.memorySourceVersion ?? 1);
|
|
42650
|
-
if (source && store.socialSources.retained(scope, source.sourceId)) continue;
|
|
42651
|
-
withholdSource(message);
|
|
42652
|
-
}
|
|
42653
|
-
}
|
|
42654
|
-
|
|
42655
|
-
// src/tools/memory-bridge.ts
|
|
42656
42676
|
var EverosSearchSchema = {
|
|
42657
42677
|
type: "object",
|
|
42658
42678
|
properties: {
|
|
@@ -44021,7 +44041,8 @@ var FIELDS = [
|
|
|
44021
44041
|
label: "\u6BCF\u8F6E\u8BB0\u5FC6\u8BFB\u53D6\u64CD\u4F5C\u4E0A\u9650",
|
|
44022
44042
|
effect: "hot",
|
|
44023
44043
|
internal: true,
|
|
44024
|
-
|
|
44044
|
+
// 上限必须 ≥ 代码默认值 250_000(1012 抬默认时漏了这里,导致托盘/CLI 拒收代码自己的默认值)
|
|
44045
|
+
validate: (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 1 && value <= 5e5 ? null : "\u6BCF\u8F6E\u8BB0\u5FC6\u8BFB\u53D6\u64CD\u4F5C\u987B\u4E3A 1\u2013500000 \u6B21"
|
|
44025
44046
|
},
|
|
44026
44047
|
{
|
|
44027
44048
|
path: "mem7.relationships",
|
|
@@ -45312,6 +45333,7 @@ init_task_manager();
|
|
|
45312
45333
|
init_execution_guard();
|
|
45313
45334
|
init_external_effect();
|
|
45314
45335
|
init_intake();
|
|
45336
|
+
init_assistant_context();
|
|
45315
45337
|
init_read_budget();
|
|
45316
45338
|
init_scope();
|
|
45317
45339
|
init_host_turn();
|
|
@@ -46532,8 +46554,21 @@ async function handleQueryInner(text, sessionId, channelName, cb, deps, channelT
|
|
|
46532
46554
|
const memoryContextRefs2 = [];
|
|
46533
46555
|
const memoryStateDir = memoryIntake?.stateDir ?? String(liveConfig.get("stateDir") ?? deps.stateDir ?? "");
|
|
46534
46556
|
const memoryReadBudget = new MemoryReadBudget({
|
|
46535
|
-
|
|
46536
|
-
|
|
46557
|
+
// 1013:时间闸默认 1500ms 同样低于合法工作集——1012 为步数闸写下的原则(「闸只防无进展的
|
|
46558
|
+
// 死循环,不该拦合法工作」)当时没套用到时间闸上,它的 447ms 依据只测了 scrub+strip+refs,
|
|
46559
|
+
// 漏掉最贵的 buildMemoryContext。实测妹妹 182MB 库:三阶段累计 1196–1474ms,1500ms 下余量
|
|
46560
|
+
// 只剩 26–304ms,正常聊天即耗尽(1013 占位符事故,reason=time)。抬到 4000ms(小柯线上
|
|
46561
|
+
// 已跑这个值);闸仍兜 1009 式事件循环冻结,只是不再切合法工作。
|
|
46562
|
+
// 真正的成本在 a75d3804 的 outbox 覆盖索引:当前上下文装配 2617ms → ~1100ms。
|
|
46563
|
+
maxMs: liveConfig.get("mem7.turnReadBudgetMs") ?? 4e3,
|
|
46564
|
+
// 1012:步数闸默认 5 万被证低于合法工作集——忙 scope 单轮两遍冷走(:438 prompt 前 + :1042 query 循环内,
|
|
46565
|
+
// 中间 worker 落盘会失效快照)≈ 5 万步,正常聊天即触发全量扣原文(占位符刷屏事故)。真安全线是时间闸
|
|
46566
|
+
// 1500ms(防 1009 式事件循环冻结),步数闸只防无进展的死循环,抬到 25 万。实测妹妹 151MB 库全轮 447ms。
|
|
46567
|
+
// ⚠️ 1013 更正:上面「真安全线是时间闸 1500ms」已被证伪——那次 447ms 只测了
|
|
46568
|
+
// scrub+strip+refs(见 scripts/_probe-scrub-steps-1012.mts),漏掉最贵的 buildMemoryContext
|
|
46569
|
+
// (同库实测 2617ms),所以 1500ms 当时就已经不可达。步数闸的结论仍然成立(实测
|
|
46570
|
+
// 三阶段 102150 步,25 万合适)。
|
|
46571
|
+
maxSteps: liveConfig.get("mem7.turnReadMaxSteps") ?? 25e4
|
|
46537
46572
|
});
|
|
46538
46573
|
let memoryReadDegraded = false;
|
|
46539
46574
|
let currentPromptMessage;
|
|
@@ -46588,7 +46623,7 @@ async function handleQueryInner(text, sessionId, channelName, cb, deps, channelT
|
|
|
46588
46623
|
Promise.resolve().then(() => (init_historical_recall(), historical_recall_exports))
|
|
46589
46624
|
]);
|
|
46590
46625
|
revalidateRecall = (items) => {
|
|
46591
|
-
if (memoryReadDegraded) return
|
|
46626
|
+
if (memoryReadDegraded) return degradeUncheckedMemory(items);
|
|
46592
46627
|
const invalidPaths = /* @__PURE__ */ new Set();
|
|
46593
46628
|
const checked = items.map((item) => isAttachmentMessage(item) || item === currentPromptMessage ? item : { ...item });
|
|
46594
46629
|
try {
|
|
@@ -46608,7 +46643,7 @@ async function handleQueryInner(text, sessionId, channelName, cb, deps, channelT
|
|
|
46608
46643
|
} catch (error) {
|
|
46609
46644
|
if (!isMemoryReadUnavailable(error)) throw error;
|
|
46610
46645
|
degradeMemoryRead(error);
|
|
46611
|
-
return
|
|
46646
|
+
return degradeUncheckedMemory(items);
|
|
46612
46647
|
}
|
|
46613
46648
|
if (invalidPaths.size) {
|
|
46614
46649
|
stripRecalledMemories(history, (path73) => invalidPaths.has(normalizeMemoryPath(path73)));
|
|
@@ -47133,7 +47168,7 @@ ${evidence}
|
|
|
47133
47168
|
} catch (error) {
|
|
47134
47169
|
if (isMemoryReadUnavailable(error)) {
|
|
47135
47170
|
degradeMemoryRead(error);
|
|
47136
|
-
messages =
|
|
47171
|
+
messages = degradeUncheckedMemory(messages);
|
|
47137
47172
|
}
|
|
47138
47173
|
console.warn(`[handle-query] mem7 current context failed: ${error?.message ?? error}`);
|
|
47139
47174
|
}
|
|
@@ -47157,7 +47192,7 @@ ${evidence}
|
|
|
47157
47192
|
} catch (error) {
|
|
47158
47193
|
if (!isMemoryReadUnavailable(error)) throw error;
|
|
47159
47194
|
degradeMemoryRead(error);
|
|
47160
|
-
messages =
|
|
47195
|
+
messages = degradeUncheckedMemory(messages);
|
|
47161
47196
|
}
|
|
47162
47197
|
}
|
|
47163
47198
|
if (memoryHostSourceId && !memoryContextRefs2.some((ref) => ref.sourceId === memoryHostSourceId)) {
|
|
@@ -47306,7 +47341,12 @@ ${evidence}
|
|
|
47306
47341
|
const rawContent = chunk.content ?? "";
|
|
47307
47342
|
const inputTokens = chunk.inputTokens ?? 0;
|
|
47308
47343
|
const outputTokens = chunk.outputTokens ?? 0;
|
|
47309
|
-
const
|
|
47344
|
+
const placeholderLeak = emittedWithheldPlaceholder(rawContent);
|
|
47345
|
+
if (placeholderLeak) {
|
|
47346
|
+
console.warn("[handle-query] \u26A0\uFE0F \u6A21\u578B\u91CD\u8BD5\u540E\u4ECD\u8F93\u51FA\u7CFB\u7EDF\u5360\u4F4D\u7B26\u2014\u2014\u62D2\u7EDD\u5165\u5386\u53F2\u4E0E\u51FA\u7AD9");
|
|
47347
|
+
}
|
|
47348
|
+
const usableContent = placeholderLeak ? "" : rawContent;
|
|
47349
|
+
const resultContent = usableContent.trim() || fullResponse.trim() || "(\u65E0\u56DE\u590D)";
|
|
47310
47350
|
if (!rawContent.trim()) {
|
|
47311
47351
|
console.log(`[handle-query] \u26A0\uFE0F EMPTY RESULT after ${toolCount} tools, falling back to ${fullResponse.length} chars accumulated`);
|
|
47312
47352
|
}
|
|
@@ -51785,6 +51825,14 @@ async function startHttpApi(ctx) {
|
|
|
51785
51825
|
httpServer.listen(CONTEXT_API_PORT, "127.0.0.1", () => {
|
|
51786
51826
|
console.log(`[api] Context usage API: http://127.0.0.1:${CONTEXT_API_PORT}/api/context`);
|
|
51787
51827
|
});
|
|
51828
|
+
httpServer.on("error", (err) => {
|
|
51829
|
+
if (err.code === "EADDRINUSE") {
|
|
51830
|
+
console.error(`[api] \u7AEF\u53E3 ${CONTEXT_API_PORT} \u5DF2\u88AB\u5360\u7528\u2014\u2014\u672C\u5730\u63A7\u5236\u53F0/\u6258\u76D8\u63A5\u53E3\u4E0D\u53EF\u7528\uFF0C\u804A\u5929\u901A\u9053\u4E0D\u53D7\u5F71\u54CD\u3002`);
|
|
51831
|
+
console.error(`[api] \u5904\u7F6E\uFF1A\u505C\u6389\u5360\u7528\u8005\uFF0C\u6216\u6539 config \u7684 api.port \u540E\u91CD\u542F\u3002`);
|
|
51832
|
+
} else {
|
|
51833
|
+
console.error(`[api] HTTP \u670D\u52A1\u542F\u52A8\u5931\u8D25\uFF08${err.code ?? err.message}\uFF09\u2014\u2014\u63A7\u5236\u53F0\u63A5\u53E3\u4E0D\u53EF\u7528\uFF0C\u5F15\u64CE\u7EE7\u7EED\u8FD0\u884C\u3002`);
|
|
51834
|
+
}
|
|
51835
|
+
});
|
|
51788
51836
|
registerOacBridge(httpServer, dispatcher, deps, config);
|
|
51789
51837
|
return httpServer;
|
|
51790
51838
|
}
|
|
@@ -55639,7 +55687,9 @@ init_external_effect();
|
|
|
55639
55687
|
import * as lark from "@larksuiteoapi/node-sdk";
|
|
55640
55688
|
import { mkdir as mkdir5, writeFile as writeFile6 } from "node:fs/promises";
|
|
55641
55689
|
import path41 from "node:path";
|
|
55642
|
-
var
|
|
55690
|
+
var QUOTE_MAX_CHARS = 500;
|
|
55691
|
+
var FEISHU_POST_BYTES = 3e4;
|
|
55692
|
+
var FEISHU_POST_BUDGET = FEISHU_POST_BYTES - 1024;
|
|
55643
55693
|
var FeishuAdapter = class _FeishuAdapter {
|
|
55644
55694
|
name = "feishu";
|
|
55645
55695
|
memoryGuardedSends = true;
|
|
@@ -55650,6 +55700,15 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
55650
55700
|
botOpenId = "";
|
|
55651
55701
|
// 消息去重 — 飞书事件可能重复投递
|
|
55652
55702
|
recentMessageIds = /* @__PURE__ */ new Set();
|
|
55703
|
+
/**
|
|
55704
|
+
* 本 app **见过**的 open_id(入站发送者 + 被别人 @ 过的)—— 1013 转 @ 时的白名单。
|
|
55705
|
+
*
|
|
55706
|
+
* open_id 是**按 app 隔离**的:同一个人的 open_id 在不同 app 下完全不同。所以
|
|
55707
|
+
* agent 从别处(另一个 app 的记忆、幻觉)拿到一个 id 时,转成飞书 @ 会失败 ——
|
|
55708
|
+
* 而失败的形态是**把 `<at user_id="…">` 原样吐出来**,比不转还丑(1013 实测)。
|
|
55709
|
+
* 只认见过的,就是为了不产出那种更丑的形态。
|
|
55710
|
+
*/
|
|
55711
|
+
_knownUserIds = /* @__PURE__ */ new Set();
|
|
55653
55712
|
// Typing indicator — 给消息加"Typing"表情(小黄人),处理完删除
|
|
55654
55713
|
_typingReactions = /* @__PURE__ */ new Map();
|
|
55655
55714
|
_typingTasks = /* @__PURE__ */ new Map();
|
|
@@ -55729,11 +55788,11 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
55729
55788
|
},
|
|
55730
55789
|
data: {
|
|
55731
55790
|
receive_id: target,
|
|
55732
|
-
msg_type: "
|
|
55733
|
-
content: JSON.stringify(
|
|
55791
|
+
msg_type: "post",
|
|
55792
|
+
content: JSON.stringify(this._buildMdPost(chunk))
|
|
55734
55793
|
}
|
|
55735
55794
|
});
|
|
55736
|
-
this._assertApiOk(res, "\u6587\u672C\u6D88\u606F\u53D1\u9001");
|
|
55795
|
+
this._assertApiOk(res, "\u5BCC\u6587\u672C\u6D88\u606F\u53D1\u9001");
|
|
55737
55796
|
console.log(`[feishu:send] ok target=${target} message_id=${res?.data?.message_id ?? "?"} chars=${chunk.length}`);
|
|
55738
55797
|
} catch (err) {
|
|
55739
55798
|
console.error(`[feishu:send] FAILED target=${target}: ${err.message} (msg=${message.slice(0, 100)})`);
|
|
@@ -55827,7 +55886,7 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
55827
55886
|
assertExternalEffectCurrent();
|
|
55828
55887
|
this._assertApiOk(await this.client.im.message.create({
|
|
55829
55888
|
params: { receive_id_type: receiveIdType },
|
|
55830
|
-
data: { receive_id: safeTarget, msg_type: "
|
|
55889
|
+
data: { receive_id: safeTarget, msg_type: "post", content: JSON.stringify(this._buildMdPost(chunk)) }
|
|
55831
55890
|
}), "\u9644\u4EF6\u6587\u5B57\u53D1\u9001");
|
|
55832
55891
|
}
|
|
55833
55892
|
}
|
|
@@ -56155,6 +56214,36 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56155
56214
|
onMessage(handler2) {
|
|
56156
56215
|
this.messageHandler = handler2;
|
|
56157
56216
|
}
|
|
56217
|
+
/** 拉取被引消息(引用回复)原文 —— 只取文本,截断 QUOTE_MAX_CHARS。
|
|
56218
|
+
* 任何失败都只告警返回空串:引用是补充,正文送达优先于引用还原。 */
|
|
56219
|
+
async _fetchQuoteText(parentId) {
|
|
56220
|
+
try {
|
|
56221
|
+
const resp = await this.client.im.message.get({ path: { message_id: parentId } });
|
|
56222
|
+
if (resp?.code) throw new Error(`code=${resp.code} ${resp?.msg || ""}`);
|
|
56223
|
+
const item = resp?.data?.items?.[0];
|
|
56224
|
+
if (!item) return "";
|
|
56225
|
+
let text = "";
|
|
56226
|
+
try {
|
|
56227
|
+
const body = JSON.parse(String(item?.body?.content ?? "{}"));
|
|
56228
|
+
if (item?.msg_type === "text") text = String(body?.text ?? "");
|
|
56229
|
+
else if (item?.msg_type === "post") {
|
|
56230
|
+
const segs = [];
|
|
56231
|
+
for (const para of body?.content ?? []) for (const r of para ?? []) {
|
|
56232
|
+
if (r?.tag === "text") segs.push(String(r.text ?? ""));
|
|
56233
|
+
}
|
|
56234
|
+
text = segs.join("");
|
|
56235
|
+
} else text = String(body?.text ?? "");
|
|
56236
|
+
} catch {
|
|
56237
|
+
}
|
|
56238
|
+
text = text.trim();
|
|
56239
|
+
if (!text) return "";
|
|
56240
|
+
const clipped = text.length > QUOTE_MAX_CHARS ? text.slice(0, QUOTE_MAX_CHARS) + "\u2026" : text;
|
|
56241
|
+
return `[\u5F15\u7528\u4E0A\u6587] ${clipped}`;
|
|
56242
|
+
} catch (e) {
|
|
56243
|
+
console.warn(`[feishu] \u5F15\u7528\u539F\u6587\u62C9\u53D6\u5931\u8D25\uFF08${parentId.slice(-8)}\uFF09: ${e?.message ?? e}`);
|
|
56244
|
+
return "";
|
|
56245
|
+
}
|
|
56246
|
+
}
|
|
56158
56247
|
// === Streaming preview — 用飞书消息卡片实现"正在思考"效果 ===
|
|
56159
56248
|
async sendPreview(channelId, content, agentName) {
|
|
56160
56249
|
const agent = agentName || "Agent";
|
|
@@ -56172,7 +56261,15 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56172
56261
|
template: this.config.previewTemplate || "yellow"
|
|
56173
56262
|
},
|
|
56174
56263
|
elements: [
|
|
56175
|
-
|
|
56264
|
+
// 1013:预览卡也用 lark_md —— 生成过程中粗体/斜体/删除线/链接就能渲染了。
|
|
56265
|
+
// 表格、列表、代码块、行内代码是 lark_md 的能力边界(卡片 1.0 的 div 文本就只有
|
|
56266
|
+
// 这个子集),仍按字面显示 —— 与改动前持平,不是退步。
|
|
56267
|
+
// 要全都渲染得上卡片 JSON 2.0 的 markdown 组件,但那要改整张卡的结构、且 2.0 对
|
|
56268
|
+
// 不认识的字段**直接报错**(1.0 是静默忽略)——预览卡只活 3 秒,不值当。
|
|
56269
|
+
// 1013 真机对照已验:plain_text / lark_md 两卡同内容互比,**没有任何一行被吃掉**
|
|
56270
|
+
// (尖括号文本、下划线、单星号全部原样),只有行内代码不渲染。
|
|
56271
|
+
// 真出问题只需把这里换回 'plain_text'。
|
|
56272
|
+
{ tag: "div", text: { tag: "lark_md", content: this._convertMentions(text, "card") } }
|
|
56176
56273
|
]
|
|
56177
56274
|
})
|
|
56178
56275
|
}
|
|
@@ -56187,7 +56284,8 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56187
56284
|
const cardObj = {
|
|
56188
56285
|
config: { wide_screen_mode: true },
|
|
56189
56286
|
elements: [
|
|
56190
|
-
{ tag: "div", text: { tag: "
|
|
56287
|
+
{ tag: "div", text: { tag: "lark_md", content: this._convertMentions(text, "card") } }
|
|
56288
|
+
// 与 sendPreview 同口径,见其注释
|
|
56191
56289
|
]
|
|
56192
56290
|
};
|
|
56193
56291
|
if (!isFinal) {
|
|
@@ -56321,6 +56419,8 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56321
56419
|
if (isBot) {
|
|
56322
56420
|
if (this.botOpenId && senderOpenId === this.botOpenId) return;
|
|
56323
56421
|
}
|
|
56422
|
+
if (senderOpenId) this._rememberUserId(senderOpenId);
|
|
56423
|
+
for (const m of message.mentions ?? []) this._rememberUserId(m?.id?.open_id);
|
|
56324
56424
|
const chatType = message.chat_type;
|
|
56325
56425
|
const isGroup = chatType === "group";
|
|
56326
56426
|
const chatId = message.chat_id || "";
|
|
@@ -56334,6 +56434,11 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56334
56434
|
}
|
|
56335
56435
|
const { text: rawContent, imageKeys, fileKey, fileName, videoKey, videoName } = this.extractContent(message);
|
|
56336
56436
|
const cleanContent = this.stripMentions(rawContent, message.mentions);
|
|
56437
|
+
const parentId = String(message.parent_id || "");
|
|
56438
|
+
let quoteText = "";
|
|
56439
|
+
if (parentId && parentId !== messageId && !cleanContent.trimStart().startsWith("/")) {
|
|
56440
|
+
quoteText = await this._fetchQuoteText(parentId);
|
|
56441
|
+
}
|
|
56337
56442
|
const msgType = message.message_type;
|
|
56338
56443
|
let imageAttachments;
|
|
56339
56444
|
const allImageKeys = msgType === "image" ? (() => {
|
|
@@ -56402,8 +56507,12 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56402
56507
|
if (!cleanContent.trim() && !imageAttachments?.length && !fileAttachment && !videoAttachment && !attachNotes.length) return;
|
|
56403
56508
|
const senderName = await this._resolveSenderName(senderOpenId);
|
|
56404
56509
|
const inbound = {
|
|
56405
|
-
|
|
56406
|
-
|
|
56510
|
+
// 1012 翀哥实测:引用原文和新正文挤一行会混——引用独占一行,正文(含附件留痕)另起一行。
|
|
56511
|
+
content: [
|
|
56512
|
+
...quoteText ? [quoteText] : [],
|
|
56513
|
+
[cleanContent, ...attachNotes].filter(Boolean).join(" ")
|
|
56514
|
+
// 附件失败留痕:让 agent 知道'没看到'而不是以为自己看到了
|
|
56515
|
+
].filter(Boolean).join("\n"),
|
|
56407
56516
|
from: senderOpenId,
|
|
56408
56517
|
fromName: senderName,
|
|
56409
56518
|
channel_id: chatId,
|
|
@@ -56420,7 +56529,8 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56420
56529
|
],
|
|
56421
56530
|
metadata: {
|
|
56422
56531
|
chatId,
|
|
56423
|
-
chatType
|
|
56532
|
+
chatType,
|
|
56533
|
+
...parentId ? { parentId } : {}
|
|
56424
56534
|
}
|
|
56425
56535
|
};
|
|
56426
56536
|
const totalAttachments = (imageAttachments?.length || 0) + (fileAttachment ? 1 : 0) + (videoAttachment ? 1 : 0);
|
|
@@ -56517,20 +56627,92 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56517
56627
|
}
|
|
56518
56628
|
return result.trim();
|
|
56519
56629
|
}
|
|
56520
|
-
/**
|
|
56630
|
+
/**
|
|
56631
|
+
* 把 markdown 原文包成飞书富文本(post)消息的 md 元素。
|
|
56632
|
+
*
|
|
56633
|
+
* 纯信封 —— `text` 就是 agent 写的那段字,一字不改。飞书按 md 标签渲染
|
|
56634
|
+
* CommonMark 0.31 + GFM(表格/任务列表/删除线/自动链接),外观仍是普通气泡。
|
|
56635
|
+
*/
|
|
56636
|
+
_buildMdPost(text) {
|
|
56637
|
+
return { zh_cn: { content: [[{ tag: "md", text: this._convertMentions(text, "post") }]] } };
|
|
56638
|
+
}
|
|
56639
|
+
/** 记住一个 open_id(有界,见 _knownUserIds 注释) */
|
|
56640
|
+
_rememberUserId(id) {
|
|
56641
|
+
if (!id) return;
|
|
56642
|
+
this._knownUserIds.add(id);
|
|
56643
|
+
if (this._knownUserIds.size > 500) {
|
|
56644
|
+
this._knownUserIds = new Set([...this._knownUserIds].slice(-250));
|
|
56645
|
+
}
|
|
56646
|
+
}
|
|
56647
|
+
/**
|
|
56648
|
+
* 把 `<@ou_xxx>` 转成飞书原生 @(1013)。
|
|
56649
|
+
*
|
|
56650
|
+
* 背景:工具(`msg-send.ts` / `media-send.ts`)无条件拼 `<@${id}>` —— 那是 **Discord**
|
|
56651
|
+
* 的写法(Discord 那边出站必须 @ 才通知得到人)。飞书不认,原样吐一串 open_id,
|
|
56652
|
+
* 看着就不像人发的。
|
|
56653
|
+
*
|
|
56654
|
+
* ⚠️ **卡片路径只删不转**(1013 实测):飞书卡片的 `<at id=…>` **不收 open_id** ——
|
|
56655
|
+
* 传进去整张卡片创建**直接失败**(code 230099 / ErrCode 100290
|
|
56656
|
+
* "invalid user resource (at/person) in your card")。
|
|
56657
|
+
*
|
|
56658
|
+
* 两条路的失败代价**不对等**,所以不能一视同仁:
|
|
56659
|
+
* - post:坏 id 只是把 `<at user_id="…">` 原样吐出来 —— 丑,但消息到了
|
|
56660
|
+
* - 卡片:**卡片根本发不出去** —— 而卡片是流式预览的载体,挂了预览就整个没了
|
|
56661
|
+
* 而且白名单守卫**挡不住它**:失败的不是"id 不有效",是"这个语法压根不收 open_id"。
|
|
56662
|
+
* 预览卡只活 3 秒,不值当为它在正文里留一个能把消息打没的引信 —— 删干净即可。
|
|
56663
|
+
*
|
|
56664
|
+
* 名字**不用我们填**:飞书一律用账号真名。1013 实测:里面手写的名字会被忽略,
|
|
56665
|
+
* `<at user_id="X">翀哥</at>` 和 `<at user_id="X"></at>` 显示的都是真名。
|
|
56666
|
+
*
|
|
56667
|
+
* 守卫:post 也只转 `_knownUserIds` 里**本 app 见过**的 id —— 假 id 会把
|
|
56668
|
+
* `<at user_id="…">` 原样吐出来,比 `<@ou_…>` 更长更丑。没见过的连壳带 id 删干净。
|
|
56669
|
+
*
|
|
56670
|
+
* 代价:转成真 @ 会**发通知**(1013 实测:不转不响、转了响)。翀哥 1013 拍板
|
|
56671
|
+
* 群聊私聊都转 —— 蓝色 @ 和响铃在飞书里绑死,拆不开。
|
|
56672
|
+
*/
|
|
56673
|
+
_convertMentions(text, form) {
|
|
56674
|
+
return text.replace(/<@[!&]?([A-Za-z0-9_]+)>/g, (_m, id) => form === "post" && this._knownUserIds.has(id) ? `<at user_id="${id}"></at>` : "");
|
|
56675
|
+
}
|
|
56676
|
+
/** 该文本作为 post 发出的**请求体字节数** —— 按它对着 FEISHU_POST_BUDGET 切(见该常量注释) */
|
|
56677
|
+
_postBodyBytes(text) {
|
|
56678
|
+
return Buffer.byteLength(JSON.stringify(this._buildMdPost(text)), "utf8");
|
|
56679
|
+
}
|
|
56680
|
+
/**
|
|
56681
|
+
* 按飞书 post 的请求体上限分段。
|
|
56682
|
+
*
|
|
56683
|
+
* 1013 修:旧实现按 30000 **字符**切(那是 text 档 150KB 的口径)。post 只有 30KB,
|
|
56684
|
+
* 中文 3 字节/字 ⇒ 30000 字 ≈ 90KB,换 post 后整片会被飞书拒收。改成按**字节**算:
|
|
56685
|
+
* 二分出该片最多能装多少字符,再尽量切在换行处(保住段落形状,别把句子拦腰截断)。
|
|
56686
|
+
*/
|
|
56521
56687
|
splitMessage(text) {
|
|
56522
|
-
if (text
|
|
56688
|
+
if (this._postBodyBytes(text) <= FEISHU_POST_BUDGET) return [text];
|
|
56523
56689
|
const chunks = [];
|
|
56524
56690
|
let remaining = text;
|
|
56525
56691
|
while (remaining.length > 0) {
|
|
56526
|
-
|
|
56527
|
-
|
|
56528
|
-
|
|
56692
|
+
if (this._postBodyBytes(remaining) <= FEISHU_POST_BUDGET) {
|
|
56693
|
+
chunks.push(remaining);
|
|
56694
|
+
break;
|
|
56695
|
+
}
|
|
56696
|
+
const hard = this._maxPrefixByBytes(remaining);
|
|
56697
|
+
let cut = remaining.lastIndexOf("\n", hard);
|
|
56698
|
+
if (cut < 200) cut = remaining.lastIndexOf(" ", hard);
|
|
56699
|
+
if (cut < 200) cut = hard;
|
|
56700
|
+
if (cut <= 0) cut = 1;
|
|
56529
56701
|
chunks.push(remaining.slice(0, cut));
|
|
56530
56702
|
remaining = remaining.slice(cut).trimStart();
|
|
56531
56703
|
}
|
|
56532
56704
|
return chunks;
|
|
56533
56705
|
}
|
|
56706
|
+
/** 二分:找最大的 i,使 text.slice(0, i) 的 post 请求体不超预算(字节数随字符数单调增) */
|
|
56707
|
+
_maxPrefixByBytes(text) {
|
|
56708
|
+
let lo = 1, hi = text.length;
|
|
56709
|
+
while (lo < hi) {
|
|
56710
|
+
const mid = Math.ceil((lo + hi) / 2);
|
|
56711
|
+
if (this._postBodyBytes(text.slice(0, mid)) <= FEISHU_POST_BUDGET) lo = mid;
|
|
56712
|
+
else hi = mid - 1;
|
|
56713
|
+
}
|
|
56714
|
+
return lo;
|
|
56715
|
+
}
|
|
56534
56716
|
};
|
|
56535
56717
|
|
|
56536
56718
|
// src/channels/wechat.ts
|