@getzep/zep-js
Advanced tools
Comparing version 0.6.3 to 0.7.0
@@ -26,2 +26,3 @@ import { DocumentCollectionModel, IDocument, IDocumentCollectionModel } from "./document_models"; | ||
* @throws {Error} If the collection name is not provided or no documents are provided. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -34,2 +35,4 @@ addDocuments(documents: IDocument[]): Promise<string[]>; | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -42,2 +45,4 @@ updateDocument({ uuid, documentId, metadata, }: IUpdateDocumentParams): Promise<void>; | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -50,2 +55,4 @@ deleteDocument(uuid: string): Promise<void>; | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -58,2 +65,4 @@ getDocument(uuid: string): Promise<IDocument>; | ||
* @throws {Error} If any of the documents do not match the expected format. | ||
* @throws {Error} If the collection name is not provided or no uuids are provided. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -69,2 +78,3 @@ getDocuments(uuids: string[]): Promise<IDocument[]>; | ||
* the search query does not have at least one of text, embedding, or metadata. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -77,2 +87,5 @@ searchReturnQueryVector(query: ISearchQuery, limit?: number): Promise<[IDocument[], Float32Array]>; | ||
* @returns {Promise<IDocument[]>} A promise that resolves to an array of documents. | ||
* @throws {Error} If the collection name is not provided or | ||
* the search query does not have at least one of text, embedding, or metadata. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -87,10 +100,5 @@ search(query: ISearchQuery, limit?: number): Promise<IDocument[]>; | ||
* has less than MIN_DOCS_TO_INDEX documents and force is not true. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
createIndex(force?: boolean): Promise<void>; | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint: string): string; | ||
} |
@@ -50,2 +50,3 @@ "use strict"; | ||
* @throws {Error} If the collection name is not provided or no documents are provided. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -63,4 +64,4 @@ addDocuments(documents) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/document`); | ||
const body = JSON.stringify((0, document_models_1.docsWithFloatArrayToDocs)(documents)); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/document`); | ||
const response = yield (0, utils_1.handleRequest)(fetch(url, { | ||
@@ -79,2 +80,4 @@ method: "POST", | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -89,3 +92,3 @@ updateDocument({ uuid, documentId, metadata, }) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/document/${uuid}`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/document/${uuid}`); | ||
yield (0, utils_1.handleRequest)(fetch(url, { | ||
@@ -107,2 +110,4 @@ method: "PATCH", | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -117,3 +122,3 @@ deleteDocument(uuid) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/document/uuid/${uuid}`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/document/uuid/${uuid}`); | ||
yield (0, utils_1.handleRequest)(fetch(url, { | ||
@@ -130,2 +135,4 @@ method: "DELETE", | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -140,3 +147,3 @@ getDocument(uuid) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/document/${uuid}`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/document/${uuid}`); | ||
const response = yield (0, utils_1.handleRequest)(fetch(url, { | ||
@@ -161,2 +168,4 @@ headers: this.client.headers, | ||
* @throws {Error} If any of the documents do not match the expected format. | ||
* @throws {Error} If the collection name is not provided or no uuids are provided. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -174,3 +183,3 @@ getDocuments(uuids) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/document/list/get`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/document/list/get`); | ||
const response = yield (0, utils_1.handleRequest)(fetch(url, { | ||
@@ -200,2 +209,3 @@ method: "POST", | ||
* the search query does not have at least one of text, embedding, or metadata. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -216,3 +226,3 @@ searchReturnQueryVector(query, limit) { | ||
const limitParam = limit ? `?limit=${limit}` : ""; | ||
const url = this.getFullUrl(`/collection/${this.name}/search${limitParam}`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/search${limitParam}`); | ||
const response = yield (0, utils_1.handleRequest)(fetch(url, { | ||
@@ -248,2 +258,5 @@ method: "POST", | ||
* @returns {Promise<IDocument[]>} A promise that resolves to an array of documents. | ||
* @throws {Error} If the collection name is not provided or | ||
* the search query does not have at least one of text, embedding, or metadata. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -263,2 +276,3 @@ search(query, limit) { | ||
* has less than MIN_DOCS_TO_INDEX documents and force is not true. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -276,3 +290,3 @@ createIndex(force) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/index/create${forceParam}`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/index/create${forceParam}`); | ||
yield (0, utils_1.handleRequest)(fetch(url, { | ||
@@ -284,11 +298,3 @@ method: "POST", | ||
} | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint) { | ||
return `${this.client.baseURL}${utils_1.API_BASEURL}${endpoint}`; | ||
} | ||
} | ||
exports.default = DocumentCollection; |
@@ -15,8 +15,2 @@ import { IAddCollectionParams, IUpdateCollectionParams, IZepClient } from "./interfaces"; | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint: string): string; | ||
/** | ||
* Adds a new collection to the Zep client. | ||
@@ -27,2 +21,3 @@ * @param {IAddCollectionParams} params - The parameters for the new collection. | ||
* @throws {Error} If embeddingDimensions is not a positive integer. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -36,2 +31,4 @@ addCollection({ name, embeddingDimensions, description, metadata, isAutoEmbedded, }: IAddCollectionParams): Promise<DocumentCollection>; | ||
* @throws {Error} If the collection name is not provided. | ||
* @throws {NotFoundError} If the collection is not found. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -45,2 +42,4 @@ getCollection(name: string): Promise<DocumentCollection>; | ||
* @throws {Error} If neither description nor metadata are provided. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the collection is not found. | ||
*/ | ||
@@ -52,2 +51,3 @@ updateCollection({ name, description, metadata, }: IUpdateCollectionParams): Promise<DocumentCollection>; | ||
* DocumentCollection instances. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -60,4 +60,6 @@ listCollections(): Promise<DocumentCollection[]>; | ||
* @throws {Error} If the collection name is not provided. | ||
* @throws {NotFoundError} If the collection is not found. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
deleteCollection(collectionName: string): Promise<void>; | ||
} |
@@ -31,10 +31,2 @@ "use strict"; | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint) { | ||
return `${this.client.baseURL}${utils_1.API_BASEURL}${endpoint}`; | ||
} | ||
/** | ||
* Adds a new collection to the Zep client. | ||
@@ -45,2 +37,3 @@ * @param {IAddCollectionParams} params - The parameters for the new collection. | ||
* @throws {Error} If embeddingDimensions is not a positive integer. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -59,3 +52,3 @@ addCollection({ name, embeddingDimensions, description, metadata, isAutoEmbedded = true, }) { | ||
}); | ||
yield (0, utils_1.handleRequest)(fetch(this.getFullUrl(`/collection/${name}`), { | ||
yield (0, utils_1.handleRequest)(fetch(this.client.getFullUrl(`/collection/${name}`), { | ||
method: "POST", | ||
@@ -74,2 +67,4 @@ headers: Object.assign(Object.assign({}, this.client.headers), { "Content-Type": "application/json" }), | ||
* @throws {Error} If the collection name is not provided. | ||
* @throws {NotFoundError} If the collection is not found. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -81,3 +76,3 @@ getCollection(name) { | ||
} | ||
const response = yield (0, utils_1.handleRequest)(fetch(this.getFullUrl(`/collection/${name}`), { | ||
const response = yield (0, utils_1.handleRequest)(fetch(this.client.getFullUrl(`/collection/${name}`), { | ||
headers: this.client.headers, | ||
@@ -108,2 +103,4 @@ })); | ||
* @throws {Error} If neither description nor metadata are provided. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the collection is not found. | ||
*/ | ||
@@ -120,3 +117,3 @@ updateCollection({ name, description, metadata, }) { | ||
}); | ||
yield (0, utils_1.handleRequest)(fetch(this.getFullUrl(`/collection/${collection.name}`), { | ||
yield (0, utils_1.handleRequest)(fetch(this.client.getFullUrl(`/collection/${collection.name}`), { | ||
method: "PATCH", | ||
@@ -133,6 +130,7 @@ headers: Object.assign(Object.assign({}, this.client.headers), { "Content-Type": "application/json" }), | ||
* DocumentCollection instances. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
listCollections() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield (0, utils_1.handleRequest)(fetch(this.getFullUrl("/collection"), { | ||
const response = yield (0, utils_1.handleRequest)(fetch(this.client.getFullUrl("/collection"), { | ||
headers: this.client.headers, | ||
@@ -162,2 +160,4 @@ })); | ||
* @throws {Error} If the collection name is not provided. | ||
* @throws {NotFoundError} If the collection is not found. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -169,3 +169,3 @@ deleteCollection(collectionName) { | ||
} | ||
yield (0, utils_1.handleRequest)(fetch(this.getFullUrl(`/collection/${collectionName}`), { | ||
yield (0, utils_1.handleRequest)(fetch(this.client.getFullUrl(`/collection/${collectionName}`), { | ||
method: "DELETE", | ||
@@ -172,0 +172,0 @@ headers: this.client.headers, |
@@ -26,2 +26,3 @@ import { DocumentCollectionModel, IDocument, IDocumentCollectionModel } from "./document_models"; | ||
* @throws {Error} If the collection name is not provided or no documents are provided. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -34,2 +35,4 @@ addDocuments(documents: IDocument[]): Promise<string[]>; | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -42,2 +45,4 @@ updateDocument({ uuid, documentId, metadata, }: IUpdateDocumentParams): Promise<void>; | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -50,2 +55,4 @@ deleteDocument(uuid: string): Promise<void>; | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -58,2 +65,4 @@ getDocument(uuid: string): Promise<IDocument>; | ||
* @throws {Error} If any of the documents do not match the expected format. | ||
* @throws {Error} If the collection name is not provided or no uuids are provided. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -69,2 +78,3 @@ getDocuments(uuids: string[]): Promise<IDocument[]>; | ||
* the search query does not have at least one of text, embedding, or metadata. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -77,2 +87,5 @@ searchReturnQueryVector(query: ISearchQuery, limit?: number): Promise<[IDocument[], Float32Array]>; | ||
* @returns {Promise<IDocument[]>} A promise that resolves to an array of documents. | ||
* @throws {Error} If the collection name is not provided or | ||
* the search query does not have at least one of text, embedding, or metadata. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -87,10 +100,5 @@ search(query: ISearchQuery, limit?: number): Promise<IDocument[]>; | ||
* has less than MIN_DOCS_TO_INDEX documents and force is not true. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
createIndex(force?: boolean): Promise<void>; | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint: string): string; | ||
} |
@@ -11,3 +11,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { docsToDocsWithFloatArray, docsWithFloatArrayToDocs, DocumentCollectionModel, isGetIDocument, } from "./document_models"; | ||
import { API_BASEURL, handleRequest, isFloat } from "./utils"; | ||
import { handleRequest, isFloat } from "./utils"; | ||
import { APIError } from "./errors"; | ||
@@ -49,2 +49,3 @@ const MIN_DOCS_TO_INDEX = 10000; | ||
* @throws {Error} If the collection name is not provided or no documents are provided. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -62,4 +63,4 @@ addDocuments(documents) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/document`); | ||
const body = JSON.stringify(docsWithFloatArrayToDocs(documents)); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/document`); | ||
const response = yield handleRequest(fetch(url, { | ||
@@ -78,2 +79,4 @@ method: "POST", | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -88,3 +91,3 @@ updateDocument({ uuid, documentId, metadata, }) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/document/${uuid}`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/document/${uuid}`); | ||
yield handleRequest(fetch(url, { | ||
@@ -106,2 +109,4 @@ method: "PATCH", | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -116,3 +121,3 @@ deleteDocument(uuid) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/document/uuid/${uuid}`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/document/uuid/${uuid}`); | ||
yield handleRequest(fetch(url, { | ||
@@ -129,2 +134,4 @@ method: "DELETE", | ||
* @throws {Error} If the collection name is not provided or the document does not have a uuid. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the request no document is found for the given uuid. | ||
*/ | ||
@@ -139,3 +146,3 @@ getDocument(uuid) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/document/${uuid}`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/document/${uuid}`); | ||
const response = yield handleRequest(fetch(url, { | ||
@@ -160,2 +167,4 @@ headers: this.client.headers, | ||
* @throws {Error} If any of the documents do not match the expected format. | ||
* @throws {Error} If the collection name is not provided or no uuids are provided. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -173,3 +182,3 @@ getDocuments(uuids) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/document/list/get`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/document/list/get`); | ||
const response = yield handleRequest(fetch(url, { | ||
@@ -199,2 +208,3 @@ method: "POST", | ||
* the search query does not have at least one of text, embedding, or metadata. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -215,3 +225,3 @@ searchReturnQueryVector(query, limit) { | ||
const limitParam = limit ? `?limit=${limit}` : ""; | ||
const url = this.getFullUrl(`/collection/${this.name}/search${limitParam}`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/search${limitParam}`); | ||
const response = yield handleRequest(fetch(url, { | ||
@@ -247,2 +257,5 @@ method: "POST", | ||
* @returns {Promise<IDocument[]>} A promise that resolves to an array of documents. | ||
* @throws {Error} If the collection name is not provided or | ||
* the search query does not have at least one of text, embedding, or metadata. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -262,2 +275,3 @@ search(query, limit) { | ||
* has less than MIN_DOCS_TO_INDEX documents and force is not true. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -275,3 +289,3 @@ createIndex(force) { | ||
} | ||
const url = this.getFullUrl(`/collection/${this.name}/index/create${forceParam}`); | ||
const url = this.client.getFullUrl(`/collection/${this.name}/index/create${forceParam}`); | ||
yield handleRequest(fetch(url, { | ||
@@ -283,10 +297,2 @@ method: "POST", | ||
} | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint) { | ||
return `${this.client.baseURL}${API_BASEURL}${endpoint}`; | ||
} | ||
} |
@@ -15,8 +15,2 @@ import { IAddCollectionParams, IUpdateCollectionParams, IZepClient } from "./interfaces"; | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint: string): string; | ||
/** | ||
* Adds a new collection to the Zep client. | ||
@@ -27,2 +21,3 @@ * @param {IAddCollectionParams} params - The parameters for the new collection. | ||
* @throws {Error} If embeddingDimensions is not a positive integer. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -36,2 +31,4 @@ addCollection({ name, embeddingDimensions, description, metadata, isAutoEmbedded, }: IAddCollectionParams): Promise<DocumentCollection>; | ||
* @throws {Error} If the collection name is not provided. | ||
* @throws {NotFoundError} If the collection is not found. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -45,2 +42,4 @@ getCollection(name: string): Promise<DocumentCollection>; | ||
* @throws {Error} If neither description nor metadata are provided. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the collection is not found. | ||
*/ | ||
@@ -52,2 +51,3 @@ updateCollection({ name, description, metadata, }: IUpdateCollectionParams): Promise<DocumentCollection>; | ||
* DocumentCollection instances. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -60,4 +60,6 @@ listCollections(): Promise<DocumentCollection[]>; | ||
* @throws {Error} If the collection name is not provided. | ||
* @throws {NotFoundError} If the collection is not found. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
deleteCollection(collectionName: string): Promise<void>; | ||
} |
@@ -11,3 +11,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { DocumentCollectionModel } from "./document_models"; | ||
import { API_BASEURL, handleRequest } from "./utils"; | ||
import { handleRequest } from "./utils"; | ||
import DocumentCollection from "./document_collection"; | ||
@@ -27,10 +27,2 @@ /** | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint) { | ||
return `${this.client.baseURL}${API_BASEURL}${endpoint}`; | ||
} | ||
/** | ||
* Adds a new collection to the Zep client. | ||
@@ -41,2 +33,3 @@ * @param {IAddCollectionParams} params - The parameters for the new collection. | ||
* @throws {Error} If embeddingDimensions is not a positive integer. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -55,3 +48,3 @@ addCollection({ name, embeddingDimensions, description, metadata, isAutoEmbedded = true, }) { | ||
}); | ||
yield handleRequest(fetch(this.getFullUrl(`/collection/${name}`), { | ||
yield handleRequest(fetch(this.client.getFullUrl(`/collection/${name}`), { | ||
method: "POST", | ||
@@ -70,2 +63,4 @@ headers: Object.assign(Object.assign({}, this.client.headers), { "Content-Type": "application/json" }), | ||
* @throws {Error} If the collection name is not provided. | ||
* @throws {NotFoundError} If the collection is not found. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -77,3 +72,3 @@ getCollection(name) { | ||
} | ||
const response = yield handleRequest(fetch(this.getFullUrl(`/collection/${name}`), { | ||
const response = yield handleRequest(fetch(this.client.getFullUrl(`/collection/${name}`), { | ||
headers: this.client.headers, | ||
@@ -104,2 +99,4 @@ })); | ||
* @throws {Error} If neither description nor metadata are provided. | ||
* @throws {APIError} If the request fails. | ||
* @throws {NotFoundError} If the collection is not found. | ||
*/ | ||
@@ -116,3 +113,3 @@ updateCollection({ name, description, metadata, }) { | ||
}); | ||
yield handleRequest(fetch(this.getFullUrl(`/collection/${collection.name}`), { | ||
yield handleRequest(fetch(this.client.getFullUrl(`/collection/${collection.name}`), { | ||
method: "PATCH", | ||
@@ -129,6 +126,7 @@ headers: Object.assign(Object.assign({}, this.client.headers), { "Content-Type": "application/json" }), | ||
* DocumentCollection instances. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
listCollections() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield handleRequest(fetch(this.getFullUrl("/collection"), { | ||
const response = yield handleRequest(fetch(this.client.getFullUrl("/collection"), { | ||
headers: this.client.headers, | ||
@@ -158,2 +156,4 @@ })); | ||
* @throws {Error} If the collection name is not provided. | ||
* @throws {NotFoundError} If the collection is not found. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -165,3 +165,3 @@ deleteCollection(collectionName) { | ||
} | ||
yield handleRequest(fetch(this.getFullUrl(`/collection/${collectionName}`), { | ||
yield handleRequest(fetch(this.client.getFullUrl(`/collection/${collectionName}`), { | ||
method: "DELETE", | ||
@@ -168,0 +168,0 @@ headers: this.client.headers, |
@@ -5,3 +5,5 @@ import MemoryManager from "./memory_manager"; | ||
import DocumentCollection from "./document_collection"; | ||
export { MemoryManager, ZepClient, DocumentManager, DocumentCollection }; | ||
import UserManager from "./user_manager"; | ||
export { MemoryManager, ZepClient, DocumentManager, DocumentCollection, UserManager, }; | ||
export { User, IUser, CreateUserRequest, UpdateUserRequest, ICreateUserRequest, IUpdateUserRequest, } from "./user_models"; | ||
export { Document, IDocument, IDocumentCollectionModel, DocumentCollectionModel, } from "./document_models"; | ||
@@ -8,0 +10,0 @@ export { Memory, Message, IMessage, Summary, ISummary, IMemory, MemorySearchPayload, IMemorySearchPayload, MemorySearchResult, IMemorySearchResult, Session, ISession, } from "./memory_models"; |
@@ -5,5 +5,7 @@ import MemoryManager from "./memory_manager"; | ||
import DocumentCollection from "./document_collection"; | ||
export { MemoryManager, ZepClient, DocumentManager, DocumentCollection }; | ||
import UserManager from "./user_manager"; | ||
export { MemoryManager, ZepClient, DocumentManager, DocumentCollection, UserManager, }; | ||
export { User, CreateUserRequest, UpdateUserRequest, } from "./user_models"; | ||
export { Document, DocumentCollectionModel, } from "./document_models"; | ||
export { Memory, Message, Summary, MemorySearchPayload, MemorySearchResult, Session, } from "./memory_models"; | ||
export { APIError, NotFoundError, AuthenticationError } from "./errors"; |
export interface IZepClient { | ||
baseURL: string; | ||
headers: any; | ||
getFullUrl(endpoint: string): string; | ||
} | ||
@@ -5,0 +6,0 @@ export interface IAddCollectionParams { |
@@ -7,8 +7,2 @@ import { Memory, MemorySearchPayload, MemorySearchResult, Session } from "./memory_models"; | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint: string): string; | ||
/** | ||
* Retrieves a session with the specified ID. | ||
@@ -19,16 +13,48 @@ * | ||
* @throws {Error} Will throw an error if the sessionId is not provided. | ||
* @throws {Error} Will throw an error if the fetch request fails. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
* @throws {NotFoundError} Will throw an error if the session is not found. | ||
*/ | ||
getSession(sessionId: string): Promise<Session>; | ||
/** | ||
* Adds or updates a session. | ||
* Adds a session. | ||
* | ||
* @param {Session} session - The Session object to add or update. | ||
* @returns {Promise<string>} A promise that resolves to the response text from the server. | ||
* @param {Session} session - The session to add. | ||
* @returns {Promise<Session>} The added session. | ||
* @throws {Error} Will throw an error if the session is not provided. | ||
* @throws {Error} Will throw an error if the session.session_id is not provided. | ||
* @throws {Error} Will throw an error if the fetch request fails. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
*/ | ||
addSession(session: Session): Promise<string>; | ||
addSession(session: Session): Promise<Session>; | ||
/** | ||
* Updates the specified session. | ||
* | ||
* @param {Session} session - The session data to update. | ||
* @returns {Promise<Session>} The updated session. | ||
* @throws {Error} Will throw an error if the session is not provided. | ||
* @throws {Error} Will throw an error if the session.session_id is not provided. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
* @throws {NotFoundError} Will throw an error if the session is not found. | ||
*/ | ||
updateSession(session: Session): Promise<Session>; | ||
/** | ||
* Asynchronously retrieve a list of paginated sessions. | ||
* | ||
* @param {number} [limit] - Limit the number of results returned. | ||
* @param {number} [cursor] - Cursor for pagination. | ||
* @returns {Promise<Array<Session>>} A list of all sessions paginated. | ||
* @throws {APIError} If the API response format is unexpected. | ||
*/ | ||
listSessions(limit?: number, cursor?: number): Promise<Array<Session>>; | ||
/** | ||
* Retrieve all sessions, handling pagination automatically. | ||
* Yields a generator of lists of sessions. | ||
* | ||
* @param {number} [chunkSize=100] - The number of sessions to retrieve at a time. | ||
* @returns {AsyncGenerator<Array<Session>, void, unknown>} | ||
* The next chunk of sessions from the server. | ||
* @throws {APIError} If the API response format is unexpected. | ||
* @throws {ConnectionError} If the connection to the server fails. | ||
*/ | ||
listSessionsChunked(chunkSize?: number): AsyncGenerator<Array<Session>, void, unknown>; | ||
/** | ||
* Retrieves memory for a specific session. | ||
@@ -38,2 +64,4 @@ * @param {string} sessionID - The ID of the session to retrieve memory for. | ||
* @returns {Promise<Array<Memory>>} - A promise that returns a Memory object. | ||
* @throws {APIError} - If the request fails. | ||
* @throws {NotFoundError} - If the session is not found. | ||
*/ | ||
@@ -46,2 +74,3 @@ getMemory(sessionID: string, lastn?: number): Promise<Memory | null>; | ||
* @returns {Promise<Memory>} A promise that resolves to the added memory. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -55,2 +84,4 @@ addMemory(sessionID: string, memory: Memory): Promise<string>; | ||
* been deleted. | ||
* @throws {APIError} - If the request fails. | ||
* @throws {NotFoundError} - If the session is not found. | ||
*/ | ||
@@ -66,4 +97,5 @@ deleteMemory(sessionID: string): Promise<string>; | ||
* results. | ||
* @throws {APIError} - If the request fails. | ||
*/ | ||
searchMemory(sessionID: string, searchPayload: MemorySearchPayload, limit?: number): Promise<Array<MemorySearchResult>>; | ||
} |
@@ -10,4 +10,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } | ||
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var g = generator.apply(thisArg, _arguments || []), i, q = []; | ||
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; | ||
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } | ||
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } | ||
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } | ||
function fulfill(value) { resume("next", value); } | ||
function reject(value) { resume("throw", value); } | ||
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } | ||
}; | ||
import { Memory, MemorySearchResult, Message, Session, } from "./memory_models"; | ||
import { API_BASEURL, handleRequest } from "./utils"; | ||
import { handleRequest } from "./utils"; | ||
export default class MemoryManager { | ||
@@ -18,10 +30,2 @@ constructor(client) { | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint) { | ||
return `${this.client.baseURL}${API_BASEURL}${endpoint}`; | ||
} | ||
/** | ||
* Retrieves a session with the specified ID. | ||
@@ -32,3 +36,4 @@ * | ||
* @throws {Error} Will throw an error if the sessionId is not provided. | ||
* @throws {Error} Will throw an error if the fetch request fails. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
* @throws {NotFoundError} Will throw an error if the session is not found. | ||
*/ | ||
@@ -40,3 +45,3 @@ getSession(sessionId) { | ||
} | ||
const response = yield handleRequest(fetch(this.getFullUrl(`/sessions/${sessionId}`), { | ||
const response = yield handleRequest(fetch(this.client.getFullUrl(`/sessions/${sessionId}`), { | ||
headers: this.client.headers, | ||
@@ -49,9 +54,9 @@ }), `No session found for session ${sessionId}`); | ||
/** | ||
* Adds or updates a session. | ||
* Adds a session. | ||
* | ||
* @param {Session} session - The Session object to add or update. | ||
* @returns {Promise<string>} A promise that resolves to the response text from the server. | ||
* @param {Session} session - The session to add. | ||
* @returns {Promise<Session>} The added session. | ||
* @throws {Error} Will throw an error if the session is not provided. | ||
* @throws {Error} Will throw an error if the session.session_id is not provided. | ||
* @throws {Error} Will throw an error if the fetch request fails. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
*/ | ||
@@ -66,3 +71,3 @@ addSession(session) { | ||
} | ||
const response = yield handleRequest(fetch(this.getFullUrl(`/sessions/${session.session_id}`), { | ||
const response = yield handleRequest(fetch(this.client.getFullUrl(`/sessions`), { | ||
method: "POST", | ||
@@ -72,6 +77,84 @@ headers: Object.assign(Object.assign({}, this.client.headers), { "Content-Type": "application/json" }), | ||
}), `Failed to add session ${session.session_id}`); | ||
return response.text(); | ||
const responseData = yield response.json(); | ||
return new Session(responseData); | ||
}); | ||
} | ||
/** | ||
* Updates the specified session. | ||
* | ||
* @param {Session} session - The session data to update. | ||
* @returns {Promise<Session>} The updated session. | ||
* @throws {Error} Will throw an error if the session is not provided. | ||
* @throws {Error} Will throw an error if the session.session_id is not provided. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
* @throws {NotFoundError} Will throw an error if the session is not found. | ||
*/ | ||
updateSession(session) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!session) { | ||
throw new Error("session must be provided"); | ||
} | ||
if (!session.session_id || session.session_id.trim() === "") { | ||
throw new Error("session.session_id must be provided"); | ||
} | ||
const response = yield handleRequest(fetch(this.client.getFullUrl(`/sessions/${session.session_id}`), { | ||
method: "PATCH", | ||
headers: Object.assign(Object.assign({}, this.client.headers), { "Content-Type": "application/json" }), | ||
body: JSON.stringify(session.toDict()), | ||
}), `Failed to update session ${session.session_id}`); | ||
const responseData = yield response.json(); | ||
return new Session(responseData); | ||
}); | ||
} | ||
/** | ||
* Asynchronously retrieve a list of paginated sessions. | ||
* | ||
* @param {number} [limit] - Limit the number of results returned. | ||
* @param {number} [cursor] - Cursor for pagination. | ||
* @returns {Promise<Array<Session>>} A list of all sessions paginated. | ||
* @throws {APIError} If the API response format is unexpected. | ||
*/ | ||
listSessions(limit, cursor) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const params = new URLSearchParams(); | ||
if (limit !== undefined) | ||
params.append("limit", limit.toString()); | ||
if (cursor !== undefined) | ||
params.append("cursor", cursor.toString()); | ||
const response = yield handleRequest(fetch(`${this.client.getFullUrl("/sessions")}?${params.toString()}`, { | ||
headers: this.client.headers, | ||
}), `Failed to get sessions`); | ||
const responseData = yield response.json(); | ||
return responseData.map((session) => new Session(session)); | ||
}); | ||
} | ||
/** | ||
* Retrieve all sessions, handling pagination automatically. | ||
* Yields a generator of lists of sessions. | ||
* | ||
* @param {number} [chunkSize=100] - The number of sessions to retrieve at a time. | ||
* @returns {AsyncGenerator<Array<Session>, void, unknown>} | ||
* The next chunk of sessions from the server. | ||
* @throws {APIError} If the API response format is unexpected. | ||
* @throws {ConnectionError} If the connection to the server fails. | ||
*/ | ||
listSessionsChunked(chunkSize = 100) { | ||
return __asyncGenerator(this, arguments, function* listSessionsChunked_1() { | ||
let cursor; | ||
while (true) { | ||
// eslint-disable-next-line no-await-in-loop | ||
const sessions = yield __await(this.listSessions(chunkSize, cursor)); | ||
if (sessions.length === 0) { | ||
// We've reached the last page | ||
break; | ||
} | ||
yield yield __await(sessions); | ||
if (cursor === undefined) { | ||
cursor = 0; | ||
} | ||
cursor += chunkSize; | ||
} | ||
}); | ||
} | ||
/** | ||
* Retrieves memory for a specific session. | ||
@@ -81,6 +164,8 @@ * @param {string} sessionID - The ID of the session to retrieve memory for. | ||
* @returns {Promise<Array<Memory>>} - A promise that returns a Memory object. | ||
* @throws {APIError} - If the request fails. | ||
* @throws {NotFoundError} - If the session is not found. | ||
*/ | ||
getMemory(sessionID, lastn) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = this.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const url = this.client.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const params = lastn !== undefined ? `?lastn=${lastn}` : ""; | ||
@@ -107,6 +192,7 @@ const response = yield handleRequest(fetch(`${url}${params}`, { | ||
* @returns {Promise<Memory>} A promise that resolves to the added memory. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
addMemory(sessionID, memory) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = this.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const url = this.client.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const response = yield handleRequest(fetch(url, { | ||
@@ -126,6 +212,8 @@ method: "POST", | ||
* been deleted. | ||
* @throws {APIError} - If the request fails. | ||
* @throws {NotFoundError} - If the session is not found. | ||
*/ | ||
deleteMemory(sessionID) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = this.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const url = this.client.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const response = yield handleRequest(fetch(url, { | ||
@@ -146,6 +234,7 @@ method: "DELETE", | ||
* results. | ||
* @throws {APIError} - If the request fails. | ||
*/ | ||
searchMemory(sessionID, searchPayload, limit) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = this.getFullUrl(`/sessions/${sessionID}/search`); | ||
const url = this.client.getFullUrl(`/sessions/${sessionID}/search`); | ||
const params = limit !== undefined ? `?limit=${limit}` : ""; | ||
@@ -152,0 +241,0 @@ const response = yield handleRequest(fetch(`${url}${params}`, { |
@@ -11,2 +11,3 @@ /** | ||
metadata: Record<string, any>; | ||
user_id?: string; | ||
} | ||
@@ -23,2 +24,3 @@ /** | ||
metadata: Record<string, any>; | ||
user_id?: string; | ||
/** | ||
@@ -36,3 +38,3 @@ * Constructs a new Session instance. | ||
/** | ||
* MessageData interface for providing input to create a Message instance. | ||
* IMessage interface for providing input to create a Message instance. | ||
*/ | ||
@@ -69,3 +71,3 @@ export interface IMessage { | ||
/** | ||
* SummaryData interface for providing input to create a Summary instance. | ||
* ISummary interface for providing input to create a Summary instance. | ||
*/ | ||
@@ -100,3 +102,3 @@ export interface ISummary { | ||
/** | ||
* MemoryData interface for providing input to create a Memory instance. | ||
* IMemory interface for providing input to create a Memory instance. | ||
*/ | ||
@@ -103,0 +105,0 @@ export interface IMemory { |
@@ -16,2 +16,3 @@ /** | ||
this.metadata = data.metadata; | ||
this.user_id = data.user_id; | ||
} | ||
@@ -30,2 +31,3 @@ /** | ||
metadata: this.metadata, | ||
user_id: this.user_id, | ||
}; | ||
@@ -32,0 +34,0 @@ } |
@@ -12,2 +12,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { docsToDocsWithFloatArray, } from "../document_models"; | ||
import { API_BASEPATH } from "../utils"; | ||
const API_URL = "http://localhost:8000"; | ||
@@ -36,2 +37,5 @@ const fetchMock = global.fetch; | ||
headers: {}, | ||
getFullUrl(endpoint) { | ||
return `${this.baseURL}${API_BASEPATH}${endpoint}`; | ||
}, | ||
}; | ||
@@ -38,0 +42,0 @@ describe("DocumentCollection", () => { |
@@ -10,2 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
var __asyncValues = (this && this.__asyncValues) || function (o) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var m = o[Symbol.asyncIterator], i; | ||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); | ||
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } | ||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } | ||
}; | ||
import { APIError, Memory, Message, NotFoundError, Session, Summary, ZepClient, } from "../"; | ||
@@ -20,3 +27,3 @@ const BASE_URL = "http://localhost:8000"; | ||
})); | ||
describe("ZepClient Session", () => { | ||
describe("getSession", () => { | ||
it("retrieves the correct session when sessionId is provided", () => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -36,4 +43,6 @@ const expectedSessionId = "test-session"; | ||
}); | ||
describe("ZepClient Session", () => { | ||
it("adds a session correctly when valid session data is provided", () => __awaiter(void 0, void 0, void 0, function* () { | ||
// Test Suite for addSession() | ||
describe("addSession", () => { | ||
// Test for adding a session | ||
it("should add a session correctly when valid session data is provided", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const expectedSessionId = "test-session"; | ||
@@ -45,8 +54,124 @@ const sessionData = { | ||
const session = new Session(sessionData); | ||
const expectedResponseText = "Session added successfully"; | ||
fetchMock.mockResponseOnce(expectedResponseText); | ||
const responseText = yield client.memory.addSession(session); | ||
expect(responseText).toEqual(expectedResponseText); | ||
const expectedResponseData = Object.assign(Object.assign({}, sessionData), { uuid: "uuid", created_at: "2022-01-01T00:00:00Z", updated_at: "2022-01-01T00:00:00Z" }); | ||
fetchMock.mockResponseOnce(JSON.stringify(expectedResponseData)); | ||
const addedSession = yield client.memory.addSession(session); | ||
expect(addedSession.toDict()).toEqual(expectedResponseData); | ||
})); | ||
}); | ||
// Test Suite for updateSession() | ||
describe("updateSession", () => { | ||
// Test for updating a session | ||
it("should update a session correctly when valid session data is provided", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const expectedSessionId = "test-session"; | ||
const sessionData = { | ||
session_id: expectedSessionId, | ||
metadata: { foo: "bar" }, | ||
}; | ||
const session = new Session(sessionData); | ||
const expectedResponseData = Object.assign(Object.assign({}, sessionData), { uuid: "uuid", created_at: "2022-01-01T00:00:00Z", updated_at: "2022-01-01T00:00:00Z" }); | ||
fetchMock.mockResponseOnce(JSON.stringify(expectedResponseData)); | ||
const updatedSession = yield client.memory.updateSession(session); | ||
expect(updatedSession.toDict()).toEqual(expectedResponseData); | ||
})); | ||
}); | ||
// Test Suite for listSessions() | ||
describe("listSessions", () => { | ||
// Test for retrieving sessions | ||
it("should retrieve sessions", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const responseData = [ | ||
{ | ||
uuid: "uuid1", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session1", | ||
metadata: {}, | ||
}, | ||
{ | ||
uuid: "uuid2", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session2", | ||
metadata: {}, | ||
}, | ||
]; | ||
fetchMock.mockResponseOnce(JSON.stringify(responseData)); | ||
const sessions = yield client.memory.listSessions(); | ||
expect(sessions).toEqual(responseData.map((session) => new Session(session))); | ||
})); | ||
// Test for retrieving sessions with limit | ||
it("should retrieve sessions with limit", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const responseData = [ | ||
{ | ||
uuid: "uuid1", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session1", | ||
metadata: {}, | ||
}, | ||
]; | ||
fetchMock.mockResponseOnce(JSON.stringify(responseData)); | ||
const sessions = yield client.memory.listSessions(1); | ||
expect(sessions).toEqual(responseData.map((session) => new Session(session))); | ||
})); | ||
}); | ||
// Test Suite for listSessionsChunked() | ||
describe("listSessionsChunked", () => { | ||
// Test for retrieving all sessions in chunks | ||
it("should retrieve all sessions in chunks", () => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a, e_1, _b, _c; | ||
const expectedSessionsData = [ | ||
[ | ||
{ | ||
uuid: "uuid1", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session1", | ||
metadata: {}, | ||
}, | ||
{ | ||
uuid: "uuid2", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session2", | ||
metadata: {}, | ||
}, | ||
], | ||
[ | ||
{ | ||
uuid: "uuid3", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session3", | ||
metadata: {}, | ||
}, | ||
{ | ||
uuid: "uuid4", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session4", | ||
metadata: {}, | ||
}, | ||
], | ||
]; | ||
fetchMock.mockResponses(JSON.stringify(expectedSessionsData[0]), JSON.stringify(expectedSessionsData[1]), JSON.stringify([]) // empty response to indicate end of list | ||
); | ||
const sessionsChunked = []; | ||
try { | ||
for (var _d = true, _e = __asyncValues(client.memory.listSessionsChunked(2)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { | ||
_c = _f.value; | ||
_d = false; | ||
const sessions = _c; | ||
sessionsChunked.push(sessions.map((session) => session.toDict())); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
expect(sessionsChunked).toEqual(expectedSessionsData); | ||
})); | ||
}); | ||
// Test Suite for getMemory() | ||
@@ -53,0 +178,0 @@ describe("getMemory", () => { |
@@ -12,4 +12,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { handleRequest, isFloat, isVersionGreaterOrEqual, MINIMUM_SERVER_VERSION, toDictFilterEmpty, warnDeprecation, } from "../utils"; | ||
import ZepClient from "../zep-client"; | ||
const fetchMock = global.fetch; | ||
describe("Utility functions", () => { | ||
let client; | ||
beforeEach(() => { | ||
client = new ZepClient("http://localhost:3000"); | ||
}); | ||
test("warnDeprecation", () => { | ||
@@ -24,5 +29,2 @@ console.warn = jest.fn(); | ||
}); | ||
test("returns true if version is greater than minimum", () => { | ||
expect(isVersionGreaterOrEqual("0.10.0")).toBe(true); | ||
}); | ||
test("returns true if version is equal to minimum", () => { | ||
@@ -84,2 +86,21 @@ expect(isVersionGreaterOrEqual(MINIMUM_SERVER_VERSION)).toBe(true); | ||
}); | ||
describe("getFullUrl", () => { | ||
const expectedUrl = "http://localhost:3000/api/v1/testEndpoint"; | ||
it("should correctly construct a URL without leading or trailing slashes", () => { | ||
const endpoint = "testEndpoint"; | ||
expect(client.getFullUrl(endpoint)).toBe(expectedUrl); | ||
}); | ||
it("should correctly construct a URL with leading slash in endpoint", () => { | ||
const endpoint = "/testEndpoint"; | ||
expect(client.getFullUrl(endpoint)).toBe(expectedUrl); | ||
}); | ||
it("should correctly construct a URL with trailing slash in endpoint", () => { | ||
const endpoint = "testEndpoint/"; | ||
expect(client.getFullUrl(endpoint)).toBe(expectedUrl); | ||
}); | ||
it("should correctly construct a URL with both leading and trailing slashes in endpoint", () => { | ||
const endpoint = "//testEndpoint/"; | ||
expect(client.getFullUrl(endpoint)).toBe(expectedUrl); | ||
}); | ||
}); | ||
}); |
@@ -1,5 +0,5 @@ | ||
declare const API_BASEURL = "/api/v1"; | ||
declare const API_BASEPATH = "/api/v1"; | ||
declare const SERVER_ERROR_MESSAGE = "Failed to connect to Zep server. Please check that:\n- the server is running \n- the API URL is correct\n- No other process is using the same port"; | ||
declare const MINIMUM_SERVER_VERSION = "0.9.0-beta.0"; | ||
declare const MIN_SERVER_WARNING_MESSAGE: string; | ||
declare const MINIMUM_SERVER_VERSION = "0.11.0"; | ||
declare const MIN_SERVER_WARNING_MESSAGE = "You are using an incompatible Zep server version. Please upgrade to {MINIMUM_SERVER_VERSION} or later."; | ||
declare function warnDeprecation(functionName: string): void; | ||
@@ -10,2 +10,9 @@ declare function isVersionGreaterOrEqual(version: string | null): boolean; | ||
declare function isFloat(n: any): boolean; | ||
export { warnDeprecation, handleRequest, SERVER_ERROR_MESSAGE, MIN_SERVER_WARNING_MESSAGE, MINIMUM_SERVER_VERSION, API_BASEURL, isVersionGreaterOrEqual, isFloat, }; | ||
/** | ||
* Joins the given paths into a single path. | ||
* | ||
* @param {...string[]} paths - The paths to join. | ||
* @returns {string} The joined path. | ||
*/ | ||
declare function joinPaths(...paths: string[]): string; | ||
export { warnDeprecation, handleRequest, SERVER_ERROR_MESSAGE, MIN_SERVER_WARNING_MESSAGE, MINIMUM_SERVER_VERSION, API_BASEPATH, isVersionGreaterOrEqual, isFloat, joinPaths, }; |
@@ -12,3 +12,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { APIError, AuthenticationError, NotFoundError } from "./errors"; | ||
const API_BASEURL = "/api/v1"; | ||
const API_BASEPATH = "/api/v1"; | ||
const SERVER_ERROR_MESSAGE = `Failed to connect to Zep server. Please check that: | ||
@@ -18,4 +18,4 @@ - the server is running | ||
- No other process is using the same port`; | ||
const MINIMUM_SERVER_VERSION = "0.9.0-beta.0"; | ||
const MIN_SERVER_WARNING_MESSAGE = `Zep server version less than ${MINIMUM_SERVER_VERSION} does not support the document vector store features of this client. Please update to ${MINIMUM_SERVER_VERSION} or newer.`; | ||
const MINIMUM_SERVER_VERSION = "0.11.0"; | ||
const MIN_SERVER_WARNING_MESSAGE = `You are using an incompatible Zep server version. Please upgrade to {MINIMUM_SERVER_VERSION} or later.`; | ||
function warnDeprecation(functionName) { | ||
@@ -87,2 +87,11 @@ console.warn(`Warning: ${functionName} is deprecated and will be removed in the next major release.`); | ||
} | ||
export { warnDeprecation, handleRequest, SERVER_ERROR_MESSAGE, MIN_SERVER_WARNING_MESSAGE, MINIMUM_SERVER_VERSION, API_BASEURL, isVersionGreaterOrEqual, isFloat, }; | ||
/** | ||
* Joins the given paths into a single path. | ||
* | ||
* @param {...string[]} paths - The paths to join. | ||
* @returns {string} The joined path. | ||
*/ | ||
function joinPaths(...paths) { | ||
return paths.map((path) => path.replace(/^\/+|\/+$/g, "")).join("/"); | ||
} | ||
export { warnDeprecation, handleRequest, SERVER_ERROR_MESSAGE, MIN_SERVER_WARNING_MESSAGE, MINIMUM_SERVER_VERSION, API_BASEPATH, isVersionGreaterOrEqual, isFloat, joinPaths, }; |
import { Memory, MemorySearchPayload, MemorySearchResult, Session } from "./memory_models"; | ||
import DocumentManager from "./document_manager"; | ||
import MemoryManager from "./memory_manager"; | ||
import UserManager from "./user_manager"; | ||
import { IZepClient } from "./interfaces"; | ||
/** | ||
* ZepClient is a Typescript class for interacting with the Zep. | ||
*/ | ||
export default class ZepClient { | ||
export default class ZepClient implements IZepClient { | ||
private static constructing; | ||
@@ -13,2 +15,3 @@ baseURL: string; | ||
document: DocumentManager; | ||
user: UserManager; | ||
/** | ||
@@ -53,3 +56,3 @@ * Constructs a new ZepClient instance. | ||
*/ | ||
addSession(session: Session): Promise<string>; | ||
addSession(session: Session): Promise<Session>; | ||
/** | ||
@@ -56,0 +59,0 @@ * Retrieves memory for a specific session. |
@@ -11,4 +11,5 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import DocumentManager from "./document_manager"; | ||
import { API_BASEURL, isVersionGreaterOrEqual, MIN_SERVER_WARNING_MESSAGE, SERVER_ERROR_MESSAGE, warnDeprecation, } from "./utils"; | ||
import { API_BASEPATH, isVersionGreaterOrEqual, MIN_SERVER_WARNING_MESSAGE, SERVER_ERROR_MESSAGE, warnDeprecation, joinPaths, } from "./utils"; | ||
import MemoryManager from "./memory_manager"; | ||
import UserManager from "./user_manager"; | ||
/** | ||
@@ -25,3 +26,3 @@ * ZepClient is a Typescript class for interacting with the Zep. | ||
if (!ZepClient.constructing) { | ||
warnDeprecation("Please use ZepClient.init(). Calling the ZepClient constructor directly"); | ||
warnDeprecation("Please use ZepClient.init(). Calling the ZepClient constructor directly is deprecated."); | ||
} | ||
@@ -36,2 +37,3 @@ this.baseURL = baseURL; | ||
this.document = new DocumentManager(this); | ||
this.user = new UserManager(this); | ||
} | ||
@@ -64,3 +66,5 @@ /** | ||
getFullUrl(endpoint) { | ||
return `${this.baseURL}${API_BASEURL}${endpoint}`; | ||
const url = new URL(this.baseURL); | ||
url.pathname = joinPaths(API_BASEPATH, endpoint); | ||
return url.toString(); | ||
} | ||
@@ -67,0 +71,0 @@ /** |
@@ -5,3 +5,5 @@ import MemoryManager from "./memory_manager"; | ||
import DocumentCollection from "./document_collection"; | ||
export { MemoryManager, ZepClient, DocumentManager, DocumentCollection }; | ||
import UserManager from "./user_manager"; | ||
export { MemoryManager, ZepClient, DocumentManager, DocumentCollection, UserManager, }; | ||
export { User, IUser, CreateUserRequest, UpdateUserRequest, ICreateUserRequest, IUpdateUserRequest, } from "./user_models"; | ||
export { Document, IDocument, IDocumentCollectionModel, DocumentCollectionModel, } from "./document_models"; | ||
@@ -8,0 +10,0 @@ export { Memory, Message, IMessage, Summary, ISummary, IMemory, MemorySearchPayload, IMemorySearchPayload, MemorySearchResult, IMemorySearchResult, Session, ISession, } from "./memory_models"; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AuthenticationError = exports.NotFoundError = exports.APIError = exports.Session = exports.MemorySearchResult = exports.MemorySearchPayload = exports.Summary = exports.Message = exports.Memory = exports.DocumentCollectionModel = exports.Document = exports.DocumentCollection = exports.DocumentManager = exports.ZepClient = exports.MemoryManager = void 0; | ||
exports.AuthenticationError = exports.NotFoundError = exports.APIError = exports.Session = exports.MemorySearchResult = exports.MemorySearchPayload = exports.Summary = exports.Message = exports.Memory = exports.DocumentCollectionModel = exports.Document = exports.UpdateUserRequest = exports.CreateUserRequest = exports.User = exports.UserManager = exports.DocumentCollection = exports.DocumentManager = exports.ZepClient = exports.MemoryManager = void 0; | ||
const memory_manager_1 = __importDefault(require("./memory_manager")); | ||
@@ -16,2 +16,8 @@ exports.MemoryManager = memory_manager_1.default; | ||
exports.DocumentCollection = document_collection_1.default; | ||
const user_manager_1 = __importDefault(require("./user_manager")); | ||
exports.UserManager = user_manager_1.default; | ||
var user_models_1 = require("./user_models"); | ||
Object.defineProperty(exports, "User", { enumerable: true, get: function () { return user_models_1.User; } }); | ||
Object.defineProperty(exports, "CreateUserRequest", { enumerable: true, get: function () { return user_models_1.CreateUserRequest; } }); | ||
Object.defineProperty(exports, "UpdateUserRequest", { enumerable: true, get: function () { return user_models_1.UpdateUserRequest; } }); | ||
var document_models_1 = require("./document_models"); | ||
@@ -18,0 +24,0 @@ Object.defineProperty(exports, "Document", { enumerable: true, get: function () { return document_models_1.Document; } }); |
export interface IZepClient { | ||
baseURL: string; | ||
headers: any; | ||
getFullUrl(endpoint: string): string; | ||
} | ||
@@ -5,0 +6,0 @@ export interface IAddCollectionParams { |
@@ -7,8 +7,2 @@ import { Memory, MemorySearchPayload, MemorySearchResult, Session } from "./memory_models"; | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint: string): string; | ||
/** | ||
* Retrieves a session with the specified ID. | ||
@@ -19,16 +13,48 @@ * | ||
* @throws {Error} Will throw an error if the sessionId is not provided. | ||
* @throws {Error} Will throw an error if the fetch request fails. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
* @throws {NotFoundError} Will throw an error if the session is not found. | ||
*/ | ||
getSession(sessionId: string): Promise<Session>; | ||
/** | ||
* Adds or updates a session. | ||
* Adds a session. | ||
* | ||
* @param {Session} session - The Session object to add or update. | ||
* @returns {Promise<string>} A promise that resolves to the response text from the server. | ||
* @param {Session} session - The session to add. | ||
* @returns {Promise<Session>} The added session. | ||
* @throws {Error} Will throw an error if the session is not provided. | ||
* @throws {Error} Will throw an error if the session.session_id is not provided. | ||
* @throws {Error} Will throw an error if the fetch request fails. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
*/ | ||
addSession(session: Session): Promise<string>; | ||
addSession(session: Session): Promise<Session>; | ||
/** | ||
* Updates the specified session. | ||
* | ||
* @param {Session} session - The session data to update. | ||
* @returns {Promise<Session>} The updated session. | ||
* @throws {Error} Will throw an error if the session is not provided. | ||
* @throws {Error} Will throw an error if the session.session_id is not provided. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
* @throws {NotFoundError} Will throw an error if the session is not found. | ||
*/ | ||
updateSession(session: Session): Promise<Session>; | ||
/** | ||
* Asynchronously retrieve a list of paginated sessions. | ||
* | ||
* @param {number} [limit] - Limit the number of results returned. | ||
* @param {number} [cursor] - Cursor for pagination. | ||
* @returns {Promise<Array<Session>>} A list of all sessions paginated. | ||
* @throws {APIError} If the API response format is unexpected. | ||
*/ | ||
listSessions(limit?: number, cursor?: number): Promise<Array<Session>>; | ||
/** | ||
* Retrieve all sessions, handling pagination automatically. | ||
* Yields a generator of lists of sessions. | ||
* | ||
* @param {number} [chunkSize=100] - The number of sessions to retrieve at a time. | ||
* @returns {AsyncGenerator<Array<Session>, void, unknown>} | ||
* The next chunk of sessions from the server. | ||
* @throws {APIError} If the API response format is unexpected. | ||
* @throws {ConnectionError} If the connection to the server fails. | ||
*/ | ||
listSessionsChunked(chunkSize?: number): AsyncGenerator<Array<Session>, void, unknown>; | ||
/** | ||
* Retrieves memory for a specific session. | ||
@@ -38,2 +64,4 @@ * @param {string} sessionID - The ID of the session to retrieve memory for. | ||
* @returns {Promise<Array<Memory>>} - A promise that returns a Memory object. | ||
* @throws {APIError} - If the request fails. | ||
* @throws {NotFoundError} - If the session is not found. | ||
*/ | ||
@@ -46,2 +74,3 @@ getMemory(sessionID: string, lastn?: number): Promise<Memory | null>; | ||
* @returns {Promise<Memory>} A promise that resolves to the added memory. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
@@ -55,2 +84,4 @@ addMemory(sessionID: string, memory: Memory): Promise<string>; | ||
* been deleted. | ||
* @throws {APIError} - If the request fails. | ||
* @throws {NotFoundError} - If the session is not found. | ||
*/ | ||
@@ -66,4 +97,5 @@ deleteMemory(sessionID: string): Promise<string>; | ||
* results. | ||
* @throws {APIError} - If the request fails. | ||
*/ | ||
searchMemory(sessionID: string, searchPayload: MemorySearchPayload, limit?: number): Promise<Array<MemorySearchResult>>; | ||
} |
@@ -11,2 +11,14 @@ "use strict"; | ||
}; | ||
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } | ||
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var g = generator.apply(thisArg, _arguments || []), i, q = []; | ||
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; | ||
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } | ||
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } | ||
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } | ||
function fulfill(value) { resume("next", value); } | ||
function reject(value) { resume("throw", value); } | ||
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -20,10 +32,2 @@ const memory_models_1 = require("./memory_models"); | ||
/** | ||
* Constructs the full URL for an API endpoint. | ||
* @param {string} endpoint - The endpoint of the API. | ||
* @returns {string} The full URL. | ||
*/ | ||
getFullUrl(endpoint) { | ||
return `${this.client.baseURL}${utils_1.API_BASEURL}${endpoint}`; | ||
} | ||
/** | ||
* Retrieves a session with the specified ID. | ||
@@ -34,3 +38,4 @@ * | ||
* @throws {Error} Will throw an error if the sessionId is not provided. | ||
* @throws {Error} Will throw an error if the fetch request fails. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
* @throws {NotFoundError} Will throw an error if the session is not found. | ||
*/ | ||
@@ -42,3 +47,3 @@ getSession(sessionId) { | ||
} | ||
const response = yield (0, utils_1.handleRequest)(fetch(this.getFullUrl(`/sessions/${sessionId}`), { | ||
const response = yield (0, utils_1.handleRequest)(fetch(this.client.getFullUrl(`/sessions/${sessionId}`), { | ||
headers: this.client.headers, | ||
@@ -51,9 +56,9 @@ }), `No session found for session ${sessionId}`); | ||
/** | ||
* Adds or updates a session. | ||
* Adds a session. | ||
* | ||
* @param {Session} session - The Session object to add or update. | ||
* @returns {Promise<string>} A promise that resolves to the response text from the server. | ||
* @param {Session} session - The session to add. | ||
* @returns {Promise<Session>} The added session. | ||
* @throws {Error} Will throw an error if the session is not provided. | ||
* @throws {Error} Will throw an error if the session.session_id is not provided. | ||
* @throws {Error} Will throw an error if the fetch request fails. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
*/ | ||
@@ -68,3 +73,3 @@ addSession(session) { | ||
} | ||
const response = yield (0, utils_1.handleRequest)(fetch(this.getFullUrl(`/sessions/${session.session_id}`), { | ||
const response = yield (0, utils_1.handleRequest)(fetch(this.client.getFullUrl(`/sessions`), { | ||
method: "POST", | ||
@@ -74,6 +79,84 @@ headers: Object.assign(Object.assign({}, this.client.headers), { "Content-Type": "application/json" }), | ||
}), `Failed to add session ${session.session_id}`); | ||
return response.text(); | ||
const responseData = yield response.json(); | ||
return new memory_models_1.Session(responseData); | ||
}); | ||
} | ||
/** | ||
* Updates the specified session. | ||
* | ||
* @param {Session} session - The session data to update. | ||
* @returns {Promise<Session>} The updated session. | ||
* @throws {Error} Will throw an error if the session is not provided. | ||
* @throws {Error} Will throw an error if the session.session_id is not provided. | ||
* @throws {APIError} Will throw an error if the fetch request fails. | ||
* @throws {NotFoundError} Will throw an error if the session is not found. | ||
*/ | ||
updateSession(session) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!session) { | ||
throw new Error("session must be provided"); | ||
} | ||
if (!session.session_id || session.session_id.trim() === "") { | ||
throw new Error("session.session_id must be provided"); | ||
} | ||
const response = yield (0, utils_1.handleRequest)(fetch(this.client.getFullUrl(`/sessions/${session.session_id}`), { | ||
method: "PATCH", | ||
headers: Object.assign(Object.assign({}, this.client.headers), { "Content-Type": "application/json" }), | ||
body: JSON.stringify(session.toDict()), | ||
}), `Failed to update session ${session.session_id}`); | ||
const responseData = yield response.json(); | ||
return new memory_models_1.Session(responseData); | ||
}); | ||
} | ||
/** | ||
* Asynchronously retrieve a list of paginated sessions. | ||
* | ||
* @param {number} [limit] - Limit the number of results returned. | ||
* @param {number} [cursor] - Cursor for pagination. | ||
* @returns {Promise<Array<Session>>} A list of all sessions paginated. | ||
* @throws {APIError} If the API response format is unexpected. | ||
*/ | ||
listSessions(limit, cursor) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const params = new URLSearchParams(); | ||
if (limit !== undefined) | ||
params.append("limit", limit.toString()); | ||
if (cursor !== undefined) | ||
params.append("cursor", cursor.toString()); | ||
const response = yield (0, utils_1.handleRequest)(fetch(`${this.client.getFullUrl("/sessions")}?${params.toString()}`, { | ||
headers: this.client.headers, | ||
}), `Failed to get sessions`); | ||
const responseData = yield response.json(); | ||
return responseData.map((session) => new memory_models_1.Session(session)); | ||
}); | ||
} | ||
/** | ||
* Retrieve all sessions, handling pagination automatically. | ||
* Yields a generator of lists of sessions. | ||
* | ||
* @param {number} [chunkSize=100] - The number of sessions to retrieve at a time. | ||
* @returns {AsyncGenerator<Array<Session>, void, unknown>} | ||
* The next chunk of sessions from the server. | ||
* @throws {APIError} If the API response format is unexpected. | ||
* @throws {ConnectionError} If the connection to the server fails. | ||
*/ | ||
listSessionsChunked(chunkSize = 100) { | ||
return __asyncGenerator(this, arguments, function* listSessionsChunked_1() { | ||
let cursor; | ||
while (true) { | ||
// eslint-disable-next-line no-await-in-loop | ||
const sessions = yield __await(this.listSessions(chunkSize, cursor)); | ||
if (sessions.length === 0) { | ||
// We've reached the last page | ||
break; | ||
} | ||
yield yield __await(sessions); | ||
if (cursor === undefined) { | ||
cursor = 0; | ||
} | ||
cursor += chunkSize; | ||
} | ||
}); | ||
} | ||
/** | ||
* Retrieves memory for a specific session. | ||
@@ -83,6 +166,8 @@ * @param {string} sessionID - The ID of the session to retrieve memory for. | ||
* @returns {Promise<Array<Memory>>} - A promise that returns a Memory object. | ||
* @throws {APIError} - If the request fails. | ||
* @throws {NotFoundError} - If the session is not found. | ||
*/ | ||
getMemory(sessionID, lastn) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = this.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const url = this.client.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const params = lastn !== undefined ? `?lastn=${lastn}` : ""; | ||
@@ -109,6 +194,7 @@ const response = yield (0, utils_1.handleRequest)(fetch(`${url}${params}`, { | ||
* @returns {Promise<Memory>} A promise that resolves to the added memory. | ||
* @throws {APIError} If the request fails. | ||
*/ | ||
addMemory(sessionID, memory) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = this.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const url = this.client.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const response = yield (0, utils_1.handleRequest)(fetch(url, { | ||
@@ -128,6 +214,8 @@ method: "POST", | ||
* been deleted. | ||
* @throws {APIError} - If the request fails. | ||
* @throws {NotFoundError} - If the session is not found. | ||
*/ | ||
deleteMemory(sessionID) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = this.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const url = this.client.getFullUrl(`/sessions/${sessionID}/memory`); | ||
const response = yield (0, utils_1.handleRequest)(fetch(url, { | ||
@@ -148,6 +236,7 @@ method: "DELETE", | ||
* results. | ||
* @throws {APIError} - If the request fails. | ||
*/ | ||
searchMemory(sessionID, searchPayload, limit) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = this.getFullUrl(`/sessions/${sessionID}/search`); | ||
const url = this.client.getFullUrl(`/sessions/${sessionID}/search`); | ||
const params = limit !== undefined ? `?limit=${limit}` : ""; | ||
@@ -154,0 +243,0 @@ const response = yield (0, utils_1.handleRequest)(fetch(`${url}${params}`, { |
@@ -11,2 +11,3 @@ /** | ||
metadata: Record<string, any>; | ||
user_id?: string; | ||
} | ||
@@ -23,2 +24,3 @@ /** | ||
metadata: Record<string, any>; | ||
user_id?: string; | ||
/** | ||
@@ -36,3 +38,3 @@ * Constructs a new Session instance. | ||
/** | ||
* MessageData interface for providing input to create a Message instance. | ||
* IMessage interface for providing input to create a Message instance. | ||
*/ | ||
@@ -69,3 +71,3 @@ export interface IMessage { | ||
/** | ||
* SummaryData interface for providing input to create a Summary instance. | ||
* ISummary interface for providing input to create a Summary instance. | ||
*/ | ||
@@ -100,3 +102,3 @@ export interface ISummary { | ||
/** | ||
* MemoryData interface for providing input to create a Memory instance. | ||
* IMemory interface for providing input to create a Memory instance. | ||
*/ | ||
@@ -103,0 +105,0 @@ export interface IMemory { |
@@ -19,2 +19,3 @@ "use strict"; | ||
this.metadata = data.metadata; | ||
this.user_id = data.user_id; | ||
} | ||
@@ -33,2 +34,3 @@ /** | ||
metadata: this.metadata, | ||
user_id: this.user_id, | ||
}; | ||
@@ -35,0 +37,0 @@ } |
@@ -17,2 +17,3 @@ "use strict"; | ||
const document_models_1 = require("../document_models"); | ||
const utils_1 = require("../utils"); | ||
const API_URL = "http://localhost:8000"; | ||
@@ -41,2 +42,5 @@ const fetchMock = global.fetch; | ||
headers: {}, | ||
getFullUrl(endpoint) { | ||
return `${this.baseURL}${utils_1.API_BASEPATH}${endpoint}`; | ||
}, | ||
}; | ||
@@ -43,0 +47,0 @@ describe("DocumentCollection", () => { |
@@ -11,2 +11,9 @@ "use strict"; | ||
}; | ||
var __asyncValues = (this && this.__asyncValues) || function (o) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var m = o[Symbol.asyncIterator], i; | ||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); | ||
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } | ||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -22,3 +29,3 @@ const __1 = require("../"); | ||
})); | ||
describe("ZepClient Session", () => { | ||
describe("getSession", () => { | ||
it("retrieves the correct session when sessionId is provided", () => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -38,4 +45,6 @@ const expectedSessionId = "test-session"; | ||
}); | ||
describe("ZepClient Session", () => { | ||
it("adds a session correctly when valid session data is provided", () => __awaiter(void 0, void 0, void 0, function* () { | ||
// Test Suite for addSession() | ||
describe("addSession", () => { | ||
// Test for adding a session | ||
it("should add a session correctly when valid session data is provided", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const expectedSessionId = "test-session"; | ||
@@ -47,8 +56,124 @@ const sessionData = { | ||
const session = new __1.Session(sessionData); | ||
const expectedResponseText = "Session added successfully"; | ||
fetchMock.mockResponseOnce(expectedResponseText); | ||
const responseText = yield client.memory.addSession(session); | ||
expect(responseText).toEqual(expectedResponseText); | ||
const expectedResponseData = Object.assign(Object.assign({}, sessionData), { uuid: "uuid", created_at: "2022-01-01T00:00:00Z", updated_at: "2022-01-01T00:00:00Z" }); | ||
fetchMock.mockResponseOnce(JSON.stringify(expectedResponseData)); | ||
const addedSession = yield client.memory.addSession(session); | ||
expect(addedSession.toDict()).toEqual(expectedResponseData); | ||
})); | ||
}); | ||
// Test Suite for updateSession() | ||
describe("updateSession", () => { | ||
// Test for updating a session | ||
it("should update a session correctly when valid session data is provided", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const expectedSessionId = "test-session"; | ||
const sessionData = { | ||
session_id: expectedSessionId, | ||
metadata: { foo: "bar" }, | ||
}; | ||
const session = new __1.Session(sessionData); | ||
const expectedResponseData = Object.assign(Object.assign({}, sessionData), { uuid: "uuid", created_at: "2022-01-01T00:00:00Z", updated_at: "2022-01-01T00:00:00Z" }); | ||
fetchMock.mockResponseOnce(JSON.stringify(expectedResponseData)); | ||
const updatedSession = yield client.memory.updateSession(session); | ||
expect(updatedSession.toDict()).toEqual(expectedResponseData); | ||
})); | ||
}); | ||
// Test Suite for listSessions() | ||
describe("listSessions", () => { | ||
// Test for retrieving sessions | ||
it("should retrieve sessions", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const responseData = [ | ||
{ | ||
uuid: "uuid1", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session1", | ||
metadata: {}, | ||
}, | ||
{ | ||
uuid: "uuid2", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session2", | ||
metadata: {}, | ||
}, | ||
]; | ||
fetchMock.mockResponseOnce(JSON.stringify(responseData)); | ||
const sessions = yield client.memory.listSessions(); | ||
expect(sessions).toEqual(responseData.map((session) => new __1.Session(session))); | ||
})); | ||
// Test for retrieving sessions with limit | ||
it("should retrieve sessions with limit", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const responseData = [ | ||
{ | ||
uuid: "uuid1", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session1", | ||
metadata: {}, | ||
}, | ||
]; | ||
fetchMock.mockResponseOnce(JSON.stringify(responseData)); | ||
const sessions = yield client.memory.listSessions(1); | ||
expect(sessions).toEqual(responseData.map((session) => new __1.Session(session))); | ||
})); | ||
}); | ||
// Test Suite for listSessionsChunked() | ||
describe("listSessionsChunked", () => { | ||
// Test for retrieving all sessions in chunks | ||
it("should retrieve all sessions in chunks", () => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a, e_1, _b, _c; | ||
const expectedSessionsData = [ | ||
[ | ||
{ | ||
uuid: "uuid1", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session1", | ||
metadata: {}, | ||
}, | ||
{ | ||
uuid: "uuid2", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session2", | ||
metadata: {}, | ||
}, | ||
], | ||
[ | ||
{ | ||
uuid: "uuid3", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session3", | ||
metadata: {}, | ||
}, | ||
{ | ||
uuid: "uuid4", | ||
created_at: "2022-01-01T00:00:00Z", | ||
updated_at: "2022-01-01T00:00:00Z", | ||
session_id: "session4", | ||
metadata: {}, | ||
}, | ||
], | ||
]; | ||
fetchMock.mockResponses(JSON.stringify(expectedSessionsData[0]), JSON.stringify(expectedSessionsData[1]), JSON.stringify([]) // empty response to indicate end of list | ||
); | ||
const sessionsChunked = []; | ||
try { | ||
for (var _d = true, _e = __asyncValues(client.memory.listSessionsChunked(2)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { | ||
_c = _f.value; | ||
_d = false; | ||
const sessions = _c; | ||
sessionsChunked.push(sessions.map((session) => session.toDict())); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
expect(sessionsChunked).toEqual(expectedSessionsData); | ||
})); | ||
}); | ||
// Test Suite for getMemory() | ||
@@ -55,0 +180,0 @@ describe("getMemory", () => { |
@@ -11,7 +11,15 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const errors_1 = require("../errors"); | ||
const utils_1 = require("../utils"); | ||
const zep_client_1 = __importDefault(require("../zep-client")); | ||
const fetchMock = global.fetch; | ||
describe("Utility functions", () => { | ||
let client; | ||
beforeEach(() => { | ||
client = new zep_client_1.default("http://localhost:3000"); | ||
}); | ||
test("warnDeprecation", () => { | ||
@@ -26,5 +34,2 @@ console.warn = jest.fn(); | ||
}); | ||
test("returns true if version is greater than minimum", () => { | ||
expect((0, utils_1.isVersionGreaterOrEqual)("0.10.0")).toBe(true); | ||
}); | ||
test("returns true if version is equal to minimum", () => { | ||
@@ -86,2 +91,21 @@ expect((0, utils_1.isVersionGreaterOrEqual)(utils_1.MINIMUM_SERVER_VERSION)).toBe(true); | ||
}); | ||
describe("getFullUrl", () => { | ||
const expectedUrl = "http://localhost:3000/api/v1/testEndpoint"; | ||
it("should correctly construct a URL without leading or trailing slashes", () => { | ||
const endpoint = "testEndpoint"; | ||
expect(client.getFullUrl(endpoint)).toBe(expectedUrl); | ||
}); | ||
it("should correctly construct a URL with leading slash in endpoint", () => { | ||
const endpoint = "/testEndpoint"; | ||
expect(client.getFullUrl(endpoint)).toBe(expectedUrl); | ||
}); | ||
it("should correctly construct a URL with trailing slash in endpoint", () => { | ||
const endpoint = "testEndpoint/"; | ||
expect(client.getFullUrl(endpoint)).toBe(expectedUrl); | ||
}); | ||
it("should correctly construct a URL with both leading and trailing slashes in endpoint", () => { | ||
const endpoint = "//testEndpoint/"; | ||
expect(client.getFullUrl(endpoint)).toBe(expectedUrl); | ||
}); | ||
}); | ||
}); |
@@ -1,5 +0,5 @@ | ||
declare const API_BASEURL = "/api/v1"; | ||
declare const API_BASEPATH = "/api/v1"; | ||
declare const SERVER_ERROR_MESSAGE = "Failed to connect to Zep server. Please check that:\n- the server is running \n- the API URL is correct\n- No other process is using the same port"; | ||
declare const MINIMUM_SERVER_VERSION = "0.9.0-beta.0"; | ||
declare const MIN_SERVER_WARNING_MESSAGE: string; | ||
declare const MINIMUM_SERVER_VERSION = "0.11.0"; | ||
declare const MIN_SERVER_WARNING_MESSAGE = "You are using an incompatible Zep server version. Please upgrade to {MINIMUM_SERVER_VERSION} or later."; | ||
declare function warnDeprecation(functionName: string): void; | ||
@@ -10,2 +10,9 @@ declare function isVersionGreaterOrEqual(version: string | null): boolean; | ||
declare function isFloat(n: any): boolean; | ||
export { warnDeprecation, handleRequest, SERVER_ERROR_MESSAGE, MIN_SERVER_WARNING_MESSAGE, MINIMUM_SERVER_VERSION, API_BASEURL, isVersionGreaterOrEqual, isFloat, }; | ||
/** | ||
* Joins the given paths into a single path. | ||
* | ||
* @param {...string[]} paths - The paths to join. | ||
* @returns {string} The joined path. | ||
*/ | ||
declare function joinPaths(...paths: string[]): string; | ||
export { warnDeprecation, handleRequest, SERVER_ERROR_MESSAGE, MIN_SERVER_WARNING_MESSAGE, MINIMUM_SERVER_VERSION, API_BASEPATH, isVersionGreaterOrEqual, isFloat, joinPaths, }; |
@@ -15,7 +15,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isFloat = exports.isVersionGreaterOrEqual = exports.API_BASEURL = exports.MINIMUM_SERVER_VERSION = exports.MIN_SERVER_WARNING_MESSAGE = exports.SERVER_ERROR_MESSAGE = exports.handleRequest = exports.warnDeprecation = exports.toDictFilterEmpty = void 0; | ||
exports.joinPaths = exports.isFloat = exports.isVersionGreaterOrEqual = exports.API_BASEPATH = exports.MINIMUM_SERVER_VERSION = exports.MIN_SERVER_WARNING_MESSAGE = exports.SERVER_ERROR_MESSAGE = exports.handleRequest = exports.warnDeprecation = exports.toDictFilterEmpty = void 0; | ||
const semver_1 = __importDefault(require("semver")); | ||
const errors_1 = require("./errors"); | ||
const API_BASEURL = "/api/v1"; | ||
exports.API_BASEURL = API_BASEURL; | ||
const API_BASEPATH = "/api/v1"; | ||
exports.API_BASEPATH = API_BASEPATH; | ||
const SERVER_ERROR_MESSAGE = `Failed to connect to Zep server. Please check that: | ||
@@ -26,5 +26,5 @@ - the server is running | ||
exports.SERVER_ERROR_MESSAGE = SERVER_ERROR_MESSAGE; | ||
const MINIMUM_SERVER_VERSION = "0.9.0-beta.0"; | ||
const MINIMUM_SERVER_VERSION = "0.11.0"; | ||
exports.MINIMUM_SERVER_VERSION = MINIMUM_SERVER_VERSION; | ||
const MIN_SERVER_WARNING_MESSAGE = `Zep server version less than ${MINIMUM_SERVER_VERSION} does not support the document vector store features of this client. Please update to ${MINIMUM_SERVER_VERSION} or newer.`; | ||
const MIN_SERVER_WARNING_MESSAGE = `You are using an incompatible Zep server version. Please upgrade to {MINIMUM_SERVER_VERSION} or later.`; | ||
exports.MIN_SERVER_WARNING_MESSAGE = MIN_SERVER_WARNING_MESSAGE; | ||
@@ -102,1 +102,11 @@ function warnDeprecation(functionName) { | ||
exports.isFloat = isFloat; | ||
/** | ||
* Joins the given paths into a single path. | ||
* | ||
* @param {...string[]} paths - The paths to join. | ||
* @returns {string} The joined path. | ||
*/ | ||
function joinPaths(...paths) { | ||
return paths.map((path) => path.replace(/^\/+|\/+$/g, "")).join("/"); | ||
} | ||
exports.joinPaths = joinPaths; |
import { Memory, MemorySearchPayload, MemorySearchResult, Session } from "./memory_models"; | ||
import DocumentManager from "./document_manager"; | ||
import MemoryManager from "./memory_manager"; | ||
import UserManager from "./user_manager"; | ||
import { IZepClient } from "./interfaces"; | ||
/** | ||
* ZepClient is a Typescript class for interacting with the Zep. | ||
*/ | ||
export default class ZepClient { | ||
export default class ZepClient implements IZepClient { | ||
private static constructing; | ||
@@ -13,2 +15,3 @@ baseURL: string; | ||
document: DocumentManager; | ||
user: UserManager; | ||
/** | ||
@@ -53,3 +56,3 @@ * Constructs a new ZepClient instance. | ||
*/ | ||
addSession(session: Session): Promise<string>; | ||
addSession(session: Session): Promise<Session>; | ||
/** | ||
@@ -56,0 +59,0 @@ * Retrieves memory for a specific session. |
@@ -18,2 +18,3 @@ "use strict"; | ||
const memory_manager_1 = __importDefault(require("./memory_manager")); | ||
const user_manager_1 = __importDefault(require("./user_manager")); | ||
/** | ||
@@ -30,3 +31,3 @@ * ZepClient is a Typescript class for interacting with the Zep. | ||
if (!ZepClient.constructing) { | ||
(0, utils_1.warnDeprecation)("Please use ZepClient.init(). Calling the ZepClient constructor directly"); | ||
(0, utils_1.warnDeprecation)("Please use ZepClient.init(). Calling the ZepClient constructor directly is deprecated."); | ||
} | ||
@@ -41,2 +42,3 @@ this.baseURL = baseURL; | ||
this.document = new document_manager_1.default(this); | ||
this.user = new user_manager_1.default(this); | ||
} | ||
@@ -69,3 +71,5 @@ /** | ||
getFullUrl(endpoint) { | ||
return `${this.baseURL}${utils_1.API_BASEURL}${endpoint}`; | ||
const url = new URL(this.baseURL); | ||
url.pathname = (0, utils_1.joinPaths)(utils_1.API_BASEPATH, endpoint); | ||
return url.toString(); | ||
} | ||
@@ -72,0 +76,0 @@ /** |
{ | ||
"show-config": "tsc --showConfig", | ||
"name": "@getzep/zep-js", | ||
"version": "0.6.3", | ||
"version": "0.7.0", | ||
"description": "Zep: A long-term memory store for conversational AI applications", | ||
@@ -19,2 +19,3 @@ "private": false, | ||
"clean": "rm -rf dist/*", | ||
"format": "eslint --fix 'src/**/*.{js,ts}' && prettier --write 'src/**/*.{js,ts,json}'", | ||
"prepublishOnly": "npm run clean && npm run build && npm run test && npm run docbuild", | ||
@@ -21,0 +22,0 @@ "prepare": "npm run build", |
321540
75
6712
60