diff --git a/src/pages/docs/ai-transport/messaging/accepting-user-input.mdx b/src/pages/docs/ai-transport/messaging/accepting-user-input.mdx index 2a66b7f43d..eb9e5eb73e 100644 --- a/src/pages/docs/ai-transport/messaging/accepting-user-input.mdx +++ b/src/pages/docs/ai-transport/messaging/accepting-user-input.mdx @@ -43,6 +43,19 @@ const claims = { 'x-ably-clientId': 'user-123' }; ``` + +{/* Swift example test harness: to modify and check it compiles, copy this comment into a +temporary Swift file, paste the example code into the function body, and run `swift build` + +func example() { + // --- example code starts here --- +*/} +```swift +let claims: [String: String] = [ + "x-ably-clientId": "user-123" +] +``` +{/* --- end example code --- */} The `clientId` is automatically attached to every message the user publishes, so agents can trust this identity. @@ -58,6 +71,28 @@ await channel.subscribe('user-input', (message) => { processAndRespond(channel, text, promptId, userId); }); ``` + +{/* Swift example test harness: to modify and check it compiles, copy this comment into a +temporary Swift file, paste the example code into the function body, and run `swift build` + +func example(channel: ARTRealtimeChannel) async throws { + // --- example code starts here --- +*/} +```swift +channel.subscribe("user-input") { message in + let userId = message.clientId + // promptId is a user-generated UUID for correlating responses + guard let data = message.data as? [String: Any], + let promptId = data["promptId"] as? String, + let text = data["text"] as? String else { + return + } + + print("Received prompt from user \(userId ?? "")") + // processAndRespond(channel, text, promptId, userId) +} +``` +{/* --- end example code --- */} ### Verify by role @@ -72,6 +107,19 @@ const claims = { 'ably.channel.*': 'user' }; ``` + +{/* Swift example test harness: to modify and check it compiles, copy this comment into a +temporary Swift file, paste the example code into the function body, and run `swift build` + +func example() { + // --- example code starts here --- +*/} +```swift +let claims: [String: String] = [ + "ably.channel.*": "user" +] +``` +{/* --- end example code --- */} The user claim is automatically attached to every message the user publishes, so agents can trust this role information. @@ -91,6 +139,32 @@ await channel.subscribe('user-input', (message) => { processAndRespond(channel, text, promptId); }); ``` + +{/* Swift example test harness: to modify and check it compiles, copy this comment into a +temporary Swift file, paste the example code into the function body, and run `swift build` + +func example(channel: ARTRealtimeChannel) async throws { + // --- example code starts here --- +*/} +```swift +channel.subscribe("user-input") { message in + let role = message.extras?["userClaim"] as? String + // promptId is a user-generated UUID for correlating responses + guard let data = message.data as? [String: Any], + let promptId = data["promptId"] as? String, + let text = data["text"] as? String else { + return + } + + if role != "user" { + print("Ignoring message from non-user") + return + } + + // processAndRespond(channel, text, promptId) +} +``` +{/* --- end example code --- */} ## Publish user input @@ -107,6 +181,26 @@ await channel.publish('user-input', { text: 'What is the weather like today?' }); ``` + +{/* Swift example test harness: to modify and check it compiles, copy this comment into a +temporary Swift file, paste the example code into the function body, and run `swift build` + +func example(channel: ARTRealtimeChannel) async throws { + // --- example code starts here --- +*/} +```swift +let promptId = UUID().uuidString +let message = ARTMessage(name: "user-input", data: [ + "promptId": promptId, + "text": "What is the weather like today?" +]) +channel.publish([message]) { error in + if let error { + print("Error publishing message: \(error)") + } +} +``` +{/* --- end example code --- */}