dsh-memu-memory 1.4.2 → 1.4.3
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/AGENTS.md +108 -110
- package/CHANGELOG.md +169 -868
- package/LICENSE +21 -21
- package/README.md +368 -374
- package/README.zh.md +354 -360
- package/assets/icon.svg +7 -7
- package/cordis.patch.yml +8 -8
- package/docs/active-memory-ab.md +279 -385
- package/docs/binding-lifecycle.md +99 -99
- package/docs/client-conventions.md +75 -75
- package/docs/contributing.md +217 -217
- package/docs/emotion-model-spec.md +247 -281
- package/docs/env-and-commands.md +149 -152
- package/docs/host-adaptation.md +339 -360
- package/docs/host-api.md +131 -137
- package/docs/host-versions.md +117 -120
- package/docs/mcp-porting.md +146 -146
- package/docs/memory-semantics.md +271 -259
- package/docs/pending-work.md +44 -51
- package/docs/troubleshooting.md +909 -854
- package/lib/active-memory.js +160 -165
- package/lib/activity-log.js +207 -207
- package/lib/auto-link/apply.js +248 -249
- package/lib/auto-link/candidates.js +163 -163
- package/lib/auto-link/decide.js +300 -300
- package/lib/auto-link/normalize.js +99 -99
- package/lib/auto-link/policy.js +42 -42
- package/lib/auto-link/suppression.js +101 -101
- package/lib/auto-link/terminology.js +311 -311
- package/lib/backup.js +1254 -1254
- package/lib/binding-diagnostics.js +634 -634
- package/lib/budget.js +118 -118
- package/lib/client.js +9820 -9751
- package/lib/commitment.js +178 -138
- package/lib/consolidation-ledger.js +434 -438
- package/lib/consolidation-receipt.js +386 -387
- package/lib/consolidation-snapshot.js +420 -405
- package/lib/consolidation-worker.js +240 -241
- package/lib/context.js +33 -33
- package/lib/curator-evidence.js +131 -131
- package/lib/curator-policy.js +198 -202
- package/lib/curator.js +1813 -1811
- package/lib/cycle.js +524 -513
- package/lib/diagnostics.js +524 -524
- package/lib/embedding-catalog.js +1051 -1055
- package/lib/embedding-download.js +637 -607
- package/lib/embedding-runtime.js +946 -946
- package/lib/engine.js +1103 -1103
- package/lib/entity-repair.js +285 -285
- package/lib/environment-check.js +394 -394
- package/lib/fork-model.js +312 -312
- package/lib/freshness.js +128 -121
- package/lib/global-graph.js +389 -389
- package/lib/graph.js +1193 -1193
- package/lib/host-surface.js +30 -30
- package/lib/inbound.js +284 -281
- package/lib/index.js +506 -511
- package/lib/inject.js +2639 -2619
- package/lib/lexicon-scope.js +143 -143
- package/lib/lexicon.js +1183 -1192
- package/lib/lifecycle-audit.js +37 -0
- package/lib/local-embedding-entry.js +145 -145
- package/lib/local-embedding-manager.js +306 -306
- package/lib/local-embedding-protocol.js +210 -210
- package/lib/local-embedding-worker.js +682 -682
- package/lib/local-vector-service.js +506 -453
- package/lib/maintenance-ledger.js +138 -0
- package/lib/maintenance-review.js +103 -0
- package/lib/maintenance-runner.js +267 -0
- package/lib/managed-runtime.js +118 -118
- package/lib/media.js +607 -513
- package/lib/memory.js +5035 -4941
- package/lib/memu-config.js +213 -213
- package/lib/memu.js +565 -565
- package/lib/migrate.js +200 -200
- package/lib/mood.js +300 -300
- package/lib/paths.js +453 -457
- package/lib/port-surface.js +82 -82
- package/lib/preset-mount.js +79 -0
- package/lib/preset-scope.js +126 -126
- package/lib/private-links.js +567 -567
- package/lib/private.js +2198 -2209
- package/lib/prompts/global-curator.md +135 -135
- package/lib/prompts/image-caption.md +1 -1
- package/lib/prompts/private-consolidation.coding.md +39 -39
- package/lib/prompts/private-consolidation.md +32 -32
- package/lib/prompts/private-consolidation.mixed.md +37 -37
- package/lib/prompts/private-consolidation.rp.md +47 -47
- package/lib/prompts/private-policy.coding.md +35 -35
- package/lib/prompts/private-policy.md +36 -36
- package/lib/prompts/private-policy.mixed.md +29 -29
- package/lib/prompts/private-policy.rp.md +43 -43
- package/lib/prompts/system-guide.coding.md +20 -20
- package/lib/prompts/system-guide.md +25 -25
- package/lib/prompts/system-guide.mixed.md +22 -22
- package/lib/prompts/system-guide.rp.md +23 -23
- package/lib/prompts.js +331 -331
- package/lib/recall-cache.js +212 -212
- package/lib/remote-summary.js +123 -123
- package/lib/rpc.js +2531 -2532
- package/lib/salient-wake.js +438 -438
- package/lib/snippet.js +125 -125
- package/lib/state.js +1948 -1928
- package/lib/stats.js +236 -236
- package/lib/temporal.js +345 -348
- package/lib/text-retrieval.js +992 -1001
- package/lib/text-similarity.js +305 -305
- package/lib/tokenizer-v2.js +271 -271
- package/lib/tools.js +1098 -1091
- package/lib/turn-log.js +545 -545
- package/lib/vector-migration.js +928 -930
- package/lib/vector-profile.js +1353 -1359
- package/lib/visibility.js +59 -59
- package/lib/webui/graph-LICENSE.txt +1 -1
- package/lib/webui/graph-view.bundle.js +2 -2
- package/lib/webui/graph-view.js +1171 -1175
- package/lib/webui/mark.svg +7 -7
- package/lib/webui/standalone.css +1178 -1178
- package/lib/webui/standalone.js +238 -238
- package/lib/webui.js +136 -136
- package/lib/worker-pool.js +1015 -979
- package/package.json +153 -153
- package/requirements.lock +34 -34
- package/scripts/acceptance-local.mjs +946 -946
- package/scripts/active-memory-ab-live.mjs +653 -655
- package/scripts/active-memory-ab.mjs +1302 -1302
- package/scripts/audit-memory-lifecycle.mjs +55 -0
- package/scripts/bench-recall.mjs +142 -142
- package/scripts/build-graph.mjs +44 -45
- package/scripts/chain-matrix.mjs +541 -541
- package/scripts/check-all.mjs +68 -74
- package/scripts/check-doc-drift.mjs +547 -547
- package/scripts/check-graph-browser.mjs +261 -261
- package/scripts/check-i18n.mjs +81 -83
- package/scripts/check-idle-compositing.mjs +61 -61
- package/scripts/check-seed-compat.mjs +115 -118
- package/scripts/check-text-format.mjs +55 -0
- package/scripts/curator-pair-calibrate.mjs +259 -259
- package/scripts/diagnose-bindings.mjs +176 -176
- package/scripts/drive-session.mjs +863 -864
- package/scripts/engine-smoke.mjs +69 -69
- package/scripts/entity-dedupe.mjs +110 -110
- package/scripts/lexicon-calibrate.mjs +230 -230
- package/scripts/lexicon-dedupe.mjs +318 -318
- package/scripts/local-runtime-install.mjs +142 -142
- package/scripts/local-runtime-verify.mjs +56 -56
- package/scripts/local-vector-verify.mjs +93 -93
- package/scripts/make-readme-shots.mjs +59 -59
- package/scripts/migrate-visibility.mjs +94 -94
- package/scripts/mock-llm.mjs +938 -903
- package/scripts/preview.mjs +58 -58
- package/scripts/prune-sessions.mjs +31 -34
- package/scripts/prune-turnlogs.mjs +29 -30
- package/scripts/prune-vectors.mjs +24 -24
- package/scripts/rebuild-auto-links.mjs +252 -252
- package/scripts/recall-bench.mjs +820 -820
- package/scripts/repetition-ab-scenarios.mjs +93 -93
- package/scripts/repetition-detector-snapshot.mjs +314 -314
- package/scripts/repetition-eval-live.mjs +360 -360
- package/scripts/repetition-eval.mjs +702 -702
- package/scripts/replay.mjs +705 -575
- package/scripts/setup.mjs +46 -46
- package/scripts/simulate-injection.mjs +299 -301
- package/tests/absorb-review.test.mjs +354 -354
- package/tests/activation.test.mjs +89 -89
- package/tests/active-memory-ab.test.mjs +745 -745
- package/tests/active-memory.test.mjs +129 -129
- package/tests/activity-log.test.mjs +126 -126
- package/tests/activity-wiring.test.mjs +155 -155
- package/tests/appendix-b.test.mjs +217 -217
- package/tests/appendix-b2.test.mjs +166 -166
- package/tests/audit-batch3.test.mjs +247 -247
- package/tests/audit-regression.test.mjs +275 -275
- package/tests/audit-safety.test.mjs +227 -227
- package/tests/auto-link-candidates.test.mjs +245 -245
- package/tests/auto-link-decide.test.mjs +512 -512
- package/tests/auto-link-migration.test.mjs +133 -133
- package/tests/backend-compensation.test.mjs +272 -272
- package/tests/backup.test.mjs +793 -793
- package/tests/binding-diagnostics.test.mjs +720 -720
- package/tests/browser.test.mjs +476 -476
- package/tests/client-export.test.mjs +521 -521
- package/tests/client-render.test.mjs +2008 -1728
- package/tests/coalesce.test.mjs +113 -113
- package/tests/commitment.test.mjs +243 -144
- package/tests/config.test.mjs +159 -159
- package/tests/consolidation-credential.test.mjs +330 -330
- package/tests/consolidation-feed.test.mjs +122 -122
- package/tests/consolidation-fork.test.mjs +1586 -1539
- package/tests/consolidation-ledger.test.mjs +389 -389
- package/tests/consolidation-snapshot.test.mjs +567 -505
- package/tests/consolidation-value.test.mjs +89 -89
- package/tests/context-scope.test.mjs +40 -40
- package/tests/cross-library-recall.test.mjs +269 -269
- package/tests/cue.test.mjs +58 -58
- package/tests/curator-absorb.test.mjs +444 -444
- package/tests/curator-calibrate.test.mjs +173 -173
- package/tests/curator-clusters.test.mjs +306 -306
- package/tests/curator-commit.test.mjs +325 -325
- package/tests/curator-contract.test.mjs +191 -191
- package/tests/curator-evidence.test.mjs +102 -102
- package/tests/curator-link.test.mjs +317 -317
- package/tests/curator-pairing.test.mjs +339 -339
- package/tests/curator-probe.test.mjs +490 -490
- package/tests/curator-review-mode.test.mjs +315 -315
- package/tests/curator.test.mjs +152 -152
- package/tests/cycle.test.mjs +567 -568
- package/tests/diagnostics.test.mjs +278 -278
- package/tests/disclosure.test.mjs +249 -249
- package/tests/doc-drift.test.mjs +342 -342
- package/tests/drive-session.test.mjs +152 -152
- package/tests/dynamic-rrf.test.mjs +182 -182
- package/tests/dynamic-threshold.test.mjs +52 -52
- package/tests/embedding-catalog.test.mjs +374 -374
- package/tests/embedding-download.test.mjs +529 -486
- package/tests/embedding-runtime.test.mjs +449 -449
- package/tests/emotion-chain.test.mjs +84 -84
- package/tests/emotion-contract.test.mjs +445 -445
- package/tests/engine.test.mjs +108 -108
- package/tests/entity-merge.test.mjs +278 -278
- package/tests/environment-check.test.mjs +187 -187
- package/tests/fact-version.test.mjs +146 -146
- package/tests/fixtures/active-memory-ab/README.md +56 -56
- package/tests/fixtures/active-memory-ab/cases/01-clean-dense-turn.json +351 -351
- package/tests/fixtures/active-memory-ab/cases/02-clean-thin-turn.json +122 -122
- package/tests/fixtures/active-memory-ab/cases/03-clean-starved-small.json +125 -125
- package/tests/fixtures/active-memory-ab/cases/04-clean-large-thin.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/05-clean-half-step.json +166 -166
- package/tests/fixtures/active-memory-ab/cases/06-clean-duplicate-reread.json +197 -197
- package/tests/fixtures/active-memory-ab/cases/07-clean-recent-recall.json +125 -125
- package/tests/fixtures/active-memory-ab/cases/08-global-relevant.json +111 -111
- package/tests/fixtures/active-memory-ab/cases/09-global-irrelevant-conflict.json +158 -158
- package/tests/fixtures/active-memory-ab/cases/10-no-human-turn.json +97 -97
- package/tests/fixtures/active-memory-ab/cases/11-clean-scale-100.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/12-clean-scale-1000.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/13-clean-scale-5000.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/14-clean-scale-20000.json +137 -137
- package/tests/fixtures/active-memory-ab/lead-ai-reviewed-reference.json +191 -191
- package/tests/fixtures/active-memory-ab/manifest.json +199 -199
- package/tests/fixtures/curator-pairs/labeled.v1.jsonl +90 -90
- package/tests/fixtures/lexicon-stub-hooks.mjs +15 -15
- package/tests/fixtures/lexicon-stub.mjs +284 -284
- package/tests/fixtures/mock-llm/reply.txt +1 -1
- package/tests/fixtures/recall-bench/01-paraphrase.json +183 -183
- package/tests/fixtures/recall-bench/02-exact.json +152 -152
- package/tests/fixtures/recall-bench/03-negation.json +172 -172
- package/tests/fixtures/recall-bench/04-no-answer.json +161 -161
- package/tests/fixtures/recall-bench/05-conflict.json +193 -193
- package/tests/fixtures/recall-bench/06-boundary.json +272 -272
- package/tests/fixtures/recall-bench/07-secret.json +190 -190
- package/tests/fixtures/recall-bench/08-coding.json +163 -163
- package/tests/fixtures/recall-bench-gold-candidates/01-paraphrase.json +193 -193
- package/tests/fixtures/recall-bench-gold-candidates/02-exact.json +197 -197
- package/tests/fixtures/recall-bench-gold-candidates/03-negation.json +244 -244
- package/tests/fixtures/recall-bench-gold-candidates/04-no-answer.json +189 -189
- package/tests/fixtures/recall-bench-gold-candidates/05-conflict.json +245 -245
- package/tests/fixtures/recall-bench-gold-candidates/06-boundary.json +279 -279
- package/tests/fixtures/recall-bench-gold-candidates/07-secret.json +224 -224
- package/tests/fixtures/recall-bench-gold-candidates/08-coding.json +218 -218
- package/tests/fixtures/repetition-eval/01-zh-rp.json +92 -92
- package/tests/fixtures/repetition-eval/02-en.json +96 -96
- package/tests/fixtures/repetition-eval/03-code.json +90 -90
- package/tests/fixtures/repetition-eval/04-names.json +115 -115
- package/tests/fixtures/repetition-eval/05-user-quote.json +95 -95
- package/tests/fixtures/repetition-eval/06-agreement.json +85 -85
- package/tests/fixtures/tokenizer-cases.mjs +27 -27
- package/tests/fork-model.test.mjs +345 -345
- package/tests/freshness.test.mjs +140 -140
- package/tests/global-graph.test.mjs +106 -106
- package/tests/global-lifecycle.test.mjs +149 -149
- package/tests/graph-cascade.test.mjs +363 -363
- package/tests/graph-view.test.mjs +414 -414
- package/tests/graph.test.mjs +371 -371
- package/tests/help-page.test.mjs +416 -416
- package/tests/hook-order.test.mjs +72 -72
- package/tests/humanlike.test.mjs +232 -232
- package/tests/hybrid-recall.test.mjs +610 -610
- package/tests/inbound.test.mjs +441 -441
- package/tests/inject-conflict.test.mjs +173 -173
- package/tests/inject-guide.test.mjs +513 -513
- package/tests/inject.test.mjs +384 -384
- package/tests/injection-budget-settings.test.mjs +35 -35
- package/tests/injection-budget.test.mjs +165 -165
- package/tests/injection-chain.test.mjs +442 -442
- package/tests/injection-intent-budget.test.mjs +92 -92
- package/tests/injection-rollback.test.mjs +428 -428
- package/tests/issue2-chain.test.mjs +169 -169
- package/tests/lexical-bm25.test.mjs +171 -171
- package/tests/lexicon-calibrate.test.mjs +48 -48
- package/tests/lexicon-dedupe.test.mjs +171 -171
- package/tests/lexicon-frequency.test.mjs +195 -195
- package/tests/lexicon-inject.test.mjs +459 -459
- package/tests/lexicon-perf.test.mjs +464 -464
- package/tests/lexicon-scope.test.mjs +256 -256
- package/tests/lexicon-ui.test.mjs +667 -667
- package/tests/lexicon.test.mjs +922 -922
- package/tests/lifecycle-audit.test.mjs +56 -0
- package/tests/loader-register.mjs +3 -3
- package/tests/loader.mjs +17 -17
- package/tests/local-embedding-entry.test.mjs +95 -95
- package/tests/local-embedding-env.test.mjs +51 -51
- package/tests/local-embedding-manager.test.mjs +494 -494
- package/tests/local-embedding-protocol.test.mjs +121 -121
- package/tests/local-embedding-worker.test.mjs +440 -440
- package/tests/local-vector-service.test.mjs +41 -0
- package/tests/maintenance-status.test.mjs +77 -0
- package/tests/maintenance.test.mjs +438 -0
- package/tests/managed-memory.test.mjs +1234 -1234
- package/tests/managed-runtime.test.mjs +72 -72
- package/tests/media-injection.test.mjs +225 -0
- package/tests/media.test.mjs +135 -135
- package/tests/memory-chain.test.mjs +169 -169
- package/tests/memory-fields.test.mjs +134 -134
- package/tests/memu-resident-route.test.mjs +130 -130
- package/tests/memu-resident.test.mjs +33 -33
- package/tests/migration-payload.test.mjs +40 -42
- package/tests/mock-llm.test.mjs +263 -225
- package/tests/mood.test.mjs +109 -109
- package/tests/multiprocess.test.mjs +29 -29
- package/tests/negation-polarity.test.mjs +135 -135
- package/tests/new-session-gate.test.mjs +617 -617
- package/tests/open-loop-date.test.mjs +416 -416
- package/tests/ops-hardening.test.mjs +209 -209
- package/tests/package-shipping.test.mjs +88 -88
- package/tests/pending-injection.test.mjs +107 -107
- package/tests/port-surface.test.mjs +138 -138
- package/tests/preset-mount.test.mjs +124 -0
- package/tests/preset-scope.test.mjs +212 -212
- package/tests/private-links.test.mjs +493 -493
- package/tests/prompt-semantics.test.mjs +51 -51
- package/tests/prompts.test.mjs +315 -315
- package/tests/query-decompose.test.mjs +62 -62
- package/tests/react-snapshot.test.mjs +24 -24
- package/tests/recall-bench.test.mjs +317 -317
- package/tests/recall-cache.test.mjs +292 -292
- package/tests/recall-corpus-eligibility.test.mjs +51 -51
- package/tests/recall-dedup.test.mjs +446 -446
- package/tests/recall-intent.test.mjs +172 -172
- package/tests/regression-sweep.test.mjs +311 -311
- package/tests/reminder-fairness.test.mjs +55 -0
- package/tests/remote.test.mjs +140 -140
- package/tests/rename.test.mjs +30 -30
- package/tests/repetition-detector-snapshot.test.mjs +231 -231
- package/tests/repetition-eval-live.test.mjs +177 -177
- package/tests/repetition-eval.test.mjs +373 -373
- package/tests/salience-domain.test.mjs +173 -173
- package/tests/salient-date.test.mjs +142 -142
- package/tests/salient-wake.test.mjs +389 -389
- package/tests/settings-beat.test.mjs +222 -222
- package/tests/settings-coverage.test.mjs +97 -97
- package/tests/settings-plumbing.test.mjs +79 -79
- package/tests/short-term.test.mjs +182 -166
- package/tests/sidebar-beat.test.mjs +213 -213
- package/tests/sidebar-refresh.test.mjs +123 -123
- package/tests/sidebar.test.mjs +102 -74
- package/tests/simulate-injection.test.mjs +76 -76
- package/tests/slot-similarity.test.mjs +153 -153
- package/tests/snippet.test.mjs +79 -79
- package/tests/source-kind.test.mjs +52 -52
- package/tests/speaker-provenance.test.mjs +120 -120
- package/tests/stale-maintenance.test.mjs +206 -131
- package/tests/state-gc.test.mjs +34 -34
- package/tests/state.test.mjs +155 -155
- package/tests/stats.test.mjs +112 -112
- package/tests/storage.test.mjs +125 -125
- package/tests/stubs/child-process-spy.mjs +14 -14
- package/tests/stubs/dsh-llm-pi-ai.js +13 -13
- package/tests/stubs/dsh-llm.js +5 -5
- package/tests/stubs/dsh-schedule.js +140 -140
- package/tests/stubs/dsh-session-query.js +26 -26
- package/tests/stubs/dsh-session.js +7 -7
- package/tests/stubs/dsh-tools.js +101 -101
- package/tests/temporal-repair.test.mjs +78 -0
- package/tests/temporal.test.mjs +124 -124
- package/tests/text-format.test.mjs +24 -0
- package/tests/tokenizer-v2.test.mjs +142 -142
- package/tests/tool-schema.test.mjs +126 -126
- package/tests/tools.test.mjs +810 -810
- package/tests/turn-log.test.mjs +487 -487
- package/tests/ui-defaults.test.mjs +57 -57
- package/tests/update-fields.test.mjs +138 -138
- package/tests/vector-gc.test.mjs +37 -37
- package/tests/vector-migration.test.mjs +810 -810
- package/tests/vector-profile.test.mjs +449 -449
- package/tests/visibility-migration.test.mjs +152 -152
- package/tests/webui-auth.test.mjs +963 -963
- package/tests/webui.test.mjs +421 -421
- package/tests/windows-env.test.mjs +89 -89
- package/tests/worker-lifecycle.test.mjs +1521 -1429
package/lib/tools.js
CHANGED
|
@@ -1,1091 +1,1098 @@
|
|
|
1
|
-
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
2
|
-
import {
|
|
3
|
-
privateRemember, privateRecall, privateList, privateUpdate, privateArchive,
|
|
4
|
-
privateUnpin, globalRecall, globalList, bindingFor, peekBinding, sessionIdOf, domainOf, relativeTime,
|
|
5
|
-
isDatedSalientDate, readMemoryByRef,
|
|
6
|
-
} from './memory.js'
|
|
7
|
-
import { engineStatus, ENGINE_VERSION } from './engine.js'
|
|
8
|
-
import { getMissingVectorLibraries } from './local-vector-service.js'
|
|
9
|
-
import { centeredSnippetWindow } from './snippet.js'
|
|
10
|
-
import { redactSecrets } from './paths.js'
|
|
11
|
-
import { linkEntries, linkNamedEntity, updateRelationship } from './graph.js'
|
|
12
|
-
import { autoLinkPrivateMemories } from './private-links.js'
|
|
13
|
-
import { getSettings, listLibraries, getLibrary, loadPrivateIndex, loadGlobalIndex, storageHealth, syncActivity } from './state.js'
|
|
14
|
-
import { lexiconSearch, lexiconSources } from './lexicon-scope.js'
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
'
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (!exact &&
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
if (!Number.
|
|
65
|
-
if (
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
let
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
`ref
|
|
104
|
-
`
|
|
105
|
-
`
|
|
106
|
-
`
|
|
107
|
-
`
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
//
|
|
131
|
-
let lexiconModulePromise = null
|
|
132
|
-
function lexiconApi() {
|
|
133
|
-
if (!lexiconModulePromise) {
|
|
134
|
-
lexiconModulePromise = import('./lexicon.js').catch((error) => {
|
|
135
|
-
lexiconModulePromise = null
|
|
136
|
-
throw error
|
|
137
|
-
})
|
|
138
|
-
}
|
|
139
|
-
return lexiconModulePromise
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
//
|
|
143
|
-
//
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
return
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
if (
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
fromEntity
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
ids:
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
},
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
type: 'object',
|
|
473
|
-
properties: {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
if
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
},
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
})
|
|
756
|
-
|
|
757
|
-
},
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
'
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
'
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
'
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
//
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
const
|
|
796
|
-
|
|
797
|
-
//
|
|
798
|
-
|
|
799
|
-
const
|
|
800
|
-
const
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
const
|
|
804
|
-
|
|
805
|
-
const
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
`
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
},
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
'
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
//
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
//
|
|
865
|
-
|
|
866
|
-
const
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
'
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
}
|
|
904
|
-
const
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
},
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
'
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
const
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
},
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
'
|
|
934
|
-
|
|
935
|
-
{
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
//
|
|
939
|
-
|
|
940
|
-
const
|
|
941
|
-
const
|
|
942
|
-
const
|
|
943
|
-
const
|
|
944
|
-
const
|
|
945
|
-
const
|
|
946
|
-
const
|
|
947
|
-
const
|
|
948
|
-
const
|
|
949
|
-
const
|
|
950
|
-
const
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
//
|
|
962
|
-
//
|
|
963
|
-
//
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
const
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
`
|
|
991
|
-
`
|
|
992
|
-
`
|
|
993
|
-
`
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
*
|
|
1077
|
-
*
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
}
|
|
1
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
2
|
+
import {
|
|
3
|
+
privateRemember, privateRecall, privateList, privateUpdate, privateArchive,
|
|
4
|
+
privateUnpin, globalRecall, globalList, bindingFor, peekBinding, sessionIdOf, domainOf, relativeTime,
|
|
5
|
+
isDatedSalientDate, readMemoryByRef,
|
|
6
|
+
} from './memory.js'
|
|
7
|
+
import { engineStatus, ENGINE_VERSION } from './engine.js'
|
|
8
|
+
import { getMissingVectorLibraries } from './local-vector-service.js'
|
|
9
|
+
import { centeredSnippetWindow } from './snippet.js'
|
|
10
|
+
import { redactSecrets } from './paths.js'
|
|
11
|
+
import { linkEntries, linkNamedEntity, updateRelationship } from './graph.js'
|
|
12
|
+
import { autoLinkPrivateMemories } from './private-links.js'
|
|
13
|
+
import { getSettings, listLibraries, getLibrary, loadPrivateIndex, loadGlobalIndex, storageHealth, syncActivity } from './state.js'
|
|
14
|
+
import { lexiconSearch, lexiconSources } from './lexicon-scope.js'
|
|
15
|
+
import { maintenanceStatus } from './maintenance-ledger.js'
|
|
16
|
+
|
|
17
|
+
function text(value) {
|
|
18
|
+
return String(value ?? '')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function truncate(value, max = 1600) {
|
|
22
|
+
const s = text(value)
|
|
23
|
+
return s.length > max ? `${s.slice(0, max)}…` : s
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function errorText(error) {
|
|
27
|
+
const message = error?.message || String(error)
|
|
28
|
+
if (error?.code === 'MEMU_ENGINE_NOT_READY') {
|
|
29
|
+
return [
|
|
30
|
+
'MemU 引擎还没就绪(安装中或未安装)。',
|
|
31
|
+
'请到 DSH 设置 → MemU 查看进度,或运行 `npx dsh-memu-memory-setup`。',
|
|
32
|
+
].join('\n')
|
|
33
|
+
}
|
|
34
|
+
if (error?.code === 'MEMU_COMMAND_FAILED') {
|
|
35
|
+
return `MemU 命令失败:${message}`
|
|
36
|
+
}
|
|
37
|
+
const code = typeof error?.code === 'string' && /^[A-Z][A-Z0-9_]{0,79}$/.test(error.code) ? ` [${error.code}]` : ''
|
|
38
|
+
return `dsh-memu-memory error${code}: ${redactSecrets(message)}`
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function recallError(code, message) {
|
|
42
|
+
throw Object.assign(new Error(message), { code })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// The host's value-schema DSL has no minimum/maximum or cross-field rules.
|
|
46
|
+
// Validate before any memory access; natural searches cannot target a library.
|
|
47
|
+
function parseRecallRequest(args, scope) {
|
|
48
|
+
if (typeof args.query !== 'string' || !args.query.trim()) recallError('INVALID_RECALL_ARGUMENT', 'query is required')
|
|
49
|
+
const exact = !/\s/.test(args.query) && /^g?mem[-_][a-z0-9_-]+(?:\.md)?$/i.test(args.query)
|
|
50
|
+
const view = args.view === undefined ? 'auto' : args.view
|
|
51
|
+
if (!['auto', 'snippet', 'full'].includes(view)) recallError('INVALID_RECALL_ARGUMENT', 'view must be auto, snippet or full')
|
|
52
|
+
if (args.topK !== undefined && (!Number.isInteger(args.topK) || args.topK < 1 || args.topK > 20)) {
|
|
53
|
+
recallError('INVALID_RECALL_ARGUMENT', 'topK must be an integer in 1..20')
|
|
54
|
+
}
|
|
55
|
+
if (!exact && view === 'full') recallError('EXACT_ID_REQUIRED', 'view=full requires an exact memory id (no whitespace)')
|
|
56
|
+
if (!exact && ['libraryId', 'offset', 'limitChars', 'revision'].some((key) => Object.hasOwn(args, key))) {
|
|
57
|
+
recallError('INVALID_RECALL_ARGUMENT', 'libraryId/offset/limitChars/revision require an exact memory id, not a natural-language query')
|
|
58
|
+
}
|
|
59
|
+
if (Object.hasOwn(args, 'libraryId') && (scope !== 'private' || typeof args.libraryId !== 'string' || !args.libraryId.trim())) {
|
|
60
|
+
recallError('INVALID_RECALL_ARGUMENT', 'libraryId is a nonempty string for private exact reads only')
|
|
61
|
+
}
|
|
62
|
+
const offset = args.offset === undefined ? 0 : args.offset
|
|
63
|
+
const limitChars = args.limitChars === undefined ? 2000 : args.limitChars
|
|
64
|
+
if (!Number.isSafeInteger(offset) || offset < 0) recallError('INVALID_RECALL_ARGUMENT', 'offset must be an integer >= 0')
|
|
65
|
+
if (!Number.isInteger(limitChars) || limitChars < 1 || limitChars > 8000) recallError('INVALID_RECALL_ARGUMENT', 'limitChars must be an integer in 1..8000')
|
|
66
|
+
if (args.revision !== undefined && (typeof args.revision !== 'string' || !args.revision)) recallError('INVALID_RECALL_ARGUMENT', 'revision must be a nonempty string')
|
|
67
|
+
if (offset > 0 && !args.revision) recallError('REVISION_REQUIRED', 'offset > 0 requires revision from the first page')
|
|
68
|
+
return { exact, query: args.query, view: view === 'auto' ? (exact ? 'full' : 'snippet') : view,
|
|
69
|
+
topK: args.topK, libraryId: args.libraryId ?? null, offset, limitChars, revision: args.revision ?? null }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function recallReadParameters() {
|
|
73
|
+
return {
|
|
74
|
+
view: { type: 'string', enum: ['auto', 'snippet', 'full'], default: 'auto', description: 'auto: exact id reads full, natural query returns a snippet. full requires an exact id; snippet stays short.' },
|
|
75
|
+
offset: { type: 'integer', description: 'Exact id only: 0-based offset in redacted Unicode code points, integer >= 0. Continuing requires revision.' },
|
|
76
|
+
limitChars: { type: 'integer', description: 'Exact id only: page size in redacted Unicode code points, 1..8000; default 2000.' },
|
|
77
|
+
revision: { type: 'string', description: 'Exact id only: redacted body SHA256 from the first page; required when offset > 0. Changed bodies return MEMORY_CHANGED.' },
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function refText(scope, libraryId, id) {
|
|
82
|
+
return `scope=${scope} libraryId=${JSON.stringify(libraryId ?? null)} id=${JSON.stringify(id)}`
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Only the read API's redacted page and whitelisted metadata are rendered. No
|
|
86
|
+
// entry/raw body fallback, second body read or conflict expansion is permitted.
|
|
87
|
+
function renderMemoryPage(page, request) {
|
|
88
|
+
const ref = page.ref
|
|
89
|
+
const provenance = page.provenance || {}
|
|
90
|
+
const restrictions = page.restrictions || {}
|
|
91
|
+
let body = page.body
|
|
92
|
+
let pagination = page.pagination
|
|
93
|
+
if (request.view === 'snippet') {
|
|
94
|
+
const snippet = centeredSnippetWindow(body, request.query)
|
|
95
|
+
const offset = pagination.offset + snippet.start
|
|
96
|
+
const end = pagination.offset + snippet.end
|
|
97
|
+
pagination = { offset, returnedChars: snippet.returnedChars, totalChars: pagination.totalChars,
|
|
98
|
+
truncated: offset > 0 || end < pagination.totalChars, nextOffset: end < pagination.totalChars ? end : null }
|
|
99
|
+
body = snippet.body
|
|
100
|
+
}
|
|
101
|
+
const lines = [
|
|
102
|
+
`${ref.scope === 'private' ? '私有' : '全局'}记忆 [${ref.id}] ${redactSecrets(page.title || '(untitled)')}`,
|
|
103
|
+
`library=${ref.libraryId ?? 'global'} · source=${provenance.source || 'inferred'} · source_kind=${provenance.sourceKind || provenance.source || 'inferred'} · speaker_id=${provenance.speakerId ?? '?'} · view=${request.view}`,
|
|
104
|
+
`ref: ${refText(ref.scope, ref.libraryId, ref.id)}`,
|
|
105
|
+
`visibility=${restrictions.visibility || 'shared'} · disclosure=${restrictions.disclosure || 'can_tell'} · status=${restrictions.status || 'active'} · layer=${restrictions.layer || 'long_term'}`,
|
|
106
|
+
`domain=${page.domain || 'unknown'} · unbound=${page.unbound === true}`,
|
|
107
|
+
`pagination: unit=codepoint · offset=${pagination.offset} · returned_chars=${pagination.returnedChars} · total_chars=${pagination.totalChars} · truncated=${pagination.truncated} · next_offset=${pagination.nextOffset ?? 'null'}`,
|
|
108
|
+
`revision=${page.revision}`,
|
|
109
|
+
]
|
|
110
|
+
const header = redactSecrets(lines.join('\n'))
|
|
111
|
+
const footer = []
|
|
112
|
+
if (pagination.nextOffset !== null) {
|
|
113
|
+
footer.push(`next: memu_${ref.scope}_recall query=${JSON.stringify(ref.id)} view="full"${ref.scope === 'private' ? ` libraryId=${JSON.stringify(ref.libraryId)}` : ''} offset=${pagination.nextOffset} limitChars=${request.limitChars} revision=${JSON.stringify(page.revision)}`)
|
|
114
|
+
}
|
|
115
|
+
for (const related of page.related || []) {
|
|
116
|
+
if (related.kind === 'conflict') footer.push(`conflict: [${related.id}] ${refText(ref.scope, related.libraryId, related.id)}· available=${related.available !== false}(尚未读取另一版,不能据此裁决)`)
|
|
117
|
+
}
|
|
118
|
+
return `${header}\nbody:\n${body}${footer.length ? '\n' + redactSecrets(footer.join('\n')) : ''}`
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function searchSnippet(item, query) {
|
|
122
|
+
// Redact before cutting: a key split at the snippet boundary must not leak.
|
|
123
|
+
return centeredSnippetWindow(redactSecrets(item.content || ''), query)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function searchRefLine(scope, item, snippet, homeLibraryId = null) {
|
|
127
|
+
return ` view=snippet · ref: ${refText(scope, scope === 'private' ? (item.libraryId || homeLibraryId) : null, item.name)} · unit=codepoint · offset=${snippet.start} · returned_chars=${snippet.returnedChars} · total_chars=${snippet.totalChars} · truncated=${snippet.truncated}`
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// 词典核心按需加载,避免模块加载失败影响其它工具注册;失败后允许重试。
|
|
131
|
+
let lexiconModulePromise = null
|
|
132
|
+
function lexiconApi() {
|
|
133
|
+
if (!lexiconModulePromise) {
|
|
134
|
+
lexiconModulePromise = import('./lexicon.js').catch((error) => {
|
|
135
|
+
lexiconModulePromise = null
|
|
136
|
+
throw error
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
return lexiconModulePromise
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Compile and register tools independently so one rejected schema does not block
|
|
143
|
+
// unrelated tools. memu_status reports failures through registrationReport.
|
|
144
|
+
let registrationReport = { attempted: 0, registered: 0, failures: [] }
|
|
145
|
+
|
|
146
|
+
function toolBase(name, description, parameters, execute, options = {}) {
|
|
147
|
+
registrationReport.attempted += 1
|
|
148
|
+
try {
|
|
149
|
+
return defineTool({
|
|
150
|
+
name,
|
|
151
|
+
description,
|
|
152
|
+
parameters,
|
|
153
|
+
output: {
|
|
154
|
+
schema: { type: 'string' },
|
|
155
|
+
render: (_args, value) => [{ type: 'text', text: String(value) }],
|
|
156
|
+
},
|
|
157
|
+
timeoutMs: options.timeoutMs ?? 60_000,
|
|
158
|
+
isConcurrencySafe: options.isConcurrencySafe ?? (() => false),
|
|
159
|
+
async execute(args, exec) {
|
|
160
|
+
try {
|
|
161
|
+
return await execute(args || {}, exec)
|
|
162
|
+
} catch (error) {
|
|
163
|
+
try { exec?.logger?.warn?.(`dsh-memu-memory ${name}: ${error?.stack || error}`) } catch {}
|
|
164
|
+
return errorText(error)
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
})
|
|
168
|
+
} catch (error) {
|
|
169
|
+
registrationReport.failures.push({ name, message: String(error?.message || error) })
|
|
170
|
+
return null
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Use distinct, consistent labels for one-off dates and recurring anniversaries.
|
|
175
|
+
function salientTag(entry) {
|
|
176
|
+
if (!entry || !entry.salientDate) return ''
|
|
177
|
+
return isDatedSalientDate(entry.salientDate) ? `date=${entry.salientDate}` : `anniversary=${entry.salientDate}`
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// 四轴名在展示层就地映射:这是给人读的一行摘要,不是本地化词条。
|
|
181
|
+
const EMOTION_LABELS = { joy: '喜', anger: '怒', sorrow: '哀', fear: '惧' }
|
|
182
|
+
const EMOTION_AXES_IN_ORDER = ['joy', 'anger', 'sorrow', 'fear']
|
|
183
|
+
|
|
184
|
+
// 摘要最多保留两项达到显示阈值的情绪份额,控制单行标签长度。
|
|
185
|
+
function emotionBrief(entry) {
|
|
186
|
+
const emotion = entry.emotion
|
|
187
|
+
if (!emotion || typeof emotion !== 'object') return ''
|
|
188
|
+
const shares = EMOTION_AXES_IN_ORDER
|
|
189
|
+
.map((axis) => ({ axis, share: Number(emotion[axis]) || 0 }))
|
|
190
|
+
.filter((item) => item.share >= 0.05)
|
|
191
|
+
.sort((a, b) => b.share - a.share)
|
|
192
|
+
.slice(0, 2)
|
|
193
|
+
if (!shares.length) return ''
|
|
194
|
+
const parts = shares.map((item) => `${EMOTION_LABELS[item.axis]}${item.share.toFixed(2)}`)
|
|
195
|
+
const dominance = Number(entry.dominance)
|
|
196
|
+
if (Number.isFinite(dominance)) parts.push(`dom${dominance >= 0 ? '+' : ''}${dominance.toFixed(2)}`)
|
|
197
|
+
const arousal = Number(entry.arousal)
|
|
198
|
+
if (Number.isFinite(arousal)) parts.push(`a${arousal.toFixed(2)}`)
|
|
199
|
+
return parts.join(' · ')
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function formatPrivateItem(item, index, options = {}) {
|
|
203
|
+
const entry = item.entry || {}
|
|
204
|
+
const layer = entry.layer || 'long_term'
|
|
205
|
+
const status = entry.status || 'active'
|
|
206
|
+
const source = entry.source || 'inferred'
|
|
207
|
+
// 省略默认 layer/status;显式读取始终标明来源,避免把推断当成引文。
|
|
208
|
+
const flags = [
|
|
209
|
+
layer !== 'long_term' ? `layer=${layer}` : '',
|
|
210
|
+
status !== 'active' ? `status=${status}` : '',
|
|
211
|
+
entry.marginalized ? 'marginalized' : '',
|
|
212
|
+
entry.openLoop ? 'open_loop' : '',
|
|
213
|
+
].filter(Boolean).join(' ')
|
|
214
|
+
const lines = [`${index}. [${item.name}] ${entry.title || item.description || '(untitled)'}`]
|
|
215
|
+
if (flags) lines.push(` ${flags}`)
|
|
216
|
+
lines.push(` source=${source}`)
|
|
217
|
+
if (entry.source_kind) lines.push(` source_kind=${entry.source_kind}`)
|
|
218
|
+
if (entry.speaker_id) lines.push(` speaker_id=${entry.speaker_id}`)
|
|
219
|
+
// Use the same relative-time format as the injected block.
|
|
220
|
+
const when = relativeTime(entry.updatedAt || entry.createdAt)
|
|
221
|
+
if (when) lines.push(` updated=${when}`)
|
|
222
|
+
if (entry.disclosure && entry.disclosure !== 'can_tell') lines.push(` disclosure=${entry.disclosure}`)
|
|
223
|
+
if (entry.owner && entry.owner !== 'me') lines.push(` owner=${entry.owner}`)
|
|
224
|
+
// Cross-library recall is read-only: the id is real, but updates/archives
|
|
225
|
+
// still resolve against the bound library. Say where it lives so the model
|
|
226
|
+
// does not treat "unknown memory" as "it was deleted".
|
|
227
|
+
if (options.homeLibraryId && item.libraryId && item.libraryId !== options.homeLibraryId) {
|
|
228
|
+
lines.push(` library=${item.libraryId}(跨库只读;写入仍落在当前库)`)
|
|
229
|
+
}
|
|
230
|
+
if (entry.visibility && entry.visibility !== 'shared') {
|
|
231
|
+
// 'secret' is a presentation rule, not a lock: it stays out of the prompt and out of
|
|
232
|
+
// automatic recall, but anyone in the same folder can still pull it up on purpose.
|
|
233
|
+
// Saying so is what keeps the model from treating it as a private vault.
|
|
234
|
+
const note = entry.visibility === 'secret'
|
|
235
|
+
? '(不进提示词、不会自动浮现;同文件夹里主动检索仍可读到,别在正文里复述)'
|
|
236
|
+
: ''
|
|
237
|
+
lines.push(` visibility=${entry.visibility}${note}`)
|
|
238
|
+
}
|
|
239
|
+
if (entry.peak === true) lines.push(' peak')
|
|
240
|
+
// Distinguish a one-off appointment from a recurring anniversary.
|
|
241
|
+
const tag = salientTag(entry)
|
|
242
|
+
if (tag) lines.push(` ${tag}`)
|
|
243
|
+
// 有构成就讲构成:同一个 valence 可能来自怒,也可能来自哀,一个数字分不出来。
|
|
244
|
+
const brief = emotionBrief(entry)
|
|
245
|
+
if (brief) lines.push(` emo=${brief}`)
|
|
246
|
+
else if (typeof entry.moodValence === 'number' && entry.moodValence !== 0) lines.push(` mood=${entry.moodValence.toFixed(2)}`)
|
|
247
|
+
const snippet = searchSnippet(item, options.query)
|
|
248
|
+
lines.push(searchRefLine('private', item, snippet, options.homeLibraryId))
|
|
249
|
+
if (snippet.body) lines.push(' ' + snippet.body.replace(/\n/g, '\n '))
|
|
250
|
+
for (const related of item.related || []) {
|
|
251
|
+
if (related.kind === 'conflict') lines.push(` conflict: ${refText('private', related.libraryId || item.libraryId || options.homeLibraryId, related.id)} · conflictComplete=${related.complete === true} · available=${related.available !== false}(关系指针,不包含另一版正文)`)
|
|
252
|
+
else lines.push(` related: [${related.id}] ${related.title} (${related.kind})`)
|
|
253
|
+
}
|
|
254
|
+
for (const image of entry.attachments || []) lines.push(` image: ${image.sha256} ${image.caption || '[图片]'}${image.missing ? ' [missing]' : ''} (${image.captionBy || 'none'})`)
|
|
255
|
+
return redactSecrets(lines.join('\n'))
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function formatGlobalItem(item, index, options = {}) {
|
|
259
|
+
const entry = item.entry || {}
|
|
260
|
+
const domain = entry.domain || 'unknown'
|
|
261
|
+
// unknown 域使用 guide 的默认语义,省略重复标注;confidence 保留。
|
|
262
|
+
const head = [
|
|
263
|
+
domain !== 'unknown' ? `domain=${domain}` : '',
|
|
264
|
+
entry.confidence !== undefined ? `confidence=${entry.confidence}` : '',
|
|
265
|
+
].filter(Boolean).join(' ')
|
|
266
|
+
const lines = [`${index}. [${item.name}] ${entry.title || item.description || '(untitled)'}`]
|
|
267
|
+
if (head) lines.push(` ${head}`)
|
|
268
|
+
const when = relativeTime(entry.updatedAt || entry.createdAt)
|
|
269
|
+
if (when) lines.push(` updated=${when}`)
|
|
270
|
+
if (Array.isArray(entry.appliesTo) && entry.appliesTo.length > 0) lines.push(` applies_to=${entry.appliesTo.join(',')}`)
|
|
271
|
+
lines.push(` source=${entry.source || 'inferred'} · source_kind=${entry.source_kind || entry.source || 'inferred'}`)
|
|
272
|
+
if (entry.speaker_id) lines.push(` speaker_id=${entry.speaker_id}`)
|
|
273
|
+
if (entry.visibility) lines.push(` visibility=${entry.visibility}`)
|
|
274
|
+
if (entry.disclosure) lines.push(` disclosure=${entry.disclosure}`)
|
|
275
|
+
const snippet = searchSnippet(item, options.query)
|
|
276
|
+
lines.push(searchRefLine('global', item, snippet))
|
|
277
|
+
if (snippet.body) lines.push(' ' + snippet.body.replace(/\n/g, '\n '))
|
|
278
|
+
return redactSecrets(lines.join('\n'))
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* 单条 remember 与批量 remember_batch 共用参数映射,避免字段漂移。
|
|
283
|
+
* 批量工具把重复率最高的公共字段放在 defaults 里,每条 item 只补差异字段。
|
|
284
|
+
*/
|
|
285
|
+
async function rememberFromArgs(exec, args = {}) {
|
|
286
|
+
return privateRemember({
|
|
287
|
+
exec,
|
|
288
|
+
content: args.content,
|
|
289
|
+
title: args.title,
|
|
290
|
+
layer: args.layer,
|
|
291
|
+
track: args.track,
|
|
292
|
+
attachments: args.attachments,
|
|
293
|
+
context: args.context,
|
|
294
|
+
pinReason: args.pin_reason,
|
|
295
|
+
pinSource: args.pin_source,
|
|
296
|
+
sideEventId: args.side_event_id,
|
|
297
|
+
tags: args.tags,
|
|
298
|
+
openLoop: args.open_loop,
|
|
299
|
+
commitment: args.commitment,
|
|
300
|
+
factKey: args.fact_key ?? null,
|
|
301
|
+
conflictsWith: args.conflicts_with ?? null,
|
|
302
|
+
supersedes: args.supersedes ?? null,
|
|
303
|
+
disclosure: args.disclosure,
|
|
304
|
+
owner: args.owner,
|
|
305
|
+
visibility: args.visibility,
|
|
306
|
+
source: args.source,
|
|
307
|
+
sourceKind: args.source_kind,
|
|
308
|
+
speakerId: args.speaker_id ?? null,
|
|
309
|
+
quoteSpan: args.quote_span ?? null,
|
|
310
|
+
storylineId: args.storyline_id,
|
|
311
|
+
branchId: args.branch_id,
|
|
312
|
+
knownBy: args.known_by,
|
|
313
|
+
confidence: args.confidence,
|
|
314
|
+
moodValence: args.mood_valence ?? null,
|
|
315
|
+
arousal: args.arousal ?? null,
|
|
316
|
+
emotion: args.emotion,
|
|
317
|
+
dominance: args.dominance ?? null,
|
|
318
|
+
peak: args.peak === true,
|
|
319
|
+
salientDate: args.salient_date ?? null,
|
|
320
|
+
observedAt: args.observed_at ?? null,
|
|
321
|
+
absoluteDate: args.absolute_date ?? null,
|
|
322
|
+
validFrom: args.valid_from,
|
|
323
|
+
validTo: args.valid_to,
|
|
324
|
+
datePrecision: args.date_precision,
|
|
325
|
+
allowDuplicate: args.allow_duplicate === true,
|
|
326
|
+
})
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** 把 privateRemember 的结果压成 worker 能直接读的一行/一段;去重与冲突必须显式说。 */
|
|
330
|
+
function describeRememberResult(result, label = '') {
|
|
331
|
+
const prefix = label ? label + ': ' : ''
|
|
332
|
+
if (result?.deduped === true) {
|
|
333
|
+
return `${prefix}未写入:库内已有几乎相同的记忆 [${result.existingId}](相似度 ${(Number(result.similarity) || 0).toFixed(2)});要补充请用 memu_private_update id=${result.existingId}。`
|
|
334
|
+
}
|
|
335
|
+
const entry = result?.entry
|
|
336
|
+
if (result?.conflict) {
|
|
337
|
+
return `${prefix}已记录但与 [${result.conflict}] 冲突,进入待复核(id=${entry?.id});复核前不会出现在召回或注入里。`
|
|
338
|
+
}
|
|
339
|
+
return `${prefix}已写入私有记忆:${result?.id} · layer=${entry?.layer || 'long_term'} · title=${entry?.title || ''}`
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async function rememberOneOutcome(exec, args) {
|
|
343
|
+
try {
|
|
344
|
+
const result = await rememberFromArgs(exec, args)
|
|
345
|
+
return { ok: true, result, text: describeRememberResult(result) }
|
|
346
|
+
} catch (error) {
|
|
347
|
+
return { ok: false, text: '未写入:' + errorText(error).replace(/\n/g, ' ') }
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
export function registerTools(ctx, { only = null } = {}) {
|
|
353
|
+
const tools = []
|
|
354
|
+
// 仅宿主全量装载更新全局 registrationReport;worker 的按需挂载保留全局清单,
|
|
355
|
+
// 避免 memu_status 将局部工具子集显示为完整注册结果。
|
|
356
|
+
const reportSaved = only ? registrationReport : null
|
|
357
|
+
if (!only) registrationReport = { attempted: 0, registered: 0, failures: [] }
|
|
358
|
+
tools.push(toolBase(
|
|
359
|
+
'memu_private_link',
|
|
360
|
+
'Connect two existing memories or a memory and an existing entity in the bound library. kind is relation, commitment, event, person, conflict or supersede. Requires an evidence note of 8-500 characters; confidence defaults to 0.7. This is also the adjudication tool for memu_private_link_suggest candidates: link a pair only when the note cites concrete evidence of the same entity or event, and pass entity {name, kind} to create or reuse that named entity. Cannot delete links or cross conversation visibility boundaries. When to use it: a new memory supersedes an older statement of the same fact → link to the old entry (supersede); several memories about the same person/thing → link them to a named entity; a promise or commitment was made → commitment; two memories contradict each other → conflict. When link_suggest returns candidates, adjudicate every one explicitly — link with evidence or discard; never leave candidates unjudged.',
|
|
361
|
+
{
|
|
362
|
+
from: { type: 'string', description: 'Exact source memory id; required unless updating a relationship snapshot.' },
|
|
363
|
+
to: { type: 'string', description: 'Exact target memory or entity id; required unless entity or relationship is supplied.' },
|
|
364
|
+
kind: { type: 'string', description: 'relation, commitment, event, person, conflict or supersede; required when linking. supersede marks a newer version replacing an older statement of the same fact.' },
|
|
365
|
+
note: { type: 'string', required: true, description: '8-500 character reason grounded in the conversation, not a guess. For a memu_private_link_suggest candidate, quote the transcript line that proves the same entity or event.' },
|
|
366
|
+
confidence: { type: 'number', description: '0-1 confidence, default 0.7.' },
|
|
367
|
+
entity: { type: 'object', properties: { name: { type: 'string' }, kind: { type: 'string' } }, additionalProperties: true, description: 'Optional named entity to create and link to instead of to; an entity with the same normalized name is reused (case/width-insensitive). New names require a complete quoted, introduced, or separately delimited name in the source memory; sentence fragments and a name invented only in the note are rejected. Existing names and aliases are reused. Never infer a person from an ambiguous pronoun.' },
|
|
368
|
+
relationship: { type: 'object', properties: {
|
|
369
|
+
fromEntity: { type: 'string' }, toEntity: { type: 'string' },
|
|
370
|
+
evidenceIds: { type: 'array', items: { type: 'string' } },
|
|
371
|
+
state: { type: 'object', properties: { trust: { type: 'number' }, warmth: { type: 'number' }, familiarity: { type: 'number' },
|
|
372
|
+
boundaries: { type: 'array', items: { type: 'string' } }, callbacks: { type: 'array', items: { type: 'string' } }, openPromises: { type: 'array', items: { type: 'string' } } }, additionalProperties: true },
|
|
373
|
+
}, additionalProperties: true, description: 'Optional complete relationship snapshot between two existing entities, with 1-20 visible evidenceIds. Unknown dimensions stay null. Never infer affection from turn counts; preserve boundaries and concrete promises.' },
|
|
374
|
+
},
|
|
375
|
+
(args, exec) => {
|
|
376
|
+
if (args.relationship) {
|
|
377
|
+
const relation = args.relationship
|
|
378
|
+
const updated = updateRelationship({ exec, fromId: relation.fromEntity, toId: relation.toEntity, state: relation.state, evidenceIds: relation.evidenceIds, reason: args.note })
|
|
379
|
+
const r = updated?.relationship || updated || {}
|
|
380
|
+
// 完整 history(≤50 版)不进模型上下文:只回一行,需要细节时再查。
|
|
381
|
+
return `已更新关系 ${r.fromId ?? relation.fromEntity}→${r.toId ?? relation.toEntity} · revision ${r.revision ?? '?'} · evidence ${Array.isArray(r.evidenceIds) ? r.evidenceIds.length : 0}`
|
|
382
|
+
}
|
|
383
|
+
if (args.entity) return JSON.stringify(linkNamedEntity({ exec, fromId: args.from, entity: args.entity, kind: args.kind, note: args.note, confidence: args.confidence }))
|
|
384
|
+
return JSON.stringify(linkEntries({ exec, fromId: args.from, toId: args.to, kind: args.kind, note: args.note, confidence: args.confidence }))
|
|
385
|
+
},
|
|
386
|
+
))
|
|
387
|
+
|
|
388
|
+
tools.push(toolBase(
|
|
389
|
+
'memu_private_link_suggest',
|
|
390
|
+
'Local algorithm pass over the memories this consolidation just wrote. It refreshes only deterministic evidence edges (mentions of confirmed entities, conflicts_with / supersedes / same fact_key) and returns uncertain statistical pairs for you to adjudicate with memu_private_link. Similarity and shared tokens never create an edge. Call it once after writing, with the ids you created or updated; it needs no extra transcript read and never returns full memory bodies. Every returned pair must be adjudicated explicitly in this turn: link with memu_private_link (note quoting the transcript evidence) or discard — silence is not adjudication.',
|
|
391
|
+
{
|
|
392
|
+
ids: {
|
|
393
|
+
type: 'array',
|
|
394
|
+
items: { type: 'string' },
|
|
395
|
+
// 宿主 schema DSL 不允许在数组节点上写 required(与 tags/aliases 同),
|
|
396
|
+
// 所以"必填"由描述 + 运行时校验表达,schema 面只能标成可选。
|
|
397
|
+
description: 'Required: ids created or updated in this consolidation, 1-20. Only the entries this run touched need to be listed.',
|
|
398
|
+
},
|
|
399
|
+
},
|
|
400
|
+
async (args, exec) => {
|
|
401
|
+
// Read-only for the binding itself: an unbound chat gets an answer, not a library.
|
|
402
|
+
const binding = peekBinding(exec)
|
|
403
|
+
if (!binding?.libraryId) return '当前对话没有绑定记忆库,无法计算私有自动关联候选。'
|
|
404
|
+
const ids = (Array.isArray(args.ids) ? args.ids : [])
|
|
405
|
+
.map((id) => String(id ?? '').trim())
|
|
406
|
+
.filter(Boolean)
|
|
407
|
+
.slice(0, 20)
|
|
408
|
+
if (!ids.length) return '没有可关联的 id:请传本轮创建/更新的记忆 id(1-20 个)。'
|
|
409
|
+
const result = await autoLinkPrivateMemories({
|
|
410
|
+
libraryId: binding.libraryId,
|
|
411
|
+
sessionId: sessionIdOf(exec),
|
|
412
|
+
ids,
|
|
413
|
+
})
|
|
414
|
+
// Compact contract: what was already linked, then the uncertain pairs. A run
|
|
415
|
+
// with no candidates still reports autoLinked so "nothing applied" and
|
|
416
|
+
// "nothing needed" stay distinguishable.
|
|
417
|
+
return JSON.stringify({ autoLinked: result.autoLinked || [], candidates: result.candidates || [] })
|
|
418
|
+
},
|
|
419
|
+
{ isConcurrencySafe: () => false, timeoutMs: 120_000 },
|
|
420
|
+
))
|
|
421
|
+
|
|
422
|
+
tools.push(toolBase(
|
|
423
|
+
'memu_private_remember',
|
|
424
|
+
'Write one durable private memory. Match the domain; never copy a transcript or invent evidence. layer=must_remember requires pin_reason and pin_source.',
|
|
425
|
+
{
|
|
426
|
+
content: { type: 'string', required: true, description: 'Self-contained 50-170 char body: people, place, time, event, exact words. Coding keeps conclusion and verification; RP keeps evidenced moments, no invented facts.' },
|
|
427
|
+
title: { type: 'string', description: '8-40 chars naming the concrete fact; title is a permanent index, the anchor shows only titles WITHOUT the body. No empty labels. Defaults to content.' },
|
|
428
|
+
layer: { type: 'string', description: 'long_term (default), short_term, must_remember. short_term expires unless promoted.' },
|
|
429
|
+
pin_reason: { type: 'string', description: 'Required for must_remember; first-person reason.' },
|
|
430
|
+
pin_source: { type: 'string', description: 'user_explicit, side_event, or self.' },
|
|
431
|
+
side_event_id: { type: 'string', description: 'Required when pin_source=side_event.' },
|
|
432
|
+
fact_key: { type: 'string', description: 'Optional stable key. Reusing it marks another version of the same fact (conflict pair unless supersedes).' },
|
|
433
|
+
conflicts_with: { type: 'string', description: 'Existing memory id with a different version of the same fact; both stay active and recall together.' },
|
|
434
|
+
supersedes: { type: 'string', description: 'Existing memory id this explicitly replaces; old text stays but is retired.' },
|
|
435
|
+
tags: { type: 'array', items: { type: 'string' }, description: 'Optional tags.' },
|
|
436
|
+
open_loop: { type: 'boolean', description: 'Unfinished promise/task; prefer commitment.' },
|
|
437
|
+
commitment: {
|
|
438
|
+
type: 'object',
|
|
439
|
+
properties: {
|
|
440
|
+
counterparty: { type: 'string' },
|
|
441
|
+
action: { type: 'string' },
|
|
442
|
+
due_at: { type: 'string' },
|
|
443
|
+
status: { type: 'string' },
|
|
444
|
+
closed_by: { type: 'string' },
|
|
445
|
+
evidence_seq: { type: 'integer' },
|
|
446
|
+
},
|
|
447
|
+
additionalProperties: false,
|
|
448
|
+
description: 'Promise: counterparty, action, due_at (YYYY-MM-DD/ISO), status (open/done/cancelled/expired), closed_by/evidence_seq.',
|
|
449
|
+
},
|
|
450
|
+
disclosure: { type: 'string', description: 'can_tell, only_if_asked, never_say.' },
|
|
451
|
+
owner: { type: 'string', description: 'me, us, about_user, about_project.' },
|
|
452
|
+
visibility: { type: 'string', description: 'shared (default) or secret (no automatic injection/recall).' },
|
|
453
|
+
source: {
|
|
454
|
+
type: 'string',
|
|
455
|
+
description: 'inferred (default), user_said (quotable), assistant_said, imagined, told, tool.',
|
|
456
|
+
},
|
|
457
|
+
source_kind: { type: 'string', description: 'Same values as source; assistant promises/plans must not be user_said.' },
|
|
458
|
+
speaker_id: { type: 'string', description: 'user, assistant, character:<id>, group:<id>; never infer from a name.' },
|
|
459
|
+
quote_span: {
|
|
460
|
+
type: 'object',
|
|
461
|
+
properties: { seq: { type: 'integer' }, turn: { type: 'integer' }, textHash: { type: 'string' } },
|
|
462
|
+
additionalProperties: false,
|
|
463
|
+
description: 'Optional source pointer; do not paste the whole quote.',
|
|
464
|
+
},
|
|
465
|
+
storyline_id: { type: 'string', description: 'Optional storyline id.' },
|
|
466
|
+
branch_id: { type: 'string', description: 'Optional branch id.' },
|
|
467
|
+
known_by: { type: 'array', items: { type: 'string' }, description: 'Character ids that know this fact.' },
|
|
468
|
+
confidence: { type: 'number', description: '0-1; below 0.45 is injected as a hedge.' },
|
|
469
|
+
mood_valence: { type: 'number', description: '-1..1; omit to inherit the conversation reading.' },
|
|
470
|
+
arousal: { type: 'number', description: '0-1 intensity.' },
|
|
471
|
+
emotion: {
|
|
472
|
+
type: 'object',
|
|
473
|
+
properties: { joy: { type: 'number' }, anger: { type: 'number' }, sorrow: { type: 'number' }, fear: { type: 'number' } },
|
|
474
|
+
// 四个轴是封闭集合:多写的键只会被误当成第五种情绪。
|
|
475
|
+
additionalProperties: false,
|
|
476
|
+
description: 'joy/anger/sorrow/fear for the remembered event, not the consolidation mood. Omit to inherit; all-zero is neutral.',
|
|
477
|
+
},
|
|
478
|
+
dominance: { type: 'number', description: '-1..1; omit to inherit.' },
|
|
479
|
+
peak: { type: 'boolean', description: 'High/low point of the arc.' },
|
|
480
|
+
allow_duplicate: { type: 'boolean', description: 'Only for a genuinely independent second observation; default false refuses near-duplicates.' },
|
|
481
|
+
salient_date: { type: 'string', description: 'MM-DD for yearly recurrence; YYYY-MM-DD for a one-off appointment. Resolve 今天/明天/下周 to the absolute date; never send a bare MM-DD for a one-off.' },
|
|
482
|
+
observed_at: { type: 'string', description: 'ISO scene time of this statement; relative dates resolve against it, never the wall clock. Omit to use the conversation sceneTime.' },
|
|
483
|
+
absolute_date: { type: 'string', description: 'YYYY-MM-DD this fact is about; omit to let the plugin resolve observed_at + relative wording.' },
|
|
484
|
+
valid_from: { type: 'string', description: 'Optional YYYY-MM-DD when the fact starts.' },
|
|
485
|
+
valid_to: { type: 'string', description: 'Optional YYYY-MM-DD after which it no longer holds.' },
|
|
486
|
+
date_precision: { type: 'string', description: 'day, month, year, or unknown.' },
|
|
487
|
+
track: { type: 'string', description: 'memory (default) or habit (aggregated repeated expression).' },
|
|
488
|
+
attachments: { type: 'array', items: { type: 'object', properties: { sha256: { type: 'string', required: true } }, additionalProperties: true }, description: 'At most 8 sha256 refs received in this conversation; never paths or bytes.' },
|
|
489
|
+
context: { type: 'object', properties: {
|
|
490
|
+
speakerId: { type: 'string' }, channelId: { type: 'string' }, characterId: { type: 'string' }, worldId: { type: 'string' }, branchId: { type: 'string' },
|
|
491
|
+
knownBy: { type: 'array', items: { type: 'string' } }, occurredAt: { type: 'string' },
|
|
492
|
+
}, additionalProperties: true, description: 'Stable identities only; scope inherits the conversation and cannot widen it.' },
|
|
493
|
+
},
|
|
494
|
+
async (args, exec) => {
|
|
495
|
+
// 与 remember_batch 共用参数映射,避免字段漂移。
|
|
496
|
+
const result = await rememberFromArgs(exec, args)
|
|
497
|
+
// 去重结果为 { deduped, existingId, hint },没有 entry,需先处理此分支。
|
|
498
|
+
if (result.deduped === true) {
|
|
499
|
+
return [
|
|
500
|
+
`未写入:库内已有几乎相同的记忆 [${result.existingId}](相似度 ${(Number(result.similarity) || 0).toFixed(2)})。`,
|
|
501
|
+
`要补充内容请改用 memu_private_update 传该 id 合并;若确属两次独立观测,带 allow_duplicate=true 重写。`,
|
|
502
|
+
].join('\n')
|
|
503
|
+
}
|
|
504
|
+
const entry = result.entry
|
|
505
|
+
if (result.conflict) {
|
|
506
|
+
// A pending_review entry is stored but excluded from recall and injection
|
|
507
|
+
// until accepted; report the held state explicitly.
|
|
508
|
+
return [
|
|
509
|
+
`已记录但与现有记忆 [${result.conflict}] 冲突,进入待复核(pending_review)。`,
|
|
510
|
+
`复核接受之前它不会出现在召回或注入里;如需要可查看复核队列并裁决。`,
|
|
511
|
+
`layer=${entry.layer} title=${entry.title}`,
|
|
512
|
+
].join('\n')
|
|
513
|
+
}
|
|
514
|
+
return [
|
|
515
|
+
`已写入私有记忆:${result.id}`,
|
|
516
|
+
`layer=${entry.layer} title=${entry.title}`,
|
|
517
|
+
entry.expiresAt ? `expires_at=${entry.expiresAt}(到期前用 memu_private_update layer=long_term 可升级)` : '',
|
|
518
|
+
entry.pinReason ? `pin_reason=${entry.pinReason}` : '',
|
|
519
|
+
].filter(Boolean).join('\n')
|
|
520
|
+
},
|
|
521
|
+
{ timeoutMs: 180_000 },
|
|
522
|
+
))
|
|
523
|
+
|
|
524
|
+
tools.push(toolBase(
|
|
525
|
+
'memu_private_remember_batch',
|
|
526
|
+
'Write several private memories in one call. Put shared fields in defaults and only per-item differences in items; each item is one fact. Use this instead of calling memu_private_remember once per entry. Items that dedupe or conflict are reported per line.',
|
|
527
|
+
{
|
|
528
|
+
defaults: {
|
|
529
|
+
type: 'object',
|
|
530
|
+
properties: {
|
|
531
|
+
speaker_id: { type: 'string' },
|
|
532
|
+
source_kind: { type: 'string' },
|
|
533
|
+
source: { type: 'string' },
|
|
534
|
+
owner: { type: 'string' },
|
|
535
|
+
confidence: { type: 'number' },
|
|
536
|
+
observed_at: { type: 'string' },
|
|
537
|
+
absolute_date: { type: 'string' },
|
|
538
|
+
date_precision: { type: 'string' },
|
|
539
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
540
|
+
layer: { type: 'string' },
|
|
541
|
+
},
|
|
542
|
+
additionalProperties: true,
|
|
543
|
+
description: 'Shared fields for every item; per-item values override these.',
|
|
544
|
+
},
|
|
545
|
+
items: {
|
|
546
|
+
type: 'array',
|
|
547
|
+
description: 'Supply 1-8 facts; only the first 8 items are processed. Each item needs title and content; per-item fields override defaults.',
|
|
548
|
+
items: {
|
|
549
|
+
type: 'object',
|
|
550
|
+
properties: {
|
|
551
|
+
title: { type: 'string', required: true },
|
|
552
|
+
content: { type: 'string', required: true },
|
|
553
|
+
observed_at: { type: 'string' },
|
|
554
|
+
absolute_date: { type: 'string' },
|
|
555
|
+
date_precision: { type: 'string' },
|
|
556
|
+
speaker_id: { type: 'string' },
|
|
557
|
+
source_kind: { type: 'string' },
|
|
558
|
+
source: { type: 'string' },
|
|
559
|
+
owner: { type: 'string' },
|
|
560
|
+
confidence: { type: 'number' },
|
|
561
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
562
|
+
emotion: {
|
|
563
|
+
type: 'object',
|
|
564
|
+
properties: { joy: { type: 'number' }, anger: { type: 'number' }, sorrow: { type: 'number' }, fear: { type: 'number' } },
|
|
565
|
+
additionalProperties: false,
|
|
566
|
+
},
|
|
567
|
+
commitment: { type: 'object', additionalProperties: true },
|
|
568
|
+
conflicts_with: { type: 'string' },
|
|
569
|
+
salient_date: { type: 'string' },
|
|
570
|
+
open_loop: { type: 'boolean' },
|
|
571
|
+
layer: { type: 'string' },
|
|
572
|
+
},
|
|
573
|
+
additionalProperties: true,
|
|
574
|
+
},
|
|
575
|
+
},
|
|
576
|
+
},
|
|
577
|
+
async (args, exec) => {
|
|
578
|
+
const defaults = args?.defaults && typeof args.defaults === 'object' ? args.defaults : {}
|
|
579
|
+
const items = Array.isArray(args?.items) ? args.items.slice(0, 8) : []
|
|
580
|
+
if (items.length === 0) return '未写入:items 为空;每条至少需要 title 和 content。'
|
|
581
|
+
const lines = []
|
|
582
|
+
let written = 0
|
|
583
|
+
let deduped = 0
|
|
584
|
+
let held = 0
|
|
585
|
+
for (let index = 0; index < items.length; index += 1) {
|
|
586
|
+
const item = items[index]
|
|
587
|
+
if (!item || typeof item !== 'object') {
|
|
588
|
+
lines.push(`${index + 1}. 未写入:item 不是对象`)
|
|
589
|
+
continue
|
|
590
|
+
}
|
|
591
|
+
const outcome = await rememberOneOutcome(exec, { ...defaults, ...item })
|
|
592
|
+
lines.push(`${index + 1}. ${outcome.text}`)
|
|
593
|
+
if (!outcome.ok) continue
|
|
594
|
+
if (outcome.result?.deduped === true) deduped += 1
|
|
595
|
+
else if (outcome.result?.entry?.status === 'pending_review' || outcome.result?.conflict) held += 1
|
|
596
|
+
else written += 1
|
|
597
|
+
}
|
|
598
|
+
return [`批量写入完成:新增 ${written} · 去重 ${deduped} · 待复核 ${held}`, ...lines].join('\n')
|
|
599
|
+
},
|
|
600
|
+
{ timeoutMs: 300_000 },
|
|
601
|
+
))
|
|
602
|
+
|
|
603
|
+
tools.push(toolBase(
|
|
604
|
+
'memu_private_recall',
|
|
605
|
+
'Retrieve relevant private memories from the library bound to this conversation. Natural queries return short snippets; exact mem- ids (or unique prefixes, optionally .md, no whitespace) read one redacted body page with auto/full. Secret memories remain explicitly readable with disclosure restrictions. Use libraryId only to disambiguate an allowed exact reference, never to widen scope.',
|
|
606
|
+
{
|
|
607
|
+
query: { type: 'string', required: true, description: 'Natural-language search or exact mem- id / unique prefix (optional .md, no whitespace).' },
|
|
608
|
+
topK: { type: 'integer', description: 'Search row limit, integer 1..20; default from settings. Exact reads return one entry.' },
|
|
609
|
+
...recallReadParameters(),
|
|
610
|
+
libraryId: { type: 'string', description: 'Private exact id only: owning library from a returned ref; must be in the allowed library set.' },
|
|
611
|
+
},
|
|
612
|
+
async (args, exec) => {
|
|
613
|
+
const request = parseRecallRequest(args, 'private')
|
|
614
|
+
if (request.exact) {
|
|
615
|
+
const page = readMemoryByRef({ exec, scope: 'private', id: request.query, libraryId: request.libraryId,
|
|
616
|
+
offset: request.offset, limitChars: request.limitChars, revision: request.revision })
|
|
617
|
+
return renderMemoryPage(page, request)
|
|
618
|
+
}
|
|
619
|
+
const result = await privateRecall({ exec, query: request.query, topK: request.topK, purpose: 'explicit' })
|
|
620
|
+
// Surface a degraded semantic leg on the explicit tool path too; silently
|
|
621
|
+
// returning fewer hits would make a failed backend look like an empty library.
|
|
622
|
+
const warning = (result.errors || []).length ? `\n注意:本次召回已降级:${(result.errors || []).join('; ')}` : ''
|
|
623
|
+
// Read-only: never materialize a binding/library from a recall header.
|
|
624
|
+
const binding = peekBinding(exec)
|
|
625
|
+
const foreign = (result.libraries || []).filter((id) => id !== binding?.libraryId)
|
|
626
|
+
const header = `私有记忆(library=${binding?.libraryId || '?'}, domain=${domainOf(binding)}${foreign.length ? `,跨库=${foreign.join(',')}` : ''})`
|
|
627
|
+
if (!result.files?.length) return `${header}\n没有命中。${warning}`
|
|
628
|
+
return [header, ...result.files.map((f, i) => formatPrivateItem(f, i + 1, { query: args.query, homeLibraryId: binding?.libraryId || null }))].join('\n\n') + warning
|
|
629
|
+
},
|
|
630
|
+
{ isConcurrencySafe: () => true },
|
|
631
|
+
))
|
|
632
|
+
|
|
633
|
+
tools.push(toolBase(
|
|
634
|
+
'memu_private_list',
|
|
635
|
+
'List private memory metadata for the library bound to this conversation. When cross-library recall is on, same-domain libraries are included too. Does not return full bodies. Paged: default 50 rows, use offset to continue.',
|
|
636
|
+
{
|
|
637
|
+
limit: { type: 'number', description: 'Rows in this page; default 50, max 200.' },
|
|
638
|
+
offset: { type: 'number', description: '0-based row offset; for the next page pass the last returned offset + count.' },
|
|
639
|
+
},
|
|
640
|
+
async (args, exec) => {
|
|
641
|
+
// A list is a pure read: if the conversation has no binding yet, say so
|
|
642
|
+
// instead of ensureSession() silently creating a placeholder library.
|
|
643
|
+
if (!peekBinding(exec)?.libraryId) return '当前对话没有绑定记忆库。'
|
|
644
|
+
const limit = Math.min(200, Math.max(1, Math.floor(Number(args?.limit) || 50)))
|
|
645
|
+
const offset = Math.max(0, Math.floor(Number(args?.offset) || 0))
|
|
646
|
+
const result = await privateList({ exec, limit, offset })
|
|
647
|
+
const warning = result.warning ? `\n注意:${result.warning}` : ''
|
|
648
|
+
const foreign = (result.libraries || []).filter((library) => library.id !== result.libraryId)
|
|
649
|
+
const scopeNote = foreign.length ? `,跨库=${foreign.map((library) => library.name || library.id).join(',')}` : ''
|
|
650
|
+
const total = Number.isFinite(result.total) ? result.total : (result.files?.length || 0)
|
|
651
|
+
if (!total) return `私有记忆库 ${result.libraryId} 为空${scopeNote}。${warning}`
|
|
652
|
+
const pageEnd = offset + (result.files?.length || 0)
|
|
653
|
+
const lines = [`私有记忆库 ${result.libraryId}(共 ${total} 条,本页 ${offset + 1}-${pageEnd}${scopeNote})`]
|
|
654
|
+
for (const file of result.files) {
|
|
655
|
+
const e = file.entry || {}
|
|
656
|
+
const tag = salientTag(e)
|
|
657
|
+
const owner = file.libraryId && file.libraryId !== result.libraryId
|
|
658
|
+
? ` · lib=${file.libraryName || file.libraryId}`
|
|
659
|
+
: ''
|
|
660
|
+
lines.push(`- [${file.name}] ${e.title || file.description || '(untitled)'} · ${e.layer || 'long_term'} · ${e.status || 'active'}${e.marginalized ? ' · marginalized' : ''}${tag ? ` · ${tag}` : ''}${owner}`)
|
|
661
|
+
}
|
|
662
|
+
const more = pageEnd < total ? `\n还有 ${total - pageEnd} 条:用 offset=${pageEnd} 继续。` : ''
|
|
663
|
+
return redactSecrets(lines.join('\n')) + more + warning
|
|
664
|
+
},
|
|
665
|
+
{ isConcurrencySafe: () => true },
|
|
666
|
+
))
|
|
667
|
+
|
|
668
|
+
tools.push(toolBase(
|
|
669
|
+
'memu_private_update',
|
|
670
|
+
'Update supplied fields of a private memory in the bound library. Commitment is patched and keeps omitted subfields. Set clear_commitment=true to remove the commitment and clear open_loop, or clear_commitment_due=true to remove only its deadline. Content changes rewrite the stored body; metadata-only changes stay local. Update the title when needed.',
|
|
671
|
+
{
|
|
672
|
+
id: { type: 'string', required: true, description: 'Memory id.' },
|
|
673
|
+
content: { type: 'string', description: 'New 50-170 char body.' },
|
|
674
|
+
title: { type: 'string', description: '8-40 chars naming the concrete fact; title is a permanent index, the anchor shows only titles WITHOUT the body. No empty labels. Omit to keep.' },
|
|
675
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
676
|
+
status: { type: 'string', description: 'active, archived, fragment, suppressed, expired.' },
|
|
677
|
+
layer: { type: 'string', description: 'long_term (promote), short_term (re-arm expiry); must_remember only via remember.' },
|
|
678
|
+
open_loop: { type: 'boolean' },
|
|
679
|
+
clear_commitment: { type: 'boolean', description: 'Remove the commitment and synchronized open_loop flag. Do not also send commitment or clear_commitment_due.' },
|
|
680
|
+
clear_commitment_due: { type: 'boolean', description: 'Explicitly clear commitment.due_at; the host schema cannot express string-or-null. Other supplied commitment fields are patched.' },
|
|
681
|
+
commitment: {
|
|
682
|
+
type: 'object',
|
|
683
|
+
properties: {
|
|
684
|
+
counterparty: { type: 'string' },
|
|
685
|
+
action: { type: 'string' },
|
|
686
|
+
due_at: { type: 'string' },
|
|
687
|
+
status: { type: 'string' },
|
|
688
|
+
closed_by: { type: 'string' },
|
|
689
|
+
evidence_seq: { type: 'integer' },
|
|
690
|
+
},
|
|
691
|
+
additionalProperties: false,
|
|
692
|
+
description: 'Patch commitment; omitted fields keep their values. status=done/cancelled/expired closes it. Use clear_commitment_due to remove a deadline.',
|
|
693
|
+
},
|
|
694
|
+
disclosure: { type: 'string', description: 'can_tell, only_if_asked, never_say.' },
|
|
695
|
+
owner: { type: 'string', description: 'me, us, about_user, about_project.' },
|
|
696
|
+
visibility: { type: 'string', description: 'shared or secret.' },
|
|
697
|
+
confidence: { type: 'number' },
|
|
698
|
+
// Expose provenance and track so callers can correct classification metadata
|
|
699
|
+
// through the same validated update path as the panel.
|
|
700
|
+
source: { type: 'string', description: 'user_said, assistant_said, told, imagined, inferred, tool.' },
|
|
701
|
+
source_kind: { type: 'string', description: 'Same values as source; keep in sync.' },
|
|
702
|
+
speaker_id: { type: 'string', description: 'user, assistant, character:<id>, group:<id>; empty clears.' },
|
|
703
|
+
quote_span: { type: 'object', properties: { seq: { type: 'integer' }, turn: { type: 'integer' }, textHash: { type: 'string' } }, additionalProperties: false, description: 'Source pointer object; omit to keep the existing pointer.' },
|
|
704
|
+
storyline_id: { type: 'string', description: 'Storyline id; empty clears.' },
|
|
705
|
+
branch_id: { type: 'string', description: 'Branch id; empty clears.' },
|
|
706
|
+
known_by: { type: 'array', items: { type: 'string' }, description: 'Character ids; empty clears.' },
|
|
707
|
+
track: { type: 'string', description: 'memory or habit.' },
|
|
708
|
+
peak: { type: 'boolean', description: 'Flashbulb flag.' },
|
|
709
|
+
mood_valence: { type: 'number', description: '-1..1 retrieval lens; omit keeps.' },
|
|
710
|
+
arousal: { type: 'number', description: '0..1 retrieval lens; omit keeps.' },
|
|
711
|
+
emotion: {
|
|
712
|
+
type: 'object',
|
|
713
|
+
properties: { joy: { type: 'number' }, anger: { type: 'number' }, sorrow: { type: 'number' }, fear: { type: 'number' } },
|
|
714
|
+
additionalProperties: false,
|
|
715
|
+
description: 'Omit to keep; an object replaces the event emotion after normalisation. An all-zero object records a neutral event.',
|
|
716
|
+
},
|
|
717
|
+
dominance: { type: 'number', description: '-1..1; omit keeps.' },
|
|
718
|
+
salient_date: { type: 'string', description: 'MM-DD yearly or YYYY-MM-DD one-off; empty clears.' },
|
|
719
|
+
observed_at: { type: 'string', description: 'ISO scene time for this edit; omit uses conversation sceneTime.' },
|
|
720
|
+
absolute_date: { type: 'string', description: 'YYYY-MM-DD; omit re-derives, empty clears.' },
|
|
721
|
+
valid_from: { type: 'string', description: 'YYYY-MM-DD start; empty clears.' },
|
|
722
|
+
valid_to: { type: 'string', description: 'YYYY-MM-DD end; empty clears.' },
|
|
723
|
+
},
|
|
724
|
+
async (args, exec) => {
|
|
725
|
+
for (const key of ['clear_commitment', 'clear_commitment_due']) {
|
|
726
|
+
if (args[key] !== undefined && typeof args[key] !== 'boolean') throw new Error(key + ' must be boolean')
|
|
727
|
+
}
|
|
728
|
+
let commitment = args.commitment
|
|
729
|
+
if (args.clear_commitment) {
|
|
730
|
+
if (commitment !== undefined || args.clear_commitment_due) throw new Error('clear_commitment cannot be combined with commitment or clear_commitment_due')
|
|
731
|
+
commitment = null
|
|
732
|
+
} else if (args.clear_commitment_due) {
|
|
733
|
+
if (commitment !== undefined && (!commitment || typeof commitment !== 'object' || Array.isArray(commitment))) throw new Error('commitment must be an object when clearing its deadline')
|
|
734
|
+
if (commitment && ['due_at', 'dueAt'].some(key => Object.hasOwn(commitment, key) && commitment[key] !== null)) throw new Error('cannot clear and set commitment deadline together')
|
|
735
|
+
commitment = { ...(commitment || {}), due_at: null }
|
|
736
|
+
}
|
|
737
|
+
const result = await privateUpdate({
|
|
738
|
+
exec, id: args.id, content: args.content, title: args.title, tags: args.tags,
|
|
739
|
+
status: args.status, openLoop: args.open_loop, commitment, disclosure: args.disclosure, confidence: args.confidence,
|
|
740
|
+
layer: args.layer, owner: args.owner, visibility: args.visibility,
|
|
741
|
+
source: args.source, track: args.track, peak: args.peak,
|
|
742
|
+
sourceKind: args.source_kind, speakerId: args.speaker_id,
|
|
743
|
+
quoteSpan: args.quote_span, storylineId: args.storyline_id,
|
|
744
|
+
branchId: args.branch_id, knownBy: args.known_by,
|
|
745
|
+
moodValence: args.mood_valence, arousal: args.arousal,
|
|
746
|
+
// undefined 就是"省略",必须原样传下去;换成 null 会把保留变成清除。
|
|
747
|
+
emotion: args.emotion, dominance: args.dominance,
|
|
748
|
+
// Sentinel: absent means "leave the date as it is", '' means "clear it".
|
|
749
|
+
salientDate: args.salient_date,
|
|
750
|
+
observedAt: args.observed_at,
|
|
751
|
+
absoluteDate: args.absolute_date,
|
|
752
|
+
validFrom: args.valid_from,
|
|
753
|
+
validTo: args.valid_to,
|
|
754
|
+
})
|
|
755
|
+
return `已更新 ${result.id}: ${result.entry.title} (${result.entry.layer}, ${result.entry.status})`
|
|
756
|
+
},
|
|
757
|
+
{ timeoutMs: 180_000 },
|
|
758
|
+
))
|
|
759
|
+
|
|
760
|
+
tools.push(toolBase(
|
|
761
|
+
'memu_private_archive',
|
|
762
|
+
'Archive a private memory. It stays in the library but is filtered out of normal retrieval.',
|
|
763
|
+
{ id: { type: 'string', required: true } },
|
|
764
|
+
async (args, exec) => {
|
|
765
|
+
const result = privateArchive({ exec, id: args.id })
|
|
766
|
+
return `已归档 ${result.id}。`
|
|
767
|
+
},
|
|
768
|
+
))
|
|
769
|
+
|
|
770
|
+
tools.push(toolBase(
|
|
771
|
+
'memu_private_unpin',
|
|
772
|
+
'Move a must_remember memory back to long_term as marginalized when its constraint or lasting significance has genuinely changed. Requires a first-person reason of at least 20 characters with evidence of that change; no invented emotional event is needed. Per-conversation quota and cooldown apply. Do not unpin effective constraints just for space or age.',
|
|
773
|
+
{
|
|
774
|
+
id: { type: 'string', required: true },
|
|
775
|
+
reason: { type: 'string', required: true, description: 'First-person reason for letting it go.' },
|
|
776
|
+
},
|
|
777
|
+
async (args, exec) => {
|
|
778
|
+
const result = privateUnpin({ exec, id: args.id, reason: args.reason })
|
|
779
|
+
return `已移出永久记忆并边缘化:${result.id}\n${result.entry.marginalReason}`
|
|
780
|
+
},
|
|
781
|
+
))
|
|
782
|
+
|
|
783
|
+
tools.push(toolBase(
|
|
784
|
+
'memu_lexicon_search',
|
|
785
|
+
'Search the ACTIVE lexicon (user-coined slang, memes, triggers and terms) this conversation can see: the bound library plus the global lexicon, and — when cross-library recall is on — other same-domain libraries; archived entries are excluded. Read-only. When a user-coined word or phrase appears and you are not sure what it means, look it up here before asking or guessing; returns term → meaning/usage with short evidence. Hits from outside the bound library are read-only: writing still lands in the bound library.',
|
|
786
|
+
{
|
|
787
|
+
query: { type: 'string', required: true, description: 'The word or phrase to look up. Exact term/alias match first with a bounded fuzzy fallback; short queries work best.' },
|
|
788
|
+
},
|
|
789
|
+
async (args, exec) => {
|
|
790
|
+
// Read-only: never materialize a binding or a library from a lexicon query,
|
|
791
|
+
// same rule as memu_private_recall.
|
|
792
|
+
const binding = peekBinding(exec)
|
|
793
|
+
if (!binding?.libraryId) return '当前对话没有绑定记忆库,无法查询词典。请先初始化或绑定记忆库。'
|
|
794
|
+
const { items, errors } = lexiconSearch(binding, String(args.query || ''))
|
|
795
|
+
const warning = errors.length ? `\n注意:本次词典查询已降级:${errors.join('; ')}` : ''
|
|
796
|
+
// 作用域头:当前库 + 全局(+ 开关打开时的同域库)。同词面只留优先级最高的一份
|
|
797
|
+
// (lexicon-scope 的源序:当前库 → 同域库 → 全局)。
|
|
798
|
+
const foreign = lexiconSources(binding).filter((source) => !source.primary && source.scope === 'private')
|
|
799
|
+
const scopeSuffix = foreign.length ? `,跨库=${foreign.map((source) => source.name || source.libraryId).join(',')}` : ''
|
|
800
|
+
const scopeLabel = `library=${binding.libraryId} + 全局${scopeSuffix}`
|
|
801
|
+
if (!items.length) return `词典(${scopeLabel})没有命中「${truncate(args.query, 80)}」。${warning}`
|
|
802
|
+
const lines = [`词典命中(${scopeLabel},${items.length} 条)`]
|
|
803
|
+
for (const [index, hit] of items.entries()) {
|
|
804
|
+
const e = hit?.entry && typeof hit.entry === 'object' ? hit.entry : hit
|
|
805
|
+
const evidence = (Array.isArray(e.evidence) ? e.evidence : []).slice(0, 2)
|
|
806
|
+
.map((item) => String(item?.quote || item?.id || '')).filter(Boolean).join(' / ')
|
|
807
|
+
const home = hit.lexiconScope === 'global'
|
|
808
|
+
? ' home=全局词典(只读;写入仍落在当前库)'
|
|
809
|
+
: hit.libraryId && hit.libraryId !== binding.libraryId
|
|
810
|
+
? ` home=${hit.libraryName || hit.libraryId}(跨库只读;写入仍落在当前库)`
|
|
811
|
+
: ''
|
|
812
|
+
lines.push([
|
|
813
|
+
`${index + 1}. [${e.term || '?'}] kind=${e.kind || 'term'} status=${e.status || 'active'}`,
|
|
814
|
+
home,
|
|
815
|
+
` meaning=${truncate(e.meaning, 300)}`,
|
|
816
|
+
e.usage ? ` usage=${truncate(e.usage, 200)}` : '',
|
|
817
|
+
evidence ? ` evidence=${truncate(evidence, 200)}` : '',
|
|
818
|
+
].filter(Boolean).join('\n'))
|
|
819
|
+
}
|
|
820
|
+
return redactSecrets(lines.join('\n')) + warning
|
|
821
|
+
},
|
|
822
|
+
{ isConcurrencySafe: () => true },
|
|
823
|
+
))
|
|
824
|
+
|
|
825
|
+
tools.push(toolBase(
|
|
826
|
+
'memu_lexicon_propose',
|
|
827
|
+
'Write one lexicon entry (slang / meme / term) for the library bound to this conversation. The entry becomes ACTIVE immediately: there is no pending state and no human review, and it is injected from the next turn. term is the index key, meaning is the explanation. Search first with memu_lexicon_search and propose only what this conversation evidences, one entry per term. Sensitive triggers cannot be created or updated by the model — those stay user-managed. There is no lexicon delete from the model side.',
|
|
828
|
+
{
|
|
829
|
+
term: { type: 'string', required: true, description: 'The index key / title of the entry: short, stable, spelled the way the user actually writes it (normally 1-12 characters, at most a short phrase). Not a sentence and not the explanation — that goes in meaning.' },
|
|
830
|
+
kind: { type: 'string', required: true, description: 'slang, meme, trigger, or term. trigger is rejected here: sensitive triggers are user-managed.' },
|
|
831
|
+
meaning: { type: 'string', required: true, description: 'The explanation of term: what it means in this library, in one or two sentences.' },
|
|
832
|
+
usage: { type: 'string', description: 'When or how the word is used.' },
|
|
833
|
+
aliases: { type: 'array', items: { type: 'string' }, description: 'Alternative spellings or short forms, at most 20.' },
|
|
834
|
+
examples: { type: 'array', items: { type: 'string' }, description: 'Short real quotes from this conversation showing the usage, at most 8. Never invent quotes.' },
|
|
835
|
+
evidence: { type: 'array', items: { type: 'object', properties: { kind: { type: 'string' }, id: { type: 'string' }, quote: { type: 'string' } }, additionalProperties: true }, description: 'Where the meaning came from: short quotes with stable ids only, no full transcripts.' },
|
|
836
|
+
confidence: { type: 'number', description: '0-1; how sure you are of the meaning. Use it honestly — it sets the entry display strength.' },
|
|
837
|
+
},
|
|
838
|
+
async (args, exec) => {
|
|
839
|
+
const binding = peekBinding(exec)
|
|
840
|
+
if (!binding?.libraryId) return '当前对话没有绑定记忆库,无法写入词条。请先初始化或绑定记忆库。'
|
|
841
|
+
const lexicon = await lexiconApi()
|
|
842
|
+
let result
|
|
843
|
+
try {
|
|
844
|
+
result = await lexicon.upsertLexiconTerm({
|
|
845
|
+
libraryId: binding.libraryId,
|
|
846
|
+
term: args.term,
|
|
847
|
+
kind: args.kind,
|
|
848
|
+
meaning: args.meaning,
|
|
849
|
+
usage: args.usage,
|
|
850
|
+
aliases: args.aliases,
|
|
851
|
+
examples: args.examples,
|
|
852
|
+
// 词条继承当前会话的场景身份;没有场景就是空 context(全场景可见)。
|
|
853
|
+
context: binding.memoryContext || {},
|
|
854
|
+
// 领域戳:跨库召回判「同域」靠它(§8.15),与 privateRemember 同一口径。
|
|
855
|
+
domain: domainOf(binding),
|
|
856
|
+
evidence: args.evidence,
|
|
857
|
+
confidence: args.confidence,
|
|
858
|
+
// 来源固定 model-proposal;普通词条直接 active(§2.1),sensitive trigger 由
|
|
859
|
+
// 核心层 typed 拒绝。
|
|
860
|
+
source: 'model-proposal',
|
|
861
|
+
})
|
|
862
|
+
} catch (error) {
|
|
863
|
+
// typed rejection 翻译:sensitive trigger 不允许模型写入是核心层的硬规则,
|
|
864
|
+
// 原始错误码对模型没有行动价值,翻成"该让用户做什么"。
|
|
865
|
+
const code = String(error?.code || '')
|
|
866
|
+
const message = String(error?.message || error)
|
|
867
|
+
if (/sensitive/i.test(code) || (/sensitive/i.test(message) && /trigger/i.test(message))) {
|
|
868
|
+
return '未写入:sensitive trigger 不能由模型写入或修改。这类触发词请让用户在词典页手动添加。'
|
|
869
|
+
}
|
|
870
|
+
throw error
|
|
871
|
+
}
|
|
872
|
+
// 核心返回 { term: 词条对象(含 termId), created, notOwned? }。
|
|
873
|
+
const termLabel = result?.term && typeof result.term === 'object' ? String(result.term.term || args.term) : String(result?.term || args.term)
|
|
874
|
+
if (result?.created === false) {
|
|
875
|
+
// 没新建:要么词条归用户(归档/墓碑/草稿),要么同名 active 词条被原地更新。
|
|
876
|
+
if (result?.notOwned === true) {
|
|
877
|
+
return `未改动:词条「${termLabel}」已归档或归用户管理,模型不能覆盖它。如需恢复或编辑请让用户在词典页操作。`
|
|
878
|
+
}
|
|
879
|
+
return `已更新现有生效词条「${termLabel}」(status=active);若字段与原来一致则等于没变。sensitive trigger 只能由用户手动管理。`
|
|
880
|
+
}
|
|
881
|
+
return [
|
|
882
|
+
`已写入词条「${termLabel}」(status=active,下一回合起参与注入)。`,
|
|
883
|
+
'词条已自动生效,无需用户审核;sensitive trigger 只能由用户手动添加。',
|
|
884
|
+
].join('\n')
|
|
885
|
+
},
|
|
886
|
+
))
|
|
887
|
+
|
|
888
|
+
tools.push(toolBase(
|
|
889
|
+
'memu_global_recall',
|
|
890
|
+
'Retrieve cross-session global memories applicable to the current domain. Natural queries return snippets; exact gmem- ids (or unique prefixes, optionally .md, no whitespace) read one redacted body page with auto/full. Global memory stays read-only with existing automatic/domain eligibility.',
|
|
891
|
+
{
|
|
892
|
+
query: { type: 'string', required: true, description: 'Natural-language search or exact gmem- id / unique prefix (optional .md, no whitespace).' },
|
|
893
|
+
topK: { type: 'integer', description: 'Search row limit, integer 1..20; default from settings. Exact reads return one entry.' },
|
|
894
|
+
...recallReadParameters(),
|
|
895
|
+
},
|
|
896
|
+
async (args, exec) => {
|
|
897
|
+
const request = parseRecallRequest(args, 'global')
|
|
898
|
+
if (request.exact) {
|
|
899
|
+
const page = readMemoryByRef({ exec, scope: 'global', id: request.query,
|
|
900
|
+
offset: request.offset, limitChars: request.limitChars, revision: request.revision })
|
|
901
|
+
return renderMemoryPage(page, request)
|
|
902
|
+
}
|
|
903
|
+
const result = await globalRecall({ exec, query: request.query, topK: request.topK, purpose: 'explicit' })
|
|
904
|
+
const warning = (result.errors || []).length ? `\n注意:本次召回已降级:${(result.errors || []).join('; ')}` : ''
|
|
905
|
+
if (!result.files?.length) return `全局记忆(domain=${result.domain})没有命中。${warning}`
|
|
906
|
+
return [
|
|
907
|
+
`全局记忆(domain=${result.domain})`,
|
|
908
|
+
...result.files.map((f, i) => formatGlobalItem(f, i + 1, { query: request.query })),
|
|
909
|
+
].join('\n\n') + warning
|
|
910
|
+
},
|
|
911
|
+
{ isConcurrencySafe: () => true },
|
|
912
|
+
))
|
|
913
|
+
|
|
914
|
+
tools.push(toolBase(
|
|
915
|
+
'memu_global_list',
|
|
916
|
+
'List global memory metadata. Global memory is read-only for the main agent.',
|
|
917
|
+
{ domain: { type: 'string', description: 'Optional domain filter.' } },
|
|
918
|
+
async (args, exec) => {
|
|
919
|
+
const result = await globalList({ exec, domain: args.domain })
|
|
920
|
+
if (!result.files?.length) return '全局记忆为空。'
|
|
921
|
+
const lines = [`全局记忆(${result.files.length} 条)`]
|
|
922
|
+
for (const file of result.files) {
|
|
923
|
+
const e = file.entry || {}
|
|
924
|
+
lines.push(`- [${file.name}] ${e.title || file.description || '(untitled)'} · ${e.domain || 'unknown'} · ${e.status || 'active'}`)
|
|
925
|
+
}
|
|
926
|
+
return lines.join('\n')
|
|
927
|
+
},
|
|
928
|
+
{ isConcurrencySafe: () => true },
|
|
929
|
+
))
|
|
930
|
+
|
|
931
|
+
tools.push(toolBase(
|
|
932
|
+
'memu_status',
|
|
933
|
+
'Show MemU engine, memory library, session binding, and injection status. Use it when memory is unavailable or the user asks what you remember.',
|
|
934
|
+
{},
|
|
935
|
+
async (_args, exec) => {
|
|
936
|
+
const engine = engineStatus()
|
|
937
|
+
// Status is a read: never let answering "what do you remember" create a
|
|
938
|
+
// binding (and potentially a placeholder library) for an unbound chat.
|
|
939
|
+
const binding = peekBinding(exec)
|
|
940
|
+
const settings = getSettings()
|
|
941
|
+
const libraries = listLibraries()
|
|
942
|
+
const library = binding?.libraryId ? getLibrary(binding.libraryId) : null
|
|
943
|
+
const privateIndex = binding?.libraryId ? loadPrivateIndex(binding.libraryId) : { entries: {} }
|
|
944
|
+
const globalIndex = loadGlobalIndex()
|
|
945
|
+
const pinned = Object.values(privateIndex.entries || {}).filter((e) => e.layer === 'must_remember' && e.status !== 'archived').length
|
|
946
|
+
const activeEntries = Object.values(privateIndex.entries || {}).filter((e) => !['archived', 'suppressed', 'expired'].includes(e.status))
|
|
947
|
+
const shortCount = activeEntries.filter((e) => e.layer === 'short_term').length
|
|
948
|
+
const globalCount = Object.keys(globalIndex.entries || {}).filter((id) => ['active'].includes(globalIndex.entries[id].status || 'active')).length
|
|
949
|
+
const health = storageHealth()
|
|
950
|
+
const storageLines = []
|
|
951
|
+
if (health.issues.length > 0) {
|
|
952
|
+
storageLines.push(`storage: ${health.issues.length} unreadable file(s), writes blocked — ${health.issues.map((i) => `${i.file} (${i.code || i.message})`).join('; ')}`)
|
|
953
|
+
}
|
|
954
|
+
if (health.quarantined.length > 0) {
|
|
955
|
+
storageLines.push(`storage: quarantined copies kept — ${health.quarantined.join(', ')}`)
|
|
956
|
+
}
|
|
957
|
+
const recallLines = binding?.lastRecallError
|
|
958
|
+
? [`recall: degraded at ${binding.lastRecallErrorAt || '?'} — ${binding.lastRecallError}`]
|
|
959
|
+
: []
|
|
960
|
+
// Separates "nothing was written" from "the panes were never told": a write
|
|
961
|
+
// with no heartbeat means no surface is polling; a heartbeat after a write
|
|
962
|
+
// means the write was visible within one beat.
|
|
963
|
+
// 显式报告向量代际缺库,为 no-database 降级提供诊断线索。
|
|
964
|
+
let vectorMissingLine = ''
|
|
965
|
+
try {
|
|
966
|
+
const vectorMissing = getMissingVectorLibraries()
|
|
967
|
+
if (vectorMissing.length) {
|
|
968
|
+
vectorMissingLine = `vector_missing: ${vectorMissing.join(', ')} — semantic recall is local-only for these libraries; a forced vector rebuild (Settings → MemU → 本地向量 · force 切换) will backfill them`
|
|
969
|
+
}
|
|
970
|
+
} catch { }
|
|
971
|
+
let maintenanceLine = ''
|
|
972
|
+
if (binding?.libraryId) {
|
|
973
|
+
try {
|
|
974
|
+
const queue = maintenanceStatus({ libraryId: binding.libraryId })
|
|
975
|
+
const reasons = Object.entries(queue.blockedReasons).map(([reason, count]) => reason + ':' + count).join(', ')
|
|
976
|
+
maintenanceLine = 'maintenance: pending=' + queue.pending + ', deferred=' + queue.deferred + ', blocked=' + queue.blocked + ', inflight=' + queue.inflight + ', retry=' + queue.retry + ', next=' + (queue.nextReviewAt || 'none') + (reasons ? ' (' + reasons + ')' : '')
|
|
977
|
+
} catch (error) { maintenanceLine = 'maintenance: degraded — ' + redactSecrets(String(error?.code || 'ledger unreadable')) }
|
|
978
|
+
}
|
|
979
|
+
const sync = syncActivity()
|
|
980
|
+
const ago = (at) => {
|
|
981
|
+
if (!at) return 'never'
|
|
982
|
+
const seconds = Math.max(0, Math.round((Date.now() - at) / 1000))
|
|
983
|
+
if (seconds < 120) return `${seconds}s ago`
|
|
984
|
+
const minutes = Math.round(seconds / 60)
|
|
985
|
+
return minutes < 120 ? `${minutes}m ago` : `${Math.round(minutes / 60)}h ago`
|
|
986
|
+
}
|
|
987
|
+
const write = sync.lastWrite
|
|
988
|
+
const syncLine = `sync: heartbeat ${ago(sync.lastBeatAt)}, last write ${write ? `${write.kind}${write.libraryId ? `@${write.libraryId}` : ''} ${ago(write.at)}` : 'none since startup'}`
|
|
989
|
+
return [
|
|
990
|
+
`engine: ${engine.status} (recommended ${ENGINE_VERSION}, current ${engine.current?.version || 'none'})`,
|
|
991
|
+
`python: ${engine.current?.python || 'none'}`,
|
|
992
|
+
`tools: ${registrationReport.registered}/${registrationReport.attempted} registered${registrationReport.failures.length ? ` (refused: ${registrationReport.failures.map((failure) => failure.name).join(', ')})` : ''}`,
|
|
993
|
+
`session: ${sessionIdOf(exec) || '?'}`,
|
|
994
|
+
`library: ${library ? `${library.name} (${library.id})` : 'none'}`,
|
|
995
|
+
`domain: ${domainOf(binding)}`,
|
|
996
|
+
`useLibrary=${binding?.useLibrary !== false} useGlobal=${binding?.useGlobal !== false} autoInject=${settings.autoInject}`,
|
|
997
|
+
`private: ${activeEntries.length} active, ${pinned}/${settings.mustRememberCap} pinned`,
|
|
998
|
+
`short_term: ${shortCount} active (ttl ${Number(settings.shortTermTtlHours) || 0}h)`,
|
|
999
|
+
`global: ${globalCount} active`,
|
|
1000
|
+
`libraries: ${libraries.length}`,
|
|
1001
|
+
...storageLines,
|
|
1002
|
+
...recallLines,
|
|
1003
|
+
...(maintenanceLine ? [maintenanceLine] : []),
|
|
1004
|
+
...(vectorMissingLine ? [vectorMissingLine] : []),
|
|
1005
|
+
syncLine,
|
|
1006
|
+
binding?.inbound || binding?.lastInboundOutcome ? `inbound: pending=${binding.inbound?.pending?.length || 0}, logical=${binding.inbound?.clock || 0}, last=${binding.lastInboundOutcome?.reason || 'none'}` : '',
|
|
1007
|
+
binding?.lastInboundError ? `inbound_error: ${redactSecrets(binding.lastInboundError)}` : '',
|
|
1008
|
+
binding?.lastRecallOutcome ? `recall_outcome: ${binding.lastRecallOutcome.reason}, candidates=${binding.lastRecallOutcome.candidateCount}, injected=${binding.lastRecallOutcome.injectedCount}` : '',
|
|
1009
|
+
binding?.lastImageCount ? `image_turn: ${binding.lastImageCount} image(s); pending ${(binding.pendingMedia || []).length}` : '',
|
|
1010
|
+
engine.status !== 'ready'
|
|
1011
|
+
? 'warning: engine is not ready; run `npx dsh-memu-memory-setup` or open Settings → MemU.'
|
|
1012
|
+
: `injection: smart, privateTopK=${settings.privateTopK}, globalTopK=${settings.globalTopK}`,
|
|
1013
|
+
].join('\n')
|
|
1014
|
+
},
|
|
1015
|
+
{ isConcurrencySafe: () => true },
|
|
1016
|
+
))
|
|
1017
|
+
|
|
1018
|
+
// 整理 worker 的挂载面(private.js 的 fork 兼容):只挂 WORKER_WRITE_TOOLS +
|
|
1019
|
+
// WORKER_READ_TOOLS(记忆读写 + 关联裁决/候选 + 词典检索/写入),不把
|
|
1020
|
+
// archive/delete/unpin 这些破坏性工具暴露给一个后台子会话。
|
|
1021
|
+
const wanted = Array.isArray(only) && only.length > 0 ? only : null
|
|
1022
|
+
const usable = tools.filter((tool) => tool && (!wanted || wanted.includes(tool.name)))
|
|
1023
|
+
if (only) {
|
|
1024
|
+
registrationReport = reportSaved
|
|
1025
|
+
if (usable.length !== wanted.length) {
|
|
1026
|
+
try {
|
|
1027
|
+
ctx?.logger?.warn?.(`dsh-memu-memory: worker tool mount compiled ${usable.length}/${wanted.length}: ${wanted.join(', ')}`)
|
|
1028
|
+
} catch {}
|
|
1029
|
+
}
|
|
1030
|
+
} else {
|
|
1031
|
+
registrationReport.registered = usable.length
|
|
1032
|
+
if (registrationReport.failures.length > 0) {
|
|
1033
|
+
try {
|
|
1034
|
+
ctx?.logger?.warn?.(`dsh-memu-memory: host schema DSL refused ${registrationReport.failures.length} of ${registrationReport.attempted} tools: ${registrationReport.failures.map((failure) => `${failure.name} (${failure.message})`).join('; ')}`)
|
|
1035
|
+
} catch {}
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
// 每项注册独立 fail-open;宿主拒绝单个工具时,仍保留已成功注册工具的 disposer,
|
|
1039
|
+
// 使 worker scope 能完整卸载这些注册。
|
|
1040
|
+
const disposers = []
|
|
1041
|
+
const attached = []
|
|
1042
|
+
const refused = []
|
|
1043
|
+
for (const tool of usable) {
|
|
1044
|
+
try {
|
|
1045
|
+
const dispose = ctx.tools.register(tool)
|
|
1046
|
+
disposers.push(typeof dispose === 'function' ? dispose : null)
|
|
1047
|
+
attached.push(tool.name)
|
|
1048
|
+
} catch (error) {
|
|
1049
|
+
refused.push(`${tool.name} (${String(error?.message || error)})`)
|
|
1050
|
+
// 全量装载(宿主)才进 registrationReport:memu_status 的 refused 清单要如实反映
|
|
1051
|
+
// 编译 + register 两道关都被拒了谁;worker 的按需挂载照旧不碰全局报告。
|
|
1052
|
+
if (!only) registrationReport.failures.push({ name: tool.name, message: String(error?.message || error) })
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
if (refused.length > 0) {
|
|
1056
|
+
if (!only) registrationReport.registered = attached.length
|
|
1057
|
+
try {
|
|
1058
|
+
ctx?.logger?.warn?.(`dsh-memu-memory: tool mount refused ${refused.length}/${usable.length} at register: ${refused.join('; ')}`)
|
|
1059
|
+
} catch {}
|
|
1060
|
+
}
|
|
1061
|
+
// 返回值仍是可直接调用的 disposer(private.js attachWorkerTools 与宿主装载都这么用),
|
|
1062
|
+
// 但附挂结构化名单:attached = 编译 + register 都真正成功的工具名;requested = 请求
|
|
1063
|
+
// 名单(宿主全量装载没有 only,为 null)。部分编译/注册失败时调用方据此写活动日志,
|
|
1064
|
+
// 不再把 wanted 全量误报成已挂载。
|
|
1065
|
+
const disposeAll = () => {
|
|
1066
|
+
for (const dispose of disposers) {
|
|
1067
|
+
try { dispose?.() } catch {}
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
disposeAll.attached = attached
|
|
1071
|
+
disposeAll.requested = wanted
|
|
1072
|
+
return disposeAll
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/**
|
|
1076
|
+
* 工具规格快照,给无宿主调用方用(MCP server / 移植适配器 / 脚本)。
|
|
1077
|
+
*
|
|
1078
|
+
* 用一个只收集、不注册的假 ctx 跑一遍 `registerTools`,拿到的就是宿主本来会看到
|
|
1079
|
+
* 的那批 defineTool 对象:`name` / `description` / `parameters`(已是 JSON Schema)
|
|
1080
|
+
* / `execute(args, exec)`。MCP 侧把 `parameters` 直接当 `inputSchema`、`execute(args, {})`
|
|
1081
|
+
* 即可调用各工具,无需重复实现工具定义。
|
|
1082
|
+
*
|
|
1083
|
+
* 不碰任何既有注册路径;跑完把模块级 `registrationReport` 还原,所以 `memu_status`
|
|
1084
|
+
* 的注册清单不会因为「有人问了一次规格」而变。
|
|
1085
|
+
*/
|
|
1086
|
+
export function collectToolSpecs() {
|
|
1087
|
+
const saved = registrationReport
|
|
1088
|
+
const collected = []
|
|
1089
|
+
try {
|
|
1090
|
+
registerTools({
|
|
1091
|
+
tools: { register: (tool) => { collected.push(tool); return () => {} } },
|
|
1092
|
+
logger: { warn() {} },
|
|
1093
|
+
})
|
|
1094
|
+
} finally {
|
|
1095
|
+
registrationReport = saved
|
|
1096
|
+
}
|
|
1097
|
+
return collected
|
|
1098
|
+
}
|