feat(ios): support multi-select agent input
This commit is contained in:
8 files changed
+234
-22
No files matched your search
@@ -1,5 +1,16 @@
|
||||
import Foundation
|
||||
|
||||
extension FeatureInputAnswer {
|
||||
var jsonValue: JSONValue {
|
||||
switch self {
|
||||
case let .text(value):
|
||||
.string(value)
|
||||
case let .selections(values):
|
||||
.array(values.map(JSONValue.string))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Composes the transport-focused Core layer with the UI-focused Features layer.
|
||||
@MainActor
|
||||
final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging {
|
||||
@@ -654,7 +665,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging {
|
||||
try await refreshThread(id: threadID, client: client)
|
||||
}
|
||||
|
||||
func resolveUserInput(id: String, answers: [String: String]) async throws {
|
||||
func resolveUserInput(id: String, answers: [String: FeatureInputAnswer]) async throws {
|
||||
guard let threadID = inputThreadIDs[id] else {
|
||||
throw NativeFeatureClientError.inputRequestNotFound
|
||||
}
|
||||
@@ -662,7 +673,7 @@ final class NativeFeatureClient: FeatureClient, FeatureDeviceManaging {
|
||||
_ = try await client.respondToUserInput(
|
||||
threadID: threadID,
|
||||
requestID: id,
|
||||
answers: answers.mapValues(JSONValue.string)
|
||||
answers: answers.mapValues(\.jsonValue)
|
||||
)
|
||||
try await refreshThread(id: threadID, client: client)
|
||||
}
|
||||
|
||||
@@ -133,9 +133,9 @@ struct FeatureComposerApprovalPanel: View {
|
||||
struct FeatureComposerUserInputPanel: View {
|
||||
let input: FeatureUserInput
|
||||
let isResponding: Bool
|
||||
let onSubmit: ([String: String]) -> Void
|
||||
let onSubmit: ([String: FeatureInputAnswer]) -> Void
|
||||
|
||||
@State private var answers: [String: String] = [:]
|
||||
@State private var answers: [String: FeatureInputAnswer] = [:]
|
||||
@State private var questionIndex = 0
|
||||
|
||||
var body: some View {
|
||||
@@ -164,6 +164,13 @@ struct FeatureComposerUserInputPanel: View {
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.padding(.top, 5)
|
||||
|
||||
if question.allowsMultiple {
|
||||
Text("Select one or more options")
|
||||
.font(.caption)
|
||||
.foregroundStyle(T3Colors.textTertiary)
|
||||
.padding(.top, 4)
|
||||
}
|
||||
|
||||
}
|
||||
.padding(.horizontal, 15)
|
||||
.padding(.vertical, 12)
|
||||
@@ -258,8 +265,13 @@ struct FeatureComposerUserInputPanel: View {
|
||||
return normalizedAnswer(for: activeQuestion.id) != nil
|
||||
}
|
||||
|
||||
private var allQuestionsAnswered: Bool {
|
||||
input.questions.allSatisfy { normalizedAnswer(for: $0.id) != nil }
|
||||
private var normalizedAnswers: [String: FeatureInputAnswer]? {
|
||||
var result: [String: FeatureInputAnswer] = [:]
|
||||
for question in input.questions {
|
||||
guard let answer = normalizedAnswer(for: question.id) else { return nil }
|
||||
result[question.id] = answer
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private func optionButton(
|
||||
@@ -267,7 +279,7 @@ struct FeatureComposerUserInputPanel: View {
|
||||
number: Int,
|
||||
question: FeatureInputQuestion
|
||||
) -> some View {
|
||||
let isSelected = answers[question.id] == option.label
|
||||
let isSelected = isOptionSelected(option.label, for: question)
|
||||
|
||||
return Button {
|
||||
select(option.label, for: question)
|
||||
@@ -323,13 +335,20 @@ struct FeatureComposerUserInputPanel: View {
|
||||
|
||||
private func answerBinding(for questionID: String) -> Binding<String> {
|
||||
Binding(
|
||||
get: { answers[questionID] ?? "" },
|
||||
set: { answers[questionID] = $0 }
|
||||
get: {
|
||||
guard case let .text(value)? = answers[questionID] else { return "" }
|
||||
return value
|
||||
},
|
||||
set: { answers[questionID] = .text($0) }
|
||||
)
|
||||
}
|
||||
|
||||
private func select(_ label: String, for question: FeatureInputQuestion) {
|
||||
answers[question.id] = label
|
||||
answers[question.id] = (answers[question.id] ?? .selections([]))
|
||||
.togglingOption(label, allowsMultiple: question.allowsMultiple)
|
||||
if question.allowsMultiple {
|
||||
return
|
||||
}
|
||||
guard !isLastQuestion else { return }
|
||||
Task { @MainActor in
|
||||
await Task.yield()
|
||||
@@ -341,8 +360,8 @@ struct FeatureComposerUserInputPanel: View {
|
||||
guard canAdvance else { return }
|
||||
if !isLastQuestion {
|
||||
questionIndex += 1
|
||||
} else if allQuestionsAnswered {
|
||||
onSubmit(answers)
|
||||
} else if let normalizedAnswers {
|
||||
onSubmit(normalizedAnswers)
|
||||
} else if let unanswered = input.questions.firstIndex(where: {
|
||||
normalizedAnswer(for: $0.id) == nil
|
||||
}) {
|
||||
@@ -350,11 +369,18 @@ struct FeatureComposerUserInputPanel: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func normalizedAnswer(for questionID: String) -> String? {
|
||||
guard let value = answers[questionID]?.trimmingCharacters(in: .whitespacesAndNewlines),
|
||||
!value.isEmpty else {
|
||||
return nil
|
||||
private func normalizedAnswer(for questionID: String) -> FeatureInputAnswer? {
|
||||
answers[questionID]?.normalized
|
||||
}
|
||||
|
||||
private func isOptionSelected(_ label: String, for question: FeatureInputQuestion) -> Bool {
|
||||
switch answers[question.id] {
|
||||
case let .text(value):
|
||||
return !question.allowsMultiple && value == label
|
||||
case let .selections(values):
|
||||
return values.contains(label)
|
||||
case nil:
|
||||
return false
|
||||
}
|
||||
return value
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ struct FeatureComposerView: View {
|
||||
private let onRuntimeModeChange: ((FeatureRuntimeMode) -> Void)?
|
||||
private let onInteractionModeChange: ((FeatureInteractionMode) -> Void)?
|
||||
private let onApprovalDecision: ((String, FeatureApprovalDecision) -> Void)?
|
||||
private let onUserInputSubmit: ((String, [String: String]) -> Void)?
|
||||
private let onUserInputSubmit: ((String, [String: FeatureInputAnswer]) -> Void)?
|
||||
|
||||
init(
|
||||
text: Binding<String>,
|
||||
@@ -47,7 +47,7 @@ struct FeatureComposerView: View {
|
||||
pendingUserInputs: [FeatureUserInput] = [],
|
||||
isResolvingRequest: Bool = false,
|
||||
onApprovalDecision: ((String, FeatureApprovalDecision) -> Void)? = nil,
|
||||
onUserInputSubmit: ((String, [String: String]) -> Void)? = nil
|
||||
onUserInputSubmit: ((String, [String: FeatureInputAnswer]) -> Void)? = nil
|
||||
) {
|
||||
_text = text
|
||||
_selection = selection
|
||||
|
||||
@@ -295,7 +295,7 @@ public final class FeatureRootModel {
|
||||
}
|
||||
}
|
||||
|
||||
public func resolveUserInput(_ id: String, answers: [String: String]) async {
|
||||
public func resolveUserInput(_ id: String, answers: [String: FeatureInputAnswer]) async {
|
||||
let environment = currentEnvironmentIdentity
|
||||
await perform {
|
||||
try await client.resolveUserInput(id: id, answers: answers)
|
||||
@@ -306,6 +306,14 @@ public final class FeatureRootModel {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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)
|
||||
|
||||
@@ -40,7 +40,7 @@ public protocol FeatureClient: AnyObject {
|
||||
) async throws
|
||||
func cancelTurn(threadID: String) async throws
|
||||
func resolveApproval(id: String, decision: FeatureApprovalDecision) async throws
|
||||
func resolveUserInput(id: String, answers: [String: String]) async throws
|
||||
func resolveUserInput(id: String, answers: [String: FeatureInputAnswer]) async throws
|
||||
|
||||
func saveSettings(_ settings: FeatureSettings) async throws
|
||||
|
||||
@@ -72,7 +72,16 @@ public extension FeatureClient {
|
||||
func removeEnvironment(id: String) async throws {}
|
||||
func disconnect() async {}
|
||||
func addProject(path: String) async throws {}
|
||||
func resolveUserInput(id: String, answers: [String: String]) async throws {}
|
||||
func resolveUserInput(id: String, answers: [String: FeatureInputAnswer]) async throws {}
|
||||
|
||||
/// Keeps simple text-only callers source-compatible while the typed API
|
||||
/// preserves multi-select answers as arrays.
|
||||
func resolveUserInput(id: String, answers: [String: String]) async throws {
|
||||
try await resolveUserInput(
|
||||
id: id,
|
||||
answers: answers.mapValues(FeatureInputAnswer.text)
|
||||
)
|
||||
}
|
||||
func setThreadSettled(id: String, settled: Bool) async throws {}
|
||||
func setThreadSnoozed(id: String, until: Date?) async throws {}
|
||||
func setRuntimeMode(id: String, mode: FeatureRuntimeMode) async throws {}
|
||||
|
||||
@@ -298,6 +298,67 @@ public struct FeatureInputOption: Sendable, Equatable, Hashable, Codable {
|
||||
}
|
||||
}
|
||||
|
||||
/// A provider answer is either free-form/single-select text or the selected
|
||||
/// labels for a multi-select question. Its Codable shape intentionally matches
|
||||
/// the provider wire contract: a JSON string or an array of JSON strings.
|
||||
public enum FeatureInputAnswer: Sendable, Equatable, Hashable, Codable {
|
||||
case text(String)
|
||||
case selections([String])
|
||||
|
||||
public init(from decoder: any Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let value = try? container.decode(String.self) {
|
||||
self = .text(value)
|
||||
} else {
|
||||
self = try .selections(container.decode([String].self))
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: any Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case let .text(value):
|
||||
try container.encode(value)
|
||||
case let .selections(values):
|
||||
try container.encode(values)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension FeatureInputAnswer {
|
||||
var normalized: FeatureInputAnswer? {
|
||||
switch self {
|
||||
case let .text(value):
|
||||
let normalized = value.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
return normalized.isEmpty ? nil : .text(normalized)
|
||||
case let .selections(values):
|
||||
var seen: Set<String> = []
|
||||
let normalized = values.compactMap { value -> String? in
|
||||
let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !trimmed.isEmpty, seen.insert(trimmed).inserted else { return nil }
|
||||
return trimmed
|
||||
}
|
||||
return normalized.isEmpty ? nil : .selections(normalized)
|
||||
}
|
||||
}
|
||||
|
||||
func togglingOption(_ label: String, allowsMultiple: Bool) -> FeatureInputAnswer {
|
||||
guard allowsMultiple else { return .text(label) }
|
||||
|
||||
let current: [String]
|
||||
if case let .selections(values) = self {
|
||||
current = values
|
||||
} else {
|
||||
current = []
|
||||
}
|
||||
|
||||
if current.contains(label) {
|
||||
return .selections(current.filter { $0 != label })
|
||||
}
|
||||
return .selections(current + [label])
|
||||
}
|
||||
}
|
||||
|
||||
public struct FeatureInputQuestion: Identifiable, Sendable, Equatable, Hashable, Codable {
|
||||
public let id: String
|
||||
public var header: String
|
||||
|
||||
@@ -174,6 +174,30 @@ struct FeatureRootModelTests {
|
||||
#expect(updated.snoozedAt != oldSnooze)
|
||||
#expect(updated.snoozedAt! > updated.attentionAt!)
|
||||
}
|
||||
|
||||
@Test
|
||||
func testResolveUserInputForwardsTypedAnswersAndClearsTheRequest() async {
|
||||
let client = FeatureClientStub()
|
||||
let thread = FeatureThread(id: "thread-1", projectID: "project-1", title: "Thread")
|
||||
let request = FeatureUserInput(
|
||||
id: "request-1",
|
||||
threadID: thread.id,
|
||||
questions: []
|
||||
)
|
||||
client.threadDetail = FeatureThreadDetail(thread: thread, userInputs: [request])
|
||||
let model = FeatureRootModel(client: client)
|
||||
_ = await model.detail(for: thread.id)
|
||||
|
||||
let answers: [String: FeatureInputAnswer] = [
|
||||
"scope": .selections(["Server", "Web"]),
|
||||
"note": .text("Ship it"),
|
||||
]
|
||||
await model.resolveUserInput(request.id, answers: answers)
|
||||
|
||||
#expect(client.resolvedInputID == request.id)
|
||||
#expect(client.resolvedInputAnswers == answers)
|
||||
#expect(model.details[thread.id]?.userInputs.isEmpty == true)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@@ -190,6 +214,8 @@ private final class FeatureClientStub: FeatureClient {
|
||||
var createThreadCallCount = 0
|
||||
var sendMessageCallCount = 0
|
||||
var loadThreadError: (any Error)?
|
||||
var resolvedInputID: String?
|
||||
var resolvedInputAnswers: [String: FeatureInputAnswer]?
|
||||
|
||||
func initialSnapshot() async throws -> FeatureSnapshot {
|
||||
if pairEndpoint != nil, let snapshotAfterPair {
|
||||
@@ -246,5 +272,12 @@ private final class FeatureClientStub: FeatureClient {
|
||||
|
||||
func cancelTurn(threadID: String) async throws {}
|
||||
func resolveApproval(id: String, decision: FeatureApprovalDecision) async throws {}
|
||||
func resolveUserInput(
|
||||
id: String,
|
||||
answers: [String: FeatureInputAnswer]
|
||||
) async throws {
|
||||
resolvedInputID = id
|
||||
resolvedInputAnswers = answers
|
||||
}
|
||||
func saveSettings(_ settings: FeatureSettings) async throws {}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import T3Code
|
||||
|
||||
@Suite("User input answers")
|
||||
struct UserInputAnswerTests {
|
||||
@Test
|
||||
func testCodableShapeMatchesProviderWireValues() throws {
|
||||
let encoder = JSONEncoder()
|
||||
let decoder = JSONDecoder()
|
||||
|
||||
let textData = try encoder.encode(FeatureInputAnswer.text("Deploy"))
|
||||
let selectionsData = try encoder.encode(
|
||||
FeatureInputAnswer.selections(["Server", "Web"])
|
||||
)
|
||||
|
||||
#expect(try decoder.decode(JSONValue.self, from: textData) == .string("Deploy"))
|
||||
#expect(
|
||||
try decoder.decode(JSONValue.self, from: selectionsData)
|
||||
== .array([.string("Server"), .string("Web")])
|
||||
)
|
||||
#expect(try decoder.decode(FeatureInputAnswer.self, from: textData) == .text("Deploy"))
|
||||
#expect(
|
||||
try decoder.decode(FeatureInputAnswer.self, from: selectionsData)
|
||||
== .selections(["Server", "Web"])
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
func testNativeJSONMappingPreservesStringAndArrayTypes() {
|
||||
#expect(FeatureInputAnswer.text("Deploy").jsonValue == .string("Deploy"))
|
||||
#expect(
|
||||
FeatureInputAnswer.selections(["Server", "Web"]).jsonValue
|
||||
== .array([.string("Server"), .string("Web")])
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
func testMultiSelectTogglesWithoutFlatteningSelections() {
|
||||
let first = FeatureInputAnswer.selections([])
|
||||
.togglingOption("Server", allowsMultiple: true)
|
||||
let second = first.togglingOption("Web", allowsMultiple: true)
|
||||
let deselected = second.togglingOption("Server", allowsMultiple: true)
|
||||
|
||||
#expect(first == .selections(["Server"]))
|
||||
#expect(second == .selections(["Server", "Web"]))
|
||||
#expect(deselected == .selections(["Web"]))
|
||||
#expect(
|
||||
second.togglingOption("CLI", allowsMultiple: false)
|
||||
== .text("CLI")
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
func testAnswersNormalizeBeforeSubmission() {
|
||||
#expect(FeatureInputAnswer.text(" ship it ").normalized == .text("ship it"))
|
||||
#expect(
|
||||
FeatureInputAnswer.selections([" Server ", "", "Server", "Web"]).normalized
|
||||
== .selections(["Server", "Web"])
|
||||
)
|
||||
#expect(FeatureInputAnswer.text(" ").normalized == nil)
|
||||
#expect(FeatureInputAnswer.selections([]).normalized == nil)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user