forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentExtensionsSections.swift
More file actions
1300 lines (1173 loc) · 43.2 KB
/
Copy pathAgentExtensionsSections.swift
File metadata and controls
1300 lines (1173 loc) · 43.2 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import AppKit
import OmiTheme
import SwiftUI
import UniformTypeIdentifiers
// MARK: - MCP Servers section
/// User-added MCP servers from ~/.omi/mcp.json, listed on the Apps page next
/// to Imports/Exports.
struct McpServersSection: View {
@ObservedObject var appProvider: AppProvider
var searchText: String = ""
let onAdd: () -> Void
let onSelectLocal: (LocalMcpStore.Entry) -> Void
let onSelectCatalogEntry: (ExtensionCatalog.Entry) -> Void
private var servers: [LocalMcpStore.Entry] {
appProvider.localMcpServers.filter { matchesSearch($0.name, $0.summary, query: searchText) }
}
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.md) {
HStack {
VStack(alignment: .leading, spacing: OmiSpacing.hairline) {
Text("MCP Servers")
.scaledFont(size: OmiType.heading, weight: .semibold)
.foregroundColor(Ink.primary)
Text("Connect remote MCP servers or local commands; their tools become available in chat")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
}
Spacer()
Button(action: onAdd) {
ConnectionModalActionButton(title: "Add Server")
}
.buttonStyle(.plain)
}
if servers.isEmpty {
AgentExtensionEmptyCard(
icon: "server.rack",
text: searchText.isEmpty
? "No servers yet. Add a remote URL, or a local command like npx @playwright/mcp@latest."
: "No installed server matches \(searchText).")
} else {
LazyVGrid(
columns: [GridItem(.adaptive(minimum: 260), spacing: OmiSpacing.md)],
alignment: .leading,
spacing: OmiSpacing.md
) {
ForEach(servers) { server in
let status = appProvider.mcpStatuses[server.name] ?? .checking
AgentExtensionCard(
icon: server.isCommand ? "terminal" : "server.rack",
imageUrl: "",
title: server.name,
subtitle: server.isCommand ? "Local command" : "Remote",
detail: status.detail ?? server.summary,
statusText: status.label,
statusActive: status.isHealthy,
actionTitle: status == .needsAuth ? "Sign In" : "Manage",
actionIsSecondary: status != .needsAuth
) {
onSelectLocal(server)
}
}
}
}
ExtensionMarketplaceSection(
entries: marketplaceEntries,
isLoading: appProvider.isLoadingMcpCatalog,
loadingText: "Loading the MCP registry…",
icon: { $0.subtitle == "Remote" ? "server.rack" : "terminal" },
onSelect: onSelectCatalogEntry
)
}
.task(id: searchText) {
await ExtensionMarketplaceSection.debounce()
guard !Task.isCancelled else { return }
await appProvider.fetchMcpCatalog(search: searchText)
}
.task(id: appProvider.localMcpServers.map(\.name)) {
// The file is hand-editable by design and nothing watches it; each visit
// here stats it once and notifies the runtime when it moved under us.
if LocalMcpStore.checkForExternalChanges() {
await appProvider.fetchUserExtensions()
}
await appProvider.refreshMcpStatuses()
}
}
/// A catalog entry whose local name is already taken is not offered again: installing it would
/// write a suffixed duplicate, which reads as a second copy rather than an upgrade.
private var marketplaceEntries: [ExtensionCatalog.Entry] {
let taken = Set(appProvider.localMcpServers.map(\.name))
return appProvider.mcpCatalog.filter { !taken.contains(LocalSkillsStore.slugify($0.name)) }
}
}
// MARK: - Skills section
/// User-authored skills (SKILL.md in ~/.omi/skills), listed on the Apps page.
struct SkillsSection: View {
@ObservedObject var appProvider: AppProvider
var searchText: String = ""
let onAdd: () -> Void
let onSelect: (LocalSkillsStore.Skill) -> Void
let onSelectCatalogEntry: (ExtensionCatalog.Entry) -> Void
private var skills: [LocalSkillsStore.Skill] {
appProvider.localSkills.filter { matchesSearch($0.name, $0.description, query: searchText) }
}
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.md) {
HStack {
VStack(alignment: .leading, spacing: OmiSpacing.hairline) {
Text("Skills")
.scaledFont(size: OmiType.heading, weight: .semibold)
.foregroundColor(Ink.primary)
Text("Teach the assistant reusable instructions it loads when relevant")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
}
Spacer()
Button(action: onAdd) {
ConnectionModalActionButton(title: "Add Skill")
}
.buttonStyle(.plain)
}
if skills.isEmpty {
AgentExtensionEmptyCard(
icon: "graduationcap",
text: searchText.isEmpty
? "No skills yet. Paste or drop a SKILL.md, or write one from scratch."
: "No installed skill matches \(searchText).")
} else {
LazyVGrid(
columns: [GridItem(.adaptive(minimum: 260), spacing: OmiSpacing.md)],
alignment: .leading,
spacing: OmiSpacing.md
) {
ForEach(skills) { skill in
AgentExtensionCard(
icon: "graduationcap",
imageUrl: "",
title: skill.name,
subtitle: "",
detail: skill.description,
statusText: "Active in chat",
statusActive: true
) {
onSelect(skill)
}
}
}
}
ExtensionMarketplaceSection(
entries: marketplaceEntries,
isLoading: appProvider.isLoadingSkillCatalog,
loadingText: "Loading skills…",
icon: { _ in "graduationcap" },
onSelect: onSelectCatalogEntry
)
}
.task(id: searchText) {
await ExtensionMarketplaceSection.debounce()
guard !Task.isCancelled else { return }
await appProvider.fetchSkillCatalog(search: searchText)
}
}
private var marketplaceEntries: [ExtensionCatalog.Entry] {
let taken = Set(appProvider.localSkills.map(\.slug))
return appProvider.skillCatalog.filter { !taken.contains(LocalSkillsStore.slugify($0.name)) }
}
}
/// The page's one search field covers whichever section is showing, so both sections narrow on
/// the same query rather than leaving it inert over their lists.
private func matchesSearch(_ fields: String..., query: String) -> Bool {
let trimmed = query.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return true }
return fields.contains { $0.localizedCaseInsensitiveContains(trimmed) }
}
// MARK: - Marketplace
/// The "Marketplace" block both extension sections end with: browsable entries from a catalog,
/// in the same card grid as the installed ones above.
///
/// It renders nothing at all when the catalog is empty — an unreachable registry then costs the
/// user a section they were not using rather than an error over the servers they installed.
struct ExtensionMarketplaceSection: View {
let entries: [ExtensionCatalog.Entry]
let isLoading: Bool
let loadingText: String
let icon: (ExtensionCatalog.Entry) -> String
let onSelect: (ExtensionCatalog.Entry) -> Void
/// Typing must not fire one registry request per keystroke; a cancelled task skips the fetch.
static func debounce() async {
try? await Task.sleep(nanoseconds: 350_000_000)
}
var body: some View {
if isLoading && entries.isEmpty {
AgentExtensionEmptyCard(icon: "bag", text: loadingText)
} else if !entries.isEmpty {
VStack(alignment: .leading, spacing: OmiSpacing.md) {
Text("Marketplace")
.scaledFont(size: OmiType.subheading, weight: .semibold)
.foregroundColor(Ink.primary)
LazyVGrid(
columns: [GridItem(.adaptive(minimum: 260), spacing: OmiSpacing.md)],
alignment: .leading,
spacing: OmiSpacing.md
) {
ForEach(entries) { entry in
AgentExtensionCard(
icon: icon(entry),
imageUrl: entry.iconURL,
title: entry.name,
subtitle: entry.subtitle,
detail: entry.detail,
statusText: entry.install.needsInput ? "Needs a key" : entry.publisher,
statusActive: false,
actionTitle: "View",
actionIsSecondary: true
) {
// A card press opens the detail sheet. Installing writes an executable command or a
// credentialed endpoint into ~/.omi, so it happens only from a screen that has shown
// the user exactly what will be written.
onSelect(entry)
}
}
}
}
}
}
}
/// What a marketplace entry is and exactly what installing it writes, so the decision to run a
/// third party's command — or hand it a credential — is made against the facts, not a card.
struct ExtensionDetailSheet: View {
let entry: ExtensionCatalog.Entry
@ObservedObject var appProvider: AppProvider
let onDismiss: () -> Void
@State private var values: [String: String] = [:]
@State private var errorText: String?
@State private var isInstalling = false
private var requiredFields: [String] {
switch entry.install {
case .mcpRemote(_, _, let header): return header.map { [$0] } ?? []
case .mcpStdio(_, _, let env): return env
case .skill: return []
}
}
/// The literal thing that lands in ~/.omi. Shown verbatim: a command a user cannot see is a
/// command they cannot refuse.
private var installSummary: String {
switch entry.install {
case .mcpRemote(let url, let transport, _): return "\(transport.uppercased()) \(url)"
case .mcpStdio(let command, let args, _): return ([command] + args).joined(separator: " ")
case .skill(let source):
let folder = "\(source.repo)/skills/\(source.slug)"
guard source.files.count > 1 else { return "\(folder) · SKILL.md" }
return "\(folder) · SKILL.md and \(source.files.count - 1) bundled files"
}
}
private var installSummaryLabel: String {
if case .skill = entry.install { return "Source" }
if case .mcpStdio = entry.install { return "Runs on your Mac" }
return "Endpoint"
}
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.md) {
HStack(spacing: OmiSpacing.md) {
ExtensionLogo(imageUrl: entry.iconURL, fallbackSymbol: fallbackSymbol, size: 44)
VStack(alignment: .leading, spacing: OmiSpacing.hairline) {
Text(entry.name)
.scaledFont(size: OmiType.subheading, weight: .semibold)
.foregroundColor(Ink.primary)
Text(entry.publisher.isEmpty ? entry.subtitle : "\(entry.subtitle) · \(entry.publisher)")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.lineLimit(1)
}
Spacer()
}
ScrollView {
VStack(alignment: .leading, spacing: OmiSpacing.md) {
if !entry.detail.isEmpty {
Text(entry.detail)
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.fixedSize(horizontal: false, vertical: true)
}
labelled(installSummaryLabel) {
Text(installSummary)
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.primary)
.textSelection(.enabled)
.fixedSize(horizontal: false, vertical: true)
}
ForEach(requiredFields, id: \.self) { field in
labelled(field) {
SecureField("Required", text: binding(for: field))
.textFieldStyle(.roundedBorder)
}
}
if let website = entry.websiteURL, let url = URL(string: website) {
Link("Open publisher page", destination: url)
.scaledFont(size: OmiType.caption)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
if let errorText {
Text(errorText)
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.errorRed)
.fixedSize(horizontal: false, vertical: true)
}
HStack {
Spacer()
Button("Cancel", action: onDismiss)
.buttonStyle(.plain)
.foregroundColor(Ink.secondary)
Button(action: install) {
ConnectionModalActionButton(title: isInstalling ? "Installing…" : "Install")
}
.buttonStyle(.plain)
.disabled(isInstalling)
}
}
.padding(OmiSpacing.lg)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
private var fallbackSymbol: String {
switch entry.install {
case .skill: return "graduationcap"
case .mcpStdio: return "terminal"
case .mcpRemote: return "server.rack"
}
}
@ViewBuilder
private func labelled<Content: View>(_ label: String, @ViewBuilder content: () -> Content)
-> some View
{
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
Text(label)
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(Ink.secondary)
content()
}
}
private func binding(for field: String) -> Binding<String> {
Binding(get: { values[field] ?? "" }, set: { values[field] = $0 })
}
private func install() {
isInstalling = true
errorText = nil
Task {
do {
try await ExtensionCatalogService.install(entry, secrets: values)
await appProvider.fetchUserExtensions()
onDismiss()
} catch {
errorText = error.localizedDescription
isInstalling = false
}
}
}
}
/// A publisher logo that degrades to a symbol.
///
/// `AsyncImage` is not usable here: many publishers serve SVG, which SwiftUI's image decoder
/// rejects while `NSImage` renders it, so half the marketplace showed a fallback symbol beside the
/// half that did not. Remote art is decoration either way — a slow, missing, or undecodable image
/// leaves the symbol rather than a hole where a card's identity should be.
struct ExtensionLogo: View {
let imageUrl: String
let fallbackSymbol: String
var size: CGFloat = 40
@State private var image: NSImage?
var body: some View {
Group {
if let image {
Image(nsImage: image).resizable().aspectRatio(contentMode: .fit).padding(size * 0.12)
} else {
symbol
}
}
.frame(width: size, height: size)
.background(Ink.wash)
.cornerRadius(OmiChrome.smallControlRadius)
// The loader hands back bytes, not an NSImage: decoded images are not Sendable, so
// returning one across the actor boundary is a strict-concurrency error. Decoding here
// also keeps every NSImage on the main actor, which is where it is drawn.
.task(id: imageUrl) {
image = await ExtensionLogoLoader.shared.data(for: imageUrl).flatMap(NSImage.init(data:))
}
}
private var symbol: some View {
Image(systemName: fallbackSymbol)
.scaledFont(size: OmiType.subheading)
.foregroundColor(Ink.primary)
}
}
/// Session cache for catalog logo *bytes*. The same publisher icon appears on a card and again in
/// the detail sheet, and the grid re-renders on every keystroke of the search field.
///
/// Bytes rather than images: `NSImage` is not Sendable, so handing one out of an actor is a
/// strict-concurrency error, and a decoded image is not safe to share across isolation domains
/// anyway. Callers decode on their own actor.
actor ExtensionLogoLoader {
static let shared = ExtensionLogoLoader()
/// Enough for every tile of a catalog page and its detail sheets. A miss costs one request,
/// so the cap trades a rare refetch for a bound that a long browsing session cannot exceed.
private static let capacity = 128
private static let maxBytes = 2 * 1024 * 1024
/// `nil` value means "fetched and unusable" — cached so a broken icon is not retried per render.
private var cache: [String: Data?] = [:]
/// Insertion order, oldest first, for eviction.
private var order: [String] = []
func data(for urlString: String) async -> Data? {
if let cached = cache[urlString] { return cached }
let loaded = await Self.fetch(urlString)
if cache.updateValue(loaded, forKey: urlString) == nil { order.append(urlString) }
while order.count > Self.capacity {
cache.removeValue(forKey: order.removeFirst())
}
return loaded
}
private static func fetch(_ urlString: String) async -> Data? {
guard let url = URL(string: urlString), url.scheme == "https" else { return nil }
var request = URLRequest(url: url, timeoutInterval: 10)
request.setValue("image/*", forHTTPHeaderField: "Accept")
guard let (data, response) = try? await URLSession.shared.data(for: request),
(response as? HTTPURLResponse).map({ (200..<300).contains($0.statusCode) }) == true,
data.count <= maxBytes
else { return nil }
return data
}
}
// MARK: - Shared section pieces
struct AgentExtensionEmptyCard: View {
let icon: String
let text: String
var body: some View {
HStack(spacing: OmiSpacing.md) {
Image(systemName: icon)
.scaledFont(size: OmiType.title)
.foregroundColor(Ink.secondary)
Text(text)
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.multilineTextAlignment(.leading)
Spacer()
}
.padding(OmiSpacing.md)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Ink.rowFill)
.cornerRadius(OmiChrome.smallControlRadius)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius)
.stroke(Ink.separator, lineWidth: 1)
)
}
}
struct AgentExtensionCard: View {
let icon: String
let imageUrl: String
let title: String
let subtitle: String
let detail: String
let statusText: String
let statusActive: Bool
/// Trailing button label. Installed cards manage; marketplace cards install.
var actionTitle: String = "Manage"
var actionIsSecondary: Bool = true
let action: () -> Void
@State private var isHovering = false
var body: some View {
Button(action: action) {
VStack(alignment: .leading, spacing: OmiSpacing.sm) {
HStack(spacing: OmiSpacing.md) {
iconView
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
Text(title)
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundColor(Ink.primary)
.lineLimit(1)
if !subtitle.isEmpty {
Text(subtitle)
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.lineLimit(1)
}
}
Spacer()
}
Text(detail)
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.lineLimit(2)
.multilineTextAlignment(.leading)
.frame(maxWidth: .infinity, minHeight: 28, alignment: .topLeading)
HStack {
Text(statusText)
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(statusActive ? Ink.primary : Ink.secondary)
Spacer()
ImportConnectorActionButton(title: actionTitle, isConnected: actionIsSecondary)
}
}
.padding(OmiSpacing.md)
.background(isHovering ? Ink.rowFillHover : Ink.rowFill)
.cornerRadius(OmiChrome.smallControlRadius)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius)
.stroke(Ink.separator, lineWidth: 1)
)
}
.buttonStyle(.plain)
.onHover { isHovering = $0 }
}
@ViewBuilder private var iconView: some View {
ExtensionLogo(imageUrl: imageUrl, fallbackSymbol: icon)
}
}
// MARK: - Add MCP Server sheet
struct AddMcpServerSheet: View {
@ObservedObject var appProvider: AppProvider
let onDismiss: () -> Void
private enum Phase: Equatable {
case editing
case submitting
case waitingForAuth
case savedLocal
}
private enum Mode: String, CaseIterable, Identifiable {
case remote = "Remote URL"
case local = "Local Command"
var id: String { rawValue }
var icon: String { self == .remote ? "server.rack" : "terminal" }
var automationID: String { self == .remote ? "remote" : "local" }
}
private enum Field: Hashable {
case name, url, key, command
}
@State private var name = ""
@State private var serverUrl = ""
@State private var apiKey = ""
@State private var commandLine = ""
@State private var mode: Mode = .remote
@State private var phase: Phase = .editing
@State private var errorMessage: String?
@FocusState private var focusedField: Field?
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.xl) {
header
switch phase {
case .savedLocal:
savedLocalContent
case .waitingForAuth:
waitingContent
default:
formContent
}
// The overlay sizes itself to the tallest lane; without this the shorter ones float
// vertically centred in a box that is not theirs.
Spacer(minLength: 0)
}
.padding(OmiSpacing.xxl)
// The title needs more room above it than beside it, or it reads as pinned to the card's edge.
.padding(.top, OmiSpacing.md)
.frame(maxWidth: .infinity, alignment: .topLeading)
.background(Ink.surface)
}
private var header: some View {
HStack(alignment: .top, spacing: OmiSpacing.md) {
Image(systemName: mode.icon)
.scaledFont(size: OmiType.subheading, weight: .medium)
.foregroundColor(Ink.primary)
.frame(width: 40, height: 40)
.background(
RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius, style: .continuous)
.fill(Ink.rowFill)
)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius, style: .continuous)
.strokeBorder(Ink.separator, lineWidth: 1)
)
.omiAnimation(.easeOut(duration: 0.15), value: mode)
VStack(alignment: .leading, spacing: OmiSpacing.hairline) {
Text("Add MCP Server")
.scaledFont(size: OmiType.heading, weight: .semibold)
.foregroundColor(Ink.primary)
Text("Its tools become available to the assistant in chat")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.fixedSize(horizontal: false, vertical: true)
}
Spacer(minLength: OmiSpacing.md)
DismissButton(action: onDismiss)
}
}
private var formContent: some View {
VStack(alignment: .leading, spacing: OmiSpacing.lg) {
modeChips
labeledField("Name", field: .name) {
TextField(mode == .remote ? "e.g. Linear" : "e.g. Playwright", text: $name)
}
.accessibilityIdentifier("add-mcp-name")
if mode == .remote {
labeledField("Server URL", field: .url) {
TextField("https://mcp.example.com/mcp", text: $serverUrl)
}
.accessibilityIdentifier("add-mcp-url")
labeledField("API key", field: .key, hint: "Leave empty for public or OAuth servers") {
SecureField("", text: $apiKey)
}
.accessibilityIdentifier("add-mcp-api-key")
} else {
labeledField(
"Command", field: .command,
hint: "Runs on your Mac and is saved to ~/.omi/mcp.json."
) {
TextField("npx @playwright/mcp@latest", text: $commandLine)
}
.accessibilityIdentifier("add-mcp-command")
}
errorRow
footer
}
}
private var modeChips: some View {
HStack(spacing: OmiSpacing.xs) {
ForEach(Mode.allCases) { candidate in
Button {
errorMessage = nil
OmiMotion.withGated(.easeOut(duration: 0.15)) { mode = candidate }
} label: {
HStack(spacing: OmiSpacing.xs) {
Image(systemName: candidate.icon)
.scaledFont(size: OmiType.caption)
Text(candidate.rawValue)
.scaledFont(size: OmiType.caption, weight: mode == candidate ? .semibold : .regular)
}
.foregroundStyle(GlassShell.controlLabel(isProminent: mode == candidate))
.padding(.horizontal, OmiSpacing.lg)
.padding(.vertical, OmiSpacing.sm)
.glassChip(isActive: mode == candidate)
}
.buttonStyle(.plain)
.accessibilityIdentifier("add-mcp-mode-\(candidate.automationID)")
.accessibilityAddTraits(mode == candidate ? .isSelected : [])
}
}
}
private var footer: some View {
HStack(spacing: OmiSpacing.lg) {
Spacer()
Button("Cancel", action: onDismiss)
.buttonStyle(.plain)
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(Ink.secondary)
Button {
Task { await submit() }
} label: {
ConnectionModalActionButton(title: phase == .submitting ? "Connecting…" : "Connect")
// The primitive has no disabled state of its own, and a live-looking button that does
// nothing is worse than a dim one.
.opacity(canSubmit && phase != .submitting ? 1 : 0.45)
}
.buttonStyle(.plain)
.keyboardShortcut(.defaultAction)
.disabled(!canSubmit || phase == .submitting)
.accessibilityIdentifier("add-mcp-submit")
}
}
@ViewBuilder
private var errorRow: some View {
if let errorMessage {
HStack(alignment: .top, spacing: OmiSpacing.xs) {
Image(systemName: "exclamationmark.triangle.fill")
.scaledFont(size: OmiType.caption)
Text(errorMessage)
.scaledFont(size: OmiType.caption)
.fixedSize(horizontal: false, vertical: true)
}
.foregroundColor(Ink.errorRed)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(OmiSpacing.md)
.background(
RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius, style: .continuous)
.fill(Ink.errorRed.opacity(0.10))
)
}
}
private var waitingContent: some View {
statusCard(
icon: { ProgressView().scaleEffect(0.6).frame(width: 20, height: 20) },
title: "Finish authorizing in your browser…",
detail: "This sheet updates automatically once the server is connected."
)
}
private var canSubmit: Bool {
guard !name.trimmingCharacters(in: .whitespaces).isEmpty else { return false }
switch mode {
case .remote:
return URL(string: serverUrl.trimmingCharacters(in: .whitespaces))?.host != nil
case .local:
return !commandLine.trimmingCharacters(in: .whitespaces).isEmpty
}
}
private var savedLocalContent: some View {
VStack(alignment: .leading, spacing: OmiSpacing.md) {
statusCard(
icon: {
Image(systemName: "checkmark.circle.fill")
.scaledFont(size: OmiType.subheading)
.foregroundColor(Ink.primary)
.frame(width: 20, height: 20)
},
title: "Server saved to ~/.omi/mcp.json",
detail: "Its tools reach chat automatically — right away, or with your next message if a reply is in flight."
)
HStack {
Spacer()
Button(action: onDismiss) {
ConnectionModalActionButton(title: "Done")
}
.buttonStyle(.plain)
}
}
}
/// The one card shape both terminal phases use, so "waiting" and "saved" differ by their words
/// rather than by their layout.
private func statusCard<Icon: View>(
@ViewBuilder icon: () -> Icon,
title: String,
detail: String
) -> some View {
HStack(alignment: .top, spacing: OmiSpacing.md) {
icon()
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
Text(title)
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundColor(Ink.primary)
.fixedSize(horizontal: false, vertical: true)
Text(detail)
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.fixedSize(horizontal: false, vertical: true)
}
Spacer(minLength: 0)
}
.padding(OmiSpacing.lg)
.frame(maxWidth: .infinity, alignment: .leading)
.background(
RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius, style: .continuous)
.fill(Ink.rowFill)
)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius, style: .continuous)
.strokeBorder(Ink.separator, lineWidth: 1)
)
}
private func labeledField<Content: View>(
_ label: String,
field: Field,
hint: String? = nil,
@ViewBuilder content: () -> Content
) -> some View {
let shape = RoundedRectangle(cornerRadius: PageGlass.fieldRadius, style: .continuous)
return VStack(alignment: .leading, spacing: OmiSpacing.xs) {
Text(label)
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(Ink.secondary)
content()
.textFieldStyle(.plain)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.primary)
.focused($focusedField, equals: field)
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.md)
.frame(maxWidth: .infinity, alignment: .leading)
.background(shape.fill(Ink.rowFill))
.overlay(
shape.strokeBorder(
focusedField == field ? Ink.accent : Ink.separator, lineWidth: 1)
)
.omiAnimation(.easeOut(duration: 0.12), value: focusedField == field)
if let hint {
Text(hint)
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.fixedSize(horizontal: false, vertical: true)
}
}
}
private func submit() async {
// Return-to-submit reaches here from any field, including a half-filled one.
guard canSubmit, phase != .submitting else { return }
focusedField = nil
errorMessage = nil
do {
if mode == .local {
try LocalMcpStore.addCommandServer(
name: name.trimmingCharacters(in: .whitespaces),
commandLine: commandLine.trimmingCharacters(in: .whitespaces))
} else {
let trimmedKey = apiKey.trimmingCharacters(in: .whitespaces)
// No key means the server may hand us to the browser for OAuth.
phase = trimmedKey.isEmpty ? .waitingForAuth : .submitting
_ = try await LocalMcpStore.addRemoteServer(
name: name.trimmingCharacters(in: .whitespaces),
url: serverUrl.trimmingCharacters(in: .whitespaces),
apiKey: trimmedKey.isEmpty ? nil : trimmedKey)
}
await appProvider.fetchUserExtensions()
phase = .savedLocal
} catch {
phase = .editing
errorMessage = error.localizedDescription
}
}
}
// MARK: - Skill editor sheet
/// Create or edit a skill: title plus SKILL.md content, with paste, file pick,
/// and drag-drop input. Saves straight to ~/.omi/skills/<slug>/SKILL.md.
struct SkillEditorSheet: View {
@ObservedObject var appProvider: AppProvider
/// nil creates a new skill; non-nil edits an existing one.
let editingSkill: LocalSkillsStore.Skill?
let onDismiss: () -> Void
@State private var title = ""
@State private var markdown = ""
@State private var isWorking = false
@State private var errorMessage: String?
@State private var isDropTargeted = false
@State private var confirmingDelete = false
private static let maxSkillBytes = 128 * 1024
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.lg) {
HStack(alignment: .top) {
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
Text(editingSkill == nil ? "Add Skill" : "Edit Skill")
.scaledFont(size: OmiType.title, weight: .semibold)
.foregroundColor(Ink.primary)
Text("The assistant reads the name and description, and loads the full instructions when relevant")
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
.fixedSize(horizontal: false, vertical: true)
}
Spacer()
DismissButton(action: onDismiss)
}
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
Text("Name")
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(Ink.secondary)
TextField("e.g. Weekly Report Format", text: $title)
.textFieldStyle(.plain)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.primary)
.padding(OmiSpacing.sm)
.background(Ink.rowFill)
.cornerRadius(OmiChrome.smallControlRadius)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius)
.stroke(Ink.separator, lineWidth: 1)
)
}
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
HStack {
Text("Instructions (Markdown)")
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(Ink.secondary)
Spacer()
Button {
pickSkillFile()
} label: {
HStack(spacing: OmiSpacing.xxs) {
Image(systemName: "square.and.arrow.down")
Text("Import .md file")
}
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(Ink.secondary)
}
.buttonStyle(.plain)
}
// `Ink.nsPrimary`, not the editor's chat-composer default of white: this well sits on a
// light surface, where white text is invisible.
OmiTextEditor(
text: $markdown,
textColor: Ink.nsPrimary,
focusOnAppear: false,
onFileDrop: { url in
Task { @MainActor in importSkillFile(url) }
},
onFileDragTargeted: { isDropTargeted = $0 }
)
.frame(maxHeight: .infinity)
.padding(OmiSpacing.sm)
.background(isDropTargeted ? Ink.rowFillHover : Ink.rowFill)
.cornerRadius(OmiChrome.smallControlRadius)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.smallControlRadius)
.stroke(isDropTargeted ? Ink.accent.opacity(0.6) : Ink.separator, lineWidth: isDropTargeted ? 1.5 : 1)
)
.onDrop(of: [UTType.fileURL], isTargeted: $isDropTargeted, perform: handleDrop)
.overlay(alignment: .center) {