feat(clients): Part.thoughtSignature and file-backed access tokens; plan rules updated
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,14 +2,31 @@
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include "google/errors.h"
|
||||
#include "google/service_account.h"
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
|
||||
#include "clients/body/json.h"
|
||||
|
||||
namespace molo::google {
|
||||
|
||||
FileTokenAuth::FileTokenAuth(std::string path) : path_(std::move(path)) {}
|
||||
|
||||
void FileTokenAuth::apply(clients::ClientRequest& request) const {
|
||||
std::ifstream in(path_, std::ios::binary);
|
||||
std::string token((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
if (token.size() > 8 * 1024) token.clear();
|
||||
while (!token.empty() && (token.back() == '\n' || token.back() == '\r' ||
|
||||
token.back() == ' '))
|
||||
token.pop_back();
|
||||
if (token.empty())
|
||||
throw GoogleError(401, "UNAUTHENTICATED",
|
||||
"access token file is missing, empty or over the size cap");
|
||||
request.bearer(token);
|
||||
}
|
||||
|
||||
std::shared_ptr<clients::Auth> Credentials::api_key(std::string key) {
|
||||
return std::make_shared<clients::ApiKeyAuth>(std::move(key), "x-goog-api-key");
|
||||
}
|
||||
@@ -62,6 +79,8 @@ std::shared_ptr<clients::Auth> Credentials::api_key_from_env(const char* name) {
|
||||
std::shared_ptr<clients::Auth> Credentials::oauth_from_env() {
|
||||
std::string token = env("GOOGLE_ACCESS_TOKEN");
|
||||
if (!token.empty()) return bearer(std::move(token));
|
||||
std::string file = env("GOOGLE_ACCESS_TOKEN_FILE");
|
||||
if (!file.empty()) return std::make_shared<FileTokenAuth>(std::move(file));
|
||||
return ServiceAccountAuth::from_env();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,19 @@
|
||||
|
||||
namespace molo::google {
|
||||
|
||||
// Bearer token read from a file on every request, so an external refresher
|
||||
// (cron, `gcloud auth print-access-token > file`, the sa-token tool) can
|
||||
// rotate it without restarting the process. File capped at 8 KiB.
|
||||
class FileTokenAuth : public clients::Auth {
|
||||
public:
|
||||
explicit FileTokenAuth(std::string path);
|
||||
void apply(clients::ClientRequest& request) const override;
|
||||
const char* scheme() const override { return "bearer-file"; }
|
||||
|
||||
private:
|
||||
std::string path_;
|
||||
};
|
||||
|
||||
class Credentials {
|
||||
public:
|
||||
static std::shared_ptr<clients::Auth> api_key(std::string key);
|
||||
@@ -22,7 +35,8 @@ class Credentials {
|
||||
// API key only (GOOGLE_API_KEY), or nullptr.
|
||||
static std::shared_ptr<clients::Auth> api_key_from_env(
|
||||
const char* name = "GOOGLE_API_KEY");
|
||||
// OAuth only: GOOGLE_ACCESS_TOKEN as a bearer, else the service account in
|
||||
// OAuth only: GOOGLE_ACCESS_TOKEN as a bearer, else GOOGLE_ACCESS_TOKEN_FILE
|
||||
// (re-read per request), else the service account in
|
||||
// GOOGLE_APPLICATION_CREDENTIALS (self-refreshing), else nullptr.
|
||||
static std::shared_ptr<clients::Auth> oauth_from_env();
|
||||
// Legacy single credential: token, else key, else nullptr.
|
||||
|
||||
@@ -72,6 +72,7 @@ Json Part::to_json() const {
|
||||
out.set("text", Json(text));
|
||||
}
|
||||
if (thought) out.set("thought", Json(true));
|
||||
if (!thought_signature.empty()) out.set("thoughtSignature", Json(thought_signature));
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -79,6 +80,7 @@ Part Part::from_json(const Json& doc) {
|
||||
Part p;
|
||||
p.text = str_of(doc, "text");
|
||||
if (const Json* t = doc.find("thought"); t && t->is_bool()) p.thought = t->as_bool();
|
||||
p.thought_signature = str_of(doc, "thoughtSignature");
|
||||
if (const Json* d = doc.find("inlineData"); d && d->is_object()) {
|
||||
std::string raw;
|
||||
Base64::decode(str_of(*d, "data"), raw);
|
||||
|
||||
@@ -39,6 +39,9 @@ struct FunctionResponse {
|
||||
struct Part {
|
||||
std::string text;
|
||||
bool thought = false;
|
||||
// Opaque, base64 as received. Gemini 3 rejects a replayed functionCall part
|
||||
// that lacks the signature it was given; keep it and send it back.
|
||||
std::string thought_signature;
|
||||
std::optional<InlineData> inline_data;
|
||||
std::optional<FileData> file_data;
|
||||
std::optional<FunctionCall> function_call;
|
||||
|
||||
Reference in New Issue
Block a user