Skip to content

Commit c06ffb2

Browse files
committed
Z.ai: fetch live models.list instead of hardcoded model list
- ZAI.models(baseURL:apiKey:): OpenAI-style GET /models on the coding endpoint (verified 200, 10 models) — replaces the 1-token ping probe - Picker shows every plan model; static list is launch-only fallback
1 parent a2a19bc commit c06ffb2

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

‎Sources/App/AppState.swift‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ final class AppState: ObservableObject {
164164
out += g.map { LLMChoice(provider: .gemini, model: $0) }
165165
}
166166
if zaiKeyPresent || !zaiModels.isEmpty {
167-
let z = zaiModels.isEmpty ? ZAINormalizer.models : zaiModels
167+
let z = zaiModels.isEmpty ? ZAINormalizer.fallbackModels : zaiModels
168168
out += z.map { LLMChoice(provider: .zai, model: $0) }
169169
}
170170
return out
@@ -529,32 +529,18 @@ final class AppState: ObservableObject {
529529
if !k.isEmpty { refreshZAIModels() }
530530
}
531531

532-
/// Z.ai exposes no public models.list on the coding endpoint — verify with a
533-
/// 1-token ping and fill the static list on success.
532+
/// Fetch the endpoint's live models.list (OpenAI-style GET /models) and swap
533+
/// it into the picker.
534534
func refreshZAIModels() {
535535
guard let key = Secrets.zaiKey else { zaiStatus = "키 없음"; return }
536536
zaiStatus = "확인 중…"
537537
let base = zaiBaseURL
538-
let model = zaiModel.isEmpty ? "glm-5.3" : zaiModel
539538
Task { [weak self] in
540539
do {
541-
var req = URLRequest(url: URL(string: base + "/chat/completions")!)
542-
req.httpMethod = "POST"
543-
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
544-
req.setValue("Bearer \(key)", forHTTPHeaderField: "Authorization")
545-
req.httpBody = try JSONSerialization.data(withJSONObject: [
546-
"model": model, "messages": [["role": "user", "content": "ping"]],
547-
"max_tokens": 1, "stream": false,
548-
])
549-
let (_, resp) = try await URLSession.shared.data(for: req)
550-
guard let http = resp as? HTTPURLResponse, http.statusCode == 200 else {
551-
let code = (resp as? HTTPURLResponse)?.statusCode ?? -1
552-
throw NSError(domain: "Z.ai", code: code,
553-
userInfo: [NSLocalizedDescriptionKey: "연결 실패 (\(code))"])
554-
}
540+
let ms = try await ZAI.models(baseURL: base, apiKey: key)
555541
guard let self else { return }
556-
self.zaiModels = ZAINormalizer.models
557-
self.zaiStatus = "연결됨 · \(model)"
542+
self.zaiModels = ms
543+
self.zaiStatus = "연결됨 · 모델 \(ms.count)개"
558544
} catch {
559545
self?.zaiStatus = "연결 실패: \(error.localizedDescription)"
560546
}

‎Sources/TTS/TextNormalizer.swift‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,9 @@ struct ZAINormalizer: Normalizing {
285285
let model: String // e.g. "glm-5.3"
286286
let instruction: String
287287

288-
static let models = ["glm-5.3"]
289288
static let defaultBaseURL = "https://api.z.ai/api/coding/paas/v4"
289+
/// Shown before the first successful models.list fetch (e.g. offline at launch).
290+
static let fallbackModels = ["glm-5.3"]
290291

291292
var temperature: Double = 0.2
292293
func normalizeStream(_ text: String) -> AsyncThrowingStream<String, Error> {
@@ -485,6 +486,32 @@ enum Gemini {
485486
}
486487
}
487488

489+
/// Z.ai (OpenAI-compatible) helpers for the UI.
490+
enum ZAI {
491+
/// Models the endpoint exposes (OpenAI-style `GET /models` → `{data:[{id}]}`).
492+
/// `baseURL` is the same API root as generation.
493+
static func models(baseURL: String, apiKey: String) async throws -> [String] {
494+
var base = baseURL.trimmingCharacters(in: .whitespacesAndNewlines)
495+
if base.isEmpty { base = ZAINormalizer.defaultBaseURL }
496+
while base.hasSuffix("/") { base.removeLast() }
497+
guard let url = URL(string: "\(base)/models") else {
498+
throw NSError(domain: "Z.ai", code: -1,
499+
userInfo: [NSLocalizedDescriptionKey: "잘못된 Z.ai 엔드포인트 URL"])
500+
}
501+
var req = URLRequest(url: url)
502+
req.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
503+
let (data, response) = try await URLSession.shared.data(for: req)
504+
guard let http = response as? HTTPURLResponse, http.statusCode == 200 else {
505+
let msg = String(data: data, encoding: .utf8) ?? "Z.ai에 연결할 수 없음"
506+
throw NSError(domain: "Z.ai", code: (response as? HTTPURLResponse)?.statusCode ?? -1,
507+
userInfo: [NSLocalizedDescriptionKey: msg])
508+
}
509+
let obj = try JSONSerialization.jsonObject(with: data) as? [String: Any]
510+
let arr = (obj?["data"] as? [[String: Any]]) ?? []
511+
return arr.compactMap { $0["id"] as? String }.sorted()
512+
}
513+
}
514+
488515
/// Ollama helpers for the UI.
489516
enum Ollama {
490517
static func models(baseURL: URL) async throws -> [String] {

0 commit comments

Comments
 (0)