agentfootprint 9.2.0 → 9.3.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/dist/adapters/memory/pgVector.js +731 -0
- package/dist/adapters/memory/pgVector.js.map +1 -0
- package/dist/adapters/memory/s3Vectors.js +628 -0
- package/dist/adapters/memory/s3Vectors.js.map +1 -0
- package/dist/adapters/memory/sqliteVector.js +16 -79
- package/dist/adapters/memory/sqliteVector.js.map +1 -1
- package/dist/embedders/index.js +240 -76
- package/dist/embedders/index.js.map +1 -1
- package/dist/esm/adapters/memory/pgVector.d.ts +243 -0
- package/dist/esm/adapters/memory/pgVector.js +726 -0
- package/dist/esm/adapters/memory/pgVector.js.map +1 -0
- package/dist/esm/adapters/memory/s3Vectors.d.ts +208 -0
- package/dist/esm/adapters/memory/s3Vectors.js +624 -0
- package/dist/esm/adapters/memory/s3Vectors.js.map +1 -0
- package/dist/esm/adapters/memory/sqliteVector.d.ts +5 -27
- package/dist/esm/adapters/memory/sqliteVector.js +10 -73
- package/dist/esm/adapters/memory/sqliteVector.js.map +1 -1
- package/dist/esm/embedders/index.d.ts +99 -19
- package/dist/esm/embedders/index.js +240 -76
- package/dist/esm/embedders/index.js.map +1 -1
- package/dist/esm/lib/embedderMismatch.d.ts +67 -0
- package/dist/esm/lib/embedderMismatch.js +96 -0
- package/dist/esm/lib/embedderMismatch.js.map +1 -0
- package/dist/esm/lib/rag/defineRAG.d.ts +28 -6
- package/dist/esm/lib/rag/defineRAG.js +39 -5
- package/dist/esm/lib/rag/defineRAG.js.map +1 -1
- package/dist/esm/memory/define.js +31 -1
- package/dist/esm/memory/define.js.map +1 -1
- package/dist/esm/memory/define.types.d.ts +13 -2
- package/dist/esm/memory/define.types.js.map +1 -1
- package/dist/esm/memory/embedding/loadRelevant.d.ts +10 -2
- package/dist/esm/memory/embedding/loadRelevant.js +36 -18
- package/dist/esm/memory/embedding/loadRelevant.js.map +1 -1
- package/dist/esm/memory/pipeline/semantic.d.ts +13 -2
- package/dist/esm/memory/pipeline/semantic.js +11 -5
- package/dist/esm/memory/pipeline/semantic.js.map +1 -1
- package/dist/esm/memory/store/capability.d.ts +25 -6
- package/dist/esm/memory/store/capability.js +68 -3
- package/dist/esm/memory/store/capability.js.map +1 -1
- package/dist/esm/memory/store/index.d.ts +1 -0
- package/dist/esm/memory/store/index.js +4 -0
- package/dist/esm/memory/store/index.js.map +1 -1
- package/dist/esm/memory/store/types.d.ts +40 -0
- package/dist/esm/memory-providers.d.ts +7 -4
- package/dist/esm/memory-providers.js +18 -4
- package/dist/esm/memory-providers.js.map +1 -1
- package/dist/lib/embedderMismatch.js +103 -0
- package/dist/lib/embedderMismatch.js.map +1 -0
- package/dist/lib/rag/defineRAG.js +39 -5
- package/dist/lib/rag/defineRAG.js.map +1 -1
- package/dist/memory/define.js +31 -1
- package/dist/memory/define.js.map +1 -1
- package/dist/memory/define.types.js.map +1 -1
- package/dist/memory/embedding/loadRelevant.js +36 -18
- package/dist/memory/embedding/loadRelevant.js.map +1 -1
- package/dist/memory/pipeline/semantic.js +11 -5
- package/dist/memory/pipeline/semantic.js.map +1 -1
- package/dist/memory/store/capability.js +70 -4
- package/dist/memory/store/capability.js.map +1 -1
- package/dist/memory/store/index.js +7 -1
- package/dist/memory/store/index.js.map +1 -1
- package/dist/memory-providers.js +22 -5
- package/dist/memory-providers.js.map +1 -1
- package/dist/types/adapters/memory/pgVector.d.ts +244 -0
- package/dist/types/adapters/memory/pgVector.d.ts.map +1 -0
- package/dist/types/adapters/memory/s3Vectors.d.ts +209 -0
- package/dist/types/adapters/memory/s3Vectors.d.ts.map +1 -0
- package/dist/types/adapters/memory/sqliteVector.d.ts +5 -27
- package/dist/types/adapters/memory/sqliteVector.d.ts.map +1 -1
- package/dist/types/embedders/index.d.ts +99 -19
- package/dist/types/embedders/index.d.ts.map +1 -1
- package/dist/types/lib/embedderMismatch.d.ts +68 -0
- package/dist/types/lib/embedderMismatch.d.ts.map +1 -0
- package/dist/types/lib/rag/defineRAG.d.ts +28 -6
- package/dist/types/lib/rag/defineRAG.d.ts.map +1 -1
- package/dist/types/memory/define.d.ts.map +1 -1
- package/dist/types/memory/define.types.d.ts +13 -2
- package/dist/types/memory/define.types.d.ts.map +1 -1
- package/dist/types/memory/embedding/loadRelevant.d.ts +10 -2
- package/dist/types/memory/embedding/loadRelevant.d.ts.map +1 -1
- package/dist/types/memory/pipeline/semantic.d.ts +13 -2
- package/dist/types/memory/pipeline/semantic.d.ts.map +1 -1
- package/dist/types/memory/store/capability.d.ts +25 -6
- package/dist/types/memory/store/capability.d.ts.map +1 -1
- package/dist/types/memory/store/index.d.ts +1 -0
- package/dist/types/memory/store/index.d.ts.map +1 -1
- package/dist/types/memory/store/types.d.ts +40 -0
- package/dist/types/memory/store/types.d.ts.map +1 -1
- package/dist/types/memory-providers.d.ts +7 -4
- package/dist/types/memory-providers.d.ts.map +1 -1
- package/package.json +9 -1
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* adapters/memory/pgVector — the target our own port has named since 2.x.
|
|
3
|
+
*
|
|
4
|
+
* `MemoryStore`'s docstring has listed the backends it was designed for from
|
|
5
|
+
* the beginning — *"Every storage backend (InMemory, Redis, DynamoDB,
|
|
6
|
+
* **Postgres**, Bedrock AgentCore) implements this interface"* — and named the
|
|
7
|
+
* query, twice, in the places an implementer would look: *"**Postgres**:
|
|
8
|
+
* multi-row INSERT … ON CONFLICT DO UPDATE"* for `putMany`, *"**pgvector**:
|
|
9
|
+
* `ORDER BY embedding <=> query LIMIT k`"* for `search`. Every one of those
|
|
10
|
+
* sentences was true about the design and false about the shipped package.
|
|
11
|
+
* This is the adapter that makes them the same sentence.
|
|
12
|
+
*
|
|
13
|
+
* It matters more than one row in a table of backends. Postgres is the database
|
|
14
|
+
* most teams already run, `pgvector` is an extension away, and a corpus that
|
|
15
|
+
* lives beside the application's own data inherits its backups, its failover,
|
|
16
|
+
* its access control and its migrations. `sqliteVectorStore` is one machine;
|
|
17
|
+
* `s3VectorsStore` is serverless and eventually consistent; this is the one for
|
|
18
|
+
* a fleet that already has a database.
|
|
19
|
+
*
|
|
20
|
+
* ── The table, and why this does not create it ──────────────────────────────
|
|
21
|
+
* A `vector(N)` column fixes N at creation, and N is a fact about your
|
|
22
|
+
* embedder. Creating the table implicitly would pick that number — and the
|
|
23
|
+
* index type, and the operator class — on your behalf, in a migration you never
|
|
24
|
+
* reviewed, in a database whose DDL is usually somebody's job. So the schema is
|
|
25
|
+
* yours to run, and this store REFUSES a table that is missing rather than
|
|
26
|
+
* silently answering "no matches" against nothing:
|
|
27
|
+
*
|
|
28
|
+
* ```sql
|
|
29
|
+
* CREATE EXTENSION IF NOT EXISTS vector;
|
|
30
|
+
*
|
|
31
|
+
* -- 1024 = your embedder's dimensions. bedrockEmbedder() default: 1024.
|
|
32
|
+
* -- openaiEmbedder() default: 1536. staticEmbedder(): 256.
|
|
33
|
+
* CREATE TABLE af_vectors (
|
|
34
|
+
* namespace TEXT NOT NULL,
|
|
35
|
+
* id TEXT NOT NULL,
|
|
36
|
+
* value JSONB NOT NULL,
|
|
37
|
+
* metadata JSONB,
|
|
38
|
+
* embedding vector(1024),
|
|
39
|
+
* embedder_fp TEXT,
|
|
40
|
+
* version INTEGER NOT NULL,
|
|
41
|
+
* created_at BIGINT NOT NULL,
|
|
42
|
+
* updated_at BIGINT NOT NULL,
|
|
43
|
+
* last_accessed_at BIGINT NOT NULL,
|
|
44
|
+
* access_count INTEGER NOT NULL,
|
|
45
|
+
* ttl BIGINT,
|
|
46
|
+
* tier TEXT,
|
|
47
|
+
* source JSONB,
|
|
48
|
+
* embedding_model TEXT,
|
|
49
|
+
* PRIMARY KEY (namespace, id)
|
|
50
|
+
* );
|
|
51
|
+
*
|
|
52
|
+
* -- Cosine, because that is the score this port reports and every threshold
|
|
53
|
+
* -- in this library is calibrated on. Match the operator class to the metric.
|
|
54
|
+
* CREATE INDEX af_vectors_hnsw ON af_vectors
|
|
55
|
+
* USING hnsw (embedding vector_cosine_ops);
|
|
56
|
+
* CREATE INDEX af_vectors_ns ON af_vectors (namespace);
|
|
57
|
+
*
|
|
58
|
+
* -- Recognition (`seen`/`recordSignature`), usefulness feedback, and the
|
|
59
|
+
* -- per-namespace embedder fingerprint. Small, and each one is a port method
|
|
60
|
+
* -- that would otherwise have to be refused.
|
|
61
|
+
* CREATE TABLE af_signatures (
|
|
62
|
+
* namespace TEXT NOT NULL, signature TEXT NOT NULL,
|
|
63
|
+
* PRIMARY KEY (namespace, signature)
|
|
64
|
+
* );
|
|
65
|
+
* CREATE TABLE af_feedback (
|
|
66
|
+
* namespace TEXT NOT NULL, id TEXT NOT NULL,
|
|
67
|
+
* total DOUBLE PRECISION NOT NULL, count INTEGER NOT NULL,
|
|
68
|
+
* PRIMARY KEY (namespace, id)
|
|
69
|
+
* );
|
|
70
|
+
* CREATE TABLE af_index_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* Every table and column name above is an OPTION with that value as its
|
|
74
|
+
* default, so this drops into a schema that already has naming conventions —
|
|
75
|
+
* see {@link PgVectorStoreOptions}. Identifiers are validated and quoted; a
|
|
76
|
+
* name that is not a plain SQL identifier is refused rather than interpolated.
|
|
77
|
+
*
|
|
78
|
+
* ── Cosine, and only cosine ─────────────────────────────────────────────────
|
|
79
|
+
* `search` is `1 - (embedding <=> $query::vector)` — pgvector's cosine
|
|
80
|
+
* DISTANCE, converted to the cosine SIMILARITY the port reports and
|
|
81
|
+
* `defineRAG`'s 0.7 default is calibrated on. `<->` (L2) and `<#>` (inner
|
|
82
|
+
* product) are deliberately not options: their ranges are not that range, and a
|
|
83
|
+
* number that reads like a cosine and is not one is the failure mode the whole
|
|
84
|
+
* fingerprint machinery exists to prevent. Build the HNSW index with
|
|
85
|
+
* `vector_cosine_ops` so the operator and the index agree — with the wrong
|
|
86
|
+
* operator class the query still returns the right answer, slowly, by scanning.
|
|
87
|
+
*
|
|
88
|
+
* ── One statement at a time, on purpose ─────────────────────────────────────
|
|
89
|
+
* The client here is anything with `query()` — a `pg.Pool` is the expected one,
|
|
90
|
+
* and a Pool hands each `query()` its own connection. `BEGIN` on one and the
|
|
91
|
+
* next statement on another is a transaction that silently is not one, so this
|
|
92
|
+
* adapter never writes multi-statement transactions. Everything that must be
|
|
93
|
+
* atomic is ONE statement: `putMany` is one multi-row upsert, `putIfVersion` is
|
|
94
|
+
* one conditional upsert, `forget` is one statement with CTEs across all four
|
|
95
|
+
* tables. That is a constraint that made the code better.
|
|
96
|
+
*
|
|
97
|
+
* ── Lazy peer dependency ────────────────────────────────────────────────────
|
|
98
|
+
* `pg` is an OPTIONAL peer dependency, required at construction time. Importing
|
|
99
|
+
* `agentfootprint/memory` costs nothing for consumers who never build one of
|
|
100
|
+
* these. Pass `client` to reuse the pool your app already has — which is the
|
|
101
|
+
* recommended shape, because a second pool to the same database is a second set
|
|
102
|
+
* of connections nobody counted.
|
|
103
|
+
*/
|
|
104
|
+
import type { MemoryIdentity } from '../../memory/identity/index.js';
|
|
105
|
+
import type { MemoryStore } from '../../memory/store/types.js';
|
|
106
|
+
/** One result set, as this adapter reads it. */
|
|
107
|
+
export interface PgQueryResult {
|
|
108
|
+
readonly rows: readonly Record<string, unknown>[];
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The slice of a `pg` client this adapter calls.
|
|
112
|
+
*
|
|
113
|
+
* Structural, so a `Pool`, a `Client`, a pgBouncer-fronted wrapper or a test
|
|
114
|
+
* double all satisfy it without this package taking a hard type dependency on
|
|
115
|
+
* the optional peer.
|
|
116
|
+
*/
|
|
117
|
+
export interface PgLikeClient {
|
|
118
|
+
query(text: string, params?: readonly unknown[]): Promise<PgQueryResult>;
|
|
119
|
+
/** Optional — awaited by {@link PgVectorStore.close} when this store built the pool. */
|
|
120
|
+
end?(): Promise<void>;
|
|
121
|
+
}
|
|
122
|
+
/** The one constructor this adapter needs out of `pg`. */
|
|
123
|
+
export interface PgSdkModule {
|
|
124
|
+
readonly Pool?: new (config: {
|
|
125
|
+
connectionString?: string;
|
|
126
|
+
}) => PgLikeClient;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Column names, so this store fits a schema that already has conventions.
|
|
130
|
+
* Every one defaults to the name in the `CREATE TABLE` above.
|
|
131
|
+
*/
|
|
132
|
+
export interface PgVectorColumns {
|
|
133
|
+
readonly namespace?: string;
|
|
134
|
+
readonly id?: string;
|
|
135
|
+
readonly value?: string;
|
|
136
|
+
readonly metadata?: string;
|
|
137
|
+
readonly embedding?: string;
|
|
138
|
+
readonly embedderFp?: string;
|
|
139
|
+
readonly version?: string;
|
|
140
|
+
readonly createdAt?: string;
|
|
141
|
+
readonly updatedAt?: string;
|
|
142
|
+
readonly lastAccessedAt?: string;
|
|
143
|
+
readonly accessCount?: string;
|
|
144
|
+
readonly ttl?: string;
|
|
145
|
+
readonly tier?: string;
|
|
146
|
+
readonly source?: string;
|
|
147
|
+
readonly embeddingModel?: string;
|
|
148
|
+
}
|
|
149
|
+
export interface PgVectorStoreOptions {
|
|
150
|
+
/**
|
|
151
|
+
* Postgres connection string, used only when this store builds its own pool.
|
|
152
|
+
* Prefer `client` — a second pool to one database is a second set of
|
|
153
|
+
* connections nobody counted.
|
|
154
|
+
*/
|
|
155
|
+
readonly connectionString?: string;
|
|
156
|
+
/** A pre-built `pg.Pool` (or anything with `query`). Recommended. */
|
|
157
|
+
readonly client?: PgLikeClient;
|
|
158
|
+
/** Schema the tables live in. Default `'public'`. */
|
|
159
|
+
readonly schema?: string;
|
|
160
|
+
/** The vectors table. Default `'af_vectors'`. */
|
|
161
|
+
readonly table?: string;
|
|
162
|
+
/** The recognition-set table (`seen`/`recordSignature`). Default `'af_signatures'`. */
|
|
163
|
+
readonly signaturesTable?: string;
|
|
164
|
+
/** The usefulness-aggregate table. Default `'af_feedback'`. */
|
|
165
|
+
readonly feedbackTable?: string;
|
|
166
|
+
/** The key/value table holding one embedder fingerprint per namespace. Default `'af_index_meta'`. */
|
|
167
|
+
readonly metaTable?: string;
|
|
168
|
+
/** Column names inside {@link table}. Each defaults to the documented one. */
|
|
169
|
+
readonly columns?: PgVectorColumns;
|
|
170
|
+
/**
|
|
171
|
+
* Rows per multi-row upsert. Default 500.
|
|
172
|
+
*
|
|
173
|
+
* Postgres caps a statement at 65,535 bound parameters and this store binds
|
|
174
|
+
* 15 per row, so 500 leaves an order of magnitude of headroom. Raising it
|
|
175
|
+
* trades round-trips for a statement that fails all-or-nothing on a bigger
|
|
176
|
+
* unit.
|
|
177
|
+
*/
|
|
178
|
+
readonly batchSize?: number;
|
|
179
|
+
/** @internal Test injection — skips the `pg` require entirely. */
|
|
180
|
+
readonly _client?: PgLikeClient;
|
|
181
|
+
/** @internal Test injection — the `pg` module (exercises the real shim with a mock module). */
|
|
182
|
+
readonly _pg?: PgSdkModule;
|
|
183
|
+
}
|
|
184
|
+
/** A Postgres-backed vector store, plus the two things it owns beyond the port. */
|
|
185
|
+
export interface PgVectorStore extends MemoryStore {
|
|
186
|
+
/**
|
|
187
|
+
* The embedder fingerprint (`'<id>@<dims>'`) a namespace was built with, or
|
|
188
|
+
* `undefined` when nothing with a vector has been written to it yet.
|
|
189
|
+
*
|
|
190
|
+
* Read this before an embedder swap: it is the fact `EmbedderMismatchError`
|
|
191
|
+
* refuses on, available up front instead of at the first failed write.
|
|
192
|
+
*/
|
|
193
|
+
fingerprintOf(identity: MemoryIdentity): Promise<string | undefined>;
|
|
194
|
+
/**
|
|
195
|
+
* Release the pool, if this store built one. Idempotent. A client you passed
|
|
196
|
+
* in is yours and is left alone.
|
|
197
|
+
*/
|
|
198
|
+
close(): Promise<void>;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Raised when the database is reachable but its schema is not this store's.
|
|
202
|
+
*
|
|
203
|
+
* The law `sqliteVectorStore` states for a file, one backend over: **an
|
|
204
|
+
* unreadable index and an empty one are different facts, and only one of them
|
|
205
|
+
* is safe to answer with "no matches".** A store that treated a missing table
|
|
206
|
+
* as an empty corpus would answer every question from the model's own weights
|
|
207
|
+
* and log nothing.
|
|
208
|
+
*/
|
|
209
|
+
export declare class PgVectorSchemaError extends Error {
|
|
210
|
+
readonly code: "ERR_PGVECTOR_SCHEMA";
|
|
211
|
+
/** The schema-qualified table that could not be used. */
|
|
212
|
+
readonly table: string;
|
|
213
|
+
/** Columns this store needs and did not find. Empty when the table is absent entirely. */
|
|
214
|
+
readonly missingColumns: readonly string[];
|
|
215
|
+
constructor(table: string, missingColumns: readonly string[], detail: string);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Open a `MemoryStore` over an existing Postgres + pgvector table.
|
|
219
|
+
*
|
|
220
|
+
* @throws when `pg` is absent and no `client` was passed.
|
|
221
|
+
* @throws PgVectorSchemaError on the first call, when the table or a column it
|
|
222
|
+
* needs is not there.
|
|
223
|
+
* @throws EmbedderMismatchError from `put`/`putMany`/`search` when a vector
|
|
224
|
+
* meets a namespace built by a different embedder.
|
|
225
|
+
*
|
|
226
|
+
* @example A corpus beside the application's own data
|
|
227
|
+
* ```ts
|
|
228
|
+
* import { Pool } from 'pg';
|
|
229
|
+
* import { defineRAG, indexDocuments } from 'agentfootprint';
|
|
230
|
+
* import { pgVectorStore } from 'agentfootprint/memory';
|
|
231
|
+
* import { openaiEmbedder } from 'agentfootprint/providers';
|
|
232
|
+
*
|
|
233
|
+
* const store = pgVectorStore({ client: new Pool({ connectionString: process.env.DATABASE_URL }) });
|
|
234
|
+
* const embedder = openaiEmbedder();
|
|
235
|
+
*
|
|
236
|
+
* await indexDocuments(store, embedder, docs, { embedderId: embedder.id });
|
|
237
|
+
*
|
|
238
|
+
* const agent = Agent.create({ provider })
|
|
239
|
+
* .rag(defineRAG({ id: 'docs', store, embedder, embedderId: embedder.id }))
|
|
240
|
+
* .build();
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
export declare function pgVectorStore(options?: PgVectorStoreOptions): PgVectorStore;
|