forked from osaurus-ai/osaurus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskDispatcher.swift
More file actions
41 lines (35 loc) · 1.43 KB
/
Copy pathTaskDispatcher.swift
File metadata and controls
41 lines (35 loc) · 1.43 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
//
// TaskDispatcher.swift
// osaurus
//
// Thin dispatch orchestrator. Every trigger source (schedules,
// shortcuts, plugins, HTTP, watchers) creates a DispatchRequest and
// hands it here; we route to BackgroundTaskManager which runs it as
// a headless chat session.
//
import Foundation
/// Routes dispatch requests to BackgroundTaskManager
@MainActor
public final class TaskDispatcher {
public static let shared = TaskDispatcher()
private init() {}
/// Dispatch a request for background execution as a headless chat task.
public func dispatch(_ request: DispatchRequest) async -> DispatchHandle? {
await BackgroundTaskManager.shared.dispatchChat(request)
}
/// Await completion of a dispatched task.
public func awaitCompletion(_ handle: DispatchHandle) async -> DispatchResult {
await BackgroundTaskManager.shared.awaitCompletion(handle.id)
}
/// Cancel a running dispatch.
public func cancel(_ id: UUID) {
guard BackgroundTaskManager.shared.isBackgroundTask(id) else { return }
BackgroundTaskManager.shared.cancelTask(id)
}
/// Lazily create a window from a dispatched execution context.
/// Called from ToastManager when the user taps a toast action.
public func openWindow(for contextId: UUID) {
guard BackgroundTaskManager.shared.isBackgroundTask(contextId) else { return }
BackgroundTaskManager.shared.openTaskWindow(contextId)
}
}