Repository navigation
Expand file tree
/
Copy pathLLMModelPicker.swift
More file actions
74 lines (69 loc) · 3.14 KB
/
Copy pathLLMModelPicker.swift
File metadata and controls
74 lines (69 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import SwiftUI
/// In-panel model selector for one LLM role. Lists models from ALL enabled
/// endpoints, grouped per endpoint — picking one sets both the role's endpoint
/// reference and that endpoint's remembered model for the role. Any number of
/// endpoints can be configured at once (settings → 엔드포인트).
struct LLMModelPicker: View {
@EnvironmentObject var app: AppState
let label: String
@Binding var endpointID: String // role → endpoint reference
@Binding var roleModels: [String: String] // endpoint id → the role's model there
var refreshOnAppear = true
var onChange: () -> Void = {}
private var current: AppState.LLMChoice? {
guard let e = app.activeEndpoint(byID: endpointID) else { return nil }
return AppState.LLMChoice(endpointID: e.id.uuidString, endpointName: e.name,
model: app.roleModel(roleModels, e))
}
/// Connected models, with the current selection folded in so a saved model
/// from an offline/unfetched endpoint still shows.
private var options: [AppState.LLMChoice] {
var m = app.connectedModels
if let c = current, !c.model.isEmpty, !m.contains(where: { $0.id == c.id }) { m.append(c) }
return m
}
private struct Group: Identifiable {
let endpointID: String
let name: String
let models: [AppState.LLMChoice]
var id: String { endpointID }
}
/// Models grouped per endpoint (in registration order), sorted by name within each.
private var grouped: [Group] {
let started = ProcessInfo.processInfo.systemUptime
defer { InputDiagnostics.shared.recordWork("modelPicker.grouped", since: started) }
return app.endpoints.compactMap { e -> Group? in
guard e.isEnabled else { return nil }
let id = e.id.uuidString
let ms = options.filter { $0.endpointID == id }
.sorted { $0.model.lowercased() < $1.model.lowercased() }
return ms.isEmpty ? nil : Group(endpointID: id, name: e.name, models: ms)
}
}
var body: some View {
HStack(spacing: 6) {
Text(label).font(.caption).foregroundStyle(.secondary)
if options.isEmpty {
Text("연결된 모델 없음").font(.caption).foregroundStyle(.tertiary)
} else {
Picker("", selection: Binding(
get: { current?.id ?? "" },
set: { id in
guard let c = options.first(where: { $0.id == id }) else { return }
endpointID = c.endpointID
roleModels[c.endpointID] = c.model
onChange()
}
)) {
ForEach(grouped) { group in
Section(group.name) {
ForEach(group.models) { Text($0.model).tag($0.id) }
}
}
}
.labelsHidden().frame(maxWidth: 260)
}
}
.onAppear { if refreshOnAppear { app.refreshAllModelsIfNeeded() } }
}
}