@@ -5,8 +5,9 @@ import vegaEmbed from 'vega-embed';
5
5
import { Prism } from "./prism" ;
6
6
import Tablesort from "./tablesort" ;
7
7
import { optimizeCode } from "./optimizations" ;
8
- import { memory_consumed_str , time_consumed_str } from "./utils" ;
8
+ import { unescapeUnicode , countSpaces , memory_consumed_str , time_consumed_str } from "./utils" ;
9
9
import { makeBar , makeGPUPie , makeMemoryPie , makeMemoryBar , makeSparkline } from "./gui-elements" ;
10
+ import { isValidApiKey , checkApiKey } from "./openai" ;
10
11
11
12
export function vsNavigate ( filename , lineno ) {
12
13
// If we are in VS Code, clicking on a line number in Scalene's web UI will navigate to that line in the source code.
@@ -32,76 +33,6 @@ const maxLinesPerRegion = 50; // Only show regions that are no more than this ma
32
33
33
34
let showedExplosion = { } ; // Used so we only show one explosion per region.
34
35
35
- function unescapeUnicode ( s ) {
36
- return s . replace ( / \\ u ( [ \d A - F ] { 4 } ) / gi, function ( match , p1 ) {
37
- return String . fromCharCode ( parseInt ( p1 , 16 ) ) ;
38
- } ) ;
39
- }
40
-
41
- async function tryApi ( apiKey ) {
42
- const response = await fetch ( "https://api.openai.com/v1/completions" , {
43
- method : "GET" ,
44
- headers : {
45
- "Content-Type" : "application/json" ,
46
- Authorization : `Bearer ${ apiKey } ` ,
47
- } ,
48
- } ) ;
49
- return response ;
50
- }
51
-
52
- async function isValidApiKey ( apiKey ) {
53
- const response = await tryApi ( apiKey ) ;
54
- const data = await response . json ( ) ;
55
- if (
56
- data . error &&
57
- data . error . code in
58
- {
59
- invalid_api_key : true ,
60
- invalid_request_error : true ,
61
- model_not_found : true ,
62
- insufficient_quota : true ,
63
- }
64
- ) {
65
- return false ;
66
- } else {
67
- return true ;
68
- }
69
- }
70
-
71
- function checkApiKey ( apiKey ) {
72
- ( async ( ) => {
73
- try {
74
- window . localStorage . setItem ( "scalene-api-key" , apiKey ) ;
75
- } catch {
76
- // Do nothing if key not found
77
- }
78
- // If the API key is empty, clear the status indicator.
79
- if ( apiKey . length === 0 ) {
80
- document . getElementById ( "valid-api-key" ) . innerHTML = "" ;
81
- return ;
82
- }
83
- const isValid = await isValidApiKey ( apiKey ) ;
84
- if ( ! isValid ) {
85
- document . getElementById ( "valid-api-key" ) . innerHTML = "✕" ;
86
- } else {
87
- document . getElementById ( "valid-api-key" ) . innerHTML = "✓" ;
88
- }
89
- } ) ( ) ;
90
- }
91
-
92
- function countSpaces ( str ) {
93
- // Use a regular expression to match any whitespace character at the start of the string
94
- const match = str . match ( / ^ \s + / ) ;
95
-
96
- // If there was a match, return the length of the match
97
- if ( match ) {
98
- return match [ 0 ] . length ;
99
- }
100
-
101
- // Otherwise, return 0
102
- return 0 ;
103
- }
104
-
105
36
export function proposeOptimizationRegion ( filename , file_number , line ) {
106
37
proposeOptimization (
107
38
filename ,
0 commit comments