NCAssistantTask.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // NCAssistantModel.swift
  3. // Nextcloud
  4. //
  5. // Created by Milen on 08.04.24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. import NextcloudKit
  11. import SwiftUI
  12. class NCAssistantTask: ObservableObject {
  13. @Published var types: [NKTextProcessingTaskType] = []
  14. @Published var filteredTasks: [NKTextProcessingTask] = []
  15. @Published var selectedType: NKTextProcessingTaskType?
  16. @Published var selectedTask: NKTextProcessingTask?
  17. @Published var hasError: Bool = false
  18. @Published var isLoading: Bool = false
  19. @Published var controller: NCMainTabBarController?
  20. private var tasks: [NKTextProcessingTask] = []
  21. private let excludedTypeIds = ["OCA\\ContextChat\\TextProcessing\\ContextChatTaskType"]
  22. private var session: NCSession.Session {
  23. NCSession.shared.getSession(controller: controller)
  24. }
  25. init(controller: NCMainTabBarController?) {
  26. self.controller = controller
  27. load()
  28. }
  29. func load() {
  30. loadAllTypes()
  31. }
  32. func filterTasks(ofType type: NKTextProcessingTaskType?) {
  33. if let type {
  34. self.filteredTasks = tasks.filter({ $0.type == type.id })
  35. } else {
  36. self.filteredTasks = tasks
  37. }
  38. self.filteredTasks = filteredTasks.sorted(by: { $0.completionExpectedAt ?? 0 > $1.completionExpectedAt ?? 0 })
  39. }
  40. func selectTaskType(_ type: NKTextProcessingTaskType?) {
  41. selectedType = type
  42. filterTasks(ofType: self.selectedType)
  43. }
  44. func selectTask(_ task: NKTextProcessingTask) {
  45. selectedTask = task
  46. guard let id = task.id else { return }
  47. isLoading = true
  48. NextcloudKit.shared.textProcessingGetTask(taskId: id, account: session.account) { _, task, _, error in
  49. self.isLoading = false
  50. if error != .success {
  51. self.hasError = true
  52. return
  53. }
  54. self.selectedTask = task
  55. }
  56. }
  57. func scheduleTask(input: String) {
  58. isLoading = true
  59. NextcloudKit.shared.textProcessingSchedule(input: input, typeId: selectedType?.id ?? "", identifier: "assistant", account: session.account) { _, task, _, error in
  60. self.isLoading = false
  61. if error != .success {
  62. self.hasError = true
  63. return
  64. }
  65. guard let task else { return }
  66. withAnimation {
  67. self.tasks.insert(task, at: 0)
  68. self.filteredTasks.insert(task, at: 0)
  69. }
  70. }
  71. }
  72. func deleteTask(_ task: NKTextProcessingTask) {
  73. guard let id = task.id else { return }
  74. isLoading = true
  75. NextcloudKit.shared.textProcessingDeleteTask(taskId: id, account: session.account) { _, task, _, error in
  76. self.isLoading = false
  77. if error != .success {
  78. self.hasError = true
  79. return
  80. }
  81. withAnimation {
  82. self.tasks.removeAll(where: { $0.id == task?.id })
  83. self.filteredTasks.removeAll(where: { $0.id == task?.id })
  84. }
  85. }
  86. }
  87. private func loadAllTypes() {
  88. isLoading = true
  89. NextcloudKit.shared.textProcessingGetTypes(account: session.account) { _, types, _, error in
  90. self.isLoading = false
  91. if error != .success {
  92. self.hasError = true
  93. return
  94. }
  95. guard let filteredTypes = types?.filter({ !self.excludedTypeIds.contains($0.id ?? "")}), !filteredTypes.isEmpty else { return }
  96. withAnimation {
  97. self.types = filteredTypes
  98. }
  99. if self.selectedType == nil {
  100. self.selectTaskType(filteredTypes.first)
  101. }
  102. self.loadAllTasks()
  103. }
  104. }
  105. private func loadAllTasks(appId: String = "assistant") {
  106. isLoading = true
  107. NextcloudKit.shared.textProcessingTaskList(appId: appId, account: session.account) { _, tasks, _, error in
  108. self.isLoading = false
  109. if error != .success {
  110. self.hasError = true
  111. return
  112. }
  113. guard let tasks = tasks else { return }
  114. self.tasks = tasks
  115. self.filterTasks(ofType: self.selectedType)
  116. }
  117. }
  118. }
  119. extension NCAssistantTask {
  120. public func loadDummyData() {
  121. let loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  122. var tasks: [NKTextProcessingTask] = []
  123. for index in 1...10 {
  124. tasks.append(NKTextProcessingTask(id: index, type: "OCP\\TextProcessing\\FreePromptTaskType", status: index, userId: "christine", appId: "assistant", input: loremIpsum, output: loremIpsum, identifier: "", completionExpectedAt: 1712666412))
  125. }
  126. self.types = [
  127. NKTextProcessingTaskType(id: "1", name: "Free Prompt", description: ""),
  128. NKTextProcessingTaskType(id: "2", name: "Summarize", description: ""),
  129. NKTextProcessingTaskType(id: "3", name: "Generate headline", description: ""),
  130. NKTextProcessingTaskType(id: "4", name: "Reformulate", description: "")
  131. ]
  132. self.tasks = tasks
  133. self.filteredTasks = tasks
  134. self.selectedType = types[0]
  135. self.selectedTask = filteredTasks[0]
  136. }
  137. }
  138. extension NKTextProcessingTask {
  139. struct StatusInfo {
  140. let stringKey, imageSystemName: String
  141. }
  142. var statusInfo: StatusInfo {
  143. return switch status {
  144. case 1: StatusInfo(stringKey: "_assistant_task_scheduled_", imageSystemName: "clock")
  145. case 2: StatusInfo(stringKey: "_assistant_task_in_progress_", imageSystemName: "clock.badge")
  146. case 3: StatusInfo(stringKey: "_assistant_task_completed_", imageSystemName: "checkmark.circle")
  147. case 4: StatusInfo(stringKey: "_assistant_task_failed_", imageSystemName: "exclamationmark.circle")
  148. default: StatusInfo(stringKey: "_assistant_task_unknown_", imageSystemName: "questionmark.circle")
  149. }
  150. }
  151. }