|
| 1 | +import cbws from './websocket'; |
| 2 | + |
| 3 | +const VectorDB = { |
| 4 | + /** |
| 5 | + * Retrieves a vector from the vector database based on the provided key. |
| 6 | + * |
| 7 | + * @param {string} key - The key of the vector to retrieve. |
| 8 | + * @returns {Promise<any>} A promise that resolves with the retrieved vector. |
| 9 | + */ |
| 10 | + getVector: async (key: string): Promise<any> => { |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + cbws.getWebsocket.send(JSON.stringify({ |
| 13 | + "type": "getVector", |
| 14 | + "message": { |
| 15 | + item: key |
| 16 | + }, |
| 17 | + })); |
| 18 | + cbws.getWebsocket.on('message', (data: string) => { |
| 19 | + const response = JSON.parse(data); |
| 20 | + if (response.type === "getVectorResponse") { |
| 21 | + resolve(response); |
| 22 | + } |
| 23 | + }); |
| 24 | + }); |
| 25 | + }, |
| 26 | + |
| 27 | + /** |
| 28 | + * Adds a new vector item to the vector database. |
| 29 | + * |
| 30 | +
|
| 31 | + * @param {any} item - The item to add to the vector. |
| 32 | + * @returns {Promise<any>} A promise that resolves when the item is successfully added. |
| 33 | + */ |
| 34 | + addVectorItem: async ( item: any): Promise<any> => { |
| 35 | + return new Promise((resolve, reject) => { |
| 36 | + cbws.getWebsocket.send(JSON.stringify({ |
| 37 | + "type": "addVectorItem", |
| 38 | + "message": { |
| 39 | + item: item |
| 40 | + }, |
| 41 | + })); |
| 42 | + cbws.getWebsocket.on('message', (data: string) => { |
| 43 | + const response = JSON.parse(data); |
| 44 | + if (response.type === "addVectorItemResponse") { |
| 45 | + resolve(response); |
| 46 | + } |
| 47 | + }); |
| 48 | + }); |
| 49 | + }, |
| 50 | + |
| 51 | + /** |
| 52 | + * Queries a vector item from the vector database based on the provided key. |
| 53 | + * |
| 54 | + * @param {string} key - The key of the vector to query the item from. |
| 55 | + * @returns {Promise<any>} A promise that resolves with the queried vector item. |
| 56 | + */ |
| 57 | + queryVectorItem: async (key: string): Promise<any> => { |
| 58 | + return new Promise((resolve, reject) => { |
| 59 | + cbws.getWebsocket.send(JSON.stringify({ |
| 60 | + "type": "queryVectorItem", |
| 61 | + "message": { |
| 62 | + item: key |
| 63 | + }, |
| 64 | + })); |
| 65 | + cbws.getWebsocket.on('message', (data: string) => { |
| 66 | + const response = JSON.parse(data); |
| 67 | + if (response.type === "qeryVectorItemResponse") { |
| 68 | + resolve(response); |
| 69 | + } |
| 70 | + }); |
| 71 | + }); |
| 72 | + }, |
| 73 | +}; |
| 74 | + |
| 75 | +export default VectorDB; |
0 commit comments