@kindgi/memory 0.0.0-bootstrap.0 → 0.1.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/LICENSE +201 -0
- package/README.md +93 -2
- package/dist/errors.d.ts +58 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +4 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +31 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +19 -0
- package/dist/log.js.map +1 -0
- package/dist/memory-binding.d.ts +94 -0
- package/dist/memory-binding.d.ts.map +1 -0
- package/dist/memory-binding.js +4 -0
- package/dist/memory-binding.js.map +1 -0
- package/dist/retrieval.d.ts +13 -0
- package/dist/retrieval.d.ts.map +1 -0
- package/dist/retrieval.js +4 -0
- package/dist/retrieval.js.map +1 -0
- package/dist/types.d.ts +97 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -0
- package/package.json +48 -4
- package/src/errors.ts +76 -0
- package/src/index.ts +41 -0
- package/src/log.ts +51 -0
- package/src/memory-binding.ts +119 -0
- package/src/retrieval.ts +16 -0
- package/src/types.ts +114 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Kindgi Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,94 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@kindgi/memory`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Type contract for Kindgi memory: typed, versioned facts, the hash-chained run log, retrieval hits, and `MemoryQueryBinding`, the interface for querying facts and appending to the run log. The package holds no state and ships no storage; its only runtime export is the `LOG_KINDS` constant. A runtime adapter implements the binding (the Kindgi runtime provides a Postgres-backed one), and any object that satisfies the interface can stand in for it.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
Give agent code, tools, and storage backends one shared vocabulary for memory, so that code that reads facts (for example the retrieval step in [`@kindgi/agents`](../agents/)) depends only on an interface and never on a database client. Every operation is tenant-scoped: callers pass `tenantId` on each call, and the binding implementation enforces isolation.
|
|
8
|
+
|
|
9
|
+
## Exports
|
|
10
|
+
|
|
11
|
+
- **Facts**
|
|
12
|
+
- **`Fact<TContent>`** — a typed, versioned record: `id`, `type`, `scope`, `version` (monotonic within `(scope, id)`), `createdAt`, and optional `updatedAt`, `content`, `contentRef` (`blob://<provider>/<bucket>/<key>` for payloads stored externally), `contentHash`, `size`, `embeddingModel`, `retention`, `source`, `causedByLogId`, `supersedes`. A fact without `source` is immutable output ("Kind A"); a fact with `source` is a cached view of external state ("Kind B").
|
|
13
|
+
- **`MemoryScope`** — where a fact or log entry lives: `tenantId` plus optional `userId`, `orgId`, `projectId`, `threadId`, `sessionId`.
|
|
14
|
+
- **`Retention`** — fact-level override of the tenant's retention defaults: `keepUntil`, `keepDays`, `legalHold`.
|
|
15
|
+
- **`Source`**, **`SourceFreshness`**, **`SourceRefresh`** — external-source metadata for Kind B facts: the source `kind` (`http-api`, `blob`, `mcp-tool`, `external-db`, `user-input`) and `uri`, freshness data (`ttlSeconds`, `lastVerifiedAt`, `etag`, `sourceVersion`), and the refresh `strategy` (`on-read`, `background`, `manual`) with an optional registered `handler` id.
|
|
16
|
+
- **Log**
|
|
17
|
+
- **`LogEntry`** — one immutable entry in a run's append-only log, ordered by `sequence` within `(tenantId, runId)`. Each entry stores `prevHash` and `entryHash`, so changing an earlier entry breaks every later link in the chain.
|
|
18
|
+
- **`LOG_KINDS`** / **`LogKind`** — the closed set of entry kinds (`user-message`, `agent-message`, `system-message`, `tool-call`, `tool-result`, `internal-thought`, `retrieval`, `artifact-produced`, `event-emitted`, `event-received`, `guardrail-triggered`, `wait-suspended`, `wait-resumed`), matching the enum in `memory.schema.json` from [`@kindgi/specs`](../specs/).
|
|
19
|
+
- **`RetrievalHit<TContent>`** — a `fact` plus a numeric `score`. Keyword scores are unbounded positive ranks; semantic scores are cosine similarity in [-1, 1]. Higher is better in both cases.
|
|
20
|
+
- **`MemoryQueryBinding`** — the data-access interface, implemented by a runtime adapter. Every method returns `Promise<Result<…, MemoryError>>`.
|
|
21
|
+
- `listFacts(input: ListFactsInput)` — facts filtered by `type` and a partial `scope`, capped by `limit`; `latestOnly` selects the latest version of each fact.
|
|
22
|
+
- `searchByKeyword(input: SearchByKeywordInput)` — full-text search over facts, returning up to `topK` hits.
|
|
23
|
+
- `searchBySemantic(input: SearchBySemanticInput)` — embeds `query` through the caller-supplied `embeddingRegistry` from [`@kindgi/embedding`](../embedding/) (pinned to `embeddingModel`, or the registry's only provider when omitted) and runs a vector search.
|
|
24
|
+
- `appendLog(input: AppendLogInput)` — appends one entry. The implementation assigns `sequence` and the hash chain and returns the stored `LogEntry`.
|
|
25
|
+
- `readLog(input: ReadLogInput)` — the entries for `(tenantId, runId)` in order, optionally narrowed with `sinceSequence`.
|
|
26
|
+
- **Errors** — **`MemoryError`**, a union of `InvalidLogEntryError`, `InvalidFactError`, `FactNotFoundError`, `LogNotFoundError`, `RefreshHandlerMissingError`, `RetentionViolationError` (`reason`: `legal-hold`, `keep-until` or `keep-days`), `PersistenceError`, and the `EmbeddingError` variants from `@kindgi/embedding`. Every variant carries a `code` for matching.
|
|
27
|
+
|
|
28
|
+
## Example
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import type { Fact, MemoryQueryBinding } from '@kindgi/memory';
|
|
32
|
+
import type { ProjectId, RunId, TenantId } from '@kindgi/types';
|
|
33
|
+
|
|
34
|
+
interface Clause {
|
|
35
|
+
readonly heading: string;
|
|
36
|
+
readonly text: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** True when a cached-view (Kind B) fact is older than its source's TTL. */
|
|
40
|
+
function isStale(fact: Fact, now = Date.now()): boolean {
|
|
41
|
+
const freshness = fact.source?.freshness;
|
|
42
|
+
if (freshness?.ttlSeconds === undefined || freshness.lastVerifiedAt === undefined) return false;
|
|
43
|
+
return now - Date.parse(freshness.lastVerifiedAt) > freshness.ttlSeconds * 1000;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function findClauses(
|
|
47
|
+
memory: MemoryQueryBinding,
|
|
48
|
+
tenantId: TenantId,
|
|
49
|
+
projectId: ProjectId,
|
|
50
|
+
runId: RunId,
|
|
51
|
+
query: string,
|
|
52
|
+
): Promise<readonly Clause[]> {
|
|
53
|
+
const hits = await memory.searchByKeyword<Clause>({
|
|
54
|
+
tenantId,
|
|
55
|
+
query,
|
|
56
|
+
type: 'acme.clause',
|
|
57
|
+
scope: { projectId },
|
|
58
|
+
topK: 5,
|
|
59
|
+
});
|
|
60
|
+
if (hits.kind === 'err') {
|
|
61
|
+
console.error(`clause search failed: ${hits.error.code}`, hits.error.message);
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Record the lookup in the run's hash-chained log.
|
|
66
|
+
const logged = await memory.appendLog({
|
|
67
|
+
tenantId,
|
|
68
|
+
runId,
|
|
69
|
+
kind: 'retrieval',
|
|
70
|
+
scope: { tenantId, projectId },
|
|
71
|
+
actor: 'acme.clause-finder',
|
|
72
|
+
payload: { query, factIds: hits.value.map((hit) => hit.fact.id) },
|
|
73
|
+
});
|
|
74
|
+
if (logged.kind === 'err') console.error('log append failed', logged.error.message);
|
|
75
|
+
|
|
76
|
+
return hits.value
|
|
77
|
+
.filter((hit) => !isStale(hit.fact))
|
|
78
|
+
.flatMap((hit) => (hit.fact.content === undefined ? [] : [hit.fact.content]));
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Non-goals
|
|
83
|
+
|
|
84
|
+
- **No implementation.** Storage, full-text and vector indexes, embedding calls, refresh handlers, and retention sweeps belong to the binding implementation. This package defines shapes only.
|
|
85
|
+
- **No provenance on the interface.** An implementation may record provenance internally, but `MemoryQueryBinding` never accepts or returns a provenance builder.
|
|
86
|
+
- **No per-fact retrieval hints.** Which indexes a fact type populates is configured once per type in the memory implementation, not on individual facts.
|
|
87
|
+
- **No pagination cursor.** `listFacts` takes a `limit` only.
|
|
88
|
+
|
|
89
|
+
## Related
|
|
90
|
+
|
|
91
|
+
- [`@kindgi/agents`](../agents/) — takes a `MemoryQueryBinding` and uses it for the retrieval step of each agent turn.
|
|
92
|
+
- [`@kindgi/embedding`](../embedding/) — the embedding provider registry used by `searchBySemantic`.
|
|
93
|
+
- [`@kindgi/specs`](../specs/) — `memory.schema.json`, the wire schema for facts and log entries.
|
|
94
|
+
- [`@kindgi/types`](../types/) — branded ids (`FactId`, `LogEntryId`, `RunId`, …) and `Result`.
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { EmbeddingError } from '@kindgi/embedding';
|
|
2
|
+
import type { FactId, LogEntryId } from '@kindgi/types';
|
|
3
|
+
/**
|
|
4
|
+
* Errors emitted by the memory subsystem. Every variant carries a `code`
|
|
5
|
+
* for pattern matching; messages are human-readable, not API contract.
|
|
6
|
+
*
|
|
7
|
+
* `EmbeddingError` from @kindgi/embedding is folded in — an
|
|
8
|
+
* implementation that embeds facts on write, and `searchBySemantic`,
|
|
9
|
+
* resolve providers via the registry and may surface any of its
|
|
10
|
+
* rejection modes (no-embedding-provider, unknown-embedding-model,
|
|
11
|
+
* ambiguous-default-provider).
|
|
12
|
+
*/
|
|
13
|
+
export type MemoryError = InvalidLogEntryError | InvalidFactError | FactNotFoundError | LogNotFoundError | EmbeddingError | RefreshHandlerMissingError | RetentionViolationError | PersistenceError;
|
|
14
|
+
export interface InvalidLogEntryError {
|
|
15
|
+
readonly code: 'invalid-log-entry';
|
|
16
|
+
readonly message: string;
|
|
17
|
+
readonly reason: string;
|
|
18
|
+
}
|
|
19
|
+
export interface InvalidFactError {
|
|
20
|
+
readonly code: 'invalid-fact';
|
|
21
|
+
readonly message: string;
|
|
22
|
+
readonly reason: string;
|
|
23
|
+
}
|
|
24
|
+
export interface FactNotFoundError {
|
|
25
|
+
readonly code: 'fact-not-found';
|
|
26
|
+
readonly message: string;
|
|
27
|
+
readonly factId: FactId;
|
|
28
|
+
}
|
|
29
|
+
export interface LogNotFoundError {
|
|
30
|
+
readonly code: 'log-not-found';
|
|
31
|
+
readonly message: string;
|
|
32
|
+
readonly logEntryId: LogEntryId;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A Kind-B fact needs on-read refresh but its `source.refresh.handler`
|
|
36
|
+
* doesn't resolve to a registered handler.
|
|
37
|
+
*/
|
|
38
|
+
export interface RefreshHandlerMissingError {
|
|
39
|
+
readonly code: 'refresh-handler-missing';
|
|
40
|
+
readonly message: string;
|
|
41
|
+
readonly handlerId: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A write attempted on a legal-hold fact (retention.legalHold = true) or
|
|
45
|
+
* a delete attempted on a fact whose retention window is still open.
|
|
46
|
+
*/
|
|
47
|
+
export interface RetentionViolationError {
|
|
48
|
+
readonly code: 'retention-violation';
|
|
49
|
+
readonly message: string;
|
|
50
|
+
readonly factId: FactId;
|
|
51
|
+
readonly reason: 'legal-hold' | 'keep-until' | 'keep-days';
|
|
52
|
+
}
|
|
53
|
+
export interface PersistenceError {
|
|
54
|
+
readonly code: 'persistence-error';
|
|
55
|
+
readonly message: string;
|
|
56
|
+
readonly cause: unknown;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAExD;;;;;;;;;GASG;AACH,MAAM,MAAM,WAAW,GACnB,oBAAoB,GACpB,gBAAgB,GAChB,iBAAiB,GACjB,gBAAgB,GAChB,cAAc,GACd,0BAA0B,GAC1B,uBAAuB,GACvB,gBAAgB,CAAC;AAErB,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;IAC/B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;CACjC;AAED;;;GAGG;AACH,MAAM,WAAW,0BAA0B;IACzC,QAAQ,CAAC,IAAI,EAAE,yBAAyB,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED;;;GAGG;AACH,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,IAAI,EAAE,qBAAqB,CAAC;IACrC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,YAAY,GAAG,YAAY,GAAG,WAAW,CAAC;CAC5D;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACzB"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type { Fact, MemoryScope, Retention, Source, SourceFreshness, SourceRefresh, } from './types.js';
|
|
2
|
+
export { LOG_KINDS } from './log.js';
|
|
3
|
+
export type { LogEntry, LogKind } from './log.js';
|
|
4
|
+
export type { RetrievalHit } from './retrieval.js';
|
|
5
|
+
export type { FactNotFoundError, InvalidFactError, InvalidLogEntryError, LogNotFoundError, MemoryError, PersistenceError, RefreshHandlerMissingError, RetentionViolationError, } from './errors.js';
|
|
6
|
+
export type { AppendLogInput, ListFactsInput, MemoryQueryBinding, ReadLogInput, SearchByKeywordInput, SearchBySemanticInput, } from './memory-binding.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,YAAY,EACV,IAAI,EACJ,WAAW,EACX,SAAS,EACT,MAAM,EACN,eAAe,EACf,aAAa,GACd,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,YAAY,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAGlD,YAAY,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAGnD,YAAY,EACV,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EACpB,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,0BAA0B,EAC1B,uBAAuB,GACxB,MAAM,aAAa,CAAC;AAGrB,YAAY,EACV,cAAc,EACd,cAAc,EACd,kBAAkB,EAClB,YAAY,EACZ,oBAAoB,EACpB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAYjC,sCAAsC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/log.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { LogEntryId, RunId, Timestamp } from '@kindgi/types';
|
|
2
|
+
import type { MemoryScope } from './types.js';
|
|
3
|
+
/** Kind identifiers for LogEntry — matches the enum in `@kindgi/specs/memory.schema.json`. */
|
|
4
|
+
export declare const LOG_KINDS: readonly ["user-message", "agent-message", "system-message", "tool-call", "tool-result", "internal-thought", "retrieval", "artifact-produced", "event-emitted", "event-received", "guardrail-triggered", "wait-suspended", "wait-resumed"];
|
|
5
|
+
export type LogKind = (typeof LOG_KINDS)[number];
|
|
6
|
+
/**
|
|
7
|
+
* One event in the append-only log for a run / thread / session. Immutable
|
|
8
|
+
* once written. Ordered by (sequence, timestamp) within `(tenantId, runId)`.
|
|
9
|
+
*
|
|
10
|
+
* Every entry commits to a hash of the previous entry in the same
|
|
11
|
+
* `(tenantId, runId)` chain via `prevHash` → `entryHash`. Tamper with any
|
|
12
|
+
* past entry and every subsequent entry's `prevHash` breaks — the whole
|
|
13
|
+
* chain is one Merkle-lite structure.
|
|
14
|
+
*/
|
|
15
|
+
export interface LogEntry {
|
|
16
|
+
readonly id: LogEntryId;
|
|
17
|
+
readonly kind: LogKind;
|
|
18
|
+
readonly scope: MemoryScope;
|
|
19
|
+
readonly runId: RunId;
|
|
20
|
+
readonly actor?: string;
|
|
21
|
+
readonly timestamp: Timestamp;
|
|
22
|
+
/** Monotonic sequence within `(tenantId, runId)`. */
|
|
23
|
+
readonly sequence: number;
|
|
24
|
+
readonly payload?: unknown;
|
|
25
|
+
readonly contentHash?: string;
|
|
26
|
+
/** Zero-hash for sequence 0. */
|
|
27
|
+
readonly prevHash: string;
|
|
28
|
+
readonly entryHash: string;
|
|
29
|
+
readonly causedByLogId?: string;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=log.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAElE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,8FAA8F;AAC9F,eAAO,MAAM,SAAS,4OAcZ,CAAC;AAEX,MAAM,MAAM,OAAO,GAAG,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC;AAEjD;;;;;;;;GAQG;AACH,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,EAAE,EAAE,UAAU,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;IAC9B,qDAAqD;IACrD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,gCAAgC;IAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;CACjC"}
|
package/dist/log.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
/** Kind identifiers for LogEntry — matches the enum in `@kindgi/specs/memory.schema.json`. */
|
|
4
|
+
export const LOG_KINDS = [
|
|
5
|
+
'user-message',
|
|
6
|
+
'agent-message',
|
|
7
|
+
'system-message',
|
|
8
|
+
'tool-call',
|
|
9
|
+
'tool-result',
|
|
10
|
+
'internal-thought',
|
|
11
|
+
'retrieval',
|
|
12
|
+
'artifact-produced',
|
|
13
|
+
'event-emitted',
|
|
14
|
+
'event-received',
|
|
15
|
+
'guardrail-triggered',
|
|
16
|
+
'wait-suspended',
|
|
17
|
+
'wait-resumed',
|
|
18
|
+
];
|
|
19
|
+
//# sourceMappingURL=log.js.map
|
package/dist/log.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.js","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAMjC,8FAA8F;AAC9F,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,cAAc;IACd,eAAe;IACf,gBAAgB;IAChB,WAAW;IACX,aAAa;IACb,kBAAkB;IAClB,WAAW;IACX,mBAAmB;IACnB,eAAe;IACf,gBAAgB;IAChB,qBAAqB;IACrB,gBAAgB;IAChB,cAAc;CACN,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { EmbeddingProviderRegistry } from '@kindgi/embedding';
|
|
2
|
+
import type { Fact, LogEntry, LogKind, MemoryError, MemoryScope, RetrievalHit } from '@kindgi/memory';
|
|
3
|
+
import type { Result, RunId, TenantId } from '@kindgi/types';
|
|
4
|
+
/**
|
|
5
|
+
* Caller-plugged data-access surface for the memory subsystem. Agent
|
|
6
|
+
* code routes its memory reads through this binding — @kindgi/agents
|
|
7
|
+
* never touches a database client directly.
|
|
8
|
+
*
|
|
9
|
+
* The Kindgi runtime supplies a Postgres-backed implementation; any
|
|
10
|
+
* object satisfying the interface works (tests, other stores). Agents
|
|
11
|
+
* receive it as `InvokeAgentBindings.memoryBinding`.
|
|
12
|
+
*
|
|
13
|
+
* Contract notes:
|
|
14
|
+
* - Every method is tenant-scoped via `input.tenantId`. The binding
|
|
15
|
+
* implementation enforces tenant isolation; callers just supply the
|
|
16
|
+
* tenant id.
|
|
17
|
+
* - Provenance emission is internal to the implementation — the
|
|
18
|
+
* binding may record provenance events, but the public interface
|
|
19
|
+
* never accepts or returns a `ProvenanceBuilder`.
|
|
20
|
+
*/
|
|
21
|
+
export interface MemoryQueryBinding {
|
|
22
|
+
/**
|
|
23
|
+
* List facts filtered by tenant, type, and scope, capped by `limit`
|
|
24
|
+
* (no pagination cursor). Default: latest version per `(type, id)`.
|
|
25
|
+
*/
|
|
26
|
+
listFacts<TContent = unknown>(input: ListFactsInput): Promise<Result<readonly Fact<TContent>[], MemoryError>>;
|
|
27
|
+
/**
|
|
28
|
+
* Full-text keyword search over facts. Tokenizer and ranking are
|
|
29
|
+
* implementation-defined; the Kindgi runtime's Postgres implementation
|
|
30
|
+
* ranks with `ts_rank_cd` and tokenizes English.
|
|
31
|
+
*/
|
|
32
|
+
searchByKeyword<TContent = unknown>(input: SearchByKeywordInput): Promise<Result<readonly RetrievalHit<TContent>[], MemoryError>>;
|
|
33
|
+
/**
|
|
34
|
+
* Semantic-search facts via a caller-supplied `EmbeddingProviderRegistry`.
|
|
35
|
+
* The registry resolves a provider (by model name or default) that
|
|
36
|
+
* embeds the query; the impl runs the vector search.
|
|
37
|
+
*/
|
|
38
|
+
searchBySemantic<TContent = unknown>(input: SearchBySemanticInput): Promise<Result<readonly RetrievalHit<TContent>[], MemoryError>>;
|
|
39
|
+
/**
|
|
40
|
+
* Append one entry to the run's hash-chained log. The implementation
|
|
41
|
+
* assigns `sequence` and `prevHash` / `entryHash` atomically; the
|
|
42
|
+
* returned `LogEntry` is the entry as stored.
|
|
43
|
+
*/
|
|
44
|
+
appendLog(input: AppendLogInput): Promise<Result<LogEntry, MemoryError>>;
|
|
45
|
+
/**
|
|
46
|
+
* Read the log entries for `(tenantId, runId)` in `(sequence, timestamp)`
|
|
47
|
+
* order. `sinceSequence` is an inclusive lower bound (entries with
|
|
48
|
+
* `sequence >= sinceSequence`); to resume after a checkpoint, pass the
|
|
49
|
+
* last seen sequence + 1.
|
|
50
|
+
*/
|
|
51
|
+
readLog(input: ReadLogInput): Promise<Result<readonly LogEntry[], MemoryError>>;
|
|
52
|
+
}
|
|
53
|
+
export interface ListFactsInput {
|
|
54
|
+
readonly tenantId: TenantId;
|
|
55
|
+
readonly type?: string;
|
|
56
|
+
readonly scope?: Partial<MemoryScope>;
|
|
57
|
+
readonly limit?: number;
|
|
58
|
+
readonly latestOnly?: boolean;
|
|
59
|
+
}
|
|
60
|
+
export interface SearchByKeywordInput {
|
|
61
|
+
readonly tenantId: TenantId;
|
|
62
|
+
readonly query: string;
|
|
63
|
+
readonly type?: string;
|
|
64
|
+
readonly scope?: Partial<MemoryScope>;
|
|
65
|
+
readonly topK?: number;
|
|
66
|
+
}
|
|
67
|
+
export interface SearchBySemanticInput {
|
|
68
|
+
readonly tenantId: TenantId;
|
|
69
|
+
readonly query: string;
|
|
70
|
+
readonly embeddingRegistry: EmbeddingProviderRegistry;
|
|
71
|
+
/**
|
|
72
|
+
* Which registered embedding model to use. Omit to fall back to the
|
|
73
|
+
* registry's sole registered provider (errors if zero or ambiguous).
|
|
74
|
+
*/
|
|
75
|
+
readonly embeddingModel?: string;
|
|
76
|
+
readonly type?: string;
|
|
77
|
+
readonly scope?: Partial<MemoryScope>;
|
|
78
|
+
readonly topK?: number;
|
|
79
|
+
}
|
|
80
|
+
export interface AppendLogInput {
|
|
81
|
+
readonly tenantId: TenantId;
|
|
82
|
+
readonly runId: RunId;
|
|
83
|
+
readonly kind: LogKind;
|
|
84
|
+
readonly scope: MemoryScope;
|
|
85
|
+
readonly actor?: string;
|
|
86
|
+
readonly payload?: unknown;
|
|
87
|
+
readonly causedByLogId?: string;
|
|
88
|
+
}
|
|
89
|
+
export interface ReadLogInput {
|
|
90
|
+
readonly tenantId: TenantId;
|
|
91
|
+
readonly runId: RunId;
|
|
92
|
+
readonly sinceSequence?: number;
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=memory-binding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-binding.d.ts","sourceRoot":"","sources":["../src/memory-binding.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,KAAK,EACV,IAAI,EACJ,QAAQ,EACR,OAAO,EACP,WAAW,EACX,WAAW,EACX,YAAY,EACb,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE7D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,SAAS,CAAC,QAAQ,GAAG,OAAO,EAC1B,KAAK,EAAE,cAAc,GACpB,OAAO,CAAC,MAAM,CAAC,SAAS,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC,CAAC;IAE3D;;;;OAIG;IACH,eAAe,CAAC,QAAQ,GAAG,OAAO,EAChC,KAAK,EAAE,oBAAoB,GAC1B,OAAO,CAAC,MAAM,CAAC,SAAS,YAAY,CAAC,QAAQ,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC,CAAC;IAEnE;;;;OAIG;IACH,gBAAgB,CAAC,QAAQ,GAAG,OAAO,EACjC,KAAK,EAAE,qBAAqB,GAC3B,OAAO,CAAC,MAAM,CAAC,SAAS,YAAY,CAAC,QAAQ,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC,CAAC;IAEnE;;;;OAIG;IACH,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC;IAEzE;;;;;OAKG;IACH,OAAO,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,SAAS,QAAQ,EAAE,EAAE,WAAW,CAAC,CAAC,CAAC;CACjF;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IACtC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;CAC/B;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IACtC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,iBAAiB,EAAE,yBAAyB,CAAC;IACtD;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IACtC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;CACjC;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;CACjC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-binding.js","sourceRoot":"","sources":["../src/memory-binding.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Fact } from './types.js';
|
|
2
|
+
/** Result of a retrieval search — fact plus a numeric score. */
|
|
3
|
+
export interface RetrievalHit<TContent = unknown> {
|
|
4
|
+
readonly fact: Fact<TContent>;
|
|
5
|
+
/**
|
|
6
|
+
* Score interpretation depends on the search kind. Keyword scores are
|
|
7
|
+
* full-text ranks, such as Postgres `ts_rank_cd` (unbounded positive;
|
|
8
|
+
* higher = better). Semantic uses cosine similarity in [-1, 1]
|
|
9
|
+
* (higher = better).
|
|
10
|
+
*/
|
|
11
|
+
readonly score: number;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=retrieval.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retrieval.d.ts","sourceRoot":"","sources":["../src/retrieval.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAEvC,gEAAgE;AAChE,MAAM,WAAW,YAAY,CAAC,QAAQ,GAAG,OAAO;IAC9C,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9B;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retrieval.js","sourceRoot":"","sources":["../src/retrieval.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { FactId, OrgId, ProjectId, SessionId, TenantId, ThreadId, Timestamp, UserId } from '@kindgi/types';
|
|
2
|
+
/**
|
|
3
|
+
* Every memory item is scoped. Reads and writes present a scope; the
|
|
4
|
+
* policy engine denies out-of-scope operations at the boundary.
|
|
5
|
+
*
|
|
6
|
+
* Exported as `MemoryScope` (not `Scope`) to disambiguate from
|
|
7
|
+
* `@kindgi/platform`'s `Scope` — a discriminated union describing org
|
|
8
|
+
* hierarchy position, distinct from memory's user/thread address.
|
|
9
|
+
*/
|
|
10
|
+
export interface MemoryScope {
|
|
11
|
+
readonly tenantId: TenantId;
|
|
12
|
+
readonly userId?: UserId;
|
|
13
|
+
readonly orgId?: OrgId;
|
|
14
|
+
/**
|
|
15
|
+
* Project scope — whatever unit of work a pack organizes its memory
|
|
16
|
+
* around; the memory layer stays neutral about what it represents.
|
|
17
|
+
*/
|
|
18
|
+
readonly projectId?: ProjectId;
|
|
19
|
+
readonly threadId?: ThreadId;
|
|
20
|
+
readonly sessionId?: SessionId;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Retention override at the fact level. Tenant policy sets defaults; a
|
|
24
|
+
* fact-level override wins if present.
|
|
25
|
+
*/
|
|
26
|
+
export interface Retention {
|
|
27
|
+
readonly keepUntil?: Timestamp;
|
|
28
|
+
readonly keepDays?: number;
|
|
29
|
+
readonly legalHold?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* External source of a Kind-B (cached-view) fact. Absent for Kind-A
|
|
33
|
+
* (immutable historical) facts.
|
|
34
|
+
*/
|
|
35
|
+
export interface Source {
|
|
36
|
+
readonly kind: 'http-api' | 'blob' | 'mcp-tool' | 'external-db' | 'user-input';
|
|
37
|
+
readonly uri?: string;
|
|
38
|
+
readonly freshness: SourceFreshness;
|
|
39
|
+
readonly refresh: SourceRefresh;
|
|
40
|
+
}
|
|
41
|
+
export interface SourceFreshness {
|
|
42
|
+
readonly ttlSeconds?: number;
|
|
43
|
+
readonly lastVerifiedAt?: Timestamp;
|
|
44
|
+
readonly etag?: string;
|
|
45
|
+
readonly sourceVersion?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface SourceRefresh {
|
|
48
|
+
/**
|
|
49
|
+
* `on-read` = reader triggers a refresh check before returning stale.
|
|
50
|
+
* `background` = a scheduled run walks stale facts and refreshes them.
|
|
51
|
+
* `manual` = only refresh when explicitly invoked.
|
|
52
|
+
*/
|
|
53
|
+
readonly strategy: 'on-read' | 'background' | 'manual';
|
|
54
|
+
/**
|
|
55
|
+
* Registered refresh-handler id, resolved by the memory implementation
|
|
56
|
+
* (`refresh-handler-missing` when it does not resolve).
|
|
57
|
+
*/
|
|
58
|
+
readonly handler?: string;
|
|
59
|
+
readonly priority?: number;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A typed, versioned record. Two flavors expressible via the same shape:
|
|
63
|
+
* - Kind A (immutable output) — no `source` block.
|
|
64
|
+
* - Kind B (cached view of external state) — has a `source` block.
|
|
65
|
+
*
|
|
66
|
+
* Retrieval strategy (which indexes populate for this `type`) is
|
|
67
|
+
* configured once per type in the memory implementation. There is no
|
|
68
|
+
* per-fact retrieval hint.
|
|
69
|
+
*/
|
|
70
|
+
export interface Fact<TContent = unknown> {
|
|
71
|
+
readonly id: FactId;
|
|
72
|
+
/**
|
|
73
|
+
* Fact type identifier. Packs define their own; a few general-purpose
|
|
74
|
+
* names are conventional ('working-memory', 'user-profile', 'summary',
|
|
75
|
+
* 'entity-index').
|
|
76
|
+
*/
|
|
77
|
+
readonly type: string;
|
|
78
|
+
readonly scope: MemoryScope;
|
|
79
|
+
/** Monotonic version within `(scope, id)`. Supersession increments. */
|
|
80
|
+
readonly version: number;
|
|
81
|
+
readonly createdAt: Timestamp;
|
|
82
|
+
readonly updatedAt?: Timestamp;
|
|
83
|
+
readonly content?: TContent;
|
|
84
|
+
/**
|
|
85
|
+
* `blob://<provider>/<bucket>/<key>` when the payload is stored externally.
|
|
86
|
+
* `content` may still hold a summary / fingerprint for cheap retrieval scoring.
|
|
87
|
+
*/
|
|
88
|
+
readonly contentRef?: string;
|
|
89
|
+
readonly contentHash?: string;
|
|
90
|
+
readonly size?: number;
|
|
91
|
+
readonly embeddingModel?: string;
|
|
92
|
+
readonly retention?: Retention;
|
|
93
|
+
readonly source?: Source;
|
|
94
|
+
readonly causedByLogId?: readonly string[];
|
|
95
|
+
readonly supersedes?: FactId;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,MAAM,EACN,KAAK,EACL,SAAS,EACT,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,MAAM,EACP,MAAM,eAAe,CAAC;AAEvB;;;;;;;GAOG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;IACvB;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;IAC/B,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;CAChC;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;IAC/B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED;;;GAGG;AACH,MAAM,WAAW,MAAM;IACrB,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,UAAU,GAAG,aAAa,GAAG,YAAY,CAAC;IAC/E,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,SAAS,EAAE,eAAe,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC;CACjC;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,CAAC;IACpC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;CACjC;AAED,MAAM,WAAW,aAAa;IAC5B;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,SAAS,GAAG,YAAY,GAAG,QAAQ,CAAC;IACvD;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,IAAI,CAAC,QAAQ,GAAG,OAAO;IACtC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,uEAAuE;IACvE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;IAC9B,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;IAC/B,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC;IAC5B;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;IAC/B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3C,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kindgi/memory",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Kindgi™ memory type surface. Public type contract for the memory subsystem: Fact (typed versioned record, Kind-A immutable or Kind-B cached-view), Retention (fact-level retention override), MemoryScope (where in the tenant/user/thread hierarchy a Fact lives), Source + SourceFreshness + SourceRefresh (external-source metadata for Kind-B facts). Also LogEntry + LOG_KINDS (hash-chained run log), RetrievalHit, the MemoryError variants, and MemoryQueryBinding (the data-access interface a binding implementation provides). Types and interfaces only — no runtime state. Storage, indexes, retrieval, and retention sweeps are provided by a binding implementation, such as the Postgres-backed one in the Kindgi runtime.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
|
-
"repository": {
|
|
7
|
-
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/kindgi/kindgi-sdk.git",
|
|
9
|
+
"directory": "packages/memory"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/kindgi/kindgi-sdk/tree/main/packages/memory#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/kindgi/kindgi-sdk/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"src",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@kindgi/embedding": "0.1.0",
|
|
31
|
+
"@kindgi/types": "0.1.0"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=22.0.0"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"provenance": true
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^22.10.5",
|
|
42
|
+
"typescript": "^5.7.3",
|
|
43
|
+
"vitest": "^2.1.8"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsc -p tsconfig.build.json",
|
|
47
|
+
"typecheck": "tsc --noEmit",
|
|
48
|
+
"test": "vitest run --typecheck --typecheck.only --passWithNoTests",
|
|
49
|
+
"clean": "rm -rf dist *.tsbuildinfo"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
import type { EmbeddingError } from '@kindgi/embedding';
|
|
5
|
+
import type { FactId, LogEntryId } from '@kindgi/types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Errors emitted by the memory subsystem. Every variant carries a `code`
|
|
9
|
+
* for pattern matching; messages are human-readable, not API contract.
|
|
10
|
+
*
|
|
11
|
+
* `EmbeddingError` from @kindgi/embedding is folded in — an
|
|
12
|
+
* implementation that embeds facts on write, and `searchBySemantic`,
|
|
13
|
+
* resolve providers via the registry and may surface any of its
|
|
14
|
+
* rejection modes (no-embedding-provider, unknown-embedding-model,
|
|
15
|
+
* ambiguous-default-provider).
|
|
16
|
+
*/
|
|
17
|
+
export type MemoryError =
|
|
18
|
+
| InvalidLogEntryError
|
|
19
|
+
| InvalidFactError
|
|
20
|
+
| FactNotFoundError
|
|
21
|
+
| LogNotFoundError
|
|
22
|
+
| EmbeddingError
|
|
23
|
+
| RefreshHandlerMissingError
|
|
24
|
+
| RetentionViolationError
|
|
25
|
+
| PersistenceError;
|
|
26
|
+
|
|
27
|
+
export interface InvalidLogEntryError {
|
|
28
|
+
readonly code: 'invalid-log-entry';
|
|
29
|
+
readonly message: string;
|
|
30
|
+
readonly reason: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface InvalidFactError {
|
|
34
|
+
readonly code: 'invalid-fact';
|
|
35
|
+
readonly message: string;
|
|
36
|
+
readonly reason: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface FactNotFoundError {
|
|
40
|
+
readonly code: 'fact-not-found';
|
|
41
|
+
readonly message: string;
|
|
42
|
+
readonly factId: FactId;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface LogNotFoundError {
|
|
46
|
+
readonly code: 'log-not-found';
|
|
47
|
+
readonly message: string;
|
|
48
|
+
readonly logEntryId: LogEntryId;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A Kind-B fact needs on-read refresh but its `source.refresh.handler`
|
|
53
|
+
* doesn't resolve to a registered handler.
|
|
54
|
+
*/
|
|
55
|
+
export interface RefreshHandlerMissingError {
|
|
56
|
+
readonly code: 'refresh-handler-missing';
|
|
57
|
+
readonly message: string;
|
|
58
|
+
readonly handlerId: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A write attempted on a legal-hold fact (retention.legalHold = true) or
|
|
63
|
+
* a delete attempted on a fact whose retention window is still open.
|
|
64
|
+
*/
|
|
65
|
+
export interface RetentionViolationError {
|
|
66
|
+
readonly code: 'retention-violation';
|
|
67
|
+
readonly message: string;
|
|
68
|
+
readonly factId: FactId;
|
|
69
|
+
readonly reason: 'legal-hold' | 'keep-until' | 'keep-days';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface PersistenceError {
|
|
73
|
+
readonly code: 'persistence-error';
|
|
74
|
+
readonly message: string;
|
|
75
|
+
readonly cause: unknown;
|
|
76
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
// ============ Wire types ============
|
|
5
|
+
export type {
|
|
6
|
+
Fact,
|
|
7
|
+
MemoryScope,
|
|
8
|
+
Retention,
|
|
9
|
+
Source,
|
|
10
|
+
SourceFreshness,
|
|
11
|
+
SourceRefresh,
|
|
12
|
+
} from './types.js';
|
|
13
|
+
|
|
14
|
+
// ============ Log types ============
|
|
15
|
+
export { LOG_KINDS } from './log.js';
|
|
16
|
+
export type { LogEntry, LogKind } from './log.js';
|
|
17
|
+
|
|
18
|
+
// ============ Retrieval types ============
|
|
19
|
+
export type { RetrievalHit } from './retrieval.js';
|
|
20
|
+
|
|
21
|
+
// ============ Errors ============
|
|
22
|
+
export type {
|
|
23
|
+
FactNotFoundError,
|
|
24
|
+
InvalidFactError,
|
|
25
|
+
InvalidLogEntryError,
|
|
26
|
+
LogNotFoundError,
|
|
27
|
+
MemoryError,
|
|
28
|
+
PersistenceError,
|
|
29
|
+
RefreshHandlerMissingError,
|
|
30
|
+
RetentionViolationError,
|
|
31
|
+
} from './errors.js';
|
|
32
|
+
|
|
33
|
+
// ============ MemoryQueryBinding — caller-plugged data-access surface ============
|
|
34
|
+
export type {
|
|
35
|
+
AppendLogInput,
|
|
36
|
+
ListFactsInput,
|
|
37
|
+
MemoryQueryBinding,
|
|
38
|
+
ReadLogInput,
|
|
39
|
+
SearchByKeywordInput,
|
|
40
|
+
SearchBySemanticInput,
|
|
41
|
+
} from './memory-binding.js';
|
package/src/log.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
import type { LogEntryId, RunId, Timestamp } from '@kindgi/types';
|
|
5
|
+
|
|
6
|
+
import type { MemoryScope } from './types.js';
|
|
7
|
+
|
|
8
|
+
/** Kind identifiers for LogEntry — matches the enum in `@kindgi/specs/memory.schema.json`. */
|
|
9
|
+
export const LOG_KINDS = [
|
|
10
|
+
'user-message',
|
|
11
|
+
'agent-message',
|
|
12
|
+
'system-message',
|
|
13
|
+
'tool-call',
|
|
14
|
+
'tool-result',
|
|
15
|
+
'internal-thought',
|
|
16
|
+
'retrieval',
|
|
17
|
+
'artifact-produced',
|
|
18
|
+
'event-emitted',
|
|
19
|
+
'event-received',
|
|
20
|
+
'guardrail-triggered',
|
|
21
|
+
'wait-suspended',
|
|
22
|
+
'wait-resumed',
|
|
23
|
+
] as const;
|
|
24
|
+
|
|
25
|
+
export type LogKind = (typeof LOG_KINDS)[number];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One event in the append-only log for a run / thread / session. Immutable
|
|
29
|
+
* once written. Ordered by (sequence, timestamp) within `(tenantId, runId)`.
|
|
30
|
+
*
|
|
31
|
+
* Every entry commits to a hash of the previous entry in the same
|
|
32
|
+
* `(tenantId, runId)` chain via `prevHash` → `entryHash`. Tamper with any
|
|
33
|
+
* past entry and every subsequent entry's `prevHash` breaks — the whole
|
|
34
|
+
* chain is one Merkle-lite structure.
|
|
35
|
+
*/
|
|
36
|
+
export interface LogEntry {
|
|
37
|
+
readonly id: LogEntryId;
|
|
38
|
+
readonly kind: LogKind;
|
|
39
|
+
readonly scope: MemoryScope;
|
|
40
|
+
readonly runId: RunId;
|
|
41
|
+
readonly actor?: string;
|
|
42
|
+
readonly timestamp: Timestamp;
|
|
43
|
+
/** Monotonic sequence within `(tenantId, runId)`. */
|
|
44
|
+
readonly sequence: number;
|
|
45
|
+
readonly payload?: unknown;
|
|
46
|
+
readonly contentHash?: string;
|
|
47
|
+
/** Zero-hash for sequence 0. */
|
|
48
|
+
readonly prevHash: string;
|
|
49
|
+
readonly entryHash: string;
|
|
50
|
+
readonly causedByLogId?: string;
|
|
51
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
import type { EmbeddingProviderRegistry } from '@kindgi/embedding';
|
|
5
|
+
import type {
|
|
6
|
+
Fact,
|
|
7
|
+
LogEntry,
|
|
8
|
+
LogKind,
|
|
9
|
+
MemoryError,
|
|
10
|
+
MemoryScope,
|
|
11
|
+
RetrievalHit,
|
|
12
|
+
} from '@kindgi/memory';
|
|
13
|
+
import type { Result, RunId, TenantId } from '@kindgi/types';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Caller-plugged data-access surface for the memory subsystem. Agent
|
|
17
|
+
* code routes its memory reads through this binding — @kindgi/agents
|
|
18
|
+
* never touches a database client directly.
|
|
19
|
+
*
|
|
20
|
+
* The Kindgi runtime supplies a Postgres-backed implementation; any
|
|
21
|
+
* object satisfying the interface works (tests, other stores). Agents
|
|
22
|
+
* receive it as `InvokeAgentBindings.memoryBinding`.
|
|
23
|
+
*
|
|
24
|
+
* Contract notes:
|
|
25
|
+
* - Every method is tenant-scoped via `input.tenantId`. The binding
|
|
26
|
+
* implementation enforces tenant isolation; callers just supply the
|
|
27
|
+
* tenant id.
|
|
28
|
+
* - Provenance emission is internal to the implementation — the
|
|
29
|
+
* binding may record provenance events, but the public interface
|
|
30
|
+
* never accepts or returns a `ProvenanceBuilder`.
|
|
31
|
+
*/
|
|
32
|
+
export interface MemoryQueryBinding {
|
|
33
|
+
/**
|
|
34
|
+
* List facts filtered by tenant, type, and scope, capped by `limit`
|
|
35
|
+
* (no pagination cursor). Default: latest version per `(type, id)`.
|
|
36
|
+
*/
|
|
37
|
+
listFacts<TContent = unknown>(
|
|
38
|
+
input: ListFactsInput,
|
|
39
|
+
): Promise<Result<readonly Fact<TContent>[], MemoryError>>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Full-text keyword search over facts. Tokenizer and ranking are
|
|
43
|
+
* implementation-defined; the Kindgi runtime's Postgres implementation
|
|
44
|
+
* ranks with `ts_rank_cd` and tokenizes English.
|
|
45
|
+
*/
|
|
46
|
+
searchByKeyword<TContent = unknown>(
|
|
47
|
+
input: SearchByKeywordInput,
|
|
48
|
+
): Promise<Result<readonly RetrievalHit<TContent>[], MemoryError>>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Semantic-search facts via a caller-supplied `EmbeddingProviderRegistry`.
|
|
52
|
+
* The registry resolves a provider (by model name or default) that
|
|
53
|
+
* embeds the query; the impl runs the vector search.
|
|
54
|
+
*/
|
|
55
|
+
searchBySemantic<TContent = unknown>(
|
|
56
|
+
input: SearchBySemanticInput,
|
|
57
|
+
): Promise<Result<readonly RetrievalHit<TContent>[], MemoryError>>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Append one entry to the run's hash-chained log. The implementation
|
|
61
|
+
* assigns `sequence` and `prevHash` / `entryHash` atomically; the
|
|
62
|
+
* returned `LogEntry` is the entry as stored.
|
|
63
|
+
*/
|
|
64
|
+
appendLog(input: AppendLogInput): Promise<Result<LogEntry, MemoryError>>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Read the log entries for `(tenantId, runId)` in `(sequence, timestamp)`
|
|
68
|
+
* order. `sinceSequence` is an inclusive lower bound (entries with
|
|
69
|
+
* `sequence >= sinceSequence`); to resume after a checkpoint, pass the
|
|
70
|
+
* last seen sequence + 1.
|
|
71
|
+
*/
|
|
72
|
+
readLog(input: ReadLogInput): Promise<Result<readonly LogEntry[], MemoryError>>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface ListFactsInput {
|
|
76
|
+
readonly tenantId: TenantId;
|
|
77
|
+
readonly type?: string;
|
|
78
|
+
readonly scope?: Partial<MemoryScope>;
|
|
79
|
+
readonly limit?: number;
|
|
80
|
+
readonly latestOnly?: boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface SearchByKeywordInput {
|
|
84
|
+
readonly tenantId: TenantId;
|
|
85
|
+
readonly query: string;
|
|
86
|
+
readonly type?: string;
|
|
87
|
+
readonly scope?: Partial<MemoryScope>;
|
|
88
|
+
readonly topK?: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface SearchBySemanticInput {
|
|
92
|
+
readonly tenantId: TenantId;
|
|
93
|
+
readonly query: string;
|
|
94
|
+
readonly embeddingRegistry: EmbeddingProviderRegistry;
|
|
95
|
+
/**
|
|
96
|
+
* Which registered embedding model to use. Omit to fall back to the
|
|
97
|
+
* registry's sole registered provider (errors if zero or ambiguous).
|
|
98
|
+
*/
|
|
99
|
+
readonly embeddingModel?: string;
|
|
100
|
+
readonly type?: string;
|
|
101
|
+
readonly scope?: Partial<MemoryScope>;
|
|
102
|
+
readonly topK?: number;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface AppendLogInput {
|
|
106
|
+
readonly tenantId: TenantId;
|
|
107
|
+
readonly runId: RunId;
|
|
108
|
+
readonly kind: LogKind;
|
|
109
|
+
readonly scope: MemoryScope;
|
|
110
|
+
readonly actor?: string;
|
|
111
|
+
readonly payload?: unknown;
|
|
112
|
+
readonly causedByLogId?: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface ReadLogInput {
|
|
116
|
+
readonly tenantId: TenantId;
|
|
117
|
+
readonly runId: RunId;
|
|
118
|
+
readonly sinceSequence?: number;
|
|
119
|
+
}
|
package/src/retrieval.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
import type { Fact } from './types.js';
|
|
5
|
+
|
|
6
|
+
/** Result of a retrieval search — fact plus a numeric score. */
|
|
7
|
+
export interface RetrievalHit<TContent = unknown> {
|
|
8
|
+
readonly fact: Fact<TContent>;
|
|
9
|
+
/**
|
|
10
|
+
* Score interpretation depends on the search kind. Keyword scores are
|
|
11
|
+
* full-text ranks, such as Postgres `ts_rank_cd` (unbounded positive;
|
|
12
|
+
* higher = better). Semantic uses cosine similarity in [-1, 1]
|
|
13
|
+
* (higher = better).
|
|
14
|
+
*/
|
|
15
|
+
readonly score: number;
|
|
16
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
FactId,
|
|
6
|
+
OrgId,
|
|
7
|
+
ProjectId,
|
|
8
|
+
SessionId,
|
|
9
|
+
TenantId,
|
|
10
|
+
ThreadId,
|
|
11
|
+
Timestamp,
|
|
12
|
+
UserId,
|
|
13
|
+
} from '@kindgi/types';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Every memory item is scoped. Reads and writes present a scope; the
|
|
17
|
+
* policy engine denies out-of-scope operations at the boundary.
|
|
18
|
+
*
|
|
19
|
+
* Exported as `MemoryScope` (not `Scope`) to disambiguate from
|
|
20
|
+
* `@kindgi/platform`'s `Scope` — a discriminated union describing org
|
|
21
|
+
* hierarchy position, distinct from memory's user/thread address.
|
|
22
|
+
*/
|
|
23
|
+
export interface MemoryScope {
|
|
24
|
+
readonly tenantId: TenantId;
|
|
25
|
+
readonly userId?: UserId;
|
|
26
|
+
readonly orgId?: OrgId;
|
|
27
|
+
/**
|
|
28
|
+
* Project scope — whatever unit of work a pack organizes its memory
|
|
29
|
+
* around; the memory layer stays neutral about what it represents.
|
|
30
|
+
*/
|
|
31
|
+
readonly projectId?: ProjectId;
|
|
32
|
+
readonly threadId?: ThreadId;
|
|
33
|
+
readonly sessionId?: SessionId;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Retention override at the fact level. Tenant policy sets defaults; a
|
|
38
|
+
* fact-level override wins if present.
|
|
39
|
+
*/
|
|
40
|
+
export interface Retention {
|
|
41
|
+
readonly keepUntil?: Timestamp;
|
|
42
|
+
readonly keepDays?: number;
|
|
43
|
+
readonly legalHold?: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* External source of a Kind-B (cached-view) fact. Absent for Kind-A
|
|
48
|
+
* (immutable historical) facts.
|
|
49
|
+
*/
|
|
50
|
+
export interface Source {
|
|
51
|
+
readonly kind: 'http-api' | 'blob' | 'mcp-tool' | 'external-db' | 'user-input';
|
|
52
|
+
readonly uri?: string;
|
|
53
|
+
readonly freshness: SourceFreshness;
|
|
54
|
+
readonly refresh: SourceRefresh;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface SourceFreshness {
|
|
58
|
+
readonly ttlSeconds?: number;
|
|
59
|
+
readonly lastVerifiedAt?: Timestamp;
|
|
60
|
+
readonly etag?: string;
|
|
61
|
+
readonly sourceVersion?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface SourceRefresh {
|
|
65
|
+
/**
|
|
66
|
+
* `on-read` = reader triggers a refresh check before returning stale.
|
|
67
|
+
* `background` = a scheduled run walks stale facts and refreshes them.
|
|
68
|
+
* `manual` = only refresh when explicitly invoked.
|
|
69
|
+
*/
|
|
70
|
+
readonly strategy: 'on-read' | 'background' | 'manual';
|
|
71
|
+
/**
|
|
72
|
+
* Registered refresh-handler id, resolved by the memory implementation
|
|
73
|
+
* (`refresh-handler-missing` when it does not resolve).
|
|
74
|
+
*/
|
|
75
|
+
readonly handler?: string;
|
|
76
|
+
readonly priority?: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A typed, versioned record. Two flavors expressible via the same shape:
|
|
81
|
+
* - Kind A (immutable output) — no `source` block.
|
|
82
|
+
* - Kind B (cached view of external state) — has a `source` block.
|
|
83
|
+
*
|
|
84
|
+
* Retrieval strategy (which indexes populate for this `type`) is
|
|
85
|
+
* configured once per type in the memory implementation. There is no
|
|
86
|
+
* per-fact retrieval hint.
|
|
87
|
+
*/
|
|
88
|
+
export interface Fact<TContent = unknown> {
|
|
89
|
+
readonly id: FactId;
|
|
90
|
+
/**
|
|
91
|
+
* Fact type identifier. Packs define their own; a few general-purpose
|
|
92
|
+
* names are conventional ('working-memory', 'user-profile', 'summary',
|
|
93
|
+
* 'entity-index').
|
|
94
|
+
*/
|
|
95
|
+
readonly type: string;
|
|
96
|
+
readonly scope: MemoryScope;
|
|
97
|
+
/** Monotonic version within `(scope, id)`. Supersession increments. */
|
|
98
|
+
readonly version: number;
|
|
99
|
+
readonly createdAt: Timestamp;
|
|
100
|
+
readonly updatedAt?: Timestamp;
|
|
101
|
+
readonly content?: TContent;
|
|
102
|
+
/**
|
|
103
|
+
* `blob://<provider>/<bucket>/<key>` when the payload is stored externally.
|
|
104
|
+
* `content` may still hold a summary / fingerprint for cheap retrieval scoring.
|
|
105
|
+
*/
|
|
106
|
+
readonly contentRef?: string;
|
|
107
|
+
readonly contentHash?: string;
|
|
108
|
+
readonly size?: number;
|
|
109
|
+
readonly embeddingModel?: string;
|
|
110
|
+
readonly retention?: Retention;
|
|
111
|
+
readonly source?: Source;
|
|
112
|
+
readonly causedByLogId?: readonly string[];
|
|
113
|
+
readonly supersedes?: FactId;
|
|
114
|
+
}
|