Skip to content

Commit 7e46295

Browse files
authored
chore: cortex.cpp gpu activation could cause a race condition (#4825)
1 parent 2271c8d commit 7e46295

File tree

6 files changed

+373
-251
lines changed

6 files changed

+373
-251
lines changed

extensions/conversational-extension/src/index.ts

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,26 @@ type MessageList = {
2222
export default class CortexConversationalExtension extends ConversationalExtension {
2323
queue = new PQueue({ concurrency: 1 })
2424

25+
api?: KyInstance
2526
/**
26-
* Extended API instance for making requests to the Cortex API.
27+
* Get the API instance
2728
* @returns
2829
*/
29-
api: KyInstance
30+
async apiInstance(): Promise<KyInstance> {
31+
if(this.api) return this.api
32+
const apiKey = (await window.core?.api.appToken()) ?? 'cortex.cpp'
33+
this.api = ky.extend({
34+
prefixUrl: API_URL,
35+
headers: {
36+
Authorization: `Bearer ${apiKey}`,
37+
},
38+
})
39+
return this.api
40+
}
3041
/**
3142
* Called when the extension is loaded.
3243
*/
3344
async onLoad() {
34-
const apiKey = await window.core?.api.appToken() ?? 'cortex.cpp'
35-
this.api = ky.extend({
36-
prefixUrl: API_URL,
37-
headers: {
38-
Authorization: `Bearer ${apiKey}`,
39-
},
40-
})
4145
this.queue.add(() => this.healthz())
4246
}
4347

@@ -51,10 +55,12 @@ export default class CortexConversationalExtension extends ConversationalExtensi
5155
*/
5256
async listThreads(): Promise<Thread[]> {
5357
return this.queue.add(() =>
54-
this.api
55-
.get('v1/threads?limit=-1')
56-
.json<ThreadList>()
57-
.then((e) => e.data)
58+
this.apiInstance().then((api) =>
59+
api
60+
.get('v1/threads?limit=-1')
61+
.json<ThreadList>()
62+
.then((e) => e.data)
63+
)
5864
) as Promise<Thread[]>
5965
}
6066

@@ -64,7 +70,9 @@ export default class CortexConversationalExtension extends ConversationalExtensi
6470
*/
6571
async createThread(thread: Thread): Promise<Thread> {
6672
return this.queue.add(() =>
67-
this.api.post('v1/threads', { json: thread }).json<Thread>()
73+
this.apiInstance().then((api) =>
74+
api.post('v1/threads', { json: thread }).json<Thread>()
75+
)
6876
) as Promise<Thread>
6977
}
7078

@@ -75,7 +83,9 @@ export default class CortexConversationalExtension extends ConversationalExtensi
7583
async modifyThread(thread: Thread): Promise<void> {
7684
return this.queue
7785
.add(() =>
78-
this.api.patch(`v1/threads/${thread.id}`, { json: thread })
86+
this.apiInstance().then((api) =>
87+
api.patch(`v1/threads/${thread.id}`, { json: thread })
88+
)
7989
)
8090
.then()
8191
}
@@ -86,7 +96,9 @@ export default class CortexConversationalExtension extends ConversationalExtensi
8696
*/
8797
async deleteThread(threadId: string): Promise<void> {
8898
return this.queue
89-
.add(() => this.api.delete(`v1/threads/${threadId}`))
99+
.add(() =>
100+
this.apiInstance().then((api) => api.delete(`v1/threads/${threadId}`))
101+
)
90102
.then()
91103
}
92104

@@ -97,11 +109,13 @@ export default class CortexConversationalExtension extends ConversationalExtensi
97109
*/
98110
async createMessage(message: ThreadMessage): Promise<ThreadMessage> {
99111
return this.queue.add(() =>
100-
this.api
101-
.post(`v1/threads/${message.thread_id}/messages`, {
102-
json: message,
103-
})
104-
.json<ThreadMessage>()
112+
this.apiInstance().then((api) =>
113+
api
114+
.post(`v1/threads/${message.thread_id}/messages`, {
115+
json: message,
116+
})
117+
.json<ThreadMessage>()
118+
)
105119
) as Promise<ThreadMessage>
106120
}
107121

@@ -112,14 +126,13 @@ export default class CortexConversationalExtension extends ConversationalExtensi
112126
*/
113127
async modifyMessage(message: ThreadMessage): Promise<ThreadMessage> {
114128
return this.queue.add(() =>
115-
this.api
116-
.patch(
117-
`v1/threads/${message.thread_id}/messages/${message.id}`,
118-
{
129+
this.apiInstance().then((api) =>
130+
api
131+
.patch(`v1/threads/${message.thread_id}/messages/${message.id}`, {
119132
json: message,
120-
}
121-
)
122-
.json<ThreadMessage>()
133+
})
134+
.json<ThreadMessage>()
135+
)
123136
) as Promise<ThreadMessage>
124137
}
125138

@@ -132,7 +145,9 @@ export default class CortexConversationalExtension extends ConversationalExtensi
132145
async deleteMessage(threadId: string, messageId: string): Promise<void> {
133146
return this.queue
134147
.add(() =>
135-
this.api.delete(`v1/threads/${threadId}/messages/${messageId}`)
148+
this.apiInstance().then((api) =>
149+
api.delete(`v1/threads/${threadId}/messages/${messageId}`)
150+
)
136151
)
137152
.then()
138153
}
@@ -144,10 +159,12 @@ export default class CortexConversationalExtension extends ConversationalExtensi
144159
*/
145160
async listMessages(threadId: string): Promise<ThreadMessage[]> {
146161
return this.queue.add(() =>
147-
this.api
148-
.get(`v1/threads/${threadId}/messages?order=asc&limit=-1`)
149-
.json<MessageList>()
150-
.then((e) => e.data)
162+
this.apiInstance().then((api) =>
163+
api
164+
.get(`v1/threads/${threadId}/messages?order=asc&limit=-1`)
165+
.json<MessageList>()
166+
.then((e) => e.data)
167+
)
151168
) as Promise<ThreadMessage[]>
152169
}
153170

@@ -159,9 +176,11 @@ export default class CortexConversationalExtension extends ConversationalExtensi
159176
*/
160177
async getThreadAssistant(threadId: string): Promise<ThreadAssistantInfo> {
161178
return this.queue.add(() =>
162-
this.api
163-
.get(`v1/assistants/${threadId}?limit=-1`)
164-
.json<ThreadAssistantInfo>()
179+
this.apiInstance().then((api) =>
180+
api
181+
.get(`v1/assistants/${threadId}?limit=-1`)
182+
.json<ThreadAssistantInfo>()
183+
)
165184
) as Promise<ThreadAssistantInfo>
166185
}
167186
/**
@@ -175,9 +194,11 @@ export default class CortexConversationalExtension extends ConversationalExtensi
175194
assistant: ThreadAssistantInfo
176195
): Promise<ThreadAssistantInfo> {
177196
return this.queue.add(() =>
178-
this.api
179-
.post(`v1/assistants/${threadId}`, { json: assistant })
180-
.json<ThreadAssistantInfo>()
197+
this.apiInstance().then((api) =>
198+
api
199+
.post(`v1/assistants/${threadId}`, { json: assistant })
200+
.json<ThreadAssistantInfo>()
201+
)
181202
) as Promise<ThreadAssistantInfo>
182203
}
183204

@@ -192,9 +213,11 @@ export default class CortexConversationalExtension extends ConversationalExtensi
192213
assistant: ThreadAssistantInfo
193214
): Promise<ThreadAssistantInfo> {
194215
return this.queue.add(() =>
195-
this.api
196-
.patch(`v1/assistants/${threadId}`, { json: assistant })
197-
.json<ThreadAssistantInfo>()
216+
this.apiInstance().then((api) =>
217+
api
218+
.patch(`v1/assistants/${threadId}`, { json: assistant })
219+
.json<ThreadAssistantInfo>()
220+
)
198221
) as Promise<ThreadAssistantInfo>
199222
}
200223

@@ -203,10 +226,12 @@ export default class CortexConversationalExtension extends ConversationalExtensi
203226
* @returns
204227
*/
205228
async healthz(): Promise<void> {
206-
return this.api
207-
.get('healthz', {
208-
retry: { limit: 20, delay: () => 500, methods: ['get'] },
209-
})
229+
return this.apiInstance()
230+
.then((api) =>
231+
api.get('healthz', {
232+
retry: { limit: 20, delay: () => 500, methods: ['get'] },
233+
})
234+
)
210235
.then(() => {})
211236
}
212237
}

0 commit comments

Comments
 (0)