hazo_chat 2.0.12 → 2.0.14

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.
Files changed (154) hide show
  1. package/README.md +63 -2
  2. package/dist/api/index.d.ts +26 -0
  3. package/dist/api/index.d.ts.map +1 -0
  4. package/dist/api/index.js +25 -0
  5. package/dist/api/index.js.map +1 -0
  6. package/dist/api/messages.d.ts +34 -0
  7. package/dist/api/messages.d.ts.map +1 -0
  8. package/dist/api/messages.js +214 -0
  9. package/dist/api/messages.js.map +1 -0
  10. package/dist/api/types.d.ts +69 -0
  11. package/dist/api/types.d.ts.map +1 -0
  12. package/dist/api/types.js +8 -0
  13. package/dist/api/types.js.map +1 -0
  14. package/dist/api/unread_count.d.ts +57 -0
  15. package/dist/api/unread_count.d.ts.map +1 -0
  16. package/dist/api/unread_count.js +86 -0
  17. package/dist/api/unread_count.js.map +1 -0
  18. package/dist/components/hazo_chat/hazo_chat.d.ts +28 -0
  19. package/dist/components/hazo_chat/hazo_chat.d.ts.map +1 -0
  20. package/dist/components/hazo_chat/hazo_chat.js +201 -0
  21. package/dist/components/hazo_chat/hazo_chat.js.map +1 -0
  22. package/dist/components/hazo_chat/hazo_chat_attachment_preview.d.ts +17 -0
  23. package/dist/components/hazo_chat/hazo_chat_attachment_preview.d.ts.map +1 -0
  24. package/dist/components/hazo_chat/hazo_chat_attachment_preview.js +60 -0
  25. package/dist/components/hazo_chat/hazo_chat_attachment_preview.js.map +1 -0
  26. package/dist/components/hazo_chat/hazo_chat_context.d.ts +42 -0
  27. package/dist/components/hazo_chat/hazo_chat_context.d.ts.map +1 -0
  28. package/dist/components/hazo_chat/hazo_chat_context.js +273 -0
  29. package/dist/components/hazo_chat/hazo_chat_context.js.map +1 -0
  30. package/dist/components/hazo_chat/hazo_chat_document_viewer.d.ts +15 -0
  31. package/dist/components/hazo_chat/hazo_chat_document_viewer.d.ts.map +1 -0
  32. package/dist/components/hazo_chat/hazo_chat_document_viewer.js +140 -0
  33. package/dist/components/hazo_chat/hazo_chat_document_viewer.js.map +1 -0
  34. package/dist/components/hazo_chat/hazo_chat_header.d.ts +16 -0
  35. package/dist/components/hazo_chat/hazo_chat_header.d.ts.map +1 -0
  36. package/dist/components/hazo_chat/hazo_chat_header.js +24 -0
  37. package/dist/components/hazo_chat/hazo_chat_header.js.map +1 -0
  38. package/dist/components/hazo_chat/hazo_chat_input.d.ts +16 -0
  39. package/dist/components/hazo_chat/hazo_chat_input.d.ts.map +1 -0
  40. package/dist/components/hazo_chat/hazo_chat_input.js +75 -0
  41. package/dist/components/hazo_chat/hazo_chat_input.js.map +1 -0
  42. package/dist/components/hazo_chat/hazo_chat_messages.d.ts +17 -0
  43. package/dist/components/hazo_chat/hazo_chat_messages.d.ts.map +1 -0
  44. package/dist/components/hazo_chat/hazo_chat_messages.js +109 -0
  45. package/dist/components/hazo_chat/hazo_chat_messages.js.map +1 -0
  46. package/dist/components/hazo_chat/hazo_chat_reference_list.d.ts +16 -0
  47. package/dist/components/hazo_chat/hazo_chat_reference_list.d.ts.map +1 -0
  48. package/dist/components/hazo_chat/hazo_chat_reference_list.js +59 -0
  49. package/dist/components/hazo_chat/hazo_chat_reference_list.js.map +1 -0
  50. package/dist/components/hazo_chat/hazo_chat_sidebar.d.ts +18 -0
  51. package/dist/components/hazo_chat/hazo_chat_sidebar.d.ts.map +1 -0
  52. package/dist/components/hazo_chat/hazo_chat_sidebar.js +72 -0
  53. package/dist/components/hazo_chat/hazo_chat_sidebar.js.map +1 -0
  54. package/dist/components/hazo_chat/index.d.ts +16 -0
  55. package/dist/components/hazo_chat/index.d.ts.map +1 -0
  56. package/dist/components/hazo_chat/index.js +19 -0
  57. package/dist/components/hazo_chat/index.js.map +1 -0
  58. package/dist/components/index.d.ts +9 -0
  59. package/dist/components/index.d.ts.map +1 -0
  60. package/dist/components/index.js +11 -0
  61. package/dist/components/index.js.map +1 -0
  62. package/dist/components/ui/avatar.d.ts +13 -0
  63. package/dist/components/ui/avatar.d.ts.map +1 -0
  64. package/dist/components/ui/avatar.js +28 -0
  65. package/dist/components/ui/avatar.js.map +1 -0
  66. package/dist/components/ui/badge.d.ts +16 -0
  67. package/dist/components/ui/badge.d.ts.map +1 -0
  68. package/dist/components/ui/badge.js +36 -0
  69. package/dist/components/ui/badge.js.map +1 -0
  70. package/dist/components/ui/button.d.ts +18 -0
  71. package/dist/components/ui/button.d.ts.map +1 -0
  72. package/dist/components/ui/button.js +47 -0
  73. package/dist/components/ui/button.js.map +1 -0
  74. package/dist/components/ui/chat_bubble.d.ts +18 -0
  75. package/dist/components/ui/chat_bubble.d.ts.map +1 -0
  76. package/dist/components/ui/chat_bubble.js +107 -0
  77. package/dist/components/ui/chat_bubble.js.map +1 -0
  78. package/dist/components/ui/hover-card.d.ts +13 -0
  79. package/dist/components/ui/hover-card.d.ts.map +1 -0
  80. package/dist/components/ui/hover-card.js +17 -0
  81. package/dist/components/ui/hover-card.js.map +1 -0
  82. package/dist/components/ui/index.d.ts +19 -0
  83. package/dist/components/ui/index.d.ts.map +1 -0
  84. package/dist/components/ui/index.js +21 -0
  85. package/dist/components/ui/index.js.map +1 -0
  86. package/dist/components/ui/input.d.ts +11 -0
  87. package/dist/components/ui/input.d.ts.map +1 -0
  88. package/dist/components/ui/input.js +18 -0
  89. package/dist/components/ui/input.js.map +1 -0
  90. package/dist/components/ui/loading_skeleton.d.ts +19 -0
  91. package/dist/components/ui/loading_skeleton.d.ts.map +1 -0
  92. package/dist/components/ui/loading_skeleton.js +30 -0
  93. package/dist/components/ui/loading_skeleton.js.map +1 -0
  94. package/dist/components/ui/scroll-area.d.ts +12 -0
  95. package/dist/components/ui/scroll-area.d.ts.map +1 -0
  96. package/dist/components/ui/scroll-area.js +25 -0
  97. package/dist/components/ui/scroll-area.js.map +1 -0
  98. package/dist/components/ui/separator.d.ts +11 -0
  99. package/dist/components/ui/separator.d.ts.map +1 -0
  100. package/dist/components/ui/separator.js +18 -0
  101. package/dist/components/ui/separator.js.map +1 -0
  102. package/dist/components/ui/skeleton.d.ts +9 -0
  103. package/dist/components/ui/skeleton.d.ts.map +1 -0
  104. package/dist/components/ui/skeleton.js +16 -0
  105. package/dist/components/ui/skeleton.js.map +1 -0
  106. package/dist/components/ui/textarea.d.ts +11 -0
  107. package/dist/components/ui/textarea.d.ts.map +1 -0
  108. package/dist/components/ui/textarea.js +18 -0
  109. package/dist/components/ui/textarea.js.map +1 -0
  110. package/dist/components/ui/tooltip.d.ts +14 -0
  111. package/dist/components/ui/tooltip.d.ts.map +1 -0
  112. package/dist/components/ui/tooltip.js +30 -0
  113. package/dist/components/ui/tooltip.js.map +1 -0
  114. package/dist/hooks/index.d.ts +10 -0
  115. package/dist/hooks/index.d.ts.map +1 -0
  116. package/dist/hooks/index.js +10 -0
  117. package/dist/hooks/index.js.map +1 -0
  118. package/dist/hooks/use_chat_messages.d.ts +33 -0
  119. package/dist/hooks/use_chat_messages.d.ts.map +1 -0
  120. package/dist/hooks/use_chat_messages.js +466 -0
  121. package/dist/hooks/use_chat_messages.js.map +1 -0
  122. package/dist/hooks/use_chat_references.d.ts +17 -0
  123. package/dist/hooks/use_chat_references.d.ts.map +1 -0
  124. package/dist/hooks/use_chat_references.js +133 -0
  125. package/dist/hooks/use_chat_references.js.map +1 -0
  126. package/dist/hooks/use_file_upload.d.ts +23 -0
  127. package/dist/hooks/use_file_upload.d.ts.map +1 -0
  128. package/dist/hooks/use_file_upload.js +212 -0
  129. package/dist/hooks/use_file_upload.js.map +1 -0
  130. package/dist/index.d.ts +13 -0
  131. package/dist/index.d.ts.map +1 -0
  132. package/dist/index.js +17 -0
  133. package/dist/index.js.map +1 -0
  134. package/dist/lib/config.d.ts +41 -0
  135. package/dist/lib/config.d.ts.map +1 -0
  136. package/dist/lib/config.js +93 -0
  137. package/dist/lib/config.js.map +1 -0
  138. package/dist/lib/constants.d.ts +41 -0
  139. package/dist/lib/constants.d.ts.map +1 -0
  140. package/dist/lib/constants.js +72 -0
  141. package/dist/lib/constants.js.map +1 -0
  142. package/dist/lib/index.d.ts +9 -0
  143. package/dist/lib/index.d.ts.map +1 -0
  144. package/dist/lib/index.js +9 -0
  145. package/dist/lib/index.js.map +1 -0
  146. package/dist/lib/utils.d.ts +17 -0
  147. package/dist/lib/utils.d.ts.map +1 -0
  148. package/dist/lib/utils.js +20 -0
  149. package/dist/lib/utils.js.map +1 -0
  150. package/dist/types/index.d.ts +404 -0
  151. package/dist/types/index.d.ts.map +1 -0
  152. package/dist/types/index.js +11 -0
  153. package/dist/types/index.js.map +1 -0
  154. package/package.json +1 -1
