ChatMessage.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Foundation
  15. enum Participant {
  16. case system
  17. case user
  18. }
  19. struct ChatMessage: Identifiable, Equatable {
  20. let id = UUID().uuidString
  21. var message: String
  22. let participant: Participant
  23. var pending = false
  24. static func pending(participant: Participant) -> ChatMessage {
  25. Self(message: "", participant: participant, pending: true)
  26. }
  27. }
  28. extension ChatMessage {
  29. static var samples: [ChatMessage] = [
  30. .init(message: "Hello. What can I do for you today?", participant: .system),
  31. .init(message: "Show me a simple loop in Swift.", participant: .user),
  32. .init(message: """
  33. Sure, here is a simple loop in Swift:
  34. # Example 1
  35. ```
  36. for i in 1...5 {
  37. print("Hello, world!")
  38. }
  39. ```
  40. This loop will print the string "Hello, world!" five times. The for loop iterates over a range of numbers,
  41. in this case the numbers from 1 to 5. The variable i is assigned each number in the range, and the code inside the loop is executed.
  42. **Here is another example of a simple loop in Swift:**
  43. ```swift
  44. var sum = 0
  45. for i in 1...100 {
  46. sum += i
  47. }
  48. print("The sum of the numbers from 1 to 100 is \\(sum).")
  49. ```
  50. This loop calculates the sum of the numbers from 1 to 100. The variable sum is initialized to 0, and then the for loop iterates over the range of numbers from 1 to 100. The variable i is assigned each number in the range, and the value of i is added to the sum variable. After the loop has finished executing, the value of sum is printed to the console.
  51. """, participant: .system),
  52. ]
  53. static var sample = samples[0]
  54. }