feat: add model load wait to ensure model is ready before use

This commit is contained in:
Akarshan 2025-06-11 12:31:25 +05:30 committed by Louis
parent 9d4e7cb2b8
commit f463008362
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2

View File

@ -369,6 +369,24 @@ export default class llamacpp_extension extends AIEngine {
return port return port
} }
private async sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
private async waitForModelLoad(port: number, timeoutMs = 30_000): Promise<void> {
const start = Date.now()
while (Date.now() - start < timeoutMs) {
try {
const res = await fetch(`http://localhost:${port}/health`)
if(res.ok) {
return
}
} catch (e) {}
await this.sleep(500) // 500 sec interval during rechecks
}
throw new Error(`Timed out loading model after ${timeoutMs}`)
}
override async load(modelId: string): Promise<SessionInfo> { override async load(modelId: string): Promise<SessionInfo> {
const sInfo = this.findSessionByModel(modelId) const sInfo = this.findSessionByModel(modelId)
if (sInfo) { if (sInfo) {
@ -464,6 +482,8 @@ export default class llamacpp_extension extends AIEngine {
args args
}) })
await this.waitForModelLoad(sInfo.port)
// Store the session info for later use // Store the session info for later use
this.activeSessions.set(sInfo.pid, sInfo) this.activeSessions.set(sInfo.pid, sInfo)
@ -586,7 +606,7 @@ export default class llamacpp_extension extends AIEngine {
const url = `${baseUrl}/chat/completions` const url = `${baseUrl}/chat/completions`
const headers = { const headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Bearer ${sessionInfo.apiKey}`, 'Authorization': `Bearer ${sessionInfo.api_key}`,
} }
const body = JSON.stringify(opts) const body = JSON.stringify(opts)