Skip to content

Conversation

@jamesrochabrun
Copy link
Owner

Summary

This PR adds Linux support to SwiftAnthropic by implementing a cross-platform networking layer, following the exact pattern used in SwiftOpenAI PR #143 by @mergesort.

Changes

1. HTTPClient Abstraction Layer

  • Created HTTPClient protocol to abstract networking functionality
  • Added HTTPRequest, HTTPResponse, and HTTPByteStream types for platform-agnostic networking
  • Implemented URLSessionHTTPClientAdapter for Apple platforms
  • Implemented AsyncHTTPClientAdapter for Linux using AsyncHTTPClient

2. Package.swift Updates

  • Added async-http-client dependency (v1.25.2) with conditional compilation for Linux only
  • Specified target path to ensure proper module resolution

3. Service Layer Updates

  • Updated AnthropicService protocol to use HTTPClient instead of URLSession
  • Modified DefaultAnthropicService to accept HTTPClient parameter
  • Updated AnthropicServiceFactory to create platform-appropriate HTTP clients
  • Modified fetch() and fetchStream() methods to work with the new abstraction

4. AIProxy Conditional Compilation

  • Wrapped AIProxyService, AIProxyCertificatePinning, and Endpoint+AIProxy in #if \!os(Linux) conditionals
  • AIProxy functionality is now excluded from Linux builds (as it doesn't make sense for server environments)

5. CI/CD Support

  • Added GitHub Actions workflow with separate jobs for macOS and Linux
  • Linux job uses Ubuntu with Swift 6.0.1 container

6. Documentation

  • Updated README to include Linux platform badge
  • Added platform support section explaining Linux compatibility
  • Noted that AIProxy is only available on Apple platforms

Platform Support

  • Apple platforms: iOS 15+, macOS 12+
  • Linux: Uses AsyncHTTPClient to work around URLSession limitations

Test Plan

  • Package builds successfully on macOS
  • Example iOS app builds without requiring AsyncHTTPClient dependency
  • Test on Linux environment
  • Verify all existing functionality works on Apple platforms
  • Confirm AIProxy is properly excluded on Linux

Notes

  • The implementation maintains full backward compatibility
  • iOS/macOS apps don't need to resolve AsyncHTTPClient dependency
  • The conditional compilation ensures platform-specific code is only included where needed

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

…od references

- Add conditional FoundationNetworking import to Endpoint.swift and DefaultAnthropicService.swift
- Fix HTTPMethod enum references by using HTTPMethod.post instead of .post
- Ensures URLRequest and related types are available on Linux

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@jamesrochabrun jamesrochabrun merged commit 0e718d8 into main Jul 16, 2025
2 checks passed
@jamesrochabrun jamesrochabrun deleted the jroch-linux-support branch July 16, 2025 05:20
@mergesort
Copy link

Hey @jamesrochabrun, sorry I didn't have a chance to look this over before it was released. This is probably the busiest summer of my life, so all lower priority open source work has fallen by the wayside for me the last couple of weeks. But I'm very excited to try this out into my server when I finally get around to integrating it — thank you so much for the Linux support!

@jamesrochabrun
Copy link
Owner Author

@mergesort this is all thank to you so take the credit! Let me know how the testing goes. Thanks!

@mergesort
Copy link

Aww thanks, too kind of you to say. Really it's all thanks to you, since this project wouldn't exist without your hard work. ☺️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants