Files
firebase-ios-sdk/FirebaseAI/Sources/History.swift
Andrew Heard 384fed9a83 feat(ai): support speech metadata on text parts (#16749)
Add SpeechMetadata (public preview) so requests to Gemini
text-to-speech models can attach turn-level instructions to a
TextPart:

- speaker assigns the turn to a voice configured in
  MultiSpeakerVoiceConfig. Multi-speaker requests require it on
  every part.
- style describes sustained delivery, such as emotion, pacing, or
  volume, so it isn't read aloud as part of the transcript.
  Momentary sounds and pauses stay inline as vocal tags, such as
  <laugh> or <short pause>.

Set metadata with the new TextPart.init(_:speechMetadata:). The
existing init(_:) is kept so references like
[String].map(TextPart.init) still compile. TextPart.speechMetadata
and the SpeechMetadata properties are internal while the API is in
preview.

InternalPart encodes and decodes speechMetadata on the wire. History
doesn't merge streamed text parts that carry speech metadata, so
speaker turns stay separate.

Document the multi-speaker requirements on SpeechConfig,
MultiSpeakerVoiceConfig, and SpeakerVoiceConfig.

Replace the gemini-3.1-flash-tts-preview integration tests with a
TextToSpeechTests suite covering gemini-3.8-flash-tts and
gemini-3.8-flash-lite-tts in unary and streaming, single- and
multi-speaker requests.
2026-10-14 18:56:25 -04:00

97 lines
3.0 KiB
Swift

// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
final class History: Sendable {
private let historyLock = NSLock()
private nonisolated(unsafe) var _history: [ModelContent] = []
/// The previous content from the chat that has been successfully sent and received from the
/// model. This will be provided to the model for each message sent as context for the discussion.
public var history: [ModelContent] {
get {
historyLock.withLock { _history }
}
set {
historyLock.withLock { _history = newValue }
}
}
init(history: [ModelContent]) {
self.history = history
}
func append(contentsOf: [ModelContent]) {
historyLock.withLock {
_history.append(contentsOf: contentsOf)
}
}
func append(_ newElement: ModelContent) {
historyLock.withLock {
_history.append(newElement)
}
}
func aggregatedChunks(_ chunks: [ModelContent]) -> ModelContent {
var parts: [InternalPart] = []
var combinedText = ""
var combinedThoughts = ""
func flush() {
if !combinedThoughts.isEmpty {
parts.append(InternalPart(.text(combinedThoughts), isThought: true, thoughtSignature: nil))
combinedThoughts = ""
}
if !combinedText.isEmpty {
parts.append(InternalPart(.text(combinedText), isThought: nil, thoughtSignature: nil))
combinedText = ""
}
}
// Loop through all the parts, aggregating the text.
for part in chunks.flatMap({ $0.internalParts }) {
// Only text parts may be combined.
if case let .text(text) = part.data,
part.thoughtSignature == nil,
part.speechMetadata == nil {
// Thought summaries must not be combined with regular text.
if part.isThought ?? false {
// If we were combining regular text, flush it before handling "thoughts".
if !combinedText.isEmpty {
flush()
}
combinedThoughts += text
} else {
// If we were combining "thoughts", flush it before handling regular text.
if !combinedThoughts.isEmpty {
flush()
}
combinedText += text
}
} else {
// This is a non-combinable part (non-text, signed thought, or speech metadata), flush
// any pending text.
flush()
parts.append(part)
}
}
// Flush any remaining text.
flush()
return ModelContent(role: "model", parts: parts)
}
}