@opensumi/ide-ai-native 3.0.3 → 3.0.4-next-1716367246.0
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/lib/browser/inline-completions/completeProvider.d.ts +5 -3
- package/lib/browser/inline-completions/completeProvider.d.ts.map +1 -1
- package/lib/browser/inline-completions/completeProvider.js +16 -30
- package/lib/browser/inline-completions/completeProvider.js.map +1 -1
- package/lib/browser/inline-completions/constants.d.ts +2 -6
- package/lib/browser/inline-completions/constants.d.ts.map +1 -1
- package/lib/browser/inline-completions/constants.js +81 -4
- package/lib/browser/inline-completions/constants.js.map +1 -1
- package/lib/browser/inline-completions/prompt/const.d.ts +9 -0
- package/lib/browser/inline-completions/prompt/const.d.ts.map +1 -0
- package/lib/browser/inline-completions/prompt/const.js +284 -0
- package/lib/browser/inline-completions/prompt/const.js.map +1 -0
- package/lib/browser/inline-completions/prompt/importedFiles.d.ts +6 -0
- package/lib/browser/inline-completions/prompt/importedFiles.d.ts.map +1 -0
- package/lib/browser/inline-completions/prompt/importedFiles.js +77 -0
- package/lib/browser/inline-completions/prompt/importedFiles.js.map +1 -0
- package/lib/browser/inline-completions/prompt/jaccardMatcher.d.ts +31 -0
- package/lib/browser/inline-completions/prompt/jaccardMatcher.d.ts.map +1 -0
- package/lib/browser/inline-completions/prompt/jaccardMatcher.js +75 -0
- package/lib/browser/inline-completions/prompt/jaccardMatcher.js.map +1 -0
- package/lib/browser/inline-completions/prompt/languages.d.ts +4 -0
- package/lib/browser/inline-completions/prompt/languages.d.ts.map +1 -0
- package/lib/browser/inline-completions/prompt/languages.js +67 -0
- package/lib/browser/inline-completions/prompt/languages.js.map +1 -0
- package/lib/browser/inline-completions/prompt/matcher.d.ts +42 -0
- package/lib/browser/inline-completions/prompt/matcher.d.ts.map +1 -0
- package/lib/browser/inline-completions/prompt/matcher.js +279 -0
- package/lib/browser/inline-completions/prompt/matcher.js.map +1 -0
- package/lib/browser/inline-completions/prompt/prompt.d.ts +24 -0
- package/lib/browser/inline-completions/prompt/prompt.d.ts.map +1 -0
- package/lib/browser/inline-completions/prompt/prompt.js +242 -0
- package/lib/browser/inline-completions/prompt/prompt.js.map +1 -0
- package/lib/browser/inline-completions/prompt/similarSnippets.d.ts +9 -0
- package/lib/browser/inline-completions/prompt/similarSnippets.d.ts.map +1 -0
- package/lib/browser/inline-completions/prompt/similarSnippets.js +110 -0
- package/lib/browser/inline-completions/prompt/similarSnippets.js.map +1 -0
- package/lib/browser/inline-completions/prompt/tokenizer.d.ts +4 -0
- package/lib/browser/inline-completions/prompt/tokenizer.d.ts.map +1 -0
- package/lib/browser/inline-completions/prompt/tokenizer.js +18 -0
- package/lib/browser/inline-completions/prompt/tokenizer.js.map +1 -0
- package/lib/browser/inline-completions/provider.d.ts +5 -12
- package/lib/browser/inline-completions/provider.d.ts.map +1 -1
- package/lib/browser/inline-completions/provider.js +79 -44
- package/lib/browser/inline-completions/provider.js.map +1 -1
- package/lib/browser/inline-completions/types.d.ts +154 -0
- package/lib/browser/inline-completions/types.d.ts.map +1 -0
- package/lib/browser/inline-completions/types.js +59 -0
- package/lib/browser/inline-completions/types.js.map +1 -0
- package/lib/browser/languages/parser.d.ts +6 -0
- package/lib/browser/languages/parser.d.ts.map +1 -1
- package/lib/browser/languages/parser.js +140 -1
- package/lib/browser/languages/parser.js.map +1 -1
- package/lib/browser/languages/tree-sitter/language-facts/types.d.ts +1 -1
- package/lib/browser/languages/tree-sitter/wasm-manager.d.ts +2 -2
- package/lib/browser/languages/tree-sitter/wasm-manager.d.ts.map +1 -1
- package/lib/browser/languages/tree-sitter/wasm-manager.js +2 -2
- package/lib/browser/languages/tree-sitter/wasm-manager.js.map +1 -1
- package/lib/common/utils.d.ts +5 -0
- package/lib/common/utils.d.ts.map +1 -0
- package/lib/common/utils.js +26 -0
- package/lib/common/utils.js.map +1 -0
- package/package.json +20 -19
- package/src/browser/inline-completions/completeProvider.ts +26 -35
- package/src/browser/inline-completions/constants.ts +87 -7
- package/src/browser/inline-completions/prompt/const.ts +286 -0
- package/src/browser/inline-completions/prompt/importedFiles.ts +92 -0
- package/src/browser/inline-completions/prompt/jaccardMatcher.ts +98 -0
- package/src/browser/inline-completions/prompt/languages.ts +65 -0
- package/src/browser/inline-completions/prompt/matcher.ts +328 -0
- package/src/browser/inline-completions/prompt/prompt.ts +297 -0
- package/src/browser/inline-completions/prompt/similarSnippets.ts +130 -0
- package/src/browser/inline-completions/prompt/tokenizer.ts +16 -0
- package/src/browser/inline-completions/provider.ts +107 -42
- package/src/browser/inline-completions/types.ts +169 -0
- package/src/browser/languages/parser.ts +147 -0
- package/src/browser/languages/tree-sitter/wasm-manager.ts +3 -3
- package/src/common/utils.ts +23 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { URI } from '@opensumi/ide-core-common';
|
|
2
|
+
|
|
3
|
+
export enum TokenizerName {
|
|
4
|
+
cl100k_base = 'cl100k_base',
|
|
5
|
+
gpt2 = 'gpt2',
|
|
6
|
+
r50k_base = 'r50k_base',
|
|
7
|
+
p50k_base = 'p50k_base',
|
|
8
|
+
p50k_edit = 'p50k_edit',
|
|
9
|
+
o200k_base = 'o200k_base',
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export enum StrategyType {
|
|
13
|
+
InterceptBasedOnChar = 'InterceptBasedOnChar',
|
|
14
|
+
InterceptBasedOnLine = 'InterceptBasedOnLine',
|
|
15
|
+
InterceptBasedOnFunction = 'InterceptBasedOnFunction',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export enum SnippetSelectionOption {
|
|
19
|
+
BestMatch = 'bestMatch',
|
|
20
|
+
TopK = 'topK',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export enum NeighboringTabsOption {
|
|
24
|
+
openFileHistory = 'openFileHistory',
|
|
25
|
+
editFileRecent = 'editFileRecent',
|
|
26
|
+
editFileHistory = 'editFileHistory',
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export enum WishListAttributeName {
|
|
30
|
+
beforeCursor = 'beforeCursor',
|
|
31
|
+
afterCursor = 'afterCursor',
|
|
32
|
+
importedFile = 'importedFile',
|
|
33
|
+
languageMarker = 'languageMarker',
|
|
34
|
+
pathMarker = 'pathMarker',
|
|
35
|
+
similarFile = 'similarFile',
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Sort Option
|
|
39
|
+
export enum SortOption {
|
|
40
|
+
Ascending = 'ascending',
|
|
41
|
+
Descending = 'descending',
|
|
42
|
+
None = 'none',
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface BaseWishListType {
|
|
46
|
+
attributeName: WishListAttributeName;
|
|
47
|
+
enable?: boolean; // 默认为 true
|
|
48
|
+
ignoreDisabledStateHigherThanVersion?: string;
|
|
49
|
+
extOption?: { [key: string]: any };
|
|
50
|
+
priority: number;
|
|
51
|
+
strategy: StrategyType;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ImportedFileOptions {
|
|
55
|
+
maxTime: number;
|
|
56
|
+
importedFilesMaxNum: number;
|
|
57
|
+
language: string[];
|
|
58
|
+
maxPercent: number;
|
|
59
|
+
patternPrefix: string;
|
|
60
|
+
patternSuffix: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export enum SnippetSemantics {
|
|
64
|
+
Function = 'function',
|
|
65
|
+
Snippet = 'snippet',
|
|
66
|
+
Variable = 'variable',
|
|
67
|
+
Parameter = 'parameter',
|
|
68
|
+
Method = 'method',
|
|
69
|
+
Class = 'class',
|
|
70
|
+
Module = 'module',
|
|
71
|
+
Alias = 'alias',
|
|
72
|
+
Enum = 'enum member',
|
|
73
|
+
Interface = 'interface',
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface SimilarFileOptions {
|
|
77
|
+
maxPercent: number;
|
|
78
|
+
snippetSelectionMode: SnippetSelectionOption;
|
|
79
|
+
neighboringTabsMaxNum: number;
|
|
80
|
+
neighboringTabsOption: NeighboringTabsOption[];
|
|
81
|
+
patternPrefix: string;
|
|
82
|
+
patternSuffix: string;
|
|
83
|
+
similarityThreshold: number;
|
|
84
|
+
snippetMaxNum: number;
|
|
85
|
+
windowSize: number;
|
|
86
|
+
maxTime?: number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 补全配置项
|
|
91
|
+
* promptTokenSize = beforeCursorTokenSize + afterCursorTokenSize + importedFileTokenSize + pathMarkerTokenSize + languageMarkerTokenSize + similarFileTokenSize
|
|
92
|
+
* maxPromptTokenSize >= promptTokenSize
|
|
93
|
+
*/
|
|
94
|
+
export interface ICompletionModel {
|
|
95
|
+
maxPromptTokenSize: number;
|
|
96
|
+
tokenizerName: TokenizerName;
|
|
97
|
+
maxExecuteTimeMillSecond: number;
|
|
98
|
+
wishList: {
|
|
99
|
+
afterCursor: BaseWishListType & {
|
|
100
|
+
extOption: {
|
|
101
|
+
suffixPercent: number; // 0.25
|
|
102
|
+
minBlockSize: number; // 20
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
beforeCursor: BaseWishListType & {
|
|
106
|
+
extOption: {
|
|
107
|
+
prefixPercent: number; // 0.75,
|
|
108
|
+
minBlockSize: number; // 20,
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
importedFile: BaseWishListType & {
|
|
112
|
+
extOption: ImportedFileOptions;
|
|
113
|
+
};
|
|
114
|
+
languageMarker: BaseWishListType & {
|
|
115
|
+
extOption: {
|
|
116
|
+
language: string[];
|
|
117
|
+
maxPercent: number;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
pathMarker: BaseWishListType;
|
|
121
|
+
similarFile: BaseWishListType & {
|
|
122
|
+
extOption: SimilarFileOptions;
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface ICompletionContext {
|
|
128
|
+
prompt: string;
|
|
129
|
+
prefix: string;
|
|
130
|
+
suffix: string;
|
|
131
|
+
filename: string;
|
|
132
|
+
language: string;
|
|
133
|
+
uri: URI;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export interface CursorContext {
|
|
137
|
+
context: string;
|
|
138
|
+
lineCount: number;
|
|
139
|
+
tokenLength: number;
|
|
140
|
+
tokenizerName: TokenizerName;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface SimilarSnippet {
|
|
144
|
+
score: number;
|
|
145
|
+
startLine: number;
|
|
146
|
+
endLine: number;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface MatchSimilarSnippet extends SimilarSnippet {
|
|
150
|
+
snippet: string;
|
|
151
|
+
semantics: SnippetSemantics;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface ResourceDocument {
|
|
155
|
+
source: string;
|
|
156
|
+
uri: URI;
|
|
157
|
+
languageId: string;
|
|
158
|
+
offset: number;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface MarkerItem {
|
|
162
|
+
attributeName: WishListAttributeName;
|
|
163
|
+
priority: number;
|
|
164
|
+
maxPercent: number;
|
|
165
|
+
enable: boolean;
|
|
166
|
+
content?: string;
|
|
167
|
+
similarSnippets?: [string, MatchSimilarSnippet[]][];
|
|
168
|
+
importedFiles?: (string | string[])[][];
|
|
169
|
+
}
|
|
@@ -9,6 +9,8 @@ import { SupportedTreeSitterLanguages, TreeSitterLanguageFacts } from './tree-si
|
|
|
9
9
|
import { ICodeBlockInfo, IOtherBlockInfo } from './tree-sitter/language-facts/base';
|
|
10
10
|
import { WasmModuleManager } from './tree-sitter/wasm-manager';
|
|
11
11
|
|
|
12
|
+
export const DEFAULT_MIN_BLOCK_COUNT = 20;
|
|
13
|
+
|
|
12
14
|
@Injectable({ multiple: true })
|
|
13
15
|
export class LanguageParser implements IDisposable {
|
|
14
16
|
private parser: Parser;
|
|
@@ -220,6 +222,151 @@ export class LanguageParser implements IDisposable {
|
|
|
220
222
|
return null;
|
|
221
223
|
}
|
|
222
224
|
|
|
225
|
+
async trimSuffixSyntaxErrors(code: string, minBlockCount = DEFAULT_MIN_BLOCK_COUNT): Promise<string> {
|
|
226
|
+
await this.parserLoaded.promise;
|
|
227
|
+
const tree = this.parser?.parse(code);
|
|
228
|
+
const rootNode = tree?.rootNode;
|
|
229
|
+
if (!rootNode) {
|
|
230
|
+
return code;
|
|
231
|
+
}
|
|
232
|
+
const { namedChildren } = rootNode;
|
|
233
|
+
|
|
234
|
+
if (!rootNode.hasError || namedChildren.length <= minBlockCount) {
|
|
235
|
+
return this.trimToLastCompleteBlock(rootNode, true);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
let lastErrorNode: Parser.SyntaxNode | null = null;
|
|
239
|
+
let targetNode: Parser.SyntaxNode | null = null;
|
|
240
|
+
for (let i = namedChildren.length - 1; i >= minBlockCount; i--) {
|
|
241
|
+
const child = namedChildren[i];
|
|
242
|
+
if (child.hasError && i !== 0) {
|
|
243
|
+
lastErrorNode = child;
|
|
244
|
+
} else if (lastErrorNode) {
|
|
245
|
+
targetNode = child;
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (!targetNode) {
|
|
251
|
+
targetNode = namedChildren[minBlockCount - 1];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const lines = code.split('\n');
|
|
255
|
+
const remainingLines = lines.slice(0, targetNode.endPosition.row + 1);
|
|
256
|
+
const lastLine = remainingLines[remainingLines.length - 1].slice(0, targetNode.endPosition.column);
|
|
257
|
+
const correctCode = `${remainingLines.length > 1 ? `${remainingLines.slice(0, -1).join('\n')}\n` : ''}${lastLine}`;
|
|
258
|
+
return correctCode;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async trimPrefixSyntaxErrors(code: string, minBlockCount = DEFAULT_MIN_BLOCK_COUNT): Promise<string> {
|
|
262
|
+
await this.parserLoaded.promise;
|
|
263
|
+
const tree = this.parser?.parse(code);
|
|
264
|
+
const rootNode = tree?.rootNode;
|
|
265
|
+
if (!rootNode) {
|
|
266
|
+
return code;
|
|
267
|
+
}
|
|
268
|
+
const { namedChildren } = rootNode;
|
|
269
|
+
|
|
270
|
+
if (!rootNode.hasError || namedChildren.length <= minBlockCount) {
|
|
271
|
+
return this.trimToLastCompleteBlock(rootNode);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
let firstErrorNode: Parser.SyntaxNode | null = null;
|
|
275
|
+
let targetNode: Parser.SyntaxNode | null = null;
|
|
276
|
+
for (let i = 0; i < namedChildren.length - minBlockCount; i++) {
|
|
277
|
+
const child = namedChildren[i];
|
|
278
|
+
if (child.hasError && i !== namedChildren.length - 1) {
|
|
279
|
+
firstErrorNode = child;
|
|
280
|
+
} else if (firstErrorNode) {
|
|
281
|
+
targetNode = child;
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (!targetNode) {
|
|
287
|
+
targetNode = namedChildren[namedChildren.length - minBlockCount];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const lines = code.split('\n');
|
|
291
|
+
const remainingLines = lines.slice(targetNode.startPosition.row);
|
|
292
|
+
const firstLine = remainingLines[0].slice(targetNode.startPosition.column);
|
|
293
|
+
const correctCode = `${firstLine}${remainingLines.length > 1 ? `\n${remainingLines.slice(1).join('\n')}` : ''}`;
|
|
294
|
+
return correctCode;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
trimToLastCompleteBlock(rootNode: Parser.SyntaxNode, reverse = false) {
|
|
298
|
+
const { namedChildren } = rootNode;
|
|
299
|
+
if (reverse) {
|
|
300
|
+
const lastNamedChild = namedChildren[namedChildren.length - 2];
|
|
301
|
+
if (lastNamedChild) {
|
|
302
|
+
const lines = rootNode.text.split('\n');
|
|
303
|
+
const remainingLines = lines.slice(0, lastNamedChild.endPosition.row + 1);
|
|
304
|
+
const lastLine = remainingLines[remainingLines.length - 1].slice(0, lastNamedChild.endPosition.column);
|
|
305
|
+
const correctCode = `${
|
|
306
|
+
remainingLines.length > 1 ? `${remainingLines.slice(0, -1).join('\n')}\n` : ''
|
|
307
|
+
}${lastLine}`;
|
|
308
|
+
return correctCode;
|
|
309
|
+
}
|
|
310
|
+
} else {
|
|
311
|
+
const firstNamedChild = namedChildren[1];
|
|
312
|
+
if (firstNamedChild) {
|
|
313
|
+
const lines = rootNode.text.split('\n');
|
|
314
|
+
const remainingLines = lines.slice(firstNamedChild.startPosition.row);
|
|
315
|
+
const firstLine = remainingLines[0].slice(firstNamedChild.startPosition.column);
|
|
316
|
+
const correctCode = `${firstLine}${remainingLines.length > 1 ? `\n${remainingLines.slice(1).join('\n')}` : ''}`;
|
|
317
|
+
return correctCode;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return rootNode.text;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async extractImportPaths(code: string) {
|
|
324
|
+
const paths: string[] = [];
|
|
325
|
+
if (this.language === 'typescript') {
|
|
326
|
+
await this.parserLoaded.promise;
|
|
327
|
+
const tree = this.parser?.parse(code);
|
|
328
|
+
const rootNode = tree?.rootNode;
|
|
329
|
+
if (rootNode) {
|
|
330
|
+
for (let i = 0; i < rootNode?.childCount; i++) {
|
|
331
|
+
const sourceChild = rootNode.child(i);
|
|
332
|
+
if (sourceChild?.type === 'import_statement') {
|
|
333
|
+
let importPath = sourceChild.child(3)?.text;
|
|
334
|
+
if (importPath?.includes("'") || importPath?.includes('"')) {
|
|
335
|
+
importPath = importPath.slice(1, -1);
|
|
336
|
+
}
|
|
337
|
+
if (importPath) {
|
|
338
|
+
paths.push(importPath);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return paths;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async extractInterfaceOrTypeCode(code: string) {
|
|
348
|
+
const snippets: string[] = [];
|
|
349
|
+
if (this.language === 'typescript') {
|
|
350
|
+
await this.parserLoaded.promise;
|
|
351
|
+
const tree = this.parser?.parse(code);
|
|
352
|
+
const rootNode = tree?.rootNode;
|
|
353
|
+
if (rootNode) {
|
|
354
|
+
for (let i = 0; i < rootNode?.childCount; i++) {
|
|
355
|
+
const sourceChild = rootNode.child(i);
|
|
356
|
+
if (sourceChild?.type === 'export_statement') {
|
|
357
|
+
const exportChild = sourceChild.child(1);
|
|
358
|
+
if (exportChild?.type === 'interface_declaration' || exportChild?.type === 'type_alias_declaration') {
|
|
359
|
+
snippets.push(exportChild.text);
|
|
360
|
+
}
|
|
361
|
+
} else if (sourceChild?.type === 'interface_declaration' || sourceChild?.type === 'type_alias_declaration') {
|
|
362
|
+
snippets.push(sourceChild.text);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return snippets;
|
|
368
|
+
}
|
|
369
|
+
|
|
223
370
|
dispose() {
|
|
224
371
|
this.parser.delete();
|
|
225
372
|
this.lruCache.clear();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Parser from 'web-tree-sitter';
|
|
2
2
|
|
|
3
3
|
import { Autowired, Injectable } from '@opensumi/di';
|
|
4
|
-
import { EKnownResources,
|
|
4
|
+
import { EKnownResources, RendererRuntime } from '@opensumi/ide-core-browser/lib/application/runtime/types';
|
|
5
5
|
import { Deferred } from '@opensumi/ide-utils';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -13,8 +13,8 @@ export class WasmModuleManager {
|
|
|
13
13
|
|
|
14
14
|
private cachedRuntime: Map<string, Deferred<ArrayBuffer>> = new Map();
|
|
15
15
|
|
|
16
|
-
@Autowired(
|
|
17
|
-
rendererRuntime:
|
|
16
|
+
@Autowired(RendererRuntime)
|
|
17
|
+
rendererRuntime: RendererRuntime;
|
|
18
18
|
|
|
19
19
|
private async resolveResourceUri() {
|
|
20
20
|
const uri = await this.rendererRuntime.provideResourceUri(EKnownResources.TreeSitterWasmDirectory);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { IEditorDocumentModel } from '@opensumi/ide-editor';
|
|
2
|
+
|
|
3
|
+
export const isDocumentTooLarge = (document: IEditorDocumentModel) => {
|
|
4
|
+
try {
|
|
5
|
+
document.getText();
|
|
6
|
+
} catch (e) {
|
|
7
|
+
if (e instanceof RangeError) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return false;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const MIN_PROMPT_CHARS = 10;
|
|
15
|
+
|
|
16
|
+
export const isDocumentTooShort = (document: IEditorDocumentModel) => document.getText().length < MIN_PROMPT_CHARS;
|
|
17
|
+
|
|
18
|
+
export const isDocumentValid = (document: IEditorDocumentModel) => {
|
|
19
|
+
if (isDocumentTooLarge(document) || isDocumentTooShort(document)) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
};
|