@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,145 @@
|
|
|
1
|
+
import i18n from '@jsb188/app/i18n/index.ts';
|
|
2
|
+
import { AccordianPanel } from '@jsb188/react-web/modules/Accordian';
|
|
3
|
+
import { ContentTransition } from '@jsb188/react-web/modules/Transition';
|
|
4
|
+
import { type ComponentProps, memo, useCallback, useId, useMemo, useState } from 'react';
|
|
5
|
+
import { AgentNotesSectionUI, AgentNotesShowMoreUI } from '../ui/AgentNotesUI';
|
|
6
|
+
import { AgentNoteItem } from './AgentNoteItem';
|
|
7
|
+
import { AGENT_NOTE_DISPLAY_LIMITS, AGENT_NOTE_GROUPS } from './AgentNotes.constants';
|
|
8
|
+
import { getAgentNotesDisplayKey, getAgentNotesSectionNextLimit } from './AgentNotes.helpers';
|
|
9
|
+
import type {
|
|
10
|
+
AgentNoteChatActions,
|
|
11
|
+
AgentNoteGroupKey,
|
|
12
|
+
AgentNoteStateActions,
|
|
13
|
+
AgentNoteRefreshActions,
|
|
14
|
+
AgentNoteStateCache,
|
|
15
|
+
AgentNotesSnapshot,
|
|
16
|
+
} from './AgentNotes.types';
|
|
17
|
+
import { useLiveAgentNoteRows, useStableAgentNoteRows } from './AgentNotesCollection.hooks';
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
* Observe retained rows independently from transition frames, revealing only the local
|
|
21
|
+
* display limit. Show more never changes the collection's query or its variables.
|
|
22
|
+
*/
|
|
23
|
+
const AgentNoteRows = memo(
|
|
24
|
+
(p: {
|
|
25
|
+
snapshot: AgentNotesSnapshot | null;
|
|
26
|
+
stateCache: AgentNoteStateCache;
|
|
27
|
+
chatActions: AgentNoteChatActions;
|
|
28
|
+
pinActions: AgentNoteStateActions;
|
|
29
|
+
priorityActions: AgentNoteStateActions;
|
|
30
|
+
refreshActions: AgentNoteRefreshActions;
|
|
31
|
+
expandedNoteId: string | null;
|
|
32
|
+
onToggleExpanded: (agentNoteId: string) => void;
|
|
33
|
+
visibleCount: number;
|
|
34
|
+
onShowMore: () => void;
|
|
35
|
+
}) => {
|
|
36
|
+
const notes = useLiveAgentNoteRows(p.snapshot?.notes || null);
|
|
37
|
+
const showMoreId = useId();
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<>
|
|
41
|
+
{notes.slice(0, p.visibleCount).map((note) => (
|
|
42
|
+
<AgentNoteItem
|
|
43
|
+
key={note.id}
|
|
44
|
+
note={note}
|
|
45
|
+
expanded={note.id === p.expandedNoteId}
|
|
46
|
+
onToggleExpanded={p.onToggleExpanded}
|
|
47
|
+
stateCache={p.stateCache}
|
|
48
|
+
sending={p.chatActions.sendingNoteId === note.id}
|
|
49
|
+
composerDisabled={note.id === p.expandedNoteId && !!p.chatActions.sendingNoteId}
|
|
50
|
+
onSend={p.chatActions.onSend}
|
|
51
|
+
updatingPriority={p.priorityActions.savingNoteIds.has(note.id)}
|
|
52
|
+
onChangePriority={p.priorityActions.onChange}
|
|
53
|
+
pinning={p.pinActions.savingNoteIds.has(note.id)}
|
|
54
|
+
onChangePin={p.pinActions.onChange}
|
|
55
|
+
refreshing={p.refreshActions.savingNoteIds.has(note.id)}
|
|
56
|
+
onRefresh={p.refreshActions.onRefresh}
|
|
57
|
+
/>
|
|
58
|
+
))}
|
|
59
|
+
<div className="rel">
|
|
60
|
+
<AccordianPanel
|
|
61
|
+
exitOverlay
|
|
62
|
+
id={showMoreId}
|
|
63
|
+
expanded={notes.length > p.visibleCount}
|
|
64
|
+
>
|
|
65
|
+
<AgentNotesShowMoreUI onClick={p.onShowMore} />
|
|
66
|
+
</AccordianPanel>
|
|
67
|
+
</div>
|
|
68
|
+
</>
|
|
69
|
+
);
|
|
70
|
+
},
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
/*
|
|
74
|
+
* Group the shared collection locally, keeping disclosure and display limits outside
|
|
75
|
+
* the lazily mounted list. Empty groups stay hidden, including after live removals.
|
|
76
|
+
*/
|
|
77
|
+
export const AgentNotesSection = (
|
|
78
|
+
p: Omit<ComponentProps<typeof AgentNoteRows>, 'visibleCount' | 'onShowMore'> & {
|
|
79
|
+
section: AgentNoteGroupKey;
|
|
80
|
+
ready: boolean;
|
|
81
|
+
accessDenied: boolean;
|
|
82
|
+
loading: boolean;
|
|
83
|
+
},
|
|
84
|
+
) => {
|
|
85
|
+
const [expanded, setExpanded] = useState(true);
|
|
86
|
+
const [visibleCount, setVisibleCount] = useState(AGENT_NOTE_DISPLAY_LIMITS.initial);
|
|
87
|
+
const controlsId = useId();
|
|
88
|
+
const section = AGENT_NOTE_GROUPS[p.section];
|
|
89
|
+
const title = i18n.t(section.title);
|
|
90
|
+
const notes = useStableAgentNoteRows(p.snapshot?.notes.filter((note) => section.kinds.some((kind) => kind === note.kind)) ?? null);
|
|
91
|
+
const snapshot = useMemo<AgentNotesSnapshot | null>(
|
|
92
|
+
() => (notes === null ? null : { notes, key: getAgentNotesDisplayKey(notes) }),
|
|
93
|
+
[notes],
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
/* Reveal the next local batch without rerendering the query owner. */
|
|
97
|
+
const showMore = useCallback(() => setVisibleCount(getAgentNotesSectionNextLimit), []);
|
|
98
|
+
|
|
99
|
+
/* Reset the display limit when collapsing so reopening starts with the initial batch. */
|
|
100
|
+
const toggleExpanded = () => {
|
|
101
|
+
if (expanded) setVisibleCount(AGENT_NOTE_DISPLAY_LIMITS.initial);
|
|
102
|
+
setExpanded((current) => !current);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
if (p.accessDenied || !snapshot?.notes.length) return null;
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<AgentNotesSectionUI
|
|
109
|
+
title={title}
|
|
110
|
+
icon={section.icon}
|
|
111
|
+
controlsId={controlsId}
|
|
112
|
+
expanded={expanded}
|
|
113
|
+
onToggle={toggleExpanded}
|
|
114
|
+
>
|
|
115
|
+
<ContentTransition
|
|
116
|
+
// Keep the same border gutter while transitions temporarily clip the list.
|
|
117
|
+
className="-mx_10 px_10"
|
|
118
|
+
role="region"
|
|
119
|
+
aria-label={title}
|
|
120
|
+
aria-busy={p.loading}
|
|
121
|
+
data={snapshot}
|
|
122
|
+
contentKey={snapshot?.key || ''}
|
|
123
|
+
transitionKey={p.section}
|
|
124
|
+
ready={p.ready || !!snapshot}
|
|
125
|
+
animate={!p.accessDenied}
|
|
126
|
+
hiddenDurationMS={100}
|
|
127
|
+
>
|
|
128
|
+
{(displayed) => (
|
|
129
|
+
<AgentNoteRows
|
|
130
|
+
snapshot={displayed}
|
|
131
|
+
stateCache={p.stateCache}
|
|
132
|
+
expandedNoteId={p.expandedNoteId}
|
|
133
|
+
onToggleExpanded={p.onToggleExpanded}
|
|
134
|
+
chatActions={p.chatActions}
|
|
135
|
+
pinActions={p.pinActions}
|
|
136
|
+
priorityActions={p.priorityActions}
|
|
137
|
+
refreshActions={p.refreshActions}
|
|
138
|
+
visibleCount={visibleCount}
|
|
139
|
+
onShowMore={showMore}
|
|
140
|
+
/>
|
|
141
|
+
)}
|
|
142
|
+
</ContentTransition>
|
|
143
|
+
</AgentNotesSectionUI>
|
|
144
|
+
);
|
|
145
|
+
};
|
package/src/parser.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export type ChatContentStatus = 'streaming' | 'complete' | 'interrupted';
|
|
2
|
+
|
|
3
|
+
export interface SourceRange {
|
|
4
|
+
start: number;
|
|
5
|
+
end: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ContentDiagnostic extends SourceRange {
|
|
9
|
+
code: 'incomplete' | 'malformed' | 'unsupported' | 'limit-exceeded';
|
|
10
|
+
reason: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type InlineStyle = 'medium' | 'semibold' | 'italic' | 'underline' | 'strike' | 'highlight';
|
|
14
|
+
|
|
15
|
+
export type InlineNode =
|
|
16
|
+
| ({ kind: 'text' | 'code'; value: string } & SourceRange)
|
|
17
|
+
| ({ kind: 'break' } & SourceRange)
|
|
18
|
+
| ({ kind: 'style'; styles: InlineStyle[]; children: InlineNode[] } & SourceRange)
|
|
19
|
+
| ({ kind: 'link'; destination: string; children: InlineNode[] } & SourceRange)
|
|
20
|
+
| ({ kind: 'emoji'; name: string; source: string } & SourceRange);
|
|
21
|
+
|
|
22
|
+
export interface BlockBase extends SourceRange {
|
|
23
|
+
source: string;
|
|
24
|
+
complete: boolean;
|
|
25
|
+
fallbackReason?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ParagraphBlock extends BlockBase {
|
|
29
|
+
kind: 'paragraph';
|
|
30
|
+
children: InlineNode[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface HeadingBlock extends BlockBase {
|
|
34
|
+
kind: 'heading';
|
|
35
|
+
level: 1 | 2 | 3 | 4 | 5 | 6;
|
|
36
|
+
children: InlineNode[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ListBlock extends BlockBase {
|
|
40
|
+
kind: 'list';
|
|
41
|
+
ordered: boolean;
|
|
42
|
+
startNumber?: number;
|
|
43
|
+
indent: number;
|
|
44
|
+
items: (SourceRange & { blocks: ContentBlock[] })[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface CodeBlock extends BlockBase {
|
|
48
|
+
kind: 'code';
|
|
49
|
+
language: string;
|
|
50
|
+
body: string;
|
|
51
|
+
bodyRange: SourceRange;
|
|
52
|
+
fence: { character: '`' | '~'; length: number; closed: boolean };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type TableCell =
|
|
56
|
+
| ({ kind: 'inline'; children: InlineNode[] } & SourceRange)
|
|
57
|
+
| ({ kind: 'literal'; value: string } & SourceRange);
|
|
58
|
+
|
|
59
|
+
export interface TableRow extends SourceRange {
|
|
60
|
+
cells: TableCell[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface TableBlock extends BlockBase {
|
|
64
|
+
kind: 'table';
|
|
65
|
+
origin: 'markdown' | 'csv';
|
|
66
|
+
// Original CSV body, excluding its Markdown code fence.
|
|
67
|
+
csvSource?: string;
|
|
68
|
+
headers: TableCell[];
|
|
69
|
+
rows: TableRow[];
|
|
70
|
+
alignments: ('left' | 'center' | 'right' | null)[];
|
|
71
|
+
provisionalTail?: SourceRange & { source: string };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface LiteralBlock extends BlockBase {
|
|
75
|
+
kind: 'literal';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type ContentBlock = ParagraphBlock | HeadingBlock | ListBlock | CodeBlock | TableBlock | LiteralBlock;
|
|
79
|
+
|
|
80
|
+
export interface ContentDocument {
|
|
81
|
+
source: string;
|
|
82
|
+
status: ChatContentStatus;
|
|
83
|
+
blocks: ContentBlock[];
|
|
84
|
+
diagnostics: ContentDiagnostic[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface ParserLimits {
|
|
88
|
+
inputLength: number;
|
|
89
|
+
nestingDepth: number;
|
|
90
|
+
nodes: number;
|
|
91
|
+
tableRows: number;
|
|
92
|
+
tableColumns: number;
|
|
93
|
+
cellLength: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const DEFAULT_PARSER_LIMITS: Readonly<ParserLimits> = {
|
|
97
|
+
inputLength: 200_000,
|
|
98
|
+
nestingDepth: 8,
|
|
99
|
+
nodes: 20_000,
|
|
100
|
+
tableRows: 500,
|
|
101
|
+
tableColumns: 32,
|
|
102
|
+
cellLength: 16_000,
|
|
103
|
+
};
|