Skip to content

Commit 8f91c7e

Browse files
committed
Pass entire transcript to Foundation Models session
1 parent 8503fd1 commit 8f91c7e

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

Sources/AnyLanguageModel/Models/SystemLanguageModel.swift

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
let fmSession = FoundationModels.LanguageModelSession(
7979
model: systemModel,
8080
tools: session.tools.toFoundationModels(),
81-
instructions: session.instructions?.toFoundationModels()
81+
transcript: session.transcript.toFoundationModels(instructions: session.instructions)
8282
)
8383

8484
let fmResponse = try await fmSession.respond(to: fmPrompt, options: fmOptions)
@@ -115,7 +115,7 @@
115115
let fmSession = FoundationModels.LanguageModelSession(
116116
model: systemModel,
117117
tools: session.tools.toFoundationModels(),
118-
instructions: session.instructions?.toFoundationModels()
118+
transcript: session.transcript.toFoundationModels(instructions: session.instructions)
119119
)
120120

121121
let stream = AsyncThrowingStream<LanguageModelSession.ResponseStream<Content>.Snapshot, any Error> {
@@ -475,4 +475,77 @@
475475
nil
476476
}
477477
}
478+
479+
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
480+
extension Transcript {
481+
fileprivate func toFoundationModels(instructions: AnyLanguageModel.Instructions?) -> FoundationModels.Transcript
482+
{
483+
var fmEntries: [FoundationModels.Transcript.Entry] = []
484+
485+
// Add instructions entry if provided and not already in transcript
486+
if let instructions = instructions {
487+
let hasInstructions =
488+
self.first.map { entry in
489+
if case .instructions = entry { return true } else { return false }
490+
} ?? false
491+
492+
if !hasInstructions {
493+
let fmInstructions = FoundationModels.Transcript.Instructions(
494+
segments: [.text(.init(content: instructions.description))],
495+
toolDefinitions: []
496+
)
497+
fmEntries.append(.instructions(fmInstructions))
498+
}
499+
}
500+
501+
// Convert each entry
502+
for entry in self {
503+
switch entry {
504+
case .instructions(let instr):
505+
let textContent = instr.segments.compactMap { segment -> String? in
506+
if case .text(let textSegment) = segment {
507+
return textSegment.content
508+
}
509+
return nil
510+
}.joined(separator: " ")
511+
let fmInstructions = FoundationModels.Transcript.Instructions(
512+
segments: [.text(.init(content: textContent))],
513+
toolDefinitions: []
514+
)
515+
fmEntries.append(.instructions(fmInstructions))
516+
517+
case .prompt(let prompt):
518+
let textContent = prompt.segments.compactMap { segment -> String? in
519+
if case .text(let textSegment) = segment {
520+
return textSegment.content
521+
}
522+
return nil
523+
}.joined(separator: " ")
524+
let fmPrompt = FoundationModels.Transcript.Prompt(
525+
segments: [.text(.init(content: textContent))]
526+
)
527+
fmEntries.append(.prompt(fmPrompt))
528+
529+
case .response(let response):
530+
let textContent = response.segments.compactMap { segment -> String? in
531+
if case .text(let textSegment) = segment {
532+
return textSegment.content
533+
}
534+
return nil
535+
}.joined(separator: " ")
536+
let fmResponse = FoundationModels.Transcript.Response(
537+
assetIDs: [],
538+
segments: [.text(.init(content: textContent))]
539+
)
540+
fmEntries.append(.response(fmResponse))
541+
542+
case .toolCalls, .toolOutput:
543+
// Tool calls/outputs require more complex conversion; skip for now
544+
break
545+
}
546+
}
547+
548+
return FoundationModels.Transcript(entries: fmEntries)
549+
}
550+
}
478551
#endif

0 commit comments

Comments
 (0)