Spaces:
Running
Running
Create instinct-central-client.mjs
Browse files- instinct-central-client.mjs +12 -0
instinct-central-client.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Unwired browser client. Never auto-installs over the manual UI.
|
| 2 |
+
// Embed the reviewed Worker URL and public Turnstile site key only after deployment.
|
| 3 |
+
export class CentralBridge {
|
| 4 |
+
constructor(url,{storage=localStorage,fetchImpl=fetch}={}){const u=new URL(url);if(u.protocol!=='https:')throw Error('HTTPS required');this.url=u.origin;this.storage=storage;this.fetch=(...args)=>fetchImpl(...args);this.key='adaptapro-central-capability:'+u.origin;}
|
| 5 |
+
async call(path,method='GET',value){const cap=this.storage.getItem(this.key);const r=await this.fetch(this.url+path,{method,headers:{'Content-Type':'application/json',...(cap?{Authorization:'Bearer '+cap}:{})},body:value===undefined?undefined:JSON.stringify(value)});const x=await r.json();if(!r.ok)throw Error(x.error||'Bridge failed');return x;}
|
| 6 |
+
async start({consent=false,turnstileToken}){if(!consent)throw Error('Explain temporary message storage and mailbox copies before consent');const x=await this.call('/sessions','POST',{consent,turnstileToken});this.storage.setItem(this.key,x.capability);return {session:x.session,expires_at:x.expires_at};}
|
| 7 |
+
async submit({agent,question,context,reviewed=false,consent=false}){return this.call('/jobs','POST',{agent,question,context,reviewed,consent});}
|
| 8 |
+
async check(){return this.call('/jobs');}
|
| 9 |
+
async run(id){return this.call('/run','POST',{id,reviewed:true});}
|
| 10 |
+
async collect(id){return this.call('/check','POST',{id});}
|
| 11 |
+
async clear(){const r=await this.call('/session','DELETE');this.storage.removeItem(this.key);return r;}
|
| 12 |
+
}
|