Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Package.swift
Original file line numberDiff line numberDiff line change
Expand Up@@ -204,5 +204,10 @@ let package = Package(
dependencies: ["SyntaxKit", "DocumentationHarness"],
swiftSettings: swiftSettings
),
.testTarget(
name: "AiSTKitTests",
dependencies: ["AiSTKit"],
swiftSettings: swiftSettings
),
]
)
96 changes: 96 additions & 0 deletions Sources/AiSTKit/AuthenticationMiddleware.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
//
// AuthenticationMiddleware.swift
// SyntaxKit
//
// Created by Leo Dion.
// Copyright © 2026 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

public import HTTPTypes
public import OpenAPIRuntime

#if os(Linux)
@preconcurrency public import struct Foundation.URL
#else
public import struct Foundation.URL
#endif

/// OpenAPI client middleware that adds Anthropic API authentication headers.
///
/// Injects the `x-api-key` and `anthropic-version` headers into every
/// outgoing request before forwarding it to the next handler in the chain.
public struct AuthenticationMiddleware: ClientMiddleware {
/// The Anthropic API version sent with every request.
private static let anthropicVersion = "2023-06-01"

/// The `x-api-key` header name, constructed once. `"x-api-key"` is a valid
/// RFC 9110 token, so this is always non-`nil`.
private static let apiKeyFieldName = HTTPField.Name("x-api-key")

/// The `anthropic-version` header name, constructed once. `"anthropic-version"`
/// is a valid RFC 9110 token, so this is always non-`nil`.
private static let anthropicVersionFieldName = HTTPField.Name("anthropic-version")

/// The Anthropic API key used to authenticate requests.
private let apiKey: String

/// Creates a middleware that authenticates requests with the given API key.
/// - Parameter apiKey: The Anthropic API key to send in the `x-api-key` header.
public init(apiKey: String) {
self.apiKey = apiKey
}

/// Adds the Anthropic authentication headers, then forwards the request.
///
/// Any `x-api-key` or `anthropic-version` header already present on the
/// request is silently overwritten with this middleware's values.
/// - Parameters:
/// - request: The outgoing HTTP request.
/// - body: The outgoing HTTP request body, if any.
/// - baseURL: The base URL of the server.
/// - operationID: The OpenAPI operation identifier for the request.
/// - next: The next handler in the middleware chain.
/// - Returns: The HTTP response and body from the next handler.
/// - Throws: Any error thrown by the `next` handler.
public func intercept(
_ request: HTTPRequest,
body: HTTPBody?,
baseURL: URL,
operationID: String,
next: @Sendable (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)
) async throws -> (HTTPResponse, HTTPBody?) {
var request = request
if let apiKeyFieldName = Self.apiKeyFieldName {
request.headerFields[apiKeyFieldName] = apiKey
} else {
assertionFailure("\"x-api-key\" is a valid header name and must never be nil.")
}
if let anthropicVersionFieldName = Self.anthropicVersionFieldName {
request.headerFields[anthropicVersionFieldName] = Self.anthropicVersion
} else {
assertionFailure("\"anthropic-version\" is a valid header name and must never be nil.")
}
return try await next(request, body, baseURL)
}
}
42 changes: 42 additions & 0 deletions Sources/SyntaxKit/Core/Architecture.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
//
// Architecture.swift
// SyntaxKit
//
// Created by Leo Dion.
// Copyright © 2026 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

/// CPU-architecture identifiers used in `arch(...)` checks.
public enum Architecture: String, Sendable {
/// `arch(arm64)`
case arm64
/// `arch(x86_64)`
case x86 = "x86_64"
/// `arch(i386)`
case i386
/// `arch(arm)`
case arm
/// `arch(wasm32)`
case wasm32
}
43 changes: 43 additions & 0 deletions Sources/SyntaxKit/Core/Comparison.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
//
// Comparison.swift
// SyntaxKit
//
// Created by Leo Dion.
// Copyright © 2026 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

/// The comparison operator used between a keyword and a version in a
/// `swift(...)` / `compiler(...)` version check.
public enum Comparison: String, Sendable {
/// `>=`
case greaterThanOrEqual = ">="
/// `>`
case greaterThan = ">"
/// `<=`
case lessThanOrEqual = "<="
/// `<`
case lessThan = "<"
/// `==`
case equal = "=="
}
54 changes: 54 additions & 0 deletions Sources/SyntaxKit/Core/OperatingSystem.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
//
// OperatingSystem.swift
// SyntaxKit
//
// Created by Leo Dion.
// Copyright © 2026 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

/// Operating-system identifiers used in `os(...)` checks.
public enum OperatingSystem: String, Sendable {
/// `os(iOS)`
case iOS
/// `os(macOS)`
case macOS
/// `os(tvOS)`
case tvOS
/// `os(watchOS)`
case watchOS
/// `os(visionOS)`
case visionOS
/// `os(anyAppleOS)` — matches any Apple operating system (Swift 6.4+).
case anyAppleOS
/// `os(Linux)`
case linux = "Linux"
/// `os(Windows)`
case windows = "Windows"
/// `os(FreeBSD)`
case freeBSD = "FreeBSD"
/// `os(Android)`
case android = "Android"
/// `os(WASI)`
case wasi = "WASI"
}
36 changes: 36 additions & 0 deletions Sources/SyntaxKit/Core/TargetEnvironment.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
//
// TargetEnvironment.swift
// SyntaxKit
//
// Created by Leo Dion.
// Copyright © 2026 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

/// Target-environment identifiers used in `targetEnvironment(...)` checks.
public enum TargetEnvironment: String, Sendable {
/// `targetEnvironment(simulator)`
case simulator
/// `targetEnvironment(macCatalyst)`
case macCatalyst
}
64 changes: 64 additions & 0 deletions Sources/SyntaxKit/Core/Version.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
//
// Version.swift
// SyntaxKit
//
// Created by Leo Dion.
// Copyright © 2026 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

/// A `major.minor.patch` version, with optional minor and patch components.
public struct Version: Sendable, CustomStringConvertible {
/// The major version component.
public let major: Int
/// The optional minor version component.
public let minor: Int?
/// The optional patch version component.
public let patch: Int?

/// The dotted string form, e.g. `5`, `5.9`, or `5.9.1`.
public var description: String {
var result = String(major)
if let minor = minor {
result += ".\(minor)"
if let patch = patch {
result += ".\(patch)"
}
}
return result
}

/// The dotted string form, e.g. `5`, `5.9`, or `5.9.1`.
internal var versionString: String { description }

/// Create a version from its components.
/// - Parameters:
/// - major: The major version component.
/// - minor: The optional minor version component.
/// - patch: The optional patch version component.
public init(_ major: Int, _ minor: Int? = nil, _ patch: Int? = nil) {
self.major = major
self.minor = minor
self.patch = patch
}
}
Loading
Loading