Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions MobileChat/CompanionWire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ nonisolated struct MobileChatSnapshot: Codable, Sendable {
nonisolated enum GlobalChatProvider: String, CaseIterable, Codable, Sendable {
case codex, claude

func canSelect(_ provider: Self, ownerChoice: Bool?) -> Bool {
provider != self || ownerChoice != true
}

var title: String {
switch self {
case .codex: "Codex"
Expand Down Expand Up @@ -326,6 +330,7 @@ nonisolated struct GlobalChatSnapshot: Codable, Sendable {
/// Events above `seenThrough` that are already seen. Nil from a host that only keeps the cursor.
var seenIDs: [Int64]? = nil
let provider: GlobalChatProvider
var providerChoice: Bool? = nil
let providerRevision: UUID
let persona: GlobalChatPersona?
var identity: GlobalChatIdentity? = nil
Expand Down
38 changes: 20 additions & 18 deletions MobileChat/GlobalChatScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,7 @@ struct GlobalChatScreen: View {
}
Spacer()
Menu {
ForEach(GlobalChatProvider.allCases, id: \.self) { provider in
Button {
beginHandoff(to: provider, snapshot: snapshot)
} label: {
if snapshot.provider == provider { Label(provider.title, systemImage: "checkmark") }
else { Text(verbatim: provider.title) }
}
.disabled(snapshot.provider == provider)
}
providerOptions(snapshot)
} label: {
Label(snapshot.provider.title, systemImage: "cpu")
}
Expand All @@ -331,6 +323,18 @@ struct GlobalChatScreen: View {
.padding(.vertical, 10)
}

private func providerOptions(_ snapshot: GlobalChatSnapshot) -> some View {
ForEach(GlobalChatProvider.allCases, id: \.self) { provider in
Button {
beginHandoff(to: provider, snapshot: snapshot)
} label: {
if snapshot.provider == provider { Label(provider.title, systemImage: "checkmark") }
else { Text(verbatim: provider.title) }
}
.disabled(!snapshot.provider.canSelect(provider, ownerChoice: snapshot.providerChoice))
}
}

/// Hides the conversation shown here; Ultra keeps its context and every stored message.
private func clearButton(_ snapshot: GlobalChatSnapshot) -> some View {
Button { confirmingClear = true } label: {
Expand All @@ -353,15 +357,7 @@ struct GlobalChatScreen: View {
private func titleMenu(_ snapshot: GlobalChatSnapshot) -> some View {
let identity = identity(snapshot)
return Menu {
Picker(
String(localized: "global.provider", defaultValue: "Chat Provider"),
selection: Binding(
get: { snapshot.provider },
set: { if $0 != snapshot.provider { beginHandoff(to: $0, snapshot: snapshot) } }
)
) {
ForEach(GlobalChatProvider.allCases, id: \.self) { Text(verbatim: $0.title).tag($0) }
}
providerOptions(snapshot)
} label: {
HStack(spacing: 8) {
GlobalChatAvatar(emoji: identity.emoji, size: 28, isActive: snapshot.busy)
Expand Down Expand Up @@ -1537,6 +1533,12 @@ struct GlobalChatScreen: View {
}

private func beginHandoff(to provider: GlobalChatProvider, snapshot: GlobalChatSnapshot) {
if provider == snapshot.provider {
Task {
await mutate(.switchProvider(id: UUID(), provider: provider, handoff: "", providerRevision: snapshot.providerRevision))
}
return
}
issue = nil
let recent = [
(GlobalChatMessage.Role.user, String(localized: "global.lastRequest", defaultValue: "Latest request")),
Expand Down
66 changes: 46 additions & 20 deletions Sources/GlobalChatCards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ final class GlobalChatCards {

enum Choice {
/// The option was typed into the agent's terminal.
case sent(text: String, workspaceID: UUID, surfaceID: UUID)
case sent
/// The card settled without typing, with the reason on the card.
case settled
case stale
case storageFailed
}

enum AskResult {
Expand Down Expand Up @@ -335,7 +336,10 @@ final class GlobalChatCards {
/// at that commit with every check passed, the terminal still runs the same agent,
/// `isAuthorized` still holds for whoever pressed it, and `allowsTyping` holds right before
/// the keystrokes, with nothing awaited in between.
func ship(_ id: UUID, isAuthorized: GlobalChatAuthorization, allowsTyping: @MainActor () -> Bool) async -> Choice {
func ship(
_ id: UUID, isAuthorized: GlobalChatAuthorization, allowsTyping: @MainActor () -> Bool,
record: (GlobalChatAction) -> Bool, unrecord: (GlobalChatAction) -> Void
) async -> Choice {
guard !shipsInFlight.contains(id), let entry = entries.first(where: { $0.card.id == id }), entry.card.state.isPending,
case let .ship(ship) = entry.card.content else { return .stale }
guard let binding = self.binding(for: entry, ship: ship), let repoSlug = entry.repoSlug else {
Expand Down Expand Up @@ -371,10 +375,10 @@ final class GlobalChatCards {
}
let reply = String(localized: "global.ship.reply", defaultValue: "Pull request #\(number) passed its checks. Merge it now with: gh pr merge \(number) --repo \(repoSlug) --match-head-commit \(headSHA)")
guard allowsTyping() else { return .stale }
guard let prepared = workspaces.prepare(workspaceID: ship.workspaceID, surfaceID: ship.surfaceID, text: reply),
workspaces.deliver(prepared) else { return .stale }
settle(id, as: .answered(choice: Self.shipChoice))
return .sent(text: reply, workspaceID: ship.workspaceID, surfaceID: ship.surfaceID)
guard let prepared = workspaces.prepare(workspaceID: ship.workspaceID, surfaceID: ship.surfaceID, text: reply) else { return .stale }
let result = Self.submit(prepared, record: record, deliver: workspaces.deliver, unrecord: unrecord)
if case .sent = result { settle(id, as: .answered(choice: Self.shipChoice)) }
return result
}

private static func isSameAgent(_ lhs: AgentSessionKey, _ rhs: AgentSessionKey) -> Bool {
Expand Down Expand Up @@ -464,23 +468,45 @@ final class GlobalChatCards {

/// The owner picked an option. It is typed only into the same terminal, to the same agent
/// session, while that agent waits at its prompt; otherwise the card says why it was not sent.
func choose(_ id: UUID, option: Int) -> Choice {
func choose(
_ id: UUID, option: Int, record: (GlobalChatAction) -> Bool, unrecord: (GlobalChatAction) -> Void
) -> Choice {
guard let entry = entries.first(where: { $0.card.id == id }), entry.card.state.isPending,
case let .ask(ask) = entry.card.content, ask.options.indices.contains(option) else { return .stale }
let choice = ask.options[option]
let failure: GlobalChatCardFailure? = {
guard let target = workspaces.askTarget(workspaceID: ask.workspaceID, surfaceID: ask.surfaceID) else {
return .terminalClosed
}
guard let current = target.session, let asked = entry.boundSession, Self.isSameAgent(current, asked) else {
return .agentChanged
}
guard let prepared = workspaces.prepare(workspaceID: ask.workspaceID, surfaceID: ask.surfaceID, text: choice),
workspaces.deliver(prepared) else { return .notWaiting }
return nil
}()
settle(id, as: failure.map { .failed($0) } ?? .answered(choice: choice))
return failure == nil ? .sent(text: choice, workspaceID: ask.workspaceID, surfaceID: ask.surfaceID) : .settled
func reject(_ failure: GlobalChatCardFailure) -> Choice {
settle(id, as: .failed(failure))
return .settled
}
guard let target = workspaces.askTarget(workspaceID: ask.workspaceID, surfaceID: ask.surfaceID) else {
return reject(.terminalClosed)
}
guard let current = target.session, let asked = entry.boundSession, Self.isSameAgent(current, asked) else {
return reject(.agentChanged)
}
guard let prepared = workspaces.prepare(workspaceID: ask.workspaceID, surfaceID: ask.surfaceID, text: choice) else {
return reject(.notWaiting)
}
switch Self.submit(prepared, record: record, deliver: workspaces.deliver, unrecord: unrecord) {
case .stale: return reject(.notWaiting)
case .sent:
settle(id, as: .answered(choice: choice))
return .sent
case .storageFailed: return .storageFailed
case .settled: return .settled
}
}

nonisolated static func submit(
_ action: GlobalChatAction, record: (GlobalChatAction) -> Bool, deliver: (GlobalChatAction) -> Bool,
unrecord: (GlobalChatAction) -> Void
) -> Choice {
guard record(action) else { return .storageFailed }
guard deliver(action) else {
unrecord(action)
return .stale
}
return .sent
}

/// Drops the oldest settled cards past the count and size limits. Open cards are never dropped.
Expand Down
Loading
Loading