ErrorView.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseVertexAI
  15. import SwiftUI
  16. struct ErrorView: View {
  17. var error: Error
  18. @State private var isDetailsSheetPresented = false
  19. var body: some View {
  20. HStack {
  21. Text("An error occurred.")
  22. Button(action: { isDetailsSheetPresented.toggle() }) {
  23. Image(systemName: "info.circle")
  24. }
  25. }
  26. .frame(maxWidth: .infinity, alignment: .center)
  27. .listRowSeparator(.hidden)
  28. .sheet(isPresented: $isDetailsSheetPresented) {
  29. ErrorDetailsView(error: error)
  30. }
  31. }
  32. }
  33. #Preview {
  34. NavigationView {
  35. let errorPromptBlocked = GenerateContentError.promptBlocked(
  36. response: GenerateContentResponse(
  37. candidates: [
  38. Candidate(
  39. content: ModelContent(role: "model", parts: [
  40. """
  41. A _hypothetical_ model response.
  42. Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
  43. """,
  44. ]),
  45. safetyRatings: [
  46. SafetyRating(
  47. category: .dangerousContent,
  48. probability: .high,
  49. probabilityScore: 0.8,
  50. severity: .medium,
  51. severityScore: 0.9,
  52. blocked: true
  53. ),
  54. SafetyRating(
  55. category: .harassment,
  56. probability: .low,
  57. probabilityScore: 0.5,
  58. severity: .low,
  59. severityScore: 0.6,
  60. blocked: false
  61. ),
  62. SafetyRating(
  63. category: .hateSpeech,
  64. probability: .low,
  65. probabilityScore: 0.3,
  66. severity: .medium,
  67. severityScore: 0.2,
  68. blocked: false
  69. ),
  70. SafetyRating(
  71. category: .sexuallyExplicit,
  72. probability: .low,
  73. probabilityScore: 0.2,
  74. severity: .negligible,
  75. severityScore: 0.5,
  76. blocked: false
  77. ),
  78. ],
  79. finishReason: FinishReason.other,
  80. citationMetadata: nil
  81. ),
  82. ]
  83. )
  84. )
  85. List {
  86. MessageView(message: ChatMessage.samples[0])
  87. MessageView(message: ChatMessage.samples[1])
  88. ErrorView(error: errorPromptBlocked)
  89. }
  90. .listStyle(.plain)
  91. .navigationTitle("Chat sample")
  92. }
  93. }