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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
import SwiftUI
import UIKit

struct AssistantAppView: View {
  @Bindable var session: AssistantSession
  @State private var selection: AssistantTab = .chat

  var body: some View {
    TabView(selection: $selection) {
      ChatView(session: session)
        .tabItem {
          Label("Chat", systemImage: "bubble.left.and.bubble.right")
        }
        .tag(AssistantTab.chat)

      ActivityView(session: session)
        .tabItem {
          Label("Activity", systemImage: "list.bullet.rectangle.portrait")
        }
        .badge(activeActivityCount)
        .tag(AssistantTab.activity)

      KnowledgeView(session: session)
        .tabItem {
          Label("Knowledge", systemImage: "books.vertical")
        }
        .tag(AssistantTab.knowledge)

      SettingsView(session: session)
        .tabItem {
          Label("Settings", systemImage: "gearshape")
        }
        .tag(AssistantTab.settings)
    }
    .sheet(item: pendingApprovalBinding) { request in
      ToolApprovalSheet(
        request: request,
        approve: session.approvePending,
        deny: session.denyPending
      )
    }
    .onReceive(
      NotificationCenter.default.publisher(
        for: .dolphinIntentHandoffConsumed
      )
    ) { _ in
      selection = .chat
    }
  }

  private var activeActivityCount: Int {
    session.pendingApproval != nil ? 1 : (session.isRunning ? 1 : 0)
  }

  private var pendingApprovalBinding: Binding<ToolApprovalRequest?> {
    Binding(
      get: { session.pendingApproval },
      set: { newValue in
        if newValue == nil, session.pendingApproval != nil {
          session.denyPending()
        }
      }
    )
  }
}

private enum AssistantTab: Hashable {
  case chat
  case activity
  case knowledge
  case settings
}

struct ChatView: View {
  @Bindable var session: AssistantSession
  @State private var showingClearConfirmation = false

  var body: some View {
    NavigationStack {
      ScrollViewReader { proxy in
        ScrollView {
          LazyVStack(spacing: 14) {
            ModelStatusBanner(session: session)

            if session.workspace.messages.isEmpty {
              EmptyChatView(isModelLoaded: session.isModelLoaded)
                .padding(.top, 36)
            } else {
              ForEach(session.workspace.messages) { message in
                MessageBubble(message: message)
                  .id(message.id)
              }
            }

            if session.isRunning {
              HStack(spacing: 10) {
                ProgressView()
                  .controlSize(.small)
                Text("Dolphin is working…")
                  .font(.footnote)
                  .foregroundStyle(.secondary)
                Spacer()
              }
              .padding(.horizontal, 4)
              .id(Self.runningAnchor)
            }
          }
          .padding(.horizontal)
          .padding(.vertical, 12)
          .frame(maxWidth: 760)
          .frame(maxWidth: .infinity)
        }
        .defaultScrollAnchor(.bottom)
        .onChange(of: session.workspace.messages.count) { _, _ in
          scrollToLatest(using: proxy)
        }
        .onChange(of: session.isRunning) { _, _ in
          scrollToLatest(using: proxy)
        }
        .scrollDismissesKeyboard(.interactively)
      }
      .safeAreaInset(edge: .bottom, spacing: 0) {
        ChatComposer(session: session)
      }
      .navigationTitle("Dolphin")
      .navigationBarTitleDisplayMode(.inline)
      .toolbar {
        ToolbarItem(placement: .topBarTrailing) {
          Menu {
            Button("Clear Chat & Run Activity", systemImage: "trash", role: .destructive) {
              showingClearConfirmation = true
            }
            .disabled(session.workspace.messages.isEmpty || session.isRunning)

            if session.isModelLoaded {
              Button("Release Model", systemImage: "eject") {
                session.releaseModel()
              }
              .disabled(session.isRunning)
            }
          } label: {
            Image(systemName: "ellipsis.circle")
          }
          .accessibilityLabel("Chat actions")
        }
      }
      .confirmationDialog(
        "Clear chat and run activity?",
        isPresented: $showingClearConfirmation,
        titleVisibility: .visible
      ) {
        Button("Clear Chat & Run Activity", role: .destructive) {
          session.clearConversation()
        }
        Button("Cancel", role: .cancel) {}
      } message: {
        Text("Messages, run records, run-scoped Activity receipts, and copied recent-chat excerpts in handoffs will be removed. Saved knowledge, memories, handoffs, tasks, and standalone Activity entries remain.")
      }
    }
  }

