birdcash-chat-sdk-alpha 1.0.2 → 1.0.3
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/README.md +36 -0
- package/dist/main.cjs.js +1 -1
- package/dist/main.esm.js +1 -1
- package/dist/types/client.d.ts +30 -4
- package/dist/types/constants.d.ts +4 -0
- package/dist/types/types.d.ts +27 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -70,6 +70,42 @@ await chat.sendMiniAppMessage(msgID, {
|
|
|
70
70
|
|
|
71
71
|
// Web link preview
|
|
72
72
|
await chat.sendWebLinkMessage(msgID, 'https://example.com')
|
|
73
|
+
|
|
74
|
+
// Choices prompt (ai_choice_prompt) — returns the new message id
|
|
75
|
+
const choiceMsgID = await chat.sendChoicesMessage(msgID, {
|
|
76
|
+
prompt: 'Which shipping option do you want?',
|
|
77
|
+
choices: [
|
|
78
|
+
{ id: 'standard', label: 'Standard (5–7 days)' },
|
|
79
|
+
{ id: 'express', label: 'Express (2 days)' },
|
|
80
|
+
],
|
|
81
|
+
// selectionMode: 'single', // optional, defaults to 'single'
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
// Official-account tweet card — returns the new message id
|
|
85
|
+
await chat.sendOfficialAccountTweetMessage(msgID, {
|
|
86
|
+
title: 'Release notes',
|
|
87
|
+
content: 'What shipped this week…',
|
|
88
|
+
link: 'https://example.com/blog',
|
|
89
|
+
version: 1,
|
|
90
|
+
})
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Editing & deleting
|
|
94
|
+
|
|
95
|
+
Send with a known `replyMsgID`, then edit that message in place (or delete it):
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const editable = crypto.randomUUID()
|
|
99
|
+
await chat.sendMessage(msgID, 'Working on it…', editable)
|
|
100
|
+
await chat.editTextMessage(editable, 'Done ✅')
|
|
101
|
+
|
|
102
|
+
// Stickers and images edit in place too
|
|
103
|
+
await chat.sendStickerMessage(msgID, 'capoo', 'capoo_1', editable)
|
|
104
|
+
await chat.editStickerMessage(editable, 'capoo', 'capoo_2')
|
|
105
|
+
await chat.editImageMessage(imgMsgID, [upload_id])
|
|
106
|
+
|
|
107
|
+
// Delete a message you sent
|
|
108
|
+
await chat.deleteMessage(editable)
|
|
73
109
|
```
|
|
74
110
|
|
|
75
111
|
## Streaming (typewriter effect)
|
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;const{clientId:n,clientSecret:r}=t;if(!n||!r)throw new Error("getAccessToken: clientId and clientSecret are required");const i=e.replace(/\/$/,""),o=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(`${i}/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:o})});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 i(e){return{elemType:exports.ElemType.Image,priority:0,imageElem:{upload_ids:e}}}function o(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,t){return{elemType:exports.ElemType.Sticker,priority:0,stickerElem:{packID:e,stickerID:t}}}function u(e){return{elemType:exports.ElemType.Custom,priority:0,customElem:{data:e}}}function p(e){return{elemType:exports.ElemType.MiniApp,priority:0,miniAppElem:e}}function c(e){const t=new Uint8Array(e.byteLength);return t.set(e),t}function d(e){const t=(new TextEncoder).encode(e);let s="";for(const e of t)s+=String.fromCharCode(e);return btoa(s)}function h(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 g{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 g({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([c(e)],{type:s}),t);const i=await this.fetchImpl(`${this.baseUrl}/v1/upload/image`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:r}),o=await i.json();if(!i.ok)throw new Error(`uploadImage failed: ${i.status} ${JSON.stringify(o)}`);if(!o.upload_id)throw new Error(`uploadImage: missing upload_id: ${JSON.stringify(o)}`);return{upload_id:o.upload_id,url:null!==(n=o.url)&&void 0!==n?n:""}}async uploadFile(e,t,s="application/octet-stream"){var n;const r=new FormData;r.append("file",new Blob([c(e)],{type:s}),t);const i=await this.fetchImpl(`${this.baseUrl}/v1/upload/file`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:r}),o=await i.json();if(!i.ok)throw new Error(`uploadFile failed: ${i.status} ${JSON.stringify(o)}`);if(!o.upload_id)throw new Error(`uploadFile: missing upload_id: ${JSON.stringify(o)}`);return{upload_id:o.upload_id,url:null!==(n=o.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 i=await this.postElements(e,n),o=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] message sent",{status:i.status,elements:n.length}),!i.ok)throw new Error(`sendMessage failed: ${i.status} ${o}`)}async sendImageMessage(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] 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,[o(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)]),i=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} ${i.slice(0,500)}`)}async sendStickerMessage(e,t,s){var n;if(!(null==t?void 0:t.trim())||!(null==s?void 0:s.trim()))return;const r=await this.postElements(e,[l(t,s)]),i=await r.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] sticker message sent",{status:r.status}),!r.ok)throw new Error(`sendStickerMessage failed: ${r.status} ${i}`)}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,[u(d(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"},i={msgID:e,...u(d(JSON.stringify(r)))},o=await this.postElements(e,[i],n?"POST":"PATCH");if(!o.ok)throw new Error(`sendStreamingChunk failed: ${o.statusText}`);await o.text()}async sendMiniAppMessage(e,t){var s;const n=await this.postElements(e,[p(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},i=await this.postElements(e,[u(d(JSON.stringify(r)))]),o=await i.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] web link sent",{status:i.status}),!i.ok)throw new Error(`sendWebLinkMessage failed: ${i.status} ${o.slice(0,500)}`)}async streamMessage(e,t,s=10){const n=h(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)}}g.ElemType=exports.ElemType,exports.ChatClient=g,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=u,exports.fileElem=o,exports.getAccessToken=s,exports.imageElem=i,exports.miniAppElem=p,exports.soundElem=a,exports.splitIntoChunks=h,exports.stickerElem=l,exports.textElem=r,exports.toBase64=d;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e="https://chat-api2-3rnt.onrender.com",t="web_link",s="ai_choice_prompt",i="official_account_tweet";async function a(t){var s;const{clientId:i,clientSecret:a}=t;if(!i||!a)throw new Error("getAccessToken: clientId and clientSecret are required");const n=e.replace(/\/$/,""),r=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 o=await fetch(`${n}/v1/oauth/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({grant_type:"client_credentials",client_id:i,client_secret:a,scope:r})});if(!o.ok)throw new Error(`Token request failed: ${o.status} ${o.statusText}`);return await o.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 l(e){return{elemType:exports.ElemType.File,priority:0,fileElem:{upload_ids:e}}}function u(e,t=0){return{elemType:exports.ElemType.Sound,priority:0,soundElem:{uploadID:e,duration:t}}}function c(e,t){return{elemType:exports.ElemType.Sticker,priority:0,stickerElem:{packID:e,stickerID:t}}}function d(e){return{elemType:exports.ElemType.Custom,priority:0,customElem:{data:e}}}function p(e){return{elemType:exports.ElemType.MiniApp,priority:0,miniAppElem:e}}function g(e){const t=new Uint8Array(e.byteLength);return t.set(e),t}function h(e){const t=(new TextEncoder).encode(e);let s="";for(const e of t)s+=String.fromCharCode(e);return btoa(s)}function m(e,t=10){if(e.length<=t)return[e];const s=[];let i="";for(const a of e.split(" "))i.length+a.length+1>t&&i.length>0?(s.push(i.trim()+" "),i=a):i+=(i?" ":"")+a;return i&&s.push(i.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 f{constructor(t){var s,i;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 a=null!==(i=t.fetch)&&void 0!==i?i:"undefined"!=typeof fetch?fetch:void 0;if(!a)throw new Error("ChatClient: no fetch implementation available; pass `fetch` in options");this.fetchImpl=a.bind(globalThis)}static async fromCredentials(e){const{access_token:t}=await a({clientId:e.clientId,clientSecret:e.clientSecret,scope:e.scope});return new f({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`}newMsgID(){return globalThis.crypto.randomUUID()}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 i;const a=new FormData;a.append("file",new Blob([g(e)],{type:s}),t);const n=await this.fetchImpl(`${this.baseUrl}/v1/upload/image`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:a}),r=await n.json();if(!n.ok)throw new Error(`uploadImage failed: ${n.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!==(i=r.url)&&void 0!==i?i:""}}async uploadFile(e,t,s="application/octet-stream"){var i;const a=new FormData;a.append("file",new Blob([g(e)],{type:s}),t);const n=await this.fetchImpl(`${this.baseUrl}/v1/upload/file`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:a}),r=await n.json();if(!n.ok)throw new Error(`uploadFile failed: ${n.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!==(i=r.url)&&void 0!==i?i:""}}async sendMessage(e,t,s){var i;let a=Array.isArray(t)?t.map((e=>e.trim())).filter(Boolean).map(r):[r(t)];if(0===a.length)return;s&&(a=a.map((e=>({...e,msgID:s}))));const n=await this.postElements(e,a),o=await n.text();if(null===(i=this.logger)||void 0===i||i.log("[chat-sdk] message sent",{status:n.status,elements:a.length}),!n.ok)throw new Error(`sendMessage failed: ${n.status} ${o}`)}async sendImageMessage(e,t){var s;if(!(null==t?void 0:t.length))return;const i=await this.postElements(e,[o(t)]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] image message sent",{status:i.status}),!i.ok)throw new Error(`sendImageMessage failed: ${i.status} ${a}`)}async sendFileMessage(e,t){var s;if(!(null==t?void 0:t.length))return;const i=await this.postElements(e,[l(t)]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] file message sent",{status:i.status}),!i.ok)throw new Error(`sendFileMessage failed: ${i.status} ${a}`)}async sendSoundMessage(e,t,s=0){var i;if(!(null==t?void 0:t.trim()))return;const a=await this.postElements(e,[u(t,s)]),n=await a.text();if(null===(i=this.logger)||void 0===i||i.log("[chat-sdk] sound message sent",{status:a.status}),!a.ok)throw new Error(`sendSoundMessage failed: ${a.status} ${n.slice(0,500)}`)}async sendStickerMessage(e,t,s,i){var a;if(!(null==t?void 0:t.trim())||!(null==s?void 0:s.trim()))return;const n=c(t,s),r=await this.postElements(e,[i?{...n,msgID:i}:n]),o=await r.text();if(null===(a=this.logger)||void 0===a||a.log("[chat-sdk] sticker message sent",{status:r.status}),!r.ok)throw new Error(`sendStickerMessage failed: ${r.status} ${o}`)}async editTextMessage(e,t){var s;const i=await this.postElements(e,[{msgID:e,...r(t)}]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] text message edited",{status:i.status}),!i.ok)throw new Error(`editTextMessage failed: ${i.status} ${a.slice(0,500)}`)}async editStickerMessage(e,t,s){var i;const a=await this.postElements(e,[{msgID:e,...c(t,s)}]),n=await a.text();if(null===(i=this.logger)||void 0===i||i.log("[chat-sdk] sticker message edited",{status:a.status}),!a.ok)throw new Error(`editStickerMessage failed: ${a.status} ${n.slice(0,500)}`)}async editImageMessage(e,t){var s;const i=await this.postElements(e,[{msgID:e,...o(t)}]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] image message edited",{status:i.status}),!i.ok)throw new Error(`editImageMessage failed: ${i.status} ${a.slice(0,500)}`)}async deleteMessage(e){var t;const s=await this.fetchImpl(`${this.baseUrl}/v1/chat/message/${e}`,{method:"DELETE",headers:this.authHeaders});if(null===(t=this.logger)||void 0===t||t.log("[chat-sdk] message deleted",{status:s.status}),!s.ok){const e=await s.text();throw new Error(`deleteMessage failed: ${s.status} ${e.slice(0,500)}`)}}async sendTypingStatus(e,t){var s,i;const a={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,[d(h(JSON.stringify(a)))])}catch(e){null===(s=this.logger)||void 0===s||s.error("[chat-sdk] typing status failed",null!==(i=null==e?void 0:e.message)&&void 0!==i?i:String(e))}}async sendStreamingChunk(e,t,s,i){const a={businessID:"chatbotPlugin",src:0,chunks:t,isFinished:s,TMessageCell_Name:"ChatbotMessageCell_Minimalist",TMessageCell_Data_Name:"ChatbotMessageCellData"},n={msgID:e,...d(h(JSON.stringify(a)))},r=await this.postElements(e,[n],i?"POST":"PATCH");if(!r.ok)throw new Error(`sendStreamingChunk failed: ${r.statusText}`);await r.text()}async sendMiniAppMessage(e,t){var s;const i=await this.postElements(e,[p(t)]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] miniapp card sent",{status:i.status}),!i.ok)throw new Error(`sendMiniAppMessage failed: ${i.status} ${a}`)}async sendWebLinkMessage(e,s){var i;const a={businessID:t,url:s},n=await this.postElements(e,[d(h(JSON.stringify(a)))]),r=await n.text();if(null===(i=this.logger)||void 0===i||i.log("[chat-sdk] web link sent",{status:n.status}),!n.ok)throw new Error(`sendWebLinkMessage failed: ${n.status} ${r.slice(0,500)}`)}async sendChoicesMessage(e,t){var i,a,n;const r=this.newMsgID(),o={businessID:s,promptId:null!==(i=t.promptId)&&void 0!==i?i:this.newMsgID(),prompt:t.prompt,selectionMode:null!==(a=t.selectionMode)&&void 0!==a?a:"single",choices:t.choices},l=await this.postElements(e,[{msgID:r,...d(h(JSON.stringify(o)))}]),u=await l.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] choices message sent",{status:l.status}),!l.ok)throw new Error(`sendChoicesMessage failed: ${l.status} ${u.slice(0,500)}`);return r}async sendOfficialAccountTweetMessage(e,t){var s;const a=this.newMsgID(),n={businessID:i,...t},r=await this.postElements(e,[{msgID:a,...d(h(JSON.stringify(n)))}]),o=await r.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] official account tweet sent",{status:r.status}),!r.ok)throw new Error(`sendOfficialAccountTweetMessage failed: ${r.status} ${o.slice(0,500)}`);return a}async streamMessage(e,t,s=10){const i=m(t,s),a=[];for(let t=0;t<i.length;t++)a.push(i[t]),await this.sendStreamingChunk(e,a,t===i.length-1,0===t)}}f.ElemType=exports.ElemType,exports.AI_CHOICE_PROMPT_BUSINESS_ID=s,exports.ChatClient=f,exports.DEFAULT_BASE_URL=e,exports.OFFICIAL_ACCOUNT_TWEET_BUSINESS_ID=i,exports.WEB_LINK_BUSINESS_ID=t,exports.arrayBufferToBase64=function(e){let t="";const s=new Uint8Array(e),i=s.byteLength;for(let e=0;e<i;e++)t+=String.fromCharCode(s[e]);return btoa(t)},exports.base64ToArrayBuffer=function(e){const t=atob(e),s=t.length,i=new Uint8Array(s);for(let e=0;e<s;e++)i[e]=t.charCodeAt(e);return i.buffer},exports.customElem=d,exports.fileElem=l,exports.getAccessToken=a,exports.imageElem=o,exports.miniAppElem=p,exports.soundElem=u,exports.splitIntoChunks=m,exports.stickerElem=c,exports.textElem=r,exports.toBase64=h;
|
|
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 s(e){var s;const{clientId:n,clientSecret:i}=e;if(!n||!i)throw new Error("getAccessToken: clientId and clientSecret are required");const r=t.replace(/\/$/,""),a=null!==(s=e.scope)&&void 0!==s?s:"chat:write uploads:write";if("undefined"==typeof fetch)throw new Error("getAccessToken: global fetch is not available");const o=await fetch(`${r}/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:i,scope:a})});if(!o.ok)throw new Error(`Token request failed: ${o.status} ${o.statusText}`);return await o.json()}var n;function i(t){return{elemType:n.Text,priority:0,textElem:{text:t.trim()}}}function r(t){return{elemType:n.Image,priority:0,imageElem:{upload_ids:t}}}function a(t){return{elemType:n.File,priority:0,fileElem:{upload_ids:t}}}function o(t,e=0){return{elemType:n.Sound,priority:0,soundElem:{uploadID:t,duration:e}}}function l(t,e){return{elemType:n.Sticker,priority:0,stickerElem:{packID:t,stickerID:e}}}function u(t){return{elemType:n.Custom,priority:0,customElem:{data:t}}}function c(t){return{elemType:n.MiniApp,priority:0,miniAppElem:t}}function d(t){const e=new Uint8Array(t.byteLength);return e.set(t),e}function h(t){let e="";const s=new Uint8Array(t),n=s.byteLength;for(let t=0;t<n;t++)e+=String.fromCharCode(s[t]);return btoa(e)}function p(t){const e=atob(t),s=e.length,n=new Uint8Array(s);for(let t=0;t<s;t++)n[t]=e.charCodeAt(t);return n.buffer}function g(t){const e=(new TextEncoder).encode(t);let s="";for(const t of e)s+=String.fromCharCode(t);return btoa(s)}function f(t,e=10){if(t.length<=e)return[t];const s=[];let n="";for(const i of t.split(" "))n.length+i.length+1>e&&n.length>0?(s.push(n.trim()+" "),n=i):n+=(n?" ":"")+i;return n&&s.push(n.trim()),s.length>0?s:[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"}(n||(n={}));class m{constructor(e){var s,n;if(!(null==e?void 0:e.token))throw new Error("ChatClient: token is required");this.token=e.token,this.baseUrl=(null!==(s=e.baseUrl)&&void 0!==s?s:t).replace(/\/$/,""),this.logger=e.logger;const i=null!==(n=e.fetch)&&void 0!==n?n:"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 s({clientId:t.clientId,clientSecret:t.clientSecret,scope:t.scope});return new m({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,s="POST"){return await this.fetchImpl(this.replyUrl(t),{method:s,headers:this.authHeaders,body:JSON.stringify(e)})}async uploadImage(t,e,s="image/png"){var n;const i=new FormData;i.append("file",new Blob([d(t)],{type:s}),e);const r=await this.fetchImpl(`${this.baseUrl}/v1/upload/image`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:i}),a=await r.json();if(!r.ok)throw new Error(`uploadImage failed: ${r.status} ${JSON.stringify(a)}`);if(!a.upload_id)throw new Error(`uploadImage: missing upload_id: ${JSON.stringify(a)}`);return{upload_id:a.upload_id,url:null!==(n=a.url)&&void 0!==n?n:""}}async uploadFile(t,e,s="application/octet-stream"){var n;const i=new FormData;i.append("file",new Blob([d(t)],{type:s}),e);const r=await this.fetchImpl(`${this.baseUrl}/v1/upload/file`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:i}),a=await r.json();if(!r.ok)throw new Error(`uploadFile failed: ${r.status} ${JSON.stringify(a)}`);if(!a.upload_id)throw new Error(`uploadFile: missing upload_id: ${JSON.stringify(a)}`);return{upload_id:a.upload_id,url:null!==(n=a.url)&&void 0!==n?n:""}}async sendMessage(t,e){var s;const n=Array.isArray(e)?e.map((t=>t.trim())).filter(Boolean).map(i):[i(e)];if(0===n.length)return;const r=await this.postElements(t,n),a=await r.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] message sent",{status:r.status,elements:n.length}),!r.ok)throw new Error(`sendMessage failed: ${r.status} ${a}`)}async sendImageMessage(t,e){var s;if(!(null==e?void 0:e.length))return;const n=await this.postElements(t,[r(e)]),i=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} ${i}`)}async sendFileMessage(t,e){var s;if(!(null==e?void 0:e.length))return;const n=await this.postElements(t,[a(e)]),i=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} ${i}`)}async sendSoundMessage(t,e,s=0){var n;if(!(null==e?void 0:e.trim()))return;const i=await this.postElements(t,[o(e,s)]),r=await i.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] sound message sent",{status:i.status}),!i.ok)throw new Error(`sendSoundMessage failed: ${i.status} ${r.slice(0,500)}`)}async sendStickerMessage(t,e,s){var n;if(!(null==e?void 0:e.trim())||!(null==s?void 0:s.trim()))return;const i=await this.postElements(t,[l(e,s)]),r=await i.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] sticker message sent",{status:i.status}),!i.ok)throw new Error(`sendStickerMessage failed: ${i.status} ${r}`)}async sendTypingStatus(t,e){var s,n;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,[u(g(JSON.stringify(i)))])}catch(t){null===(s=this.logger)||void 0===s||s.error("[chat-sdk] typing status failed",null!==(n=null==t?void 0:t.message)&&void 0!==n?n:String(t))}}async sendStreamingChunk(t,e,s,n){const i={businessID:"chatbotPlugin",src:0,chunks:e,isFinished:s,TMessageCell_Name:"ChatbotMessageCell_Minimalist",TMessageCell_Data_Name:"ChatbotMessageCellData"},r={msgID:t,...u(g(JSON.stringify(i)))},a=await this.postElements(t,[r],n?"POST":"PATCH");if(!a.ok)throw new Error(`sendStreamingChunk failed: ${a.statusText}`);await a.text()}async sendMiniAppMessage(t,e){var s;const n=await this.postElements(t,[c(e)]),i=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} ${i}`)}async sendWebLinkMessage(t,s){var n;const i={businessID:e,url:s},r=await this.postElements(t,[u(g(JSON.stringify(i)))]),a=await r.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] web link sent",{status:r.status}),!r.ok)throw new Error(`sendWebLinkMessage failed: ${r.status} ${a.slice(0,500)}`)}async streamMessage(t,e,s=10){const n=f(e,s),i=[];for(let e=0;e<n.length;e++)i.push(n[e]),await this.sendStreamingChunk(t,i,e===n.length-1,0===e)}}m.ElemType=n;export{m as ChatClient,t as DEFAULT_BASE_URL,n as ElemType,e as WEB_LINK_BUSINESS_ID,h as arrayBufferToBase64,p as base64ToArrayBuffer,u as customElem,a as fileElem,s as getAccessToken,r as imageElem,c as miniAppElem,o as soundElem,f as splitIntoChunks,l as stickerElem,i as textElem,g as toBase64};
|
|
1
|
+
const t="https://chat-api2-3rnt.onrender.com",e="web_link",s="ai_choice_prompt",i="official_account_tweet";async function a(e){var s;const{clientId:i,clientSecret:a}=e;if(!i||!a)throw new Error("getAccessToken: clientId and clientSecret are required");const n=t.replace(/\/$/,""),r=null!==(s=e.scope)&&void 0!==s?s:"chat:write uploads:write";if("undefined"==typeof fetch)throw new Error("getAccessToken: global fetch is not available");const o=await fetch(`${n}/v1/oauth/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({grant_type:"client_credentials",client_id:i,client_secret:a,scope:r})});if(!o.ok)throw new Error(`Token request failed: ${o.status} ${o.statusText}`);return await o.json()}var n;function r(t){return{elemType:n.Text,priority:0,textElem:{text:t.trim()}}}function o(t){return{elemType:n.Image,priority:0,imageElem:{upload_ids:t}}}function l(t){return{elemType:n.File,priority:0,fileElem:{upload_ids:t}}}function u(t,e=0){return{elemType:n.Sound,priority:0,soundElem:{uploadID:t,duration:e}}}function c(t,e){return{elemType:n.Sticker,priority:0,stickerElem:{packID:t,stickerID:e}}}function d(t){return{elemType:n.Custom,priority:0,customElem:{data:t}}}function g(t){return{elemType:n.MiniApp,priority:0,miniAppElem:t}}function h(t){const e=new Uint8Array(t.byteLength);return e.set(t),e}function p(t){let e="";const s=new Uint8Array(t),i=s.byteLength;for(let t=0;t<i;t++)e+=String.fromCharCode(s[t]);return btoa(e)}function m(t){const e=atob(t),s=e.length,i=new Uint8Array(s);for(let t=0;t<s;t++)i[t]=e.charCodeAt(t);return i.buffer}function f(t){const e=(new TextEncoder).encode(t);let s="";for(const t of e)s+=String.fromCharCode(t);return btoa(s)}function w(t,e=10){if(t.length<=e)return[t];const s=[];let i="";for(const a of t.split(" "))i.length+a.length+1>e&&i.length>0?(s.push(i.trim()+" "),i=a):i+=(i?" ":"")+a;return i&&s.push(i.trim()),s.length>0?s:[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"}(n||(n={}));class y{constructor(e){var s,i;if(!(null==e?void 0:e.token))throw new Error("ChatClient: token is required");this.token=e.token,this.baseUrl=(null!==(s=e.baseUrl)&&void 0!==s?s:t).replace(/\/$/,""),this.logger=e.logger;const a=null!==(i=e.fetch)&&void 0!==i?i:"undefined"!=typeof fetch?fetch:void 0;if(!a)throw new Error("ChatClient: no fetch implementation available; pass `fetch` in options");this.fetchImpl=a.bind(globalThis)}static async fromCredentials(t){const{access_token:e}=await a({clientId:t.clientId,clientSecret:t.clientSecret,scope:t.scope});return new y({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`}newMsgID(){return globalThis.crypto.randomUUID()}async postElements(t,e,s="POST"){return await this.fetchImpl(this.replyUrl(t),{method:s,headers:this.authHeaders,body:JSON.stringify(e)})}async uploadImage(t,e,s="image/png"){var i;const a=new FormData;a.append("file",new Blob([h(t)],{type:s}),e);const n=await this.fetchImpl(`${this.baseUrl}/v1/upload/image`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:a}),r=await n.json();if(!n.ok)throw new Error(`uploadImage failed: ${n.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!==(i=r.url)&&void 0!==i?i:""}}async uploadFile(t,e,s="application/octet-stream"){var i;const a=new FormData;a.append("file",new Blob([h(t)],{type:s}),e);const n=await this.fetchImpl(`${this.baseUrl}/v1/upload/file`,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:a}),r=await n.json();if(!n.ok)throw new Error(`uploadFile failed: ${n.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!==(i=r.url)&&void 0!==i?i:""}}async sendMessage(t,e,s){var i;let a=Array.isArray(e)?e.map((t=>t.trim())).filter(Boolean).map(r):[r(e)];if(0===a.length)return;s&&(a=a.map((t=>({...t,msgID:s}))));const n=await this.postElements(t,a),o=await n.text();if(null===(i=this.logger)||void 0===i||i.log("[chat-sdk] message sent",{status:n.status,elements:a.length}),!n.ok)throw new Error(`sendMessage failed: ${n.status} ${o}`)}async sendImageMessage(t,e){var s;if(!(null==e?void 0:e.length))return;const i=await this.postElements(t,[o(e)]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] image message sent",{status:i.status}),!i.ok)throw new Error(`sendImageMessage failed: ${i.status} ${a}`)}async sendFileMessage(t,e){var s;if(!(null==e?void 0:e.length))return;const i=await this.postElements(t,[l(e)]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] file message sent",{status:i.status}),!i.ok)throw new Error(`sendFileMessage failed: ${i.status} ${a}`)}async sendSoundMessage(t,e,s=0){var i;if(!(null==e?void 0:e.trim()))return;const a=await this.postElements(t,[u(e,s)]),n=await a.text();if(null===(i=this.logger)||void 0===i||i.log("[chat-sdk] sound message sent",{status:a.status}),!a.ok)throw new Error(`sendSoundMessage failed: ${a.status} ${n.slice(0,500)}`)}async sendStickerMessage(t,e,s,i){var a;if(!(null==e?void 0:e.trim())||!(null==s?void 0:s.trim()))return;const n=c(e,s),r=await this.postElements(t,[i?{...n,msgID:i}:n]),o=await r.text();if(null===(a=this.logger)||void 0===a||a.log("[chat-sdk] sticker message sent",{status:r.status}),!r.ok)throw new Error(`sendStickerMessage failed: ${r.status} ${o}`)}async editTextMessage(t,e){var s;const i=await this.postElements(t,[{msgID:t,...r(e)}]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] text message edited",{status:i.status}),!i.ok)throw new Error(`editTextMessage failed: ${i.status} ${a.slice(0,500)}`)}async editStickerMessage(t,e,s){var i;const a=await this.postElements(t,[{msgID:t,...c(e,s)}]),n=await a.text();if(null===(i=this.logger)||void 0===i||i.log("[chat-sdk] sticker message edited",{status:a.status}),!a.ok)throw new Error(`editStickerMessage failed: ${a.status} ${n.slice(0,500)}`)}async editImageMessage(t,e){var s;const i=await this.postElements(t,[{msgID:t,...o(e)}]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] image message edited",{status:i.status}),!i.ok)throw new Error(`editImageMessage failed: ${i.status} ${a.slice(0,500)}`)}async deleteMessage(t){var e;const s=await this.fetchImpl(`${this.baseUrl}/v1/chat/message/${t}`,{method:"DELETE",headers:this.authHeaders});if(null===(e=this.logger)||void 0===e||e.log("[chat-sdk] message deleted",{status:s.status}),!s.ok){const t=await s.text();throw new Error(`deleteMessage failed: ${s.status} ${t.slice(0,500)}`)}}async sendTypingStatus(t,e){var s,i;const a={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,[d(f(JSON.stringify(a)))])}catch(t){null===(s=this.logger)||void 0===s||s.error("[chat-sdk] typing status failed",null!==(i=null==t?void 0:t.message)&&void 0!==i?i:String(t))}}async sendStreamingChunk(t,e,s,i){const a={businessID:"chatbotPlugin",src:0,chunks:e,isFinished:s,TMessageCell_Name:"ChatbotMessageCell_Minimalist",TMessageCell_Data_Name:"ChatbotMessageCellData"},n={msgID:t,...d(f(JSON.stringify(a)))},r=await this.postElements(t,[n],i?"POST":"PATCH");if(!r.ok)throw new Error(`sendStreamingChunk failed: ${r.statusText}`);await r.text()}async sendMiniAppMessage(t,e){var s;const i=await this.postElements(t,[g(e)]),a=await i.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] miniapp card sent",{status:i.status}),!i.ok)throw new Error(`sendMiniAppMessage failed: ${i.status} ${a}`)}async sendWebLinkMessage(t,s){var i;const a={businessID:e,url:s},n=await this.postElements(t,[d(f(JSON.stringify(a)))]),r=await n.text();if(null===(i=this.logger)||void 0===i||i.log("[chat-sdk] web link sent",{status:n.status}),!n.ok)throw new Error(`sendWebLinkMessage failed: ${n.status} ${r.slice(0,500)}`)}async sendChoicesMessage(t,e){var i,a,n;const r=this.newMsgID(),o={businessID:s,promptId:null!==(i=e.promptId)&&void 0!==i?i:this.newMsgID(),prompt:e.prompt,selectionMode:null!==(a=e.selectionMode)&&void 0!==a?a:"single",choices:e.choices},l=await this.postElements(t,[{msgID:r,...d(f(JSON.stringify(o)))}]),u=await l.text();if(null===(n=this.logger)||void 0===n||n.log("[chat-sdk] choices message sent",{status:l.status}),!l.ok)throw new Error(`sendChoicesMessage failed: ${l.status} ${u.slice(0,500)}`);return r}async sendOfficialAccountTweetMessage(t,e){var s;const a=this.newMsgID(),n={businessID:i,...e},r=await this.postElements(t,[{msgID:a,...d(f(JSON.stringify(n)))}]),o=await r.text();if(null===(s=this.logger)||void 0===s||s.log("[chat-sdk] official account tweet sent",{status:r.status}),!r.ok)throw new Error(`sendOfficialAccountTweetMessage failed: ${r.status} ${o.slice(0,500)}`);return a}async streamMessage(t,e,s=10){const i=w(e,s),a=[];for(let e=0;e<i.length;e++)a.push(i[e]),await this.sendStreamingChunk(t,a,e===i.length-1,0===e)}}y.ElemType=n;export{s as AI_CHOICE_PROMPT_BUSINESS_ID,y as ChatClient,t as DEFAULT_BASE_URL,n as ElemType,i as OFFICIAL_ACCOUNT_TWEET_BUSINESS_ID,e as WEB_LINK_BUSINESS_ID,p as arrayBufferToBase64,m as base64ToArrayBuffer,d as customElem,l as fileElem,a as getAccessToken,o as imageElem,g as miniAppElem,u as soundElem,w as splitIntoChunks,c as stickerElem,r as textElem,f as toBase64};
|
|
2
2
|
//# sourceMappingURL=main.esm.js.map
|
package/dist/types/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChatClientOptions, ElemType, Logger, MiniAppElem, UploadV1FileResponse, UploadV1ImageResponse } from "./types";
|
|
1
|
+
import { ChatClientOptions, ChoicesPrompt, ElemType, Logger, MiniAppElem, OfficialAccountTweet, UploadV1FileResponse, UploadV1ImageResponse } from "./types";
|
|
2
2
|
/**
|
|
3
3
|
* ChatClient wraps the chat API reply/upload endpoints. Construct once with a
|
|
4
4
|
* token (and optionally a baseUrl / logger), then call the message helpers.
|
|
@@ -31,6 +31,8 @@ export declare class ChatClient {
|
|
|
31
31
|
}): Promise<ChatClient>;
|
|
32
32
|
private get authHeaders();
|
|
33
33
|
private replyUrl;
|
|
34
|
+
/** Generate a fresh message id (so a sent message can later be edited/streamed). */
|
|
35
|
+
private newMsgID;
|
|
34
36
|
/** POST an array of message elements to a message's reply endpoint. */
|
|
35
37
|
private postElements;
|
|
36
38
|
/** Upload a binary image (POST /v1/upload/image) and return { upload_id, url }. */
|
|
@@ -40,16 +42,30 @@ export declare class ChatClient {
|
|
|
40
42
|
/**
|
|
41
43
|
* Send one or more text messages as a reply. A string sends a single element;
|
|
42
44
|
* an array sends one request with multiple elements (one per line/paragraph).
|
|
45
|
+
*
|
|
46
|
+
* Pass `replyMsgID` to assign the outgoing message a known id (so it can later
|
|
47
|
+
* be edited via {@link editTextMessage}); it is applied to every element sent.
|
|
43
48
|
*/
|
|
44
|
-
sendMessage(msgID: string, message: string | string[]): Promise<void>;
|
|
49
|
+
sendMessage(msgID: string, message: string | string[], replyMsgID?: string): Promise<void>;
|
|
45
50
|
/** Send an image message by upload_id(s) (use uploadImage first). */
|
|
46
51
|
sendImageMessage(msgID: string, uploadIDs: string[]): Promise<void>;
|
|
47
52
|
/** Send a file message by upload_id(s) (use uploadFile first). */
|
|
48
53
|
sendFileMessage(msgID: string, uploadIDs: string[]): Promise<void>;
|
|
49
54
|
/** Send a sound message by uploadID (duration in seconds). */
|
|
50
55
|
sendSoundMessage(msgID: string, uploadID: string, durationSec?: number): Promise<void>;
|
|
51
|
-
/**
|
|
52
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Send a sticker message by pack + sticker identifiers. Pass `replyMsgID` to
|
|
58
|
+
* assign a known id so it can later be edited via {@link editStickerMessage}.
|
|
59
|
+
*/
|
|
60
|
+
sendStickerMessage(msgID: string, packID: string, stickerID: string, replyMsgID?: string): Promise<void>;
|
|
61
|
+
/** Edit a text message in place (`event.message.modified`). */
|
|
62
|
+
editTextMessage(msgID: string, message: string): Promise<void>;
|
|
63
|
+
/** Edit a sticker message in place (`event.message.modified`). */
|
|
64
|
+
editStickerMessage(msgID: string, packID: string, stickerID: string): Promise<void>;
|
|
65
|
+
/** Replace the image variants on an existing image message in place. */
|
|
66
|
+
editImageMessage(msgID: string, uploadIDs: string[]): Promise<void>;
|
|
67
|
+
/** Delete a business message (`event.message.deleted`). */
|
|
68
|
+
deleteMessage(msgID: string): Promise<void>;
|
|
53
69
|
/** Send typing status (best effort — never throws). */
|
|
54
70
|
sendTypingStatus(msgID: string, isTyping: boolean): Promise<void>;
|
|
55
71
|
/**
|
|
@@ -61,6 +77,16 @@ export declare class ChatClient {
|
|
|
61
77
|
sendMiniAppMessage(msgID: string, miniApp: MiniAppElem): Promise<void>;
|
|
62
78
|
/** Send a web link preview custom message (matches iOS `webLinkPreviewMessage`). */
|
|
63
79
|
sendWebLinkMessage(msgID: string, url: string): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Send a choices prompt message (iOS `ai_choice_prompt`). Returns the new
|
|
82
|
+
* message's id so it can later be edited (e.g. to mark a selection).
|
|
83
|
+
*/
|
|
84
|
+
sendChoicesMessage(msgID: string, prompt: ChoicesPrompt): Promise<string>;
|
|
85
|
+
/**
|
|
86
|
+
* Send an official-account tweet card (iOS `TUIOfficialAccountMessage`).
|
|
87
|
+
* Returns the new message's id.
|
|
88
|
+
*/
|
|
89
|
+
sendOfficialAccountTweetMessage(msgID: string, tweet: OfficialAccountTweet): Promise<string>;
|
|
64
90
|
/**
|
|
65
91
|
* Stream a message in chunks for a typewriter effect. Splits `text`, then
|
|
66
92
|
* sends each chunk via sendStreamingChunk (POST first, PATCH after).
|
|
@@ -2,3 +2,7 @@
|
|
|
2
2
|
export declare const DEFAULT_BASE_URL = "https://chat-api2-3rnt.onrender.com";
|
|
3
3
|
/** Must match iOS `WebLinkMessageCellData.businessID`. */
|
|
4
4
|
export declare const WEB_LINK_BUSINESS_ID = "web_link";
|
|
5
|
+
/** Must match iOS `AIChoiceMessageCellData` (`businessID: ai_choice_prompt`). */
|
|
6
|
+
export declare const AI_CHOICE_PROMPT_BUSINESS_ID = "ai_choice_prompt";
|
|
7
|
+
/** Must match iOS `OfficialAccountMessageBusinessID`. */
|
|
8
|
+
export declare const OFFICIAL_ACCOUNT_TWEET_BUSINESS_ID = "official_account_tweet";
|
package/dist/types/types.d.ts
CHANGED
|
@@ -41,6 +41,33 @@ export type StickerElem = {
|
|
|
41
41
|
packID: string;
|
|
42
42
|
stickerID: string;
|
|
43
43
|
};
|
|
44
|
+
/** A single choice in an `ai_choice_prompt` message. */
|
|
45
|
+
export type Choice = {
|
|
46
|
+
id: string;
|
|
47
|
+
label: string;
|
|
48
|
+
};
|
|
49
|
+
/** Payload for a choices prompt message (iOS `AIChoiceMessageCellData`). */
|
|
50
|
+
export type ChoicesPrompt = {
|
|
51
|
+
prompt: string;
|
|
52
|
+
choices: Choice[];
|
|
53
|
+
/** Defaults to 'single'. */
|
|
54
|
+
selectionMode?: 'single' | 'multiple';
|
|
55
|
+
/** Defaults to a generated UUID. */
|
|
56
|
+
promptId?: string;
|
|
57
|
+
};
|
|
58
|
+
export type OfficialAccountTweetImageInfo = {
|
|
59
|
+
url: string;
|
|
60
|
+
width: number;
|
|
61
|
+
height: number;
|
|
62
|
+
};
|
|
63
|
+
/** Payload for an official-account tweet card (iOS `TUIOfficialAccountMessage`). */
|
|
64
|
+
export type OfficialAccountTweet = {
|
|
65
|
+
title: string;
|
|
66
|
+
content: string;
|
|
67
|
+
link: string;
|
|
68
|
+
version: number;
|
|
69
|
+
imageInfo?: OfficialAccountTweetImageInfo;
|
|
70
|
+
};
|
|
44
71
|
/** OAuth client_credentials token response (POST /v1/oauth/token). */
|
|
45
72
|
export type TokenResponse = {
|
|
46
73
|
access_token: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "birdcash-chat-sdk-alpha",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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",
|