@nexo-labs/payload-typesense 1.6.20 → 1.7.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/dist/index.mjs CHANGED
@@ -856,6 +856,53 @@ async function closeSession(payload, userId, conversationId, config = {}) {
856
856
  last_activity: session.last_activity
857
857
  };
858
858
  }
859
+ /**
860
+ * Get user chat sessions
861
+ *
862
+ * @param payload - Payload CMS instance
863
+ * @param userId - User ID
864
+ * @param config - Session configuration
865
+ * @returns Promise with list of sessions
866
+ */
867
+ async function getUserSessions(payload, userId, config = {}) {
868
+ const collectionName = config.collectionName;
869
+ if (!collectionName) throw new Error("Collection name is required to get user sessions");
870
+ return (await payload.find({
871
+ collection: collectionName,
872
+ where: { user: { equals: userId } },
873
+ sort: "-last_activity",
874
+ limit: 50
875
+ })).docs.map((doc) => ({
876
+ ...doc,
877
+ title: doc.title
878
+ }));
879
+ }
880
+ /**
881
+ * Rename a chat session
882
+ *
883
+ * @param payload - Payload CMS instance
884
+ * @param userId - User ID
885
+ * @param conversationId - Conversation ID
886
+ * @param newTitle - New title for the session
887
+ * @param config - Session configuration
888
+ * @returns Promise with updated session or null
889
+ */
890
+ async function renameSession(payload, userId, conversationId, newTitle, config = {}) {
891
+ const collectionName = config.collectionName;
892
+ if (!collectionName) throw new Error("Collection name is required to rename a session");
893
+ if (!(await payload.find({
894
+ collection: collectionName,
895
+ where: { and: [{ conversation_id: { equals: conversationId } }, { user: { equals: userId } }] },
896
+ limit: 1
897
+ })).docs.length) return null;
898
+ const updated = await payload.update({
899
+ collection: collectionName,
900
+ where: { conversation_id: { equals: conversationId } },
901
+ data: { title: newTitle }
902
+ });
903
+ if (!updated.docs.length) return null;
904
+ return updated.docs[0];
905
+ }
859
906
 
860
907
  //#endregion
861
908
  //#region src/features/rag/utils/sse-utils.ts