  private func scrollToLatest(using proxy: ScrollViewProxy) {
    withAnimation(.easeOut(duration: 0.2)) {
      if session.isRunning {
        proxy.scrollTo(Self.runningAnchor, anchor: .bottom)
      } else if let lastID = session.workspace.messages.last?.id {
        proxy.scrollTo(lastID, anchor: .bottom)
      }
    }
  }

  private static let runningAnchor = "assistant-running"
}

private struct ModelStatusBanner: View {
  @Bindable var session: AssistantSession

  var body: some View {
    VStack(alignment: .leading, spacing: 10) {
      HStack(spacing: 10) {
        Image(systemName: statusSymbol)
          .font(.title3)
          .foregroundStyle(statusColor)
          .symbolEffect(
            .pulse,
            isActive: session.isRunning || session.isModelLoading
          )
          .accessibilityHidden(true)

        VStack(alignment: .leading, spacing: 2) {
          Text(session.modelStatus)
            .font(.subheadline.weight(.semibold))
          Text("Private, on-device tools and Core ML assistant")
            .font(.caption)
            .foregroundStyle(.secondary)
        }

        Spacer(minLength: 8)

        if session.isModelLoading {
          ProgressView()
            .controlSize(.small)
            .accessibilityLabel("Loading model")

          Button("Cancel") {
            session.cancelModelLoad()
          }
          .buttonStyle(.bordered)
          .controlSize(.small)
          .accessibilityLabel("Cancel model load")
          .accessibilityIdentifier("cancel-model-load")
        } else if !session.isModelLoaded {
          Button("Load") {
            session.loadModel()
          }
          .buttonStyle(.borderedProminent)
          .controlSize(.small)
          .disabled(!session.isWorkspaceReady)
          .accessibilityIdentifier("load-model")
        }
      }

      if let error = session.errorMessage, !error.isEmpty {
        Label(error, systemImage: "exclamationmark.triangle.fill")
          .font(.footnote)
          .foregroundStyle(.red)
          .textSelection(.enabled)
          .accessibilityIdentifier("assistant-error")
      }

      if let warning = session.resourceWarning, !warning.isEmpty {
        Label(warning, systemImage: "gauge.with.dots.needle.50percent")
          .font(.footnote)
          .foregroundStyle(.orange)
          .accessibilityIdentifier("resource-warning")
      }
    }
    .padding(14)
    .background(.thinMaterial, in: RoundedRectangle(cornerRadius: 16))
  }

  private var statusSymbol: String {
    if session.isRunning { return "sparkles" }
    if !session.isWorkspaceReady { return "exclamationmark.triangle.fill" }
    if session.isModelLoading { return "memorychip.fill" }
    return session.isModelLoaded ? "checkmark.circle.fill" : "memorychip"
  }

  private var statusColor: Color {
    if session.isRunning { return .orange }
    if !session.isWorkspaceReady { return .red }
    if session.isModelLoading { return .orange }
    return session.isModelLoaded ? .green : .secondary
  }
}

private struct EmptyChatView: View {
  let isModelLoaded: Bool

  var body: some View {
    ContentUnavailableView {
      Label("Your on-device assistant", systemImage: "sparkles")
    } description: {
      Text(
        isModelLoaded
          ? "Ask a question, save a memory, or manage a task. Actions stay bounded and auditable."
          : "Explicit local commands work now. Load the model when you want a private conversation."
      )
    }
  }
}

private struct MessageBubble: View {
  let message: AssistantMessage

