-
Notifications
You must be signed in to change notification settings - Fork 39
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
Implement initial version of web handlers #519
Conversation
Prompt: #519 <-- seems like wrong url? |
src/Chat/ChatBase.tsx
Outdated
@@ -156,6 +157,26 @@ function ChatBase({ chat }: ChatBaseProps) { | |||
prompt = "/help"; | |||
} | |||
|
|||
// If we have a web handler registered for this url | |||
if (prompt && WebHandler.getMatchingHandler(prompt)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does prompt
here mean current chat message
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think promptText
is a better name. But it's already in use later in the same function
src/Chat/ChatBase.tsx
Outdated
@@ -156,6 +157,26 @@ function ChatBase({ chat }: ChatBaseProps) { | |||
prompt = "/help"; | |||
} | |||
|
|||
// If we have a web handler registered for this url | |||
if (prompt && WebHandler.getMatchingHandler(prompt)) { | |||
const handler = WebHandler.getMatchingHandler(prompt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should do const handler above if and then reuse it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, just pushed in latest commit
src/lib/WebHandler.ts
Outdated
new WebHandler({ | ||
handlerUrl: "https://taras-scrape2md.web.val.run/", | ||
method: HttpMethod.GET, | ||
matchPattern: /^https:\/\/\S+/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should fix match pattern to have $ at end to match entire message only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in both
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for noticing, I've made the change.
Not critical, but I'd vote to get rid of
|
Deploying chatcraft-org with Cloudflare Pages
|
@humphd Agreed, I've removed the |
src/Chat/ChatBase.tsx
Outdated
|
||
if (prompt && handler) { | ||
try { | ||
const result = await handler!.executeHandler(prompt); // We know handler is always there |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would just do const executedHandlerResult = WebHandler.executeHandler(prompt ?? "");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/prompt/message/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eg should just execute first matching handler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think you also wanna set some sort of network spinner given that you doing network io
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see when i run it, there is a spinner..don't understand why it works :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would just do const executedHandlerResult = WebHandler.executeHandler(prompt ?? "");
That will work, but I would still have to have a function to know if the prompt text matches a web handler.
src/lib/WebHandler.ts
Outdated
this.matchPattern = matchPattern; | ||
} | ||
|
||
isMatchingHandler(url: string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/url/message/ almost everywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I didn't get that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohhh, you mean I should rename url
to message
Didn't realize you were talking in sed
command 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just pushed 9cf7f2d
src/lib/WebHandler.ts
Outdated
`${command}\n\n` + | ||
// If it's already markdown, dump it into the message as is; | ||
// otherwise, wrap it in a code block with appropriate type | ||
(type === "markdown" ? content : `\`\`\`${type}\n${content}` + `\n\`\`\``); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think text/plain is also ok to show directly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I noticed it doesn't make any difference if I skip this step. Will make the change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tarasglek Just pushed the changes
src/lib/WebHandler.ts
Outdated
} | ||
|
||
async executeHandler(url: string): Promise<string> { | ||
url = extractFirstUrl(url) ?? ""; // When the input is not a url itself |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't understand what this code is doing. needs a comment re algo here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was for handling non-url prompts. But now that we have decided to not allow those, we can remove this
src/lib/WebHandler.ts
Outdated
new WebHandler({ | ||
handlerUrl: "https://taras-scrape2md.web.val.run/", | ||
method: HttpMethod.GET, | ||
matchPattern: /^https:\/\/\S+$/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think you also need to make this pattern do /^(https://\S+)$/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so the algo is you apply regexp..then if it has a match group, eg ()..pass that to url. if it doesn't just pass whole message(tho that should only work for post i think)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can then get fancy and if regexp uses named captch groups ala https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Named_capturing_group then you can pass them as matchName=matchValue form
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think you also need to make this pattern do /^(https://\S+)$/
Do you mean wrapping in parenthesis?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so the algo is you apply regexp..then if it has a match group, eg ()..pass that to url. if it doesn't just pass whole message(tho that should only work for post i think)
Yes, I go through each registered Web Handler, and try to match the prompt against match pattern of the handler. If one matches, I invoke that handler's configured handlerUrl
which takes the prompt text as url
query param for now.
Maybe we can make how the prompt is sent with request configurable too in the future
I would love it if you could expose this as a webhandler section in prefs(maybe hidden by default under advanced), so i could enter yaml directly |
Until it's editable there isn't too much value in specifying handlers this way |
if i put
into a message. then it's handled as a call to llm. if i do edit, cut it down to
and press re-ask it's also passed to llm. Should be webhandled |
@tarasglek Nice catch, we should fix it. Can we do it in a follow up? |
e11439f
to
197ca00
Compare
We can delay re-ask to followup, but webhandlers(agree this is a cool name) should land with config editing. |
@tarasglek @humphd Just pushed some new changes. The user can now enter YAML configurations for Web Handlers (as Taras suggested) with a modal that a pops up by clicking the new Web Handlers menu item in our profile image drop menu. This config is persisted in localStorage with the help of a Now, we can configure as many patterns we want to and the first match is executed. Based on my config, if I enter a Youtube Url the request is redirected to the api of one of my projects (I have intentionally set CORS headers to *). |
Can you change the font in that textarea to be monospaced? Another idea (follow-up) would be to use the code I already have for the CodeMirror editor that we use in functions, so we get a better dev experience. |
@humphd Agreed, I should have used that in the first place. Just filed a follow up. Also, I've changed the I think we should extend this functionality by allowing to configure more options in follow-ups. For example, how the message should be sent to the handler url - Please let me know if you have any more suggestions. |
532e5b4
to
d4514ca
Compare
tried this out, mostly worked great! Small issue:
|
@tarasglek Thanks for reviewing! I am trying to avoid having it in the Also, we were disusing the weird Markdown today in the meeting. Not sure, but we're probably adding redundant backticks somewhere. I just filed #547 for that |
This is my initial attempt in implementing the idea of what we can call web handlers as discussed in #507 .
I have:
WebHandler
class registering/searching web handlers and executing their logic i.e. sending request tohandler url
based on request config (so far request method)localStorage
.Demo:
Prompt:
https://github.com/tarasglek/chatcraft.org/pull/519
What I don't like is how we are currently allowing prompt like
import <url>
to be registered as match patterns. This causes ambiguity and I had to write specialized logic to handle edge cases likeas an example.
I feel like we should restrict web handler match patterns to just urls, and let import command serve its own purpose.
Maybe I am wrong and don't understand fully where we are going with this.
@humphd @tarasglek Please let me know what you think.