### Automatic Tool Invocation with .whenInputAvailable Strategy Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This example shows how to configure automatic tool invocation using the `.whenInputAvailable` strategy. When this strategy is set, tools are automatically invoked by SwiftClaude as soon as their inputs are successfully decoded. This simplifies the process for simple tools that provide context or display UI, reducing the need for manual invocation requests. ```Swift let message = claude.nextMessage( in: conversation, tools: Tools { CatEmojiTool() EmojiTool() }, invokeTools: .whenInputAvailable ) ``` -------------------------------- ### Defining Custom Tool Input with @ToolInput Macro in Swift Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This example shows how to define a more sophisticated input type for a tool using the `@ToolInput` macro. It creates an `enum` named `Command` that can represent different browser navigation actions, including navigating to a specific URL. This allows for structured and type-safe tool inputs. ```Swift @ToolInput enum Command { case goBack case goForward case navigate(to: String) } ``` -------------------------------- ### Displaying SwiftClaude Content Blocks in SwiftUI Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This SwiftUI example shows how to render content blocks from an observable `assistant` object, which conforms to `Observation`. It uses a `ForEach` loop to iterate over `currentContentBlocks`, displaying text blocks as `Text` views and tool use blocks with their names and outputs (if available). This pattern simplifies integrating Claude's responses, including tool interactions, into a SwiftUI user interface. ```Swift ForEach(assistant.currentContentBlocks) { block in switch block { case .textBlock(let textBlock): Text(textBlock.currentText) case .toolUseBlock(let toolUseBlock): if let output = toolUseBlock.toolUse.currentOutput { Text("[Using \(toolUseBlock.toolUse.toolName): \(output)]") } else { Text("[Using \(toolUseBlock.toolUse.toolName)]") } } } ``` -------------------------------- ### Listing All Swift Tests Source: https://github.com/georgelyon/swiftclaude/blob/main/CLAUDE.md Displays a comprehensive list of all available test cases and suites within the Swift project. This is useful for identifying specific tests to run or debug. ```Shell swift test list ``` -------------------------------- ### Building Swift Project Source: https://github.com/georgelyon/swiftclaude/blob/main/CLAUDE.md Compiles the Swift project. This command is used to build the executable or library targets defined in the Swift package. ```Shell swift build ``` -------------------------------- ### Creating Claude Instance with Authenticator Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md Illustrates how to instantiate the main Claude object by passing an initialized authenticator instance. This sets up the Claude API client for subsequent operations. ```Swift let claude = Claude(authenticator: authenticator) ``` -------------------------------- ### Formatting Swift Code Source: https://github.com/georgelyon/swiftclaude/blob/main/CLAUDE.md Applies standard Swift formatting rules to the codebase. This command ensures consistent code style across the project, improving readability and maintainability. ```Shell swift-format ``` -------------------------------- ### Running All Swift Tests Source: https://github.com/georgelyon/swiftclaude/blob/main/CLAUDE.md Executes all unit tests defined within the Swift project. This command is essential for verifying the correctness of the codebase. ```Shell swift test ``` -------------------------------- ### Initializing KeychainAuthenticator for Secure API Key Storage Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md Shows how to create an instance of Claude.KeychainAuthenticator to securely store API keys in the keychain on Apple platforms. It requires a namespace and an identifier for the key. ```Swift let authenticator = Claude.KeychainAuthenticator( namespace: "com.codebygeorge.SwiftClaude.HaikuGenerator", identifier: "api-key" ) /// Call `authenticator.save(…)` with your API key ``` -------------------------------- ### Providing Tools to Claude in SwiftClaude Conversation Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This snippet demonstrates how to provide a list of available tools to Claude when requesting the next message in a conversation. The `Tools` builder is used to register instances of `CatEmojiTool` and `EmojiTool`, making them accessible for Claude to invoke based on its understanding of the conversation and tool definitions. ```Swift let message = claude.nextMessage( in: conversation, tools: Tools { CatEmojiTool() EmojiTool() } ) ``` -------------------------------- ### Defining a Simple Tool with @Tool Macro in SwiftClaude Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This snippet demonstrates how to define a basic tool in SwiftClaude using the `@Tool` macro. The `invoke` method is the entry point for Claude to call the tool, and its comments are crucial for Claude's understanding of the tool's purpose and parameters. This tool simply returns the input emoji. ```Swift @Tool struct EmojiTool { /// Displays an emoji /// Great for spicing up a haiku! func invoke( _ emoji: String ) -> String { emoji } } ``` -------------------------------- ### Running Specific Swift Test Suite Source: https://github.com/georgelyon/swiftclaude/blob/main/CLAUDE.md Executes all test cases within a specified test suite. The --filter option can be used to run an entire suite by providing its name. ```Shell swift test --filter "ToolTests.BoolSchemaTests" ``` -------------------------------- ### Handling Tool Results and Continuing Conversation Loop in SwiftClaude Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This snippet illustrates a common pattern for managing conversations that involve tool use. It uses a `repeat-while` loop to continuously fetch the next message from Claude and append it to the conversation. The loop continues as long as `conversation.nextStep()` indicates that a tool use result is expected, ensuring that all tool interactions are processed before proceeding with the conversation. ```Swift repeat { let message = claude.nextMessage( in: conversation, tools: Tools { … } ) conversation.append(message) } while try await conversation.nextStep() == .toolUseResult ``` -------------------------------- ### Running Specific Swift Test Case Source: https://github.com/georgelyon/swiftclaude/blob/main/CLAUDE.md Executes a single, specific test case within a test suite. The --filter option allows targeting a particular test by its full name, including the suite and test function. ```Shell swift test --filter "ToolTests.BoolSchemaTests/testSchemaEncoding" ``` -------------------------------- ### Adding SwiftClaude to Package.swift Dependencies Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md Provides the Swift Package Manager configuration snippet to include SwiftClaude as a dependency in a Package.swift file. This step is necessary to integrate the library into a Swift project. ```Swift let package = Package( … dependencies: [ .package(url: "git@github.com:GeorgeLyon/SwiftClaude", branch: "main") ], … ) ``` -------------------------------- ### Integrating Custom @ToolInput into a SwiftClaude Tool Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This snippet illustrates how to use a custom `@ToolInput` type (like the `Command` enum) as a parameter in a tool's `invoke` function. The `Browser` tool accepts a `Command` object, enabling Claude to issue complex, structured commands to control a simulated browser. The `invoke` function would contain the logic to execute the given command. ```Swift @Tool struct Browser { /// Controls a browser func invoke( _ command: Command ) -> String { /// Execute `command` } } ``` -------------------------------- ### Defining a Claude Conversation in Swift Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This snippet defines a `Conversation` struct conforming to the `Claude.Conversation` protocol, which serves as the core abstraction for managing messages in a dialogue with Claude. It demonstrates initializing a conversation with an initial user message. ```Swift import SwiftClaude struct Conversation: Claude.Conversation { var messages: [Message] } var conversation = Conversation( messages: [ .user("Write me a haiku about a really well-made tool.") ] ) ``` -------------------------------- ### Processing Message Content Blocks with Async API in SwiftClaude Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This snippet demonstrates how to asynchronously process different types of content blocks received from a Claude message, including text and tool use blocks. It iterates through `message.contentBlocks`, handling `textBlock` by printing its fragments and `toolUseBlock` by printing the tool name and its output. This is crucial for conversations involving tool interactions. ```Swift for try await block in message.contentBlocks { switch block { case .textBlock(let textBlock): for try await textFragment in textBlock.textFragments { print(textFragment, terminator: "") fflush(stdout) } case .toolUseBlock(let toolUseBlock): print("[Using \(toolUseBlock.toolName): \(try await toolUseBlock.output())"]") } } print() ``` -------------------------------- ### Adding SwiftClaude to Target Dependencies Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md Shows how to add "SwiftClaude" to the dependencies of a specific target within a Swift Package Manager configuration. This ensures the library is linked and available for use in the target's code. ```Swift .target( … dependencies: [ "SwiftClaude" ], … ), ``` -------------------------------- ### Specifying ToolOutput Type for SwiftClaude Conversation Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This code defines a `Conversation` struct conforming to `Claude.Conversation` and explicitly sets its `ToolOutput` typealias to `String`. This is necessary to inform SwiftClaude about the expected return type when tools are invoked within the conversation context. `String` is the simplest output type, but more complex types can be used. ```Swift private struct Conversation: Claude.Conversation { var messages: [Message] = [] typealias ToolOutput = String } ``` -------------------------------- ### Continuing a Multi-Turn Claude Conversation in Swift Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This snippet demonstrates how to continue a conversation with Claude by appending the assistant's previous response and a new user message to the existing conversation. After updating the conversation, a new message is requested to facilitate multi-turn interactions. ```Swift conversation.messages += [ .assistant(message), .user("That was great! Can you write me one more, this time about track saws?") ] let nextMessage = claude.nextMessage(in: conversation) ``` -------------------------------- ### Appending Image to User Message in SwiftClaude Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md Demonstrates how to include UIImage or NSImage directly in a user message within SwiftClaude for vision capabilities. This allows the model to process and describe the provided image. ```Swift conversation.messages.append( .user("Describe this image: \(image)") ) ``` -------------------------------- ### Processing Claude Message Text in Swift Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This snippet illustrates two methods for processing the text content of a message received from Claude: awaiting the full text or processing it asynchronously in segments as they arrive. The segmented approach is useful for real-time display or streaming interfaces. ```Swift /// Print the full text print(try await message.text()) /// Print the text as segments come in for try await segment in message.textSegments { print(segment, terminator: "") fflush(stdout) } print() ``` -------------------------------- ### Requesting the Next Message from Claude in Swift Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This snippet demonstrates how to request the next message from Claude within an existing conversation. It assumes that a `claude` instance has been created and a `conversation` object has been properly initialized with previous messages. ```Swift let message = claude.nextMessage(in: conversation) ``` -------------------------------- ### Displaying Claude Message Text in SwiftUI Source: https://github.com/georgelyon/swiftclaude/blob/main/Readme.md This snippet shows how to directly use an `Observable` message's `currentText` property within a SwiftUI `Text` view. This allows for real-time updates of the displayed text as the message content streams in, leveraging SwiftUI's reactive capabilities. ```Swift Text(message.currentText) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.