forked from getcursor/cursor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
195 lines (179 loc) · 5.71 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
export const API_ROOT = 'https://aicursor.com'
export class NoAuthRateLimitError extends Error {
constructor(
message = 'You have reached the rate limit for unauthenticated requests. Please authenticate to continue.'
) {
super(message)
this.name = 'NoAuthRateLimitError'
}
}
export class AuthRateLimitError extends Error {
constructor(
message = 'You have reached the rate limit for authenticated requests. Please wait before making more requests.'
) {
super(message)
this.name = 'AuthRateLimitError'
}
}
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
}