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
36 changes: 34 additions & 2 deletions Sources/AnyLanguageModel/LanguageModelSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,25 @@ public final class LanguageModelSession: @unchecked Sendable {
options: options
)

// Add response entries to transcript
// Add response entry to transcript
let textContent: String
if case .string(let str) = response.rawContent.kind {
textContent = str
} else {
textContent = response.rawContent.jsonString
}

let responseEntry = Transcript.Entry.response(
Transcript.Response(
assetIDs: [],
segments: [.text(.init(content: textContent))]
)
)

// Add tool entries and response to transcript
await MainActor.run {
self.transcript.append(contentsOf: response.transcriptEntries)
self.transcript.append(responseEntry)
}

return response
Expand Down Expand Up @@ -525,9 +541,25 @@ extension LanguageModelSession {
options: options
)

// Add response entries to transcript
// Add response entry to transcript
let textContent: String
if case .string(let str) = response.rawContent.kind {
textContent = str
} else {
textContent = response.rawContent.jsonString
}

let responseEntry = Transcript.Entry.response(
Transcript.Response(
assetIDs: [],
segments: [.text(.init(content: textContent))]
)
)

// Add tool entries and response to transcript
await MainActor.run {
self.transcript.append(contentsOf: response.transcriptEntries)
self.transcript.append(responseEntry)
}

return response
Expand Down
4 changes: 1 addition & 3 deletions Tests/AnyLanguageModelTests/MockLanguageModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ struct MockLanguageModelTests {
let response = try await session.respond(to: "Say hello")
#expect(response.content == "Hello, World!")

// Verify transcript was updated
// Verify transcript was updated (prompt + response)
#expect(session.transcript.count == 2)
#expect(response.transcriptEntries.count > 0)
}

@Test func echoResponse() async throws {
Expand Down Expand Up @@ -99,7 +98,6 @@ struct MockLanguageModelTests {
try await Task.sleep(for: .milliseconds(10))
#expect(asyncSession.isResponding == false)
#expect(asyncSession.transcript.count == 2)
#expect(response.transcriptEntries.count > 0)

// Test streaming response with isResponding state
let streamModel = MockLanguageModel.streamingMock()
Expand Down
9 changes: 1 addition & 8 deletions Tests/AnyLanguageModelTests/Shared/MockLanguageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,10 @@ struct MockLanguageModel: LanguageModel {
let promptWithInstructions = Prompt("Instructions: \(session.instructions?.description ?? "N/A")\n\(prompt)")
let text = try await responseProvider(promptWithInstructions, options)

let responseEntry = Transcript.Entry.response(
Transcript.Response(
assetIDs: [],
segments: [.text(.init(content: text))]
)
)

return LanguageModelSession.Response(
content: text as! Content,
rawContent: GeneratedContent(text),
transcriptEntries: [responseEntry]
transcriptEntries: []
)
}

Expand Down