resonate-sdk-ts icon indicating copy to clipboard operation
resonate-sdk-ts copied to clipboard

Tasks

Open avillega opened this issue 1 year ago • 5 comments

Both the SDK and the Resonate server are converging in what we now call tasks. There are two types of tasks that represent the events of the global event loop, Resume and Invoke.

Resume event signals a Resonate node that a promise the node was awaiting on has been completed and the node can resume work where it left off.

Invoke event signal a Resonate node to invoke a function in its local execution context and report back its results.

Considerations

  • It is necessary to have a generic mechanism to accept task in the SDK, task could come as an http request, messages in distributed queue system like SQS or be polled by the SDK itself from the server.
  • We should allow users of the resonate SDK to implement their own tasks sources in a way that is compatible with the rest of the Resonate architecture like the server communication.
  • We need a local tasks source to be able to use the Resonate SDK without a server running.

avillega avatar Aug 15 '24 21:08 avillega

I am very interested in using SDK-ts with a distributed network, I'd love to help you test things out here, I'm just not sure where to start. I would like to use the SDK in Bun, Nodejs, browsers and web workers. (I don't have a resonate server running )

jamesgibson14 avatar Aug 15 '24 21:08 jamesgibson14

Hey James, you can take a look at some of the examples, currently the sdk works both with Nodejs and Bun (Probably Deno too, he haven't tested out) but does not work in the Browser. You can definitely use the sdk without a server, said that, spinning out a server should be very simple, You can follow the instructions here https://docs.resonatehq.io/resonate/overview

More info about the SDK: https://docs.resonatehq.io/sdks/typescript

avillega avatar Aug 15 '24 21:08 avillega

Actually it work fine in Chrome at least, paste this in the console:

const { Resonate, Context } = await import('https://esm.run/@resonatehq/[email protected]')
async function downloadAndSummarize(context, url) {
  console.log('downloadAndSummarize: main function-')
    // Download the content from the provided URL
    let content = await context.run(download, url);

    // Summarize the downloaded content
    let summary = await context.run(summarize, content);

    // Return the summary of the content
    return summary;
}

async function download(context, url) {
  console.log('download: function-')

    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (Math.random() < 0.95) { // 10% chance to fail
              console.log('oops, download failed!')
                reject("download failed");
            } else {
                resolve("This is the text of the page");
            }
        }, 2500);
    })
}

async function summarize(context, text) {
    console.log('summarize: function-')

    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (Math.random() < 0.85) { // 10% chance to fail
                reject("summarize failed");
            } else {
                resolve("This is a summary of the text");
            }
        }, 2500);
    });
}
const resonate = new Resonate();
// resonate.start();
resonate.register("downloadAndSummarize", 
  downloadAndSummarize, 
  { timeout: 120000 }
);
resonate.start()
console.log('running runDownloadAndSummarize', this.resonate)

let summary = await resonate.run("downloadAndSummarize", /* id */ `summarize-abc`, /* param */ 'abc').catch(console.error);
console.log('summary', summary )

jamesgibson14 avatar Aug 15 '24 23:08 jamesgibson14

My idea is to implement a RemoteStore to pass to SDK that connects with websockets to a distributed network of. Maybe I'm way off base, but it seems like it has to do with this issue "Tasks"

jamesgibson14 avatar Aug 15 '24 23:08 jamesgibson14

Depending on what you want Resonate to do for you it might or might not have to do with this issue. You can currently implement your own store. how ever the Resonate server does not support web sockets. To talk between different Resonate nodes Tasks might be the solutions, we are still in design phase for it, but as of now, we will still go through the server, but probably in a more generic way than just http.

avillega avatar Aug 16 '24 05:08 avillega