@qwanyx/stack 0.2.98 → 0.2.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -132,6 +132,7 @@ export declare class AuthService {
132
132
  firstName?: string;
133
133
  lastName?: string;
134
134
  phone?: string;
135
+ tenantDomain?: string;
135
136
  }): Promise<RegisterResult>;
136
137
  /**
137
138
  * Verify email with code received by email
@@ -152,7 +153,9 @@ export declare class AuthService {
152
153
  * Uses system coprocessor for authentication
153
154
  * Auto-adds user as member of configured system on first login
154
155
  */
155
- login(email: string, password: string): Promise<LoginResult>;
156
+ login(email: string, password: string, options?: {
157
+ tenantDomain?: string;
158
+ }): Promise<LoginResult>;
156
159
  /**
157
160
  * Resend verification code to email
158
161
  * (Uses same endpoint as password reset)
package/dist/index.cjs.js CHANGED
@@ -25,7 +25,7 @@ ${s}
25
25
  `&&(e=" "),(e.charCodeAt(0)>=33||[" "," "].includes(e))&&(this.node.value+=e),this.escaped=!1}}const Zd=50;function Dn(r,e){e=e||{};let t=e._depth||0;if(t>Zd)return[];let s=new Xd(r).tokenize(),i=[],o=[],a=[];if(s.forEach(l=>{l.type==="operator"&&(l.value===","||l.value===";")?(o.length&&i.push(o),o=[]):o.push(l)}),o.length&&i.push(o),i.forEach(l=>{l=Jd(l,t),l.length&&(a=a.concat(l))}),e.flatten){let l=[],u=c=>{c.forEach(d=>{if(d.group)return u(d.group);l.push(d)})};return u(a),l}return a}function ef(r){for(var e="",t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",n=new Uint8Array(r),s=n.byteLength,i=s%3,o=s-i,a,l,u,c,d,f=0;f<o;f=f+3)d=n[f]<<16|n[f+1]<<8|n[f+2],a=(d&16515072)>>18,l=(d&258048)>>12,u=(d&4032)>>6,c=d&63,e+=t[a]+t[l]+t[u]+t[c];return i==1?(d=n[o],a=(d&252)>>2,l=(d&3)<<4,e+=t[a]+t[l]+"=="):i==2&&(d=n[o]<<8|n[o+1],a=(d&64512)>>10,l=(d&1008)>>4,u=(d&15)<<2,e+=t[a]+t[l]+t[u]+"="),e}const tf=256,rf=2*1024*1024;class Ds{static parse(e,t){return new Ds(t).parse(e)}constructor(e){this.options=e||{},this.mimeOptions={maxNestingDepth:this.options.maxNestingDepth||tf,maxHeadersSize:this.options.maxHeadersSize||rf},this.root=this.currentNode=new va({postalMime:this,...this.mimeOptions}),this.boundaries=[],this.textContent={},this.attachments=[],this.attachmentEncoding=(this.options.attachmentEncoding||"").toString().replace(/[-_\s]/g,"").trim().toLowerCase()||"arraybuffer",this.started=!1}async finalize(){await this.root.finalize()}async processLine(e,t){let n=this.boundaries;if(n.length&&e.length>2&&e[0]===45&&e[1]===45)for(let s=n.length-1;s>=0;s--){let i=n[s];if(e.length!==i.value.length+2&&e.length!==i.value.length+4)continue;let o=e.length===i.value.length+4;if(o&&(e[e.length-2]!==45||e[e.length-1]!==45))continue;let a=!0;for(let l=0;l<i.value.length;l++)if(e[l+2]!==i.value[l]){a=!1;break}if(a)return o?(await i.node.finalize(),this.currentNode=i.node.parentNode||this.root):(await i.node.finalizeChildNodes(),this.currentNode=new va({postalMime:this,parentNode:i.node,...this.mimeOptions})),t?this.finalize():void 0}if(this.currentNode.feed(e),t)return this.finalize()}readLine(){let e=this.readPos,t=this.readPos,n=()=>({bytes:new Uint8Array(this.buf,e,t-e),done:this.readPos>=this.av.length});for(;this.readPos<this.av.length;){const s=this.av[this.readPos++];if(s!==13&&s!==10&&(t=this.readPos),s===10)return n()}return n()}async processNodeTree(){let e={},t=new Set,n=this.textMap=new Map,s=this.forceRfc822Attachments(),i=async(o,a,l)=>{if(a=a||!1,l=l||!1,o.contentType.multipart)o.contentType.multipart==="alternative"?a=o:o.contentType.multipart==="related"&&(l=o);else if(this.isInlineMessageRfc822(o)&&!s){const u=new Ds;o.subMessage=await u.parse(o.content),n.has(o)||n.set(o,{});let c=n.get(o);(o.subMessage.text||!o.subMessage.html)&&(c.plain=c.plain||[],c.plain.push({type:"subMessage",value:o.subMessage}),t.add("plain")),o.subMessage.html&&(c.html=c.html||[],c.html.push({type:"subMessage",value:o.subMessage}),t.add("html")),u.textMap&&u.textMap.forEach((d,f)=>{n.set(f,d)});for(let d of o.subMessage.attachments||[])this.attachments.push(d)}else if(this.isInlineTextNode(o)){let u=o.contentType.parsed.value.substr(o.contentType.parsed.value.indexOf("/")+1),c=a||o;n.has(c)||n.set(c,{});let d=n.get(c);d[u]=d[u]||[],d[u].push({type:"text",value:o.getTextContent()}),t.add(u)}else if(o.content){const u=o.contentDisposition.parsed.params.filename||o.contentType.parsed.params.name||null,c={filename:u?En(u):null,mimeType:o.contentType.parsed.value,disposition:o.contentDisposition.parsed.value||null};switch(l&&o.contentId&&(c.related=!0),o.contentDescription&&(c.description=o.contentDescription),o.contentId&&(c.contentId=o.contentId),o.contentType.parsed.value){case"text/calendar":case"application/ics":{o.contentType.parsed.params.method&&(c.method=o.contentType.parsed.params.method.toString().toUpperCase().trim());const d=o.getTextContent().replace(/\r?\n/g,`
26
26
  `).replace(/\n*$/,`
27
27
  `);c.content=Xr.encode(d);break}default:c.content=o.content}this.attachments.push(c)}for(let u of o.childNodes)await i(u,a,l)};await i(this.root,!1,[]),n.forEach(o=>{t.forEach(a=>{if(e[a]||(e[a]=[]),o[a])o[a].forEach(l=>{switch(l.type){case"text":e[a].push(l.value);break;case"subMessage":switch(a){case"html":e[a].push(Ca(l.value));break;case"plain":e[a].push(wa(l.value));break}break}});else{let l;switch(a){case"html":l="plain";break;case"plain":l="html";break}(o[l]||[]).forEach(u=>{switch(u.type){case"text":switch(a){case"html":e[a].push(Kd(u.value));break;case"plain":e[a].push(Yd(u.value));break}break;case"subMessage":switch(a){case"html":e[a].push(Ca(u.value));break;case"plain":e[a].push(wa(u.value));break}break}})}})}),Object.keys(e).forEach(o=>{e[o]=e[o].join(`
28
- `)}),this.textContent=e}isInlineTextNode(e){if(e.contentDisposition.parsed.value==="attachment")return!1;switch(e.contentType.parsed.value){case"text/html":case"text/plain":return!0;case"text/calendar":case"text/csv":default:return!1}}isInlineMessageRfc822(e){return e.contentType.parsed.value!=="message/rfc822"?!1:(e.contentDisposition.parsed.value||(this.options.rfc822Attachments?"attachment":"inline"))==="inline"}forceRfc822Attachments(){if(this.options.forceRfc822Attachments)return!0;let e=!1,t=n=>{n.contentType.multipart||["message/delivery-status","message/feedback-report"].includes(n.contentType.parsed.value)&&(e=!0);for(let s of n.childNodes)t(s)};return t(this.root),e}async resolveStream(e){let t=0,n=[];const s=e.getReader();for(;;){const{done:a,value:l}=await s.read();if(a)break;n.push(l),t+=l.length}const i=new Uint8Array(t);let o=0;for(let a of n)i.set(a,o),o+=a.length;return i}async parse(e){var s,i;if(this.started)throw new Error("Can not reuse parser, create a new PostalMime object");for(this.started=!0,e&&typeof e.getReader=="function"&&(e=await this.resolveStream(e)),e=e||new ArrayBuffer(0),typeof e=="string"&&(e=Xr.encode(e)),(e instanceof Blob||Object.prototype.toString.call(e)==="[object Blob]")&&(e=await ni(e)),e.buffer instanceof ArrayBuffer&&(e=new Uint8Array(e).buffer),this.buf=e,this.av=new Uint8Array(e),this.readPos=0;this.readPos<this.av.length;){const o=this.readLine();await this.processLine(o.bytes,o.done)}await this.processNodeTree();const t={headers:this.root.headers.map(o=>({key:o.key,value:o.value})).reverse()};for(const o of["from","sender"]){const a=this.root.headers.find(l=>l.key===o);if(a&&a.value){const l=Dn(a.value);l&&l.length&&(t[o]=l[0])}}for(const o of["delivered-to","return-path"]){const a=this.root.headers.find(l=>l.key===o);if(a&&a.value){const l=Dn(a.value);if(l&&l.length&&l[0].address){const u=o.replace(/\-(.)/g,(c,d)=>d.toUpperCase());t[u]=l[0].address}}}for(const o of["to","cc","bcc","reply-to"]){const a=this.root.headers.filter(u=>u.key===o);let l=[];if(a.filter(u=>u&&u.value).map(u=>Dn(u.value)).forEach(u=>l=l.concat(u||[])),l&&l.length){const u=o.replace(/\-(.)/g,(c,d)=>d.toUpperCase());t[u]=l}}for(const o of["subject","message-id","in-reply-to","references"]){const a=this.root.headers.find(l=>l.key===o);if(a&&a.value){const l=o.replace(/\-(.)/g,(u,c)=>c.toUpperCase());t[l]=En(a.value)}}let n=this.root.headers.find(o=>o.key==="date");if(n){let o=new Date(n.value);!o||o.toString()==="Invalid Date"?o=n.value:o=o.toISOString(),t.date=o}switch((s=this.textContent)!=null&&s.html&&(t.html=this.textContent.html),(i=this.textContent)!=null&&i.plain&&(t.text=this.textContent.plain),t.attachments=this.attachments,this.attachmentEncoding){case"arraybuffer":break;case"base64":for(let a of t.attachments||[])a!=null&&a.content&&(a.content=ef(a.content),a.encoding="base64");break;case"utf8":let o=new TextDecoder("utf8");for(let a of t.attachments||[])a!=null&&a.content&&(a.content=o.decode(a.content),a.encoding="utf8");break;default:throw new Error("Unknwon attachment encoding")}return t}}class iu{constructor(e){vt(this,"config");if(!e.system_id)throw new Error("MailClient: system_id is REQUIRED");this.config=e}async callCoprocessor(e,t,n={}){const s={coprocessor:e,method:t,system_id:this.config.system_id,params:{user_id:this.config.system_id,...n}};try{const i=await fetch(`${this.config.baseUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.getToken()}`},body:JSON.stringify(s)});if(!i.ok){const a=await i.text();throw new Error(`API error (${i.status}): ${a}`)}const o=await i.json();if(!o.success&&o.error)throw new Error(o.error);return o.result}catch(i){throw console.error("API call failed:",i),i}}async callMail(e,t={}){return this.callCoprocessor("mail",e,t)}async callEmail(e,t={}){return this.callCoprocessor("email",e,t)}getToken(){return typeof window<"u"&&window.localStorage&&localStorage.getItem("qwanyx_token")||""}async getSettings(){try{return await this.callMail("get_email_settings")||{accounts:[]}}catch{return{accounts:[]}}}async saveSettings(e){await this.callMail("save_email_settings",{accounts:e})}async listEmails(e,t=150,n){return this.callMail("list_emails",{account_id:e,limit:t,folder:n})}async deleteEmails(e,t,n=!0){return this.callMail("delete_emails",{account_id:e,uids:t,expunge:n})}async imapExec(e,t){return this.callMail("imap_exec",{account_id:e,commands:t})}async trashEmails(e,t,n="INBOX"){const s=["[Gmail]/Trash","[Gmail]/Corbeille","[Gmail]/Bin","Trash"];for(const i of s){const o=[`SELECT "${n}"`,...t.map(u=>`UID MOVE ${u} "${i}"`)],l=(await this.imapExec(e,o)).responses.filter(u=>u.ok&&u.command==="UID MOVE").length;if(l>0)return{success:!0,moved:l}}return{success:!1,moved:0}}async archiveEmails(e,t,n="INBOX"){const s=[`SELECT "${n}"`,...t.map(a=>`UID STORE ${a} +FLAGS (\\Deleted)`),"EXPUNGE"],o=(await this.imapExec(e,s)).responses.filter(a=>a.ok&&a.command==="UID STORE").length;return{success:o>0,archived:o}}async listFolders(e){const n=(await this.imapExec(e,['LIST "" *'])).responses.find(s=>s.command==="LIST");return n!=null&&n.ok&&n.folders?{success:!0,folders:n.folders}:{success:!1,folders:[]}}async listEmailsSorted(e,t="INBOX",n=150){const s=await this.imapExec(e,[`SELECT ${t}`,"FETCH 1:* (UID INTERNALDATE)"]),i=s.responses.find(p=>p.command==="SELECT");if(!(i!=null&&i.ok))throw new Error(`Failed to select folder: ${(i==null?void 0:i.error)||"Unknown error"}`);const o=s.responses.find(p=>p.command==="FETCH");if(!(o!=null&&o.ok)||!o.messages)return{account:{id:e,label:"",email:""},mailbox:{name:t,total:i.exists||0},messages:[]};const l=[...o.messages].sort((p,g)=>{const m=p.internalDate?new Date(p.internalDate).getTime():0;return(g.internalDate?new Date(g.internalDate).getTime():0)-m}).slice(0,n).map(p=>p.uid).filter(Boolean);if(l.length===0)return{account:{id:e,label:"",email:""},mailbox:{name:t,total:i.exists||0},messages:[]};const c=(await this.imapExec(e,[`SELECT ${t}`,`UID FETCH ${l.join(",")} (UID FLAGS ENVELOPE)`])).responses.find(p=>p.command==="UID FETCH");if(!(c!=null&&c.ok)||!c.messages)throw new Error(`Failed to fetch details: ${(c==null?void 0:c.error)||"Unknown error"}`);const d=new Map;for(const p of c.messages)p.uid&&d.set(p.uid,p);const f=l.map(p=>{var _,S,D,k,w;const g=d.get(p);if(!g)return null;const m=((_=g.flags)==null?void 0:_.some(E=>E.includes("Seen")))||!1,x=((D=(S=g.envelope)==null?void 0:S.from)==null?void 0:D.email)||"Unknown",v=((k=g.envelope)==null?void 0:k.subject)||"(No subject)",b=((w=g.envelope)==null?void 0:w.date)||"";return{uid:p,from:x,subject:v,date:b,seen:m}}).filter(Boolean);return{account:{id:e,label:"",email:""},mailbox:{name:t,total:i.exists||0},messages:f}}async getEmail(e,t,n="INBOX"){return this.callMail("get_email",{account_id:e,uid:t,folder:n})}async getAttachment(e,t,n,s="INBOX"){return this.callMail("get_attachment",{account_id:e,uid:t,attachment_index:n,folder:s})}async searchContactEmails(e,t,n=50){return this.callMail("search_contact_emails",{account_id:e,contact_email:t,limit:n})}async getEmailParsed(e,t,n="INBOX"){var g,m,x,v,b,_;const i=(await this.imapExec(e,[`SELECT "${n}"`,`UID FETCH ${t} (FLAGS BODY[])`])).responses.find(S=>S.command==="UID FETCH");if(!(i!=null&&i.ok)||!((g=i.messages)!=null&&g[0]))return console.error("Failed to fetch raw email:",i==null?void 0:i.error),null;const o=i.messages[0],a=o.body||o.raw;if(!a)return console.error("No raw email content in response"),null;const u=await new Ds().parse(a);console.log("[MailClient] Parsed email - attachments count:",((m=u.attachments)==null?void 0:m.length)||0),console.log("[MailClient] Parsed attachments:",(x=u.attachments)==null?void 0:x.map(S=>({filename:S.filename,mimeType:S.mimeType,contentId:S.contentId})));const c=new Map;for(const S of u.attachments||[])if(S.contentId){const D=this.contentToBase64(S.content),k=`data:${S.mimeType};base64,${D}`,w=S.contentId.replace(/^<|>$/g,"");c.set(w,k),c.set(`<${w}>`,k)}let d=u.html||"";d&&c.size>0&&(d=d.replace(/src=["']cid:([^"']+)["']/gi,(S,D)=>{const k=c.get(D)||c.get(`<${D}>`);return k?`src="${k}"`:S}));const f=((v=o.flags)==null?void 0:v.some(S=>S.toLowerCase().includes("seen")))||!1,p=((b=u.from)==null?void 0:b.address)||((_=u.from)==null?void 0:_.name)||"Unknown";return{uid:t,from:p,subject:u.subject||"(No subject)",date:u.date||"",body:d||u.text||"",html:d,text:u.text||"",seen:f,attachments:(u.attachments||[]).map(S=>({filename:S.filename||"attachment",mimeType:S.mimeType||"application/octet-stream",size:this.getContentSize(S.content)}))}}async findEmailInAllMail(e,t){var i,o;const n=await this.getSettings(),s=((i=n.accounts)==null?void 0:i.find(a=>a.id===e))||((o=n.accounts)==null?void 0:o[0]);if(!(s!=null&&s.imap))return console.error("[MailClient] No IMAP configuration found for account"),{found:!1};try{return await this.callEmail("findEmailInAllMail",{message_id:t.message_id,from:t.from,subject:t.subject,folder:t.folder,imap_config:{host:s.imap.host,port:parseInt(s.imap.port,10)||993,username:s.imap.user,password:s.imap.password}})}catch(a){return console.error("[MailClient] findEmailInAllMail failed:",a),{found:!1}}}contentToBase64(e){if(typeof e=="string")try{return btoa(e)}catch{return e}const t=e instanceof Uint8Array?e:new Uint8Array(e);let n="";for(let s=0;s<t.byteLength;s++)n+=String.fromCharCode(t[s]);return btoa(n)}getContentSize(e){return e?typeof e=="string"?e.length:(e instanceof Uint8Array,e.byteLength):0}async sendMail(e){const t={to:e.to,subject:e.subject,body:e.body};return e.html&&(t.html=e.html),e.from&&(t.from=e.from),e.smtpConfig&&(t.smtp_config=e.smtpConfig),this.callEmail("send",t)}async sendEmail(e,t){var n,s;try{const i=await this.getSettings(),o=((n=i.accounts)==null?void 0:n.find(u=>u.id===e))||((s=i.accounts)==null?void 0:s[0]);if(!(o!=null&&o.smtp))return{success:!1,error:"No SMTP configuration found for account"};const a={to:Array.isArray(t.to)?t.to:[t.to],subject:t.subject,body:t.text||"",html:t.html,from:o.email,smtp_config:{host:o.smtp.host,port:parseInt(o.smtp.port,10)||587,username:o.smtp.user,password:o.smtp.password}};t.in_reply_to&&(a.in_reply_to=t.in_reply_to),t.attachments&&t.attachments.length>0&&(a.attachments=t.attachments);const l=await this.callEmail("send",a);return{success:(l==null?void 0:l.sent)??!1,message_id:l==null?void 0:l.message_id,error:l==null?void 0:l.error}}catch(i){return{success:!1,error:i instanceof Error?i.message:String(i)}}}}const ou=[{id:"marin",name:"Marin",description:"⭐ Féminin, naturel et fluide"},{id:"cedar",name:"Cedar",description:"⭐ Masculin, expressif et vivant"},{id:"coral",name:"Coral",description:"Féminin, chaleureux et doux"},{id:"alloy",name:"Alloy",description:"Neutre, polyvalent"},{id:"ash",name:"Ash",description:"Masculin, calme et posé"},{id:"ballad",name:"Ballad",description:"Neutre, expressif et narratif"},{id:"echo",name:"Echo",description:"Masculin, profond et résonant"},{id:"fable",name:"Fable",description:"Masculin, accent britannique"},{id:"onyx",name:"Onyx",description:"Masculin, grave et autoritaire"},{id:"nova",name:"Nova",description:"Féminin, dynamique et énergique"},{id:"sage",name:"Sage",description:"Neutre, calme et apaisant"},{id:"shimmer",name:"Shimmer",description:"Féminin, léger et aérien"},{id:"verse",name:"Verse",description:"Neutre, poétique et mélodieux"}];class nf{constructor(e){vt(this,"config");vt(this,"promptCache",{});if(!e.system_id)throw new Error("LLMClient: system_id is REQUIRED");this.config=e}async callLLM(e,t={}){const n={coprocessor:"llm",method:e,system_id:this.config.system_id,params:t};try{const s=await fetch(`${this.config.baseUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.getToken()}`},body:JSON.stringify(n)});if(!s.ok){const o=await s.text();throw new Error(`LLM API error (${s.status}): ${o}`)}const i=await s.json();if(!i.success&&i.error)throw new Error(i.error);return i}catch(s){throw console.error("LLM API call failed:",s),s}}getToken(){return typeof window<"u"&&window.localStorage&&localStorage.getItem("qwanyx_token")||""}async complete(e,t){var i;const n={model:(t==null?void 0:t.model)??"mlm",prompt:e,max_completion_tokens:(t==null?void 0:t.maxTokens)??2e3};t!=null&&t.systemPrompt&&(n.system=t.systemPrompt),(t==null?void 0:t.temperature)!==void 0&&(n.temperature=t.temperature);const s=await this.callLLM("complete",n);return((i=s.result)==null?void 0:i.text)||s.result||""}async chat(e,t){const n={model:(t==null?void 0:t.model)??"mlm",messages:e};(t==null?void 0:t.temperature)!==void 0&&(n.temperature=t.temperature),(t==null?void 0:t.maxTokens)!==void 0&&(n.max_completion_tokens=t.maxTokens);const i=(await this.callLLM("chat",n)).result;return typeof i=="string"?i:typeof(i==null?void 0:i.response)=="string"?i.response:typeof(i==null?void 0:i.text)=="string"?i.text:typeof(i==null?void 0:i.content)=="string"?i.content:typeof(i==null?void 0:i.message)=="string"?i.message:(console.warn("LLMClient.chat: unexpected result format",JSON.stringify(i,null,2)),"")}async chatJson(e,t,n){const s=[{role:"system",content:e},{role:"user",content:t}];let o=await this.chat(s,n);return typeof o=="string"&&(o=o.trim(),o.startsWith("```json")?o=o.slice(7):o.startsWith("```")&&(o=o.slice(3)),o.endsWith("```")&&(o=o.slice(0,-3)),o=o.trim()),JSON.parse(o)}async analyzeDocuments(e,t,n){var o;const s={prompt:e,files:t,model:(n==null?void 0:n.model)??"xlm"};return(n==null?void 0:n.temperature)!==void 0&&(s.temperature=n.temperature),(n==null?void 0:n.maxTokens)!==void 0&&(s.max_tokens=n.maxTokens),((o=(await this.callLLM("analyze",s)).result)==null?void 0:o.text)||""}async tts(e,t){var i;const n={model:"gpt-4o-mini-tts",voice:(t==null?void 0:t.voice)??"coral",input:e,response_format:"mp3"},s=await this.callLLM("tts",n);return((i=s.result)==null?void 0:i.audio)||s.result||""}async transcribe(e,t){var i;const n={audio_data:e};return t!=null&&t.filename&&(n.filename=t.filename),t!=null&&t.language&&(n.language=t.language),((i=(await this.callLLM("transcribe",n)).result)==null?void 0:i.text)||""}async getPrompt(e){var s;if(this.promptCache[e])return this.promptCache[e];const n=((s=(await this.callLLM("get_prompt",{name:e})).result)==null?void 0:s.content)||"";return this.promptCache[e]=n,n}clearPromptCache(){this.promptCache={}}buildPrompt(e,t){let n=e;for(const[s,i]of Object.entries(t))n=n.replace(new RegExp(`\\{\\{${s}\\}\\}`,"g"),i);return n}}const Ai="qwanyx_auth_token",Ti="qwanyx_refresh_token",Ni="qwanyx_auth_user";class nt{static setToken(e){typeof window<"u"&&localStorage.setItem(Ai,e)}static getToken(){return typeof window<"u"?localStorage.getItem(Ai):null}static clearToken(){typeof window<"u"&&(localStorage.removeItem(Ai),localStorage.removeItem(Ti))}static setRefreshToken(e){typeof window<"u"&&localStorage.setItem(Ti,e)}static getRefreshToken(){return typeof window<"u"?localStorage.getItem(Ti):null}static isAuthenticated(){return!!this.getToken()}static getAuthHeader(){const e=this.getToken();return e?{Authorization:`Bearer ${e}`}:{}}static setUser(e){typeof window<"u"&&localStorage.setItem(Ni,JSON.stringify(e))}static getUser(){if(typeof window<"u"){const e=localStorage.getItem(Ni);if(e)try{return JSON.parse(e)}catch{return null}}return null}static clearUser(){typeof window<"u"&&localStorage.removeItem(Ni)}static clearAll(){this.clearToken(),this.clearUser()}}class Ao{constructor(e){vt(this,"config");this.config=e}async register(e,t,n){var s,i;try{const a=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"register",params:{email:e,password:t,...n,system_id:this.config.systemId}})})).json();return a.success&&((s=a.result)!=null&&s.success)?{success:!0,userId:a.result.user_id,email:a.result.email,requiresVerification:a.result.requiresVerification}:{success:!1,error:((i=a.result)==null?void 0:i.message)||a.error||"Registration failed"}}catch(o){return{success:!1,error:o instanceof Error?o.message:"Network error"}}}async verifyEmail(e,t){var n,s,i,o;try{const l=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"verify_email",params:{email:e,code:t}})})).json();if(l.success&&((n=l.result)!=null&&n.token)){const{token:u,user:c}=l.result;return nt.setToken(u),nt.setUser(c),(i=(s=this.config).onLogin)==null||i.call(s,c),{success:!0,user:c,token:u}}else return{success:!1,error:((o=l.result)==null?void 0:o.error)||l.error||"Verification failed"}}catch(a){return{success:!1,error:a instanceof Error?a.message:"Network error"}}}async requestPasswordReset(e){var t,n;try{const i=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"request_password_reset",params:{email:e}})})).json();return i.success&&((t=i.result)!=null&&t.success)?{success:!0,message:i.result.message}:{success:!1,error:((n=i.result)==null?void 0:n.message)||i.error||"Request failed"}}catch(s){return{success:!1,error:s instanceof Error?s.message:"Network error"}}}async setPasswordWithCode(e,t,n){var s,i,o,a;try{const u=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"set_password_with_code",params:{email:e,code:t,password:n}})})).json();if(u.success&&((s=u.result)!=null&&s.token)){const{token:c,user:d}=u.result;return nt.setToken(c),nt.setUser(d),(o=(i=this.config).onLogin)==null||o.call(i,d),{success:!0,user:d,token:c}}else return{success:!1,error:((a=u.result)==null?void 0:a.message)||u.error||"Password reset failed"}}catch(l){return{success:!1,error:l instanceof Error?l.message:"Network error"}}}async login(e,t){var n,s,i,o,a,l;try{const c=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"login",params:{email:e,password:t,system_id:this.config.systemId}})})).json();if((n=c.result)!=null&&n.requiresVerification||c.requiresVerification)return{success:!1,error:((s=c.result)==null?void 0:s.error)||c.error||"Please verify your email first",requiresVerification:!0};if(c.success&&((i=c.result)!=null&&i.token)){const{token:d,user:f}=c.result;return nt.setToken(d),nt.setUser(f),(a=(o=this.config).onLogin)==null||a.call(o,f),{success:!0,user:f,token:d}}else return{success:!1,error:((l=c.result)==null?void 0:l.error)||c.error||"Login failed"}}catch(u){return{success:!1,error:u instanceof Error?u.message:"Network error"}}}async resendVerificationCode(e){return this.requestPasswordReset(e)}logout(){var e,t;nt.clearAll(),(t=(e=this.config).onLogout)==null||t.call(e)}async checkRole(e,t){var n;try{const i=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:this.config.systemId||"system",coprocessor:"graph",method:"get_user_role",params:{group_id:e,target_user_id:t}})})).json();return i.success&&((n=i.result)!=null&&n.role)?i.result.role:null}catch{return null}}async isAdmin(e){return this.config.systemId?await this.checkRole(`${this.config.systemId}__admins`,e)==="admin":!1}getCurrentUser(){return nt.getUser()}isAuthenticated(){return nt.isAuthenticated()}async listAllUsers(e){try{const n=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"list_users",params:{...(e==null?void 0:e.limit)&&{limit:e.limit},...(e==null?void 0:e.offset)&&{offset:e.offset}}})})).json();return n.success&&n.result?{success:!0,users:n.result.users||[],total:n.result.total||0}:{success:!1,error:n.error||"Failed to list users"}}catch(t){return{success:!1,error:t instanceof Error?t.message:"Network error"}}}async listSystemUsers(e){if(!this.config.systemId)return{success:!1,error:"System ID not configured"};try{const n=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"list_system_users",params:{system_id:this.config.systemId,...(e==null?void 0:e.limit)&&{limit:e.limit},...(e==null?void 0:e.offset)&&{offset:e.offset}}})})).json();return n.success&&n.result?{success:!0,users:n.result.users||[],total:n.result.total||0}:{success:!1,error:n.error||"Failed to list users"}}catch(t){return{success:!1,error:t instanceof Error?t.message:"Network error"}}}async addUserToSystem(e,t="member"){if(!this.config.systemId)return{success:!1,error:"System ID not configured"};try{const s=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"add_system_to_user",params:{user_id:e,system_id:this.config.systemId,role:t}})})).json();return{success:s.success||!1,error:s.error}}catch(n){return{success:!1,error:n instanceof Error?n.message:"Network error"}}}async updateMembershipRole(e,t){if(!this.config.systemId)return{success:!1,error:"System ID not configured"};try{const s=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"add_system_to_user",params:{user_id:e,system_id:this.config.systemId,role:t}})})).json();return{success:s.success||!1,error:s.error}}catch(n){return{success:!1,error:n instanceof Error?n.message:"Network error"}}}async listUserSystems(e){try{const n=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"list_systems",params:{user_id:e}})})).json();return n.success&&n.result?{success:!0,systems:n.result.systems||[]}:{success:!1,error:n.error||"Failed to list user systems"}}catch(t){return{success:!1,error:t instanceof Error?t.message:"Network error"}}}async removeUserFromSystem(e){if(!this.config.systemId)return{success:!1,error:"System ID not configured"};try{const n=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"remove_system_from_user",params:{user_id:e,system_id:this.config.systemId}})})).json();return{success:n.success||!1,error:n.error}}catch(t){return{success:!1,error:t instanceof Error?t.message:"Network error"}}}}let As=null;function sf(r){return As=new Ao(r),As}function of(){if(!As)throw new Error("Auth service not initialized. Call initializeAuthService() first.");return As}class au{constructor(e){vt(this,"config");this.config={timeout:3e4,headers:{"Content-Type":"application/json"},...e}}buildQueryString(e){if(!e||Object.keys(e).length===0)return"";const t=new URLSearchParams;Object.entries(e).forEach(([s,i])=>{i!=null&&t.append(s,String(i))});const n=t.toString();return n?`?${n}`:""}async request(e,t={}){const{method:n="GET",headers:s={},body:i,params:o}=t,a=`${this.config.baseUrl}/${e}${this.buildQueryString(o)}`,l={...this.config.headers,...nt.getAuthHeader(),...s},u={method:n,headers:l};i&&n!=="GET"&&(u.body=JSON.stringify(i));try{const c=new AbortController,d=setTimeout(()=>c.abort(),this.config.timeout),f=await fetch(a,{...u,signal:c.signal});if(clearTimeout(d),!f.ok){const p=await f.json().catch(()=>({message:f.statusText}));throw new Error(p.message||`HTTP ${f.status}`)}return await f.json()}catch(c){throw c instanceof Error?c:new Error("An unexpected error occurred")}}async get(e,t){return this.request(e,{method:"GET",params:t})}async post(e,t,n){return this.request(e,{method:"POST",body:t,params:n})}async put(e,t,n){return this.request(e,{method:"PUT",body:t,params:n})}async patch(e,t,n){return this.request(e,{method:"PATCH",body:t,params:n})}async delete(e,t){return this.request(e,{method:"DELETE",params:t})}setBaseUrl(e){this.config.baseUrl=e}getBaseUrl(){return this.config.baseUrl}}let Ts=null;function af(r){return Ts=new au(r),Ts}function To(){if(!Ts)throw new Error("API client not initialized. Call initializeApiClient() first.");return Ts}var Vi={exports:{}},cn={};/**
28
+ `)}),this.textContent=e}isInlineTextNode(e){if(e.contentDisposition.parsed.value==="attachment")return!1;switch(e.contentType.parsed.value){case"text/html":case"text/plain":return!0;case"text/calendar":case"text/csv":default:return!1}}isInlineMessageRfc822(e){return e.contentType.parsed.value!=="message/rfc822"?!1:(e.contentDisposition.parsed.value||(this.options.rfc822Attachments?"attachment":"inline"))==="inline"}forceRfc822Attachments(){if(this.options.forceRfc822Attachments)return!0;let e=!1,t=n=>{n.contentType.multipart||["message/delivery-status","message/feedback-report"].includes(n.contentType.parsed.value)&&(e=!0);for(let s of n.childNodes)t(s)};return t(this.root),e}async resolveStream(e){let t=0,n=[];const s=e.getReader();for(;;){const{done:a,value:l}=await s.read();if(a)break;n.push(l),t+=l.length}const i=new Uint8Array(t);let o=0;for(let a of n)i.set(a,o),o+=a.length;return i}async parse(e){var s,i;if(this.started)throw new Error("Can not reuse parser, create a new PostalMime object");for(this.started=!0,e&&typeof e.getReader=="function"&&(e=await this.resolveStream(e)),e=e||new ArrayBuffer(0),typeof e=="string"&&(e=Xr.encode(e)),(e instanceof Blob||Object.prototype.toString.call(e)==="[object Blob]")&&(e=await ni(e)),e.buffer instanceof ArrayBuffer&&(e=new Uint8Array(e).buffer),this.buf=e,this.av=new Uint8Array(e),this.readPos=0;this.readPos<this.av.length;){const o=this.readLine();await this.processLine(o.bytes,o.done)}await this.processNodeTree();const t={headers:this.root.headers.map(o=>({key:o.key,value:o.value})).reverse()};for(const o of["from","sender"]){const a=this.root.headers.find(l=>l.key===o);if(a&&a.value){const l=Dn(a.value);l&&l.length&&(t[o]=l[0])}}for(const o of["delivered-to","return-path"]){const a=this.root.headers.find(l=>l.key===o);if(a&&a.value){const l=Dn(a.value);if(l&&l.length&&l[0].address){const u=o.replace(/\-(.)/g,(c,d)=>d.toUpperCase());t[u]=l[0].address}}}for(const o of["to","cc","bcc","reply-to"]){const a=this.root.headers.filter(u=>u.key===o);let l=[];if(a.filter(u=>u&&u.value).map(u=>Dn(u.value)).forEach(u=>l=l.concat(u||[])),l&&l.length){const u=o.replace(/\-(.)/g,(c,d)=>d.toUpperCase());t[u]=l}}for(const o of["subject","message-id","in-reply-to","references"]){const a=this.root.headers.find(l=>l.key===o);if(a&&a.value){const l=o.replace(/\-(.)/g,(u,c)=>c.toUpperCase());t[l]=En(a.value)}}let n=this.root.headers.find(o=>o.key==="date");if(n){let o=new Date(n.value);!o||o.toString()==="Invalid Date"?o=n.value:o=o.toISOString(),t.date=o}switch((s=this.textContent)!=null&&s.html&&(t.html=this.textContent.html),(i=this.textContent)!=null&&i.plain&&(t.text=this.textContent.plain),t.attachments=this.attachments,this.attachmentEncoding){case"arraybuffer":break;case"base64":for(let a of t.attachments||[])a!=null&&a.content&&(a.content=ef(a.content),a.encoding="base64");break;case"utf8":let o=new TextDecoder("utf8");for(let a of t.attachments||[])a!=null&&a.content&&(a.content=o.decode(a.content),a.encoding="utf8");break;default:throw new Error("Unknwon attachment encoding")}return t}}class iu{constructor(e){vt(this,"config");if(!e.system_id)throw new Error("MailClient: system_id is REQUIRED");this.config=e}async callCoprocessor(e,t,n={}){const s={coprocessor:e,method:t,system_id:this.config.system_id,params:{user_id:this.config.system_id,...n}};try{const i=await fetch(`${this.config.baseUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.getToken()}`},body:JSON.stringify(s)});if(!i.ok){const a=await i.text();throw new Error(`API error (${i.status}): ${a}`)}const o=await i.json();if(!o.success&&o.error)throw new Error(o.error);return o.result}catch(i){throw console.error("API call failed:",i),i}}async callMail(e,t={}){return this.callCoprocessor("mail",e,t)}async callEmail(e,t={}){return this.callCoprocessor("email",e,t)}getToken(){return typeof window<"u"&&window.localStorage&&localStorage.getItem("qwanyx_token")||""}async getSettings(){try{return await this.callMail("get_email_settings")||{accounts:[]}}catch{return{accounts:[]}}}async saveSettings(e){await this.callMail("save_email_settings",{accounts:e})}async listEmails(e,t=150,n){return this.callMail("list_emails",{account_id:e,limit:t,folder:n})}async deleteEmails(e,t,n=!0){return this.callMail("delete_emails",{account_id:e,uids:t,expunge:n})}async imapExec(e,t){return this.callMail("imap_exec",{account_id:e,commands:t})}async trashEmails(e,t,n="INBOX"){const s=["[Gmail]/Trash","[Gmail]/Corbeille","[Gmail]/Bin","Trash"];for(const i of s){const o=[`SELECT "${n}"`,...t.map(u=>`UID MOVE ${u} "${i}"`)],l=(await this.imapExec(e,o)).responses.filter(u=>u.ok&&u.command==="UID MOVE").length;if(l>0)return{success:!0,moved:l}}return{success:!1,moved:0}}async archiveEmails(e,t,n="INBOX"){const s=[`SELECT "${n}"`,...t.map(a=>`UID STORE ${a} +FLAGS (\\Deleted)`),"EXPUNGE"],o=(await this.imapExec(e,s)).responses.filter(a=>a.ok&&a.command==="UID STORE").length;return{success:o>0,archived:o}}async listFolders(e){const n=(await this.imapExec(e,['LIST "" *'])).responses.find(s=>s.command==="LIST");return n!=null&&n.ok&&n.folders?{success:!0,folders:n.folders}:{success:!1,folders:[]}}async listEmailsSorted(e,t="INBOX",n=150){const s=await this.imapExec(e,[`SELECT ${t}`,"FETCH 1:* (UID INTERNALDATE)"]),i=s.responses.find(p=>p.command==="SELECT");if(!(i!=null&&i.ok))throw new Error(`Failed to select folder: ${(i==null?void 0:i.error)||"Unknown error"}`);const o=s.responses.find(p=>p.command==="FETCH");if(!(o!=null&&o.ok)||!o.messages)return{account:{id:e,label:"",email:""},mailbox:{name:t,total:i.exists||0},messages:[]};const l=[...o.messages].sort((p,g)=>{const m=p.internalDate?new Date(p.internalDate).getTime():0;return(g.internalDate?new Date(g.internalDate).getTime():0)-m}).slice(0,n).map(p=>p.uid).filter(Boolean);if(l.length===0)return{account:{id:e,label:"",email:""},mailbox:{name:t,total:i.exists||0},messages:[]};const c=(await this.imapExec(e,[`SELECT ${t}`,`UID FETCH ${l.join(",")} (UID FLAGS ENVELOPE)`])).responses.find(p=>p.command==="UID FETCH");if(!(c!=null&&c.ok)||!c.messages)throw new Error(`Failed to fetch details: ${(c==null?void 0:c.error)||"Unknown error"}`);const d=new Map;for(const p of c.messages)p.uid&&d.set(p.uid,p);const f=l.map(p=>{var _,S,D,k,w;const g=d.get(p);if(!g)return null;const m=((_=g.flags)==null?void 0:_.some(E=>E.includes("Seen")))||!1,x=((D=(S=g.envelope)==null?void 0:S.from)==null?void 0:D.email)||"Unknown",v=((k=g.envelope)==null?void 0:k.subject)||"(No subject)",b=((w=g.envelope)==null?void 0:w.date)||"";return{uid:p,from:x,subject:v,date:b,seen:m}}).filter(Boolean);return{account:{id:e,label:"",email:""},mailbox:{name:t,total:i.exists||0},messages:f}}async getEmail(e,t,n="INBOX"){return this.callMail("get_email",{account_id:e,uid:t,folder:n})}async getAttachment(e,t,n,s="INBOX"){return this.callMail("get_attachment",{account_id:e,uid:t,attachment_index:n,folder:s})}async searchContactEmails(e,t,n=50){return this.callMail("search_contact_emails",{account_id:e,contact_email:t,limit:n})}async getEmailParsed(e,t,n="INBOX"){var g,m,x,v,b,_;const i=(await this.imapExec(e,[`SELECT "${n}"`,`UID FETCH ${t} (FLAGS BODY[])`])).responses.find(S=>S.command==="UID FETCH");if(!(i!=null&&i.ok)||!((g=i.messages)!=null&&g[0]))return console.error("Failed to fetch raw email:",i==null?void 0:i.error),null;const o=i.messages[0],a=o.body||o.raw;if(!a)return console.error("No raw email content in response"),null;const u=await new Ds().parse(a);console.log("[MailClient] Parsed email - attachments count:",((m=u.attachments)==null?void 0:m.length)||0),console.log("[MailClient] Parsed attachments:",(x=u.attachments)==null?void 0:x.map(S=>({filename:S.filename,mimeType:S.mimeType,contentId:S.contentId})));const c=new Map;for(const S of u.attachments||[])if(S.contentId){const D=this.contentToBase64(S.content),k=`data:${S.mimeType};base64,${D}`,w=S.contentId.replace(/^<|>$/g,"");c.set(w,k),c.set(`<${w}>`,k)}let d=u.html||"";d&&c.size>0&&(d=d.replace(/src=["']cid:([^"']+)["']/gi,(S,D)=>{const k=c.get(D)||c.get(`<${D}>`);return k?`src="${k}"`:S}));const f=((v=o.flags)==null?void 0:v.some(S=>S.toLowerCase().includes("seen")))||!1,p=((b=u.from)==null?void 0:b.address)||((_=u.from)==null?void 0:_.name)||"Unknown";return{uid:t,from:p,subject:u.subject||"(No subject)",date:u.date||"",body:d||u.text||"",html:d,text:u.text||"",seen:f,attachments:(u.attachments||[]).map(S=>({filename:S.filename||"attachment",mimeType:S.mimeType||"application/octet-stream",size:this.getContentSize(S.content)}))}}async findEmailInAllMail(e,t){var i,o;const n=await this.getSettings(),s=((i=n.accounts)==null?void 0:i.find(a=>a.id===e))||((o=n.accounts)==null?void 0:o[0]);if(!(s!=null&&s.imap))return console.error("[MailClient] No IMAP configuration found for account"),{found:!1};try{return await this.callEmail("findEmailInAllMail",{message_id:t.message_id,from:t.from,subject:t.subject,folder:t.folder,imap_config:{host:s.imap.host,port:parseInt(s.imap.port,10)||993,username:s.imap.user,password:s.imap.password}})}catch(a){return console.error("[MailClient] findEmailInAllMail failed:",a),{found:!1}}}contentToBase64(e){if(typeof e=="string")try{return btoa(e)}catch{return e}const t=e instanceof Uint8Array?e:new Uint8Array(e);let n="";for(let s=0;s<t.byteLength;s++)n+=String.fromCharCode(t[s]);return btoa(n)}getContentSize(e){return e?typeof e=="string"?e.length:(e instanceof Uint8Array,e.byteLength):0}async sendMail(e){const t={to:e.to,subject:e.subject,body:e.body};return e.html&&(t.html=e.html),e.from&&(t.from=e.from),e.smtpConfig&&(t.smtp_config=e.smtpConfig),this.callEmail("send",t)}async sendEmail(e,t){var n,s;try{const i=await this.getSettings(),o=((n=i.accounts)==null?void 0:n.find(u=>u.id===e))||((s=i.accounts)==null?void 0:s[0]);if(!(o!=null&&o.smtp))return{success:!1,error:"No SMTP configuration found for account"};const a={to:Array.isArray(t.to)?t.to:[t.to],subject:t.subject,body:t.text||"",html:t.html,from:o.email,smtp_config:{host:o.smtp.host,port:parseInt(o.smtp.port,10)||587,username:o.smtp.user,password:o.smtp.password}};t.in_reply_to&&(a.in_reply_to=t.in_reply_to),t.attachments&&t.attachments.length>0&&(a.attachments=t.attachments);const l=await this.callEmail("send",a);return{success:(l==null?void 0:l.sent)??!1,message_id:l==null?void 0:l.message_id,error:l==null?void 0:l.error}}catch(i){return{success:!1,error:i instanceof Error?i.message:String(i)}}}}const ou=[{id:"marin",name:"Marin",description:"⭐ Féminin, naturel et fluide"},{id:"cedar",name:"Cedar",description:"⭐ Masculin, expressif et vivant"},{id:"coral",name:"Coral",description:"Féminin, chaleureux et doux"},{id:"alloy",name:"Alloy",description:"Neutre, polyvalent"},{id:"ash",name:"Ash",description:"Masculin, calme et posé"},{id:"ballad",name:"Ballad",description:"Neutre, expressif et narratif"},{id:"echo",name:"Echo",description:"Masculin, profond et résonant"},{id:"fable",name:"Fable",description:"Masculin, accent britannique"},{id:"onyx",name:"Onyx",description:"Masculin, grave et autoritaire"},{id:"nova",name:"Nova",description:"Féminin, dynamique et énergique"},{id:"sage",name:"Sage",description:"Neutre, calme et apaisant"},{id:"shimmer",name:"Shimmer",description:"Féminin, léger et aérien"},{id:"verse",name:"Verse",description:"Neutre, poétique et mélodieux"}];class nf{constructor(e){vt(this,"config");vt(this,"promptCache",{});if(!e.system_id)throw new Error("LLMClient: system_id is REQUIRED");this.config=e}async callLLM(e,t={}){const n={coprocessor:"llm",method:e,system_id:this.config.system_id,params:t};try{const s=await fetch(`${this.config.baseUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.getToken()}`},body:JSON.stringify(n)});if(!s.ok){const o=await s.text();throw new Error(`LLM API error (${s.status}): ${o}`)}const i=await s.json();if(!i.success&&i.error)throw new Error(i.error);return i}catch(s){throw console.error("LLM API call failed:",s),s}}getToken(){return typeof window<"u"&&window.localStorage&&localStorage.getItem("qwanyx_token")||""}async complete(e,t){var i;const n={model:(t==null?void 0:t.model)??"mlm",prompt:e,max_completion_tokens:(t==null?void 0:t.maxTokens)??2e3};t!=null&&t.systemPrompt&&(n.system=t.systemPrompt),(t==null?void 0:t.temperature)!==void 0&&(n.temperature=t.temperature);const s=await this.callLLM("complete",n);return((i=s.result)==null?void 0:i.text)||s.result||""}async chat(e,t){const n={model:(t==null?void 0:t.model)??"mlm",messages:e};(t==null?void 0:t.temperature)!==void 0&&(n.temperature=t.temperature),(t==null?void 0:t.maxTokens)!==void 0&&(n.max_completion_tokens=t.maxTokens);const i=(await this.callLLM("chat",n)).result;return typeof i=="string"?i:typeof(i==null?void 0:i.response)=="string"?i.response:typeof(i==null?void 0:i.text)=="string"?i.text:typeof(i==null?void 0:i.content)=="string"?i.content:typeof(i==null?void 0:i.message)=="string"?i.message:(console.warn("LLMClient.chat: unexpected result format",JSON.stringify(i,null,2)),"")}async chatJson(e,t,n){const s=[{role:"system",content:e},{role:"user",content:t}];let o=await this.chat(s,n);return typeof o=="string"&&(o=o.trim(),o.startsWith("```json")?o=o.slice(7):o.startsWith("```")&&(o=o.slice(3)),o.endsWith("```")&&(o=o.slice(0,-3)),o=o.trim()),JSON.parse(o)}async analyzeDocuments(e,t,n){var o;const s={prompt:e,files:t,model:(n==null?void 0:n.model)??"xlm"};return(n==null?void 0:n.temperature)!==void 0&&(s.temperature=n.temperature),(n==null?void 0:n.maxTokens)!==void 0&&(s.max_tokens=n.maxTokens),((o=(await this.callLLM("analyze",s)).result)==null?void 0:o.text)||""}async tts(e,t){var i;const n={model:"gpt-4o-mini-tts",voice:(t==null?void 0:t.voice)??"coral",input:e,response_format:"mp3"},s=await this.callLLM("tts",n);return((i=s.result)==null?void 0:i.audio)||s.result||""}async transcribe(e,t){var i;const n={audio_data:e};return t!=null&&t.filename&&(n.filename=t.filename),t!=null&&t.language&&(n.language=t.language),((i=(await this.callLLM("transcribe",n)).result)==null?void 0:i.text)||""}async getPrompt(e){var s;if(this.promptCache[e])return this.promptCache[e];const n=((s=(await this.callLLM("get_prompt",{name:e})).result)==null?void 0:s.content)||"";return this.promptCache[e]=n,n}clearPromptCache(){this.promptCache={}}buildPrompt(e,t){let n=e;for(const[s,i]of Object.entries(t))n=n.replace(new RegExp(`\\{\\{${s}\\}\\}`,"g"),i);return n}}const Ai="qwanyx_auth_token",Ti="qwanyx_refresh_token",Ni="qwanyx_auth_user";class nt{static setToken(e){typeof window<"u"&&localStorage.setItem(Ai,e)}static getToken(){return typeof window<"u"?localStorage.getItem(Ai):null}static clearToken(){typeof window<"u"&&(localStorage.removeItem(Ai),localStorage.removeItem(Ti))}static setRefreshToken(e){typeof window<"u"&&localStorage.setItem(Ti,e)}static getRefreshToken(){return typeof window<"u"?localStorage.getItem(Ti):null}static isAuthenticated(){return!!this.getToken()}static getAuthHeader(){const e=this.getToken();return e?{Authorization:`Bearer ${e}`}:{}}static setUser(e){typeof window<"u"&&localStorage.setItem(Ni,JSON.stringify(e))}static getUser(){if(typeof window<"u"){const e=localStorage.getItem(Ni);if(e)try{return JSON.parse(e)}catch{return null}}return null}static clearUser(){typeof window<"u"&&localStorage.removeItem(Ni)}static clearAll(){this.clearToken(),this.clearUser()}}class Ao{constructor(e){vt(this,"config");this.config=e}async register(e,t,n){var s,i;try{const a=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"register",params:{email:e,password:t,...n,system_id:this.config.systemId}})})).json();return a.success&&((s=a.result)!=null&&s.success)?{success:!0,userId:a.result.user_id,email:a.result.email,requiresVerification:a.result.requiresVerification}:{success:!1,error:((i=a.result)==null?void 0:i.message)||a.error||"Registration failed"}}catch(o){return{success:!1,error:o instanceof Error?o.message:"Network error"}}}async verifyEmail(e,t){var n,s,i,o;try{const l=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"verify_email",params:{email:e,code:t}})})).json();if(l.success&&((n=l.result)!=null&&n.token)){const{token:u,user:c}=l.result;return nt.setToken(u),nt.setUser(c),(i=(s=this.config).onLogin)==null||i.call(s,c),{success:!0,user:c,token:u}}else return{success:!1,error:((o=l.result)==null?void 0:o.error)||l.error||"Verification failed"}}catch(a){return{success:!1,error:a instanceof Error?a.message:"Network error"}}}async requestPasswordReset(e){var t,n;try{const i=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"request_password_reset",params:{email:e}})})).json();return i.success&&((t=i.result)!=null&&t.success)?{success:!0,message:i.result.message}:{success:!1,error:((n=i.result)==null?void 0:n.message)||i.error||"Request failed"}}catch(s){return{success:!1,error:s instanceof Error?s.message:"Network error"}}}async setPasswordWithCode(e,t,n){var s,i,o,a;try{const u=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"set_password_with_code",params:{email:e,code:t,password:n}})})).json();if(u.success&&((s=u.result)!=null&&s.token)){const{token:c,user:d}=u.result;return nt.setToken(c),nt.setUser(d),(o=(i=this.config).onLogin)==null||o.call(i,d),{success:!0,user:d,token:c}}else return{success:!1,error:((a=u.result)==null?void 0:a.message)||u.error||"Password reset failed"}}catch(l){return{success:!1,error:l instanceof Error?l.message:"Network error"}}}async login(e,t,n){var s,i,o,a,l,u;try{const d=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"login",params:{email:e,password:t,system_id:this.config.systemId,...n}})})).json();if((s=d.result)!=null&&s.requiresVerification||d.requiresVerification)return{success:!1,error:((i=d.result)==null?void 0:i.error)||d.error||"Please verify your email first",requiresVerification:!0};if(d.success&&((o=d.result)!=null&&o.token)){const{token:f,user:p}=d.result;return nt.setToken(f),nt.setUser(p),(l=(a=this.config).onLogin)==null||l.call(a,p),{success:!0,user:p,token:f}}else return{success:!1,error:((u=d.result)==null?void 0:u.error)||d.error||"Login failed"}}catch(c){return{success:!1,error:c instanceof Error?c.message:"Network error"}}}async resendVerificationCode(e){return this.requestPasswordReset(e)}logout(){var e,t;nt.clearAll(),(t=(e=this.config).onLogout)==null||t.call(e)}async checkRole(e,t){var n;try{const i=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:this.config.systemId||"system",coprocessor:"graph",method:"get_user_role",params:{group_id:e,target_user_id:t}})})).json();return i.success&&((n=i.result)!=null&&n.role)?i.result.role:null}catch{return null}}async isAdmin(e){return this.config.systemId?await this.checkRole(`${this.config.systemId}__admins`,e)==="admin":!1}getCurrentUser(){return nt.getUser()}isAuthenticated(){return nt.isAuthenticated()}async listAllUsers(e){try{const n=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"list_users",params:{...(e==null?void 0:e.limit)&&{limit:e.limit},...(e==null?void 0:e.offset)&&{offset:e.offset}}})})).json();return n.success&&n.result?{success:!0,users:n.result.users||[],total:n.result.total||0}:{success:!1,error:n.error||"Failed to list users"}}catch(t){return{success:!1,error:t instanceof Error?t.message:"Network error"}}}async listSystemUsers(e){if(!this.config.systemId)return{success:!1,error:"System ID not configured"};try{const n=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"list_system_users",params:{system_id:this.config.systemId,...(e==null?void 0:e.limit)&&{limit:e.limit},...(e==null?void 0:e.offset)&&{offset:e.offset}}})})).json();return n.success&&n.result?{success:!0,users:n.result.users||[],total:n.result.total||0}:{success:!1,error:n.error||"Failed to list users"}}catch(t){return{success:!1,error:t instanceof Error?t.message:"Network error"}}}async addUserToSystem(e,t="member"){if(!this.config.systemId)return{success:!1,error:"System ID not configured"};try{const s=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"add_system_to_user",params:{user_id:e,system_id:this.config.systemId,role:t}})})).json();return{success:s.success||!1,error:s.error}}catch(n){return{success:!1,error:n instanceof Error?n.message:"Network error"}}}async updateMembershipRole(e,t){if(!this.config.systemId)return{success:!1,error:"System ID not configured"};try{const s=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"add_system_to_user",params:{user_id:e,system_id:this.config.systemId,role:t}})})).json();return{success:s.success||!1,error:s.error}}catch(n){return{success:!1,error:n instanceof Error?n.message:"Network error"}}}async listUserSystems(e){try{const n=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"list_systems",params:{user_id:e}})})).json();return n.success&&n.result?{success:!0,systems:n.result.systems||[]}:{success:!1,error:n.error||"Failed to list user systems"}}catch(t){return{success:!1,error:t instanceof Error?t.message:"Network error"}}}async removeUserFromSystem(e){if(!this.config.systemId)return{success:!1,error:"System ID not configured"};try{const n=await(await fetch(`${this.config.apiUrl}/spu/invoke`,{method:"POST",headers:{"Content-Type":"application/json",...nt.getAuthHeader()},body:JSON.stringify({system_id:"system",coprocessor:"auth",method:"remove_system_from_user",params:{user_id:e,system_id:this.config.systemId}})})).json();return{success:n.success||!1,error:n.error}}catch(t){return{success:!1,error:t instanceof Error?t.message:"Network error"}}}}let As=null;function sf(r){return As=new Ao(r),As}function of(){if(!As)throw new Error("Auth service not initialized. Call initializeAuthService() first.");return As}class au{constructor(e){vt(this,"config");this.config={timeout:3e4,headers:{"Content-Type":"application/json"},...e}}buildQueryString(e){if(!e||Object.keys(e).length===0)return"";const t=new URLSearchParams;Object.entries(e).forEach(([s,i])=>{i!=null&&t.append(s,String(i))});const n=t.toString();return n?`?${n}`:""}async request(e,t={}){const{method:n="GET",headers:s={},body:i,params:o}=t,a=`${this.config.baseUrl}/${e}${this.buildQueryString(o)}`,l={...this.config.headers,...nt.getAuthHeader(),...s},u={method:n,headers:l};i&&n!=="GET"&&(u.body=JSON.stringify(i));try{const c=new AbortController,d=setTimeout(()=>c.abort(),this.config.timeout),f=await fetch(a,{...u,signal:c.signal});if(clearTimeout(d),!f.ok){const p=await f.json().catch(()=>({message:f.statusText}));throw new Error(p.message||`HTTP ${f.status}`)}return await f.json()}catch(c){throw c instanceof Error?c:new Error("An unexpected error occurred")}}async get(e,t){return this.request(e,{method:"GET",params:t})}async post(e,t,n){return this.request(e,{method:"POST",body:t,params:n})}async put(e,t,n){return this.request(e,{method:"PUT",body:t,params:n})}async patch(e,t,n){return this.request(e,{method:"PATCH",body:t,params:n})}async delete(e,t){return this.request(e,{method:"DELETE",params:t})}setBaseUrl(e){this.config.baseUrl=e}getBaseUrl(){return this.config.baseUrl}}let Ts=null;function af(r){return Ts=new au(r),Ts}function To(){if(!Ts)throw new Error("API client not initialized. Call initializeApiClient() first.");return Ts}var Vi={exports:{}},cn={};/**
29
29
  * @license React
30
30
  * react-jsx-runtime.production.min.js
31
31
  *
package/dist/index.esm.js CHANGED
@@ -4224,10 +4224,10 @@ class uu {
4224
4224
  * Uses system coprocessor for authentication
4225
4225
  * Auto-adds user as member of configured system on first login
4226
4226
  */
