birdcash-chat-sdk-alpha 1.0.0 → 1.0.1

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.
package/dist/main.cjs.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e="https://chat-api2-3rnt.onrender.com",t="web_link";async function s(t){var s,n,r;const{clientId:o,clientSecret:i}=t;if(!o||!i)throw new Error("getAccessToken: clientId and clientSecret are required");const a=(null!==(s=t.baseUrl)&&void 0!==s?s:e).replace(/\/$/,""),l=null!==(n=t.scope)&&void 0!==n?n:"chat:write uploads:write",u=null!==(r=t.fetch)&&void 0!==r?r:"undefined"!=typeof fetch?fetch.bind(globalThis):void 0;if(!u)throw new Error("getAccessToken: no fetch implementation available; pass `fetch` in options");const p=await u(`${a}/v1/oauth/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({grant_type:"client_credentials",client_id:o,client_secret:i,scope:l})});if(!p.ok)throw new Error(`Token request failed: ${p.status} ${p.statusText}`);return await p.json()}var n;function r(e){return{elemType:exports.ElemType.Text,priority:0,textElem:{text:e.trim()}}}function o(e){return{elemType:exports.ElemType.Image,priority:0,imageElem:{upload_ids:e}}}function i(e){return{elemType:exports.ElemType.File,priority:0,fileElem:{upload_ids:e}}}function a(e,t=0){return{elemType:exports.ElemType.Sound,priority:0,soundElem:{uploadID:e,duration:t}}}function l(e){return{elemType:exports.ElemType.Custom,priority:0,customElem:{data:e}}}function u(e){return{elemType:exports.ElemType.MiniApp,priority:0,miniAppElem:e}}function p(e){const t=new Uint8Array(e.byteLength);return t.set(e),t}function c(e){const t=(new TextEncoder).encode(e);let s="";for(const e of t)s+=String.fromCharCode(e);return btoa(s)}function d(e,t=10){if(e.length<=t)return[e];const s=[];let n="";for(const r of e.split(" "))n.length+r.length+1>t&&n.length>0?(s.push(n.trim()+" "),n=r):n+=(n?" ":"")+r;return n&&s.push(n.trim()),s.length>0?s:[e]}exports.ElemType=void 0,(n=exports.ElemType||(exports.ElemType={}))[n.None=0]="None",n[n.Text=1]="Text",n[n.Image=2]="Image",n[n.Sound=3]="Sound",n[n.Video=4]="Video",n[n.File=5]="File",n[n.Sticker=6]="Sticker",n[n.GroupTips=7]="GroupTips",n[n.Merger=8]="Merger",n[n.Custom=9]="Custom",n[n.Location=10]="Location",n[n.GroupAnnouncement=11]="GroupAnnouncement",n[n.Quote=12]="Quote",n[n.InputStatus=14]="InputStatus",n[n.TypingStatus=15]="TypingStatus",n[n.MiniApp=16]="MiniApp",n[n.Order=17]="Order",n[n.Transfer=18]="Transfer";class h{constructor(t){var s,n;if(!(null==t?void 0:t.token))throw new Error("ChatClient: token is required");this.token=t.token,this.baseUrl=(null!==(s=t.baseUrl)&&void 0!==s?s:e).replace(/\/$/,""),this.logger=t.logger;const r=null!==(n=t.fetch)&&void 0!==n?n:"undefined"!=typeof fetch?fetch:void 0;if(!r)throw new Error("ChatClient: no fetch implementation available; pass `fetch` in options");this.fetchImpl=r.bind(globalThis)}static async fromCredentials(e){const{access_token:t}=await s({clientId:e.clientId,clientSecret:e.clientSecret,scope:e.scope,baseUrl:e.baseUrl,fetch:e.fetch});return new h({token:t,baseUrl:e.baseUrl,logger:e.logger,fetch:e.fetch})}get authHeaders(){return{"Content-Type":"application/json",Authorization:`Bearer ${this.token}`}}replyUrl(e){return`${this.baseUrl}/v1/chat/message/${e}/reply`}async postElements(e,t,s="POST"){return await this.fetchImpl(this.replyUrl(e),{method:s,headers:this.authHeaders,body:JSON.stringify(t)})}async uploadImage(e,t,s="image/png"){var n;const r=new FormData;r.append("file",new Blob([p(e)],{type:s}),t);const o=await this.fetchImpl(`${this.baseUrl}/v1/upload/image`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:r}),i=await o.json();if(!o.ok)throw new Error(`uploadImage failed: ${o.status} ${JSON.stringify(i)}`);if(!i.upload_id)throw new Error(`uploadImage: missing upload_id: ${JSON.stringify(i)}`);return{upload_id:i.upload_id,url:null!==(n=i.url)&&void 0!==n?n:""}}async uploadFile(e,t,s="application/octet-stream"){var n;const r=new FormData;r.append("file",new Blob([p(e)],{type:s}),t);const o=await this.fetchImpl(`${this.baseUrl}/v1/upload/file`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:r}),i=await o.json();if(!o.ok)throw new Error(`uploadFile failed: ${o.status} ${JSON.stringify(i)}`);if(!i.upload_id)throw new Error(`uploadFile: missing upload_id: ${JSON.stringify(i)}`);return{upload_id:i.upload_id,url:null!==(n=i.url)&&void 0!==n?n:""}}async sendMessage(e,t){var s;const n=Array.isArray(t)?t.map((e=>e.trim())).filter(Boolean).map(r):[r(t)];if(0===n.length)return;const o=await this.postElements(e,n),i=await o.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] message sent",{status:o.status,elements:n.length}),!o.ok)throw new Error(`sendMessage failed: ${o.status} ${i}`)}async sendImageMessage(e,t){var s;if(!(null==t?void 0:t.length))return;const n=await this.postElements(e,[o(t)]),r=await n.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] image message sent",{status:n.status}),!n.ok)throw new Error(`sendImageMessage failed: ${n.status} ${r}`)}async sendFileMessage(e,t){var s;if(!(null==t?void 0:t.length))return;const n=await this.postElements(e,[i(t)]),r=await n.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] file message sent",{status:n.status}),!n.ok)throw new Error(`sendFileMessage failed: ${n.status} ${r}`)}async sendSoundMessage(e,t,s=0){var n;if(!(null==t?void 0:t.trim()))return;const r=await this.postElements(e,[a(t,s)]),o=await r.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] sound message sent",{status:r.status}),!r.ok)throw new Error(`sendSoundMessage failed: ${r.status} ${o.slice(0,500)}`)}async sendTypingStatus(e,t){var s,n;const r={businessID:"user_typing_status",typingStatus:t?1:0,version:1,userAction:14,actionParam:t?"EIMAMSG_InputStatus_Ing":"EIMAMSG_InputStatus_End"};try{await this.postElements(e,[l(c(JSON.stringify(r)))])}catch(e){null===(s=this.logger)||void 0===s||s.error("[chat-sdk] typing status failed",null!==(n=null==e?void 0:e.message)&&void 0!==n?n:String(e))}}async sendStreamingChunk(e,t,s,n){const r={businessID:"chatbotPlugin",src:0,chunks:t,isFinished:s,TMessageCell_Name:"ChatbotMessageCell_Minimalist",TMessageCell_Data_Name:"ChatbotMessageCellData"},o={msgID:e,...l(c(JSON.stringify(r)))},i=await this.postElements(e,[o],n?"POST":"PATCH");if(!i.ok)throw new Error(`sendStreamingChunk failed: ${i.statusText}`);await i.text()}async sendMiniAppMessage(e,t){var s;const n=await this.postElements(e,[u(t)]),r=await n.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] miniapp card sent",{status:n.status}),!n.ok)throw new Error(`sendMiniAppMessage failed: ${n.status} ${r}`)}async sendWebLinkMessage(e,s){var n;const r={businessID:t,url:s},o=await this.postElements(e,[l(c(JSON.stringify(r)))]),i=await o.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] web link sent",{status:o.status}),!o.ok)throw new Error(`sendWebLinkMessage failed: ${o.status} ${i.slice(0,500)}`)}async streamMessage(e,t,s=10){const n=d(t,s),r=[];for(let t=0;t<n.length;t++)r.push(n[t]),await this.sendStreamingChunk(e,r,t===n.length-1,0===t)}}h.ElemType=exports.ElemType,exports.ChatClient=h,exports.DEFAULT_BASE_URL=e,exports.WEB_LINK_BUSINESS_ID=t,exports.arrayBufferToBase64=function(e){let t="";const s=new Uint8Array(e),n=s.byteLength;for(let e=0;e<n;e++)t+=String.fromCharCode(s[e]);return btoa(t)},exports.base64ToArrayBuffer=function(e){const t=atob(e),s=t.length,n=new Uint8Array(s);for(let e=0;e<s;e++)n[e]=t.charCodeAt(e);return n.buffer},exports.customElem=l,exports.fileElem=i,exports.getAccessToken=s,exports.imageElem=o,exports.miniAppElem=u,exports.soundElem=a,exports.splitIntoChunks=d,exports.textElem=r,exports.toBase64=c;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e="https://chat-api2-3rnt.onrender.com",t="web_link";async function s(t){var s;const{clientId:n,clientSecret:r}=t;if(!n||!r)throw new Error("getAccessToken: clientId and clientSecret are required");const o=e.replace(/\/$/,""),i=null!==(s=t.scope)&&void 0!==s?s:"chat:write uploads:write";if("undefined"==typeof fetch)throw new Error("getAccessToken: global fetch is not available");const a=await fetch(`${o}/v1/oauth/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({grant_type:"client_credentials",client_id:n,client_secret:r,scope:i})});if(!a.ok)throw new Error(`Token request failed: ${a.status} ${a.statusText}`);return await a.json()}var n;function r(e){return{elemType:exports.ElemType.Text,priority:0,textElem:{text:e.trim()}}}function o(e){return{elemType:exports.ElemType.Image,priority:0,imageElem:{upload_ids:e}}}function i(e){return{elemType:exports.ElemType.File,priority:0,fileElem:{upload_ids:e}}}function a(e,t=0){return{elemType:exports.ElemType.Sound,priority:0,soundElem:{uploadID:e,duration:t}}}function l(e){return{elemType:exports.ElemType.Custom,priority:0,customElem:{data:e}}}function u(e){return{elemType:exports.ElemType.MiniApp,priority:0,miniAppElem:e}}function p(e){const t=new Uint8Array(e.byteLength);return t.set(e),t}function c(e){const t=(new TextEncoder).encode(e);let s="";for(const e of t)s+=String.fromCharCode(e);return btoa(s)}function d(e,t=10){if(e.length<=t)return[e];const s=[];let n="";for(const r of e.split(" "))n.length+r.length+1>t&&n.length>0?(s.push(n.trim()+" "),n=r):n+=(n?" ":"")+r;return n&&s.push(n.trim()),s.length>0?s:[e]}exports.ElemType=void 0,(n=exports.ElemType||(exports.ElemType={}))[n.None=0]="None",n[n.Text=1]="Text",n[n.Image=2]="Image",n[n.Sound=3]="Sound",n[n.Video=4]="Video",n[n.File=5]="File",n[n.Sticker=6]="Sticker",n[n.GroupTips=7]="GroupTips",n[n.Merger=8]="Merger",n[n.Custom=9]="Custom",n[n.Location=10]="Location",n[n.GroupAnnouncement=11]="GroupAnnouncement",n[n.Quote=12]="Quote",n[n.InputStatus=14]="InputStatus",n[n.TypingStatus=15]="TypingStatus",n[n.MiniApp=16]="MiniApp",n[n.Order=17]="Order",n[n.Transfer=18]="Transfer";class h{constructor(t){var s,n;if(!(null==t?void 0:t.token))throw new Error("ChatClient: token is required");this.token=t.token,this.baseUrl=(null!==(s=t.baseUrl)&&void 0!==s?s:e).replace(/\/$/,""),this.logger=t.logger;const r=null!==(n=t.fetch)&&void 0!==n?n:"undefined"!=typeof fetch?fetch:void 0;if(!r)throw new Error("ChatClient: no fetch implementation available; pass `fetch` in options");this.fetchImpl=r.bind(globalThis)}static async fromCredentials(e){const{access_token:t}=await s({clientId:e.clientId,clientSecret:e.clientSecret,scope:e.scope});return new h({token:t,baseUrl:e.baseUrl,logger:e.logger,fetch:e.fetch})}get authHeaders(){return{"Content-Type":"application/json",Authorization:`Bearer ${this.token}`}}replyUrl(e){return`${this.baseUrl}/v1/chat/message/${e}/reply`}async postElements(e,t,s="POST"){return await this.fetchImpl(this.replyUrl(e),{method:s,headers:this.authHeaders,body:JSON.stringify(t)})}async uploadImage(e,t,s="image/png"){var n;const r=new FormData;r.append("file",new Blob([p(e)],{type:s}),t);const o=await this.fetchImpl(`${this.baseUrl}/v1/upload/image`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:r}),i=await o.json();if(!o.ok)throw new Error(`uploadImage failed: ${o.status} ${JSON.stringify(i)}`);if(!i.upload_id)throw new Error(`uploadImage: missing upload_id: ${JSON.stringify(i)}`);return{upload_id:i.upload_id,url:null!==(n=i.url)&&void 0!==n?n:""}}async uploadFile(e,t,s="application/octet-stream"){var n;const r=new FormData;r.append("file",new Blob([p(e)],{type:s}),t);const o=await this.fetchImpl(`${this.baseUrl}/v1/upload/file`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:r}),i=await o.json();if(!o.ok)throw new Error(`uploadFile failed: ${o.status} ${JSON.stringify(i)}`);if(!i.upload_id)throw new Error(`uploadFile: missing upload_id: ${JSON.stringify(i)}`);return{upload_id:i.upload_id,url:null!==(n=i.url)&&void 0!==n?n:""}}async sendMessage(e,t){var s;const n=Array.isArray(t)?t.map((e=>e.trim())).filter(Boolean).map(r):[r(t)];if(0===n.length)return;const o=await this.postElements(e,n),i=await o.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] message sent",{status:o.status,elements:n.length}),!o.ok)throw new Error(`sendMessage failed: ${o.status} ${i}`)}async sendImageMessage(e,t){var s;if(!(null==t?void 0:t.length))return;const n=await this.postElements(e,[o(t)]),r=await n.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] image message sent",{status:n.status}),!n.ok)throw new Error(`sendImageMessage failed: ${n.status} ${r}`)}async sendFileMessage(e,t){var s;if(!(null==t?void 0:t.length))return;const n=await this.postElements(e,[i(t)]),r=await n.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] file message sent",{status:n.status}),!n.ok)throw new Error(`sendFileMessage failed: ${n.status} ${r}`)}async sendSoundMessage(e,t,s=0){var n;if(!(null==t?void 0:t.trim()))return;const r=await this.postElements(e,[a(t,s)]),o=await r.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] sound message sent",{status:r.status}),!r.ok)throw new Error(`sendSoundMessage failed: ${r.status} ${o.slice(0,500)}`)}async sendTypingStatus(e,t){var s,n;const r={businessID:"user_typing_status",typingStatus:t?1:0,version:1,userAction:14,actionParam:t?"EIMAMSG_InputStatus_Ing":"EIMAMSG_InputStatus_End"};try{await this.postElements(e,[l(c(JSON.stringify(r)))])}catch(e){null===(s=this.logger)||void 0===s||s.error("[chat-sdk] typing status failed",null!==(n=null==e?void 0:e.message)&&void 0!==n?n:String(e))}}async sendStreamingChunk(e,t,s,n){const r={businessID:"chatbotPlugin",src:0,chunks:t,isFinished:s,TMessageCell_Name:"ChatbotMessageCell_Minimalist",TMessageCell_Data_Name:"ChatbotMessageCellData"},o={msgID:e,...l(c(JSON.stringify(r)))},i=await this.postElements(e,[o],n?"POST":"PATCH");if(!i.ok)throw new Error(`sendStreamingChunk failed: ${i.statusText}`);await i.text()}async sendMiniAppMessage(e,t){var s;const n=await this.postElements(e,[u(t)]),r=await n.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] miniapp card sent",{status:n.status}),!n.ok)throw new Error(`sendMiniAppMessage failed: ${n.status} ${r}`)}async sendWebLinkMessage(e,s){var n;const r={businessID:t,url:s},o=await this.postElements(e,[l(c(JSON.stringify(r)))]),i=await o.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] web link sent",{status:o.status}),!o.ok)throw new Error(`sendWebLinkMessage failed: ${o.status} ${i.slice(0,500)}`)}async streamMessage(e,t,s=10){const n=d(t,s),r=[];for(let t=0;t<n.length;t++)r.push(n[t]),await this.sendStreamingChunk(e,r,t===n.length-1,0===t)}}h.ElemType=exports.ElemType,exports.ChatClient=h,exports.DEFAULT_BASE_URL=e,exports.WEB_LINK_BUSINESS_ID=t,exports.arrayBufferToBase64=function(e){let t="";const s=new Uint8Array(e),n=s.byteLength;for(let e=0;e<n;e++)t+=String.fromCharCode(s[e]);return btoa(t)},exports.base64ToArrayBuffer=function(e){const t=atob(e),s=t.length,n=new Uint8Array(s);for(let e=0;e<s;e++)n[e]=t.charCodeAt(e);return n.buffer},exports.customElem=l,exports.fileElem=i,exports.getAccessToken=s,exports.imageElem=o,exports.miniAppElem=u,exports.soundElem=a,exports.splitIntoChunks=d,exports.textElem=r,exports.toBase64=c;
2
2
  //# sourceMappingURL=main.cjs.js.map