  var body: some View {
    HStack(alignment: .bottom, spacing: 8) {
      if message.role == .user {
        Spacer(minLength: 46)
      }

      VStack(alignment: message.role == .user ? .trailing : .leading, spacing: 5) {
        Text(message.content)
          .font(.body)
          .foregroundStyle(message.role == .user ? Color.white : Color.primary)
          .textSelection(.enabled)

        HStack(spacing: 5) {
          if message.wasStopped {
            Image(systemName: "stop.circle")
            Text("Stopped")
          }
          Text(message.createdAt, format: .dateTime.hour().minute())
        }
        .font(.caption2)
        .foregroundStyle(message.role == .user ? Color.white.opacity(0.75) : .secondary)
      }
      .padding(.horizontal, 14)
      .padding(.vertical, 10)
      .background(bubbleColor, in: RoundedRectangle(cornerRadius: 17))
      .accessibilityElement(children: .combine)

      if message.role == .assistant {
        Spacer(minLength: 46)
      }
    }
    .frame(maxWidth: .infinity)
  }

  private var bubbleColor: Color {
    message.role == .user ? .accentColor : Color(uiColor: .secondarySystemBackground)
  }
}

private struct ChatComposer: View {
  @Bindable var session: AssistantSession

  var body: some View {
    HStack(alignment: .bottom, spacing: 10) {
      TextField(
        session.isModelLoaded
          ? "Message Dolphin"
          : "Local command, or load model to chat",
        text: $session.draft,
        axis: .vertical
      )
        .lineLimit(1...5)
        .textFieldStyle(.plain)
        .padding(.horizontal, 14)
        .padding(.vertical, 11)
        .background(Color(uiColor: .secondarySystemBackground), in: RoundedRectangle(cornerRadius: 20))
        .disabled(
          session.isRunning
            || session.isModelLoading
            || !session.isWorkspaceReady
        )
        .submitLabel(.send)
        .onSubmit(sendIfPossible)
        .accessibilityIdentifier("assistant-composer")

      if session.isRunning {
        Button(role: .destructive) {
          session.stop()
        } label: {
          Image(systemName: "stop.fill")
            .font(.headline)
            .frame(width: 44, height: 44)
        }
        .buttonStyle(.borderedProminent)
        .clipShape(Circle())
        .accessibilityLabel("Stop assistant")
        .accessibilityIdentifier("stop-assistant")
      } else {
        Button(action: sendIfPossible) {
          Image(systemName: "arrow.up")
            .font(.headline)
            .frame(width: 44, height: 44)
        }
        .buttonStyle(.borderedProminent)
        .clipShape(Circle())
        .disabled(!canSend)
        .accessibilityLabel("Send message")
        .accessibilityIdentifier("send-message")
      }
    }
    .padding(.horizontal)
    .padding(.vertical, 10)
    .background(.bar)
  }

  private var canSend: Bool {
    session.canSubmitDraft
  }

  private func sendIfPossible() {
    guard canSend else { return }
    session.send()
  }
}

struct ActivityView: View {
  @Bindable var session: AssistantSession
  @State private var expandedRunIDs: Set<UUID> = []

  var body: some View {
    NavigationStack {
      Group {
        if session.workspace.runs.isEmpty && session.workspace.events.isEmpty {
          ContentUnavailableView(
            "No activity yet",
            systemImage: "list.bullet.rectangle.portrait",
            description: Text("Model turns, approvals, and tool actions will appear here.")
          )
        } else {
          List {
            if !orderedRuns.isEmpty {
              Section("Runs") {
                ForEach(orderedRuns) { run in
                  DisclosureGroup(
                    isExpanded: expansionBinding(for: run.id)
                  ) {
                    let events = runEvents(for: run.id)
                    if events.isEmpty {
                      Text("No timeline events were recorded for this run.")
                        .font(.footnote)
                        .foregroundStyle(.secondary)
                        .padding(.vertical, 6)
                    } else {
                      ForEach(events) { event in
                        ActivityRow(event: event)
                          .padding(.leading, 4)
                      }
                    }
                  } label: {
                    RunSummaryCard(run: run)
                  }
                }
              }
            }

            if !standaloneEvents.isEmpty {
              Section("Standalone events") {
                ForEach(standaloneEvents) { event in
                  ActivityRow(event: event)
                }
              }
            }
          }
          .listStyle(.insetGrouped)
        }
      }
      .navigationTitle("Activity")
      .navigationBarTitleDisplayMode(.inline)
      .toolbar {
        ToolbarItem(placement: .topBarTrailing) {
          if session.isRunning {
            Button("Stop", systemImage: "stop.fill", role: .destructive) {
              session.stop()
            }
          }
        }
      }
    }
  }

