Repository navigation
Custom AI keys: change the selection only when inserting the result - #889
Conversation
LeanBitLab
left a comment
There was a problem hiding this comment.
Thanks for this. Keeping a custom key result when the editor hides the keyboard is useful, but the PR changes more than custom keys. A few regressions and risks before we can merge:
1. Proofread and translate are no longer cancelled when the keyboard hides (regression)
ProofreadHelper.cancelCurrentJob() is removed from both onWindowHidden and onFinishInputView. That also applies to proofread and translate, but their onSuccess still drops the result when the input view is hidden. So:
- the request runs to the end for nothing (a paid cloud API call, or minutes of CPU on the offline model),
- the loading border is still there when the keyboard comes back, possibly in another app,
- the first tap on proofread in the new field only cancels the old request (with #890, translate and the custom keys do the same),
cancelCurrentJob()becomes dead code in both flavors.
Please keep the cancel on hide and skip it only while a custom key request runs. For example, ProofreadHelper could remember whether the running job was started by customAsync (cancelCurrentJob(keepCustomKeyJob = true)), so proofread and translate keep their current behaviour.
2. Typing during the request now sends the result to the clipboard (behaviour change with the keyboard open)
targetField.isCurrent() now also runs while the keyboard stays open. If the user keeps typing in the same field while waiting, the text no longer matches and the result goes to the clipboard. For #append this is a regression: before, the result was appended. Please keep the existing package/field id check for the normal path and use the text fingerprint only for a result that arrived while the keyboard was hidden (the case this PR is about).
3. Extra reads of the whole field on the main thread
currentAiTarget() reads up to 60k characters before and after the cursor plus getExtractedText (hint 120k chars) right after handleCustomAIKey has already read the same text, and again when the result arrives. In big fields or slow web editors that is several large IPC calls on the main thread. Suggestions:
- build the request snapshot from the text already read for
textToProcess, - only call
getExtractedTextwhen the first comparison fails.
4. A cancelled request still generates after waiting for generateLock (offline)
A request that was cancelled while it waited for the lock still runs a full generation once it gets the lock, because the plugin call can't be interrupted. Please check coroutineContext.isActive after taking the lock and skip provider.generate for a cancelled request.
5. Minor
mPendingAIResultshould be cleared when a new AI request starts, so an old result can't be inserted after a newer one.onFinishInputViewruns on every field switch, so with (1) fixed this mostly goes away, but please test switching fields during a request in a chat app (same field id for every chat).
To keep #888 and #890 easy to merge, please keep the changes inside handleCustomAIKey, AiFieldCheck and ProofreadHelper, and leave the prompt building in ProofreadService (touched by #888) and the toolbar loading code in SuggestionStripView (touched by #890) as they are.
LeanBitLab
left a comment
There was a problem hiding this comment.
Marking as changes requested: please address the regression and risks in my review comment above (mainly: keep cancelling proofread/translate when the keyboard hides, and keep the old insert behaviour while the keyboard stays open).
509bc75 to
583d1b3
Compare
- Proofread and translate are cancelled again when the keyboard hides (their result is dropped then anyway); only a custom key request keeps running. - Appending with a custom key while typing on in the same field works again; the field check is used after another field was opened, and for replacing. - A new custom key request drops an older kept result; a kept result is not inserted while a newer request runs. - Offline: a request cancelled while waiting for the previous generation does not start one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VuiaMhLXwhErf9shhrr1uD
|
Thanks for the careful review! All points are addressed in the updated branch (one commit, force-pushed):
The changes stay in Tested on my phone: switching to another chat during a custom key request puts the result into the clipboard instead of the other chat, and appending while typing on in the same field works as before. |
- Proofread and translate are cancelled again when the keyboard hides (their result is dropped then anyway); only a custom key request keeps running. - Appending with a custom key while typing on in the same field works again; the field check is used after another field was opened, and for replacing. - A new custom key request drops an older kept result; a kept result is not inserted while a newer request runs. - Offline: a request cancelled while waiting for the previous generation does not start one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VuiaMhLXwhErf9shhrr1uD
LeanBitLab
left a comment
There was a problem hiding this comment.
Thanks. We don't want to merge this in its current form: it adds a lot of state for a narrow case. Kept requests lose their loading indicator, can keep running after the keyboard is gone, and can end up in the clipboard (history included). There are also bugs: isBusy can be cleared while the next generation runs, empty fields match across chats, and a waiting result can be dropped silently.
Could you try a smaller fix in Nine Mail: don't select all or move the cursor before the request, and do it only when inserting (replaceEntireText already selects all)? Separately, we'd welcome the offline generation lock as its own PR, with isBusy set and cleared inside the lock.
The custom AI keys selected all text (or moved the cursor to the end for appending) before the request. Some apps, e.g. Nine Mail, hide the keyboard when the selection changes, which cancelled the request and lost the result. The text is still read before the request, but the cursor is moved only when the result is appended, and replacing all text selects it then (replaceEntireText). A selection the request was made for stays as it is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
583d1b3 to
a1a65ff
Compare
|
Thanks, much simpler indeed. Replaced with your suggestion: the custom AI key no longer selects all or moves the cursor before the request; appending moves the cursor at insertion, replacing uses replaceEntireText. Everything else is gone. The generation lock is in its own PR now. |
LeanBitLab
left a comment
There was a problem hiding this comment.
Thanks, this is much simpler. One regression needs fixing first.
Text can be duplicated. When a custom key is pressed while a word is still composing, replaceEntireText can replace only that word and keep the older text: hello wor can become hello Hello world.. The early select-all that this PR removes used to avoid that.
I reproduced it in InputLogicTest with a real key press, calling replaceEntireText directly in place of the AI result. Finishing the composing word right before inserting fixed it there, and typing continued normally.
Please finish the composing word and reset the keyboard's composing state just before inserting (the user can type while the request runs), and add a test. Please also confirm whether this version fixes the Nine Mail issue.
…esult
The user can type while the request runs. Replacing all text while a word was
still composing could replace only that word and keep the text before it
("hello wor" became "hello Hello world."). The composing word is now finished
and the composing state reset right before the result is inserted. Tests for
replacing and appending while a word is composing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After replacing all text, the suggestions for the old composing word stayed on the strip (picking one added it to the result), and the shift state wasn't updated, because the app's selection update after replaceEntireText is the expected one. The strip is cleared and the space state reset before the result is inserted, and the shift state is updated after replacing. Tests for appending at the end when the cursor is inside the text and for replacing a selection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the review! Both points are addressed:
Nine Mail, tested on a phone: replacing all text, replacing a selection and a request while a word is still composing all work now. The keyboard stays open and the result arrives. |
Some apps, e.g. Nine Mail, hide the keyboard when the selection changes. The custom AI keys selected all text (or moved the cursor to the end for appending) before the request, which cancelled it and lost the result.
Now the text is still read before the request, but the selection only changes when the result is inserted:
replaceEntireText, which selects all itself.The generation lock for the offline AI is in its own PR.
🤖 Generated with Claude Code