forked from getcursor/cursor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
268 lines (244 loc) · 8.18 KB
/
utils.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
export const API_ROOT = 'https://aicursor.com'
export const HOMEPAGE_ROOT = 'https://cursor.so'
export class ExpectedBackendError extends Error {
public title: string | null = null
}
export class NoAuthRateLimitError extends ExpectedBackendError {
constructor(
message = "You've reached the rate limit for unauthenticated requests. Please log in to continue."
) {
super(message)
this.name = 'NoAuthRateLimitError'
this.title = 'Please log in to continue...'
}
}
export class AuthRateLimitError extends ExpectedBackendError {
constructor(
message = "It seems like you're making an unusual number of AI requests. Please try again later. If you think this is a mistake, please contact [email protected]"
) {
super(message)
this.name = 'AuthRateLimitError'
this.title = "You're going a bit fast..."
}
}
export class NoAuthLocalRateLimitError extends ExpectedBackendError {
constructor(
message = 'To protect our backend, we ask that free users limit their usage to 30 prompts per hour. To raise this limit, feel free to upgrade to pro.'
) {
super(message)
this.name = 'NoAuthLocalRateLimitError'
this.title = "You're going a bit fast..."
}
}
export class NoAuthGlobalOldRateLimitError extends ExpectedBackendError {
constructor(
message = "If you've enjoyed using Cursor, please consider subscribing to one of our paid plans. Otherwise, you can enter your Open AI key (gear icon) to continue using the AI features at-cost."
) {
super(message)
this.name = 'NoAuthGlobalOldRateLimitError'
this.title = 'Free tier limit exceeded'
}
}
export class NoAuthGlobalNewRateLimitError extends ExpectedBackendError {
constructor(
message = "We're currently experiencing a high volume of requests. Please try again in a few minutes. For support, please contact [email protected]."
) {
super(message)
this.name = 'NoAuthGlobalNewRateLimitError'
this.title = 'Our servers are overloaded...'
}
}
export class OpenAIError extends ExpectedBackendError {}
export class BadOpenAIAPIKeyError extends OpenAIError {
constructor(
message = 'The provided OpenAI API key is invalid. Please provide a valid API key.'
) {
super(message)
this.name = 'BadOpenAIAPIKeyError'
}
}
export class BadModelError extends ExpectedBackendError {
constructor(
message = 'The provided model ID is invalid. Please provide a valid model ID.'
) {
super(message)
this.name = 'BadModelError'
}
}
export class NotLoggedInError extends ExpectedBackendError {
constructor(message = 'You are not logged in. Please log in to continue.') {
super(message)
this.name = 'NotLoggedInError'
}
}
export type ExpectedError =
| NoAuthRateLimitError
| AuthRateLimitError
| NoAuthLocalRateLimitError
| NoAuthGlobalOldRateLimitError
| NoAuthGlobalNewRateLimitError
| BadOpenAIAPIKeyError
| BadModelError
| NotLoggedInError
export async function fetchWithCookies(url: string, options: RequestInit = {}) {
const response = await fetch(url, options)
// Get the cookies
const cookies = response.headers.get('Set-Cookie')
if (cookies) {
console.log(cookies)
const [name, value] = cookies.split('=')
await connector.setCookies({
url: url,
name,
value,
})
}
return response
}
export async function* streamSource(response: Response): AsyncGenerator<any> {
if (response.status == 429) {
// Check the error text
if (response.statusText == 'NO_AUTH') {
throw new NoAuthRateLimitError()
} else {
throw new AuthRateLimitError()
}
}
// Check if the response is an event-stream
if (
response.headers.get('content-type') ==
'text/event-stream; charset=utf-8'
) {
// Create a reader to read the response body as a stream
// const reader = response.body.getReader();
// Fix the above error: `response.body is possibly null`
const reader = response.body!.getReader()
// Create a decoder to decode the stream as UTF-8 text
const decoder = new TextDecoder('utf-8')
// Loop until the stream is done
while (true) {
const { value, done } = await reader.read()
if (done) {
break
}
const rawValue = decoder.decode(value)
const lines = rawValue.split('\n')
for (const line of lines) {
if (line.startsWith('data: ')) {
const jsonString = line.slice(6)
if (jsonString == '[DONE]') {
return
}
yield JSON.parse(jsonString)
}
}
}
} else {
// Raise exception
throw new Error('Response is not an event-stream')
}
}
// Another streaming function similar to streamSource, but slightly different
export async function* anotherStreamSource(
response: Response
): AsyncGenerator<any> {
// Check if the response is an event-stream
if (
response.headers.get('content-type') ==
'text/event-stream; charset=utf-8'
) {
// Create a reader to read the response body as a stream
const reader = response.body!.getReader()
// Create a decoder to decode the stream as UTF-8 text
const decoder = new TextDecoder('utf-8')
// Loop until the stream is done
while (true) {
const { value, done } = await reader.read()
if (done) {
break
}
const rawValue = decoder.decode(value)
const lines = rawValue.split('\n')
for (const line of lines) {
if (line.startsWith('data: ')) {
const jsonString = line.slice(6)
if (jsonString == '[DONE]') {
return
}
// Slightly different: wrap the parsed JSON object in an additional object
yield { data: JSON.parse(jsonString) }
}
}
}
} else {
// Raise exception
throw new Error('Response is not an event-stream')
}
}
export function getPlatformInfo(): {
PLATFORM_DELIMITER: string
PLATFORM_META_KEY: string
PLATFORM_CM_KEY: string
IS_WINDOWS: boolean
} {
let PLATFORM_DELIMITER: string
let PLATFORM_META_KEY: string
let PLATFORM_CM_KEY: string
let IS_WINDOWS: boolean
if (process.platform === 'win32') {
PLATFORM_DELIMITER = '\\'
PLATFORM_META_KEY = 'Ctrl+'
PLATFORM_CM_KEY = 'Ctrl'
IS_WINDOWS = true
} else if (process.platform === 'darwin') {
PLATFORM_DELIMITER = '/'
PLATFORM_META_KEY = '⌘'
PLATFORM_CM_KEY = 'Cmd'
IS_WINDOWS = false
} else {
PLATFORM_DELIMITER = '/'
PLATFORM_META_KEY = 'Ctrl+'
PLATFORM_CM_KEY = 'Ctrl'
IS_WINDOWS = false
}
return {
PLATFORM_DELIMITER,
PLATFORM_META_KEY,
PLATFORM_CM_KEY,
IS_WINDOWS,
}
}
export function join(a: string, b: string): string {
if (a[a.length - 1] === connector.PLATFORM_DELIMITER) {
return a + b
}
return a + connector.PLATFORM_DELIMITER + b
}
// make a join method that can handle ./ and ../
export function joinAdvanced(a: string, b: string): string {
if (b.startsWith('./')) {
return joinAdvanced(a, b.slice(2))
}
if (b.startsWith('../')) {
// if a ends with slash
if (a[a.length - 1] === connector.PLATFORM_DELIMITER) {
a = a.slice(0, -1)
}
const aOneHigher = a.slice(
0,
a.lastIndexOf(connector.PLATFORM_DELIMITER)
)
return joinAdvanced(aOneHigher, b.slice(3))
}
return join(a, b)
}
export function removeBeginningAndEndingLineBreaks(str: string): string {
str = str.trimEnd()
while (str[0] === '\n') {
str = str.slice(1)
}
while (str[str.length - 1] === '\n') {
str = str.slice(0, -1)
}
return str
}