Skip to content

Custom AI keys: change the selection only when inserting the result - #889

Merged
LeanBitLab merged 3 commits into
LeanBitLab:mainfrom
4di-hello:fix/ai-key-result-after-hide
Nov 30, 2026
Merged

LeanBitLab merged 3 commits into
LeanBitLab:mainfrom
4di-hello:fix/ai-key-result-after-hide

Conversation

@4di-hello

@4di-hello 4di-hello commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Appending moves the cursor (after the selection the request was made for, or to the end of the text) right before inserting.
  • Replacing uses replaceEntireText, which selects all itself.
  • A selection the request was made for stays as it is.

The generation lock for the offline AI is in its own PR.

🤖 Generated with Claude Code

@LeanBitLab LeanBitLab left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 getExtractedText when 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

  • mPendingAIResult should be cleared when a new AI request starts, so an old result can't be inserted after a newer one.
  • onFinishInputView runs 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 LeanBitLab left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@4di-hello
4di-hello force-pushed the fix/ai-key-result-after-hide branch from 509bc75 to 583d1b3 Compare November 22, 2026 09:28
4di-hello added a commit to 4di-hello/LeanType that referenced this pull request Nov 22, 2026
- 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
@4di-hello

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review! All points are addressed in the updated branch (one commit, force-pushed):

  1. The cancel on hide is back. ProofreadHelper.cancelCurrentJob(keepCustomKeyJob = true) skips it only while a request started by customAsync runs; proofread and translate are cancelled as before, in both flavors.
  2. While the keyboard stays in the same field, the behaviour is as before (package/field id check only), so typing on during an #append request still appends. The text fingerprint is only used for a result that arrived while the keyboard was hidden, and when another field was opened during the request (counted in startInput) - the chat case with the same field id for every chat.
  3. No extra reads for the request: the fingerprint is built from textToProcess (the whole text without a selection; with a selection, a kept result goes to the clipboard), and only a hash and the length are stored. For the check, the text around the cursor is read once, getExtractedText only if that comparison fails.
  4. After taking generateLock, a request that was cancelled while waiting is skipped and doesn't start a generation.
  5. mPendingAIResult is cleared when a new custom key request starts, and a kept result isn't inserted while a newer request runs.

The changes stay in handleCustomAIKey and helpers next to it, two lines in startInput for the kept result, AiFieldCheck (with tests), ProofreadHelper, and the two cancel calls in LatinIME. The prompt building in ProofreadService and SuggestionStripView are untouched; the only ProofreadService change is the lock around provider.generate. The branch merges cleanly together with #888 and #890, and the unit tests pass in both flavors.

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.

4di-hello added a commit to 4di-hello/LeanType that referenced this pull request Nov 23, 2026
- 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 LeanBitLab left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@4di-hello
4di-hello force-pushed the fix/ai-key-result-after-hide branch from 583d1b3 to a1a65ff Compare November 27, 2026 06:35
@4di-hello

Copy link
Copy Markdown
Contributor Author

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.

@4di-hello 4di-hello changed the title Custom AI keys: keep the result when the keyboard hides, insert it only into the same field Custom AI keys: change the selection only when inserting the result Nov 27, 2026

@LeanBitLab LeanBitLab left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

4di-hello and others added 2 commits November 27, 2026 09:04
…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>
@4di-hello

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Both points are addressed:

  • Composing word duplicated ("hello wor" → "hello Hello world."): the composing word is now finished before the result is inserted, so replacing all text replaces everything. Covered by a unit test.
  • Also fixed along the way: suggestions for the old text are cleared and the shift state is updated after the result is inserted (picking a stale suggestion could add it to the result). Tests cover replace-all and append while a word is composing, append with the cursor inside the text, and replacing a selection.

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.
One case doesn't: appending with the cursor inside the text. Nine Mail hides the keyboard whenever the keyboard moves the cursor, so the text sent after moving to the end never arrives (also within a batch edit). Selecting all isn't affected, which is why replacing works. We tried sending the whole text with the result appended, which works there, but in other apps it drops formatting and spans, so we left it out. With the cursor at the end, appending works in Nine Mail too.

@LeanBitLab
LeanBitLab merged commit 79c3bcc into LeanBitLab:main Nov 30, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants