@jsb188/graphql 1.1.10 → 1.1.11
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/cache/config.ts +64 -50
- package/cache/index.test.ts +671 -109
- package/cache/index.ts +1700 -1240
- package/cache/persist.ts +721 -0
- package/cache/store.ts +437 -0
- package/client/data-table-realtime-subscriptions.test.ts +740 -0
- package/client/data-table-realtime-subscriptions.ts +955 -0
- package/client/data-table-realtime.test.ts +792 -0
- package/client/data-table-realtime.ts +1218 -0
- package/client/fragment-realtime-sequence.ts +52 -0
- package/client/index.ts +2 -0
- package/client/open-sheet.ts +137 -0
- package/client/request-managed-session.test.ts +574 -0
- package/client/request-no-store.test.ts +157 -0
- package/client/request.ts +954 -163
- package/client/sse-ticket.test.ts +26 -0
- package/deno.lock +170 -0
- package/deno.test.json +8 -4
- package/drafts/index.ts +149 -0
- package/drafts/store.ts +397 -0
- package/gql/eagerQueryMap.ts +12 -0
- package/gql/fragments/accountAuthFragments.ts +5 -0
- package/gql/fragments/accountFragments.ts +0 -3
- package/gql/fragments/accountSessionFragments.ts +16 -0
- package/gql/fragments/agentDocFragments.ts +46 -0
- package/gql/fragments/agentMemoryFragments.ts +19 -0
- package/gql/fragments/aiChatFragments.ts +16 -0
- package/gql/fragments/billingFragments.ts +0 -11
- package/gql/fragments/campaignFragments.ts +277 -0
- package/gql/fragments/dataTableFragments.ts +18 -128
- package/gql/fragments/emailFragments.ts +19 -9
- package/gql/fragments/fieldDocFragments.ts +82 -0
- package/gql/fragments/logFragments.ts +20 -79
- package/gql/fragments/organizationFragments.ts +43 -5
- package/gql/fragments/scheduleFragments.ts +155 -0
- package/gql/fragments/sheetFragments.ts +552 -22
- package/gql/fragments/storageFragments.ts +21 -0
- package/gql/fragments/workflowFragments.ts +28 -4
- package/gql/mutations/accountMutations.ts +0 -2
- package/gql/mutations/agentDocMutations.ts +177 -0
- package/gql/mutations/agentMemoryMutations.ts +36 -0
- package/gql/mutations/aiChatMutations.ts +17 -4
- package/gql/mutations/authMutations.ts +158 -17
- package/gql/mutations/campaignMutations.ts +215 -0
- package/gql/mutations/dataTableMutations.ts +48 -26
- package/gql/mutations/fieldDocMutations.ts +24 -0
- package/gql/mutations/index.ts +6 -1
- package/gql/mutations/logMutations.ts +6 -29
- package/gql/mutations/organizationMutations.ts +13 -4
- package/gql/mutations/scheduleMutations.ts +423 -0
- package/gql/mutations/sheetMutations.ts +273 -56
- package/gql/mutations/storageMutations.ts +20 -0
- package/gql/mutations/workflowMutations.ts +42 -0
- package/gql/queries/agentDocQueries.ts +120 -0
- package/gql/queries/agentMemoryQueries.ts +22 -0
- package/gql/queries/aiChatQueries.ts +18 -2
- package/gql/queries/authQueries.ts +41 -0
- package/gql/queries/billingQueries.ts +23 -0
- package/gql/queries/campaignQueries.ts +127 -0
- package/gql/queries/dataTableQueries.ts +100 -11
- package/gql/queries/emailQueries.ts +55 -3
- package/gql/queries/fieldDocQueries.ts +158 -0
- package/gql/queries/index.ts +8 -2
- package/gql/queries/logQueries.ts +11 -28
- package/gql/queries/organizationQueries.ts +52 -1
- package/gql/queries/scheduleQueries.ts +315 -0
- package/gql/queries/sheetQueries.ts +336 -25
- package/gql/queries/storageQueries.ts +38 -0
- package/gql/queries/workflowQueries.ts +72 -3
- package/hooks/index.ts +2065 -971
- package/hooks/use-account-mtn.ts +76 -81
- package/hooks/use-agentDoc-mtn.ts +285 -0
- package/hooks/use-agentDoc-qry.ts +208 -0
- package/hooks/use-agentMemory-mtn.ts +73 -0
- package/hooks/use-agentMemory-qry.ts +44 -0
- package/hooks/use-aiChat-mtn.ts +232 -137
- package/hooks/use-aiChat-qry.ts +480 -222
- package/hooks/use-app-qry.ts +12 -9
- package/hooks/use-auth-mtn.ts +406 -252
- package/hooks/use-auth-qry.ts +51 -0
- package/hooks/use-campaign-mtn.ts +377 -0
- package/hooks/use-campaign-qry.ts +257 -0
- package/hooks/use-dataTable-mtn.ts +116 -33
- package/hooks/use-dataTable-qry.ts +859 -35
- package/hooks/use-email-qry.ts +194 -79
- package/hooks/use-fieldDoc-mtn.ts +50 -0
- package/hooks/use-fieldDoc-qry.ts +180 -0
- package/hooks/use-inboundContact-mtn.ts +19 -16
- package/hooks/use-inboundContact-qry.ts +63 -55
- package/hooks/use-integration-mtn.ts +56 -38
- package/hooks/use-integration-qry.ts +24 -9
- package/hooks/use-log-mtn.ts +72 -56
- package/hooks/use-log-qry.ts +93 -69
- package/hooks/use-organization-mtn.ts +334 -277
- package/hooks/use-organization-qry.ts +199 -129
- package/hooks/use-schedule-mtn.ts +416 -0
- package/hooks/use-schedule-qry.ts +157 -0
- package/hooks/use-sheet-mtn.ts +233 -47
- package/hooks/use-sheet-qry.ts +353 -70
- package/hooks/use-storage-mtn.ts +81 -41
- package/hooks/use-storage-qry.ts +104 -13
- package/hooks/use-workflow-mtn.ts +55 -0
- package/hooks/use-workflow-qry.ts +206 -48
- package/loaders/campaign.ts +137 -0
- package/package.json +94 -71
- package/sse.ts +157 -104
- package/tsconfig.json +11 -10
- package/types.d.ts +60 -30
- package/utils.ts +78 -49
- package/gql/fragments/productFragments.ts +0 -39
- package/gql/fragments/reportFragments.ts +0 -90
- package/gql/mutations/billingMutations.ts +0 -34
- package/gql/queries/productQueries.ts +0 -31
- package/gql/queries/reportQueries.ts +0 -134
- package/hooks/use-product-qry.ts +0 -67
- package/hooks/use-report-qry.ts +0 -153
package/hooks/use-aiChat-qry.ts
CHANGED
|
@@ -1,11 +1,28 @@
|
|
|
1
1
|
import { getENVVariable } from '@jsb188/app';
|
|
2
|
-
import { getAuthToken } from '@jsb188/app/utils/api.ts';
|
|
3
2
|
import { makeVariablesKey } from '@jsb188/app/utils/logic.ts';
|
|
4
3
|
import SSE from '@jsb188/sse';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
import {
|
|
5
|
+
FRAGMENT_NAMES,
|
|
6
|
+
getFragmentBaseName,
|
|
7
|
+
makeFragmentKey,
|
|
8
|
+
} from '@jsb188/mday/constants/graphqlReactivity.ts';
|
|
9
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
10
|
+
import {
|
|
11
|
+
loadFragment,
|
|
12
|
+
loadQuery,
|
|
13
|
+
resetQuery,
|
|
14
|
+
updateQuery,
|
|
15
|
+
} from '../cache/index.ts';
|
|
16
|
+
import {
|
|
17
|
+
useQuery,
|
|
18
|
+
useReactiveFragment,
|
|
19
|
+
useReactiveFragmentMap,
|
|
20
|
+
} from '../client/index.ts';
|
|
21
|
+
import {
|
|
22
|
+
aiChatMessagesQry,
|
|
23
|
+
aiChatQry,
|
|
24
|
+
aiChatsQry,
|
|
25
|
+
} from '../gql/queries/aiChatQueries.ts';
|
|
9
26
|
import handleSSEData, { type PublishPayload } from '../sse.ts';
|
|
10
27
|
import type { UpdateObserversFn, UseQueryParams } from '../types.d.ts';
|
|
11
28
|
|
|
@@ -17,197 +34,412 @@ const AI_CHATS_LIMIT = 70; // Must match server code exactly
|
|
|
17
34
|
const AI_CHAT_MESSAGES_LIMIT = 55; // Must match server code exactly
|
|
18
35
|
|
|
19
36
|
/**
|
|
20
|
-
*
|
|
37
|
+
* Helper; build AI chats query key
|
|
21
38
|
*/
|
|
22
39
|
|
|
23
|
-
|
|
24
|
-
|
|
40
|
+
function getAIChatsQueryKey(
|
|
41
|
+
organizationId?: string | null,
|
|
42
|
+
filter?: 'CAL_DATE' | 'CHATS' | 'ARCHIVED',
|
|
43
|
+
) {
|
|
44
|
+
if (!filter || (filter !== 'CAL_DATE' && !organizationId)) {
|
|
45
|
+
return '';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const variablesKey = makeVariablesKey({
|
|
49
|
+
organizationId: filter === 'CAL_DATE' ? null : organizationId,
|
|
50
|
+
filter,
|
|
51
|
+
cursor: null,
|
|
52
|
+
after: false,
|
|
53
|
+
limit: AI_CHATS_LIMIT,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return `#aiChats:${variablesKey}`;
|
|
25
57
|
}
|
|
26
58
|
|
|
27
59
|
/**
|
|
28
|
-
* Helper;
|
|
60
|
+
* Helper; get generating (status) key from SSE data
|
|
29
61
|
*/
|
|
30
62
|
|
|
31
|
-
function
|
|
32
|
-
|
|
33
|
-
|
|
63
|
+
function getGeneratingFragmentId(sseData: PublishPayload) {
|
|
64
|
+
const { __type, dataFragmentKey, fragmentKey, fragmentNamespace, data } =
|
|
65
|
+
sseData;
|
|
66
|
+
const finished = data?.finished;
|
|
67
|
+
|
|
68
|
+
if (__type === 'CONNECTED') {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const fragmentName = FRAGMENT_NAMES.aiChatMessage + ':';
|
|
73
|
+
|
|
74
|
+
if (typeof finished === 'boolean') {
|
|
75
|
+
if (dataFragmentKey?.startsWith(fragmentName)) {
|
|
76
|
+
return finished ? null : dataFragmentKey;
|
|
77
|
+
} else if (fragmentKey?.startsWith(fragmentName)) {
|
|
78
|
+
return finished ? null : fragmentKey;
|
|
79
|
+
}
|
|
80
|
+
} else if (
|
|
81
|
+
fragmentKey?.startsWith(fragmentName) &&
|
|
82
|
+
fragmentNamespace === 'text'
|
|
83
|
+
) {
|
|
84
|
+
return fragmentKey;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Return the message id from an AI chat message fragment key.
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
function getAIChatMessageIdFromFragmentKey(fragmentKey?: string | null) {
|
|
93
|
+
const fragmentName = FRAGMENT_NAMES.aiChatMessage + ':';
|
|
94
|
+
return fragmentKey?.startsWith(fragmentName)
|
|
95
|
+
? fragmentKey.substring(fragmentName.length)
|
|
96
|
+
: null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Merge one message into a local live message list.
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
function upsertLocalAIChatMessage(
|
|
104
|
+
messages: any[],
|
|
105
|
+
message: any,
|
|
106
|
+
prepend?: boolean,
|
|
34
107
|
) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
108
|
+
if (!message?.id) {
|
|
109
|
+
return messages;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const index = messages.findIndex((item) => item?.id === message.id);
|
|
113
|
+
if (index >= 0) {
|
|
114
|
+
const nextMessages = messages.slice(0);
|
|
115
|
+
nextMessages[index] = {
|
|
116
|
+
...nextMessages[index],
|
|
117
|
+
...message,
|
|
118
|
+
};
|
|
119
|
+
return nextMessages;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return prepend ? [message, ...messages] : [...messages, message];
|
|
48
123
|
}
|
|
49
124
|
|
|
50
125
|
/**
|
|
51
|
-
*
|
|
126
|
+
* Merge SSE payloads into local live AI chat messages.
|
|
52
127
|
*/
|
|
53
128
|
|
|
54
|
-
function
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
129
|
+
function mergeSSEPayloadToLocalAIChatMessages(
|
|
130
|
+
messages: any[],
|
|
131
|
+
sseData: PublishPayload,
|
|
132
|
+
) {
|
|
133
|
+
const {
|
|
134
|
+
__type,
|
|
135
|
+
data,
|
|
136
|
+
dataFragmentKey,
|
|
137
|
+
fragmentKey,
|
|
138
|
+
fragmentNamespace,
|
|
139
|
+
otherData,
|
|
140
|
+
} = sseData;
|
|
141
|
+
|
|
142
|
+
switch (__type) {
|
|
143
|
+
case 'QUERY_APPEND':
|
|
144
|
+
case 'QUERY_PREPEND':
|
|
145
|
+
{
|
|
146
|
+
const messageId = getAIChatMessageIdFromFragmentKey(dataFragmentKey);
|
|
147
|
+
if (messageId && data && typeof data === 'object') {
|
|
148
|
+
return upsertLocalAIChatMessage(
|
|
149
|
+
messages,
|
|
150
|
+
{
|
|
151
|
+
...data,
|
|
152
|
+
id: data.id || messageId,
|
|
153
|
+
},
|
|
154
|
+
__type === 'QUERY_PREPEND',
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
159
|
+
case 'DATA_APPEND':
|
|
160
|
+
case 'DATA_PREPEND':
|
|
161
|
+
{
|
|
162
|
+
const messageId = getAIChatMessageIdFromFragmentKey(fragmentKey);
|
|
163
|
+
if (messageId && fragmentNamespace === 'text') {
|
|
164
|
+
const index = messages.findIndex((item) => item?.id === messageId);
|
|
165
|
+
const currentMessage =
|
|
166
|
+
index >= 0
|
|
167
|
+
? messages[index]
|
|
168
|
+
: {
|
|
169
|
+
id: messageId,
|
|
170
|
+
accountId: null,
|
|
171
|
+
type: 'NORMAL',
|
|
172
|
+
cursor: null,
|
|
173
|
+
text: '',
|
|
174
|
+
finished: false,
|
|
175
|
+
at: new Date().toISOString(),
|
|
176
|
+
};
|
|
177
|
+
const currentText = currentMessage.text || '';
|
|
178
|
+
const nextText =
|
|
179
|
+
__type === 'DATA_PREPEND'
|
|
180
|
+
? `${data || ''}${currentText}`
|
|
181
|
+
: `${currentText}${data || ''}`;
|
|
182
|
+
|
|
183
|
+
return upsertLocalAIChatMessage(messages, {
|
|
184
|
+
...currentMessage,
|
|
185
|
+
...(otherData && typeof otherData === 'object' ? otherData : {}),
|
|
186
|
+
text: nextText,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
case 'DATA_UPDATE':
|
|
192
|
+
{
|
|
193
|
+
const messageId = getAIChatMessageIdFromFragmentKey(
|
|
194
|
+
dataFragmentKey || fragmentKey,
|
|
195
|
+
);
|
|
196
|
+
if (messageId && data && typeof data === 'object') {
|
|
197
|
+
return upsertLocalAIChatMessage(messages, {
|
|
198
|
+
id: messageId,
|
|
199
|
+
...data,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
break;
|
|
204
|
+
default:
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return messages;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Merge local live messages into the cached AI chat response.
|
|
213
|
+
*/
|
|
214
|
+
|
|
215
|
+
function mergeLocalAIChatMessages(aiChat: any, localMessages: any[]) {
|
|
216
|
+
if (!aiChat || !localMessages.length) {
|
|
217
|
+
return aiChat;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const localMessageMap = new Map(
|
|
221
|
+
localMessages.map((message) => [message.id, message]),
|
|
222
|
+
);
|
|
223
|
+
const usedMessageIds = new Set<string>();
|
|
224
|
+
const messages = (aiChat.messages || []).map((message: any) => {
|
|
225
|
+
const localMessage = localMessageMap.get(message.id);
|
|
226
|
+
if (!localMessage) {
|
|
227
|
+
return message;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
usedMessageIds.add(message.id);
|
|
231
|
+
return {
|
|
232
|
+
...message,
|
|
233
|
+
...localMessage,
|
|
234
|
+
};
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
for (const message of localMessages) {
|
|
238
|
+
if (!usedMessageIds.has(message.id)) {
|
|
239
|
+
messages.push(message);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return {
|
|
244
|
+
...aiChat,
|
|
245
|
+
messages,
|
|
246
|
+
};
|
|
76
247
|
}
|
|
77
248
|
|
|
78
249
|
/**
|
|
79
250
|
* Fetch AI chat
|
|
80
251
|
*/
|
|
81
252
|
|
|
82
|
-
export function useAIChatWithSSE(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
253
|
+
export function useAIChatWithSSE(
|
|
254
|
+
aiChatId?: string,
|
|
255
|
+
initialSessionKey?: string | null,
|
|
256
|
+
params: UseQueryParams = {},
|
|
257
|
+
) {
|
|
258
|
+
const { data, ...rest } = useQuery(aiChatQry, {
|
|
259
|
+
variables: {
|
|
260
|
+
aiChatId,
|
|
261
|
+
},
|
|
262
|
+
skip: !aiChatId,
|
|
263
|
+
persistTime: 20 * 60 * 1000,
|
|
264
|
+
...params,
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const { updateObservers, variablesKey } = rest;
|
|
268
|
+
const [sessionKey, setSessionKey] = useState<string | null>(
|
|
269
|
+
initialSessionKey || null,
|
|
270
|
+
);
|
|
271
|
+
const [generatingFragmentId, setGeneratingFragmentId] = useState<
|
|
272
|
+
string | null
|
|
273
|
+
>(null);
|
|
274
|
+
const [localMessages, setLocalMessages] = useState<any[]>([]);
|
|
275
|
+
const baseAIChat =
|
|
276
|
+
data?.aiChat ||
|
|
277
|
+
(aiChatId
|
|
278
|
+
? loadFragment(makeFragmentKey(FRAGMENT_NAMES.aiChat, aiChatId))
|
|
279
|
+
: null);
|
|
280
|
+
const aiChat = useReactiveAIChatFragment(aiChatId, baseAIChat);
|
|
281
|
+
const mergedAIChat = useMemo(
|
|
282
|
+
() => mergeLocalAIChatMessages(aiChat, localMessages),
|
|
283
|
+
[aiChat, localMessages],
|
|
284
|
+
);
|
|
285
|
+
const dataAIChatId = aiChat?.id;
|
|
286
|
+
|
|
287
|
+
const addLocalMessage = useCallback((message: any) => {
|
|
288
|
+
setLocalMessages((prevMessages) =>
|
|
289
|
+
upsertLocalAIChatMessage(prevMessages, message),
|
|
290
|
+
);
|
|
291
|
+
}, []);
|
|
292
|
+
|
|
293
|
+
/*
|
|
294
|
+
* Switch to a newly issued SSE session and hide the assistant draft superseded by that send.
|
|
295
|
+
*/
|
|
296
|
+
const activateAIChatSession = useCallback(
|
|
297
|
+
(nextSessionKey: string) => {
|
|
298
|
+
const supersededMessageId =
|
|
299
|
+
getAIChatMessageIdFromFragmentKey(generatingFragmentId);
|
|
300
|
+
if (supersededMessageId) {
|
|
301
|
+
setLocalMessages((prevMessages) =>
|
|
302
|
+
upsertLocalAIChatMessage(prevMessages, {
|
|
303
|
+
id: supersededMessageId,
|
|
304
|
+
deleted: true,
|
|
305
|
+
finished: true,
|
|
306
|
+
}),
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
setSessionKey(nextSessionKey);
|
|
310
|
+
},
|
|
311
|
+
[generatingFragmentId],
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
useEffect(() => {
|
|
315
|
+
setLocalMessages([]);
|
|
316
|
+
}, [dataAIChatId]);
|
|
317
|
+
|
|
318
|
+
useEffect(() => {
|
|
319
|
+
// Browser has too many issues such as proxy, network, roadblocks
|
|
320
|
+
// that make it unreliable to use perpetual SSE in production.
|
|
321
|
+
// So we have to write the logic to make it work without perpetual session.
|
|
322
|
+
|
|
323
|
+
if (dataAIChatId && sessionKey) {
|
|
324
|
+
// Only listen to events if data is loaded
|
|
325
|
+
const connection = new SSE({
|
|
326
|
+
host: getENVVariable('SSE_SERVER') as string,
|
|
327
|
+
path: `page_events/ai_chat/${dataAIChatId}`,
|
|
328
|
+
// path: 'test-sse',
|
|
329
|
+
searchParams: {
|
|
330
|
+
session_key: sessionKey,
|
|
331
|
+
variablesKey,
|
|
332
|
+
},
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
const connectParams = {
|
|
336
|
+
onMessage: (sseData: PublishPayload) => {
|
|
337
|
+
handleSSEData(sseData, updateObservers);
|
|
338
|
+
setLocalMessages((prevMessages) =>
|
|
339
|
+
mergeSSEPayloadToLocalAIChatMessages(prevMessages, sseData),
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
const nextValue = getGeneratingFragmentId(sseData);
|
|
343
|
+
if (nextValue === null || typeof nextValue === 'string') {
|
|
344
|
+
setGeneratingFragmentId(nextValue);
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
onError: () => {
|
|
348
|
+
setGeneratingFragmentId(null);
|
|
349
|
+
},
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
connection.connect(connectParams);
|
|
353
|
+
// const removeNetworkListeners = reconnectOnBrowserNetwork(connection, connectParams);
|
|
354
|
+
|
|
355
|
+
return () => {
|
|
356
|
+
connection.close();
|
|
357
|
+
// removeNetworkListeners();
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
}, [dataAIChatId, sessionKey, variablesKey]);
|
|
361
|
+
|
|
362
|
+
useEffect(() => {
|
|
363
|
+
// Every time AIChat is fetched, check if it exists in lists cache
|
|
364
|
+
if (dataAIChatId) {
|
|
365
|
+
const queryKey = getAIChatsQueryKey(
|
|
366
|
+
aiChat.organizationId,
|
|
367
|
+
aiChat.calDate ? 'CAL_DATE' : aiChat.archivedAt ? 'ARCHIVED' : 'CHATS',
|
|
368
|
+
);
|
|
369
|
+
const queryCache = loadQuery(queryKey);
|
|
370
|
+
|
|
371
|
+
if (queryCache) {
|
|
372
|
+
const notFound = queryCache.every((a: any) => a.id !== dataAIChatId);
|
|
373
|
+
|
|
374
|
+
if (notFound) {
|
|
375
|
+
// If not found, add to cache
|
|
376
|
+
updateQuery(
|
|
377
|
+
queryKey,
|
|
378
|
+
(prev: any) => {
|
|
379
|
+
return [
|
|
380
|
+
{
|
|
381
|
+
cursor: aiChat.cursor,
|
|
382
|
+
__flat: {
|
|
383
|
+
__cache: true,
|
|
384
|
+
data: [
|
|
385
|
+
makeFragmentKey(FRAGMENT_NAMES.aiChat, dataAIChatId),
|
|
386
|
+
],
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
...prev,
|
|
390
|
+
];
|
|
391
|
+
},
|
|
392
|
+
false,
|
|
393
|
+
updateObservers,
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}, [dataAIChatId]);
|
|
399
|
+
|
|
400
|
+
useEffect(() => {
|
|
401
|
+
// Force reset if chat message is in progress
|
|
402
|
+
if (aiChat?.inProgress) {
|
|
403
|
+
return () => {
|
|
404
|
+
resetQuery(rest.queryKey, true, updateObservers);
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
}, [aiChat?.inProgress]);
|
|
408
|
+
|
|
409
|
+
return {
|
|
410
|
+
aiChat: mergedAIChat,
|
|
411
|
+
generatingFragmentId,
|
|
412
|
+
sessionKey,
|
|
413
|
+
addLocalMessage,
|
|
414
|
+
setSessionKey: activateAIChatSession,
|
|
415
|
+
...rest,
|
|
416
|
+
};
|
|
187
417
|
}
|
|
188
418
|
|
|
189
419
|
/**
|
|
190
420
|
* Fetch AI chat messages
|
|
191
421
|
*/
|
|
192
422
|
|
|
193
|
-
export function useAIChatMessages(
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
423
|
+
export function useAIChatMessages(
|
|
424
|
+
aiChatId?: string,
|
|
425
|
+
params: UseQueryParams = {},
|
|
426
|
+
) {
|
|
427
|
+
const { data, ...other } = useQuery(aiChatMessagesQry, {
|
|
428
|
+
variables: {
|
|
429
|
+
aiChatId,
|
|
430
|
+
cursor: null,
|
|
431
|
+
after: true,
|
|
432
|
+
limit: AI_CHAT_MESSAGES_LIMIT,
|
|
433
|
+
},
|
|
434
|
+
// If this query is used for virtualized list pagination, set {params.skip=true}
|
|
435
|
+
skip: !aiChatId,
|
|
436
|
+
...params,
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
return {
|
|
440
|
+
aiChatMessages: data?.aiChatMessages,
|
|
441
|
+
...other,
|
|
442
|
+
};
|
|
211
443
|
}
|
|
212
444
|
|
|
213
445
|
/**
|
|
@@ -215,63 +447,86 @@ export function useAIChatMessages(aiChatId?: string, params: UseQueryParams = {}
|
|
|
215
447
|
*/
|
|
216
448
|
|
|
217
449
|
export function updateAIChats(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
450
|
+
aiChat: any,
|
|
451
|
+
organizationId: string | null | undefined,
|
|
452
|
+
updateObservers: UpdateObserversFn,
|
|
221
453
|
) {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
454
|
+
if (aiChat) {
|
|
455
|
+
const isCalDate = !!aiChat.calDate;
|
|
456
|
+
const isArchived = !!aiChat.archivedAt;
|
|
457
|
+
const queryKey = getAIChatsQueryKey(
|
|
458
|
+
isCalDate ? null : organizationId || aiChat.organizationId,
|
|
459
|
+
isCalDate ? 'CAL_DATE' : isArchived ? 'ARCHIVED' : 'CHATS',
|
|
460
|
+
);
|
|
461
|
+
const fragmentKey = makeFragmentKey(FRAGMENT_NAMES.aiChat, aiChat.id);
|
|
462
|
+
|
|
463
|
+
updateQuery(
|
|
464
|
+
queryKey,
|
|
465
|
+
(res: any[]) => {
|
|
466
|
+
if (res.some((item: any) => item?.__flat?.data?.[0] === fragmentKey)) {
|
|
467
|
+
return res;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return [
|
|
471
|
+
{
|
|
472
|
+
cursor: aiChat.cursor,
|
|
473
|
+
__flat: {
|
|
474
|
+
__cache: true,
|
|
475
|
+
data: [fragmentKey],
|
|
476
|
+
},
|
|
477
|
+
},
|
|
478
|
+
...res,
|
|
479
|
+
];
|
|
480
|
+
},
|
|
481
|
+
false,
|
|
482
|
+
updateObservers,
|
|
483
|
+
);
|
|
484
|
+
}
|
|
253
485
|
}
|
|
254
486
|
|
|
255
487
|
/**
|
|
256
488
|
* Fetch AI chats list
|
|
257
489
|
*/
|
|
258
490
|
|
|
259
|
-
export function useAIChats(
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
491
|
+
export function useAIChats(
|
|
492
|
+
organizationId?: string | null,
|
|
493
|
+
filter?: 'CAL_DATE' | 'CHATS' | 'ARCHIVED',
|
|
494
|
+
params: UseQueryParams = {},
|
|
495
|
+
) {
|
|
496
|
+
const queryOrganizationId = filter === 'CAL_DATE' ? null : organizationId;
|
|
497
|
+
const { data, ...other } = useQuery(aiChatsQry, {
|
|
498
|
+
variables: {
|
|
499
|
+
organizationId: queryOrganizationId,
|
|
500
|
+
filter,
|
|
501
|
+
cursor: null,
|
|
502
|
+
after: false,
|
|
503
|
+
limit: AI_CHATS_LIMIT,
|
|
504
|
+
},
|
|
505
|
+
skip: filter !== 'CAL_DATE' && !organizationId,
|
|
506
|
+
...params,
|
|
507
|
+
persistTime: params.persistTime ?? 15 * 60 * 1000,
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
return {
|
|
511
|
+
aiChats: data?.aiChats,
|
|
512
|
+
...other,
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Get reactive AI chat fragment for one chat.
|
|
518
|
+
*/
|
|
519
|
+
|
|
520
|
+
export function useReactiveAIChatFragment(
|
|
521
|
+
aiChatId?: string | null,
|
|
522
|
+
currentData?: any,
|
|
523
|
+
queryCount?: number,
|
|
524
|
+
) {
|
|
525
|
+
return useReactiveFragment(
|
|
526
|
+
currentData,
|
|
527
|
+
aiChatId ? [makeFragmentKey(FRAGMENT_NAMES.aiChat, aiChatId)] : [],
|
|
528
|
+
queryCount,
|
|
529
|
+
);
|
|
275
530
|
}
|
|
276
531
|
|
|
277
532
|
/**
|
|
@@ -279,5 +534,8 @@ export function useAIChats(organizationId: string, filter?: 'CAL_DATE' | 'CHATS'
|
|
|
279
534
|
*/
|
|
280
535
|
|
|
281
536
|
export function useReactiveAIChatFragmentMap(currentData?: any[] | null) {
|
|
282
|
-
|
|
537
|
+
return useReactiveFragmentMap(
|
|
538
|
+
currentData || null,
|
|
539
|
+
getFragmentBaseName(FRAGMENT_NAMES.aiChat),
|
|
540
|
+
);
|
|
283
541
|
}
|