1071 lines
40 KiB
Swift
1071 lines
40 KiB
Swift
import Foundation
|
|
import Observation
|
|
|
|
enum FeatureDetailRenderChange: Equatable {
|
|
case full
|
|
case delta(FeatureDetailDelta)
|
|
}
|
|
|
|
struct FeatureDetailRenderUpdate: Equatable {
|
|
let baseRevision: UInt64
|
|
let revision: UInt64
|
|
let change: FeatureDetailRenderChange
|
|
}
|
|
|
|
@MainActor
|
|
@Observable
|
|
public final class FeatureRootModel {
|
|
public private(set) var snapshot = FeatureSnapshot()
|
|
public private(set) var details: [String: FeatureThreadDetail] = [:]
|
|
/// Advances whenever a Home presentation input changes.
|
|
public private(set) var homePresentationRevision: UInt64 = 0
|
|
/// Advances when a Home-visible thread is inserted, removed, or changed.
|
|
public private(set) var threadCollectionRevision: UInt64 = 0
|
|
/// Advances for any selected-thread metadata, message, approval, or input change.
|
|
public private(set) var detailRevision: UInt64 = 0
|
|
/// The latest detail revision for each loaded thread.
|
|
public private(set) var detailRevisions: [String: UInt64] = [:]
|
|
private(set) var detailRenderUpdates: [String: FeatureDetailRenderUpdate] = [:]
|
|
public private(set) var isLoading = true
|
|
public private(set) var isPerformingAction = false
|
|
public private(set) var isManagingConnections = false
|
|
public var errorMessage: String?
|
|
|
|
let client: any FeatureClient
|
|
private let outboxStore: FeatureOutboxStore
|
|
private var pendingSubmissionsByID: [String: FeatureQueuedSubmission] = [:]
|
|
private var pendingThreadsByID: [String: FeatureThread] = [:]
|
|
private var outboxDrainTask: Task<Void, Never>?
|
|
private var outboxRetryAttempt = 0
|
|
private var outboxGeneration: UInt64 = 0
|
|
|
|
public init(
|
|
client: any FeatureClient,
|
|
outboxStore: FeatureOutboxStore = .shared
|
|
) {
|
|
self.client = client
|
|
self.outboxStore = outboxStore
|
|
}
|
|
|
|
public func start() async {
|
|
do {
|
|
install(try await client.initialSnapshot())
|
|
} catch {
|
|
if !Self.isBenignCancellation(error) {
|
|
errorMessage = error.localizedDescription
|
|
}
|
|
}
|
|
await restoreOutbox()
|
|
isLoading = false
|
|
scheduleOutboxDrain()
|
|
|
|
for await event in client.events() {
|
|
apply(event)
|
|
}
|
|
}
|
|
|
|
public func reload() async {
|
|
do {
|
|
install(try await client.initialSnapshot())
|
|
} catch {
|
|
if !Self.isBenignCancellation(error) {
|
|
errorMessage = error.localizedDescription
|
|
}
|
|
}
|
|
}
|
|
|
|
public func pair(endpoint: String, token: String?) async -> Bool {
|
|
await perform {
|
|
try await client.pair(endpoint: endpoint, token: token)
|
|
clearDetails()
|
|
install(try await client.initialSnapshot())
|
|
}
|
|
}
|
|
|
|
public func activateEnvironment(_ id: String) async {
|
|
await perform {
|
|
try await client.activateEnvironment(id: id)
|
|
install(try await client.initialSnapshot())
|
|
clearDetails()
|
|
}
|
|
}
|
|
|
|
public func removeEnvironment(_ id: String) async {
|
|
await stopOutboxDrain()
|
|
await perform {
|
|
try await client.removeEnvironment(id: id)
|
|
try? await outboxStore.removeAll(environmentID: id)
|
|
removePendingSubmissions(environmentID: id)
|
|
install(try await client.initialSnapshot())
|
|
clearDetails()
|
|
}
|
|
scheduleOutboxDrain()
|
|
}
|
|
|
|
public func disconnect() async {
|
|
await stopOutboxDrain()
|
|
isManagingConnections = false
|
|
await client.disconnect()
|
|
let disconnectedEnvironments = snapshot.environments.map { environment in
|
|
var environment = environment
|
|
environment.connectionState = .disconnected
|
|
environment.connectionDetail = nil
|
|
return environment
|
|
}
|
|
install(FeatureSnapshot(
|
|
environments: disconnectedEnvironments,
|
|
settings: snapshot.settings
|
|
))
|
|
clearDetails()
|
|
}
|
|
|
|
public func setConnectionManagementPresented(_ isPresented: Bool) {
|
|
isManagingConnections = isPresented
|
|
}
|
|
|
|
public func addProject(path: String) async -> Bool {
|
|
await perform {
|
|
try await client.addProject(path: path)
|
|
install(try await client.initialSnapshot())
|
|
}
|
|
}
|
|
|
|
public func createThread(
|
|
projectID: String,
|
|
title: String?,
|
|
selection: FeatureSelection?
|
|
) async -> FeatureThread? {
|
|
let environment = currentEnvironmentIdentity
|
|
var created: FeatureThread?
|
|
let succeeded = await perform {
|
|
let thread = try await client.createThread(
|
|
projectID: projectID,
|
|
title: title,
|
|
selection: selection
|
|
)
|
|
guard currentEnvironmentIdentity == environment else {
|
|
throw CancellationError()
|
|
}
|
|
upsert(thread)
|
|
created = thread
|
|
}
|
|
return succeeded ? created : nil
|
|
}
|
|
|
|
public func startTask(_ request: NewTaskRequest) async -> FeatureThread? {
|
|
let prompt = request.trimmedPrompt
|
|
guard !prompt.isEmpty || !request.attachments.isEmpty else { return nil }
|
|
guard request.workspaceMode != .worktree || request.branch != nil else { return nil }
|
|
|
|
guard let project = snapshot.projects.first(where: { $0.id == request.projectID }) else {
|
|
errorMessage = "That project is no longer available."
|
|
return nil
|
|
}
|
|
let identity = FeatureSubmissionIdentity()
|
|
let threadID = FeatureScopedID.thread(
|
|
environmentID: project.environmentID,
|
|
wireID: identity.threadID
|
|
)
|
|
let uploads = request.attachments.map(\.upload)
|
|
let queued = FeatureQueuedSubmission(
|
|
environmentID: project.environmentID,
|
|
identity: identity,
|
|
threadID: threadID,
|
|
text: prompt,
|
|
selection: request.selection,
|
|
runtimeMode: request.runtimeMode,
|
|
interactionMode: request.interactionMode,
|
|
attachments: uploads,
|
|
creation: FeatureQueuedCreation(
|
|
projectID: request.projectID,
|
|
projectName: project.name,
|
|
workspaceMode: request.workspaceMode,
|
|
branch: request.branch,
|
|
worktreePath: request.worktreePath,
|
|
startFromOrigin: request.startFromOrigin
|
|
)
|
|
)
|
|
guard await enqueue(queued) else { return nil }
|
|
installPendingCreation(queued, project: project)
|
|
|
|
isPerformingAction = true
|
|
defer { isPerformingAction = false }
|
|
do {
|
|
let thread = try await client.createThreadAndSend(
|
|
projectID: request.projectID,
|
|
prompt: prompt,
|
|
selection: request.selection,
|
|
runtimeMode: request.runtimeMode.mobileNormalized,
|
|
interactionMode: request.interactionMode.mobileNormalized,
|
|
workspaceMode: request.workspaceMode,
|
|
branch: request.branch,
|
|
worktreePath: request.worktreePath,
|
|
startFromOrigin: request.startFromOrigin,
|
|
attachments: uploads,
|
|
identity: identity
|
|
)
|
|
await completeQueuedSubmission(queued)
|
|
if thread.id != queued.threadID {
|
|
removeThread(id: queued.threadID)
|
|
removeDetail(id: queued.threadID)
|
|
}
|
|
upsert(thread)
|
|
return thread
|
|
} catch {
|
|
if Self.shouldQueue(error, environmentID: project.environmentID, snapshot: snapshot) {
|
|
if isEnvironmentConnected(project.environmentID) {
|
|
scheduleOutboxRetry()
|
|
}
|
|
return pendingThreadsByID[threadID]
|
|
}
|
|
await discardQueuedSubmission(queued)
|
|
if !Self.isBenignCancellation(error) {
|
|
errorMessage = error.localizedDescription
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
public func workspaceBranches(
|
|
projectID: String,
|
|
refresh: Bool = false
|
|
) async throws -> [FeatureWorkspaceBranch] {
|
|
try await client.listWorkspaceBranches(projectID: projectID, refresh: refresh)
|
|
}
|
|
|
|
public func renameThread(_ id: String, title: String) async {
|
|
let environment = currentEnvironmentIdentity
|
|
await perform {
|
|
try await client.renameThread(id: id, title: title)
|
|
guard currentEnvironmentIdentity == environment else { return }
|
|
mutateThread(id: id) { $0.title = title }
|
|
}
|
|
}
|
|
|
|
public func setArchived(_ id: String, archived: Bool) async {
|
|
let environment = currentEnvironmentIdentity
|
|
await perform {
|
|
try await client.setThreadArchived(id: id, archived: archived)
|
|
guard currentEnvironmentIdentity == environment else { return }
|
|
mutateThread(id: id) { $0.isArchived = archived }
|
|
}
|
|
}
|
|
|
|
public func setSettled(_ id: String, settled: Bool) async {
|
|
let environment = currentEnvironmentIdentity
|
|
await perform {
|
|
try await client.setThreadSettled(id: id, settled: settled)
|
|
guard currentEnvironmentIdentity == environment else { return }
|
|
let settledAt = settled ? Date.now : nil
|
|
mutateThread(id: id) {
|
|
$0.isSettled = settled
|
|
$0.keepsActive = !settled
|
|
$0.settledAt = settledAt
|
|
}
|
|
}
|
|
}
|
|
|
|
public func setSnoozed(_ id: String, until: Date?) async {
|
|
let environment = currentEnvironmentIdentity
|
|
await perform {
|
|
try await client.setThreadSnoozed(id: id, until: until)
|
|
guard currentEnvironmentIdentity == environment else { return }
|
|
let snoozedAt = until.map { _ in Date.now }
|
|
mutateThread(id: id) {
|
|
$0.snoozedUntil = until
|
|
$0.snoozedAt = snoozedAt
|
|
}
|
|
}
|
|
}
|
|
|
|
public func setRuntimeMode(_ id: String, mode: FeatureRuntimeMode) async {
|
|
let mode = mode.mobileNormalized
|
|
let environment = currentEnvironmentIdentity
|
|
await perform {
|
|
try await client.setRuntimeMode(id: id, mode: mode)
|
|
guard currentEnvironmentIdentity == environment else { return }
|
|
mutateThread(id: id) { $0.runtimeMode = mode }
|
|
}
|
|
}
|
|
|
|
public func setInteractionMode(_ id: String, mode: FeatureInteractionMode) async {
|
|
let mode = mode.mobileNormalized
|
|
let environment = currentEnvironmentIdentity
|
|
await perform {
|
|
try await client.setInteractionMode(id: id, mode: mode)
|
|
guard currentEnvironmentIdentity == environment else { return }
|
|
mutateThread(id: id) { $0.interactionMode = mode }
|
|
}
|
|
}
|
|
|
|
public func deleteThread(_ id: String) async {
|
|
let environment = currentEnvironmentIdentity
|
|
await perform {
|
|
try await client.deleteThread(id: id)
|
|
guard currentEnvironmentIdentity == environment else { return }
|
|
removeThread(id: id)
|
|
removeDetail(id: id)
|
|
}
|
|
}
|
|
|
|
public func detail(for id: String, force: Bool = false) async -> FeatureThreadDetail? {
|
|
if !force, let cached = details[id] {
|
|
return cached
|
|
}
|
|
let environment = currentEnvironmentIdentity
|
|
do {
|
|
let detail = try await client.loadThread(id: id)
|
|
guard currentEnvironmentIdentity == environment else {
|
|
return details[id]
|
|
}
|
|
store(detail)
|
|
upsert(detail.thread)
|
|
return detail
|
|
} catch {
|
|
if !Self.isBenignCancellation(error) {
|
|
errorMessage = error.localizedDescription
|
|
}
|
|
return details[id]
|
|
}
|
|
}
|
|
|
|
/// Ends any selected-thread transport work when its detail view closes.
|
|
public func releaseThread(_ id: String) {
|
|
client.releaseThread(id: id)
|
|
}
|
|
|
|
public func sendMessage(threadID: String, text: String, selection: FeatureSelection?) async -> Bool {
|
|
await sendMessage(
|
|
FeatureMessageSubmission(
|
|
threadID: threadID,
|
|
text: text,
|
|
selection: selection
|
|
)
|
|
)
|
|
}
|
|
|
|
public func sendMessage(_ submission: FeatureMessageSubmission) async -> Bool {
|
|
let trimmed = submission.text.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !trimmed.isEmpty || !submission.attachments.isEmpty else { return false }
|
|
|
|
guard let thread = snapshot.threads.first(where: { $0.id == submission.threadID }),
|
|
let environmentID = thread.environmentID else {
|
|
return false
|
|
}
|
|
let identity = FeatureSubmissionIdentity(threadID: thread.wireID ?? thread.id)
|
|
let uploads = submission.attachments.map(\.upload)
|
|
let queued = FeatureQueuedSubmission(
|
|
environmentID: environmentID,
|
|
identity: identity,
|
|
threadID: submission.threadID,
|
|
text: trimmed,
|
|
selection: submission.selection,
|
|
runtimeMode: thread.runtimeMode,
|
|
interactionMode: thread.interactionMode,
|
|
attachments: uploads
|
|
)
|
|
guard await enqueue(queued) else { return false }
|
|
|
|
let optimistic = FeatureMessage(
|
|
id: identity.messageID,
|
|
role: .user,
|
|
text: trimmed,
|
|
createdAt: identity.createdAt,
|
|
state: .queued,
|
|
attachments: submission.attachments.map {
|
|
FeatureMessageAttachment(
|
|
id: $0.id.uuidString,
|
|
name: $0.filename,
|
|
mimeType: $0.mimeType,
|
|
sizeBytes: $0.byteCount
|
|
)
|
|
}
|
|
)
|
|
mutateDetail(
|
|
id: submission.threadID,
|
|
change: .delta(FeatureDetailDelta(
|
|
changedMessages: [optimistic],
|
|
appendedMessageIDs: [optimistic.id]
|
|
))
|
|
) {
|
|
$0.messages.append(optimistic)
|
|
}
|
|
|
|
isPerformingAction = true
|
|
defer { isPerformingAction = false }
|
|
do {
|
|
try await client.sendMessage(
|
|
threadID: submission.threadID,
|
|
text: trimmed,
|
|
selection: submission.selection,
|
|
attachments: uploads,
|
|
identity: identity
|
|
)
|
|
await completeQueuedSubmission(queued)
|
|
return true
|
|
} catch {
|
|
if Self.shouldQueue(error, environmentID: environmentID, snapshot: snapshot) {
|
|
if isEnvironmentConnected(environmentID) {
|
|
scheduleOutboxRetry()
|
|
}
|
|
return true
|
|
}
|
|
await discardQueuedSubmission(queued)
|
|
mutateDetail(id: submission.threadID) {
|
|
$0.messages.removeAll { $0.id == identity.messageID }
|
|
}
|
|
if !Self.isBenignCancellation(error) {
|
|
errorMessage = error.localizedDescription
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
public func cancelTurn(threadID: String) async {
|
|
await perform {
|
|
try await client.cancelTurn(threadID: threadID)
|
|
}
|
|
}
|
|
|
|
public func resolveApproval(_ id: String, decision: FeatureApprovalDecision) async {
|
|
let environment = currentEnvironmentIdentity
|
|
await perform {
|
|
try await client.resolveApproval(id: id, decision: decision)
|
|
guard currentEnvironmentIdentity == environment else { return }
|
|
for key in Array(details.keys) {
|
|
mutateDetail(
|
|
id: key,
|
|
change: .delta(FeatureDetailDelta(changedMessages: []))
|
|
) {
|
|
$0.approvals.removeAll { $0.id == id }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public func resolveUserInput(_ id: String, answers: [String: FeatureInputAnswer]) async {
|
|
let environment = currentEnvironmentIdentity
|
|
await perform {
|
|
try await client.resolveUserInput(id: id, answers: answers)
|
|
guard currentEnvironmentIdentity == environment else { return }
|
|
for key in Array(details.keys) {
|
|
mutateDetail(
|
|
id: key,
|
|
change: .delta(FeatureDetailDelta(changedMessages: []))
|
|
) {
|
|
$0.userInputs.removeAll { $0.id == id }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Convenience for callers that only submit free-form or single-select text.
|
|
public func resolveUserInput(_ id: String, answers: [String: String]) async {
|
|
await resolveUserInput(
|
|
id,
|
|
answers: answers.mapValues(FeatureInputAnswer.text)
|
|
)
|
|
}
|
|
|
|
public func saveSettings(_ settings: FeatureSettings) async {
|
|
await perform {
|
|
try await client.saveSettings(settings)
|
|
snapshot.settings = settings
|
|
}
|
|
}
|
|
|
|
@discardableResult
|
|
private func perform(
|
|
reportError: Bool = true,
|
|
_ operation: () async throws -> Void
|
|
) async -> Bool {
|
|
isPerformingAction = true
|
|
defer { isPerformingAction = false }
|
|
do {
|
|
try await operation()
|
|
return true
|
|
} catch {
|
|
if reportError, !Self.isBenignCancellation(error) {
|
|
errorMessage = error.localizedDescription
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
private static func isBenignCancellation(_ error: any Error) -> Bool {
|
|
if error is CancellationError { return true }
|
|
let message = error.localizedDescription
|
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
.lowercased()
|
|
return message == "cancelled" || message == "canceled"
|
|
}
|
|
|
|
private var currentEnvironmentIdentity: String {
|
|
let active = snapshot.environments.first(where: \.isActive)
|
|
return [
|
|
active?.id,
|
|
active?.endpoint,
|
|
snapshot.connection.endpoint,
|
|
]
|
|
.compactMap { $0 }
|
|
.joined(separator: "|")
|
|
}
|
|
|
|
private func apply(_ event: FeatureEvent) {
|
|
switch event {
|
|
case let .snapshot(value):
|
|
install(value)
|
|
case let .connection(value):
|
|
guard snapshot.connection != value else { return }
|
|
snapshot.connection = value
|
|
homePresentationRevision &+= 1
|
|
if value.state == .connected {
|
|
scheduleOutboxDrain()
|
|
}
|
|
case let .thread(value):
|
|
acknowledgeAuthoritativeThread(value.id)
|
|
upsert(value)
|
|
mutateDetail(
|
|
id: value.id,
|
|
change: .delta(FeatureDetailDelta(changedMessages: []))
|
|
) {
|
|
$0.thread = value
|
|
}
|
|
case let .threadRemoved(id):
|
|
removeThread(id: id)
|
|
removeDetail(id: id)
|
|
case let .detail(value):
|
|
store(value)
|
|
upsert(value.thread)
|
|
case let .detailDelta(value, delta):
|
|
store(value, delta: delta)
|
|
upsert(value.thread)
|
|
case let .failure(message):
|
|
errorMessage = message
|
|
}
|
|
}
|
|
|
|
private func upsert(_ thread: FeatureThread) {
|
|
if let index = snapshot.threads.firstIndex(where: { $0.id == thread.id }) {
|
|
let previous = snapshot.threads[index]
|
|
guard previous != thread else { return }
|
|
snapshot.threads[index] = thread
|
|
if previous.projectID != thread.projectID {
|
|
adjustProjectCount(id: previous.projectID, by: -1)
|
|
adjustProjectCount(id: thread.projectID, by: 1)
|
|
}
|
|
} else {
|
|
snapshot.threads.append(thread)
|
|
adjustProjectCount(id: thread.projectID, by: 1)
|
|
}
|
|
threadCollectionRevision &+= 1
|
|
homePresentationRevision &+= 1
|
|
}
|
|
|
|
private func removeThread(id: String) {
|
|
guard let index = snapshot.threads.firstIndex(where: { $0.id == id }) else { return }
|
|
let projectID = snapshot.threads[index].projectID
|
|
snapshot.threads.remove(at: index)
|
|
adjustProjectCount(id: projectID, by: -1)
|
|
threadCollectionRevision &+= 1
|
|
homePresentationRevision &+= 1
|
|
}
|
|
|
|
private func adjustProjectCount(id: String, by delta: Int) {
|
|
guard let index = snapshot.projects.firstIndex(where: { $0.id == id }) else { return }
|
|
snapshot.projects[index].threadCount = max(0, snapshot.projects[index].threadCount + delta)
|
|
}
|
|
|
|
private func install(_ value: FeatureSnapshot) {
|
|
var value = value
|
|
let authoritativeThreadIDs = Set(value.threads.map(\.id))
|
|
for id in authoritativeThreadIDs {
|
|
acknowledgeAuthoritativeThread(id)
|
|
}
|
|
for pending in pendingThreadsByID.values where !authoritativeThreadIDs.contains(pending.id) {
|
|
value.threads.append(pending)
|
|
if let index = value.projects.firstIndex(where: { $0.id == pending.projectID }) {
|
|
value.projects[index].threadCount += 1
|
|
}
|
|
}
|
|
|
|
if snapshot.connection != value.connection
|
|
|| snapshot.environments != value.environments
|
|
|| snapshot.projects != value.projects
|
|
|| snapshot.providers != value.providers
|
|
|| snapshot.threads != value.threads {
|
|
homePresentationRevision &+= 1
|
|
}
|
|
if snapshot.threads != value.threads {
|
|
threadCollectionRevision &+= 1
|
|
}
|
|
snapshot = value
|
|
if value.connection.state == .connected
|
|
|| value.environments.contains(where: { $0.connectionState == .connected }) {
|
|
scheduleOutboxDrain()
|
|
}
|
|
}
|
|
|
|
private func mutateThread(
|
|
id: String,
|
|
_ mutation: (inout FeatureThread) -> Void
|
|
) {
|
|
if let index = snapshot.threads.firstIndex(where: { $0.id == id }) {
|
|
let previous = snapshot.threads[index]
|
|
mutation(&snapshot.threads[index])
|
|
if snapshot.threads[index] != previous {
|
|
threadCollectionRevision &+= 1
|
|
homePresentationRevision &+= 1
|
|
}
|
|
}
|
|
mutateDetail(
|
|
id: id,
|
|
change: .delta(FeatureDetailDelta(changedMessages: []))
|
|
) {
|
|
mutation(&$0.thread)
|
|
}
|
|
}
|
|
|
|
private func store(_ incoming: FeatureThreadDetail) {
|
|
let id = incoming.thread.id
|
|
acknowledgeDeliveredMessages(incoming.messages)
|
|
let incoming = addingPendingMessages(to: incoming)
|
|
let next = details[id].map { current in
|
|
FeatureThreadDetail(
|
|
thread: incoming.thread,
|
|
messages: replacingChangedSuffix(current.messages, with: incoming.messages),
|
|
approvals: replacingChangedSuffix(current.approvals, with: incoming.approvals),
|
|
userInputs: replacingChangedSuffix(current.userInputs, with: incoming.userInputs)
|
|
)
|
|
} ?? incoming
|
|
guard details[id] != next else { return }
|
|
details[id] = next
|
|
bumpDetailRevision(id: id, change: .full)
|
|
}
|
|
|
|
private func store(_ incoming: FeatureThreadDetail, delta: FeatureDetailDelta) {
|
|
let id = incoming.thread.id
|
|
acknowledgeDeliveredMessages(incoming.messages)
|
|
let next = addingPendingMessages(to: incoming)
|
|
details[id] = next
|
|
let appended = next.messages.dropFirst(incoming.messages.count).map(\.id)
|
|
let pendingDelta = FeatureDetailDelta(
|
|
changedMessages: delta.changedMessages + next.messages.dropFirst(incoming.messages.count),
|
|
appendedMessageIDs: delta.appendedMessageIDs + appended
|
|
)
|
|
bumpDetailRevision(id: id, change: .delta(pendingDelta))
|
|
}
|
|
|
|
private func mutateDetail(
|
|
id: String,
|
|
change: FeatureDetailRenderChange = .full,
|
|
_ mutation: (inout FeatureThreadDetail) -> Void
|
|
) {
|
|
guard var detail = details[id] else { return }
|
|
let previous = detail
|
|
mutation(&detail)
|
|
guard detail != previous else { return }
|
|
details[id] = detail
|
|
bumpDetailRevision(id: id, change: change)
|
|
}
|
|
|
|
private func removeDetail(id: String) {
|
|
guard details.removeValue(forKey: id) != nil else { return }
|
|
bumpDetailRevision(id: id, change: .full)
|
|
}
|
|
|
|
private func clearDetails() {
|
|
guard !details.isEmpty else { return }
|
|
details.removeAll()
|
|
detailRevision &+= 1
|
|
detailRevisions.removeAll()
|
|
detailRenderUpdates.removeAll()
|
|
}
|
|
|
|
private func bumpDetailRevision(id: String, change: FeatureDetailRenderChange) {
|
|
let baseRevision = detailRevisions[id] ?? 0
|
|
detailRevision &+= 1
|
|
detailRevisions[id] = detailRevision
|
|
detailRenderUpdates[id] = FeatureDetailRenderUpdate(
|
|
baseRevision: baseRevision,
|
|
revision: detailRevision,
|
|
change: change
|
|
)
|
|
}
|
|
|
|
private func replacingChangedSuffix<Element: Equatable>(
|
|
_ current: [Element],
|
|
with incoming: [Element]
|
|
) -> [Element] {
|
|
guard current != incoming else { return current }
|
|
let prefixCount = zip(current, incoming).prefix { pair in
|
|
pair.0 == pair.1
|
|
}.count
|
|
var result = current
|
|
result.replaceSubrange(prefixCount..., with: incoming.dropFirst(prefixCount))
|
|
return result
|
|
}
|
|
|
|
private func restoreOutbox() async {
|
|
let submissions: [FeatureQueuedSubmission]
|
|
do {
|
|
submissions = try await outboxStore.submissions()
|
|
} catch {
|
|
errorMessage = "Could not restore queued messages: \(error.localizedDescription)"
|
|
return
|
|
}
|
|
|
|
for submission in submissions {
|
|
if let creation = submission.creation {
|
|
if snapshot.threads.contains(where: { $0.id == submission.threadID }) {
|
|
try? await outboxStore.remove(id: submission.id)
|
|
continue
|
|
}
|
|
guard let project = snapshot.projects.first(where: {
|
|
$0.id == creation.projectID && $0.environmentID == submission.environmentID
|
|
}) else {
|
|
if isEnvironmentConnected(submission.environmentID) {
|
|
try? await outboxStore.remove(id: submission.id)
|
|
} else {
|
|
pendingSubmissionsByID[submission.id] = submission
|
|
}
|
|
continue
|
|
}
|
|
pendingSubmissionsByID[submission.id] = submission
|
|
installPendingCreation(submission, project: project)
|
|
continue
|
|
}
|
|
|
|
guard snapshot.threads.contains(where: { $0.id == submission.threadID }) else {
|
|
if pendingThreadsByID[submission.threadID] != nil {
|
|
pendingSubmissionsByID[submission.id] = submission
|
|
} else if isEnvironmentConnected(submission.environmentID) {
|
|
try? await outboxStore.remove(id: submission.id)
|
|
} else {
|
|
pendingSubmissionsByID[submission.id] = submission
|
|
}
|
|
continue
|
|
}
|
|
pendingSubmissionsByID[submission.id] = submission
|
|
if let detail = details[submission.threadID] {
|
|
store(addingPendingMessages(to: detail))
|
|
}
|
|
}
|
|
}
|
|
|
|
private func enqueue(_ submission: FeatureQueuedSubmission) async -> Bool {
|
|
do {
|
|
try await outboxStore.enqueue(submission)
|
|
pendingSubmissionsByID[submission.id] = submission
|
|
return true
|
|
} catch {
|
|
errorMessage = "Could not safely queue this message: \(error.localizedDescription)"
|
|
return false
|
|
}
|
|
}
|
|
|
|
private func installPendingCreation(
|
|
_ submission: FeatureQueuedSubmission,
|
|
project: FeatureProject
|
|
) {
|
|
guard let creation = submission.creation else { return }
|
|
let provider = provider(
|
|
id: submission.selection?.providerID,
|
|
environmentID: submission.environmentID
|
|
)
|
|
let environmentName = snapshot.environments.first {
|
|
$0.id == submission.environmentID
|
|
}?.name
|
|
let title = submission.text
|
|
.split(whereSeparator: \.isNewline)
|
|
.first
|
|
.map(String.init)?
|
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
let thread = FeatureThread(
|
|
id: submission.threadID,
|
|
wireID: submission.identity.threadID,
|
|
projectID: project.id,
|
|
environmentID: submission.environmentID,
|
|
environmentName: environmentName,
|
|
title: title?.isEmpty == false ? title! : "New task",
|
|
preview: submission.text,
|
|
branch: creation.branch,
|
|
worktreePath: creation.worktreePath,
|
|
createdAt: submission.identity.createdAt,
|
|
updatedAt: submission.identity.createdAt,
|
|
state: .queued,
|
|
providerID: submission.selection?.providerID,
|
|
providerName: provider?.name,
|
|
modelID: submission.selection?.modelID,
|
|
runtimeMode: .fullAccess,
|
|
interactionMode: .standard
|
|
)
|
|
pendingThreadsByID[thread.id] = thread
|
|
upsert(thread)
|
|
store(FeatureThreadDetail(
|
|
thread: thread,
|
|
messages: [queuedMessage(for: submission)]
|
|
))
|
|
}
|
|
|
|
private func provider(id: String?, environmentID: String) -> FeatureProvider? {
|
|
guard let id else { return nil }
|
|
let providers = snapshot.providersByEnvironment?[environmentID] ?? snapshot.providers
|
|
return providers.first { $0.id == id }
|
|
}
|
|
|
|
private func queuedMessage(for submission: FeatureQueuedSubmission) -> FeatureMessage {
|
|
FeatureMessage(
|
|
id: submission.identity.messageID,
|
|
role: .user,
|
|
text: submission.text,
|
|
createdAt: submission.identity.createdAt,
|
|
state: .queued,
|
|
attachments: submission.attachments.enumerated().map { index, attachment in
|
|
FeatureMessageAttachment(
|
|
id: "\(submission.id)-attachment-\(index)",
|
|
name: attachment.name,
|
|
mimeType: attachment.mimeType,
|
|
sizeBytes: attachment.data.count
|
|
)
|
|
}
|
|
)
|
|
}
|
|
|
|
private func addingPendingMessages(to incoming: FeatureThreadDetail) -> FeatureThreadDetail {
|
|
let queued = pendingSubmissionsByID.values
|
|
.filter { $0.threadID == incoming.thread.id }
|
|
.sorted { $0.identity.createdAt < $1.identity.createdAt }
|
|
guard !queued.isEmpty else { return incoming }
|
|
var result = incoming
|
|
let existing = Set(result.messages.map(\.id))
|
|
result.messages.append(contentsOf: queued.lazy
|
|
.filter { !existing.contains($0.identity.messageID) }
|
|
.map(queuedMessage(for:)))
|
|
return result
|
|
}
|
|
|
|
private func acknowledgeAuthoritativeThread(_ id: String) {
|
|
guard pendingThreadsByID.removeValue(forKey: id) != nil,
|
|
let submission = pendingSubmissionsByID.values.first(where: {
|
|
$0.threadID == id && $0.creation != nil
|
|
}) else { return }
|
|
pendingSubmissionsByID.removeValue(forKey: submission.id)
|
|
markQueuedMessageDelivered(submission)
|
|
let store = outboxStore
|
|
Task { try? await store.remove(id: submission.id) }
|
|
}
|
|
|
|
private func acknowledgeDeliveredMessages(_ messages: [FeatureMessage]) {
|
|
let messageIDs = Set(messages.map(\.id))
|
|
let delivered = pendingSubmissionsByID.values.filter {
|
|
$0.creation == nil && messageIDs.contains($0.identity.messageID)
|
|
}
|
|
for submission in delivered {
|
|
pendingSubmissionsByID.removeValue(forKey: submission.id)
|
|
let store = outboxStore
|
|
Task { try? await store.remove(id: submission.id) }
|
|
}
|
|
}
|
|
|
|
private func completeQueuedSubmission(_ submission: FeatureQueuedSubmission) async {
|
|
try? await outboxStore.remove(id: submission.id)
|
|
pendingSubmissionsByID.removeValue(forKey: submission.id)
|
|
pendingThreadsByID.removeValue(forKey: submission.threadID)
|
|
markQueuedMessageDelivered(submission)
|
|
outboxRetryAttempt = 0
|
|
}
|
|
|
|
private func markQueuedMessageDelivered(_ submission: FeatureQueuedSubmission) {
|
|
mutateDetail(
|
|
id: submission.threadID,
|
|
change: .delta(FeatureDetailDelta(changedMessages: []))
|
|
) { detail in
|
|
guard let index = detail.messages.firstIndex(where: {
|
|
$0.id == submission.identity.messageID
|
|
}) else { return }
|
|
detail.messages[index].state = .complete
|
|
}
|
|
}
|
|
|
|
private func discardQueuedSubmission(_ submission: FeatureQueuedSubmission) async {
|
|
try? await outboxStore.remove(id: submission.id)
|
|
pendingSubmissionsByID.removeValue(forKey: submission.id)
|
|
let wasPendingCreation = pendingThreadsByID.removeValue(forKey: submission.threadID) != nil
|
|
if wasPendingCreation {
|
|
removeThread(id: submission.threadID)
|
|
removeDetail(id: submission.threadID)
|
|
} else {
|
|
mutateDetail(id: submission.threadID) {
|
|
$0.messages.removeAll { $0.id == submission.identity.messageID }
|
|
}
|
|
}
|
|
}
|
|
|
|
private func removePendingSubmissions(environmentID: String) {
|
|
let removed = pendingSubmissionsByID.values.filter {
|
|
$0.environmentID == environmentID
|
|
}
|
|
for submission in removed {
|
|
pendingSubmissionsByID.removeValue(forKey: submission.id)
|
|
if pendingThreadsByID.removeValue(forKey: submission.threadID) != nil {
|
|
removeThread(id: submission.threadID)
|
|
removeDetail(id: submission.threadID)
|
|
} else {
|
|
mutateDetail(id: submission.threadID) {
|
|
$0.messages.removeAll { $0.id == submission.identity.messageID }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func scheduleOutboxDrain(after delay: Duration = .zero) {
|
|
guard outboxDrainTask == nil, !pendingSubmissionsByID.isEmpty else { return }
|
|
let generation = outboxGeneration
|
|
outboxDrainTask = Task { @MainActor [weak self] in
|
|
if delay > .zero {
|
|
try? await Task.sleep(for: delay)
|
|
}
|
|
guard !Task.isCancelled,
|
|
let self,
|
|
self.outboxGeneration == generation else { return }
|
|
let needsRetry = await self.drainOutbox(generation: generation)
|
|
self.outboxDrainTask = nil
|
|
if needsRetry,
|
|
!Task.isCancelled,
|
|
self.outboxGeneration == generation {
|
|
self.scheduleOutboxRetry()
|
|
}
|
|
}
|
|
}
|
|
|
|
private func stopOutboxDrain() async {
|
|
outboxGeneration &+= 1
|
|
guard let task = outboxDrainTask else { return }
|
|
task.cancel()
|
|
await task.value
|
|
outboxDrainTask = nil
|
|
}
|
|
|
|
private func scheduleOutboxRetry() {
|
|
guard outboxDrainTask == nil else { return }
|
|
let seconds = min(16, 1 << min(outboxRetryAttempt, 4))
|
|
outboxRetryAttempt += 1
|
|
scheduleOutboxDrain(after: .seconds(seconds))
|
|
}
|
|
|
|
private func drainOutbox(generation: UInt64) async -> Bool {
|
|
let submissions = pendingSubmissionsByID.values.sorted {
|
|
$0.identity.createdAt < $1.identity.createdAt
|
|
}
|
|
var needsRetry = false
|
|
for submission in submissions where pendingSubmissionsByID[submission.id] != nil {
|
|
guard !Task.isCancelled, outboxGeneration == generation else { return false }
|
|
var policySnapshot = snapshot
|
|
if pendingThreadsByID[submission.threadID] != nil {
|
|
policySnapshot.threads.removeAll { $0.id == submission.threadID }
|
|
}
|
|
switch FeatureOutboxPolicy.decision(
|
|
for: submission,
|
|
snapshot: policySnapshot,
|
|
pendingCreationThreadIDs: Set(pendingThreadsByID.keys)
|
|
) {
|
|
case .discard:
|
|
await discardQueuedSubmission(submission)
|
|
case .wait:
|
|
// Connectivity and snapshot events wake the drain immediately.
|
|
// Avoid a permanent timer while the owning device is offline.
|
|
continue
|
|
case .send:
|
|
do {
|
|
guard pendingSubmissionsByID[submission.id] != nil,
|
|
snapshot.environments.contains(where: {
|
|
$0.id == submission.environmentID
|
|
}) else {
|
|
continue
|
|
}
|
|
if let creation = submission.creation {
|
|
let thread = try await client.createThreadAndSend(
|
|
projectID: creation.projectID,
|
|
prompt: submission.text,
|
|
selection: submission.selection,
|
|
runtimeMode: .fullAccess,
|
|
interactionMode: .standard,
|
|
workspaceMode: creation.workspaceMode,
|
|
branch: creation.branch,
|
|
worktreePath: creation.worktreePath,
|
|
startFromOrigin: creation.startFromOrigin,
|
|
attachments: submission.uploads,
|
|
identity: submission.identity
|
|
)
|
|
guard !Task.isCancelled,
|
|
outboxGeneration == generation else { return false }
|
|
await completeQueuedSubmission(submission)
|
|
if thread.id != submission.threadID {
|
|
removeThread(id: submission.threadID)
|
|
removeDetail(id: submission.threadID)
|
|
}
|
|
upsert(thread)
|
|
} else {
|
|
try await client.sendMessage(
|
|
threadID: submission.threadID,
|
|
text: submission.text,
|
|
selection: submission.selection,
|
|
attachments: submission.uploads,
|
|
identity: submission.identity
|
|
)
|
|
guard !Task.isCancelled,
|
|
outboxGeneration == generation else { return false }
|
|
await completeQueuedSubmission(submission)
|
|
}
|
|
} catch {
|
|
if Self.shouldQueue(
|
|
error,
|
|
environmentID: submission.environmentID,
|
|
snapshot: snapshot
|
|
) {
|
|
needsRetry = true
|
|
} else {
|
|
await discardQueuedSubmission(submission)
|
|
errorMessage = error.localizedDescription
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return needsRetry
|
|
}
|
|
|
|
private func isEnvironmentConnected(_ environmentID: String) -> Bool {
|
|
guard let environment = snapshot.environments.first(where: { $0.id == environmentID }) else {
|
|
return false
|
|
}
|
|
return environment.connectionState == .connected
|
|
|| (environment.isActive && snapshot.connection.state == .connected)
|
|
}
|
|
|
|
private static func shouldQueue(
|
|
_ error: any Error,
|
|
environmentID: String,
|
|
snapshot: FeatureSnapshot
|
|
) -> Bool {
|
|
if error is CancellationError || error is URLError { return true }
|
|
if let environment = snapshot.environments.first(where: { $0.id == environmentID }) {
|
|
let disconnected = environment.connectionState != .connected
|
|
&& !(environment.isActive && snapshot.connection.state == .connected)
|
|
if disconnected { return true }
|
|
}
|
|
let message = error.localizedDescription.lowercased()
|
|
return [
|
|
"cancelled", "canceled", "connection", "network", "offline",
|
|
"socket", "timed out", "timeout", "transport", "not connected",
|
|
"request deadline",
|
|
].contains { message.contains($0) }
|
|
}
|
|
}
|
|
|
|
private extension FeatureDraftAttachment {
|
|
var upload: FeatureUploadAttachment {
|
|
FeatureUploadAttachment(data: data, name: filename, mimeType: mimeType)
|
|
}
|
|
}
|