import{stripMarkdown as I}from"openclaw/plugin-sdk/weixin";import{sendMessage as l}from"../api/api.js";import{MessageItemType as g,MessageState as c,MessageType as x}from"../api/types.js";import{logger as a}from"../util/logger.js";import{generateId as _}from"../util/random.js";function f(){return _("openclaw-weixin")}function b(s){let e=s;return e=e.replace(/```[^\n]*\n?([\s\S]*?)```/g,(i,t)=>t.trim()),e=e.replace(/!\[[^\]]*\]\([^)]*\)/g,""),e=e.replace(/\[([^\]]+)\]\([^)]*\)/g,"$1"),e=e.replace(/^\|[\s:|-]+\|$/gm,""),e=e.replace(/^\|(.+)\|$/gm,(i,t)=>t.split("|").map(n=>n.trim()).join("  ")),e=I(e),e}function k(s){const{to:e,text:i,contextToken:t,clientId:n}=s,o=i?[{type:g.TEXT,text_item:{text:i}}]:[];return{msg:{from_user_id:"",to_user_id:e,client_id:n,message_type:x.BOT,message_state:c.FINISH,item_list:o.length?o:void 0,context_token:t??void 0}}}function M(s){const{to:e,contextToken:i,payload:t,clientId:n}=s;return k({to:e,text:t.text??"",contextToken:i,clientId:n})}async function h(s){const{to:e,text:i,opts:t}=s;t.contextToken||a.warn(`sendMessageWeixin: contextToken missing for to=${e}, sending without context`);const n=f(),o=M({to:e,contextToken:t.contextToken,payload:{text:i},clientId:n});try{await l({baseUrl:t.baseUrl,token:t.token,timeoutMs:t.timeoutMs,body:o})}catch(r){throw a.error(`sendMessageWeixin: failed to=${e} clientId=${n} err=${String(r)}`),r}return{messageId:n}}async function m(s){const{to:e,text:i,mediaItem:t,opts:n,label:o}=s,r=[];i&&r.push({type:g.TEXT,text_item:{text:i}}),r.push(t);let d="";for(const y of r){d=f();const u={msg:{from_user_id:"",to_user_id:e,client_id:d,message_type:x.BOT,message_state:c.FINISH,item_list:[y],context_token:n.contextToken??void 0}};try{await l({baseUrl:n.baseUrl,token:n.token,timeoutMs:n.timeoutMs,body:u})}catch(p){throw a.error(`${o}: failed to=${e} clientId=${d} err=${String(p)}`),p}}return a.info(`${o}: success to=${e} clientId=${d}`),{messageId:d}}async function q(s){const{to:e,text:i,uploaded:t,opts:n}=s;n.contextToken||a.warn(`sendImageMessageWeixin: contextToken missing for to=${e}, sending without context`),a.info(`sendImageMessageWeixin: to=${e} filekey=${t.filekey} fileSize=${t.fileSize} aeskey=present`);const o={type:g.IMAGE,image_item:{media:{encrypt_query_param:t.downloadEncryptedQueryParam,aes_key:Buffer.from(t.aeskey).toString("base64"),encrypt_type:1},mid_size:t.fileSizeCiphertext}};return m({to:e,text:i,mediaItem:o,opts:n,label:"sendImageMessageWeixin"})}async function P(s){const{to:e,text:i,uploaded:t,opts:n}=s;n.contextToken||a.warn(`sendVideoMessageWeixin: contextToken missing for to=${e}, sending without context`);const o={type:g.VIDEO,video_item:{media:{encrypt_query_param:t.downloadEncryptedQueryParam,aes_key:Buffer.from(t.aeskey).toString("base64"),encrypt_type:1},video_size:t.fileSizeCiphertext}};return m({to:e,text:i,mediaItem:o,opts:n,label:"sendVideoMessageWeixin"})}async function F(s){const{to:e,text:i,fileName:t,uploaded:n,opts:o}=s;o.contextToken||a.warn(`sendFileMessageWeixin: contextToken missing for to=${e}, sending without context`);const r={type:g.FILE,file_item:{media:{encrypt_query_param:n.downloadEncryptedQueryParam,aes_key:Buffer.from(n.aeskey).toString("base64"),encrypt_type:1},file_name:t,len:String(n.fileSize)}};return m({to:e,text:i,mediaItem:r,opts:o,label:"sendFileMessageWeixin"})}export{b as markdownToPlainText,F as sendFileMessageWeixin,q as sendImageMessageWeixin,h as sendMessageWeixin,P as sendVideoMessageWeixin};