  private var orderedRuns: [AgentRunRecord] {
    session.workspace.runs.sorted { first, second in
      first.createdAt > second.createdAt
    }
  }

  private var standaloneEvents: [AgentEvent] {
    let currentRunIDs = Set(session.workspace.runs.map(\.id))
    return session.workspace.events.filter { event in
      guard let runID = event.runID else { return true }
      return !currentRunIDs.contains(runID)
    }.sorted { first, second in
      first.createdAt > second.createdAt
    }
  }

  private func runEvents(for runID: UUID) -> [AgentEvent] {
    session.workspace.events.filter { $0.runID == runID }.sorted {
      first, second in
      if first.sequence == second.sequence {
        return first.createdAt < second.createdAt
      }
      return first.sequence < second.sequence
    }
  }

  private func expansionBinding(for runID: UUID) -> Binding<Bool> {
    Binding(
      get: { expandedRunIDs.contains(runID) },
      set: { isExpanded in
        if isExpanded {
          expandedRunIDs.insert(runID)
        } else {
          expandedRunIDs.remove(runID)
        }
      }
    )
  }
}

private struct RunSummaryCard: View {
  let run: AgentRunRecord

  var body: some View {
    VStack(alignment: .leading, spacing: 10) {
      Text(run.requestText.isEmpty ? "Untitled request" : run.requestText)
        .font(.headline)
        .lineLimit(3)

      HStack(spacing: 8) {
        StatusPill(text: run.status.label, color: run.status.tint)

        if let stopReason = run.stopReason {
          Label(stopReason.label, systemImage: "stop.circle")
            .font(.caption)
            .foregroundStyle(.secondary)
        }

        Spacer(minLength: 4)

        Text(run.createdAt, format: .dateTime.month().day().hour().minute())
          .font(.caption)
          .foregroundStyle(.tertiary)
      }

      Label(
        "Phase: \(run.checkpoint.phase.label)",
        systemImage: run.checkpoint.phase.symbolName
      )
      .font(.subheadline)
      .foregroundStyle(.secondary)

      HStack(spacing: 14) {
        RunMetric(
          value: run.checkpoint.modelTurns,
          label: "model",
          systemImage: "memorychip"
        )
        RunMetric(
          value: run.checkpoint.toolCallsUsed,
          label: "tools",
          systemImage: "wrench.and.screwdriver"
        )
        RunMetric(
          value: run.checkpoint.completedReceipts.count,
          label: "verified",
          systemImage: "checkmark.shield"
        )
      }

      if let error = run.errorSummary, !error.isEmpty {
        Label(error, systemImage: "exclamationmark.triangle.fill")
          .font(.footnote)
          .foregroundStyle(.red)
          .textSelection(.enabled)
      }
    }
    .padding(.vertical, 6)
  }
}

private struct RunMetric: View {
  let value: Int
  let label: String
  let systemImage: String

  var body: some View {
    Label("\(value) \(label)", systemImage: systemImage)
      .font(.caption)
      .foregroundStyle(.secondary)
      .accessibilityLabel("\(value) \(label)")
  }
}

private struct ActivityRow: View {
  let event: AgentEvent

