@epam/statgpt-dial-toolkit 0.1.0-rc.1 → 0.1.0-rc.2

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/index.cjs CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m="v1",R=`/${m}/ops/resource/share`,u={VERSION:`/${m}`,BUCKET:`/${m}/bucket`,CONVERSATIONS:`/${m}/metadata/conversations`,CONVERSATION_BY_ID:s=>`/${m}/metadata/conversations/${s}`,CHAT:s=>`/openai/deployments/${s}/chat/completions`,MODELS:"/openai/models",CONFIGURATION:s=>`/${m}/deployments/${s}/configuration`,SHARE_CONVERSATION:`${R}/create`,SHARE_CONVERSATION_ACCEPT:s=>`/${m}/invitations/${s}?accept=true`,SHARE_CONVERSATION_DETAILS:s=>`/${m}/invitations/${s}`,SHARE_CONVERSATION_LIST:`${R}/list`,SHARE_CONVERSATION_DISCARD:`${R}/discard`,SHARE_CONVERSATION_REVOKE:`${R}/revoke`,RATE:`/${m}/rate`,RENAME:`/${m}/ops/resource/move`};var _=(s=>(s.CSV="text/csv",s.TABLE="application/dial-ttyd-table",s.PLOTLY="application/vnd.plotly.v1+json",s.MARKDOWN="text/markdown",s.JSON="application/json",s.JPEG="image/jpeg",s.PNG="image/png",s.CUSTOM_DATA_GRID="custom_data_grid",s.CUSTOM_CHART="custom_chart",s))(_||{}),I=(s=>(s.LINK="link",s))(I||{});const q=s=>{var t,r,e,n,o,a;return s.content?(console.info(`Using direct content format: ${s.content}`),s.content):(e=(r=(t=s.choices)==null?void 0:t[0])==null?void 0:r.delta)!=null&&e.content?(console.info(`Using OpenAI delta format: ${s.choices[0].delta.content}`),s.choices[0].delta.content):(a=(o=(n=s.choices)==null?void 0:n[0])==null?void 0:o.message)!=null&&a.content?(console.info(`Using complete message format: ${s.choices[0].message.content}`),s.choices[0].message.content):(console.info("Unknown SSE data format:",s),null)},T=(s,t,r)=>{if(t==null||t(s),r){const e=q(s);e&&r(e)}},$=(s,t)=>s.error||s.message||`${t.status} ${t.statusText}`,b=(s,t)=>{const r=s==null?void 0:s.reduce((e,n)=>(e[n.index]=n,e),{});return t.forEach(e=>{r[e.index]?(e.attachments&&(r[e.index].attachments=(r[e.index].attachments||[]).concat(e.attachments)),e.content&&(r[e.index].content=(r[e.index].content||"")+e.content),e.name&&(r[e.index].name=(r[e.index].name||"")+e.name),e.status&&(r[e.index].status=e.status)):r[e.index]=e}),Object.values(r)},v=(s,t)=>{const r=structuredClone(s);return t.forEach(e=>{e.errorMessage&&(r.errorMessage=e.errorMessage),e.role&&(r.role=e.role),e.responseId&&(r.responseId=e.responseId),e.content&&(r.content=`${r.content||""}${e.content}`),e.custom_content&&(r.custom_content||(r.custom_content={}),e.custom_content.attachments&&(r.custom_content.attachments||(r.custom_content.attachments=[]),r.custom_content.attachments=r.custom_content.attachments.concat(e.custom_content.attachments)),e.custom_content.stages&&(r.custom_content.stages||(r.custom_content.stages=[]),r.custom_content.stages=b(r.custom_content.stages,e.custom_content.stages)),e.custom_content.state&&(r.custom_content.state=e.custom_content.state),e.custom_content.form_schema&&(r.custom_content.form_schema=e.custom_content.form_schema),e.custom_content.form_value&&(r.custom_content.form_value=e.custom_content.form_value))}),r},P=s=>(s==null?void 0:s.toLowerCase().replace(/[^\p{L}\p{N}]+/gu,"-").replace(/^-+|-+$/g,"").replace(/-/g," "))||"",p=s=>{var n;const t=((n=s.name)==null?void 0:n.split("__"))||[],r=t.length>1?t.slice(1).join("__"):s.name;return{modelId:t[0],conversationName:r}},O=s=>{const t=Date.now(),r=P(s.name);return`${s.folderId}/${r}-${t}`},E=async(s,t,r)=>await fetch(s,{method:r.method||"GET",headers:t,body:r!=null&&r.isFormData&&typeof(r==null?void 0:r.body)=="string"?r.body:JSON.stringify(r.body),signal:r==null?void 0:r.signal});function f(s){return s.split("/").map(t=>encodeURIComponent(t)).join("/")}function x(s){return s.split("/").map(t=>decodeURIComponent(t)).join("/")}const H={CHAT:"/api/chat"},D="application/json",L="Content-Type",A="Api-Key",V="X-CONVERSATION-ID",y=(s,t,r)=>{const e={[L]:(t==null?void 0:t.contentType)||D};return t!=null&&t.jwt?e.Authorization=`Bearer ${t.jwt}`:s&&(e[A]=s),t!=null&&t.chatReference&&(e[V]=t.chatReference),{...e,...r}},U=s=>{const t={...s};return t[A]&&(t[A]=t[A].substring(0,8)+"...[REDACTED]"),t.Authorization&&(t.Authorization="Bearer [REDACTED]"),t},j=s=>({"Content-Type":`multipart/form-data; boundary=${s}`});class g{constructor(){this.decoder=new TextDecoder}async streamChat(t,r,e={},n){const{onMessage:o,onError:a,onComplete:c,signal:d}=e;try{const i=await this.initializeStreamRequest(t,r,d,n);await this.processStreamData(i,o),c==null||c()}catch(i){this.handleStreamError(i,a)}}async initializeStreamRequest(t,r,e,n){const o=y(void 0,{jwt:n}),a=await E(t,{Accept:"text/event-stream",...o},{method:"POST",body:r,signal:e});if(!a.ok){const c=await a.text();throw new Error(`HTTP ${a.status}: ${c}`)}if(!a.body)throw new Error("No response body");return a.body.getReader()}async processStreamData(t,r){let e="";try{for(;;){const{done:n,value:o}=await t.read();if(n){e.trim()&&this.parseSSEDataLine(e,r);break}const a=this.decoder.decode(o,{stream:!0});e+=a;const c=e.split(`
2
- `);e=c.pop()||"";for(const d of c)this.parseSSEDataLine(d,r)}}finally{t.releaseLock()}}handleStreamError(t,r){const e=t instanceof Error?t:new Error(String(t));throw r==null||r(e),e}parseSSEDataLine(t,r){const e=t.trim();if(!(!e||e.startsWith(":"))&&e.startsWith("data: ")){const n=e.slice(6);if(n==="[DONE]"){console.info("SSE: Stream completed");return}try{const o=JSON.parse(n);console.info(`SSE: Parsed data: ${o}`),r==null||r(o)}catch(o){console.error(`Failed to parse SSE data: ${n} ${o}`)}}}}const N=new g,k=async(s,t,r,e,n)=>{const{onMessage:o,onToken:a,onComplete:c,onError:d,model:i,signal:l}=r,C={conversationId:s,messages:t,model:i,custom_fields:n};await N.streamChat(H.CHAT,C,{onMessage:w=>T(w,o,a),onComplete:c,onError:d,signal:l},e)},h=s=>s instanceof Error&&s.message.includes("404"),S=s=>`/v1/conversations/${f(s)}`;class M{constructor(t){this.client=t}async getConversations(t,r,e){const n=`${r?e?`${r}/${e}`:`${r}`:""}`,o=`${u.CONVERSATIONS}/${n}`;try{return(await this.client.getRequest(o+"/?limit=1000&recursive=false",t).then(c=>c.items||[])).map(c=>{var l;const{conversationName:d,modelId:i}=p(c);return{id:((l=c.url)==null?void 0:l.replace("conversations/",""))||c.name,name:d,folderId:n,createdAt:c.createdAt,updatedAt:c.updatedAt,model:{id:i,name:i}}})}catch(a){if(h(a))return[];throw a}}async getConversation(t,r){try{return await this.client.getRequest(S(t),r)}catch(e){if(h(e))return null;throw e}}async getFile(t,r){try{const e=`${u.VERSION}/${f(t)}`;return await this.client.getRequest(e,r)}catch(e){if(h(e))return null;throw e}}async putOnboardingFile(t,r,e,n){try{const o=`${u.VERSION}/${f(r)}`,a="----NodeMultipartBoundary",d=[`--${a}`,`Content-Disposition: form-data; name="file"; filename="${t}"`,"Content-Type: application/json","",JSON.stringify(e),`--${a}--`,""].join(`\r
3
- `);return await this.client.request(o,n,{method:"PUT",body:d,headers:j(a),isFormData:!0})}catch(o){if(h(o))return null;throw o}}async getOnboardingFile(t,r){try{const e=`${u.VERSION}/${f(t)}`;return await this.client.getRequest(e,r)}catch(e){if(h(e))return null;throw e}}async getFileBlob(t,r){try{const e=`${u.VERSION}/${f(t)}`;return await this.client.requestBlob(e,r,{method:"GET"})}catch(e){if(h(e))return null;throw e}}async createConversation(t,r){const e=(t==null?void 0:t.id)||O(t),{name:n,folderId:o,model:a,messages:c,custom_fields:d}=t,i={id:e,name:n,folderId:o,model:a,messages:c||[],selectedAddons:t.selectedAddons||[],prompt:t.prompt||"",temperature:t.temperature||.7,createdAt:Date.now(),updatedAt:Date.now(),custom_fields:d};return await this.client.request(S(e),r,{method:"PUT",body:i}),{id:e,name:n,folderId:o,model:a,createdAt:i.createdAt,updatedAt:i.updatedAt}}async generateConversationLink(t,r){return await this.client.postRequest(u.SHARE_CONVERSATION,t,{body:r})}async getSharedConversations(t,r){return await this.client.postRequest(u.SHARE_CONVERSATION_LIST,t,{body:r})}async revokeSharedConversations(t,r){await this.client.postRequest(u.SHARE_CONVERSATION_REVOKE,t,{body:r})}async updateConversation(t,r,e){const n=await this.getConversation(t,e);if(!n)throw new Error(`Conversation with id ${t} not found`);const o={...n,...r,updatedAt:Date.now()};return await this.client.request(S(t),e,{method:"PUT",body:o})}async deleteConversation(t,r){t!=null&&t.isShared?await this.client.postRequest(u.SHARE_CONVERSATION_DISCARD,r,{body:{resources:[{url:t==null?void 0:t.url}]}}):await this.client.request(S(decodeURI(t==null?void 0:t.id)),r,{method:"DELETE"})}async streamChat(t,r){const e=t.model.id,n=encodeURIComponent(e),o=`${u.CHAT(n)}?api-version=${this.client.config.version}`,a={messages:t.messages,stream:!0,temperature:.7,max_tokens:4096,custom_fields:t.custom_fields};return await this.client.stream(o,r,{method:"POST",body:a,chatReference:t.conversationId})}async rateResponse(t,r,e){return await this.client.postRequest(u.RATE,e,{body:{responseId:t,rate:r}})}async renameConversation(t,r,e){return await this.client.postRequest(u.RENAME,e,{body:{sourceUrl:t,destinationUrl:r,overwrite:!0}})}}class F{constructor(t){this.config=t,console.info("DialApiClient initialized",{host:t.host||"NOT SET",hasApiKey:!!t.apiKey,version:t.version})}async getRequest(t,r,e){return this.request(t,r,{...e,method:"GET"})}async postRequest(t,r,e){return this.request(t,r,{...e,method:"POST"})}async requestBlob(t,r,e){const n=`${this.config.host}${t}`,o={...y(this.config.apiKey,{jwt:r,chatReference:e.chatReference}),...e.headers};try{return(await E(n,o,e)).blob()}catch(a){throw console.error("API Request Exception",{method:e.method,url:n,error:a instanceof Error?a.message:String(a)}),a}}async request(t,r,e){const n=Date.now(),o=`${this.config.host}${t}`,a={...y(this.config.apiKey,{jwt:r,chatReference:e.chatReference}),...e.headers};this.addInfoRequestLog("API Request",o,e,a);try{const c=await E(o,a,e),d=Date.now()-n;let i;const l=await c.text();try{i=l?JSON.parse(l):{}}catch{if(this.addErrorRequestParsing(o,e,c,d,l),!c.ok)throw new Error(`API request failed: ${c.status} ${c.statusText} - ${l.substring(0,100)}`);i={data:l}}if(!c.ok){this.addErrorRequestLog(o,e,c,d,i);const C=$(i,c);throw new Error(`API request failed: ${C}`)}return i}catch(c){const d=Date.now()-n;throw console.error("API Request Exception",{method:e.method,url:o,duration:`${d}ms`,error:c instanceof Error?c.message:String(c)}),c}}async stream(t,r,e){const n=`${this.config.host}${t}`,o=y(this.config.apiKey,{jwt:r,chatReference:e.chatReference},e.headers);this.addInfoRequestLog("Stream Request",n,e,o);const a=await E(n,o,e);if(!a.ok)throw console.error("Stream Request Failed",{method:e.method||"POST",url:n,status:a.status,statusText:a.statusText}),new Error(`Stream request failed: ${a.status} ${a.statusText}`);if(!a.body)throw new Error("No response body for stream");return a.body}addInfoRequestLog(t,r,e,n){const o={method:e.method||"GET",url:r,headers:U(n)};e.body&&(o.body=e.body),console.info(t,o)}addErrorRequestLog(t,r,e,n,o){console.error("API Request Failed",{method:r.method,url:t,status:e.status,statusText:e.statusText,duration:`${n}ms`,response:o})}addErrorRequestParsing(t,r,e,n,o){console.error("API Response Parse Error",{method:r.method,url:t,status:e.status,statusText:e.statusText,duration:`${n}ms`,responseText:o.substring(0,200),error:"Response is not valid JSON"})}}exports.AttachmentType=_;exports.ChatStreamSSEClient=g;exports.ConversationApi=M;exports.DIAL_API_ROUTES=u;exports.DialApiClient=F;exports.InvitationType=I;exports.chatStreamSSEClient=N;exports.decodeApiUrl=x;exports.encodeApiUrl=f;exports.generateConversationId=O;exports.getErrorMessage=$;exports.handleStreamMessage=T;exports.mergeMessages=v;exports.parseConversationName=p;exports.sendRequest=E;exports.streamChatResponse=k;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m="v1",R=`/${m}/ops/resource/share`,u={VERSION:`/${m}`,BUCKET:`/${m}/bucket`,CONVERSATIONS:`/${m}/metadata/conversations`,CONVERSATION_BY_ID:s=>`/${m}/metadata/conversations/${s}`,CHAT:s=>`/openai/deployments/${s}/chat/completions`,MODELS:"/openai/models",CONFIGURATION:s=>`/${m}/deployments/${s}/configuration`,SHARE_CONVERSATION:`${R}/create`,SHARE_CONVERSATION_ACCEPT:s=>`/${m}/invitations/${s}?accept=true`,SHARE_CONVERSATION_DETAILS:s=>`/${m}/invitations/${s}`,SHARE_CONVERSATION_LIST:`${R}/list`,SHARE_CONVERSATION_DISCARD:`${R}/discard`,SHARE_CONVERSATION_REVOKE:`${R}/revoke`,RATE:`/${m}/rate`,RENAME:`/${m}/ops/resource/move`};var _=(s=>(s.CONVERSATION="CONVERSATION",s))(_||{}),T=(s=>(s.ME="me",s.OTHERS="others",s))(T||{}),O=(s=>(s.CSV="text/csv",s.TABLE="application/dial-ttyd-table",s.PLOTLY="application/vnd.plotly.v1+json",s.MARKDOWN="text/markdown",s.JSON="application/json",s.JPEG="image/jpeg",s.PNG="image/png",s.CUSTOM_DATA_GRID="custom_data_grid",s.CUSTOM_CHART="custom_chart",s))(O||{}),I=(s=>(s.LINK="link",s))(I||{});const v=s=>{var t,r,e,n,o,a;return s.content?(console.info(`Using direct content format: ${s.content}`),s.content):(e=(r=(t=s.choices)==null?void 0:t[0])==null?void 0:r.delta)!=null&&e.content?(console.info(`Using OpenAI delta format: ${s.choices[0].delta.content}`),s.choices[0].delta.content):(a=(o=(n=s.choices)==null?void 0:n[0])==null?void 0:o.message)!=null&&a.content?(console.info(`Using complete message format: ${s.choices[0].message.content}`),s.choices[0].message.content):(console.info("Unknown SSE data format:",s),null)},p=(s,t,r)=>{if(t==null||t(s),r){const e=v(s);e&&r(e)}},$=(s,t)=>s.error||s.message||`${t.status} ${t.statusText}`,P=(s,t)=>{const r=s==null?void 0:s.reduce((e,n)=>(e[n.index]=n,e),{});return t.forEach(e=>{r[e.index]?(e.attachments&&(r[e.index].attachments=(r[e.index].attachments||[]).concat(e.attachments)),e.content&&(r[e.index].content=(r[e.index].content||"")+e.content),e.name&&(r[e.index].name=(r[e.index].name||"")+e.name),e.status&&(r[e.index].status=e.status)):r[e.index]=e}),Object.values(r)},x=(s,t)=>{const r=structuredClone(s);return t.forEach(e=>{e.errorMessage&&(r.errorMessage=e.errorMessage),e.role&&(r.role=e.role),e.responseId&&(r.responseId=e.responseId),e.content&&(r.content=`${r.content||""}${e.content}`),e.custom_content&&(r.custom_content||(r.custom_content={}),e.custom_content.attachments&&(r.custom_content.attachments||(r.custom_content.attachments=[]),r.custom_content.attachments=r.custom_content.attachments.concat(e.custom_content.attachments)),e.custom_content.stages&&(r.custom_content.stages||(r.custom_content.stages=[]),r.custom_content.stages=P(r.custom_content.stages,e.custom_content.stages)),e.custom_content.state&&(r.custom_content.state=e.custom_content.state),e.custom_content.form_schema&&(r.custom_content.form_schema=e.custom_content.form_schema),e.custom_content.form_value&&(r.custom_content.form_value=e.custom_content.form_value))}),r},H=s=>(s==null?void 0:s.toLowerCase().replace(/[^\p{L}\p{N}]+/gu,"-").replace(/^-+|-+$/g,"").replace(/-/g," "))||"",g=s=>{var n;const t=((n=s.name)==null?void 0:n.split("__"))||[],r=t.length>1?t.slice(1).join("__"):s.name;return{modelId:t[0],conversationName:r}},N=s=>{const t=Date.now(),r=H(s.name);return`${s.folderId}/${r}-${t}`},E=async(s,t,r)=>await fetch(s,{method:r.method||"GET",headers:t,body:r!=null&&r.isFormData&&typeof(r==null?void 0:r.body)=="string"?r.body:JSON.stringify(r.body),signal:r==null?void 0:r.signal});function f(s){return s.split("/").map(t=>encodeURIComponent(t)).join("/")}function V(s){return s.split("/").map(t=>decodeURIComponent(t)).join("/")}const D={CHAT:"/api/chat"},L="application/json",U="Content-Type",A="Api-Key",j="X-CONVERSATION-ID",C=(s,t,r)=>{const e={[U]:(t==null?void 0:t.contentType)||L};return t!=null&&t.jwt?e.Authorization=`Bearer ${t.jwt}`:s&&(e[A]=s),t!=null&&t.chatReference&&(e[j]=t.chatReference),{...e,...r}},M=s=>{const t={...s};return t[A]&&(t[A]=t[A].substring(0,8)+"...[REDACTED]"),t.Authorization&&(t.Authorization="Bearer [REDACTED]"),t},k=s=>({"Content-Type":`multipart/form-data; boundary=${s}`});class w{constructor(){this.decoder=new TextDecoder}async streamChat(t,r,e={},n){const{onMessage:o,onError:a,onComplete:c,signal:d}=e;try{const i=await this.initializeStreamRequest(t,r,d,n);await this.processStreamData(i,o),c==null||c()}catch(i){this.handleStreamError(i,a)}}async initializeStreamRequest(t,r,e,n){const o=C(void 0,{jwt:n}),a=await E(t,{Accept:"text/event-stream",...o},{method:"POST",body:r,signal:e});if(!a.ok){const c=await a.text();throw new Error(`HTTP ${a.status}: ${c}`)}if(!a.body)throw new Error("No response body");return a.body.getReader()}async processStreamData(t,r){let e="";try{for(;;){const{done:n,value:o}=await t.read();if(n){e.trim()&&this.parseSSEDataLine(e,r);break}const a=this.decoder.decode(o,{stream:!0});e+=a;const c=e.split(`
2
+ `);e=c.pop()||"";for(const d of c)this.parseSSEDataLine(d,r)}}finally{t.releaseLock()}}handleStreamError(t,r){const e=t instanceof Error?t:new Error(String(t));throw r==null||r(e),e}parseSSEDataLine(t,r){const e=t.trim();if(!(!e||e.startsWith(":"))&&e.startsWith("data: ")){const n=e.slice(6);if(n==="[DONE]"){console.info("SSE: Stream completed");return}try{const o=JSON.parse(n);console.info(`SSE: Parsed data: ${o}`),r==null||r(o)}catch(o){console.error(`Failed to parse SSE data: ${n} ${o}`)}}}}const q=new w,F=async(s,t,r,e,n)=>{const{onMessage:o,onToken:a,onComplete:c,onError:d,model:i,signal:l}=r,y={conversationId:s,messages:t,model:i,custom_fields:n};await q.streamChat(D.CHAT,y,{onMessage:b=>p(b,o,a),onComplete:c,onError:d,signal:l},e)},h=s=>s instanceof Error&&s.message.includes("404"),S=s=>`/v1/conversations/${f(s)}`;class K{constructor(t){this.client=t}async getConversations(t,r,e){const n=`${r?e?`${r}/${e}`:`${r}`:""}`,o=`${u.CONVERSATIONS}/${n}`;try{return(await this.client.getRequest(o+"/?limit=1000&recursive=false",t).then(c=>c.items||[])).map(c=>{var l;const{conversationName:d,modelId:i}=g(c);return{id:((l=c.url)==null?void 0:l.replace("conversations/",""))||c.name,name:d,folderId:n,createdAt:c.createdAt,updatedAt:c.updatedAt,model:{id:i,name:i}}})}catch(a){if(h(a))return[];throw a}}async getConversation(t,r){try{return await this.client.getRequest(S(t),r)}catch(e){if(h(e))return null;throw e}}async getFile(t,r){try{const e=`${u.VERSION}/${f(t)}`;return await this.client.getRequest(e,r)}catch(e){if(h(e))return null;throw e}}async putOnboardingFile(t,r,e,n){try{const o=`${u.VERSION}/${f(r)}`,a="----NodeMultipartBoundary",d=[`--${a}`,`Content-Disposition: form-data; name="file"; filename="${t}"`,"Content-Type: application/json","",JSON.stringify(e),`--${a}--`,""].join(`\r
3
+ `);return await this.client.request(o,n,{method:"PUT",body:d,headers:k(a),isFormData:!0})}catch(o){if(h(o))return null;throw o}}async getOnboardingFile(t,r){try{const e=`${u.VERSION}/${f(t)}`;return await this.client.getRequest(e,r)}catch(e){if(h(e))return null;throw e}}async getFileBlob(t,r){try{const e=`${u.VERSION}/${f(t)}`;return await this.client.requestBlob(e,r,{method:"GET"})}catch(e){if(h(e))return null;throw e}}async createConversation(t,r){const e=(t==null?void 0:t.id)||N(t),{name:n,folderId:o,model:a,messages:c,custom_fields:d}=t,i={id:e,name:n,folderId:o,model:a,messages:c||[],selectedAddons:t.selectedAddons||[],prompt:t.prompt||"",temperature:t.temperature||.7,createdAt:Date.now(),updatedAt:Date.now(),custom_fields:d};return await this.client.request(S(e),r,{method:"PUT",body:i}),{id:e,name:n,folderId:o,model:a,createdAt:i.createdAt,updatedAt:i.updatedAt}}async generateConversationLink(t,r){return await this.client.postRequest(u.SHARE_CONVERSATION,t,{body:r})}async getSharedConversations(t,r){return await this.client.postRequest(u.SHARE_CONVERSATION_LIST,t,{body:r})}async revokeSharedConversations(t,r){await this.client.postRequest(u.SHARE_CONVERSATION_REVOKE,t,{body:r})}async updateConversation(t,r,e){const n=await this.getConversation(t,e);if(!n)throw new Error(`Conversation with id ${t} not found`);const o={...n,...r,updatedAt:Date.now()};return await this.client.request(S(t),e,{method:"PUT",body:o})}async deleteConversation(t,r){t!=null&&t.isShared?await this.client.postRequest(u.SHARE_CONVERSATION_DISCARD,r,{body:{resources:[{url:t==null?void 0:t.url}]}}):await this.client.request(S(decodeURI(t==null?void 0:t.id)),r,{method:"DELETE"})}async streamChat(t,r){const e=t.model.id,n=encodeURIComponent(e),o=`${u.CHAT(n)}?api-version=${this.client.config.version}`,a={messages:t.messages,stream:!0,temperature:.7,max_tokens:4096,custom_fields:t.custom_fields};return await this.client.stream(o,r,{method:"POST",body:a,chatReference:t.conversationId})}async rateResponse(t,r,e){return await this.client.postRequest(u.RATE,e,{body:{responseId:t,rate:r}})}async renameConversation(t,r,e){return await this.client.postRequest(u.RENAME,e,{body:{sourceUrl:t,destinationUrl:r,overwrite:!0}})}}class B{constructor(t){this.config=t,console.info("DialApiClient initialized",{host:t.host||"NOT SET",hasApiKey:!!t.apiKey,version:t.version})}async getRequest(t,r,e){return this.request(t,r,{...e,method:"GET"})}async postRequest(t,r,e){return this.request(t,r,{...e,method:"POST"})}async requestBlob(t,r,e){const n=`${this.config.host}${t}`,o={...C(this.config.apiKey,{jwt:r,chatReference:e.chatReference}),...e.headers};try{return(await E(n,o,e)).blob()}catch(a){throw console.error("API Request Exception",{method:e.method,url:n,error:a instanceof Error?a.message:String(a)}),a}}async request(t,r,e){const n=Date.now(),o=`${this.config.host}${t}`,a={...C(this.config.apiKey,{jwt:r,chatReference:e.chatReference}),...e.headers};this.addInfoRequestLog("API Request",o,e,a);try{const c=await E(o,a,e),d=Date.now()-n;let i;const l=await c.text();try{i=l?JSON.parse(l):{}}catch{if(this.addErrorRequestParsing(o,e,c,d,l),!c.ok)throw new Error(`API request failed: ${c.status} ${c.statusText} - ${l.substring(0,100)}`);i={data:l}}if(!c.ok){this.addErrorRequestLog(o,e,c,d,i);const y=$(i,c);throw new Error(`API request failed: ${y}`)}return i}catch(c){const d=Date.now()-n;throw console.error("API Request Exception",{method:e.method,url:o,duration:`${d}ms`,error:c instanceof Error?c.message:String(c)}),c}}async stream(t,r,e){const n=`${this.config.host}${t}`,o=C(this.config.apiKey,{jwt:r,chatReference:e.chatReference},e.headers);this.addInfoRequestLog("Stream Request",n,e,o);const a=await E(n,o,e);if(!a.ok)throw console.error("Stream Request Failed",{method:e.method||"POST",url:n,status:a.status,statusText:a.statusText}),new Error(`Stream request failed: ${a.status} ${a.statusText}`);if(!a.body)throw new Error("No response body for stream");return a.body}addInfoRequestLog(t,r,e,n){const o={method:e.method||"GET",url:r,headers:M(n)};e.body&&(o.body=e.body),console.info(t,o)}addErrorRequestLog(t,r,e,n,o){console.error("API Request Failed",{method:r.method,url:t,status:e.status,statusText:e.statusText,duration:`${n}ms`,response:o})}addErrorRequestParsing(t,r,e,n,o){console.error("API Response Parse Error",{method:r.method,url:t,status:e.status,statusText:e.statusText,duration:`${n}ms`,responseText:o.substring(0,200),error:"Response is not valid JSON"})}}exports.AttachmentType=O;exports.ChatStreamSSEClient=w;exports.ConversationApi=K;exports.DIAL_API_ROUTES=u;exports.DialApiClient=B;exports.InvitationType=I;exports.ResourceTypes=_;exports.ShareTarget=T;exports.chatStreamSSEClient=q;exports.decodeApiUrl=V;exports.encodeApiUrl=f;exports.generateConversationId=N;exports.getErrorMessage=$;exports.handleStreamMessage=p;exports.mergeMessages=x;exports.parseConversationName=g;exports.sendRequest=E;exports.streamChatResponse=F;
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './constants/api-urls';
2
+ export * from './constants/share-conversation';
2
3
  export * from './models';
