File size: 688 Bytes
65567a2
 
6c2bcb4
65567a2
 
 
 
 
 
 
 
 
6c2bcb4
 
 
 
 
 
 
65567a2
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import GPT3Tokenizer from "gpt3-tokenizer";
import prettier from "prettier";
import parserBabel from "prettier/parser-babel";

export const tokenizer = new GPT3Tokenizer({ type: "gpt3" });

export function getTokens(text: string) {
	return tokenizer.encode(text).bpe.length;
}

export function prettify(code: string) {
	try {
		return prettier.format(code, {
			useTabs: true,
			semi: true,
			parser: "babel",
			plugins: [parserBabel],
		});
	} catch (error) {
		return code;
	}
}

export function getTheme(mode: string | undefined, systemMode: string | undefined) {
	if (mode === "system") {
		return `vs-${systemMode}`;
	}
	if (mode) {
		return `vs-${mode}`;
	}
	return undefined;
}