alexanderatallah / window.ai

Use your own AI models on the web
https://windowai.io
MIT License
782 stars 68 forks source link

Access window.ai from inside another extension #47

Open JeroenvdV opened 1 year ago

JeroenvdV commented 1 year ago

I'm working on a browser extension myself that could I would like to use window.ai as a backend. Unlike in a page, my injected js code currently can't access window.ai (in Chrome), because it remains undefined. Is there a way to access the API from an extension or what would be needed to enable that?

alexanderatallah commented 1 year ago

Are you injecting your script before the document load event? What happens if you wait for load and then try? Also, what happens if you use “npm i window.ai” and then await getWindowAI ?

louisgv commented 1 year ago

You will need to inject your CS inside the mainworld to be able to consume this API as-is. The default world content script (CS) lives in does not share the same window context as the main world (where window.ai exposes the API).

One way we can do is to add a secondary interface via externallyConnectable, so that other extension can call window.ai directly via extension messaging instead of having to pipe message from main -> their extension. This will also allow other extension to not be rely on the main world (which is only exposed when user visit a site).

Another way is to have window.ai be a central model config store (like a password manager). Since extension communication are not real time (or at least I don't recall there was an API for that :d...). Then, other extension can ask for the auth data from window.ai, and use a window.ai's client package that implement the API interface in their own extension, invoking the model however they like.

alexanderatallah commented 1 year ago

extension communication are not real time (or at least I don't recall there was an API for that :d...) @louisgv what do you mean by this?

@JeroenvdV It sounds like making the extension externally connectable is the best path so far to making this easy, but in the meantime you can make a main world script. (e.g. With Plasmo:

export const config: PlasmoCSConfig = {
  matches: ["<all_urls>"],
  world: "MAIN",
  all_frames: true
}

Will close this for now but feel free to reopen if this doesn't work.

louisgv commented 1 year ago

@alexanderatallah by realtime I mean like the port API - I shoulda say -"long-lived" comms :d

alexanderatallah commented 1 year ago

Reopening this to mark a task for improving support for connecting from other extensions

YanniKouloumbis commented 1 year ago

I will take a look at this tomorrow! I would love to see to some sort of Plasmo template that makes it easy to get started with window.ai in a chrome extension!