blob: 7042321ab536b31c6e9012d8b2195ab1236202c2 [file] [log] [blame]
Mingyu Leif4477672024-04-12 07:23:561// Copyright 2024 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Mingyu Leifd46adf2024-06-14 06:06:405#ifndef CONTENT_BROWSER_AI_ECHO_AI_MANAGER_IMPL_H_
6#define CONTENT_BROWSER_AI_ECHO_AI_MANAGER_IMPL_H_
Mingyu Leif4477672024-04-12 07:23:567
Jose Dapena Paz2d529e892024-09-04 06:52:228#include <variant>
9
Mike Wasserman311bf8c2025-07-10 18:13:3710#include "base/containers/flat_set.h"
Mingyu Lei682f6582024-06-14 04:26:5911#include "base/no_destructor.h"
12#include "content/public/browser/browser_context.h"
Mingyu Leif6f9bc1c2024-08-15 05:23:2113#include "content/public/browser/render_frame_host.h"
Mingyu Leif4477672024-04-12 07:23:5614#include "mojo/public/cpp/bindings/pending_receiver.h"
Mingyu Lei682f6582024-06-14 04:26:5915#include "mojo/public/cpp/bindings/receiver_set.h"
Mingyu Lei4c04e112024-10-25 04:36:4416#include "mojo/public/cpp/bindings/remote_set.h"
Mingyu Lei5299ac12024-11-13 02:06:1517#include "third_party/blink/public/mojom/ai/ai_language_model.mojom.h"
Mingyu Lei9de94c62024-05-29 07:14:4218#include "third_party/blink/public/mojom/ai/ai_manager.mojom.h"
Mingyu Lei4c04e112024-10-25 04:36:4419#include "third_party/blink/public/mojom/ai/model_download_progress_observer.mojom.h"
Mingyu Leif4477672024-04-12 07:23:5620
Mingyu Lei65f1a30b2024-05-29 06:21:4721namespace content {
Mingyu Lei9da71c872024-05-28 02:22:0222
Mingyu Leifd46adf2024-06-14 06:06:4023// The implementation of `blink::mojom::AIManager` that creates session which
Mingyu Leif70193c2024-08-16 10:26:1724// only echoes back the prompt text used for testing, and all the parameters
25// will be set using the default value.
Mingyu Leifd46adf2024-06-14 06:06:4026class EchoAIManagerImpl : public blink::mojom::AIManager {
Mingyu Lei65f1a30b2024-05-29 06:21:4727 public:
Mingyu Lei298dc8b2024-11-11 09:32:1928 // The context size for EchoAIManagerImpl is intentionally set to a small
29 // value so we can easily simulate the context overflow scenario.
30 static constexpr int kMaxContextSizeInTokens = 1000;
31
Mingyu Leifd46adf2024-06-14 06:06:4032 EchoAIManagerImpl(const EchoAIManagerImpl&) = delete;
33 EchoAIManagerImpl& operator=(const EchoAIManagerImpl&) = delete;
Mingyu Lei65f1a30b2024-05-29 06:21:4734
Mingyu Leifd46adf2024-06-14 06:06:4035 ~EchoAIManagerImpl() override;
Mingyu Leif4477672024-04-12 07:23:5636
Mingyu Leic26cb002024-12-04 15:19:1937 static void Create(mojo::PendingReceiver<blink::mojom::AIManager> receiver);
Mingyu Leif4477672024-04-12 07:23:5638
39 private:
Mingyu Leifd46adf2024-06-14 06:06:4040 friend base::NoDestructor<EchoAIManagerImpl>;
Mingyu Leif4477672024-04-12 07:23:5641
Fergal Dalye835ff22024-10-11 20:07:1342 EchoAIManagerImpl();
Mingyu Leif4477672024-04-12 07:23:5643
Mingyu Lei9de94c62024-05-29 07:14:4244 // `blink::mojom::AIManager` implementation.
Mingyu Leifc5585f2025-02-13 01:33:0045 void CanCreateLanguageModel(
Mike Wasserman16b5ba72025-03-24 21:17:0946 blink::mojom::AILanguageModelCreateOptionsPtr options,
Mingyu Leifc5585f2025-02-13 01:33:0047 CanCreateLanguageModelCallback callback) override;
Mingyu Lei5299ac12024-11-13 02:06:1548 void CreateLanguageModel(
49 mojo::PendingRemote<blink::mojom::AIManagerCreateLanguageModelClient>
50 client,
51 blink::mojom::AILanguageModelCreateOptionsPtr options) override;
Daseul Lee1b3f19a2025-02-05 16:30:0052 void CanCreateSummarizer(blink::mojom::AISummarizerCreateOptionsPtr options,
53 CanCreateSummarizerCallback callback) override;
Jiacheng Guo370bade42024-08-24 04:32:0354 void CreateSummarizer(
Jiacheng Guo49e046a72024-08-29 06:46:1555 mojo::PendingRemote<blink::mojom::AIManagerCreateSummarizerClient> client,
Tsuyoshi Horode5d5f8a2024-09-03 07:31:1356 blink::mojom::AISummarizerCreateOptionsPtr options) override;
Mingyu Leie7787432025-01-28 09:23:1757 void GetLanguageModelParams(GetLanguageModelParamsCallback callback) override;
Mike Wassermand5077dff2024-12-13 18:10:1158 void CanCreateWriter(blink::mojom::AIWriterCreateOptionsPtr options,
59 CanCreateWriterCallback callback) override;
Tsuyoshi Horo0a2451b2024-08-15 02:25:5160 void CreateWriter(
Tsuyoshi Horode5d5f8a2024-09-03 07:31:1361 mojo::PendingRemote<blink::mojom::AIManagerCreateWriterClient> client,
62 blink::mojom::AIWriterCreateOptionsPtr options) override;
Mike Wassermand5077dff2024-12-13 18:10:1163 void CanCreateRewriter(blink::mojom::AIRewriterCreateOptionsPtr options,
64 CanCreateRewriterCallback callback) override;
Tsuyoshi Horo2e2bb012024-08-15 07:57:2965 void CreateRewriter(
Tsuyoshi Horode5d5f8a2024-09-03 07:31:1366 mojo::PendingRemote<blink::mojom::AIManagerCreateRewriterClient> client,
67 blink::mojom::AIRewriterCreateOptionsPtr options) override;
Queenie Zhangf78ea0f2025-06-17 18:03:5068 void CanCreateProofreader(blink::mojom::AIProofreaderCreateOptionsPtr options,
69 CanCreateProofreaderCallback callback) override;
70 void CreateProofreader(
71 mojo::PendingRemote<blink::mojom::AIManagerCreateProofreaderClient>
72 client,
73 blink::mojom::AIProofreaderCreateOptionsPtr options) override;
Mingyu Lei4c04e112024-10-25 04:36:4474 void AddModelDownloadProgressObserver(
75 mojo::PendingRemote<blink::mojom::ModelDownloadProgressObserver>
76 observer_remote) override;
77
Queenie Zhangf78ea0f2025-06-17 18:03:5078 template <typename CanCreateCallback>
79 void CanCreateClient(CanCreateCallback callback);
80
81 template <typename AIClientRemote,
82 typename AIPendingRemote,
83 typename EchoAIClient>
84 void CreateClient(mojo::Remote<AIClientRemote> client_remote);
85
Daseul Leeafef8e822025-05-15 21:52:2486 template <typename AICreateOptions, typename CanCreateCallback>
87 void CanCreateWritingAssistanceClient(AICreateOptions options,
88 CanCreateCallback callback);
89
90 template <typename AICreateOptions,
91 typename AIClientRemote,
92 typename AIPendingRemote,
93 typename EchoAIClient>
94 void CreateWritingAssistanceClient(mojo::PendingRemote<AIClientRemote> client,
95 AICreateOptions options);
96
97 template <typename AIClientRemote,
98 typename AIPendingRemote,
99 typename EchoAIClient>
100 void ReturnAIClientCreationResult(mojo::Remote<AIClientRemote> client_remote);
101
Mingyu Lei5299ac12024-11-13 02:06:15102 void ReturnAILanguageModelCreationResult(
103 mojo::Remote<blink::mojom::AIManagerCreateLanguageModelClient>
Mingyu Lei82e02252025-02-25 16:23:27104 client_remote,
Brad Triebwassere163bfa2025-04-18 02:22:09105 blink::mojom::AILanguageModelSamplingParamsPtr sampling_params,
106 base::flat_set<blink::mojom::AILanguageModelPromptType>
Brad Triebwasser666c7c82025-06-09 22:25:25107 enabled_input_types,
108 uint32_t initial_input_usage);
Mingyu Lei4c04e112024-10-25 04:36:44109
Jiacheng Guo50379cb2024-12-09 06:10:43110 void DoMockDownloadingAndReturn(base::OnceClosure callback);
111
Mike Wasserman311bf8c2025-07-10 18:13:37112 // Returns whether the current mojo receiver triggered mock model download.
113 bool IsModelDownloadedForCurrentReciever() const;
114
115 // The set of mojo receivers that have triggered mock model download.
116 base::flat_set<mojo::ReceiverId> model_downloaded_receivers_;
Jiacheng Guo50379cb2024-12-09 06:10:43117
Mingyu Lei4c04e112024-10-25 04:36:44118 mojo::RemoteSet<blink::mojom::ModelDownloadProgressObserver>
119 download_progress_observers_;
Mingyu Leif4477672024-04-12 07:23:56120
Mingyu Lei46478fe2024-11-01 08:09:27121 mojo::ReceiverSet<blink::mojom::AIManager> receivers_;
Mingyu Lei4c04e112024-10-25 04:36:44122
123 base::WeakPtrFactory<EchoAIManagerImpl> weak_ptr_factory_{this};
Mingyu Leif4477672024-04-12 07:23:56124};
125
Mingyu Lei65f1a30b2024-05-29 06:21:47126} // namespace content
127
Mingyu Leifd46adf2024-06-14 06:06:40128#endif // CONTENT_BROWSER_AI_ECHO_AI_MANAGER_IMPL_H_