@@ -28,6 +28,11 @@ import type {
2828 AssistantModelDownloadState ,
2929 AssistantModelUploadRequest ,
3030} from '../../shared/types' ;
31+ import {
32+ isResumeNotSatisfiable ,
33+ resolveCorruptedArtifactCleanupPaths ,
34+ resolveResumePreparation ,
35+ } from './modelDownloadRecovery' ;
3136
3237interface DownloadSnapshot {
3338 state : AssistantModelDownloadState ;
@@ -479,34 +484,34 @@ export class ModelInstallService {
479484 unlinkSync ( finalPath ) ;
480485 }
481486
482- let existingPartBytes = existsSync ( partPath ) ? statSync ( partPath ) . size : 0 ;
483- if ( existingPartBytes >= artifact . expectedSizeBytes ) {
487+ const existingPartBytes = existsSync ( partPath ) ? statSync ( partPath ) . size : 0 ;
488+ const resumePreparation = resolveResumePreparation ( existingPartBytes , artifact . expectedSizeBytes ) ;
489+ if ( resumePreparation . resetPart ) {
484490 this . safeDeleteFile ( partPath ) ;
485- existingPartBytes = 0 ;
486491 }
487492
488493 const headers : Record < string , string > = { } ;
489- if ( existingPartBytes > 0 ) {
490- headers . Range = `bytes= ${ existingPartBytes } -` ;
494+ if ( resumePreparation . rangeHeader ) {
495+ headers . Range = resumePreparation . rangeHeader ;
491496 }
492497 const response = await fetch ( artifact . url , {
493498 method : 'GET' ,
494499 ...( Object . keys ( headers ) . length > 0 ? { headers } : { } ) ,
495500 signal,
496501 } ) ;
497- if ( response . status === 416 ) {
502+ if ( isResumeNotSatisfiable ( response . status ) ) {
498503 this . safeDeleteFile ( partPath ) ;
499504 throw new Error ( `Failed to resume download for ${ artifact . fileName } : HTTP 416` ) ;
500505 }
501506 if ( ! response . ok && response . status !== 206 ) {
502507 throw new Error ( `Failed to download ${ artifact . fileName } : HTTP ${ response . status } ` ) ;
503508 }
504509
505- const appendMode = response . status === 206 && existingPartBytes > 0 ;
510+ const appendMode = response . status === 206 && resumePreparation . nextPartBytes > 0 ;
506511 const fileStream = createWriteStream ( partPath , { flags : appendMode ? 'a' : 'w' } ) ;
507512
508513 if ( this . activeDownloadTask && appendMode ) {
509- this . activeDownloadTask . downloadedBytes += existingPartBytes ;
514+ this . activeDownloadTask . downloadedBytes += resumePreparation . nextPartBytes ;
510515 this . updateDownloadProgress ( { currentFile : artifact . fileName } ) ;
511516 }
512517
@@ -556,8 +561,9 @@ export class ModelInstallService {
556561
557562 private cleanupCorruptedArtifact ( artifact : DownloadArtifact ) : void {
558563 const partPath = this . getPartPath ( artifact . fileName ) ;
559- this . safeDeleteFile ( partPath ) ;
560- this . safeDeleteFile ( artifact . destinationPath ) ;
564+ for ( const cleanupPath of resolveCorruptedArtifactCleanupPaths ( partPath , artifact . destinationPath ) ) {
565+ this . safeDeleteFile ( cleanupPath ) ;
566+ }
561567 }
562568
563569 private safeDeleteFile ( filePath : string ) : void {
0 commit comments