mirror of
https://github.com/Louis-CFM/coucou.git
synced 2026-12-07 07:32:35 +09:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B9uCpBfczfSCvLARne8hCd
256 lines
12 KiB
Swift
256 lines
12 KiB
Swift
import SwiftUI
|
|
|
|
/// Start a task from the iPhone: pick the agent (only those installed on the Mac), a folder
|
|
/// the Mac worked in lately, write or dictate what to do; Face ID sends it, the Mac runs it in
|
|
/// the background and tells how it ended. GitHub build of Coucou only.
|
|
struct NewTaskView: View {
|
|
let link: PhoneLink
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
@AppStorage("newTaskAgent") private var agentID = PhoneAgent.claude.rawValue
|
|
@AppStorage("newTaskProject") private var projectPath = ""
|
|
@State private var text = ""
|
|
@State private var sending = false
|
|
@State private var sentTaskId: String?
|
|
@State private var sentAgent: PhoneAgent?
|
|
@State private var error: String?
|
|
@State private var dictation = Dictation()
|
|
@FocusState private var focused: Bool
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Group {
|
|
if let mac = link.macAgents, !mac.agents.isEmpty {
|
|
form(mac)
|
|
} else {
|
|
ContentUnavailableView {
|
|
Label("New tasks need your Mac", systemImage: "laptopcomputer")
|
|
} description: {
|
|
Text("Use Coucou from GitHub on your Mac and turn on “Let my iPhone send instructions” in its Settings. The App Store version can't start agents.")
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("New task")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Close") { dictation.stop(); dismiss() }
|
|
}
|
|
if let mac = link.macAgents, !mac.agents.isEmpty, sentTaskId == nil {
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button("Start") { Task { await send(mac) } }
|
|
.disabled(!canSend(mac) || sending)
|
|
}
|
|
}
|
|
}
|
|
// A drag or a tap outside the field puts the keyboard away, like Notes.
|
|
.scrollDismissesKeyboard(.immediately)
|
|
.onChange(of: dictation.transcript) { _, words in
|
|
if dictation.isRecording { text = dictation.prefix + words }
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Form
|
|
|
|
private func agent(_ mac: MacAgents) -> PhoneAgent {
|
|
let chosen = PhoneAgent(rawValue: agentID) ?? .claude
|
|
return mac.agents.contains(chosen) ? chosen : mac.agents[0]
|
|
}
|
|
|
|
private func project(_ mac: MacAgents) -> MacAgents.Project? {
|
|
mac.projects.first { $0.path == projectPath } ?? mac.projects.first
|
|
}
|
|
|
|
private func canSend(_ mac: MacAgents) -> Bool {
|
|
project(mac) != nil && !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
|
}
|
|
|
|
@ViewBuilder private func form(_ mac: MacAgents) -> some View {
|
|
let chosen = agent(mac)
|
|
Form {
|
|
if let sentTaskId, let sentAgent {
|
|
progress(taskId: sentTaskId, agent: sentAgent)
|
|
} else {
|
|
Section {
|
|
Picker("Agent", selection: Binding(get: { chosen }, set: { agentID = $0.rawValue })) {
|
|
ForEach(mac.agents, id: \.self) { Text($0.name).tag($0) }
|
|
}
|
|
if mac.projects.isEmpty {
|
|
Text("No recent folder yet. Start can't send a task without one: work in a project with Claude Code or Codex on your Mac, it shows up here within a minute.")
|
|
.font(.footnote).foregroundStyle(.secondary)
|
|
} else {
|
|
Picker("Project", selection: Binding(get: { project(mac)?.path ?? "" },
|
|
set: { projectPath = $0 })) {
|
|
ForEach(mac.projects, id: \.path) { Text($0.name).tag($0.path) }
|
|
}
|
|
}
|
|
} footer: {
|
|
Text(chosen.asksOnPhone
|
|
? "Claude Code asks you here before each command, as usual."
|
|
: "\(chosen.name) can't ask you from here: it follows its own settings on your Mac. Without changes on your side, it doesn't edit files or run commands that need your OK.")
|
|
}
|
|
|
|
Section("What to do") {
|
|
HStack(alignment: .top) {
|
|
TextField("Fix the failing test, then explain why…", text: $text, axis: .vertical)
|
|
.lineLimit(4...12)
|
|
.focused($focused)
|
|
VStack(spacing: 14) {
|
|
Button {
|
|
Task { await dictation.toggle(startingFrom: text) }
|
|
} label: {
|
|
Image(systemName: dictation.isRecording ? "waveform" : "mic.fill")
|
|
.foregroundStyle(dictation.isRecording ? Color.red : Color.secondary)
|
|
.symbolEffect(.variableColor.iterative, isActive: dictation.isRecording)
|
|
}
|
|
// Puts the keyboard away, in the field's own style.
|
|
if focused {
|
|
Button {
|
|
focused = false
|
|
} label: {
|
|
Image(systemName: "keyboard.chevron.compact.down")
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.transition(.opacity)
|
|
.accessibilityLabel("Hide keyboard")
|
|
}
|
|
}
|
|
.buttonStyle(.borderless)
|
|
.animation(.easeOut(duration: 0.2), value: focused)
|
|
}
|
|
}
|
|
|
|
if let error {
|
|
Section { Text(error).foregroundStyle(.red) }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// After sending: waiting for the Mac, then how it ended.
|
|
@ViewBuilder private func progress(taskId: String, agent: PhoneAgent) -> some View {
|
|
if let result = link.taskResults[taskId] {
|
|
Section {
|
|
Label(result.ok ? "\(agent.name) finished" : "\(agent.name) stopped",
|
|
systemImage: result.ok ? "checkmark.circle.fill" : "exclamationmark.triangle.fill")
|
|
.foregroundStyle(result.ok ? .green : .orange)
|
|
LabeledContent("Project", value: result.project)
|
|
LabeledContent("Ended", value: result.endedAt.formatted(date: .omitted, time: .shortened))
|
|
}
|
|
Section("Last words") {
|
|
Text(result.summary)
|
|
.font(.footnote.monospaced())
|
|
.textSelection(.enabled)
|
|
}
|
|
if let needs = result.needs, !needs.isEmpty {
|
|
Section {
|
|
ForEach(needs, id: \.self) { need in
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text(need.tool)
|
|
.font(.caption.weight(.semibold))
|
|
.foregroundStyle(.secondary)
|
|
Text(need.detail)
|
|
.font(.footnote.monospaced())
|
|
.foregroundStyle(result.ok ? .secondary : .primary)
|
|
.lineLimit(result.ok ? 3 : nil)
|
|
.textSelection(.enabled)
|
|
}
|
|
}
|
|
Button {
|
|
Task { await allowAndContinue(result) }
|
|
} label: {
|
|
if result.ok {
|
|
Label("Allow and continue anyway", systemImage: "faceid")
|
|
.font(.subheadline)
|
|
} else {
|
|
Label(needs.count == 1 ? "Allow this and continue" : "Allow these \(needs.count) and continue",
|
|
systemImage: "faceid")
|
|
}
|
|
}
|
|
.tint(result.ok ? Color.secondary : Color.accentColor)
|
|
.disabled(sending)
|
|
} header: {
|
|
// A task that finished found its way without them: only a note then.
|
|
Text(result.ok ? "Skipped" : "Needs your OK")
|
|
} footer: {
|
|
Text(result.ok
|
|
? "\(agent.name) couldn't ask you for these while it ran and finished without them. Continue only if the answer is missing something."
|
|
: "\(agent.name) couldn't ask you while it ran, so it skipped these. Allowing them lets this task, and only this one, do exactly what's shown.")
|
|
}
|
|
}
|
|
if let error {
|
|
Section { Text(error).foregroundStyle(.red) }
|
|
}
|
|
Section {
|
|
Button("New task") {
|
|
sentTaskId = nil
|
|
self.sentAgent = nil
|
|
}
|
|
}
|
|
} else {
|
|
Section {
|
|
HStack(spacing: 12) {
|
|
ProgressView()
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
Text("Sent to your Mac")
|
|
Text("It starts within 15 s. You can close this screen, the result lands in History.")
|
|
.font(.footnote).foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Sending
|
|
|
|
/// Same conversation, with exactly the actions shown above allowed (the Mac checks them).
|
|
private func allowAndContinue(_ result: TaskResult) async {
|
|
guard let mac = link.macAgents,
|
|
let project = mac.projects.first(where: { $0.name == result.project }) ?? project(mac) else { return }
|
|
sending = true
|
|
defer { sending = false }
|
|
error = nil
|
|
guard await OwnerCheck.confirm(reason: "Allow \(result.agent.name) to do this in \(result.project)") else {
|
|
Haptics.warning()
|
|
return
|
|
}
|
|
if let id = await link.sendTask("You couldn't do some actions earlier because nobody could approve them. They are now allowed: do them and finish the task.",
|
|
agent: result.agent, project: project, continuing: result.id) {
|
|
sentAgent = result.agent
|
|
sentTaskId = id
|
|
Haptics.success()
|
|
} else {
|
|
error = link.lastPong ?? "Couldn't reach iCloud."
|
|
Haptics.error()
|
|
}
|
|
}
|
|
|
|
private func send(_ mac: MacAgents) async {
|
|
let task = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !task.isEmpty, let project = project(mac) else { return }
|
|
let chosen = agent(mac)
|
|
dictation.stop()
|
|
focused = false
|
|
sending = true
|
|
defer { sending = false }
|
|
error = nil
|
|
guard await OwnerCheck.confirm(reason: "Start \(chosen.name) in \(project.name) on your Mac") else {
|
|
error = "Face ID didn't confirm. Nothing was sent."
|
|
Haptics.warning()
|
|
return
|
|
}
|
|
if let id = await link.sendTask(task, agent: chosen, project: project) {
|
|
agentID = chosen.rawValue
|
|
projectPath = project.path
|
|
text = ""
|
|
sentAgent = chosen
|
|
sentTaskId = id
|
|
Haptics.success()
|
|
} else {
|
|
error = link.lastPong ?? "Couldn't reach iCloud."
|
|
Haptics.error()
|
|
}
|
|
}
|
|
}
|