Skip to content

Change initializer apiKey parameter to be a token provider closure#6

Merged
mattt merged 2 commits intomainfrom
mattt/token-provider
Oct 28, 2025
Merged

Change initializer apiKey parameter to be a token provider closure#6
mattt merged 2 commits intomainfrom
mattt/token-provider

Conversation

@mattt
Copy link
Owner

@mattt mattt commented Oct 28, 2025

This PR updates the initializers for AnthropicLanguageModel and OpenAILanguageModel to accept closures for the apiKey parameter. By using @autoclosure, we can maintain the existing usage providing String arguments (such as ProcessInfo.processInfo.environment["ANTHROPIC_API_KEY"].

Making this a closure allows callers to dynamically inject credentials, such as with an OAuth manager.

@mattt mattt requested a review from Copilot October 28, 2025 10:59
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the API key handling in AnthropicLanguageModel and OpenAILanguageModel to use closure-based token providers instead of static strings. By leveraging @autoclosure, existing code passing string literals continues to work unchanged, while new use cases can dynamically provide credentials (e.g., through OAuth managers).

Key Changes:

  • Replaced apiKey: String parameters with @autoclosure closures that return String
  • Made token providers private and invoked them at request time rather than initialization
  • Removed cached headers in AnthropicLanguageModel, rebuilding them per-request to use fresh tokens

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
Sources/AnyLanguageModel/Models/OpenAILanguageModel.swift Changed apiKey to a closure-based tokenProvider, updated all authorization headers to invoke the provider
Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift Changed apiKey to a closure-based tokenProvider, removed cached headers field, and rebuilt headers per-request
Tests/AnyLanguageModelTests/OpenAILanguageModelTests.swift Removed test assertion for apiKey property since it's now private

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mattt mattt merged commit 13e1b29 into main Oct 28, 2025
2 checks passed
@mattt mattt deleted the mattt/token-provider branch October 28, 2025 11:12
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