harden(ai): AIRuntime 去掉冗余 weak self,prepare loading 路径加注释

按 code quality review 反馈(2×P0):
- generate() 的 Task 闭包不再 [weak self];actor 单例 strong capture
  没有循环引用风险,且避免 Swift 5.10+ weak-on-actor 警告
- prepare() 的 case .loading: return 加注释说明这是有意设计,
  调用方需轮询或显示 loading UI(W3 引入 prepare 队列优化)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
link2026
2026-05-25 15:33:51 +08:00
parent 4dcd951821
commit e7cdb45472

View File

@@ -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()