@@ -29,71 +29,21 @@ export async function autocomplete(args: {
2929 // Receiving tokens
3030 let res = '' ;
3131 let totalLines = 1 ;
32- let blockStack : ( '[' | '(' | '{' ) [ ] = [ ] ;
33- outer: for await ( let tokens of ollamaTokenGenerator ( args . endpoint + '/api/generate' , data , args . bearerToken ) ) {
32+ for await ( let tokens of ollamaTokenGenerator ( args . endpoint + '/api/generate' , data , args . bearerToken ) ) {
3433 if ( args . canceled && args . canceled ( ) ) {
3534 break ;
3635 }
3736
38- // Block stack
39- for ( let c of tokens . response ) {
40-
41- // Open block
42- if ( c === '[' ) {
43- blockStack . push ( '[' ) ;
44- } else if ( c === '(' ) {
45- blockStack . push ( '(' ) ;
46- }
47- if ( c === '{' ) {
48- blockStack . push ( '{' ) ;
49- }
50-
51- // Close block
52- if ( c === ']' ) {
53- if ( blockStack . length > 0 && blockStack [ blockStack . length - 1 ] === '[' ) {
54- blockStack . pop ( ) ;
55- } else {
56- info ( 'Block stack error, breaking.' ) ;
57- break outer;
58- }
59- }
60- if ( c === ')' ) {
61- if ( blockStack . length > 0 && blockStack [ blockStack . length - 1 ] === '(' ) {
62- blockStack . pop ( ) ;
63- } else {
64- info ( 'Block stack error, breaking.' ) ;
65- break outer;
66- }
67- }
68- if ( c === '}' ) {
69- if ( blockStack . length > 0 && blockStack [ blockStack . length - 1 ] === '{' ) {
70- blockStack . pop ( ) ;
71- } else {
72- info ( 'Block stack error, breaking.' ) ;
73- break outer;
74- }
75- }
76-
77- // Append charater
78- res += c ;
79- }
37+ res = res + tokens . response ;
8038
8139 // Update total lines
8240 totalLines += countSymbol ( tokens . response , '\n' ) ;
8341 // Break if too many lines and on top level
84- if ( totalLines > args . maxLines && blockStack . length === 0 ) {
42+ if ( totalLines > args . maxLines ) {
8543 info ( 'Too many lines, breaking.' ) ;
8644 break ;
8745 }
8846 }
8947
90- // Remove <EOT>
91- if ( res . endsWith ( '<EOT>' ) ) {
92- res = res . slice ( 0 , res . length - 5 ) ;
93- }
94-
95- // Trim ends of all lines since sometimes the AI completion will add extra spaces
96- res = res . split ( '\n' ) . map ( ( v ) => v . trimEnd ( ) ) . join ( '\n' ) ;
97-
9848 return res ;
9949}
0 commit comments