@jsb188/graphql 1.1.8 → 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 +66 -42
- package/cache/index.test.ts +881 -0
- package/cache/index.ts +1815 -979
- 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 +4 -2
- 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 +973 -143
- package/client/sse-ticket.test.ts +26 -0
- package/deno.lock +170 -0
- package/deno.test.json +10 -0
- 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 +1 -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 +17 -0
- package/gql/fragments/billingFragments.ts +0 -11
- package/gql/fragments/campaignFragments.ts +277 -0
- package/gql/fragments/dataTableFragments.ts +102 -0
- package/gql/fragments/emailFragments.ts +36 -0
- package/gql/fragments/fieldDocFragments.ts +82 -0
- package/gql/fragments/inboundContactFragments.ts +15 -0
- package/gql/fragments/integrationFragments.ts +22 -0
- package/gql/fragments/logFragments.ts +33 -64
- package/gql/fragments/organizationFragments.ts +94 -21
- package/gql/fragments/scheduleFragments.ts +155 -0
- package/gql/fragments/sheetFragments.ts +675 -0
- package/gql/fragments/storageFragments.ts +21 -1
- package/gql/fragments/workflowFragments.ts +67 -0
- package/gql/mutations/accountMutations.ts +24 -4
- package/gql/mutations/agentDocMutations.ts +177 -0
- package/gql/mutations/agentMemoryMutations.ts +36 -0
- package/gql/mutations/aiChatMutations.ts +43 -3
- package/gql/mutations/authMutations.ts +160 -19
- package/gql/mutations/campaignMutations.ts +215 -0
- package/gql/mutations/dataTableMutations.ts +106 -0
- package/gql/mutations/fieldDocMutations.ts +24 -0
- package/gql/mutations/inboundContactMutations.ts +24 -0
- package/gql/mutations/index.ts +17 -8
- package/gql/mutations/integrationMutations.ts +25 -0
- package/gql/mutations/logMutations.ts +10 -19
- package/gql/mutations/organizationMutations.ts +160 -2
- package/gql/mutations/scheduleMutations.ts +423 -0
- package/gql/mutations/sheetMutations.ts +385 -0
- package/gql/mutations/storageMutations.ts +21 -2
- 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 +24 -1
- package/gql/queries/campaignQueries.ts +127 -0
- package/gql/queries/dataTableQueries.ts +134 -0
- package/gql/queries/emailQueries.ts +96 -0
- package/gql/queries/fieldDocQueries.ts +158 -0
- package/gql/queries/inboundContactQueries.ts +40 -0
- package/gql/queries/index.ts +17 -6
- package/gql/queries/logQueries.ts +42 -32
- package/gql/queries/organizationQueries.ts +113 -8
- package/gql/queries/scheduleQueries.ts +315 -0
- package/gql/queries/sheetQueries.ts +383 -0
- package/gql/queries/storageQueries.ts +38 -0
- package/gql/queries/workflowQueries.ts +99 -0
- package/hooks/index.ts +2104 -860
- package/hooks/use-account-mtn.ts +91 -48
- 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 +248 -97
- package/hooks/use-aiChat-qry.ts +501 -191
- package/hooks/use-app-qry.ts +14 -11
- package/hooks/use-auth-mtn.ts +410 -256
- 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 +170 -0
- package/hooks/use-dataTable-qry.ts +996 -0
- package/hooks/use-email-qry.ts +227 -0
- package/hooks/use-fieldDoc-mtn.ts +50 -0
- package/hooks/use-fieldDoc-qry.ts +180 -0
- package/hooks/use-inboundContact-mtn.ts +34 -0
- package/hooks/use-inboundContact-qry.ts +96 -0
- package/hooks/use-integration-mtn.ts +77 -0
- package/hooks/use-integration-qry.ts +30 -0
- package/hooks/use-log-mtn.ts +77 -61
- package/hooks/use-log-qry.ts +99 -72
- package/hooks/use-organization-mtn.ts +409 -80
- package/hooks/use-organization-qry.ts +211 -92
- package/hooks/use-schedule-mtn.ts +416 -0
- package/hooks/use-schedule-qry.ts +157 -0
- package/hooks/use-sheet-mtn.ts +367 -0
- package/hooks/use-sheet-qry.ts +440 -0
- package/hooks/use-storage-mtn.ts +83 -42
- package/hooks/use-storage-qry.ts +104 -13
- package/hooks/use-workflow-mtn.ts +55 -0
- package/hooks/use-workflow-qry.ts +230 -0
- package/loaders/campaign.ts +137 -0
- package/package.json +94 -52
- package/sse.ts +158 -105
- package/tsconfig.json +13 -0
- package/types.d.ts +60 -29
- package/utils.ts +77 -50
- package/gql/fragments/actionFragments.ts +0 -16
- package/gql/fragments/productFragments.ts +0 -63
- package/gql/fragments/reportFragments.ts +0 -48
- package/gql/mutations/billingMutations.ts +0 -34
- package/gql/queries/productQueries.ts +0 -70
- package/gql/queries/reportQueries.ts +0 -66
- package/hooks/use-product-qry.ts +0 -109
- package/hooks/use-report-qry.ts +0 -69
package/hooks/use-aiChat-qry.ts
CHANGED
|
@@ -1,13 +1,30 @@
|
|
|
1
1
|
import { getENVVariable } from '@jsb188/app';
|
|
2
|
-
import {
|
|
3
|
-
import { makeVariablesKey } from '@jsb188/app/utils/logic';
|
|
2
|
+
import { makeVariablesKey } from '@jsb188/app/utils/logic.ts';
|
|
4
3
|
import SSE from '@jsb188/sse';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import
|
|
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';
|
|
26
|
+
import handleSSEData, { type PublishPayload } from '../sse.ts';
|
|
27
|
+
import type { UpdateObserversFn, UseQueryParams } from '../types.d.ts';
|
|
11
28
|
|
|
12
29
|
/**
|
|
13
30
|
* Constants
|
|
@@ -16,216 +33,509 @@ import type { UpdateObserversFn, UseQueryParams } from '../types.d';
|
|
|
16
33
|
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
|
|
|
36
|
+
/**
|
|
37
|
+
* Helper; build AI chats query key
|
|
38
|
+
*/
|
|
39
|
+
|
|
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}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
19
59
|
/**
|
|
20
60
|
* Helper; get generating (status) key from SSE data
|
|
21
61
|
*/
|
|
22
62
|
|
|
23
63
|
function getGeneratingFragmentId(sseData: PublishPayload) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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,
|
|
107
|
+
) {
|
|
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];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Merge SSE payloads into local live AI chat messages.
|
|
127
|
+
*/
|
|
128
|
+
|
|
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
|
+
};
|
|
45
247
|
}
|
|
46
248
|
|
|
47
249
|
/**
|
|
48
250
|
* Fetch AI chat
|
|
49
251
|
*/
|
|
50
252
|
|
|
51
|
-
export function useAIChatWithSSE(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
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
|
+
};
|
|
156
417
|
}
|
|
157
418
|
|
|
158
419
|
/**
|
|
159
420
|
* Fetch AI chat messages
|
|
160
421
|
*/
|
|
161
422
|
|
|
162
|
-
export function useAIChatMessages(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
+
};
|
|
180
443
|
}
|
|
181
444
|
|
|
182
445
|
/**
|
|
183
446
|
* Update sidebar AI chats query
|
|
184
447
|
*/
|
|
185
448
|
|
|
186
|
-
export function updateAIChats(
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
449
|
+
export function updateAIChats(
|
|
450
|
+
aiChat: any,
|
|
451
|
+
organizationId: string | null | undefined,
|
|
452
|
+
updateObservers: UpdateObserversFn,
|
|
453
|
+
) {
|
|
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
|
+
}
|
|
209
485
|
}
|
|
210
486
|
|
|
211
487
|
/**
|
|
212
488
|
* Fetch AI chats list
|
|
213
489
|
*/
|
|
214
490
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
+
);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Get reactive AI chat fragments for a list.
|
|
534
|
+
*/
|
|
535
|
+
|
|
536
|
+
export function useReactiveAIChatFragmentMap(currentData?: any[] | null) {
|
|
537
|
+
return useReactiveFragmentMap(
|
|
538
|
+
currentData || null,
|
|
539
|
+
getFragmentBaseName(FRAGMENT_NAMES.aiChat),
|
|
540
|
+
);
|
|
231
541
|
}
|