package/dist/main.esm.js CHANGED
@@ -1,2 +1,2 @@
1
- const t="https://chat-api2-3rnt.onrender.com",e="web_link";async function n(e){var n,s,i;const{clientId:a,clientSecret:r}=e;if(!a||!r)throw new Error("getAccessToken: clientId and clientSecret are required");const o=(null!==(n=e.baseUrl)&&void 0!==n?n:t).replace(/\/$/,""),l=null!==(s=e.scope)&&void 0!==s?s:"chat:write uploads:write",u=null!==(i=e.fetch)&&void 0!==i?i:"undefined"!=typeof fetch?fetch.bind(globalThis):void 0;if(!u)throw new Error("getAccessToken: no fetch implementation available; pass `fetch` in options");const c=await u(`${o}/v1/oauth/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({grant_type:"client_credentials",client_id:a,client_secret:r,scope:l})});if(!c.ok)throw new Error(`Token request failed: ${c.status} ${c.statusText}`);return await c.json()}var s;function i(t){return{elemType:s.Text,priority:0,textElem:{text:t.trim()}}}function a(t){return{elemType:s.Image,priority:0,imageElem:{upload_ids:t}}}function r(t){return{elemType:s.File,priority:0,fileElem:{upload_ids:t}}}function o(t,e=0){return{elemType:s.Sound,priority:0,soundElem:{uploadID:t,duration:e}}}function l(t){return{elemType:s.Custom,priority:0,customElem:{data:t}}}function u(t){return{elemType:s.MiniApp,priority:0,miniAppElem:t}}function c(t){const e=new Uint8Array(t.byteLength);return e.set(t),e}function d(t){let e="";const n=new Uint8Array(t),s=n.byteLength;for(let t=0;t<s;t++)e+=String.fromCharCode(n[t]);return btoa(e)}function h(t){const e=atob(t),n=e.length,s=new Uint8Array(n);for(let t=0;t<n;t++)s[t]=e.charCodeAt(t);return s.buffer}function p(t){const e=(new TextEncoder).encode(t);let n="";for(const t of e)n+=String.fromCharCode(t);return btoa(n)}function g(t,e=10){if(t.length<=e)return[t];const n=[];let s="";for(const i of t.split(" "))s.length+i.length+1>e&&s.length>0?(n.push(s.trim()+" "),s=i):s+=(s?" ":"")+i;return s&&n.push(s.trim()),n.length>0?n:[t]}!function(t){t[t.None=0]="None",t[t.Text=1]="Text",t[t.Image=2]="Image",t[t.Sound=3]="Sound",t[t.Video=4]="Video",t[t.File=5]="File",t[t.Sticker=6]="Sticker",t[t.GroupTips=7]="GroupTips",t[t.Merger=8]="Merger",t[t.Custom=9]="Custom",t[t.Location=10]="Location",t[t.GroupAnnouncement=11]="GroupAnnouncement",t[t.Quote=12]="Quote",t[t.InputStatus=14]="InputStatus",t[t.TypingStatus=15]="TypingStatus",t[t.MiniApp=16]="MiniApp",t[t.Order=17]="Order",t[t.Transfer=18]="Transfer"}(s||(s={}));class f{constructor(e){var n,s;if(!(null==e?void 0:e.token))throw new Error("ChatClient: token is required");this.token=e.token,this.baseUrl=(null!==(n=e.baseUrl)&&void 0!==n?n:t).replace(/\/$/,""),this.logger=e.logger;const i=null!==(s=e.fetch)&&void 0!==s?s:"undefined"!=typeof fetch?fetch:void 0;if(!i)throw new Error("ChatClient: no fetch implementation available; pass `fetch` in options");this.fetchImpl=i.bind(globalThis)}static async fromCredentials(t){const{access_token:e}=await n({clientId:t.clientId,clientSecret:t.clientSecret,scope:t.scope,baseUrl:t.baseUrl,fetch:t.fetch});return new f({token:e,baseUrl:t.baseUrl,logger:t.logger,fetch:t.fetch})}get authHeaders(){return{"Content-Type":"application/json",Authorization:`Bearer ${this.token}`}}replyUrl(t){return`${this.baseUrl}/v1/chat/message/${t}/reply`}async postElements(t,e,n="POST"){return await this.fetchImpl(this.replyUrl(t),{method:n,headers:this.authHeaders,body:JSON.stringify(e)})}async uploadImage(t,e,n="image/png"){var s;const i=new FormData;i.append("file",new Blob([c(t)],{type:n}),e);const a=await this.fetchImpl(`${this.baseUrl}/v1/upload/image`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:i}),r=await a.json();if(!a.ok)throw new Error(`uploadImage failed: ${a.status} ${JSON.stringify(r)}`);if(!r.upload_id)throw new Error(`uploadImage: missing upload_id: ${JSON.stringify(r)}`);return{upload_id:r.upload_id,url:null!==(s=r.url)&&void 0!==s?s:""}}async uploadFile(t,e,n="application/octet-stream"){var s;const i=new FormData;i.append("file",new Blob([c(t)],{type:n}),e);const a=await this.fetchImpl(`${this.baseUrl}/v1/upload/file`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:i}),r=await a.json();if(!a.ok)throw new Error(`uploadFile failed: ${a.status} ${JSON.stringify(r)}`);if(!r.upload_id)throw new Error(`uploadFile: missing upload_id: ${JSON.stringify(r)}`);return{upload_id:r.upload_id,url:null!==(s=r.url)&&void 0!==s?s:""}}async sendMessage(t,e){var n;const s=Array.isArray(e)?e.map((t=>t.trim())).filter(Boolean).map(i):[i(e)];if(0===s.length)return;const a=await this.postElements(t,s),r=await a.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] message sent",{status:a.status,elements:s.length}),!a.ok)throw new Error(`sendMessage failed: ${a.status} ${r}`)}async sendImageMessage(t,e){var n;if(!(null==e?void 0:e.length))return;const s=await this.postElements(t,[a(e)]),i=await s.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] image message sent",{status:s.status}),!s.ok)throw new Error(`sendImageMessage failed: ${s.status} ${i}`)}async sendFileMessage(t,e){var n;if(!(null==e?void 0:e.length))return;const s=await this.postElements(t,[r(e)]),i=await s.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] file message sent",{status:s.status}),!s.ok)throw new Error(`sendFileMessage failed: ${s.status} ${i}`)}async sendSoundMessage(t,e,n=0){var s;if(!(null==e?void 0:e.trim()))return;const i=await this.postElements(t,[o(e,n)]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] sound message sent",{status:i.status}),!i.ok)throw new Error(`sendSoundMessage failed: ${i.status} ${a.slice(0,500)}`)}async sendTypingStatus(t,e){var n,s;const i={businessID:"user_typing_status",typingStatus:e?1:0,version:1,userAction:14,actionParam:e?"EIMAMSG_InputStatus_Ing":"EIMAMSG_InputStatus_End"};try{await this.postElements(t,[l(p(JSON.stringify(i)))])}catch(t){null===(n=this.logger)||void 0===n||n.error("[chat-sdk] typing status failed",null!==(s=null==t?void 0:t.message)&&void 0!==s?s:String(t))}}async sendStreamingChunk(t,e,n,s){const i={businessID:"chatbotPlugin",src:0,chunks:e,isFinished:n,TMessageCell_Name:"ChatbotMessageCell_Minimalist",TMessageCell_Data_Name:"ChatbotMessageCellData"},a={msgID:t,...l(p(JSON.stringify(i)))},r=await this.postElements(t,[a],s?"POST":"PATCH");if(!r.ok)throw new Error(`sendStreamingChunk failed: ${r.statusText}`);await r.text()}async sendMiniAppMessage(t,e){var n;const s=await this.postElements(t,[u(e)]),i=await s.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] miniapp card sent",{status:s.status}),!s.ok)throw new Error(`sendMiniAppMessage failed: ${s.status} ${i}`)}async sendWebLinkMessage(t,n){var s;const i={businessID:e,url:n},a=await this.postElements(t,[l(p(JSON.stringify(i)))]),r=await a.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] web link sent",{status:a.status}),!a.ok)throw new Error(`sendWebLinkMessage failed: ${a.status} ${r.slice(0,500)}`)}async streamMessage(t,e,n=10){const s=g(e,n),i=[];for(let e=0;e<s.length;e++)i.push(s[e]),await this.sendStreamingChunk(t,i,e===s.length-1,0===e)}}f.ElemType=s;export{f as ChatClient,t as DEFAULT_BASE_URL,s as ElemType,e as WEB_LINK_BUSINESS_ID,d as arrayBufferToBase64,h as base64ToArrayBuffer,l as customElem,r as fileElem,n as getAccessToken,a as imageElem,u as miniAppElem,o as soundElem,g as splitIntoChunks,i as textElem,p as toBase64};
1
+ const t="https://chat-api2-3rnt.onrender.com",e="web_link";async function n(e){var n;const{clientId:s,clientSecret:i}=e;if(!s||!i)throw new Error("getAccessToken: clientId and clientSecret are required");const a=t.replace(/\/$/,""),r=null!==(n=e.scope)&&void 0!==n?n:"chat:write uploads:write";if("undefined"==typeof fetch)throw new Error("getAccessToken: global fetch is not available");const o=await fetch(`${a}/v1/oauth/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({grant_type:"client_credentials",client_id:s,client_secret:i,scope:r})});if(!o.ok)throw new Error(`Token request failed: ${o.status} ${o.statusText}`);return await o.json()}var s;function i(t){return{elemType:s.Text,priority:0,textElem:{text:t.trim()}}}function a(t){return{elemType:s.Image,priority:0,imageElem:{upload_ids:t}}}function r(t){return{elemType:s.File,priority:0,fileElem:{upload_ids:t}}}function o(t,e=0){return{elemType:s.Sound,priority:0,soundElem:{uploadID:t,duration:e}}}function l(t){return{elemType:s.Custom,priority:0,customElem:{data:t}}}function u(t){return{elemType:s.MiniApp,priority:0,miniAppElem:t}}function c(t){const e=new Uint8Array(t.byteLength);return e.set(t),e}function d(t){let e="";const n=new Uint8Array(t),s=n.byteLength;for(let t=0;t<s;t++)e+=String.fromCharCode(n[t]);return btoa(e)}function h(t){const e=atob(t),n=e.length,s=new Uint8Array(n);for(let t=0;t<n;t++)s[t]=e.charCodeAt(t);return s.buffer}function p(t){const e=(new TextEncoder).encode(t);let n="";for(const t of e)n+=String.fromCharCode(t);return btoa(n)}function g(t,e=10){if(t.length<=e)return[t];const n=[];let s="";for(const i of t.split(" "))s.length+i.length+1>e&&s.length>0?(n.push(s.trim()+" "),s=i):s+=(s?" ":"")+i;return s&&n.push(s.trim()),n.length>0?n:[t]}!function(t){t[t.None=0]="None",t[t.Text=1]="Text",t[t.Image=2]="Image",t[t.Sound=3]="Sound",t[t.Video=4]="Video",t[t.File=5]="File",t[t.Sticker=6]="Sticker",t[t.GroupTips=7]="GroupTips",t[t.Merger=8]="Merger",t[t.Custom=9]="Custom",t[t.Location=10]="Location",t[t.GroupAnnouncement=11]="GroupAnnouncement",t[t.Quote=12]="Quote",t[t.InputStatus=14]="InputStatus",t[t.TypingStatus=15]="TypingStatus",t[t.MiniApp=16]="MiniApp",t[t.Order=17]="Order",t[t.Transfer=18]="Transfer"}(s||(s={}));class f{constructor(e){var n,s;if(!(null==e?void 0:e.token))throw new Error("ChatClient: token is required");this.token=e.token,this.baseUrl=(null!==(n=e.baseUrl)&&void 0!==n?n:t).replace(/\/$/,""),this.logger=e.logger;const i=null!==(s=e.fetch)&&void 0!==s?s:"undefined"!=typeof fetch?fetch:void 0;if(!i)throw new Error("ChatClient: no fetch implementation available; pass `fetch` in options");this.fetchImpl=i.bind(globalThis)}static async fromCredentials(t){const{access_token:e}=await n({clientId:t.clientId,clientSecret:t.clientSecret,scope:t.scope});return new f({token:e,baseUrl:t.baseUrl,logger:t.logger,fetch:t.fetch})}get authHeaders(){return{"Content-Type":"application/json",Authorization:`Bearer ${this.token}`}}replyUrl(t){return`${this.baseUrl}/v1/chat/message/${t}/reply`}async postElements(t,e,n="POST"){return await this.fetchImpl(this.replyUrl(t),{method:n,headers:this.authHeaders,body:JSON.stringify(e)})}async uploadImage(t,e,n="image/png"){var s;const i=new FormData;i.append("file",new Blob([c(t)],{type:n}),e);const a=await this.fetchImpl(`${this.baseUrl}/v1/upload/image`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:i}),r=await a.json();if(!a.ok)throw new Error(`uploadImage failed: ${a.status} ${JSON.stringify(r)}`);if(!r.upload_id)throw new Error(`uploadImage: missing upload_id: ${JSON.stringify(r)}`);return{upload_id:r.upload_id,url:null!==(s=r.url)&&void 0!==s?s:""}}async uploadFile(t,e,n="application/octet-stream"){var s;const i=new FormData;i.append("file",new Blob([c(t)],{type:n}),e);const a=await this.fetchImpl(`${this.baseUrl}/v1/upload/file`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:i}),r=await a.json();if(!a.ok)throw new Error(`uploadFile failed: ${a.status} ${JSON.stringify(r)}`);if(!r.upload_id)throw new Error(`uploadFile: missing upload_id: ${JSON.stringify(r)}`);return{upload_id:r.upload_id,url:null!==(s=r.url)&&void 0!==s?s:""}}async sendMessage(t,e){var n;const s=Array.isArray(e)?e.map((t=>t.trim())).filter(Boolean).map(i):[i(e)];if(0===s.length)return;const a=await this.postElements(t,s),r=await a.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] message sent",{status:a.status,elements:s.length}),!a.ok)throw new Error(`sendMessage failed: ${a.status} ${r}`)}async sendImageMessage(t,e){var n;if(!(null==e?void 0:e.length))return;const s=await this.postElements(t,[a(e)]),i=await s.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] image message sent",{status:s.status}),!s.ok)throw new Error(`sendImageMessage failed: ${s.status} ${i}`)}async sendFileMessage(t,e){var n;if(!(null==e?void 0:e.length))return;const s=await this.postElements(t,[r(e)]),i=await s.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] file message sent",{status:s.status}),!s.ok)throw new Error(`sendFileMessage failed: ${s.status} ${i}`)}async sendSoundMessage(t,e,n=0){var s;if(!(null==e?void 0:e.trim()))return;const i=await this.postElements(t,[o(e,n)]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] sound message sent",{status:i.status}),!i.ok)throw new Error(`sendSoundMessage failed: ${i.status} ${a.slice(0,500)}`)}async sendTypingStatus(t,e){var n,s;const i={businessID:"user_typing_status",typingStatus:e?1:0,version:1,userAction:14,actionParam:e?"EIMAMSG_InputStatus_Ing":"EIMAMSG_InputStatus_End"};try{await this.postElements(t,[l(p(JSON.stringify(i)))])}catch(t){null===(n=this.logger)||void 0===n||n.error("[chat-sdk] typing status failed",null!==(s=null==t?void 0:t.message)&&void 0!==s?s:String(t))}}async sendStreamingChunk(t,e,n,s){const i={businessID:"chatbotPlugin",src:0,chunks:e,isFinished:n,TMessageCell_Name:"ChatbotMessageCell_Minimalist",TMessageCell_Data_Name:"ChatbotMessageCellData"},a={msgID:t,...l(p(JSON.stringify(i)))},r=await this.postElements(t,[a],s?"POST":"PATCH");if(!r.ok)throw new Error(`sendStreamingChunk failed: ${r.statusText}`);await r.text()}async sendMiniAppMessage(t,e){var n;const s=await this.postElements(t,[u(e)]),i=await s.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] miniapp card sent",{status:s.status}),!s.ok)throw new Error(`sendMiniAppMessage failed: ${s.status} ${i}`)}async sendWebLinkMessage(t,n){var s;const i={businessID:e,url:n},a=await this.postElements(t,[l(p(JSON.stringify(i)))]),r=await a.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] web link sent",{status:a.status}),!a.ok)throw new Error(`sendWebLinkMessage failed: ${a.status} ${r.slice(0,500)}`)}async streamMessage(t,e,n=10){const s=g(e,n),i=[];for(let e=0;e<s.length;e++)i.push(s[e]),await this.sendStreamingChunk(t,i,e===s.length-1,0===e)}}f.ElemType=s;export{f as ChatClient,t as DEFAULT_BASE_URL,s as ElemType,e as WEB_LINK_BUSINESS_ID,d as arrayBufferToBase64,h as base64ToArrayBuffer,l as customElem,r as fileElem,n as getAccessToken,a as imageElem,u as miniAppElem,o as soundElem,g as splitIntoChunks,i as textElem,p as toBase64};
2
2
  //# sourceMappingURL=main.esm.js.map
@@ -1,13 +1,9 @@
1
- import { TokenResponse } from './types';
1
+ import { TokenResponse } from "./types";
2
2
  export interface GetAccessTokenOptions {
3
3
  clientId: string;
4
4
  clientSecret: string;
5
5
  /** Space-separated scopes. Defaults to 'chat:write uploads:write'. */
6
6
  scope?: string;
7
- /** Base URL of the chat API. Defaults to the hosted endpoint. */
8
- baseUrl?: string;
9
- /** Optional custom fetch implementation. */
10
- fetch?: typeof fetch;
11
7
  }
12
8
  /**
13
9
  * Exchange OAuth client credentials for an access token
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "birdcash-chat-sdk-alpha",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "TypeScript SDK for sending messages through the chat API (text, images, files, miniapp cards, streaming).",
5
5
  "main": "dist/main.cjs.js",
6
6
  "module": "dist/main.esm.js",