  var body: some View {
    HStack(alignment: .top, spacing: 12) {
      Image(systemName: event.status.symbolName)
        .foregroundStyle(event.status.tint)
        .font(.title3)
        .frame(width: 26)
        .accessibilityHidden(true)

      VStack(alignment: .leading, spacing: 5) {
        HStack(alignment: .firstTextBaseline) {
          Text(event.title)
            .font(.headline)
          Spacer()
          StatusPill(text: event.status.label, color: event.status.tint)
        }

        if !event.detail.isEmpty {
          Text(event.detail)
            .font(.subheadline)
            .foregroundStyle(.secondary)
            .textSelection(.enabled)
        }

        HStack(spacing: 6) {
          Label(event.kind.label, systemImage: event.kind.symbolName)
          Text("·")
          Text("Step \(event.sequence)")
          Text("·")
          Text(event.createdAt, format: .dateTime.hour().minute().second())
        }
        .font(.caption)
        .foregroundStyle(.tertiary)
      }
    }
    .padding(.vertical, 4)
    .accessibilityElement(children: .combine)
  }
}

struct SettingsView: View {
  @Bindable var session: AssistantSession
  @Environment(\.openURL) private var openURL
  @State private var settingsDraft = AgentSettings()
  @State private var hasSynchronizedSettings = false

  var body: some View {
    NavigationStack {
      Form {
        modelSection
        autonomySection
        permissionsSection
        systemContextSection
        instructionSection
        privacySection
      }
      .navigationTitle("Settings")
      .navigationBarTitleDisplayMode(.inline)
      .toolbar {
        ToolbarItem(placement: .topBarTrailing) {
          Button("Save") {
            if let appliedSettings = session.saveSettings(settingsDraft) {
              settingsDraft = appliedSettings
              hasSynchronizedSettings = true
            }
          }
          .disabled(
            !session.isWorkspaceReady
              || session.isRunning
              || !hasUnsavedSettings
          )
          .accessibilityIdentifier("save-settings")
        }
      }
      .onAppear {
        synchronizeSettingsIfNeeded()
      }
      .onChange(of: session.isWorkspaceReady) { _, isReady in
        if isReady {
          synchronizeSettings(force: true)
        } else {
          hasSynchronizedSettings = false
        }
      }
      .onChange(
        of: session.workspace.settings.enabledSystemCapabilities
      ) { _, capabilities in
        settingsDraft.enabledSystemCapabilities = capabilities
      }
      .task(id: session.isWorkspaceReady) {
        guard session.isWorkspaceReady else { return }
        session.refreshResourceSnapshot()
        await session.refreshCalendarAuthorization()
      }
    }
  }

  private var modelSection: some View {
    Section("On-device model") {
      LabeledContent("Status") {
        HStack(spacing: 6) {
          Circle()
            .fill(
              !session.isWorkspaceReady
                ? Color.red
                : (session.isModelLoaded ? Color.green : Color.secondary)
            )
            .frame(width: 8, height: 8)
          Text(session.modelStatus)
        }
      }

      if session.isModelLoaded {
        Button("Release Model", systemImage: "eject") {
          session.releaseModel()
        }
        .disabled(session.isRunning)
      } else if session.isModelLoading {
        HStack {
          ProgressView()
            .controlSize(.small)
          Text("Loading model…")
            .foregroundStyle(.secondary)
          Spacer()
          Button("Cancel Load", systemImage: "xmark.circle") {
            session.cancelModelLoad()
          }
          .accessibilityIdentifier("settings-cancel-model-load")
        }
      } else {
        Button("Load Model", systemImage: "memorychip") {
          session.loadModel()
        }
        .buttonStyle(.borderedProminent)
        .disabled(!session.isWorkspaceReady || session.isModelLoading)
      }

      Text("The 1.81 GB stateful INT4 model stays on this device. Releasing it recovers memory.")
        .font(.footnote)
        .foregroundStyle(.secondary)

      if let snapshot = session.resourceSnapshot {
        LabeledContent("Thermal state", value: snapshot.thermalLevel.label)
        LabeledContent(
          "Low Power Mode",
          value: snapshot.lowPowerModeEnabled ? "On" : "Off"
        )
        if let availableMemory = snapshot.availableMemoryBytes {
          LabeledContent(
            "Available memory (advisory)",
            value: ByteCountFormatter.string(
              fromByteCount: Int64(clamping: availableMemory),
              countStyle: .memory
            )
          )
        }
      }

      if let warning = session.resourceWarning, !warning.isEmpty {
        Label(warning, systemImage: "exclamationmark.triangle")
          .font(.footnote)
          .foregroundStyle(.orange)
      }
    }
  }

