From f2de31966ad6dc1bdeddf0c0a07da9454ae7b004 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Fri, 9 Oct 2026 16:06:55 +0800 Subject: [PATCH 01/18] Add Foundation Models 27 request options --- README.md | 27 +++++++ Sources/AnyLanguageModel/ContextOptions.swift | 27 +++++++ .../AnyLanguageModel/GenerationOptions.swift | 80 ++++++++++++++++++- .../Models/SystemLanguageModel.swift | 15 ++++ ...undationModelsGenerationOptionsTests.swift | 36 +++++++++ .../RequestOptionsTests.swift | 66 +++++++++++++++ 6 files changed, 247 insertions(+), 4 deletions(-) create mode 100644 Sources/AnyLanguageModel/ContextOptions.swift create mode 100644 Tests/AnyLanguageModelTests/RequestOptionsTests.swift diff --git a/README.md b/README.md index 5a969f0b..37d7d245 100644 --- a/README.md +++ b/README.md @@ -487,6 +487,28 @@ but never become part of the session's transcript. > Otherwise, it throws `SystemLanguageModel.Error.dynamicInstructionsUnavailable` > for a session with dynamic instructions. +### Foundation Models 27 request options + +`ContextOptions` mirrors Foundation Models 27's schema-in-prompt and reasoning-level +settings for compatibility-layer request construction. `GenerationOptions` also +provides the OS 27 `samplingMode` spelling and `ToolCallingMode` values: + +```swift +let context = ContextOptions( + includeSchemaInPrompt: true, + reasoningLevel: .moderate +) +let options = GenerationOptions( + samplingMode: .greedy, + toolCallingMode: .required +) +``` + +The older `sampling` property and initializer remain source-compatible. +Tool-calling mode is preserved in Codable transcripts and forwarded by the +Foundation Models bridge on OS 27. Other model providers can read the same +option from their request's `GenerationOptions`. + ### Reasoning in the transcript Reasoning is transcript content, separate from the answer in `response.content`. @@ -626,6 +648,11 @@ say which API they follow. - `DynamicInstructions`, its builder, and `LanguageModelSession.init(model:dynamicInstructions:history:)`: [dynamic instructions](#dynamic-instructions), which follow the Foundation Models 27 API. +- `ContextOptions`, `GenerationOptions.samplingMode`, and + `GenerationOptions.ToolCallingMode`: + [request options](#foundation-models-27-request-options), which follow the + Foundation Models 27 API while remaining available on the package's existing + deployment targets. - `Transcript.Entry.reasoning` and `Transcript.Reasoning`: [reasoning in the transcript](#reasoning-in-the-transcript), which follows the Foundation Models 27 API. diff --git a/Sources/AnyLanguageModel/ContextOptions.swift b/Sources/AnyLanguageModel/ContextOptions.swift new file mode 100644 index 00000000..f63020c3 --- /dev/null +++ b/Sources/AnyLanguageModel/ContextOptions.swift @@ -0,0 +1,27 @@ +/// Options that configure a model's request context. +/// +/// This mirrors the Foundation Models 27 request-context surface while keeping +/// the package available on its existing deployment targets. +public struct ContextOptions: Sendable, Equatable { + /// The amount of reasoning the model should perform for a request. + public enum ReasoningLevel: Sendable, Equatable { + case light + case moderate + case deep + case custom(String) + } + + /// Whether a structured-generation schema should also appear in the prompt. + public var includeSchemaInPrompt: Bool? + + /// The amount of reasoning the model should perform. + public var reasoningLevel: ReasoningLevel? + + public init( + includeSchemaInPrompt: Bool? = nil, + reasoningLevel: ReasoningLevel? = nil + ) { + self.includeSchemaInPrompt = includeSchemaInPrompt + self.reasoningLevel = reasoningLevel + } +} diff --git a/Sources/AnyLanguageModel/GenerationOptions.swift b/Sources/AnyLanguageModel/GenerationOptions.swift index cd2352a9..f02e7fb6 100644 --- a/Sources/AnyLanguageModel/GenerationOptions.swift +++ b/Sources/AnyLanguageModel/GenerationOptions.swift @@ -93,6 +93,15 @@ public struct GenerationOptions: Sendable, Equatable { /// a reasonable default on your behalf. public var sampling: SamplingMode? + /// The sampling strategy for generation. + /// + /// This is the Foundation Models 27 spelling. ``sampling`` remains for + /// source compatibility with earlier AnyLanguageModel releases. + public var samplingMode: SamplingMode? { + get { sampling } + set { sampling = newValue } + } + /// Temperature influences the confidence of the models response. /// /// The value of this property must be a number between `0` and `1` inclusive. @@ -121,6 +130,9 @@ public struct GenerationOptions: Sendable, Equatable { /// an error will be thrown. public var maximumResponseTokens: Int? + /// Whether tool calling is allowed, required, or disallowed. + public var toolCallingMode: ToolCallingMode? + /// Storage for model-specific custom options. private var customOptionsStorage: CustomOptionsStorage = .init() @@ -169,6 +181,43 @@ public struct GenerationOptions: Sendable, Equatable { self.sampling = sampling self.temperature = temperature self.maximumResponseTokens = maximumResponseTokens + self.toolCallingMode = nil + } + + /// Creates Foundation Models 27-style generation options. + public init( + samplingMode: SamplingMode? = nil, + temperature: Double? = nil, + maximumResponseTokens: Int? = nil, + toolCallingMode: ToolCallingMode? + ) { + self.init( + sampling: samplingMode, + temperature: temperature, + maximumResponseTokens: maximumResponseTokens + ) + self.toolCallingMode = toolCallingMode + } +} + +extension GenerationOptions { + /// A tool-calling policy for a generation request. + public struct ToolCallingMode: Sendable, Equatable { + public enum Kind: Sendable, Equatable, Hashable { + case allowed + case required + case disallowed + } + + public var kind: Kind + + private init(kind: Kind) { + self.kind = kind + } + + public static let allowed = Self(kind: .allowed) + public static let required = Self(kind: .required) + public static let disallowed = Self(kind: .disallowed) } } @@ -180,7 +229,8 @@ extension GenerationOptions { /// `GenerationOptions` isn't `Codable`, matching Foundation Models, /// but ``Transcript/Prompt`` is, so it codes its options through this type. /// It codes the sampling mode, temperature, and maximum response tokens - /// in the same format as earlier releases. + /// in the same format as earlier releases, and adds the optional OS 27 + /// tool-calling mode without changing older transcript payloads. /// Custom options aren't coded, /// because decoding them would need a registry of every model's option types. struct TranscriptCoding: Codable { @@ -188,21 +238,43 @@ extension GenerationOptions { var mode: SamplingMode.Mode } + enum ToolCallingModeCoding: String, Codable { + case allowed + case required + case disallowed + } + var sampling: Sampling? var temperature: Double? var maximumResponseTokens: Int? + var toolCallingMode: ToolCallingModeCoding? init(_ options: GenerationOptions) { self.sampling = options.sampling.map { Sampling(mode: $0.mode) } self.temperature = options.temperature self.maximumResponseTokens = options.maximumResponseTokens + self.toolCallingMode = + switch options.toolCallingMode?.kind { + case .allowed: .allowed + case .required: .required + case .disallowed: .disallowed + case nil: nil + } } var options: GenerationOptions { - GenerationOptions( - sampling: sampling.map { SamplingMode(mode: $0.mode) }, + let toolCallingMode: GenerationOptions.ToolCallingMode? = + switch toolCallingMode { + case .allowed: .allowed + case .required: .required + case .disallowed: .disallowed + case nil: nil + } + return GenerationOptions( + samplingMode: sampling.map { SamplingMode(mode: $0.mode) }, temperature: temperature, - maximumResponseTokens: maximumResponseTokens + maximumResponseTokens: maximumResponseTokens, + toolCallingMode: toolCallingMode ) } } diff --git a/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift b/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift index 87763ed6..86746df1 100644 --- a/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift @@ -458,6 +458,21 @@ } #if compiler(>=6.4) && !os(tvOS) + if #available(macOS 27, iOS 27, visionOS 27, watchOS 27, *) { + let toolCallingMode: FoundationModels.GenerationOptions.ToolCallingMode? = + switch self.toolCallingMode?.kind { + case .allowed: .allowed + case .required: .required + case .disallowed: .disallowed + case nil: nil + } + return FoundationModels.GenerationOptions( + samplingMode: sampling, + temperature: temperature, + maximumResponseTokens: maximumResponseTokens, + toolCallingMode: toolCallingMode + ) + } return FoundationModels.GenerationOptions( samplingMode: sampling, temperature: temperature, diff --git a/Tests/AnyLanguageModelTests/FoundationModelsGenerationOptionsTests.swift b/Tests/AnyLanguageModelTests/FoundationModelsGenerationOptionsTests.swift index 06c723fc..976903f5 100644 --- a/Tests/AnyLanguageModelTests/FoundationModelsGenerationOptionsTests.swift +++ b/Tests/AnyLanguageModelTests/FoundationModelsGenerationOptionsTests.swift @@ -67,6 +67,42 @@ import Testing #endif #expect(converted.temperature == nil) #expect(converted.maximumResponseTokens == nil) + #if compiler(>=6.4) && !os(tvOS) + if #available(macOS 27, iOS 27, visionOS 27, watchOS 27, *) { + #expect(converted.toolCallingMode == nil) + } + #endif } + + #if compiler(>=6.4) && !os(tvOS) + @available(macOS 27, iOS 27, visionOS 27, watchOS 27, *) + @Test( + "Forwards tool calling modes", + arguments: [ + ( + AnyLanguageModel.GenerationOptions.ToolCallingMode.allowed, + FoundationModels.GenerationOptions.ToolCallingMode.Kind.allowed + ), + ( + AnyLanguageModel.GenerationOptions.ToolCallingMode.required, + FoundationModels.GenerationOptions.ToolCallingMode.Kind.required + ), + ( + AnyLanguageModel.GenerationOptions.ToolCallingMode.disallowed, + FoundationModels.GenerationOptions.ToolCallingMode.Kind.disallowed + ), + ] + ) + func forwardsToolCallingMode( + mode: AnyLanguageModel.GenerationOptions.ToolCallingMode, + expected: FoundationModels.GenerationOptions.ToolCallingMode.Kind + ) { + let converted = AnyLanguageModel.GenerationOptions( + toolCallingMode: mode + ).toFoundationModels() + + #expect(converted.toolCallingMode?.kind == expected) + } + #endif } #endif diff --git a/Tests/AnyLanguageModelTests/RequestOptionsTests.swift b/Tests/AnyLanguageModelTests/RequestOptionsTests.swift new file mode 100644 index 00000000..7822a614 --- /dev/null +++ b/Tests/AnyLanguageModelTests/RequestOptionsTests.swift @@ -0,0 +1,66 @@ +import Foundation +import Testing + +@testable import AnyLanguageModel + +@Suite("Foundation Models 27 request options") +struct RequestOptionsTests { + @Test func contextOptionsRetainValues() { + #expect(ContextOptions() == ContextOptions()) + #expect( + ContextOptions(includeSchemaInPrompt: true, reasoningLevel: .deep) + == ContextOptions(includeSchemaInPrompt: true, reasoningLevel: .deep) + ) + #expect( + ContextOptions(reasoningLevel: .custom("provider-level")) + != ContextOptions(reasoningLevel: .moderate) + ) + } + + @Test( + arguments: [ + (GenerationOptions.ToolCallingMode.allowed, GenerationOptions.ToolCallingMode.Kind.allowed), + (.required, .required), + (.disallowed, .disallowed), + ] + ) + func toolCallingModesExposeTheirKind( + mode: GenerationOptions.ToolCallingMode, + expected: GenerationOptions.ToolCallingMode.Kind + ) { + #expect(mode.kind == expected) + } + + @Test func generationOptionsKeepOldAndNewSamplingSpellingsInSync() { + var options = GenerationOptions(samplingMode: .greedy, toolCallingMode: .required) + #expect(options.sampling == .greedy) + #expect(options.toolCallingMode == .required) + + options.sampling = .random(top: 4, seed: 7) + #expect(options.samplingMode == .random(top: 4, seed: 7)) + + options.samplingMode = .random(probabilityThreshold: 0.8, seed: 9) + #expect(options.sampling == .random(probabilityThreshold: 0.8, seed: 9)) + } + + @Test func transcriptRoundTripsToolCallingMode() throws { + let options = GenerationOptions( + samplingMode: .greedy, + temperature: 0.5, + maximumResponseTokens: 64, + toolCallingMode: .required + ) + let prompt = Transcript.Prompt( + id: "prompt", + segments: [.text(.init(id: "text", content: "Hello"))], + options: options + ) + + let encoder = JSONEncoder() + encoder.outputFormatting = .sortedKeys + let data = try encoder.encode(prompt) + #expect(String(decoding: data, as: UTF8.self).contains(#""toolCallingMode":"required""#)) + let decoded = try JSONDecoder().decode(Transcript.Prompt.self, from: data) + #expect(decoded.options == options) + } +} From cd1d6fcbcf8fe45ec8b7d779cdc3fe4727337e68 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Sun, 11 Oct 2026 22:17:19 +0800 Subject: [PATCH 02/18] Add session properties to dynamic instructions --- .../LanguageModelSession.swift | 32 ++++- .../AnyLanguageModel/SessionProperties.swift | 114 ++++++++++++++++ Sources/AnyLanguageModelMacros/Plugin.swift | 1 + .../SessionPropertyEntryMacro.swift | 53 ++++++++ ...PICompatibilityAnyLanguageModelTests.swift | 12 +- ...PICompatibilityFoundationModelsTests.swift | 12 +- .../SessionPropertyTests.swift | 127 ++++++++++++++++++ 7 files changed, 345 insertions(+), 6 deletions(-) create mode 100644 Sources/AnyLanguageModel/SessionProperties.swift create mode 100644 Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift create mode 100644 Tests/AnyLanguageModelTests/SessionPropertyTests.swift diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 4f08efc6..10627f0d 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -110,6 +110,9 @@ public final class LanguageModelSession: @unchecked Sendable { private let dynamicInstructions: AnyDynamicInstructions? private let dynamicInstructionsLock = NSLock() + /// Session-scoped values available to dynamic instructions. + public let properties: SessionPropertyValues + /// Whether the session resolves its instructions and tools before each request. nonisolated var usesDynamicInstructions: Bool { dynamicInstructions != nil @@ -172,8 +175,13 @@ public final class LanguageModelSession: @unchecked Sendable { // Evaluate the body for this request. // The resolved instructions go into the request's transcript, // never into the session's. + let canonicalHistory = Array(transcript) let resolved = dynamicInstructionsLock.withLock { - dynamicInstructions.resolveForRequest() + SessionPropertyBinding.$values.withValue(properties) { + SessionPropertyBinding.$history.withValue(canonicalHistory) { + dynamicInstructions.resolveForRequest() + } + } } var requestTranscript = transcript if let instructions = resolved.instructions { @@ -322,7 +330,27 @@ public final class LanguageModelSession: @unchecked Sendable { } } - self.state = .init(.init(finalTranscript)) + let state = Locked(State(finalTranscript)) + self.state = state + self.properties = SessionPropertyValues( + historyGetter: { + state.withLock { locked in + locked.transcript.filter { entry in + if case .instructions = entry { return false } + return true + } + } + }, + historySetter: { history in + state.withLock { locked in + let instructions = locked.transcript.prefix { + if case .instructions = $0 { return true } + return false + } + locked.transcript = Transcript(entries: Array(instructions) + history) + } + } + ) } private static func instructions(from transcript: Transcript) -> Instructions? { diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift new file mode 100644 index 00000000..9b503bff --- /dev/null +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -0,0 +1,114 @@ +import Foundation +import Observation + +extension Transcript { + /// The mutable, instruction-free history exposed through session properties. + /// + /// - Note: This API is exclusive to AnyLanguageModel on OS 26. It mirrors + /// Foundation Models 27's `Transcript.HistoryView` role with value semantics. + public typealias HistoryView = [Entry] +} + +/// A type-keyed session property. +/// +/// - Note: This API is exclusive to AnyLanguageModel on OS 26 and mirrors the +/// Foundation Models 27 session-property contract. +public protocol SessionPropertyKey: SendableMetatype { + associatedtype Value + static var defaultValue: Value { get } +} + +/// Declares a stored value on ``SessionPropertyValues``. +@attached(accessor) +@attached(peer, names: prefixed(__Key_)) +public macro SessionPropertyEntry() = + #externalMacro(module: "AnyLanguageModelMacros", type: "SessionPropertyEntryMacro") + +/// Session-scoped storage available while dynamic instructions are evaluated. +/// +/// - Note: This API is exclusive to AnyLanguageModel on OS 26 and mirrors +/// Foundation Models 27. +@Observable +public final class SessionPropertyValues: @unchecked Sendable { + @ObservationIgnored private let customValues = Locked<[ObjectIdentifier: Any]>([:]) + @ObservationIgnored private let historyGetter: @Sendable () -> Transcript.HistoryView + @ObservationIgnored private let historySetter: @Sendable (Transcript.HistoryView) -> Void + + init( + historyGetter: @escaping @Sendable () -> Transcript.HistoryView, + historySetter: @escaping @Sendable (Transcript.HistoryView) -> Void + ) { + self.historyGetter = historyGetter + self.historySetter = historySetter + } + + /// The session history without an instructions entry. + public var history: Transcript.HistoryView { + get { SessionPropertyBinding.history ?? historyGetter() } + set { + precondition( + SessionPropertyBinding.history == nil, + "Session history is read-only while dynamic instructions are active" + ) + historySetter(newValue) + } + } + + public subscript(key: Key.Type) -> Key.Value where Key: SessionPropertyKey { + get { + customValues.withLock { values in + if let value = values[ObjectIdentifier(key)] as? Key.Value { return value } + let value = Key.defaultValue + values[ObjectIdentifier(key)] = value + return value + } + } + set { + customValues.withLock { values in + values[ObjectIdentifier(key)] = newValue + } + } + } +} + +enum SessionPropertyBinding { + @TaskLocal static var values: SessionPropertyValues? + @TaskLocal static var history: Transcript.HistoryView? +} + +extension LanguageModelSession { + /// Accesses a value in the currently evaluating session. + @propertyWrapper + public struct SessionProperty { + private let keyPath: ReferenceWritableKeyPath + + public init(_ keyPath: ReferenceWritableKeyPath) { + self.keyPath = keyPath + } + + public var wrappedValue: Value { + get { + guard let values = SessionPropertyBinding.values else { + preconditionFailure( + "SessionProperty can only be accessed while dynamic instructions are active" + ) + } + return values[keyPath: keyPath] + } + nonmutating set { + guard let values = SessionPropertyBinding.values else { + preconditionFailure( + "SessionProperty can only be accessed while dynamic instructions are active" + ) + } + values[keyPath: keyPath] = newValue + } + } + } +} + +extension LanguageModelSession.SessionProperty: @unchecked Sendable where Value: Sendable {} + +extension DynamicInstructions { + public typealias SessionProperty = LanguageModelSession.SessionProperty +} diff --git a/Sources/AnyLanguageModelMacros/Plugin.swift b/Sources/AnyLanguageModelMacros/Plugin.swift index 917d2deb..dee43755 100644 --- a/Sources/AnyLanguageModelMacros/Plugin.swift +++ b/Sources/AnyLanguageModelMacros/Plugin.swift @@ -6,5 +6,6 @@ struct AnyLanguageModelMacrosPlugin: CompilerPlugin { let providingMacros: [any Macro.Type] = [ GenerableMacro.self, GuideMacro.self, + SessionPropertyEntryMacro.self, ] } diff --git a/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift b/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift new file mode 100644 index 00000000..1aae689b --- /dev/null +++ b/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift @@ -0,0 +1,53 @@ +import SwiftSyntax +import SwiftSyntaxBuilder +import SwiftSyntaxMacros + +/// Synthesizes a `SessionPropertyKey` and forwards the declared property through +/// `SessionPropertyValues`' type-keyed storage. +public struct SessionPropertyEntryMacro: AccessorMacro, PeerMacro { + public static func expansion( + of node: AttributeSyntax, + providingAccessorsOf declaration: some DeclSyntaxProtocol, + in context: some MacroExpansionContext + ) throws -> [AccessorDeclSyntax] { + guard let name = propertyName(in: declaration) else { return [] } + return [ + "get { self[__Key_\(raw: name).self] }", + "set { self[__Key_\(raw: name).self] = newValue }", + ] + } + + public static func expansion( + of node: AttributeSyntax, + providingPeersOf declaration: some DeclSyntaxProtocol, + in context: some MacroExpansionContext + ) throws -> [DeclSyntax] { + guard + let variable = declaration.as(VariableDeclSyntax.self), + variable.bindings.count == 1, + let binding = variable.bindings.first, + let name = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text, + let initializer = binding.initializer?.value + else { return [] } + + return [ + """ + private struct __Key_\(raw: name): SessionPropertyKey { + static let defaultValue = \(initializer) + } + """ + ] + } + + private static func propertyName(in declaration: some DeclSyntaxProtocol) -> String? { + guard + let variable = declaration.as(VariableDeclSyntax.self), + variable.bindings.count == 1, + let binding = variable.bindings.first, + binding.accessorBlock == nil, + let name = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text, + binding.initializer != nil + else { return nil } + return name + } +} diff --git a/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift b/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift index 18bf1cba..2afe9d3e 100644 --- a/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift +++ b/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift @@ -10,12 +10,18 @@ import Testing return false }() + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) + extension SessionPropertyValues { + @SessionPropertyEntry var compatibilityRevision = 0 + } + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) private struct CompatibilityDynamicInstructions: DynamicInstructions { let includeDetail: Bool + @SessionProperty(\.compatibilityRevision) private var revision var body: some DynamicInstructions { - Instructions("You are a helpful assistant.") + Instructions("You are a helpful assistant. Revision \(revision).") if includeDetail { Instructions("Include useful detail.") } @@ -32,11 +38,13 @@ import Testing ) if #available(macOS 27.0, iOS 27.0, visionOS 27.0, *) { - _ = LanguageModelSession( + let dynamicSession = LanguageModelSession( model: model, dynamicInstructions: CompatibilityDynamicInstructions(includeDetail: true), history: session.transcript ) + dynamicSession.properties.compatibilityRevision = 1 + _ = dynamicSession.properties.history } let options = GenerationOptions(temperature: 0.7) diff --git a/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift b/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift index 8142648e..d8d0c51e 100644 --- a/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift +++ b/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift @@ -12,12 +12,18 @@ import Testing #if compiler(>=6.4) #if os(macOS) || os(iOS) || os(visionOS) + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) + extension SessionPropertyValues { + @SessionPropertyEntry var compatibilityRevision = 0 + } + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) private struct CompatibilityDynamicInstructions: DynamicInstructions { let includeDetail: Bool + @SessionProperty(\.compatibilityRevision) private var revision var body: some DynamicInstructions { - Instructions("You are a helpful assistant.") + Instructions("You are a helpful assistant. Revision \(revision).") if includeDetail { Instructions("Include useful detail.") } @@ -41,11 +47,13 @@ import Testing #if compiler(>=6.4) #if os(macOS) || os(iOS) || os(visionOS) if #available(macOS 27.0, iOS 27.0, visionOS 27.0, *) { - _ = LanguageModelSession( + let dynamicSession = LanguageModelSession( model: model, dynamicInstructions: CompatibilityDynamicInstructions(includeDetail: true), history: session.transcript ) + dynamicSession.properties.compatibilityRevision = 1 + _ = dynamicSession.properties.history } #endif #endif diff --git a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift new file mode 100644 index 00000000..d35425b8 --- /dev/null +++ b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift @@ -0,0 +1,127 @@ +import Foundation +import Observation +import Testing + +@testable import AnyLanguageModel + +extension SessionPropertyValues { + @SessionPropertyEntry var revision = 0 +} + +@Suite("Session properties") +struct SessionPropertyTests { + @Test func dynamicInstructionsReadAndUpdateSessionProperties() { + let session = LanguageModelSession( + model: SessionPropertyFixtureModel(), + dynamicInstructions: SessionPropertyFixtureInstructions() + ) + + #expect(session.properties.revision == 0) + let first = session.resolvedRequestContext() + let second = session.resolvedRequestContext() + + #expect(first.instructions?.description == "Revision 0") + #expect(second.instructions?.description == "Revision 1") + #expect(session.properties.revision == 2) + } + + @Test func historyIsInstructionFreeAndUsesTheCurrentRequestSnapshot() { + let prompt = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Earlier"))]) + ) + let session = LanguageModelSession( + model: SessionPropertyFixtureModel(), + dynamicInstructions: SessionPropertyHistoryInstructions(), + history: [prompt] + ) + + let context = session.resolvedRequestContext() + + #expect(context.instructions?.description == "History count 1") + #expect(context.transcript.count == 2) + #expect(session.properties.history == [prompt]) + } + + @Test func propertiesAndHistoryAreIsolatedBetweenSessions() { + let first = LanguageModelSession(model: SessionPropertyFixtureModel()) + let second = LanguageModelSession(model: SessionPropertyFixtureModel()) + let prompt = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Replacement"))]) + ) + + first.properties.revision = 7 + first.properties.history = [prompt] + + #expect(first.properties.revision == 7) + #expect(first.transcript == Transcript(entries: [prompt])) + #expect(second.properties.revision == 0) + #expect(second.transcript.isEmpty) + let _: any Observable = first.properties + } + + @Test func replacingHistoryPreservesStaticInstructions() { + let session = LanguageModelSession( + model: SessionPropertyFixtureModel(), + instructions: "Keep these instructions" + ) + let prompt = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Replacement"))]) + ) + + session.properties.history = [prompt] + + #expect(session.properties.history == [prompt]) + #expect(session.transcript.count == 2) + guard case .instructions = session.transcript[0] else { + Issue.record("Expected the static instructions entry to remain first") + return + } + #expect(session.transcript[1] == prompt) + } +} + +private struct SessionPropertyFixtureInstructions: DynamicInstructions { + @SessionProperty(\.revision) private var revision + + var body: some DynamicInstructions { + let currentRevision = revision + revision += 1 + return Instructions("Revision \(currentRevision)") + } +} + +private struct SessionPropertyHistoryInstructions: DynamicInstructions { + @SessionProperty(\.history) private var history + + var body: some DynamicInstructions { + Instructions("History count \(history.count)") + } +} + +private struct SessionPropertyFixtureModel: LanguageModel { + typealias UnavailableReason = Never + + var availability: Availability { .available } + + func supports(locale: Locale) -> Bool { true } + + func respond( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) async throws -> LanguageModelSession.Response where Content: Generable { + fatalError("SessionPropertyFixtureModel does not generate responses") + } + + func streamResponse( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) -> sending LanguageModelSession.ResponseStream where Content: Generable { + fatalError("SessionPropertyFixtureModel does not generate responses") + } +} From 94813809b5f362e93cec22f5ff2e36ff5ae6fd29 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Sun, 11 Oct 2026 22:17:19 +0800 Subject: [PATCH 03/18] Add session properties to dynamic instructions --- .../LanguageModelSession.swift | 32 ++++- .../AnyLanguageModel/SessionProperties.swift | 114 ++++++++++++++++ Sources/AnyLanguageModelMacros/Plugin.swift | 1 + .../SessionPropertyEntryMacro.swift | 53 ++++++++ ...PICompatibilityAnyLanguageModelTests.swift | 12 +- ...PICompatibilityFoundationModelsTests.swift | 12 +- .../SessionPropertyTests.swift | 127 ++++++++++++++++++ 7 files changed, 345 insertions(+), 6 deletions(-) create mode 100644 Sources/AnyLanguageModel/SessionProperties.swift create mode 100644 Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift create mode 100644 Tests/AnyLanguageModelTests/SessionPropertyTests.swift diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 4f08efc6..10627f0d 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -110,6 +110,9 @@ public final class LanguageModelSession: @unchecked Sendable { private let dynamicInstructions: AnyDynamicInstructions? private let dynamicInstructionsLock = NSLock() + /// Session-scoped values available to dynamic instructions. + public let properties: SessionPropertyValues + /// Whether the session resolves its instructions and tools before each request. nonisolated var usesDynamicInstructions: Bool { dynamicInstructions != nil @@ -172,8 +175,13 @@ public final class LanguageModelSession: @unchecked Sendable { // Evaluate the body for this request. // The resolved instructions go into the request's transcript, // never into the session's. + let canonicalHistory = Array(transcript) let resolved = dynamicInstructionsLock.withLock { - dynamicInstructions.resolveForRequest() + SessionPropertyBinding.$values.withValue(properties) { + SessionPropertyBinding.$history.withValue(canonicalHistory) { + dynamicInstructions.resolveForRequest() + } + } } var requestTranscript = transcript if let instructions = resolved.instructions { @@ -322,7 +330,27 @@ public final class LanguageModelSession: @unchecked Sendable { } } - self.state = .init(.init(finalTranscript)) + let state = Locked(State(finalTranscript)) + self.state = state + self.properties = SessionPropertyValues( + historyGetter: { + state.withLock { locked in + locked.transcript.filter { entry in + if case .instructions = entry { return false } + return true + } + } + }, + historySetter: { history in + state.withLock { locked in + let instructions = locked.transcript.prefix { + if case .instructions = $0 { return true } + return false + } + locked.transcript = Transcript(entries: Array(instructions) + history) + } + } + ) } private static func instructions(from transcript: Transcript) -> Instructions? { diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift new file mode 100644 index 00000000..9b503bff --- /dev/null +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -0,0 +1,114 @@ +import Foundation +import Observation + +extension Transcript { + /// The mutable, instruction-free history exposed through session properties. + /// + /// - Note: This API is exclusive to AnyLanguageModel on OS 26. It mirrors + /// Foundation Models 27's `Transcript.HistoryView` role with value semantics. + public typealias HistoryView = [Entry] +} + +/// A type-keyed session property. +/// +/// - Note: This API is exclusive to AnyLanguageModel on OS 26 and mirrors the +/// Foundation Models 27 session-property contract. +public protocol SessionPropertyKey: SendableMetatype { + associatedtype Value + static var defaultValue: Value { get } +} + +/// Declares a stored value on ``SessionPropertyValues``. +@attached(accessor) +@attached(peer, names: prefixed(__Key_)) +public macro SessionPropertyEntry() = + #externalMacro(module: "AnyLanguageModelMacros", type: "SessionPropertyEntryMacro") + +/// Session-scoped storage available while dynamic instructions are evaluated. +/// +/// - Note: This API is exclusive to AnyLanguageModel on OS 26 and mirrors +/// Foundation Models 27. +@Observable +public final class SessionPropertyValues: @unchecked Sendable { + @ObservationIgnored private let customValues = Locked<[ObjectIdentifier: Any]>([:]) + @ObservationIgnored private let historyGetter: @Sendable () -> Transcript.HistoryView + @ObservationIgnored private let historySetter: @Sendable (Transcript.HistoryView) -> Void + + init( + historyGetter: @escaping @Sendable () -> Transcript.HistoryView, + historySetter: @escaping @Sendable (Transcript.HistoryView) -> Void + ) { + self.historyGetter = historyGetter + self.historySetter = historySetter + } + + /// The session history without an instructions entry. + public var history: Transcript.HistoryView { + get { SessionPropertyBinding.history ?? historyGetter() } + set { + precondition( + SessionPropertyBinding.history == nil, + "Session history is read-only while dynamic instructions are active" + ) + historySetter(newValue) + } + } + + public subscript(key: Key.Type) -> Key.Value where Key: SessionPropertyKey { + get { + customValues.withLock { values in + if let value = values[ObjectIdentifier(key)] as? Key.Value { return value } + let value = Key.defaultValue + values[ObjectIdentifier(key)] = value + return value + } + } + set { + customValues.withLock { values in + values[ObjectIdentifier(key)] = newValue + } + } + } +} + +enum SessionPropertyBinding { + @TaskLocal static var values: SessionPropertyValues? + @TaskLocal static var history: Transcript.HistoryView? +} + +extension LanguageModelSession { + /// Accesses a value in the currently evaluating session. + @propertyWrapper + public struct SessionProperty { + private let keyPath: ReferenceWritableKeyPath + + public init(_ keyPath: ReferenceWritableKeyPath) { + self.keyPath = keyPath + } + + public var wrappedValue: Value { + get { + guard let values = SessionPropertyBinding.values else { + preconditionFailure( + "SessionProperty can only be accessed while dynamic instructions are active" + ) + } + return values[keyPath: keyPath] + } + nonmutating set { + guard let values = SessionPropertyBinding.values else { + preconditionFailure( + "SessionProperty can only be accessed while dynamic instructions are active" + ) + } + values[keyPath: keyPath] = newValue + } + } + } +} + +extension LanguageModelSession.SessionProperty: @unchecked Sendable where Value: Sendable {} + +extension DynamicInstructions { + public typealias SessionProperty = LanguageModelSession.SessionProperty +} diff --git a/Sources/AnyLanguageModelMacros/Plugin.swift b/Sources/AnyLanguageModelMacros/Plugin.swift index 917d2deb..dee43755 100644 --- a/Sources/AnyLanguageModelMacros/Plugin.swift +++ b/Sources/AnyLanguageModelMacros/Plugin.swift @@ -6,5 +6,6 @@ struct AnyLanguageModelMacrosPlugin: CompilerPlugin { let providingMacros: [any Macro.Type] = [ GenerableMacro.self, GuideMacro.self, + SessionPropertyEntryMacro.self, ] } diff --git a/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift b/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift new file mode 100644 index 00000000..1aae689b --- /dev/null +++ b/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift @@ -0,0 +1,53 @@ +import SwiftSyntax +import SwiftSyntaxBuilder +import SwiftSyntaxMacros + +/// Synthesizes a `SessionPropertyKey` and forwards the declared property through +/// `SessionPropertyValues`' type-keyed storage. +public struct SessionPropertyEntryMacro: AccessorMacro, PeerMacro { + public static func expansion( + of node: AttributeSyntax, + providingAccessorsOf declaration: some DeclSyntaxProtocol, + in context: some MacroExpansionContext + ) throws -> [AccessorDeclSyntax] { + guard let name = propertyName(in: declaration) else { return [] } + return [ + "get { self[__Key_\(raw: name).self] }", + "set { self[__Key_\(raw: name).self] = newValue }", + ] + } + + public static func expansion( + of node: AttributeSyntax, + providingPeersOf declaration: some DeclSyntaxProtocol, + in context: some MacroExpansionContext + ) throws -> [DeclSyntax] { + guard + let variable = declaration.as(VariableDeclSyntax.self), + variable.bindings.count == 1, + let binding = variable.bindings.first, + let name = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text, + let initializer = binding.initializer?.value + else { return [] } + + return [ + """ + private struct __Key_\(raw: name): SessionPropertyKey { + static let defaultValue = \(initializer) + } + """ + ] + } + + private static func propertyName(in declaration: some DeclSyntaxProtocol) -> String? { + guard + let variable = declaration.as(VariableDeclSyntax.self), + variable.bindings.count == 1, + let binding = variable.bindings.first, + binding.accessorBlock == nil, + let name = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text, + binding.initializer != nil + else { return nil } + return name + } +} diff --git a/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift b/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift index 18bf1cba..2afe9d3e 100644 --- a/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift +++ b/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift @@ -10,12 +10,18 @@ import Testing return false }() + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) + extension SessionPropertyValues { + @SessionPropertyEntry var compatibilityRevision = 0 + } + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) private struct CompatibilityDynamicInstructions: DynamicInstructions { let includeDetail: Bool + @SessionProperty(\.compatibilityRevision) private var revision var body: some DynamicInstructions { - Instructions("You are a helpful assistant.") + Instructions("You are a helpful assistant. Revision \(revision).") if includeDetail { Instructions("Include useful detail.") } @@ -32,11 +38,13 @@ import Testing ) if #available(macOS 27.0, iOS 27.0, visionOS 27.0, *) { - _ = LanguageModelSession( + let dynamicSession = LanguageModelSession( model: model, dynamicInstructions: CompatibilityDynamicInstructions(includeDetail: true), history: session.transcript ) + dynamicSession.properties.compatibilityRevision = 1 + _ = dynamicSession.properties.history } let options = GenerationOptions(temperature: 0.7) diff --git a/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift b/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift index 8142648e..d8d0c51e 100644 --- a/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift +++ b/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift @@ -12,12 +12,18 @@ import Testing #if compiler(>=6.4) #if os(macOS) || os(iOS) || os(visionOS) + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) + extension SessionPropertyValues { + @SessionPropertyEntry var compatibilityRevision = 0 + } + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) private struct CompatibilityDynamicInstructions: DynamicInstructions { let includeDetail: Bool + @SessionProperty(\.compatibilityRevision) private var revision var body: some DynamicInstructions { - Instructions("You are a helpful assistant.") + Instructions("You are a helpful assistant. Revision \(revision).") if includeDetail { Instructions("Include useful detail.") } @@ -41,11 +47,13 @@ import Testing #if compiler(>=6.4) #if os(macOS) || os(iOS) || os(visionOS) if #available(macOS 27.0, iOS 27.0, visionOS 27.0, *) { - _ = LanguageModelSession( + let dynamicSession = LanguageModelSession( model: model, dynamicInstructions: CompatibilityDynamicInstructions(includeDetail: true), history: session.transcript ) + dynamicSession.properties.compatibilityRevision = 1 + _ = dynamicSession.properties.history } #endif #endif diff --git a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift new file mode 100644 index 00000000..d35425b8 --- /dev/null +++ b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift @@ -0,0 +1,127 @@ +import Foundation +import Observation +import Testing + +@testable import AnyLanguageModel + +extension SessionPropertyValues { + @SessionPropertyEntry var revision = 0 +} + +@Suite("Session properties") +struct SessionPropertyTests { + @Test func dynamicInstructionsReadAndUpdateSessionProperties() { + let session = LanguageModelSession( + model: SessionPropertyFixtureModel(), + dynamicInstructions: SessionPropertyFixtureInstructions() + ) + + #expect(session.properties.revision == 0) + let first = session.resolvedRequestContext() + let second = session.resolvedRequestContext() + + #expect(first.instructions?.description == "Revision 0") + #expect(second.instructions?.description == "Revision 1") + #expect(session.properties.revision == 2) + } + + @Test func historyIsInstructionFreeAndUsesTheCurrentRequestSnapshot() { + let prompt = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Earlier"))]) + ) + let session = LanguageModelSession( + model: SessionPropertyFixtureModel(), + dynamicInstructions: SessionPropertyHistoryInstructions(), + history: [prompt] + ) + + let context = session.resolvedRequestContext() + + #expect(context.instructions?.description == "History count 1") + #expect(context.transcript.count == 2) + #expect(session.properties.history == [prompt]) + } + + @Test func propertiesAndHistoryAreIsolatedBetweenSessions() { + let first = LanguageModelSession(model: SessionPropertyFixtureModel()) + let second = LanguageModelSession(model: SessionPropertyFixtureModel()) + let prompt = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Replacement"))]) + ) + + first.properties.revision = 7 + first.properties.history = [prompt] + + #expect(first.properties.revision == 7) + #expect(first.transcript == Transcript(entries: [prompt])) + #expect(second.properties.revision == 0) + #expect(second.transcript.isEmpty) + let _: any Observable = first.properties + } + + @Test func replacingHistoryPreservesStaticInstructions() { + let session = LanguageModelSession( + model: SessionPropertyFixtureModel(), + instructions: "Keep these instructions" + ) + let prompt = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Replacement"))]) + ) + + session.properties.history = [prompt] + + #expect(session.properties.history == [prompt]) + #expect(session.transcript.count == 2) + guard case .instructions = session.transcript[0] else { + Issue.record("Expected the static instructions entry to remain first") + return + } + #expect(session.transcript[1] == prompt) + } +} + +private struct SessionPropertyFixtureInstructions: DynamicInstructions { + @SessionProperty(\.revision) private var revision + + var body: some DynamicInstructions { + let currentRevision = revision + revision += 1 + return Instructions("Revision \(currentRevision)") + } +} + +private struct SessionPropertyHistoryInstructions: DynamicInstructions { + @SessionProperty(\.history) private var history + + var body: some DynamicInstructions { + Instructions("History count \(history.count)") + } +} + +private struct SessionPropertyFixtureModel: LanguageModel { + typealias UnavailableReason = Never + + var availability: Availability { .available } + + func supports(locale: Locale) -> Bool { true } + + func respond( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) async throws -> LanguageModelSession.Response where Content: Generable { + fatalError("SessionPropertyFixtureModel does not generate responses") + } + + func streamResponse( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) -> sending LanguageModelSession.ResponseStream where Content: Generable { + fatalError("SessionPropertyFixtureModel does not generate responses") + } +} From a03fd6beeb0aef9b23bffd90af896156ec3c2238 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Mon, 12 Oct 2026 03:06:31 +0800 Subject: [PATCH 04/18] Add Foundation Models 27 dynamic profiles --- .../DynamicInstructions.swift | 2 +- Sources/AnyLanguageModel/DynamicProfile.swift | 556 ++++++++++++++ .../LanguageModelSession.swift | 695 ++++++++++++++++-- .../AnyLanguageModel/SessionProperties.swift | 42 +- ...PICompatibilityAnyLanguageModelTests.swift | 24 + ...PICompatibilityFoundationModelsTests.swift | 24 + .../DynamicProfileTests.swift | 491 +++++++++++++ 7 files changed, 1753 insertions(+), 81 deletions(-) create mode 100644 Sources/AnyLanguageModel/DynamicProfile.swift create mode 100644 Tests/AnyLanguageModelTests/DynamicProfileTests.swift diff --git a/Sources/AnyLanguageModel/DynamicInstructions.swift b/Sources/AnyLanguageModel/DynamicInstructions.swift index 1f4fe4bf..e83fda4d 100644 --- a/Sources/AnyLanguageModel/DynamicInstructions.swift +++ b/Sources/AnyLanguageModel/DynamicInstructions.swift @@ -337,7 +337,7 @@ extension Instructions: PrimitiveDynamicInstructions { } } -private func resolveDynamicInstructions( +func resolveDynamicInstructions( _ dynamicInstructions: any DynamicInstructions ) -> ResolvedDynamicInstructions { func resolve(_ content: Content) -> ResolvedDynamicInstructions diff --git a/Sources/AnyLanguageModel/DynamicProfile.swift b/Sources/AnyLanguageModel/DynamicProfile.swift new file mode 100644 index 00000000..6e4c0668 --- /dev/null +++ b/Sources/AnyLanguageModel/DynamicProfile.swift @@ -0,0 +1,556 @@ +extension LanguageModelSession { + /// A declarative, per-model-request session profile. + /// + /// - Note: This API is exclusive to AnyLanguageModel on OS 26 and mirrors + /// Foundation Models 27. + @_typeEraser(AnyDynamicProfile) + public protocol DynamicProfile { + associatedtype Body: DynamicProfile + + @DynamicProfileBuilder + var body: Body { get } + } + + /// A concrete profile containing dynamic instructions and Tools. + public struct Profile: DynamicProfile { + public typealias Body = Never + + fileprivate let dynamicInstructions: AnyDynamicInstructions + fileprivate let identity: String + + public init( + @DynamicInstructionsBuilder _ dynamicInstructions: () -> some DynamicInstructions + ) { + let value = dynamicInstructions() + self.dynamicInstructions = AnyDynamicInstructions(value) + self.identity = String(reflecting: type(of: value)) + } + + public var body: Never { + fatalError("LanguageModelSession.Profile has no body") + } + } + + /// A type-erased dynamic profile. + public struct AnyDynamicProfile: DynamicProfile { + public typealias Body = Never + + fileprivate let resolveValue: () -> ResolvedDynamicProfile + + public init(_ dynamicProfile: any DynamicProfile) { + resolveValue = { resolveDynamicProfile(dynamicProfile) } + } + + public init(erasing dynamicProfile: some DynamicProfile) { + self.init(dynamicProfile) + } + + public var body: Never { + fatalError("LanguageModelSession.AnyDynamicProfile has no body") + } + + func resolveForRequest() -> ResolvedDynamicProfile { + resolveValue() + } + } + + public struct ConditionalDynamicProfile: DynamicProfile + where TrueContent: DynamicProfile, FalseContent: DynamicProfile { + public enum Branch { + case trueContent(TrueContent) + case falseContent(FalseContent) + } + + public typealias Body = Never + fileprivate let branch: Branch + + public init(_ branch: Branch) { + self.branch = branch + } + + public var body: Never { + fatalError("ConditionalDynamicProfile has no body") + } + } + + @resultBuilder + public struct DynamicProfileBuilder { + public static func buildBlock(_ content: Content) -> Content + where Content: DynamicProfile { + content + } + + public static func buildEither( + first content: TrueContent + ) -> ConditionalDynamicProfile + where TrueContent: DynamicProfile, FalseContent: DynamicProfile { + ConditionalDynamicProfile(.trueContent(content)) + } + + public static func buildEither( + second content: FalseContent + ) -> ConditionalDynamicProfile + where TrueContent: DynamicProfile, FalseContent: DynamicProfile { + ConditionalDynamicProfile(.falseContent(content)) + } + + @available(*, unavailable, message: "The body of a 'DynamicProfile' must evaluate to a single active profile") + public static func buildBlock(_ contents: repeat each Content) -> Never + where repeat each Content: DynamicProfile { + fatalError("A DynamicProfile must have one active profile") + } + + public static func buildLimitedAvailability( + _ component: some DynamicProfile + ) -> AnyDynamicProfile { + AnyDynamicProfile(component) + } + } + + public protocol DynamicProfileModifier { + associatedtype Body: DynamicProfile + typealias Content = DynamicProfileModifierContent + + @DynamicProfileBuilder + func body(content: Content) -> Body + } + + public struct DynamicProfileModifierContent: DynamicProfile + where Modifier: DynamicProfileModifier { + public typealias Body = Never + fileprivate let content: AnyDynamicProfile + + fileprivate init(_ content: Content) where Content: DynamicProfile { + self.content = AnyDynamicProfile(content) + } + + public var body: Never { + fatalError("DynamicProfileModifierContent has no body") + } + } + + public struct ModifiedDynamicProfile: DynamicProfile + where Content: DynamicProfile, Modifier: DynamicProfileModifier { + public typealias Body = Never + fileprivate let content: Content + fileprivate let modifier: Modifier + + fileprivate init(content: Content, modifier: Modifier) { + self.content = content + self.modifier = modifier + } + + public var body: Never { + fatalError("ModifiedDynamicProfile has no body") + } + } +} + +extension LanguageModelSession.DynamicProfile { + public typealias Profile = LanguageModelSession.Profile + public typealias DynamicProfile = LanguageModelSession.DynamicProfile + public typealias SessionProperty = LanguageModelSession.SessionProperty + + public func modifier( + _ modifier: Modifier + ) -> some LanguageModelSession.DynamicProfile + where Modifier: LanguageModelSession.DynamicProfileModifier { + LanguageModelSession.ModifiedDynamicProfile(content: self, modifier: modifier) + } + + public func model( + _ model: any LanguageModel + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .model(model)) + } + + public func model( + _ model: Model + ) -> some LanguageModelSession.DynamicProfile where Model: LanguageModel { + BuiltinModifiedDynamicProfile(content: self, modifier: .model(model)) + } + + public func temperature(_ temperature: Double?) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .temperature(temperature)) + } + + public func samplingMode( + _ samplingMode: GenerationOptions.SamplingMode? + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .samplingMode(samplingMode)) + } + + public func maximumResponseTokens( + _ maximumResponseTokens: Int? + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile( + content: self, + modifier: .maximumResponseTokens(maximumResponseTokens) + ) + } + + public func reasoningLevel( + _ reasoningLevel: ContextOptions.ReasoningLevel? + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .reasoningLevel(reasoningLevel)) + } + + public func toolCallingMode( + _ toolCallingMode: GenerationOptions.ToolCallingMode? + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .toolCallingMode(toolCallingMode)) + } + + public func historyTransform( + _ transform: @escaping ([Transcript.Entry]) -> [Transcript.Entry] + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .historyTransform(transform)) + } + + public func transcriptErrorHandlingPolicy( + _ policy: TranscriptErrorHandlingPolicy? + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .transcriptErrorHandlingPolicy(policy)) + } + + public func onPrompt( + perform action: @Sendable @escaping (Transcript.Prompt) async throws -> Void + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .onPrompt(action)) + } + + public func onPrompt( + perform action: @Sendable @escaping () async throws -> Void + ) -> some LanguageModelSession.DynamicProfile { + onPrompt { _ in try await action() } + } + + public func onResponse( + perform action: @Sendable @escaping (Transcript.Response) async throws -> Void + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .onResponse(action)) + } + + public func onResponse( + perform action: @Sendable @escaping () async throws -> Void + ) -> some LanguageModelSession.DynamicProfile { + onResponse { _ in try await action() } + } + + public func onReasoning( + perform action: @Sendable @escaping (Transcript.Reasoning) async throws -> Void + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .onReasoning(action)) + } + + public func onReasoning( + perform action: @Sendable @escaping () async throws -> Void + ) -> some LanguageModelSession.DynamicProfile { + onReasoning { _ in try await action() } + } + + public func onToolCall( + perform action: @Sendable @escaping (Transcript.ToolCall) async throws -> Void + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .onToolCall(action)) + } + + public func onToolCall( + perform action: @Sendable @escaping () async throws -> Void + ) -> some LanguageModelSession.DynamicProfile { + onToolCall { _ in try await action() } + } + + public func onToolOutput( + perform action: @Sendable @escaping (Transcript.ToolCall, Transcript.ToolOutput) async throws -> Void + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .onToolOutput(action)) + } + + public func onToolOutput( + perform action: @Sendable @escaping () async throws -> Void + ) -> some LanguageModelSession.DynamicProfile { + onToolOutput { _, _ in try await action() } + } + + public func onActivate( + perform action: @Sendable @escaping () async -> Void + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .onActivate(action)) + } + + public func onDeactivate( + perform action: @Sendable @escaping () async -> Void + ) -> some LanguageModelSession.DynamicProfile { + BuiltinModifiedDynamicProfile(content: self, modifier: .onDeactivate(action)) + } +} + +extension LanguageModelSession.DynamicProfileModifier { + public typealias DynamicProfile = LanguageModelSession.DynamicProfile + public typealias SessionProperty = LanguageModelSession.SessionProperty +} + +extension Never: LanguageModelSession.DynamicProfile {} + +private protocol PrimitiveDynamicProfile { + func resolve() -> ResolvedDynamicProfile +} + +struct ResolvedDynamicProfile: @unchecked Sendable { + var identity: String + var model: (any LanguageModel)? + var instructions: Instructions? + var tools: [any Tool] + var samplingMode: GenerationOptions.SamplingMode? + var hasSamplingMode = false + var temperature: Double? + var hasTemperature = false + var maximumResponseTokens: Int? + var hasMaximumResponseTokens = false + var reasoningLevel: ContextOptions.ReasoningLevel? + var hasReasoningLevel = false + var toolCallingMode: GenerationOptions.ToolCallingMode? + var hasToolCallingMode = false + var transcriptErrorHandlingPolicy: TranscriptErrorHandlingPolicy? + var hasTranscriptErrorHandlingPolicy = false + var historyTransforms: [([Transcript.Entry]) -> [Transcript.Entry]] = [] + var onPrompt: [@Sendable (Transcript.Prompt) async throws -> Void] = [] + var onResponse: [@Sendable (Transcript.Response) async throws -> Void] = [] + var onReasoning: [@Sendable (Transcript.Reasoning) async throws -> Void] = [] + var onToolCall: [@Sendable (Transcript.ToolCall) async throws -> Void] = [] + var onToolOutput: [@Sendable (Transcript.ToolCall, Transcript.ToolOutput) async throws -> Void] = [] + var onActivate: [@Sendable () async -> Void] = [] + var onDeactivate: [@Sendable () async -> Void] = [] +} + +extension LanguageModelSession.Profile: PrimitiveDynamicProfile { + fileprivate func resolve() -> ResolvedDynamicProfile { + let dynamic = dynamicInstructions.resolveForRequest() + return ResolvedDynamicProfile( + identity: identity, + model: nil, + instructions: dynamic.instructions, + tools: dynamic.tools + ) + } +} + +extension LanguageModelSession.AnyDynamicProfile: PrimitiveDynamicProfile { + fileprivate func resolve() -> ResolvedDynamicProfile { + resolveForRequest() + } +} + +extension LanguageModelSession.ConditionalDynamicProfile: PrimitiveDynamicProfile { + fileprivate func resolve() -> ResolvedDynamicProfile { + var resolved: ResolvedDynamicProfile + switch branch { + case .trueContent(let content): + resolved = resolveDynamicProfile(content) + resolved.identity = "true/" + resolved.identity + case .falseContent(let content): + resolved = resolveDynamicProfile(content) + resolved.identity = "false/" + resolved.identity + } + return resolved + } +} + +extension LanguageModelSession.DynamicProfileModifierContent: PrimitiveDynamicProfile { + fileprivate func resolve() -> ResolvedDynamicProfile { + content.resolveForRequest() + } +} + +extension LanguageModelSession.ModifiedDynamicProfile: PrimitiveDynamicProfile { + fileprivate func resolve() -> ResolvedDynamicProfile { + resolveDynamicProfile(modifier.body(content: .init(content))) + } +} + +private struct BuiltinModifiedDynamicProfile: LanguageModelSession.DynamicProfile, + PrimitiveDynamicProfile +where Content: LanguageModelSession.DynamicProfile { + typealias Body = Never + let content: Content + let modifier: BuiltinDynamicProfileModifier + + var body: Never { + fatalError("BuiltinModifiedDynamicProfile has no body") + } + + func resolve() -> ResolvedDynamicProfile { + var resolved = resolveDynamicProfile(content) + modifier.apply(to: &resolved) + return resolved + } +} + +private enum BuiltinDynamicProfileModifier: @unchecked Sendable { + case model(any LanguageModel) + case temperature(Double?) + case samplingMode(GenerationOptions.SamplingMode?) + case maximumResponseTokens(Int?) + case reasoningLevel(ContextOptions.ReasoningLevel?) + case toolCallingMode(GenerationOptions.ToolCallingMode?) + case historyTransform(([Transcript.Entry]) -> [Transcript.Entry]) + case transcriptErrorHandlingPolicy(TranscriptErrorHandlingPolicy?) + case onPrompt(@Sendable (Transcript.Prompt) async throws -> Void) + case onResponse(@Sendable (Transcript.Response) async throws -> Void) + case onReasoning(@Sendable (Transcript.Reasoning) async throws -> Void) + case onToolCall(@Sendable (Transcript.ToolCall) async throws -> Void) + case onToolOutput(@Sendable (Transcript.ToolCall, Transcript.ToolOutput) async throws -> Void) + case onActivate(@Sendable () async -> Void) + case onDeactivate(@Sendable () async -> Void) + + func apply(to resolved: inout ResolvedDynamicProfile) { + switch self { + case .model(let model): + if resolved.model == nil { resolved.model = model } + case .temperature(let value): + if !resolved.hasTemperature { + resolved.temperature = value + resolved.hasTemperature = true + } + case .samplingMode(let value): + if !resolved.hasSamplingMode { + resolved.samplingMode = value + resolved.hasSamplingMode = true + } + case .maximumResponseTokens(let value): + if !resolved.hasMaximumResponseTokens { + resolved.maximumResponseTokens = value + resolved.hasMaximumResponseTokens = true + } + case .reasoningLevel(let value): + if !resolved.hasReasoningLevel { + resolved.reasoningLevel = value + resolved.hasReasoningLevel = true + } + case .toolCallingMode(let value): + if !resolved.hasToolCallingMode { + resolved.toolCallingMode = value + resolved.hasToolCallingMode = true + } + case .transcriptErrorHandlingPolicy(let value): + if !resolved.hasTranscriptErrorHandlingPolicy { + resolved.transcriptErrorHandlingPolicy = value + resolved.hasTranscriptErrorHandlingPolicy = true + } + case .historyTransform(let transform): + resolved.historyTransforms.insert(transform, at: 0) + case .onPrompt(let action): + resolved.onPrompt.insert(action, at: 0) + case .onResponse(let action): + resolved.onResponse.insert(action, at: 0) + case .onReasoning(let action): + resolved.onReasoning.insert(action, at: 0) + case .onToolCall(let action): + resolved.onToolCall.insert(action, at: 0) + case .onToolOutput(let action): + resolved.onToolOutput.insert(action, at: 0) + case .onActivate(let action): + resolved.onActivate.insert(action, at: 0) + case .onDeactivate(let action): + resolved.onDeactivate.append(action) + } + } +} + +private func resolveDynamicProfile( + _ dynamicProfile: any LanguageModelSession.DynamicProfile +) -> ResolvedDynamicProfile { + func resolve(_ content: Content) -> ResolvedDynamicProfile + where Content: LanguageModelSession.DynamicProfile { + if let primitive = content as? any PrimitiveDynamicProfile { + return primitive.resolve() + } + return resolve(content.body) + } + + return resolve(dynamicProfile) +} + +actor ProfileLifecycle { + private var activeProfile: ResolvedDynamicProfile? + private var lastPromptID: String? + + func prepare( + _ profile: ResolvedDynamicProfile, + properties: SessionPropertyValues, + history: [Transcript.Entry] + ) async throws { + if activeProfile?.identity != profile.identity { + if let previous = activeProfile { + await withBindings(properties: properties, history: history) { + for action in previous.onDeactivate { await action() } + } + } + await withBindings(properties: properties, history: history) { + for action in profile.onActivate { await action() } + } + activeProfile = profile + } else { + activeProfile = profile + } + + guard + let prompt = history.reversed().compactMap({ entry -> Transcript.Prompt? in + if case .prompt(let prompt) = entry { return prompt } + return nil + }).first, + prompt.id != lastPromptID + else { return } + + try await withBindings(properties: properties, history: history) { + for action in profile.onPrompt { try await action(prompt) } + } + lastPromptID = prompt.id + } + + private func withBindings( + properties: SessionPropertyValues, + history: [Transcript.Entry], + operation: @Sendable () async throws -> Result + ) async rethrows -> Result { + let history = SessionHistoryBinding(history, isWritable: true) + return try await SessionPropertyBinding.$values.withValue(properties) { + try await SessionPropertyBinding.$history.withValue(history) { + try await operation() + } + } + } +} + +struct MissingDynamicProfileModel: LanguageModel { + typealias UnavailableReason = Never + + func respond( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) async throws -> LanguageModelSession.Response where Content: Generable { + throw MissingDynamicProfileModelError.missingModelModifier + } + + func streamResponse( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) -> sending LanguageModelSession.ResponseStream where Content: Generable { + let stream = AsyncThrowingStream.Snapshot, any Error> { + continuation in + continuation.finish(throwing: MissingDynamicProfileModelError.missingModelModifier) + } + return LanguageModelSession.ResponseStream(stream: stream) + } +} + +enum MissingDynamicProfileModelError: Error { + case missingModelModifier +} diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 10627f0d..e6163b30 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -108,14 +108,17 @@ public final class LanguageModelSession: @unchecked Sendable { public let instructions: Instructions? private let dynamicInstructions: AnyDynamicInstructions? + private let dynamicProfile: AnyDynamicProfile? private let dynamicInstructionsLock = NSLock() + private let dynamicProfileLock = NSLock() + private let profileLifecycle = ProfileLifecycle() - /// Session-scoped values available to dynamic instructions. + /// Session-scoped values available to profiles and dynamic instructions. public let properties: SessionPropertyValues /// Whether the session resolves its instructions and tools before each request. nonisolated var usesDynamicInstructions: Bool { - dynamicInstructions != nil + dynamicInstructions != nil || dynamicProfile != nil } /// A delegate that observes and controls tool execution. @@ -151,10 +154,53 @@ public final class LanguageModelSession: @unchecked Sendable { /// The tools that the model can call in response to the request. public let tools: [any Tool] - fileprivate init(transcript: Transcript, instructions: Instructions?, tools: [any Tool]) { + /// The model selected for this request. + public let model: any LanguageModel + + /// Generation options after profile defaults are combined with call-site options. + public let options: GenerationOptions + + /// Context options selected by the active profile. + public let contextOptions: ContextOptions + + fileprivate let profile: ResolvedDynamicProfile? + fileprivate let canonicalTranscript: [Transcript.Entry] + fileprivate let callbackState: ProfileCallbackState + + fileprivate init( + transcript: Transcript, + instructions: Instructions?, + tools: [any Tool], + model: any LanguageModel, + options: GenerationOptions = .init(), + contextOptions: ContextOptions = .init(), + profile: ResolvedDynamicProfile? = nil, + canonicalTranscript: [Transcript.Entry]? = nil, + callbackState: ProfileCallbackState = ProfileCallbackState() + ) { self.transcript = transcript self.instructions = instructions self.tools = tools + self.model = model + self.options = options + self.contextOptions = contextOptions + self.profile = profile + self.canonicalTranscript = canonicalTranscript ?? Array(transcript) + self.callbackState = callbackState + } + + fileprivate func sharingCallbackState(with context: RequestContext) -> RequestContext { + RequestContext( + transcript: transcript, + instructions: instructions, + tools: tools, + model: model, + options: options, + contextOptions: contextOptions, + profile: profile, + canonicalTranscript: canonicalTranscript, + callbackState: context.callbackState + ) } } @@ -168,34 +214,254 @@ public final class LanguageModelSession: @unchecked Sendable { /// It's public so that language models outside this module /// can read the inputs for each request. nonisolated public func resolvedRequestContext() -> RequestContext { - guard let dynamicInstructions else { - return RequestContext(transcript: transcript, instructions: instructions, tools: tools) + if let prepared = ProfileRequestBinding.prepared, + prepared.session === self + { + return prepared.context + } + return resolveRequestContext(additionalEntries: [], options: .init()) + } + + /// Resolves the active profile for one actual model request. + /// + /// `additionalEntries` supplies canonical entries produced in the current Tool + /// round but not yet committed to the durable Session transcript. A profile's + /// history transform sees those entries before selecting the request projection. + /// + /// - Note: This overload is exclusive to AnyLanguageModel. Compatibility + /// executors use it to preserve Foundation Models 27 request timing while + /// AnyLanguageModel still exposes its legacy model interface. + nonisolated public func resolvedRequestContext( + including additionalEntries: [Transcript.Entry], + options: GenerationOptions = .init() + ) async throws -> RequestContext { + let prepared = ProfileRequestBinding.prepared + if additionalEntries.isEmpty, let prepared, prepared.session === self { + return prepared.context + } + let callSiteOptions = + if let prepared, prepared.session === self { + prepared.callSiteOptions + } else { + options + } + var context = resolveRequestContext( + additionalEntries: additionalEntries, + options: callSiteOptions + ) + if let prepared, prepared.session === self { + context = context.sharingCallbackState(with: prepared.context) + } + if let profile = context.profile { + try await profileLifecycle.prepare( + profile, + properties: properties, + history: context.canonicalTranscript + ) + } + if let prepared, prepared.session === self { + prepared.context = context + } + return context + } + + nonisolated private func resolveRequestContext( + additionalEntries: [Transcript.Entry], + options: GenerationOptions + ) -> RequestContext { + var canonical = Array(transcript) + additionalEntries + guard let dynamicProfile else { + guard let dynamicInstructions else { + return RequestContext( + transcript: Transcript(entries: canonical), + instructions: instructions, + tools: tools, + model: model, + options: options, + canonicalTranscript: canonical + ) + } + + // Dynamic instructions are request-only. Their body can observe + // session properties and canonical history, but cannot rewrite history. + let history = SessionHistoryBinding(canonical, isWritable: false) + let resolved = dynamicInstructionsLock.withLock { + SessionPropertyBinding.$values.withValue(properties) { + SessionPropertyBinding.$history.withValue(history) { + dynamicInstructions.resolveForRequest() + } + } + } + var requestTranscript = Transcript(entries: canonical) + if let currentInstructions = resolved.instructions { + requestTranscript = Transcript( + entries: [instructionsEntry(currentInstructions, tools: resolved.tools)] + canonical + ) + } + return RequestContext( + transcript: requestTranscript, + instructions: resolved.instructions, + tools: resolved.tools, + model: model, + options: options, + canonicalTranscript: canonical + ) } - // Evaluate the body for this request. - // The resolved instructions go into the request's transcript, - // never into the session's. - let canonicalHistory = Array(transcript) - let resolved = dynamicInstructionsLock.withLock { + let history = SessionHistoryBinding(canonical, isWritable: true) + let resolved = dynamicProfileLock.withLock { SessionPropertyBinding.$values.withValue(properties) { - SessionPropertyBinding.$history.withValue(canonicalHistory) { - dynamicInstructions.resolveForRequest() + SessionPropertyBinding.$history.withValue(history) { + dynamicProfile.resolveForRequest() } } } - var requestTranscript = transcript - if let instructions = resolved.instructions { - let instructionsEntry = Transcript.Entry.instructions( - Transcript.Instructions( - segments: [.text(Transcript.TextSegment(content: instructions.description))], - toolDefinitions: resolved.tools - .filter(\.includesSchemaInInstructions) - .map { Transcript.ToolDefinition(tool: $0) } - ) + canonical = history.entries + var projected = canonical + for transform in resolved.historyTransforms { + let transformHistory = SessionHistoryBinding(canonical, isWritable: true) + projected = SessionPropertyBinding.$values.withValue(properties) { + SessionPropertyBinding.$history.withValue(transformHistory) { + transform(projected) + } + } + canonical = transformHistory.entries + } + var requestTranscript = Transcript(entries: projected) + if let currentInstructions = resolved.instructions { + requestTranscript = Transcript( + entries: [instructionsEntry(currentInstructions, tools: resolved.tools)] + projected + ) + } + + var resolvedOptions = options + if resolvedOptions.sampling == nil, resolved.hasSamplingMode { + resolvedOptions.sampling = resolved.samplingMode + } + if resolvedOptions.temperature == nil, resolved.hasTemperature { + resolvedOptions.temperature = resolved.temperature + } + if resolvedOptions.maximumResponseTokens == nil, resolved.hasMaximumResponseTokens { + resolvedOptions.maximumResponseTokens = resolved.maximumResponseTokens + } + if resolvedOptions.toolCallingMode == nil, resolved.hasToolCallingMode { + resolvedOptions.toolCallingMode = resolved.toolCallingMode + } + if resolved.hasTranscriptErrorHandlingPolicy { + state.withLock { $0.transcriptErrorHandlingPolicy = resolved.transcriptErrorHandlingPolicy } + } + return RequestContext( + transcript: requestTranscript, + instructions: resolved.instructions, + tools: resolved.tools, + model: resolved.model ?? model, + options: resolvedOptions, + contextOptions: .init(reasoningLevel: resolved.reasoningLevel), + profile: resolved, + canonicalTranscript: canonical + ) + } + + nonisolated private func instructionsEntry( + _ instructions: Instructions, + tools: [any Tool] + ) -> Transcript.Entry { + .instructions( + .init( + segments: [.text(.init(content: instructions.description))], + toolDefinitions: + tools + .filter(\.includesSchemaInInstructions) + .map { Transcript.ToolDefinition(tool: $0) } ) - requestTranscript = Transcript(entries: [instructionsEntry] + requestTranscript) + ) + } + + /// Runs the active request snapshot's Tool-call callbacks. + nonisolated public func profileWillExecuteToolCall( + _ call: Transcript.ToolCall, + requestContext: RequestContext, + currentRoundEntries: [Transcript.Entry] + ) async throws { + guard let profile = requestContext.profile else { return } + let history = Array(transcript) + currentRoundEntries + try await withSessionProperties(history: history, isHistoryWritable: true) { + for action in profile.onToolCall { try await action(call) } + } + } + + /// Runs the active request snapshot's Tool-output callbacks. + nonisolated public func profileDidProduceToolOutput( + for call: Transcript.ToolCall, + output: Transcript.ToolOutput, + requestContext: RequestContext, + currentRoundEntries: [Transcript.Entry] + ) async throws { + guard let profile = requestContext.profile else { return } + let history = Array(transcript) + currentRoundEntries + try await withSessionProperties(history: history, isHistoryWritable: true) { + for action in profile.onToolOutput { try await action(call, output) } + } + } + + /// Runs reasoning and response callbacks from the profile snapshot that + /// produced the entries. Each stable entry ID is dispatched at most once + /// for the active request, including when streaming snapshots repeat it. + /// + /// - Note: This API is exclusive to AnyLanguageModel's compatibility model + /// seam. Ordinary callers compose callbacks through ``DynamicProfile``. + nonisolated public func profileDidProduceEntries( + _ entries: [Transcript.Entry], + requestContext: RequestContext, + currentRoundEntries: [Transcript.Entry] + ) async throws { + guard let profile = requestContext.profile else { return } + for entry in entries { + if requestContext.callbackState.wasDispatched(entry.id) { + continue + } + let history = Array(transcript) + currentRoundEntries + switch entry { + case .reasoning(let reasoning): + try await withSessionProperties(history: history, isHistoryWritable: true) { + for action in profile.onReasoning { try await action(reasoning) } + } + case .response(let response): + try await withSessionProperties(history: history, isHistoryWritable: true) { + for action in profile.onResponse { try await action(response) } + } + default: + continue + } + requestContext.callbackState.markDispatched(entry.id) + } + } + + /// Executes compatibility-model Tool work with session properties bound to + /// the producing request. Custom properties remain mutable, while the + /// canonical history is read-only in a Tool, matching Foundation Models 27. + nonisolated public func withProfileToolExecution( + requestContext: RequestContext, + currentRoundEntries: [Transcript.Entry], + operation: @Sendable () async throws -> Result + ) async rethrows -> Result { + let history = Array(transcript) + currentRoundEntries + return try await withSessionProperties(history: history, isHistoryWritable: false) { + try await operation() + } + } + + nonisolated private func withSessionProperties( + history: [Transcript.Entry], + isHistoryWritable: Bool, + operation: @Sendable () async throws -> Result + ) async rethrows -> Result { + let history = SessionHistoryBinding(history, isWritable: isHistoryWritable) + return try await SessionPropertyBinding.$values.withValue(properties) { + try await SessionPropertyBinding.$history.withValue(history) { + try await operation() + } } - return RequestContext(transcript: requestTranscript, instructions: resolved.instructions, tools: resolved.tools) } /// Creates a session with a model, tools, @@ -290,7 +556,28 @@ public final class LanguageModelSession: @unchecked Sendable { tools: [], instructions: nil, transcript: Transcript(entries: history), - dynamicInstructions: AnyDynamicInstructions(dynamicInstructions) + dynamicInstructions: AnyDynamicInstructions(dynamicInstructions), + dynamicProfile: nil + ) + } + + /// Creates a session whose active model, instructions, Tools, and request + /// projection are resolved before every model request. + public convenience init( + profile: sending some DynamicProfile, + history: some Collection = [] + ) { + let history = history.filter { entry in + if case .instructions = entry { return false } + return true + } + self.init( + model: MissingDynamicProfileModel(), + tools: [], + instructions: nil, + transcript: Transcript(entries: history), + dynamicInstructions: nil, + dynamicProfile: AnyDynamicProfile(profile) ) } @@ -299,11 +586,13 @@ public final class LanguageModelSession: @unchecked Sendable { tools: [any Tool], instructions: Instructions?, transcript: Transcript, - dynamicInstructions: AnyDynamicInstructions? = nil + dynamicInstructions: AnyDynamicInstructions? = nil, + dynamicProfile: AnyDynamicProfile? = nil ) { self.model = model self.tools = tools self.dynamicInstructions = dynamicInstructions + self.dynamicProfile = dynamicProfile let resolvedInstructions = instructions ?? Self.instructions(from: transcript) self.instructions = resolvedInstructions @@ -433,7 +722,8 @@ public final class LanguageModelSession: @unchecked Sendable { nonisolated private func wrapStream( _ upstream: sending ResponseStream, - promptEntry: Transcript.Entry + promptEntry: Transcript.Entry, + profileTracker: PreparedProfileRequestTracker? = nil ) -> ResponseStream where Content: Generable, Content.PartiallyGenerated: Sendable { let session = self let relay = AsyncThrowingStream.Snapshot, any Error> { continuation in @@ -447,6 +737,7 @@ public final class LanguageModelSession: @unchecked Sendable { session.beginResponding() var lastSnapshot: ResponseStream.Snapshot? var accountedUsage = Usage.zero + var committedEntryIDs: Set = [] do { for try await snapshot in stream { lastSnapshot = snapshot @@ -504,11 +795,28 @@ public final class LanguageModelSession: @unchecked Sendable { $0.transcript.append(responseEntry) } } + committedEntryIDs = Set( + lastSnapshot.transcriptEntries.map(\.id) + [responseEntry.id] + ) + if let prepared = profileTracker?.prepared { + try await session.profileDidProduceEntries( + Array(lastSnapshot.transcriptEntries) + [responseEntry], + requestContext: prepared.context, + currentRoundEntries: [] + ) + } session.endResponding() continuation.finish() } catch { session.withMutation(keyPath: \.transcript) { session.state.withLock { state in + if !committedEntryIDs.isEmpty { + state.transcript = Transcript( + entries: state.transcript.filter { + !committedEntryIDs.contains($0.id) + } + ) + } if state.transcriptErrorHandlingPolicy == .preserveTranscript { state.transcript.append(contentsOf: lastSnapshot?.transcriptEntries ?? []) } else if state.transcriptErrorHandlingPolicy == .revertTranscript { @@ -735,21 +1043,50 @@ public final class LanguageModelSession: @unchecked Sendable { responseFormat: type == String.self ? nil : .init(type: type), options: options ) { - try await model.respond( - within: self, - to: prompt, - generating: type, - includeSchemaInPrompt: includeSchemaInPrompt, - options: options + try await profiledResponse(options: options) { model, resolvedOptions, contextOptions in + try await model.respond( + within: self, + to: prompt, + generating: type, + includeSchemaInPrompt: contextOptions.includeSchemaInPrompt + ?? includeSchemaInPrompt, + options: resolvedOptions + ) + } + } + } + + nonisolated private func profiledResponse( + options: GenerationOptions, + generate: ( + any LanguageModel, + GenerationOptions, + ContextOptions + ) async throws -> Response + ) async throws -> ProfiledModelResponse where Content: Generable { + guard dynamicProfile != nil else { + return ProfiledModelResponse( + response: try await generate(model, options, .init()), + prepared: nil ) } + let context = try await resolvedRequestContext(including: [], options: options) + let prepared = PreparedProfileRequest( + session: self, + context: context, + callSiteOptions: options + ) + let response = try await ProfileRequestBinding.$prepared.withValue(prepared) { + try await generate(context.model, context.options, context.contextOptions) + } + return ProfiledModelResponse(response: response, prepared: prepared) } nonisolated private func respond( to prompt: Prompt, responseFormat: Transcript.ResponseFormat?, options: GenerationOptions, - generate: () async throws -> Response + generate: () async throws -> ProfiledModelResponse ) async throws -> Response { try await wrapRespond { promptID in // Add prompt to transcript @@ -765,7 +1102,11 @@ public final class LanguageModelSession: @unchecked Sendable { state.withLock { $0.transcript.append(promptEntry) } } - let response = Self.representingNilExplicitly(in: try await generate(), responseFormat: responseFormat) + let profiled = try await generate() + let response = Self.representingNilExplicitly( + in: profiled.response, + responseFormat: responseFormat + ) recordUsage(response.usage) @@ -785,15 +1126,45 @@ public final class LanguageModelSession: @unchecked Sendable { ) ) - // Add tool entries and response to transcript + try await commitResponse( + response, + entry: responseEntry, + prepared: profiled.prepared + ) + + return response + } + } + + nonisolated private func commitResponse( + _ response: Response, + entry responseEntry: Transcript.Entry, + prepared: PreparedProfileRequest? + ) async throws where Content: Generable { + withMutation(keyPath: \.transcript) { + state.withLock { lockedState in + lockedState.transcript.append(contentsOf: response.transcriptEntries) + lockedState.transcript.append(responseEntry) + } + } + + guard let prepared else { return } + do { + try await profileDidProduceEntries( + Array(response.transcriptEntries) + [responseEntry], + requestContext: prepared.context, + currentRoundEntries: [] + ) + } catch { + let ids = Set(response.transcriptEntries.map(\.id) + [responseEntry.id]) withMutation(keyPath: \.transcript) { state.withLock { lockedState in - lockedState.transcript.append(contentsOf: response.transcriptEntries) - lockedState.transcript.append(responseEntry) + lockedState.transcript = Transcript( + entries: lockedState.transcript.filter { !ids.contains($0.id) } + ) } } - - return response + throw error } } @@ -815,17 +1186,92 @@ public final class LanguageModelSession: @unchecked Sendable { state.withLock { $0.transcript.append(promptEntry) } } - return wrapStream( - model.streamResponse( + let upstream: ResponseStream + let profileTracker: PreparedProfileRequestTracker? + if dynamicProfile != nil { + let tracker = PreparedProfileRequestTracker() + profileTracker = tracker + upstream = profileDispatchedStream( + prompt: prompt, + type: type, + includeSchemaInPrompt: includeSchemaInPrompt, + options: options, + tracker: tracker + ) + } else { + profileTracker = nil + upstream = model.streamResponse( within: self, to: prompt, generating: type, includeSchemaInPrompt: includeSchemaInPrompt, options: options - ), - promptEntry: promptEntry + ) + } + return wrapStream( + upstream, + promptEntry: promptEntry, + profileTracker: profileTracker ) } + + nonisolated private func profileDispatchedStream( + prompt: Prompt, + type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions, + tracker: PreparedProfileRequestTracker + ) -> ResponseStream where Content: Generable { + profileDispatchedStream(options: options, tracker: tracker) { + model, + resolvedOptions, + contextOptions in + model.streamResponse( + within: self, + to: prompt, + generating: type, + includeSchemaInPrompt: contextOptions.includeSchemaInPrompt + ?? includeSchemaInPrompt, + options: resolvedOptions + ) + } + } + + nonisolated private func profileDispatchedStream( + options: GenerationOptions, + tracker: PreparedProfileRequestTracker, + generate: + @Sendable @escaping ( + any LanguageModel, + GenerationOptions, + ContextOptions + ) -> ResponseStream + ) -> ResponseStream where Content: Generable { + let stream = AsyncThrowingStream.Snapshot, any Error> { continuation in + let task = Task { + do { + let context = try await self.resolvedRequestContext(including: [], options: options) + let prepared = PreparedProfileRequest( + session: self, + context: context, + callSiteOptions: options + ) + tracker.prepared = prepared + let upstream = ProfileRequestBinding.$prepared.withValue(prepared) { + generate(context.model, context.options, context.contextOptions) + } + for try await snapshot in upstream { + continuation.yield(snapshot) + } + continuation.finish() + } catch { + continuation.finish(throwing: error) + } + } + continuation.onTermination = { _ in task.cancel() } + } + return ResponseStream(stream: stream) + } } // MARK: - String Response Convenience Methods @@ -900,13 +1346,16 @@ extension LanguageModelSession { options: GenerationOptions = GenerationOptions() ) async throws -> Response { try await respond(to: prompt, responseFormat: .init(schema: schema), options: options) { - try await model.respond( - within: self, - to: prompt, - schema: schema, - includeSchemaInPrompt: includeSchemaInPrompt, - options: options - ) + try await profiledResponse(options: options) { model, resolvedOptions, contextOptions in + try await model.respond( + within: self, + to: prompt, + schema: schema, + includeSchemaInPrompt: contextOptions.includeSchemaInPrompt + ?? includeSchemaInPrompt, + options: resolvedOptions + ) + } } } @@ -963,15 +1412,38 @@ extension LanguageModelSession { state.withLock { $0.transcript.append(promptEntry) } } - return wrapStream( - model.streamResponse( + let upstream: ResponseStream + let profileTracker: PreparedProfileRequestTracker? + if dynamicProfile != nil { + let tracker = PreparedProfileRequestTracker() + profileTracker = tracker + upstream = profileDispatchedStream(options: options, tracker: tracker) { + model, + resolvedOptions, + contextOptions in + model.streamResponse( + within: self, + to: prompt, + schema: schema, + includeSchemaInPrompt: contextOptions.includeSchemaInPrompt + ?? includeSchemaInPrompt, + options: resolvedOptions + ) + } + } else { + profileTracker = nil + upstream = model.streamResponse( within: self, to: prompt, schema: schema, includeSchemaInPrompt: includeSchemaInPrompt, options: options - ), - promptEntry: promptEntry + ) + } + return wrapStream( + upstream, + promptEntry: promptEntry, + profileTracker: profileTracker ) } @@ -1165,14 +1637,21 @@ extension LanguageModelSession { // Extract text content for the Prompt parameter let textPrompt = Prompt(prompt) - let response = Self.representingNilExplicitly( - in: try await model.respond( + let profiled = try await profiledResponse(options: options) { + model, + resolvedOptions, + contextOptions in + try await model.respond( within: self, to: textPrompt, generating: type, - includeSchemaInPrompt: includeSchemaInPrompt, - options: options - ), + includeSchemaInPrompt: contextOptions.includeSchemaInPrompt + ?? includeSchemaInPrompt, + options: resolvedOptions + ) + } + let response = Self.representingNilExplicitly( + in: profiled.response, responseFormat: type == String.self ? nil : .init(type: type) ) @@ -1194,13 +1673,11 @@ extension LanguageModelSession { ) ) - // Add tool entries and response to transcript - withMutation(keyPath: \.transcript) { - state.withLock { lockedState in - lockedState.transcript.append(contentsOf: response.transcriptEntries) - lockedState.transcript.append(responseEntry) - } - } + try await commitResponse( + response, + entry: responseEntry, + prepared: profiled.prepared + ) return response } @@ -1297,15 +1774,38 @@ extension LanguageModelSession { // Extract text content for the Prompt parameter let textPrompt = Prompt(prompt) - return wrapStream( - model.streamResponse( + let upstream: ResponseStream + let profileTracker: PreparedProfileRequestTracker? + if dynamicProfile != nil { + let tracker = PreparedProfileRequestTracker() + profileTracker = tracker + upstream = profileDispatchedStream(options: options, tracker: tracker) { + model, + resolvedOptions, + contextOptions in + model.streamResponse( + within: self, + to: textPrompt, + generating: type, + includeSchemaInPrompt: contextOptions.includeSchemaInPrompt + ?? includeSchemaInPrompt, + options: resolvedOptions + ) + } + } else { + profileTracker = nil + upstream = model.streamResponse( within: self, to: textPrompt, generating: type, includeSchemaInPrompt: includeSchemaInPrompt, options: options - ), - promptEntry: promptEntry + ) + } + return wrapStream( + upstream, + promptEntry: promptEntry, + profileTracker: profileTracker ) } } @@ -1688,6 +2188,61 @@ private enum ResponseStreamError: Error, LocalizedError { } } +private struct ProfiledModelResponse where Content: Generable { + let response: LanguageModelSession.Response + let prepared: PreparedProfileRequest? +} + +fileprivate final class ProfileCallbackState: @unchecked Sendable { + private let dispatchedEntryIDs = Locked>([]) + + func wasDispatched(_ id: String) -> Bool { + dispatchedEntryIDs.withLock { $0.contains(id) } + } + + func markDispatched(_ id: String) { + dispatchedEntryIDs.withLock { _ = $0.insert(id) } + } +} + +private final class PreparedProfileRequest: @unchecked Sendable { + let session: LanguageModelSession + let callSiteOptions: GenerationOptions + private let state: Locked + + init( + session: LanguageModelSession, + context: LanguageModelSession.RequestContext, + callSiteOptions: GenerationOptions + ) { + self.session = session + self.callSiteOptions = callSiteOptions + state = Locked(State(context: context)) + } + + var context: LanguageModelSession.RequestContext { + get { state.withLock { $0.context } } + set { state.withLock { $0.context = newValue } } + } + + private struct State: Sendable { + var context: LanguageModelSession.RequestContext + } +} + +private final class PreparedProfileRequestTracker: @unchecked Sendable { + private let storage = Locked(nil) + + var prepared: PreparedProfileRequest? { + get { storage.withLock { $0 } } + set { storage.withLock { $0 = newValue } } + } +} + +private enum ProfileRequestBinding { + @TaskLocal static var prepared: PreparedProfileRequest? +} + // MARK: - private struct State: Equatable, Sendable { diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift index 9b503bff..27b22188 100644 --- a/Sources/AnyLanguageModel/SessionProperties.swift +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -24,7 +24,7 @@ public protocol SessionPropertyKey: SendableMetatype { public macro SessionPropertyEntry() = #externalMacro(module: "AnyLanguageModelMacros", type: "SessionPropertyEntryMacro") -/// Session-scoped storage available while dynamic instructions are evaluated. +/// Session-scoped storage shared by profiles, dynamic instructions, and tools. /// /// - Note: This API is exclusive to AnyLanguageModel on OS 26 and mirrors /// Foundation Models 27. @@ -42,14 +42,17 @@ public final class SessionPropertyValues: @unchecked Sendable { self.historySetter = historySetter } - /// The session history without an instructions entry. + /// The canonical session history after dynamic instructions. public var history: Transcript.HistoryView { - get { SessionPropertyBinding.history ?? historyGetter() } + get { SessionPropertyBinding.history?.entries ?? historyGetter() } set { - precondition( - SessionPropertyBinding.history == nil, - "Session history is read-only while dynamic instructions are active" - ) + if let history = SessionPropertyBinding.history { + precondition( + history.isWritable, + "Session history is read-only while dynamic instructions or a Tool is active" + ) + history.entries = newValue + } historySetter(newValue) } } @@ -73,7 +76,22 @@ public final class SessionPropertyValues: @unchecked Sendable { enum SessionPropertyBinding { @TaskLocal static var values: SessionPropertyValues? - @TaskLocal static var history: Transcript.HistoryView? + @TaskLocal static var history: SessionHistoryBinding? +} + +final class SessionHistoryBinding: @unchecked Sendable { + private let storage: Locked + let isWritable: Bool + + init(_ entries: Transcript.HistoryView, isWritable: Bool) { + storage = Locked(entries) + self.isWritable = isWritable + } + + var entries: Transcript.HistoryView { + get { storage.withLock { $0 } } + set { storage.withLock { $0 = newValue } } + } } extension LanguageModelSession { @@ -90,7 +108,7 @@ extension LanguageModelSession { get { guard let values = SessionPropertyBinding.values else { preconditionFailure( - "SessionProperty can only be accessed while dynamic instructions are active" + "SessionProperty can only be accessed while a session profile, dynamic instructions, or a Tool is active" ) } return values[keyPath: keyPath] @@ -98,7 +116,7 @@ extension LanguageModelSession { nonmutating set { guard let values = SessionPropertyBinding.values else { preconditionFailure( - "SessionProperty can only be accessed while dynamic instructions are active" + "SessionProperty can only be accessed while a session profile, dynamic instructions, or a Tool is active" ) } values[keyPath: keyPath] = newValue @@ -112,3 +130,7 @@ extension LanguageModelSession.SessionProperty: @unchecked Sendable where Value: extension DynamicInstructions { public typealias SessionProperty = LanguageModelSession.SessionProperty } + +extension Tool { + public typealias SessionProperty = LanguageModelSession.SessionProperty +} diff --git a/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift b/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift index 2afe9d3e..c1e27220 100644 --- a/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift +++ b/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift @@ -28,6 +28,26 @@ import Testing } } + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) + private struct CompatibilityDynamicProfile: LanguageModelSession.DynamicProfile { + let model: any LanguageModel + @SessionProperty(\.compatibilityRevision) private var revision + + var body: some LanguageModelSession.DynamicProfile { + Profile { + Instructions("Revision \(revision)") + } + .model(model) + .samplingMode(.greedy) + .temperature(0.2) + .maximumResponseTokens(32) + .reasoningLevel(.light) + .toolCallingMode(.allowed) + .historyTransform { $0 } + .transcriptErrorHandlingPolicy(nil) + } + } + @available(macOS 26.0, iOS 26.0, tvOS 26.0, visionOS 26.0, *) @Test("AnyLanguageModel Drop-In Compatibility", .enabled(if: isSystemLanguageModelAvailable)) func anyLanguageModelCompatibility() async throws { @@ -45,6 +65,10 @@ import Testing ) dynamicSession.properties.compatibilityRevision = 1 _ = dynamicSession.properties.history + _ = LanguageModelSession( + profile: CompatibilityDynamicProfile(model: model), + history: session.transcript + ) } let options = GenerationOptions(temperature: 0.7) diff --git a/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift b/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift index d8d0c51e..532f90ec 100644 --- a/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift +++ b/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift @@ -29,6 +29,26 @@ import Testing } } } + + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) + private struct CompatibilityDynamicProfile: LanguageModelSession.DynamicProfile { + let model: any LanguageModel + @SessionProperty(\.compatibilityRevision) private var revision + + var body: some LanguageModelSession.DynamicProfile { + Profile { + Instructions("Revision \(revision)") + } + .model(model) + .samplingMode(.greedy) + .temperature(0.2) + .maximumResponseTokens(32) + .reasoningLevel(.light) + .toolCallingMode(.allowed) + .historyTransform { $0 } + .transcriptErrorHandlingPolicy(nil) + } + } #endif #endif @@ -54,6 +74,10 @@ import Testing ) dynamicSession.properties.compatibilityRevision = 1 _ = dynamicSession.properties.history + _ = LanguageModelSession( + profile: CompatibilityDynamicProfile(model: model), + history: session.transcript + ) } #endif #endif diff --git a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift new file mode 100644 index 00000000..64ec740f --- /dev/null +++ b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift @@ -0,0 +1,491 @@ +import Foundation +import Testing + +@_spi(Compatibility) @testable import AnyLanguageModel + +extension SessionPropertyValues { + @SessionPropertyEntry var profileRevision = 0 +} + +@Suite("Dynamic profiles") +struct DynamicProfileTests { + @Test func sameSessionReevaluatesModelInstructionsOptionsAndProperties() async throws { + let state = ProfileFixtureState() + let first = ProfileFixtureModel(id: "model-a", state: state) + let second = ProfileFixtureModel(id: "model-b", state: state) + let session = LanguageModelSession( + profile: FixtureProfile(state: state, first: first, second: second) + ) + + _ = try await session.respond(to: "First") + state.selectSecond() + session.properties.profileRevision = 7 + _ = try await session.respond(to: "Second") + + let requests = state.requests.withLock { $0 } + #expect(requests.map(\.modelID) == ["model-a", "model-b"]) + #expect(requests.map(\.instructions) == ["Profile A revision 0", "Profile B revision 7"]) + #expect(requests.map(\.temperature) == [0.2, 0.8]) + #expect(requests.map(\.maximumResponseTokens) == [64, 128]) + #expect(requests.map(\.reasoningLevel) == [.light, .deep]) + #expect(session.transcript.count == 4) + #expect(!session.transcript.contains { if case .instructions = $0 { true } else { false } }) + } + + @Test func callSiteOptionsOverrideProfileDefaults() async throws { + let state = ProfileFixtureState() + let model = ProfileFixtureModel(id: "model", state: state) + let session = LanguageModelSession( + profile: FixtureProfile(state: state, first: model, second: model) + ) + + _ = try await session.respond( + to: "Override", + options: .init( + temperature: 0.45, + maximumResponseTokens: 17, + toolCallingMode: .required + ) + ) + + let request = try #require(state.requests.withLock { $0.last }) + #expect(request.temperature == 0.45) + #expect(request.maximumResponseTokens == 17) + #expect(request.toolCallingMode == .required) + #expect(request.reasoningLevel == .light) + } + + @Test func lifecycleCallbacksAccumulateInOrderForResponseAndStream() async throws { + let state = LifecycleFixtureState() + let model = LifecycleFixtureModel() + let session = LanguageModelSession(profile: LifecycleFixtureProfile(state: state, model: model)) + + _ = try await session.respond(to: "One") + _ = try await session.streamResponse(to: "Two").collect() + state.useSecond.withLock { $0 = true } + _ = try await session.respond(to: "Three") + + #expect( + state.events.withLock { $0 } == [ + "outer-activate:A", "inner-activate:A", "outer-prompt:A", "inner-prompt:A", + "outer-reasoning:A", "inner-reasoning:A", "outer-response:A", "inner-response:A", + "outer-prompt:A", "inner-prompt:A", "outer-reasoning:A", "inner-reasoning:A", + "outer-response:A", "inner-response:A", "inner-deactivate:A", "outer-deactivate:A", + "outer-activate:B", "inner-activate:B", "outer-prompt:B", "inner-prompt:B", + "outer-reasoning:B", "inner-reasoning:B", "outer-response:B", "inner-response:B", + ] + ) + } + + @Test(arguments: [false, true]) + func callbackErrorsPropagateWithoutCommittingAResponse(streaming: Bool) async throws { + let model = LifecycleFixtureModel() + let session = LanguageModelSession(profile: ThrowingResponseProfile(model: model)) + + if streaming { + await #expect(throws: LifecycleFixtureError.self) { + _ = try await session.streamResponse(to: "Fail").collect() + } + } else { + await #expect(throws: LifecycleFixtureError.self) { + _ = try await session.respond(to: "Fail") + } + } + + #expect(session.transcript.count == 1) + #expect(session.transcript.allSatisfy { if case .prompt = $0 { true } else { false } }) + } + + @Test func dynamicInstructionsAndToolsShareSessionProperties() async throws { + let model = ProfileFixtureModel(id: "model", state: ProfileFixtureState()) + let session = LanguageModelSession( + model: model, + dynamicInstructions: PropertyInstructions() + ) + session.properties.profileRevision = 4 + + let context = session.resolvedRequestContext() + #expect(context.instructions?.description == "Revision 4") + + let tool = PropertyFixtureTool() + _ = try await session.withProfileToolExecution( + requestContext: context, + currentRoundEntries: [] + ) { + try await tool.makeOutputSegments(from: GeneratedContent(properties: [:])) + } + #expect(session.properties.profileRevision == 5) + } + + @Test func rehydrationKeepsCanonicalHistoryAndUsesTheCurrentProfile() async throws { + let oldState = ProfileFixtureState() + let model = ProfileFixtureModel(id: "model", state: oldState) + let oldSession = LanguageModelSession( + profile: FixtureProfile(state: oldState, first: model, second: model) + ) + _ = try await oldSession.respond(to: "Old") + let saved = oldSession.transcript + + let restoredState = ProfileFixtureState() + restoredState.selectSecond() + let restoredModel = ProfileFixtureModel(id: "restored", state: restoredState) + let restored = LanguageModelSession( + profile: FixtureProfile( + state: restoredState, + first: restoredModel, + second: restoredModel + ), + history: saved + ) + _ = try await restored.respond(to: "New") + + #expect(restoredState.requests.withLock { $0.last?.instructions } == "Profile B revision 0") + #expect(restored.transcript.count == saved.count + 2) + #expect(!restored.transcript.contains { if case .instructions = $0 { true } else { false } }) + } + + @Test func transformedCurrentRoundDoesNotReplaceCanonicalTranscript() async throws { + let state = ProfileFixtureState() + let model = ProfileFixtureModel(id: "model-a", state: state) + let session = LanguageModelSession( + profile: FixtureProfile(state: state, first: model, second: model) + ) + _ = try await session.respond(to: "First") + let durableBefore = session.transcript + let call = Transcript.ToolCall( + id: "call-1", + toolName: "fixture", + arguments: GeneratedContent(properties: [:]) + ) + let output = Transcript.ToolOutput( + id: call.id, + toolName: call.toolName, + segments: [.text(.init(content: "output"))] + ) + let inFlight: [Transcript.Entry] = [ + .toolCalls(.init([call])), + .toolOutput(output), + ] + + let context = try await session.resolvedRequestContext( + including: inFlight, + options: .init() + ) + + #expect(context.transcript.contains { $0.id == output.id }) + #expect(state.transformedHistory.withLock { $0.last?.contains(output.id) } == true) + #expect(session.transcript == durableBefore) + } + + @Test func toolCallbacksUseProducingSnapshotAndSessionPropertiesStayIsolated() async throws { + let state = ProfileFixtureState() + let model = ProfileFixtureModel(id: "model-a", state: state) + let first = LanguageModelSession( + profile: FixtureProfile(state: state, first: model, second: model) + ) + let second = LanguageModelSession( + profile: FixtureProfile(state: state, first: model, second: model) + ) + let call = Transcript.ToolCall( + id: "call", + toolName: "fixture", + arguments: GeneratedContent(properties: [:]) + ) + let output = Transcript.ToolOutput( + id: call.id, + toolName: call.toolName, + segments: [.text(.init(content: "done"))] + ) + let request = try await first.resolvedRequestContext(including: [], options: .init()) + + try await first.profileWillExecuteToolCall( + call, + requestContext: request, + currentRoundEntries: [.toolCalls(.init([call]))] + ) + try await first.profileDidProduceToolOutput( + for: call, + output: output, + requestContext: request, + currentRoundEntries: [.toolCalls(.init([call])), .toolOutput(output)] + ) + + #expect(first.properties.profileRevision == 1) + #expect(second.properties.profileRevision == 0) + #expect(state.callbackEvents.withLock { $0 } == ["call:call", "output:call"]) + } +} + +private struct FixtureProfile: LanguageModelSession.DynamicProfile, @unchecked Sendable { + let state: ProfileFixtureState + let first: ProfileFixtureModel + let second: ProfileFixtureModel + @SessionProperty(\.profileRevision) private var revision + + var body: some LanguageModelSession.DynamicProfile { + if state.usesSecond { + LanguageModelSession.Profile { + Instructions("Profile B revision \(revision)") + FixtureProfileTool() + } + .model(second) + .temperature(0.8) + .maximumResponseTokens(128) + .reasoningLevel(.deep) + .historyTransform(recordingTransform) + .onToolCall(perform: recordCall) + .onToolOutput(perform: recordOutput) + } else { + LanguageModelSession.Profile { + Instructions("Profile A revision \(revision)") + FixtureProfileTool() + } + .model(first) + .temperature(0.2) + .maximumResponseTokens(64) + .reasoningLevel(.light) + .historyTransform(recordingTransform) + .onToolCall(perform: recordCall) + .onToolOutput(perform: recordOutput) + } + } + + private func recordingTransform(_ entries: [Transcript.Entry]) -> [Transcript.Entry] { + state.transformedHistory.withLock { $0.append(entries.map(\.id)) } + return entries + } + + private func recordCall(_ call: Transcript.ToolCall) async throws { + state.callbackEvents.withLock { $0.append("call:\(call.id)") } + } + + private func recordOutput( + _ call: Transcript.ToolCall, + _ output: Transcript.ToolOutput + ) async throws { + revision += 1 + state.callbackEvents.withLock { $0.append("output:\(output.id)") } + } +} + +private struct FixtureProfileTool: Tool { + let name = "fixture" + let description = "Fixture" + + @Generable + struct Arguments {} + + func call(arguments: Arguments) async throws -> String { "done" } +} + +private struct ProfileFixtureModel: LanguageModel { + typealias UnavailableReason = Never + let id: String + let state: ProfileFixtureState + + func respond( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) async throws -> LanguageModelSession.Response where Content: Generable { + let context = try await session.resolvedRequestContext(including: [], options: options) + state.requests.withLock { + $0.append( + .init( + modelID: id, + instructions: context.instructions?.description, + temperature: context.options.temperature, + maximumResponseTokens: context.options.maximumResponseTokens, + toolCallingMode: context.options.toolCallingMode, + reasoningLevel: context.contextOptions.reasoningLevel + ) + ) + } + let raw = GeneratedContent("response") + return .init(content: try Content(raw), rawContent: raw, transcriptEntries: []) + } + + func streamResponse( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) -> sending LanguageModelSession.ResponseStream where Content: Generable { + let stream = AsyncThrowingStream.Snapshot, any Error> { + continuation in + let task = Task { + do { + let response: LanguageModelSession.Response = try await respond( + within: session, + to: prompt, + generating: type, + includeSchemaInPrompt: includeSchemaInPrompt, + options: options + ) + continuation.yield( + .init( + content: response.content.asPartiallyGenerated(), + rawContent: response.rawContent + ) + ) + continuation.finish() + } catch { + continuation.finish(throwing: error) + } + } + continuation.onTermination = { _ in task.cancel() } + } + return .init(stream: stream) + } +} + +private final class ProfileFixtureState: @unchecked Sendable { + struct Request: Equatable { + let modelID: String + let instructions: String? + let temperature: Double? + let maximumResponseTokens: Int? + let toolCallingMode: GenerationOptions.ToolCallingMode? + let reasoningLevel: ContextOptions.ReasoningLevel? + } + + private let selection = Locked(false) + let requests = Locked<[Request]>([]) + let transformedHistory = Locked<[[String]]>([]) + let callbackEvents = Locked<[String]>([]) + + var usesSecond: Bool { selection.withLock { $0 } } + func selectSecond() { selection.withLock { $0 = true } } +} + +private struct PropertyInstructions: DynamicInstructions, @unchecked Sendable { + @SessionProperty(\.profileRevision) private var revision + + var body: some DynamicInstructions { + Instructions("Revision \(revision)") + } +} + +private struct PropertyFixtureTool: Tool, @unchecked Sendable { + let name = "property" + let description = "Updates a session property" + @SessionProperty(\.profileRevision) private var revision + + @Generable struct Arguments {} + + func call(arguments: Arguments) async throws -> String { + revision += 1 + return "updated" + } +} + +private enum LifecycleFixtureError: Error { + case rejected +} + +private final class LifecycleFixtureState: @unchecked Sendable { + let useSecond = Locked(false) + let events = Locked<[String]>([]) + + func record(_ value: String) { + events.withLock { $0.append(value) } + } +} + +private struct LifecycleFixtureProfile: LanguageModelSession.DynamicProfile, @unchecked Sendable { + let state: LifecycleFixtureState + let model: LifecycleFixtureModel + + var body: some LanguageModelSession.DynamicProfile { + if state.useSecond.withLock({ $0 }) { + profile(label: "B") + } else { + profile(label: "A") + } + } + + private func profile(label: String) -> some LanguageModelSession.DynamicProfile { + LanguageModelSession.Profile { Instructions("Lifecycle \(label)") } + .model(model) + .onActivate { state.record("inner-activate:\(label)") } + .onDeactivate { state.record("inner-deactivate:\(label)") } + .onPrompt { state.record("inner-prompt:\(label)") } + .onReasoning { state.record("inner-reasoning:\(label)") } + .onResponse { state.record("inner-response:\(label)") } + .onActivate { state.record("outer-activate:\(label)") } + .onDeactivate { state.record("outer-deactivate:\(label)") } + .onPrompt { state.record("outer-prompt:\(label)") } + .onReasoning { state.record("outer-reasoning:\(label)") } + .onResponse { state.record("outer-response:\(label)") } + } +} + +private struct ThrowingResponseProfile: LanguageModelSession.DynamicProfile { + let model: LifecycleFixtureModel + + var body: some LanguageModelSession.DynamicProfile { + LanguageModelSession.Profile { Instructions("Throw after response") } + .model(model) + .onResponse { throw LifecycleFixtureError.rejected } + } +} + +private struct LifecycleFixtureModel: LanguageModel { + typealias UnavailableReason = Never + + func respond( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) async throws -> LanguageModelSession.Response where Content: Generable { + let raw = GeneratedContent("response") + let reasoning = Transcript.Entry.reasoning( + .init(segments: [.text(.init(content: "reasoning"))]) + ) + return .init( + content: try Content(raw), + rawContent: raw, + transcriptEntries: [reasoning] + ) + } + + func streamResponse( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) -> sending LanguageModelSession.ResponseStream where Content: Generable { + let stream = AsyncThrowingStream.Snapshot, any Error> { + continuation in + let task = Task { + do { + let response: LanguageModelSession.Response = try await respond( + within: session, + to: prompt, + generating: type, + includeSchemaInPrompt: includeSchemaInPrompt, + options: options + ) + continuation.yield( + .init( + content: response.content.asPartiallyGenerated(), + rawContent: response.rawContent, + transcriptEntries: response.transcriptEntries + ) + ) + continuation.finish() + } catch { + continuation.finish(throwing: error) + } + } + continuation.onTermination = { _ in task.cancel() } + } + return .init(stream: stream) + } +} From da630296a0a5bfb37d21e0e71c14f84e0150a3ea Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Mon, 12 Oct 2026 09:14:45 +0800 Subject: [PATCH 05/18] Keep session property requests observable --- .../LanguageModelSession.swift | 33 ++++++-- .../AnyLanguageModel/SessionProperties.swift | 20 +++++ .../SessionPropertyEntryMacro.swift | 15 +++- .../SessionPropertyTests.swift | 78 +++++++++++++++++++ 4 files changed, 137 insertions(+), 9 deletions(-) diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 10627f0d..59a1c075 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -183,7 +183,7 @@ public final class LanguageModelSession: @unchecked Sendable { } } } - var requestTranscript = transcript + var requestTranscript = Transcript(entries: canonicalHistory) if let instructions = resolved.instructions { let instructionsEntry = Transcript.Entry.instructions( Transcript.Instructions( @@ -332,6 +332,16 @@ public final class LanguageModelSession: @unchecked Sendable { let state = Locked(State(finalTranscript)) self.state = state + let setHistory: @Sendable (Transcript.HistoryView) -> Void = { history in + state.withLock { locked in + let instructions = locked.transcript.prefix { + if case .instructions = $0 { return true } + return false + } + locked.transcript = Transcript(entries: Array(instructions) + history) + } + } + let observedHistorySetter = Locked<(@Sendable (Transcript.HistoryView) -> Void)?>(nil) self.properties = SessionPropertyValues( historyGetter: { state.withLock { locked in @@ -342,15 +352,24 @@ public final class LanguageModelSession: @unchecked Sendable { } }, historySetter: { history in - state.withLock { locked in - let instructions = locked.transcript.prefix { - if case .instructions = $0 { return true } - return false - } - locked.transcript = Transcript(entries: Array(instructions) + history) + if let setter = observedHistorySetter.withLock({ $0 }) { + setter(history) + } else { + setHistory(history) } } ) + observedHistorySetter.withLock { setter in + setter = { [weak self] history in + guard let self else { + setHistory(history) + return + } + self.withMutation(keyPath: \.transcript) { + setHistory(history) + } + } + } } private static func instructions(from transcript: Transcript) -> Instructions? { diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift index 9b503bff..0adfadb8 100644 --- a/Sources/AnyLanguageModel/SessionProperties.swift +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -30,6 +30,7 @@ public macro SessionPropertyEntry() = /// Foundation Models 27. @Observable public final class SessionPropertyValues: @unchecked Sendable { + @ObservationIgnored private let sessionPropertyRegistrar = ObservationRegistrar() @ObservationIgnored private let customValues = Locked<[ObjectIdentifier: Any]>([:]) @ObservationIgnored private let historyGetter: @Sendable () -> Transcript.HistoryView @ObservationIgnored private let historySetter: @Sendable (Transcript.HistoryView) -> Void @@ -69,6 +70,25 @@ public final class SessionPropertyValues: @unchecked Sendable { } } } + + @_documentation(visibility: internal) + public func _accessSessionProperty( + keyPath: KeyPath + ) { + sessionPropertyRegistrar.access(self, keyPath: keyPath) + } + + @_documentation(visibility: internal) + public func _withSessionPropertyMutation( + keyPath: KeyPath, + _ mutation: () throws -> Result + ) rethrows -> Result { + try sessionPropertyRegistrar.withMutation( + of: self, + keyPath: keyPath, + mutation + ) + } } enum SessionPropertyBinding { diff --git a/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift b/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift index 1aae689b..823aae64 100644 --- a/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift +++ b/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift @@ -12,8 +12,19 @@ public struct SessionPropertyEntryMacro: AccessorMacro, PeerMacro { ) throws -> [AccessorDeclSyntax] { guard let name = propertyName(in: declaration) else { return [] } return [ - "get { self[__Key_\(raw: name).self] }", - "set { self[__Key_\(raw: name).self] = newValue }", + """ + get { + self._accessSessionProperty(keyPath: \\.\(raw: name)) + return self[__Key_\(raw: name).self] + } + """, + """ + set { + self._withSessionPropertyMutation(keyPath: \\.\(raw: name)) { + self[__Key_\(raw: name).self] = newValue + } + } + """, ] } diff --git a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift index d35425b8..e8421891 100644 --- a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift +++ b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift @@ -42,6 +42,71 @@ struct SessionPropertyTests { #expect(session.properties.history == [prompt]) } + @Test func requestUsesOneHistorySnapshotDuringDynamicResolution() async { + let initial = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Initial"))]) + ) + let replacement = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Replacement"))]) + ) + let (started, startedContinuation) = AsyncStream.makeStream() + let resume = DispatchSemaphore(value: 0) + let session = LanguageModelSession( + model: SessionPropertyFixtureModel(), + dynamicInstructions: BlockingHistoryInstructions( + started: startedContinuation, + resume: resume + ), + history: [initial] + ) + + let request = Task.detached { session.resolvedRequestContext() } + for await _ in started.prefix(1) {} + session.properties.history = [replacement] + resume.signal() + let context = await request.value + + #expect(context.instructions?.description == "History count 1") + #expect( + context.transcript.filter { entry in + if case .instructions = entry { return false } + return true + } == [initial] + ) + #expect(session.properties.history == [replacement]) + } + + @Test func replacingHistoryNotifiesTranscriptObservers() { + let session = LanguageModelSession(model: SessionPropertyFixtureModel()) + let changed = Locked(false) + let prompt = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Replacement"))]) + ) + + withObservationTracking { + _ = session.transcript + } onChange: { + changed.withLock { $0 = true } + } + session.properties.history = [prompt] + + #expect(changed.withLock { $0 }) + } + + @Test func generatedPropertiesParticipateInObservation() { + let session = LanguageModelSession(model: SessionPropertyFixtureModel()) + let changed = Locked(false) + + withObservationTracking { + _ = session.properties.revision + } onChange: { + changed.withLock { $0 = true } + } + session.properties.revision = 1 + + #expect(changed.withLock { $0 }) + } + @Test func propertiesAndHistoryAreIsolatedBetweenSessions() { let first = LanguageModelSession(model: SessionPropertyFixtureModel()) let second = LanguageModelSession(model: SessionPropertyFixtureModel()) @@ -98,6 +163,19 @@ private struct SessionPropertyHistoryInstructions: DynamicInstructions { } } +private struct BlockingHistoryInstructions: DynamicInstructions { + @SessionProperty(\.history) private var history + let started: AsyncStream.Continuation + let resume: DispatchSemaphore + + var body: some DynamicInstructions { + let count = history.count + started.yield() + resume.wait() + return Instructions("History count \(count)") + } +} + private struct SessionPropertyFixtureModel: LanguageModel { typealias UnavailableReason = Never From 8b3a234ff6c4248c6540940e420e67519fe5a4a8 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Mon, 12 Oct 2026 09:14:45 +0800 Subject: [PATCH 06/18] Keep session property requests observable --- .../LanguageModelSession.swift | 31 ++++++-- .../AnyLanguageModel/SessionProperties.swift | 20 +++++ .../SessionPropertyEntryMacro.swift | 15 +++- .../SessionPropertyTests.swift | 78 +++++++++++++++++++ 4 files changed, 136 insertions(+), 8 deletions(-) diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index e6163b30..6b9a0e81 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -621,6 +621,16 @@ public final class LanguageModelSession: @unchecked Sendable { let state = Locked(State(finalTranscript)) self.state = state + let setHistory: @Sendable (Transcript.HistoryView) -> Void = { history in + state.withLock { locked in + let instructions = locked.transcript.prefix { + if case .instructions = $0 { return true } + return false + } + locked.transcript = Transcript(entries: Array(instructions) + history) + } + } + let observedHistorySetter = Locked<(@Sendable (Transcript.HistoryView) -> Void)?>(nil) self.properties = SessionPropertyValues( historyGetter: { state.withLock { locked in @@ -631,15 +641,24 @@ public final class LanguageModelSession: @unchecked Sendable { } }, historySetter: { history in - state.withLock { locked in - let instructions = locked.transcript.prefix { - if case .instructions = $0 { return true } - return false - } - locked.transcript = Transcript(entries: Array(instructions) + history) + if let setter = observedHistorySetter.withLock({ $0 }) { + setter(history) + } else { + setHistory(history) } } ) + observedHistorySetter.withLock { setter in + setter = { [weak self] history in + guard let self else { + setHistory(history) + return + } + self.withMutation(keyPath: \.transcript) { + setHistory(history) + } + } + } } private static func instructions(from transcript: Transcript) -> Instructions? { diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift index 27b22188..5c8ab58e 100644 --- a/Sources/AnyLanguageModel/SessionProperties.swift +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -30,6 +30,7 @@ public macro SessionPropertyEntry() = /// Foundation Models 27. @Observable public final class SessionPropertyValues: @unchecked Sendable { + @ObservationIgnored private let sessionPropertyRegistrar = ObservationRegistrar() @ObservationIgnored private let customValues = Locked<[ObjectIdentifier: Any]>([:]) @ObservationIgnored private let historyGetter: @Sendable () -> Transcript.HistoryView @ObservationIgnored private let historySetter: @Sendable (Transcript.HistoryView) -> Void @@ -72,6 +73,25 @@ public final class SessionPropertyValues: @unchecked Sendable { } } } + + @_documentation(visibility: internal) + public func _accessSessionProperty( + keyPath: KeyPath + ) { + sessionPropertyRegistrar.access(self, keyPath: keyPath) + } + + @_documentation(visibility: internal) + public func _withSessionPropertyMutation( + keyPath: KeyPath, + _ mutation: () throws -> Result + ) rethrows -> Result { + try sessionPropertyRegistrar.withMutation( + of: self, + keyPath: keyPath, + mutation + ) + } } enum SessionPropertyBinding { diff --git a/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift b/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift index 1aae689b..823aae64 100644 --- a/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift +++ b/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift @@ -12,8 +12,19 @@ public struct SessionPropertyEntryMacro: AccessorMacro, PeerMacro { ) throws -> [AccessorDeclSyntax] { guard let name = propertyName(in: declaration) else { return [] } return [ - "get { self[__Key_\(raw: name).self] }", - "set { self[__Key_\(raw: name).self] = newValue }", + """ + get { + self._accessSessionProperty(keyPath: \\.\(raw: name)) + return self[__Key_\(raw: name).self] + } + """, + """ + set { + self._withSessionPropertyMutation(keyPath: \\.\(raw: name)) { + self[__Key_\(raw: name).self] = newValue + } + } + """, ] } diff --git a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift index d35425b8..e8421891 100644 --- a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift +++ b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift @@ -42,6 +42,71 @@ struct SessionPropertyTests { #expect(session.properties.history == [prompt]) } + @Test func requestUsesOneHistorySnapshotDuringDynamicResolution() async { + let initial = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Initial"))]) + ) + let replacement = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Replacement"))]) + ) + let (started, startedContinuation) = AsyncStream.makeStream() + let resume = DispatchSemaphore(value: 0) + let session = LanguageModelSession( + model: SessionPropertyFixtureModel(), + dynamicInstructions: BlockingHistoryInstructions( + started: startedContinuation, + resume: resume + ), + history: [initial] + ) + + let request = Task.detached { session.resolvedRequestContext() } + for await _ in started.prefix(1) {} + session.properties.history = [replacement] + resume.signal() + let context = await request.value + + #expect(context.instructions?.description == "History count 1") + #expect( + context.transcript.filter { entry in + if case .instructions = entry { return false } + return true + } == [initial] + ) + #expect(session.properties.history == [replacement]) + } + + @Test func replacingHistoryNotifiesTranscriptObservers() { + let session = LanguageModelSession(model: SessionPropertyFixtureModel()) + let changed = Locked(false) + let prompt = Transcript.Entry.prompt( + Transcript.Prompt(segments: [.text(.init(content: "Replacement"))]) + ) + + withObservationTracking { + _ = session.transcript + } onChange: { + changed.withLock { $0 = true } + } + session.properties.history = [prompt] + + #expect(changed.withLock { $0 }) + } + + @Test func generatedPropertiesParticipateInObservation() { + let session = LanguageModelSession(model: SessionPropertyFixtureModel()) + let changed = Locked(false) + + withObservationTracking { + _ = session.properties.revision + } onChange: { + changed.withLock { $0 = true } + } + session.properties.revision = 1 + + #expect(changed.withLock { $0 }) + } + @Test func propertiesAndHistoryAreIsolatedBetweenSessions() { let first = LanguageModelSession(model: SessionPropertyFixtureModel()) let second = LanguageModelSession(model: SessionPropertyFixtureModel()) @@ -98,6 +163,19 @@ private struct SessionPropertyHistoryInstructions: DynamicInstructions { } } +private struct BlockingHistoryInstructions: DynamicInstructions { + @SessionProperty(\.history) private var history + let started: AsyncStream.Continuation + let resume: DispatchSemaphore + + var body: some DynamicInstructions { + let count = history.count + started.yield() + resume.wait() + return Instructions("History count \(count)") + } +} + private struct SessionPropertyFixtureModel: LanguageModel { typealias UnavailableReason = Never From d0cae54870d015b9451b52ae1c506a19f20d5f4e Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Tue, 13 Oct 2026 09:19:17 +0800 Subject: [PATCH 07/18] Isolate dynamic profile request state --- Sources/AnyLanguageModel/DynamicProfile.swift | 28 ++++ .../LanguageModelSession.swift | 121 ++++++++++++++---- .../AnyLanguageModel/SessionProperties.swift | 4 + .../DynamicProfileTests.swift | 84 +++++++++++- .../SessionPropertyTests.swift | 18 +++ 5 files changed, 225 insertions(+), 30 deletions(-) diff --git a/Sources/AnyLanguageModel/DynamicProfile.swift b/Sources/AnyLanguageModel/DynamicProfile.swift index 6e4c0668..111159fb 100644 --- a/Sources/AnyLanguageModel/DynamicProfile.swift +++ b/Sources/AnyLanguageModel/DynamicProfile.swift @@ -475,11 +475,39 @@ private func resolveDynamicProfile( actor ProfileLifecycle { private var activeProfile: ResolvedDynamicProfile? private var lastPromptID: String? + private var pendingPreparation: (id: Int, task: Task)? + private var nextPreparationID = 0 func prepare( _ profile: ResolvedDynamicProfile, properties: SessionPropertyValues, history: [Transcript.Entry] + ) async throws { + let previous = pendingPreparation?.task + nextPreparationID += 1 + let id = nextPreparationID + let task = Task { + _ = try? await previous?.value + try await self.performPreparation( + profile, + properties: properties, + history: history + ) + } + pendingPreparation = (id, task) + do { + try await task.value + if pendingPreparation?.id == id { pendingPreparation = nil } + } catch { + if pendingPreparation?.id == id { pendingPreparation = nil } + throw error + } + } + + private func performPreparation( + _ profile: ResolvedDynamicProfile, + properties: SessionPropertyValues, + history: [Transcript.Entry] ) async throws { if activeProfile?.identity != profile.identity { if let previous = activeProfile { diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 6b9a0e81..784fa438 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -165,6 +165,7 @@ public final class LanguageModelSession: @unchecked Sendable { fileprivate let profile: ResolvedDynamicProfile? fileprivate let canonicalTranscript: [Transcript.Entry] + fileprivate let transcriptErrorHandlingPolicy: TranscriptErrorHandlingPolicy? fileprivate let callbackState: ProfileCallbackState fileprivate init( @@ -176,6 +177,7 @@ public final class LanguageModelSession: @unchecked Sendable { contextOptions: ContextOptions = .init(), profile: ResolvedDynamicProfile? = nil, canonicalTranscript: [Transcript.Entry]? = nil, + transcriptErrorHandlingPolicy: TranscriptErrorHandlingPolicy? = nil, callbackState: ProfileCallbackState = ProfileCallbackState() ) { self.transcript = transcript @@ -186,6 +188,7 @@ public final class LanguageModelSession: @unchecked Sendable { self.contextOptions = contextOptions self.profile = profile self.canonicalTranscript = canonicalTranscript ?? Array(transcript) + self.transcriptErrorHandlingPolicy = transcriptErrorHandlingPolicy self.callbackState = callbackState } @@ -199,6 +202,7 @@ public final class LanguageModelSession: @unchecked Sendable { contextOptions: contextOptions, profile: profile, canonicalTranscript: canonicalTranscript, + transcriptErrorHandlingPolicy: transcriptErrorHandlingPolicy, callbackState: context.callbackState ) } @@ -253,6 +257,9 @@ public final class LanguageModelSession: @unchecked Sendable { context = context.sharingCallbackState(with: prepared.context) } if let profile = context.profile { + if let prepared, prepared.session === self { + prepared.context = context + } try await profileLifecycle.prepare( profile, properties: properties, @@ -347,9 +354,12 @@ public final class LanguageModelSession: @unchecked Sendable { if resolvedOptions.toolCallingMode == nil, resolved.hasToolCallingMode { resolvedOptions.toolCallingMode = resolved.toolCallingMode } - if resolved.hasTranscriptErrorHandlingPolicy { - state.withLock { $0.transcriptErrorHandlingPolicy = resolved.transcriptErrorHandlingPolicy } - } + let transcriptErrorHandlingPolicy = + if resolved.hasTranscriptErrorHandlingPolicy { + resolved.transcriptErrorHandlingPolicy + } else { + state.withLock { $0.transcriptErrorHandlingPolicy } + } return RequestContext( transcript: requestTranscript, instructions: resolved.instructions, @@ -358,7 +368,8 @@ public final class LanguageModelSession: @unchecked Sendable { options: resolvedOptions, contextOptions: .init(reasoningLevel: resolved.reasoningLevel), profile: resolved, - canonicalTranscript: canonical + canonicalTranscript: canonical, + transcriptErrorHandlingPolicy: transcriptErrorHandlingPolicy ) } @@ -384,7 +395,7 @@ public final class LanguageModelSession: @unchecked Sendable { currentRoundEntries: [Transcript.Entry] ) async throws { guard let profile = requestContext.profile else { return } - let history = Array(transcript) + currentRoundEntries + let history = requestContext.canonicalTranscript + currentRoundEntries try await withSessionProperties(history: history, isHistoryWritable: true) { for action in profile.onToolCall { try await action(call) } } @@ -398,7 +409,7 @@ public final class LanguageModelSession: @unchecked Sendable { currentRoundEntries: [Transcript.Entry] ) async throws { guard let profile = requestContext.profile else { return } - let history = Array(transcript) + currentRoundEntries + let history = requestContext.canonicalTranscript + currentRoundEntries try await withSessionProperties(history: history, isHistoryWritable: true) { for action in profile.onToolOutput { try await action(call, output) } } @@ -420,7 +431,7 @@ public final class LanguageModelSession: @unchecked Sendable { if requestContext.callbackState.wasDispatched(entry.id) { continue } - let history = Array(transcript) + currentRoundEntries + let history = requestContext.canonicalTranscript + currentRoundEntries switch entry { case .reasoning(let reasoning): try await withSessionProperties(history: history, isHistoryWritable: true) { @@ -445,7 +456,7 @@ public final class LanguageModelSession: @unchecked Sendable { currentRoundEntries: [Transcript.Entry], operation: @Sendable () async throws -> Result ) async rethrows -> Result { - let history = Array(transcript) + currentRoundEntries + let history = requestContext.canonicalTranscript + currentRoundEntries return try await withSessionProperties(history: history, isHistoryWritable: false) { try await operation() } @@ -704,7 +715,10 @@ public final class LanguageModelSession: @unchecked Sendable { } } - nonisolated private func wrapRespond(_ operation: (String) async throws -> T) async throws -> T { + nonisolated private func wrapRespond( + profileTracker: PreparedProfileRequestTracker? = nil, + _ operation: (String) async throws -> T + ) async throws -> T { let promptID = UUID().uuidString beginResponding() do { @@ -712,7 +726,13 @@ public final class LanguageModelSession: @unchecked Sendable { endResponding() return result } catch { - if transcriptErrorHandlingPolicy == .revertTranscript { + let policy: TranscriptErrorHandlingPolicy? + if let prepared = profileTracker?.prepared { + policy = prepared.context.transcriptErrorHandlingPolicy + } else { + policy = transcriptErrorHandlingPolicy + } + if policy == .revertTranscript { removePrompt(id: promptID) } endResponding() @@ -836,9 +856,15 @@ public final class LanguageModelSession: @unchecked Sendable { } ) } - if state.transcriptErrorHandlingPolicy == .preserveTranscript { + let policy: TranscriptErrorHandlingPolicy? + if let prepared = profileTracker?.prepared { + policy = prepared.context.transcriptErrorHandlingPolicy + } else { + policy = state.transcriptErrorHandlingPolicy + } + if policy == .preserveTranscript { state.transcript.append(contentsOf: lastSnapshot?.transcriptEntries ?? []) - } else if state.transcriptErrorHandlingPolicy == .revertTranscript { + } else if policy == .revertTranscript { state.transcript = Transcript( entries: state.transcript.filter { $0.id != promptEntry.id } ) @@ -1057,12 +1083,17 @@ public final class LanguageModelSession: @unchecked Sendable { includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions() ) async throws -> Response where Content: Generable { - try await respond( + let profileTracker = dynamicProfile == nil ? nil : PreparedProfileRequestTracker() + return try await respond( to: prompt, responseFormat: type == String.self ? nil : .init(type: type), - options: options + options: options, + profileTracker: profileTracker ) { - try await profiledResponse(options: options) { model, resolvedOptions, contextOptions in + try await profiledResponse(options: options, tracker: profileTracker) { + model, + resolvedOptions, + contextOptions in try await model.respond( within: self, to: prompt, @@ -1077,6 +1108,7 @@ public final class LanguageModelSession: @unchecked Sendable { nonisolated private func profiledResponse( options: GenerationOptions, + tracker: PreparedProfileRequestTracker?, generate: ( any LanguageModel, GenerationOptions, @@ -1089,14 +1121,22 @@ public final class LanguageModelSession: @unchecked Sendable { prepared: nil ) } - let context = try await resolvedRequestContext(including: [], options: options) + let context = resolveRequestContext(additionalEntries: [], options: options) let prepared = PreparedProfileRequest( session: self, context: context, callSiteOptions: options ) + tracker?.prepared = prepared let response = try await ProfileRequestBinding.$prepared.withValue(prepared) { - try await generate(context.model, context.options, context.contextOptions) + if let profile = context.profile { + try await profileLifecycle.prepare( + profile, + properties: properties, + history: context.canonicalTranscript + ) + } + return try await generate(context.model, context.options, context.contextOptions) } return ProfiledModelResponse(response: response, prepared: prepared) } @@ -1105,9 +1145,10 @@ public final class LanguageModelSession: @unchecked Sendable { to prompt: Prompt, responseFormat: Transcript.ResponseFormat?, options: GenerationOptions, + profileTracker: PreparedProfileRequestTracker?, generate: () async throws -> ProfiledModelResponse ) async throws -> Response { - try await wrapRespond { promptID in + try await wrapRespond(profileTracker: profileTracker) { promptID in // Add prompt to transcript let promptEntry = Transcript.Entry.prompt( Transcript.Prompt( @@ -1269,18 +1310,32 @@ public final class LanguageModelSession: @unchecked Sendable { let stream = AsyncThrowingStream.Snapshot, any Error> { continuation in let task = Task { do { - let context = try await self.resolvedRequestContext(including: [], options: options) + let context = self.resolveRequestContext( + additionalEntries: [], + options: options + ) let prepared = PreparedProfileRequest( session: self, context: context, callSiteOptions: options ) tracker.prepared = prepared - let upstream = ProfileRequestBinding.$prepared.withValue(prepared) { - generate(context.model, context.options, context.contextOptions) - } - for try await snapshot in upstream { - continuation.yield(snapshot) + try await ProfileRequestBinding.$prepared.withValue(prepared) { + if let profile = context.profile { + try await self.profileLifecycle.prepare( + profile, + properties: self.properties, + history: context.canonicalTranscript + ) + } + let upstream = generate( + context.model, + context.options, + context.contextOptions + ) + for try await snapshot in upstream { + continuation.yield(snapshot) + } } continuation.finish() } catch { @@ -1364,8 +1419,17 @@ extension LanguageModelSession { includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions() ) async throws -> Response { - try await respond(to: prompt, responseFormat: .init(schema: schema), options: options) { - try await profiledResponse(options: options) { model, resolvedOptions, contextOptions in + let profileTracker = dynamicProfile == nil ? nil : PreparedProfileRequestTracker() + return try await respond( + to: prompt, + responseFormat: .init(schema: schema), + options: options, + profileTracker: profileTracker + ) { + try await profiledResponse(options: options, tracker: profileTracker) { + model, + resolvedOptions, + contextOptions in try await model.respond( within: self, to: prompt, @@ -1632,7 +1696,8 @@ extension LanguageModelSession { includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions() ) async throws -> Response where Content: Generable { - try await wrapRespond { promptID in + let profileTracker = dynamicProfile == nil ? nil : PreparedProfileRequestTracker() + return try await wrapRespond(profileTracker: profileTracker) { promptID in // Build segments from text and images var segments: [Transcript.Segment] = [] if !prompt.isEmpty { @@ -1656,7 +1721,7 @@ extension LanguageModelSession { // Extract text content for the Prompt parameter let textPrompt = Prompt(prompt) - let profiled = try await profiledResponse(options: options) { + let profiled = try await profiledResponse(options: options, tracker: profileTracker) { model, resolvedOptions, contextOptions in diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift index 5c8ab58e..5b8078b8 100644 --- a/Sources/AnyLanguageModel/SessionProperties.swift +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -47,6 +47,10 @@ public final class SessionPropertyValues: @unchecked Sendable { public var history: Transcript.HistoryView { get { SessionPropertyBinding.history?.entries ?? historyGetter() } set { + let newValue = newValue.filter { entry in + if case .instructions = entry { return false } + return true + } if let history = SessionPropertyBinding.history { precondition( history.isWritable, diff --git a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift index 64ec740f..4f58a076 100644 --- a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift +++ b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift @@ -96,6 +96,43 @@ struct DynamicProfileTests { #expect(session.transcript.allSatisfy { if case .prompt = $0 { true } else { false } }) } + @Test(arguments: [false, true]) + func promptFailureUsesTheProducingRequestPolicy(streaming: Bool) async throws { + let state = PromptFailureProfileState() + let session = LanguageModelSession( + profile: PromptFailureProfile(state: state, model: LifecycleFixtureModel()) + ) + + if streaming { + await #expect(throws: LifecycleFixtureError.self) { + _ = try await session.streamResponse(to: "Revert").collect() + } + } else { + await #expect(throws: LifecycleFixtureError.self) { + _ = try await session.respond(to: "Revert") + } + } + #expect(session.transcript.isEmpty) + #expect(session.transcriptErrorHandlingPolicy == nil) + + state.useRevert.withLock { $0 = false } + if streaming { + await #expect(throws: LifecycleFixtureError.self) { + _ = try await session.streamResponse(to: "Keep").collect() + } + } else { + await #expect(throws: LifecycleFixtureError.self) { + _ = try await session.respond(to: "Keep") + } + } + #expect(session.transcript.count == 1) + guard case .prompt = session.transcript[0] else { + Issue.record("Expected the second request prompt to be retained") + return + } + #expect(session.transcriptErrorHandlingPolicy == nil) + } + @Test func dynamicInstructionsAndToolsShareSessionProperties() async throws { let model = ProfileFixtureModel(id: "model", state: ProfileFixtureState()) let session = LanguageModelSession( @@ -197,22 +234,39 @@ struct DynamicProfileTests { segments: [.text(.init(content: "done"))] ) let request = try await first.resolvedRequestContext(including: [], options: .init()) + let unrelated = Transcript.Entry.prompt( + .init(id: "unrelated", segments: [.text(.init(content: "Other request"))]) + ) + first.properties.history = [unrelated] + let callEntry = Transcript.Entry.toolCalls(.init([call])) + let outputEntry = Transcript.Entry.toolOutput(output) try await first.profileWillExecuteToolCall( call, requestContext: request, - currentRoundEntries: [.toolCalls(.init([call]))] + currentRoundEntries: [callEntry] ) try await first.profileDidProduceToolOutput( for: call, output: output, requestContext: request, - currentRoundEntries: [.toolCalls(.init([call])), .toolOutput(output)] + currentRoundEntries: [callEntry, outputEntry] ) + let toolHistory = await first.withProfileToolExecution( + requestContext: request, + currentRoundEntries: [callEntry, outputEntry] + ) { + first.properties.history.map(\.id) + } #expect(first.properties.profileRevision == 1) #expect(second.properties.profileRevision == 0) #expect(state.callbackEvents.withLock { $0 } == ["call:call", "output:call"]) + #expect( + state.callbackHistories.withLock { $0 } + == [[callEntry.id], [callEntry.id, outputEntry.id]] + ) + #expect(toolHistory == [callEntry.id, outputEntry.id]) } } @@ -221,6 +275,7 @@ private struct FixtureProfile: LanguageModelSession.DynamicProfile, @unchecked S let first: ProfileFixtureModel let second: ProfileFixtureModel @SessionProperty(\.profileRevision) private var revision + @SessionProperty(\.history) private var history var body: some LanguageModelSession.DynamicProfile { if state.usesSecond { @@ -257,6 +312,7 @@ private struct FixtureProfile: LanguageModelSession.DynamicProfile, @unchecked S private func recordCall(_ call: Transcript.ToolCall) async throws { state.callbackEvents.withLock { $0.append("call:\(call.id)") } + state.callbackHistories.withLock { $0.append(history.map(\.id)) } } private func recordOutput( @@ -265,6 +321,7 @@ private struct FixtureProfile: LanguageModelSession.DynamicProfile, @unchecked S ) async throws { revision += 1 state.callbackEvents.withLock { $0.append("output:\(output.id)") } + state.callbackHistories.withLock { $0.append(history.map(\.id)) } } } @@ -356,6 +413,7 @@ private final class ProfileFixtureState: @unchecked Sendable { let requests = Locked<[Request]>([]) let transformedHistory = Locked<[[String]]>([]) let callbackEvents = Locked<[String]>([]) + let callbackHistories = Locked<[[String]]>([]) var usesSecond: Bool { selection.withLock { $0 } } func selectSecond() { selection.withLock { $0 = true } } @@ -433,6 +491,28 @@ private struct ThrowingResponseProfile: LanguageModelSession.DynamicProfile { } } +private final class PromptFailureProfileState: @unchecked Sendable { + let useRevert = Locked(true) +} + +private struct PromptFailureProfile: LanguageModelSession.DynamicProfile, @unchecked Sendable { + let state: PromptFailureProfileState + let model: LifecycleFixtureModel + + var body: some LanguageModelSession.DynamicProfile { + if state.useRevert.withLock({ $0 }) { + LanguageModelSession.Profile { Instructions("Revert prompt failures") } + .model(model) + .transcriptErrorHandlingPolicy(.revertTranscript) + .onPrompt { throw LifecycleFixtureError.rejected } + } else { + LanguageModelSession.Profile { Instructions("Keep prompt failures") } + .model(model) + .onPrompt { throw LifecycleFixtureError.rejected } + } + } +} + private struct LifecycleFixtureModel: LanguageModel { typealias UnavailableReason = Never diff --git a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift index e8421891..b9e9f4e8 100644 --- a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift +++ b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift @@ -143,6 +143,24 @@ struct SessionPropertyTests { } #expect(session.transcript[1] == prompt) } + + @Test func replacingHistoryDropsInstructionEntries() { + let session = LanguageModelSession(model: SessionPropertyFixtureModel()) + let instructions = Transcript.Entry.instructions( + .init( + segments: [.text(.init(content: "Do not persist"))], + toolDefinitions: [] + ) + ) + let prompt = Transcript.Entry.prompt( + .init(segments: [.text(.init(content: "Keep"))]) + ) + + session.properties.history = [instructions, prompt] + + #expect(session.properties.history == [prompt]) + #expect(Array(session.transcript) == [prompt]) + } } private struct SessionPropertyFixtureInstructions: DynamicInstructions { From 93e4d4d7fdfb5e211882c948793dc2230214a752 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Thu, 15 Oct 2026 09:03:50 +0800 Subject: [PATCH 08/18] Fix session property observation and typed defaults --- .../LanguageModelSession.swift | 28 ++++++--- .../AnyLanguageModel/SessionProperties.swift | 17 +++-- .../SessionPropertyEntryMacro.swift | 24 ++++--- .../SessionPropertyTests.swift | 62 +++++++++++++++++++ 4 files changed, 108 insertions(+), 23 deletions(-) diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 59a1c075..683dae8e 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -405,10 +405,18 @@ public final class LanguageModelSession: @unchecked Sendable { } } + nonisolated private func withTranscriptMutation( + _ mutation: () throws -> Result + ) rethrows -> Result { + try properties._withSessionPropertyMutation(keyPath: \.history) { + try withMutation(keyPath: \.transcript, mutation) + } + } + // The prompt is the only entry committed before a request succeeds. Checkpoints and // the final response commit atomically at completion, so rollback owns only this ID. nonisolated private func removePrompt(id: String) { - withMutation(keyPath: \.transcript) { + withTranscriptMutation { state.withLock { state in state.transcript = Transcript(entries: state.transcript.filter { $0.id != id }) } @@ -517,7 +525,7 @@ public final class LanguageModelSession: @unchecked Sendable { providerMetadata: lastSnapshot.providerMetadata ) ) - session.withMutation(keyPath: \.transcript) { + session.withTranscriptMutation { session.state.withLock { $0.transcript.append(contentsOf: lastSnapshot.transcriptEntries) $0.transcript.append(responseEntry) @@ -526,7 +534,7 @@ public final class LanguageModelSession: @unchecked Sendable { session.endResponding() continuation.finish() } catch { - session.withMutation(keyPath: \.transcript) { + session.withTranscriptMutation { session.state.withLock { state in if state.transcriptErrorHandlingPolicy == .preserveTranscript { state.transcript.append(contentsOf: lastSnapshot?.transcriptEntries ?? []) @@ -780,7 +788,7 @@ public final class LanguageModelSession: @unchecked Sendable { responseFormat: responseFormat ) ) - withMutation(keyPath: \.transcript) { + withTranscriptMutation { state.withLock { $0.transcript.append(promptEntry) } } @@ -805,7 +813,7 @@ public final class LanguageModelSession: @unchecked Sendable { ) // Add tool entries and response to transcript - withMutation(keyPath: \.transcript) { + withTranscriptMutation { state.withLock { lockedState in lockedState.transcript.append(contentsOf: response.transcriptEntries) lockedState.transcript.append(responseEntry) @@ -830,7 +838,7 @@ public final class LanguageModelSession: @unchecked Sendable { responseFormat: type == String.self ? nil : .init(type: type) ) ) - withMutation(keyPath: \.transcript) { + withTranscriptMutation { state.withLock { $0.transcript.append(promptEntry) } } @@ -978,7 +986,7 @@ extension LanguageModelSession { responseFormat: .init(schema: schema) ) ) - withMutation(keyPath: \.transcript) { + withTranscriptMutation { state.withLock { $0.transcript.append(promptEntry) } } @@ -1177,7 +1185,7 @@ extension LanguageModelSession { responseFormat: type == String.self ? nil : .init(type: type) ) ) - withMutation(keyPath: \.transcript) { + withTranscriptMutation { state.withLock { $0.transcript.append(promptEntry) } } @@ -1214,7 +1222,7 @@ extension LanguageModelSession { ) // Add tool entries and response to transcript - withMutation(keyPath: \.transcript) { + withTranscriptMutation { state.withLock { lockedState in lockedState.transcript.append(contentsOf: response.transcriptEntries) lockedState.transcript.append(responseEntry) @@ -1309,7 +1317,7 @@ extension LanguageModelSession { responseFormat: type == String.self ? nil : .init(type: type) ) ) - withMutation(keyPath: \.transcript) { + withTranscriptMutation { state.withLock { $0.transcript.append(promptEntry) } } diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift index 0adfadb8..42aa3e92 100644 --- a/Sources/AnyLanguageModel/SessionProperties.swift +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -45,13 +45,18 @@ public final class SessionPropertyValues: @unchecked Sendable { /// The session history without an instructions entry. public var history: Transcript.HistoryView { - get { SessionPropertyBinding.history ?? historyGetter() } + get { + _accessSessionProperty(keyPath: \.history) + return SessionPropertyBinding.history ?? historyGetter() + } set { - precondition( - SessionPropertyBinding.history == nil, - "Session history is read-only while dynamic instructions are active" - ) - historySetter(newValue) + _withSessionPropertyMutation(keyPath: \.history) { + precondition( + SessionPropertyBinding.history == nil, + "Session history is read-only while dynamic instructions are active" + ) + historySetter(newValue) + } } } diff --git a/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift b/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift index 823aae64..e6625660 100644 --- a/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift +++ b/Sources/AnyLanguageModelMacros/SessionPropertyEntryMacro.swift @@ -41,13 +41,23 @@ public struct SessionPropertyEntryMacro: AccessorMacro, PeerMacro { let initializer = binding.initializer?.value else { return [] } - return [ - """ - private struct __Key_\(raw: name): SessionPropertyKey { - static let defaultValue = \(initializer) - } - """ - ] + let defaultValue: DeclSyntax + if let typeAnnotation = binding.typeAnnotation { + defaultValue = + """ + private struct __Key_\(raw: name): SessionPropertyKey { + static let defaultValue \(typeAnnotation) = \(initializer) + } + """ + } else { + defaultValue = + """ + private struct __Key_\(raw: name): SessionPropertyKey { + static let defaultValue = \(initializer) + } + """ + } + return [defaultValue] } private static func propertyName(in declaration: some DeclSyntaxProtocol) -> String? { diff --git a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift index e8421891..b4a4011d 100644 --- a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift +++ b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift @@ -6,6 +6,14 @@ import Testing extension SessionPropertyValues { @SessionPropertyEntry var revision = 0 + @SessionPropertyEntry var optionalSelection: String? = nil + @SessionPropertyEntry var items: [Int] = [] + @SessionPropertyEntry var mode: SessionPropertyMode = .first +} + +enum SessionPropertyMode { + case first + case second } @Suite("Session properties") @@ -107,6 +115,35 @@ struct SessionPropertyTests { #expect(changed.withLock { $0 }) } + @Test func generatedPropertiesPreserveExplicitTypeContext() { + let session = LanguageModelSession(model: SessionPropertyFixtureModel()) + + #expect(session.properties.optionalSelection == nil) + #expect(session.properties.items.isEmpty) + #expect(session.properties.mode == .first) + session.properties.optionalSelection = "selected" + session.properties.items = [1, 2] + session.properties.mode = .second + #expect(session.properties.optionalSelection == "selected") + #expect(session.properties.items == [1, 2]) + #expect(session.properties.mode == .second) + } + + @Test func historyObservationTracksSessionTranscriptChanges() async throws { + let session = LanguageModelSession(model: CompletingSessionPropertyModel()) + let changed = Locked(false) + + withObservationTracking { + _ = session.properties.history + } onChange: { + changed.withLock { $0 = true } + } + _ = try await session.respond(to: "Observe history") + + #expect(changed.withLock { $0 }) + #expect(session.properties.history.count == 2) + } + @Test func propertiesAndHistoryAreIsolatedBetweenSessions() { let first = LanguageModelSession(model: SessionPropertyFixtureModel()) let second = LanguageModelSession(model: SessionPropertyFixtureModel()) @@ -203,3 +240,28 @@ private struct SessionPropertyFixtureModel: LanguageModel { fatalError("SessionPropertyFixtureModel does not generate responses") } } + +private struct CompletingSessionPropertyModel: LanguageModel { + typealias UnavailableReason = Never + + func respond( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) async throws -> LanguageModelSession.Response where Content: Generable { + let raw = GeneratedContent("Done") + return .init(content: try Content(raw), rawContent: raw, transcriptEntries: []) + } + + func streamResponse( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) -> sending LanguageModelSession.ResponseStream where Content: Generable { + fatalError("This fixture only supports non-streaming responses") + } +} From cdeed51bcb34b465ecde2134c0647948df739e39 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Thu, 15 Oct 2026 09:07:21 +0800 Subject: [PATCH 09/18] Fix dynamic profile callback concurrency --- Sources/AnyLanguageModel/DynamicProfile.swift | 6 +- .../LanguageModelSession.swift | 35 +++++++---- .../DynamicProfileTests.swift | 61 +++++++++++++++++++ 3 files changed, 86 insertions(+), 16 deletions(-) diff --git a/Sources/AnyLanguageModel/DynamicProfile.swift b/Sources/AnyLanguageModel/DynamicProfile.swift index 111159fb..d2199126 100644 --- a/Sources/AnyLanguageModel/DynamicProfile.swift +++ b/Sources/AnyLanguageModel/DynamicProfile.swift @@ -202,7 +202,7 @@ extension LanguageModelSession.DynamicProfile { } public func historyTransform( - _ transform: @escaping ([Transcript.Entry]) -> [Transcript.Entry] + _ transform: @Sendable @escaping ([Transcript.Entry]) -> [Transcript.Entry] ) -> some LanguageModelSession.DynamicProfile { BuiltinModifiedDynamicProfile(content: self, modifier: .historyTransform(transform)) } @@ -314,7 +314,7 @@ struct ResolvedDynamicProfile: @unchecked Sendable { var hasToolCallingMode = false var transcriptErrorHandlingPolicy: TranscriptErrorHandlingPolicy? var hasTranscriptErrorHandlingPolicy = false - var historyTransforms: [([Transcript.Entry]) -> [Transcript.Entry]] = [] + var historyTransforms: [@Sendable ([Transcript.Entry]) -> [Transcript.Entry]] = [] var onPrompt: [@Sendable (Transcript.Prompt) async throws -> Void] = [] var onResponse: [@Sendable (Transcript.Response) async throws -> Void] = [] var onReasoning: [@Sendable (Transcript.Reasoning) async throws -> Void] = [] @@ -394,7 +394,7 @@ private enum BuiltinDynamicProfileModifier: @unchecked Sendable { case maximumResponseTokens(Int?) case reasoningLevel(ContextOptions.ReasoningLevel?) case toolCallingMode(GenerationOptions.ToolCallingMode?) - case historyTransform(([Transcript.Entry]) -> [Transcript.Entry]) + case historyTransform(@Sendable ([Transcript.Entry]) -> [Transcript.Entry]) case transcriptErrorHandlingPolicy(TranscriptErrorHandlingPolicy?) case onPrompt(@Sendable (Transcript.Prompt) async throws -> Void) case onResponse(@Sendable (Transcript.Response) async throws -> Void) diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 50bc9cd9..96cf5e5d 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -428,23 +428,32 @@ public final class LanguageModelSession: @unchecked Sendable { ) async throws { guard let profile = requestContext.profile else { return } for entry in entries { - if requestContext.callbackState.wasDispatched(entry.id) { - continue - } - let history = requestContext.canonicalTranscript + currentRoundEntries switch entry { case .reasoning(let reasoning): - try await withSessionProperties(history: history, isHistoryWritable: true) { - for action in profile.onReasoning { try await action(reasoning) } + guard requestContext.callbackState.reserve(entry.id) else { continue } + do { + let history = requestContext.canonicalTranscript + currentRoundEntries + try await withSessionProperties(history: history, isHistoryWritable: true) { + for action in profile.onReasoning { try await action(reasoning) } + } + } catch { + requestContext.callbackState.release(entry.id) + throw error } case .response(let response): - try await withSessionProperties(history: history, isHistoryWritable: true) { - for action in profile.onResponse { try await action(response) } + guard requestContext.callbackState.reserve(entry.id) else { continue } + do { + let history = requestContext.canonicalTranscript + currentRoundEntries + try await withSessionProperties(history: history, isHistoryWritable: true) { + for action in profile.onResponse { try await action(response) } + } + } catch { + requestContext.callbackState.release(entry.id) + throw error } default: continue } - requestContext.callbackState.markDispatched(entry.id) } } @@ -2304,12 +2313,12 @@ private struct ProfiledModelResponse where Content: Generable { fileprivate final class ProfileCallbackState: @unchecked Sendable { private let dispatchedEntryIDs = Locked>([]) - func wasDispatched(_ id: String) -> Bool { - dispatchedEntryIDs.withLock { $0.contains(id) } + func reserve(_ id: String) -> Bool { + dispatchedEntryIDs.withLock { $0.insert(id).inserted } } - func markDispatched(_ id: String) { - dispatchedEntryIDs.withLock { _ = $0.insert(id) } + func release(_ id: String) { + dispatchedEntryIDs.withLock { _ = $0.remove(id) } } } diff --git a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift index 4f58a076..5ad5b108 100644 --- a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift +++ b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift @@ -77,6 +77,43 @@ struct DynamicProfileTests { ) } + @Test func concurrentSnapshotsDispatchEachCallbackOnce() async throws { + let probe = ConcurrentCallbackProbe() + let model = LifecycleFixtureModel() + let session = LanguageModelSession( + profile: LanguageModelSession.Profile { Instructions("Concurrent callback") } + .model(model) + .onResponse { _ in await probe.recordInvocation() } + ) + let context = try await session.resolvedRequestContext(including: [], options: .init()) + let entry = Transcript.Entry.response( + .init(assetIDs: [], segments: [.text(.init(content: "response"))]) + ) + + let first = Task { + try await session.profileDidProduceEntries( + [entry], + requestContext: context, + currentRoundEntries: [entry] + ) + } + await probe.waitUntilFirstInvocation() + let second = Task { + try await session.profileDidProduceEntries( + [entry], + requestContext: context, + currentRoundEntries: [entry] + ) + } + + try await second.value + let invocationCount = await probe.invocationCount + await probe.releaseFirstInvocation() + try await first.value + + #expect(invocationCount == 1) + } + @Test(arguments: [false, true]) func callbackErrorsPropagateWithoutCommittingAResponse(streaming: Bool) async throws { let model = LifecycleFixtureModel() @@ -444,6 +481,30 @@ private enum LifecycleFixtureError: Error { case rejected } +private actor ConcurrentCallbackProbe { + private(set) var invocationCount = 0 + private var firstInvocationWaiter: CheckedContinuation? + private var firstInvocationRelease: CheckedContinuation? + + func recordInvocation() async { + invocationCount += 1 + guard invocationCount == 1 else { return } + firstInvocationWaiter?.resume() + firstInvocationWaiter = nil + await withCheckedContinuation { firstInvocationRelease = $0 } + } + + func waitUntilFirstInvocation() async { + guard invocationCount == 0 else { return } + await withCheckedContinuation { firstInvocationWaiter = $0 } + } + + func releaseFirstInvocation() { + firstInvocationRelease?.resume() + firstInvocationRelease = nil + } +} + private final class LifecycleFixtureState: @unchecked Sendable { let useSecond = Locked(false) let events = Locked<[String]>([]) From d211109334fc467309eb86b47005398bbfdb07b2 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Fri, 16 Oct 2026 09:05:28 +0800 Subject: [PATCH 10/18] Allow sampling mode without tool calling policy --- Sources/AnyLanguageModel/GenerationOptions.swift | 12 ++++++++++++ .../AnyLanguageModelTests/RequestOptionsTests.swift | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Sources/AnyLanguageModel/GenerationOptions.swift b/Sources/AnyLanguageModel/GenerationOptions.swift index f02e7fb6..b6092652 100644 --- a/Sources/AnyLanguageModel/GenerationOptions.swift +++ b/Sources/AnyLanguageModel/GenerationOptions.swift @@ -184,6 +184,18 @@ public struct GenerationOptions: Sendable, Equatable { self.toolCallingMode = nil } + public init( + samplingMode: SamplingMode?, + temperature: Double? = nil, + maximumResponseTokens: Int? = nil + ) { + self.init( + sampling: samplingMode, + temperature: temperature, + maximumResponseTokens: maximumResponseTokens + ) + } + /// Creates Foundation Models 27-style generation options. public init( samplingMode: SamplingMode? = nil, diff --git a/Tests/AnyLanguageModelTests/RequestOptionsTests.swift b/Tests/AnyLanguageModelTests/RequestOptionsTests.swift index 7822a614..ba9d9b2f 100644 --- a/Tests/AnyLanguageModelTests/RequestOptionsTests.swift +++ b/Tests/AnyLanguageModelTests/RequestOptionsTests.swift @@ -43,6 +43,19 @@ struct RequestOptionsTests { #expect(options.sampling == .random(probabilityThreshold: 0.8, seed: 9)) } + @Test func samplingModeInitializerDoesNotRequireAToolPolicy() { + let options = GenerationOptions(samplingMode: .greedy) + #expect(options.sampling == .greedy) + #expect(options.toolCallingMode == nil) + #expect(GenerationOptions(samplingMode: nil) == GenerationOptions()) + #expect( + GenerationOptions(samplingMode: .greedy, temperature: 0.4, maximumResponseTokens: 16) + == GenerationOptions(sampling: .greedy, temperature: 0.4, maximumResponseTokens: 16) + ) + #expect(GenerationOptions(temperature: 0.4).temperature == 0.4) + #expect(GenerationOptions(toolCallingMode: .required).toolCallingMode == .required) + } + @Test func transcriptRoundTripsToolCallingMode() throws { let options = GenerationOptions( samplingMode: .greedy, From 3ad28fb00d2e6b6aade9eb13227d6b4b29a94b66 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Fri, 16 Oct 2026 09:06:28 +0800 Subject: [PATCH 11/18] Keep session history bindings isolated and instruction free --- .../LanguageModelSession.swift | 4 ++ .../AnyLanguageModel/SessionProperties.swift | 7 ++- .../SessionPropertyTests.swift | 45 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 683dae8e..7f751a51 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -333,6 +333,10 @@ public final class LanguageModelSession: @unchecked Sendable { let state = Locked(State(finalTranscript)) self.state = state let setHistory: @Sendable (Transcript.HistoryView) -> Void = { history in + let history = history.filter { entry in + if case .instructions = entry { return false } + return true + } state.withLock { locked in let instructions = locked.transcript.prefix { if case .instructions = $0 { return true } diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift index 42aa3e92..47ef8a8c 100644 --- a/Sources/AnyLanguageModel/SessionProperties.swift +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -47,12 +47,15 @@ public final class SessionPropertyValues: @unchecked Sendable { public var history: Transcript.HistoryView { get { _accessSessionProperty(keyPath: \.history) - return SessionPropertyBinding.history ?? historyGetter() + if SessionPropertyBinding.values === self, let history = SessionPropertyBinding.history { + return history + } + return historyGetter() } set { _withSessionPropertyMutation(keyPath: \.history) { precondition( - SessionPropertyBinding.history == nil, + SessionPropertyBinding.values !== self || SessionPropertyBinding.history == nil, "Session history is read-only while dynamic instructions are active" ) historySetter(newValue) diff --git a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift index b4a4011d..ebecf1ed 100644 --- a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift +++ b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift @@ -180,6 +180,51 @@ struct SessionPropertyTests { } #expect(session.transcript[1] == prompt) } + + @Test func boundHistoryOnlyAppliesToItsOwningSession() { + let earlier = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "Earlier"))])) + let replacement = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "Replacement"))])) + let other = LanguageModelSession(model: SessionPropertyFixtureModel()) + other.properties.history = [earlier] + let session = LanguageModelSession( + model: SessionPropertyFixtureModel(), + dynamicInstructions: ForeignSessionHistoryInstructions(other: other, replacement: replacement) + ) + + let context = session.resolvedRequestContext() + + #expect(context.instructions?.description == "Other history count 1") + #expect(session.properties.history.isEmpty) + #expect(other.properties.history == [replacement]) + } + + @Test func replacingHistoryRejectsForeignInstructions() { + let other = LanguageModelSession(model: SessionPropertyFixtureModel(), instructions: "Foreign") + let prompt = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "Earlier"))])) + other.properties.history = [prompt] + let session = LanguageModelSession( + model: SessionPropertyFixtureModel(), + dynamicInstructions: SessionPropertyHistoryInstructions() + ) + + session.properties.history = Array(other.transcript) + let context = session.resolvedRequestContext() + + #expect(session.transcript == Transcript(entries: [prompt])) + #expect(context.instructions?.description == "History count 1") + #expect(context.transcript.count == 2) + } +} + +private struct ForeignSessionHistoryInstructions: DynamicInstructions { + let other: LanguageModelSession + let replacement: Transcript.Entry + + var body: some DynamicInstructions { + let count = other.properties.history.count + other.properties.history = [replacement] + return Instructions("Other history count \(count)") + } } private struct SessionPropertyFixtureInstructions: DynamicInstructions { From 9b76d74cd575ef4f3e6e098f826acfbbcba0b8bd Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Fri, 16 Oct 2026 09:11:41 +0800 Subject: [PATCH 12/18] Preserve profile history and request callback identity --- Sources/AnyLanguageModel/DynamicProfile.swift | 39 ++-- .../LanguageModelSession.swift | 137 ++++++++----- .../Models/SystemLanguageModel.swift | 4 +- .../AnyLanguageModel/SessionProperties.swift | 7 +- .../DynamicProfileTests.swift | 189 ++++++++++++++++++ 5 files changed, 305 insertions(+), 71 deletions(-) diff --git a/Sources/AnyLanguageModel/DynamicProfile.swift b/Sources/AnyLanguageModel/DynamicProfile.swift index d2199126..9d00f1ef 100644 --- a/Sources/AnyLanguageModel/DynamicProfile.swift +++ b/Sources/AnyLanguageModel/DynamicProfile.swift @@ -326,7 +326,12 @@ struct ResolvedDynamicProfile: @unchecked Sendable { extension LanguageModelSession.Profile: PrimitiveDynamicProfile { fileprivate func resolve() -> ResolvedDynamicProfile { - let dynamic = dynamicInstructions.resolveForRequest() + let history = SessionPropertyBinding.history.map { + SessionHistoryBinding($0.entries, isWritable: false) + } + let dynamic = SessionPropertyBinding.$history.withValue(history) { + dynamicInstructions.resolveForRequest() + } return ResolvedDynamicProfile( identity: identity, model: nil, @@ -474,14 +479,15 @@ private func resolveDynamicProfile( actor ProfileLifecycle { private var activeProfile: ResolvedDynamicProfile? - private var lastPromptID: String? private var pendingPreparation: (id: Int, task: Task)? private var nextPreparationID = 0 func prepare( _ profile: ResolvedDynamicProfile, properties: SessionPropertyValues, - history: [Transcript.Entry] + history: [Transcript.Entry], + prompt: Transcript.Prompt?, + callbackState: ProfileCallbackState ) async throws { let previous = pendingPreparation?.task nextPreparationID += 1 @@ -491,7 +497,9 @@ actor ProfileLifecycle { try await self.performPreparation( profile, properties: properties, - history: history + history: history, + prompt: prompt, + callbackState: callbackState ) } pendingPreparation = (id, task) @@ -507,7 +515,9 @@ actor ProfileLifecycle { private func performPreparation( _ profile: ResolvedDynamicProfile, properties: SessionPropertyValues, - history: [Transcript.Entry] + history: [Transcript.Entry], + prompt: Transcript.Prompt?, + callbackState: ProfileCallbackState ) async throws { if activeProfile?.identity != profile.identity { if let previous = activeProfile { @@ -523,18 +533,15 @@ actor ProfileLifecycle { activeProfile = profile } - guard - let prompt = history.reversed().compactMap({ entry -> Transcript.Prompt? in - if case .prompt(let prompt) = entry { return prompt } - return nil - }).first, - prompt.id != lastPromptID - else { return } - - try await withBindings(properties: properties, history: history) { - for action in profile.onPrompt { try await action(prompt) } + guard let prompt, callbackState.reserve(prompt.id) else { return } + do { + try await withBindings(properties: properties, history: history) { + for action in profile.onPrompt { try await action(prompt) } + } + } catch { + callbackState.release(prompt.id) + throw error } - lastPromptID = prompt.id } private func withBindings( diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 8e976ddc..0e63ddf7 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -206,6 +206,21 @@ public final class LanguageModelSession: @unchecked Sendable { callbackState: context.callbackState ) } + + fileprivate func withCanonicalTranscript(_ entries: [Transcript.Entry]) -> RequestContext { + RequestContext( + transcript: transcript, + instructions: instructions, + tools: tools, + model: model, + options: options, + contextOptions: contextOptions, + profile: profile, + canonicalTranscript: entries, + transcriptErrorHandlingPolicy: transcriptErrorHandlingPolicy, + callbackState: callbackState + ) + } } /// Returns the transcript, instructions, and tools for the next model request. @@ -263,7 +278,14 @@ public final class LanguageModelSession: @unchecked Sendable { try await profileLifecycle.prepare( profile, properties: properties, - history: context.canonicalTranscript + history: context.canonicalTranscript, + prompt: prepared?.session === self + ? prepared?.prompt + : context.canonicalTranscript.reversed().compactMap { entry in + if case .prompt(let prompt) = entry { return prompt } + return nil + }.first, + callbackState: context.callbackState ) } if let prepared, prepared.session === self { @@ -859,9 +881,10 @@ public final class LanguageModelSession: @unchecked Sendable { lastSnapshot.transcriptEntries.map(\.id) + [responseEntry.id] ) if let prepared = profileTracker?.prepared { + let context = prepared.context.withCanonicalTranscript(Array(session.transcript)) try await session.profileDidProduceEntries( Array(lastSnapshot.transcriptEntries) + [responseEntry], - requestContext: prepared.context, + requestContext: context, currentRoundEntries: [] ) } @@ -1110,8 +1133,8 @@ public final class LanguageModelSession: @unchecked Sendable { responseFormat: type == String.self ? nil : .init(type: type), options: options, profileTracker: profileTracker - ) { - try await profiledResponse(options: options, tracker: profileTracker) { + ) { requestPrompt in + try await profiledResponse(prompt: requestPrompt, options: options, tracker: profileTracker) { model, resolvedOptions, contextOptions in @@ -1128,6 +1151,7 @@ public final class LanguageModelSession: @unchecked Sendable { } nonisolated private func profiledResponse( + prompt: Transcript.Prompt, options: GenerationOptions, tracker: PreparedProfileRequestTracker?, generate: ( @@ -1146,6 +1170,7 @@ public final class LanguageModelSession: @unchecked Sendable { let prepared = PreparedProfileRequest( session: self, context: context, + prompt: prompt, callSiteOptions: options ) tracker?.prepared = prepared @@ -1154,7 +1179,9 @@ public final class LanguageModelSession: @unchecked Sendable { try await profileLifecycle.prepare( profile, properties: properties, - history: context.canonicalTranscript + history: context.canonicalTranscript, + prompt: prepared.prompt, + callbackState: context.callbackState ) } return try await generate(context.model, context.options, context.contextOptions) @@ -1167,23 +1194,22 @@ public final class LanguageModelSession: @unchecked Sendable { responseFormat: Transcript.ResponseFormat?, options: GenerationOptions, profileTracker: PreparedProfileRequestTracker?, - generate: () async throws -> ProfiledModelResponse + generate: (Transcript.Prompt) async throws -> ProfiledModelResponse ) async throws -> Response { try await wrapRespond(profileTracker: profileTracker) { promptID in // Add prompt to transcript - let promptEntry = Transcript.Entry.prompt( - Transcript.Prompt( - id: promptID, - segments: try prompt.makeTranscriptSegments(occurrenceID: promptID), - options: options, - responseFormat: responseFormat - ) + let requestPrompt = Transcript.Prompt( + id: promptID, + segments: try prompt.makeTranscriptSegments(occurrenceID: promptID), + options: options, + responseFormat: responseFormat ) + let promptEntry = Transcript.Entry.prompt(requestPrompt) withTranscriptMutation { state.withLock { $0.transcript.append(promptEntry) } } - let profiled = try await generate() + let profiled = try await generate(requestPrompt) let response = Self.representingNilExplicitly( in: profiled.response, responseFormat: responseFormat @@ -1222,7 +1248,7 @@ public final class LanguageModelSession: @unchecked Sendable { entry responseEntry: Transcript.Entry, prepared: PreparedProfileRequest? ) async throws where Content: Generable { - withMutation(keyPath: \.transcript) { + withTranscriptMutation { state.withLock { lockedState in lockedState.transcript.append(contentsOf: response.transcriptEntries) lockedState.transcript.append(responseEntry) @@ -1230,15 +1256,16 @@ public final class LanguageModelSession: @unchecked Sendable { } guard let prepared else { return } + let context = prepared.context.withCanonicalTranscript(Array(transcript)) do { try await profileDidProduceEntries( Array(response.transcriptEntries) + [responseEntry], - requestContext: prepared.context, + requestContext: context, currentRoundEntries: [] ) } catch { let ids = Set(response.transcriptEntries.map(\.id) + [responseEntry.id]) - withMutation(keyPath: \.transcript) { + withTranscriptMutation { state.withLock { lockedState in lockedState.transcript = Transcript( entries: lockedState.transcript.filter { !ids.contains($0.id) } @@ -1263,14 +1290,13 @@ public final class LanguageModelSession: @unchecked Sendable { } catch { return ResponseStream(stream: AsyncThrowingStream { $0.finish(throwing: error) }) } - let promptEntry = Transcript.Entry.prompt( - Transcript.Prompt( - id: promptID, - segments: segments, - options: options, - responseFormat: type == String.self ? nil : .init(type: type) - ) + let requestPrompt = Transcript.Prompt( + id: promptID, + segments: segments, + options: options, + responseFormat: type == String.self ? nil : .init(type: type) ) + let promptEntry = Transcript.Entry.prompt(requestPrompt) withTranscriptMutation { state.withLock { $0.transcript.append(promptEntry) } } @@ -1282,6 +1308,7 @@ public final class LanguageModelSession: @unchecked Sendable { profileTracker = tracker upstream = profileDispatchedStream( prompt: prompt, + requestPrompt: requestPrompt, type: type, includeSchemaInPrompt: includeSchemaInPrompt, options: options, @@ -1306,12 +1333,13 @@ public final class LanguageModelSession: @unchecked Sendable { nonisolated private func profileDispatchedStream( prompt: Prompt, + requestPrompt: Transcript.Prompt, type: Content.Type, includeSchemaInPrompt: Bool, options: GenerationOptions, tracker: PreparedProfileRequestTracker ) -> ResponseStream where Content: Generable { - profileDispatchedStream(options: options, tracker: tracker) { + profileDispatchedStream(requestPrompt: requestPrompt, options: options, tracker: tracker) { model, resolvedOptions, contextOptions in @@ -1327,6 +1355,7 @@ public final class LanguageModelSession: @unchecked Sendable { } nonisolated private func profileDispatchedStream( + requestPrompt: Transcript.Prompt, options: GenerationOptions, tracker: PreparedProfileRequestTracker, generate: @@ -1346,6 +1375,7 @@ public final class LanguageModelSession: @unchecked Sendable { let prepared = PreparedProfileRequest( session: self, context: context, + prompt: requestPrompt, callSiteOptions: options ) tracker.prepared = prepared @@ -1354,7 +1384,9 @@ public final class LanguageModelSession: @unchecked Sendable { try await self.profileLifecycle.prepare( profile, properties: self.properties, - history: context.canonicalTranscript + history: context.canonicalTranscript, + prompt: prepared.prompt, + callbackState: context.callbackState ) } let upstream = generate( @@ -1454,8 +1486,8 @@ extension LanguageModelSession { responseFormat: .init(schema: schema), options: options, profileTracker: profileTracker - ) { - try await profiledResponse(options: options, tracker: profileTracker) { + ) { requestPrompt in + try await profiledResponse(prompt: requestPrompt, options: options, tracker: profileTracker) { model, resolvedOptions, contextOptions in @@ -1520,14 +1552,13 @@ extension LanguageModelSession { } catch { return ResponseStream(stream: AsyncThrowingStream { $0.finish(throwing: error) }) } - let promptEntry = Transcript.Entry.prompt( - Transcript.Prompt( - id: promptID, - segments: segments, - options: options, - responseFormat: .init(schema: schema) - ) + let requestPrompt = Transcript.Prompt( + id: promptID, + segments: segments, + options: options, + responseFormat: .init(schema: schema) ) + let promptEntry = Transcript.Entry.prompt(requestPrompt) withTranscriptMutation { state.withLock { $0.transcript.append(promptEntry) } } @@ -1537,7 +1568,7 @@ extension LanguageModelSession { if dynamicProfile != nil { let tracker = PreparedProfileRequestTracker() profileTracker = tracker - upstream = profileDispatchedStream(options: options, tracker: tracker) { + upstream = profileDispatchedStream(requestPrompt: requestPrompt, options: options, tracker: tracker) { model, resolvedOptions, contextOptions in @@ -1743,14 +1774,13 @@ extension LanguageModelSession { segments.append(contentsOf: images.map { .image($0) }) // Add prompt to transcript - let promptEntry = Transcript.Entry.prompt( - Transcript.Prompt( - id: promptID, - segments: segments, - options: options, - responseFormat: type == String.self ? nil : .init(type: type) - ) + let requestPrompt = Transcript.Prompt( + id: promptID, + segments: segments, + options: options, + responseFormat: type == String.self ? nil : .init(type: type) ) + let promptEntry = Transcript.Entry.prompt(requestPrompt) withTranscriptMutation { state.withLock { $0.transcript.append(promptEntry) } } @@ -1758,7 +1788,8 @@ extension LanguageModelSession { // Extract text content for the Prompt parameter let textPrompt = Prompt(prompt) - let profiled = try await profiledResponse(options: options, tracker: profileTracker) { + let profiled = try await profiledResponse(prompt: requestPrompt, options: options, tracker: profileTracker) + { model, resolvedOptions, contextOptions in @@ -1881,13 +1912,12 @@ extension LanguageModelSession { segments.append(contentsOf: images.map { .image($0) }) // Create prompt entry that will be added when stream starts - let promptEntry = Transcript.Entry.prompt( - Transcript.Prompt( - segments: segments, - options: options, - responseFormat: type == String.self ? nil : .init(type: type) - ) + let requestPrompt = Transcript.Prompt( + segments: segments, + options: options, + responseFormat: type == String.self ? nil : .init(type: type) ) + let promptEntry = Transcript.Entry.prompt(requestPrompt) withTranscriptMutation { state.withLock { $0.transcript.append(promptEntry) } } @@ -1900,7 +1930,7 @@ extension LanguageModelSession { if dynamicProfile != nil { let tracker = PreparedProfileRequestTracker() profileTracker = tracker - upstream = profileDispatchedStream(options: options, tracker: tracker) { + upstream = profileDispatchedStream(requestPrompt: requestPrompt, options: options, tracker: tracker) { model, resolvedOptions, contextOptions in @@ -2314,7 +2344,7 @@ private struct ProfiledModelResponse where Content: Generable { let prepared: PreparedProfileRequest? } -fileprivate final class ProfileCallbackState: @unchecked Sendable { +final class ProfileCallbackState: @unchecked Sendable { private let dispatchedEntryIDs = Locked>([]) func reserve(_ id: String) -> Bool { @@ -2328,15 +2358,18 @@ fileprivate final class ProfileCallbackState: @unchecked Sendable { private final class PreparedProfileRequest: @unchecked Sendable { let session: LanguageModelSession + let prompt: Transcript.Prompt let callSiteOptions: GenerationOptions private let state: Locked init( session: LanguageModelSession, context: LanguageModelSession.RequestContext, + prompt: Transcript.Prompt, callSiteOptions: GenerationOptions ) { self.session = session + self.prompt = prompt self.callSiteOptions = callSiteOptions state = Locked(State(context: context)) } diff --git a/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift b/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift index 194ab949..86e1571e 100644 --- a/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift @@ -398,12 +398,13 @@ session: LanguageModelSession, prompt: Prompt ) -> FoundationModels.LanguageModelSession { + let requestContext = session.resolvedRequestContext() if session.usesDynamicInstructions { // Foundation Models evaluates the dynamic instructions itself, // so the history leaves out the instructions entry. let history = fmTranscriptDroppingDuplicatePrompt( Transcript( - entries: session.transcript.filter { entry in + entries: requestContext.transcript.filter { entry in if case .instructions = entry { return false } return true } @@ -417,7 +418,6 @@ ) } - let requestContext = session.resolvedRequestContext() return FoundationModels.LanguageModelSession( model: model, tools: requestContext.tools.toFoundationModels(), diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift index edbe7a75..c56da70e 100644 --- a/Sources/AnyLanguageModel/SessionProperties.swift +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -116,7 +116,12 @@ final class SessionHistoryBinding: @unchecked Sendable { let isWritable: Bool init(_ entries: Transcript.HistoryView, isWritable: Bool) { - storage = Locked(entries) + storage = Locked( + entries.filter { entry in + if case .instructions = entry { return false } + return true + } + ) self.isWritable = isWritable } diff --git a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift index 5ad5b108..2dc45a1d 100644 --- a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift +++ b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift @@ -1,6 +1,11 @@ import Foundation +import Observation import Testing +#if canImport(FoundationModels) && compiler(>=6.4) && !os(tvOS) && !os(watchOS) + import class FoundationModels.SystemLanguageModel +#endif + @_spi(Compatibility) @testable import AnyLanguageModel extension SessionPropertyValues { @@ -114,6 +119,153 @@ struct DynamicProfileTests { #expect(invocationCount == 1) } + @Test(arguments: [false, true]) + func completionCallbacksSeeAndPreserveCommittedHistory(streaming: Bool) async throws { + let histories = Locked<[[Transcript.Entry]]>([]) + let session = LanguageModelSession( + profile: LanguageModelSession.Profile { Instructions("Completion history") } + .model(LifecycleFixtureModel()) + .onResponse { _ in + let properties = try #require(SessionPropertyBinding.values) + let history = properties.history + histories.withLock { $0.append(history) } + properties.history = history + } + ) + + if streaming { + _ = try await session.streamResponse(to: "Complete").collect() + } else { + _ = try await session.respond(to: "Complete") + } + + let history = try #require(histories.withLock { $0.first }) + #expect(history == Array(session.transcript)) + #expect(history.contains { if case .response = $0 { true } else { false } }) + #expect(history.contains { if case .reasoning = $0 { true } else { false } }) + } + + @Test(arguments: [false, true]) + func historyObserversSeeCompletionAndCallbackRollback(streaming: Bool) async throws { + let completionObserved = Locked(false) + let rollbackObserved = Locked(false) + let session = LanguageModelSession( + profile: LanguageModelSession.Profile { Instructions("Observe completion") } + .model(LifecycleFixtureModel()) + .onPrompt { _ in + let properties = try #require(SessionPropertyBinding.values) + withObservationTracking { + _ = properties.history + } onChange: { + completionObserved.withLock { $0 = true } + } + } + .onResponse { _ in + let properties = try #require(SessionPropertyBinding.values) + withObservationTracking { + _ = properties.history + } onChange: { + rollbackObserved.withLock { $0 = true } + } + throw LifecycleFixtureError.rejected + } + ) + + await #expect(throws: LifecycleFixtureError.self) { + if streaming { + _ = try await session.streamResponse(to: "Roll back").collect() + } else { + _ = try await session.respond(to: "Roll back") + } + } + + #expect(completionObserved.withLock { $0 }) + #expect(rollbackObserved.withLock { $0 }) + #expect(session.transcript.count == 1) + } + + @Test func nestedInstructionsReceiveReadOnlyHistory() { + let writability = Locked<[Bool]>([]) + let session = LanguageModelSession(profile: HistoryBindingProfile(writability: writability)) + + _ = session.resolvedRequestContext() + + #expect(writability.withLock { $0 } == [true, false]) + } + + @Test func toolHistoryExcludesStaticInstructions() async { + let session = LanguageModelSession(model: LifecycleFixtureModel(), instructions: "Static") + let context = session.resolvedRequestContext() + let history = await session.withProfileToolExecution(requestContext: context, currentRoundEntries: []) { + session.properties.history + } + + #expect(history.isEmpty) + #expect(session.transcript.count == 1) + } + + @Test(arguments: [false, true]) + func promptCallbackUsesTheActualRequestAfterHistoryMutation(streaming: Bool) async throws { + let prompts = Locked<[String]>([]) + let session = LanguageModelSession(profile: PromptRemovingProfile(prompts: prompts)) + + if streaming { + _ = try await session.streamResponse(to: "Actual request").collect() + } else { + _ = try await session.respond(to: "Actual request") + } + + #expect(prompts.withLock { $0 } == ["Actual request"]) + } + + @Test func concurrentStreamsKeepTheirOwnPromptCallbacks() async throws { + let prompts = Locked<[String]>([]) + let session = LanguageModelSession( + profile: LanguageModelSession.Profile { Instructions("Concurrent prompts") } + .model(LifecycleFixtureModel()) + .onPrompt { (prompt: Transcript.Prompt) in + let text = prompt.segments.compactMap { segment in + if case .text(let text) = segment { return text.content } + return nil + }.joined() + prompts.withLock { $0.append(text) } + } + ) + let first = session.streamResponse(to: "First") + let second = session.streamResponse(to: "Second") + async let firstResponse = first.collect() + async let secondResponse = second.collect() + _ = try await (firstResponse, secondResponse) + + #expect(prompts.withLock { $0.sorted() } == ["First", "Second"]) + } + + #if canImport(FoundationModels) && compiler(>=6.4) && !os(tvOS) && !os(watchOS) + @Test func nativeSessionUsesProjectedHistory() throws { + guard #available(macOS 27.0, iOS 27.0, visionOS 27.0, *) else { return } + let earlier = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "Earlier"))])) + let prompt = Prompt("Current") + let current = Transcript.Entry.prompt( + .init(id: "current", segments: try prompt.makeTranscriptSegments(occurrenceID: "current")) + ) + let session = LanguageModelSession( + profile: LanguageModelSession.Profile { Instructions("Projected") } + .model(LifecycleFixtureModel()) + .historyTransform { $0.filter { $0.id != earlier.id } }, + history: [earlier, current] + ) + + let native = makeFoundationModelsSession( + model: FoundationModels.SystemLanguageModel.default, + session: session, + prompt: prompt + ) + + #expect(!native.transcript.contains { if case .prompt = $0 { true } else { false } }) + #expect(Array(session.transcript) == [earlier, current]) + } + #endif + @Test(arguments: [false, true]) func callbackErrorsPropagateWithoutCommittingAResponse(streaming: Bool) async throws { let model = LifecycleFixtureModel() @@ -481,6 +633,43 @@ private enum LifecycleFixtureError: Error { case rejected } +private struct HistoryBindingProfile: LanguageModelSession.DynamicProfile, @unchecked Sendable { + let writability: Locked<[Bool]> + + var body: some LanguageModelSession.DynamicProfile { + writability.withLock { $0.append(SessionPropertyBinding.history?.isWritable ?? false) } + return LanguageModelSession.Profile { HistoryBindingInstructions(writability: writability) } + .model(LifecycleFixtureModel()) + } +} + +private struct HistoryBindingInstructions: DynamicInstructions, @unchecked Sendable { + let writability: Locked<[Bool]> + + var body: some DynamicInstructions { + writability.withLock { $0.append(SessionPropertyBinding.history?.isWritable ?? true) } + return Instructions("Read-only instructions") + } +} + +private struct PromptRemovingProfile: LanguageModelSession.DynamicProfile, @unchecked Sendable { + let prompts: Locked<[String]> + @SessionProperty(\.history) private var history + + var body: some LanguageModelSession.DynamicProfile { + history = [] + return LanguageModelSession.Profile { Instructions("Removed prompt") } + .model(LifecycleFixtureModel()) + .onPrompt { (prompt: Transcript.Prompt) in + let text = prompt.segments.compactMap { segment in + if case .text(let text) = segment { return text.content } + return nil + }.joined() + prompts.withLock { $0.append(text) } + } + } +} + private actor ConcurrentCallbackProbe { private(set) var invocationCount = 0 private var firstInvocationWaiter: CheckedContinuation? From 2c5c4889e5ed7428deb1aafcc4be580a4e843b6e Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Sat, 17 Oct 2026 09:01:50 +0800 Subject: [PATCH 13/18] Invalidate stale history commits and evaluate property defaults outside the lock --- .../LanguageModelSession.swift | 10 +- .../AnyLanguageModel/SessionProperties.swift | 11 +- .../SessionPropertyTests.swift | 125 ++++++++++++++++++ 3 files changed, 139 insertions(+), 7 deletions(-) diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 7f751a51..ab14a6c5 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -530,9 +530,10 @@ public final class LanguageModelSession: @unchecked Sendable { ) ) session.withTranscriptMutation { - session.state.withLock { - $0.transcript.append(contentsOf: lastSnapshot.transcriptEntries) - $0.transcript.append(responseEntry) + session.state.withLock { state in + guard state.transcript.contains(where: { $0.id == promptEntry.id }) else { return } + state.transcript.append(contentsOf: lastSnapshot.transcriptEntries) + state.transcript.append(responseEntry) } } session.endResponding() @@ -540,6 +541,7 @@ public final class LanguageModelSession: @unchecked Sendable { } catch { session.withTranscriptMutation { session.state.withLock { state in + guard state.transcript.contains(where: { $0.id == promptEntry.id }) else { return } if state.transcriptErrorHandlingPolicy == .preserveTranscript { state.transcript.append(contentsOf: lastSnapshot?.transcriptEntries ?? []) } else if state.transcriptErrorHandlingPolicy == .revertTranscript { @@ -819,6 +821,7 @@ public final class LanguageModelSession: @unchecked Sendable { // Add tool entries and response to transcript withTranscriptMutation { state.withLock { lockedState in + guard lockedState.transcript.contains(where: { $0.id == promptID }) else { return } lockedState.transcript.append(contentsOf: response.transcriptEntries) lockedState.transcript.append(responseEntry) } @@ -1228,6 +1231,7 @@ extension LanguageModelSession { // Add tool entries and response to transcript withTranscriptMutation { state.withLock { lockedState in + guard lockedState.transcript.contains(where: { $0.id == promptID }) else { return } lockedState.transcript.append(contentsOf: response.transcriptEntries) lockedState.transcript.append(responseEntry) } diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift index 47ef8a8c..b7b968d2 100644 --- a/Sources/AnyLanguageModel/SessionProperties.swift +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -65,12 +65,15 @@ public final class SessionPropertyValues: @unchecked Sendable { public subscript(key: Key.Type) -> Key.Value where Key: SessionPropertyKey { get { - customValues.withLock { values in - if let value = values[ObjectIdentifier(key)] as? Key.Value { return value } - let value = Key.defaultValue - values[ObjectIdentifier(key)] = value + if let value = customValues.withLock({ $0[ObjectIdentifier(key)] as? Key.Value }) { return value } + let defaultValue = Key.defaultValue + return customValues.withLock { values in + if let value = values[ObjectIdentifier(key)] as? Key.Value { return value } + values[ObjectIdentifier(key)] = defaultValue + return defaultValue + } } set { customValues.withLock { values in diff --git a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift index ebecf1ed..00311fdc 100644 --- a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift +++ b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift @@ -18,6 +18,64 @@ enum SessionPropertyMode { @Suite("Session properties") struct SessionPropertyTests { + @Test func propertyDefaultsCanReadAnotherProperty() { + let session = LanguageModelSession(model: SessionPropertyFixtureModel()) + SessionPropertyBinding.$values.withValue(session.properties) { + #expect(session.properties[DerivedPropertyKey.self] == 42) + session.properties[BasePropertyKey.self] = 7 + #expect(session.properties[DerivedPropertyKey.self] == 42) + } + } + + @Test(arguments: [false, true], [false, true]) + func replacingHistoryDiscardsStaleGenerationCommits(streaming: Bool, images: Bool) async throws { + let (started, start) = AsyncStream.makeStream() + let (resume, resumeContinuation) = AsyncStream.makeStream() + let session = LanguageModelSession( + model: PausedHistoryModel(started: start, resume: resume), + instructions: "Keep instructions" + ) + let request = Task { + if streaming { + let stream = + images + ? session.streamResponse(to: "Old request", images: []) + : session.streamResponse(to: "Old request") + return try await stream.collect().content + } + let response = + try await images + ? session.respond(to: "Old request", images: []) + : session.respond(to: "Old request") + return response.content + } + for await _ in started.prefix(1) {} + let replacement = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "New conversation"))])) + session.properties.history = [replacement] + resumeContinuation.yield() + + #expect(try await request.value == "Done") + #expect(session.properties.history == [replacement]) + #expect(session.transcript.count == 2) + #expect(!session.isResponding) + } + + @Test func replacingHistoryDiscardsStaleStreamingFailureCheckpoint() async { + let (started, start) = AsyncStream.makeStream() + let (resume, resumeContinuation) = AsyncStream.makeStream() + let session = LanguageModelSession(model: PausedHistoryModel(started: start, resume: resume, fail: true)) + session.transcriptErrorHandlingPolicy = .preserveTranscript + let request = Task { try await session.streamResponse(to: "Old request").collect() } + for await _ in started.prefix(1) {} + let replacement = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "New conversation"))])) + session.properties.history = [replacement] + resumeContinuation.yield() + + await #expect(throws: PausedHistoryError.self) { try await request.value } + #expect(session.properties.history == [replacement]) + #expect(!session.isResponding) + } + @Test func dynamicInstructionsReadAndUpdateSessionProperties() { let session = LanguageModelSession( model: SessionPropertyFixtureModel(), @@ -216,6 +274,73 @@ struct SessionPropertyTests { } } +private enum BasePropertyKey: SessionPropertyKey { + static let defaultValue = 42 +} + +private enum DerivedPropertyKey: SessionPropertyKey { + static var defaultValue: Int { + SessionPropertyBinding.values![BasePropertyKey.self] + } +} + +private enum PausedHistoryError: Error { case failed } + +private struct PausedHistoryModel: LanguageModel { + typealias UnavailableReason = Never + let started: AsyncStream.Continuation + let resume: AsyncStream + var fail = false + + private var entries: [Transcript.Entry] { + [.reasoning(.init(segments: [.text(.init(content: "Old reasoning"))]))] + } + + func respond( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) async throws -> LanguageModelSession.Response where Content: Generable { + started.yield() + for await _ in resume.prefix(1) {} + let raw = GeneratedContent("Done") + return .init(content: try Content(raw), rawContent: raw, transcriptEntries: entries[...]) + } + + func streamResponse( + within session: LanguageModelSession, + to prompt: Prompt, + generating type: Content.Type, + includeSchemaInPrompt: Bool, + options: GenerationOptions + ) -> sending LanguageModelSession.ResponseStream where Content: Generable { + .init( + stream: AsyncThrowingStream { continuation in + Task { + do { + let raw = GeneratedContent("Done") + continuation.yield( + .init( + content: try Content.PartiallyGenerated(raw), + rawContent: raw, + transcriptEntries: entries[...] + ) + ) + started.yield() + for await _ in resume.prefix(1) {} + if fail { throw PausedHistoryError.failed } + continuation.finish() + } catch { + continuation.finish(throwing: error) + } + } + } + ) + } +} + private struct ForeignSessionHistoryInstructions: DynamicInstructions { let other: LanguageModelSession let replacement: Transcript.Entry From d9e5f31f545299fcfd395def4586b4a2dea6a1c9 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Sat, 17 Oct 2026 09:05:46 +0800 Subject: [PATCH 14/18] Preserve profile lifecycle identity and evolving callback history --- Sources/AnyLanguageModel/DynamicProfile.swift | 11 +- .../LanguageModelSession.swift | 28 ++- .../Models/SystemLanguageModel.swift | 12 +- .../DynamicProfileTests.swift | 185 ++++++++++++++++++ 4 files changed, 223 insertions(+), 13 deletions(-) diff --git a/Sources/AnyLanguageModel/DynamicProfile.swift b/Sources/AnyLanguageModel/DynamicProfile.swift index 9d00f1ef..b996fca5 100644 --- a/Sources/AnyLanguageModel/DynamicProfile.swift +++ b/Sources/AnyLanguageModel/DynamicProfile.swift @@ -38,7 +38,12 @@ extension LanguageModelSession { fileprivate let resolveValue: () -> ResolvedDynamicProfile public init(_ dynamicProfile: any DynamicProfile) { - resolveValue = { resolveDynamicProfile(dynamicProfile) } + let identity = String(reflecting: type(of: dynamicProfile)) + resolveValue = { + var resolved = resolveDynamicProfile(dynamicProfile) + resolved.identity = identity + "/" + resolved.identity + return resolved + } } public init(erasing dynamicProfile: some DynamicProfile) { @@ -519,6 +524,7 @@ actor ProfileLifecycle { prompt: Transcript.Prompt?, callbackState: ProfileCallbackState ) async throws { + let history = SessionHistoryBinding(history, isWritable: true) if activeProfile?.identity != profile.identity { if let previous = activeProfile { await withBindings(properties: properties, history: history) { @@ -546,10 +552,9 @@ actor ProfileLifecycle { private func withBindings( properties: SessionPropertyValues, - history: [Transcript.Entry], + history: SessionHistoryBinding, operation: @Sendable () async throws -> Result ) async rethrows -> Result { - let history = SessionHistoryBinding(history, isWritable: true) return try await SessionPropertyBinding.$values.withValue(properties) { try await SessionPropertyBinding.$history.withValue(history) { try await operation() diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 805ee771..8a427658 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -64,6 +64,7 @@ public final class LanguageModelSession: @unchecked Sendable { @ObservationIgnored private let state: Locked @ObservationIgnored private let responseRelays = Locked<[UUID: Task]>([:]) + @ObservationIgnored private let lastResponseModel = Locked<(any LanguageModel)?>(nil) /// Waits for transcript cleanup of all streaming relays registered when this call begins. /// @@ -121,6 +122,8 @@ public final class LanguageModelSession: @unchecked Sendable { dynamicInstructions != nil || dynamicProfile != nil } + nonisolated var usesDynamicProfile: Bool { dynamicProfile != nil } + /// A delegate that observes and controls tool execution. /// /// Set this property to intercept tool calls, provide custom output, @@ -449,13 +452,16 @@ public final class LanguageModelSession: @unchecked Sendable { currentRoundEntries: [Transcript.Entry] ) async throws { guard let profile = requestContext.profile else { return } + let history = SessionHistoryBinding( + requestContext.canonicalTranscript + currentRoundEntries, + isWritable: true + ) for entry in entries { switch entry { case .reasoning(let reasoning): guard requestContext.callbackState.reserve(entry.id) else { continue } do { - let history = requestContext.canonicalTranscript + currentRoundEntries - try await withSessionProperties(history: history, isHistoryWritable: true) { + try await withSessionProperties(history: history) { for action in profile.onReasoning { try await action(reasoning) } } } catch { @@ -465,8 +471,7 @@ public final class LanguageModelSession: @unchecked Sendable { case .response(let response): guard requestContext.callbackState.reserve(entry.id) else { continue } do { - let history = requestContext.canonicalTranscript + currentRoundEntries - try await withSessionProperties(history: history, isHistoryWritable: true) { + try await withSessionProperties(history: history) { for action in profile.onResponse { try await action(response) } } } catch { @@ -499,6 +504,13 @@ public final class LanguageModelSession: @unchecked Sendable { operation: @Sendable () async throws -> Result ) async rethrows -> Result { let history = SessionHistoryBinding(history, isWritable: isHistoryWritable) + return try await withSessionProperties(history: history, operation: operation) + } + + nonisolated private func withSessionProperties( + history: SessionHistoryBinding, + operation: @Sendable () async throws -> Result + ) async rethrows -> Result { return try await SessionPropertyBinding.$values.withValue(properties) { try await SessionPropertyBinding.$history.withValue(history) { try await operation() @@ -718,6 +730,7 @@ public final class LanguageModelSession: @unchecked Sendable { } public func prewarm(promptPrefix: Prompt? = nil) { + let model = usesDynamicProfile ? resolvedRequestContext().model : self.model model.prewarm(for: self, promptPrefix: promptPrefix) } @@ -891,6 +904,7 @@ public final class LanguageModelSession: @unchecked Sendable { requestContext: context, currentRoundEntries: [] ) + session.lastResponseModel.withLock { $0 = prepared.context.model } } session.endResponding() continuation.finish() @@ -1272,6 +1286,7 @@ public final class LanguageModelSession: @unchecked Sendable { requestContext: context, currentRoundEntries: [] ) + lastResponseModel.withLock { $0 = prepared.context.model } } catch { let ids = Set(response.transcriptEntries.map(\.id) + [responseEntry.id]) withTranscriptMutation { @@ -1980,7 +1995,10 @@ extension LanguageModelSession { issues: [LanguageModelFeedback.Issue] = [], desiredOutput: Transcript.Entry? = nil ) -> Data { - model.logFeedbackAttachment( + let model = + lastResponseModel.withLock { $0 } + ?? (usesDynamicProfile ? resolvedRequestContext().model : self.model) + return model.logFeedbackAttachment( within: self, sentiment: sentiment, issues: issues, diff --git a/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift b/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift index 86e1571e..50306093 100644 --- a/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift @@ -398,16 +398,17 @@ session: LanguageModelSession, prompt: Prompt ) -> FoundationModels.LanguageModelSession { - let requestContext = session.resolvedRequestContext() if session.usesDynamicInstructions { // Foundation Models evaluates the dynamic instructions itself, // so the history leaves out the instructions entry. let history = fmTranscriptDroppingDuplicatePrompt( Transcript( - entries: requestContext.transcript.filter { entry in - if case .instructions = entry { return false } - return true - } + entries: (session.usesDynamicProfile + ? session.resolvedRequestContext().transcript + : session.transcript).filter { entry in + if case .instructions = entry { return false } + return true + } ), prompt: prompt ).toFoundationModels(instructions: nil, toolDefinitions: []) @@ -418,6 +419,7 @@ ) } + let requestContext = session.resolvedRequestContext() return FoundationModels.LanguageModelSession( model: model, tools: requestContext.tools.toFoundationModels(), diff --git a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift index 2dc45a1d..689068f2 100644 --- a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift +++ b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift @@ -14,6 +14,94 @@ extension SessionPropertyValues { @Suite("Dynamic profiles") struct DynamicProfileTests { + @Test func erasedConcreteProfilesKeepDistinctLifecycleIdentities() async throws { + let state = LifecycleFixtureState() + let session = LanguageModelSession(profile: ErasedRoutingProfile(state: state)) + _ = try await session.respond(to: "First") + _ = try await session.respond(to: "Same profile") + state.useSecond.withLock { $0 = true } + _ = try await session.respond(to: "Switch") + + #expect(state.events.withLock { $0 } == ["activate:A", "deactivate:A", "activate:B"]) + } + + @Test func lifecycleHistoryEditsAccumulateAcrossPreparationStages() async throws { + let state = LifecycleFixtureState() + let first = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "First old entry"))])) + let second = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "Second old entry"))])) + let seen = Locked<[[String]]>([]) + let session = LanguageModelSession( + profile: HistoryEditingLifecycleProfile(state: state, first: first.id, second: second.id, seen: seen), + history: [first, second] + ) + _ = try await session.respond(to: "Activate A") + state.useSecond.withLock { $0 = true } + _ = try await session.respond(to: "Activate B") + + let stages = seen.withLock { $0 } + #expect(stages.count == 2) + #expect(stages.allSatisfy { !$0.contains(first.id) }) + #expect(stages.last?.contains(second.id) == false) + #expect(!session.properties.history.contains { $0.id == first.id || $0.id == second.id }) + } + + @Test(arguments: [false, true]) + func completionEntryCallbacksShareHistoryEdits(streaming: Bool) async throws { + let earlier = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "Remove me"))])) + let marker = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "Marker"))])) + let session = LanguageModelSession( + profile: LanguageModelSession.Profile { Instructions("Evolving callback history") } + .model(LifecycleFixtureModel()) + .onReasoning { _ in + let properties = try #require(SessionPropertyBinding.values) + properties.history.removeAll { $0.id == earlier.id } + } + .onResponse { _ in + let properties = try #require(SessionPropertyBinding.values) + #expect(!properties.history.contains { $0.id == earlier.id }) + properties.history.append(marker) + }, + history: [earlier] + ) + if streaming { + _ = try await session.streamResponse(to: "Complete").collect() + } else { + _ = try await session.respond(to: "Complete") + } + + #expect(!session.properties.history.contains { $0.id == earlier.id }) + #expect(session.properties.history.last == marker) + } + + @Test(arguments: [false, true]) + func prewarmUsesActiveModelAndFeedbackUsesProducingModel(streaming: Bool) async throws { + let state = ProfileFixtureState() + let session = LanguageModelSession( + profile: FixtureProfile( + state: state, + first: ProfileFixtureModel(id: "a", state: state), + second: ProfileFixtureModel(id: "b", state: state) + ) + ) + session.prewarm(promptPrefix: Prompt("Prefix A")) + if streaming { + _ = try await session.streamResponse(to: "First").collect() + } else { + _ = try await session.respond(to: "First") + } + state.selectSecond() + session.prewarm(promptPrefix: Prompt("Prefix B")) + #expect(session.logFeedbackAttachment(sentiment: .positive) == Data("a".utf8)) + if streaming { + _ = try await session.streamResponse(to: "Second").collect() + } else { + _ = try await session.respond(to: "Second") + } + #expect(session.logFeedbackAttachment(sentiment: .positive) == Data("b".utf8)) + #expect(state.prewarms.withLock { $0 } == ["a:Prefix A", "b:Prefix B"]) + #expect(state.feedback.withLock { $0 } == ["a", "b"]) + } + @Test func sameSessionReevaluatesModelInstructionsOptionsAndProperties() async throws { let state = ProfileFixtureState() let first = ProfileFixtureModel(id: "model-a", state: state) @@ -241,6 +329,23 @@ struct DynamicProfileTests { } #if canImport(FoundationModels) && compiler(>=6.4) && !os(tvOS) && !os(watchOS) + @Test func nativeSessionEvaluatesOrdinaryDynamicInstructionsOnce() { + guard #available(macOS 27.0, iOS 27.0, visionOS 27.0, *) else { return } + let session = LanguageModelSession( + model: LifecycleFixtureModel(), + dynamicInstructions: CountingNativeInstructions() + ) + + _ = makeFoundationModelsSession( + model: FoundationModels.SystemLanguageModel.default, + session: session, + prompt: Prompt("Current") + ) + #expect(session.properties.profileRevision == 1) + _ = FoundationModelsDynamicInstructionsAdapter(session: session).body + #expect(session.properties.profileRevision == 2) + } + @Test func nativeSessionUsesProjectedHistory() throws { guard #available(macOS 27.0, iOS 27.0, visionOS 27.0, *) else { return } let earlier = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "Earlier"))])) @@ -529,6 +634,20 @@ private struct ProfileFixtureModel: LanguageModel { let id: String let state: ProfileFixtureState + func prewarm(for session: LanguageModelSession, promptPrefix: Prompt?) { + state.prewarms.withLock { $0.append("\(id):\(promptPrefix?.description ?? "")") } + } + + func logFeedbackAttachment( + within session: LanguageModelSession, + sentiment: LanguageModelFeedback.Sentiment?, + issues: [LanguageModelFeedback.Issue], + desiredOutput: Transcript.Entry? + ) -> Data { + state.feedback.withLock { $0.append(id) } + return Data(id.utf8) + } + func respond( within session: LanguageModelSession, to prompt: Prompt, @@ -603,6 +722,8 @@ private final class ProfileFixtureState: @unchecked Sendable { let transformedHistory = Locked<[[String]]>([]) let callbackEvents = Locked<[String]>([]) let callbackHistories = Locked<[[String]]>([]) + let prewarms = Locked<[String]>([]) + let feedback = Locked<[String]>([]) var usesSecond: Bool { selection.withLock { $0 } } func selectSecond() { selection.withLock { $0 = true } } @@ -703,6 +824,70 @@ private final class LifecycleFixtureState: @unchecked Sendable { } } +private enum FirstErasedProfile {} +private enum SecondErasedProfile {} + +private struct ErasedLeafProfile: LanguageModelSession.DynamicProfile { + var body: some LanguageModelSession.DynamicProfile { + LanguageModelSession.Profile { Instructions("Same leaf type") } + } +} + +private struct ErasedRoutingProfile: LanguageModelSession.DynamicProfile { + let state: LifecycleFixtureState + + var body: some LanguageModelSession.DynamicProfile { + let second = state.useSecond.withLock { $0 } + let profile = + second + ? LanguageModelSession.AnyDynamicProfile(ErasedLeafProfile()) + : LanguageModelSession.AnyDynamicProfile(ErasedLeafProfile()) + let label = second ? "B" : "A" + return profile.model(LifecycleFixtureModel()) + .onActivate { state.record("activate:\(label)") } + .onDeactivate { state.record("deactivate:\(label)") } + } +} + +private struct HistoryEditingLifecycleProfile: LanguageModelSession.DynamicProfile { + let state: LifecycleFixtureState + let first: String + let second: String + let seen: Locked<[[String]]> + + var body: some LanguageModelSession.DynamicProfile { + if state.useSecond.withLock({ $0 }) { + LanguageModelSession.Profile { Instructions("B") } + .model(LifecycleFixtureModel()) + .onActivate { + guard let properties = SessionPropertyBinding.values else { return } + seen.withLock { $0.append(properties.history.map(\.id)) } + properties.history.removeAll { $0.id == second } + } + .onPrompt { _ in + let properties = try #require(SessionPropertyBinding.values) + seen.withLock { $0.append(properties.history.map(\.id)) } + } + } else { + LanguageModelSession.Profile { Instructions("A") } + .model(LifecycleFixtureModel()) + .onDeactivate { + guard let properties = SessionPropertyBinding.values else { return } + properties.history.removeAll { $0.id == first } + } + } + } +} + +private struct CountingNativeInstructions: DynamicInstructions { + @SessionProperty(\.profileRevision) private var revision + + var body: some DynamicInstructions { + revision += 1 + return Instructions("Revision \(revision)") + } +} + private struct LifecycleFixtureProfile: LanguageModelSession.DynamicProfile, @unchecked Sendable { let state: LifecycleFixtureState let model: LifecycleFixtureModel From 0a3580bc97e421f23d6d04ff4f93e695f2b0c2e3 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Sat, 17 Oct 2026 23:52:14 +0800 Subject: [PATCH 15/18] Match HistoryView collection shape and bind session properties in context tools --- .../LanguageModelSession.swift | 39 ++++++--- .../AnyLanguageModel/SessionProperties.swift | 81 +++++++++++++++++-- ...PICompatibilityAnyLanguageModelTests.swift | 17 ++++ ...PICompatibilityFoundationModelsTests.swift | 18 +++++ .../DynamicInstructionsTests.swift | 2 +- .../HistoryViewTests.swift | 47 +++++++++++ .../MLXLanguageModelTests.swift | 2 +- .../NativeSessionPropertyTests.swift | 50 ++++++++++++ .../RequestContextTests.swift | 2 +- .../SessionPropertyTests.swift | 74 +++++++++++++++-- .../Shared/MockLanguageModel.swift | 2 +- 11 files changed, 308 insertions(+), 26 deletions(-) create mode 100644 Tests/AnyLanguageModelTests/HistoryViewTests.swift create mode 100644 Tests/AnyLanguageModelTests/NativeSessionPropertyTests.swift diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 86f00d82..31e464d9 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -141,6 +141,7 @@ public final class LanguageModelSession: @unchecked Sendable { /// with the Foundation Models framework. /// It's public so that language models outside this module /// can read the inputs for each request. + @_spi(Compatibility) public struct RequestContext: Sendable { /// The transcript to send with the request. public let transcript: Transcript @@ -160,22 +161,36 @@ public final class LanguageModelSession: @unchecked Sendable { /// Returns the transcript, instructions, and tools for the next model request. /// - /// Calling this method doesn't change the session. + /// Dynamic instructions may read and update custom session properties while + /// being resolved. Their history snapshot is read-only and instructions are + /// not committed to the canonical transcript. /// /// - Note: This API is exclusive to AnyLanguageModel /// and using it means your code is no longer drop-in compatible /// with the Foundation Models framework. /// It's public so that language models outside this module /// can read the inputs for each request. + @_spi(Compatibility) nonisolated public func resolvedRequestContext() -> RequestContext { guard let dynamicInstructions else { - return RequestContext(transcript: transcript, instructions: instructions, tools: tools) + let transcript = transcript + let history = Transcript.HistoryView( + transcript.filter { + if case .instructions = $0 { return false } + return true + } + ) + return RequestContext( + transcript: transcript, + instructions: instructions, + tools: tools.map { $0.bindingSessionProperties(properties, history: history) } + ) } // Evaluate the body for this request. // The resolved instructions go into the request's transcript, // never into the session's. - let canonicalHistory = Array(transcript) + let canonicalHistory = Transcript.HistoryView(transcript) let resolved = dynamicInstructionsLock.withLock { SessionPropertyBinding.$values.withValue(properties) { SessionPropertyBinding.$history.withValue(canonicalHistory) { @@ -183,7 +198,7 @@ public final class LanguageModelSession: @unchecked Sendable { } } } - var requestTranscript = Transcript(entries: canonicalHistory) + var requestTranscript = Transcript(entries: Array(canonicalHistory)) if let instructions = resolved.instructions { let instructionsEntry = Transcript.Entry.instructions( Transcript.Instructions( @@ -195,7 +210,11 @@ public final class LanguageModelSession: @unchecked Sendable { ) requestTranscript = Transcript(entries: [instructionsEntry] + requestTranscript) } - return RequestContext(transcript: requestTranscript, instructions: resolved.instructions, tools: resolved.tools) + return RequestContext( + transcript: requestTranscript, + instructions: resolved.instructions, + tools: resolved.tools.map { $0.bindingSessionProperties(properties, history: canonicalHistory) } + ) } /// Creates a session with a model, tools, @@ -349,10 +368,12 @@ public final class LanguageModelSession: @unchecked Sendable { self.properties = SessionPropertyValues( historyGetter: { state.withLock { locked in - locked.transcript.filter { entry in - if case .instructions = entry { return false } - return true - } + Transcript.HistoryView( + locked.transcript.filter { entry in + if case .instructions = entry { return false } + return true + } + ) } }, historySetter: { history in diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift index b7b968d2..ecc300d2 100644 --- a/Sources/AnyLanguageModel/SessionProperties.swift +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -6,7 +6,50 @@ extension Transcript { /// /// - Note: This API is exclusive to AnyLanguageModel on OS 26. It mirrors /// Foundation Models 27's `Transcript.HistoryView` role with value semantics. - public typealias HistoryView = [Entry] + public struct HistoryView: RandomAccessCollection, MutableCollection, RangeReplaceableCollection, + ExpressibleByArrayLiteral, Sendable + { + public typealias Element = Entry + public typealias SubSequence = HistoryView + public typealias Indices = Range + private var entries: ArraySlice + + public struct Index: Comparable, Hashable, Strideable, Sendable { + fileprivate let offset: Int + + public static func < (lhs: Self, rhs: Self) -> Bool { lhs.offset < rhs.offset } + public func distance(to other: Self) -> Int { other.offset - offset } + public func advanced(by n: Int) -> Self { Self(offset: offset + n) } + } + + public init() { entries = [] } + public init(_ elements: S) where S.Element == Entry { entries = Array(elements)[...] } + public init(arrayLiteral elements: Entry...) { entries = elements[...] } + private init(entries: ArraySlice) { self.entries = entries } + + public var startIndex: Index { Index(offset: entries.startIndex) } + public var endIndex: Index { Index(offset: entries.endIndex) } + public var indices: Indices { startIndex ..< endIndex } + public func index(after i: Index) -> Index { i.advanced(by: 1) } + public func index(before i: Index) -> Index { i.advanced(by: -1) } + public func index(_ i: Index, offsetBy distance: Int) -> Index { i.advanced(by: distance) } + public func distance(from start: Index, to end: Index) -> Int { start.distance(to: end) } + + public subscript(position: Index) -> Entry { + get { entries[position.offset] } + set { entries[position.offset] = newValue } + } + + public subscript(bounds: Range) -> HistoryView { + get { HistoryView(entries: entries[bounds.lowerBound.offset ..< bounds.upperBound.offset]) } + set { replaceSubrange(bounds, with: newValue) } + } + + public mutating func replaceSubrange(_ subrange: Range, with newElements: C) + where C.Element == Entry { + entries.replaceSubrange(subrange.lowerBound.offset ..< subrange.upperBound.offset, with: newElements) + } + } } /// A type-keyed session property. @@ -24,7 +67,7 @@ public protocol SessionPropertyKey: SendableMetatype { public macro SessionPropertyEntry() = #externalMacro(module: "AnyLanguageModelMacros", type: "SessionPropertyEntryMacro") -/// Session-scoped storage available while dynamic instructions are evaluated. +/// Session-scoped storage shared by dynamic instructions and Tools. /// /// - Note: This API is exclusive to AnyLanguageModel on OS 26 and mirrors /// Foundation Models 27. @@ -56,7 +99,7 @@ public final class SessionPropertyValues: @unchecked Sendable { _withSessionPropertyMutation(keyPath: \.history) { precondition( SessionPropertyBinding.values !== self || SessionPropertyBinding.history == nil, - "Session history is read-only while dynamic instructions are active" + "Session history is read-only while dynamic instructions or a Tool is active" ) historySetter(newValue) } @@ -121,7 +164,7 @@ extension LanguageModelSession { get { guard let values = SessionPropertyBinding.values else { preconditionFailure( - "SessionProperty can only be accessed while dynamic instructions are active" + "SessionProperty can only be accessed while dynamic instructions or a Tool is active" ) } return values[keyPath: keyPath] @@ -129,7 +172,7 @@ extension LanguageModelSession { nonmutating set { guard let values = SessionPropertyBinding.values else { preconditionFailure( - "SessionProperty can only be accessed while dynamic instructions are active" + "SessionProperty can only be accessed while dynamic instructions or a Tool is active" ) } values[keyPath: keyPath] = newValue @@ -143,3 +186,31 @@ extension LanguageModelSession.SessionProperty: @unchecked Sendable where Value: extension DynamicInstructions { public typealias SessionProperty = LanguageModelSession.SessionProperty } + +extension Tool { + func bindingSessionProperties(_ properties: SessionPropertyValues, history: Transcript.HistoryView) -> any Tool { + SessionPropertyTool(wrapped: self, properties: properties, history: history) + } +} + +private struct SessionPropertyTool: Tool { + let wrapped: Wrapped + let properties: SessionPropertyValues + let history: Transcript.HistoryView + + var name: String { wrapped.name } + var description: String { wrapped.description } + var parameters: GenerationSchema { wrapped.parameters } + var includesSchemaInInstructions: Bool { wrapped.includesSchemaInInstructions } + + func call(arguments: Wrapped.Arguments) async throws -> Wrapped.Output { + let history = + SessionPropertyBinding.values === properties + ? SessionPropertyBinding.history ?? history : history + return try await SessionPropertyBinding.$values.withValue(properties) { + try await SessionPropertyBinding.$history.withValue(history) { + try await wrapped.call(arguments: arguments) + } + } + } +} diff --git a/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift b/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift index 2afe9d3e..67f509c8 100644 --- a/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift +++ b/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift @@ -10,6 +10,23 @@ import Testing return false }() + @Test func anyLanguageModelHistoryViewCollectionCompatibility() { + func requireContract(_ history: C) + where + C: RandomAccessCollection & MutableCollection & RangeReplaceableCollection + & ExpressibleByArrayLiteral & Sendable, + C.Element == Transcript.Entry, C.SubSequence == C, C.Index: Hashable & Strideable, + C.Index.Stride == Int + {} + var history: Transcript.HistoryView = [] + let empty: Transcript.HistoryView = history[history.startIndex ..< history.endIndex] + history[history.startIndex ..< history.endIndex] = empty + history.append(contentsOf: empty) + history.replaceSubrange(history.startIndex ..< history.endIndex, with: empty) + requireContract(history) + #expect(history.isEmpty) + } + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) extension SessionPropertyValues { @SessionPropertyEntry var compatibilityRevision = 0 diff --git a/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift b/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift index d8d0c51e..79cfe2d3 100644 --- a/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift +++ b/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift @@ -12,6 +12,24 @@ import Testing #if compiler(>=6.4) #if os(macOS) || os(iOS) || os(visionOS) + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) + @Test func foundationModelsHistoryViewCollectionCompatibility() { + func requireContract(_ history: C) + where + C: RandomAccessCollection & MutableCollection & RangeReplaceableCollection + & ExpressibleByArrayLiteral & Sendable, + C.Element == Transcript.Entry, C.SubSequence == C, C.Index: Hashable & Strideable, + C.Index.Stride == Int + {} + var history: Transcript.HistoryView = [] + let empty: Transcript.HistoryView = history[history.startIndex ..< history.endIndex] + history[history.startIndex ..< history.endIndex] = empty + history.append(contentsOf: empty) + history.replaceSubrange(history.startIndex ..< history.endIndex, with: empty) + requireContract(history) + #expect(history.isEmpty) + } + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) extension SessionPropertyValues { @SessionPropertyEntry var compatibilityRevision = 0 diff --git a/Tests/AnyLanguageModelTests/DynamicInstructionsTests.swift b/Tests/AnyLanguageModelTests/DynamicInstructionsTests.swift index af43ad37..baba7aa1 100644 --- a/Tests/AnyLanguageModelTests/DynamicInstructionsTests.swift +++ b/Tests/AnyLanguageModelTests/DynamicInstructionsTests.swift @@ -1,7 +1,7 @@ import Foundation import Testing -@testable import AnyLanguageModel +@_spi(Compatibility) @testable import AnyLanguageModel @Suite("Dynamic instructions") struct DynamicInstructionsTests { diff --git a/Tests/AnyLanguageModelTests/HistoryViewTests.swift b/Tests/AnyLanguageModelTests/HistoryViewTests.swift new file mode 100644 index 00000000..57ee4107 --- /dev/null +++ b/Tests/AnyLanguageModelTests/HistoryViewTests.swift @@ -0,0 +1,47 @@ +import Testing + +import AnyLanguageModel + +@Suite("HistoryView collection contract") +struct HistoryViewTests { + private func entry(_ text: String) -> Transcript.Entry { + .prompt(.init(segments: [.text(.init(content: text))])) + } + + @Test func mutableSlicesPreserveIndicesAndValueSemantics() { + let first = entry("first") + let second = entry("second") + let third = entry("third") + var history: Transcript.HistoryView = [first, second, third] + let original = history + let middle = history.index(after: history.startIndex) + var slice: Transcript.HistoryView = history[middle ..< history.endIndex] + + #expect(slice.startIndex == middle) + #expect(slice.endIndex == history.endIndex) + #expect(slice.distance(from: slice.startIndex, to: slice.endIndex) == 2) + #expect(slice.index(before: slice.endIndex) == history.index(history.startIndex, offsetBy: 2)) + #expect(Array(slice.indices) == [middle, middle.advanced(by: 1)]) + #expect(slice.startIndex.distance(to: slice.endIndex) == 2) + slice[slice.startIndex] = first + #expect(Array(history) == [first, second, third]) + history[middle ..< history.endIndex] = slice + #expect(Array(history) == [first, first, third]) + #expect(Array(original) == [first, second, third]) + } + + @Test func rangeReplacementAndCollectionInitializers() { + let first = entry("first") + let second = entry("second") + let third = entry("third") + var history = Transcript.HistoryView([first, second]) + history.replaceSubrange(history.startIndex ..< history.index(after: history.startIndex), with: [third, first]) + #expect(Array(history) == [third, first, second]) + history.append(third) + history.append(contentsOf: [second]) + history.removeFirst() + #expect(Array(history) == [first, second, third, second]) + history.removeAll() + #expect(history.isEmpty) + } +} diff --git a/Tests/AnyLanguageModelTests/MLXLanguageModelTests.swift b/Tests/AnyLanguageModelTests/MLXLanguageModelTests.swift index 18841c72..3e931d8f 100644 --- a/Tests/AnyLanguageModelTests/MLXLanguageModelTests.swift +++ b/Tests/AnyLanguageModelTests/MLXLanguageModelTests.swift @@ -1,7 +1,7 @@ import Foundation import Testing -@testable import AnyLanguageModel +@_spi(Compatibility) @testable import AnyLanguageModel #if MLX import class MLX.MLXArray diff --git a/Tests/AnyLanguageModelTests/NativeSessionPropertyTests.swift b/Tests/AnyLanguageModelTests/NativeSessionPropertyTests.swift new file mode 100644 index 00000000..595b0714 --- /dev/null +++ b/Tests/AnyLanguageModelTests/NativeSessionPropertyTests.swift @@ -0,0 +1,50 @@ +import Testing + +#if canImport(FoundationModels) && !os(tvOS) && !os(watchOS) + import FoundationModels + @_spi(Compatibility) @testable import AnyLanguageModel + + @Suite("Native Tool session-property binding") + struct NativeSessionPropertyTests { + @Test func nativeWrappersBindPropertiesWithoutTaskLocalInheritance() async throws { + guard #available(macOS 26.0, iOS 26.0, visionOS 26.0, *) else { return } + let first = AnyLanguageModel.LanguageModelSession(model: MockLanguageModel(), tools: [NativePropertyTool()]) + let second = AnyLanguageModel.LanguageModelSession( + model: MockLanguageModel(), + tools: [NativePropertyTool()] + ) + first.properties.revision = 1 + second.properties.revision = 20 + let firstTool = try #require(first.resolvedRequestContext().tools.toFoundationModels().first) + let secondTool = try #require(second.resolvedRequestContext().tools.toFoundationModels().first) + + let firstCall = Task.detached { try await callNativeTool(firstTool) } + let secondCall = Task.detached { try await callNativeTool(secondTool) } + try await firstCall.value + try await secondCall.value + + #expect(first.properties.revision == 2) + #expect(second.properties.revision == 21) + #expect(AnyLanguageModel.SessionPropertyBinding.values == nil) + } + } + + @available(macOS 26.0, iOS 26.0, visionOS 26.0, *) + private func callNativeTool(_ tool: T) async throws { + #expect(AnyLanguageModel.SessionPropertyBinding.values == nil) + let arguments = try T.Arguments(FoundationModels.GeneratedContent(json: "\"read\"")) + _ = try await tool.call(arguments: arguments) + } + + private struct NativePropertyTool: AnyLanguageModel.Tool { + @AnyLanguageModel.LanguageModelSession.SessionProperty(\.revision) private var revision + let name = "property" + let description = "Update a session property" + var parameters: AnyLanguageModel.GenerationSchema { String.generationSchema } + + func call(arguments: String) async throws -> String { + revision += 1 + return "done" + } + } +#endif diff --git a/Tests/AnyLanguageModelTests/RequestContextTests.swift b/Tests/AnyLanguageModelTests/RequestContextTests.swift index a39cbf7a..5503f183 100644 --- a/Tests/AnyLanguageModelTests/RequestContextTests.swift +++ b/Tests/AnyLanguageModelTests/RequestContextTests.swift @@ -1,6 +1,6 @@ import Testing -@testable import AnyLanguageModel +@_spi(Compatibility) @testable import AnyLanguageModel @Suite("Request context") struct RequestContextTests { diff --git a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift index 00311fdc..df99a875 100644 --- a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift +++ b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift @@ -2,7 +2,7 @@ import Foundation import Observation import Testing -@testable import AnyLanguageModel +@_spi(Compatibility) @testable import AnyLanguageModel extension SessionPropertyValues { @SessionPropertyEntry var revision = 0 @@ -18,6 +18,42 @@ enum SessionPropertyMode { @Suite("Session properties") struct SessionPropertyTests { + @Test(arguments: [false, true]) + func contextToolsCarryPropertiesIntoDetachedExecution(dynamic: Bool) async throws { + let session = + dynamic + ? LanguageModelSession( + model: SessionPropertyFixtureModel(), + dynamicInstructions: PropertyToolInstructions() + ) + : LanguageModelSession( + model: SessionPropertyFixtureModel(), + tools: [PropertyBoundTool()], + instructions: "Static" + ) + let earlier = Transcript.Entry.prompt(.init(segments: [.text(.init(content: "Earlier"))])) + session.properties.history = [earlier] + session.properties.revision = 7 + let context = session.resolvedRequestContext() + let tool = try #require(context.tools.first) + let output = try await Task.detached { + #expect(SessionPropertyBinding.values == nil) + return try await tool.makeOutputSegments(from: GeneratedContent("read")) + }.value + + switch try #require(output.first) { + case .text(let text): + #expect(text.content == "revision:7 history:1") + case .structure(let structured): + #expect(try String(structured.content) == "revision:7 history:1") + case .image: + Issue.record("Unexpected image output") + } + #expect(session.properties.revision == 8) + #expect(Array(session.properties.history) == [earlier]) + #expect(SessionPropertyBinding.values == nil) + } + @Test func propertyDefaultsCanReadAnotherProperty() { let session = LanguageModelSession(model: SessionPropertyFixtureModel()) SessionPropertyBinding.$values.withValue(session.properties) { @@ -55,7 +91,7 @@ struct SessionPropertyTests { resumeContinuation.yield() #expect(try await request.value == "Done") - #expect(session.properties.history == [replacement]) + #expect(Array(session.properties.history) == [replacement]) #expect(session.transcript.count == 2) #expect(!session.isResponding) } @@ -72,7 +108,7 @@ struct SessionPropertyTests { resumeContinuation.yield() await #expect(throws: PausedHistoryError.self) { try await request.value } - #expect(session.properties.history == [replacement]) + #expect(Array(session.properties.history) == [replacement]) #expect(!session.isResponding) } @@ -105,7 +141,7 @@ struct SessionPropertyTests { #expect(context.instructions?.description == "History count 1") #expect(context.transcript.count == 2) - #expect(session.properties.history == [prompt]) + #expect(Array(session.properties.history) == [prompt]) } @Test func requestUsesOneHistorySnapshotDuringDynamicResolution() async { @@ -139,7 +175,7 @@ struct SessionPropertyTests { return true } == [initial] ) - #expect(session.properties.history == [replacement]) + #expect(Array(session.properties.history) == [replacement]) } @Test func replacingHistoryNotifiesTranscriptObservers() { @@ -230,7 +266,7 @@ struct SessionPropertyTests { session.properties.history = [prompt] - #expect(session.properties.history == [prompt]) + #expect(Array(session.properties.history) == [prompt]) #expect(session.transcript.count == 2) guard case .instructions = session.transcript[0] else { Issue.record("Expected the static instructions entry to remain first") @@ -253,7 +289,7 @@ struct SessionPropertyTests { #expect(context.instructions?.description == "Other history count 1") #expect(session.properties.history.isEmpty) - #expect(other.properties.history == [replacement]) + #expect(Array(other.properties.history) == [replacement]) } @Test func replacingHistoryRejectsForeignInstructions() { @@ -265,7 +301,7 @@ struct SessionPropertyTests { dynamicInstructions: SessionPropertyHistoryInstructions() ) - session.properties.history = Array(other.transcript) + session.properties.history = .init(other.transcript) let context = session.resolvedRequestContext() #expect(session.transcript == Transcript(entries: [prompt])) @@ -274,6 +310,28 @@ struct SessionPropertyTests { } } +private struct PropertyToolInstructions: DynamicInstructions { + var body: some DynamicInstructions { + Instructions("Property Tool") + PropertyBoundTool() + } +} + +private struct PropertyBoundTool: Tool { + @LanguageModelSession.SessionProperty(\.revision) private var revision + @LanguageModelSession.SessionProperty(\.history) private var history + + let name = "property" + let description = "Read and update session properties" + var parameters: GenerationSchema { String.generationSchema } + + func call(arguments: String) async throws -> String { + let result = "revision:\(revision) history:\(history.count)" + revision += 1 + return result + } +} + private enum BasePropertyKey: SessionPropertyKey { static let defaultValue = 42 } diff --git a/Tests/AnyLanguageModelTests/Shared/MockLanguageModel.swift b/Tests/AnyLanguageModelTests/Shared/MockLanguageModel.swift index 7fa23bc1..e4e46c98 100644 --- a/Tests/AnyLanguageModelTests/Shared/MockLanguageModel.swift +++ b/Tests/AnyLanguageModelTests/Shared/MockLanguageModel.swift @@ -1,4 +1,4 @@ -@testable import AnyLanguageModel +@_spi(Compatibility) @testable import AnyLanguageModel struct MockLanguageModel: LanguageModel { enum UnavailableReason: Hashable, Sendable { From 25d51f1ff5df796d00d618eaa675ad9bf2596369 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Mon, 19 Oct 2026 09:08:26 +0800 Subject: [PATCH 16/18] Preserve public request context and expose Tool session properties --- .../LanguageModelSession.swift | 2 -- .../AnyLanguageModel/SessionProperties.swift | 2 ++ .../RequestContextPublicAPITests.swift | 26 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 Tests/AnyLanguageModelTests/RequestContextPublicAPITests.swift diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 31e464d9..63711343 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -141,7 +141,6 @@ public final class LanguageModelSession: @unchecked Sendable { /// with the Foundation Models framework. /// It's public so that language models outside this module /// can read the inputs for each request. - @_spi(Compatibility) public struct RequestContext: Sendable { /// The transcript to send with the request. public let transcript: Transcript @@ -170,7 +169,6 @@ public final class LanguageModelSession: @unchecked Sendable { /// with the Foundation Models framework. /// It's public so that language models outside this module /// can read the inputs for each request. - @_spi(Compatibility) nonisolated public func resolvedRequestContext() -> RequestContext { guard let dynamicInstructions else { let transcript = transcript diff --git a/Sources/AnyLanguageModel/SessionProperties.swift b/Sources/AnyLanguageModel/SessionProperties.swift index ecc300d2..7f897e19 100644 --- a/Sources/AnyLanguageModel/SessionProperties.swift +++ b/Sources/AnyLanguageModel/SessionProperties.swift @@ -188,6 +188,8 @@ extension DynamicInstructions { } extension Tool { + public typealias SessionProperty = LanguageModelSession.SessionProperty + func bindingSessionProperties(_ properties: SessionPropertyValues, history: Transcript.HistoryView) -> any Tool { SessionPropertyTool(wrapped: self, properties: properties, history: history) } diff --git a/Tests/AnyLanguageModelTests/RequestContextPublicAPITests.swift b/Tests/AnyLanguageModelTests/RequestContextPublicAPITests.swift new file mode 100644 index 00000000..ee991bff --- /dev/null +++ b/Tests/AnyLanguageModelTests/RequestContextPublicAPITests.swift @@ -0,0 +1,26 @@ +import AnyLanguageModel +import Testing + +@Test func requestContextRemainsPublicWithoutSPI() { + let session = LanguageModelSession( + model: OpenAILanguageModel(apiKey: "unused", model: "test"), + tools: [PublicSessionPropertyTool()], + instructions: "Public request context" + ) + let context: LanguageModelSession.RequestContext = session.resolvedRequestContext() + #expect(context.transcript.count == session.transcript.count) + #expect(context.instructions?.description == "Public request context") + #expect(context.tools.map(\.name) == ["publicSessionProperty"]) + let _: PublicSessionPropertyTool.SessionProperty = .init(\.history) +} + +private struct PublicSessionPropertyTool: Tool { + @SessionProperty(\.history) private var history + + let name = "publicSessionProperty" + let description = "Read session history" + + func call(arguments: String) async throws -> String { + String(history.count) + } +} From 982cc0704c88710bc691c8bd5af1ca4f2f4a12c4 Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Mon, 19 Oct 2026 09:10:41 +0800 Subject: [PATCH 17/18] Match the SDK history transform closure contract --- Sources/AnyLanguageModel/DynamicProfile.swift | 6 +++--- .../AnyLanguageModel/GenerationOptions.swift | 1 + ...APICompatibilityAnyLanguageModelTests.swift | 17 +++++++++++++++++ ...APICompatibilityFoundationModelsTests.swift | 18 ++++++++++++++++++ .../DynamicProfileTests.swift | 18 ++++++++++++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) diff --git a/Sources/AnyLanguageModel/DynamicProfile.swift b/Sources/AnyLanguageModel/DynamicProfile.swift index f08c4d3d..51ce2c90 100644 --- a/Sources/AnyLanguageModel/DynamicProfile.swift +++ b/Sources/AnyLanguageModel/DynamicProfile.swift @@ -217,7 +217,7 @@ extension LanguageModelSession.DynamicProfile { } public func historyTransform( - _ transform: @Sendable @escaping ([Transcript.Entry]) -> [Transcript.Entry] + _ transform: @escaping ([Transcript.Entry]) -> [Transcript.Entry] ) -> some LanguageModelSession.DynamicProfile { BuiltinModifiedDynamicProfile(content: self, modifier: .historyTransform(transform)) } @@ -349,7 +349,7 @@ struct ResolvedDynamicProfile: @unchecked Sendable { var hasToolCallingMode = false var transcriptErrorHandlingPolicy: TranscriptErrorHandlingPolicy? var hasTranscriptErrorHandlingPolicy = false - var historyTransforms: [@Sendable ([Transcript.Entry]) -> [Transcript.Entry]] = [] + var historyTransforms: [([Transcript.Entry]) -> [Transcript.Entry]] = [] var onPrompt: [(Transcript.Prompt) async throws -> Void] = [] var onResponse: [(Transcript.Response) async throws -> Void] = [] var onReasoning: [(Transcript.Reasoning) async throws -> Void] = [] @@ -430,7 +430,7 @@ private enum BuiltinDynamicProfileModifier: @unchecked Sendable { case reasoningLevel(ContextOptions.ReasoningLevel?) case includeSchemaInPrompt(Bool?) case toolCallingMode(GenerationOptions.ToolCallingMode?) - case historyTransform(@Sendable ([Transcript.Entry]) -> [Transcript.Entry]) + case historyTransform(([Transcript.Entry]) -> [Transcript.Entry]) case transcriptErrorHandlingPolicy(TranscriptErrorHandlingPolicy?) case onPrompt((Transcript.Prompt) async throws -> Void) case onResponse((Transcript.Response) async throws -> Void) diff --git a/Sources/AnyLanguageModel/GenerationOptions.swift b/Sources/AnyLanguageModel/GenerationOptions.swift index 56cd4fc4..cfe15d38 100644 --- a/Sources/AnyLanguageModel/GenerationOptions.swift +++ b/Sources/AnyLanguageModel/GenerationOptions.swift @@ -156,6 +156,7 @@ public struct GenerationOptions: Sendable, Equatable { precondition(maximumToolRounds >= 1, "maximumToolRounds must be at least 1, got \(maximumToolRounds)") } } + /// Storage for model-specific custom options. private var customOptionsStorage: CustomOptionsStorage = .init() diff --git a/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift b/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift index de982bb6..9d2eb3bb 100644 --- a/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift +++ b/Tests/AnyLanguageModelTests/APICompatibilityAnyLanguageModelTests.swift @@ -65,6 +65,23 @@ import Testing } } + @Test func anyLanguageModelNonSendableHistoryTransformCompatibility() { + let captured = NonSendableHistoryCapture() + let transform: ([Transcript.Entry]) -> [Transcript.Entry] = { entries in + captured.calls += 1 + return entries + } + _ = LanguageModelSession.AnyDynamicProfile( + LanguageModelSession.Profile { Instructions("Projection") } + .historyTransform(transform) + ) + #expect(captured.calls == 0) + } + + private final class NonSendableHistoryCapture { + var calls = 0 + } + @available(macOS 26.0, iOS 26.0, tvOS 26.0, visionOS 26.0, *) @Test("AnyLanguageModel Drop-In Compatibility", .enabled(if: isSystemLanguageModelAvailable)) func anyLanguageModelCompatibility() async throws { diff --git a/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift b/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift index 37b3eec8..8901e486 100644 --- a/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift +++ b/Tests/AnyLanguageModelTests/APICompatibilityFoundationModelsTests.swift @@ -67,6 +67,24 @@ import Testing .transcriptErrorHandlingPolicy(nil) } } + + @available(macOS 27.0, iOS 27.0, visionOS 27.0, *) + @Test func foundationModelsNonSendableHistoryTransformCompatibility() { + let captured = NonSendableHistoryCapture() + let transform: ([Transcript.Entry]) -> [Transcript.Entry] = { entries in + captured.calls += 1 + return entries + } + _ = LanguageModelSession.AnyDynamicProfile( + LanguageModelSession.Profile { Instructions("Projection") } + .historyTransform(transform) + ) + #expect(captured.calls == 0) + } + + private final class NonSendableHistoryCapture { + var calls = 0 + } #endif #endif diff --git a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift index a54909d8..45431adc 100644 --- a/Tests/AnyLanguageModelTests/DynamicProfileTests.swift +++ b/Tests/AnyLanguageModelTests/DynamicProfileTests.swift @@ -51,6 +51,20 @@ struct DynamicProfileBuilderTests { #expect(profile.maximumResponseTokens == 64) } + @Test func historyTransformCanCaptureNonSendableState() { + let recorder = HistoryTransformRecorder() + let profile = LanguageModelSession.AnyDynamicProfile( + LanguageModelSession.Profile { Instructions("Projection") } + .historyTransform { entries in + recorder.entryIDs = entries.map(\.id) + return entries + } + ).resolveForRequest() + let history: [Transcript.Entry] = [.prompt(.init(segments: [.text(.init(content: "Hello"))]))] + #expect(profile.historyTransforms[0](history) == history) + #expect(recorder.entryIDs == history.map(\.id)) + } + @Test func historyTransformsAndCallbacksKeepCompositionOrder() async throws { let events = Locked<[String]>([]) let profile = LanguageModelSession.AnyDynamicProfile( @@ -87,6 +101,10 @@ struct DynamicProfileBuilderTests { } } +private final class HistoryTransformRecorder { + var entryIDs: [String] = [] +} + private struct ConditionalBuilderProfile: LanguageModelSession.DynamicProfile { let first: Bool From db6bc62dc7c31d35cbfcdaf12368e0aa90edc5bc Mon Sep 17 00:00:00 2001 From: "Ronnie W." Date: Wed, 21 Oct 2026 09:13:04 +0800 Subject: [PATCH 18/18] Preserve public Tool identity and bind at execution boundaries --- .../LanguageModelSession.swift | 20 +++++++++++-------- .../Models/AnthropicLanguageModel.swift | 4 ++-- .../Models/GeminiLanguageModel.swift | 4 ++-- .../Models/LlamaLanguageModel.swift | 4 ++-- .../Models/MLXLanguageModel.swift | 4 ++-- .../Models/OllamaLanguageModel.swift | 4 ++-- .../Models/OpenAILanguageModel.swift | 6 +++--- .../Models/OpenResponsesLanguageModel.swift | 4 ++-- .../PrivateCloudComputeLanguageModel.swift | 2 +- .../Models/SystemLanguageModel.swift | 8 ++++---- .../DynamicInstructionsTests.swift | 6 +++--- .../NativeSessionPropertyTests.swift | 8 ++++++-- .../RequestContextPublicAPITests.swift | 18 +++++++++++++++++ .../SessionPropertyTests.swift | 5 +++-- 14 files changed, 62 insertions(+), 35 deletions(-) diff --git a/Sources/AnyLanguageModel/LanguageModelSession.swift b/Sources/AnyLanguageModel/LanguageModelSession.swift index 63711343..0dcf9b74 100644 --- a/Sources/AnyLanguageModel/LanguageModelSession.swift +++ b/Sources/AnyLanguageModel/LanguageModelSession.swift @@ -151,6 +151,16 @@ public final class LanguageModelSession: @unchecked Sendable { /// The tools that the model can call in response to the request. public let tools: [any Tool] + func toolsForExecution(in session: LanguageModelSession) -> [any Tool] { + let history = Transcript.HistoryView( + transcript.filter { + if case .instructions = $0 { return false } + return true + } + ) + return tools.map { $0.bindingSessionProperties(session.properties, history: history) } + } + fileprivate init(transcript: Transcript, instructions: Instructions?, tools: [any Tool]) { self.transcript = transcript self.instructions = instructions @@ -172,16 +182,10 @@ public final class LanguageModelSession: @unchecked Sendable { nonisolated public func resolvedRequestContext() -> RequestContext { guard let dynamicInstructions else { let transcript = transcript - let history = Transcript.HistoryView( - transcript.filter { - if case .instructions = $0 { return false } - return true - } - ) return RequestContext( transcript: transcript, instructions: instructions, - tools: tools.map { $0.bindingSessionProperties(properties, history: history) } + tools: tools ) } @@ -211,7 +215,7 @@ public final class LanguageModelSession: @unchecked Sendable { return RequestContext( transcript: requestTranscript, instructions: resolved.instructions, - tools: resolved.tools.map { $0.bindingSessionProperties(properties, history: canonicalHistory) } + tools: resolved.tools ) } diff --git a/Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift b/Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift index 3f370420..91677d36 100644 --- a/Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift @@ -489,7 +489,7 @@ public struct AnthropicLanguageModel: LanguageModel { if !toolUses.isEmpty { let resolution = try await resolveToolUses( toolUses, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) switch resolution { @@ -698,7 +698,7 @@ public struct AnthropicLanguageModel: LanguageModel { try toolRounds.record(completed: state.entries) switch try await resolveToolUses( toolUses, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) { case .stop(let calls): diff --git a/Sources/AnyLanguageModel/Models/GeminiLanguageModel.swift b/Sources/AnyLanguageModel/Models/GeminiLanguageModel.swift index 7e69a923..242d061e 100644 --- a/Sources/AnyLanguageModel/Models/GeminiLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/GeminiLanguageModel.swift @@ -293,7 +293,7 @@ public struct GeminiLanguageModel: LanguageModel { try toolRounds.record(completed: entries) let resolution = try await resolveFunctionCalls( functionCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) switch resolution { @@ -475,7 +475,7 @@ public struct GeminiLanguageModel: LanguageModel { try toolRounds.record(completed: state.entries) switch try await resolveFunctionCalls( functionCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) { case .stop(let calls): diff --git a/Sources/AnyLanguageModel/Models/LlamaLanguageModel.swift b/Sources/AnyLanguageModel/Models/LlamaLanguageModel.swift index d03386d4..d5efeb11 100644 --- a/Sources/AnyLanguageModel/Models/LlamaLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/LlamaLanguageModel.swift @@ -932,7 +932,7 @@ import Foundation let resolution = try await resolveToolCalls( parsedCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) switch resolution { @@ -1194,7 +1194,7 @@ import Foundation let resolution = try await self.resolveToolCalls( parsedCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) switch resolution { diff --git a/Sources/AnyLanguageModel/Models/MLXLanguageModel.swift b/Sources/AnyLanguageModel/Models/MLXLanguageModel.swift index 51be9b0c..c53f0e82 100644 --- a/Sources/AnyLanguageModel/Models/MLXLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/MLXLanguageModel.swift @@ -1199,7 +1199,7 @@ import Foundation let resolution = try await resolveToolCalls( collectedToolCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) switch resolution { @@ -1440,7 +1440,7 @@ import Foundation let resolution = try await resolveToolCalls( collectedToolCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) switch resolution { diff --git a/Sources/AnyLanguageModel/Models/OllamaLanguageModel.swift b/Sources/AnyLanguageModel/Models/OllamaLanguageModel.swift index 36c3ddfb..2701770e 100644 --- a/Sources/AnyLanguageModel/Models/OllamaLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/OllamaLanguageModel.swift @@ -160,7 +160,7 @@ public struct OllamaLanguageModel: LanguageModel { if let toolCalls = chatResponse.message.toolCalls, !toolCalls.isEmpty { let resolution = try await resolveToolCalls( toolCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) switch resolution { @@ -296,7 +296,7 @@ public struct OllamaLanguageModel: LanguageModel { try toolRounds.record(completed: state.entries) switch try await resolveToolCalls( toolCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) { case .stop(let calls): diff --git a/Sources/AnyLanguageModel/Models/OpenAILanguageModel.swift b/Sources/AnyLanguageModel/Models/OpenAILanguageModel.swift index b6f17391..f7ed4bd9 100644 --- a/Sources/AnyLanguageModel/Models/OpenAILanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/OpenAILanguageModel.swift @@ -555,7 +555,7 @@ public struct OpenAILanguageModel: LanguageModel { try toolRounds.record(completed: entries) let resolution = try await resolveToolCalls( toolCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) switch resolution { @@ -686,7 +686,7 @@ public struct OpenAILanguageModel: LanguageModel { try toolRounds.record(completed: entries) let resolution = try await resolveToolCalls( toolCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) switch resolution { @@ -902,7 +902,7 @@ public struct OpenAILanguageModel: LanguageModel { try toolRounds.record(completed: state.entries) switch try await resolveToolCalls( toolCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) { case .stop(let calls): diff --git a/Sources/AnyLanguageModel/Models/OpenResponsesLanguageModel.swift b/Sources/AnyLanguageModel/Models/OpenResponsesLanguageModel.swift index 807bbb5e..f4e80a2d 100644 --- a/Sources/AnyLanguageModel/Models/OpenResponsesLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/OpenResponsesLanguageModel.swift @@ -553,7 +553,7 @@ public struct OpenResponsesLanguageModel: LanguageModel { try toolRounds.record(completed: state.entries) switch try await resolveToolCalls( toolCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) { case .stop(let calls): @@ -645,7 +645,7 @@ public struct OpenResponsesLanguageModel: LanguageModel { try toolRounds.record(completed: entries) let resolution = try await resolveToolCalls( toolCalls, - tools: requestContext.tools, + tools: requestContext.toolsForExecution(in: session), session: session ) switch resolution { diff --git a/Sources/AnyLanguageModel/Models/PrivateCloudComputeLanguageModel.swift b/Sources/AnyLanguageModel/Models/PrivateCloudComputeLanguageModel.swift index d76657c2..b8c6bc3c 100644 --- a/Sources/AnyLanguageModel/Models/PrivateCloudComputeLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/PrivateCloudComputeLanguageModel.swift @@ -130,7 +130,7 @@ // Attach the feedback to the session's conversation, including its latest response. let fmSession = FoundationModels.LanguageModelSession( model: pccModel, - tools: requestContext.tools.toFoundationModels(), + tools: requestContext.toolsForExecution(in: session).toFoundationModels(), transcript: requestContext.transcript.toFoundationModels( instructions: requestContext.instructions, toolDefinitions: requestContext.tools diff --git a/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift b/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift index 88a5e632..8e5300c4 100644 --- a/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/SystemLanguageModel.swift @@ -277,7 +277,7 @@ // Attach the feedback to the session's conversation, including its latest response. let fmSession = FoundationModels.LanguageModelSession( model: systemModel, - tools: requestContext.tools.toFoundationModels(in: session), + tools: requestContext.toolsForExecution(in: session).toFoundationModels(in: session), transcript: requestContext.transcript.toFoundationModels( instructions: requestContext.instructions, toolDefinitions: requestContext.tools @@ -319,7 +319,7 @@ let requestContext = session.resolvedRequestContext() return FoundationModels.LanguageModelSession( model: systemModel, - tools: requestContext.tools.toFoundationModels(in: session), + tools: requestContext.toolsForExecution(in: session).toFoundationModels(in: session), transcript: fmTranscriptDroppingDuplicatePrompt( requestContext.transcript, prompt: prompt @@ -388,7 +388,7 @@ if let instructions = requestContext.instructions { instructions.toFoundationModels() } - requestContext.tools.toFoundationModels(in: session) + requestContext.toolsForExecution(in: session).toFoundationModels(in: session) } } @@ -420,7 +420,7 @@ let requestContext = session.resolvedRequestContext() return FoundationModels.LanguageModelSession( model: model, - tools: requestContext.tools.toFoundationModels(in: session), + tools: requestContext.toolsForExecution(in: session).toFoundationModels(in: session), transcript: fmTranscriptDroppingDuplicatePrompt( requestContext.transcript, prompt: prompt diff --git a/Tests/AnyLanguageModelTests/DynamicInstructionsTests.swift b/Tests/AnyLanguageModelTests/DynamicInstructionsTests.swift index baba7aa1..8943439b 100644 --- a/Tests/AnyLanguageModelTests/DynamicInstructionsTests.swift +++ b/Tests/AnyLanguageModelTests/DynamicInstructionsTests.swift @@ -407,7 +407,7 @@ private struct DynamicContextModel: LanguageModel { var entries: [Transcript.Entry] = [] if continuesAfterTool { - let tool = try #require(first.tools.first) + let tool = try #require(first.toolsForExecution(in: session).first) state.select(.b) let call = Transcript.ToolCall( id: "request-a-call", @@ -475,7 +475,7 @@ private struct FailingAfterToolModel: LanguageModel { return !didFail } if shouldFail { - let tool = try #require(context.tools.first) + let tool = try #require(context.toolsForExecution(in: session).first) _ = try await tool.makeOutputSegments(from: GeneratedContent(properties: [:])) state.select(.b) throw DynamicFixtureError.failed @@ -565,7 +565,7 @@ private struct CancellingAfterToolModel: LanguageModel { return !didSuspend } if shouldSuspend { - let tool = try #require(context.tools.first) + let tool = try #require(context.toolsForExecution(in: session).first) _ = try await tool.makeOutputSegments(from: GeneratedContent(properties: [:])) state.select(.b) control.signalStarted() diff --git a/Tests/AnyLanguageModelTests/NativeSessionPropertyTests.swift b/Tests/AnyLanguageModelTests/NativeSessionPropertyTests.swift index 595b0714..cc76f281 100644 --- a/Tests/AnyLanguageModelTests/NativeSessionPropertyTests.swift +++ b/Tests/AnyLanguageModelTests/NativeSessionPropertyTests.swift @@ -15,8 +15,12 @@ import Testing ) first.properties.revision = 1 second.properties.revision = 20 - let firstTool = try #require(first.resolvedRequestContext().tools.toFoundationModels().first) - let secondTool = try #require(second.resolvedRequestContext().tools.toFoundationModels().first) + let firstTool = try #require( + first.resolvedRequestContext().toolsForExecution(in: first).toFoundationModels().first + ) + let secondTool = try #require( + second.resolvedRequestContext().toolsForExecution(in: second).toFoundationModels().first + ) let firstCall = Task.detached { try await callNativeTool(firstTool) } let secondCall = Task.detached { try await callNativeTool(secondTool) } diff --git a/Tests/AnyLanguageModelTests/RequestContextPublicAPITests.swift b/Tests/AnyLanguageModelTests/RequestContextPublicAPITests.swift index ee991bff..d5b313b2 100644 --- a/Tests/AnyLanguageModelTests/RequestContextPublicAPITests.swift +++ b/Tests/AnyLanguageModelTests/RequestContextPublicAPITests.swift @@ -11,9 +11,27 @@ import Testing #expect(context.transcript.count == session.transcript.count) #expect(context.instructions?.description == "Public request context") #expect(context.tools.map(\.name) == ["publicSessionProperty"]) + #expect(context.tools.first is PublicSessionPropertyTool) let _: PublicSessionPropertyTool.SessionProperty = .init(\.history) } +@Test func requestContextRetainsConcreteToolIdentity() { + let original = PublicReferenceTool() + let session = LanguageModelSession( + model: OpenAILanguageModel(apiKey: "unused", model: "test"), + tools: [original] + ) + let context = session.resolvedRequestContext() + #expect(context.tools.first as? PublicReferenceTool === original) +} + +private final class PublicReferenceTool: Tool { + let name = "reference" + let description = "Preserve the supplied Tool instance" + + func call(arguments: String) async throws -> String { arguments } +} + private struct PublicSessionPropertyTool: Tool { @SessionProperty(\.history) private var history diff --git a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift index df99a875..6ac6d154 100644 --- a/Tests/AnyLanguageModelTests/SessionPropertyTests.swift +++ b/Tests/AnyLanguageModelTests/SessionPropertyTests.swift @@ -19,7 +19,7 @@ enum SessionPropertyMode { @Suite("Session properties") struct SessionPropertyTests { @Test(arguments: [false, true]) - func contextToolsCarryPropertiesIntoDetachedExecution(dynamic: Bool) async throws { + func executionToolsCarryPropertiesIntoDetachedExecution(dynamic: Bool) async throws { let session = dynamic ? LanguageModelSession( @@ -35,7 +35,8 @@ struct SessionPropertyTests { session.properties.history = [earlier] session.properties.revision = 7 let context = session.resolvedRequestContext() - let tool = try #require(context.tools.first) + #expect(context.tools.first is PropertyBoundTool) + let tool = try #require(context.toolsForExecution(in: session).first) let output = try await Task.detached { #expect(SessionPropertyBinding.values == nil) return try await tool.makeOutputSegments(from: GeneratedContent("read"))