@dnotifier-realtime/dnotifier 1.0.18 → 1.0.20

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.
@@ -1 +1 @@
1
- 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['DNotifier']=void 0x0;const httpTransport_js_1=require('./transport/httpTransport.js'),websocketTransport_js_1=require('./transport/websocketTransport.js'),id_js_1=require('./utils/id.js'),frame_js_1=require('./utils/frame.js'),payload_js_1=require('./utils/payload.js'),buffer_js_1=require('./utils/buffer.js'),openai_js_1=require('./providers/openai.js');class DNotifier{constructor({appId:_0x2408ae,secret:_0x17a975,transport:_0x7fd49f,userId:_0x544f04,onConnected:_0x2d2ffa,onMessage:_0x360e9d,onDisconnected:_0x4c4ccd,WebSocketImpl:_0x5c45d8}){if(!(_0x2408ae&&_0x17a975&&_0x7fd49f&&_0x544f04))throw new Error('appId,\x20secret,\x20transport,\x20and\x20userId\x20are\x20required');this['appId']=_0x2408ae,this['secret']=_0x17a975,this['userId']=_0x544f04,this['url']='wss://link.dchatter.com/ws',this['onConnectedCallback']=_0x2d2ffa||(()=>{}),this['onMessageCallback']=_0x360e9d||(()=>{}),this['onDisconnectedCallback']=_0x4c4ccd||(()=>{}),this['isConnected']=!0x1,this['transport']='ws'===_0x7fd49f?new websocketTransport_js_1['WebsocketTransport'](_0x5c45d8):new httpTransport_js_1['HttpTransport']();}async['connect'](){const _0x10efa7=new httpTransport_js_1['HttpTransport']({'url':'https://link.dchatter.com/api/v1/sdk/auth'}),_0x52ca72=await _0x10efa7['send']({'appId':this['appId'],'appSecret':this['secret'],'userID':this['userId']});if(0xc8!==_0x52ca72['_responseCode'])throw new Error('Authentication\x20Failed.');this['authToken']=_0x52ca72['_responseData'],await this['transport']['connect'](this['url']);const _0xdfb23c={'type':'text','code':0x65,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],this['userId']),'timestamp':Date['now'](),'token':this['authToken']},_0x4a82c0=(0x0,buffer_js_1['toUint8'])(JSON['stringify']({'token':this['authToken']})),_0x574df7=(0x0,frame_js_1['framePacket'])(_0xdfb23c,_0x4a82c0);this['transport']['sendBinary'](_0x574df7);let _0x1cd18d=!0x1;this['transport']['on']('message',_0x478aef=>{try{const {metadata:_0x5d2889,data:_0x5d1947}=_0x478aef;if(!_0x1cd18d&&'102'===_0x5d2889['packet']['code']&&_0x5d2889['packet']['sender']['includes']((0x0,id_js_1['prefixId'])(this['appId'],this['userId'])))return _0x1cd18d=!0x0,this['isConnected']=!0x0,void this['onConnectedCallback']();_0x1cd18d&&this['onMessageCallback']({'metadata':{'sender':_0x5d2889['packet']['sender'],'timestamp':_0x5d2889['packet']['timestamp']},'payload':new payload_js_1['Payload'](_0x5d1947)});}catch(_0x18a51a){console['error']('Packet\x20error:',_0x18a51a);}}),this['transport']['on']('close',({code:_0x18dcd2,reason:_0x1c02e4})=>{this['isConnected']&&(this['isConnected']=!0x1,this['onDisconnectedCallback']({'code':_0x18dcd2,'reason':_0x1c02e4||'Disconnected'}));}),this['transport']['on']('error',_0x51f7a5=>{this['isConnected']=!0x1,this['onDisconnectedCallback']({'code':0x1f4,'reason':(null==_0x51f7a5?void 0x0:_0x51f7a5['message'])||'Transport\x20error'});});}['send']({senderId:_0x3091b5,receiverId:_0x5c6cdc,receiverIds:_0x2cb5d2,data:_0x13cbfa}){if(!_0x3091b5)throw new Error('senderId\x20required');if(!_0x5c6cdc&&!_0x2cb5d2)throw new Error('receiverId\x20or\x20receiverIds\x20required');const _0x89a4b2=null!=_0x2cb5d2?_0x2cb5d2:[_0x5c6cdc],_0xb64e62={'type':'text','code':0xc8,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x3091b5),'receivers':_0x89a4b2['map'](_0x2b5166=>(0x0,id_js_1['prefixId'])(this['appId'],_0x2b5166)),'timestamp':Date['now'](),'token':this['authToken']},_0x461d1b=(0x0,buffer_js_1['toUint8'])(JSON['stringify'](_0x13cbfa)),_0x2fd298=(0x0,frame_js_1['framePacket'])(_0xb64e62,_0x461d1b);this['transport']['sendBinary'](_0x2fd298);}['sendBinary']({senderId:_0x42abbc,receiverIds:_0x445fda,buffer:_0x507b43,type:_0x38b9cd='binary'}){if(!_0x42abbc)throw new Error('senderId\x20required');if(!_0x507b43)throw new Error('buffer\x20is\x20required');const _0x236801={'type':_0x38b9cd,'code':0xca,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x42abbc),'receivers':_0x445fda['map'](_0x2a89a5=>(0x0,id_js_1['prefixId'])(this['appId'],_0x2a89a5)),'timestamp':Date['now'](),'token':this['authToken']},_0x1423f6=(0x0,frame_js_1['framePacket'])(_0x236801,_0x507b43);this['transport']['sendBinary'](_0x1423f6);}async['sendWithOpenAI']({senderId:_0x23d2ea,message:_0x223f26,openaiApiKey:_0x212d0b,model:_0x404b94,baseUrl:_0x3303fc}){if(!_0x23d2ea)throw new Error('senderId\x20is\x20required');if(!_0x223f26)throw new Error('message\x20is\x20required');if(!_0x212d0b)throw new Error('openaiApiKey\x20is\x20required');const {content:_0x3cc712}=await(0x0,openai_js_1['chatStream'])({'apiKey':_0x212d0b,'model':_0x404b94,'messages':_0x223f26,'baseUrl':_0x3303fc}),_0x402670={'type':'openai','content':_0x3cc712,'ts':Date['now']()};return this['onMessageCallback']({'metadata':{'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x23d2ea),'timestamp':Date['now']()},'payload':new payload_js_1['Payload']((0x0,buffer_js_1['toUint8'])(JSON['stringify'](_0x402670)))}),{'content':_0x3cc712};}}exports['DNotifier']=DNotifier;
1
+ 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['DNotifier']=void 0x0;const httpTransport_js_1=require('./transport/httpTransport.js'),websocketTransport_js_1=require('./transport/websocketTransport.js'),id_js_1=require('./utils/id.js'),frame_js_1=require('./utils/frame.js'),payload_js_1=require('./utils/payload.js'),buffer_js_1=require('./utils/buffer.js'),openai_js_1=require('./providers/openai.js');class DNotifier{constructor({appId:_0x35a22a,secret:_0x2134d4,transport:_0x57b844,userId:_0x2be4e6,onConnected:_0x240d06,onMessage:_0x4b14f3,onDisconnected:_0x3b26db,WebSocketImpl:_0x13c690}){if(!(_0x35a22a&&_0x2134d4&&_0x57b844&&_0x2be4e6))throw new Error('appId,\x20secret,\x20transport,\x20and\x20userId\x20are\x20required');this['appId']=_0x35a22a,this['secret']=_0x2134d4,this['userId']=_0x2be4e6,this['url']='wss://link.dchatter.com/ws',this['onConnectedCallback']=_0x240d06||(()=>{}),this['onMessageCallback']=_0x4b14f3||(()=>{}),this['onDisconnectedCallback']=_0x3b26db||(()=>{}),this['isConnected']=!0x1,this['transport']='ws'===_0x57b844?new websocketTransport_js_1['WebsocketTransport'](_0x13c690):new httpTransport_js_1['HttpTransport']();}async['connect'](){const _0x1a0eae=new httpTransport_js_1['HttpTransport']({'url':'https://link.dchatter.com/api/v1/sdk/auth'}),_0xa64117=await _0x1a0eae['send']({'appId':this['appId'],'appSecret':this['secret'],'userID':this['userId']});if(0xc8!==_0xa64117['_responseCode'])throw new Error('Authentication\x20Failed.');this['authToken']=_0xa64117['_responseData'],await this['transport']['connect'](this['url']);const _0x26176d={'type':'text','code':0x65,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],this['userId']),'timestamp':Date['now'](),'token':this['authToken']},_0x55bfa4=(0x0,buffer_js_1['toUint8'])(JSON['stringify']({'token':this['authToken']})),_0x37943f=(0x0,frame_js_1['framePacket'])(_0x26176d,_0x55bfa4);this['transport']['sendBinary'](_0x37943f);let _0x136177=!0x1;this['transport']['on']('message',_0x2f48be=>{try{const {metadata:_0x13a76e,data:_0x1b025e}=_0x2f48be;if(!_0x136177&&'102'===_0x13a76e['packet']['code']&&_0x13a76e['packet']['sender']['includes']((0x0,id_js_1['prefixId'])(this['appId'],this['userId'])))return _0x136177=!0x0,this['isConnected']=!0x0,void this['onConnectedCallback']();_0x136177&&this['onMessageCallback']({'metadata':{'sender':_0x13a76e['packet']['sender'],'timestamp':_0x13a76e['packet']['timestamp']},'payload':new payload_js_1['Payload'](_0x1b025e)});}catch(_0x1a457a){console['error']('Packet\x20error:',_0x1a457a);}}),this['transport']['on']('close',({code:_0x263371,reason:_0xfa1f95})=>{this['isConnected']&&(this['isConnected']=!0x1,this['onDisconnectedCallback']({'code':_0x263371,'reason':_0xfa1f95||'Disconnected'}));}),this['transport']['on']('error',_0x20f7f8=>{this['isConnected']=!0x1,this['onDisconnectedCallback']({'code':0x1f4,'reason':(null==_0x20f7f8?void 0x0:_0x20f7f8['message'])||'Transport\x20error'});});}['send']({senderId:_0x1e29e9,receiverId:_0x563096,receiverIds:_0x5129e2,data:_0x9bc71a}){if(!_0x1e29e9)throw new Error('senderId\x20required');if(!_0x563096&&!_0x5129e2)throw new Error('receiverId\x20or\x20receiverIds\x20required');const _0x4fc782=null!=_0x5129e2?_0x5129e2:[_0x563096],_0x11eb05={'type':'text','code':0xc8,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x1e29e9),'receivers':_0x4fc782['map'](_0x4195de=>(0x0,id_js_1['prefixId'])(this['appId'],_0x4195de)),'timestamp':Date['now'](),'token':this['authToken']},_0x1392c2=(0x0,buffer_js_1['toUint8'])(JSON['stringify'](_0x9bc71a)),_0x4cbf40=(0x0,frame_js_1['framePacket'])(_0x11eb05,_0x1392c2);this['transport']['sendBinary'](_0x4cbf40);}['sendBinary']({senderId:_0x4a37d9,receiverIds:_0x434368,buffer:_0xe9e67c,type:_0x11cd7d='binary'}){if(!_0x4a37d9)throw new Error('senderId\x20required');if(!_0xe9e67c)throw new Error('buffer\x20is\x20required');const _0x3470f0={'type':_0x11cd7d,'code':0xca,'appId':this['appId'],'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x4a37d9),'receivers':_0x434368['map'](_0x57ed5a=>(0x0,id_js_1['prefixId'])(this['appId'],_0x57ed5a)),'timestamp':Date['now'](),'token':this['authToken']},_0x27875f=(0x0,frame_js_1['framePacket'])(_0x3470f0,_0xe9e67c);this['transport']['sendBinary'](_0x27875f);}async['sendWithOpenAI']({senderId:_0x700549,message:_0x4a67ac,openaiApiKey:_0x1e0b82,model:_0x233a81,baseUrl:_0x460ae6}){if(!_0x700549)throw new Error('senderId\x20is\x20required');if(!_0x4a67ac)throw new Error('message\x20is\x20required');if(!_0x1e0b82)throw new Error('openaiApiKey\x20is\x20required');const {content:_0x214aea}=await(0x0,openai_js_1['chatStream'])({'apiKey':_0x1e0b82,'model':_0x233a81,'messages':_0x4a67ac,'baseUrl':_0x460ae6}),_0x2f61ec={'type':'openai','content':_0x214aea,'ts':Date['now']()};return this['onMessageCallback']({'metadata':{'sender':(0x0,id_js_1['prefixId'])(this['appId'],_0x700549),'timestamp':Date['now']()},'payload':new payload_js_1['Payload']((0x0,buffer_js_1['toUint8'])(JSON['stringify'](_0x2f61ec)))}),{'content':_0x214aea};}}exports['DNotifier']=DNotifier;
@@ -1 +1 @@
1
- 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['chat']=chat,exports['chatStream']=chatStream;const DEFAULT_BASE_URL='https://api.openai.com',DEFAULT_MODEL='gpt-4o-mini';function normalizeMessages(_0x10cda2){if('string'==typeof _0x10cda2)return[{'role':'user','content':_0x10cda2}];if(Array['isArray'](_0x10cda2)&&_0x10cda2['length']>0x0)return _0x10cda2['map'](_0x3bad43=>({'role':_0x3bad43['role']||'user','content':'string'==typeof _0x3bad43['content']?_0x3bad43['content']:String(_0x3bad43['content'])}));throw new Error('message\x20must\x20be\x20a\x20non-empty\x20string\x20or\x20array\x20of\x20{\x20role,\x20content\x20}');}async function chat(_0x240bad){var _0xf30f90,_0x5540e7,_0x3d4f6b,_0x1d47e2,_0x570512,_0x16ba54;const {apiKey:_0x297118,model:_0x2b526f=DEFAULT_MODEL,messages:_0x13b9c8,baseUrl:_0x3e54b5=DEFAULT_BASE_URL}=_0x240bad;if(!_0x297118||!_0x13b9c8)throw new Error('openai.chat:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x4187e6=_0x3e54b5['replace'](/\/$/,'')+'/v1/chat/completions',_0xfcf2e8={'model':_0x2b526f,'messages':normalizeMessages(_0x13b9c8),'stream':!0x1},_0x76d028=await fetch(_0x4187e6,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x297118},'body':JSON['stringify'](_0xfcf2e8)}),_0x5b2d62=await _0x76d028['json']();if(!_0x76d028['ok']){const _0x3a5111=new Error((null===(_0xf30f90=null==_0x5b2d62?void 0x0:_0x5b2d62['error'])||void 0x0===_0xf30f90?void 0x0:_0xf30f90['message'])||'OpenAI\x20API\x20error:\x20'+_0x76d028['status']);throw _0x3a5111['status']=_0x76d028['status'],_0x3a5111['code']=null===(_0x5540e7=null==_0x5b2d62?void 0x0:_0x5b2d62['error'])||void 0x0===_0x5540e7?void 0x0:_0x5540e7['code'],_0x3a5111;}return{'content':null!==(_0x16ba54=null===(_0x570512=null===(_0x1d47e2=null===(_0x3d4f6b=null==_0x5b2d62?void 0x0:_0x5b2d62['choices'])||void 0x0===_0x3d4f6b?void 0x0:_0x3d4f6b[0x0])||void 0x0===_0x1d47e2?void 0x0:_0x1d47e2['message'])||void 0x0===_0x570512?void 0x0:_0x570512['content'])&&void 0x0!==_0x16ba54?_0x16ba54:'','usage':null==_0x5b2d62?void 0x0:_0x5b2d62['usage'],'raw':_0x5b2d62};}async function chatStream(_0x4aa2b2){var _0x8ef050,_0x286a90,_0x4e0065,_0x101486,_0x589569;const {apiKey:_0x1ee4f6,model:_0x2c7edd=DEFAULT_MODEL,messages:_0x117b89,baseUrl:_0x398f5b=DEFAULT_BASE_URL,onChunk:_0x1f125d}=_0x4aa2b2;if(!_0x1ee4f6||!_0x117b89)throw new Error('openai.chatStream:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x3d8988=_0x398f5b['replace'](/\/$/,'')+'/v1/chat/completions',_0x3497c2={'model':_0x2c7edd,'messages':normalizeMessages(_0x117b89),'stream':!0x0},_0x5e73c5=await fetch(_0x3d8988,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x1ee4f6},'body':JSON['stringify'](_0x3497c2)});if(!_0x5e73c5['ok']){const _0x1e1055=await _0x5e73c5['json']()['catch'](()=>({})),_0x2561d4=new Error((null===(_0x8ef050=null==_0x1e1055?void 0x0:_0x1e1055['error'])||void 0x0===_0x8ef050?void 0x0:_0x8ef050['message'])||'OpenAI\x20API\x20error:\x20'+_0x5e73c5['status']);throw _0x2561d4['status']=_0x5e73c5['status'],_0x2561d4['code']=null===(_0x286a90=null==_0x1e1055?void 0x0:_0x1e1055['error'])||void 0x0===_0x286a90?void 0x0:_0x286a90['code'],_0x2561d4;}const _0x32a640=_0x5e73c5['body']['getReader'](),_0x1ecebd=new TextDecoder();let _0x4797c0='',_0x5ca956='';for(;;){const {done:_0x24cc5f,value:_0x584d6e}=await _0x32a640['read']();if(_0x24cc5f)break;_0x5ca956+=_0x1ecebd['decode'](_0x584d6e,{'stream':!0x0});const _0x516841=_0x5ca956['split']('\x0a');_0x5ca956=_0x516841['pop']()||'';for(const _0x2173e7 of _0x516841){const _0x3fdcb3=_0x2173e7['trim']();if(''!==_0x3fdcb3&&'data:\x20[DONE]'!==_0x3fdcb3&&_0x3fdcb3['startsWith']('data:\x20'))try{const _0x45d740=JSON['parse'](_0x3fdcb3['slice'](0x6)),_0x286e15=null===(_0x589569=null===(_0x101486=null===(_0x4e0065=null==_0x45d740?void 0x0:_0x45d740['choices'])||void 0x0===_0x4e0065?void 0x0:_0x4e0065[0x0])||void 0x0===_0x101486?void 0x0:_0x101486['delta'])||void 0x0===_0x589569?void 0x0:_0x589569['content'];'string'==typeof _0x286e15&&(_0x4797c0+=_0x286e15,_0x1f125d&&_0x1f125d(_0x286e15));}catch(_0x56ff28){}}}return{'content':_0x4797c0};}
1
+ 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['chat']=chat,exports['chatStream']=chatStream;const DEFAULT_BASE_URL='https://api.openai.com',DEFAULT_MODEL='gpt-4o-mini';function normalizeMessages(_0x37b2e0){if('string'==typeof _0x37b2e0)return[{'role':'user','content':_0x37b2e0}];if(Array['isArray'](_0x37b2e0)&&_0x37b2e0['length']>0x0)return _0x37b2e0['map'](_0x34ac64=>({'role':_0x34ac64['role']||'user','content':'string'==typeof _0x34ac64['content']?_0x34ac64['content']:String(_0x34ac64['content'])}));throw new Error('message\x20must\x20be\x20a\x20non-empty\x20string\x20or\x20array\x20of\x20{\x20role,\x20content\x20}');}async function chat(_0x3e97db){var _0x2b1e56,_0x47d7dd,_0x4f6aa4,_0x481238,_0x2bfe0a,_0x59b573;const {apiKey:_0x329c4e,model:_0x44d78b=DEFAULT_MODEL,messages:_0x254567,baseUrl:_0x1ad47c=DEFAULT_BASE_URL}=_0x3e97db;if(!_0x329c4e||!_0x254567)throw new Error('openai.chat:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x40a8bb=_0x1ad47c['replace'](/\/$/,'')+'/v1/chat/completions',_0xe4f074={'model':_0x44d78b,'messages':normalizeMessages(_0x254567),'stream':!0x1},_0x1c0dd5=await fetch(_0x40a8bb,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x329c4e},'body':JSON['stringify'](_0xe4f074)}),_0x5e9a42=await _0x1c0dd5['json']();if(!_0x1c0dd5['ok']){const _0x412383=new Error((null===(_0x2b1e56=null==_0x5e9a42?void 0x0:_0x5e9a42['error'])||void 0x0===_0x2b1e56?void 0x0:_0x2b1e56['message'])||'OpenAI\x20API\x20error:\x20'+_0x1c0dd5['status']);throw _0x412383['status']=_0x1c0dd5['status'],_0x412383['code']=null===(_0x47d7dd=null==_0x5e9a42?void 0x0:_0x5e9a42['error'])||void 0x0===_0x47d7dd?void 0x0:_0x47d7dd['code'],_0x412383;}return{'content':null!==(_0x59b573=null===(_0x2bfe0a=null===(_0x481238=null===(_0x4f6aa4=null==_0x5e9a42?void 0x0:_0x5e9a42['choices'])||void 0x0===_0x4f6aa4?void 0x0:_0x4f6aa4[0x0])||void 0x0===_0x481238?void 0x0:_0x481238['message'])||void 0x0===_0x2bfe0a?void 0x0:_0x2bfe0a['content'])&&void 0x0!==_0x59b573?_0x59b573:'','usage':null==_0x5e9a42?void 0x0:_0x5e9a42['usage'],'raw':_0x5e9a42};}async function chatStream(_0x429e8b){var _0x1727aa,_0x190c48,_0x594c51,_0x3209ae,_0x477f49;const {apiKey:_0x35ebd6,model:_0x33d398=DEFAULT_MODEL,messages:_0x37210d,baseUrl:_0x181391=DEFAULT_BASE_URL,onChunk:_0x2da7eb}=_0x429e8b;if(!_0x35ebd6||!_0x37210d)throw new Error('openai.chatStream:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x10e687=_0x181391['replace'](/\/$/,'')+'/v1/chat/completions',_0x19c2df={'model':_0x33d398,'messages':normalizeMessages(_0x37210d),'stream':!0x0},_0x516de6=await fetch(_0x10e687,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x35ebd6},'body':JSON['stringify'](_0x19c2df)});if(!_0x516de6['ok']){const _0x2ccd65=await _0x516de6['json']()['catch'](()=>({})),_0x3d4d08=new Error((null===(_0x1727aa=null==_0x2ccd65?void 0x0:_0x2ccd65['error'])||void 0x0===_0x1727aa?void 0x0:_0x1727aa['message'])||'OpenAI\x20API\x20error:\x20'+_0x516de6['status']);throw _0x3d4d08['status']=_0x516de6['status'],_0x3d4d08['code']=null===(_0x190c48=null==_0x2ccd65?void 0x0:_0x2ccd65['error'])||void 0x0===_0x190c48?void 0x0:_0x190c48['code'],_0x3d4d08;}const _0x3b2d99=_0x516de6['body']['getReader'](),_0x853ff=new TextDecoder();let _0x27572e='',_0x19acd1='';for(;;){const {done:_0x1bedf2,value:_0x1bfe88}=await _0x3b2d99['read']();if(_0x1bedf2)break;_0x19acd1+=_0x853ff['decode'](_0x1bfe88,{'stream':!0x0});const _0x30438a=_0x19acd1['split']('\x0a');_0x19acd1=_0x30438a['pop']()||'';for(const _0x433aee of _0x30438a){const _0x292f57=_0x433aee['trim']();if(''!==_0x292f57&&'data:\x20[DONE]'!==_0x292f57&&_0x292f57['startsWith']('data:\x20'))try{const _0x3943b7=JSON['parse'](_0x292f57['slice'](0x6)),_0x12c1d7=null===(_0x477f49=null===(_0x3209ae=null===(_0x594c51=null==_0x3943b7?void 0x0:_0x3943b7['choices'])||void 0x0===_0x594c51?void 0x0:_0x594c51[0x0])||void 0x0===_0x3209ae?void 0x0:_0x3209ae['delta'])||void 0x0===_0x477f49?void 0x0:_0x477f49['content'];'string'==typeof _0x12c1d7&&(_0x27572e+=_0x12c1d7,_0x2da7eb&&_0x2da7eb(_0x12c1d7));}catch(_0x18f635){}}}return{'content':_0x27572e};}
@@ -1 +1 @@
1
- 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['HttpTransport']=void 0x0;class HttpTransport{constructor({url:_0x1f3cd1}){this['url']=_0x1f3cd1;}async['send'](_0x173459){const _0x4e9892=await fetch(''+this['url'],{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](_0x173459)});if(!_0x4e9892['ok']){const _0x396d2d=await _0x4e9892['text']();throw new Error('DNotifier\x20HTTP\x20error:\x20'+_0x396d2d);}return await _0x4e9892['json']();}['sendBinary'](){throw new Error('Binary\x20is\x20not\x20supported\x20over\x20HTTP\x20yet');}}exports['HttpTransport']=HttpTransport;
1
+ 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['HttpTransport']=void 0x0;class HttpTransport{constructor({url:_0x481071}){this['url']=_0x481071;}async['send'](_0x476e98){const _0x54ca24=await fetch(''+this['url'],{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](_0x476e98)});if(!_0x54ca24['ok']){const _0x100527=await _0x54ca24['text']();throw new Error('DNotifier\x20HTTP\x20error:\x20'+_0x100527);}return await _0x54ca24['json']();}['sendBinary'](){throw new Error('Binary\x20is\x20not\x20supported\x20over\x20HTTP\x20yet');}}exports['HttpTransport']=HttpTransport;
@@ -1 +1 @@
1
- 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['WebsocketTransport']=void 0x0;const events_1=require('events'),frame_js_1=require('../utils/frame.js');class WebsocketTransport extends events_1['default']{constructor(_0x3bbdb6=null){if(super(),this['socket']=null,this['WebSocketImpl']=_0x3bbdb6||('undefined'!=typeof window?window['WebSocket']:null),!this['WebSocketImpl'])throw new Error('No\x20WebSocket\x20implementation\x20provided');}['connect'](_0x41f9f5){return new Promise((_0x5e82aa,_0x430fee)=>{this['socket']=new this['WebSocketImpl'](_0x41f9f5),'undefined'!=typeof window&&(this['socket']['binaryType']='arrayBuffer'),this['_on']('open',()=>_0x5e82aa(!0x0)),this['_on']('error',_0x154812=>{console['error']('WS\x20error',_0x154812),_0x430fee(_0x154812);}),this['_on']('close',_0x3d7e86=>{const _0x38687d=null==_0x3d7e86?void 0x0:_0x3d7e86['code'],_0x584dd7=null==_0x3d7e86?void 0x0:_0x3d7e86['reason'];this['emit']('close',{'code':_0x38687d,'reason':_0x584dd7});}),this['_on']('message',async(_0x57d3f9,_0x4a76f1)=>{var _0x531aab,_0x59340e=null!==(_0x531aab=null==_0x57d3f9?void 0x0:_0x57d3f9['data'])&&void 0x0!==_0x531aab?_0x531aab:_0x57d3f9;'undefined'!=typeof Blob&&_0x59340e instanceof Blob&&(_0x59340e=await _0x59340e['arrayBuffer']());const _0x5b70d0=(0x0,frame_js_1['decodePacket'])(_0x59340e);_0x5b70d0&&this['emit']('message',_0x5b70d0,_0x4a76f1);});});}['_on'](_0x5694fd,_0x152f06){this['socket']['on']?this['socket']['on'](_0x5694fd,_0x152f06):this['socket']['on'+_0x5694fd]=_0x152f06;}['send'](_0x1b4c27){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x1b4c27);}['sendBinary'](_0x2ed054){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x2ed054);}['isOpen'](){return this['socket']&&this['socket']['readyState']===this['WebSocketImpl']['OPEN'];}['close'](){var _0x194107;null===(_0x194107=this['socket'])||void 0x0===_0x194107||_0x194107['close']();}}exports['WebsocketTransport']=WebsocketTransport;
1
+ 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['WebsocketTransport']=void 0x0;const events_1=require('events'),frame_js_1=require('../utils/frame.js');class WebsocketTransport extends events_1['default']{constructor(_0x1a30c3=null){if(super(),this['socket']=null,this['WebSocketImpl']=_0x1a30c3||('undefined'!=typeof window?window['WebSocket']:null),!this['WebSocketImpl'])throw new Error('No\x20WebSocket\x20implementation\x20provided');}['connect'](_0x8a4d4){return new Promise((_0x2b618c,_0x46deda)=>{this['socket']=new this['WebSocketImpl'](_0x8a4d4),'undefined'!=typeof window&&(this['socket']['binaryType']='arrayBuffer'),this['_on']('open',()=>_0x2b618c(!0x0)),this['_on']('error',_0x536b69=>{console['error']('WS\x20error',_0x536b69),_0x46deda(_0x536b69);}),this['_on']('close',_0x4098fc=>{const _0x2b892d=null==_0x4098fc?void 0x0:_0x4098fc['code'],_0x419a31=null==_0x4098fc?void 0x0:_0x4098fc['reason'];this['emit']('close',{'code':_0x2b892d,'reason':_0x419a31});}),this['_on']('message',async(_0x194773,_0x4f6852)=>{var _0x41fd56,_0x117ebe=null!==(_0x41fd56=null==_0x194773?void 0x0:_0x194773['data'])&&void 0x0!==_0x41fd56?_0x41fd56:_0x194773;'undefined'!=typeof Blob&&_0x117ebe instanceof Blob&&(_0x117ebe=await _0x117ebe['arrayBuffer']());const _0x4f3197=(0x0,frame_js_1['decodePacket'])(_0x117ebe);_0x4f3197&&this['emit']('message',_0x4f3197,_0x4f6852);});});}['_on'](_0x39a410,_0x38109e){this['socket']['on']?this['socket']['on'](_0x39a410,_0x38109e):this['socket']['on'+_0x39a410]=_0x38109e;}['send'](_0x986199){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x986199);}['sendBinary'](_0x4f2957){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x4f2957);}['isOpen'](){return this['socket']&&this['socket']['readyState']===this['WebSocketImpl']['OPEN'];}['close'](){var _0x201725;null===(_0x201725=this['socket'])||void 0x0===_0x201725||_0x201725['close']();}}exports['WebsocketTransport']=WebsocketTransport;
@@ -1 +1 @@
1
- 'use strict';function toUint8(_0x1b2007){return'undefined'!=typeof Buffer?Buffer['from'](_0x1b2007,'utf8'):new TextEncoder()['encode'](_0x1b2007);}Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['toUint8']=toUint8;
1
+ 'use strict';function toUint8(_0x1dfe92){return'undefined'!=typeof Buffer?Buffer['from'](_0x1dfe92,'utf8'):new TextEncoder()['encode'](_0x1dfe92);}Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['toUint8']=toUint8;
@@ -1 +1 @@
1
- 'use strict';function framePacket(_0x2c9595,_0xa5c916){const _0x46f17f=Buffer['from'](JSON['stringify'](_0x2c9595)),_0x5cf100=Buffer['isBuffer'](_0xa5c916)?_0xa5c916:Buffer['from'](_0xa5c916),_0x5b0cfd=Buffer['alloc'](0x4+_0x46f17f['length']+0x4+_0x5cf100['length']);let _0x232436=0x0;return _0x5b0cfd['writeUInt32BE'](_0x46f17f['length'],_0x232436),_0x232436+=0x4,_0x46f17f['copy'](_0x5b0cfd,_0x232436),_0x232436+=_0x46f17f['length'],_0x5b0cfd['writeUInt32BE'](_0x5cf100['length'],_0x232436),_0x232436+=0x4,_0x5cf100['copy'](_0x5b0cfd,_0x232436),_0x5b0cfd;}Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['framePacket']=framePacket,exports['decodePacket']=decodePacket;let internalBuffer=Buffer['alloc'](0x0);function decodePacket(_0x45abbc){try{Buffer['isBuffer'](_0x45abbc)||(_0x45abbc=Buffer['from'](_0x45abbc)),internalBuffer=Buffer['concat']([internalBuffer,_0x45abbc]);let _0x36b70e=0x0;if(internalBuffer['length']<0x4)return null;const _0x3f61e5=internalBuffer['readUInt32BE'](_0x36b70e);if(_0x36b70e+=0x4,internalBuffer['length']<_0x36b70e+_0x3f61e5)return null;const _0x4b5b68=internalBuffer['slice'](_0x36b70e,_0x36b70e+_0x3f61e5),_0x157bd3=JSON['parse'](_0x4b5b68['toString']());if(_0x36b70e+=_0x3f61e5,internalBuffer['length']<_0x36b70e+0x4)return null;const _0x58ade7=internalBuffer['readUInt32BE'](_0x36b70e);if(_0x36b70e+=0x4,internalBuffer['length']<_0x36b70e+_0x58ade7)return null;const _0x26cfcb=internalBuffer['slice'](_0x36b70e,_0x36b70e+_0x58ade7);_0x36b70e+=_0x58ade7,internalBuffer=internalBuffer['slice'](_0x36b70e);let _0x345dd9=_0x26cfcb;if('text'===_0x157bd3['type'])try{_0x345dd9=JSON['parse'](_0x26cfcb['toString']('utf8'));}catch{_0x345dd9=_0x26cfcb;}return{'metadata':_0x157bd3,'data':_0x345dd9};}catch(_0x525d66){return console['error']('decodePacket\x20error:',_0x525d66),null;}}
1
+ 'use strict';function framePacket(_0x5c121e,_0x1751a7){const _0x15bc6e=Buffer['from'](JSON['stringify'](_0x5c121e)),_0x5b1ed5=Buffer['isBuffer'](_0x1751a7)?_0x1751a7:Buffer['from'](_0x1751a7),_0xb6e358=Buffer['alloc'](0x4+_0x15bc6e['length']+0x4+_0x5b1ed5['length']);let _0x271585=0x0;return _0xb6e358['writeUInt32BE'](_0x15bc6e['length'],_0x271585),_0x271585+=0x4,_0x15bc6e['copy'](_0xb6e358,_0x271585),_0x271585+=_0x15bc6e['length'],_0xb6e358['writeUInt32BE'](_0x5b1ed5['length'],_0x271585),_0x271585+=0x4,_0x5b1ed5['copy'](_0xb6e358,_0x271585),_0xb6e358;}Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['framePacket']=framePacket,exports['decodePacket']=decodePacket;let internalBuffer=Buffer['alloc'](0x0);function decodePacket(_0x775088){try{Buffer['isBuffer'](_0x775088)||(_0x775088=Buffer['from'](_0x775088)),internalBuffer=Buffer['concat']([internalBuffer,_0x775088]);let _0x5e6ba2=0x0;if(internalBuffer['length']<0x4)return null;const _0x545b2a=internalBuffer['readUInt32BE'](_0x5e6ba2);if(_0x5e6ba2+=0x4,internalBuffer['length']<_0x5e6ba2+_0x545b2a)return null;const _0x300b09=internalBuffer['slice'](_0x5e6ba2,_0x5e6ba2+_0x545b2a),_0x4e5bcb=JSON['parse'](_0x300b09['toString']());if(_0x5e6ba2+=_0x545b2a,internalBuffer['length']<_0x5e6ba2+0x4)return null;const _0x35c1f8=internalBuffer['readUInt32BE'](_0x5e6ba2);if(_0x5e6ba2+=0x4,internalBuffer['length']<_0x5e6ba2+_0x35c1f8)return null;const _0x3a5523=internalBuffer['slice'](_0x5e6ba2,_0x5e6ba2+_0x35c1f8);_0x5e6ba2+=_0x35c1f8,internalBuffer=internalBuffer['slice'](_0x5e6ba2);let _0x223e6a=_0x3a5523;if('text'===_0x4e5bcb['type'])try{_0x223e6a=JSON['parse'](_0x3a5523['toString']('utf8'));}catch{_0x223e6a=_0x3a5523;}return{'metadata':_0x4e5bcb,'data':_0x223e6a};}catch(_0x28318a){return console['error']('decodePacket\x20error:',_0x28318a),null;}}
@@ -1 +1 @@
1
- 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['prefixId']=void 0x0;const prefixId=(_0x580949,_0x33a3a7)=>_0x33a3a7['includes'](':')?_0x33a3a7:_0x580949+':'+_0x33a3a7;exports['prefixId']=prefixId;
1
+ 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['prefixId']=void 0x0;const prefixId=(_0x23dd5d,_0x57f943)=>_0x57f943['includes'](':')?_0x57f943:_0x23dd5d+':'+_0x57f943;exports['prefixId']=prefixId;
@@ -1 +1 @@
1
- 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['Payload']=void 0x0;class Payload{constructor(_0xb5e082){this['buffer']=_0xb5e082;}['raw'](){return this['buffer'];}['toString'](_0x13189f='utf8'){return this['buffer']['toString'](_0x13189f);}['toJSON'](_0x2223f9='utf8'){try{return JSON['parse'](this['buffer']['toString'](_0x2223f9));}catch(_0x1559fc){return null;}}['toBase64'](){return this['buffer']['toString']('base64');}}exports['Payload']=Payload;
1
+ 'use strict';Object['defineProperty'](exports,'__esModule',{'value':!0x0}),exports['Payload']=void 0x0;class Payload{constructor(_0x5e7ac3){this['buffer']=_0x5e7ac3;}['raw'](){return this['buffer'];}['toString'](_0x445e97='utf8'){return this['buffer']['toString'](_0x445e97);}['toJSON'](_0x541f3c='utf8'){try{return JSON['parse'](this['buffer']['toString'](_0x541f3c));}catch(_0x22ff8c){return null;}}['toBase64'](){return this['buffer']['toString']('base64');}}exports['Payload']=Payload;
@@ -33,6 +33,7 @@ __export(dnotifier_exports, {
33
33
  DNotifier: () => DNotifier
34
34
  });