package/README.md CHANGED
@@ -9,7 +9,7 @@ A full-featured React chat component library for 1-1 communication with document
9
9
  - 📱 **Responsive Design** - Works on desktop and mobile with adaptive layout
10
10
  - 💬 **Real-time Messaging** - Polling or manual refresh modes for message updates with optimistic UI
11
11
  - 📎 **File Attachments** - Support for documents and images with preview
12
- - 📄 **Document Viewer** - Built-in PDF and image viewer with expand/collapse toggle
12
+ - 📄 **Document Viewer** - Built-in PDF and image viewer with expand/collapse toggle, download, and open in new tab actions
13
13
  - 👤 **User Profiles** - Avatar display and user information
14
14
  - 🔄 **Infinite Scroll** - Cursor-based pagination for message history
15
15
  - ✅ **Read Receipts** - Track message read status
@@ -102,7 +102,54 @@ const config: Config = {
102
102
  ],
103
103
  theme: {
104
104
  extend: {
105
- // Your theme extensions
105
+ // Required: Font family configuration for consistent typography
106
+ fontFamily: {
107
+ sans: ['var(--font-sans)', 'system-ui', 'sans-serif'],
108
+ mono: ['var(--font-mono)', 'monospace'],
109
+ },
110
+ // Required: Color variables for shadcn/ui compatibility
111
+ colors: {
112
+ background: 'var(--background)',
113
+ foreground: 'var(--foreground)',
114
+ card: {
115
+ DEFAULT: 'var(--card)',
116
+ foreground: 'var(--card-foreground)',
117
+ },
118
+ popover: {
119
+ DEFAULT: 'var(--popover)',
120
+ foreground: 'var(--popover-foreground)',
121
+ },
122
+ primary: {
123
+ DEFAULT: 'var(--primary)',
124
+ foreground: 'var(--primary-foreground)',
125
+ },
126
+ secondary: {
127
+ DEFAULT: 'var(--secondary)',
128
+ foreground: 'var(--secondary-foreground)',
129
+ },
130
+ muted: {
131
+ DEFAULT: 'var(--muted)',
132
+ foreground: 'var(--muted-foreground)',
133
+ },
134
+ accent: {
135
+ DEFAULT: 'var(--accent)',
136
+ foreground: 'var(--accent-foreground)',
137
+ },
138
+ destructive: {
139
+ DEFAULT: 'var(--destructive)',
140
+ foreground: 'var(--destructive-foreground)',
141
+ },
142
+ border: 'var(--border)',
143
+ input: 'var(--input)',
144
+ ring: 'var(--ring)',
145
+ },
146
+ // Required: Border radius configuration
147
+ borderRadius: {
148
+ lg: 'var(--radius)',
149
+ md: 'calc(var(--radius) - 2px)',
150
+ sm: 'calc(var(--radius) - 4px)',
151
+ },
152
+ // Your additional theme extensions can go here
106
153
  },
107
154
  },
108
155
  plugins: [],
@@ -111,6 +158,18 @@ const config: Config = {
111
158
  export default config;
112
159
  ```
113
160
 
161
+ **Important Theme Extensions:**
162
+
163
+ The `hazo_chat` component relies on specific Tailwind theme extensions to ensure consistent styling:
164
+
165
+ 1. **Font Families:** The `font-sans` and `font-mono` utilities must map to CSS variables for consistent typography across all consuming applications.
166
+
167
+ 2. **Color Variables:** All color utilities (e.g., `bg-background`, `text-foreground`, `border-border`) must map to CSS variables defined in your `globals.css`.
168
+
169
+ 3. **Border Radius:** The component uses `rounded-lg`, `rounded-md`, and `rounded-sm` which must map to your CSS `--radius` variable.
170
+
171
+ **Note:** If you're using shadcn/ui in your project, your Tailwind config likely already includes these extensions. Simply add the `hazo_chat` package path to the `content` array.
172
+
114
173
  **Why This Is Required:**
115
174
 
116
175
  The `hazo_chat` component library uses Tailwind CSS utility classes (e.g., `flex`, `h-full`, `bg-background`, `p-4`) for all styling. These classes are not compiled into CSS by default. By adding the package path to your Tailwind `content` array, Tailwind will:
@@ -910,6 +969,8 @@ interface ChatReferenceItem {
910
969
  }
911
970
  ```
912
971
 
972
+ **Note on MIME Type:** The `mime_type` property is optional. If not provided, the component will automatically infer the MIME type from the file extension (e.g., `.jpg` → `image/jpeg`, `.pdf` → `application/pdf`). This ensures document preview works even when `mime_type` is not explicitly set. Supported file extensions for inference include: `pdf`, `png`, `jpg`, `jpeg`, `gif`, `webp`, `txt`, `doc`, `docx`.
973
+
913
974
  ## Database Schema
914
975
 
915
976
  ### hazo_chat Table
@@ -0,0 +1,26 @@
1
+ /**
2
+ * hazo_chat API Module
3
+ *
4
+ * Provides exportable API route handlers for Next.js applications.
5
+ * These handlers can be imported and re-exported in your API routes.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // app/api/hazo_chat/messages/route.ts
10
+ * import { createMessagesHandler } from 'hazo_chat/api';
11
+ * import { getHazoConnectSingleton } from 'hazo_connect/nextjs/setup';
12
+ *
13
+ * export const dynamic = 'force-dynamic';
14
+ *
15
+ * const { GET, POST } = createMessagesHandler({
16
+ * getHazoConnect: () => getHazoConnectSingleton()
17
+ * });
18
+ *
19
+ * export { GET, POST };
20
+ * ```
21
+ */
22
+ export { createMessagesHandler } from './messages.js';
23
+ export { createUnreadCountFunction } from './unread_count.js';
24
+ export type { MessagesHandlerOptions, ChatMessageInput, ChatMessageRecord } from './types.js';
25
+ export type { UnreadCountFunctionOptions, UnreadCountResult } from './unread_count.js';
26
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AAG9D,YAAY,EACV,sBAAsB,EACtB,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,YAAY,CAAC;AACpB,YAAY,EACV,0BAA0B,EAC1B,iBAAiB,EAClB,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * hazo_chat API Module
3
+ *
4
+ * Provides exportable API route handlers for Next.js applications.
5
+ * These handlers can be imported and re-exported in your API routes.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // app/api/hazo_chat/messages/route.ts
10
+ * import { createMessagesHandler } from 'hazo_chat/api';
11
+ * import { getHazoConnectSingleton } from 'hazo_connect/nextjs/setup';
12
+ *
13
+ * export const dynamic = 'force-dynamic';
14
+ *
15
+ * const { GET, POST } = createMessagesHandler({
16
+ * getHazoConnect: () => getHazoConnectSingleton()
17
+ * });
18
+ *
19
+ * export { GET, POST };
20
+ * ```
21
+ */
22
+ // Export handler factories
23
+ export { createMessagesHandler } from './messages.js';
24
+ export { createUnreadCountFunction } from './unread_count.js';
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,2BAA2B;AAC3B,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Messages API Handler Factory
3
+ *
4
+ * Creates GET and POST handlers for the /api/hazo_chat/messages endpoint.
5
+ * These handlers should be used in a Next.js API route.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // app/api/hazo_chat/messages/route.ts
10
+ * import { createMessagesHandler } from 'hazo_chat/api';
11
+ * import { getHazoConnectSingleton } from 'hazo_connect/nextjs/setup';
12
+ *
13
+ * export const dynamic = 'force-dynamic';
14
+ *
15
+ * const { GET, POST } = createMessagesHandler({
16
+ * getHazoConnect: () => getHazoConnectSingleton()
17
+ * });
18
+ *
19
+ * export { GET, POST };
20
+ * ```
21
+ */
22
+ import { NextRequest, NextResponse } from 'next/server';
23
+ import type { MessagesHandlerOptions } from './types.js';
24
+ /**
25
+ * Creates GET and POST handlers for chat messages
26
+ *
27
+ * @param options - Configuration options
28
+ * @returns Object with GET and POST handlers
29
+ */
30
+ export declare function createMessagesHandler(options: MessagesHandlerOptions): {
31
+ GET: (request: NextRequest) => Promise<NextResponse>;
32
+ POST: (request: NextRequest) => Promise<NextResponse>;
33
+ };
34
+ //# sourceMappingURL=messages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/api/messages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAIxD,OAAO,KAAK,EAAE,sBAAsB,EAAuC,MAAM,YAAY,CAAC;AAmB9F;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,sBAAsB;mBAWvC,WAAW,KAAG,OAAO,CAAC,YAAY,CAAC;oBA+FlC,WAAW,KAAG,OAAO,CAAC,YAAY,CAAC;EA0GjE"}
@@ -0,0 +1,214 @@
1
+ /**
2
+ * Messages API Handler Factory
3
+ *
4
+ * Creates GET and POST handlers for the /api/hazo_chat/messages endpoint.
5
+ * These handlers should be used in a Next.js API route.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // app/api/hazo_chat/messages/route.ts
10
+ * import { createMessagesHandler } from 'hazo_chat/api';
11
+ * import { getHazoConnectSingleton } from 'hazo_connect/nextjs/setup';
12
+ *
13
+ * export const dynamic = 'force-dynamic';
14
+ *
15
+ * const { GET, POST } = createMessagesHandler({
16
+ * getHazoConnect: () => getHazoConnectSingleton()
17
+ * });
18
+ *
19
+ * export { GET, POST };
20
+ * ```
21
+ */
22
+ import { NextResponse } from 'next/server';
23
+ import { cookies } from 'next/headers';
24
+ import { createCrudService } from 'hazo_connect/server';
25
+ // UUID generation for message IDs
26
+ function generateUUID() {
27
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
28
+ const r = Math.random() * 16 | 0;
29
+ const v = c === 'x' ? r : (r & 0x3 | 0x8);
30
+ return v.toString(16);
31
+ });
32
+ }
33
+ /**
34
+ * Default function to get user ID from request cookies
35
+ */
36
+ async function defaultGetUserIdFromRequest() {
37
+ const cookieStore = await cookies();
38
+ return cookieStore.get('hazo_auth_user_id')?.value || null;
39
+ }
40
+ /**
41
+ * Creates GET and POST handlers for chat messages
42
+ *
43
+ * @param options - Configuration options
44
+ * @returns Object with GET and POST handlers
45
+ */
46
+ export function createMessagesHandler(options) {
47
+ const { getHazoConnect, getUserIdFromRequest } = options;
48
+ /**
49
+ * GET handler - Fetch chat messages
50
+ *
51
+ * Query params:
52
+ * - receiver_user_id (required): The other user in the conversation
53
+ * - reference_id (optional): Filter by reference ID
54
+ * - reference_type (optional): Filter by reference type
55
+ */
56
+ async function GET(request) {
57
+ try {
58
+ // Get current user ID
59
+ const current_user_id = getUserIdFromRequest
60
+ ? await getUserIdFromRequest(request)
61
+ : await defaultGetUserIdFromRequest();
62
+ if (!current_user_id) {
63
+ console.error('[hazo_chat/messages GET] No user ID - not authenticated');
64
+ return NextResponse.json({ success: false, error: 'User not authenticated', messages: [] }, { status: 401 });
65
+ }
66
+ // Get query params
67
+ const { searchParams } = new URL(request.url);
68
+ const receiver_user_id = searchParams.get('receiver_user_id');
69
+ const reference_id = searchParams.get('reference_id') || '';
70
+ const reference_type = searchParams.get('reference_type') || '';
71
+ if (!receiver_user_id) {
72
+ console.error('[hazo_chat/messages GET] Missing receiver_user_id');
73
+ return NextResponse.json({ success: false, error: 'receiver_user_id is required', messages: [] }, { status: 400 });
74
+ }
75
+ console.log('[hazo_chat/messages GET] Fetching messages:', {
76
+ current_user_id,
77
+ receiver_user_id,
78
+ reference_id,
79
+ reference_type,
80
+ });
81
+ // Get hazo_connect instance and create CRUD service
82
+ const hazoConnect = getHazoConnect();
83
+ const chatService = createCrudService(hazoConnect, 'hazo_chat');
84
+ let messages = [];
85
+ try {
86
+ // Fetch all messages with reference filters
87
+ const all_messages = await chatService.list((qb) => {
88
+ let builder = qb.select('*');
89
+ if (reference_id) {
90
+ builder = builder.where('reference_id', 'eq', reference_id);
91
+ }
92
+ if (reference_type) {
93
+ builder = builder.where('reference_type', 'eq', reference_type);
94
+ }
95
+ return builder.order('created_at', 'asc');
96
+ });
97
+ // Filter to only messages between current user and receiver
98
+ messages = all_messages.filter((msg) => {
99
+ const is_sent_by_me = msg.sender_user_id === current_user_id && msg.receiver_user_id === receiver_user_id;
100
+ const is_sent_to_me = msg.sender_user_id === receiver_user_id && msg.receiver_user_id === current_user_id;
101
+ return is_sent_by_me || is_sent_to_me;
102
+ });
103
+ }
104
+ catch (dbError) {
105
+ console.error('[hazo_chat/messages GET] Database error:', dbError);
106
+ throw dbError;
107
+ }
108
+ console.log('[hazo_chat/messages GET] Found messages:', messages.length);
109
+ return NextResponse.json({
110
+ success: true,
111
+ messages,
112
+ current_user_id,
113
+ });
114
+ }
115
+ catch (error) {
116
+ const error_message = error instanceof Error ? error.message : 'Unknown error';
117
+ console.error('[hazo_chat/messages GET] Error:', error_message, error);
118
+ return NextResponse.json({ success: false, error: 'Failed to fetch messages', messages: [] }, { status: 500 });
119
+ }
120
+ }
121
+ /**
122
+ * POST handler - Create a new chat message
123
+ *
124
+ * Request body:
125
+ * - receiver_user_id (required): The recipient user ID
126
+ * - message_text (required): The message content
127
+ * - reference_id (optional): Reference ID for context grouping
128
+ * - reference_type (optional): Reference type (default: 'chat')
129
+ */
130
+ async function POST(request) {
131
+ try {
132
+ // Get current user ID (sender)
133
+ const sender_user_id = getUserIdFromRequest
134
+ ? await getUserIdFromRequest(request)
135
+ : await defaultGetUserIdFromRequest();
136
+ if (!sender_user_id) {
137
+ console.error('[hazo_chat/messages POST] No user ID - not authenticated');
138
+ return NextResponse.json({ success: false, error: 'User not authenticated' }, { status: 401 });
139
+ }
140
+ // Parse request body
141
+ const body = await request.json();
142
+ const { receiver_user_id, message_text, reference_id, reference_type } = body;
143
+ // Validate required fields
144
+ if (!receiver_user_id) {
145
+ console.error('[hazo_chat/messages POST] Missing receiver_user_id');
146
+ return NextResponse.json({ success: false, error: 'receiver_user_id is required' }, { status: 400 });
147
+ }
148
+ if (!message_text || message_text.trim() === '') {
149
+ console.error('[hazo_chat/messages POST] Missing or empty message_text');
150
+ return NextResponse.json({ success: false, error: 'message_text is required' }, { status: 400 });
151
+ }
152
+ // Get hazo_connect instance and create CRUD service
153
+ const hazoConnect = getHazoConnect();
154
+ const chatService = createCrudService(hazoConnect, 'hazo_chat');
155
+ // Generate message ID and timestamps
156
+ const message_id = generateUUID();
157
+ const now = new Date().toISOString();
158
+ // Create message record
159
+ const message_record = {
160
+ id: message_id,
161
+ reference_id: reference_id || '',
162
+ reference_type: reference_type || 'chat',
163
+ sender_user_id,
164
+ receiver_user_id,
165
+ message_text: message_text.trim(),
166
+ reference_list: null,
167
+ read_at: null,
168
+ deleted_at: null,
169
+ created_at: now,
170
+ changed_at: now,
171
+ };
172
+ console.log('[hazo_chat/messages POST] Saving message:', {
173
+ id: message_id,
174
+ sender_user_id,
175
+ receiver_user_id,
176
+ reference_id: reference_id || '',
177
+ reference_type: reference_type || 'chat',
178
+ message_length: message_text.length,
179
+ });
180
+ // Save to database
181
+ try {
182
+ await chatService.insert(message_record);
183
+ }
184
+ catch (dbError) {
185
+ console.error('[hazo_chat/messages POST] Database error:', dbError);
186
+ throw dbError;
187
+ }
188
+ console.log('[hazo_chat/messages POST] Message saved successfully:', message_id);
189
+ return NextResponse.json({
190
+ success: true,
191
+ message: {
192
+ id: message_id,
193
+ sender_user_id,
194
+ receiver_user_id,
195
+ reference_id: reference_id || '',
196
+ reference_type: reference_type || 'chat',
197
+ message_text: message_text.trim(),
198
+ reference_list: null,
199
+ read_at: null,
200
+ deleted_at: null,
201
+ created_at: now,
202
+ changed_at: now,
203
+ },
204
+ });
205
+ }
206
+ catch (error) {
207
+ const error_message = error instanceof Error ? error.message : 'Unknown error';
208
+ console.error('[hazo_chat/messages POST] Error:', error_message, error);
209
+ return NextResponse.json({ success: false, error: 'Failed to save message' }, { status: 500 });
210
+ }
211
+ }
212
+ return { GET, POST };
213
+ }
214
+ //# sourceMappingURL=messages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/api/messages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAe,YAAY,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAIxD,kCAAkC;AAClC,SAAS,YAAY;IACnB,OAAO,sCAAsC,CAAC,OAAO,CAAC,OAAO,EAAE,UAAS,CAAC;QACvE,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;QACjC,MAAM,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC;QAC1C,OAAO,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,2BAA2B;IACxC,MAAM,WAAW,GAAG,MAAM,OAAO,EAAE,CAAC;IACpC,OAAO,WAAW,CAAC,GAAG,CAAC,mBAAmB,CAAC,EAAE,KAAK,IAAI,IAAI,CAAC;AAC7D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAA+B;IACnE,MAAM,EAAE,cAAc,EAAE,oBAAoB,EAAE,GAAG,OAAO,CAAC;IAEzD;;;;;;;OAOG;IACH,KAAK,UAAU,GAAG,CAAC,OAAoB;QACrC,IAAI,CAAC;YACH,sBAAsB;YACtB,MAAM,eAAe,GAAG,oBAAoB;gBAC1C,CAAC,CAAC,MAAM,oBAAoB,CAAC,OAAO,CAA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,OAAO,EAAE,CAAC;gBACjB,OAAO,CAAC,KAAK,CAAC,2CAA2C,EAAE,OAAO,CAAC,CAAC;gBACpE,MAAM,OAAO,CAAC;YAChB,CAAC;YAED,OAAO,CAAC,GAAG,CAAC,uDAAuD,EAAE,UAAU,CAAC,CAAC;YAEjF,OAAO,YAAY,CAAC,IAAI,CAAC;gBACvB,OAAO,EAAE,IAAI;gBACb,OAAO,EAAE;oBACP,EAAE,EAAE,UAAU;oBACd,cAAc;oBACd,gBAAgB;oBAChB,YAAY,EAAE,YAAY,IAAI,EAAE;oBAChC,cAAc,EAAE,cAAc,IAAI,MAAM;oBACxC,YAAY,EAAE,YAAY,CAAC,IAAI,EAAE;oBACjC,cAAc,EAAE,IAAI;oBACpB,OAAO,EAAE,IAAI;oBACb,UAAU,EAAE,IAAI;oBAChB,UAAU,EAAE,GAAG;oBACf,UAAU,EAAE,GAAG;iBAChB;aACF,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,aAAa,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC;YAC/E,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,aAAa,EAAE,KAAK,CAAC,CAAC;YAExE,OAAO,YAAY,CAAC,IAAI,CACtB,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,wBAAwB,EAAE,EACnD,EAAE,MAAM,EAAE,GAAG,EAAE,CAChB,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC;AACvB,CAAC"}
@@ -0,0 +1,69 @@
1
+ /**
2
+ * API Handler Types
3
+ *
4
+ * Shared types for the exportable API route handlers.
5
+ * These are used by consumers when setting up their API routes.
6
+ */
7
+ import type { NextRequest } from 'next/server';
8
+ /**
9
+ * Options for creating message API handlers
10
+ */
11
+ export interface MessagesHandlerOptions {
12
+ /**
13
+ * Function to get the hazo_connect adapter instance.
14
+ * Called on each request to get a fresh connection.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { getHazoConnectSingleton } from 'hazo_connect/nextjs/setup';
19
+ *
20
+ * const options = {
21
+ * getHazoConnect: () => getHazoConnectSingleton()
22
+ * };
23
+ * ```
24
+ */
25
+ getHazoConnect: () => unknown;
26
+ /**
27
+ * Optional function to extract user ID from the request.
28
+ * Defaults to reading from 'hazo_auth_user_id' cookie.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * // Custom auth extraction
33
+ * const options = {
34
+ * getUserIdFromRequest: (request) => {
35
+ * const token = request.headers.get('Authorization');
36
+ * return decodeToken(token)?.userId || null;
37
+ * }
38
+ * };
39
+ * ```
40
+ */
41
+ getUserIdFromRequest?: (request: NextRequest) => string | null | Promise<string | null>;
42
+ }
43
+ /**
44
+ * Chat message input for creating new messages
45
+ */
46
+ export interface ChatMessageInput {
47
+ receiver_user_id: string;
48
+ message_text: string;
49
+ reference_id?: string;
50
+ reference_type?: string;
51
+ }
52
+ /**
53
+ * Chat message database record
54
+ */
55
+ export interface ChatMessageRecord {
56
+ id: string;
57
+ reference_id: string;
58
+ reference_type: string;
59
+ sender_user_id: string;
60
+ receiver_user_id: string;
61
+ message_text: string;
62
+ reference_list: string | null;
63
+ read_at: string | null;
64
+ deleted_at: string | null;
65
+ created_at: string;
66
+ changed_at: string;
67
+ [key: string]: unknown;
68
+ }
69
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/api/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;;;;;;;;;;OAYG;IACH,cAAc,EAAE,MAAM,OAAO,CAAC;IAE9B;;;;;;;;;;;;;;OAcG;IACH,oBAAoB,CAAC,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;CACzF;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * API Handler Types
3
+ *
4
+ * Shared types for the exportable API route handlers.
5
+ * These are used by consumers when setting up their API routes.
6
+ */
7
+ export {};
8
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/api/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Unread Count Library Function Factory
3
+ *
4
+ * Creates a function to get unread message counts grouped by reference_id
5
+ * for a given receiver user ID.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // In your server-side code
10
+ * import { createUnreadCountFunction } from 'hazo_chat/api';
11
+ * import { getHazoConnectSingleton } from 'hazo_connect/nextjs/setup';
12
+ *
13
+ * const getUnreadCount = createUnreadCountFunction({
14
+ * getHazoConnect: () => getHazoConnectSingleton()
15
+ * });
16
+ *
17
+ * // Use the function
18
+ * const unreadCounts = await getUnreadCount('receiver-user-id-123');
19
+ * // Returns: [{ reference_id: 'ref-1', count: 5 }, { reference_id: 'ref-2', count: 3 }]
20
+ * ```
21
+ */
22
+ /**
23
+ * Options for creating the unread count function
24
+ */
25
+ export interface UnreadCountFunctionOptions {
26
+ /**
27
+ * Function to get the hazo_connect adapter instance.
28
+ * Called each time the function is invoked to get a fresh connection.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * import { getHazoConnectSingleton } from 'hazo_connect/nextjs/setup';
33
+ *
34
+ * const options = {
35
+ * getHazoConnect: () => getHazoConnectSingleton()
36
+ * };
37
+ * ```
38
+ */
39
+ getHazoConnect: () => unknown;
40
+ }
41
+ /**
42
+ * Result type for unread count function
43
+ */
44
+ export interface UnreadCountResult {
45
+ /** The reference ID */
46
+ reference_id: string;
47
+ /** Number of unread messages for this reference */
48
+ count: number;
49
+ }
50
+ /**
51
+ * Creates a function to get unread message counts by reference_id
52
+ *
53
+ * @param options - Configuration options
54
+ * @returns Function that takes receiver_user_id and returns unread counts
55
+ */
56
+ export declare function createUnreadCountFunction(options: UnreadCountFunctionOptions): (receiver_user_id: string) => Promise<UnreadCountResult[]>;
57
+ //# sourceMappingURL=unread_count.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unread_count.d.ts","sourceRoot":"","sources":["../../src/api/unread_count.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAMH;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC;;;;;;;;;;;;OAYG;IACH,cAAc,EAAE,MAAM,OAAO,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,uBAAuB;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB,mDAAmD;IACnD,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,0BAA0B,IAUzE,kBAAkB,MAAM,KACvB,OAAO,CAAC,iBAAiB,EAAE,CAAC,CA2DhC"}
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Unread Count Library Function Factory
3
+ *
4
+ * Creates a function to get unread message counts grouped by reference_id
5
+ * for a given receiver user ID.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // In your server-side code
10
+ * import { createUnreadCountFunction } from 'hazo_chat/api';
11
+ * import { getHazoConnectSingleton } from 'hazo_connect/nextjs/setup';
12
+ *
13
+ * const getUnreadCount = createUnreadCountFunction({
14
+ * getHazoConnect: () => getHazoConnectSingleton()
15
+ * });
16
+ *
17
+ * // Use the function
18
+ * const unreadCounts = await getUnreadCount('receiver-user-id-123');
19
+ * // Returns: [{ reference_id: 'ref-1', count: 5 }, { reference_id: 'ref-2', count: 3 }]
20
+ * ```
21
+ */
22
+ import { createCrudService } from 'hazo_connect/server';
23
+ /**
24
+ * Creates a function to get unread message counts by reference_id
25
+ *
26
+ * @param options - Configuration options
27
+ * @returns Function that takes receiver_user_id and returns unread counts
28
+ */
29
+ export function createUnreadCountFunction(options) {
30
+ const { getHazoConnect } = options;
31
+ /**
32
+ * Get unread message counts grouped by reference_id for a receiver user
33
+ *
34
+ * @param receiver_user_id - The user ID to get unread counts for
35
+ * @returns Array of objects with reference_id and count of unread messages
36
+ */
37
+ return async function hazo_chat_get_unread_count(receiver_user_id) {
38
+ try {
39
+ if (!receiver_user_id || receiver_user_id.trim() === '') {
40
+ console.error('[hazo_chat_get_unread_count] Missing receiver_user_id');
41
+ return [];
42
+ }
43
+ console.log('[hazo_chat_get_unread_count] Fetching unread counts for:', {
44
+ receiver_user_id,
45
+ });
46
+ // Get hazo_connect instance and create CRUD service
47
+ const hazoConnect = getHazoConnect();
48
+ const chatService = createCrudService(hazoConnect, 'hazo_chat');
49
+ // Fetch all unread messages for this receiver
50
+ const unread_messages = await chatService.list((qb) => {
51
+ return qb
52
+ .select('*')
53
+ .where('receiver_user_id', 'eq', receiver_user_id)
54
+ .where('read_at', 'is', null)
55
+ .where('deleted_at', 'is', null);
56
+ });
57
+ // Group by reference_id and count
58
+ const count_map = new Map();
59
+ for (const message of unread_messages) {
60
+ const ref_id = message.reference_id || '';
61
+ const current_count = count_map.get(ref_id) || 0;
62
+ count_map.set(ref_id, current_count + 1);
63
+ }
64
+ // Convert map to array of results
65
+ const results = Array.from(count_map.entries()).map(([reference_id, count]) => ({
66
+ reference_id,
67
+ count,
68
+ }));
69
+ // Sort by count descending (most unread first)
70
+ results.sort((a, b) => b.count - a.count);
71
+ console.log('[hazo_chat_get_unread_count] Found unread counts:', {
72
+ receiver_user_id,
73
+ total_references: results.length,
74
+ total_unread: unread_messages.length,
75
+ });
76
+ return results;
77
+ }
78
+ catch (error) {
79
+ const error_message = error instanceof Error ? error.message : 'Unknown error';
80
+ console.error('[hazo_chat_get_unread_count] Error:', error_message, error);
81
+ // Return empty array on error rather than throwing
82
+ return [];
83
+ }
84
+ };
85
+ }
86
+ //# sourceMappingURL=unread_count.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unread_count.js","sourceRoot":"","sources":["../../src/api/unread_count.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAkCxD;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAmC;IAC3E,MAAM,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC;IAEnC;;;;;OAKG;IACH,OAAO,KAAK,UAAU,0BAA0B,CAC9C,gBAAwB;QAExB,IAAI,CAAC;YACH,IAAI,CAAC,gBAAgB,IAAI,gBAAgB,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;gBACxD,OAAO,CAAC,KAAK,CAAC,uDAAuD,CAAC,CAAC;gBACvE,OAAO,EAAE,CAAC;YACZ,CAAC;YAED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