@jsb188/ai-chat 0.0.0-stage → 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/README.md +109 -2
- package/VERIFICATION.md +118 -0
- package/benchmarks/browser.mjs +204 -0
- package/benchmarks/streaming.mjs +89 -0
- package/package.json +36 -5
- package/src/main.ts +5 -0
- package/src/modules/AIChatContent.tsx +84 -0
- package/src/modules/AgentNoteActionsMenu.tsx +121 -0
- package/src/modules/AgentNoteBootstrapProgress.tsx +41 -0
- package/src/modules/AgentNoteItem.helpers.ts +115 -0
- package/src/modules/AgentNoteItem.tsx +232 -0
- package/src/modules/AgentNotes.constants.ts +10 -0
- package/src/modules/AgentNotes.helpers.ts +274 -0
- package/src/modules/AgentNotes.hooks.ts +449 -0
- package/src/modules/AgentNotes.tsx +74 -0
- package/src/modules/AgentNotes.types.ts +50 -0
- package/src/modules/AgentNotesCollection.hooks.ts +69 -0
- package/src/modules/AgentNotesSection.tsx +145 -0
- package/src/parser.ts +4 -0
- package/src/types/content.ts +103 -0
- package/src/ui/AIChatUI.css +557 -0
- package/src/ui/AIChatUI.tsx +816 -0
- package/src/ui/AgentNotesUI.tsx +375 -0
- package/src/ui/AgentNotesUI.types.ts +19 -0
- package/src/ui/ChatBlocks.tsx +92 -0
- package/src/ui/ChatCodeBlock.tsx +16 -0
- package/src/ui/ChatInline.helpers.ts +20 -0
- package/src/ui/ChatInline.tsx +89 -0
- package/src/ui/ChatTable.helpers.ts +37 -0
- package/src/ui/ChatTable.tsx +126 -0
- package/src/utils/contentIdentity.ts +108 -0
- package/src/utils/parseBlocks.helpers.ts +50 -0
- package/src/utils/parseBlocks.ts +266 -0
- package/src/utils/parseContent.helpers.ts +102 -0
- package/src/utils/parseContent.ts +33 -0
- package/src/utils/parseContentIncrementally.ts +46 -0
- package/src/utils/parseCsv.ts +99 -0
- package/src/utils/parseInline.helpers.ts +48 -0
- package/src/utils/parseInline.ts +396 -0
- package/src/utils/parseTable.ts +202 -0
- package/tests/AgentNotes.test.tsx +1672 -0
- package/tests/fixtures/content.ts +13 -0
- package/tests/parseContent.test.ts +227 -0
- package/tests/parseCsv.test.ts +82 -0
- package/tests/parseInline.test.ts +222 -0
- package/tests/renderContent.ssr.test.tsx +19 -0
- package/tests/renderContent.test.tsx +282 -0
- package/tests/streaming.test.ts +51 -0
- package/tests/streamingEquivalence.test.ts +265 -0
- package/tsconfig.json +16 -0
- package/vitest.config.ts +18 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import type { ChatContentStatus, ContentDocument } from '../types/content.ts';
|
|
4
|
+
import { ChatBlocks } from '../ui/ChatBlocks.tsx';
|
|
5
|
+
import { ChatTable, type ChatTableContentProps } from '../ui/ChatTable.tsx';
|
|
6
|
+
import { reconcileContentDocument } from '../utils/contentIdentity.ts';
|
|
7
|
+
import { parseContentIncrementally } from '../utils/parseContentIncrementally.ts';
|
|
8
|
+
|
|
9
|
+
export interface AIChatContentProps {
|
|
10
|
+
text: string;
|
|
11
|
+
status?: ChatContentStatus;
|
|
12
|
+
tableClassName?: string;
|
|
13
|
+
tableScrollIndicatorOffset?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
* Measure actual horizontal overflow and expose keyboard scrolling only when the table needs it.
|
|
18
|
+
*/
|
|
19
|
+
const ContentTable = memo((p: ChatTableContentProps) => {
|
|
20
|
+
const scrollRef = useRef<HTMLDivElement>(null);
|
|
21
|
+
const [overflowing, setOverflowing] = useState(false);
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
* Recheck widths after source updates, parent resizes, and table layout changes.
|
|
25
|
+
*/
|
|
26
|
+
const measure = useCallback(() => {
|
|
27
|
+
const element = scrollRef.current;
|
|
28
|
+
if (element) setOverflowing(element.scrollWidth > element.clientWidth + 1);
|
|
29
|
+
}, []);
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
const element = scrollRef.current;
|
|
33
|
+
if (!element) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(measure);
|
|
37
|
+
observer?.observe(element);
|
|
38
|
+
if (element.firstElementChild) {
|
|
39
|
+
observer?.observe(element.firstElementChild);
|
|
40
|
+
}
|
|
41
|
+
window.addEventListener('resize', measure);
|
|
42
|
+
return () => {
|
|
43
|
+
observer?.disconnect();
|
|
44
|
+
window.removeEventListener('resize', measure);
|
|
45
|
+
};
|
|
46
|
+
}, [measure]);
|
|
47
|
+
|
|
48
|
+
// Streaming updates can change overflow without resizing the viewport or table itself.
|
|
49
|
+
useEffect(measure, [measure, p.block]);
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<ChatTable
|
|
53
|
+
block={p.block}
|
|
54
|
+
className={p.className}
|
|
55
|
+
scrollIndicatorOffset={p.scrollIndicatorOffset}
|
|
56
|
+
scrollRef={scrollRef}
|
|
57
|
+
overflowing={overflowing}
|
|
58
|
+
scrollLabel={i18n.t('c.scrollable_table')}
|
|
59
|
+
/>
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/*
|
|
64
|
+
* Parse the changing tail synchronously and retain stable content from this message's last committed render.
|
|
65
|
+
*/
|
|
66
|
+
export const AIChatContent = memo((p: AIChatContentProps) => {
|
|
67
|
+
const status = p.status ?? 'complete';
|
|
68
|
+
const previousDocument = useRef<ContentDocument | null>(null);
|
|
69
|
+
const document = useMemo(
|
|
70
|
+
() => reconcileContentDocument(previousDocument.current, parseContentIncrementally(previousDocument.current, p.text, status)),
|
|
71
|
+
[p.text, status],
|
|
72
|
+
);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
previousDocument.current = document;
|
|
75
|
+
}, [document]);
|
|
76
|
+
return (
|
|
77
|
+
<ChatBlocks
|
|
78
|
+
blocks={document.blocks}
|
|
79
|
+
TableComponent={ContentTable}
|
|
80
|
+
tableClassName={p.tableClassName}
|
|
81
|
+
tableScrollIndicatorOffset={p.tableScrollIndicatorOffset}
|
|
82
|
+
/>
|
|
83
|
+
);
|
|
84
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { usePopOverActions, usePopOverEvent } from '@jsb188/react/states';
|
|
3
|
+
import type { POListItemObj } from '@jsb188/react/types/PopOver.d';
|
|
4
|
+
import { PopOverButton } from '@jsb188/react-web/modules/PopOver';
|
|
5
|
+
import { Icon } from '@jsb188/react-web/svgs/Icon';
|
|
6
|
+
import { useEffect, useRef } from 'react';
|
|
7
|
+
import type { AgentNoteActionState } from '../ui/AgentNotesUI.types';
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* Keep note actions in a scoped menu and recheck current permissions before handling a selection.
|
|
11
|
+
*/
|
|
12
|
+
export const AgentNoteActionsMenu = (p: {
|
|
13
|
+
id: string;
|
|
14
|
+
priorityAction: AgentNoteActionState;
|
|
15
|
+
needsAttention: boolean;
|
|
16
|
+
archiveAction: AgentNoteActionState;
|
|
17
|
+
deleteScheduledTaskAction: AgentNoteActionState | null;
|
|
18
|
+
onCollapse: () => void;
|
|
19
|
+
}) => {
|
|
20
|
+
const event = usePopOverEvent({ popOverId: p.id });
|
|
21
|
+
const { acknowledgePopOverEvent, closePopOver } = usePopOverActions();
|
|
22
|
+
const currentProps = useRef(p);
|
|
23
|
+
currentProps.current = p;
|
|
24
|
+
const options: POListItemObj[] = [
|
|
25
|
+
{
|
|
26
|
+
__type: 'LIST_ITEM',
|
|
27
|
+
name: 'collapse',
|
|
28
|
+
value: true,
|
|
29
|
+
text: i18n.t('c.collapse_item'),
|
|
30
|
+
iconName: 'arrow-rectangle-up',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
__type: 'LIST_ITEM',
|
|
34
|
+
name: 'priority',
|
|
35
|
+
value: true,
|
|
36
|
+
iconName: 'alert-circle',
|
|
37
|
+
text: i18n.t(
|
|
38
|
+
p.priorityAction.busy
|
|
39
|
+
? 'agent_note.updating_priority'
|
|
40
|
+
: p.needsAttention === true
|
|
41
|
+
? 'agent_note.decrease_priority'
|
|
42
|
+
: 'agent_note.increase_priority',
|
|
43
|
+
),
|
|
44
|
+
disabled: !!p.priorityAction.disabledReason || p.priorityAction.busy,
|
|
45
|
+
loading: p.priorityAction.busy,
|
|
46
|
+
description: p.priorityAction.disabledReason,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
__type: 'LIST_ITEM',
|
|
50
|
+
name: 'archive',
|
|
51
|
+
value: true,
|
|
52
|
+
text: i18n.t('agent_note.archive_note'),
|
|
53
|
+
iconName: 'bin',
|
|
54
|
+
preset: 'delete_hover',
|
|
55
|
+
disabled: !!p.archiveAction.disabledReason || p.archiveAction.busy,
|
|
56
|
+
loading: p.archiveAction.busy,
|
|
57
|
+
description: p.archiveAction.disabledReason,
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
if (p.deleteScheduledTaskAction) {
|
|
61
|
+
options.push({
|
|
62
|
+
__type: 'LIST_ITEM',
|
|
63
|
+
name: 'delete_scheduled_task',
|
|
64
|
+
value: true,
|
|
65
|
+
text: i18n.t('workflow.delete_scheduled_task'),
|
|
66
|
+
iconName: 'bin',
|
|
67
|
+
preset: 'delete_hover',
|
|
68
|
+
disabled: !!p.deleteScheduledTaskAction.disabledReason || p.deleteScheduledTaskAction.busy,
|
|
69
|
+
loading: p.deleteScheduledTaskAction.busy,
|
|
70
|
+
description: p.deleteScheduledTaskAction.disabledReason,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Acknowledge selections before invoking actions so remounts cannot replay them.
|
|
76
|
+
* Read current permissions because the menu can outlive the state that opened it.
|
|
77
|
+
*/
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (!event) return;
|
|
80
|
+
acknowledgePopOverEvent(event.eventId);
|
|
81
|
+
if (event.action !== 'ITEM') return;
|
|
82
|
+
const { id, archiveAction, priorityAction, deleteScheduledTaskAction, onCollapse } = currentProps.current;
|
|
83
|
+
if (event.name === 'priority') {
|
|
84
|
+
if (!priorityAction.disabledReason && !priorityAction.busy) priorityAction.onClick();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
closePopOver({ id });
|
|
88
|
+
if (event.name === 'collapse') onCollapse();
|
|
89
|
+
else if (event.name === 'archive' && !archiveAction.disabledReason && !archiveAction.busy) archiveAction.onClick();
|
|
90
|
+
else if (
|
|
91
|
+
event.name === 'delete_scheduled_task' &&
|
|
92
|
+
deleteScheduledTaskAction &&
|
|
93
|
+
!deleteScheduledTaskAction.disabledReason &&
|
|
94
|
+
!deleteScheduledTaskAction.busy
|
|
95
|
+
)
|
|
96
|
+
deleteScheduledTaskAction.onClick();
|
|
97
|
+
}, [event]);
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<PopOverButton
|
|
101
|
+
as="button"
|
|
102
|
+
id={p.id}
|
|
103
|
+
className="rel v_center no_shrink r no_link w_32 h_32 cl_md bg_hv"
|
|
104
|
+
activeClassName="bg"
|
|
105
|
+
ariaLabel={i18n.t('c.actions')}
|
|
106
|
+
position="bottom_right"
|
|
107
|
+
offsetY={-3}
|
|
108
|
+
dismissOnExternalScroll
|
|
109
|
+
syncOpenIface
|
|
110
|
+
syncOpenIfaceKey={JSON.stringify(options)}
|
|
111
|
+
iface={{ name: 'PO_LIST', variables: { options } }}
|
|
112
|
+
>
|
|
113
|
+
<span
|
|
114
|
+
className="ic_sm"
|
|
115
|
+
aria-hidden="true"
|
|
116
|
+
>
|
|
117
|
+
<Icon name="dots-vertical-filled" />
|
|
118
|
+
</span>
|
|
119
|
+
</PopOverButton>
|
|
120
|
+
);
|
|
121
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { useAgentNoteBootstrapProgress } from '@jsb188/graphql/hooks/use-agentNote-qry';
|
|
3
|
+
import { AgentNoteBootstrapProgressUI } from '../ui/AgentNotesUI';
|
|
4
|
+
|
|
5
|
+
const FILE_CONSTANTS = {
|
|
6
|
+
messages: {
|
|
7
|
+
NO_USEFUL_BRIEF: {
|
|
8
|
+
title: 'agent_note.agent_note_discovery_waiting',
|
|
9
|
+
description: 'agent_note.agent_note_discovery_no_useful_note',
|
|
10
|
+
},
|
|
11
|
+
BUDGET_REACHED: { title: 'agent_note.agent_note_discovery_paused', description: 'agent_note.agent_note_discovery_limit_reached' },
|
|
12
|
+
NO_AUTHORIZED_MEMBER: {
|
|
13
|
+
title: 'agent_note.agent_note_discovery_permission',
|
|
14
|
+
description: 'agent_note.agent_note_discovery_permission_needed',
|
|
15
|
+
},
|
|
16
|
+
NO_ELIGIBLE_LOG_CONTEXT: {
|
|
17
|
+
title: 'agent_note.agent_note_discovery_waiting',
|
|
18
|
+
description: 'agent_note.agent_note_discovery_records_needed',
|
|
19
|
+
},
|
|
20
|
+
ERROR: { title: 'agent_note.agent_note_discovery_interrupted', description: 'agent_note.agent_note_discovery_retry' },
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
* Present a fixed, safe explanation of the last review while keeping its internal report private.
|
|
26
|
+
*/
|
|
27
|
+
export const AgentNoteBootstrapProgress = (p: { organizationId: string }) => {
|
|
28
|
+
const { progress, error } = useAgentNoteBootstrapProgress({ organizationId: p.organizationId });
|
|
29
|
+
if (error || !progress || progress.outcome === 'CREATED') return null;
|
|
30
|
+
const message = FILE_CONSTANTS.messages[progress.outcome];
|
|
31
|
+
if (!message) return null;
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<AgentNoteBootstrapProgressUI
|
|
35
|
+
title={i18n.t(message.title)}
|
|
36
|
+
description={i18n.t(message.description)}
|
|
37
|
+
lastReviewedAt={progress.lastReviewedAt}
|
|
38
|
+
nextReviewAt={progress.nextReviewAt}
|
|
39
|
+
/>
|
|
40
|
+
);
|
|
41
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import type { AgentNoteGQL, AgentNoteLinkGQL } from '@jsb188/mday/types/agentNote.d.ts';
|
|
3
|
+
import type { OrganizationOperationEnum } from '@jsb188/mday/types/organization.d.ts';
|
|
4
|
+
import { makeFieldDocFormPath } from '@jsb188/mday-web/utils/route';
|
|
5
|
+
import type { POListIfaceItem } from '@jsb188/react/types/PopOver.d';
|
|
6
|
+
import { COMMON_ICON_NAMES } from '@jsb188/react-web/svgs/Icon';
|
|
7
|
+
import { getAgentNoteSourceLink } from './AgentNotes.helpers';
|
|
8
|
+
|
|
9
|
+
const FILE_CONSTANTS = {
|
|
10
|
+
hiddenReferenceTypes: ['LOG_ACTIVITY', 'LOG_ACTIVITY_GROUP', 'CAMPAIGN_TEMPLATE'] as readonly string[],
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
* Resolve references with unambiguous viewer routes; other references remain visible as labels.
|
|
15
|
+
*/
|
|
16
|
+
function getReferencePath(link: AgentNoteLinkGQL) {
|
|
17
|
+
const id = encodeURIComponent(link.entityId);
|
|
18
|
+
switch (link.entityType) {
|
|
19
|
+
case 'DATA_TABLE':
|
|
20
|
+
return `/app/d/${id}`;
|
|
21
|
+
case 'SHEET':
|
|
22
|
+
return `/app/s/${id}`;
|
|
23
|
+
case 'WORKFLOW':
|
|
24
|
+
return `/app/runs/${id}`;
|
|
25
|
+
case 'FIELD_DOC':
|
|
26
|
+
return link.fieldDocProgramId ? makeFieldDocFormPath(link.fieldDocProgramId, link.entityId) : undefined;
|
|
27
|
+
default:
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* Group the source and unique additional references under separate muted section labels.
|
|
34
|
+
*/
|
|
35
|
+
export function getAgentNoteReferenceOptions(
|
|
36
|
+
note: AgentNoteGQL,
|
|
37
|
+
context: { operation: OrganizationOperationEnum | null; timeZone: string | null },
|
|
38
|
+
): POListIfaceItem[] {
|
|
39
|
+
const source = getAgentNoteSourceLink(note);
|
|
40
|
+
const options: POListIfaceItem[] = source
|
|
41
|
+
? [
|
|
42
|
+
{ __type: 'TEXT', text: i18n.t('agent_note.link_source'), fontClassName: 'ft_xs ft_medium', designClassName: 'cl_md' },
|
|
43
|
+
note.kind === 'REMINDER' && note.reminderWorkflowId
|
|
44
|
+
? {
|
|
45
|
+
__type: 'LIST_ITEM_MODAL',
|
|
46
|
+
text: i18n.t('agent_note.source_type.REMINDER'),
|
|
47
|
+
iconName: 'book-book-pages',
|
|
48
|
+
variables: {
|
|
49
|
+
name: 'WORKFLOW',
|
|
50
|
+
props: { organizationId: note.organizationId, workflowId: note.reminderWorkflowId, timeZone: context.timeZone },
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
: {
|
|
54
|
+
__type: 'LIST_ITEM',
|
|
55
|
+
name: 'source',
|
|
56
|
+
value: true,
|
|
57
|
+
...source,
|
|
58
|
+
iconName: COMMON_ICON_NAMES.open_external_link,
|
|
59
|
+
text: i18n.t(`agent_note.source_type.${note.kind}`),
|
|
60
|
+
},
|
|
61
|
+
]
|
|
62
|
+
: [];
|
|
63
|
+
if (!note.links?.length) return options;
|
|
64
|
+
const seen = new Set<string>();
|
|
65
|
+
let hasReferences = false;
|
|
66
|
+
for (const link of note.links) {
|
|
67
|
+
if (FILE_CONSTANTS.hiddenReferenceTypes.includes(link.entityType)) continue;
|
|
68
|
+
const key = `${link.entityType}:${link.entityId}`;
|
|
69
|
+
if (seen.has(key)) continue;
|
|
70
|
+
seen.add(key);
|
|
71
|
+
const to = getReferencePath(link);
|
|
72
|
+
if (source && (to === source.to || (link.entityType === 'CAMPAIGN' && link.entityId === note.campaignId))) continue;
|
|
73
|
+
if (!hasReferences) {
|
|
74
|
+
options.push({ __type: 'TEXT', text: i18n.t('agent_note.sources'), fontClassName: 'ft_xs ft_medium', designClassName: 'cl_md' });
|
|
75
|
+
hasReferences = true;
|
|
76
|
+
}
|
|
77
|
+
const description = i18n.t(`agent_note.reference_type.${link.entityType}`);
|
|
78
|
+
if (link.entityType === 'CHILD_ORG' || link.entityType === 'WORKFLOW') {
|
|
79
|
+
options.push({
|
|
80
|
+
__type: 'LIST_ITEM_MODAL',
|
|
81
|
+
text: link.title || description,
|
|
82
|
+
description,
|
|
83
|
+
iconName: 'book-book-pages',
|
|
84
|
+
variables:
|
|
85
|
+
link.entityType === 'CHILD_ORG'
|
|
86
|
+
? {
|
|
87
|
+
name: 'ORG_PROFILE',
|
|
88
|
+
props: {
|
|
89
|
+
organizationId: note.organizationId,
|
|
90
|
+
childOrgId: link.entityId,
|
|
91
|
+
childId: '',
|
|
92
|
+
parentOperation: context.operation,
|
|
93
|
+
allowEdit: false,
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
: {
|
|
97
|
+
name: 'WORKFLOW',
|
|
98
|
+
props: { organizationId: note.organizationId, workflowId: link.entityId, timeZone: context.timeZone },
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
options.push({
|
|
104
|
+
__type: 'LIST_ITEM',
|
|
105
|
+
name: key,
|
|
106
|
+
iconName: to ? COMMON_ICON_NAMES.open_external_link : 'book-book-pages',
|
|
107
|
+
value: link.entityId,
|
|
108
|
+
text: link.title || description,
|
|
109
|
+
description,
|
|
110
|
+
to,
|
|
111
|
+
disabled: !to,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return options;
|
|
115
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { cn } from '@jsb188/app/utils/string.ts';
|
|
3
|
+
import { useOrgRelFromMyOrganizations } from '@jsb188/graphql/hooks/use-organization-qry';
|
|
4
|
+
import { useAgentNote } from '@jsb188/graphql/hooks/use-agentNote-qry';
|
|
5
|
+
import type { AgentNoteGQL, AgentNoteResultBlockGQL } from '@jsb188/mday/types/agentNote.d.ts';
|
|
6
|
+
import { PopOverTooltipButton } from '@jsb188/react-web/modules/PopOver';
|
|
7
|
+
import { Icon } from '@jsb188/react-web/svgs/Icon';
|
|
8
|
+
import { memo, useId } from 'react';
|
|
9
|
+
import { AgentNoteComposer, AgentNoteItemUI, AgentNoteResultBlockUI } from '../ui/AgentNotesUI';
|
|
10
|
+
import { getAgentNoteReferenceOptions } from './AgentNoteItem.helpers';
|
|
11
|
+
import { AgentNoteActionsMenu } from './AgentNoteActionsMenu';
|
|
12
|
+
import {
|
|
13
|
+
getAgentNoteDataTablePath,
|
|
14
|
+
getAgentNoteDisplayTime,
|
|
15
|
+
getAgentNoteRefreshWarning,
|
|
16
|
+
getAgentNoteResultBlockStatus,
|
|
17
|
+
getAgentNoteSharedResultComputedAt,
|
|
18
|
+
} from './AgentNotes.helpers';
|
|
19
|
+
import { useAgentNoteItem, useAgentNoteRefreshLabel } from './AgentNotes.hooks';
|
|
20
|
+
import type { AgentNoteChatActions, AgentNoteStateActions, AgentNoteRefreshActions, AgentNoteStateCache } from './AgentNotes.types';
|
|
21
|
+
import { AIChatContent } from './AIChatContent';
|
|
22
|
+
import { canViewerReadAgentNoteSourceKind } from '@jsb188/mday/utils/agentNote.ts';
|
|
23
|
+
import { checkACLPermission } from '@jsb188/mday/utils/organization.ts';
|
|
24
|
+
import { useOpenModalScreen } from '@jsb188/react/states';
|
|
25
|
+
|
|
26
|
+
/*
|
|
27
|
+
* Offer report navigation from an expanded email note without fetching any report data.
|
|
28
|
+
*/
|
|
29
|
+
const AgentNoteRepliesAction = (p: { note: AgentNoteGQL }) => {
|
|
30
|
+
const { organizationRelationship } = useOrgRelFromMyOrganizations(p.note.organizationId);
|
|
31
|
+
const openModalScreen = useOpenModalScreen();
|
|
32
|
+
const allowed = canViewerReadAgentNoteSourceKind('CAMPAIGN_EMAIL', (check, permission) =>
|
|
33
|
+
checkACLPermission(organizationRelationship, check, permission),
|
|
34
|
+
);
|
|
35
|
+
if (!p.note.campaignId || !allowed) return null;
|
|
36
|
+
return (
|
|
37
|
+
<button
|
|
38
|
+
type='button'
|
|
39
|
+
className='link ft_sm mt_sm'
|
|
40
|
+
onClick={() =>
|
|
41
|
+
openModalScreen({
|
|
42
|
+
name: 'AGENT_NOTE',
|
|
43
|
+
props: { organizationId: p.note.organizationId, agentNoteId: p.note.id, initialSection: 'replies' },
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
>
|
|
47
|
+
{i18n.t('campaign.reply_report.view_replies')}
|
|
48
|
+
</button>
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/*
|
|
53
|
+
* Read shared note detail while expanded and show its source and references in a navigation menu.
|
|
54
|
+
*/
|
|
55
|
+
const AgentNoteReferencesMenu = (p: { id: string; note: AgentNoteGQL }) => {
|
|
56
|
+
const { agentNote, loading } = useAgentNote({ organizationId: p.note.organizationId, agentNoteId: p.note.id }, { markRead: false });
|
|
57
|
+
const { organization } = useOrgRelFromMyOrganizations(p.note.organizationId);
|
|
58
|
+
const options = getAgentNoteReferenceOptions(agentNote || p.note, {
|
|
59
|
+
operation: organization?.operation || null,
|
|
60
|
+
timeZone: organization?.settings?.timeZone || null,
|
|
61
|
+
});
|
|
62
|
+
if (!options.length) return null;
|
|
63
|
+
return (
|
|
64
|
+
<PopOverTooltipButton
|
|
65
|
+
as="button"
|
|
66
|
+
id={p.id}
|
|
67
|
+
className={cn('rel v_center no_shrink r no_link w_32 h_32', loading ? 'cl_lt' : 'cl_md', !loading && 'bg_hv')}
|
|
68
|
+
activeClassName="bg"
|
|
69
|
+
ariaLabel={i18n.t('agent_note.source_materials')}
|
|
70
|
+
tooltipText={i18n.t('agent_note.source_materials')}
|
|
71
|
+
showTooltipWhenDisabled
|
|
72
|
+
tooltipPosition="top"
|
|
73
|
+
tooltipOffsetY={-2}
|
|
74
|
+
disabled={!!loading}
|
|
75
|
+
position="bottom_left"
|
|
76
|
+
offsetY={-3}
|
|
77
|
+
offsetX={-4}
|
|
78
|
+
dismissOnExternalScroll
|
|
79
|
+
syncOpenIface
|
|
80
|
+
syncOpenIfaceKey={JSON.stringify(options)}
|
|
81
|
+
iface={{ name: 'PO_LIST', variables: { options } }}
|
|
82
|
+
>
|
|
83
|
+
<span
|
|
84
|
+
className="ic_sm"
|
|
85
|
+
aria-hidden="true"
|
|
86
|
+
>
|
|
87
|
+
<Icon name="list-bullets" />
|
|
88
|
+
</span>
|
|
89
|
+
</PopOverTooltipButton>
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
* Render a note's saved query results below its body with the chat parser. The server-rendered markdown
|
|
95
|
+
* is followed by footer metadata; matching computation times appear only after the final result.
|
|
96
|
+
* A block's title opens its listed table.
|
|
97
|
+
*/
|
|
98
|
+
const AgentNoteResultBlocks = memo((p: { blocks?: AgentNoteResultBlockGQL[] }) => {
|
|
99
|
+
const blocks = p.blocks || [];
|
|
100
|
+
const sharedComputedAt = getAgentNoteSharedResultComputedAt(blocks);
|
|
101
|
+
return blocks.map((block, index) => (
|
|
102
|
+
<AgentNoteResultBlockUI
|
|
103
|
+
key={block.name}
|
|
104
|
+
title={block.title}
|
|
105
|
+
to={block.dataTableId ? getAgentNoteDataTablePath(block.dataTableId) : null}
|
|
106
|
+
status={getAgentNoteResultBlockStatus(block)}
|
|
107
|
+
computedAt={block.markdown && (!sharedComputedAt || index === blocks.length - 1) ? block.computedAt : null}
|
|
108
|
+
timeZone={block.timeZone}
|
|
109
|
+
shownCount={block.markdown ? block.shownCount : null}
|
|
110
|
+
matchedCount={block.matchedCount}
|
|
111
|
+
>
|
|
112
|
+
{block.markdown ? (
|
|
113
|
+
<div
|
|
114
|
+
className="mdy_msg p_n ofw"
|
|
115
|
+
style={{ minWidth: 0 }}
|
|
116
|
+
>
|
|
117
|
+
<AIChatContent
|
|
118
|
+
text={block.markdown}
|
|
119
|
+
tableClassName="bd_t_1 bd_b_1 bd_lt"
|
|
120
|
+
/>
|
|
121
|
+
</div>
|
|
122
|
+
) : null}
|
|
123
|
+
</AgentNoteResultBlockUI>
|
|
124
|
+
));
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
/*
|
|
128
|
+
* Fetch full note content only while its disclosure is open and render it with the chat parser,
|
|
129
|
+
* followed by its saved query results.
|
|
130
|
+
*/
|
|
131
|
+
const AgentNoteBody = memo((p: { organizationId: string; agentNoteId: string }) => {
|
|
132
|
+
const { agentNote, error } = useAgentNote(p, { markRead: false });
|
|
133
|
+
|
|
134
|
+
if (error || !agentNote) return null;
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<>
|
|
138
|
+
<div
|
|
139
|
+
className="mdy_msg p_n ofw"
|
|
140
|
+
style={{ minWidth: 0 }}
|
|
141
|
+
>
|
|
142
|
+
<AIChatContent
|
|
143
|
+
text={agentNote.body || ''}
|
|
144
|
+
tableClassName="bd_t_1 bd_b_1 bd_lt"
|
|
145
|
+
/>
|
|
146
|
+
</div>
|
|
147
|
+
<AgentNoteResultBlocks blocks={agentNote.resultBlocks} />
|
|
148
|
+
{agentNote.kind === 'CAMPAIGN_EMAIL' && <AgentNoteRepliesAction note={agentNote} />}
|
|
149
|
+
</>
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
/*
|
|
154
|
+
* Render collection-controlled expansion while retaining each note's unsent message.
|
|
155
|
+
*/
|
|
156
|
+
export const AgentNoteItem = memo(
|
|
157
|
+
(p: {
|
|
158
|
+
note: AgentNoteGQL;
|
|
159
|
+
stateCache: AgentNoteStateCache;
|
|
160
|
+
sending: boolean;
|
|
161
|
+
composerDisabled: boolean;
|
|
162
|
+
onSend: AgentNoteChatActions['onSend'];
|
|
163
|
+
updatingPriority: boolean;
|
|
164
|
+
onChangePriority: AgentNoteStateActions['onChange'];
|
|
165
|
+
pinning: boolean;
|
|
166
|
+
onChangePin: AgentNoteStateActions['onChange'];
|
|
167
|
+
refreshing: boolean;
|
|
168
|
+
onRefresh: AgentNoteRefreshActions['onRefresh'];
|
|
169
|
+
expanded: boolean;
|
|
170
|
+
onToggleExpanded: (agentNoteId: string) => void;
|
|
171
|
+
}) => {
|
|
172
|
+
const { draft, changeDraft, actions } = useAgentNoteItem(p);
|
|
173
|
+
const controlsId = useId();
|
|
174
|
+
const refreshWarning = getAgentNoteRefreshWarning(p.note.refreshOutcome);
|
|
175
|
+
const refreshLabel = useAgentNoteRefreshLabel(p.note.refreshAvailableAt, p.note.refreshing, p.expanded && !p.note.archivedAt);
|
|
176
|
+
|
|
177
|
+
return (
|
|
178
|
+
<div>
|
|
179
|
+
<AgentNoteItemUI
|
|
180
|
+
emoji={p.note.emoji}
|
|
181
|
+
headline={p.note.headline?.trim() || i18n.t('agent_note.agent_note')}
|
|
182
|
+
expanded={p.expanded}
|
|
183
|
+
needsAttention={p.note.needsAttention === true}
|
|
184
|
+
publishedAt={getAgentNoteDisplayTime(p.note)}
|
|
185
|
+
refreshLabel={refreshLabel}
|
|
186
|
+
// An archived note cannot be refreshed (the server refuses it until the note is restored)
|
|
187
|
+
refreshAction={p.note.archivedAt ? null : actions.refresh}
|
|
188
|
+
pinAction={actions.pin}
|
|
189
|
+
pinned={p.note.pinned}
|
|
190
|
+
refreshWarning={refreshWarning}
|
|
191
|
+
ActionsComponent={
|
|
192
|
+
p.expanded ? (
|
|
193
|
+
<AgentNoteActionsMenu
|
|
194
|
+
id={`agent_note_actions_${controlsId}`}
|
|
195
|
+
priorityAction={actions.priority}
|
|
196
|
+
needsAttention={p.note.needsAttention}
|
|
197
|
+
archiveAction={actions.archive}
|
|
198
|
+
deleteScheduledTaskAction={actions.deleteScheduledTask}
|
|
199
|
+
onCollapse={() => p.onToggleExpanded(p.note.id)}
|
|
200
|
+
/>
|
|
201
|
+
) : undefined
|
|
202
|
+
}
|
|
203
|
+
ReferencesComponent={
|
|
204
|
+
p.expanded ? (
|
|
205
|
+
<AgentNoteReferencesMenu
|
|
206
|
+
id={`agent_note_references_${controlsId}`}
|
|
207
|
+
note={p.note}
|
|
208
|
+
/>
|
|
209
|
+
) : undefined
|
|
210
|
+
}
|
|
211
|
+
controlsId={controlsId}
|
|
212
|
+
onToggle={() => p.onToggleExpanded(p.note.id)}
|
|
213
|
+
expandedHeight={420}
|
|
214
|
+
FooterComponent={
|
|
215
|
+
<AgentNoteComposer
|
|
216
|
+
value={draft}
|
|
217
|
+
sending={p.sending}
|
|
218
|
+
disabled={p.composerDisabled}
|
|
219
|
+
onChange={changeDraft}
|
|
220
|
+
onSend={() => p.onSend(p.note.id)}
|
|
221
|
+
/>
|
|
222
|
+
}
|
|
223
|
+
>
|
|
224
|
+
<AgentNoteBody
|
|
225
|
+
organizationId={p.note.organizationId}
|
|
226
|
+
agentNoteId={p.note.id}
|
|
227
|
+
/>
|
|
228
|
+
</AgentNoteItemUI>
|
|
229
|
+
</div>
|
|
230
|
+
);
|
|
231
|
+
},
|
|
232
|
+
);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AgentNoteKindEnum } from '@jsb188/mday/types/agentNote.d.ts';
|
|
2
|
+
|
|
3
|
+
// Presentation-only groups: extending this registry never changes the query or database reads.
|
|
4
|
+
export const AGENT_NOTE_GROUPS = {
|
|
5
|
+
SCHEDULED_TASKS: { kinds: ['REMINDER'], title: 'c.scheduled_tasks', icon: 'calendar-3' },
|
|
6
|
+
CAMPAIGNS: { kinds: ['CAMPAIGN_EMAIL', 'CAMPAIGN_FORM'], title: 'campaign.campaigns', icon: 'email-megaphone' },
|
|
7
|
+
DATA_INSIGHTS: { kinds: ['INSIGHT', 'FIELD_DOC'], title: 'c.data_insights', icon: 'workflow-data-table-2' },
|
|
8
|
+
} as const satisfies Record<string, { kinds: readonly AgentNoteKindEnum[]; title: string; icon: string }>;
|
|
9
|
+
|
|
10
|
+
export const AGENT_NOTE_DISPLAY_LIMITS = { initial: 5, increment: 25 };
|