File: proofreader.cc

package info (click to toggle)
chromium 141.0.7390.107-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,254,812 kB
  • sloc: cpp: 35,264,957; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,636; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,319; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (681 lines) | stat: -rw-r--r-- 25,307 bytes parent folder | download
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/ai/proofreader.h"

#include "base/containers/span.h"
#include "base/metrics/histogram_functions.h"
#include "third_party/blink/public/mojom/ai/model_streaming_responder.mojom-blink.h"
#include "third_party/blink/renderer/modules/ai/ai_interface_proxy.h"
#include "third_party/blink/renderer/modules/ai/ai_metrics.h"
#include "third_party/blink/renderer/modules/ai/ai_writing_assistance_create_client.h"
#include "third_party/blink/renderer/modules/ai/model_execution_responder.h"
#include "third_party/blink/renderer/platform/text/text_break_iterator.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"
#include "third_party/blink/renderer/platform/wtf/text/strcat.h"
#include "third_party/blink/renderer/platform/wtf/text/unicode_string.h"
#include "third_party/re2/src/re2/re2.h"

namespace blink {

// Represents two vector spans with matching subsequences; e.g.
// ['a', 'b', 'c', 'd'] and ['b', 'c', 'z'] -> {1, 0, 2}
struct MatchingBlock {
  // The start index of the matching subsequence in the first list.
  uint32_t start_a = 0;
  // The start index of the matching subsequence in the second list.
  uint32_t start_b = 0;
  // The size of the matching block.
  uint32_t size = 0;
};

// Start and end indices for a pair of subsequences from two vector spans.
struct BlockPair {
  size_t start_a;
  size_t end_a;
  size_t start_b;
  size_t end_b;
};

// Tokenize `text` by splitting it into a vector of words/spaces/punctuation.
Vector<String> Tokenize(const String& text) {
  Vector<String> tokens;
  if (text.empty()) {
    return tokens;
  }

  // Obtain a word break iterator for the entire string.
  // This iterator will find boundaries between words, punctuation, and spaces.
  TextBreakIterator* it = WordBreakIterator(text, 0, text.length());

  if (!it) {
    return tokens;
  }

  // The iterator returns the indices of the boundaries. Iterate through them to
  // create substrings, which we consider as tokens.
  int32_t start = it->first();
  for (int32_t end = it->next(); end != -1; end = it->next()) {
    tokens.push_back(text.Substring(start, end - start));
    start = end;
  }

  return tokens;
}

// Find the longest matching block in a and b.
// If there are multiple maximal matching blocks,
// return one that starts earliest in a, and of all those maximal matching
// blocks that start earliest in a, return the one that starts earliest in b.
//
// I.e:
// a = [" ", "a", "b"],
// b = ["a", "b", " ", "a", "b"]
// returns {0, 2, 3}
MatchingBlock LongestCommonSubsequence(base::span<const String> a,
                                       base::span<const String> b) {
  MatchingBlock result;
  const size_t a_size = a.size();
  const size_t b_size = b.size();
  for (size_t i = 0; i < a_size; ++i) {
    for (size_t j = 0; j < b_size; ++j) {
      uint32_t size = 0;
      while (i + size < a_size && j + size < b_size &&
             a[i + size] == b[j + size]) {
        ++size;
      }
      if (size > result.size) {
        result.start_a = i;
        result.start_b = j;
        result.size = size;
      }
    }
  }
  return result;
}

Vector<MatchingBlock> GetMatchingBlocks(Vector<String> seq_1,
                                        Vector<String> seq_2) {
  base::span seq_1_span = seq_1;
  base::span seq_2_span = seq_2;
  // A queue of block pairs to compare from the two sequences.
  Deque<BlockPair> blocks_to_compare_queue;
  blocks_to_compare_queue.push_back(
      BlockPair(0, seq_1.size(), 0, seq_2.size()));

  Vector<MatchingBlock> matching_blocks;

  while (!blocks_to_compare_queue.empty()) {
    BlockPair block_pair = blocks_to_compare_queue.front();
    blocks_to_compare_queue.pop_front();
    // Find the longest common subsequence in the two subspans.
    MatchingBlock matching_block = LongestCommonSubsequence(
        seq_1_span.subspan(block_pair.start_a,
                           block_pair.end_a - block_pair.start_a),
        seq_2_span.subspan(block_pair.start_b,
                           block_pair.end_b - block_pair.start_b));
    if (matching_block.size == 0) {
      continue;
    }
    // Calculate the location of the matching block in the two original
    // sequences.
    uint32_t matching_start_in_seq_1 =
        block_pair.start_a + matching_block.start_a;
    uint32_t matching_start_in_seq_2 =
        block_pair.start_b + matching_block.start_b;
    matching_blocks.push_back(MatchingBlock(
        matching_start_in_seq_1, matching_start_in_seq_2, matching_block.size));
    // Push the remaining of the blocks to the left of the longest matching
    // block found in this iteration for further process.
    if (block_pair.start_a < matching_start_in_seq_1 &&
        block_pair.start_b < matching_start_in_seq_2) {
      blocks_to_compare_queue.push_back(
          BlockPair(block_pair.start_a, matching_start_in_seq_1,
                    block_pair.start_b, matching_start_in_seq_2));
    }
    // Push the remaining of the blocks to the right of the longest matching
    // block found in this iteration for further process.
    if (matching_start_in_seq_1 + matching_block.size < block_pair.end_a &&
        matching_start_in_seq_2 + matching_block.size < block_pair.end_b) {
      blocks_to_compare_queue.push_back(BlockPair(
          matching_start_in_seq_1 + matching_block.size, block_pair.end_a,
          matching_start_in_seq_2 + matching_block.size, block_pair.end_b));
    }
  }
  // Sort all matching blocks increasingly based on the start index in the
  // first sequence.
  std::sort(
      matching_blocks.begin(), matching_blocks.end(),
      [](MatchingBlock a, MatchingBlock b) { return a.start_a < b.start_a; });

  return matching_blocks;
}

Vector<Correction> FindDifferences(Vector<String> a, Vector<String> b) {
  // Index of next token to process.
  uint32_t a_index = 0;
  uint32_t b_index = 0;
  // Index for error location in the original string that corresponds with
  // the tokenized sequence a.
  uint32_t error_start_index = 0;
  uint32_t error_end_index = 0;
  // Index for correction location in the corrected string that corresponds
  // with the tokenized sequence b.
  uint32_t correction_start_index = 0;
  uint32_t correction_end_index = 0;

  Vector<Correction> corrections;

  Vector<MatchingBlock> matching_blocks = GetMatchingBlocks(a, b);

  // Insert zero block at the end to cover the case when there's no matching
  // block between the two sequences.
  matching_blocks.push_back(MatchingBlock({a.size(), b.size(), 0}));

  for (const MatchingBlock matching_block : matching_blocks) {
    // When there's difference in the two tokenized sequence before the next
    // matching block, a correction should be made to change from "a" to "b".
    if (a_index < matching_block.start_a || b_index < matching_block.start_b) {
      // Calculate error_end_index in the original string by accumulating
      // all the tokens' sizes.
      for (uint32_t i = a_index; i < matching_block.start_a; ++i) {
        error_end_index += a[i].length();
      }

      String correction_text = "";
      // Calculate correction_end_index in the new string by accumulating
      // all the tokens' sizes.
      for (uint32_t i = b_index; i < matching_block.start_b; ++i) {
        correction_end_index += b[i].length();
        // Concatenate tokens to find the correction text.
        correction_text = correction_text + b[i];
      }

      Correction c = Correction({error_start_index, error_end_index,
                                 correction_start_index, correction_end_index,
                                 correction_text});
      corrections.push_back(c);
    }
    // Increment error indexes to the next potential location of difference
    // in the original string.
    error_start_index = error_end_index;
    for (uint32_t i = matching_block.start_a;
         i < matching_block.start_a + matching_block.size; ++i) {
      error_start_index += a[i].length();
    }
    error_end_index = error_start_index;

    // Increment correction indexes to the next potential location of difference
    // in the new string.
    correction_start_index = correction_end_index;
    for (uint32_t i = matching_block.start_b;
         i < matching_block.start_b + matching_block.size; ++i) {
      correction_start_index += b[i].length();
    }
    correction_end_index = correction_start_index;

    // Increment index for processed tokens to the next potential location of
    // difference in the tokenized sequences
    a_index = matching_block.start_a + matching_block.size;
    b_index = matching_block.start_b + matching_block.size;
  }

  return corrections;
}

Vector<Correction> GetCorrections(const String& input,
                                  const String& corrected_input) {
  // Tokenize to find differences on token-level.
  Vector<String> tokenized_input = Tokenize(input);
  Vector<String> tokenized_corrected_input = Tokenize(corrected_input);

  Vector<Correction> corrections =
      FindDifferences(tokenized_input, tokenized_corrected_input);
  return corrections;
}

HeapVector<Member<ProofreadCorrection>> ToProofreadCorrections(
    Vector<Correction> raw_corrections) {
  HeapVector<Member<ProofreadCorrection>> corrections;
  for (const Correction& c : raw_corrections) {
    auto* correction = MakeGarbageCollected<ProofreadCorrection>();
    correction->setStartIndex(c.error_start);
    correction->setEndIndex(c.error_end);
    correction->setCorrection(c.correction);
    corrections.push_back(correction);
  }
  return corrections;
}

V8CorrectionType GetV8CorrectionTypeFromString(const String& type) {
  if (type == "Spelling") {
    return V8CorrectionType(V8CorrectionType::Enum::kSpelling);
  }
  if (type == "Punctuation") {
    return V8CorrectionType(V8CorrectionType::Enum::kPunctuation);
  }
  if (type == "Capitalization") {
    return V8CorrectionType(V8CorrectionType::Enum::kCapitalization);
  }
  if (type == "Preposition") {
    return V8CorrectionType(V8CorrectionType::Enum::kPreposition);
  }
  if (type == "Missing words") {
    return V8CorrectionType(V8CorrectionType::Enum::kMissingWords);
  }
  return V8CorrectionType(V8CorrectionType::Enum::kGrammar);
}

template <>
void AIWritingAssistanceCreateClient<
    mojom::blink::AIProofreader,
    mojom::blink::AIManagerCreateProofreaderClient,
    ProofreaderCreateOptions,
    Proofreader>::
    RemoteCreate(
        mojo::PendingRemote<mojom::blink::AIManagerCreateProofreaderClient>
          client_remote) {
  HeapMojoRemote<mojom::blink::AIManager>& ai_manager_remote =
      AIInterfaceProxy::GetAIManagerRemote(GetExecutionContext());
  ai_manager_remote->CreateProofreader(
      std::move(client_remote), ToMojoProofreaderCreateOptions(options_));
}

template <>
void AIWritingAssistanceCreateClient<
    mojom::blink::AIProofreader,
    mojom::blink::AIManagerCreateProofreaderClient,
    ProofreaderCreateOptions,
    Proofreader>::RemoteCanCreate(CanCreateCallback callback) {
  HeapMojoRemote<mojom::blink::AIManager>& ai_manager_remote =
      AIInterfaceProxy::GetAIManagerRemote(GetExecutionContext());
  ai_manager_remote->CanCreateProofreader(
      ToMojoProofreaderCreateOptions(options_), std::move(callback));
}

Proofreader::Proofreader(
    ScriptState* script_state,
    scoped_refptr<base::SequencedTaskRunner> task_runner,
    mojo::PendingRemote<mojom::blink::AIProofreader> pending_remote,
    ProofreaderCreateOptions* options)
    : ExecutionContextClient(ExecutionContext::From(script_state)),
      remote_(GetExecutionContext()),
      options_(std::move(options)),
      destruction_abort_controller_(AbortController::Create(script_state)),
      create_abort_signal_(options_->getSignalOr(nullptr)),
      task_runner_(std::move(task_runner)) {
  remote_.Bind(std::move(pending_remote), task_runner_);

  if (create_abort_signal_) {
    CHECK(!create_abort_signal_->aborted());
    create_abort_handle_ = create_abort_signal_->AddAlgorithm(
        BindOnce(&Proofreader::OnCreateAbortSignalAborted,
                 WrapWeakPersistent(this), WrapWeakPersistent(script_state)));
  }
}

void Proofreader::Trace(Visitor* visitor) const {
  ScriptWrappable::Trace(visitor);
  ExecutionContextClient::Trace(visitor);
  visitor->Trace(remote_);
  visitor->Trace(options_);
  visitor->Trace(destruction_abort_controller_);
  visitor->Trace(create_abort_signal_);
  visitor->Trace(create_abort_handle_);
}

ScriptPromise<V8Availability> Proofreader::availability(
    ScriptState* script_state,
    ProofreaderCreateCoreOptions* options,
    ExceptionState& exception_state) {
  if (!script_state->ContextIsValid()) {
    ThrowInvalidContextException(exception_state);
    return ScriptPromise<V8Availability>();
  }
  CHECK(options);
  if (!ValidateAndCanonicalizeOptionLanguages(script_state->GetIsolate(),
                                              options)) {
    return ScriptPromise<V8Availability>();
  }

  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<V8Availability>>(script_state);
  auto promise = resolver->Promise();

  ExecutionContext* execution_context = ExecutionContext::From(script_state);
  HeapMojoRemote<mojom::blink::AIManager>& ai_manager_remote =
      AIInterfaceProxy::GetAIManagerRemote(execution_context);

  if (!ai_manager_remote.is_connected()) {
    RejectPromiseWithInternalError(resolver);
    return promise;
  }

  auto callback = BindOnce(
      [](ScriptPromiseResolver<V8Availability>* resolver,
         ExecutionContext* execution_context,
         mojom::blink::ModelAvailabilityCheckResult result) {
        Availability availability = HandleModelAvailabilityCheckResult(
            execution_context, AIMetrics::AISessionType::kProofreader, result);
        resolver->Resolve(AvailabilityToV8(availability));
      },
      WrapPersistent(resolver), WrapPersistent(execution_context));
  ai_manager_remote->CanCreateProofreader(
      ToMojoProofreaderCreateOptions(options), std::move(callback));

  return promise;
}

ScriptPromise<Proofreader> Proofreader::create(
    ScriptState* script_state,
    ProofreaderCreateOptions* options,
    ExceptionState& exception_state) {
  if (!script_state->ContextIsValid()) {
    ThrowInvalidContextException(exception_state);
    return ScriptPromise<Proofreader>();
  }

  CHECK(options);
  if (!ValidateAndCanonicalizeOptionLanguages(script_state->GetIsolate(),
                                              options)) {
    return ScriptPromise<Proofreader>();
  }

  AbortSignal* signal = options->getSignalOr(nullptr);
  if (HandleAbortSignal(signal, script_state, exception_state)) {
    return EmptyPromise();
  }

  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<Proofreader>>(script_state);
  auto promise = resolver->Promise();

  ExecutionContext* execution_context = ExecutionContext::From(script_state);
  HeapMojoRemote<mojom::blink::AIManager>& ai_manager_remote =
      AIInterfaceProxy::GetAIManagerRemote(execution_context);

  if (!ai_manager_remote.is_connected()) {
    RejectPromiseWithInternalError(resolver);
    return promise;
  }

  MakeGarbageCollected<AIWritingAssistanceCreateClient<
      mojom::blink::AIProofreader,
      mojom::blink::AIManagerCreateProofreaderClient, ProofreaderCreateOptions,
      Proofreader>>(script_state, resolver, options);
  return promise;
}

ScriptPromise<ProofreadResult> Proofreader::proofread(
    ScriptState* script_state,
    const String& input,
    const ProofreaderProofreadOptions* options,
    ExceptionState& exception_state) {
  if (!script_state->ContextIsValid()) {
    ThrowInvalidContextException(exception_state);
    return ScriptPromise<ProofreadResult>();
  }

  CHECK(options);
  AbortSignal* composite_signal = CreateCompositeSignal(script_state, options);
  if (HandleAbortSignal(composite_signal, script_state, exception_state)) {
    return EmptyPromise();
  }

  if (!remote_) {
    ThrowSessionDestroyedException(exception_state);
    return ScriptPromise<ProofreadResult>();
  }

  base::UmaHistogramCounts1M(AIMetrics::GetAISessionRequestSizeMetricName(
                                 AIMetrics::AISessionType::kProofreader),
                             static_cast<int>(input.CharactersSizeInBytes()));

  // Resolver and Promise for the final proofread() result.
  auto* resolver = MakeGarbageCollected<ScriptPromiseResolver<ProofreadResult>>(
      script_state);
  auto promise = resolver->Promise();

  String trimmed_input = input.StripWhiteSpace();
  if (trimmed_input.empty()) {
    auto* proofread_result = MakeGarbageCollected<ProofreadResult>();
    proofread_result->setCorrectedInput(input);
    resolver->Resolve(std::move(proofread_result));
    return promise;
  }

  // Step 1: Prompt the model to proofread and return fully corrected text.
  // Pass persistent refs to keep this instance alive during the response.
  auto pending_remote = CreateModelExecutionResponder(
      script_state, composite_signal, task_runner_,
      AIMetrics::AISessionType::kProofreader,

      BindOnce(&Proofreader::OnProofreadComplete, WrapPersistent(this),
               WrapPersistent(resolver), WrapPersistent(script_state),
               WrapPersistent(composite_signal), input),
      /*overflow_callback=*/base::DoNothingWithBoundArgs(WrapPersistent(this)),
      BindOnce(&Proofreader::OnProofreadError, WrapPersistent(this),
               WrapPersistent(resolver)),
      BindOnce(&Proofreader::OnProofreadAbort, WrapPersistent(this),
               WrapPersistent(resolver), WrapPersistent(composite_signal),
               WrapPersistent(script_state)));
  remote_->Proofread(input, std::move(pending_remote));

  return promise;
}

void Proofreader::destroy(ScriptState* script_state,
                          ExceptionState& exception_state) {
  if (!script_state->ContextIsValid()) {
    ThrowInvalidContextException(exception_state);
    return;
  }

  remote_.reset();
  destruction_abort_controller_->abort(script_state);
  DestroyImpl();
}

// TODO(crbug.com424659255): Consolidate with AIWritingAssistanceBase.

void Proofreader::DestroyImpl() {
  remote_.reset();

  if (create_abort_handle_) {
    create_abort_signal_->RemoveAlgorithm(create_abort_handle_);
    create_abort_handle_ = nullptr;
  }
}

void Proofreader::OnCreateAbortSignalAborted(ScriptState* script_state) {
  if (script_state) {
    destruction_abort_controller_->abort(
        script_state, create_abort_signal_->reason(script_state));
  }
  DestroyImpl();
}

AbortSignal* Proofreader::CreateCompositeSignal(
    ScriptState* script_state,
    const ProofreaderProofreadOptions* options) {
  HeapVector<Member<AbortSignal>> signals;

  signals.push_back(destruction_abort_controller_->signal());

  CHECK(options);
  if (options->hasSignal()) {
    signals.push_back(options->signal());
  }

  return MakeGarbageCollected<AbortSignal>(script_state, signals);
}

bool Proofreader::ValidateAndCanonicalizeOptionLanguages(
    v8::Isolate* isolate,
    ProofreaderCreateCoreOptions* options) {
  using LanguageList = std::optional<Vector<String>>;
  if (options->hasExpectedInputLanguages()) {
    LanguageList result = ValidateAndCanonicalizeBCP47Languages(
        isolate, options->expectedInputLanguages());
    if (!result) {
      return false;
    }
    options->setExpectedInputLanguages(*result);
  }

  if (options->hasCorrectionExplanationLanguage()) {
    LanguageList result = ValidateAndCanonicalizeBCP47Languages(
        isolate, {options->correctionExplanationLanguage()});
    if (!result) {
      return false;
    }
    options->setCorrectionExplanationLanguage((*result)[0]);
  }
  return true;
}

void Proofreader::OnProofreadComplete(
    ScriptPromiseResolver<ProofreadResult>* resolver,
    ScriptState* script_state,
    AbortSignal* signal,
    const String& input,
    const String& corrected_input,
    mojom::blink::ModelExecutionContextInfoPtr context_info) {
  DCHECK(resolver);
  if (signal && signal->aborted()) {
    resolver->Reject(signal->reason(script_state));
    return;
  }
  auto* proofread_result = MakeGarbageCollected<ProofreadResult>();
  proofread_result->setCorrectedInput(corrected_input);
  // Step 2: Find list of corrections by comparing original input and fully
  // corrected input from model execution
  auto raw_corrections = GetCorrections(input, corrected_input);
  auto corrections = ToProofreadCorrections(raw_corrections);
  proofread_result->setCorrections(corrections);

  // Resolve if correction types and explanations are not requested.
  if (!options_->includeCorrectionTypes() || corrections.empty()) {
    resolver->Resolve(proofread_result);
    return;
  }

  // Step 3: Fetch correction type labels for all corrections, if requested.
  // Labels are fetched one-by-one, and when all labels are received,
  // GetCorrectionTypes will be responsible for resolving the promise for
  // proofread() with the `proofread_result`.
  GetCorrectionTypes(resolver, script_state, signal, proofread_result,
                     raw_corrections, input, 0);
}

void Proofreader::OnProofreadError(
    ScriptPromiseResolver<ProofreadResult>* resolver,
    DOMException* exception) {
  resolver->Reject(exception);
}

void Proofreader::OnProofreadAbort(
    ScriptPromiseResolver<ProofreadResult>* resolver,
    AbortSignal* signal,
    ScriptState* script_state) {
  resolver->Reject(signal->reason(script_state));
}

void Proofreader::GetCorrectionTypes(
    ScriptPromiseResolver<ProofreadResult>* resolver,
    ScriptState* script_state,
    AbortSignal* signal,
    ProofreadResult* result,
    Vector<Correction> raw_corrections,
    const String& input,
    uint32_t correction_index) {
  // Done getting all correction type labels.
  if (correction_index == result->corrections().size()) {
    resolver->Resolve(result);
    return;
  }

  // Get correction type label for the next correction.
  auto correction = raw_corrections[correction_index];

  auto pending_remote = CreateModelExecutionResponder(
      script_state, signal, task_runner_,
      AIMetrics::AISessionType::kProofreader,
      BindOnce(&Proofreader::OnLabelComplete, WrapPersistent(this),
               WrapPersistent(resolver), WrapPersistent(script_state),
               WrapPersistent(signal), WrapPersistent(result), raw_corrections,
               input, correction_index),
      /*overflow_callback=*/
      base::DoNothingWithBoundArgs(WrapPersistent(this)),
      /*error_callback=*/
      BindOnce([](ScriptPromiseResolver<ProofreadResult>* resolver,
                  DOMException* exception) { resolver->Reject(exception); },
               WrapPersistent(resolver)),
      /*abort_callback=*/
      BindOnce(
          [](ScriptPromiseResolver<ProofreadResult>* resolver,
             AbortSignal* signal, ScriptState* script_state) {
            resolver->Reject(signal->reason(script_state));
          },
          WrapPersistent(resolver), WrapPersistent(signal),
          WrapPersistent(script_state)));

  String from = input.Substring(correction.error_start,
                                correction.error_end - correction.error_start);
  String to = correction.correction;
  String correction_instruction =
      StrCat({"Correcting `", from, "` to `", to, "`"});

  // Annotate the current error in the original input.
  String input_with_error =
      StrCat({input.Substring(0, correction.error_start), "`", from, "`",
              input.Substring(correction.error_end)});

  // Annotate the current correction in the corrected input.
  String corrected_input = result->correctedInput();
  String corrected_input_with_correction =
      StrCat({corrected_input.Substring(0, correction.correction_start), "`",
              to, "`", corrected_input.Substring(correction.correction_end)});

  remote_->GetCorrectionType(input_with_error, corrected_input_with_correction,
                             correction_instruction, std::move(pending_remote));
}

void Proofreader::OnLabelComplete(
    ScriptPromiseResolver<ProofreadResult>* resolver,
    ScriptState* script_state,
    AbortSignal* signal,
    ProofreadResult* result,
    Vector<Correction> raw_corrections,
    const String& input,
    uint32_t correction_index,
    const String& model_response,
    mojom::blink::ModelExecutionContextInfoPtr context_info) {
  DCHECK(resolver);
  if (signal && signal->aborted()) {
    resolver->Reject(signal->reason(script_state));
    return;
  }

  // Default correction type
  String label = "Grammar";

  // Parse the label from the response of the format {"label": "label0"}
  RE2 pattern("{\"label\":\\s*\"([^\"]+)\"}");
  StringUtf8Adaptor adaptor(model_response);
  std::string_view response = adaptor.AsStringView();
  std::string_view label_value;
  if (RE2::FullMatch(response, pattern, &label_value)) {
    label = String::FromUTF8(label_value);
  }
  result->corrections()[correction_index]->setType(
      GetV8CorrectionTypeFromString(label));

  uint32_t next_index = correction_index + 1;

  GetCorrectionTypes(resolver, script_state, signal, result, raw_corrections,
                     input, next_index);
}

}  // namespace blink