@copilotkitnext/core 0.0.2 → 0.0.4

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.
@@ -1,271 +0,0 @@
1
- export function completePartialMarkdown(input: string): string {
2
- let s = input;
3
-
4
- // Handle code fences first - use FIRST unmatched fence for proper nesting
5
- const fenceMatches = Array.from(s.matchAll(/^(\s*)(`{3,}|~{3,})/gm));
6
- if (fenceMatches.length % 2 === 1) {
7
- const [, indent, fence] = fenceMatches[0]!;
8
- s += `\n${indent}${fence}`;
9
- }
10
-
11
- // Identify incomplete links at the end and close them
12
- const incompleteLinkMatch = s.match(/\[([^\]]*)\]\(([^)]*)$/);
13
- if (incompleteLinkMatch) {
14
- s += ")";
15
- }
16
-
17
- // State-based parsing
18
- interface OpenElement {
19
- type: string;
20
- marker: string;
21
- position: number;
22
- }
23
-
24
- const openElements: OpenElement[] = [];
25
- const chars = Array.from(s);
26
-
27
- // First pass: identify code block boundaries and inline code to avoid processing their content
28
- const codeBlockRanges: Array<{ start: number; end: number }> = [];
29
- const inlineCodeRanges: Array<{ start: number; end: number }> = [];
30
-
31
- // Find code block ranges
32
- let tempCodeFenceCount = 0;
33
- let currentCodeBlockStart = -1;
34
-
35
- for (let i = 0; i < chars.length; i++) {
36
- if (i === 0 || chars[i - 1] === "\n") {
37
- const lineMatch = s.substring(i).match(/^(\s*)(`{3,}|~{3,})/);
38
- if (lineMatch) {
39
- tempCodeFenceCount++;
40
- if (tempCodeFenceCount % 2 === 1) {
41
- currentCodeBlockStart = i;
42
- } else if (currentCodeBlockStart !== -1) {
43
- codeBlockRanges.push({
44
- start: currentCodeBlockStart,
45
- end: i + lineMatch[0].length,
46
- });
47
- currentCodeBlockStart = -1;
48
- }
49
- i += lineMatch[0].length - 1;
50
- }
51
- }
52
- }
53
-
54
- // Find inline code ranges
55
- for (let i = 0; i < chars.length; i++) {
56
- if (chars[i] === "`") {
57
- // Check if escaped
58
- let backslashCount = 0;
59
- for (let j = i - 1; j >= 0 && chars[j] === "\\"; j--) {
60
- backslashCount++;
61
- }
62
- if (backslashCount % 2 === 0) {
63
- // Not escaped - find the closing backtick
64
- for (let j = i + 1; j < chars.length; j++) {
65
- if (chars[j] === "`") {
66
- let closingBackslashCount = 0;
67
- for (let k = j - 1; k >= 0 && chars[k] === "\\"; k--) {
68
- closingBackslashCount++;
69
- }
70
- if (closingBackslashCount % 2 === 0) {
71
- inlineCodeRanges.push({ start: i, end: j + 1 });
72
- i = j;
73
- break;
74
- }
75
- }
76
- }
77
- }
78
- }
79
- }
80
-
81
- // Helper function to check if position is in code
82
- const isInCode = (pos: number): boolean => {
83
- return (
84
- codeBlockRanges.some((range) => pos >= range.start && pos < range.end) ||
85
- inlineCodeRanges.some((range) => pos >= range.start && pos < range.end)
86
- );
87
- };
88
-
89
- // Second pass: process markdown elements, skipping code regions
90
- for (let i = 0; i < chars.length; i++) {
91
- const char = chars[i];
92
- const nextChar = chars[i + 1];
93
- const prevChar = chars[i - 1];
94
-
95
- if (isInCode(i)) {
96
- continue;
97
- }
98
-
99
- // Handle brackets (but not if they're part of already-complete links)
100
- if (char === "[") {
101
- // Check if this is part of a complete link [text](url)
102
- let isCompleteLink = false;
103
- let bracketDepth = 1;
104
- let j = i + 1;
105
-
106
- // Find the matching ]
107
- while (j < chars.length && bracketDepth > 0) {
108
- if (chars[j] === "[" && !isInCode(j)) bracketDepth++;
109
- if (chars[j] === "]" && !isInCode(j)) bracketDepth--;
110
- j++;
111
- }
112
-
113
- // Check if followed by (
114
- if (bracketDepth === 0 && chars[j] === "(") {
115
- // Find the closing )
116
- let parenDepth = 1;
117
- j++;
118
- while (j < chars.length && parenDepth > 0) {
119
- if (chars[j] === "(" && !isInCode(j)) parenDepth++;
120
- if (chars[j] === ")" && !isInCode(j)) parenDepth--;
121
- j++;
122
- }
123
- if (parenDepth === 0) {
124
- isCompleteLink = true;
125
- i = j - 1;
126
- continue;
127
- }
128
- }
129
-
130
- // This is a standalone bracket, treat as markdown
131
- if (!isCompleteLink) {
132
- const existingIndex = openElements.findIndex(
133
- (el) => el.type === "bracket"
134
- );
135
- if (existingIndex !== -1) {
136
- openElements.splice(existingIndex, 1);
137
- } else {
138
- openElements.push({ type: "bracket", marker: "[", position: i });
139
- }
140
- }
141
- }
142
-
143
- // Handle double emphasis first (**, __, ~~) - these take precedence
144
- else if (char === "*" && nextChar === "*") {
145
- const existingIndex = openElements.findIndex(
146
- (el) => el.type === "bold_star"
147
- );
148
- if (existingIndex !== -1) {
149
- openElements.splice(existingIndex, 1);
150
- } else {
151
- openElements.push({ type: "bold_star", marker: "**", position: i });
152
- }
153
- i++; // Skip next character
154
- } else if (char === "_" && nextChar === "_") {
155
- const existingIndex = openElements.findIndex(
156
- (el) => el.type === "bold_underscore"
157
- );
158
- if (existingIndex !== -1) {
159
- openElements.splice(existingIndex, 1);
160
- } else {
161
- openElements.push({
162
- type: "bold_underscore",
163
- marker: "__",
164
- position: i,
165
- });
166
- }
167
- i++; // Skip next character
168
- } else if (char === "~" && nextChar === "~") {
169
- const existingIndex = openElements.findIndex(
170
- (el) => el.type === "strike"
171
- );
172
- if (existingIndex !== -1) {
173
- openElements.splice(existingIndex, 1);
174
- } else {
175
- openElements.push({ type: "strike", marker: "~~", position: i });
176
- }
177
- i++; // Skip next character
178
- }
179
-
180
- // Handle single emphasis (*, _) - only if not part of double
181
- else if (char === "*" && prevChar !== "*" && nextChar !== "*") {
182
- const existingIndex = openElements.findIndex(
183
- (el) => el.type === "italic_star"
184
- );
185
- if (existingIndex !== -1) {
186
- openElements.splice(existingIndex, 1);
187
- } else {
188
- openElements.push({ type: "italic_star", marker: "*", position: i });
189
- }
190
- } else if (char === "_" && prevChar !== "_" && nextChar !== "_") {
191
- const existingIndex = openElements.findIndex(
192
- (el) => el.type === "italic_underscore"
193
- );
194
- if (existingIndex !== -1) {
195
- openElements.splice(existingIndex, 1);
196
- } else {
197
- openElements.push({
198
- type: "italic_underscore",
199
- marker: "_",
200
- position: i,
201
- });
202
- }
203
- }
204
- }
205
-
206
- // Handle remaining unmatched backticks (outside of inline code ranges)
207
- let backtickCount = 0;
208
- for (let i = 0; i < chars.length; i++) {
209
- if (chars[i] === "`" && !isInCode(i)) {
210
- backtickCount++;
211
- }
212
- }
213
- if (backtickCount % 2 === 1) {
214
- s += "`";
215
- }
216
-
217
- // Close remaining open elements in reverse order (LIFO stack semantics)
218
- openElements.sort((a, b) => b.position - a.position);
219
-
220
- const closers = openElements.map((el) => {
221
- switch (el.type) {
222
- case "bracket":
223
- return "]";
224
- case "bold_star":
225
- return "**";
226
- case "bold_underscore":
227
- return "__";
228
- case "strike":
229
- return "~~";
230
- case "italic_star":
231
- return "*";
232
- case "italic_underscore":
233
- return "_";
234
- default:
235
- return "";
236
- }
237
- });
238
-
239
- let result = s + closers.join("");
240
-
241
- // Handle parentheses ONLY if not inside code
242
- const finalFenceMatches = Array.from(
243
- result.matchAll(/^(\s*)(`{3,}|~{3,})/gm)
244
- );
245
- const hasUnclosedBacktick = (result.match(/`/g) || []).length % 2 === 1;
246
- const hasUnclosedCodeFence = finalFenceMatches.length % 2 === 1;
247
-
248
- let shouldCloseParens = !hasUnclosedBacktick && !hasUnclosedCodeFence;
249
-
250
- if (shouldCloseParens) {
251
- const lastOpenParen = result.lastIndexOf("(");
252
- if (lastOpenParen !== -1) {
253
- // Check if this paren is inside a backtick pair
254
- const beforeParen = result.substring(0, lastOpenParen);
255
- const backticksBeforeParen = (beforeParen.match(/`/g) || []).length;
256
- if (backticksBeforeParen % 2 === 1) {
257
- shouldCloseParens = false;
258
- }
259
- }
260
- }
261
-
262
- if (shouldCloseParens) {
263
- const openParens = (result.match(/\(/g) || []).length;
264
- const closeParens = (result.match(/\)/g) || []).length;
265
- if (openParens > closeParens) {
266
- result += ")".repeat(openParens - closeParens);
267
- }
268
- }
269
-
270
- return result;
271
- }
package/tsconfig.json DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "extends": "@copilotkitnext/typescript-config/base.json",
3
- "compilerOptions": {
4
- "outDir": "dist",
5
- "rootDir": "src",
6
- "declaration": true,
7
- "declarationMap": true,
8
- "sourceMap": true
9
- },
10
- "include": ["src/**/*"],
11
- "exclude": ["dist", "node_modules"]
12
- }
package/tsup.config.ts DELETED
@@ -1,11 +0,0 @@
1
- import { defineConfig } from 'tsup';
2
-
3
- export default defineConfig({
4
- entry: ['src/index.ts'],
5
- format: ['cjs', 'esm'],
6
- dts: true,
7
- sourcemap: true,
8
- clean: true,
9
- target: 'es2022',
10
- outDir: 'dist',
11
- });