1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/manta/orca_provider.h"
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "base/check.h"
#include "base/containers/fixed_flat_map.h"
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/endpoint_fetcher/endpoint_fetcher.h"
#include "components/manta/base_provider.h"
#include "components/manta/features.h"
#include "components/manta/manta_service_callbacks.h"
#include "components/manta/manta_status.h"
#include "components/manta/proto/manta.pb.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace manta {
namespace {
constexpr char kOauthConsumerName[] = "manta_orca";
constexpr base::TimeDelta kTimeout = base::Seconds(30);
using Tone = proto::RequestConfig::Tone;
std::optional<Tone> GetTone(const std::string& tone) {
static constexpr auto tone_map =
base::MakeFixedFlatMap<std::string_view, Tone>({
{"UNSPECIFIED", proto::RequestConfig::UNSPECIFIED},
{"SHORTEN", proto::RequestConfig::SHORTEN},
{"ELABORATE", proto::RequestConfig::ELABORATE},
{"REPHRASE", proto::RequestConfig::REPHRASE},
{"FORMALIZE", proto::RequestConfig::FORMALIZE},
{"EMOJIFY", proto::RequestConfig::EMOJIFY},
{"FREEFORM_REWRITE", proto::RequestConfig::FREEFORM_REWRITE},
{"FREEFORM_WRITE", proto::RequestConfig::FREEFORM_WRITE},
{"PROOFREAD", proto::RequestConfig::PROOFREAD},
});
const auto iter = tone_map.find(tone);
return iter != tone_map.end() ? std::optional<Tone>(iter->second)
: std::nullopt;
}
std::optional<proto::Request> ComposeRequest(
const std::map<std::string, std::string>& input) {
const auto& tone_iter = input.find("tone");
if (tone_iter == input.end()) {
DVLOG(1) << "Tone not found in the parameters";
return std::nullopt;
}
auto tone = GetTone(tone_iter->second);
if (tone == std::nullopt) {
DVLOG(1) << "Invalid tone";
return std::nullopt;
}
proto::Request request;
request.set_feature_name(proto::FeatureName::TEXT_TEST);
auto& request_config = *request.mutable_request_config();
request_config.set_tone(tone.value());
for (const auto& kv : input) {
auto* input_data = request.add_input_data();
input_data->set_tag(kv.first);
input_data->set_text(kv.second);
}
return request;
}
void OnServerResponseOrErrorReceived(
MantaGenericCallback callback,
std::unique_ptr<proto::Response> manta_response,
MantaStatus manta_status) {
if (manta_status.status_code != MantaStatusCode::kOk) {
DCHECK(manta_response == nullptr);
std::move(callback).Run(base::Value::Dict(), std::move(manta_status));
return;
}
DCHECK(manta_response != nullptr);
auto output_data_list = base::Value::List();
for (const auto& output_data : manta_response->output_data()) {
if (output_data.has_text()) {
output_data_list.Append(
base::Value::Dict().Set("text", output_data.text()));
}
}
if (output_data_list.size() == 0) {
std::move(callback).Run(
base::Value::Dict(),
{MantaStatusCode::kBlockedOutputs, /*message=*/std::string()});
return;
}
std::move(callback).Run(
base::Value::Dict().Set("outputData", std::move(output_data_list)),
std::move(manta_status));
}
} // namespace
OrcaProvider::OrcaProvider(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
signin::IdentityManager* identity_manager,
const ProviderParams& provider_params)
: BaseProvider(url_loader_factory, identity_manager, provider_params) {}
OrcaProvider::~OrcaProvider() = default;
void OrcaProvider::Call(const std::map<std::string, std::string>& input,
MantaGenericCallback done_callback) {
std::optional<proto::Request> request = ComposeRequest(input);
if (request == std::nullopt) {
std::move(done_callback)
.Run(base::Value::Dict(),
{MantaStatusCode::kInvalidInput, /*message=*/std::string()});
return;
}
const net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("help_me_write_request", R"(
semantics {
sender: "Help Me Write"
description:
"ChromeOS can help you write and rewrite text by sending a "
"freeform text query, along with any selected text, to Google's "
"servers. Google returns suggested text which you may choose to "
"insert into the selected text field."
trigger: "User right clicks within an editable text field and "
"chooses 'Help me write' and then chooses a preset query or "
"enters a free-form text query."
internal {
contacts {
email: "cros-manta-team@google.com"
}
}
user_data {
type: ACCESS_TOKEN
type: USER_CONTENT
}
data: "A preset or free-form user query, along with any text a user "
"has selected in the editable text field. Query metadata is "
"also sent including the user's preferred input language."
destination: GOOGLE_OWNED_SERVICE
last_reviewed: "2024-03-15"
}
policy {
cookies_allowed: NO
setting:
"You can enable or disable this feature via 'Help me write' in "
"ChromeOS's settings under 'Inputs > Suggestions'."
chrome_policy {
OrcaEnabled {
OrcaEnabled: false
}
}
})");
RequestInternal(
GURL{GetProviderEndpoint(features::IsOrcaUseProdServerEnabled())},
kOauthConsumerName, traffic_annotation, request.value(),
MantaMetricType::kOrca,
base::BindOnce(&OnServerResponseOrErrorReceived,
std::move(done_callback)),
kTimeout);
}
} // namespace manta
|