diff --git a/体己/AI/AIRuntime.swift b/体己/AI/AIRuntime.swift index 4efbcee..cdc518a 100644 --- a/体己/AI/AIRuntime.swift +++ b/体己/AI/AIRuntime.swift @@ -34,9 +34,15 @@ actor AIRuntime { /// 加载模型。首次调用会真正加载,后续幂等。 func prepare() async throws { switch status { - case .ready: return - case .loading: return // 已经在加载 - case .error, .notReady: break + case .ready: + return + case .loading: + // 已有其他调用方在加载;本次 prepare 直接返回, + // 调用方需稍后 await prepare() 再判 status,或自行轮询 / 显示加载 UI。 + // W3 引入 prepare 队列时优化。 + return + case .error, .notReady: + break } guard ModelStore.shared.isReady(.llm) else { @@ -65,7 +71,7 @@ actor AIRuntime { let snapshotSession = llmSession return AsyncThrowingStream { continuation in - Task { [weak self] in + Task { guard snapshotStatus == .ready, let session = snapshotSession else { continuation.finish(throwing: AIRuntimeError.notReady) return @@ -74,7 +80,7 @@ actor AIRuntime { // session.generate 跨 actor 边界,需要 await let stream = await session.generate(prompt: prompt, maxTokens: maxTokens) for try await chunk in stream { - await self?.recordRate(chunk.decodeRate) + await self.recordRate(chunk.decodeRate) continuation.yield(chunk) } continuation.finish()