ReviewOS

also looking at this

craft-native/craft

fix(ios): hand the page the answers native was already sending

#222
Merged glennmichael123 wants to merge fix/ios-unanswered-posts into main
18 files +597 -164
packages/zig/src/bridge_mobile_speech.zigmodified+30-26
Changes to packages/zig/src/bridge_mobile_speech.zig
@@ -1,13 +1,12 @@
11//! The `mobile` namespace's speech pair: `startListening` and `stopListening`.
22//!
3//! Neither action replies. Both are raw `postMessage` calls with no
4//! `callbackId` (`CraftApp.swift:1562-1568`), so nothing on the page is
5//! awaiting an answer and every outcome — success, refusal, transcript, end —
6//! travels as a `craftSpeech*` event. That is why both are declared
7//! `.reply = .none` below, and why no path in this file returns an error to
8//! `ios_dispatch`: an error there would call `sendErrorToJS` under a request
9//! id no page is holding, which is console noise standing in for the event a
10//! page is actually listening for.
3//! Both actions answer `true` as soon as the request is taken, and nothing
4//! more. The page awaits that answer (#207), but everything that happens after
5//! it — a declined prompt, no recognizer, a transcript, the end — travels as a
6//! `craftSpeech*` event, because none of it is known while the dispatch frame
7//! is still open. No path in this file returns an error to `ios_dispatch`
8//! either: a failure is reported as the event a page is listening for, and
9//! the promise has already been answered.
1110//!
1211//! ## What this round changed
1312//!
@@ -188,25 +187,28 @@ pub const A = struct {
188187 pub const stop_listening = "stopListening";
189188};
190189
191/// `.none` for both, and the distinction matters.
190/// `.result` for both: `true`, sent from `handleMessage` while the dispatch
191/// frame still holds the page's request id.
192192///
193/// `.result` would tell an app to await a reply that never comes: neither
194/// action calls `sendResultToJS` on any path, because neither has a page-side
195/// promise to settle. Everything observable leaves through `ios_events`.
193/// Settling on the outcome instead would leave the promise open. The
194/// authorization answer arrives after `ios_dispatch` has popped the request
195/// context, so a reply sent from there carries no id and the page drops it,
196/// and a start while authorization is already in flight has no outcome of its
197/// own. Everything after the `true` leaves through `ios_events`.
196198///
197199/// `.live` for both. A refusal here is a specific condition — speech not
198200/// configured, authorization declined, no input route — and never the normal
199201/// answer, which is what `.unavailable` would claim.
200202pub const capability_actions = [_]capabilities.ActionDecl{
201 .{ .name = A.start_listening, .reply = .none },
202 .{ .name = A.stop_listening, .reply = .none },
203 .{ .name = A.start_listening, .reply = .result },
204 .{ .name = A.stop_listening, .reply = .result },
203205};
204206
205207pub const SpeechBridge = struct {
206 /// Held for the interface `ios_dispatch` builds every mobile bridge with.
207 /// Unused: nothing in a dispatch allocates, and the two paths that do —
208 /// shaping a transcript and shaping an error — run from callbacks where
209 /// this bridge is long gone, so they take the C allocator instead.
208 /// Shapes the `true` each action answers with. The two other paths that
209 /// allocate — shaping a transcript and shaping an error — run from
210 /// callbacks where this bridge is long gone, so they take the C allocator
211 /// instead.
210212 allocator: std.mem.Allocator,
211213
212214 const Self = @This();
@@ -218,15 +220,17 @@ pub const SpeechBridge = struct {
218220 pub fn deinit(_: *Self) void {}
219221
220222 /// The payload is ignored, deliberately: Swift reads no field from either
221 /// action, and the injected JS posts neither a body nor a `callbackId`.
222 /// Parsing it would only create a way to fail that the spec has not got.
223 pub fn handleMessage(_: *Self, action: []const u8, _: []const u8) !void {
223 /// action, and the injected JS posts only a `callbackId`. Parsing it would
224 /// only create a way to fail that the spec has not got.
225 pub fn handleMessage(self: *Self, action: []const u8, _: []const u8) !void {
224226 if (std.mem.eql(u8, action, A.start_listening)) {
225227 startListening();
228 bridge_error.sendResultToJS(self.allocator, A.start_listening, "true");
226229 return;
227230 }
228231 if (std.mem.eql(u8, action, A.stop_listening)) {
229232 stopListening();
233 bridge_error.sendResultToJS(self.allocator, A.stop_listening, "true");
230234 return;
231235 }
232236 return BridgeError.UnknownAction;
@@ -1184,11 +1188,12 @@ test "the action names match the Swift case labels exactly" {
11841188 try testing.expectEqualStrings("stopListening", A.stop_listening);
11851189}
11861190
1187test "both actions are declared, and neither promises a reply" {
1191test "both actions are declared, and both answer the promise the page awaits" {
11881192 try testing.expectEqual(@as(usize, 2), capability_actions.len);
11891193 for (capability_actions) |decl| {
1190 // `.result` would tell an app to await something no path here sends.
1191 try testing.expectEqual(capabilities.Reply.none, decl.reply);
1194 // The page posts a callbackId for both (#207), so `.none` would be a
1195 // promise that never settles.
1196 try testing.expectEqual(capabilities.Reply.result, decl.reply);
11921197 try testing.expectEqual(capabilities.ActionStatus.live, decl.status);
11931198 }
11941199}
@@ -1207,8 +1212,7 @@ test "an unrelated action falls through rather than being refused" {
12071212
12081213test "a malformed payload is not a reason to refuse either action" {
12091214 // Neither action reads a field, so neither parses one. A parser here would
1210 // invent a failure the spec has not got — and it would surface as a
1211 // dispatcher error under a request id no page holds.
1215 // invent a failure the spec has not got.
12121216 var bridge = SpeechBridge.init(testing.allocator);
12131217 defer bridge.deinit();
12141218