  private var autonomySection: some View {
    Section("Run limits") {
      Stepper(
        "Tool steps: \(settingsDraft.maxToolSteps)",
        value: settingBinding(\.maxToolSteps),
        in: AgentSettings.maxToolStepsRange
      )

      Stepper(
        "Output budget: \(settingsDraft.maxNewTokens) tokens",
        value: settingBinding(\.maxNewTokens),
        in: AgentSettings.maxNewTokensRange,
        step: 32
      )

      Stepper(
        "Time limit: \(settingsDraft.maxRunSeconds) seconds",
        value: settingBinding(\.maxRunSeconds),
        in: AgentSettings.maxRunSecondsRange,
        step: 30
      )

      Text("Tool and output limits are hard boundaries. At the time deadline, Dolphin requests cancellation; a Core ML prediction already in progress must return before the stop is observed.")
        .font(.footnote)
        .foregroundStyle(.secondary)
    }
    .disabled(!session.isWorkspaceReady || session.isRunning)
  }

  private var permissionsSection: some View {
    Section("Permissions") {
      Toggle(
        "Require approval for local changes",
        isOn: settingBinding(\.requireApprovalForLocalWrites)
      )

      Label(
        "External network tools are disabled until connected-peer validation can be enforced.",
        systemImage: "network.slash"
      )
      .font(.footnote)
      .foregroundStyle(.secondary)
    }
    .disabled(!session.isWorkspaceReady || session.isRunning)
  }

  private var systemContextSection: some View {
    Section("System context") {
      LabeledContent("Calendar") {
        Label(
          calendarStatusText,
          systemImage: calendarContextEnabled
            ? "checkmark.shield.fill"
            : "calendar.badge.exclamationmark"
        )
        .foregroundStyle(
          calendarContextEnabled ? Color.green : Color.secondary
        )
      }

      if session.isCalendarAccessRequesting {
        HStack(spacing: 10) {
          ProgressView()
          Text("Waiting for Calendar permission…")
            .foregroundStyle(.secondary)
        }
      } else if calendarContextEnabled {
        Button("Disable Calendar Context", systemImage: "calendar.badge.minus") {
          session.disableCalendarContext()
        }
      } else {
        calendarEnableAction
      }

      Text(
        "Calendar is opt-in. Dolphin exposes only bounded event titles and times, only in the foreground, and asks you to approve each exact read. It never creates or edits events."
      )
      .font(.footnote)
      .foregroundStyle(.secondary)
    }
    .disabled(
      !session.isWorkspaceReady
        || session.isRunning
        || session.isCalendarAccessRequesting
    )
  }

  @ViewBuilder
  private var calendarEnableAction: some View {
    switch session.calendarAuthorization {
    case .notDetermined:
      Button("Grant and Enable Calendar", systemImage: "calendar.badge.plus") {
        Task { await session.requestAndEnableCalendarAccess() }
      }
      .buttonStyle(.borderedProminent)
      .accessibilityIdentifier("grant-calendar-access")

    case .fullAccess:
      Button("Enable Calendar Context", systemImage: "calendar.badge.plus") {
        session.enableCalendarContext()
      }
      .buttonStyle(.borderedProminent)
      .accessibilityIdentifier("enable-calendar-context")

    case .denied, .writeOnly:
      Button("Open iOS Settings", systemImage: "gear") {
        guard let url = URL(string: UIApplication.openSettingsURLString) else {
          return
        }
        openURL(url)
      }
      .accessibilityIdentifier("open-calendar-settings")

    case .restricted:
      Label(
        "Calendar access is restricted on this iPhone.",
        systemImage: "lock.trianglebadge.exclamationmark"
      )
      .foregroundStyle(.secondary)
    }
  }

  private var calendarContextEnabled: Bool {
    session.workspace.settings.enabledSystemCapabilities.contains(.calendarRead)
      && session.calendarAuthorization == .fullAccess
  }

