From de9a678b298813b0e87306d7ed701a40a9887394 Mon Sep 17 00:00:00 2001 From: evgeny Date: Mon, 26 Jan 2026 12:07:55 +0000 Subject: [PATCH 1/2] [AIT-278][AIT-303] feat: Add Python and Java examples for AIT guides - Added examples in Python and Java for processing user input, streaming responses, human-approval flows, and chain-of-thought reasoning. - Covered annotations use cases, including citations with metadata. - Updated documentation with code illustrating multi-channel communication. --- .../messaging/accepting-user-input.mdx | 327 +++++++++++ .../messaging/chain-of-thought.mdx | 281 +++++++++ .../docs/ai-transport/messaging/citations.mdx | 74 +++ .../messaging/human-in-the-loop.mdx | 216 +++++++ .../ai-transport/messaging/tool-calls.mdx | 325 +++++++++++ .../identifying-users-and-agents.mdx | 488 ++++++++++++++++ .../sessions-identity/online-status.mdx | 287 +++++++++ .../sessions-identity/resuming-sessions.mdx | 101 ++++ .../token-streaming/message-per-response.mdx | 542 +++++++++++++++++ .../token-streaming/message-per-token.mdx | 545 ++++++++++++++++++ 10 files changed, 3186 insertions(+) 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..9ec4af4b8f 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,15 @@ const claims = { 'x-ably-clientId': 'user-123' }; ``` +```python +claims = { + 'x-ably-clientId': 'user-123' +} +``` +```java +Map claims = new HashMap<>(); +claims.put("x-ably-clientId", "user-123"); +``` The `clientId` is automatically attached to every message the user publishes, so agents can trust this identity. @@ -58,6 +67,30 @@ await channel.subscribe('user-input', (message) => { processAndRespond(channel, text, promptId, userId); }); ``` +```python +def on_user_input(message): + user_id = message.client_id + # promptId is a user-generated UUID for correlating responses + prompt_id = message.data['promptId'] + text = message.data['text'] + + print(f'Received prompt from user {user_id}') + process_and_respond(channel, text, prompt_id, user_id) + +await channel.subscribe('user-input', on_user_input) +``` +```java +channel.subscribe("user-input", message -> { + String userId = message.clientId; + // promptId is a user-generated UUID for correlating responses + JsonObject data = (JsonObject) message.data; + String promptId = data.get("promptId").getAsString(); + String text = data.get("text").getAsString(); + + System.out.println("Received prompt from user " + userId); + processAndRespond(channel, text, promptId, userId); +}); +``` ### Verify by role @@ -72,6 +105,15 @@ const claims = { 'ably.channel.*': 'user' }; ``` +```python +claims = { + 'ably.channel.*': 'user' +} +``` +```java +Map claims = new HashMap<>(); +claims.put("ably.channel.*", "user"); +``` The user claim is automatically attached to every message the user publishes, so agents can trust this role information. @@ -91,6 +133,37 @@ await channel.subscribe('user-input', (message) => { processAndRespond(channel, text, promptId); }); ``` +```python +def on_user_input(message): + role = message.extras.get('userClaim') + # promptId is a user-generated UUID for correlating responses + prompt_id = message.data['promptId'] + text = message.data['text'] + + if role != 'user': + print('Ignoring message from non-user') + return + + process_and_respond(channel, text, prompt_id) + +await channel.subscribe('user-input', on_user_input) +``` +```java +channel.subscribe("user-input", message -> { + String role = message.extras.get("userClaim").getAsString(); + // promptId is a user-generated UUID for correlating responses + JsonObject data = (JsonObject) message.data; + String promptId = data.get("promptId").getAsString(); + String text = data.get("text").getAsString(); + + if (!role.equals("user")) { + System.out.println("Ignoring message from non-user"); + return; + } + + processAndRespond(channel, text, promptId); +}); +``` ## Publish user input @@ -107,6 +180,30 @@ await channel.publish('user-input', { text: 'What is the weather like today?' }); ``` +```python +import uuid + +channel = ably.channels.get('{{RANDOM_CHANNEL_NAME}}') + +prompt_id = str(uuid.uuid4()) +message = Message( + name='user-input', + data={ + 'promptId': prompt_id, + 'text': 'What is the weather like today?' + } +) +await channel.publish(message) +``` +```java +Channel channel = ably.channels.get("{{RANDOM_CHANNEL_NAME}}"); + +String promptId = UUID.randomUUID().toString(); +JsonObject data = new JsonObject(); +data.addProperty("promptId", promptId); +data.addProperty("text", "What is the weather like today?"); +channel.publish("user-input", data); +```