@@ -51,7 +51,7 @@ protected OcrCorrectFilterBase(IOptionsSnapshot<LanguageOptions> langOptions, IO
5151 new ( ) { Category = HarmCategory . HARM_CATEGORY_DANGEROUS_CONTENT , Threshold = HarmBlockThreshold . BLOCK_NONE } ,
5252 ] ,
5353 systemInstruction : GetSystem ( langOptions . Value , options ) ) ;
54- Task . Run ( Correct , this . cts . Token ) ;
54+ Task . Run ( LoopCorrect , this . cts . Token ) ;
5555 }
5656
5757 protected abstract string GetSystem ( LanguageOptions languageOptions , GoogleAIOptions googleAIOptions ) ;
@@ -98,7 +98,7 @@ public async IAsyncEnumerable<TextRect> ExecutePreTranslate(IAsyncEnumerable<Tex
9898 // WaitCorrectが有効な場合:補正処理を完了してから結果を返す
9999 if ( this . waitCorrect )
100100 {
101- await CorrectCore ( queueData , this . cts . Token ) . ConfigureAwait ( false ) ;
101+ await Correct ( queueData , this . cts . Token ) . ConfigureAwait ( false ) ;
102102 // 補正後のテキストをキャッシュから返す
103103 foreach ( var text in targets )
104104 {
@@ -118,40 +118,45 @@ public async IAsyncEnumerable<TextRect> ExecutePreTranslate(IAsyncEnumerable<Tex
118118
119119 protected abstract ValueTask < IReadOnlyList < T > > GetQueueData ( IEnumerable < TextRect > targets , FilterContext context ) ;
120120
121- private async Task Correct ( )
121+ private async Task LoopCorrect ( )
122122 {
123123 await foreach ( var texts in this . queue . Reader . ReadAllAsync ( this . cts . Token ) )
124124 {
125- while ( true )
125+ await Correct ( texts , this . cts . Token ) . ConfigureAwait ( false ) ;
126+ }
127+ }
128+
129+ private async Task Correct ( IReadOnlyList < T > texts , CancellationToken token )
130+ {
131+ while ( true )
132+ {
133+ try
126134 {
127- try
128- {
129- await CorrectCore ( texts , this . cts . Token ) . ConfigureAwait ( false ) ;
130- break ;
131- }
132- catch ( ApiException e ) when ( e . ErrorCode == 400 )
133- {
134- throw new ApiException ( e . ErrorCode , "GeminiのAPIキーが無効です。設定ダイアログからGoogleAIオプションを設定してください" , e . ErrorStatus ) ;
135- }
136- // サービスが一時的に過負荷になっているか、ダウンしている可能性があります。
137- catch ( ApiException e ) when ( e . ErrorCode == 503 )
138- {
139- this . Logger . LogWarning ( "Geminiのサービスが一時的に過負荷になっているか、ダウンしている可能性があります。500ミリ秒待機して再試行します。" ) ;
140- await Task . Delay ( 500 ) . ConfigureAwait ( false ) ;
141- continue ;
142- }
143- // レート制限を超えました。
144- catch ( ApiException e ) when ( e . ErrorCode == 429 )
145- {
146- this . Logger . LogWarning ( "Geminiのレート制限を超えました。10秒待機して再試行します。" ) ;
147- await Task . Delay ( 10000 ) . ConfigureAwait ( false ) ;
148- continue ;
149- }
150- // Jsonエラーということは指定した以外のレスポンスが返ってきたのでもう一度
151- catch ( JsonException )
152- {
153- continue ;
154- }
135+ await CorrectCore ( texts , token ) . ConfigureAwait ( false ) ;
136+ break ;
137+ }
138+ catch ( ApiException e ) when ( e . ErrorCode == 400 )
139+ {
140+ throw new ApiException ( e . ErrorCode , "GeminiのAPIキーが無効です。設定ダイアログからGoogleAIオプションを設定してください" , e . ErrorStatus ) ;
141+ }
142+ // サービスが一時的に過負荷になっているか、ダウンしている可能性があります。
143+ catch ( ApiException e ) when ( e . ErrorCode == 503 )
144+ {
145+ this . Logger . LogWarning ( "Geminiのサービスが一時的に過負荷になっているか、ダウンしている可能性があります。500ミリ秒待機して再試行します。" ) ;
146+ await Task . Delay ( 500 , token ) . ConfigureAwait ( false ) ;
147+ continue ;
148+ }
149+ // レート制限を超えました。
150+ catch ( ApiException e ) when ( e . ErrorCode == 429 )
151+ {
152+ this . Logger . LogWarning ( "Geminiのレート制限を超えました。10秒待機して再試行します。" ) ;
153+ await Task . Delay ( 10000 , token ) . ConfigureAwait ( false ) ;
154+ continue ;
155+ }
156+ // Jsonエラーということは指定した以外のレスポンスが返ってきたのでもう一度
157+ catch ( JsonException )
158+ {
159+ continue ;
155160 }
156161 }
157162 }
0 commit comments