-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
328 additions
and
360 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
line-length = 100 | ||
|
||
[format] | ||
quote-style = "single" |
This file contains 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 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 |
---|---|---|
@@ -1,20 +1,25 @@ | ||
import sys | ||
|
||
# clear modules cache if package is reloaded (after update?) | ||
prefix = __package__ + ".plugins" # don't clear the base package | ||
for module_name in [ | ||
module_name | ||
for module_name in sys.modules | ||
if module_name.startswith(prefix) | ||
]: | ||
prefix = __package__ + '.plugins' # type: ignore # don't clear the base package | ||
for module_name in [module_name for module_name in sys.modules if module_name.startswith(prefix)]: | ||
del sys.modules[module_name] | ||
del prefix | ||
|
||
from .plugins.openai import Openai | ||
from .plugins.active_view_event import ActiveViewEventListener | ||
from .plugins.openai_panel import OpenaiPanelCommand | ||
from .plugins.stop_worker_execution import StopOpenaiExecutionCommand | ||
from .plugins.worker_running_context import OpenaiWorkerRunningContext | ||
from .plugins.settings_reloader import ReloadSettingsListener | ||
from .plugins.output_panel import SharedOutputPanelListener, AIChatViewEventListener | ||
from .plugins.buffer import TextStreamAtCommand, ReplaceRegionCommand, EraseRegionCommand | ||
from .plugins.active_view_event import ActiveViewEventListener # noqa: E402, F401 | ||
from .plugins.ai_chat_event import AIChatViewEventListener # noqa: E402, F401 | ||
from .plugins.buffer import ( # noqa: E402, F401 | ||
EraseRegionCommand, | ||
ReplaceRegionCommand, | ||
TextStreamAtCommand, | ||
) | ||
from .plugins.openai import Openai # noqa: E402, F401 | ||
from .plugins.openai_panel import OpenaiPanelCommand # noqa: E402, F401 | ||
from .plugins.output_panel import SharedOutputPanelListener # noqa: E402, F401 | ||
from .plugins.settings_reloader import ReloadSettingsListener # noqa: E402, F401 | ||
from .plugins.stop_worker_execution import ( # noqa: E402 | ||
StopOpenaiExecutionCommand, # noqa: F401 | ||
) | ||
from .plugins.worker_running_context import ( # noqa: E402, | ||
OpenaiWorkerRunningContext, # noqa: F401 | ||
) |
This file contains 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 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,44 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Dict | ||
|
||
from sublime import Window | ||
from sublime_plugin import ViewEventListener | ||
|
||
from .cacher import Cacher | ||
|
||
|
||
class AIChatViewEventListener(ViewEventListener): | ||
@classmethod | ||
def is_applicable(cls, settings) -> bool: | ||
return ( | ||
settings.get('syntax') == 'Packages/Markdown/MultiMarkdown.sublime-syntax' | ||
or settings.get('syntax') == 'Packages/Markdown/PlainText.sublime-syntax' | ||
) | ||
|
||
def on_activated(self) -> None: | ||
self.update_status_message(self.view.window()) # type: ignore | ||
|
||
def update_status_message(self, window: Window) -> None: | ||
project_settings: Dict[str, str] | None = window.active_view().settings().get('ai_assistant') # type: ignore | ||
|
||
cache_prefix = project_settings.get('cache_prefix') if project_settings else None | ||
|
||
cacher = Cacher(name=cache_prefix) | ||
if self.is_ai_chat_tab_active(window): | ||
status_message = self.get_status_message(cacher=cacher) | ||
active_view = window.active_view() | ||
if active_view and active_view.name() == 'AI Chat': | ||
active_view.set_status('ai_chat_status', status_message) | ||
|
||
def is_ai_chat_tab_active(self, window: Window) -> bool: | ||
active_view = window.active_view() | ||
return active_view.name() == 'AI Chat' if active_view else False | ||
|
||
def get_status_message(self, cacher: Cacher) -> str: | ||
tokens = cacher.read_tokens_count() | ||
prompt = tokens['prompt_tokens'] if tokens else 0 | ||
completion = tokens['completion_tokens'] if tokens else 0 | ||
total = prompt + completion | ||
|
||
return f'[⬆️: {prompt:,} + ⬇️: {completion:,} = {total:,}]' |
This file contains 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 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 |
---|---|---|
@@ -1,29 +1,34 @@ | ||
from sublime import Edit, Region, View | ||
from sublime_plugin import TextCommand | ||
|
||
class TextStreamer(): | ||
|
||
class TextStreamer: | ||
def __init__(self, view: View) -> None: | ||
self.view = view | ||
|
||
def update_completion(self, completion: str): | ||
## Till this line selection has to be cleared and the carret should be placed in to a desired starting point. | ||
## So begin() and end() sould be the very same carret offset. | ||
start_of_selection = self.view.sel()[0].begin() ## begin() because if we point an end there — it'll start to reverse prompting. | ||
self.view.run_command("text_stream_at", {"position": start_of_selection, "text": completion}) | ||
## begin() because if we point an end there — it'll start to reverse prompting. | ||
start_of_selection = self.view.sel()[0].begin() | ||
self.view.run_command('text_stream_at', {'position': start_of_selection, 'text': completion}) | ||
return | ||
|
||
def delete_selected_region(self, region: Region): | ||
json_reg = {'a': region.begin(), 'b': region.end()} | ||
self.view.run_command("erase_region", {"region": json_reg}) | ||
self.view.run_command('erase_region', {'region': json_reg}) | ||
|
||
|
||
class TextStreamAtCommand(TextCommand): | ||
def run(self, edit: Edit, position: int, text: str): | ||
def run(self, edit: Edit, position: int, text: str): # type: ignore | ||
_ = self.view.insert(edit=edit, pt=position, text=text) | ||
|
||
|
||
class ReplaceRegionCommand(TextCommand): | ||
def run(self, edit: Edit, region, text: str): | ||
def run(self, edit: Edit, region, text: str): # type: ignore | ||
self.view.replace(edit=edit, region=Region(region['a'], region['b']), text=text) | ||
|
||
|
||
class EraseRegionCommand(TextCommand): | ||
def run(self, edit: Edit, region): | ||
def run(self, edit: Edit, region): # type: ignore | ||
self.view.erase(edit=edit, region=Region(region['a'], region['b'])) |
This file contains 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 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 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
Oops, something went wrong.