From bf39cad5536744daeab224d528d8665ed9cea36d Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 30 Jan 2026 19:52:23 +0000 Subject: [PATCH 1/2] Fix Swift harness comments in tool-calls.mdx - Add StreamEvent struct definition to first example harness - Add renderWeatherCard function parameter to generative UI example - Add getGeolocationPosition function parameter to client-side tools example These changes ensure the test harness comments are complete and can be used standalone for compilation verification. https://claude.ai/code/session_01RUFvjvGEQkg5UZp71Ak9hV --- .../ai-transport/messaging/tool-calls.mdx | 260 ++++++++++++++++++ 1 file changed, 260 insertions(+) diff --git a/src/pages/docs/ai-transport/messaging/tool-calls.mdx b/src/pages/docs/ai-transport/messaging/tool-calls.mdx index 15de573ff2..5b509df4b0 100644 --- a/src/pages/docs/ai-transport/messaging/tool-calls.mdx +++ b/src/pages/docs/ai-transport/messaging/tool-calls.mdx @@ -84,6 +84,95 @@ for await (const event of stream) { } } ``` + +{/* 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` + +struct StreamEvent { + let type: String + let name: String? + let args: String? + let result: String? + let text: String? + let toolCallId: String? + let responseId: String +} + +func example_publish_tool_calls(realtime: ARTRealtime, stream: AsyncStream) async throws { + // --- example code starts here --- +*/} +```swift +let channel = realtime.channels.get("{{RANDOM_CHANNEL_NAME}}") + +// Example: stream returns events like: +// { type: 'tool_call', name: 'get_weather', args: '{"location":"San Francisco"}', toolCallId: 'tool_123', responseId: 'resp_abc123' } +// { type: 'tool_result', name: 'get_weather', result: '{"temp":72,"conditions":"sunny"}', toolCallId: 'tool_123', responseId: 'resp_abc123' } +// { type: 'message', text: 'The weather in San Francisco is 72°F and sunny.', responseId: 'resp_abc123' } + +for await event in stream { + if event.type == "tool_call" { + // Publish tool call arguments + let message = ARTMessage(name: "tool_call", data: [ + "name": event.name ?? "", + "args": event.args ?? "" + ]) + message.extras = [ + "headers": [ + "responseId": event.responseId, + "toolCallId": event.toolCallId ?? "" + ] + ] as ARTJsonCompatible + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in + channel.publish([message]) { error in + if let error = error { + continuation.resume(throwing: error) + } else { + continuation.resume() + } + } + } + } else if event.type == "tool_result" { + // Publish tool call results + let message = ARTMessage(name: "tool_result", data: [ + "name": event.name ?? "", + "result": event.result ?? "" + ]) + message.extras = [ + "headers": [ + "responseId": event.responseId, + "toolCallId": event.toolCallId ?? "" + ] + ] as ARTJsonCompatible + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in + channel.publish([message]) { error in + if let error = error { + continuation.resume(throwing: error) + } else { + continuation.resume() + } + } + } + } else if event.type == "message" { + // Publish model output messages + let message = ARTMessage(name: "message", data: event.text ?? "") + message.extras = [ + "headers": [ + "responseId": event.responseId + ] + ] as ARTJsonCompatible + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in + channel.publish([message]) { error in + if let error = error { + continuation.resume(throwing: error) + } else { + continuation.resume() + } + } + } + } +} +``` +{/* --- end example code --- */}