@jsb188/graphql 1.1.11 → 1.1.12
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/audience.d.ts +343 -0
- package/cache/config.ts +58 -24
- package/cache/index.chat-activity.test.ts +119 -0
- package/cache/index.chat.test.ts +92 -0
- package/cache/index.helpers.ts +96 -0
- package/cache/index.regression.test.ts +149 -0
- package/cache/index.test.ts +370 -42
- package/cache/index.ts +326 -799
- package/cache/optimistic-ui-runtime.ts +46 -0
- package/cache/optimistic-ui.ts +401 -0
- package/cache/persist.ts +98 -19
- package/cache/store.ts +22 -64
- package/cache/value.helpers.ts +17 -0
- package/{drafts/store.ts → client/draft-persistence-indexed-db.ts} +19 -3
- package/{drafts/index.ts → client/draft-persistence.ts} +5 -2
- package/client/index.ts +3 -2
- package/client/request.helpers.ts +14 -0
- package/client/request.ts +130 -215
- package/deno.lock +17 -0
- package/deno.test.json +2 -0
- package/gql/fragments/agentMemoryFragments.ts +5 -1
- package/gql/fragments/agentNoteFragments.ts +150 -0
- package/gql/fragments/aiChatFragments.ts +36 -10
- package/gql/fragments/audienceFragments.ts +141 -0
- package/gql/fragments/campaignFragments.ts +415 -126
- package/gql/fragments/dataTableFragments.ts +48 -80
- package/gql/fragments/emailFragments.ts +28 -23
- package/gql/fragments/fieldDocFragments.ts +222 -40
- package/gql/fragments/inboundContactFragments.ts +2 -0
- package/gql/fragments/logFragments.ts +93 -2
- package/gql/fragments/organizationFragments.ts +10 -0
- package/gql/fragments/qrCodeFragments.ts +29 -0
- package/gql/fragments/scheduleFragments.ts +652 -140
- package/gql/fragments/sheetFragments.ts +84 -373
- package/gql/fragments/storageFragments.ts +25 -1
- package/gql/fragments/workflowFragments.ts +13 -1
- package/gql/mutations/agentMemoryMutations.ts +32 -0
- package/gql/mutations/agentNoteMutations.ts +62 -0
- package/gql/mutations/aiChatMutations.ts +36 -4
- package/gql/mutations/audienceMutations.ts +71 -0
- package/gql/mutations/authMutations.ts +45 -1
- package/gql/mutations/billingMutations.ts +24 -0
- package/gql/mutations/campaignMutations.ts +170 -127
- package/gql/mutations/contactMutations.ts +23 -0
- package/gql/mutations/dataTableMutations.ts +18 -75
- package/gql/mutations/fieldDocMutations.ts +162 -13
- package/gql/mutations/index.ts +8 -5
- package/gql/mutations/organizationMutations.ts +6 -0
- package/gql/mutations/qrCodeMutations.ts +20 -0
- package/gql/mutations/scheduleMutations.ts +1052 -354
- package/gql/mutations/sheetMutations.ts +62 -118
- package/gql/mutations/storageMutations.ts +0 -1
- package/gql/mutations/workflowMutations.ts +18 -0
- package/gql/queries/agentMemoryQueries.ts +6 -0
- package/gql/queries/agentNoteQueries.ts +89 -0
- package/gql/queries/aiChatQueries.ts +56 -4
- package/gql/queries/audienceQueries.ts +128 -0
- package/gql/queries/authQueries.ts +6 -0
- package/gql/queries/billingQueries.ts +95 -6
- package/gql/queries/campaignQueries.ts +132 -23
- package/gql/queries/dataTableQueries.ts +24 -65
- package/gql/queries/emailQueries.ts +18 -61
- package/gql/queries/emailReplyReportQueries.ts +76 -0
- package/gql/queries/fieldDocQueries.ts +100 -92
- package/gql/queries/inboundContactQueries.ts +4 -0
- package/gql/queries/index.ts +3 -1
- package/gql/queries/logQueries.ts +62 -7
- package/gql/queries/organizationQueries.ts +13 -0
- package/gql/queries/qrCodeQueries.ts +20 -0
- package/gql/queries/scheduleQueries.ts +633 -257
- package/gql/queries/sheetQueries.ts +59 -152
- package/gql/queries/workflowQueries.ts +10 -4
- package/hooks/index.helpers.test.ts +50 -0
- package/hooks/index.helpers.ts +52 -0
- package/hooks/index.ts +863 -685
- package/hooks/use-agentMemory-mtn.ts +29 -31
- package/hooks/use-agentMemory-qry.ts +15 -3
- package/hooks/use-agentNote-mtn.ts +82 -0
- package/hooks/use-agentNote-qry.ts +190 -0
- package/hooks/use-agentNote-recovery.helpers.test.ts +68 -0
- package/hooks/use-agentNote-recovery.helpers.ts +58 -0
- package/hooks/use-agentNote-recovery.ts +72 -0
- package/hooks/use-aiChat-mtn.ts +96 -112
- package/hooks/use-aiChat-qry.helpers.test.ts +63 -0
- package/hooks/use-aiChat-qry.helpers.ts +124 -0
- package/hooks/use-aiChat-qry.ts +229 -383
- package/hooks/use-aiChat-stream-mtn.ts +14 -0
- package/hooks/use-audience-mtn.ts +194 -0
- package/hooks/use-audience-qry.helpers.test.ts +80 -0
- package/hooks/use-audience-qry.helpers.ts +63 -0
- package/hooks/use-audience-qry.ts +330 -0
- package/hooks/use-auth-mtn.ts +30 -1
- package/hooks/use-billing-mtn.ts +31 -0
- package/hooks/use-billing-qry.ts +76 -0
- package/hooks/use-campaign-generation-recovery.ts +53 -0
- package/hooks/use-campaign-mtn.ts +224 -165
- package/hooks/use-campaign-qry.helpers.ts +40 -0
- package/hooks/use-campaign-qry.ts +631 -54
- package/hooks/use-dataTable-mtn.ts +23 -115
- package/hooks/use-dataTable-qry.ts +66 -916
- package/hooks/use-email-qry.helpers.ts +37 -0
- package/hooks/use-email-qry.ts +33 -178
- package/hooks/use-emailCampaignReplyReport-qry.ts +27 -0
- package/hooks/use-fieldDoc-mtn.ts +244 -32
- package/hooks/use-fieldDoc-qry.helpers.test.ts +65 -0
- package/hooks/use-fieldDoc-qry.helpers.ts +49 -0
- package/hooks/use-fieldDoc-qry.ts +134 -102
- package/hooks/use-inboundContact-qry.ts +7 -9
- package/hooks/use-log-mtn.ts +25 -12
- package/hooks/use-log-qry.ts +99 -46
- package/hooks/use-organization-qry.ts +16 -60
- package/hooks/use-publicFormEmail-mtn.ts +27 -0
- package/hooks/use-qrCode-mtn.ts +40 -0
- package/hooks/use-qrCode-qry.ts +78 -0
- package/hooks/use-schedule-mtn.ts +506 -333
- package/hooks/use-schedule-qry.ts +682 -101
- package/hooks/use-schedule-reactive.ts +486 -0
- package/hooks/use-sheet-mtn.ts +91 -199
- package/hooks/use-sheet-qry.ts +84 -238
- package/hooks/use-storage-qry.ts +84 -61
- package/hooks/use-workflow-mtn.ts +39 -22
- package/hooks/use-workflow-qry.ts +40 -61
- package/libs/ai-chat-outgoing.helpers.ts +153 -0
- package/libs/ai-chat-outgoing.ts +11 -0
- package/libs/ai-chat-outgoing.types.ts +25 -0
- package/libs/ai-chat-run-realtime.ts +20 -0
- package/libs/ai-chat-submission-realtime.ts +150 -0
- package/libs/fragment-realtime-sequence.ts +55 -0
- package/libs/fragment-recovery.test.ts +326 -0
- package/libs/fragment-recovery.ts +325 -0
- package/libs/open-sheet.ts +138 -0
- package/libs/orchestrator-realtime.helpers.test.ts +229 -0
- package/libs/orchestrator-realtime.helpers.ts +317 -0
- package/libs/orchestrator-realtime.ts +433 -0
- package/libs/request-cache-regression.test.ts +122 -0
- package/{client → libs}/request-managed-session.test.ts +1 -1
- package/libs/request-no-store.test.ts +419 -0
- package/libs/sheet-command-batch.ts +44 -0
- package/libs/sheet-view-realtime-router.test.ts +214 -0
- package/libs/sheet-view-realtime-router.ts +99 -0
- package/loaders/campaign.test.ts +186 -0
- package/loaders/campaign.ts +56 -83
- package/loaders/emailPreference.ts +75 -0
- package/loaders/rest.ts +12 -0
- package/package.json +42 -17
- package/schedule.d.ts +1790 -0
- package/sse.helpers.ts +19 -0
- package/sse.ts +26 -115
- package/tests/agent-note-detail-cache.test.ts +120 -0
- package/tests/agent-note-search.test.ts +444 -0
- package/types.d.ts +46 -25
- package/client/data-table-realtime-subscriptions.test.ts +0 -740
- package/client/data-table-realtime-subscriptions.ts +0 -955
- package/client/data-table-realtime.test.ts +0 -792
- package/client/data-table-realtime.ts +0 -1218
- package/client/fragment-realtime-sequence.ts +0 -52
- package/client/open-sheet.ts +0 -137
- package/client/request-no-store.test.ts +0 -157
- package/gql/fragments/agentDocFragments.ts +0 -46
- package/gql/fragments/billingFragments.ts +0 -14
- package/gql/mutations/agentDocMutations.ts +0 -177
- package/gql/queries/agentDocQueries.ts +0 -120
- package/hooks/use-agentDoc-mtn.ts +0 -285
- package/hooks/use-agentDoc-qry.ts +0 -208
- /package/{client → libs}/sse-ticket.test.ts +0 -0
|
@@ -1,25 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
useReactiveFragmentMap,
|
|
5
|
-
} from '@jsb188/graphql/client';
|
|
6
|
-
import type {
|
|
7
|
-
WorkflowRunsFilterGQL,
|
|
8
|
-
WorkflowsFilterGQL,
|
|
9
|
-
} from '@jsb188/mday/types/workflow.d.ts';
|
|
10
|
-
import {
|
|
11
|
-
FRAGMENT_NAMES,
|
|
12
|
-
getFragmentBaseName,
|
|
13
|
-
makeFragmentKey,
|
|
14
|
-
} from '@jsb188/mday/constants/graphqlReactivity.ts';
|
|
1
|
+
import { useQuery, useReactiveFragment, useReactiveFragmentMap } from '@jsb188/graphql/client';
|
|
2
|
+
import type { WorkflowRunsFilterGQL, WorkflowsFilterGQL } from '@jsb188/mday/types/workflow.d.ts';
|
|
3
|
+
import { FRAGMENT_NAMES, getFragmentBaseName, makeFragmentKey } from '@jsb188/mday/constants/graphqlReactivity.ts';
|
|
15
4
|
import { useCallback } from 'react';
|
|
16
|
-
import {
|
|
17
|
-
workflowRunQry,
|
|
18
|
-
workflowRunsQry,
|
|
19
|
-
workflowsQry,
|
|
20
|
-
} from '../gql/queries/workflowQueries.ts';
|
|
5
|
+
import { workflowQry, workflowRunQry, workflowRunsQry, workflowsQry } from '../gql/queries/workflowQueries.ts';
|
|
21
6
|
import type { PaginationArgs, UseQueryParams } from '../types.d.ts';
|
|
22
7
|
|
|
8
|
+
/*
|
|
9
|
+
* Reuse the requested reactive workflow from cache and only query when it is absent.
|
|
10
|
+
*/
|
|
11
|
+
export function useWorkflow(workflowId: string, params: UseQueryParams = {}) {
|
|
12
|
+
// Read the reactive fragment first; it already loads and observes the workflow in cache.
|
|
13
|
+
// A separate loadFragment() call would duplicate that cache lookup.
|
|
14
|
+
const observedWorkflow = useReactiveWorkflowFragment(workflowId);
|
|
15
|
+
// The observer can retain the previous workflow until its effect runs after an ID change.
|
|
16
|
+
// Only the requested workflow may satisfy this read or prevent its query from running.
|
|
17
|
+
const reactiveWorkflow = observedWorkflow?.id === workflowId ? observedWorkflow : null;
|
|
18
|
+
// Always call the query hook to preserve hook order, but skip its automatic request when
|
|
19
|
+
// the requested workflow is cached, its ID is absent, or the caller explicitly skips.
|
|
20
|
+
const { data, ...rest } = useQuery(workflowQry, {
|
|
21
|
+
...params,
|
|
22
|
+
variables: { workflowId },
|
|
23
|
+
skip: !!reactiveWorkflow || !workflowId || params.skip,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
// Prefer the reactive cache; otherwise use a matching query result while the cache observer catches up.
|
|
28
|
+
workflow: reactiveWorkflow || (data?.workflow?.id === workflowId ? data.workflow : null),
|
|
29
|
+
...rest,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
23
33
|
/**
|
|
24
34
|
* Constants
|
|
25
35
|
*/
|
|
@@ -31,10 +41,9 @@ const WORKFLOW_LISTS_LIMIT = 60;
|
|
|
31
41
|
* (route table filters carry extra URL-only keys)
|
|
32
42
|
*/
|
|
33
43
|
|
|
34
|
-
function normalizeWorkflowsFilter(
|
|
35
|
-
filter?: Record<string, any> | null,
|
|
36
|
-
): WorkflowsFilterGQL {
|
|
44
|
+
function normalizeWorkflowsFilter(filter?: Record<string, any> | null): WorkflowsFilterGQL {
|
|
37
45
|
return {
|
|
46
|
+
activeOnly: filter?.activeOnly === true,
|
|
38
47
|
kind: filter?.kind || null,
|
|
39
48
|
startDate: filter?.startDate || null,
|
|
40
49
|
endDate: filter?.endDate || null,
|
|
@@ -45,9 +54,7 @@ function normalizeWorkflowsFilter(
|
|
|
45
54
|
* Keep run list filters aligned with the WorkflowRunsFilter input shape
|
|
46
55
|
*/
|
|
47
56
|
|
|
48
|
-
function normalizeWorkflowRunsFilter(
|
|
49
|
-
filter?: Record<string, any> | null,
|
|
50
|
-
): WorkflowRunsFilterGQL {
|
|
57
|
+
function normalizeWorkflowRunsFilter(filter?: Record<string, any> | null): WorkflowRunsFilterGQL {
|
|
51
58
|
return {
|
|
52
59
|
workflowId: filter?.workflowId || null,
|
|
53
60
|
kind: filter?.kind || null,
|
|
@@ -108,10 +115,7 @@ export function useWorkflows(
|
|
|
108
115
|
skip: !variables.organizationId || params.skip,
|
|
109
116
|
});
|
|
110
117
|
|
|
111
|
-
const fetchMoreWorkflows = useNormalizedFilterFetchMore(
|
|
112
|
-
fetchMore,
|
|
113
|
-
normalizeWorkflowsFilter,
|
|
114
|
-
);
|
|
118
|
+
const fetchMoreWorkflows = useNormalizedFilterFetchMore(fetchMore, normalizeWorkflowsFilter);
|
|
115
119
|
|
|
116
120
|
return {
|
|
117
121
|
workflows: data?.workflows,
|
|
@@ -145,10 +149,7 @@ export function useWorkflowRuns(
|
|
|
145
149
|
skip: !variables.organizationId || params.skip,
|
|
146
150
|
});
|
|
147
151
|
|
|
148
|
-
const fetchMoreWorkflowRuns = useNormalizedFilterFetchMore(
|
|
149
|
-
fetchMore,
|
|
150
|
-
normalizeWorkflowRunsFilter,
|
|
151
|
-
);
|
|
152
|
+
const fetchMoreWorkflowRuns = useNormalizedFilterFetchMore(fetchMore, normalizeWorkflowRunsFilter);
|
|
152
153
|
|
|
153
154
|
return {
|
|
154
155
|
workflowRuns: data?.workflowRuns,
|
|
@@ -175,10 +176,7 @@ export function useWorkflowRun(
|
|
|
175
176
|
});
|
|
176
177
|
|
|
177
178
|
// Always query; the cached list fragment does not carry the nested workflow
|
|
178
|
-
const reactiveWorkflowRun = useReactiveWorkflowRunFragment(
|
|
179
|
-
variables.workflowRunId || '',
|
|
180
|
-
data?.workflowRun ?? null,
|
|
181
|
-
);
|
|
179
|
+
const reactiveWorkflowRun = useReactiveWorkflowRunFragment(variables.workflowRunId || '', data?.workflowRun ?? null);
|
|
182
180
|
|
|
183
181
|
return {
|
|
184
182
|
workflowRun: reactiveWorkflowRun || data?.workflowRun,
|
|
@@ -190,32 +188,16 @@ export function useWorkflowRun(
|
|
|
190
188
|
* Get reactive workflow fragment
|
|
191
189
|
*/
|
|
192
190
|
|
|
193
|
-
export function useReactiveWorkflowFragment(
|
|
194
|
-
workflowId
|
|
195
|
-
currentData?: any,
|
|
196
|
-
queryCount?: number,
|
|
197
|
-
) {
|
|
198
|
-
return useReactiveFragment(
|
|
199
|
-
currentData,
|
|
200
|
-
[makeFragmentKey(FRAGMENT_NAMES.workflow, workflowId)],
|
|
201
|
-
queryCount,
|
|
202
|
-
);
|
|
191
|
+
export function useReactiveWorkflowFragment(workflowId: string, currentData?: any, queryCount?: number) {
|
|
192
|
+
return useReactiveFragment(currentData, [makeFragmentKey(FRAGMENT_NAMES.workflow, workflowId)], queryCount);
|
|
203
193
|
}
|
|
204
194
|
|
|
205
195
|
/**
|
|
206
196
|
* Get reactive workflow run fragment for a single run
|
|
207
197
|
*/
|
|
208
198
|
|
|
209
|
-
export function useReactiveWorkflowRunFragment(
|
|
210
|
-
workflowRunId
|
|
211
|
-
currentData?: any,
|
|
212
|
-
queryCount?: number,
|
|
213
|
-
) {
|
|
214
|
-
return useReactiveFragment(
|
|
215
|
-
currentData,
|
|
216
|
-
[makeFragmentKey(FRAGMENT_NAMES.workflowRun, workflowRunId)],
|
|
217
|
-
queryCount,
|
|
218
|
-
);
|
|
199
|
+
export function useReactiveWorkflowRunFragment(workflowRunId: string, currentData?: any, queryCount?: number) {
|
|
200
|
+
return useReactiveFragment(currentData, [makeFragmentKey(FRAGMENT_NAMES.workflowRun, workflowRunId)], queryCount);
|
|
219
201
|
}
|
|
220
202
|
|
|
221
203
|
/**
|
|
@@ -223,8 +205,5 @@ export function useReactiveWorkflowRunFragment(
|
|
|
223
205
|
*/
|
|
224
206
|
|
|
225
207
|
export function useReactiveWorkflowRunFragmentMap(workflowRuns?: any[] | null) {
|
|
226
|
-
return useReactiveFragmentMap(
|
|
227
|
-
workflowRuns || null,
|
|
228
|
-
getFragmentBaseName(FRAGMENT_NAMES.workflowRun),
|
|
229
|
-
);
|
|
208
|
+
return useReactiveFragmentMap(workflowRuns || null, getFragmentBaseName(FRAGMENT_NAMES.workflowRun));
|
|
230
209
|
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { ServerErrorObj } from '@jsb188/app/types/app.d.ts';
|
|
2
|
+
import { releaseObjectUrl, retainObjectUrl } from '@jsb188/app/utils/url_client.ts';
|
|
3
|
+
import { getOptimisticAIChatMessageAttachments, type OptimisticAIChatAttachmentInput } from '@jsb188/mday/utils/storage.ts';
|
|
4
|
+
import { clearAIChatSubmission, registerAIChatSubmission } from './ai-chat-submission-realtime.ts';
|
|
5
|
+
import type { AIChatOutgoingMessage, AIChatSendVariables } from './ai-chat-outgoing.types.ts';
|
|
6
|
+
|
|
7
|
+
export type { AIChatOutgoingMessage } from './ai-chat-outgoing.types.ts';
|
|
8
|
+
|
|
9
|
+
const OUTGOING = {
|
|
10
|
+
messages: new Map<string, AIChatOutgoingMessage[]>(),
|
|
11
|
+
listeners: new Map<string, Set<() => void>>(),
|
|
12
|
+
empty: [] as AIChatOutgoingMessage[],
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/* Publish immutable snapshots independently of the composer's mount lifetime. */
|
|
16
|
+
function publishOutgoingMessages(aiChatId: string, messages: AIChatOutgoingMessage[]) {
|
|
17
|
+
if (messages.length) OUTGOING.messages.set(aiChatId, messages);
|
|
18
|
+
else OUTGOING.messages.delete(aiChatId);
|
|
19
|
+
OUTGOING.listeners.get(aiChatId)?.forEach((listener) => {
|
|
20
|
+
listener();
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/* Read pending messages without inventing server cache entries or pagination IDs. */
|
|
25
|
+
export function getAIChatOutgoingMessages(aiChatId?: string) {
|
|
26
|
+
return (aiChatId && OUTGOING.messages.get(aiChatId)) || OUTGOING.empty;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* Observe tab-local sends; unmounting a thread does not discard an unfinished send. */
|
|
30
|
+
export function subscribeToAIChatOutgoingMessages(aiChatId: string | undefined, listener: () => void) {
|
|
31
|
+
if (!aiChatId) return () => {};
|
|
32
|
+
const listeners = OUTGOING.listeners.get(aiChatId) || new Set();
|
|
33
|
+
OUTGOING.listeners.set(aiChatId, listeners);
|
|
34
|
+
listeners.add(listener);
|
|
35
|
+
return () => {
|
|
36
|
+
listeners.delete(listener);
|
|
37
|
+
if (!listeners.size) OUTGOING.listeners.delete(aiChatId);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/* Snapshot one existing-chat submission and retain its previews before clearing the editor. */
|
|
42
|
+
export function beginAIChatOutgoingMessage(p: {
|
|
43
|
+
aiChatId: string;
|
|
44
|
+
accountId: string;
|
|
45
|
+
variables: AIChatSendVariables;
|
|
46
|
+
attachments: OptimisticAIChatAttachmentInput[];
|
|
47
|
+
qrCodes?: AIChatOutgoingMessage['qrCodes'];
|
|
48
|
+
}) {
|
|
49
|
+
const messages = getAIChatOutgoingMessages(p.aiChatId);
|
|
50
|
+
if (messages.some((message) => message.deliveryStatus === 'SENDING')) return false;
|
|
51
|
+
const variables = structuredClone(p.variables);
|
|
52
|
+
const at = new Date().toISOString();
|
|
53
|
+
const previewUrls = p.attachments.flatMap((attachment) => (attachment.previewUrl ? [attachment.previewUrl] : []));
|
|
54
|
+
previewUrls.forEach(retainObjectUrl);
|
|
55
|
+
const message: AIChatOutgoingMessage = {
|
|
56
|
+
id: `pending:${variables.clientRequestId}`,
|
|
57
|
+
clientRequestId: variables.clientRequestId,
|
|
58
|
+
accountId: p.accountId,
|
|
59
|
+
type: 'MESSAGE',
|
|
60
|
+
text: variables.text?.trim() || '',
|
|
61
|
+
at,
|
|
62
|
+
cursor: null,
|
|
63
|
+
finished: true,
|
|
64
|
+
attachments: getOptimisticAIChatMessageAttachments(p.attachments, at),
|
|
65
|
+
qrCodes: p.qrCodes ? structuredClone(p.qrCodes) : undefined,
|
|
66
|
+
deliveryStatus: 'SENDING',
|
|
67
|
+
variables,
|
|
68
|
+
previewUrls,
|
|
69
|
+
};
|
|
70
|
+
registerAIChatSubmission(p.aiChatId, variables.clientRequestId);
|
|
71
|
+
publishOutgoingMessages(p.aiChatId, [...messages, message]);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* Replay the original immutable payload, preserving the server's idempotency key. */
|
|
76
|
+
export function retryAIChatOutgoingMessage(aiChatId: string, clientRequestId: string) {
|
|
77
|
+
const messages = getAIChatOutgoingMessages(aiChatId);
|
|
78
|
+
const message = messages.find((item) => item.clientRequestId === clientRequestId);
|
|
79
|
+
if (message?.deliveryStatus !== 'FAILED' || messages.some((item) => item.deliveryStatus === 'SENDING')) return null;
|
|
80
|
+
registerAIChatSubmission(aiChatId, clientRequestId);
|
|
81
|
+
publishOutgoingMessages(
|
|
82
|
+
aiChatId,
|
|
83
|
+
messages.map((item) => (item === message ? { ...item, deliveryStatus: 'SENDING', error: null } : item)),
|
|
84
|
+
);
|
|
85
|
+
return structuredClone(message.variables);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* Keep a recoverable row on failure without overwriting anything typed into the next draft. */
|
|
89
|
+
export function failAIChatOutgoingMessage(aiChatId: string, clientRequestId: string, error?: ServerErrorObj | null) {
|
|
90
|
+
const messages = getAIChatOutgoingMessages(aiChatId);
|
|
91
|
+
const message = messages.find((item) => item.clientRequestId === clientRequestId);
|
|
92
|
+
if (!message || message.deliveryStatus === 'ACCEPTED') return;
|
|
93
|
+
clearAIChatSubmission(aiChatId, clientRequestId);
|
|
94
|
+
publishOutgoingMessages(
|
|
95
|
+
aiChatId,
|
|
96
|
+
messages.map((item) => (item === message ? { ...item, deliveryStatus: 'FAILED', error } : item)),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* Remove pending opacity only after the server returns canonical message data. */
|
|
101
|
+
export function acceptAIChatOutgoingMessage(aiChatId: string, clientRequestId: string, canonicalMessage: any) {
|
|
102
|
+
const messages = getAIChatOutgoingMessages(aiChatId);
|
|
103
|
+
const message = messages.find((item) => item.clientRequestId === clientRequestId);
|
|
104
|
+
if (!message || !canonicalMessage?.id) return;
|
|
105
|
+
publishOutgoingMessages(
|
|
106
|
+
aiChatId,
|
|
107
|
+
messages.map((item) =>
|
|
108
|
+
item === message
|
|
109
|
+
? {
|
|
110
|
+
...item,
|
|
111
|
+
...canonicalMessage,
|
|
112
|
+
clientRequestId,
|
|
113
|
+
deliveryStatus: 'ACCEPTED',
|
|
114
|
+
error: null,
|
|
115
|
+
}
|
|
116
|
+
: item,
|
|
117
|
+
),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/* Release one locally retained payload after canonical handoff or explicit dismissal. */
|
|
122
|
+
export function dismissAIChatOutgoingMessage(aiChatId: string, clientRequestId: string) {
|
|
123
|
+
const messages = getAIChatOutgoingMessages(aiChatId);
|
|
124
|
+
const message = messages.find((item) => item.clientRequestId === clientRequestId);
|
|
125
|
+
if (!message) return;
|
|
126
|
+
clearAIChatSubmission(aiChatId, clientRequestId);
|
|
127
|
+
message.previewUrls.forEach(releaseObjectUrl);
|
|
128
|
+
publishOutgoingMessages(
|
|
129
|
+
aiChatId,
|
|
130
|
+
messages.filter((item) => item !== message),
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/* Retire only submissions whose canonical rows have entered the thread's latest message data. */
|
|
135
|
+
export function reconcileAIChatOutgoingMessages(aiChatId: string, messages: { id: string; clientRequestId?: string | null }[]) {
|
|
136
|
+
const ids = new Set(messages.map((message) => message.id));
|
|
137
|
+
const requestIds = new Set(messages.map((message) => message.clientRequestId).filter(Boolean));
|
|
138
|
+
for (const message of getAIChatOutgoingMessages(aiChatId)) {
|
|
139
|
+
if (ids.has(message.id) || requestIds.has(message.clientRequestId))
|
|
140
|
+
dismissAIChatOutgoingMessage(aiChatId, message.clientRequestId);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* Clear private outgoing content and local preview holds when the app clears its authenticated cache. */
|
|
145
|
+
export function clearAIChatOutgoingMessages() {
|
|
146
|
+
for (const [aiChatId, messages] of OUTGOING.messages) {
|
|
147
|
+
clearAIChatSubmission(aiChatId);
|
|
148
|
+
messages.forEach((message) => {
|
|
149
|
+
message.previewUrls.forEach(releaseObjectUrl);
|
|
150
|
+
});
|
|
151
|
+
publishOutgoingMessages(aiChatId, []);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useCallback, useSyncExternalStore } from 'react';
|
|
2
|
+
import { getAIChatOutgoingMessages, subscribeToAIChatOutgoingMessages } from './ai-chat-outgoing.helpers.ts';
|
|
3
|
+
|
|
4
|
+
export * from './ai-chat-outgoing.helpers.ts';
|
|
5
|
+
|
|
6
|
+
/* Subscribe to one existing chat's outgoing rows using immutable store snapshots. */
|
|
7
|
+
export function useAIChatOutgoingMessages(aiChatId?: string) {
|
|
8
|
+
const subscribe = useCallback((listener: () => void) => subscribeToAIChatOutgoingMessages(aiChatId, listener), [aiChatId]);
|
|
9
|
+
const getSnapshot = useCallback(() => getAIChatOutgoingMessages(aiChatId), [aiChatId]);
|
|
10
|
+
return useSyncExternalStore(subscribe, getSnapshot, getAIChatOutgoingMessages);
|
|
11
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { QrCodeJSONValue } from '@jsb188/mday/types/qrCode.d.ts';
|
|
2
|
+
import type { ServerErrorObj } from '@jsb188/app/types/app.d.ts';
|
|
3
|
+
import type { getOptimisticAIChatMessageAttachments } from '@jsb188/mday/utils/storage.ts';
|
|
4
|
+
|
|
5
|
+
export interface AIChatSendVariables extends Record<string, any> {
|
|
6
|
+
aiChatId: string;
|
|
7
|
+
clientRequestId: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface AIChatOutgoingMessage {
|
|
11
|
+
qrCodes?: Record<string, QrCodeJSONValue>[];
|
|
12
|
+
id: string;
|
|
13
|
+
clientRequestId: string;
|
|
14
|
+
accountId: string;
|
|
15
|
+
type: 'MESSAGE';
|
|
16
|
+
text: string;
|
|
17
|
+
at: string;
|
|
18
|
+
cursor: string | null;
|
|
19
|
+
finished: boolean;
|
|
20
|
+
attachments: ReturnType<typeof getOptimisticAIChatMessageAttachments>;
|
|
21
|
+
deliveryStatus: 'SENDING' | 'FAILED' | 'ACCEPTED';
|
|
22
|
+
variables: AIChatSendVariables;
|
|
23
|
+
error?: ServerErrorObj | null;
|
|
24
|
+
previewUrls: string[];
|
|
25
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AIChatRunData } from '@jsb188/app/types/ai.d.ts';
|
|
2
|
+
import { FRAGMENT_NAMES, makeFragmentKey } from '@jsb188/mday/constants/graphqlReactivity.ts';
|
|
3
|
+
import { mergeAIChatRuns } from '@jsb188/mday/utils/aiChatWork.ts';
|
|
4
|
+
import { addFragmentToCache, loadFragment, updateFragment } from '../cache/index.ts';
|
|
5
|
+
import type { UpdateObserversFn } from '../types.d.ts';
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* Persist authoritative timing and source associations so returning to a cached
|
|
9
|
+
* chat cannot restart its timer. Older stream snapshots cannot reopen a run.
|
|
10
|
+
*/
|
|
11
|
+
export function cacheAIChatRunUpdate(run: AIChatRunData, updateObservers: UpdateObserversFn) {
|
|
12
|
+
const runKey = makeFragmentKey(FRAGMENT_NAMES.aiChatRun, run.id);
|
|
13
|
+
const previousRun = loadFragment(runKey);
|
|
14
|
+
const canonicalRun = mergeAIChatRuns(previousRun ? { [run.id]: previousRun } : {}, [run])[run.id];
|
|
15
|
+
if (!updateFragment(runKey, canonicalRun, null, false, updateObservers)) addFragmentToCache(runKey, canonicalRun);
|
|
16
|
+
for (const sourceId of canonicalRun.sourceMessageIds) {
|
|
17
|
+
updateFragment(makeFragmentKey(FRAGMENT_NAMES.aiChatMessage, sourceId), { run: canonicalRun }, null, false, updateObservers);
|
|
18
|
+
}
|
|
19
|
+
return canonicalRun;
|
|
20
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import type { AIChatSubmissionWSObj } from '@jsb188/app/types/app.d.ts';
|
|
2
|
+
|
|
3
|
+
interface AIChatSubmissionState {
|
|
4
|
+
acceptReceived: boolean;
|
|
5
|
+
clientRequestId: string;
|
|
6
|
+
generationActive: boolean;
|
|
7
|
+
listeners: Set<(waitingForGeneration: boolean) => void>;
|
|
8
|
+
serverReceived: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const AI_CHAT_SUBMISSION_STATES = new Map<string, AIChatSubmissionState>();
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
* Return whether one chat should render its transient server-received placeholder.
|
|
15
|
+
*/
|
|
16
|
+
function isWaitingForAIChatGeneration(state: AIChatSubmissionState) {
|
|
17
|
+
return (
|
|
18
|
+
state.acceptReceived && state.serverReceived && !state.generationActive
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/*
|
|
23
|
+
* Notify every mounted observer when one chat's transient submission phase changes.
|
|
24
|
+
*/
|
|
25
|
+
function notifyAIChatSubmissionState(state: AIChatSubmissionState) {
|
|
26
|
+
const waitingForGeneration = isWaitingForAIChatGeneration(state);
|
|
27
|
+
state.listeners.forEach((listener) => {
|
|
28
|
+
listener(waitingForGeneration);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* Register the request identity that the requester tab expects the server to acknowledge.
|
|
34
|
+
*/
|
|
35
|
+
export function registerAIChatSubmission(
|
|
36
|
+
aiChatId: string,
|
|
37
|
+
clientRequestId: string,
|
|
38
|
+
) {
|
|
39
|
+
const previousState = AI_CHAT_SUBMISSION_STATES.get(aiChatId);
|
|
40
|
+
const state: AIChatSubmissionState = {
|
|
41
|
+
acceptReceived: true,
|
|
42
|
+
clientRequestId,
|
|
43
|
+
generationActive: previousState?.generationActive || false,
|
|
44
|
+
listeners: previousState?.listeners || new Set(),
|
|
45
|
+
serverReceived: false,
|
|
46
|
+
};
|
|
47
|
+
AI_CHAT_SUBMISSION_STATES.set(aiChatId, state);
|
|
48
|
+
notifyAIChatSubmissionState(state);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* Apply one server acknowledgement only when it matches the current request in this tab.
|
|
53
|
+
*/
|
|
54
|
+
export function receiveAIChatSubmission(update: AIChatSubmissionWSObj) {
|
|
55
|
+
const state = AI_CHAT_SUBMISSION_STATES.get(update.aiChatId);
|
|
56
|
+
if (
|
|
57
|
+
!state ||
|
|
58
|
+
state.clientRequestId !== update.clientRequestId ||
|
|
59
|
+
update.phase !== 'RECEIVED'
|
|
60
|
+
) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (state.generationActive) {
|
|
64
|
+
state.acceptReceived = false;
|
|
65
|
+
state.serverReceived = false;
|
|
66
|
+
notifyAIChatSubmissionState(state);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
state.serverReceived = true;
|
|
71
|
+
notifyAIChatSubmissionState(state);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Supersede the transient server acknowledgement before exposing real generation state.
|
|
76
|
+
*/
|
|
77
|
+
export function startAIChatGeneration(aiChatId: string) {
|
|
78
|
+
const state = AI_CHAT_SUBMISSION_STATES.get(aiChatId);
|
|
79
|
+
if (!state) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
state.acceptReceived = false;
|
|
84
|
+
state.generationActive = true;
|
|
85
|
+
state.serverReceived = false;
|
|
86
|
+
notifyAIChatSubmissionState(state);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/*
|
|
90
|
+
* Record that real generation ended without allowing an older acknowledgement to reappear.
|
|
91
|
+
*/
|
|
92
|
+
export function finishAIChatGeneration(aiChatId: string) {
|
|
93
|
+
const state = AI_CHAT_SUBMISSION_STATES.get(aiChatId);
|
|
94
|
+
if (!state) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
state.generationActive = false;
|
|
99
|
+
notifyAIChatSubmissionState(state);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/*
|
|
103
|
+
* Clear one failed or abandoned submission while retaining mounted chat observers.
|
|
104
|
+
*/
|
|
105
|
+
export function clearAIChatSubmission(
|
|
106
|
+
aiChatId: string,
|
|
107
|
+
clientRequestId?: string | null,
|
|
108
|
+
) {
|
|
109
|
+
const state = AI_CHAT_SUBMISSION_STATES.get(aiChatId);
|
|
110
|
+
if (
|
|
111
|
+
!state ||
|
|
112
|
+
(clientRequestId && state.clientRequestId !== clientRequestId)
|
|
113
|
+
) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
state.acceptReceived = false;
|
|
118
|
+
state.serverReceived = false;
|
|
119
|
+
notifyAIChatSubmissionState(state);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/*
|
|
123
|
+
* Observe the requester-tab loading phase for one mounted AI chat.
|
|
124
|
+
*/
|
|
125
|
+
export function subscribeToAIChatSubmission(
|
|
126
|
+
aiChatId: string,
|
|
127
|
+
listener: (waitingForGeneration: boolean) => void,
|
|
128
|
+
) {
|
|
129
|
+
let state = AI_CHAT_SUBMISSION_STATES.get(aiChatId);
|
|
130
|
+
if (!state) {
|
|
131
|
+
state = {
|
|
132
|
+
acceptReceived: false,
|
|
133
|
+
clientRequestId: '',
|
|
134
|
+
generationActive: false,
|
|
135
|
+
listeners: new Set(),
|
|
136
|
+
serverReceived: false,
|
|
137
|
+
};
|
|
138
|
+
AI_CHAT_SUBMISSION_STATES.set(aiChatId, state);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
state.listeners.add(listener);
|
|
142
|
+
listener(isWaitingForAIChatGeneration(state));
|
|
143
|
+
|
|
144
|
+
return () => {
|
|
145
|
+
state.listeners.delete(listener);
|
|
146
|
+
if (!state.listeners.size) {
|
|
147
|
+
AI_CHAT_SUBMISSION_STATES.delete(aiChatId);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const FRAGMENT_REALTIME_SEQUENCES = new Map<string, string>();
|
|
2
|
+
let fragmentRealtimeEpoch = 0;
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Compare two unsigned decimal sequence strings without narrowing them to
|
|
6
|
+
* JavaScript's unsafe integer range.
|
|
7
|
+
*/
|
|
8
|
+
function compareFragmentRealtimeSequences(left: string, right: string) {
|
|
9
|
+
const normalizedLeft = left.replace(/^0+(?=\d)/, '');
|
|
10
|
+
const normalizedRight = right.replace(/^0+(?=\d)/, '');
|
|
11
|
+
|
|
12
|
+
if (normalizedLeft.length !== normalizedRight.length) {
|
|
13
|
+
return normalizedLeft.length > normalizedRight.length ? 1 : -1;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return normalizedLeft === normalizedRight ? 0 : normalizedLeft > normalizedRight ? 1 : -1;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
* Check ordering without advancing the fence while an oversized fragment is
|
|
21
|
+
* still being recovered through an authorized single-record read.
|
|
22
|
+
*/
|
|
23
|
+
export function isFragmentRealtimeSequenceCurrent(fragmentKey: string, sequence?: string) {
|
|
24
|
+
if (!sequence || !/^\d+$/.test(sequence)) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const currentSequence = FRAGMENT_REALTIME_SEQUENCES.get(fragmentKey);
|
|
29
|
+
if (currentSequence && compareFragmentRealtimeSequences(sequence, currentSequence) < 0) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* Accept an update and advance its sequence only when its data can be applied. */
|
|
37
|
+
export function shouldApplyFragmentRealtimeSequence(fragmentKey: string, sequence?: string) {
|
|
38
|
+
if (!isFragmentRealtimeSequenceCurrent(fragmentKey, sequence)) return false;
|
|
39
|
+
if (sequence && /^\d+$/.test(sequence)) FRAGMENT_REALTIME_SEQUENCES.set(fragmentKey, sequence);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/* Bind asynchronous fragment recovery to the current authorization/cache lifetime. */
|
|
44
|
+
export function getFragmentRealtimeEpoch() {
|
|
45
|
+
return fragmentRealtimeEpoch;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/*
|
|
49
|
+
* Clear fragment sequence fences when authorization-bound cache state is
|
|
50
|
+
* discarded.
|
|
51
|
+
*/
|
|
52
|
+
export function clearFragmentRealtimeSequences() {
|
|
53
|
+
fragmentRealtimeEpoch += 1;
|
|
54
|
+
FRAGMENT_REALTIME_SEQUENCES.clear();
|
|
55
|
+
}
|