@fastpaca/cria 0.0.1 → 1.0.1
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/README.md +93 -106
- package/dist/ai-sdk/index.d.ts +43 -0
- package/dist/ai-sdk/index.d.ts.map +1 -0
- package/dist/ai-sdk/index.js +303 -0
- package/dist/ai-sdk/index.js.map +1 -0
- package/dist/ai-sdk/index.test.d.ts +2 -0
- package/dist/ai-sdk/index.test.d.ts.map +1 -0
- package/dist/ai-sdk/index.test.js +101 -0
- package/dist/ai-sdk/index.test.js.map +1 -0
- package/dist/anthropic/index.d.ts +74 -0
- package/dist/anthropic/index.d.ts.map +1 -0
- package/dist/anthropic/index.js +238 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/anthropic/index.test.d.ts +2 -0
- package/dist/anthropic/index.test.d.ts.map +1 -0
- package/dist/anthropic/index.test.js +115 -0
- package/dist/anthropic/index.test.js.map +1 -0
- package/dist/components/additional.test.d.ts +2 -0
- package/dist/components/additional.test.d.ts.map +1 -0
- package/dist/components/additional.test.js +31 -0
- package/dist/components/additional.test.js.map +1 -0
- package/dist/components/index.d.ts +148 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +184 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/summary.d.ts +91 -0
- package/dist/components/summary.d.ts.map +1 -0
- package/dist/components/summary.js +118 -0
- package/dist/components/summary.js.map +1 -0
- package/dist/components/summary.test.d.ts +2 -0
- package/dist/components/summary.test.d.ts.map +1 -0
- package/dist/components/summary.test.js +101 -0
- package/dist/components/summary.test.js.map +1 -0
- package/dist/components/vector-search.d.ts +70 -0
- package/dist/components/vector-search.d.ts.map +1 -0
- package/dist/components/vector-search.js +110 -0
- package/dist/components/vector-search.js.map +1 -0
- package/dist/components/vector-search.test.d.ts +2 -0
- package/dist/components/vector-search.test.d.ts.map +1 -0
- package/dist/components/vector-search.test.js +113 -0
- package/dist/components/vector-search.test.js.map +1 -0
- package/dist/index.d.ts +12 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -5
- package/dist/index.js.map +1 -1
- package/dist/instrumentation/otel.d.ts +19 -0
- package/dist/instrumentation/otel.d.ts.map +1 -0
- package/dist/instrumentation/otel.js +102 -0
- package/dist/instrumentation/otel.js.map +1 -0
- package/dist/instrumentation/otel.test.d.ts +2 -0
- package/dist/instrumentation/otel.test.d.ts.map +1 -0
- package/dist/instrumentation/otel.test.js +116 -0
- package/dist/instrumentation/otel.test.js.map +1 -0
- package/dist/jsx-dev-runtime.d.ts +2 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-dev-runtime.js +2 -0
- package/dist/jsx-dev-runtime.js.map +1 -0
- package/dist/jsx-runtime.d.ts +30 -8
- package/dist/jsx-runtime.d.ts.map +1 -1
- package/dist/jsx-runtime.js +13 -10
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/memory/chroma/index.d.ts +59 -0
- package/dist/memory/chroma/index.d.ts.map +1 -0
- package/dist/memory/chroma/index.js +172 -0
- package/dist/memory/chroma/index.js.map +1 -0
- package/dist/memory/index.d.ts +4 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +2 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/key-value.d.ts +71 -0
- package/dist/memory/key-value.d.ts.map +1 -0
- package/dist/memory/key-value.js +34 -0
- package/dist/memory/key-value.js.map +1 -0
- package/dist/memory/postgres.d.ts +71 -0
- package/dist/memory/postgres.d.ts.map +1 -0
- package/dist/memory/postgres.js +109 -0
- package/dist/memory/postgres.js.map +1 -0
- package/dist/memory/qdrant/index.d.ts +64 -0
- package/dist/memory/qdrant/index.d.ts.map +1 -0
- package/dist/memory/qdrant/index.js +136 -0
- package/dist/memory/qdrant/index.js.map +1 -0
- package/dist/memory/redis.d.ts +70 -0
- package/dist/memory/redis.d.ts.map +1 -0
- package/dist/memory/redis.js +100 -0
- package/dist/memory/redis.js.map +1 -0
- package/dist/memory/vector.d.ts +53 -0
- package/dist/memory/vector.d.ts.map +1 -0
- package/dist/memory/vector.js +2 -0
- package/dist/memory/vector.js.map +1 -0
- package/dist/openai/index.d.ts +46 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai/index.js +260 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/openai/index.test.d.ts +2 -0
- package/dist/openai/index.test.d.ts.map +1 -0
- package/dist/openai/index.test.js +204 -0
- package/dist/openai/index.test.js.map +1 -0
- package/dist/providers/index.d.ts +2 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +2 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/types.d.ts +2 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +2 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/render.d.ts +44 -40
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +162 -148
- package/dist/render.js.map +1 -1
- package/dist/render.test.js +146 -28
- package/dist/render.test.js.map +1 -1
- package/dist/renderers/markdown.d.ts +3 -0
- package/dist/renderers/markdown.d.ts.map +1 -0
- package/dist/renderers/markdown.js +43 -0
- package/dist/renderers/markdown.js.map +1 -0
- package/dist/renderers/shared.d.ts +82 -0
- package/dist/renderers/shared.d.ts.map +1 -0
- package/dist/renderers/shared.js +156 -0
- package/dist/renderers/shared.js.map +1 -0
- package/dist/snapshot.d.ts +47 -0
- package/dist/snapshot.d.ts.map +1 -0
- package/dist/snapshot.js +140 -0
- package/dist/snapshot.js.map +1 -0
- package/dist/snapshot.test.d.ts +2 -0
- package/dist/snapshot.test.d.ts.map +1 -0
- package/dist/snapshot.test.js +68 -0
- package/dist/snapshot.test.js.map +1 -0
- package/dist/tokenizers.d.ts +14 -0
- package/dist/tokenizers.d.ts.map +1 -0
- package/dist/tokenizers.js +45 -0
- package/dist/tokenizers.js.map +1 -0
- package/dist/types.d.ts +212 -84
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +109 -0
- package/dist/types.js.map +1 -1
- package/package.json +88 -3
- package/dist/components.d.ts +0 -78
- package/dist/components.d.ts.map +0 -1
- package/dist/components.js +0 -98
- package/dist/components.js.map +0 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { PoolConfig } from "pg";
|
|
2
|
+
import type { KVMemory, MemoryEntry } from "./key-value";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for the Postgres store.
|
|
5
|
+
*/
|
|
6
|
+
export interface PostgresStoreOptions extends PoolConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Table name for storing entries.
|
|
9
|
+
* The table will be created automatically if it doesn't exist.
|
|
10
|
+
* @default "cria_kv_store"
|
|
11
|
+
*/
|
|
12
|
+
tableName?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Whether to create the table on first use if it doesn't exist.
|
|
15
|
+
* @default true
|
|
16
|
+
*/
|
|
17
|
+
autoCreateTable?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Postgres-backed implementation of KVMemory.
|
|
21
|
+
*
|
|
22
|
+
* Plug-and-play adapter using node-postgres (pg). Just pass your connection options.
|
|
23
|
+
*
|
|
24
|
+
* The store will automatically create the required table if it doesn't exist.
|
|
25
|
+
*
|
|
26
|
+
* @template T - The type of data to store
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { PostgresStore } from "@fastpaca/cria/memory/postgres";
|
|
31
|
+
*
|
|
32
|
+
* // Connect using environment variables (PG* env vars)
|
|
33
|
+
* const store = new PostgresStore<{ content: string }>();
|
|
34
|
+
*
|
|
35
|
+
* // Connect with options
|
|
36
|
+
* const store = new PostgresStore<{ content: string }>({
|
|
37
|
+
* connectionString: "postgres://user:pass@localhost/mydb",
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* await store.set("key-1", { content: "Hello" });
|
|
41
|
+
* const entry = await store.get("key-1");
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // With custom table name
|
|
47
|
+
* const store = new PostgresStore<string>({
|
|
48
|
+
* host: "localhost",
|
|
49
|
+
* database: "myapp",
|
|
50
|
+
* tableName: "my_app_memory",
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare class PostgresStore<T = unknown> implements KVMemory<T> {
|
|
55
|
+
private readonly pool;
|
|
56
|
+
private readonly tableName;
|
|
57
|
+
private readonly autoCreateTable;
|
|
58
|
+
private tableCreated;
|
|
59
|
+
constructor(options?: PostgresStoreOptions);
|
|
60
|
+
private ensureTable;
|
|
61
|
+
get(key: string): Promise<MemoryEntry<T> | null>;
|
|
62
|
+
set(key: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
|
|
63
|
+
delete(key: string): Promise<boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* Close the connection pool.
|
|
66
|
+
* Call this when you're done using the store to clean up connections.
|
|
67
|
+
*/
|
|
68
|
+
end(): Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
export type { KVMemory, MemoryEntry } from "./key-value";
|
|
71
|
+
//# sourceMappingURL=postgres.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../src/memory/postgres.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAErC,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEzD;;GAEG;AACH,MAAM,WAAW,oBAAqB,SAAQ,UAAU;IACtD;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAaD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,qBAAa,aAAa,CAAC,CAAC,GAAG,OAAO,CAAE,YAAW,QAAQ,CAAC,CAAC,CAAC;IAC5D,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAO;IAC5B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAU;IAC1C,OAAO,CAAC,YAAY,CAAS;gBAEjB,OAAO,GAAE,oBAAyB;YAQhC,WAAW;IAkBnB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAsBhD,GAAG,CACP,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,CAAC,EACP,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACjC,OAAO,CAAC,IAAI,CAAC;IAyBV,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAW3C;;;OAGG;IACG,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;CAG3B;AAGD,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Pool } from "pg";
|
|
2
|
+
/**
|
|
3
|
+
* Postgres-backed implementation of KVMemory.
|
|
4
|
+
*
|
|
5
|
+
* Plug-and-play adapter using node-postgres (pg). Just pass your connection options.
|
|
6
|
+
*
|
|
7
|
+
* The store will automatically create the required table if it doesn't exist.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of data to store
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { PostgresStore } from "@fastpaca/cria/memory/postgres";
|
|
14
|
+
*
|
|
15
|
+
* // Connect using environment variables (PG* env vars)
|
|
16
|
+
* const store = new PostgresStore<{ content: string }>();
|
|
17
|
+
*
|
|
18
|
+
* // Connect with options
|
|
19
|
+
* const store = new PostgresStore<{ content: string }>({
|
|
20
|
+
* connectionString: "postgres://user:pass@localhost/mydb",
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* await store.set("key-1", { content: "Hello" });
|
|
24
|
+
* const entry = await store.get("key-1");
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // With custom table name
|
|
30
|
+
* const store = new PostgresStore<string>({
|
|
31
|
+
* host: "localhost",
|
|
32
|
+
* database: "myapp",
|
|
33
|
+
* tableName: "my_app_memory",
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export class PostgresStore {
|
|
38
|
+
pool;
|
|
39
|
+
tableName;
|
|
40
|
+
autoCreateTable;
|
|
41
|
+
tableCreated = false;
|
|
42
|
+
constructor(options = {}) {
|
|
43
|
+
const { tableName, autoCreateTable, ...poolConfig } = options;
|
|
44
|
+
this.pool = new Pool(poolConfig);
|
|
45
|
+
this.tableName = tableName ?? "cria_kv_store";
|
|
46
|
+
this.autoCreateTable = autoCreateTable ?? true;
|
|
47
|
+
}
|
|
48
|
+
async ensureTable() {
|
|
49
|
+
if (this.tableCreated || !this.autoCreateTable) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
await this.pool.query(`
|
|
53
|
+
CREATE TABLE IF NOT EXISTS ${this.tableName} (
|
|
54
|
+
key TEXT PRIMARY KEY,
|
|
55
|
+
data JSONB NOT NULL,
|
|
56
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
57
|
+
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
58
|
+
metadata JSONB
|
|
59
|
+
)
|
|
60
|
+
`);
|
|
61
|
+
this.tableCreated = true;
|
|
62
|
+
}
|
|
63
|
+
async get(key) {
|
|
64
|
+
await this.ensureTable();
|
|
65
|
+
const result = await this.pool.query(`SELECT key, data, created_at, updated_at, metadata FROM ${this.tableName} WHERE key = $1`, [key]);
|
|
66
|
+
const row = result.rows[0];
|
|
67
|
+
if (row === undefined) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
data: row.data,
|
|
72
|
+
createdAt: new Date(row.created_at).getTime(),
|
|
73
|
+
updatedAt: new Date(row.updated_at).getTime(),
|
|
74
|
+
...(row.metadata && { metadata: row.metadata }),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
async set(key, data, metadata) {
|
|
78
|
+
await this.ensureTable();
|
|
79
|
+
const now = new Date();
|
|
80
|
+
// Use UPSERT to handle both insert and update cases
|
|
81
|
+
// ON CONFLICT preserves created_at and updates updated_at
|
|
82
|
+
await this.pool.query(`
|
|
83
|
+
INSERT INTO ${this.tableName} (key, data, created_at, updated_at, metadata)
|
|
84
|
+
VALUES ($1, $2, $3, $3, $4)
|
|
85
|
+
ON CONFLICT (key) DO UPDATE SET
|
|
86
|
+
data = EXCLUDED.data,
|
|
87
|
+
updated_at = EXCLUDED.updated_at,
|
|
88
|
+
metadata = EXCLUDED.metadata
|
|
89
|
+
`, [
|
|
90
|
+
key,
|
|
91
|
+
JSON.stringify(data),
|
|
92
|
+
now,
|
|
93
|
+
metadata ? JSON.stringify(metadata) : null,
|
|
94
|
+
]);
|
|
95
|
+
}
|
|
96
|
+
async delete(key) {
|
|
97
|
+
await this.ensureTable();
|
|
98
|
+
const result = await this.pool.query(`DELETE FROM ${this.tableName} WHERE key = $1`, [key]);
|
|
99
|
+
return (result.rowCount ?? 0) > 0;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Close the connection pool.
|
|
103
|
+
* Call this when you're done using the store to clean up connections.
|
|
104
|
+
*/
|
|
105
|
+
async end() {
|
|
106
|
+
await this.pool.end();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=postgres.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres.js","sourceRoot":"","sources":["../../src/memory/postgres.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,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C7C,SAAS,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE;YAC7C,GAAG,CAAC,GAAG,CAAC,QAAQ,IAAI,EAAE,QAAQ,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC;SAChD,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,GAAG,CACP,GAAW,EACX,IAAO,EACP,QAAkC;QAElC,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;QAEzB,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;QAEvB,oDAAoD;QACpD,0DAA0D;QAC1D,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,CACnB;oBACc,IAAI,CAAC,SAAS;;;;;;OAM3B,EACD;YACE,GAAG;YACH,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;YACpB,GAAG;YACH,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI;SAC3C,CACF,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,GAAW;QACtB,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;QAEzB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,CAClC,eAAe,IAAI,CAAC,SAAS,iBAAiB,EAC9C,CAAC,GAAG,CAAC,CACN,CAAC;QAEF,OAAO,CAAC,MAAM,CAAC,QAAQ,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IACpC,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,GAAG;QACP,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;IACxB,CAAC;CACF"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { QdrantClient } from "@qdrant/js-client-rest";
|
|
2
|
+
import type { MemoryEntry } from "../key-value";
|
|
3
|
+
import type { VectorMemory, VectorSearchOptions, VectorSearchResult } from "../vector";
|
|
4
|
+
/**
|
|
5
|
+
* Function to generate embeddings for text.
|
|
6
|
+
*/
|
|
7
|
+
export type EmbeddingFunction = (text: string) => Promise<number[]>;
|
|
8
|
+
/**
|
|
9
|
+
* Options for creating a QdrantStore.
|
|
10
|
+
*/
|
|
11
|
+
export interface QdrantStoreOptions {
|
|
12
|
+
/** The Qdrant client instance */
|
|
13
|
+
client: QdrantClient;
|
|
14
|
+
/** The name of the collection to use */
|
|
15
|
+
collectionName: string;
|
|
16
|
+
/** Function to generate embeddings from text */
|
|
17
|
+
embed: EmbeddingFunction;
|
|
18
|
+
/** Optional: the name of the vector field (for collections with named vectors) */
|
|
19
|
+
vectorName?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* VectorMemory implementation backed by Qdrant.
|
|
23
|
+
*
|
|
24
|
+
* This adapter wraps a Qdrant collection and implements the VectorMemory interface,
|
|
25
|
+
* allowing you to use Qdrant for RAG workflows with Cria's VectorSearch component.
|
|
26
|
+
*
|
|
27
|
+
* @template T - The type of data stored in the payload
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { QdrantClient } from "@qdrant/js-client-rest";
|
|
32
|
+
* import { QdrantStore } from "@fastpaca/cria/memory/qdrant";
|
|
33
|
+
* import { VectorSearch } from "@fastpaca/cria";
|
|
34
|
+
*
|
|
35
|
+
* const client = new QdrantClient({ url: "http://localhost:6333" });
|
|
36
|
+
*
|
|
37
|
+
* const store = new QdrantStore({
|
|
38
|
+
* client,
|
|
39
|
+
* collectionName: "my-docs",
|
|
40
|
+
* embed: async (text) => {
|
|
41
|
+
* // Use your embedding model (OpenAI, Cohere, etc.)
|
|
42
|
+
* return await getEmbedding(text);
|
|
43
|
+
* },
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // Use with VectorSearch
|
|
47
|
+
* <VectorSearch store={store} limit={5}>
|
|
48
|
+
* What is RAG?
|
|
49
|
+
* </VectorSearch>
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare class QdrantStore<T = unknown> implements VectorMemory<T> {
|
|
53
|
+
private readonly client;
|
|
54
|
+
private readonly collectionName;
|
|
55
|
+
private readonly embedFn;
|
|
56
|
+
private readonly vectorName;
|
|
57
|
+
constructor(options: QdrantStoreOptions);
|
|
58
|
+
private embed;
|
|
59
|
+
get(key: string): Promise<MemoryEntry<T> | null>;
|
|
60
|
+
set(key: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
|
|
61
|
+
delete(key: string): Promise<boolean>;
|
|
62
|
+
search(query: string, options?: VectorSearchOptions): Promise<VectorSearchResult<T>[]>;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/memory/qdrant/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EACV,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,EACnB,MAAM,WAAW,CAAC;AAEnB;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;AAEpE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,iCAAiC;IACjC,MAAM,EAAE,YAAY,CAAC;IACrB,wCAAwC;IACxC,cAAc,EAAE,MAAM,CAAC;IACvB,gDAAgD;IAChD,KAAK,EAAE,iBAAiB,CAAC;IACzB,kFAAkF;IAClF,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAwBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBAAa,WAAW,CAAC,CAAC,GAAG,OAAO,CAAE,YAAW,YAAY,CAAC,CAAC,CAAC;IAC9D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAe;IACtC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAS;IACxC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAoB;IAC5C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAqB;gBAEpC,OAAO,EAAE,kBAAkB;YAOzB,KAAK;IAUb,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAgBhD,GAAG,CACP,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,CAAC,EACP,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACjC,OAAO,CAAC,IAAI,CAAC;IA0BV,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAYrC,MAAM,CACV,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,mBAAmB,GAC5B,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAC,EAAE,CAAC;CAiCpC"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Qdrant payload to a MemoryEntry.
|
|
3
|
+
*/
|
|
4
|
+
function payloadToEntry(payload) {
|
|
5
|
+
return {
|
|
6
|
+
data: payload.data,
|
|
7
|
+
createdAt: payload.createdAt,
|
|
8
|
+
updatedAt: payload.updatedAt,
|
|
9
|
+
...(payload.metadata && { metadata: payload.metadata }),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* VectorMemory implementation backed by Qdrant.
|
|
14
|
+
*
|
|
15
|
+
* This adapter wraps a Qdrant collection and implements the VectorMemory interface,
|
|
16
|
+
* allowing you to use Qdrant for RAG workflows with Cria's VectorSearch component.
|
|
17
|
+
*
|
|
18
|
+
* @template T - The type of data stored in the payload
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { QdrantClient } from "@qdrant/js-client-rest";
|
|
23
|
+
* import { QdrantStore } from "@fastpaca/cria/memory/qdrant";
|
|
24
|
+
* import { VectorSearch } from "@fastpaca/cria";
|
|
25
|
+
*
|
|
26
|
+
* const client = new QdrantClient({ url: "http://localhost:6333" });
|
|
27
|
+
*
|
|
28
|
+
* const store = new QdrantStore({
|
|
29
|
+
* client,
|
|
30
|
+
* collectionName: "my-docs",
|
|
31
|
+
* embed: async (text) => {
|
|
32
|
+
* // Use your embedding model (OpenAI, Cohere, etc.)
|
|
33
|
+
* return await getEmbedding(text);
|
|
34
|
+
* },
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // Use with VectorSearch
|
|
38
|
+
* <VectorSearch store={store} limit={5}>
|
|
39
|
+
* What is RAG?
|
|
40
|
+
* </VectorSearch>
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export class QdrantStore {
|
|
44
|
+
client;
|
|
45
|
+
collectionName;
|
|
46
|
+
embedFn;
|
|
47
|
+
vectorName;
|
|
48
|
+
constructor(options) {
|
|
49
|
+
this.client = options.client;
|
|
50
|
+
this.collectionName = options.collectionName;
|
|
51
|
+
this.embedFn = options.embed;
|
|
52
|
+
this.vectorName = options.vectorName;
|
|
53
|
+
}
|
|
54
|
+
async embed(text, context) {
|
|
55
|
+
try {
|
|
56
|
+
return await this.embedFn(text);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
throw new Error(`QdrantStore: embedding failed during ${context}`, {
|
|
60
|
+
cause: error,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async get(key) {
|
|
65
|
+
const response = await this.client.retrieve(this.collectionName, {
|
|
66
|
+
ids: [key],
|
|
67
|
+
with_payload: true,
|
|
68
|
+
});
|
|
69
|
+
const point = response[0];
|
|
70
|
+
if (!point) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const payload = point.payload;
|
|
74
|
+
return payload ? payloadToEntry(payload) : null;
|
|
75
|
+
}
|
|
76
|
+
async set(key, data, metadata) {
|
|
77
|
+
const now = Date.now();
|
|
78
|
+
// Convert data to text for embedding
|
|
79
|
+
const textToEmbed = typeof data === "string" ? data : JSON.stringify(data);
|
|
80
|
+
const vector = await this.embed(textToEmbed, `set("${key}")`);
|
|
81
|
+
const payload = {
|
|
82
|
+
data,
|
|
83
|
+
createdAt: now,
|
|
84
|
+
updatedAt: now,
|
|
85
|
+
...(metadata && { metadata }),
|
|
86
|
+
};
|
|
87
|
+
await this.client.upsert(this.collectionName, {
|
|
88
|
+
wait: true,
|
|
89
|
+
points: [
|
|
90
|
+
{
|
|
91
|
+
id: key,
|
|
92
|
+
vector: this.vectorName ? { [this.vectorName]: vector } : vector,
|
|
93
|
+
payload: payload,
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
async delete(key) {
|
|
99
|
+
const existing = await this.get(key);
|
|
100
|
+
if (!existing) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
await this.client.delete(this.collectionName, {
|
|
104
|
+
wait: true,
|
|
105
|
+
points: [key],
|
|
106
|
+
});
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
async search(query, options) {
|
|
110
|
+
const limit = options?.limit ?? 10;
|
|
111
|
+
const threshold = options?.threshold;
|
|
112
|
+
const queryVector = await this.embed(query, "search");
|
|
113
|
+
const response = await this.client.search(this.collectionName, {
|
|
114
|
+
vector: this.vectorName
|
|
115
|
+
? { name: this.vectorName, vector: queryVector }
|
|
116
|
+
: queryVector,
|
|
117
|
+
limit,
|
|
118
|
+
with_payload: true,
|
|
119
|
+
...(threshold !== undefined && { score_threshold: threshold }),
|
|
120
|
+
});
|
|
121
|
+
const results = [];
|
|
122
|
+
for (const point of response) {
|
|
123
|
+
const payload = point.payload;
|
|
124
|
+
if (!payload) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
results.push({
|
|
128
|
+
key: String(point.id),
|
|
129
|
+
score: point.score,
|
|
130
|
+
entry: payloadToEntry(payload),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return results;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/memory/qdrant/index.ts"],"names":[],"mappings":"AAqCA;;GAEG;AACH,SAAS,cAAc,CAAI,OAAyB;IAClD,OAAO;QACL,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,GAAG,CAAC,OAAO,CAAC,QAAQ,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC;KACxD,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,OAAO,WAAW;IACL,MAAM,CAAe;IACrB,cAAc,CAAS;IACvB,OAAO,CAAoB;IAC3B,UAAU,CAAqB;IAEhD,YAAY,OAA2B;QACrC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;QAC7C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IACvC,CAAC;IAEO,KAAK,CAAC,KAAK,CAAC,IAAY,EAAE,OAAe;QAC/C,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAClC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,wCAAwC,OAAO,EAAE,EAAE;gBACjE,KAAK,EAAE,KAAK;aACb,CAAC,CAAC;QACL,CAAC;I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9D,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,SAAS;YACX,CAAC;YAED,OAAO,CAAC,IAAI,CAAC;gBACX,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;gBACrB,KAAK,EAAE,KAAK,CAAC,KAAK;gBAClB,KAAK,EAAE,cAAc,CAAC,OAAO,CAAC;aAC/B,CAAC,CAAC;QACL,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { RedisOptions } from "ioredis";
|
|
2
|
+
import type { KVMemory, MemoryEntry } from "./key-value";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for the Redis store.
|
|
5
|
+
*/
|
|
6
|
+
export interface RedisStoreOptions extends RedisOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Key prefix for all entries stored by this instance.
|
|
9
|
+
* Useful for namespacing multiple stores in the same Redis instance.
|
|
10
|
+
* @default "cria:kv:"
|
|
11
|
+
*/
|
|
12
|
+
keyPrefix?: string;
|
|
13
|
+
/**
|
|
14
|
+
* TTL (time-to-live) in seconds for entries.
|
|
15
|
+
* If set, entries will automatically expire after this duration.
|
|
16
|
+
* @default undefined (no expiration)
|
|
17
|
+
*/
|
|
18
|
+
ttlSeconds?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Redis-backed implementation of KVMemory.
|
|
22
|
+
*
|
|
23
|
+
* Plug-and-play adapter using ioredis. Just pass your connection options.
|
|
24
|
+
*
|
|
25
|
+
* @template T - The type of data to store
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import { RedisStore } from "@fastpaca/cria/memory/redis";
|
|
30
|
+
*
|
|
31
|
+
* // Connect to localhost:6379
|
|
32
|
+
* const store = new RedisStore<{ content: string }>();
|
|
33
|
+
*
|
|
34
|
+
* // Connect with options
|
|
35
|
+
* const store = new RedisStore<{ content: string }>({
|
|
36
|
+
* host: "redis.example.com",
|
|
37
|
+
* port: 6379,
|
|
38
|
+
* password: "secret",
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* await store.set("key-1", { content: "Hello" });
|
|
42
|
+
* const entry = await store.get("key-1");
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* // With TTL and custom prefix
|
|
48
|
+
* const store = new RedisStore<string>({
|
|
49
|
+
* keyPrefix: "myapp:memory:",
|
|
50
|
+
* ttlSeconds: 3600, // 1 hour TTL
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare class RedisStore<T = unknown> implements KVMemory<T> {
|
|
55
|
+
private readonly client;
|
|
56
|
+
private readonly prefix;
|
|
57
|
+
private readonly ttlSeconds?;
|
|
58
|
+
constructor(options?: RedisStoreOptions);
|
|
59
|
+
private prefixedKey;
|
|
60
|
+
get(key: string): Promise<MemoryEntry<T> | null>;
|
|
61
|
+
set(key: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
|
|
62
|
+
delete(key: string): Promise<boolean>;
|
|
63
|
+
/**
|
|
64
|
+
* Disconnect from Redis.
|
|
65
|
+
* Call this when you're done using the store to clean up connections.
|
|
66
|
+
*/
|
|
67
|
+
disconnect(): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
export type { KVMemory, MemoryEntry } from "./key-value";
|
|
70
|
+
//# sourceMappingURL=redis.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis.d.ts","sourceRoot":"","sources":["../../src/memory/redis.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEzD;;GAEG;AACH,MAAM,WAAW,iBAAkB,SAAQ,YAAY;IACrD;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAaD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,qBAAa,UAAU,CAAC,CAAC,GAAG,OAAO,CAAE,YAAW,QAAQ,CAAC,CAAC,CAAC;IACzD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAS;gBAEzB,OAAO,GAAE,iBAAsB;IAW3C,OAAO,CAAC,WAAW;IAIb,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAiBhD,GAAG,CACP,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,CAAC,EACP,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACjC,OAAO,CAAC,IAAI,CAAC;IA6BV,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAK3C;;;OAGG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;CAGlC;AAGD,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import Redis from "ioredis";
|
|
2
|
+
/**
|
|
3
|
+
* Redis-backed implementation of KVMemory.
|
|
4
|
+
*
|
|
5
|
+
* Plug-and-play adapter using ioredis. Just pass your connection options.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The type of data to store
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { RedisStore } from "@fastpaca/cria/memory/redis";
|
|
12
|
+
*
|
|
13
|
+
* // Connect to localhost:6379
|
|
14
|
+
* const store = new RedisStore<{ content: string }>();
|
|
15
|
+
*
|
|
16
|
+
* // Connect with options
|
|
17
|
+
* const store = new RedisStore<{ content: string }>({
|
|
18
|
+
* host: "redis.example.com",
|
|
19
|
+
* port: 6379,
|
|
20
|
+
* password: "secret",
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* await store.set("key-1", { content: "Hello" });
|
|
24
|
+
* const entry = await store.get("key-1");
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // With TTL and custom prefix
|
|
30
|
+
* const store = new RedisStore<string>({
|
|
31
|
+
* keyPrefix: "myapp:memory:",
|
|
32
|
+
* ttlSeconds: 3600, // 1 hour TTL
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export class RedisStore {
|
|
37
|
+
client;
|
|
38
|
+
prefix;
|
|
39
|
+
ttlSeconds;
|
|
40
|
+
constructor(options = {}) {
|
|
41
|
+
const { keyPrefix, ttlSeconds, ...redisOptions } = options;
|
|
42
|
+
this.client = new Redis(redisOptions);
|
|
43
|
+
this.prefix = keyPrefix ?? "cria:kv:";
|
|
44
|
+
if (ttlSeconds !== undefined) {
|
|
45
|
+
this.ttlSeconds = ttlSeconds;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
prefixedKey(key) {
|
|
49
|
+
return `${this.prefix}${key}`;
|
|
50
|
+
}
|
|
51
|
+
async get(key) {
|
|
52
|
+
const raw = await this.client.get(this.prefixedKey(key));
|
|
53
|
+
if (raw === null) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
const stored = JSON.parse(raw);
|
|
57
|
+
return {
|
|
58
|
+
data: stored.data,
|
|
59
|
+
createdAt: stored.createdAt,
|
|
60
|
+
updatedAt: stored.updatedAt,
|
|
61
|
+
...(stored.metadata && { metadata: stored.metadata }),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
async set(key, data, metadata) {
|
|
65
|
+
const prefixedKey = this.prefixedKey(key);
|
|
66
|
+
const now = Date.now();
|
|
67
|
+
// Try to get existing entry to preserve createdAt
|
|
68
|
+
const existingRaw = await this.client.get(prefixedKey);
|
|
69
|
+
let createdAt = now;
|
|
70
|
+
if (existingRaw !== null) {
|
|
71
|
+
const existing = JSON.parse(existingRaw);
|
|
72
|
+
createdAt = existing.createdAt;
|
|
73
|
+
}
|
|
74
|
+
const entry = {
|
|
75
|
+
data,
|
|
76
|
+
createdAt,
|
|
77
|
+
updatedAt: now,
|
|
78
|
+
...(metadata && { metadata }),
|
|
79
|
+
};
|
|
80
|
+
const value = JSON.stringify(entry);
|
|
81
|
+
if (this.ttlSeconds !== undefined) {
|
|
82
|
+
await this.client.set(prefixedKey, value, "EX", this.ttlSeconds);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
await this.client.set(prefixedKey, value);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async delete(key) {
|
|
89
|
+
const count = await this.client.del(this.prefixedKey(key));
|
|
90
|
+
return count > 0;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Disconnect from Redis.
|
|
94
|
+
* Call this when you're done using the store to clean up connections.
|
|
95
|
+
*/
|
|
96
|
+
async disconnect() {
|
|
97
|
+
await this.client.quit();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=redis.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis.js","sourceRoot":"","sources":["../../src/memory/redis.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,SAAS,CAAC;AAiC5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,OAAO,UAAU;IACJ,MAAM,CAAQ;IACd,MAAM,CAAS;IACf,UAAU,CAAU;IAErC,YAAY,UAA6B,EAAE;QACzC,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,YAAY,EAAE,GAAG,OAAO,CAAC;QAE3D,IAAI,CAAC,MA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AAC,KAAK,CAAC,WAAW,CAAmB,CAAC;YAC3D,SAAS,GAAG,QAAQ,CAAC,SAAS,CAAC;QACjC,CAAC;QAED,MAAM,KAAK,GAAmB;YAC5B,IAAI;YACJ,SAAS;YACT,SAAS,EAAE,GAAG;YACd,GAAG,CAAC,QAAQ,IAAI,EAAE,QAAQ,EAAE,CAAC;SAC9B,CAAC;QAEF,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QAEpC,IAAI,IAAI,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YAClC,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;QACnE,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,GAAW;QACtB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC;QAC3D,OAAO,KAAK,GAAG,CAAC,CAAC;IACnB,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,UAAU;QACd,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC3B,CAAC;CACF"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { MaybePromise } from "../types";
|
|
2
|
+
import type { KVMemory, MemoryEntry } from "./key-value";
|
|
3
|
+
/**
|
|
4
|
+
* Search result from a vector memory query.
|
|
5
|
+
*/
|
|
6
|
+
export interface VectorSearchResult<T = unknown> {
|
|
7
|
+
/** The matching entry */
|
|
8
|
+
entry: MemoryEntry<T>;
|
|
9
|
+
/** The key of the matching entry */
|
|
10
|
+
key: string;
|
|
11
|
+
/** Similarity score (0-1, higher is more similar) */
|
|
12
|
+
score: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Options for vector search operations.
|
|
16
|
+
*/
|
|
17
|
+
export interface VectorSearchOptions {
|
|
18
|
+
/** Maximum number of results to return. Default: 10 */
|
|
19
|
+
limit?: number;
|
|
20
|
+
/** Minimum similarity threshold (0-1). Results below this are excluded. */
|
|
21
|
+
threshold?: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Vector memory interface for semantic search.
|
|
25
|
+
*
|
|
26
|
+
* Extends the KV memory interface with vector-based similarity search.
|
|
27
|
+
* Useful for RAG (Retrieval Augmented Generation), semantic memory,
|
|
28
|
+
* and finding related content.
|
|
29
|
+
*
|
|
30
|
+
* @template T - The type of data stored in the memory
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // Implementation would use embeddings from OpenAI, Cohere, etc.
|
|
35
|
+
* class PineconeStore<T> implements VectorMemory<T> {
|
|
36
|
+
* async search(query: string, options?: VectorSearchOptions) {
|
|
37
|
+
* const embedding = await this.embedder(query);
|
|
38
|
+
* return this.client.query(embedding, options);
|
|
39
|
+
* }
|
|
40
|
+
* // ... other methods
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export interface VectorMemory<T = unknown> extends KVMemory<T> {
|
|
45
|
+
/**
|
|
46
|
+
* Search for entries semantically similar to the query.
|
|
47
|
+
* @param query - The search query (will be embedded)
|
|
48
|
+
* @param options - Search options
|
|
49
|
+
* @returns Matching entries sorted by similarity (highest first)
|
|
50
|
+
*/
|
|
51
|
+
search(query: string, options?: VectorSearchOptions): MaybePromise<VectorSearchResult<T>[]>;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=vector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector.d.ts","sourceRoot":"","sources":["../../src/memory/vector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEzD;;GAEG;AACH,MAAM,WAAW,kBAAkB,CAAC,CAAC,GAAG,OAAO;IAC7C,yBAAyB;IACzB,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IACtB,oCAAoC;IACpC,GAAG,EAAE,MAAM,CAAC;IACZ,qDAAqD;IACrD,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,uDAAuD;IACvD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,2EAA2E;IAC3E,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,OAAO,CAAE,SAAQ,QAAQ,CAAC,CAAC,CAAC;IAC5D;;;;;OAKG;IACH,MAAM,CACJ,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,mBAAmB,GAC5B,YAAY,CAAC,kBAAkB,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;CAC1C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector.js","sourceRoot":"","sources":["../../src/memory/vector.ts"],"names":[],"mappings":""}
|