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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Sources/AnyLanguageModel/Models/SystemLanguageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,14 @@
struct FoundationModelsDynamicInstructionsAdapter: FoundationModels.DynamicInstructions {
let session: LanguageModelSession

/// Whether the current task is evaluating instructions through an adapter's ``body``.
/// Tests read this to tell the evaluations that Foundation Models makes from any others.
@TaskLocal static var isEvaluating = false

var body: some FoundationModels.DynamicInstructions {
let requestContext = session.resolvedRequestContext()
let requestContext = Self.$isEvaluating.withValue(true) {
session.resolvedRequestContext()
}
if let instructions = requestContext.instructions {
instructions.toFoundationModels()
}
Expand Down
26 changes: 22 additions & 4 deletions Tests/AnyLanguageModelTests/NativeProfileHistoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,13 @@ import Testing
@Test func ordinaryDynamicInstructionsAreEvaluatedOnlyByTheNativeSession() throws {
guard #available(macOS 27.0, iOS 27.0, visionOS 27.0, *) else { return }
let evaluations = Locked(0)
let nativeEvaluations = Locked(0)
let session = AnyLanguageModel.LanguageModelSession(
model: MockLanguageModel(),
dynamicInstructions: CountingProjectionInstructions(evaluations: evaluations)
dynamicInstructions: CountingProjectionInstructions(
evaluations: evaluations,
nativeEvaluations: nativeEvaluations
)
)
#expect(evaluations.withLock { $0 } == 0)
_ = try makeFoundationModelsRequest(
Expand All @@ -605,10 +609,13 @@ import Testing
)
// Foundation Models can evaluate the instructions more than once
// when it creates the native session; macOS 27.2 adds one evaluation.
// Every evaluation must come through the adapter.
let evaluationsAfterRequest = evaluations.withLock { $0 }
#expect(evaluationsAfterRequest >= 1)
#expect(nativeEvaluations.withLock { $0 } >= 1)
#expect(nativeEvaluations.withLock { $0 } == evaluationsAfterRequest)
_ = FoundationModelsDynamicInstructionsAdapter(session: session).body
#expect(evaluations.withLock { $0 } == evaluationsAfterRequest + 1)
#expect(nativeEvaluations.withLock { $0 } == evaluationsAfterRequest + 1)
}
}

Expand Down Expand Up @@ -932,12 +939,23 @@ import Testing

private struct CountingProjectionInstructions: AnyLanguageModel.DynamicInstructions {
let evaluations: Locked<Int>
/// The evaluations made through `FoundationModelsDynamicInstructionsAdapter.body`.
var nativeEvaluations: Locked<Int>? = nil

var body: some AnyLanguageModel.DynamicInstructions {
let count = evaluations.withLock {
let count = recordEvaluation()
AnyLanguageModel.Instructions("Revision \(count)")
}

private func recordEvaluation() -> Int {
if #available(macOS 27.0, iOS 27.0, visionOS 27.0, watchOS 27.0, *),
FoundationModelsDynamicInstructionsAdapter.isEvaluating
{
nativeEvaluations?.withLock { $0 += 1 }
}
return evaluations.withLock {
$0 += 1; return $0
}
AnyLanguageModel.Instructions("Revision \(count)")
}
}
#endif
Loading