  private var calendarStatusText: String {
    if calendarContextEnabled { return "Enabled" }
    switch session.calendarAuthorization {
    case .fullAccess: return "OS access granted; Dolphin disabled"
    case .notDetermined: return "Not requested"
    case .denied: return "Denied in iOS"
    case .restricted: return "Restricted"
    case .writeOnly: return "Write-only; read access required"
    }
  }

  private var instructionSection: some View {
    Section("Assistant instruction") {
      TextEditor(text: settingBinding(\.systemPrompt))
        .frame(minHeight: 150)
        .font(.body.monospaced())
        .disabled(session.isRunning)

      Button("Restore Default") {
        settingsDraft.systemPrompt = AgentSettings.defaultSystemPrompt
      }
      .disabled(session.isRunning)
    }
    .disabled(!session.isWorkspaceReady || session.isRunning)
  }

  private var privacySection: some View {
    Section("Privacy and control") {
      Label("Conversation, memories, tasks, and activity are stored locally.", systemImage: "iphone")
      Label("Tool actions are recorded in Activity.", systemImage: "checkmark.shield")
      Label("Closing or backgrounding the app stops an active run.", systemImage: "stop.circle")
    }
    .font(.footnote)
    .foregroundStyle(.secondary)
  }

  private func settingBinding<Value>(
    _ keyPath: WritableKeyPath<AgentSettings, Value>
  ) -> Binding<Value> {
    Binding(
      get: { settingsDraft[keyPath: keyPath] },
      set: { settingsDraft[keyPath: keyPath] = $0 }
    )
  }

  private var hasUnsavedSettings: Bool {
    hasSynchronizedSettings
      && settingsDraft != session.workspace.settings
  }

  private func synchronizeSettingsIfNeeded() {
    guard session.isWorkspaceReady, !hasSynchronizedSettings else { return }
    synchronizeSettings(force: true)
  }

  private func synchronizeSettings(force: Bool) {
    guard session.isWorkspaceReady else { return }
    guard force || !hasUnsavedSettings else { return }
    settingsDraft = session.workspace.settings
    hasSynchronizedSettings = true
  }
}

private struct ToolApprovalSheet: View {
  let request: ToolApprovalRequest
  let approve: () -> Void
  let deny: () -> Void

  var body: some View {
    NavigationStack {
      ScrollView {
        VStack(alignment: .leading, spacing: 20) {
          Label("Dolphin is waiting for you", systemImage: "hand.raised.fill")
            .font(.title2.weight(.bold))
            .foregroundStyle(.orange)

          VStack(alignment: .leading, spacing: 8) {
            Text(request.toolName)
              .font(.headline)
            Text(request.reason)
              .foregroundStyle(.secondary)
          }

          VStack(alignment: .leading, spacing: 8) {
            Text("Exact action")
              .font(.caption.weight(.semibold))
              .foregroundStyle(.secondary)
              .textCase(.uppercase)
            Text(request.call.name)
              .font(.body.monospaced().weight(.semibold))
            Text(JSONValue.object(request.call.arguments).canonicalJSON)
              .font(.footnote.monospaced())
              .textSelection(.enabled)
              .frame(maxWidth: .infinity, alignment: .leading)
              .padding(12)
              .background(Color(uiColor: .secondarySystemBackground), in: RoundedRectangle(cornerRadius: 10))
          }

          Label(
            request.isExpired()
              ? "This approval has expired. Deny it and run the request again."
              : "Approval applies once, only to the exact action shown above, and expires at \(request.expiresAt.formatted(date: .omitted, time: .shortened)).",
            systemImage: request.isExpired() ? "clock.badge.xmark" : "lock.shield"
          )
          .font(.footnote)
          .foregroundStyle(request.isExpired() ? .red : .secondary)

          VStack(spacing: 10) {
            Button {
              approve()
            } label: {
              Label("Approve Once", systemImage: "checkmark.shield.fill")
                .frame(maxWidth: .infinity)
            }
            .buttonStyle(.borderedProminent)
            .controlSize(.large)
            .disabled(request.isExpired())
            .accessibilityIdentifier("approve-tool")

            Button("Deny", systemImage: "xmark", role: .destructive) {
              deny()
            }
            .buttonStyle(.bordered)
            .controlSize(.large)
            .frame(maxWidth: .infinity)
            .accessibilityIdentifier("deny-tool")
          }
        }
        .padding(20)
        .frame(maxWidth: 620)
        .frame(maxWidth: .infinity)
      }
      .navigationTitle("Approve Action")
      .navigationBarTitleDisplayMode(.inline)
      .toolbar {
        ToolbarItem(placement: .cancellationAction) {
          Button("Deny", role: .cancel) {
            deny()
          }
        }
      }
    }
    .interactiveDismissDisabled()
    .presentationDetents([.medium, .large])
  }
}

