Skip to content

Commit 441b4a7

Browse files
andrewhearddaymxn
authored andcommitted
[Firebase AI] Add starter types for Live API
1 parent 584a726 commit 441b4a7

16 files changed

+588
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// The different ways of handling user activity.
18+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
19+
public struct ActivityHandling: EncodableProtoEnum, Hashable, Sendable {
20+
enum Kind: String {
21+
case interrupts = "START_OF_ACTIVITY_INTERRUPTS"
22+
case noInterrupt = "NO_INTERRUPTION"
23+
}
24+
25+
/// If true, start of activity will interrupt the model's response (also
26+
/// called "barge in"). The model's current response will be cut-off in the
27+
/// moment of the interruption. This is the default behavior.
28+
public static let interrupts = ActivityHandling(kind: .interrupts)
29+
30+
/// The model's response will not be interrupted.
31+
public static let noInterrupt = ActivityHandling(kind: .noInterrupt)
32+
33+
/// Returns the raw string representation of the `ActivityHandling` value.
34+
public let rawValue: String
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// Incremental update of the current conversation delivered from the client.
18+
/// All the content here is unconditionally appended to the conversation
19+
/// history and used as part of the prompt to the model to generate content.
20+
///
21+
/// A message here will interrupt any current model generation.
22+
struct BidiGenerateContentClientContent: Encodable {
23+
/// The content appended to the current conversation with the model.
24+
///
25+
/// For single-turn queries, this is a single instance. For multi-turn
26+
/// queries, this is a repeated field that contains conversation history and
27+
/// latest request.
28+
let turns: [ModelContent]?
29+
30+
/// If true, indicates that the server content generation should start with
31+
/// the currently accumulated prompt. Otherwise, the server will await
32+
/// additional messages before starting generation.
33+
let turnComplete: Bool?
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// Messages sent by the client in the BidiGenerateContent RPC call.
18+
enum BidiGenerateContentClientMessage: Encodable {
19+
/// Message to be sent in the first and only first client message.
20+
case setup(BidiGenerateContentSetup)
21+
22+
/// Incremental update of the current conversation delivered from the client.
23+
case clientContent(BidiGenerateContentClientContent)
24+
25+
/// User input that is sent in real time.
26+
case realtimeInput(BidiGenerateContentRealtimeInput)
27+
28+
/// Response to a `ToolCallMessage` received from the server.
29+
case toolResponse(BidiGenerateContentToolResponse)
30+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// User input that is sent in real time.
18+
///
19+
/// This is different from `ClientContentUpdate` in a few ways:
20+
///
21+
/// - Can be sent continuously without interruption to model generation.
22+
/// - If there is a need to mix data interleaved across the
23+
/// `ClientContentUpdate` and the `RealtimeUpdate`, server attempts to
24+
/// optimize for best response, but there are no guarantees.
25+
/// - End of turn is not explicitly specified, but is rather derived from user
26+
/// activity (for example, end of speech).
27+
/// - Even before the end of turn, the data is processed incrementally
28+
/// to optimize for a fast start of the response from the model.
29+
/// - Is always assumed to be the user's input (cannot be used to populate
30+
/// conversation history).
31+
struct BidiGenerateContentRealtimeInput: Encodable {
32+
/// These form the realtime audio input stream.
33+
let audio: Data?
34+
35+
/// Indicates that the audio stream has ended, e.g. because the microphone was
36+
/// turned off.
37+
///
38+
/// This should only be sent when automatic activity detection is enabled
39+
/// (which is the default).
40+
///
41+
/// The client can reopen the stream by sending an audio message.
42+
let audioStreamEnd: Bool?
43+
44+
/// These form the realtime video input stream.
45+
let video: Data?
46+
47+
/// These form the realtime text input stream.
48+
let text: String?
49+
50+
/// Marks the start of user activity.
51+
struct ActivityStart: Encodable {}
52+
53+
/// Marks the start of user activity. This can only be sent if automatic
54+
/// (i.e. server-side) activity detection is disabled.
55+
let activityStart: ActivityStart?
56+
57+
/// Marks the end of user activity.
58+
struct ActivityEnd: Encodable {}
59+
60+
/// Marks the end of user activity. This can only be sent if automatic (i.e.
61+
// server-side) activity detection is disabled.
62+
let activityEnd: ActivityEnd?
63+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// Incremental server update generated by the model in response to client
18+
/// messages.
19+
///
20+
/// Content is generated as quickly as possible, and not in realtime. Clients
21+
/// may choose to buffer and play it out in realtime.
22+
struct BidiGenerateContentServerContent: Decodable {
23+
/// The content that the model has generated as part of the current
24+
/// conversation with the user.
25+
let modelTurn: ModelContent?
26+
27+
/// If true, indicates that the model is done generating. Generation will only
28+
/// start in response to additional client messages. Can be set alongside
29+
/// `content`, indicating that the `content` is the last in the turn.
30+
let turnComplete: Bool?
31+
32+
/// If true, indicates that a client message has interrupted current model
33+
/// generation. If the client is playing out the content in realtime, this is a
34+
/// good signal to stop and empty the current queue. If the client is playing
35+
/// out the content in realtime, this is a good signal to stop and empty the
36+
/// current playback queue.
37+
let interrupted: Bool?
38+
39+
/// If true, indicates that the model is done generating.
40+
///
41+
/// When model is interrupted while generating there will be no
42+
/// 'generation_complete' message in interrupted turn, it will go through
43+
/// 'interrupted > turn_complete'.
44+
///
45+
/// When model assumes realtime playback there will be delay between
46+
/// generation_complete and turn_complete that is caused by model waiting for
47+
/// playback to finish.
48+
let generationComplete: Bool?
49+
50+
/// Metadata specifies sources used to ground generated content.
51+
let groundingMetadata: GroundingMetadata?
52+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// Response message for BidiGenerateContent RPC call.
18+
struct BidiGenerateContentServerMessage: Decodable {
19+
/// The type of the message.
20+
enum MessageType: Decodable {
21+
/// Sent in response to a `BidiGenerateContentSetup` message from the client.
22+
case setupComplete(BidiGenerateContentSetupComplete)
23+
24+
/// Content generated by the model in response to client messages.
25+
case serverContent(BidiGenerateContentServerContent)
26+
27+
/// Request for the client to execute the `function_calls` and return the
28+
/// responses with the matching `id`s.
29+
case toolCall(BidiGenerateContentToolCall)
30+
31+
/// Notification for the client that a previously issued
32+
/// `ToolCallMessage` with the specified `id`s should have been not executed
33+
/// and should be cancelled.
34+
case toolCallCancellation(BidiGenerateContentToolCallCancellation)
35+
36+
/// Server will disconnect soon.
37+
case goAway(GoAway)
38+
}
39+
40+
/// The message type.
41+
let messageType: MessageType
42+
43+
/// Usage metadata about the response(s).
44+
let usageMetadata: GenerateContentResponse.UsageMetadata?
45+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// Message to be sent in the first and only first
18+
/// `BidiGenerateContentClientMessage`. Contains configuration that will apply
19+
/// for the duration of the streaming RPC.
20+
///
21+
/// Clients should wait for a `BidiGenerateContentSetupComplete` message before
22+
/// sending any additional messages.
23+
struct BidiGenerateContentSetup: Encodable {
24+
/// The fully qualified name of the publisher model.
25+
///
26+
/// Publisher model format:
27+
/// `projects/{project}/locations/{location}/publishers/*/models/*`
28+
let model: String
29+
30+
/// Generation config.
31+
///
32+
/// The following fields aren't supported:
33+
///
34+
/// - `response_logprobs`
35+
/// - `response_mime_type`
36+
/// - `logprobs`
37+
/// - `response_schema`
38+
/// - `stop_sequence`
39+
/// - `routing_config`
40+
/// - `audio_timestamp`
41+
let generationConfig: GenerationConfig?
42+
43+
/// The user provided system instructions for the model.
44+
/// Note: only text should be used in parts and content in each part will be
45+
/// in a separate paragraph.
46+
let systemInstruction: ModelContent?
47+
48+
/// A list of `Tools` the model may use to generate the next response.
49+
///
50+
/// A `Tool` is a piece of code that enables the system to interact with
51+
/// external systems to perform an action, or set of actions, outside of
52+
/// knowledge and scope of the model.
53+
let tools: [Tool]?
54+
55+
/// Configures the handling of realtime input.
56+
let realtimeInputConfig: RealtimeInputConfig?
57+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// Sent in response to a `BidiGenerateContentSetup` message from the client.
18+
struct BidiGenerateContentSetupComplete: Decodable {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// Request for the client to execute the `function_calls` and return the
18+
/// responses with the matching `id`s.
19+
struct BidiGenerateContentToolCall: Decodable {
20+
/// The function call to be executed.
21+
let functionCalls: [FunctionCall]?
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
/// Notification for the client that a previously issued `ToolCallMessage`
18+
/// with the specified `id`s should have been not executed and should be
19+
/// cancelled. If there were side-effects to those tool calls, clients may
20+
/// attempt to undo the tool calls. This message occurs only in cases where the
21+
/// clients interrupt server turns.
22+
struct BidiGenerateContentToolCallCancellation: Decodable {
23+
/// The ids of the tool calls to be cancelled.
24+
let ids: [String]?
25+
}

0 commit comments

Comments
 (0)