Skip to content

Commit

Permalink
feat(demos) add JS client
Browse files Browse the repository at this point in the history
  • Loading branch information
saghul committed Sep 13, 2024
1 parent 6ccb949 commit 1d9f5bc
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions demos/js-client/skynet.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export class SkynetClient {
constructor(options = {}) {
this._baseUrl = options.baseUrl ?? 'http://localhost:8000';
}

async summary(text, options) {
return this._fetchAndPoll(`${this._baseUrl}/summaries/v1/summary`, text, options)
}

async actionItems(text, options) {
return this._fetchAndPoll(`${this._baseUrl}/summaries/v1/action-items`, text, options)
}

async _fetchAndPoll(url, text, options = {}) {
// Submit the job.
const r = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
hint: options?.hint ?? 'text',
text
})
});
const data = await r.json();
const jobId = data.id;

if (!jobId) {
throw new Error('Could not create job');
}

const d = createDeferred();

// Poll for it.
const int = setInterval(async () => {
try {
const r = await fetch(`${this._baseUrl}/summaries/v1/job/${jobId}`);
const data = await r.json();

if (data.status === 'success') {
clearInterval(int);
d.resolve(data.result);
} else if (data.status === 'error') {
clearInterval(int);
d.reject(new Error(data.result));
}
} catch(_) {}
}, 5 * 1000);

return d.promise;
}
}


function createDeferred() {
if (Promise.withResolvers) {
return Promise.withResolvers();
}

const d = {};

d.promise = new Promise((resolve, reject) => {
d.resolve = resolve;
d.reject = reject;
})

return d;
}

0 comments on commit 1d9f5bc

Please sign in to comment.