4227
- async login(e, t) {
4228
- var n, s, i, o, a, l;
4227
+ async login(e, t, n) {
4228
+ var s, i, o, a, l, u;
4229
4229
  try {
4230
- const c = await (await fetch(`${this.config.apiUrl}/spu/invoke`, {
4230
+ const d = await (await fetch(`${this.config.apiUrl}/spu/invoke`, {
4231
4231
  method: "POST",
4232
4232
  headers: { "Content-Type": "application/json" },
4233
4233
  body: JSON.stringify({
@@ -4237,26 +4237,27 @@ class uu {
4237
4237
  params: {
4238
4238
  email: e,
4239
4239
  password: t,
4240
- system_id: this.config.systemId
4240
+ system_id: this.config.systemId,
4241
4241
  // Auto-add as member on login
4242
+ ...n
4242
4243
  }
4243
4244
  })
4244
4245
  })).json();
4245
- if ((n = c.result) != null && n.requiresVerification || c.requiresVerification)
4246
+ if ((s = d.result) != null && s.requiresVerification || d.requiresVerification)
4246
4247
  return {
4247
4248
  success: !1,
4248
- error: ((s = c.result) == null ? void 0 : s.error) || c.error || "Please verify your email first",
4249
+ error: ((i = d.result) == null ? void 0 : i.error) || d.error || "Please verify your email first",
4249
4250
  requiresVerification: !0
4250
4251
  };
4251
- if (c.success && ((i = c.result) != null && i.token)) {
4252
- const { token: d, user: f } = c.result;
4253
- return ct.setToken(d), ct.setUser(f), (a = (o = this.config).onLogin) == null || a.call(o, f), { success: !0, user: f, token: d };
4252
+ if (d.success && ((o = d.result) != null && o.token)) {
4253
+ const { token: f, user: p } = d.result;
4254
+ return ct.setToken(f), ct.setUser(p), (l = (a = this.config).onLogin) == null || l.call(a, p), { success: !0, user: p, token: f };
4254
4255
  } else
4255
- return { success: !1, error: ((l = c.result) == null ? void 0 : l.error) || c.error || "Login failed" };
4256
- } catch (u) {
4256
+ return { success: !1, error: ((u = d.result) == null ? void 0 : u.error) || d.error || "Login failed" };
4257
+ } catch (c) {
4257
4258
  return {
4258
4259
  success: !1,
4259
- error: u instanceof Error ? u.message : "Network error"
4260
+ error: c instanceof Error ? c.message : "Network error"
4260
4261
  };
4261
4262
  }
4262
4263
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qwanyx/stack",
3
- "version": "0.2.98",
3
+ "version": "0.2.100",
4
4
  "description": "Modern HyperCard for React - All-in-one data management (REST + Graph API + Auth + Hooks + UI)",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",