chore: Refactor LLMStreamComplete class to improve code readability and maintainability
This commit is contained in:
parent
4a954a623a
commit
091a96e59f
@ -28,7 +28,17 @@ export class LLMStreamComplete {
|
|||||||
constructor(token: vscode.CancellationToken, contentLines: string[], curLineNum: number, curColumn: number) {
|
constructor(token: vscode.CancellationToken, contentLines: string[], curLineNum: number, curColumn: number) {
|
||||||
this.contentLines = contentLines;
|
this.contentLines = contentLines;
|
||||||
this.curLineNum = curLineNum;
|
this.curLineNum = curLineNum;
|
||||||
let curlineIndent = this.contentLines[curLineNum].search(/\S/);
|
let curlineIndent = 0;
|
||||||
|
for (let i = 0; i < this.contentLines[curLineNum].length; i++) {
|
||||||
|
if (this.contentLines[curLineNum][i] === ' ') {
|
||||||
|
curlineIndent += 1;
|
||||||
|
} else if (this.contentLines[curLineNum][i] === '\t') {
|
||||||
|
curlineIndent += 4;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (curlineIndent === -1) {
|
if (curlineIndent === -1) {
|
||||||
curlineIndent = this.contentLines[curLineNum].length;
|
curlineIndent = this.contentLines[curLineNum].length;
|
||||||
}
|
}
|
||||||
@ -55,6 +65,7 @@ export class LLMStreamComplete {
|
|||||||
async * chunkStopCanceled(chunks: AsyncIterable<CodeCompletionChunk>) {
|
async * chunkStopCanceled(chunks: AsyncIterable<CodeCompletionChunk>) {
|
||||||
for await (const chunk of chunks) {
|
for await (const chunk of chunks) {
|
||||||
if (this.token.isCancellationRequested) {
|
if (this.token.isCancellationRequested) {
|
||||||
|
logger.channel()?.trace("stop on canceled");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
yield chunk;
|
yield chunk;
|
||||||
@ -66,6 +77,7 @@ export class LLMStreamComplete {
|
|||||||
for await (const chunk of chunks) {
|
for await (const chunk of chunks) {
|
||||||
// 如果遇到特定的注释标记,则停止生成过程
|
// 如果遇到特定的注释标记,则停止生成过程
|
||||||
if(chunk.text.includes(`${commentPrefix}<filename>`)) {
|
if(chunk.text.includes(`${commentPrefix}<filename>`)) {
|
||||||
|
logger.channel()?.trace(`stopOnFilenameComment: ${chunk.text}`);
|
||||||
return; // 直接退出生成器函数
|
return; // 直接退出生成器函数
|
||||||
}
|
}
|
||||||
yield chunk;
|
yield chunk;
|
||||||
@ -77,6 +89,7 @@ export class LLMStreamComplete {
|
|||||||
for await (const chunk of chunks) {
|
for await (const chunk of chunks) {
|
||||||
// 如果遇到特定的注释标记,则停止生成过程
|
// 如果遇到特定的注释标记,则停止生成过程
|
||||||
if(chunk.text.includes(`//Code omitted...`)) {
|
if(chunk.text.includes(`//Code omitted...`)) {
|
||||||
|
logger.channel()?.trace(`stopOnOmittedCodeComment: ${chunk.text}`);
|
||||||
return; // 直接退出生成器函数
|
return; // 直接退出生成器函数
|
||||||
}
|
}
|
||||||
yield chunk;
|
yield chunk;
|
||||||
@ -92,6 +105,7 @@ export class LLMStreamComplete {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isFirst && chunk.text[0] === "\n") {
|
if (isFirst && chunk.text[0] === "\n") {
|
||||||
|
logger.channel()?.trace("stopWhenFirstCharIsNewLine");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
isFirst = false;
|
isFirst = false;
|
||||||
@ -139,16 +153,17 @@ export class LLMStreamComplete {
|
|||||||
async * stopAtFirstBrace(chunks: AsyncIterable<CodeCompletionChunk>) {
|
async * stopAtFirstBrace(chunks: AsyncIterable<CodeCompletionChunk>) {
|
||||||
let firstChunk = true;
|
let firstChunk = true;
|
||||||
for await (const chunk of chunks) {
|
for await (const chunk of chunks) {
|
||||||
|
yield chunk;
|
||||||
|
|
||||||
if (firstChunk) {
|
if (firstChunk) {
|
||||||
if (["}", "]", ")"].includes(chunk.text.trim())) {
|
if (["}", "]", ")"].includes(chunk.text.trim())) {
|
||||||
|
logger.channel()?.trace("stopAtFirstBrace: stop at first brace");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (chunk.text.trim().length > 0) {
|
if (chunk.text.trim().length > 0) {
|
||||||
firstChunk = false;
|
firstChunk = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
yield chunk;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,12 +173,14 @@ export class LLMStreamComplete {
|
|||||||
if (firstChunk) {
|
if (firstChunk) {
|
||||||
const curlineText = this.curLine + chunk.text;
|
const curlineText = this.curLine + chunk.text;
|
||||||
if (curlineText.trim() === this.nextLine.trim() && this.nextLine.trim().length > 5) {
|
if (curlineText.trim() === this.nextLine.trim() && this.nextLine.trim().length > 5) {
|
||||||
|
logger.channel()?.trace("stopAtFirstBrace: same with next line");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
firstChunk = false;
|
firstChunk = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chunk.text.trim() === this.nextLine.trim() && this.nextLine.trim().length > 1) {
|
if (chunk.text.trim() === this.nextLine.trim() && this.nextLine.trim().length > 1) {
|
||||||
|
logger.channel()?.trace("stopAtFirstBrace: same with next line");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,33 +201,55 @@ export class LLMStreamComplete {
|
|||||||
|
|
||||||
if (firstChunk) {
|
if (firstChunk) {
|
||||||
firstChunk = false;
|
firstChunk = false;
|
||||||
if (inMiddleLine) {
|
const chunkText = chunk.text.trim();
|
||||||
|
let isBrace = false;
|
||||||
|
|
||||||
|
if (chunkText.length > 0 && ["{", "(", "["].includes(chunkText.slice(-1))) {
|
||||||
|
isBrace = true;
|
||||||
|
}
|
||||||
|
if (inMiddleLine && !isBrace) {
|
||||||
|
logger.channel()?.trace("stopAtFirstBrace: inMiddleLine");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async * stopAtSameBlock(chunks: AsyncIterable<CodeCompletionChunk>) {
|
async * stopAtSameBlock(chunks: AsyncIterable<CodeCompletionChunk>) {
|
||||||
let index = 0;
|
let index = 0;
|
||||||
let preIndent = -1;
|
let preIndent = -1;
|
||||||
let hasIndentBigger = false;
|
let hasIndentBigger = false;
|
||||||
|
let firstChunk = true;
|
||||||
|
|
||||||
for await (const chunk of chunks) {
|
for await (const chunk of chunks) {
|
||||||
let lineIndent = chunk.text.search(/\S/);
|
let lineIndent = 0;
|
||||||
if (lineIndent === -1) {
|
for (let i=0; i<chunk.text.length; i++) {
|
||||||
lineIndent = this.curlineIndent;
|
if (chunk.text[i] === " ") {
|
||||||
|
lineIndent += 1;
|
||||||
|
} else if (chunk.text[i] === "\t") {
|
||||||
|
lineIndent += 4;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (index === 0) {
|
if (firstChunk) {
|
||||||
lineIndent = this.curlineIndent;
|
lineIndent = this.curlineIndent;
|
||||||
|
firstChunk = false;
|
||||||
|
} else {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index > 0 && chunk.text.trim().length > 0 && lineIndent < this.curlineIndent ) {
|
if (index > 0 && chunk.text.trim().length > 0 && lineIndent < this.curlineIndent ) {
|
||||||
|
logger.channel()?.trace(`stopAtSameBlock1: ${chunk.text} ${lineIndent} ${this.curlineIndent}`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (index > 0 && preIndent === 0 && lineIndent === 0) {
|
if (index > 0 && preIndent === 0 && lineIndent === 0) {
|
||||||
|
logger.channel()?.trace(`stopAtSameBlock2: ${chunk.text}`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (index > 0 && hasIndentBigger && lineIndent === this.curlineIndent && chunk.text.trim().length > 0 && ![')',']', '}'].includes(chunk.text.trim()[0])) {
|
if (index > 0 && hasIndentBigger && lineIndent === this.curlineIndent && chunk.text.trim().length > 0 && ![')',']', '}'].includes(chunk.text.trim()[0])) {
|
||||||
|
logger.channel()?.trace(`stopAtSameBlock3: ${chunk.text}`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (lineIndent > this.curlineIndent) {
|
if (lineIndent > this.curlineIndent) {
|
||||||
@ -263,7 +302,7 @@ export class LLMStreamComplete {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isAllMatch) {
|
if (isAllMatch && firstMatchLine + lines.length >= this.curLineNum) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
return lines;
|
return lines;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user