ai-database 0.0.0-development → 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/dist/index.mjs ADDED
@@ -0,0 +1,430 @@
1
+ // src/utils.ts
2
+ var handleError = (error, operation, collection) => {
3
+ const errorMessage = error.message || "Unknown error";
4
+ const statusCode = error.status || error.statusCode || 500;
5
+ const enhancedError = new Error(`${operation} operation failed on collection '${collection}': ${errorMessage}`);
6
+ Object.assign(enhancedError, {
7
+ statusCode,
8
+ operation,
9
+ collection,
10
+ originalError: error
11
+ });
12
+ throw enhancedError;
13
+ };
14
+ var transformQueryOptions = (options = {}) => {
15
+ const result = {};
16
+ if (options.where) {
17
+ result.where = options.where;
18
+ }
19
+ if (options.sort) {
20
+ if (Array.isArray(options.sort)) {
21
+ result.sort = options.sort.join(",");
22
+ } else {
23
+ result.sort = options.sort;
24
+ }
25
+ }
26
+ if (options.limit) {
27
+ result.limit = options.limit;
28
+ }
29
+ if (options.page) {
30
+ result.page = options.page;
31
+ }
32
+ if (options.select) {
33
+ if (Array.isArray(options.select)) {
34
+ result.fields = options.select;
35
+ } else {
36
+ result.fields = [options.select];
37
+ }
38
+ }
39
+ if (options.populate) {
40
+ if (typeof options.populate === "string") {
41
+ result.depth = 1;
42
+ } else if (Array.isArray(options.populate) && options.populate.length > 0) {
43
+ result.depth = 1;
44
+ }
45
+ }
46
+ return result;
47
+ };
48
+ var getPayloadInstance = (options) => {
49
+ if (options.payload) {
50
+ return options.payload;
51
+ }
52
+ if (options.apiUrl) {
53
+ const restConfig = {
54
+ apiUrl: options.apiUrl,
55
+ apiKey: options.apiKey,
56
+ headers: options.headers,
57
+ fetchOptions: options.fetchOptions
58
+ };
59
+ return createRestClient(restConfig);
60
+ }
61
+ try {
62
+ if (typeof global !== "undefined" && global.payload) {
63
+ return global.payload;
64
+ }
65
+ } catch (e) {
66
+ }
67
+ throw new Error("No Payload instance provided. Please provide a payload instance or apiUrl.");
68
+ };
69
+ var createRestClient = (config) => {
70
+ const { apiUrl, apiKey, headers = {}, fetchOptions = {} } = config;
71
+ const requestHeaders = {
72
+ "Content-Type": "application/json",
73
+ ...headers
74
+ };
75
+ if (apiKey) {
76
+ requestHeaders["Authorization"] = `Bearer ${apiKey}`;
77
+ }
78
+ return {
79
+ find: async (options) => {
80
+ const { collection, ...params } = options;
81
+ const searchParams = new URLSearchParams();
82
+ Object.entries(params).forEach(([key, value]) => {
83
+ if (typeof value === "object") {
84
+ searchParams.append(key, JSON.stringify(value));
85
+ } else {
86
+ searchParams.append(key, String(value));
87
+ }
88
+ });
89
+ const url = `${apiUrl}/${collection}?${searchParams.toString()}`;
90
+ const response = await fetch(url, {
91
+ method: "GET",
92
+ headers: requestHeaders,
93
+ ...fetchOptions
94
+ });
95
+ if (!response.ok) {
96
+ const error = await response.json();
97
+ throw new Error(error.message || `Failed to fetch from ${collection}`);
98
+ }
99
+ return response.json();
100
+ },
101
+ findByID: async (options) => {
102
+ const { collection, id, ...params } = options;
103
+ const searchParams = new URLSearchParams();
104
+ Object.entries(params).forEach(([key, value]) => {
105
+ if (typeof value === "object") {
106
+ searchParams.append(key, JSON.stringify(value));
107
+ } else {
108
+ searchParams.append(key, String(value));
109
+ }
110
+ });
111
+ const url = `${apiUrl}/${collection}/${id}?${searchParams.toString()}`;
112
+ const response = await fetch(url, {
113
+ method: "GET",
114
+ headers: requestHeaders,
115
+ ...fetchOptions
116
+ });
117
+ if (!response.ok) {
118
+ const error = await response.json();
119
+ throw new Error(error.message || `Failed to fetch ${id} from ${collection}`);
120
+ }
121
+ return response.json();
122
+ },
123
+ create: async (options) => {
124
+ const { collection, data } = options;
125
+ const url = `${apiUrl}/${collection}`;
126
+ const response = await fetch(url, {
127
+ method: "POST",
128
+ headers: requestHeaders,
129
+ body: JSON.stringify(data),
130
+ ...fetchOptions
131
+ });
132
+ if (!response.ok) {
133
+ const error = await response.json();
134
+ throw new Error(error.message || `Failed to create document in ${collection}`);
135
+ }
136
+ return response.json();
137
+ },
138
+ update: async (options) => {
139
+ const { collection, id, data } = options;
140
+ const url = `${apiUrl}/${collection}/${id}`;
141
+ const response = await fetch(url, {
142
+ method: "PATCH",
143
+ headers: requestHeaders,
144
+ body: JSON.stringify(data),
145
+ ...fetchOptions
146
+ });
147
+ if (!response.ok) {
148
+ const error = await response.json();
149
+ throw new Error(error.message || `Failed to update document ${id} in ${collection}`);
150
+ }
151
+ return response.json();
152
+ },
153
+ delete: async (options) => {
154
+ const { collection, id } = options;
155
+ const url = `${apiUrl}/${collection}/${id}`;
156
+ const response = await fetch(url, {
157
+ method: "DELETE",
158
+ headers: requestHeaders,
159
+ ...fetchOptions
160
+ });
161
+ if (!response.ok) {
162
+ const error = await response.json();
163
+ throw new Error(error.message || `Failed to delete document ${id} from ${collection}`);
164
+ }
165
+ return response.json();
166
+ }
167
+ };
168
+ };
169
+
170
+ // src/collectionHandler.ts
171
+ var CollectionHandler = class {
172
+ payload;
173
+ collection;
174
+ /**
175
+ * Creates a new collection handler
176
+ * @param options Collection handler options
177
+ */
178
+ constructor(options) {
179
+ this.payload = options.payload;
180
+ this.collection = options.collectionName;
181
+ }
182
+ /**
183
+ * Find documents in the collection
184
+ * @template T Type of documents to return
185
+ * @param options Query options including filtering, sorting, and pagination
186
+ * @returns Promise resolving to a list of documents
187
+ */
188
+ async find(options = {}) {
189
+ try {
190
+ const transformedOptions = transformQueryOptions(options);
191
+ const result = await this.payload.find({
192
+ collection: this.collection,
193
+ ...transformedOptions
194
+ });
195
+ return {
196
+ docs: result.docs || [],
197
+ totalDocs: result.totalDocs || 0,
198
+ page: result.page || 1,
199
+ totalPages: result.totalPages || 1,
200
+ limit: result.limit || 10,
201
+ hasPrevPage: result.hasPrevPage || false,
202
+ hasNextPage: result.hasNextPage || false,
203
+ prevPage: result.prevPage || null,
204
+ nextPage: result.nextPage || null
205
+ };
206
+ } catch (error) {
207
+ return handleError(error, "find", this.collection);
208
+ }
209
+ }
210
+ /**
211
+ * Find a single document by ID
212
+ * @template T Type of document to return
213
+ * @param id Document ID
214
+ * @returns Promise resolving to a single document
215
+ */
216
+ async findOne(id) {
217
+ try {
218
+ return await this.payload.findByID({
219
+ collection: this.collection,
220
+ id
221
+ });
222
+ } catch (error) {
223
+ return handleError(error, "findOne", this.collection);
224
+ }
225
+ }
226
+ /**
227
+ * Create a new document in the collection
228
+ * @template T Type of document to create
229
+ * @param data Document data
230
+ * @returns Promise resolving to the created document
231
+ */
232
+ async create(data) {
233
+ try {
234
+ return await this.payload.create({
235
+ collection: this.collection,
236
+ data
237
+ });
238
+ } catch (error) {
239
+ return handleError(error, "create", this.collection);
240
+ }
241
+ }
242
+ /**
243
+ * Update an existing document
244
+ * @template T Type of document to update
245
+ * @param id Document ID
246
+ * @param data Updated document data
247
+ * @returns Promise resolving to the updated document
248
+ */
249
+ async update(id, data) {
250
+ try {
251
+ return await this.payload.update({
252
+ collection: this.collection,
253
+ id,
254
+ data
255
+ });
256
+ } catch (error) {
257
+ return handleError(error, "update", this.collection);
258
+ }
259
+ }
260
+ /**
261
+ * Delete a document
262
+ * @template T Type of deleted document
263
+ * @param id Document ID
264
+ * @returns Promise resolving to the deleted document
265
+ */
266
+ async delete(id) {
267
+ try {
268
+ return await this.payload.delete({
269
+ collection: this.collection,
270
+ id
271
+ });
272
+ } catch (error) {
273
+ return handleError(error, "delete", this.collection);
274
+ }
275
+ }
276
+ /**
277
+ * Search for documents
278
+ * @template T Type of documents to search
279
+ * @param query Search query string
280
+ * @param options Query options
281
+ * @returns Promise resolving to a list of matching documents
282
+ */
283
+ async search(query, options = {}) {
284
+ try {
285
+ const transformedOptions = transformQueryOptions(options);
286
+ if (typeof this.payload.search === "function") {
287
+ const result2 = await this.payload.search({
288
+ collection: this.collection,
289
+ query,
290
+ ...transformedOptions
291
+ });
292
+ return {
293
+ docs: result2.docs || [],
294
+ totalDocs: result2.totalDocs || 0,
295
+ page: result2.page || 1,
296
+ totalPages: result2.totalPages || 1,
297
+ limit: result2.limit || 10,
298
+ hasPrevPage: result2.hasPrevPage || false,
299
+ hasNextPage: result2.hasNextPage || false,
300
+ prevPage: result2.prevPage || null,
301
+ nextPage: result2.nextPage || null
302
+ };
303
+ }
304
+ const result = await this.payload.find({
305
+ collection: this.collection,
306
+ search: query,
307
+ ...transformedOptions
308
+ });
309
+ return {
310
+ docs: result.docs || [],
311
+ totalDocs: result.totalDocs || 0,
312
+ page: result.page || 1,
313
+ totalPages: result.totalPages || 1,
314
+ limit: result.limit || 10,
315
+ hasPrevPage: result.hasPrevPage || false,
316
+ hasNextPage: result.hasNextPage || false,
317
+ prevPage: result.prevPage || null,
318
+ nextPage: result.nextPage || null
319
+ };
320
+ } catch (error) {
321
+ return handleError(error, "search", this.collection);
322
+ }
323
+ }
324
+ };
325
+
326
+ // src/adapters/node.ts
327
+ var createNodeClient = (options = {}) => {
328
+ return DB(options);
329
+ };
330
+
331
+ // src/adapters/edge.ts
332
+ var createEdgeClient = (options = {}) => {
333
+ if (!options.apiUrl) {
334
+ throw new Error("apiUrl is required for Edge environments");
335
+ }
336
+ return DB(options);
337
+ };
338
+
339
+ // src/embedding.ts
340
+ async function generateEmbedding(text, options = {}) {
341
+ try {
342
+ const modelName = options.model || "text-embedding-3-small";
343
+ const apiKey = options.apiKey || typeof globalThis !== "undefined" && globalThis.process?.env?.OPENAI_API_KEY;
344
+ if (!apiKey) {
345
+ throw new Error("apiKey option or OPENAI_API_KEY environment variable is required for embedding generation");
346
+ }
347
+ const input = Array.isArray(text) ? text : [text];
348
+ const response = await fetch("https://api.openai.com/v1/embeddings", {
349
+ method: "POST",
350
+ headers: {
351
+ "Content-Type": "application/json",
352
+ Authorization: `Bearer ${apiKey}`
353
+ },
354
+ body: JSON.stringify({
355
+ input,
356
+ model: modelName
357
+ })
358
+ });
359
+ if (!response.ok) {
360
+ const errorData = await response.json().catch(() => ({}));
361
+ throw new Error(`OpenAI API error: ${errorData.error?.message || response.statusText}`);
362
+ }
363
+ const data = await response.json();
364
+ const embeddings = [];
365
+ if (data && data.data && Array.isArray(data.data)) {
366
+ for (const item of data.data) {
367
+ if (item && item.embedding && Array.isArray(item.embedding)) {
368
+ embeddings.push(item.embedding);
369
+ }
370
+ }
371
+ }
372
+ return {
373
+ embedding: embeddings.length > 0 ? embeddings : null,
374
+ model: modelName,
375
+ success: embeddings.length > 0
376
+ };
377
+ } catch (error) {
378
+ console.error("Error generating embedding:", error);
379
+ return {
380
+ embedding: null,
381
+ model: options.model || "text-embedding-3-small",
382
+ success: false,
383
+ error: error instanceof Error ? error.message : String(error)
384
+ };
385
+ }
386
+ }
387
+ function calculateSimilarity(embedding1, embedding2) {
388
+ if (embedding1.length !== embedding2.length) {
389
+ throw new Error("Embeddings must have the same dimensions");
390
+ }
391
+ let dotProduct = 0;
392
+ let magnitude1 = 0;
393
+ let magnitude2 = 0;
394
+ for (let i = 0; i < embedding1.length; i++) {
395
+ dotProduct += embedding1[i] * embedding2[i];
396
+ magnitude1 += embedding1[i] * embedding1[i];
397
+ magnitude2 += embedding2[i] * embedding2[i];
398
+ }
399
+ magnitude1 = Math.sqrt(magnitude1);
400
+ magnitude2 = Math.sqrt(magnitude2);
401
+ if (magnitude1 === 0 || magnitude2 === 0) {
402
+ return 0;
403
+ }
404
+ return dotProduct / (magnitude1 * magnitude2);
405
+ }
406
+
407
+ // src/index.ts
408
+ var DB = (options = {}) => {
409
+ const payload = getPayloadInstance(options);
410
+ return new Proxy({}, {
411
+ get: (target, prop) => {
412
+ if (typeof prop === "string" && prop !== "then") {
413
+ const collectionName = prop === "resources" ? "things" : String(prop);
414
+ return new CollectionHandler({ payload, collectionName });
415
+ }
416
+ return target[prop];
417
+ }
418
+ });
419
+ };
420
+ var db = DB();
421
+ var index_default = DB;
422
+ export {
423
+ DB,
424
+ calculateSimilarity,
425
+ createEdgeClient,
426
+ createNodeClient,
427
+ db,
428
+ index_default as default,
429
+ generateEmbedding
430
+ };
package/package.json CHANGED
@@ -1,33 +1,47 @@
1
1
  {
2
2
  "name": "ai-database",
3
- "version": "0.0.0-development",
4
- "description": "AI-native database abstraction with hybrid vector search for synthetic data and RAG applications. Currently using local mdxdb types until @mdxdb/types is published.",
5
- "type": "module",
3
+ "version": "0.1.0",
4
+ "description": "Direct interface to Payload CMS with database.do compatibility",
6
5
  "main": "dist/index.js",
7
- "types": "dist/types/index.d.ts",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
8
  "files": [
9
- "dist/types"
9
+ "dist",
10
+ "README.md"
10
11
  ],
11
12
  "scripts": {
12
- "build": "tsc",
13
- "test": "vitest",
14
- "lint": "eslint .",
15
- "format": "prettier --write ."
13
+ "build": "tsup src/index.ts --format esm,cjs --dts",
14
+ "dev": "tsup --watch",
15
+ "lint": "eslint src",
16
+ "format": "prettier --write src",
17
+ "test": "vitest run",
18
+ "test:watch": "vitest"
19
+ },
20
+ "keywords": [
21
+ "payload",
22
+ "cms",
23
+ "database",
24
+ "api"
25
+ ],
26
+ "author": "Drivly",
27
+ "license": "MIT",
28
+ "homepage": "https://mdx.org.ai",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/drivly/ai"
32
+ },
33
+ "bugs": {
34
+ "url": "https://github.com/drivly/ai/issues"
16
35
  },