35
35
  module.exports = __toCommonJS(dnotifier_exports);
36
+ var import_crypto = __toESM(require("crypto"), 1);
36
37
 
37
38
  // src/transport/httpTransport.js
38
39
  var HttpTransport = class {
@@ -235,83 +236,9 @@ function toUint8(data) {
235
236
  return new TextEncoder().encode(data);
236
237
  }
237
238
 
238
- // src/providers/openai.js
239
- var DEFAULT_BASE_URL = "https://api.openai.com";
240
- var DEFAULT_MODEL = "gpt-4o-mini";
241
- function normalizeMessages(message) {
242
- if (typeof message === "string") {
243
- return [{ role: "user", content: message }];
244
- }
245
- if (Array.isArray(message) && message.length > 0) {
246
- return message.map((m) => ({
247
- role: m.role || "user",
248
- content: typeof m.content === "string" ? m.content : String(m.content)
249
- }));
250
- }
251
- throw new Error("message must be a non-empty string or array of { role, content }");
252
- }
253
- async function chatStream(options) {
254
- const {
255
- apiKey,
256
- model = DEFAULT_MODEL,
257
- messages,
258
- baseUrl = DEFAULT_BASE_URL,
259
- onChunk
260
- } = options;
261
- if (!apiKey || !messages) {
262
- throw new Error("openai.chatStream: apiKey and messages are required");
263
- }
264
- const url = `${baseUrl.replace(/\/$/, "")}/v1/chat/completions`;
265
- const body = {
266
- model,
267
- messages: normalizeMessages(messages),
268
- stream: true
269
- };
270
- const res = await fetch(url, {
271
- method: "POST",
272
- headers: {
273
- "Content-Type": "application/json",
274
- Authorization: `Bearer ${apiKey}`
275
- },
276
- body: JSON.stringify(body)
277
- });
278
- if (!res.ok) {
279
- const raw = await res.json().catch(() => ({}));
280
- const err = new Error(raw?.error?.message || `OpenAI API error: ${res.status}`);
281
- err.status = res.status;
282
- err.code = raw?.error?.code;
283
- throw err;
284
- }
285
- const reader = res.body.getReader();
286
- const decoder = new TextDecoder();
287
- let content = "";
288
- let buffer = "";
289
- while (true) {
290
- const { done, value } = await reader.read();
291
- if (done) break;
292
- buffer += decoder.decode(value, { stream: true });
293
- const lines = buffer.split("\n");
294
- buffer = lines.pop() || "";
295
- for (const line of lines) {
296
- const trimmed = line.trim();
297
- if (trimmed === "" || trimmed === "data: [DONE]") continue;
298
- if (!trimmed.startsWith("data: ")) continue;
299
- try {
300
- const json = JSON.parse(trimmed.slice(6));
301
- const delta = json?.choices?.[0]?.delta?.content;
302
- if (typeof delta === "string") {
303
- content += delta;
304
- if (onChunk) onChunk(delta);
305
- }
306
- } catch (_) {
307
- }
308
- }
309
- }
310
- return { content };
311
- }
312
-
313
239
  // src/dnotifier.js
314
240
  var _defaultWsUrl = "wss://api.dnotifier.com/ecs/ws";
241
+ var _defaultAuthUrl = "https://api.dnotifier.com/ecs/api/v1/sdk/auth";
315
242
  var DNotifier = class {
316
243
  constructor({
317
244
  appId,
@@ -344,7 +271,7 @@ var DNotifier = class {
344
271
  }
345
272
  async connect() {
346
273
  const authServer = new HttpTransport({
347
- url: "https://api.dnotifier.com/ecs/api/v1/sdk/auth"
274
+ url: _defaultAuthUrl
348
275
  });
349
276
  const res = await authServer.send({
350
277
  appId: this.appId,
@@ -357,8 +284,9 @@ var DNotifier = class {
357
284
  }
358
285
  throw new Error("Authentication Failed.");
359
286
  }
287
+ this._authResponse = res._responseData;
360
288
  this.authToken = res._responseData.token;
361
- this.aiEnabled = res._responseData.limits.aiEnabled;
289
+ this.aiEnabled = res._responseData.limits?.aiEnabled ?? false;
362
290
  await this.transport.connect(this.url);
363
291
  const metadata = {
364
292
  type: "text",
@@ -416,6 +344,26 @@ var DNotifier = class {
416
344
  });
417
345
  });
418
346
  }
347
+ /**
348
+ * Returns the plan limits from the last successful authentication.
349
+ * @returns {{ messagesHardLimit: number, maxRequestsPerMonth: number, maxAIWordsPerMonth: number, knowledgeBaseMaxWords: number, aiEnabled: boolean, maxUsers: number, maxRowsPerUser: number } | null}
350
+ * @throws {Error} If not connected.
351
+ */
352
+ getPlanLimits() {
353
+ if (!this.isConnected) throw new Error("Not connected");
354
+ const limits = this._authResponse?.limits;
355
+ if (!limits) return null;
356
+ const aiEnabled = limits.aiEnabled ?? false;
357
+ return {
358
+ messagesHardLimit: limits.messagesHardLimit,
359
+ maxAIRequestsPerMonth: aiEnabled ? limits.ai_limits?.maxRequestsPerMonth ?? 0 : 0,
360
+ maxAIWordsPerMonth: aiEnabled ? limits.ai_limits?.maxAIWordsPerMonth ?? 0 : 0,
361
+ knowledgeBaseMaxWords: aiEnabled ? limits.knowledge_base_limits?.maxWords ?? 0 : 0,
362
+ aiEnabled,
363
+ maxUsers: limits.db_storage_limits?.maxUsers ?? 0,
364
+ maxRowsPerUser: limits.db_storage_limits?.maxRowsPerUser ?? 0
365
+ };
366
+ }
419
367
  send({ senderId, receiverId, receiverIds, data }) {
420
368
  if (!this.isConnected) throw new Error("Not connected");
421
369
  if (!senderId) throw new Error("senderId required");
@@ -423,6 +371,7 @@ var DNotifier = class {
423
371
  throw new Error("receiverId or receiverIds required");
424
372
  const receivers = receiverIds ?? [receiverId];
425
373
  const metadata = {
374
+ id: import_crypto.default.randomUUID(),
426
375
  type: "text",
427
376
  code: 200,
428
377
  appId: this.appId,
@@ -437,6 +386,45 @@ var DNotifier = class {
437
386
  const frame = framePacket(metadata, payloadBuffer);
438
387
  this.transport.sendBinary(frame);
439
388
  }
389
+ /**
390
+ * Send a message to the AI pipeline.
391
+ * @param {Object} options
392
+ * @param {string} options.senderId - Sender user id.
393
+ * @param {string | object} options.message - Message or payload to send to AI.
394
+ */
395
+ sendAI({ senderId, message }) {
396
+ if (!this.isConnected) throw new Error("Not connected");
397
+ if (!senderId) throw new Error("senderId required");
398
+ const metadata = {
399
+ id: import_crypto.default.randomUUID(),
400
+ type: "ai",
401
+ code: 300,
402
+ appId: this.appId,
403
+ sender: prefixId(this.appId, senderId),
404
+ receiverId: prefixId(this.appId, senderId),
405
+ timestamp: Date.now(),
406
+ token: this.authToken
407
+ };
408
+ const payloadBuffer = toUint8(JSON.stringify(message));
409
+ const frame = framePacket(metadata, payloadBuffer);
410
+ this.transport.sendBinary(frame);
411
+ }
412
+ fetchAIHistory({ senderId }) {
413
+ if (!this.isConnected) throw new Error("Not connected");
414
+ if (!senderId) throw new Error("senderId required");
415
+ const metadata = {
416
+ id: import_crypto.default.randomUUID(),
417
+ type: "ai",
418
+ code: 301,
419
+ appId: this.appId,
420
+ sender: prefixId(this.appId, senderId),
421
+ timestamp: Date.now(),
422
+ token: this.authToken
423
+ };
424
+ const payloadBuffer = toUint8(JSON.stringify({}));
425
+ const frame = framePacket(metadata, payloadBuffer);
426
+ this.transport.sendBinary(frame);
427
+ }
440
428
  sendBinary({ senderId, receiverIds, buffer, type = "binary" }) {
441
429
  if (!this.isConnected) throw new Error("Not connected");
442
430
  if (!senderId) throw new Error("senderId required");
@@ -467,31 +455,47 @@ var DNotifier = class {
467
455
  * @param {string} [options.baseUrl] - OpenAI-compatible API base URL (e.g. Azure/custom).
468
456
  * @returns {Promise<{ content: string }>} Resolves with the full assistant content when done.
469
457
  */
458
+ // async sendWithOpenAI({ senderId, message, openaiApiKey, model, baseUrl }) {
459
+ // if (!this.aiEnabled) throw new Error("AI is not enabled for this subscription.");
460
+ // if (!this.isConnected) throw new Error("Not connected");
461
+ // if (!senderId) throw new Error("senderId is required");
462
+ // if (!message) throw new Error("message is required");
463
+ // if (!openaiApiKey) throw new Error("openaiApiKey is required");
464
+ // const { content } = await openaiChatStream({
465
+ // apiKey: openaiApiKey,
466
+ // model,
467
+ // messages: message,
468
+ // baseUrl,
469
+ // });
470
+ // const data = {
471
+ // type: "openai",
472
+ // content,
473
+ // ts: Date.now(),
474
+ // };
475
+ // this.onMessageCallback({
476
+ // metadata: {
477
+ // sender: prefixId(this.appId, senderId),
478
+ // timestamp: Date.now(),
479
+ // },
480
+ // payload: new Payload(toUint8(JSON.stringify(data))),
481
+ // });
482
+ // return { content };
483
+ // }
484
+ /**
485
+ * Send a message to the AI pipeline. This method still works but is deprecated.
486
+ * Use {@link sendAI} instead. This function will be removed in a future release.
487
+ *
488
+ * @deprecated Use sendAI({ senderId, message }) instead. Will be removed in a future release.
489
+ * @param {Object} options
490
+ * @param {string} options.senderId - Sender user id.
491
+ * @param {string | object} options.message - Message or payload to send to AI.
492
+ */
470
493
  async sendWithOpenAI({ senderId, message, openaiApiKey, model, baseUrl }) {
471
494
  if (!this.aiEnabled) throw new Error("AI is not enabled for this subscription.");
472
495
  if (!this.isConnected) throw new Error("Not connected");
473
496
  if (!senderId) throw new Error("senderId is required");
474
497
  if (!message) throw new Error("message is required");
475
- if (!openaiApiKey) throw new Error("openaiApiKey is required");
476
- const { content } = await chatStream({
477
- apiKey: openaiApiKey,
478
- model,
479
- messages: message,
480
- baseUrl
481
- });
482
- const data = {
483
- type: "openai",
484
- content,
485
- ts: Date.now()
486
- };
487
- this.onMessageCallback({
488
- metadata: {
489
- sender: prefixId(this.appId, senderId),
490
- timestamp: Date.now()
491
- },
492
- payload: new Payload(toUint8(JSON.stringify(data)))
493
- });
494
- return { content };
498
+ this.sendAI({ senderId, message });
495
499
  }
496
500
  };
497
501
  // Annotate the CommonJS export names for ESM import in node:
@@ -21,13 +21,30 @@ export class DNotifier {
21
21
  aiEnabled: boolean;
22
22
  transport: HttpTransport | WebsocketTransport;
23
23
  connect(): Promise<void>;
24
+ _authResponse: any;
24
25
  authToken: any;
26
+ getPlanLimits(): {
27
+ messagesHardLimit: number;
28
+ maxRequestsPerMonth: number;
29
+ maxAIWordsPerMonth: number;
30
+ knowledgeBaseMaxWords: number;
31
+ aiEnabled: boolean;
32
+ maxUsers: number;
33
+ maxRowsPerUser: number;
34
+ } | null;
25
35
  send({ senderId, receiverId, receiverIds, data }: {
26
36
  senderId: any;
27
37
  receiverId: any;
28
38
  receiverIds: any;
29
39
  data: any;
30
40
  }): void;
41
+ sendAI({ senderId, message }: {
42
+ senderId: string;
43
+ message: string | object;
44
+ }): void;
45
+ fetchAIHistory({ senderId }: {
46
+ senderId: any;
47
+ }): void;
31
48
  sendBinary({ senderId, receiverIds, buffer, type }: {
32
49
  senderId: any;
33
50
  receiverIds: any;
@@ -36,16 +53,8 @@ export class DNotifier {
36
53
  }): void;
37
54
  sendWithOpenAI({ senderId, message, openaiApiKey, model, baseUrl }: {
38
55
  senderId: string;
39
- message: string | Array<{
40
- role: string;
41
- content: string;
42
- }>;
43
- openaiApiKey: string;
44
- model?: string | undefined;
45
- baseUrl?: string | undefined;
46
- }): Promise<{
47
- content: string;
48
- }>;
56
+ message: string | object;
57
+ }): Promise<void>;
49
58
  }
50
59
  import { HttpTransport } from "./transport/httpTransport.js";
51
60
  import { WebsocketTransport } from "./transport/websocketTransport.js";
@@ -1 +1 @@
1
- {"version":3,"file":"dnotifier.d.ts","sourceRoot":"","sources":["../src/dnotifier.js"],"names":[],"mappings":"AAUA;IACE;;;;;;;;;;OA+BC;IAhBC,WAAkB;IAClB,YAAoB;IACpB,YAAoB;IACpB,SAA+B;IAE/B,yBAAoD;IACpD,uBAAgD;IAChD,4BAA0D;IAE1D,qBAAwB;IACxB,mBAAsB;IAEtB,8CAGyB;IAG3B,yBA8FC;IA3EC,eAAwC;IA6E1C;;;;;aAwBC;IAED;;;;;aAmBC;IAcD,oEAPG;QAAwB,QAAQ,EAAxB,MAAM;QACmD,OAAO,EAAhE,MAAM,GAAG,KAAK,CAAC;YAAC,IAAI,EAAE,MAAM,CAAC;YAAC,OAAO,EAAE,MAAM,CAAA;SAAC,CAAC;QAC/B,YAAY,EAA5B,MAAM;QACW,KAAK;QACL,OAAO;KAChC,GAAU,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CA8BxC;CACF;8BApO6B,8BAA8B;mCACzB,mCAAmC"}
1
+ {"version":3,"file":"dnotifier.d.ts","sourceRoot":"","sources":["../src/dnotifier.js"],"names":[],"mappings":"AAaA;IACE;;;;;;;;;;OA+BC;IAhBC,WAAkB;IAClB,YAAoB;IACpB,YAAoB;IACpB,SAA+B;IAE/B,yBAAoD;IACpD,uBAAgD;IAChD,4BAA0D;IAE1D,qBAAwB;IACxB,mBAAsB;IAEtB,8CAGyB;IAG3B,yBA+FC;IA5EC,mBAAsC;IACtC,eAAwC;IAkF1C,iBAHa;QAAE,iBAAiB,EAAE,MAAM,CAAC;QAAC,mBAAmB,EAAE,MAAM,CAAC;QAAC,kBAAkB,EAAE,MAAM,CAAC;QAAC,qBAAqB,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,OAAO,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,cAAc,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAiBtM;IAED;;;;;aAyBC;IAQD,8BAHG;QAAwB,QAAQ,EAAxB,MAAM;QACmB,OAAO,EAAhC,MAAM,GAAG,MAAM;KACzB,QAoBA;IAED;;aAkBC;IAED;;;;;aAmBC;IAqDD,oEAHG;QAAwB,QAAQ,EAAxB,MAAM;QACmB,OAAO,EAAhC,MAAM,GAAG,MAAM;KACzB,iBAQA;CACF;8BA9T6B,8BAA8B;mCACzB,mCAAmC"}
package/dist/dnotifier.js CHANGED
@@ -1 +1 @@
1
- import{HttpTransport}from'./transport/httpTransport.js';import{WebsocketTransport}from'./transport/websocketTransport.js';import{prefixId}from'./utils/id.js';import{framePacket}from'./utils/frame.js';import{Payload}from'./utils/payload.js';import{toUint8}from'./utils/buffer.js';import{chatStream as _0x506236}from'./providers/openai.js';const _defaultWsUrl='wss://api.dnotifier.com/ecs/ws';export class DNotifier{constructor({appId:_0x559735,secret:_0x38d7d7,transport:_0x4c0091,userId:_0x2340ab,url:_0x43d78f,onConnected:_0xdd1575,onMessage:_0x5bf1b4,onDisconnected:_0x1933b2,WebSocketImpl:_0x1d4f45}){if(!(_0x559735&&_0x38d7d7&&_0x4c0091&&_0x2340ab))throw new Error('appId,\x20secret,\x20transport,\x20and\x20userId\x20are\x20required');this['appId']=_0x559735,this['secret']=_0x38d7d7,this['userId']=_0x2340ab,this['url']=null!=_0x43d78f?_0x43d78f:_defaultWsUrl,this['onConnectedCallback']=_0xdd1575||(()=>{}),this['onMessageCallback']=_0x5bf1b4||(()=>{}),this['onDisconnectedCallback']=_0x1933b2||(()=>{}),this['isConnected']=!0x1,this['aiEnabled']=!0x1,this['transport']='ws'===_0x4c0091?new WebsocketTransport(_0x1d4f45):new HttpTransport();}async['connect'](){const _0xae2fd6=new HttpTransport({'url':'https://api.dnotifier.com/ecs/api/v1/sdk/auth'}),_0x4a4290=await _0xae2fd6['send']({'appId':this['appId'],'appSecret':this['secret'],'userID':this['userId']});if(0xc8!==_0x4a4290['_responseCode']){if(0x190===_0x4a4290['_responseCode'])throw new Error(_0x4a4290['_responseData']);throw new Error('Authentication\x20Failed.');}this['authToken']=_0x4a4290['_responseData']['token'],this['aiEnabled']=_0x4a4290['_responseData']['limits']['aiEnabled'],await this['transport']['connect'](this['url']);const _0x3016b9={'type':'text','code':0x65,'appId':this['appId'],'sender':prefixId(this['appId'],this['userId']),'timestamp':Date['now'](),'token':this['authToken']},_0x17711b=toUint8(JSON['stringify']({'token':this['authToken']})),_0x5f4a64=framePacket(_0x3016b9,_0x17711b);this['transport']['sendBinary'](_0x5f4a64);let _0x48fa37=!0x1;this['transport']['on']('message',_0x525cde=>{try{const {metadata:_0x1f87d5,data:_0x5c8988}=_0x525cde;if(!_0x48fa37&&'102'===_0x1f87d5['packet']['code']&&_0x1f87d5['packet']['sender']['includes'](prefixId(this['appId'],this['userId'])))return _0x48fa37=!0x0,this['isConnected']=!0x0,void this['onConnectedCallback']();_0x48fa37&&this['onMessageCallback']({'metadata':{'sender':_0x1f87d5['packet']['sender'],'timestamp':_0x1f87d5['packet']['timestamp']},'payload':new Payload(_0x5c8988)});}catch(_0x2a6d2c){console['error']('Packet\x20error:',_0x2a6d2c);}}),this['transport']['on']('close',({code:_0x4e7d76,reason:_0x5d4923})=>{const _0x514739=this['isConnected'];this['isConnected']=!0x1,_0x514739&&this['onDisconnectedCallback']&&this['onDisconnectedCallback']({'code':_0x4e7d76,'reason':_0x5d4923||'Disconnected'});}),this['transport']['on']('error',_0x42e790=>{this['isConnected']=!0x1,this['onDisconnectedCallback']({'code':0x1f4,'reason':(null==_0x42e790?void 0x0:_0x42e790['message'])||'Transport\x20error'});});}['send']({senderId:_0x407504,receiverId:_0x488569,receiverIds:_0x5a40a8,data:_0x47a4a7}){if(!this['isConnected'])throw new Error('Not\x20connected');if(!_0x407504)throw new Error('senderId\x20required');if(!_0x488569&&!_0x5a40a8)throw new Error('receiverId\x20or\x20receiverIds\x20required');const _0xd5a87c=null!=_0x5a40a8?_0x5a40a8:[_0x488569],_0x35f6c9={'type':'text','code':0xc8,'appId':this['appId'],'sender':prefixId(this['appId'],_0x407504),'receivers':_0xd5a87c['map'](_0x533bea=>prefixId(this['appId'],_0x533bea)),'timestamp':Date['now'](),'token':this['authToken']},_0x3f30e6=toUint8(JSON['stringify'](_0x47a4a7)),_0x37f221=framePacket(_0x35f6c9,_0x3f30e6);this['transport']['sendBinary'](_0x37f221);}['sendBinary']({senderId:_0x51af4e,receiverIds:_0x22383e,buffer:_0x11ce92,type:_0x2f39da='binary'}){if(!this['isConnected'])throw new Error('Not\x20connected');if(!_0x51af4e)throw new Error('senderId\x20required');if(!_0x11ce92)throw new Error('buffer\x20is\x20required');const _0x4b5234={'type':_0x2f39da,'code':0xca,'appId':this['appId'],'sender':prefixId(this['appId'],_0x51af4e),'receivers':_0x22383e['map'](_0x4f4be7=>prefixId(this['appId'],_0x4f4be7)),'timestamp':Date['now'](),'token':this['authToken']},_0x19a17e=framePacket(_0x4b5234,_0x11ce92);this['transport']['sendBinary'](_0x19a17e);}async['sendWithOpenAI']({senderId:_0x3cd6b3,message:_0x2e6517,openaiApiKey:_0x2ee89b,model:_0x4b92c7,baseUrl:_0x2c4234}){if(!this['aiEnabled'])throw new Error('AI\x20is\x20not\x20enabled\x20for\x20this\x20subscription.');if(!this['isConnected'])throw new Error('Not\x20connected');if(!_0x3cd6b3)throw new Error('senderId\x20is\x20required');if(!_0x2e6517)throw new Error('message\x20is\x20required');if(!_0x2ee89b)throw new Error('openaiApiKey\x20is\x20required');const {content:_0x5360bb}=await _0x506236({'apiKey':_0x2ee89b,'model':_0x4b92c7,'messages':_0x2e6517,'baseUrl':_0x2c4234}),_0x9dadcb={'type':'openai','content':_0x5360bb,'ts':Date['now']()};return this['onMessageCallback']({'metadata':{'sender':prefixId(this['appId'],_0x3cd6b3),'timestamp':Date['now']()},'payload':new Payload(toUint8(JSON['stringify'](_0x9dadcb)))}),{'content':_0x5360bb};}}
1
+ import _0x31f4e1 from'crypto';import{HttpTransport}from'./transport/httpTransport.js';import{WebsocketTransport}from'./transport/websocketTransport.js';import{prefixId}from'./utils/id.js';import{framePacket}from'./utils/frame.js';import{Payload}from'./utils/payload.js';import{toUint8}from'./utils/buffer.js';import{chatStream as _0x3ae2da}from'./providers/openai.js';const _defaultWsUrl='wss://api.dnotifier.com/ecs/ws',_defaultAuthUrl='https://api.dnotifier.com/ecs/api/v1/sdk/auth';export class DNotifier{constructor({appId:_0x109ce3,secret:_0x380fe0,transport:_0x85713a,userId:_0x5e1876,url:_0x2e666c,onConnected:_0x17fb61,onMessage:_0x39095f,onDisconnected:_0x176849,WebSocketImpl:_0x24048a}){if(!(_0x109ce3&&_0x380fe0&&_0x85713a&&_0x5e1876))throw new Error('appId,\x20secret,\x20transport,\x20and\x20userId\x20are\x20required');this['appId']=_0x109ce3,this['secret']=_0x380fe0,this['userId']=_0x5e1876,this['url']=null!=_0x2e666c?_0x2e666c:_defaultWsUrl,this['onConnectedCallback']=_0x17fb61||(()=>{}),this['onMessageCallback']=_0x39095f||(()=>{}),this['onDisconnectedCallback']=_0x176849||(()=>{}),this['isConnected']=!0x1,this['aiEnabled']=!0x1,this['transport']='ws'===_0x85713a?new WebsocketTransport(_0x24048a):new HttpTransport();}async['connect'](){var _0x34b910,_0xfa55d4;const _0x5e6211=new HttpTransport({'url':_defaultAuthUrl}),_0x39dbf5=await _0x5e6211['send']({'appId':this['appId'],'appSecret':this['secret'],'userID':this['userId']});if(0xc8!==_0x39dbf5['_responseCode']){if(0x190===_0x39dbf5['_responseCode'])throw new Error(_0x39dbf5['_responseData']);throw new Error('Authentication\x20Failed.');}this['_authResponse']=_0x39dbf5['_responseData'],this['authToken']=_0x39dbf5['_responseData']['token'],this['aiEnabled']=null!==(_0xfa55d4=null===(_0x34b910=_0x39dbf5['_responseData']['limits'])||void 0x0===_0x34b910?void 0x0:_0x34b910['aiEnabled'])&&void 0x0!==_0xfa55d4&&_0xfa55d4,await this['transport']['connect'](this['url']);const _0x3d7cf5={'type':'text','code':0x65,'appId':this['appId'],'sender':prefixId(this['appId'],this['userId']),'timestamp':Date['now'](),'token':this['authToken']},_0x450c17=toUint8(JSON['stringify']({'token':this['authToken']})),_0x2181cb=framePacket(_0x3d7cf5,_0x450c17);this['transport']['sendBinary'](_0x2181cb);let _0x40c1cf=!0x1;this['transport']['on']('message',_0x4e2cad=>{try{const {metadata:_0x4bcff3,data:_0x4d3d04}=_0x4e2cad;if(!_0x40c1cf&&'102'===_0x4bcff3['packet']['code']&&_0x4bcff3['packet']['sender']['includes'](prefixId(this['appId'],this['userId'])))return _0x40c1cf=!0x0,this['isConnected']=!0x0,void this['onConnectedCallback']();_0x40c1cf&&this['onMessageCallback']({'metadata':{'sender':_0x4bcff3['packet']['sender'],'timestamp':_0x4bcff3['packet']['timestamp']},'payload':new Payload(_0x4d3d04)});}catch(_0x446923){console['error']('Packet\x20error:',_0x446923);}}),this['transport']['on']('close',({code:_0x561577,reason:_0x151919})=>{const _0x478ac8=this['isConnected'];this['isConnected']=!0x1,_0x478ac8&&this['onDisconnectedCallback']&&this['onDisconnectedCallback']({'code':_0x561577,'reason':_0x151919||'Disconnected'});}),this['transport']['on']('error',_0x7d2d5d=>{this['isConnected']=!0x1,this['onDisconnectedCallback']({'code':0x1f4,'reason':(null==_0x7d2d5d?void 0x0:_0x7d2d5d['message'])||'Transport\x20error'});});}['getPlanLimits'](){var _0x10663f,_0x398735,_0x582ff1,_0x3ffc95,_0x1ea032,_0x4d4fbc,_0x57d0b6,_0x399c1e,_0x34ef6d,_0x41026b,_0x120a3b,_0x1540e2;if(!this['isConnected'])throw new Error('Not\x20connected');const _0x2a2511=null===(_0x10663f=this['_authResponse'])||void 0x0===_0x10663f?void 0x0:_0x10663f['limits'];if(!_0x2a2511)return null;const _0x490feb=null!==(_0x398735=_0x2a2511['aiEnabled'])&&void 0x0!==_0x398735&&_0x398735;return{'messagesHardLimit':_0x2a2511['messagesHardLimit'],'maxAIRequestsPerMonth':_0x490feb&&null!==(_0x3ffc95=null===(_0x582ff1=_0x2a2511['ai_limits'])||void 0x0===_0x582ff1?void 0x0:_0x582ff1['maxRequestsPerMonth'])&&void 0x0!==_0x3ffc95?_0x3ffc95:0x0,'maxAIWordsPerMonth':_0x490feb&&null!==(_0x4d4fbc=null===(_0x1ea032=_0x2a2511['ai_limits'])||void 0x0===_0x1ea032?void 0x0:_0x1ea032['maxAIWordsPerMonth'])&&void 0x0!==_0x4d4fbc?_0x4d4fbc:0x0,'knowledgeBaseMaxWords':_0x490feb&&null!==(_0x399c1e=null===(_0x57d0b6=_0x2a2511['knowledge_base_limits'])||void 0x0===_0x57d0b6?void 0x0:_0x57d0b6['maxWords'])&&void 0x0!==_0x399c1e?_0x399c1e:0x0,'aiEnabled':_0x490feb,'maxUsers':null!==(_0x41026b=null===(_0x34ef6d=_0x2a2511['db_storage_limits'])||void 0x0===_0x34ef6d?void 0x0:_0x34ef6d['maxUsers'])&&void 0x0!==_0x41026b?_0x41026b:0x0,'maxRowsPerUser':null!==(_0x1540e2=null===(_0x120a3b=_0x2a2511['db_storage_limits'])||void 0x0===_0x120a3b?void 0x0:_0x120a3b['maxRowsPerUser'])&&void 0x0!==_0x1540e2?_0x1540e2:0x0};}['send']({senderId:_0xc44f25,receiverId:_0x33ad69,receiverIds:_0x514142,data:_0x1c96fd}){if(!this['isConnected'])throw new Error('Not\x20connected');if(!_0xc44f25)throw new Error('senderId\x20required');if(!_0x33ad69&&!_0x514142)throw new Error('receiverId\x20or\x20receiverIds\x20required');const _0x5e33c5=null!=_0x514142?_0x514142:[_0x33ad69],_0x2546ad={'id':_0x31f4e1['randomUUID'](),'type':'text','code':0xc8,'appId':this['appId'],'sender':prefixId(this['appId'],_0xc44f25),'receivers':_0x5e33c5['map'](_0x396c8b=>prefixId(this['appId'],_0x396c8b)),'timestamp':Date['now'](),'token':this['authToken']},_0x45ba34=toUint8(JSON['stringify'](_0x1c96fd)),_0x2cc662=framePacket(_0x2546ad,_0x45ba34);this['transport']['sendBinary'](_0x2cc662);}['sendAI']({senderId:_0x1e8d18,message:_0x33ff1d}){if(!this['isConnected'])throw new Error('Not\x20connected');if(!_0x1e8d18)throw new Error('senderId\x20required');const _0x1fa398={'id':_0x31f4e1['randomUUID'](),'type':'ai','code':0x12c,'appId':this['appId'],'sender':prefixId(this['appId'],_0x1e8d18),'receiverId':prefixId(this['appId'],_0x1e8d18),'timestamp':Date['now'](),'token':this['authToken']},_0xba4ae=toUint8(JSON['stringify'](_0x33ff1d)),_0x55d010=framePacket(_0x1fa398,_0xba4ae);this['transport']['sendBinary'](_0x55d010);}['fetchAIHistory']({senderId:_0x5a6e1a}){if(!this['isConnected'])throw new Error('Not\x20connected');if(!_0x5a6e1a)throw new Error('senderId\x20required');const _0xaed7fd={'id':_0x31f4e1['randomUUID'](),'type':'ai','code':0x12d,'appId':this['appId'],'sender':prefixId(this['appId'],_0x5a6e1a),'timestamp':Date['now'](),'token':this['authToken']},_0x5ebb80=toUint8(JSON['stringify']({})),_0xb25183=framePacket(_0xaed7fd,_0x5ebb80);this['transport']['sendBinary'](_0xb25183);}['sendBinary']({senderId:_0x2decc0,receiverIds:_0x53cdab,buffer:_0x4da3cf,type:_0x44bb34='binary'}){if(!this['isConnected'])throw new Error('Not\x20connected');if(!_0x2decc0)throw new Error('senderId\x20required');if(!_0x4da3cf)throw new Error('buffer\x20is\x20required');const _0x2d3cb8={'type':_0x44bb34,'code':0xca,'appId':this['appId'],'sender':prefixId(this['appId'],_0x2decc0),'receivers':_0x53cdab['map'](_0x219b93=>prefixId(this['appId'],_0x219b93)),'timestamp':Date['now'](),'token':this['authToken']},_0x3e7345=framePacket(_0x2d3cb8,_0x4da3cf);this['transport']['sendBinary'](_0x3e7345);}async['sendWithOpenAI']({senderId:_0x532290,message:_0x599762,openaiApiKey:_0x33cd68,model:_0x20afa9,baseUrl:_0xe50e9f}){if(!this['aiEnabled'])throw new Error('AI\x20is\x20not\x20enabled\x20for\x20this\x20subscription.');if(!this['isConnected'])throw new Error('Not\x20connected');if(!_0x532290)throw new Error('senderId\x20is\x20required');if(!_0x599762)throw new Error('message\x20is\x20required');this['sendAI']({'senderId':_0x532290,'message':_0x599762});}}
@@ -1 +1 @@
1
- const DEFAULT_BASE_URL='https://api.openai.com',DEFAULT_MODEL='gpt-4o-mini';function normalizeMessages(_0x526cda){if('string'==typeof _0x526cda)return[{'role':'user','content':_0x526cda}];if(Array['isArray'](_0x526cda)&&_0x526cda['length']>0x0)return _0x526cda['map'](_0x597c2f=>({'role':_0x597c2f['role']||'user','content':'string'==typeof _0x597c2f['content']?_0x597c2f['content']:String(_0x597c2f['content'])}));throw new Error('message\x20must\x20be\x20a\x20non-empty\x20string\x20or\x20array\x20of\x20{\x20role,\x20content\x20}');}export async function chat(_0x389a79){var _0x158681,_0x7814b2,_0x4aab12,_0x4ed344,_0x41df42,_0x59acfc;const {apiKey:_0x43a030,model:_0x13b6cc=DEFAULT_MODEL,messages:_0x159e73,baseUrl:_0x527bc4=DEFAULT_BASE_URL}=_0x389a79;if(!_0x43a030||!_0x159e73)throw new Error('openai.chat:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x35a8eb=_0x527bc4['replace'](/\/$/,'')+'/v1/chat/completions',_0x3b245b={'model':_0x13b6cc,'messages':normalizeMessages(_0x159e73),'stream':!0x1},_0x175f0c=await fetch(_0x35a8eb,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x43a030},'body':JSON['stringify'](_0x3b245b)}),_0x37f9d0=await _0x175f0c['json']();if(!_0x175f0c['ok']){const _0x5537c9=new Error((null===(_0x158681=null==_0x37f9d0?void 0x0:_0x37f9d0['error'])||void 0x0===_0x158681?void 0x0:_0x158681['message'])||'OpenAI\x20API\x20error:\x20'+_0x175f0c['status']);throw _0x5537c9['status']=_0x175f0c['status'],_0x5537c9['code']=null===(_0x7814b2=null==_0x37f9d0?void 0x0:_0x37f9d0['error'])||void 0x0===_0x7814b2?void 0x0:_0x7814b2['code'],_0x5537c9;}return{'content':null!==(_0x59acfc=null===(_0x41df42=null===(_0x4ed344=null===(_0x4aab12=null==_0x37f9d0?void 0x0:_0x37f9d0['choices'])||void 0x0===_0x4aab12?void 0x0:_0x4aab12[0x0])||void 0x0===_0x4ed344?void 0x0:_0x4ed344['message'])||void 0x0===_0x41df42?void 0x0:_0x41df42['content'])&&void 0x0!==_0x59acfc?_0x59acfc:'','usage':null==_0x37f9d0?void 0x0:_0x37f9d0['usage'],'raw':_0x37f9d0};}export async function chatStream(_0x4394c1){var _0x4b389b,_0x5efe77,_0x44ab30,_0x40aa7e,_0x517840;const {apiKey:_0x204b1a,model:_0x40b0a5=DEFAULT_MODEL,messages:_0x4d3076,baseUrl:_0x55f11a=DEFAULT_BASE_URL,onChunk:_0x35a894}=_0x4394c1;if(!_0x204b1a||!_0x4d3076)throw new Error('openai.chatStream:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x50b736=_0x55f11a['replace'](/\/$/,'')+'/v1/chat/completions',_0x3ca35a={'model':_0x40b0a5,'messages':normalizeMessages(_0x4d3076),'stream':!0x0},_0x10b31c=await fetch(_0x50b736,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x204b1a},'body':JSON['stringify'](_0x3ca35a)});if(!_0x10b31c['ok']){const _0x304fde=await _0x10b31c['json']()['catch'](()=>({})),_0x152c52=new Error((null===(_0x4b389b=null==_0x304fde?void 0x0:_0x304fde['error'])||void 0x0===_0x4b389b?void 0x0:_0x4b389b['message'])||'OpenAI\x20API\x20error:\x20'+_0x10b31c['status']);throw _0x152c52['status']=_0x10b31c['status'],_0x152c52['code']=null===(_0x5efe77=null==_0x304fde?void 0x0:_0x304fde['error'])||void 0x0===_0x5efe77?void 0x0:_0x5efe77['code'],_0x152c52;}const _0x1a417c=_0x10b31c['body']['getReader'](),_0x78ee17=new TextDecoder();let _0x14c6ed='',_0x2ecd71='';for(;;){const {done:_0x257cf3,value:_0x59f87a}=await _0x1a417c['read']();if(_0x257cf3)break;_0x2ecd71+=_0x78ee17['decode'](_0x59f87a,{'stream':!0x0});const _0x43976a=_0x2ecd71['split']('\x0a');_0x2ecd71=_0x43976a['pop']()||'';for(const _0x5c2177 of _0x43976a){const _0x33d7d4=_0x5c2177['trim']();if(''!==_0x33d7d4&&'data:\x20[DONE]'!==_0x33d7d4&&_0x33d7d4['startsWith']('data:\x20'))try{const _0x144b10=JSON['parse'](_0x33d7d4['slice'](0x6)),_0x58358b=null===(_0x517840=null===(_0x40aa7e=null===(_0x44ab30=null==_0x144b10?void 0x0:_0x144b10['choices'])||void 0x0===_0x44ab30?void 0x0:_0x44ab30[0x0])||void 0x0===_0x40aa7e?void 0x0:_0x40aa7e['delta'])||void 0x0===_0x517840?void 0x0:_0x517840['content'];'string'==typeof _0x58358b&&(_0x14c6ed+=_0x58358b,_0x35a894&&_0x35a894(_0x58358b));}catch(_0x3511b8){}}}return{'content':_0x14c6ed};}
1
+ const DEFAULT_BASE_URL='https://api.openai.com',DEFAULT_MODEL='gpt-4o-mini';function normalizeMessages(_0x11c81d){if('string'==typeof _0x11c81d)return[{'role':'user','content':_0x11c81d}];if(Array['isArray'](_0x11c81d)&&_0x11c81d['length']>0x0)return _0x11c81d['map'](_0x4bcf3d=>({'role':_0x4bcf3d['role']||'user','content':'string'==typeof _0x4bcf3d['content']?_0x4bcf3d['content']:String(_0x4bcf3d['content'])}));throw new Error('message\x20must\x20be\x20a\x20non-empty\x20string\x20or\x20array\x20of\x20{\x20role,\x20content\x20}');}export async function chat(_0x1622b0){var _0xdd8dbb,_0x1a234e,_0x32762f,_0x14b5ae,_0x419e9f,_0x418cb8;const {apiKey:_0x1e43f3,model:_0x91b0a9=DEFAULT_MODEL,messages:_0x160ab4,baseUrl:_0x26efc8=DEFAULT_BASE_URL}=_0x1622b0;if(!_0x1e43f3||!_0x160ab4)throw new Error('openai.chat:\x20apiKey\x20and\x20messages\x20are\x20required');const _0x12b184=_0x26efc8['replace'](/\/$/,'')+'/v1/chat/completions',_0x6f4c52={'model':_0x91b0a9,'messages':normalizeMessages(_0x160ab4),'stream':!0x1},_0x224c38=await fetch(_0x12b184,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x1e43f3},'body':JSON['stringify'](_0x6f4c52)}),_0x1d89e4=await _0x224c38['json']();if(!_0x224c38['ok']){const _0x4cf6ff=new Error((null===(_0xdd8dbb=null==_0x1d89e4?void 0x0:_0x1d89e4['error'])||void 0x0===_0xdd8dbb?void 0x0:_0xdd8dbb['message'])||'OpenAI\x20API\x20error:\x20'+_0x224c38['status']);throw _0x4cf6ff['status']=_0x224c38['status'],_0x4cf6ff['code']=null===(_0x1a234e=null==_0x1d89e4?void 0x0:_0x1d89e4['error'])||void 0x0===_0x1a234e?void 0x0:_0x1a234e['code'],_0x4cf6ff;}return{'content':null!==(_0x418cb8=null===(_0x419e9f=null===(_0x14b5ae=null===(_0x32762f=null==_0x1d89e4?void 0x0:_0x1d89e4['choices'])||void 0x0===_0x32762f?void 0x0:_0x32762f[0x0])||void 0x0===_0x14b5ae?void 0x0:_0x14b5ae['message'])||void 0x0===_0x419e9f?void 0x0:_0x419e9f['content'])&&void 0x0!==_0x418cb8?_0x418cb8:'','usage':null==_0x1d89e4?void 0x0:_0x1d89e4['usage'],'raw':_0x1d89e4};}export async function chatStream(_0xdf6b84){var _0x24d47c,_0x82689e,_0x324b28,_0x55a775,_0x24d228;const {apiKey:_0x38056f,model:_0x12fa5f=DEFAULT_MODEL,messages:_0x497862,baseUrl:_0x23e38f=DEFAULT_BASE_URL,onChunk:_0x339fad}=_0xdf6b84;if(!_0x38056f||!_0x497862)throw new Error('openai.chatStream:\x20apiKey\x20and\x20messages\x20are\x20required');const _0xa3def9=_0x23e38f['replace'](/\/$/,'')+'/v1/chat/completions',_0x3af0bc={'model':_0x12fa5f,'messages':normalizeMessages(_0x497862),'stream':!0x0},_0x190eff=await fetch(_0xa3def9,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x38056f},'body':JSON['stringify'](_0x3af0bc)});if(!_0x190eff['ok']){const _0x16435e=await _0x190eff['json']()['catch'](()=>({})),_0x426d62=new Error((null===(_0x24d47c=null==_0x16435e?void 0x0:_0x16435e['error'])||void 0x0===_0x24d47c?void 0x0:_0x24d47c['message'])||'OpenAI\x20API\x20error:\x20'+_0x190eff['status']);throw _0x426d62['status']=_0x190eff['status'],_0x426d62['code']=null===(_0x82689e=null==_0x16435e?void 0x0:_0x16435e['error'])||void 0x0===_0x82689e?void 0x0:_0x82689e['code'],_0x426d62;}const _0x5dc678=_0x190eff['body']['getReader'](),_0x3ccbde=new TextDecoder();let _0x1cd4a4='',_0x2c56f4='';for(;;){const {done:_0x22445a,value:_0x35428b}=await _0x5dc678['read']();if(_0x22445a)break;_0x2c56f4+=_0x3ccbde['decode'](_0x35428b,{'stream':!0x0});const _0x342cd5=_0x2c56f4['split']('\x0a');_0x2c56f4=_0x342cd5['pop']()||'';for(const _0x3d06bc of _0x342cd5){const _0x117027=_0x3d06bc['trim']();if(''!==_0x117027&&'data:\x20[DONE]'!==_0x117027&&_0x117027['startsWith']('data:\x20'))try{const _0x4f5cac=JSON['parse'](_0x117027['slice'](0x6)),_0x3172c2=null===(_0x24d228=null===(_0x55a775=null===(_0x324b28=null==_0x4f5cac?void 0x0:_0x4f5cac['choices'])||void 0x0===_0x324b28?void 0x0:_0x324b28[0x0])||void 0x0===_0x55a775?void 0x0:_0x55a775['delta'])||void 0x0===_0x24d228?void 0x0:_0x24d228['content'];'string'==typeof _0x3172c2&&(_0x1cd4a4+=_0x3172c2,_0x339fad&&_0x339fad(_0x3172c2));}catch(_0x49f8b0){}}}return{'content':_0x1cd4a4};}
@@ -1 +1 @@
1
- export class HttpTransport{constructor({url:_0x1ff7d5}){this['url']=_0x1ff7d5;}async['send'](_0x520f0d){const _0x8b5c43=await fetch(''+this['url'],{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](_0x520f0d)});if(!_0x8b5c43['ok']){const _0x22479f=await _0x8b5c43['text']();throw new Error('DNotifier\x20HTTP\x20error:\x20'+_0x22479f);}return await _0x8b5c43['json']();}['sendBinary'](){throw new Error('Binary\x20is\x20not\x20supported\x20over\x20HTTP\x20yet');}}
1
+ export class HttpTransport{constructor({url:_0x172890}){this['url']=_0x172890;}async['send'](_0x2f6ab7){const _0x5a94fc=await fetch(''+this['url'],{'method':'POST','headers':{'Content-Type':'application/json'},'body':JSON['stringify'](_0x2f6ab7)});if(!_0x5a94fc['ok']){const _0x5f584d=await _0x5a94fc['text']();throw new Error('DNotifier\x20HTTP\x20error:\x20'+_0x5f584d);}return await _0x5a94fc['json']();}['sendBinary'](){throw new Error('Binary\x20is\x20not\x20supported\x20over\x20HTTP\x20yet');}}
@@ -1 +1 @@
1
- import _0x46cfab from'events';import{decodePacket}from'../utils/frame.js';export class WebsocketTransport extends _0x46cfab{constructor(_0x2f4f59=null){if(super(),this['socket']=null,this['WebSocketImpl']=_0x2f4f59||('undefined'!=typeof window?window['WebSocket']:null),!this['WebSocketImpl'])throw new Error('No\x20WebSocket\x20implementation\x20provided');}['connect'](_0x62a86){return new Promise((_0x39afb7,_0x39924b)=>{this['socket']=new this['WebSocketImpl'](_0x62a86),'undefined'!=typeof window&&(this['socket']['binaryType']='arrayBuffer'),this['_on']('open',()=>_0x39afb7(!0x0)),this['_on']('error',_0x174e69=>{console['error']('WS\x20error',_0x174e69),_0x39924b(_0x174e69);}),this['_on']('close',_0x442db1=>{const _0x51b4a6=null==_0x442db1?void 0x0:_0x442db1['code'],_0x2b90bb=null==_0x442db1?void 0x0:_0x442db1['reason'];this['emit']('close',{'code':_0x51b4a6,'reason':_0x2b90bb});}),this['_on']('message',async(_0x703371,_0x5d77dd)=>{var _0x2b5dad,_0x1d3a20=null!==(_0x2b5dad=null==_0x703371?void 0x0:_0x703371['data'])&&void 0x0!==_0x2b5dad?_0x2b5dad:_0x703371;'undefined'!=typeof Blob&&_0x1d3a20 instanceof Blob&&(_0x1d3a20=await _0x1d3a20['arrayBuffer']());const _0x4298be=decodePacket(_0x1d3a20);_0x4298be&&this['emit']('message',_0x4298be,_0x5d77dd);});});}['_on'](_0x504650,_0x2e8556){this['socket']['on']?this['socket']['on'](_0x504650,_0x2e8556):this['socket']['on'+_0x504650]=_0x2e8556;}['send'](_0xc741bb){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0xc741bb);}['sendBinary'](_0x50b328){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x50b328);}['isOpen'](){return this['socket']&&this['socket']['readyState']===this['WebSocketImpl']['OPEN'];}['close'](){var _0x6044c;null===(_0x6044c=this['socket'])||void 0x0===_0x6044c||_0x6044c['close']();}}
1
+ import _0x47fa2a from'events';import{decodePacket}from'../utils/frame.js';export class WebsocketTransport extends _0x47fa2a{constructor(_0x8e02e9=null){if(super(),this['socket']=null,this['WebSocketImpl']=_0x8e02e9||('undefined'!=typeof window?window['WebSocket']:null),!this['WebSocketImpl'])throw new Error('No\x20WebSocket\x20implementation\x20provided');}['connect'](_0x45aae1){return new Promise((_0x34d540,_0x4b42d0)=>{this['socket']=new this['WebSocketImpl'](_0x45aae1),'undefined'!=typeof window&&(this['socket']['binaryType']='arrayBuffer'),this['_on']('open',()=>_0x34d540(!0x0)),this['_on']('error',_0x21a39f=>{console['error']('WS\x20error',_0x21a39f),_0x4b42d0(_0x21a39f);}),this['_on']('close',_0x2d40c2=>{const _0xd45a93=null==_0x2d40c2?void 0x0:_0x2d40c2['code'],_0x3e35ac=null==_0x2d40c2?void 0x0:_0x2d40c2['reason'];this['emit']('close',{'code':_0xd45a93,'reason':_0x3e35ac});}),this['_on']('message',async(_0x260056,_0x21e1e0)=>{var _0x26fa5d,_0x271d24=null!==(_0x26fa5d=null==_0x260056?void 0x0:_0x260056['data'])&&void 0x0!==_0x26fa5d?_0x26fa5d:_0x260056;'undefined'!=typeof Blob&&_0x271d24 instanceof Blob&&(_0x271d24=await _0x271d24['arrayBuffer']());const _0x1198e8=decodePacket(_0x271d24);_0x1198e8&&this['emit']('message',_0x1198e8,_0x21e1e0);});});}['_on'](_0x21ce3b,_0x38ef54){this['socket']['on']?this['socket']['on'](_0x21ce3b,_0x38ef54):this['socket']['on'+_0x21ce3b]=_0x38ef54;}['send'](_0x37b6e7){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x37b6e7);}['sendBinary'](_0x297888){if(!this['isOpen']())throw new Error('WebSocket\x20not\x20connected');this['socket']['send'](_0x297888);}['isOpen'](){return this['socket']&&this['socket']['readyState']===this['WebSocketImpl']['OPEN'];}['close'](){var _0x3fde14;null===(_0x3fde14=this['socket'])||void 0x0===_0x3fde14||_0x3fde14['close']();}}
@@ -1 +1 @@
1
- export function toUint8(_0x652015){return'undefined'!=typeof Buffer?Buffer['from'](_0x652015,'utf8'):new TextEncoder()['encode'](_0x652015);}
1
+ export function toUint8(_0x38b3a0){return'undefined'!=typeof Buffer?Buffer['from'](_0x38b3a0,'utf8'):new TextEncoder()['encode'](_0x38b3a0);}
@@ -1 +1 @@
1
- export function framePacket(_0x33ee91,_0x1597d5){const _0x559ed3=Buffer['from'](JSON['stringify'](_0x33ee91)),_0x5db7af=Buffer['isBuffer'](_0x1597d5)?_0x1597d5:Buffer['from'](_0x1597d5),_0x39116e=Buffer['alloc'](0x4+_0x559ed3['length']+0x4+_0x5db7af['length']);let _0x5a9c83=0x0;return _0x39116e['writeUInt32BE'](_0x559ed3['length'],_0x5a9c83),_0x5a9c83+=0x4,_0x559ed3['copy'](_0x39116e,_0x5a9c83),_0x5a9c83+=_0x559ed3['length'],_0x39116e['writeUInt32BE'](_0x5db7af['length'],_0x5a9c83),_0x5a9c83+=0x4,_0x5db7af['copy'](_0x39116e,_0x5a9c83),_0x39116e;}let internalBuffer=Buffer['alloc'](0x0);export function decodePacket(_0xc952d){try{Buffer['isBuffer'](_0xc952d)||(_0xc952d=Buffer['from'](_0xc952d)),internalBuffer=Buffer['concat']([internalBuffer,_0xc952d]);let _0x40410f=0x0;if(internalBuffer['length']<0x4)return null;const _0x53f154=internalBuffer['readUInt32BE'](_0x40410f);if(_0x40410f+=0x4,internalBuffer['length']<_0x40410f+_0x53f154)return null;const _0x1bd3c1=internalBuffer['slice'](_0x40410f,_0x40410f+_0x53f154),_0x3a089c=JSON['parse'](_0x1bd3c1['toString']());if(_0x40410f+=_0x53f154,internalBuffer['length']<_0x40410f+0x4)return null;const _0x4b6bd7=internalBuffer['readUInt32BE'](_0x40410f);if(_0x40410f+=0x4,internalBuffer['length']<_0x40410f+_0x4b6bd7)return null;const _0x1a548b=internalBuffer['slice'](_0x40410f,_0x40410f+_0x4b6bd7);_0x40410f+=_0x4b6bd7,internalBuffer=internalBuffer['slice'](_0x40410f);let _0x4e7eb6=_0x1a548b;if('text'===_0x3a089c['type'])try{_0x4e7eb6=JSON['parse'](_0x1a548b['toString']('utf8'));}catch{_0x4e7eb6=_0x1a548b;}return{'metadata':_0x3a089c,'data':_0x4e7eb6};}catch(_0x251cf8){return console['error']('decodePacket\x20error:',_0x251cf8),null;}}
1
+ export function framePacket(_0x12666f,_0xd8806a){const _0x398f5c=Buffer['from'](JSON['stringify'](_0x12666f)),_0x1e1f16=Buffer['isBuffer'](_0xd8806a)?_0xd8806a:Buffer['from'](_0xd8806a),_0x4ab99f=Buffer['alloc'](0x4+_0x398f5c['length']+0x4+_0x1e1f16['length']);let _0x1f03fe=0x0;return _0x4ab99f['writeUInt32BE'](_0x398f5c['length'],_0x1f03fe),_0x1f03fe+=0x4,_0x398f5c['copy'](_0x4ab99f,_0x1f03fe),_0x1f03fe+=_0x398f5c['length'],_0x4ab99f['writeUInt32BE'](_0x1e1f16['length'],_0x1f03fe),_0x1f03fe+=0x4,_0x1e1f16['copy'](_0x4ab99f,_0x1f03fe),_0x4ab99f;}let internalBuffer=Buffer['alloc'](0x0);export function decodePacket(_0x5a4f31){try{Buffer['isBuffer'](_0x5a4f31)||(_0x5a4f31=Buffer['from'](_0x5a4f31)),internalBuffer=Buffer['concat']([internalBuffer,_0x5a4f31]);let _0x258b4b=0x0;if(internalBuffer['length']<0x4)return null;const _0x5874fa=internalBuffer['readUInt32BE'](_0x258b4b);if(_0x258b4b+=0x4,internalBuffer['length']<_0x258b4b+_0x5874fa)return null;const _0x52ebc0=internalBuffer['slice'](_0x258b4b,_0x258b4b+_0x5874fa),_0x287d05=JSON['parse'](_0x52ebc0['toString']());if(_0x258b4b+=_0x5874fa,internalBuffer['length']<_0x258b4b+0x4)return null;const _0x4b0340=internalBuffer['readUInt32BE'](_0x258b4b);if(_0x258b4b+=0x4,internalBuffer['length']<_0x258b4b+_0x4b0340)return null;const _0x29b577=internalBuffer['slice'](_0x258b4b,_0x258b4b+_0x4b0340);_0x258b4b+=_0x4b0340,internalBuffer=internalBuffer['slice'](_0x258b4b);let _0x2de255=_0x29b577;if('text'===_0x287d05['type'])try{_0x2de255=JSON['parse'](_0x29b577['toString']('utf8'));}catch{_0x2de255=_0x29b577;}return{'metadata':_0x287d05,'data':_0x2de255};}catch(_0x254413){return console['error']('decodePacket\x20error:',_0x254413),null;}}
package/dist/utils/id.js CHANGED
@@ -1 +1 @@
1
- export const prefixId=(_0x2aa983,_0x21381a)=>_0x21381a['includes'](':')?_0x21381a:_0x2aa983+':'+_0x21381a;
1
+ export const prefixId=(_0xdbecd4,_0x18937f)=>_0x18937f['includes'](':')?_0x18937f:_0xdbecd4+':'+_0x18937f;
@@ -1 +1 @@
1
- export class Payload{constructor(_0x3d6505){this['buffer']=_0x3d6505;}['raw'](){return this['buffer'];}['toString'](_0x1c4044='utf8'){return this['buffer']['toString'](_0x1c4044);}['toJSON'](_0x28d15c='utf8'){try{return JSON['parse'](this['buffer']['toString'](_0x28d15c));}catch(_0x3281a6){return null;}}['toBase64'](){return this['buffer']['toString']('base64');}}
1
+ export class Payload{constructor(_0x21d1ef){this['buffer']=_0x21d1ef;}['raw'](){return this['buffer'];}['toString'](_0x382c98='utf8'){return this['buffer']['toString'](_0x382c98);}['toJSON'](_0x2a8c01='utf8'){try{return JSON['parse'](this['buffer']['toString'](_0x2a8c01));}catch(_0x23337c){return null;}}['toBase64'](){return this['buffer']['toString']('base64');}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnotifier-realtime/dnotifier",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "Real-time notification & messaging SDK",
5
5
  "main": "dist/dnotifier.js",
6
6
  "types": "dist/dnotifier.d.ts",