Elffuss BraidHub

kikocis/continue public

2d2c10bf01a5 · 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
47 this.workspaceUris = await this.ide.getWorkspaceDirs();
48
49 this._fileContents =
50 this.input.manuallyPassFileContents ??
51 (await this.ide.readFile(this.filepath));
52
53 this._fileLines = this._fileContents.split("\n");
54
55 // Construct full prefix/suffix (a few edge cases handled in here)
56 const { prefix: fullPrefix, suffix: fullSuffix } =
57 await constructInitialPrefixSuffix(this.input, this.ide);
58 this._fullPrefix = fullPrefix;
59 this._fullSuffix = fullSuffix;
60
61 const { prunedPrefix, prunedSuffix } = this.prunePrefixSuffix();
62 this._prunedPrefix = prunedPrefix;
63 this._prunedSuffix = prunedSuffix;
64
65 try {
66 const ast = await getAst(this.filepath, fullPrefix + fullSuffix);
67 if (ast) {
68 this.treePath = await getTreePathAtCursor(ast, fullPrefix.length);
69 }
70 } catch (e) {
71 console.error("Failed to parse AST", e);
72 }
73 }
74
75 static async create(
76 input: AutocompleteInput,
77 options: TabAutocompleteOptions,
78 modelName: string,
79 ide: IDE,
80 ): Promise<HelperVars> {
81 const instance = new HelperVars(input, options, modelName, ide);
82 await instance.init();
83 return instance;
84 }
85
86 prunePrefixSuffix() {
87 // Construct basic prefix
88 const maxPrefixTokens =
89 this.options.maxPromptTokens * this.options.prefixPercentage;
90 const prunedPrefix = pruneLinesFromTop(
91 this.fullPrefix,
92 maxPrefixTokens,
93 this.modelName,
94 );
95
96 // Construct suffix
97 const maxSuffixTokens = Math.min(
98 this.options.maxPromptTokens - countTokens(prunedPrefix, this.modelName),
99 this.options.maxSuffixPercentage * this.options.maxPromptTokens,
100 );
101 const prunedSuffix = pruneLinesFromBottom(
102 this.fullSuffix,
103 maxSuffixTokens,
104 this.modelName,
105 );
106
107 return {
108 prunedPrefix,
109 prunedSuffix,
110 };
111 }
112
113 // Fast access
114 get filepath() {
115 return this.input.filepath;
116 }
117 get pos() {
118 return this.input.pos;
119 }
120
121 get prunedCaretWindow() {
122 return this.prunedPrefix + this.prunedSuffix;
123 }
124
125 // Getters for lazy access
126 get fileContents(): string {
127 if (this._fileContents === undefined) {
128 throw new Error(
129 "HelperVars must be initialized before accessing fileContents",
130 );
131 }
132 return this._fileContents;
133 }
134
135 get fileLines(): string[] {
136 if (this._fileLines === undefined) {
137 throw new Error(
138 "HelperVars must be initialized before accessing fileLines",
139 );
140 }
141 return this._fileLines;
142 }
143
144 get fullPrefix(): string {
145 if (this._fullPrefix === undefined) {
146 throw new Error(
147 "HelperVars must be initialized before accessing fullPrefix",
148 );
149 }
150 return this._fullPrefix;
151 }
152
153 get fullSuffix(): string {
154 if (this._fullSuffix === undefined) {
155 throw new Error(
156 "HelperVars must be initialized before accessing fullSuffix",
157 );
158 }
159 return this._fullSuffix;
160 }
161
162 get prunedPrefix(): string {
163 if (this._prunedPrefix === undefined) {
164 throw new Error(
165 "HelperVars must be initialized before accessing prunedPrefix",
166 );
167 }
168 return this._prunedPrefix;
169 }
170
171 get prunedSuffix(): string {
172 if (this._prunedSuffix === undefined) {
173 throw new Error(
174 "HelperVars must be initialized before accessing prunedSuffix",
175 );
176 }
177 return this._prunedSuffix;
178 }
179}