Skip to content
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

add explain command and the buttons #125

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Context.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"caption": "Generate Shareable Link",
"command": "pieces_generate_shareable_link",
},
{
"caption": "Explain",
"command": "pieces_explain",
},
{
"id": "pieces-ask",
"caption": "Ask Copilot",
Expand Down
4 changes: 3 additions & 1 deletion assets/share_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def run_async(self):
self.view.set_status("pieces_share",f"Link Generated {link}")

# self.show_dialog(link)
self.create_popup(self.view,link)

# self.create_popup(self.view,link)
self.show_dialog(link)
sublime.set_timeout(lambda: self.view.erase_status("pieces_share"),4000)


Expand Down
5 changes: 3 additions & 2 deletions copilot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .ask_command import PiecesAskStreamCommand,PiecesEnterResponseCommand
from .ask_command import PiecesAskStreamCommand,PiecesEnterResponseCommand,PiecesInsertTextCommand
from .conversation_websocket import ConversationWS
from .conversations import ConversationsSnapshot
from .conversations import ConversationsSnapshot
from .explain import PiecesExplainCommand
9 changes: 7 additions & 2 deletions copilot/ask_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def run(self,pieces_choose_type,pieces_query=None,pieces_conversation_id=None):
copilot.ask_websocket.start()
copilot.render_conversation(pieces_conversation_id)
if pieces_query:
copilot.gpt_view.run_command("append",{"charaters":pieces_query})
copilot.gpt_view.run_command("pieces_enter_response")
copilot.add_query(pieces_query)
return

def input(self,args):
Expand Down Expand Up @@ -77,3 +76,9 @@ def list_items(self):
def placeholder(self):
return "Choose a conversation or start new one"

class PiecesInsertTextCommand(sublime_plugin.TextCommand):
def run(self,edit,text,point=None):
self.view.window().focus_view(self.view)
if not point:
point = self.view.sel()[0].begin()
self.view.insert(edit,point,text)
47 changes: 40 additions & 7 deletions copilot/ask_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
from sublime import Region
import re


PHANTOM_A_TAG_STYLE = "padding: 4px;background-color: var(--accent); border-radius: 6px;color: var(--foreground);text-decoration: None;text-align: center"

PHANTOM_CONTENT = f"""
<div style="padding-right:2px">
<a style="{PHANTOM_A_TAG_STYLE}" href ="save_{{id}}">{{save}}</a>
<a style="{PHANTOM_A_TAG_STYLE}" href="copy_{{id}}">{{copy}}</a>
<a style="{PHANTOM_A_TAG_STYLE}" href="share_{{id}}">{{share}}</a>
<a style="{PHANTOM_A_TAG_STYLE}" href="insert_{{id}}">{{insert}}</a>
</div>
"""

class CopilotViewManager:
can_type = True
_gpt_view = None

@property
def gpt_view(self) -> sublime.View:
Expand Down Expand Up @@ -161,7 +165,7 @@ def new_line(self,lines = 2) -> None:
for _ in range(lines):
self.gpt_view.run_command("append",{"characters":"\n"})

def ask(self,relevant=RelevantQGPTSeeds(iterable=[])):
def ask(self,relevant=RelevantQGPTSeeds(iterable=[]),pipeline=None):
query = self.gpt_view.substr(Region(self.end_response,self.gpt_view.size()))
if not query:
return
Expand All @@ -174,7 +178,8 @@ def ask(self,relevant=RelevantQGPTSeeds(iterable=[])):
query=query,
relevant = relevant,
application=PiecesSettings.get_application().id,
model = PiecesSettings.model_id
model = PiecesSettings.model_id,
pipeline=pipeline
),
conversation = self.conversation_id,

Expand Down Expand Up @@ -223,13 +228,20 @@ def on_nav(self,href:str):
sublime.set_clipboard(code)
self.update_phantom_set(region,id,copy="Copied")
sublime.set_timeout_async(lambda:self.update_phantom_set(region,id,copy="Copy"),5000)


def update_phantom_set(self,region,id,save="Save",copy="Copy",reset = False):
elif command == "share":
self.gpt_view.run_command("pieces_generate_shareable_link",{"data":code})
self.update_phantom_set(region,id,share="Sharing")
sublime.set_timeout_async(lambda:self.update_phantom_set(region,id),5000)
elif command == "insert":
s = self.secondary_view
if s:
s.run_command("pieces_insert_text",{"text":code})

def update_phantom_set(self,region,id,save="Save",copy="Copy",share="Share",insert="Insert",reset = False):
# Change the text
phantom = sublime.Phantom(
region,
PHANTOM_CONTENT.format(id = id,copy=copy,save=save),
PHANTOM_CONTENT.format(id = id,copy=copy,save=save,share=share,insert=insert),
sublime.LAYOUT_BELOW,
on_navigate=self.on_nav
)
Expand Down Expand Up @@ -281,4 +293,25 @@ def render_conversation(self,conversation_id):

self.show_cursor
self.end_response = self.gpt_view.size()
self.add_code_phantoms()
self.add_code_phantoms()

@property
def secondary_view(self):
return getattr(self,"_secondary_view",None) # Will be updated via event listeners
@secondary_view.setter
def secondary_view(self,s):
self._secendary_view = s if s != self._gpt_view else self.secondary_view

def clear(self):
self.end_response = 0
self.conversation_id = None
self.can_type = True
view = self._gpt_view
self._gpt_view = None
view.run_command("select_all")
view.run_command("delete")
self.gpt_view

def add_query(self,query):
self.gpt_view.run_command("append",{"characters":query})
self.gpt_view.run_command("pieces_enter_response")
26 changes: 26 additions & 0 deletions copilot/explain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sublime_plugin
import sublime
from .._pieces_lib.pieces_os_client import (QGPTTaskPipeline,
QGPTTaskPipelineForCodeExplanation,
RelevantQGPTSeed,
RelevantQGPTSeeds,
)
from .ask_command import copilot
from ..assets.create_asset import PiecesCreateAssetCommand

class PiecesExplainCommand(sublime_plugin.TextCommand):
def run(self,edit):
copilot.clear()
ext = self.view.file_name().split(".")[-1]
seed = PiecesCreateAssetCommand(self.view).get_seeds()
query = f"Can you explain this \n```{ext}\n{seed.asset.format.fragment.string.raw}\n```"
copilot.add_query(query)
copilot.ask(
relevant=RelevantQGPTSeeds(
iterable=[RelevantQGPTSeed(seed=seed)]
),
pipeline=QGPTTaskPipeline(
code_explanation=QGPTTaskPipelineForCodeExplanation()
)
)

12 changes: 7 additions & 5 deletions event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from .settings import PiecesSettings
from .misc import PiecesOnboardingCommand
from .copilot.ask_command import copilot

from .copilot.ask_view import CopilotViewManager
from .copilot.conversation_websocket import ConversationWS

class PiecesEventListener(sublime_plugin.EventListener):
secondary_view = None # Used in the ask to know the ssecondary view at insert
commands_to_exclude = ["pieces_onboarding","pieces_reload","pieces_support"]

onboarding_commands_dict = {
Expand Down Expand Up @@ -105,10 +107,10 @@ def on_init(self,views):
if conversation:
on_close = lambda x:copilot.render_conversation(conversation)
sublime.set_timeout(lambda: view.close(on_close),5000)# Wait some sec until the conversations is loaded

@staticmethod
def on_deactivated(view):
copilot.secondary_view = view


class PiecesViewEventListener(sublime_plugin.ViewEventListener):
Expand Down