3
4
  export * from './types';
4
5
  export * from './utils';
package/index.mjs CHANGED
@@ -15,18 +15,18 @@ const E = "/v1/ops/resource/share", u = {
15
15
  RATE: "/v1/rate",
16
16
  RENAME: "/v1/ops/resource/move"
17
17
  };
18
- var _ = /* @__PURE__ */ ((s) => (s.CSV = "text/csv", s.TABLE = "application/dial-ttyd-table", s.PLOTLY = "application/vnd.plotly.v1+json", s.MARKDOWN = "text/markdown", s.JSON = "application/json", s.JPEG = "image/jpeg", s.PNG = "image/png", s.CUSTOM_DATA_GRID = "custom_data_grid", s.CUSTOM_CHART = "custom_chart", s))(_ || {}), y = /* @__PURE__ */ ((s) => (s.LINK = "link", s))(y || {});
18
+ var _ = /* @__PURE__ */ ((s) => (s.CONVERSATION = "CONVERSATION", s))(_ || {}), y = /* @__PURE__ */ ((s) => (s.ME = "me", s.OTHERS = "others", s))(y || {}), N = /* @__PURE__ */ ((s) => (s.CSV = "text/csv", s.TABLE = "application/dial-ttyd-table", s.PLOTLY = "application/vnd.plotly.v1+json", s.MARKDOWN = "text/markdown", s.JSON = "application/json", s.JPEG = "image/jpeg", s.PNG = "image/png", s.CUSTOM_DATA_GRID = "custom_data_grid", s.CUSTOM_CHART = "custom_chart", s))(N || {}), C = /* @__PURE__ */ ((s) => (s.LINK = "link", s))(C || {});
19
19
  const $ = (s) => {
20
20
  var t, r, e, n, o, c;
21
21
  return s.content ? (console.info(`Using direct content format: ${s.content}`), s.content) : (e = (r = (t = s.choices) == null ? void 0 : t[0]) == null ? void 0 : r.delta) != null && e.content ? (console.info(`Using OpenAI delta format: ${s.choices[0].delta.content}`), s.choices[0].delta.content) : (c = (o = (n = s.choices) == null ? void 0 : n[0]) == null ? void 0 : o.message) != null && c.content ? (console.info(
22
22
  `Using complete message format: ${s.choices[0].message.content}`
23
23
  ), s.choices[0].message.content) : (console.info("Unknown SSE data format:", s), null);
24
- }, C = (s, t, r) => {
24
+ }, T = (s, t, r) => {
25
25
  if (t == null || t(s), r) {
26
26
  const e = $(s);
27
27
  e && r(e);
28
28
  }
29
- }, N = (s, t) => s.error || s.message || `${t.status} ${t.statusText}`, T = (s, t) => {
29
+ }, p = (s, t) => s.error || s.message || `${t.status} ${t.statusText}`, g = (s, t) => {
30
30
  const r = s == null ? void 0 : s.reduce(
31
31
  (e, n) => (e[n.index] = n, e),
32
32
  {}
@@ -34,22 +34,22 @@ const $ = (s) => {
34
34
  return t.forEach((e) => {
35
35
  r[e.index] ? (e.attachments && (r[e.index].attachments = (r[e.index].attachments || []).concat(e.attachments)), e.content && (r[e.index].content = (r[e.index].content || "") + e.content), e.name && (r[e.index].name = (r[e.index].name || "") + e.name), e.status && (r[e.index].status = e.status)) : r[e.index] = e;
36
36
  }), Object.values(r);
37
- }, D = (s, t) => {
37
+ }, j = (s, t) => {
38
38
  const r = structuredClone(s);
39
39
  return t.forEach((e) => {
40
40
  e.errorMessage && (r.errorMessage = e.errorMessage), e.role && (r.role = e.role), e.responseId && (r.responseId = e.responseId), e.content && (r.content = `${r.content || ""}${e.content}`), e.custom_content && (r.custom_content || (r.custom_content = {}), e.custom_content.attachments && (r.custom_content.attachments || (r.custom_content.attachments = []), r.custom_content.attachments = r.custom_content.attachments.concat(
41
41
  e.custom_content.attachments
42
- )), e.custom_content.stages && (r.custom_content.stages || (r.custom_content.stages = []), r.custom_content.stages = T(
42
+ )), e.custom_content.stages && (r.custom_content.stages || (r.custom_content.stages = []), r.custom_content.stages = g(
43
43
  r.custom_content.stages,
44
44
  e.custom_content.stages
45
45
  )), e.custom_content.state && (r.custom_content.state = e.custom_content.state), e.custom_content.form_schema && (r.custom_content.form_schema = e.custom_content.form_schema), e.custom_content.form_value && (r.custom_content.form_value = e.custom_content.form_value));
46
46
  }), r;
47
- }, p = (s) => (s == null ? void 0 : s.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-+|-+$/g, "").replace(/-/g, " ")) || "", g = (s) => {
47
+ }, w = (s) => (s == null ? void 0 : s.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-+|-+$/g, "").replace(/-/g, " ")) || "", q = (s) => {
48
48
  var n;
49
49
  const t = ((n = s.name) == null ? void 0 : n.split("__")) || [], r = t.length > 1 ? t.slice(1).join("__") : s.name;
50
50
  return { modelId: t[0], conversationName: r };
51
- }, w = (s) => {
52
- const t = Date.now(), r = p(s.name);
51
+ }, b = (s) => {
52
+ const t = Date.now(), r = w(s.name);
53
53
  return `${s.folderId}/${r}-${t}`;
54
54
  }, R = async (s, t, r) => await fetch(s, {
55
55
  method: r.method || "GET",
@@ -60,23 +60,23 @@ const $ = (s) => {
60
60
  function h(s) {
61
61
  return s.split("/").map((t) => encodeURIComponent(t)).join("/");
62
62
  }
63
- function U(s) {
63
+ function k(s) {
64
64
  return s.split("/").map((t) => decodeURIComponent(t)).join("/");
65
65
  }
66
- const q = {
66
+ const V = {
67
67
  CHAT: "/api/chat"
68
- }, b = "application/json", V = "Content-Type", S = "Api-Key", v = "X-CONVERSATION-ID", A = (s, t, r) => {
68
+ }, v = "application/json", x = "Content-Type", S = "Api-Key", P = "X-CONVERSATION-ID", O = (s, t, r) => {
69
69
  const e = {
70
- [V]: (t == null ? void 0 : t.contentType) || b
70
+ [x]: (t == null ? void 0 : t.contentType) || v
71
71
  };
72
- return t != null && t.jwt ? e.Authorization = `Bearer ${t.jwt}` : s && (e[S] = s), t != null && t.chatReference && (e[v] = t.chatReference), { ...e, ...r };
73
- }, x = (s) => {
72
+ return t != null && t.jwt ? e.Authorization = `Bearer ${t.jwt}` : s && (e[S] = s), t != null && t.chatReference && (e[P] = t.chatReference), { ...e, ...r };
73
+ }, H = (s) => {
74
74
  const t = { ...s };
75
75
  return t[S] && (t[S] = t[S].substring(0, 8) + "...[REDACTED]"), t.Authorization && (t.Authorization = "Bearer [REDACTED]"), t;
76
- }, P = (s) => ({
76
+ }, L = (s) => ({
77
77
  "Content-Type": `multipart/form-data; boundary=${s}`
78
78
  });
79
- class H {
79
+ class D {
80
80
  constructor() {
81
81
  this.decoder = new TextDecoder();
82
82
  }
@@ -95,7 +95,7 @@ class H {
95
95
  }
96
96
  }
97
97
  async initializeStreamRequest(t, r, e, n) {
98
- const o = A(void 0, {
98
+ const o = O(void 0, {
99
99
  jwt: n
100
100
  }), c = await R(
101
101
  t,
@@ -159,18 +159,18 @@ class H {
159
159
  }
160
160
  }
161
161
  }
162
- const L = new H(), j = async (s, t, r, e, n) => {
163
- const { onMessage: o, onToken: c, onComplete: a, onError: d, model: i, signal: l } = r, I = {
162
+ const U = new D(), F = async (s, t, r, e, n) => {
163
+ const { onMessage: o, onToken: c, onComplete: a, onError: d, model: i, signal: l } = r, A = {
164
164
  conversationId: s,
165
165
  messages: t,
166
166
  model: i,
167
167
  custom_fields: n
168
168
  };
169
- await L.streamChat(
170
- q.CHAT,
171
- I,
169
+ await U.streamChat(
170
+ V.CHAT,
171
+ A,
172
172
  {
173
- onMessage: (O) => C(O, o, c),
173
+ onMessage: (I) => T(I, o, c),
174
174
  onComplete: a,
175
175
  onError: d,
176
176
  signal: l
@@ -178,7 +178,7 @@ const L = new H(), j = async (s, t, r, e, n) => {
178
178
  e
179
179
  );
180
180
  }, m = (s) => s instanceof Error && s.message.includes("404"), f = (s) => `/v1/conversations/${h(s)}`;
181
- class k {
181
+ class K {
182
182
  constructor(t) {
183
183
  this.client = t;
184
184
  }
@@ -187,7 +187,7 @@ class k {
187
187
  try {
188
188
  return (await this.client.getRequest(o + "/?limit=1000&recursive=false", t).then((a) => a.items || [])).map((a) => {
189
189
  var l;
190
- const { conversationName: d, modelId: i } = g(a);
190
+ const { conversationName: d, modelId: i } = q(a);
191
191
  return {
192
192
  id: ((l = a.url) == null ? void 0 : l.replace("conversations/", "")) || a.name,
193
193
  name: d,
@@ -240,7 +240,7 @@ class k {
240
240
  return await this.client.request(o, n, {
241
241
  method: "PUT",
242
242
  body: d,
243
- headers: P(c),
243
+ headers: L(c),
244
244
  isFormData: !0
245
245
  });
246
246
  } catch (o) {
@@ -270,7 +270,7 @@ class k {
270
270
  }
271
271
  }
272
272
  async createConversation(t, r) {
273
- const e = (t == null ? void 0 : t.id) || w(t), { name: n, folderId: o, model: c, messages: a, custom_fields: d } = t, i = {
273
+ const e = (t == null ? void 0 : t.id) || b(t), { name: n, folderId: o, model: c, messages: a, custom_fields: d } = t, i = {
274
274
  id: e,
275
275
  name: n,
276
276
  folderId: o,
@@ -397,7 +397,7 @@ class k {
397
397
  });
398
398
  }
399
399
  }
400
- class F {
400
+ class B {
401
401
  constructor(t) {
402
402
  this.config = t, console.info("DialApiClient initialized", {
403
403
  host: t.host || "NOT SET",
@@ -413,7 +413,7 @@ class F {
413
413
  }
414
414
  async requestBlob(t, r, e) {
415
415
  const n = `${this.config.host}${t}`, o = {
416
- ...A(this.config.apiKey, {
416
+ ...O(this.config.apiKey, {
417
417
  jwt: r,
418
418
  chatReference: e.chatReference
419
419
  }),
@@ -431,7 +431,7 @@ class F {
431
431
  }
432
432
  async request(t, r, e) {
433
433
  const n = Date.now(), o = `${this.config.host}${t}`, c = {
434
- ...A(this.config.apiKey, {
434
+ ...O(this.config.apiKey, {
435
435
  jwt: r,
436
436
  chatReference: e.chatReference
437
437
  }),
@@ -459,8 +459,8 @@ class F {
459
459
  }
460
460
  if (!a.ok) {
461
461
  this.addErrorRequestLog(o, e, a, d, i);
462
- const I = N(i, a);
463
- throw new Error(`API request failed: ${I}`);
462
+ const A = p(i, a);
463
+ throw new Error(`API request failed: ${A}`);
464
464
  }
465
465
  return i;
466
466
  } catch (a) {
@@ -474,7 +474,7 @@ class F {
474
474
  }
475
475
  }
476
476
  async stream(t, r, e) {
477
- const n = `${this.config.host}${t}`, o = A(
477
+ const n = `${this.config.host}${t}`, o = O(
478
478
  this.config.apiKey,
479
479
  {
480
480
  jwt: r,
@@ -501,7 +501,7 @@ class F {
501
501
  const o = {
502
502
  method: e.method || "GET",
503
503
  url: r,
504
- headers: x(n)
504
+ headers: H(n)
505
505
  };
506
506
  e.body && (o.body = e.body), console.info(t, o);
507
507
  }
@@ -529,20 +529,22 @@ class F {
529
529
  }
530
530
  }
531
531
  export {
532
- _ as AttachmentType,
533
- H as ChatStreamSSEClient,
534
- k as ConversationApi,
532
+ N as AttachmentType,
533
+ D as ChatStreamSSEClient,
534
+ K as ConversationApi,
535
535
  u as DIAL_API_ROUTES,
536
- F as DialApiClient,
537
- y as InvitationType,
538
- L as chatStreamSSEClient,
539
- U as decodeApiUrl,
536
+ B as DialApiClient,
537
+ C as InvitationType,
538
+ _ as ResourceTypes,
539
+ y as ShareTarget,
540
+ U as chatStreamSSEClient,
541
+ k as decodeApiUrl,
540
542
  h as encodeApiUrl,
541
- w as generateConversationId,
542
- N as getErrorMessage,
543
- C as handleStreamMessage,
544
- D as mergeMessages,
545
- g as parseConversationName,
543
+ b as generateConversationId,
544
+ p as getErrorMessage,
545
+ T as handleStreamMessage,
546
+ j as mergeMessages,
547
+ q as parseConversationName,
546
548
  R as sendRequest,
547
- j as streamChatResponse
549
+ F as streamChatResponse
548
550
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@epam/statgpt-dial-toolkit",
3
- "version": "0.1.0-rc.1",
3
+ "version": "0.1.0-rc.2",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": {
@@ -14,7 +14,7 @@
14
14
  "module": "./index.mjs",
15
15
  "types": "./index.d.ts",
16
16
  "dependencies": {
17
- "@epam/statgpt-shared-toolkit": "0.1.0-rc.1",
17
+ "@epam/statgpt-shared-toolkit": "0.1.0-rc.2",
18
18
  "@epam/ai-dial-shared": "^0.34.0"
19
19
  }
20
20
  }