dev: add threads module for puter.js

This commit is contained in:
KernelDeimos
2025-03-04 13:00:01 -05:00
parent 01ba6168d9
commit 1f05df68eb
2 changed files with 49 additions and 0 deletions
+2
View File
@@ -25,6 +25,7 @@ import { PWispHandler } from './modules/networking/PWispHandler.js';
import { make_http_api } from './lib/http.js';
import Exec from './modules/Exec.js';
import Convert from './modules/Convert.js';
import Threads from './modules/Threads.js';
// TODO: This is for a safe-guard below; we should check if we can
// generalize this behavior rather than hard-coding it.
@@ -98,6 +99,7 @@ window.puter = (function() {
this.registerModule('apps', Apps);
this.registerModule('ai', AI);
this.registerModule('kv', KV);
this.registerModule('threads', Threads);
this.registerModule('drivers', Drivers);
this.registerModule('debug', Debug);
this.registerModule('exec', Exec);
+47
View File
@@ -0,0 +1,47 @@
export default class Threads {
setAuthToken (authToken) {
this.authToken = authToken;
}
setAPIOrigin (APIOrigin) {
this.APIOrigin = APIOrigin;
}
async req_ (method, route, body) {
const resp = await fetch(
this.APIOrigin + route, {
method,
headers: {
Authorization: `Bearer ${this.authToken}`,
...(body ? { 'Content-Type': 'application/json' } : {}),
},
...(body ? { body: JSON.stringify(body) } : {}),
}
);
return await resp.json();
}
async create (spec, parent) {
if ( typeof spec === 'string' ) spec = { text: spec };
await this.req_('POST', '/threads/create', {
...spec,
...(parent ? { parent } : {}),
});
}
async edit (uid, spec = {}) {
if ( typeof spec === 'string' ) spec = { text: spec };
await this.req_('PUT', '/threads/edit/' + encodeURIComponent(uid), {
...spec,
});
}
async delete (uid) {
await this.req_('DELETE', '/threads/' + encodeURIComponent(uid));
}
async list (uid, page, options) {
await this.req_('POST',
'/threads/list/' + encodeURIComponent(uid) + '/' + page,
options ?? {},
);
}
}