17
36
  "devDependencies": {
18
37
  "@types/node": "^20.0.0",
19
- "@typescript-eslint/eslint-plugin": "^6.0.0",
20
- "@typescript-eslint/parser": "^6.0.0",
21
- "eslint": "^8.0.0",
22
- "eslint-config-prettier": "^9.0.0",
23
- "mdxld": "^0.1.0",
24
- "prettier": "^3.0.0",
38
+ "apis.do": "0.1.0",
39
+ "payload": "^2.0.0",
40
+ "tsup": "^8.0.0",
25
41
  "typescript": "^5.0.0",
26
42
  "vitest": "^1.0.0"
27
43
  },
28
- "dependencies": {
29
- "@ai-sdk/openai": "^1.0.8",
30
- "ai": "^2.2.37",
31
- "openai": "^4.76.3"
44
+ "engines": {
45
+ "node": ">=18"
32
46
  }
33
47
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 AI Primitives
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,46 +0,0 @@
1
- import { AIDocument } from './document';
2
- import { AIVectorSearchOptions } from './vector';
3
- /**
4
- * Configuration options for the AI-native database
5
- */
6
- export interface DatabaseConfig {
7
- /**
8
- * Database name or identifier
9
- */
10
- name: string;
11
- /**
12
- * Optional vector search configuration
13
- */
14
- vectorSearch?: {
15
- /**
16
- * Dimensions for vector embeddings
17
- */
18
- dimensions?: number;
19
- /**
20
- * Model to use for embeddings
21
- */
22
- model?: string;
23
- };
24
- /**
25
- * Optional synthetic data generation configuration
26
- */
27
- synthetic?: {
28
- /**
29
- * Model to use for synthetic data generation
30
- */
31
- model?: string;
32
- };
33
- }
34
- /**
35
- * Database provider interface
36
- */
37
- export interface DatabaseProvider {
38
- /**
39
- * Query documents using AI-powered vector search
40
- */
41
- query(options: AIVectorSearchOptions): Promise<AIDocument[]>;
42
- /**
43
- * Insert a document into the database
44
- */
45
- insert(document: AIDocument): Promise<void>;
46
- }
@@ -1,15 +0,0 @@
1
- import type { Document } from './mdxdb/types';
2
- export interface AIDocument extends Document {
3
- metadata?: {
4
- model?: string;
5
- temperature?: number;
6
- tokens?: number;
7
- provider?: string;
8
- };
9
- synthetic?: boolean;
10
- toolCalls?: Array<{
11
- name: string;
12
- arguments: Record<string, unknown>;
13
- result?: unknown;
14
- }>;
15
- }
@@ -1,5 +0,0 @@
1
- export * from './document';
2
- export * from './vector';
3
- export * from './tools';
4
- export * from './synthetic';
5
- export * from './database';
@@ -1,7 +0,0 @@
1
- export interface EmbeddingOptions {
2
- dimensions?: number;
3
- model?: string;
4
- }
5
- export interface EmbeddingProvider {
6
- embed(text: string, options?: EmbeddingOptions): Promise<number[]>;
7
- }
@@ -1,59 +0,0 @@
1
- import type { MDXLD } from 'mdxld';
2
- export interface Document extends MDXLD {
3
- embeddings?: number[];
4
- collections?: string[];
5
- }
6
- export interface VectorSearchOptions {
7
- vector?: number[];
8
- query?: string;
9
- filter?: Record<string, unknown>;
10
- k?: number;
11
- threshold?: number;
12
- }
13
- export interface NamespaceOptions {
14
- defaultNamespace?: string;
15
- enforceHttps?: boolean;
16
- maxPathDepth?: number;
17
- allowSubdomains?: boolean;
18
- }
19
- export interface DatabaseOptions {
20
- namespace: string;
21
- baseUrl?: string;
22
- options?: NamespaceOptions;
23
- }
24
- export interface CollectionOptions {
25
- path: string;
26
- database: DatabaseProvider;
27
- }
28
- export interface DatabaseProvider<T extends Document = Document> {
29
- namespace: string;
30
- connect(): Promise<void>;
31
- disconnect(): Promise<void>;
32
- list(): Promise<string[]>;
33
- collection(name: string): CollectionProvider<T>;
34
- [key: string]: DatabaseProvider<T> | CollectionProvider<T> | string | (() => Promise<void>) | (() => Promise<string[]>) | ((name: string) => CollectionProvider<T>);
35
- }
36
- export type FilterQuery<T> = {
37
- [K in keyof T]?: T[K] | {
38
- $eq?: T[K];
39
- $gt?: T[K];
40
- $gte?: T[K];
41
- $lt?: T[K];
42
- $lte?: T[K];
43
- $in?: T[K][];
44
- $nin?: T[K][];
45
- };
46
- };
47
- export interface SearchOptions<T extends Document = Document> {
48
- filter?: FilterQuery<T>;
49
- threshold?: number;
50
- limit?: number;
51
- offset?: number;
52
- includeVectors?: boolean;
53
- }
54
- export interface CollectionProvider<T extends Document = Document> {
55
- path: string;
56
- find(filter: FilterQuery<T>, options?: SearchOptions<T>): Promise<T[]>;
57
- search(query: string, options?: SearchOptions<T>): Promise<T[]>;
58
- vectorSearch(options: VectorSearchOptions & SearchOptions<T>): Promise<T[]>;
59
- }
@@ -1,9 +0,0 @@
1
- export interface SyntheticDataOptions {
2
- schema: Record<string, unknown>;
3
- count: number;
4
- model?: string;
5
- temperature?: number;
6
- }
7
- export interface SyntheticDataProvider {
8
- generate(options: SyntheticDataOptions): Promise<unknown[]>;
9
- }
@@ -1,10 +0,0 @@
1
- export interface ToolDefinition {
2
- name: string;
3
- description: string;
4
- parameters: Record<string, unknown>;
5
- returns: Record<string, unknown>;
6
- }
7
- export interface ToolProvider {
8
- namespace: string;
9
- tools: ToolDefinition[];
10
- }
@@ -1,16 +0,0 @@
1
- import type { VectorSearchOptions } from './mdxdb/types';
2
- import type { EmbeddingOptions, EmbeddingProvider } from './mdxdb/embedding';
3
- export interface AIVectorSearchOptions extends VectorSearchOptions {
4
- rerank?: boolean;
5
- hybridWeight?: number;
6
- contextWindow?: number;
7
- }
8
- export interface AIEmbeddingOptions extends EmbeddingOptions {
9
- provider?: string;
10
- batchSize?: number;
11
- normalize?: boolean;
12
- }
13
- export interface AIEmbeddingProvider extends EmbeddingProvider {
14
- batchEmbed?(texts: string[], options?: AIEmbeddingOptions): Promise<number[][]>;
15
- normalize?(vector: number[]): number[];
16
- }