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/main.mjs
CHANGED
|
@@ -4261,7 +4261,7 @@ var init_read_budget = __esm({
|
|
|
4261
4261
|
check(depth = 0) {
|
|
4262
4262
|
if (this.failure) throw this.failure;
|
|
4263
4263
|
const elapsed = this.elapsedMs + (this.started === void 0 ? 0 : this.now() - this.started);
|
|
4264
|
-
const reason = depth > (this.limits.maxDepth ?? 256) ? "depth" : ++this.steps > (this.limits.maxSteps ??
|
|
4264
|
+
const reason = depth > (this.limits.maxDepth ?? 256) ? "depth" : ++this.steps > (this.limits.maxSteps ?? 25e4) ? "work" : elapsed >= (this.limits.maxMs ?? 4e3) ? "time" : void 0;
|
|
4265
4265
|
if (reason) {
|
|
4266
4266
|
this.failure = new MemoryReadBudgetExceededError(reason);
|
|
4267
4267
|
throw this.failure;
|
|
@@ -7754,6 +7754,11 @@ CREATE TABLE memory_state_outbox (
|
|
|
7754
7754
|
payload_json TEXT NOT NULL, created_at TEXT NOT NULL,
|
|
7755
7755
|
PRIMARY KEY(scope_id,event_seq), FOREIGN KEY(scope_id) REFERENCES memory_scopes(scope_id)
|
|
7756
7756
|
);
|
|
7757
|
+
-- 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
|
|
7758
|
+
-- \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
|
|
7759
|
+
-- \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
|
|
7760
|
+
-- 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
|
|
7761
|
+
CREATE INDEX IF NOT EXISTS idx_outbox_scope_kind_version ON memory_state_outbox(scope_id,kind,aggregate_version);
|
|
7757
7762
|
`;
|
|
7758
7763
|
}
|
|
7759
7764
|
});
|
|
@@ -11384,6 +11389,14 @@ function migrateMem7(db2, initializeLegacySchema) {
|
|
|
11384
11389
|
version: 29,
|
|
11385
11390
|
name: "idempotent-source-backed-chat-people-updates",
|
|
11386
11391
|
apply: (database) => database.exec(SOCIAL_WRITE_SCHEMA)
|
|
11392
|
+
}, {
|
|
11393
|
+
version: 30,
|
|
11394
|
+
name: "indexed-outbox-aggregate-revisions",
|
|
11395
|
+
// 存量库补索引:聚合版本查询此前要扫该 scope 下全部 outbox 行算 MAX,
|
|
11396
|
+
// 实测妹妹 182MB 库 buildMemoryContext 2617ms → 719ms(读预算默认 1500ms 本来必破)。
|
|
11397
|
+
apply: (database) => database.exec(
|
|
11398
|
+
"CREATE INDEX IF NOT EXISTS idx_outbox_scope_kind_version ON memory_state_outbox(scope_id,kind,aggregate_version)"
|
|
11399
|
+
)
|
|
11387
11400
|
}]);
|
|
11388
11401
|
} finally {
|
|
11389
11402
|
if (foreignKeys) db2.exec("PRAGMA foreign_keys=ON");
|
|
@@ -11409,7 +11422,7 @@ var init_migrations = __esm({
|
|
|
11409
11422
|
init_relationship_cognition();
|
|
11410
11423
|
init_relationship_notices();
|
|
11411
11424
|
init_relationship_history();
|
|
11412
|
-
MEM7_SCHEMA_VERSION =
|
|
11425
|
+
MEM7_SCHEMA_VERSION = 30;
|
|
11413
11426
|
}
|
|
11414
11427
|
});
|
|
11415
11428
|
|
|
@@ -15175,6 +15188,130 @@ var init_executor2 = __esm({
|
|
|
15175
15188
|
}
|
|
15176
15189
|
});
|
|
15177
15190
|
|
|
15191
|
+
// src/mem7/assistant-context.ts
|
|
15192
|
+
function emittedWithheldPlaceholder(text) {
|
|
15193
|
+
return typeof text === "string" && text.trim() === withheldSourceBody;
|
|
15194
|
+
}
|
|
15195
|
+
function withholdSource(message) {
|
|
15196
|
+
message.content = withheldSourceBody;
|
|
15197
|
+
delete message.memoryOriginalText;
|
|
15198
|
+
delete message.reasoning_content;
|
|
15199
|
+
delete message.thinking_signature;
|
|
15200
|
+
delete message.responses_reasoning;
|
|
15201
|
+
}
|
|
15202
|
+
function degradeUncheckedMemory(messages) {
|
|
15203
|
+
const safeMessages = [];
|
|
15204
|
+
for (const message of messages) {
|
|
15205
|
+
if (isAttachmentMessage(message)) {
|
|
15206
|
+
if (message.attachment.type !== "relevant_memories") safeMessages.push(message);
|
|
15207
|
+
continue;
|
|
15208
|
+
}
|
|
15209
|
+
safeMessages.push(message);
|
|
15210
|
+
}
|
|
15211
|
+
return safeMessages;
|
|
15212
|
+
}
|
|
15213
|
+
function memoryContextRefs(store, scope, value) {
|
|
15214
|
+
if (!value || typeof value !== "object") return [];
|
|
15215
|
+
const input = value;
|
|
15216
|
+
if (input.scopeId !== void 0 && input.scopeId !== scopeId(scope)) return [];
|
|
15217
|
+
const refs = /* @__PURE__ */ new Map();
|
|
15218
|
+
const add = (candidate) => {
|
|
15219
|
+
checkMemoryReadBudget();
|
|
15220
|
+
if (!candidate || typeof candidate.sourceId !== "string") return;
|
|
15221
|
+
const source = store.sources.get(scope, candidate.sourceId);
|
|
15222
|
+
if (!source || !store.socialSources.retained(scope, source.sourceId)) return;
|
|
15223
|
+
if (candidate.scopeId !== void 0 && candidate.scopeId !== scopeId(scope)) return;
|
|
15224
|
+
if (candidate.sourceVersion !== void 0 && candidate.sourceVersion !== source.sourceVersion) return;
|
|
15225
|
+
const ref = { scopeId: scopeId(scope), sourceId: source.sourceId, sourceVersion: source.sourceVersion };
|
|
15226
|
+
if (candidate.relationshipId !== void 0 || candidate.semanticVersion !== void 0) {
|
|
15227
|
+
if (typeof candidate.relationshipId !== "string" || !Number.isSafeInteger(candidate.semanticVersion) || candidate.semanticVersion < 1) return;
|
|
15228
|
+
ref.relationshipId = candidate.relationshipId;
|
|
15229
|
+
ref.semanticVersion = candidate.semanticVersion;
|
|
15230
|
+
}
|
|
15231
|
+
if (candidate.purpose !== void 0) {
|
|
15232
|
+
if (!["evidence", "context", "historical"].includes(candidate.purpose)) return;
|
|
15233
|
+
ref.purpose = candidate.purpose;
|
|
15234
|
+
}
|
|
15235
|
+
if (store.socialSources.referenceRetained(scope, ref)) refs.set(canonicalJson(ref), ref);
|
|
15236
|
+
};
|
|
15237
|
+
for (const list2 of [input.contextRefs, input.relationships?.contextRefs, input.citations, input.pendingSources]) {
|
|
15238
|
+
if (Array.isArray(list2)) for (const ref of list2) add(ref);
|
|
15239
|
+
}
|
|
15240
|
+
const relationships = Array.isArray(input.relationships) ? input.relationships : input.relationships?.relationships;
|
|
15241
|
+
if (Array.isArray(relationships)) for (const relationship of relationships) {
|
|
15242
|
+
if (!relationship || typeof relationship.relationshipId !== "string" || !Number.isSafeInteger(relationship.semanticVersion)) continue;
|
|
15243
|
+
for (const list2 of [relationship.evidenceSources, relationship.basisSources]) {
|
|
15244
|
+
if (Array.isArray(list2)) for (const source of list2) add({
|
|
15245
|
+
sourceId: source.sourceId,
|
|
15246
|
+
sourceVersion: source.sourceVersion,
|
|
15247
|
+
relationshipId: relationship.relationshipId,
|
|
15248
|
+
semanticVersion: relationship.semanticVersion,
|
|
15249
|
+
purpose: relationship.current === false ? "historical" : "context"
|
|
15250
|
+
});
|
|
15251
|
+
}
|
|
15252
|
+
}
|
|
15253
|
+
for (const list2 of [input.historicalEvidence, input.episodes, input.entries]) {
|
|
15254
|
+
if (Array.isArray(list2)) {
|
|
15255
|
+
for (const item of list2) if (Array.isArray(item.sourceIds)) for (const sourceId of item.sourceIds) add({ sourceId });
|
|
15256
|
+
}
|
|
15257
|
+
}
|
|
15258
|
+
if (typeof input.sourceId === "string" && Number.isSafeInteger(input.sourceVersion)) add(input);
|
|
15259
|
+
return [...refs.values()];
|
|
15260
|
+
}
|
|
15261
|
+
function assistantContextRefs(store, scope, messages) {
|
|
15262
|
+
const refs = /* @__PURE__ */ new Map();
|
|
15263
|
+
const add = (ref) => refs.set(canonicalJson(ref), ref);
|
|
15264
|
+
for (const message of messages) {
|
|
15265
|
+
checkMemoryReadBudget();
|
|
15266
|
+
if (isAttachmentMessage(message)) {
|
|
15267
|
+
if (message.attachment.type !== "relevant_memories") continue;
|
|
15268
|
+
for (const memory of message.attachment.memories) {
|
|
15269
|
+
if (memory.mem7 && scopeId(memory.mem7.scope) === scopeId(scope)) {
|
|
15270
|
+
for (const sourceId of memory.mem7.sourceIds) {
|
|
15271
|
+
const source = store.sources.get(scope, sourceId);
|
|
15272
|
+
if (source && store.socialSources.retained(scope, sourceId)) add({ scopeId: scopeId(scope), sourceId, sourceVersion: source.sourceVersion });
|
|
15273
|
+
}
|
|
15274
|
+
}
|
|
15275
|
+
if (memory.path.startsWith("mem7-context://")) {
|
|
15276
|
+
let value;
|
|
15277
|
+
try {
|
|
15278
|
+
value = JSON.parse(memory.content);
|
|
15279
|
+
} catch (error) {
|
|
15280
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
15281
|
+
continue;
|
|
15282
|
+
}
|
|
15283
|
+
for (const ref of memoryContextRefs(store, scope, value)) add(ref);
|
|
15284
|
+
}
|
|
15285
|
+
}
|
|
15286
|
+
} else if (message.content !== withheldSourceBody && message.memoryOriginKey && message.memoryScope && scopeId(message.memoryScope) === scopeId(scope)) {
|
|
15287
|
+
const source = store.sources.getByOrigin(scope, message.memoryOriginKey, message.memorySourceVersion ?? 1);
|
|
15288
|
+
if (source && source.role === message.role && store.socialSources.retained(scope, source.sourceId))
|
|
15289
|
+
add({ scopeId: scopeId(scope), sourceId: source.sourceId, sourceVersion: source.sourceVersion });
|
|
15290
|
+
}
|
|
15291
|
+
}
|
|
15292
|
+
return [...refs.values()];
|
|
15293
|
+
}
|
|
15294
|
+
function scrubUnavailableSourceMessages(store, scope, messages) {
|
|
15295
|
+
for (const message of messages) {
|
|
15296
|
+
checkMemoryReadBudget();
|
|
15297
|
+
if (isAttachmentMessage(message) || !message.memoryOriginKey || !message.memoryScope || scopeId(message.memoryScope) !== scopeId(scope) || !store.sources.hasOrigin(scope, message.memoryOriginKey)) continue;
|
|
15298
|
+
const source = store.sources.getByOrigin(scope, message.memoryOriginKey, message.memorySourceVersion ?? 1);
|
|
15299
|
+
if (source && store.socialSources.retained(scope, source.sourceId)) continue;
|
|
15300
|
+
withholdSource(message);
|
|
15301
|
+
}
|
|
15302
|
+
}
|
|
15303
|
+
var withheldSourceBody;
|
|
15304
|
+
var init_assistant_context = __esm({
|
|
15305
|
+
"src/mem7/assistant-context.ts"() {
|
|
15306
|
+
"use strict";
|
|
15307
|
+
init_types();
|
|
15308
|
+
init_scope();
|
|
15309
|
+
init_temporal();
|
|
15310
|
+
init_read_budget();
|
|
15311
|
+
withheldSourceBody = "[\u5BF9\u5E94\u8BB0\u5FC6\u6765\u6E90\u672C\u8F6E\u672A\u901A\u8FC7\u6821\u9A8C\uFF0C\u539F\u6587\u6682\u4E0D\u63D0\u4F9B\u3002]";
|
|
15312
|
+
}
|
|
15313
|
+
});
|
|
15314
|
+
|
|
15178
15315
|
// src/memory/memdir/memoryAge.ts
|
|
15179
15316
|
function memoryAgeDays(mtimeMs) {
|
|
15180
15317
|
return Math.max(0, Math.floor((Date.now() - mtimeMs) / 864e5));
|
|
@@ -17596,6 +17733,7 @@ var init_query = __esm({
|
|
|
17596
17733
|
"use strict";
|
|
17597
17734
|
init_executor2();
|
|
17598
17735
|
init_host_turn();
|
|
17736
|
+
init_assistant_context();
|
|
17599
17737
|
init_external_effect();
|
|
17600
17738
|
init_attachments();
|
|
17601
17739
|
init_context_dump();
|
|
@@ -18046,6 +18184,10 @@ ${perTurnSystemDynamic}` : deferredHint || perTurnSystemDynamic;
|
|
|
18046
18184
|
return;
|
|
18047
18185
|
}
|
|
18048
18186
|
if (toolCalls.length === 0) {
|
|
18187
|
+
if (emittedWithheldPlaceholder(textContent)) {
|
|
18188
|
+
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`);
|
|
18189
|
+
textContent = "";
|
|
18190
|
+
}
|
|
18049
18191
|
if (!textContent.trim()) {
|
|
18050
18192
|
const thinkingAteBudget = turnThinking.length > 0;
|
|
18051
18193
|
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}`);
|
|
@@ -42909,130 +43051,8 @@ var NoteRateLimiter = class {
|
|
|
42909
43051
|
|
|
42910
43052
|
// src/tools/memory-bridge.ts
|
|
42911
43053
|
init_task_manager();
|
|
43054
|
+
init_assistant_context();
|
|
42912
43055
|
import { join as join28 } from "node:path";
|
|
42913
|
-
|
|
42914
|
-
// src/mem7/assistant-context.ts
|
|
42915
|
-
init_types();
|
|
42916
|
-
init_scope();
|
|
42917
|
-
init_temporal();
|
|
42918
|
-
init_read_budget();
|
|
42919
|
-
var withheldSourceBody = "[\u5BF9\u5E94\u8BB0\u5FC6\u6765\u6E90\u672C\u8F6E\u672A\u901A\u8FC7\u6821\u9A8C\uFF0C\u539F\u6587\u6682\u4E0D\u63D0\u4F9B\u3002]";
|
|
42920
|
-
function withholdSource(message) {
|
|
42921
|
-
message.content = withheldSourceBody;
|
|
42922
|
-
delete message.memoryOriginalText;
|
|
42923
|
-
delete message.reasoning_content;
|
|
42924
|
-
delete message.thinking_signature;
|
|
42925
|
-
delete message.responses_reasoning;
|
|
42926
|
-
}
|
|
42927
|
-
function withholdUncheckedMemory(messages, currentMessage) {
|
|
42928
|
-
const safeMessages = [];
|
|
42929
|
-
for (const message of messages) {
|
|
42930
|
-
if (isAttachmentMessage(message)) {
|
|
42931
|
-
if (message.attachment.type !== "relevant_memories") safeMessages.push(message);
|
|
42932
|
-
continue;
|
|
42933
|
-
}
|
|
42934
|
-
if (!message.memoryOriginKey || message === currentMessage) {
|
|
42935
|
-
safeMessages.push(message);
|
|
42936
|
-
continue;
|
|
42937
|
-
}
|
|
42938
|
-
const safe = { ...message };
|
|
42939
|
-
withholdSource(safe);
|
|
42940
|
-
safeMessages.push(safe);
|
|
42941
|
-
}
|
|
42942
|
-
return safeMessages;
|
|
42943
|
-
}
|
|
42944
|
-
function memoryContextRefs(store, scope, value) {
|
|
42945
|
-
if (!value || typeof value !== "object") return [];
|
|
42946
|
-
const input = value;
|
|
42947
|
-
if (input.scopeId !== void 0 && input.scopeId !== scopeId(scope)) return [];
|
|
42948
|
-
const refs = /* @__PURE__ */ new Map();
|
|
42949
|
-
const add = (candidate) => {
|
|
42950
|
-
checkMemoryReadBudget();
|
|
42951
|
-
if (!candidate || typeof candidate.sourceId !== "string") return;
|
|
42952
|
-
const source = store.sources.get(scope, candidate.sourceId);
|
|
42953
|
-
if (!source || !store.socialSources.retained(scope, source.sourceId)) return;
|
|
42954
|
-
if (candidate.scopeId !== void 0 && candidate.scopeId !== scopeId(scope)) return;
|
|
42955
|
-
if (candidate.sourceVersion !== void 0 && candidate.sourceVersion !== source.sourceVersion) return;
|
|
42956
|
-
const ref = { scopeId: scopeId(scope), sourceId: source.sourceId, sourceVersion: source.sourceVersion };
|
|
42957
|
-
if (candidate.relationshipId !== void 0 || candidate.semanticVersion !== void 0) {
|
|
42958
|
-
if (typeof candidate.relationshipId !== "string" || !Number.isSafeInteger(candidate.semanticVersion) || candidate.semanticVersion < 1) return;
|
|
42959
|
-
ref.relationshipId = candidate.relationshipId;
|
|
42960
|
-
ref.semanticVersion = candidate.semanticVersion;
|
|
42961
|
-
}
|
|
42962
|
-
if (candidate.purpose !== void 0) {
|
|
42963
|
-
if (!["evidence", "context", "historical"].includes(candidate.purpose)) return;
|
|
42964
|
-
ref.purpose = candidate.purpose;
|
|
42965
|
-
}
|
|
42966
|
-
if (store.socialSources.referenceRetained(scope, ref)) refs.set(canonicalJson(ref), ref);
|
|
42967
|
-
};
|
|
42968
|
-
for (const list2 of [input.contextRefs, input.relationships?.contextRefs, input.citations, input.pendingSources]) {
|
|
42969
|
-
if (Array.isArray(list2)) for (const ref of list2) add(ref);
|
|
42970
|
-
}
|
|
42971
|
-
const relationships = Array.isArray(input.relationships) ? input.relationships : input.relationships?.relationships;
|
|
42972
|
-
if (Array.isArray(relationships)) for (const relationship of relationships) {
|
|
42973
|
-
if (!relationship || typeof relationship.relationshipId !== "string" || !Number.isSafeInteger(relationship.semanticVersion)) continue;
|
|
42974
|
-
for (const list2 of [relationship.evidenceSources, relationship.basisSources]) {
|
|
42975
|
-
if (Array.isArray(list2)) for (const source of list2) add({
|
|
42976
|
-
sourceId: source.sourceId,
|
|
42977
|
-
sourceVersion: source.sourceVersion,
|
|
42978
|
-
relationshipId: relationship.relationshipId,
|
|
42979
|
-
semanticVersion: relationship.semanticVersion,
|
|
42980
|
-
purpose: relationship.current === false ? "historical" : "context"
|
|
42981
|
-
});
|
|
42982
|
-
}
|
|
42983
|
-
}
|
|
42984
|
-
for (const list2 of [input.historicalEvidence, input.episodes, input.entries]) {
|
|
42985
|
-
if (Array.isArray(list2)) {
|
|
42986
|
-
for (const item of list2) if (Array.isArray(item.sourceIds)) for (const sourceId of item.sourceIds) add({ sourceId });
|
|
42987
|
-
}
|
|
42988
|
-
}
|
|
42989
|
-
if (typeof input.sourceId === "string" && Number.isSafeInteger(input.sourceVersion)) add(input);
|
|
42990
|
-
return [...refs.values()];
|
|
42991
|
-
}
|
|
42992
|
-
function assistantContextRefs(store, scope, messages) {
|
|
42993
|
-
const refs = /* @__PURE__ */ new Map();
|
|
42994
|
-
const add = (ref) => refs.set(canonicalJson(ref), ref);
|
|
42995
|
-
for (const message of messages) {
|
|
42996
|
-
checkMemoryReadBudget();
|
|
42997
|
-
if (isAttachmentMessage(message)) {
|
|
42998
|
-
if (message.attachment.type !== "relevant_memories") continue;
|
|
42999
|
-
for (const memory of message.attachment.memories) {
|
|
43000
|
-
if (memory.mem7 && scopeId(memory.mem7.scope) === scopeId(scope)) {
|
|
43001
|
-
for (const sourceId of memory.mem7.sourceIds) {
|
|
43002
|
-
const source = store.sources.get(scope, sourceId);
|
|
43003
|
-
if (source && store.socialSources.retained(scope, sourceId)) add({ scopeId: scopeId(scope), sourceId, sourceVersion: source.sourceVersion });
|
|
43004
|
-
}
|
|
43005
|
-
}
|
|
43006
|
-
if (memory.path.startsWith("mem7-context://")) {
|
|
43007
|
-
let value;
|
|
43008
|
-
try {
|
|
43009
|
-
value = JSON.parse(memory.content);
|
|
43010
|
-
} catch (error) {
|
|
43011
|
-
if (!(error instanceof SyntaxError)) throw error;
|
|
43012
|
-
continue;
|
|
43013
|
-
}
|
|
43014
|
-
for (const ref of memoryContextRefs(store, scope, value)) add(ref);
|
|
43015
|
-
}
|
|
43016
|
-
}
|
|
43017
|
-
} else if (message.content !== withheldSourceBody && message.memoryOriginKey && message.memoryScope && scopeId(message.memoryScope) === scopeId(scope)) {
|
|
43018
|
-
const source = store.sources.getByOrigin(scope, message.memoryOriginKey, message.memorySourceVersion ?? 1);
|
|
43019
|
-
if (source && source.role === message.role && store.socialSources.retained(scope, source.sourceId))
|
|
43020
|
-
add({ scopeId: scopeId(scope), sourceId: source.sourceId, sourceVersion: source.sourceVersion });
|
|
43021
|
-
}
|
|
43022
|
-
}
|
|
43023
|
-
return [...refs.values()];
|
|
43024
|
-
}
|
|
43025
|
-
function scrubUnavailableSourceMessages(store, scope, messages) {
|
|
43026
|
-
for (const message of messages) {
|
|
43027
|
-
checkMemoryReadBudget();
|
|
43028
|
-
if (isAttachmentMessage(message) || !message.memoryOriginKey || !message.memoryScope || scopeId(message.memoryScope) !== scopeId(scope) || !store.sources.hasOrigin(scope, message.memoryOriginKey)) continue;
|
|
43029
|
-
const source = store.sources.getByOrigin(scope, message.memoryOriginKey, message.memorySourceVersion ?? 1);
|
|
43030
|
-
if (source && store.socialSources.retained(scope, source.sourceId)) continue;
|
|
43031
|
-
withholdSource(message);
|
|
43032
|
-
}
|
|
43033
|
-
}
|
|
43034
|
-
|
|
43035
|
-
// src/tools/memory-bridge.ts
|
|
43036
43056
|
var EverosSearchSchema = {
|
|
43037
43057
|
type: "object",
|
|
43038
43058
|
properties: {
|
|
@@ -44306,7 +44326,8 @@ var FIELDS = [
|
|
|
44306
44326
|
label: "\u6BCF\u8F6E\u8BB0\u5FC6\u8BFB\u53D6\u64CD\u4F5C\u4E0A\u9650",
|
|
44307
44327
|
effect: "hot",
|
|
44308
44328
|
internal: true,
|
|
44309
|
-
|
|
44329
|
+
// 上限必须 ≥ 代码默认值 250_000(1012 抬默认时漏了这里,导致托盘/CLI 拒收代码自己的默认值)
|
|
44330
|
+
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"
|
|
44310
44331
|
},
|
|
44311
44332
|
{
|
|
44312
44333
|
path: "mem7.relationships",
|
|
@@ -45597,6 +45618,7 @@ init_task_manager();
|
|
|
45597
45618
|
init_execution_guard();
|
|
45598
45619
|
init_external_effect();
|
|
45599
45620
|
init_intake();
|
|
45621
|
+
init_assistant_context();
|
|
45600
45622
|
init_read_budget();
|
|
45601
45623
|
init_scope();
|
|
45602
45624
|
init_host_turn();
|
|
@@ -46817,8 +46839,21 @@ async function handleQueryInner(text, sessionId, channelName, cb, deps, channelT
|
|
|
46817
46839
|
const memoryContextRefs2 = [];
|
|
46818
46840
|
const memoryStateDir = memoryIntake?.stateDir ?? String(liveConfig.get("stateDir") ?? deps.stateDir ?? "");
|
|
46819
46841
|
const memoryReadBudget = new MemoryReadBudget({
|
|
46820
|
-
|
|
46821
|
-
|
|
46842
|
+
// 1013:时间闸默认 1500ms 同样低于合法工作集——1012 为步数闸写下的原则(「闸只防无进展的
|
|
46843
|
+
// 死循环,不该拦合法工作」)当时没套用到时间闸上,它的 447ms 依据只测了 scrub+strip+refs,
|
|
46844
|
+
// 漏掉最贵的 buildMemoryContext。实测妹妹 182MB 库:三阶段累计 1196–1474ms,1500ms 下余量
|
|
46845
|
+
// 只剩 26–304ms,正常聊天即耗尽(1013 占位符事故,reason=time)。抬到 4000ms(小柯线上
|
|
46846
|
+
// 已跑这个值);闸仍兜 1009 式事件循环冻结,只是不再切合法工作。
|
|
46847
|
+
// 真正的成本在 a75d3804 的 outbox 覆盖索引:当前上下文装配 2617ms → ~1100ms。
|
|
46848
|
+
maxMs: liveConfig.get("mem7.turnReadBudgetMs") ?? 4e3,
|
|
46849
|
+
// 1012:步数闸默认 5 万被证低于合法工作集——忙 scope 单轮两遍冷走(:438 prompt 前 + :1042 query 循环内,
|
|
46850
|
+
// 中间 worker 落盘会失效快照)≈ 5 万步,正常聊天即触发全量扣原文(占位符刷屏事故)。真安全线是时间闸
|
|
46851
|
+
// 1500ms(防 1009 式事件循环冻结),步数闸只防无进展的死循环,抬到 25 万。实测妹妹 151MB 库全轮 447ms。
|
|
46852
|
+
// ⚠️ 1013 更正:上面「真安全线是时间闸 1500ms」已被证伪——那次 447ms 只测了
|
|
46853
|
+
// scrub+strip+refs(见 scripts/_probe-scrub-steps-1012.mts),漏掉最贵的 buildMemoryContext
|
|
46854
|
+
// (同库实测 2617ms),所以 1500ms 当时就已经不可达。步数闸的结论仍然成立(实测
|
|
46855
|
+
// 三阶段 102150 步,25 万合适)。
|
|
46856
|
+
maxSteps: liveConfig.get("mem7.turnReadMaxSteps") ?? 25e4
|
|
46822
46857
|
});
|
|
46823
46858
|
let memoryReadDegraded = false;
|
|
46824
46859
|
let currentPromptMessage;
|
|
@@ -46873,7 +46908,7 @@ async function handleQueryInner(text, sessionId, channelName, cb, deps, channelT
|
|
|
46873
46908
|
Promise.resolve().then(() => (init_historical_recall(), historical_recall_exports))
|
|
46874
46909
|
]);
|
|
46875
46910
|
revalidateRecall = (items) => {
|
|
46876
|
-
if (memoryReadDegraded) return
|
|
46911
|
+
if (memoryReadDegraded) return degradeUncheckedMemory(items);
|
|
46877
46912
|
const invalidPaths = /* @__PURE__ */ new Set();
|
|
46878
46913
|
const checked = items.map((item) => isAttachmentMessage(item) || item === currentPromptMessage ? item : { ...item });
|
|
46879
46914
|
try {
|
|
@@ -46893,7 +46928,7 @@ async function handleQueryInner(text, sessionId, channelName, cb, deps, channelT
|
|
|
46893
46928
|
} catch (error) {
|
|
46894
46929
|
if (!isMemoryReadUnavailable(error)) throw error;
|
|
46895
46930
|
degradeMemoryRead(error);
|
|
46896
|
-
return
|
|
46931
|
+
return degradeUncheckedMemory(items);
|
|
46897
46932
|
}
|
|
46898
46933
|
if (invalidPaths.size) {
|
|
46899
46934
|
stripRecalledMemories(history, (path73) => invalidPaths.has(normalizeMemoryPath(path73)));
|
|
@@ -47418,7 +47453,7 @@ ${evidence}
|
|
|
47418
47453
|
} catch (error) {
|
|
47419
47454
|
if (isMemoryReadUnavailable(error)) {
|
|
47420
47455
|
degradeMemoryRead(error);
|
|
47421
|
-
messages =
|
|
47456
|
+
messages = degradeUncheckedMemory(messages);
|
|
47422
47457
|
}
|
|
47423
47458
|
console.warn(`[handle-query] mem7 current context failed: ${error?.message ?? error}`);
|
|
47424
47459
|
}
|
|
@@ -47442,7 +47477,7 @@ ${evidence}
|
|
|
47442
47477
|
} catch (error) {
|
|
47443
47478
|
if (!isMemoryReadUnavailable(error)) throw error;
|
|
47444
47479
|
degradeMemoryRead(error);
|
|
47445
|
-
messages =
|
|
47480
|
+
messages = degradeUncheckedMemory(messages);
|
|
47446
47481
|
}
|
|
47447
47482
|
}
|
|
47448
47483
|
if (memoryHostSourceId && !memoryContextRefs2.some((ref) => ref.sourceId === memoryHostSourceId)) {
|
|
@@ -47591,7 +47626,12 @@ ${evidence}
|
|
|
47591
47626
|
const rawContent = chunk.content ?? "";
|
|
47592
47627
|
const inputTokens = chunk.inputTokens ?? 0;
|
|
47593
47628
|
const outputTokens = chunk.outputTokens ?? 0;
|
|
47594
|
-
const
|
|
47629
|
+
const placeholderLeak = emittedWithheldPlaceholder(rawContent);
|
|
47630
|
+
if (placeholderLeak) {
|
|
47631
|
+
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");
|
|
47632
|
+
}
|
|
47633
|
+
const usableContent = placeholderLeak ? "" : rawContent;
|
|
47634
|
+
const resultContent = usableContent.trim() || fullResponse.trim() || "(\u65E0\u56DE\u590D)";
|
|
47595
47635
|
if (!rawContent.trim()) {
|
|
47596
47636
|
console.log(`[handle-query] \u26A0\uFE0F EMPTY RESULT after ${toolCount} tools, falling back to ${fullResponse.length} chars accumulated`);
|
|
47597
47637
|
}
|
|
@@ -52070,6 +52110,14 @@ async function startHttpApi(ctx) {
|
|
|
52070
52110
|
httpServer.listen(CONTEXT_API_PORT, "127.0.0.1", () => {
|
|
52071
52111
|
console.log(`[api] Context usage API: http://127.0.0.1:${CONTEXT_API_PORT}/api/context`);
|
|
52072
52112
|
});
|
|
52113
|
+
httpServer.on("error", (err) => {
|
|
52114
|
+
if (err.code === "EADDRINUSE") {
|
|
52115
|
+
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`);
|
|
52116
|
+
console.error(`[api] \u5904\u7F6E\uFF1A\u505C\u6389\u5360\u7528\u8005\uFF0C\u6216\u6539 config \u7684 api.port \u540E\u91CD\u542F\u3002`);
|
|
52117
|
+
} else {
|
|
52118
|
+
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`);
|
|
52119
|
+
}
|
|
52120
|
+
});
|
|
52073
52121
|
registerOacBridge(httpServer, dispatcher, deps, config2);
|
|
52074
52122
|
return httpServer;
|
|
52075
52123
|
}
|
|
@@ -55924,7 +55972,9 @@ init_external_effect();
|
|
|
55924
55972
|
import * as lark from "@larksuiteoapi/node-sdk";
|
|
55925
55973
|
import { mkdir as mkdir5, writeFile as writeFile6 } from "node:fs/promises";
|
|
55926
55974
|
import path41 from "node:path";
|
|
55927
|
-
var
|
|
55975
|
+
var QUOTE_MAX_CHARS = 500;
|
|
55976
|
+
var FEISHU_POST_BYTES = 3e4;
|
|
55977
|
+
var FEISHU_POST_BUDGET = FEISHU_POST_BYTES - 1024;
|
|
55928
55978
|
var FeishuAdapter = class _FeishuAdapter {
|
|
55929
55979
|
name = "feishu";
|
|
55930
55980
|
memoryGuardedSends = true;
|
|
@@ -55935,6 +55985,15 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
55935
55985
|
botOpenId = "";
|
|
55936
55986
|
// 消息去重 — 飞书事件可能重复投递
|
|
55937
55987
|
recentMessageIds = /* @__PURE__ */ new Set();
|
|
55988
|
+
/**
|
|
55989
|
+
* 本 app **见过**的 open_id(入站发送者 + 被别人 @ 过的)—— 1013 转 @ 时的白名单。
|
|
55990
|
+
*
|
|
55991
|
+
* open_id 是**按 app 隔离**的:同一个人的 open_id 在不同 app 下完全不同。所以
|
|
55992
|
+
* agent 从别处(另一个 app 的记忆、幻觉)拿到一个 id 时,转成飞书 @ 会失败 ——
|
|
55993
|
+
* 而失败的形态是**把 `<at user_id="…">` 原样吐出来**,比不转还丑(1013 实测)。
|
|
55994
|
+
* 只认见过的,就是为了不产出那种更丑的形态。
|
|
55995
|
+
*/
|
|
55996
|
+
_knownUserIds = /* @__PURE__ */ new Set();
|
|
55938
55997
|
// Typing indicator — 给消息加"Typing"表情(小黄人),处理完删除
|
|
55939
55998
|
_typingReactions = /* @__PURE__ */ new Map();
|
|
55940
55999
|
_typingTasks = /* @__PURE__ */ new Map();
|
|
@@ -56014,11 +56073,11 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56014
56073
|
},
|
|
56015
56074
|
data: {
|
|
56016
56075
|
receive_id: target,
|
|
56017
|
-
msg_type: "
|
|
56018
|
-
content: JSON.stringify(
|
|
56076
|
+
msg_type: "post",
|
|
56077
|
+
content: JSON.stringify(this._buildMdPost(chunk))
|
|
56019
56078
|
}
|
|
56020
56079
|
});
|
|
56021
|
-
this._assertApiOk(res, "\u6587\u672C\u6D88\u606F\u53D1\u9001");
|
|
56080
|
+
this._assertApiOk(res, "\u5BCC\u6587\u672C\u6D88\u606F\u53D1\u9001");
|
|
56022
56081
|
console.log(`[feishu:send] ok target=${target} message_id=${res?.data?.message_id ?? "?"} chars=${chunk.length}`);
|
|
56023
56082
|
} catch (err) {
|
|
56024
56083
|
console.error(`[feishu:send] FAILED target=${target}: ${err.message} (msg=${message.slice(0, 100)})`);
|
|
@@ -56112,7 +56171,7 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56112
56171
|
assertExternalEffectCurrent();
|
|
56113
56172
|
this._assertApiOk(await this.client.im.message.create({
|
|
56114
56173
|
params: { receive_id_type: receiveIdType },
|
|
56115
|
-
data: { receive_id: safeTarget, msg_type: "
|
|
56174
|
+
data: { receive_id: safeTarget, msg_type: "post", content: JSON.stringify(this._buildMdPost(chunk)) }
|
|
56116
56175
|
}), "\u9644\u4EF6\u6587\u5B57\u53D1\u9001");
|
|
56117
56176
|
}
|
|
56118
56177
|
}
|
|
@@ -56440,6 +56499,36 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56440
56499
|
onMessage(handler2) {
|
|
56441
56500
|
this.messageHandler = handler2;
|
|
56442
56501
|
}
|
|
56502
|
+
/** 拉取被引消息(引用回复)原文 —— 只取文本,截断 QUOTE_MAX_CHARS。
|
|
56503
|
+
* 任何失败都只告警返回空串:引用是补充,正文送达优先于引用还原。 */
|
|
56504
|
+
async _fetchQuoteText(parentId) {
|
|
56505
|
+
try {
|
|
56506
|
+
const resp = await this.client.im.message.get({ path: { message_id: parentId } });
|
|
56507
|
+
if (resp?.code) throw new Error(`code=${resp.code} ${resp?.msg || ""}`);
|
|
56508
|
+
const item = resp?.data?.items?.[0];
|
|
56509
|
+
if (!item) return "";
|
|
56510
|
+
let text = "";
|
|
56511
|
+
try {
|
|
56512
|
+
const body = JSON.parse(String(item?.body?.content ?? "{}"));
|
|
56513
|
+
if (item?.msg_type === "text") text = String(body?.text ?? "");
|
|
56514
|
+
else if (item?.msg_type === "post") {
|
|
56515
|
+
const segs = [];
|
|
56516
|
+
for (const para of body?.content ?? []) for (const r of para ?? []) {
|
|
56517
|
+
if (r?.tag === "text") segs.push(String(r.text ?? ""));
|
|
56518
|
+
}
|
|
56519
|
+
text = segs.join("");
|
|
56520
|
+
} else text = String(body?.text ?? "");
|
|
56521
|
+
} catch {
|
|
56522
|
+
}
|
|
56523
|
+
text = text.trim();
|
|
56524
|
+
if (!text) return "";
|
|
56525
|
+
const clipped = text.length > QUOTE_MAX_CHARS ? text.slice(0, QUOTE_MAX_CHARS) + "\u2026" : text;
|
|
56526
|
+
return `[\u5F15\u7528\u4E0A\u6587] ${clipped}`;
|
|
56527
|
+
} catch (e) {
|
|
56528
|
+
console.warn(`[feishu] \u5F15\u7528\u539F\u6587\u62C9\u53D6\u5931\u8D25\uFF08${parentId.slice(-8)}\uFF09: ${e?.message ?? e}`);
|
|
56529
|
+
return "";
|
|
56530
|
+
}
|
|
56531
|
+
}
|
|
56443
56532
|
// === Streaming preview — 用飞书消息卡片实现"正在思考"效果 ===
|
|
56444
56533
|
async sendPreview(channelId, content, agentName) {
|
|
56445
56534
|
const agent = agentName || "Agent";
|
|
@@ -56457,7 +56546,15 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56457
56546
|
template: this.config.previewTemplate || "yellow"
|
|
56458
56547
|
},
|
|
56459
56548
|
elements: [
|
|
56460
|
-
|
|
56549
|
+
// 1013:预览卡也用 lark_md —— 生成过程中粗体/斜体/删除线/链接就能渲染了。
|
|
56550
|
+
// 表格、列表、代码块、行内代码是 lark_md 的能力边界(卡片 1.0 的 div 文本就只有
|
|
56551
|
+
// 这个子集),仍按字面显示 —— 与改动前持平,不是退步。
|
|
56552
|
+
// 要全都渲染得上卡片 JSON 2.0 的 markdown 组件,但那要改整张卡的结构、且 2.0 对
|
|
56553
|
+
// 不认识的字段**直接报错**(1.0 是静默忽略)——预览卡只活 3 秒,不值当。
|
|
56554
|
+
// 1013 真机对照已验:plain_text / lark_md 两卡同内容互比,**没有任何一行被吃掉**
|
|
56555
|
+
// (尖括号文本、下划线、单星号全部原样),只有行内代码不渲染。
|
|
56556
|
+
// 真出问题只需把这里换回 'plain_text'。
|
|
56557
|
+
{ tag: "div", text: { tag: "lark_md", content: this._convertMentions(text, "card") } }
|
|
56461
56558
|
]
|
|
56462
56559
|
})
|
|
56463
56560
|
}
|
|
@@ -56472,7 +56569,8 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56472
56569
|
const cardObj = {
|
|
56473
56570
|
config: { wide_screen_mode: true },
|
|
56474
56571
|
elements: [
|
|
56475
|
-
{ tag: "div", text: { tag: "
|
|
56572
|
+
{ tag: "div", text: { tag: "lark_md", content: this._convertMentions(text, "card") } }
|
|
56573
|
+
// 与 sendPreview 同口径,见其注释
|
|
56476
56574
|
]
|
|
56477
56575
|
};
|
|
56478
56576
|
if (!isFinal) {
|
|
@@ -56606,6 +56704,8 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56606
56704
|
if (isBot) {
|
|
56607
56705
|
if (this.botOpenId && senderOpenId === this.botOpenId) return;
|
|
56608
56706
|
}
|
|
56707
|
+
if (senderOpenId) this._rememberUserId(senderOpenId);
|
|
56708
|
+
for (const m of message.mentions ?? []) this._rememberUserId(m?.id?.open_id);
|
|
56609
56709
|
const chatType = message.chat_type;
|
|
56610
56710
|
const isGroup = chatType === "group";
|
|
56611
56711
|
const chatId = message.chat_id || "";
|
|
@@ -56619,6 +56719,11 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56619
56719
|
}
|
|
56620
56720
|
const { text: rawContent, imageKeys, fileKey, fileName, videoKey, videoName } = this.extractContent(message);
|
|
56621
56721
|
const cleanContent = this.stripMentions(rawContent, message.mentions);
|
|
56722
|
+
const parentId = String(message.parent_id || "");
|
|
56723
|
+
let quoteText = "";
|
|
56724
|
+
if (parentId && parentId !== messageId && !cleanContent.trimStart().startsWith("/")) {
|
|
56725
|
+
quoteText = await this._fetchQuoteText(parentId);
|
|
56726
|
+
}
|
|
56622
56727
|
const msgType = message.message_type;
|
|
56623
56728
|
let imageAttachments;
|
|
56624
56729
|
const allImageKeys = msgType === "image" ? (() => {
|
|
@@ -56687,8 +56792,12 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56687
56792
|
if (!cleanContent.trim() && !imageAttachments?.length && !fileAttachment && !videoAttachment && !attachNotes.length) return;
|
|
56688
56793
|
const senderName = await this._resolveSenderName(senderOpenId);
|
|
56689
56794
|
const inbound = {
|
|
56690
|
-
|
|
56691
|
-
|
|
56795
|
+
// 1012 翀哥实测:引用原文和新正文挤一行会混——引用独占一行,正文(含附件留痕)另起一行。
|
|
56796
|
+
content: [
|
|
56797
|
+
...quoteText ? [quoteText] : [],
|
|
56798
|
+
[cleanContent, ...attachNotes].filter(Boolean).join(" ")
|
|
56799
|
+
// 附件失败留痕:让 agent 知道'没看到'而不是以为自己看到了
|
|
56800
|
+
].filter(Boolean).join("\n"),
|
|
56692
56801
|
from: senderOpenId,
|
|
56693
56802
|
fromName: senderName,
|
|
56694
56803
|
channel_id: chatId,
|
|
@@ -56705,7 +56814,8 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56705
56814
|
],
|
|
56706
56815
|
metadata: {
|
|
56707
56816
|
chatId,
|
|
56708
|
-
chatType
|
|
56817
|
+
chatType,
|
|
56818
|
+
...parentId ? { parentId } : {}
|
|
56709
56819
|
}
|
|
56710
56820
|
};
|
|
56711
56821
|
const totalAttachments = (imageAttachments?.length || 0) + (fileAttachment ? 1 : 0) + (videoAttachment ? 1 : 0);
|
|
@@ -56802,20 +56912,92 @@ var FeishuAdapter = class _FeishuAdapter {
|
|
|
56802
56912
|
}
|
|
56803
56913
|
return result.trim();
|
|
56804
56914
|
}
|
|
56805
|
-
/**
|
|
56915
|
+
/**
|
|
56916
|
+
* 把 markdown 原文包成飞书富文本(post)消息的 md 元素。
|
|
56917
|
+
*
|
|
56918
|
+
* 纯信封 —— `text` 就是 agent 写的那段字,一字不改。飞书按 md 标签渲染
|
|
56919
|
+
* CommonMark 0.31 + GFM(表格/任务列表/删除线/自动链接),外观仍是普通气泡。
|
|
56920
|
+
*/
|
|
56921
|
+
_buildMdPost(text) {
|
|
56922
|
+
return { zh_cn: { content: [[{ tag: "md", text: this._convertMentions(text, "post") }]] } };
|
|
56923
|
+
}
|
|
56924
|
+
/** 记住一个 open_id(有界,见 _knownUserIds 注释) */
|
|
56925
|
+
_rememberUserId(id) {
|
|
56926
|
+
if (!id) return;
|
|
56927
|
+
this._knownUserIds.add(id);
|
|
56928
|
+
if (this._knownUserIds.size > 500) {
|
|
56929
|
+
this._knownUserIds = new Set([...this._knownUserIds].slice(-250));
|
|
56930
|
+
}
|
|
56931
|
+
}
|
|
56932
|
+
/**
|
|
56933
|
+
* 把 `<@ou_xxx>` 转成飞书原生 @(1013)。
|
|
56934
|
+
*
|
|
56935
|
+
* 背景:工具(`msg-send.ts` / `media-send.ts`)无条件拼 `<@${id}>` —— 那是 **Discord**
|
|
56936
|
+
* 的写法(Discord 那边出站必须 @ 才通知得到人)。飞书不认,原样吐一串 open_id,
|
|
56937
|
+
* 看着就不像人发的。
|
|
56938
|
+
*
|
|
56939
|
+
* ⚠️ **卡片路径只删不转**(1013 实测):飞书卡片的 `<at id=…>` **不收 open_id** ——
|
|
56940
|
+
* 传进去整张卡片创建**直接失败**(code 230099 / ErrCode 100290
|
|
56941
|
+
* "invalid user resource (at/person) in your card")。
|
|
56942
|
+
*
|
|
56943
|
+
* 两条路的失败代价**不对等**,所以不能一视同仁:
|
|
56944
|
+
* - post:坏 id 只是把 `<at user_id="…">` 原样吐出来 —— 丑,但消息到了
|
|
56945
|
+
* - 卡片:**卡片根本发不出去** —— 而卡片是流式预览的载体,挂了预览就整个没了
|
|
56946
|
+
* 而且白名单守卫**挡不住它**:失败的不是"id 不有效",是"这个语法压根不收 open_id"。
|
|
56947
|
+
* 预览卡只活 3 秒,不值当为它在正文里留一个能把消息打没的引信 —— 删干净即可。
|
|
56948
|
+
*
|
|
56949
|
+
* 名字**不用我们填**:飞书一律用账号真名。1013 实测:里面手写的名字会被忽略,
|
|
56950
|
+
* `<at user_id="X">翀哥</at>` 和 `<at user_id="X"></at>` 显示的都是真名。
|
|
56951
|
+
*
|
|
56952
|
+
* 守卫:post 也只转 `_knownUserIds` 里**本 app 见过**的 id —— 假 id 会把
|
|
56953
|
+
* `<at user_id="…">` 原样吐出来,比 `<@ou_…>` 更长更丑。没见过的连壳带 id 删干净。
|
|
56954
|
+
*
|
|
56955
|
+
* 代价:转成真 @ 会**发通知**(1013 实测:不转不响、转了响)。翀哥 1013 拍板
|
|
56956
|
+
* 群聊私聊都转 —— 蓝色 @ 和响铃在飞书里绑死,拆不开。
|
|
56957
|
+
*/
|
|
56958
|
+
_convertMentions(text, form) {
|
|
56959
|
+
return text.replace(/<@[!&]?([A-Za-z0-9_]+)>/g, (_m, id) => form === "post" && this._knownUserIds.has(id) ? `<at user_id="${id}"></at>` : "");
|
|
56960
|
+
}
|
|
56961
|
+
/** 该文本作为 post 发出的**请求体字节数** —— 按它对着 FEISHU_POST_BUDGET 切(见该常量注释) */
|
|
56962
|
+
_postBodyBytes(text) {
|
|
56963
|
+
return Buffer.byteLength(JSON.stringify(this._buildMdPost(text)), "utf8");
|
|
56964
|
+
}
|
|
56965
|
+
/**
|
|
56966
|
+
* 按飞书 post 的请求体上限分段。
|
|
56967
|
+
*
|
|
56968
|
+
* 1013 修:旧实现按 30000 **字符**切(那是 text 档 150KB 的口径)。post 只有 30KB,
|
|
56969
|
+
* 中文 3 字节/字 ⇒ 30000 字 ≈ 90KB,换 post 后整片会被飞书拒收。改成按**字节**算:
|
|
56970
|
+
* 二分出该片最多能装多少字符,再尽量切在换行处(保住段落形状,别把句子拦腰截断)。
|
|
56971
|
+
*/
|
|
56806
56972
|
splitMessage(text) {
|
|
56807
|
-
if (text
|
|
56973
|
+
if (this._postBodyBytes(text) <= FEISHU_POST_BUDGET) return [text];
|
|
56808
56974
|
const chunks = [];
|
|
56809
56975
|
let remaining = text;
|
|
56810
56976
|
while (remaining.length > 0) {
|
|
56811
|
-
|
|
56812
|
-
|
|
56813
|
-
|
|
56977
|
+
if (this._postBodyBytes(remaining) <= FEISHU_POST_BUDGET) {
|
|
56978
|
+
chunks.push(remaining);
|
|
56979
|
+
break;
|
|
56980
|
+
}
|
|
56981
|
+
const hard = this._maxPrefixByBytes(remaining);
|
|
56982
|
+
let cut = remaining.lastIndexOf("\n", hard);
|
|
56983
|
+
if (cut < 200) cut = remaining.lastIndexOf(" ", hard);
|
|
56984
|
+
if (cut < 200) cut = hard;
|
|
56985
|
+
if (cut <= 0) cut = 1;
|
|
56814
56986
|
chunks.push(remaining.slice(0, cut));
|
|
56815
56987
|
remaining = remaining.slice(cut).trimStart();
|
|
56816
56988
|
}
|
|
56817
56989
|
return chunks;
|
|
56818
56990
|
}
|
|
56991
|
+
/** 二分:找最大的 i,使 text.slice(0, i) 的 post 请求体不超预算(字节数随字符数单调增) */
|
|
56992
|
+
_maxPrefixByBytes(text) {
|
|
56993
|
+
let lo = 1, hi = text.length;
|
|
56994
|
+
while (lo < hi) {
|
|
56995
|
+
const mid = Math.ceil((lo + hi) / 2);
|
|
56996
|
+
if (this._postBodyBytes(text.slice(0, mid)) <= FEISHU_POST_BUDGET) lo = mid;
|
|
56997
|
+
else hi = mid - 1;
|
|
56998
|
+
}
|
|
56999
|
+
return lo;
|
|
57000
|
+
}
|
|
56819
57001
|
};
|
|
56820
57002
|
|
|
56821
57003
|
// src/channels/wechat.ts
|