Skip to content

Open source CapCut alternative.

TypeScript

fix(app): decode user.choose forms with omitted optional fields and object input

David Le9d349ce51501
diff --git a/apps/Donkey/Sources/DonkeyAI/HostedHarnessStepPlanner.swift b/apps/Donkey/Sources/DonkeyAI/HostedHarnessStepPlanner.swift
index a47b785e..bc1b8175 100644
--- a/apps/Donkey/Sources/DonkeyAI/HostedHarnessStepPlanner.swift
+++ b/apps/Donkey/Sources/DonkeyAI/HostedHarnessStepPlanner.swift
@@ -379,10 +379,18 @@ public final class HostedHarnessStepPlanner: HarnessNextStepPlanning {
value = String(double)
} else if container.decodeNil() {
value = ""
+ } else if let json = try? RemoteInferenceJSONValue(from: decoder),
+ let encoded = try? JSONEncoder().encode(json),
+ let text = String(data: encoded, encoding: .utf8) {
+ // A non-scalar input (object/array) — e.g. user.choose's `form`, which the planner emits
+ // as a JSON object per its descriptor. Re-serialize it to compact JSON text so the tool,
+ // which reads `form` as a JSON string, gets exactly that. Keeps every tool input a string
+ // without forcing the model to hand-stringify nested JSON (which wastes turns and truncates).
+ value = text
} else {
throw DecodingError.dataCorruptedError(
in: container,
- debugDescription: "Tool input value is not a JSON scalar"
+ debugDescription: "Tool input value is not valid JSON"
)
}
}
diff --git a/apps/Donkey/Sources/DonkeyContracts/HarnessChoiceForm.swift b/apps/Donkey/Sources/DonkeyContracts/HarnessChoiceForm.swift
index d3563bc5..0c0a2045 100644
--- a/apps/Donkey/Sources/DonkeyContracts/HarnessChoiceForm.swift
+++ b/apps/Donkey/Sources/DonkeyContracts/HarnessChoiceForm.swift
@@ -65,6 +65,24 @@ public struct HarnessChoiceForm: Codable, Equatable, Sendable {
self.on = on
}
+ private enum CodingKeys: String, CodingKey {
+ case id, label, help, control, options, selected, on
+ }
+
+ public init(from decoder: Decoder) throws {
+ let container = try decoder.container(keyedBy: CodingKeys.self)
+ id = try container.decode(String.self, forKey: .id)
+ label = try container.decode(String.self, forKey: .label)
+ help = try container.decodeIfPresent(String.self, forKey: .help)
+ control = try container.decode(ControlKind.self, forKey: .control)
+ // A `toggle` legitimately carries no `options`. Synthesized Decodable would require the key
+ // and reject the whole form (an invalid-input dead end for any form with a toggle), so decode
+ // it as empty when absent.
+ options = try container.decodeIfPresent([Option].self, forKey: .options) ?? []
+ selected = try container.decodeIfPresent(String.self, forKey: .selected)
+ on = try container.decodeIfPresent(Bool.self, forKey: .on)
+ }
+
/// The value this field starts on — the planner's guessed default. Segmented/dropdown resolve to
/// the declared `selected` if it names a real option, else the first option's id; a toggle to
/// "true"/"false". Returns nil only for an option-less select (a malformed field).
@@ -97,6 +115,21 @@ public struct HarnessChoiceForm: Codable, Equatable, Sendable {
self.submitLabel = submitLabel
self.fields = fields
}
+
+ private enum CodingKeys: String, CodingKey {
+ case title, subtitle, submitLabel, fields
+ }
+
+ public init(from decoder: Decoder) throws {
+ let container = try decoder.container(keyedBy: CodingKeys.self)
+ // Only `fields` is required — the executor rejects a form solely for an empty/absent fields
+ // array. `title`/`submitLabel` are optional per the tool contract, so default them instead of
+ // failing the whole form when the planner omits them.
+ title = try container.decodeIfPresent(String.self, forKey: .title) ?? ""
+ subtitle = try container.decodeIfPresent(String.self, forKey: .subtitle)
+ submitLabel = try container.decodeIfPresent(String.self, forKey: .submitLabel) ?? "Continue"
+ fields = try container.decode([Field].self, forKey: .fields)
+ }
}
public extension HarnessChoiceForm {
diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/HarnessChoiceFormDecodeTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/HarnessChoiceFormDecodeTests.swift
new file mode 100644
index 00000000..648a5760
--- /dev/null
+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/HarnessChoiceFormDecodeTests.swift
@@ -0,0 +1,67 @@
+import DonkeyContracts
+import Testing
+
+/// Locks the `user.choose` form contract: a form is undecodable — and the tool dead-ends on
+/// `invalidInput` — if a `toggle` field (which carries no `options`) forces the whole form to fail.
+@Suite
+struct HarnessChoiceFormDecodeTests {
+ /// The exact shape the video skill emits: a segmented `tier`, an option-less `audio` toggle, and
+ /// more segmented fields. A toggle omitting `options` must still decode.
+ private let videoForm = """
+ {"title":"Video Generation Options","submitLabel":"Generate","fields":[\
+ {"id":"tier","label":"Speed & Quality","control":"segmented","options":[\
+ {"id":"fast","label":"Fast","detail":"Quicker, lower cost"},\
+ {"id":"standard","label":"Standard","detail":"Balanced"},\
+ {"id":"high","label":"High","detail":"Best quality, slower"}],"selected":"standard"},\
+ {"id":"audio","label":"Audio","control":"toggle","on":true},\
+ {"id":"length","label":"Length (seconds)","control":"segmented","options":[\
+ {"id":"4","label":"4s"},{"id":"6","label":"6s"},{"id":"8","label":"8s"}],"selected":"8"},\
+ {"id":"aspect","label":"Aspect Ratio","control":"segmented","options":[\
+ {"id":"16:9","label":"16:9 (Landscape)"},{"id":"9:16","label":"9:16 (Portrait)"}],"selected":"16:9"}]}
+ """
+
+ @Test
+ func decodesFormContainingAnOptionlessToggle() throws {
+ let form = try #require(HarnessChoiceForm.decode(fromJSON: videoForm))
+ #expect(form.fields.count == 4)
+
+ let toggle = try #require(form.fields.first { $0.control == .toggle })
+ #expect(toggle.id == "audio")
+ #expect(toggle.options.isEmpty)
+ #expect(toggle.defaultValue == "true")
+ }
+
+ @Test
+ func guessedDefaultsRoundTripToTheSelectionResponse() throws {
+ let form = try #require(HarnessChoiceForm.decode(fromJSON: videoForm))
+ let response = form.encodeSelectionResponse(form.defaultSelection)
+ #expect(response == "Selected options: tier=standard, audio=true, length=8, aspect=16:9")
+ }
+
+ /// A bare toggle with only `id`/`label`/`control` must decode — the minimum a toggle can be.
+ @Test
+ func decodesAMinimalToggleForm() throws {
+ let json = #"{"title":"T","fields":[{"id":"go","label":"Go","control":"toggle"}]}"#
+ let form = try #require(HarnessChoiceForm.decode(fromJSON: json))
+ #expect(form.fields.count == 1)
+ #expect(form.fields[0].options.isEmpty)
+ #expect(form.fields[0].defaultValue == "false")
+ }
+
+ /// The one hard requirement is a non-empty `fields` array; `title`/`submitLabel` are optional and
+ /// default when the planner omits them.
+ @Test
+ func decodesWithOnlyFields() throws {
+ let json = #"{"fields":[{"id":"go","label":"Go","control":"toggle"}]}"#
+ let form = try #require(HarnessChoiceForm.decode(fromJSON: json))
+ #expect(form.title.isEmpty)
+ #expect(form.submitLabel == "Continue")
+ #expect(form.fields.count == 1)
+ }
+
+ @Test
+ func rejectsAFormWithNoFields() {
+ #expect(HarnessChoiceForm.decode(fromJSON: #"{"title":"T","fields":[]}"#) == nil)
+ #expect(HarnessChoiceForm.decode(fromJSON: #"{"title":"T"}"#) == nil)
+ }
+}