@medyll/idae-chroma 0.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 ADDED
@@ -0,0 +1,58 @@
1
+ # create-svelte
2
+
3
+ Everything you need to build a Svelte library, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte).
4
+
5
+ Read more about creating a library [in the docs](https://kit.svelte.dev/docs/packaging).
6
+
7
+ ## Creating a project
8
+
9
+ If you're seeing this, you've probably already done this step. Congrats!
10
+
11
+ ```bash
12
+ # create a new project in the current directory
13
+ npm create svelte@latest
14
+
15
+ # create a new project in my-app
16
+ npm create svelte@latest my-app
17
+ ```
18
+
19
+ ## Developing
20
+
21
+ Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
22
+
23
+ ```bash
24
+ npm run dev
25
+
26
+ # or start the server and open the app in a new browser tab
27
+ npm run dev -- --open
28
+ ```
29
+
30
+ Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app.
31
+
32
+ ## Building
33
+
34
+ To build your library:
35
+
36
+ ```bash
37
+ npm run package
38
+ ```
39
+
40
+ To create a production version of your showcase app:
41
+
42
+ ```bash
43
+ npm run build
44
+ ```
45
+
46
+ You can preview the production build with `npm run preview`.
47
+
48
+ > To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
49
+
50
+ ## Publishing
51
+
52
+ Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)).
53
+
54
+ To publish your library to [npm](https://www.npmjs.com):
55
+
56
+ ```bash
57
+ npm publish
58
+ ```
@@ -0,0 +1,22 @@
1
+ import { QueryParams } from 'chromadb';
2
+ type Metadata = Record<string, string | number | boolean>;
3
+ export type ChromaQueryParams = QueryParams;
4
+ export declare class IdaeChromaClient {
5
+ private client;
6
+ private collection;
7
+ private collectionName;
8
+ constructor(config?: {
9
+ path: string;
10
+ });
11
+ initCollection(collectionName: string): Promise<void>;
12
+ addEntries(ids: string | string[], contents: string | string[], metadatas: Metadata | Metadata[], embeddings?: number[] | number[][]): Promise<void>;
13
+ updateEntries(ids: string | string[], contents: string | string[], metadatas: Metadata | Metadata[], embeddings?: number[] | number[][]): Promise<void>;
14
+ deleteEntries(ids: string | string[]): Promise<void>;
15
+ query(query: QueryParams): Promise<import("chromadb").QueryResponse>;
16
+ getEntries(ids: string | string[]): Promise<import("chromadb").GetResponse>;
17
+ getEntriesByMetadata(where: Metadata): Promise<import("chromadb").GetResponse>;
18
+ listCollections(): Promise<import("chromadb").CollectionType[]>;
19
+ deleteCollection(name: string): Promise<void>;
20
+ count(): Promise<number>;
21
+ }
22
+ export {};
@@ -0,0 +1,109 @@
1
+ import { ChromaClient as Chroma, Collection, QueryParams } from 'chromadb';
2
+ export class IdaeChromaClient {
3
+ client;
4
+ collection = null;
5
+ collectionName;
6
+ constructor(config) {
7
+ try {
8
+ this.client = new Chroma(config);
9
+ }
10
+ catch (error) {
11
+ console.error('Erreur lors de la création du client Chroma:', error);
12
+ throw new Error('Impossible de se connecter à la base de données Chroma');
13
+ }
14
+ }
15
+ async initCollection(collectionName) {
16
+ if (!this.collection) {
17
+ try {
18
+ this.collectionName = collectionName;
19
+ this.collection = await this.client.getOrCreateCollection({
20
+ name: this.collectionName
21
+ });
22
+ }
23
+ catch (error) {
24
+ console.error('Erreur lors de la création ou récupération de la collection:', error);
25
+ throw new Error(`Impossible de créer ou récupérer la collection ${this.collectionName}`);
26
+ }
27
+ }
28
+ }
29
+ async addEntries(ids, contents, metadatas, embeddings) {
30
+ if (!this.collection)
31
+ throw new Error('Collection not initialized');
32
+ await this.collection.add({
33
+ ids: Array.isArray(ids) ? ids : [ids],
34
+ documents: Array.isArray(contents) ? contents : [contents],
35
+ metadatas: Array.isArray(metadatas) ? metadatas : [metadatas],
36
+ embeddings: embeddings
37
+ ? Array.isArray(embeddings[0])
38
+ ? embeddings
39
+ : [embeddings]
40
+ : undefined
41
+ });
42
+ }
43
+ async updateEntries(ids, contents, metadatas, embeddings) {
44
+ if (!this.collection)
45
+ throw new Error('Collection not initialized');
46
+ await this.collection.update({
47
+ ids: Array.isArray(ids) ? ids : [ids],
48
+ documents: Array.isArray(contents) ? contents : [contents],
49
+ metadatas: Array.isArray(metadatas) ? metadatas : [metadatas],
50
+ embeddings: embeddings
51
+ ? Array.isArray(embeddings[0])
52
+ ? embeddings
53
+ : [embeddings]
54
+ : undefined
55
+ });
56
+ }
57
+ async deleteEntries(ids) {
58
+ if (!this.collection)
59
+ throw new Error('Collection not initialized');
60
+ await this.collection.delete({
61
+ ids: Array.isArray(ids) ? ids : [ids]
62
+ });
63
+ }
64
+ async query(query) {
65
+ if (!this.collection)
66
+ throw new Error('Collection not initialized');
67
+ return await this.collection.query(query);
68
+ }
69
+ async getEntries(ids) {
70
+ if (!this.collection)
71
+ throw new Error('Collection not initialized');
72
+ return await this.collection.get({
73
+ ids: Array.isArray(ids) ? ids : [ids]
74
+ });
75
+ }
76
+ async getEntriesByMetadata(where) {
77
+ if (!this.collection)
78
+ throw new Error('Collection not initialized');
79
+ return await this.collection.get({
80
+ where
81
+ });
82
+ }
83
+ async listCollections() {
84
+ try {
85
+ return await this.client.listCollections();
86
+ }
87
+ catch (error) {
88
+ console.error('Erreur lors de la liste des collections:', error);
89
+ throw new Error('Impossible de lister les collections');
90
+ }
91
+ }
92
+ async deleteCollection(name) {
93
+ try {
94
+ await this.client.deleteCollection({ name });
95
+ if (this.collection?.name === name) {
96
+ this.collection = null;
97
+ }
98
+ }
99
+ catch (error) {
100
+ console.error('Erreur lors de la suppression de la collection:', error);
101
+ throw new Error(`Impossible de supprimer la collection ${name}`);
102
+ }
103
+ }
104
+ async count() {
105
+ if (!this.collection)
106
+ throw new Error('Collection not initialized');
107
+ return await this.collection.count();
108
+ }
109
+ }
@@ -0,0 +1,8 @@
1
+ import { IdaeChromaClient } from '../database/chromaClient.js';
2
+ export declare class Processor {
3
+ private chromaClient;
4
+ constructor(chromaClient: IdaeChromaClient);
5
+ processFile(filePath: string, action: 'add' | 'change' | 'delete'): Promise<void>;
6
+ private generateFileId;
7
+ private getFileMetadata;
8
+ }
@@ -0,0 +1,36 @@
1
+ import { IdaeChromaClient } from '../database/chromaClient.js';
2
+ import fs from 'fs/promises';
3
+ import path from 'path';
4
+ export class Processor {
5
+ chromaClient;
6
+ constructor(chromaClient) {
7
+ this.chromaClient = chromaClient;
8
+ }
9
+ async processFile(filePath, action) {
10
+ const fileId = this.generateFileId(filePath);
11
+ const metadata = await this.getFileMetadata(filePath);
12
+ switch (action) {
13
+ case 'add':
14
+ case 'change': {
15
+ const content = await fs.readFile(filePath, 'utf-8');
16
+ await this.chromaClient.addEntries(fileId, content, metadata);
17
+ break;
18
+ }
19
+ case 'delete':
20
+ await this.chromaClient.deleteEntries(fileId);
21
+ break;
22
+ }
23
+ }
24
+ generateFileId(filePath) {
25
+ return path.relative(process.cwd(), filePath);
26
+ }
27
+ async getFileMetadata(filePath) {
28
+ const stats = await fs.stat(filePath);
29
+ return {
30
+ path: filePath,
31
+ size: stats.size,
32
+ lastModified: stats.mtime.toISOString(),
33
+ extension: path.extname(filePath)
34
+ };
35
+ }
36
+ }
@@ -0,0 +1,12 @@
1
+ import { IdaeChromaClient } from '../database/chromaClient.js';
2
+ import { WatcherOptions } from '../main.js';
3
+ export declare class Watcher {
4
+ private directory;
5
+ private options;
6
+ private chromaClient;
7
+ private watcher;
8
+ private processor;
9
+ constructor(directory: string, options: WatcherOptions, chromaClient: IdaeChromaClient);
10
+ start(): void;
11
+ stop(): void;
12
+ }
@@ -0,0 +1,27 @@
1
+ import chokidar from 'chokidar';
2
+ import { Processor } from './processor.js';
3
+ import { IdaeChromaClient } from '../database/chromaClient.js';
4
+ import { WatcherOptions } from '../main.js';
5
+ export class Watcher {
6
+ directory;
7
+ options;
8
+ chromaClient;
9
+ watcher;
10
+ processor;
11
+ constructor(directory, options, chromaClient) {
12
+ this.directory = directory;
13
+ this.options = options;
14
+ this.chromaClient = chromaClient;
15
+ this.processor = new Processor(chromaClient);
16
+ }
17
+ start() {
18
+ this.watcher = chokidar.watch(this.directory, this.options);
19
+ this.watcher
20
+ .on('add', (path) => this.processor.processFile(path, 'add'))
21
+ .on('change', (path) => this.processor.processFile(path, 'change'))
22
+ .on('unlink', (path) => this.processor.processFile(path, 'delete'));
23
+ }
24
+ stop() {
25
+ this.watcher.close();
26
+ }
27
+ }
@@ -0,0 +1,4 @@
1
+ export * from './main.js';
2
+ export * from './engine/watcher.js';
3
+ export * from './engine/processor.js';
4
+ export * from './database/chromaClient.js';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ // auto exports of entry components
2
+ export * from './main.js';
3
+ export * from './engine/watcher.js';
4
+ export * from './engine/processor.js';
5
+ export * from './database/chromaClient.js';
package/dist/main.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { IdaeChromaClient } from './database/chromaClient.js';
2
+ export interface ChromaDbConfig {
3
+ path: string;
4
+ collectionName?: string;
5
+ }
6
+ export interface WatcherOptions {
7
+ ignored?: string | RegExp | ((path: string) => boolean);
8
+ persistent?: boolean;
9
+ ignoreInitial?: boolean;
10
+ followSymlinks?: boolean;
11
+ cwd?: string;
12
+ disableGlobbing?: boolean;
13
+ usePolling?: boolean;
14
+ interval?: number;
15
+ binaryInterval?: number;
16
+ alwaysStat?: boolean;
17
+ depth?: number;
18
+ awaitWriteFinish?: boolean | {
19
+ stabilityThreshold?: number;
20
+ pollInterval?: number;
21
+ };
22
+ ignorePermissionErrors?: boolean;
23
+ atomic?: boolean | number;
24
+ }
25
+ export interface SearchParams {
26
+ nResults?: number;
27
+ where?: Record<string, any>;
28
+ whereDocument?: Record<string, any>;
29
+ }
30
+ declare class IdaeChroma {
31
+ #private;
32
+ private watchers;
33
+ private configDir;
34
+ constructor(chromaDbConfig?: ChromaDbConfig);
35
+ get chromaClient(): IdaeChromaClient;
36
+ addWatcher(directory: string, options: WatcherOptions): Promise<void>;
37
+ searchEntries(query: string, params: SearchParams): Promise<any>;
38
+ stopAllWatchers(): void;
39
+ private saveWatcherConfig;
40
+ loadSavedWatchers(): Promise<void>;
41
+ }
42
+ export default IdaeChroma;
package/dist/main.js ADDED
@@ -0,0 +1,77 @@
1
+ import { Watcher } from './engine/watcher.js';
2
+ import { IdaeChromaClient } from './database/chromaClient.js';
3
+ import fs from 'fs/promises';
4
+ import path from 'path';
5
+ class IdaeChroma {
6
+ watchers = new Map();
7
+ #chromaClient;
8
+ configDir;
9
+ constructor(chromaDbConfig) {
10
+ this.#chromaClient = new IdaeChromaClient(chromaDbConfig);
11
+ this.configDir = path.join(process.cwd(), 'config');
12
+ }
13
+ get chromaClient() {
14
+ return this.#chromaClient;
15
+ }
16
+ async addWatcher(directory, options) {
17
+ try {
18
+ const watcher = new Watcher(directory, options, this.#chromaClient);
19
+ this.watchers.set(directory, watcher);
20
+ watcher.start();
21
+ await this.saveWatcherConfig(directory, options);
22
+ }
23
+ catch (error) {
24
+ console.error(`Failed to add watcher for directory: ${directory}`, error);
25
+ throw error;
26
+ }
27
+ }
28
+ async searchEntries(query, params) {
29
+ try {
30
+ return await this.#chromaClient.query(query, params.nResults, params.where);
31
+ }
32
+ catch (error) {
33
+ console.error('Failed to search entries:', error);
34
+ throw error;
35
+ }
36
+ }
37
+ stopAllWatchers() {
38
+ this.watchers.forEach((watcher) => {
39
+ try {
40
+ watcher.stop();
41
+ }
42
+ catch (error) {
43
+ console.error(`Failed to stop watcher:`, error);
44
+ }
45
+ });
46
+ }
47
+ async saveWatcherConfig(directory, options) {
48
+ try {
49
+ const configName = `last_config_${path.basename(directory)}.json`;
50
+ const configPath = path.join(this.configDir, configName);
51
+ await fs.mkdir(this.configDir, { recursive: true });
52
+ await fs.writeFile(configPath, JSON.stringify({ directory, options }, null, 2));
53
+ }
54
+ catch (error) {
55
+ console.error(`Failed to save watcher config for directory: ${directory}`, error);
56
+ throw error;
57
+ }
58
+ }
59
+ async loadSavedWatchers() {
60
+ try {
61
+ const files = await fs.readdir(this.configDir);
62
+ for (const file of files) {
63
+ if (file.startsWith('last_config_') && file.endsWith('.json')) {
64
+ const configPath = path.join(this.configDir, file);
65
+ const configContent = await fs.readFile(configPath, 'utf-8');
66
+ const { directory, options } = JSON.parse(configContent);
67
+ await this.addWatcher(directory, options);
68
+ }
69
+ }
70
+ }
71
+ catch (error) {
72
+ console.error('Failed to load saved watchers:', error);
73
+ throw error;
74
+ }
75
+ }
76
+ }
77
+ export default IdaeChroma;
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@medyll/idae-chroma",
3
+ "version": "0.0.1",
4
+ "scripts": {
5
+ "dev": "vite dev",
6
+ "build": "vite build && npm run package",
7
+ "preview": "vite preview",
8
+ "package": "svelte-kit sync && svelte-package && publint",
9
+ "prepublishOnly": "npm run package",
10
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
11
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
12
+ "test": "vitest",
13
+ "lint": "prettier --check . && eslint .",
14
+ "format": "prettier --write .",
15
+ "package:pre": "node scripts/package-pre.js"
16
+ },
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "svelte": "./dist/index.js"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "!dist/**/*.test.*",
26
+ "!dist/**/*.spec.*"
27
+ ],
28
+ "peerDependencies": {
29
+ "svelte": "^5.0.0-next.1"
30
+ },
31
+ "devDependencies": {
32
+ "@sveltejs/adapter-auto": "^3.0.0",
33
+ "@sveltejs/kit": "^2.0.0",
34
+ "@sveltejs/package": "^2.0.0",
35
+ "@sveltejs/vite-plugin-svelte": "^4.0.0-next.6",
36
+ "@types/eslint": "^9.6.0",
37
+ "eslint": "^9.0.0",
38
+ "eslint-config-prettier": "^9.1.0",
39
+ "eslint-plugin-svelte": "^2.36.0",
40
+ "globals": "^15.0.0",
41
+ "prettier": "^3.1.1",
42
+ "prettier-plugin-svelte": "^3.1.2",
43
+ "publint": "^0.2.0",
44
+ "svelte": "^5.0.0-next.241",
45
+ "svelte-check": "^3.6.0",
46
+ "typescript": "^5.0.0",
47
+ "typescript-eslint": "^8.0.0",
48
+ "vite": "^5.0.11",
49
+ "vitest": "^2.0.0"
50
+ },
51
+ "svelte": "./dist/index.js",
52
+ "types": "./dist/index.d.ts",
53
+ "type": "module",
54
+ "scope": "@medyll",
55
+ "dependencies": {
56
+ "chokidar": "^3.6.0",
57
+ "chromadb": "^1.8.1",
58
+ "chromadb-default-embed": "^2.13.2"
59
+ }
60
+ }