@afaik-lab/weigh 0.0.0-stage → 0.6.0
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/.env.example +49 -0
- package/LICENSE +373 -0
- package/README.md +102 -2
- package/dist/agent.d.ts +83 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +204 -0
- package/dist/agent.js.map +1 -0
- package/dist/ceilings.d.ts +32 -0
- package/dist/ceilings.d.ts.map +1 -0
- package/dist/ceilings.js +61 -0
- package/dist/ceilings.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +530 -0
- package/dist/cli.js.map +1 -0
- package/dist/config-cli.d.ts +15 -0
- package/dist/config-cli.d.ts.map +1 -0
- package/dist/config-cli.js +132 -0
- package/dist/config-cli.js.map +1 -0
- package/dist/config.d.ts +198 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +373 -0
- package/dist/config.js.map +1 -0
- package/dist/context-cli.d.ts +35 -0
- package/dist/context-cli.d.ts.map +1 -0
- package/dist/context-cli.js +251 -0
- package/dist/context-cli.js.map +1 -0
- package/dist/context.d.ts +118 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +159 -0
- package/dist/context.js.map +1 -0
- package/dist/corrections/correction-set.d.ts +67 -0
- package/dist/corrections/correction-set.d.ts.map +1 -0
- package/dist/corrections/correction-set.js +104 -0
- package/dist/corrections/correction-set.js.map +1 -0
- package/dist/corrections/correction.d.ts +74 -0
- package/dist/corrections/correction.d.ts.map +1 -0
- package/dist/corrections/correction.js +154 -0
- package/dist/corrections/correction.js.map +1 -0
- package/dist/corrections/follow-up.d.ts +69 -0
- package/dist/corrections/follow-up.d.ts.map +1 -0
- package/dist/corrections/follow-up.js +217 -0
- package/dist/corrections/follow-up.js.map +1 -0
- package/dist/corrections/index.d.ts +31 -0
- package/dist/corrections/index.d.ts.map +1 -0
- package/dist/corrections/index.js +31 -0
- package/dist/corrections/index.js.map +1 -0
- package/dist/corrections/ledger.d.ts +78 -0
- package/dist/corrections/ledger.d.ts.map +1 -0
- package/dist/corrections/ledger.js +117 -0
- package/dist/corrections/ledger.js.map +1 -0
- package/dist/corrections/promotion.d.ts +119 -0
- package/dist/corrections/promotion.d.ts.map +1 -0
- package/dist/corrections/promotion.js +221 -0
- package/dist/corrections/promotion.js.map +1 -0
- package/dist/corrections/schema.d.ts +125 -0
- package/dist/corrections/schema.d.ts.map +1 -0
- package/dist/corrections/schema.js +100 -0
- package/dist/corrections/schema.js.map +1 -0
- package/dist/corrections-cli.d.ts +28 -0
- package/dist/corrections-cli.d.ts.map +1 -0
- package/dist/corrections-cli.js +157 -0
- package/dist/corrections-cli.js.map +1 -0
- package/dist/diff.d.ts +105 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +202 -0
- package/dist/diff.js.map +1 -0
- package/dist/doctor.d.ts +38 -0
- package/dist/doctor.d.ts.map +1 -0
- package/dist/doctor.js +197 -0
- package/dist/doctor.js.map +1 -0
- package/dist/events.d.ts +21 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +5 -0
- package/dist/events.js.map +1 -0
- package/dist/findings.d.ts +510 -0
- package/dist/findings.d.ts.map +1 -0
- package/dist/findings.js +645 -0
- package/dist/findings.js.map +1 -0
- package/dist/follow-up-cli.d.ts +88 -0
- package/dist/follow-up-cli.d.ts.map +1 -0
- package/dist/follow-up-cli.js +208 -0
- package/dist/follow-up-cli.js.map +1 -0
- package/dist/harness.d.ts +64 -0
- package/dist/harness.d.ts.map +1 -0
- package/dist/harness.js +71 -0
- package/dist/harness.js.map +1 -0
- package/dist/help/command-help.d.ts +9 -0
- package/dist/help/command-help.d.ts.map +1 -0
- package/dist/help/command-help.js +21 -0
- package/dist/help/command-help.js.map +1 -0
- package/dist/help/commands/answer.d.ts +3 -0
- package/dist/help/commands/answer.d.ts.map +1 -0
- package/dist/help/commands/answer.js +39 -0
- package/dist/help/commands/answer.js.map +1 -0
- package/dist/help/commands/config.d.ts +3 -0
- package/dist/help/commands/config.d.ts.map +1 -0
- package/dist/help/commands/config.js +25 -0
- package/dist/help/commands/config.js.map +1 -0
- package/dist/help/commands/context.d.ts +3 -0
- package/dist/help/commands/context.d.ts.map +1 -0
- package/dist/help/commands/context.js +44 -0
- package/dist/help/commands/context.js.map +1 -0
- package/dist/help/commands/corrections.d.ts +3 -0
- package/dist/help/commands/corrections.d.ts.map +1 -0
- package/dist/help/commands/corrections.js +30 -0
- package/dist/help/commands/corrections.js.map +1 -0
- package/dist/help/commands/doctor.d.ts +3 -0
- package/dist/help/commands/doctor.d.ts.map +1 -0
- package/dist/help/commands/doctor.js +12 -0
- package/dist/help/commands/doctor.js.map +1 -0
- package/dist/help/commands/index.d.ts +4 -0
- package/dist/help/commands/index.d.ts.map +1 -0
- package/dist/help/commands/index.js +27 -0
- package/dist/help/commands/index.js.map +1 -0
- package/dist/help/commands/memory.d.ts +3 -0
- package/dist/help/commands/memory.d.ts.map +1 -0
- package/dist/help/commands/memory.js +33 -0
- package/dist/help/commands/memory.js.map +1 -0
- package/dist/help/commands/models.d.ts +3 -0
- package/dist/help/commands/models.d.ts.map +1 -0
- package/dist/help/commands/models.js +17 -0
- package/dist/help/commands/models.js.map +1 -0
- package/dist/help/commands/options.d.ts +4 -0
- package/dist/help/commands/options.d.ts.map +1 -0
- package/dist/help/commands/options.js +13 -0
- package/dist/help/commands/options.js.map +1 -0
- package/dist/help/commands/review.d.ts +3 -0
- package/dist/help/commands/review.d.ts.map +1 -0
- package/dist/help/commands/review.js +50 -0
- package/dist/help/commands/review.js.map +1 -0
- package/dist/help/commands/sessions.d.ts +3 -0
- package/dist/help/commands/sessions.d.ts.map +1 -0
- package/dist/help/commands/sessions.js +23 -0
- package/dist/help/commands/sessions.js.map +1 -0
- package/dist/help/commands/show.d.ts +3 -0
- package/dist/help/commands/show.d.ts.map +1 -0
- package/dist/help/commands/show.js +18 -0
- package/dist/help/commands/show.js.map +1 -0
- package/dist/help/help.d.ts +16 -0
- package/dist/help/help.d.ts.map +1 -0
- package/dist/help/help.js +68 -0
- package/dist/help/help.js.map +1 -0
- package/dist/help/index.d.ts +4 -0
- package/dist/help/index.d.ts.map +1 -0
- package/dist/help/index.js +7 -0
- package/dist/help/index.js.map +1 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/dist/knowledge/block.d.ts +65 -0
- package/dist/knowledge/block.d.ts.map +1 -0
- package/dist/knowledge/block.js +89 -0
- package/dist/knowledge/block.js.map +1 -0
- package/dist/knowledge/claim.d.ts +143 -0
- package/dist/knowledge/claim.d.ts.map +1 -0
- package/dist/knowledge/claim.js +297 -0
- package/dist/knowledge/claim.js.map +1 -0
- package/dist/knowledge/index.d.ts +32 -0
- package/dist/knowledge/index.d.ts.map +1 -0
- package/dist/knowledge/index.js +29 -0
- package/dist/knowledge/index.js.map +1 -0
- package/dist/knowledge/novelty.d.ts +65 -0
- package/dist/knowledge/novelty.d.ts.map +1 -0
- package/dist/knowledge/novelty.js +94 -0
- package/dist/knowledge/novelty.js.map +1 -0
- package/dist/knowledge/recall.d.ts +134 -0
- package/dist/knowledge/recall.d.ts.map +1 -0
- package/dist/knowledge/recall.js +229 -0
- package/dist/knowledge/recall.js.map +1 -0
- package/dist/knowledge/recalled-claim.d.ts +35 -0
- package/dist/knowledge/recalled-claim.d.ts.map +1 -0
- package/dist/knowledge/recalled-claim.js +67 -0
- package/dist/knowledge/recalled-claim.js.map +1 -0
- package/dist/knowledge/recalled.d.ts +70 -0
- package/dist/knowledge/recalled.d.ts.map +1 -0
- package/dist/knowledge/recalled.js +149 -0
- package/dist/knowledge/recalled.js.map +1 -0
- package/dist/knowledge/schema.d.ts +83 -0
- package/dist/knowledge/schema.d.ts.map +1 -0
- package/dist/knowledge/schema.js +96 -0
- package/dist/knowledge/schema.js.map +1 -0
- package/dist/knowledge/staleness.d.ts +48 -0
- package/dist/knowledge/staleness.d.ts.map +1 -0
- package/dist/knowledge/staleness.js +85 -0
- package/dist/knowledge/staleness.js.map +1 -0
- package/dist/knowledge/store.d.ts +100 -0
- package/dist/knowledge/store.d.ts.map +1 -0
- package/dist/knowledge/store.js +184 -0
- package/dist/knowledge/store.js.map +1 -0
- package/dist/memory/compose-file.d.ts +14 -0
- package/dist/memory/compose-file.d.ts.map +1 -0
- package/dist/memory/compose-file.js +25 -0
- package/dist/memory/compose-file.js.map +1 -0
- package/dist/memory/index.d.ts +43 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +119 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/oss.d.ts +62 -0
- package/dist/memory/oss.d.ts.map +1 -0
- package/dist/memory/oss.js +230 -0
- package/dist/memory/oss.js.map +1 -0
- package/dist/memory/platform.d.ts +37 -0
- package/dist/memory/platform.d.ts.map +1 -0
- package/dist/memory/platform.js +131 -0
- package/dist/memory/platform.js.map +1 -0
- package/dist/memory/scope.d.ts +49 -0
- package/dist/memory/scope.d.ts.map +1 -0
- package/dist/memory/scope.js +70 -0
- package/dist/memory/scope.js.map +1 -0
- package/dist/memory/types.d.ts +113 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +18 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/memory-cli.d.ts +17 -0
- package/dist/memory-cli.d.ts.map +1 -0
- package/dist/memory-cli.js +107 -0
- package/dist/memory-cli.js.map +1 -0
- package/dist/models-cli.d.ts +12 -0
- package/dist/models-cli.d.ts.map +1 -0
- package/dist/models-cli.js +54 -0
- package/dist/models-cli.js.map +1 -0
- package/dist/models.d.ts +111 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +227 -0
- package/dist/models.js.map +1 -0
- package/dist/paths.d.ts +35 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +65 -0
- package/dist/paths.js.map +1 -0
- package/dist/profile.d.ts +28 -0
- package/dist/profile.d.ts.map +1 -0
- package/dist/profile.js +154 -0
- package/dist/profile.js.map +1 -0
- package/dist/progress.d.ts +70 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +132 -0
- package/dist/progress.js.map +1 -0
- package/dist/quiet.d.ts +2 -0
- package/dist/quiet.d.ts.map +1 -0
- package/dist/quiet.js +21 -0
- package/dist/quiet.js.map +1 -0
- package/dist/reconcile/continuation.d.ts +132 -0
- package/dist/reconcile/continuation.d.ts.map +1 -0
- package/dist/reconcile/continuation.js +306 -0
- package/dist/reconcile/continuation.js.map +1 -0
- package/dist/reconcile/index.d.ts +26 -0
- package/dist/reconcile/index.d.ts.map +1 -0
- package/dist/reconcile/index.js +28 -0
- package/dist/reconcile/index.js.map +1 -0
- package/dist/reconcile/outcome-set.d.ts +53 -0
- package/dist/reconcile/outcome-set.d.ts.map +1 -0
- package/dist/reconcile/outcome-set.js +124 -0
- package/dist/reconcile/outcome-set.js.map +1 -0
- package/dist/reconcile/outcome.d.ts +56 -0
- package/dist/reconcile/outcome.d.ts.map +1 -0
- package/dist/reconcile/outcome.js +60 -0
- package/dist/reconcile/outcome.js.map +1 -0
- package/dist/reconcile/reconciliation.d.ts +49 -0
- package/dist/reconcile/reconciliation.d.ts.map +1 -0
- package/dist/reconcile/reconciliation.js +88 -0
- package/dist/reconcile/reconciliation.js.map +1 -0
- package/dist/reconcile/schema.d.ts +157 -0
- package/dist/reconcile/schema.d.ts.map +1 -0
- package/dist/reconcile/schema.js +97 -0
- package/dist/reconcile/schema.js.map +1 -0
- package/dist/record/index.d.ts +23 -0
- package/dist/record/index.d.ts.map +1 -0
- package/dist/record/index.js +25 -0
- package/dist/record/index.js.map +1 -0
- package/dist/record/review-record.d.ts +28 -0
- package/dist/record/review-record.d.ts.map +1 -0
- package/dist/record/review-record.js +78 -0
- package/dist/record/review-record.js.map +1 -0
- package/dist/record/schema.d.ts +153 -0
- package/dist/record/schema.d.ts.map +1 -0
- package/dist/record/schema.js +87 -0
- package/dist/record/schema.js.map +1 -0
- package/dist/record/store.d.ts +83 -0
- package/dist/record/store.d.ts.map +1 -0
- package/dist/record/store.js +162 -0
- package/dist/record/store.js.map +1 -0
- package/dist/record/stored-review.d.ts +36 -0
- package/dist/record/stored-review.d.ts.map +1 -0
- package/dist/record/stored-review.js +90 -0
- package/dist/record/stored-review.js.map +1 -0
- package/dist/refute.d.ts +318 -0
- package/dist/refute.d.ts.map +1 -0
- package/dist/refute.js +513 -0
- package/dist/refute.js.map +1 -0
- package/dist/render.d.ts +212 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +544 -0
- package/dist/render.js.map +1 -0
- package/dist/review-cli.d.ts +181 -0
- package/dist/review-cli.d.ts.map +1 -0
- package/dist/review-cli.js +598 -0
- package/dist/review-cli.js.map +1 -0
- package/dist/reviewer.d.ts +297 -0
- package/dist/reviewer.d.ts.map +1 -0
- package/dist/reviewer.js +580 -0
- package/dist/reviewer.js.map +1 -0
- package/dist/session-cli.d.ts +17 -0
- package/dist/session-cli.d.ts.map +1 -0
- package/dist/session-cli.js +62 -0
- package/dist/session-cli.js.map +1 -0
- package/dist/session.d.ts +36 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +105 -0
- package/dist/session.js.map +1 -0
- package/dist/settings.d.ts +68 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/settings.js +175 -0
- package/dist/settings.js.map +1 -0
- package/dist/show-cli.d.ts +39 -0
- package/dist/show-cli.d.ts.map +1 -0
- package/dist/show-cli.js +56 -0
- package/dist/show-cli.js.map +1 -0
- package/dist/stdout.d.ts +28 -0
- package/dist/stdout.d.ts.map +1 -0
- package/dist/stdout.js +44 -0
- package/dist/stdout.js.map +1 -0
- package/dist/target.d.ts +58 -0
- package/dist/target.d.ts.map +1 -0
- package/dist/target.js +96 -0
- package/dist/target.js.map +1 -0
- package/dist/tools/index.d.ts +93 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +146 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/root.d.ts +62 -0
- package/dist/tools/root.d.ts.map +1 -0
- package/dist/tools/root.js +51 -0
- package/dist/tools/root.js.map +1 -0
- package/dist/usage.d.ts +75 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +100 -0
- package/dist/usage.js.map +1 -0
- package/dist/version.d.ts +14 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +26 -0
- package/dist/version.js.map +1 -0
- package/dist/workspace.d.ts +48 -0
- package/dist/workspace.d.ts.map +1 -0
- package/dist/workspace.js +126 -0
- package/dist/workspace.js.map +1 -0
- package/docker-compose.yml +61 -0
- package/package.json +70 -4
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
import { spawnSync } from 'child_process';
|
|
5
|
+
/**
|
|
6
|
+
* Which claims cite code that changed since they were last known to hold.
|
|
7
|
+
*
|
|
8
|
+
* A refresh cannot re-read every claim once a repository holds hundreds, and
|
|
9
|
+
* most of them cite files nobody touched. This narrows it to the ones whose
|
|
10
|
+
* evidence moved, by asking git, so the model reads only those.
|
|
11
|
+
*
|
|
12
|
+
* One `git diff` per distinct commit rather than one per claim. A priming run
|
|
13
|
+
* stamps every claim it writes with the same commit, so a store of a thousand
|
|
14
|
+
* claims written in five runs costs five diffs.
|
|
15
|
+
*
|
|
16
|
+
* The diff is from that commit to HEAD, commit to commit, because a stamp
|
|
17
|
+
* can only name a commit: a claim confirmed over uncommitted edits would
|
|
18
|
+
* name code nobody read. `uncommitted()` is how `context confirm` refuses
|
|
19
|
+
* those. Renames are off, so a cited file that was moved away shows up under
|
|
20
|
+
* the name the claim cites rather than only under its new one.
|
|
21
|
+
*
|
|
22
|
+
* What this cannot see: a claim that went wrong while its own files stayed the
|
|
23
|
+
* same, such as a convention that new code elsewhere stopped following.
|
|
24
|
+
* Reviews catch those, when a finding contradicts the claim and someone rules
|
|
25
|
+
* on it.
|
|
26
|
+
*/
|
|
27
|
+
export class Staleness {
|
|
28
|
+
_root;
|
|
29
|
+
/** Files changed since each commit; null for a commit git does not know. */
|
|
30
|
+
_changes = new Map();
|
|
31
|
+
constructor(_root) {
|
|
32
|
+
this._root = _root;
|
|
33
|
+
}
|
|
34
|
+
/** The claims worth reading again, in the order given. */
|
|
35
|
+
check(claims) {
|
|
36
|
+
const stale = [];
|
|
37
|
+
for (const claim of claims) {
|
|
38
|
+
const commit = claim.knownAt;
|
|
39
|
+
if (!commit) {
|
|
40
|
+
stale.push({ claim, reason: 'unstamped', changed: [] });
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const changes = this._changedSince(commit);
|
|
44
|
+
if (!changes) {
|
|
45
|
+
stale.push({ claim, reason: 'unknown-commit', changed: [] });
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const changed = claim.cites.filter((path) => changes.has(path));
|
|
49
|
+
if (changed.length)
|
|
50
|
+
stale.push({ claim, reason: 'changed', changed });
|
|
51
|
+
}
|
|
52
|
+
return stale;
|
|
53
|
+
}
|
|
54
|
+
/** One line saying why, for the text listing. */
|
|
55
|
+
static describe(stale) {
|
|
56
|
+
switch (stale.reason) {
|
|
57
|
+
case 'changed':
|
|
58
|
+
return `changed since ${stale.claim.knownAt.slice(0, 7)}: ${stale.changed.join(', ')}`;
|
|
59
|
+
case 'unstamped':
|
|
60
|
+
return 'no commit recorded, so nothing says when it last held';
|
|
61
|
+
case 'unknown-commit':
|
|
62
|
+
return `commit ${stale.claim.knownAt.slice(0, 7)} is not in this repository (rewritten or not fetched)`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Tracked files whose working copy differs from HEAD, staged or not. */
|
|
66
|
+
uncommitted() {
|
|
67
|
+
const diff = this._git(['diff', ...Staleness._NAMES, 'HEAD', '--']);
|
|
68
|
+
return new Set(diff.ok ? diff.out.split('\n').filter(Boolean) : []);
|
|
69
|
+
}
|
|
70
|
+
static _NAMES = ['--name-only', '--no-renames', '--no-ext-diff', '--no-textconv'];
|
|
71
|
+
_changedSince(commit) {
|
|
72
|
+
if (this._changes.has(commit))
|
|
73
|
+
return this._changes.get(commit);
|
|
74
|
+
const known = this._git(['rev-parse', '--verify', '--quiet', `${commit}^{commit}`]);
|
|
75
|
+
const diff = known.ok ? this._git(['diff', ...Staleness._NAMES, commit, 'HEAD', '--']) : undefined;
|
|
76
|
+
const changes = diff?.ok ? new Set(diff.out.split('\n').filter(Boolean)) : null;
|
|
77
|
+
this._changes.set(commit, changes);
|
|
78
|
+
return changes;
|
|
79
|
+
}
|
|
80
|
+
_git(args) {
|
|
81
|
+
const proc = spawnSync('git', args, { cwd: this._root, encoding: 'utf-8', maxBuffer: 64 * 1024 * 1024 });
|
|
82
|
+
return { ok: proc.status === 0, out: (proc.stdout ?? '').trim() };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=staleness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"staleness.js","sourceRoot":"","sources":["../../src/knowledge/staleness.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,sEAAsE;AACtE,4DAA4D;AAE5D,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAc1C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,OAAO,SAAS;IAIS,KAAK;IAHlC,4EAA4E;IAC3D,QAAQ,GAAG,IAAI,GAAG,EAAsC,CAAC;IAE1E,YAA6B,KAAa;qBAAb,KAAK;IAAW,CAAC;IAE9C,0DAA0D;IAC1D,KAAK,CAAC,MAAwB;QAC5B,MAAM,KAAK,GAAiB,EAAE,CAAC;QAC/B,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC;YAC7B,IAAI,CAAC,MAAM,EAAE,CAAC;gBAC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AE,GAAG,EAAE,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,GAAG,IAAI,GAAG,IAAI,EAAE,CAAC,CAAC;QACzG,OAAO,EAAE,EAAE,EAAE,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;IACpE,CAAC;CACF"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { AgentConfig } from '../config.js';
|
|
2
|
+
import type { MemoryStore } from '../memory/index.js';
|
|
3
|
+
import type { Scope } from '../memory/scope.js';
|
|
4
|
+
import { Claim } from './claim.js';
|
|
5
|
+
/** What `Knowledge.confirm` stamps, and what it refuses. */
|
|
6
|
+
export interface ConfirmAt {
|
|
7
|
+
/** The commit the claims were found to hold at. */
|
|
8
|
+
commit: string;
|
|
9
|
+
now?: Date;
|
|
10
|
+
/** A reason to refuse one claim, or nothing to accept it. */
|
|
11
|
+
check?: (claim: Claim) => string | undefined;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Repository knowledge, over whichever memory backend is configured.
|
|
15
|
+
*
|
|
16
|
+
* Deliberately narrow: typed claims in, typed claims out, one scope at a time.
|
|
17
|
+
* The store underneath takes free text and will remember anything — this is the
|
|
18
|
+
* only thing allowed to write knowledge into it, so that there is one schema and
|
|
19
|
+
* one place validation lives.
|
|
20
|
+
*
|
|
21
|
+
* Nothing here degrades quietly. `recall` swallows a failed read because a
|
|
22
|
+
* review is still worth running without memory; `weigh context` is *about* the
|
|
23
|
+
* store, so a backend that is down has to say so rather than print an empty
|
|
24
|
+
* list that reads as "nothing is remembered".
|
|
25
|
+
*/
|
|
26
|
+
export declare class Knowledge {
|
|
27
|
+
private readonly _store;
|
|
28
|
+
private readonly _config;
|
|
29
|
+
constructor(_store: MemoryStore, _config: AgentConfig);
|
|
30
|
+
get enabled(): boolean;
|
|
31
|
+
/** Why memory is off, when it is. */
|
|
32
|
+
get reason(): string | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Writes claims, one call each.
|
|
35
|
+
*
|
|
36
|
+
* `infer: false` is the point of this method. Left to itself the backend runs
|
|
37
|
+
* an LLM over what it is given and keeps whatever it decides is durable —
|
|
38
|
+
* which is the right behaviour for a conversation and the wrong one for a
|
|
39
|
+
* claim that has already been written, validated and cited. A paraphrase of
|
|
40
|
+
* an invariant is not the invariant.
|
|
41
|
+
*
|
|
42
|
+
* Each claim comes back with the id the store gave it, when the store says,
|
|
43
|
+
* so the writer can take one back without listing the scope to find it.
|
|
44
|
+
*/
|
|
45
|
+
add(claims: readonly Claim[], scope: Scope): Promise<Claim[]>;
|
|
46
|
+
/** Everything remembered in one scope, newest first where the backend says. */
|
|
47
|
+
list(scope: Scope, limit?: number): Promise<Claim[]>;
|
|
48
|
+
/**
|
|
49
|
+
* The claims in one scope that bear on a question.
|
|
50
|
+
*
|
|
51
|
+
* The caller supplies the cap and the floor rather than inheriting the
|
|
52
|
+
* prompt path's, because recall into a review is tuned against claims and
|
|
53
|
+
* hunks while `memory.topK` is tuned against a conversation.
|
|
54
|
+
*/
|
|
55
|
+
search(query: string, scope: Scope, options?: {
|
|
56
|
+
topK?: number;
|
|
57
|
+
threshold?: number;
|
|
58
|
+
}): Promise<Claim[]>;
|
|
59
|
+
/** Forgets one scope. Returns how many records went, claims or not. */
|
|
60
|
+
clear(scope: Scope): Promise<number>;
|
|
61
|
+
/**
|
|
62
|
+
* Takes back claims by id, and returns what was taken.
|
|
63
|
+
*
|
|
64
|
+
* Every id is checked before anything is deleted: it must be a claim, and
|
|
65
|
+
* one stored under this scope for this user and agent. The delete underneath
|
|
66
|
+
* is by id alone, so this check is what keeps `context remove` inside the
|
|
67
|
+
* repository it was run in. One bad id refuses the lot, so a typo never
|
|
68
|
+
* leaves half a retraction done.
|
|
69
|
+
*/
|
|
70
|
+
remove(ids: readonly string[], scope: Scope): Promise<Claim[]>;
|
|
71
|
+
/**
|
|
72
|
+
* Records that claims were read again at `commit` and still hold.
|
|
73
|
+
*
|
|
74
|
+
* The id, the text and when it was written stay as they were; only the
|
|
75
|
+
* verified stamp moves. Checked exactly as `remove` checks, all before any
|
|
76
|
+
* is touched.
|
|
77
|
+
*/
|
|
78
|
+
confirm(ids: readonly string[], scope: Scope, at: ConfirmAt): Promise<Claim[]>;
|
|
79
|
+
/** How many store calls a remove or confirm has in flight at once. */
|
|
80
|
+
private static readonly _PARALLEL;
|
|
81
|
+
/**
|
|
82
|
+
* One write per claim, a few at a time, waiting for all of them to settle.
|
|
83
|
+
*
|
|
84
|
+
* A failed write does not stop the others, so the error can name which
|
|
85
|
+
* claims were written and which were not. Stopping at the first failure
|
|
86
|
+
* would leave the rest unknown, and a retry of `remove` with every id is
|
|
87
|
+
* refused once some of them are gone.
|
|
88
|
+
*/
|
|
89
|
+
private _writeEach;
|
|
90
|
+
private static _message;
|
|
91
|
+
/**
|
|
92
|
+
* The claims these ids name, each checked to be a claim stored under this
|
|
93
|
+
* scope for this user and agent, or an error naming every id that is not.
|
|
94
|
+
* `check` adds a caller's own reason to refuse one.
|
|
95
|
+
*/
|
|
96
|
+
private _owned;
|
|
97
|
+
/** Records that are claims, in the order the store returned them. */
|
|
98
|
+
private static _claims;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/knowledge/store.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,4DAA4D;AAC5D,MAAM,WAAW,SAAS;IACxB,mDAAmD;I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AE,EAAE,SAAS,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC,CAKnF;IAED,sEAAsE;IACtE,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAK;IAEtC;;;;;;;OAOG;YACW,UAAU;IAiBxB,OAAO,CAAC,MAAM,CAAC,QAAQ;IAIvB;;;;OAIG;YACW,MAAM;IA+BpB,qEAAqE;IACrE,OAAO,CAAC,MAAM,CAAC,OAAO;CAQvB"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
import PQueue from 'p-queue';
|
|
5
|
+
import { Claim } from './claim.js';
|
|
6
|
+
/**
|
|
7
|
+
* Repository knowledge, over whichever memory backend is configured.
|
|
8
|
+
*
|
|
9
|
+
* Deliberately narrow: typed claims in, typed claims out, one scope at a time.
|
|
10
|
+
* The store underneath takes free text and will remember anything — this is the
|
|
11
|
+
* only thing allowed to write knowledge into it, so that there is one schema and
|
|
12
|
+
* one place validation lives.
|
|
13
|
+
*
|
|
14
|
+
* Nothing here degrades quietly. `recall` swallows a failed read because a
|
|
15
|
+
* review is still worth running without memory; `weigh context` is *about* the
|
|
16
|
+
* store, so a backend that is down has to say so rather than print an empty
|
|
17
|
+
* list that reads as "nothing is remembered".
|
|
18
|
+
*/
|
|
19
|
+
export class Knowledge {
|
|
20
|
+
_store;
|
|
21
|
+
_config;
|
|
22
|
+
constructor(_store, _config) {
|
|
23
|
+
this._store = _store;
|
|
24
|
+
this._config = _config;
|
|
25
|
+
}
|
|
26
|
+
get enabled() {
|
|
27
|
+
return this._store.enabled;
|
|
28
|
+
}
|
|
29
|
+
/** Why memory is off, when it is. */
|
|
30
|
+
get reason() {
|
|
31
|
+
return this._store.reason;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Writes claims, one call each.
|
|
35
|
+
*
|
|
36
|
+
* `infer: false` is the point of this method. Left to itself the backend runs
|
|
37
|
+
* an LLM over what it is given and keeps whatever it decides is durable —
|
|
38
|
+
* which is the right behaviour for a conversation and the wrong one for a
|
|
39
|
+
* claim that has already been written, validated and cited. A paraphrase of
|
|
40
|
+
* an invariant is not the invariant.
|
|
41
|
+
*
|
|
42
|
+
* Each claim comes back with the id the store gave it, when the store says,
|
|
43
|
+
* so the writer can take one back without listing the scope to find it.
|
|
44
|
+
*/
|
|
45
|
+
async add(claims, scope) {
|
|
46
|
+
const written = [];
|
|
47
|
+
for (const claim of claims) {
|
|
48
|
+
const records = await this._store.add([{ role: 'user', content: claim.text() }], scope.for(this._config), {
|
|
49
|
+
infer: false,
|
|
50
|
+
metadata: claim.metadata(),
|
|
51
|
+
});
|
|
52
|
+
const id = records.length === 1 ? records[0].id : undefined;
|
|
53
|
+
written.push(id
|
|
54
|
+
? Claim.fromRecord({ id, memory: claim.text(), metadata: claim.metadata(), ...(scope.id && { runId: scope.id }) })
|
|
55
|
+
: claim);
|
|
56
|
+
}
|
|
57
|
+
return written;
|
|
58
|
+
}
|
|
59
|
+
/** Everything remembered in one scope, newest first where the backend says. */
|
|
60
|
+
async list(scope, limit) {
|
|
61
|
+
const records = await this._store.list(scope.for(this._config), {
|
|
62
|
+
...(limit !== undefined && { limit }),
|
|
63
|
+
});
|
|
64
|
+
return Knowledge._claims(records);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The claims in one scope that bear on a question.
|
|
68
|
+
*
|
|
69
|
+
* The caller supplies the cap and the floor rather than inheriting the
|
|
70
|
+
* prompt path's, because recall into a review is tuned against claims and
|
|
71
|
+
* hunks while `memory.topK` is tuned against a conversation.
|
|
72
|
+
*/
|
|
73
|
+
async search(query, scope, options = {}) {
|
|
74
|
+
const records = await this._store.search(query, scope.for(this._config), {
|
|
75
|
+
topK: options.topK ?? this._config.memory.topK,
|
|
76
|
+
...(options.threshold !== undefined
|
|
77
|
+
? { threshold: options.threshold }
|
|
78
|
+
: this._config.memory.threshold !== undefined && { threshold: this._config.memory.threshold }),
|
|
79
|
+
});
|
|
80
|
+
return Knowledge._claims(records);
|
|
81
|
+
}
|
|
82
|
+
/** Forgets one scope. Returns how many records went, claims or not. */
|
|
83
|
+
async clear(scope) {
|
|
84
|
+
return this._store.clear(scope.for(this._config));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Takes back claims by id, and returns what was taken.
|
|
88
|
+
*
|
|
89
|
+
* Every id is checked before anything is deleted: it must be a claim, and
|
|
90
|
+
* one stored under this scope for this user and agent. The delete underneath
|
|
91
|
+
* is by id alone, so this check is what keeps `context remove` inside the
|
|
92
|
+
* repository it was run in. One bad id refuses the lot, so a typo never
|
|
93
|
+
* leaves half a retraction done.
|
|
94
|
+
*/
|
|
95
|
+
async remove(ids, scope) {
|
|
96
|
+
const found = await this._owned(ids, scope, 'removed');
|
|
97
|
+
await this._writeEach(found, 'removed', (claim) => this._store.delete(claim.id));
|
|
98
|
+
return found;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Records that claims were read again at `commit` and still hold.
|
|
102
|
+
*
|
|
103
|
+
* The id, the text and when it was written stay as they were; only the
|
|
104
|
+
* verified stamp moves. Checked exactly as `remove` checks, all before any
|
|
105
|
+
* is touched.
|
|
106
|
+
*/
|
|
107
|
+
async confirm(ids, scope, at) {
|
|
108
|
+
const found = await this._owned(ids, scope, 'confirmed', at.check);
|
|
109
|
+
const confirmed = found.map((claim) => claim.verifiedAt(at.commit, at.now));
|
|
110
|
+
await this._writeEach(confirmed, 'confirmed', (claim) => this._store.update(claim.id, claim.metadata()));
|
|
111
|
+
return confirmed;
|
|
112
|
+
}
|
|
113
|
+
/** How many store calls a remove or confirm has in flight at once. */
|
|
114
|
+
static _PARALLEL = 8;
|
|
115
|
+
/**
|
|
116
|
+
* One write per claim, a few at a time, waiting for all of them to settle.
|
|
117
|
+
*
|
|
118
|
+
* A failed write does not stop the others, so the error can name which
|
|
119
|
+
* claims were written and which were not. Stopping at the first failure
|
|
120
|
+
* would leave the rest unknown, and a retry of `remove` with every id is
|
|
121
|
+
* refused once some of them are gone.
|
|
122
|
+
*/
|
|
123
|
+
async _writeEach(claims, verb, write) {
|
|
124
|
+
const queue = new PQueue({ concurrency: Knowledge._PARALLEL });
|
|
125
|
+
const results = await Promise.allSettled(claims.map((claim) => queue.add(() => write(claim))));
|
|
126
|
+
const failed = results.flatMap((result, i) => result.status === 'rejected' ? [`${claims[i].id} (${Knowledge._message(result.reason)})`] : []);
|
|
127
|
+
if (!failed.length)
|
|
128
|
+
return;
|
|
129
|
+
const done = claims.filter((_, i) => results[i].status === 'fulfilled').map((claim) => claim.id);
|
|
130
|
+
const which = done.length ? ` (${done.join(', ')})` : '';
|
|
131
|
+
const Verb = verb[0].toUpperCase() + verb.slice(1);
|
|
132
|
+
throw new Error(`${Verb} ${done.length} of ${claims.length} claims${which}. Not ${verb}: ${failed.join('; ')}.`);
|
|
133
|
+
}
|
|
134
|
+
static _message(reason) {
|
|
135
|
+
return reason instanceof Error ? reason.message : String(reason);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The claims these ids name, each checked to be a claim stored under this
|
|
139
|
+
* scope for this user and agent, or an error naming every id that is not.
|
|
140
|
+
* `check` adds a caller's own reason to refuse one.
|
|
141
|
+
*/
|
|
142
|
+
async _owned(ids, scope, verb, check) {
|
|
143
|
+
const owner = scope.for(this._config);
|
|
144
|
+
const unique = [...new Set(ids)];
|
|
145
|
+
const queue = new PQueue({ concurrency: Knowledge._PARALLEL });
|
|
146
|
+
const records = await queue.addAll(unique.map((id) => () => this._store.get(id)));
|
|
147
|
+
const found = [];
|
|
148
|
+
const problems = [];
|
|
149
|
+
for (const [i, id] of unique.entries()) {
|
|
150
|
+
const record = records[i];
|
|
151
|
+
const claim = record && Claim.fromRecord(record);
|
|
152
|
+
if (!record || record.userId !== owner.userId || record.agentId !== owner.agentId) {
|
|
153
|
+
problems.push(`no claim ${id}`);
|
|
154
|
+
}
|
|
155
|
+
else if (!claim) {
|
|
156
|
+
problems.push(`${id} is a memory, not a claim`);
|
|
157
|
+
}
|
|
158
|
+
else if (record.runId !== owner.runId) {
|
|
159
|
+
problems.push(`${id} is a claim about ${record.runId ?? 'user'}, not ${scope}`);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
const refused = check?.(claim);
|
|
163
|
+
if (refused)
|
|
164
|
+
problems.push(`${id} ${refused}`);
|
|
165
|
+
else
|
|
166
|
+
found.push(claim);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (problems.length)
|
|
170
|
+
throw new Error(`Nothing ${verb}: ${problems.join('; ')}.`);
|
|
171
|
+
return found;
|
|
172
|
+
}
|
|
173
|
+
/** Records that are claims, in the order the store returned them. */
|
|
174
|
+
static _claims(records) {
|
|
175
|
+
const claims = [];
|
|
176
|
+
for (const record of records) {
|
|
177
|
+
const claim = Claim.fromRecord(record);
|
|
178
|
+
if (claim)
|
|
179
|
+
claims.push(claim);
|
|
180
|
+
}
|
|
181
|
+
return claims;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/knowledge/store.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,sEAAsE;AACtE,4DAA4D;AAE5D,OAAO,MAAM,MAAM,SAAS,CAAC;AAI7B,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAWnC;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,SAAS;IAED,MAAM;IACN,OAAO;IAF1B,YACmB,MAAmB,EACnB,OAAoB;sBADpB,MAAM;uBACN,OAAO;IACvB,CAAC;IAEJ,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;IAC7B,CAAC;IAED,qCAAqC;IACrC,IAAI,MA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C/B,IAAI,OAAO;oBAAE,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,OAAO,EAAE,CAAC,CAAC;;oBAC1C,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACzB,CAAC;QACH,CAAC;QACD,IAAI,QAAQ,CAAC,MAAM;YAAE,MAAM,IAAI,KAAK,CAAC,WAAW,IAAI,KAAK,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACjF,OAAO,KAAK,CAAC;IACf,CAAC;IAED,qEAAqE;IAC7D,MAAM,CAAC,OAAO,CAAC,OAA0C;QAC/D,MAAM,MAAM,GAAY,EAAE,CAAC;QAC3B,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC7B,MAAM,KAAK,GAAG,KAAK,CAAC,UAAU,CAAC,MAAe,CAAC,CAAC;YAChD,IAAI,KAAK;gBAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAChC,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Docker Compose file for the self-hosted backend, as the package ships it.
|
|
3
|
+
*
|
|
4
|
+
* Read rather than embedded, so the repository's docker-compose.yml stays the
|
|
5
|
+
* one copy. Two levels up from this file is the package root whether it runs
|
|
6
|
+
* from `src/memory/` under bun or from `dist/memory/` under node, and the
|
|
7
|
+
* package's `files` list ships the compose file beside package.json.
|
|
8
|
+
*/
|
|
9
|
+
export declare class ComposeFile {
|
|
10
|
+
static readonly URL: URL;
|
|
11
|
+
/** The file's text. Throws when the install is missing it. */
|
|
12
|
+
static read(): string;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=compose-file.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compose-file.d.ts","sourceRoot":"","sources":["../../src/memory/compose-file.ts"],"names":[],"mappings":"AAMA;;;;;;;GAOG;AACH,qBAAa,WAAW;IACtB,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAwD;IAE3E,8DAA8D;IAC9D,MAAM,CAAC,IAAI,IAAI,MAAM,CAQpB;CACF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
import { readFileSync } from 'fs';
|
|
5
|
+
/**
|
|
6
|
+
* The Docker Compose file for the self-hosted backend, as the package ships it.
|
|
7
|
+
*
|
|
8
|
+
* Read rather than embedded, so the repository's docker-compose.yml stays the
|
|
9
|
+
* one copy. Two levels up from this file is the package root whether it runs
|
|
10
|
+
* from `src/memory/` under bun or from `dist/memory/` under node, and the
|
|
11
|
+
* package's `files` list ships the compose file beside package.json.
|
|
12
|
+
*/
|
|
13
|
+
export class ComposeFile {
|
|
14
|
+
static URL = new URL('../../docker-compose.yml', import.meta.url);
|
|
15
|
+
/** The file's text. Throws when the install is missing it. */
|
|
16
|
+
static read() {
|
|
17
|
+
try {
|
|
18
|
+
return readFileSync(ComposeFile.URL, 'utf-8');
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
throw new Error('This install of Weigh has no docker-compose.yml. Reinstall it with npm install -g @afaik-lab/weigh.');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=compose-file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compose-file.js","sourceRoot":"","sources":["../../src/memory/compose-file.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,sEAAsE;AACtE,4DAA4D;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAElC;;;;;;;GAOG;AACH,MAAM,OAAO,WAAW;IACtB,MAAM,CAAU,GAAG,GAAG,IAAI,GAAG,CAAC,0BAA0B,EAAE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;IAE3E,8DAA8D;IAC9D,MAAM,CAAC,IAAI;QACT,IAAI,CAAC;YACH,OAAO,YAAY,CAAC,WAAW,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAChD,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,KAAK,CACb,qGAAqG,CACtG,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { AgentConfig, MemorySettings } from '../config.js';
|
|
2
|
+
import type { AddOptions, ListOptions, MemoryMessage, MemoryRecord, MemoryScope, MemoryStore, SearchOptions } from './types.js';
|
|
3
|
+
export * from './types.js';
|
|
4
|
+
export { PlatformMemoryStore } from './platform.js';
|
|
5
|
+
export { OssMemoryStore } from './oss.js';
|
|
6
|
+
export { ComposeFile } from './compose-file.js';
|
|
7
|
+
/** Stand-in used whenever memory is off or unconfigured. Every call is a no-op. */
|
|
8
|
+
export declare class NullMemoryStore implements MemoryStore {
|
|
9
|
+
readonly backend: 'disabled';
|
|
10
|
+
readonly enabled = false;
|
|
11
|
+
readonly reason: string;
|
|
12
|
+
constructor(reason: string);
|
|
13
|
+
search(): Promise<MemoryRecord[]>;
|
|
14
|
+
add(): Promise<MemoryRecord[]>;
|
|
15
|
+
list(): Promise<MemoryRecord[]>;
|
|
16
|
+
clear(): Promise<number>;
|
|
17
|
+
get(): Promise<MemoryRecord | undefined>;
|
|
18
|
+
delete(): Promise<void>;
|
|
19
|
+
update(): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export declare function defaultUserId(): string;
|
|
22
|
+
export declare function resolveScope(config: AgentConfig, runId?: string): MemoryScope;
|
|
23
|
+
/**
|
|
24
|
+
* Build the configured store. Never throws — a backend that cannot be
|
|
25
|
+
* constructed degrades to a NullMemoryStore carrying the reason, so a missing
|
|
26
|
+
* key turns memory off instead of taking the whole run down.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createMemoryStore(settings: MemorySettings): MemoryStore;
|
|
29
|
+
/**
|
|
30
|
+
* Recall for the automatic pre-run injection. Memory is an enhancement, so a
|
|
31
|
+
* backend that is down warns and returns nothing rather than failing the run.
|
|
32
|
+
* Callers that need to know a search failed — `weigh memory search`, which is
|
|
33
|
+
* about the store itself — go to the store directly.
|
|
34
|
+
*/
|
|
35
|
+
export declare function recall(store: MemoryStore, query: string, scope: MemoryScope, options?: SearchOptions & {
|
|
36
|
+
onWarn?: (message: string) => void;
|
|
37
|
+
}): Promise<MemoryRecord[]>;
|
|
38
|
+
/** Post-run capture. Same degrade-don't-fail contract as recall(). */
|
|
39
|
+
export declare function capture(store: MemoryStore, messages: MemoryMessage[], scope: MemoryScope, options?: AddOptions & {
|
|
40
|
+
onWarn?: (message: string) => void;
|
|
41
|
+
}): Promise<MemoryRecord[]>;
|
|
42
|
+
export type { AddOptions, ListOptions, MemoryMessage, MemoryRecord, MemoryScope, MemoryStore, SearchOptions };
|
|
43
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/memory/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAGhE,OAAO,KAAK,EACV,UAAU,EACV,WAAW,EACX,aAAa,EACb,YAAY,EACZ,WAAW,EACX,WAAW,EACX,aAAa,EACd,MAAM,YAAY,CAAC;AAEpB,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,mFAAmF;AACnF,qBAAa,eAAgB,YAAW,WAAW;IACjD,QAAQ,CAAC,OAAO,EAAG,UAAU,CAAU;IACvC,QAAQ,CAAC,OAAO,SAAS;IAC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AY,EAAE,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
import { userInfo } from 'os';
|
|
5
|
+
import { PlatformMemoryStore } from './platform.js';
|
|
6
|
+
import { OssMemoryStore } from './oss.js';
|
|
7
|
+
export * from './types.js';
|
|
8
|
+
export { PlatformMemoryStore } from './platform.js';
|
|
9
|
+
export { OssMemoryStore } from './oss.js';
|
|
10
|
+
export { ComposeFile } from './compose-file.js';
|
|
11
|
+
/** Stand-in used whenever memory is off or unconfigured. Every call is a no-op. */
|
|
12
|
+
export class NullMemoryStore {
|
|
13
|
+
backend = 'disabled';
|
|
14
|
+
enabled = false;
|
|
15
|
+
reason;
|
|
16
|
+
constructor(reason) {
|
|
17
|
+
this.reason = reason;
|
|
18
|
+
}
|
|
19
|
+
async search() {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
async add() {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
async list() {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
async clear() {
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
async get() {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
async delete() { }
|
|
35
|
+
async update() { }
|
|
36
|
+
}
|
|
37
|
+
export function defaultUserId() {
|
|
38
|
+
try {
|
|
39
|
+
return process.env.USER || userInfo().username || 'default';
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return 'default';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export function resolveScope(config, runId) {
|
|
46
|
+
return {
|
|
47
|
+
userId: config.memory.userId,
|
|
48
|
+
agentId: config.memory.agentId,
|
|
49
|
+
...(runId && { runId }),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Build the configured store. Never throws — a backend that cannot be
|
|
54
|
+
* constructed degrades to a NullMemoryStore carrying the reason, so a missing
|
|
55
|
+
* key turns memory off instead of taking the whole run down.
|
|
56
|
+
*/
|
|
57
|
+
export function createMemoryStore(settings) {
|
|
58
|
+
if (!settings.enabled)
|
|
59
|
+
return new NullMemoryStore('memory is disabled');
|
|
60
|
+
try {
|
|
61
|
+
if (settings.backend === 'platform') {
|
|
62
|
+
if (!settings.platform.apiKey) {
|
|
63
|
+
return new NullMemoryStore('MEM0_API_KEY is not set (platform backend)');
|
|
64
|
+
}
|
|
65
|
+
return new PlatformMemoryStore(settings.platform);
|
|
66
|
+
}
|
|
67
|
+
return new OssMemoryStore({
|
|
68
|
+
collectionName: settings.oss.collectionName,
|
|
69
|
+
qdrantUrl: settings.oss.qdrantUrl,
|
|
70
|
+
...(settings.oss.qdrantApiKey && { qdrantApiKey: settings.oss.qdrantApiKey }),
|
|
71
|
+
openRouterApiKey: settings.oss.openRouterApiKey,
|
|
72
|
+
extractionModel: settings.oss.extractionModel,
|
|
73
|
+
embeddingProvider: settings.oss.embeddingProvider,
|
|
74
|
+
embeddingModel: settings.oss.embeddingModel,
|
|
75
|
+
embeddingDims: settings.oss.embeddingDims,
|
|
76
|
+
ollamaUrl: settings.oss.ollamaUrl,
|
|
77
|
+
...(settings.oss.openAiApiKey && { openAiApiKey: settings.oss.openAiApiKey }),
|
|
78
|
+
...(settings.oss.overrides && { overrides: settings.oss.overrides }),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
return new NullMemoryStore(err?.message ?? String(err));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Recall for the automatic pre-run injection. Memory is an enhancement, so a
|
|
87
|
+
* backend that is down warns and returns nothing rather than failing the run.
|
|
88
|
+
* Callers that need to know a search failed — `weigh memory search`, which is
|
|
89
|
+
* about the store itself — go to the store directly.
|
|
90
|
+
*/
|
|
91
|
+
export async function recall(store, query, scope, options = {}) {
|
|
92
|
+
if (!store.enabled)
|
|
93
|
+
return [];
|
|
94
|
+
const { onWarn, ...searchOptions } = options;
|
|
95
|
+
try {
|
|
96
|
+
return await store.search(query, scope, searchOptions);
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
onWarn?.(`memory recall failed: ${err?.message ?? err}`);
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** Post-run capture. Same degrade-don't-fail contract as recall(). */
|
|
104
|
+
export async function capture(store, messages, scope, options = {}) {
|
|
105
|
+
if (!store.enabled)
|
|
106
|
+
return [];
|
|
107
|
+
const { onWarn, ...addOptions } = options;
|
|
108
|
+
// Nothing useful is extracted from an empty exchange, and the API rejects it.
|
|
109
|
+
if (messages.every((m) => !m.content.trim()))
|
|
110
|
+
return [];
|
|
111
|
+
try {
|
|
112
|
+
return await store.add(messages, scope, addOptions);
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
onWarn?.(`memory capture failed: ${err?.message ?? err}`);
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/memory/index.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,sEAAsE;AACtE,4DAA4D;AAE5D,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAE9B,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAW1C,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE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