Skip to content

Commit 298858c

Browse files
authored
[VertexAI] Add version info to the rest calls (#1238)
* [VertexAI] Add version info to the rest calls * Update FirebaseInterops.cs * Change the version number being used
1 parent 264df10 commit 298858c

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

vertexai/src/GenerativeModel.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,12 @@ private string GetURL() {
330330

331331
private async Task SetRequestHeaders(HttpRequestMessage request) {
332332
request.Headers.Add("x-goog-api-key", _firebaseApp.Options.ApiKey);
333-
// TODO: Get the Version from the Firebase.VersionInfo.SdkVersion (requires exposing it via App)
334-
request.Headers.Add("x-goog-api-client", "genai-csharp/0.1.0");
333+
string version = FirebaseInterops.GetVersionInfoSdkVersion();
334+
request.Headers.Add("x-goog-api-client", $"genai-csharp/{version}");
335+
if (FirebaseInterops.GetIsDataCollectionDefaultEnabled(_firebaseApp)) {
336+
request.Headers.Add("X-Firebase-AppId", _firebaseApp.Options.AppId);
337+
request.Headers.Add("X-Firebase-AppVersion", UnityEngine.Application.version);
338+
}
335339
// Add additional Firebase tokens to the header.
336340
await FirebaseInterops.AddFirebaseTokensAsync(request, _firebaseApp);
337341
}

vertexai/src/Internal/FirebaseInterops.cs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace Firebase.VertexAI.Internal {
2424

2525
// Contains internal helper methods for interacting with other Firebase libraries.
2626
internal static class FirebaseInterops {
27+
// The cached fields for FirebaseApp reflection.
28+
private static PropertyInfo _dataCollectionProperty = null;
2729

2830
// The various App Check types needed to retrieve the token, cached via reflection on startup.
2931
private static Type _appCheckType;
@@ -48,6 +50,7 @@ internal static class FirebaseInterops {
4850
private const string authHeader = "Authorization";
4951

5052
static FirebaseInterops() {
53+
InitializeAppReflection();
5154
InitializeAppCheckReflection();
5255
InitializeAuthReflection();
5356
}
@@ -58,6 +61,72 @@ private static void LogError(string message) {
5861
#endif
5962
}
6063

64+
// Cache the methods needed for FirebaseApp reflection.
65+
private static void InitializeAppReflection() {
66+
try {
67+
_dataCollectionProperty = typeof(FirebaseApp).GetProperty(
68+
"IsDataCollectionDefaultEnabled",
69+
BindingFlags.Instance | BindingFlags.NonPublic);
70+
if (_dataCollectionProperty == null) {
71+
LogError("Could not find FirebaseApp.IsDataCollectionDefaultEnabled property via reflection.");
72+
return;
73+
}
74+
if (_dataCollectionProperty.PropertyType != typeof(bool)) {
75+
LogError("FirebaseApp.IsDataCollectionDefaultEnabled is not a bool, " +
76+
$"but is {_dataCollectionProperty.PropertyType}");
77+
return;
78+
}
79+
} catch (Exception e) {
80+
LogError($"Failed to initialize FirebaseApp reflection: {e}");
81+
}
82+
}
83+
84+
// Gets the property FirebaseApp.IsDataCollectionDefaultEnabled.
85+
public static bool GetIsDataCollectionDefaultEnabled(FirebaseApp firebaseApp) {
86+
if (firebaseApp == null || _dataCollectionProperty == null) {
87+
return false;
88+
}
89+
90+
try {
91+
return (bool)_dataCollectionProperty.GetValue(firebaseApp);
92+
} catch (Exception e) {
93+
LogError($"Error accessing 'IsDataCollectionDefaultEnabled': {e}");
94+
return false;
95+
}
96+
}
97+
98+
// SDK version to use if unable to find it.
99+
private const string _unknownSdkVersion = "unknown";
100+
private static readonly Lazy<string> _sdkVersionFetcher = new(() => {
101+
try {
102+
// Get the type Firebase.VersionInfo from the assembly that defines FirebaseApp.
103+
Type versionInfoType = typeof(FirebaseApp).Assembly.GetType("Firebase.VersionInfo");
104+
if (versionInfoType == null) {
105+
LogError("Firebase.VersionInfo type not found via reflection");
106+
return _unknownSdkVersion;
107+
}
108+
109+
// Firebase.VersionInfo.SdkVersion
110+
PropertyInfo sdkVersionProperty = versionInfoType.GetProperty(
111+
"SdkVersion",
112+
BindingFlags.Static | BindingFlags.NonPublic);
113+
if (sdkVersionProperty == null) {
114+
LogError("Firebase.VersionInfo.SdkVersion property not found via reflection.");
115+
return _unknownSdkVersion;
116+
}
117+
118+
return sdkVersionProperty.GetValue(null) as string ?? _unknownSdkVersion;
119+
} catch (Exception e) {
120+
LogError($"Error accessing SdkVersion via reflection: {e}");
121+
return _unknownSdkVersion;
122+
}
123+
});
124+
125+
// Gets the internal property Firebase.VersionInfo.SdkVersion
126+
internal static string GetVersionInfoSdkVersion() {
127+
return _sdkVersionFetcher.Value;
128+
}
129+
61130
// Cache the various types and methods needed for AppCheck token retrieval.
62131
private static void InitializeAppCheckReflection() {
63132
const string firebaseAppCheckTypeName = "Firebase.AppCheck.FirebaseAppCheck, Firebase.AppCheck";
@@ -301,7 +370,6 @@ internal static async Task AddFirebaseTokensAsync(ClientWebSocket socket, Fireba
301370
socket.Options.SetRequestHeader(authHeader, $"Firebase {authToken}");
302371
}
303372
}
304-
305373
}
306374

307375
}

vertexai/src/LiveGenerativeModel.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ public async Task<LiveSession> ConnectAsync(CancellationToken cancellationToken
9090
string endpoint = GetURL();
9191

9292
// Set initial headers
93-
// TODO: Get the Version from the Firebase.VersionInfo.SdkVersion (requires exposing it via App)
94-
clientWebSocket.Options.SetRequestHeader("x-goog-api-client", "genai-csharp/0.1.0");
93+
string version = FirebaseInterops.GetVersionInfoSdkVersion();
94+
clientWebSocket.Options.SetRequestHeader("x-goog-api-client", $"genai-csharp/{version}");
95+
if (FirebaseInterops.GetIsDataCollectionDefaultEnabled(_firebaseApp)) {
96+
clientWebSocket.Options.SetRequestHeader("X-Firebase-AppId", _firebaseApp.Options.AppId);
97+
clientWebSocket.Options.SetRequestHeader("X-Firebase-AppVersion", UnityEngine.Application.version);
98+
}
9599
// Add additional Firebase tokens to the header.
96100
await FirebaseInterops.AddFirebaseTokensAsync(clientWebSocket, _firebaseApp);
97101

0 commit comments

Comments
 (0)