Elffuss BraidHub

kikocis/continue public

e31fcddb0dec · 4.7 KiBRawPermalink
1import { IDE, TabAutocompleteOptions } from "../..";
2import {
3 countTokens,
4 pruneLinesFromBottom,
5 pruneLinesFromTop,
6} from "../../llm/countTokens";
7import {
8 AutocompleteLanguageInfo,
9 languageForFilepath,
10} from "../constants/AutocompleteLanguageInfo";
11import { constructInitialPrefixSuffix } from "../templating/constructPrefixSuffix";
12
13import { AstPath, getAst, getTreePathAtCursor } from "./ast";
14import { AutocompleteInput } from "./types";
15
16/**
17 * A collection of variables that are often accessed throughout the autocomplete pipeline
18 * It's noisy to re-calculate all the time or inject them into each function
19 */
20export class HelperVars {
21 lang: AutocompleteLanguageInfo;
22 treePath: AstPath | undefined;
23 workspaceUris: string[] = [];
24
25 private _fileContents: string | undefined;
26 private _fileLines: string[] | undefined;
27 private _fullPrefix: string | undefined;
28 private _fullSuffix: string | undefined;
29 private _prunedPrefix: string | undefined;
30 private _prunedSuffix: string | undefined;
31
32 private constructor(
33 public readonly input: AutocompleteInput,
34 public readonly options: TabAutocompleteOptions,
35 public readonly modelName: string,
36 private readonly ide: IDE,
37 ) {
38 this.lang = languageForFilepath(input.filepath);
39 }
40
41 private async init() {
42 // Don't do anything if already initialized
43 if (this._fileContents !== undefined) {
44 return;
45 }
46 this.workspaceUris = await this.ide.getWorkspaceDirs();
47
48 this._fileContents =
49 this.input.manuallyPassFileContents ??
50 (await this.ide.readFile(this.filepath));
51
52 this._fileLines = this._fileContents.split("\n");
53
54 // Construct full prefix/suffix (a few edge cases handled in here)
55 const { prefix: fullPrefix, suffix: fullSuffix } =
56 await constructInitialPrefixSuffix(this.input, this.ide);
57 this._fullPrefix = fullPrefix;
58 this._fullSuffix = fullSuffix;
59
60 const { prunedPrefix, prunedSuffix } = this.prunePrefixSuffix();
61 this._prunedPrefix = prunedPrefix;
62 this._prunedSuffix = prunedSuffix;
63
64 try {
65 const ast = await getAst(this.filepath, fullPrefix + fullSuffix);
66 if (ast) {
67 this.treePath = await getTreePathAtCursor(ast, fullPrefix.length);
68 }
69 } catch (e) {
70 console.error("Failed to parse AST", e);
71 }
72 }
73
74 static async create(
75 input: AutocompleteInput,
76 options: TabAutocompleteOptions,
77 modelName: string,
78 ide: IDE,
79 ): Promise<HelperVars> {
80 const instance = new HelperVars(input, options, modelName, ide);
81 await instance.init();
82 return instance;
83 }
84
85 prunePrefixSuffix() {
86 // Construct basic prefix
87 const maxPrefixTokens =
88 this.options.maxPromptTokens * this.options.prefixPercentage;
89 const prunedPrefix = pruneLinesFromTop(
90 this.fullPrefix,
91 maxPrefixTokens,
92 this.modelName,
93 );
94
95 // Construct suffix
96 const maxSuffixTokens = Math.min(
97 this.options.maxPromptTokens - countTokens(prunedPrefix, this.modelName),
98 this.options.maxSuffixPercentage * this.options.maxPromptTokens,
99 );
100 const prunedSuffix = pruneLinesFromBottom(
101 this.fullSuffix,
102 maxSuffixTokens,
103 this.modelName,
104 );
105
106 return {
107 prunedPrefix,
108 prunedSuffix,
109 };
110 }
111
112 // Fast access
113 get filepath() {
114 return this.input.filepath;
115 }
116 get pos() {
117 return this.input.pos;
118 }
119
120 get prunedCaretWindow() {
121 return this.prunedPrefix + this.prunedSuffix;
122 }
123
124 // Getters for lazy access
125 get fileContents(): string {
126 if (this._fileContents === undefined) {
127 throw new Error(
128 "HelperVars must be initialized before accessing fileContents",
129 );
130 }
131 return this._fileContents;
132 }
133
134 get fileLines(): string[] {
135 if (this._fileLines === undefined) {
136 throw new Error(
137 "HelperVars must be initialized before accessing fileLines",
138 );
139 }
140 return this._fileLines;
141 }
142
143 get fullPrefix(): string {
144 if (this._fullPrefix === undefined) {
145 throw new Error(
146 "HelperVars must be initialized before accessing fullPrefix",
147 );
148 }
149 return this._fullPrefix;
150 }
151
152 get fullSuffix(): string {
153 if (this._fullSuffix === undefined) {
154 throw new Error(
155 "HelperVars must be initialized before accessing fullSuffix",
156 );
157 }
158 return this._fullSuffix;
159 }
160
161 get prunedPrefix(): string {
162 if (this._prunedPrefix === undefined) {
163 throw new Error(
164 "HelperVars must be initialized before accessing prunedPrefix",
165 );
166 }
167 return this._prunedPrefix;
168 }
169
170 get prunedSuffix(): string {
171 if (this._prunedSuffix === undefined) {
172 throw new Error(
173 "HelperVars must be initialized before accessing prunedSuffix",
174 );
175 }
176 return this._prunedSuffix;
177 }
178}