@@ -1501,25 +1548,25 @@ function createSessionDELETEHandler(config) {
1501
1548
  const conversationId = searchParams.get("conversationId");
1502
1549
  if (!conversationId) return jsonResponse({ error: "Se requiere un conversationId válido." }, { status: 400 });
1503
1550
  const payload = await config.getPayload();
1504
- logger$1.info("Closing chat session", {
1551
+ logger$1.info("Deleting chat session", {
1505
1552
  conversationId,
1506
1553
  userId
1507
1554
  });
1508
- const session = await closeSession(payload, userId, conversationId, config.sessionConfig);
1509
- if (!session) return jsonResponse({ error: "Sesión de chat no encontrada o no tienes permisos." }, { status: 404 });
1510
- logger$1.info("Chat session closed successfully", {
1511
- conversationId,
1512
- totalTokens: session.total_tokens,
1513
- totalCost: session.total_cost
1555
+ const collectionName = config.sessionConfig?.collectionName;
1556
+ if (!collectionName) throw new Error("Collection name is required to delete a session");
1557
+ const result = await payload.delete({
1558
+ collection: collectionName,
1559
+ where: { and: [{ conversation_id: { equals: conversationId } }, { user: { equals: userId } }] }
1514
1560
  });
1561
+ if (!result.docs || result.docs.length === 0) return jsonResponse({ error: "Sesión de chat no encontrada o no tienes permisos." }, { status: 404 });
1562
+ result.docs[0];
1563
+ logger$1.info("Chat session deleted successfully", { conversationId });
1515
1564
  return jsonResponse({
1516
1565
  success: true,
1517
- message: "Sesión cerrada correctamente",
1566
+ message: "Sesión eliminada correctamente",
1518
1567
  session: {
1519
1568
  conversation_id: conversationId,
1520
- status: "closed",
1521
- total_tokens: session.total_tokens,
1522
- total_cost: session.total_cost
1569
+ status: "deleted"
1523
1570
  }
1524
1571
  });
1525
1572
  } catch (error) {
@@ -1534,6 +1581,67 @@ function createSessionDELETEHandler(config) {
1534
1581
  }
1535
1582
  };
1536
1583
  }
1584
+ /**
1585
+ * Create a parameterizable PATCH handler for session endpoint
1586
+ *
1587
+ * PATCH /api/chat/session?conversationId=xxx
1588
+ * Body: { title: "New Title" }
1589
+ * Rename a chat session
1590
+ */
1591
+ function createSessionPATCHHandler(config) {
1592
+ return async function PATCH(request) {
1593
+ try {
1594
+ if (!await config.checkPermissions(request)) return jsonResponse({ error: "No tienes permisos para acceder a esta sesión." }, { status: 403 });
1595
+ const userId = request.user?.id;
1596
+ if (!request.url || !userId) return jsonResponse({ error: "URL not found" }, { status: 400 });
1597
+ const { searchParams } = new URL(request.url);
1598
+ const conversationId = searchParams.get("conversationId");
1599
+ if (!conversationId) return jsonResponse({ error: "Se requiere un conversationId válido." }, { status: 400 });
1600
+ const { title } = await request.json?.();
1601
+ if (!title || typeof title !== "string") return jsonResponse({ error: "Se requiere un título válido." }, { status: 400 });
1602
+ const session = await renameSession(await config.getPayload(), userId, conversationId, title, config.sessionConfig);
1603
+ if (!session) return jsonResponse({ error: "Sesión de chat no encontrada o no tienes permisos." }, { status: 404 });
1604
+ return jsonResponse({
1605
+ success: true,
1606
+ message: "Sesión renombrada correctamente",
1607
+ session
1608
+ });
1609
+ } catch (error) {
1610
+ logger$1.error("Error renaming chat session", error, {
1611
+ conversationId: request.url ? new URL(request.url).searchParams.get("conversationId") : void 0,
1612
+ userId: request.user?.id
1613
+ });
1614
+ return jsonResponse({
1615
+ error: "Error al renombrar la sesión.",
1616
+ details: error instanceof Error ? error.message : "Error desconocido"
1617
+ }, { status: 500 });
1618
+ }
1619
+ };
1620
+ }
1621
+
1622
+ //#endregion
1623
+ //#region src/features/rag/endpoints/chat/sessions/route.ts
1624
+ /**
1625
+ * Create a parameterizable GET handler for sessions list endpoint
1626
+ *
1627
+ * GET /api/chat/sessions
1628
+ */
1629
+ function createSessionsListGETHandler(config) {
1630
+ return async function GET(request) {
1631
+ try {
1632
+ if (!await config.checkPermissions(request)) return jsonResponse({ error: "No tienes permisos para acceder." }, { status: 403 });
1633
+ const userId = request.user?.id;
1634
+ if (!userId) return jsonResponse({ error: "Usuario no autenticado" }, { status: 401 });
1635
+ return jsonResponse({ sessions: await getUserSessions(await config.getPayload(), userId, config.sessionConfig) });
1636
+ } catch (error) {
1637
+ logger$1.error("Error retrieving chat sessions list", error, { userId: request.user?.id });
1638
+ return jsonResponse({
1639
+ error: "Error al recuperar el historial de chat.",
1640
+ details: error instanceof Error ? error.message : "Error desconocido"
1641
+ }, { status: 500 });
1642
+ }
1643
+ };
1644
+ }
1537
1645
 
1538
1646
  //#endregion
1539
1647
  //#region src/features/rag/endpoints/chunks/[id]/route.ts
@@ -1640,7 +1748,8 @@ function createRAGPayloadHandlers(config) {
1640
1748
  method: "get",
1641
1749
  handler: createSessionGETHandler({
1642
1750
  getPayload: callbacks.getPayload,
1643
- checkPermissions: callbacks.checkPermissions
1751
+ checkPermissions: callbacks.checkPermissions,
1752
+ sessionConfig: { collectionName: config.collectionName }
1644
1753
  })
1645
1754
  });
1646
1755
  endpoints.push({
@@ -1648,7 +1757,26 @@ function createRAGPayloadHandlers(config) {
1648
1757
  method: "delete",
1649
1758
  handler: createSessionDELETEHandler({
1650
1759
  getPayload: callbacks.getPayload,
1651
- checkPermissions: callbacks.checkPermissions
1760
+ checkPermissions: callbacks.checkPermissions,
1761
+ sessionConfig: { collectionName: config.collectionName }
1762
+ })
1763
+ });
1764
+ endpoints.push({
1765
+ path: "/chat/session",
1766
+ method: "patch",
1767
+ handler: createSessionPATCHHandler({
1768
+ getPayload: callbacks.getPayload,
1769
+ checkPermissions: callbacks.checkPermissions,
1770
+ sessionConfig: { collectionName: config.collectionName }
1771
+ })
1772
+ });
1773
+ endpoints.push({
1774
+ path: "/chat/sessions",
1775
+ method: "get",
1776
+ handler: createSessionsListGETHandler({
1777
+ getPayload: callbacks.getPayload,
1778
+ checkPermissions: callbacks.checkPermissions,
1779
+ sessionConfig: { collectionName: config.collectionName }
1652
1780
  })
1653
1781
  });
1654
1782
  endpoints.push({