-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
feat: regenerate message with different model #7617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
appflowy
merged 4 commits into
AppFlowy-IO:main
from
richardshiue:chore/improve-model-selection-ui
Mar 27, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 0 additions & 138 deletions
138
frontend/appflowy_flutter/lib/ai/service/ai_input_control.dart
This file was deleted.
Oops, something went wrong.
172 changes: 172 additions & 0 deletions
172
frontend/appflowy_flutter/lib/ai/service/ai_model_state_notifier.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import 'package:appflowy/ai/ai.dart'; | ||
| import 'package:appflowy/generated/locale_keys.g.dart'; | ||
| import 'package:appflowy/plugins/ai_chat/application/ai_model_switch_listener.dart'; | ||
| import 'package:appflowy/workspace/application/settings/ai/local_llm_listener.dart'; | ||
| import 'package:appflowy_backend/dispatch/dispatch.dart'; | ||
| import 'package:appflowy_backend/log.dart'; | ||
| import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart'; | ||
| import 'package:appflowy_result/appflowy_result.dart'; | ||
| import 'package:easy_localization/easy_localization.dart'; | ||
| import 'package:protobuf/protobuf.dart'; | ||
| import 'package:universal_platform/universal_platform.dart'; | ||
|
|
||
| typedef OnModelStateChangedCallback = void Function(AiType, bool, String); | ||
| typedef OnAvailableModelsChangedCallback = void Function( | ||
| List<AIModelPB>, | ||
| AIModelPB?, | ||
| ); | ||
|
|
||
| class AIModelStateNotifier { | ||
| AIModelStateNotifier({required this.objectId}) | ||
| : _localAIListener = | ||
| UniversalPlatform.isDesktop ? LocalAIStateListener() : null, | ||
| _aiModelSwitchListener = AIModelSwitchListener(objectId: objectId) { | ||
| _startListening(); | ||
| _init(); | ||
| } | ||
|
|
||
| final String objectId; | ||
| final LocalAIStateListener? _localAIListener; | ||
| final AIModelSwitchListener _aiModelSwitchListener; | ||
| LocalAIPB? _localAIState; | ||
| AvailableModelsPB? _availableModels; | ||
|
|
||
| // callbacks | ||
| final List<OnModelStateChangedCallback> _stateChangedCallbacks = []; | ||
| final List<OnAvailableModelsChangedCallback> | ||
| _availableModelsChangedCallbacks = []; | ||
|
|
||
| void _startListening() { | ||
| if (UniversalPlatform.isDesktop) { | ||
| _localAIListener?.start( | ||
| stateCallback: (state) async { | ||
| _localAIState = state; | ||
| _notifyStateChanged(); | ||
|
|
||
| if (state.state == RunningStatePB.Running || | ||
| state.state == RunningStatePB.Stopped) { | ||
| await _loadAvailableModels(); | ||
| _notifyAvailableModelsChanged(); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| _aiModelSwitchListener.start( | ||
| onUpdateSelectedModel: (model) async { | ||
| final updatedModels = _availableModels?.deepCopy() | ||
| ?..selectedModel = model; | ||
| _availableModels = updatedModels; | ||
| _notifyAvailableModelsChanged(); | ||
|
|
||
| if (model.isLocal && UniversalPlatform.isDesktop) { | ||
| await _loadLocalAiState(); | ||
| } | ||
| _notifyStateChanged(); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| void _init() async { | ||
| await Future.wait([_loadLocalAiState(), _loadAvailableModels()]); | ||
| _notifyStateChanged(); | ||
| _notifyAvailableModelsChanged(); | ||
| } | ||
|
|
||
| void addListener({ | ||
| OnModelStateChangedCallback? onStateChanged, | ||
| OnAvailableModelsChangedCallback? onAvailableModelsChanged, | ||
| }) { | ||
| if (onStateChanged != null) { | ||
| _stateChangedCallbacks.add(onStateChanged); | ||
| } | ||
| if (onAvailableModelsChanged != null) { | ||
| _availableModelsChangedCallbacks.add(onAvailableModelsChanged); | ||
| } | ||
| } | ||
|
|
||
| void removeListener({ | ||
| OnModelStateChangedCallback? onStateChanged, | ||
| OnAvailableModelsChangedCallback? onAvailableModelsChanged, | ||
| }) { | ||
| if (onStateChanged != null) { | ||
| _stateChangedCallbacks.remove(onStateChanged); | ||
| } | ||
| if (onAvailableModelsChanged != null) { | ||
| _availableModelsChangedCallbacks.remove(onAvailableModelsChanged); | ||
| } | ||
| } | ||
|
|
||
| Future<void> dispose() async { | ||
|
appflowy marked this conversation as resolved.
|
||
| _stateChangedCallbacks.clear(); | ||
| _availableModelsChangedCallbacks.clear(); | ||
| await _localAIListener?.stop(); | ||
| await _aiModelSwitchListener.stop(); | ||
| } | ||
|
|
||
| (AiType, String, bool) getState() { | ||
| if (UniversalPlatform.isMobile) { | ||
| return (AiType.cloud, LocaleKeys.chat_inputMessageHint.tr(), true); | ||
| } | ||
|
|
||
| final availableModels = _availableModels; | ||
| final localAiState = _localAIState; | ||
|
|
||
| if (availableModels == null) { | ||
| Log.warn("No available models"); | ||
| return (AiType.cloud, LocaleKeys.chat_inputMessageHint.tr(), true); | ||
| } | ||
| if (localAiState == null) { | ||
| Log.warn("Cannot get local AI state"); | ||
| return (AiType.cloud, LocaleKeys.chat_inputMessageHint.tr(), true); | ||
| } | ||
|
|
||
| if (!availableModels.selectedModel.isLocal) { | ||
| return (AiType.cloud, LocaleKeys.chat_inputMessageHint.tr(), true); | ||
| } | ||
|
|
||
| final editable = localAiState.state == RunningStatePB.Running; | ||
| final hintText = editable | ||
| ? LocaleKeys.chat_inputLocalAIMessageHint.tr() | ||
| : LocaleKeys.settings_aiPage_keys_localAIInitializing.tr(); | ||
|
|
||
| return (AiType.local, hintText, editable); | ||
| } | ||
|
|
||
| (List<AIModelPB>, AIModelPB?) getAvailableModels() { | ||
| final availableModels = _availableModels; | ||
| if (availableModels == null) { | ||
| return ([], null); | ||
| } | ||
| return (availableModels.models, availableModels.selectedModel); | ||
| } | ||
|
|
||
| void _notifyAvailableModelsChanged() { | ||
| final (models, selectedModel) = getAvailableModels(); | ||
| for (final callback in _availableModelsChangedCallbacks) { | ||
| callback(models, selectedModel); | ||
| } | ||
| } | ||
|
|
||
| void _notifyStateChanged() { | ||
| final (type, hintText, isEditable) = getState(); | ||
| for (final callback in _stateChangedCallbacks) { | ||
| callback(type, isEditable, hintText); | ||
| } | ||
| } | ||
|
|
||
| Future<void> _loadAvailableModels() { | ||
| final payload = AvailableModelsQueryPB(source: objectId); | ||
| return AIEventGetAvailableModels(payload).send().fold( | ||
| (models) => _availableModels = models, | ||
| (err) => Log.error("Failed to get available models: $err"), | ||
| ); | ||
| } | ||
|
|
||
| Future<void> _loadLocalAiState() { | ||
| return AIEventGetLocalAIState().send().fold( | ||
| (localAIState) => _localAIState = localAIState, | ||
| (error) => Log.error("Failed to get local AI state: $error"), | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.