@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.
Files changed (51) hide show
  1. package/README.md +109 -2
  2. package/VERIFICATION.md +118 -0
  3. package/benchmarks/browser.mjs +204 -0
  4. package/benchmarks/streaming.mjs +89 -0
  5. package/package.json +36 -5
  6. package/src/main.ts +5 -0
  7. package/src/modules/AIChatContent.tsx +84 -0
  8. package/src/modules/AgentNoteActionsMenu.tsx +121 -0
  9. package/src/modules/AgentNoteBootstrapProgress.tsx +41 -0
  10. package/src/modules/AgentNoteItem.helpers.ts +115 -0
  11. package/src/modules/AgentNoteItem.tsx +232 -0
  12. package/src/modules/AgentNotes.constants.ts +10 -0
  13. package/src/modules/AgentNotes.helpers.ts +274 -0
  14. package/src/modules/AgentNotes.hooks.ts +449 -0
  15. package/src/modules/AgentNotes.tsx +74 -0
  16. package/src/modules/AgentNotes.types.ts +50 -0
  17. package/src/modules/AgentNotesCollection.hooks.ts +69 -0
  18. package/src/modules/AgentNotesSection.tsx +145 -0
  19. package/src/parser.ts +4 -0
  20. package/src/types/content.ts +103 -0
  21. package/src/ui/AIChatUI.css +557 -0
  22. package/src/ui/AIChatUI.tsx +816 -0
  23. package/src/ui/AgentNotesUI.tsx +375 -0
  24. package/src/ui/AgentNotesUI.types.ts +19 -0
  25. package/src/ui/ChatBlocks.tsx +92 -0
  26. package/src/ui/ChatCodeBlock.tsx +16 -0
  27. package/src/ui/ChatInline.helpers.ts +20 -0
  28. package/src/ui/ChatInline.tsx +89 -0
  29. package/src/ui/ChatTable.helpers.ts +37 -0
  30. package/src/ui/ChatTable.tsx +126 -0
  31. package/src/utils/contentIdentity.ts +108 -0
  32. package/src/utils/parseBlocks.helpers.ts +50 -0
  33. package/src/utils/parseBlocks.ts +266 -0
  34. package/src/utils/parseContent.helpers.ts +102 -0
  35. package/src/utils/parseContent.ts +33 -0
  36. package/src/utils/parseContentIncrementally.ts +46 -0
  37. package/src/utils/parseCsv.ts +99 -0
  38. package/src/utils/parseInline.helpers.ts +48 -0
  39. package/src/utils/parseInline.ts +396 -0
  40. package/src/utils/parseTable.ts +202 -0
  41. package/tests/AgentNotes.test.tsx +1672 -0
  42. package/tests/fixtures/content.ts +13 -0
  43. package/tests/parseContent.test.ts +227 -0
  44. package/tests/parseCsv.test.ts +82 -0
  45. package/tests/parseInline.test.ts +222 -0
  46. package/tests/renderContent.ssr.test.tsx +19 -0
  47. package/tests/renderContent.test.tsx +282 -0
  48. package/tests/streaming.test.ts +51 -0
  49. package/tests/streamingEquivalence.test.ts +265 -0
  50. package/tsconfig.json +16 -0
  51. package/vitest.config.ts +18 -0
@@ -0,0 +1,202 @@
1
+ import type {
2
+ ChatContentStatus,
3
+ ContentBlock,
4
+ ContentDiagnostic,
5
+ InlineNode,
6
+ ParserLimits,
7
+ SourceRange,
8
+ TableCell,
9
+ TableBlock,
10
+ TableRow,
11
+ } from '../types/content.ts';
12
+ import type { SourceLine } from './parseBlocks.helpers.ts';
13
+ import { parseInline } from './parseInline.ts';
14
+
15
+ export interface MarkdownTableResult {
16
+ block: ContentBlock;
17
+ nextLine: number;
18
+ diagnostics: ContentDiagnostic[];
19
+ }
20
+
21
+ const cellDiagnostics = new WeakMap<TableCell[], ContentDiagnostic[]>();
22
+
23
+ /*
24
+ * Decode table-specific pipe escapes inside code while retaining original node ranges.
25
+ */
26
+ function decodeCodePipes(nodes: InlineNode[]): InlineNode[] {
27
+ return nodes.map((node) => {
28
+ if (node.kind === 'code') return { ...node, value: node.value.replace(/\\\|/g, '|') };
29
+ if ('children' in node) return { ...node, children: decodeCodePipes(node.children) };
30
+ return node;
31
+ });
32
+ }
33
+
34
+ /*
35
+ * Locate structural unescaped pipes, including those inside inline code spans.
36
+ */
37
+ function pipePositions(text: string): number[] {
38
+ const positions: number[] = [];
39
+ for (let index = 0; index < text.length; index++) {
40
+ if (text[index] === '\\') index++;
41
+ else if (text[index] === '|') positions.push(index);
42
+ }
43
+ return positions;
44
+ }
45
+
46
+ /*
47
+ * Split Markdown cells while trimming only structural horizontal padding.
48
+ */
49
+ export function splitMarkdownRow(line: SourceLine): { ranges: SourceRange[]; hasPipe: boolean } {
50
+ const pipes = pipePositions(line.text);
51
+ let start = 0;
52
+ let end = line.text.length;
53
+ if (pipes.length && /^[ \t]*$/.test(line.text.slice(0, pipes[0]))) start = pipes.shift()! + 1;
54
+ if (pipes.length && /^[ \t]*$/.test(line.text.slice(pipes[pipes.length - 1] + 1))) end = pipes.pop()!;
55
+ const ranges: SourceRange[] = [];
56
+ for (const boundary of [...pipes, end]) {
57
+ let cellStart = start;
58
+ let cellEnd = boundary;
59
+ while (cellStart < cellEnd && /[ \t]/.test(line.text[cellStart])) cellStart++;
60
+ while (cellEnd > cellStart && /[ \t]/.test(line.text[cellEnd - 1])) cellEnd--;
61
+ ranges.push({ start: line.start + cellStart, end: line.start + cellEnd });
62
+ start = boundary + 1;
63
+ }
64
+ return { ranges, hasPipe: pipePositions(line.text).length > 0 };
65
+ }
66
+
67
+ /*
68
+ * Recognize only complete, width-matched table delimiters at top-level indentation.
69
+ */
70
+ export function markdownTableCandidate(source: string, lines: SourceLine[], index: number, status: ChatContentStatus) {
71
+ const header = lines[index];
72
+ const delimiter = lines[index + 1];
73
+ if (!header || !delimiter || !/^ {0,3}\S/.test(header.text) || !/^ {0,3}\S/.test(delimiter.text)) return null;
74
+ if (/^ {0,3}(?:>|(?:[-+*•]|\d{1,9}[.)])(?:[ \t]+|$))/.test(header.text)) return null;
75
+ if (status !== 'complete' && delimiter.end === delimiter.contentEnd && index + 2 === lines.length) return null;
76
+ const headerCells = splitMarkdownRow(header);
77
+ const delimiterCells = splitMarkdownRow(delimiter);
78
+ if (!headerCells.hasPipe && !delimiterCells.hasPipe) return null;
79
+ if (headerCells.ranges.length !== delimiterCells.ranges.length) return null;
80
+ const values = delimiterCells.ranges.map((range) => source.slice(range.start, range.end));
81
+ if (values.some((value) => !/^:?-+:?$/.test(value))) return null;
82
+ return {
83
+ headerCells: headerCells.ranges,
84
+ alignments: values.map((value) =>
85
+ value.startsWith(':') && value.endsWith(':') ? 'center' : value.endsWith(':') ? 'right' : value.startsWith(':') ? 'left' : null,
86
+ ) as ('left' | 'center' | 'right' | null)[],
87
+ };
88
+ }
89
+
90
+ /*
91
+ * Parse a recognized Markdown table, preserving malformed candidates and unfinished tails.
92
+ */
93
+ export function parseMarkdownTable(
94
+ source: string,
95
+ lines: SourceLine[],
96
+ index: number,
97
+ status: ChatContentStatus,
98
+ limits: ParserLimits,
99
+ isBoundary: (line: SourceLine) => boolean,
100
+ previous?: TableBlock,
101
+ ): MarkdownTableResult | null {
102
+ const candidate = markdownTableCandidate(source, lines, index, status);
103
+ if (!candidate) return null;
104
+ let nextLine = index + 2;
105
+ let failure: ContentDiagnostic | undefined;
106
+ const rows: TableRow[] = [];
107
+ const diagnostics: ContentDiagnostic[] = [];
108
+ let provisionalTail: (SourceRange & { source: string }) | undefined;
109
+
110
+ /*
111
+ * Parse bounded Markdown cells into inline content without interpreting block markers.
112
+ */
113
+ function cells(ranges: SourceRange[]): TableCell[] {
114
+ const diagnosticStart = diagnostics.length;
115
+ const parsed: TableCell[] = ranges.map((range) => ({
116
+ kind: 'inline',
117
+ ...range,
118
+ children: decodeCodePipes(parseInline(source.slice(range.start, range.end), range.start, limits, diagnostics)),
119
+ }));
120
+ cellDiagnostics.set(parsed, diagnostics.slice(diagnosticStart));
121
+ return parsed;
122
+ }
123
+
124
+ /*
125
+ * Reuse immutable cells and their original diagnostics only within a verified source prefix.
126
+ */
127
+ function retainedCells(cached: TableCell[] | undefined, ranges?: SourceRange[]) {
128
+ if (!cached || !cellDiagnostics.has(cached)) return undefined;
129
+ if (
130
+ ranges &&
131
+ (cached.length !== ranges.length ||
132
+ cached.some((cell, index) => cell.start !== ranges[index].start || cell.end !== ranges[index].end))
133
+ )
134
+ return undefined;
135
+ diagnostics.push(...cellDiagnostics.get(cached)!);
136
+ return cached;
137
+ }
138
+
139
+ /*
140
+ * Keep the first validation failure while consuming the complete candidate for fallback.
141
+ */
142
+ function fail(reason: string, range: SourceRange, code: ContentDiagnostic['code'] = 'limit-exceeded') {
143
+ failure ||= { ...range, code, reason };
144
+ }
145
+
146
+ if (candidate.headerCells.length > limits.tableColumns) fail('Table column limit exceeded.', lines[index]);
147
+ if (candidate.headerCells.some((cell) => cell.end - cell.start > limits.cellLength))
148
+ fail('Table cell length limit exceeded.', lines[index]);
149
+ const reusable = previous?.origin === 'markdown' ? previous : undefined;
150
+ const headers = failure ? [] : retainedCells(reusable?.headers, candidate.headerCells) || cells(candidate.headerCells);
151
+ while (nextLine < lines.length) {
152
+ const line = lines[nextLine];
153
+ if (!line.text.trim() || isBoundary(line) || !/^ {0,3}\S/.test(line.text)) break;
154
+ const previousRow = reusable?.rows[rows.length];
155
+ if (
156
+ !failure &&
157
+ previousRow?.start === line.start &&
158
+ previousRow.end === line.end &&
159
+ (line.end > line.contentEnd || status === 'complete' || nextLine + 1 < lines.length) &&
160
+ retainedCells(previousRow.cells)
161
+ ) {
162
+ rows.push(previousRow);
163
+ nextLine++;
164
+ continue;
165
+ }
166
+ const row = splitMarkdownRow(line);
167
+ if (!row.hasPipe) break;
168
+ nextLine++;
169
+ if (status !== 'complete' && nextLine === lines.length && line.end === line.contentEnd) {
170
+ provisionalTail = { start: line.start, end: line.end, source: source.slice(line.start, line.end) };
171
+ break;
172
+ }
173
+ if (row.ranges.length !== headers.length && !failure) fail('Markdown row width differs from its header.', line, 'malformed');
174
+ if (rows.length >= limits.tableRows) fail('Table row limit exceeded.', line);
175
+ if (row.ranges.some((cell) => cell.end - cell.start > limits.cellLength)) fail('Table cell length limit exceeded.', line);
176
+ if (!failure) rows.push({ start: line.start, end: line.end, cells: cells(row.ranges) });
177
+ }
178
+ const start = lines[index].start;
179
+ const end = lines[nextLine - 1].end;
180
+ const base = { start, end, source: source.slice(start, end), complete: !provisionalTail };
181
+ if (failure) return { block: { ...base, kind: 'literal', fallbackReason: failure.reason }, nextLine, diagnostics: [failure] };
182
+ if (provisionalTail)
183
+ diagnostics.push({
184
+ code: 'incomplete',
185
+ reason: 'Unfinished Markdown table row.',
186
+ start: provisionalTail.start,
187
+ end: provisionalTail.end,
188
+ });
189
+ return {
190
+ block: {
191
+ ...base,
192
+ kind: 'table',
193
+ origin: 'markdown',
194
+ headers,
195
+ rows,
196
+ alignments: candidate.alignments,
197
+ ...(provisionalTail ? { provisionalTail } : {}),
198
+ },
199
+ nextLine,
200
+ diagnostics,
201
+ };
202
+ }