private struct StatusPill: View {
  let text: String
  let color: Color

  var body: some View {
    Text(text)
      .font(.caption2.weight(.semibold))
      .foregroundStyle(color)
      .padding(.horizontal, 7)
      .padding(.vertical, 3)
      .background(color.opacity(0.12), in: Capsule())
  }
}

private extension AgentEventStatus {
  var label: String {
    switch self {
    case .information: "Info"
    case .running: "Running"
    case .awaitingApproval: "Approval"
    case .succeeded: "Succeeded"
    case .denied: "Denied"
    case .failed: "Failed"
    case .cancelled: "Cancelled"
    }
  }

  var symbolName: String {
    switch self {
    case .information: "info.circle.fill"
    case .running: "arrow.trianglehead.2.clockwise.rotate.90"
    case .awaitingApproval: "hand.raised.fill"
    case .succeeded: "checkmark.circle.fill"
    case .denied: "hand.raised.slash.fill"
    case .failed: "xmark.octagon.fill"
    case .cancelled: "stop.circle.fill"
    }
  }

  var tint: Color {
    switch self {
    case .information: .blue
    case .running: .orange
    case .awaitingApproval: .orange
    case .succeeded: .green
    case .denied: .secondary
    case .failed: .red
    case .cancelled: .secondary
    }
  }
}

private extension AgentEventKind {
  var label: String {
    switch self {
    case .run: "Run"
    case .model: "Model"
    case .tool: "Tool"
    case .approval: "Approval"
    case .persistence: "Storage"
    }
  }

  var symbolName: String {
    switch self {
    case .run: "play.circle"
    case .model: "memorychip"
    case .tool: "wrench.and.screwdriver"
    case .approval: "hand.raised"
    case .persistence: "externaldrive"
    }
  }
}

private extension AgentRunStatus {
  var label: String {
    switch self {
    case .running: "Running"
    case .cancellationRequested: "Stopping"
    case .succeeded: "Succeeded"
    case .failed: "Failed"
    case .cancelled: "Cancelled"
    case .interrupted: "Interrupted"
    }
  }

  var tint: Color {
    switch self {
    case .running, .cancellationRequested: .orange
    case .succeeded: .green
    case .failed, .interrupted: .red
    case .cancelled: .secondary
    }
  }
}

private extension AgentRunPhase {
  var label: String {
    switch self {
    case .preparing: "Preparing"
    case .routing: "Routing"
    case .modelGeneration: "Model generation"
    case .awaitingApproval: "Awaiting approval"
    case .toolExecution: "Tool execution"
    case .finalizing: "Finalizing"
    }
  }

  var symbolName: String {
    switch self {
    case .preparing: "list.clipboard"
    case .routing: "arrow.triangle.branch"
    case .modelGeneration: "memorychip"
    case .awaitingApproval: "hand.raised"
    case .toolExecution: "wrench.and.screwdriver"
    case .finalizing: "checkmark.seal"
    }
  }
}

private extension AgentRunStopReason {
  var label: String {
    switch self {
    case .user: "Stopped by user"
    case .background: "App backgrounded"
    case .deadline: "Time limit reached"
    case .storageFailure: "Storage failure"
    case .processEnded: "App process ended"
    }
  }
}

private extension AssistantThermalLevel {
  var label: String {
    switch self {
    case .nominal: "Nominal"
    case .fair: "Fair"
    case .serious: "Serious"
    case .critical: "Critical"
    case .unknown: "Unknown"
    }
  }
}