@epam/statgpt-dial-toolkit 0.7.0-dev.16 → 0.7.0-dev.19
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 +3 -3
- package/index.mjs +234 -256
- package/package.json +4 -2
package/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l="v1",T=`/${
|
|
2
|
-
`);e=i.pop()||"";for(const c of i)this.parseSSEDataLine(c,n)}}finally{t.releaseLock()}}handleStreamError(t,n){const e=t instanceof Error?t:new Error(String(t));throw n?.(e),e}parseSSEDataLine(t,n){const e=t.trim();if(!(!e||e.startsWith(":"))&&e.startsWith("data: ")){const
|
|
3
|
-
`);return await this.client.request(o,
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@epam/statgpt-shared-toolkit"),m="v1",T=`/${m}/ops/resource/share`,u={VERSION:`/${m}`,BUCKET:`/${m}/bucket`,CONVERSATIONS:`/${m}/metadata/conversations`,CONVERSATION_BY_ID:r=>`/${m}/metadata/conversations/${r}`,CHAT:r=>`/openai/deployments/${r}/chat/completions`,MODELS:"/openai/models",CONFIGURATION:r=>`/${m}/deployments/${r}/configuration`,DATASETS_METADATA:r=>`/v1/deployments/${r}/route/metadata/datasets`,PYTHON_ATTACHMENT:r=>`/v1/deployments/${r}/route/python-attachment`,SHARE_CONVERSATION:`${T}/create`,SHARE_CONVERSATION_ACCEPT:r=>`/${m}/invitations/${r}?accept=true`,SHARE_CONVERSATION_DETAILS:r=>`/${m}/invitations/${r}`,SHARE_CONVERSATION_LIST:`${T}/list`,SHARE_CONVERSATION_DISCARD:`${T}/discard`,SHARE_CONVERSATION_REVOKE:`${T}/revoke`,RATE:r=>`/${m}/${r}/rate`,RENAME:`/${m}/ops/resource/move`},M={LENGTH:"length",STOP:"stop"},_={MINUTE:"minute",DAILY:"daily",WEEKLY:"weekly",MONTHLY:"monthly"},H={CONTENT_FILTER:"content_filter",500:"500"},U={RUNTIME_ERROR:"runtime_error",RATE_LIMIT_EXCEEDED:"rate_limit_exceeded"},x=[_.MINUTE,_.DAILY,_.WEEKLY,_.MONTHLY],V="statgpt",A={RATE_LIMIT:"rate_limit",UNKNOWN:"unknown"};var O=(r=>(r.CONVERSATION="CONVERSATION",r))(O||{}),g=(r=>(r.ME="me",r.OTHERS="others",r))(g||{}),C=(r=>(r.CSV="text/csv",r.TABLE="application/dial-ttyd-table",r.PLOTLY="application/vnd.plotly.v1+json",r.MARKDOWN="text/markdown",r.JSON="application/json",r.JPEG="image/jpeg",r.PNG="image/png",r.CROSS_DATASET_GRID="cross_dataset_grid",r.CUSTOM_DATA_GRID="custom_data_grid",r.CUSTOM_CHART="custom_chart",r.CUSTOM_CODE_SAMPLE="custom_code_sample",r))(C||{}),y=(r=>(r.LINK="link",r))(y||{});const P=r=>r.content?(console.info(`Using direct content format: ${r.content}`),r.content):r.choices?.[0]?.delta?.content?(console.info(`Using OpenAI delta format: ${r.choices[0].delta.content}`),r.choices[0].delta.content):r.choices?.[0]?.message?.content?(console.info(`Using complete message format: ${r.choices[0].message.content}`),r.choices[0].message.content):(console.info("Unknown SSE data format:",r),null),N=(r,t,n)=>{if(t?.(r),n){const e=P(r);e&&n(e)}},$=(r,t)=>r.error||r.message||`${t.status} ${t.statusText}`,F=(r,t)=>{const n=r?.reduce((e,o)=>(e[o.index]=o,e),{});return t.forEach(e=>{n[e.index]?(e.attachments&&(n[e.index].attachments=(n[e.index].attachments||[]).concat(e.attachments)),e.content&&(n[e.index].content=(n[e.index].content||"")+e.content),e.name&&(n[e.index].name=(n[e.index].name||"")+e.name),e.status&&(n[e.index].status=e.status)):n[e.index]=e}),Object.values(n)},k=(r,t)=>{const n=structuredClone(r);return t.forEach(e=>{e.errorMessage&&(n.errorMessage=e.errorMessage),e.role&&(n.role=e.role),e.responseId&&(n.responseId=e.responseId),e.content&&K(n,e),e.custom_content&&(n.custom_content||(n.custom_content={}),e.custom_content.attachments&&(n.custom_content.attachments||(n.custom_content.attachments=[]),n.custom_content.attachments=n.custom_content.attachments.concat(e.custom_content.attachments)),e.custom_content.stages&&(n.custom_content.stages||(n.custom_content.stages=[]),n.custom_content.stages=F(n.custom_content.stages,e.custom_content.stages)),e.custom_content.state&&(n.custom_content.state=e.custom_content.state),e.custom_content.form_schema&&(n.custom_content.form_schema=e.custom_content.form_schema),e.custom_content.form_value&&(n.custom_content.form_value=e.custom_content.form_value))}),n};function K(r,t){if(t.content){const n=j(t.content);n!=null?r.content=r.content.slice(0,-n):r.content=`${r.content||""}${t.content}`}}function j(r){const t=r.match(/delete_chars\((\d+)\)/);return t!=null?parseInt(t[1],10):null}const v=r=>{const t=r.name?.split("__")||[],n=t.length>1?t.slice(1).join("__"):r.name;return{modelId:t[0],conversationName:n}},w=r=>{const t=Date.now(),n=l.getClearedConversationName(r.name);return`${r.folderId}/${n}-${t}`},Y=r=>({resourceTypes:[O.CONVERSATION],with:r}),p=async(r,t,n)=>{const e=n?.body===void 0?void 0:n?.isFormData?n.body:JSON.stringify(n.body);return await fetch(r,{method:n.method||"GET",headers:t,body:e,signal:n?.signal})};function R(r){return r.split("/").map(t=>encodeURIComponent(t)).join("/")}function G(r){return r.split("/").map(t=>decodeURIComponent(t)).join("/")}function D(r){if(r?.kind!==A.RATE_LIMIT||typeof r.retryAfterSeconds!="number")return null;const t=new Date(r.occurredAt).getTime();return Number.isNaN(t)?null:new Date(t+r.retryAfterSeconds*1e3)}function B(r,t=Date.now()){const n=D(r);return n?n.getTime()>t:!1}function J(r,t="en-GB"){return{date:r.toLocaleDateString(t),time:r.toLocaleTimeString(t,{hour:"2-digit",minute:"2-digit"})}}class L{constructor(){this.decoder=new TextDecoder}async streamChat(t,n,e={},o){const{onMessage:s,onError:a,onComplete:i,signal:c}=e;try{const d=await this.initializeStreamRequest(t,n,c,o);await this.processStreamData(d,s),i?.()}catch(d){this.handleStreamError(d,a)}}async initializeStreamRequest(t,n,e,o){const s=l.getHeaders(void 0,{jwt:o}),a=await p(t,{Accept:"text/event-stream",...s},{method:"POST",body:n,signal:e});if(!a.ok){const i=await a.text();let c={};try{c=JSON.parse(i)}catch{c.error="Failed to parse error body"}throw new l.HttpError({status:a.status,message:c.error??"No response body"})}if(!a.body)throw new l.HttpError({message:"No response body",status:a.status});return a.body.getReader()}async processStreamData(t,n){let e="";try{for(;;){const{done:o,value:s}=await t.read();if(o){e.trim()&&this.parseSSEDataLine(e,n);break}const a=this.decoder.decode(s,{stream:!0});e+=a;const i=e.split(`
|
|
2
|
+
`);e=i.pop()||"";for(const c of i)this.parseSSEDataLine(c,n)}}finally{t.releaseLock()}}handleStreamError(t,n){const e=t instanceof Error?t:new Error(String(t));throw n?.(e),e}parseSSEDataLine(t,n){const e=t.trim();if(!(!e||e.startsWith(":"))&&e.startsWith("data: ")){const o=e.slice(6);if(o==="[DONE]"){console.info("SSE: Stream completed");return}let s;try{s=JSON.parse(o)}catch(a){console.error(`Failed to parse SSE data: ${o} ${a}`)}n?.(s)}}}const q=new L,W=async(r,t,n,e,o)=>{const{onMessage:s,onToken:a,onComplete:i,onError:c,model:d,signal:S}=n,I={conversationId:r,messages:t,model:d,custom_fields:o};await q.streamChat(l.API_ROUTES.CHAT,I,{onMessage:b=>N(b,s,a),onComplete:i,onError:c,signal:S},e)},E=r=>r instanceof Error&&r.message.includes("404"),f=r=>`/v1/conversations/${R(r)}`;function h(r){console.warn("[ConversationApi] suppressed error, returning empty result",r)}class X{constructor(t){this.client=t}async getConversations(t,n,e){const o=`${n?e?`${n}/${e}`:`${n}`:""}`,s=`${u.CONVERSATIONS}/${o}`;try{return(await this.client.getRequest(s+"/?limit=1000&recursive=false",t).then(i=>i.items||[])).map(i=>{const{conversationName:c,modelId:d}=v(i);return{id:i.url?.replace("conversations/","")||i.name,name:c,folderId:o,createdAt:i.createdAt,updatedAt:i.updatedAt,model:{id:d,name:d}}})}catch(a){if(E(a))return h({endpoint:s,error:a}),[];throw a}}async getConversation(t,n){try{return await this.client.getRequest(f(t),n)}catch(e){if(E(e))return h({endpoint:f(t),error:e}),null;throw e}}async getFile(t,n){try{const e=`${u.VERSION}/${R(t)}`;return await this.client.getRequest(e,n)}catch(e){if(E(e))return h({filePath:t,error:e}),null;throw e}}async putOnboardingFile(t,n,e,o){try{const s=`${u.VERSION}/${R(n)}`,a="----NodeMultipartBoundary",c=[`--${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(s,o,{method:"PUT",body:c,headers:l.getMultipartHeaders(a),isFormData:!0})}catch(s){if(E(s))return h({filePath:n,error:s}),null;throw s}}async getOnboardingFile(t,n){try{const e=`${u.VERSION}/${R(t)}`;return await this.client.getRequest(e,n)}catch(e){if(E(e))return h({filePath:t,error:e}),null;throw e}}async getFileBlob(t,n){try{const e=`${u.VERSION}/${R(t)}`;return await this.client.requestBlob(e,n,{method:"GET"})}catch(e){if(E(e))return h({filePath:t,error:e}),null;throw e}}async deleteFile(t,n){try{const e=`${u.VERSION}/${R(t)}`;await this.client.request(e,n,{method:"DELETE"})}catch(e){if(E(e)){h({filePath:t,error:e});return}throw e}}async putFile(t,n,e){try{const o=`${u.VERSION}/${R(t)}`,s=new FormData,a=decodeURIComponent(t.split("/").at(-1)??"file");s.append("attachment",n,a),await this.client.request(o,e,{method:"PUT",body:s,isFormData:!0})}catch(o){if(E(o)){h({filePath:t,error:o});return}throw o}}async createConversation(t,n){const e=t?.id||w(t),{name:o,folderId:s,model:a,messages:i,custom_fields:c}=t,d={id:e,name:o,folderId:s,model:a,messages:i||[],selectedAddons:t.selectedAddons||[],prompt:t.prompt||"",temperature:t.temperature||.7,createdAt:Date.now(),updatedAt:Date.now(),custom_fields:c};return await this.client.request(f(e),n,{method:"PUT",body:d}),{id:e,name:o,folderId:s,model:a,createdAt:d.createdAt,updatedAt:d.updatedAt}}async generateConversationLink(t,n){return await this.client.postRequest(u.SHARE_CONVERSATION,t,{body:n})}async getSharedConversations(t,n){return await this.client.postRequest(u.SHARE_CONVERSATION_LIST,t,{body:n})}async revokeSharedConversations(t,n){await this.client.postRequest(u.SHARE_CONVERSATION_REVOKE,t,{body:n})}async updateConversation(t,n,e){const o=await this.getConversation(t,e);if(!o)throw new Error(`Conversation with id ${t} not found`);const s={...o,...n,updatedAt:Date.now()};return await this.client.request(f(t),e,{method:"PUT",body:s})}async deleteConversation(t,n,e){n?.isShared?await this.client.postRequest(u.SHARE_CONVERSATION_DISCARD,e,{body:{resources:[{url:n?.url}]}}):await this.client.request(f(decodeURI(t)),e,{method:"DELETE"})}async streamChat(t,n){const e=t.model.id,o=encodeURIComponent(e),s=`${u.CHAT(o)}?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(s,n,{method:"POST",body:a,chatReference:t.conversationId})}async rateResponse(t,n,e,o){return await this.client.postRequest(u.RATE(t),o,{body:{responseId:n,rate:e}})}async renameConversation(t,n,e){return await this.client.postRequest(u.RENAME,e,{body:{sourceUrl:t,destinationUrl:n,overwrite:!0}})}}class z{constructor(t){this.config=t,console.info("DialApiClient initialized",{host:t.host||"NOT SET",hasApiKey:!!t.apiKey,version:t.version})}async getRequest(t,n,e){return this.request(t,n,{...e,method:"GET"})}async postRequest(t,n,e){return this.request(t,n,{...e,method:"POST"})}async requestBlob(t,n,e){const o=`${this.config.host}${t}`,s={...l.getHeaders(this.config.apiKey,{jwt:n,chatReference:e.chatReference,contentType:e.isFormData?null:void 0}),...e.headers};try{return(await p(o,s,e)).blob()}catch(a){throw console.error("API Request Exception",{method:e.method,url:o,error:a instanceof Error?a.message:String(a)}),a}}async request(t,n,e){const o=Date.now(),s=`${this.config.host}${t}`,a={...l.getHeaders(this.config.apiKey,{jwt:n,chatReference:e.chatReference,contentType:e.isFormData?null:void 0}),...e.headers};this.addInfoRequestLog("API Request",s,e,a);try{const i=await p(s,a,e),c=Date.now()-o;let d;const S=await i.text();try{d=S?JSON.parse(S):{}}catch{if(this.addErrorRequestParsing(s,e,i,c,S),!i.ok)throw new Error(`API request failed: ${i.status} ${i.statusText} - ${S.substring(0,100)}`);d={data:S}}if(!i.ok){this.addErrorRequestLog(s,e,i,c,d);const I=$(d,i);throw new Error(`API request failed: ${I}`)}return d}catch(i){const c=Date.now()-o;throw console.error("API Request Exception",{method:e.method,url:s,duration:`${c}ms`,error:i instanceof Error?i.message:String(i)}),i}}async stream(t,n,e){const o=`${this.config.host}${t}`,s=l.getHeaders(this.config.apiKey,{jwt:n,chatReference:e.chatReference,contentType:e.isFormData?null:void 0},e.headers);this.addInfoRequestLog("Stream Request",o,e,s);const a=await p(o,s,e);if(!a.ok)throw console.error("Stream Request Failed",{method:e.method||"POST",url:o,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,n,e,o){const s={method:e.method||"GET",url:n,headers:l.sanitizeHeaders(o)};e.body&&(s.body=e.body),console.info(t,s)}addErrorRequestLog(t,n,e,o,s){console.error("API Request Failed",{method:n.method,url:t,status:e.status,statusText:e.statusText,duration:`${o}ms`,response:s})}addErrorRequestParsing(t,n,e,o,s){console.error("API Response Parse Error",{method:n.method,url:t,status:e.status,statusText:e.statusText,duration:`${o}ms`,responseText:s.substring(0,200),error:"Response is not valid JSON"})}}exports.AttachmentType=C;exports.COMPLETION_FINISH_REASON=M;exports.CUSTOM_VIEW_STATE_KEY=V;exports.ChatStreamSSEClient=L;exports.ConversationApi=X;exports.DIAL_API_ROUTES=u;exports.DIAL_ERROR_CODES=H;exports.DIAL_ERROR_TYPES=U;exports.DialApiClient=z;exports.ERROR_CONTEXT_KIND=A;exports.EXCEEDED_LIMIT=_;exports.EXCEEDED_LIMIT_ORDER=x;exports.InvitationType=y;exports.ResourceTypes=O;exports.ShareTarget=g;exports.chatStreamSSEClient=q;exports.decodeApiUrl=G;exports.encodeApiUrl=R;exports.formatDateTime=J;exports.generateConversationId=w;exports.getErrorMessage=$;exports.getRateLimitRestoreDate=D;exports.getSharedConversationsRequest=Y;exports.handleStreamMessage=N;exports.isRateLimitStillActive=B;exports.mergeMessages=k;exports.parseConversationName=v;exports.sendRequest=p;exports.streamChatResponse=W;
|
package/index.mjs
CHANGED
|
@@ -1,170 +1,148 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { getClearedConversationName as N, getHeaders as _, HttpError as O, API_ROUTES as g, getMultipartHeaders as C, sanitizeHeaders as $ } from "@epam/statgpt-shared-toolkit";
|
|
2
|
+
const m = "v1", S = `/${m}/ops/resource/share`, u = {
|
|
3
|
+
VERSION: `/${m}`,
|
|
4
|
+
BUCKET: `/${m}/bucket`,
|
|
5
|
+
CONVERSATIONS: `/${m}/metadata/conversations`,
|
|
6
|
+
CONVERSATION_BY_ID: (o) => `/${m}/metadata/conversations/${o}`,
|
|
7
|
+
CHAT: (o) => `/openai/deployments/${o}/chat/completions`,
|
|
7
8
|
MODELS: "/openai/models",
|
|
8
|
-
CONFIGURATION: (
|
|
9
|
-
DATASETS_METADATA: (
|
|
10
|
-
PYTHON_ATTACHMENT: (
|
|
11
|
-
SHARE_CONVERSATION: `${
|
|
12
|
-
SHARE_CONVERSATION_ACCEPT: (
|
|
13
|
-
SHARE_CONVERSATION_DETAILS: (
|
|
14
|
-
SHARE_CONVERSATION_LIST: `${
|
|
15
|
-
SHARE_CONVERSATION_DISCARD: `${
|
|
16
|
-
SHARE_CONVERSATION_REVOKE: `${
|
|
17
|
-
RATE: (
|
|
18
|
-
RENAME:
|
|
19
|
-
},
|
|
9
|
+
CONFIGURATION: (o) => `/${m}/deployments/${o}/configuration`,
|
|
10
|
+
DATASETS_METADATA: (o) => `/v1/deployments/${o}/route/metadata/datasets`,
|
|
11
|
+
PYTHON_ATTACHMENT: (o) => `/v1/deployments/${o}/route/python-attachment`,
|
|
12
|
+
SHARE_CONVERSATION: `${S}/create`,
|
|
13
|
+
SHARE_CONVERSATION_ACCEPT: (o) => `/${m}/invitations/${o}?accept=true`,
|
|
14
|
+
SHARE_CONVERSATION_DETAILS: (o) => `/${m}/invitations/${o}`,
|
|
15
|
+
SHARE_CONVERSATION_LIST: `${S}/list`,
|
|
16
|
+
SHARE_CONVERSATION_DISCARD: `${S}/discard`,
|
|
17
|
+
SHARE_CONVERSATION_REVOKE: `${S}/revoke`,
|
|
18
|
+
RATE: (o) => `/${m}/${o}/rate`,
|
|
19
|
+
RENAME: `/${m}/ops/resource/move`
|
|
20
|
+
}, G = {
|
|
20
21
|
LENGTH: "length",
|
|
21
22
|
STOP: "stop"
|
|
22
|
-
},
|
|
23
|
+
}, p = {
|
|
23
24
|
MINUTE: "minute",
|
|
24
25
|
DAILY: "daily",
|
|
25
26
|
WEEKLY: "weekly",
|
|
26
27
|
MONTHLY: "monthly"
|
|
27
|
-
},
|
|
28
|
+
}, Y = {
|
|
28
29
|
CONTENT_FILTER: "content_filter",
|
|
29
30
|
500: "500"
|
|
30
|
-
},
|
|
31
|
+
}, B = {
|
|
31
32
|
RUNTIME_ERROR: "runtime_error",
|
|
32
33
|
RATE_LIMIT_EXCEEDED: "rate_limit_exceeded"
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
],
|
|
34
|
+
}, J = [
|
|
35
|
+
p.MINUTE,
|
|
36
|
+
p.DAILY,
|
|
37
|
+
p.WEEKLY,
|
|
38
|
+
p.MONTHLY
|
|
39
|
+
], W = "statgpt", v = {
|
|
39
40
|
RATE_LIMIT: "rate_limit",
|
|
40
41
|
UNKNOWN: "unknown"
|
|
41
42
|
};
|
|
42
|
-
var
|
|
43
|
-
const
|
|
44
|
-
`Using complete message format: ${
|
|
45
|
-
),
|
|
46
|
-
if (t?.(
|
|
47
|
-
const e =
|
|
43
|
+
var A = /* @__PURE__ */ ((o) => (o.CONVERSATION = "CONVERSATION", o))(A || {}), w = /* @__PURE__ */ ((o) => (o.ME = "me", o.OTHERS = "others", o))(w || {}), q = /* @__PURE__ */ ((o) => (o.CSV = "text/csv", o.TABLE = "application/dial-ttyd-table", o.PLOTLY = "application/vnd.plotly.v1+json", o.MARKDOWN = "text/markdown", o.JSON = "application/json", o.JPEG = "image/jpeg", o.PNG = "image/png", o.CROSS_DATASET_GRID = "cross_dataset_grid", o.CUSTOM_DATA_GRID = "custom_data_grid", o.CUSTOM_CHART = "custom_chart", o.CUSTOM_CODE_SAMPLE = "custom_code_sample", o))(q || {}), D = /* @__PURE__ */ ((o) => (o.LINK = "link", o))(D || {});
|
|
44
|
+
const b = (o) => o.content ? (console.info(`Using direct content format: ${o.content}`), o.content) : o.choices?.[0]?.delta?.content ? (console.info(`Using OpenAI delta format: ${o.choices[0].delta.content}`), o.choices[0].delta.content) : o.choices?.[0]?.message?.content ? (console.info(
|
|
45
|
+
`Using complete message format: ${o.choices[0].message.content}`
|
|
46
|
+
), o.choices[0].message.content) : (console.info("Unknown SSE data format:", o), null), L = (o, t, n) => {
|
|
47
|
+
if (t?.(o), n) {
|
|
48
|
+
const e = b(o);
|
|
48
49
|
e && n(e);
|
|
49
50
|
}
|
|
50
|
-
},
|
|
51
|
-
const n =
|
|
51
|
+
}, M = (o, t) => o.error || o.message || `${t.status} ${t.statusText}`, x = (o, t) => {
|
|
52
|
+
const n = o?.reduce(
|
|
52
53
|
(e, r) => (e[r.index] = r, e),
|
|
53
54
|
{}
|
|
54
55
|
);
|
|
55
56
|
return t.forEach((e) => {
|
|
56
57
|
n[e.index] ? (e.attachments && (n[e.index].attachments = (n[e.index].attachments || []).concat(e.attachments)), e.content && (n[e.index].content = (n[e.index].content || "") + e.content), e.name && (n[e.index].name = (n[e.index].name || "") + e.name), e.status && (n[e.index].status = e.status)) : n[e.index] = e;
|
|
57
58
|
}), Object.values(n);
|
|
58
|
-
},
|
|
59
|
-
const n = structuredClone(
|
|
59
|
+
}, z = (o, t) => {
|
|
60
|
+
const n = structuredClone(o);
|
|
60
61
|
return t.forEach((e) => {
|
|
61
|
-
e.errorMessage && (n.errorMessage = e.errorMessage), e.role && (n.role = e.role), e.responseId && (n.responseId = e.responseId), e.content &&
|
|
62
|
+
e.errorMessage && (n.errorMessage = e.errorMessage), e.role && (n.role = e.role), e.responseId && (n.responseId = e.responseId), e.content && H(n, e), e.custom_content && (n.custom_content || (n.custom_content = {}), e.custom_content.attachments && (n.custom_content.attachments || (n.custom_content.attachments = []), n.custom_content.attachments = n.custom_content.attachments.concat(
|
|
62
63
|
e.custom_content.attachments
|
|
63
|
-
)), e.custom_content.stages && (n.custom_content.stages || (n.custom_content.stages = []), n.custom_content.stages =
|
|
64
|
+
)), e.custom_content.stages && (n.custom_content.stages || (n.custom_content.stages = []), n.custom_content.stages = x(
|
|
64
65
|
n.custom_content.stages,
|
|
65
66
|
e.custom_content.stages
|
|
66
67
|
)), e.custom_content.state && (n.custom_content.state = e.custom_content.state), e.custom_content.form_schema && (n.custom_content.form_schema = e.custom_content.form_schema), e.custom_content.form_value && (n.custom_content.form_value = e.custom_content.form_value));
|
|
67
68
|
}), n;
|
|
68
69
|
};
|
|
69
|
-
function
|
|
70
|
+
function H(o, t) {
|
|
70
71
|
if (t.content) {
|
|
71
|
-
const n =
|
|
72
|
-
n != null ?
|
|
72
|
+
const n = U(t.content);
|
|
73
|
+
n != null ? o.content = o.content.slice(0, -n) : o.content = `${o.content || ""}${t.content}`;
|
|
73
74
|
}
|
|
74
75
|
}
|
|
75
|
-
function
|
|
76
|
-
const t =
|
|
76
|
+
function U(o) {
|
|
77
|
+
const t = o.match(/delete_chars\((\d+)\)/);
|
|
77
78
|
return t != null ? parseInt(t[1], 10) : null;
|
|
78
79
|
}
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
}, H = "application/json", x = "Content-Type", p = "Api-Key", P = "X-CONVERSATION-ID", I = "Ocp-Apim-Subscription-Key";
|
|
82
|
-
class A extends Error {
|
|
83
|
-
constructor(t) {
|
|
84
|
-
super(t.message), this.isHttpError = !0, this.name = "HttpError", this.code = t.code, this.status = t.status, this.details = t.details, this.displayMessage = t.displayMessage;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
const U = (s) => new RegExp("\\p{L}", "u").test(s) || new RegExp("\\p{N}", "u").test(s), F = (s) => {
|
|
88
|
-
if (!s) return "";
|
|
89
|
-
const t = s.toLowerCase(), n = [];
|
|
90
|
-
let e = "";
|
|
91
|
-
for (const r of t)
|
|
92
|
-
U(r) ? e += r : e && (n.push(e), e = "");
|
|
93
|
-
return e && n.push(e), n.join(" ");
|
|
94
|
-
}, _ = (s, t, n) => {
|
|
95
|
-
const e = {};
|
|
96
|
-
return t?.contentType !== null && (e[x] = t?.contentType || H), t?.jwt ? e.Authorization = `Bearer ${t.jwt}` : s && (e[p] = s), t?.chatReference && (e[P] = t.chatReference), { ...e, ...n };
|
|
97
|
-
}, K = (s) => {
|
|
98
|
-
const t = { ...s };
|
|
99
|
-
return t[p] && (t[p] = t[p].substring(0, 8) + "...[REDACTED]"), t.Authorization && (t.Authorization = "Bearer [REDACTED]"), t[I] && (t[I] = t[I].substring(0, 8) + "...[REDACTED]"), t;
|
|
100
|
-
}, j = (s) => ({
|
|
101
|
-
"Content-Type": `multipart/form-data; boundary=${s}`
|
|
102
|
-
}), k = (s) => {
|
|
103
|
-
const t = s.name?.split("__") || [], n = t.length > 1 ? t.slice(1).join("__") : s.name;
|
|
80
|
+
const V = (o) => {
|
|
81
|
+
const t = o.name?.split("__") || [], n = t.length > 1 ? t.slice(1).join("__") : o.name;
|
|
104
82
|
return { modelId: t[0], conversationName: n };
|
|
105
|
-
},
|
|
106
|
-
const t = Date.now(), n =
|
|
107
|
-
return `${
|
|
108
|
-
},
|
|
83
|
+
}, P = (o) => {
|
|
84
|
+
const t = Date.now(), n = N(o.name);
|
|
85
|
+
return `${o.folderId}/${n}-${t}`;
|
|
86
|
+
}, X = (o) => ({ resourceTypes: [A.CONVERSATION], with: o }), T = async (o, t, n) => {
|
|
109
87
|
const e = n?.body === void 0 ? void 0 : n?.isFormData ? n.body : JSON.stringify(n.body);
|
|
110
|
-
return await fetch(
|
|
88
|
+
return await fetch(o, {
|
|
111
89
|
method: n.method || "GET",
|
|
112
90
|
headers: t,
|
|
113
91
|
body: e,
|
|
114
92
|
signal: n?.signal
|
|
115
93
|
});
|
|
116
94
|
};
|
|
117
|
-
function
|
|
118
|
-
return
|
|
95
|
+
function R(o) {
|
|
96
|
+
return o.split("/").map((t) => encodeURIComponent(t)).join("/");
|
|
119
97
|
}
|
|
120
|
-
function
|
|
121
|
-
return
|
|
98
|
+
function Q(o) {
|
|
99
|
+
return o.split("/").map((t) => decodeURIComponent(t)).join("/");
|
|
122
100
|
}
|
|
123
|
-
function
|
|
124
|
-
if (
|
|
101
|
+
function F(o) {
|
|
102
|
+
if (o?.kind !== v.RATE_LIMIT || typeof o.retryAfterSeconds != "number")
|
|
125
103
|
return null;
|
|
126
|
-
const t = new Date(
|
|
127
|
-
return Number.isNaN(t) ? null : new Date(t +
|
|
104
|
+
const t = new Date(o.occurredAt).getTime();
|
|
105
|
+
return Number.isNaN(t) ? null : new Date(t + o.retryAfterSeconds * 1e3);
|
|
128
106
|
}
|
|
129
|
-
function
|
|
130
|
-
const n =
|
|
107
|
+
function Z(o, t = Date.now()) {
|
|
108
|
+
const n = F(o);
|
|
131
109
|
return n ? n.getTime() > t : !1;
|
|
132
110
|
}
|
|
133
|
-
function
|
|
111
|
+
function ee(o, t = "en-GB") {
|
|
134
112
|
return {
|
|
135
|
-
date:
|
|
136
|
-
time:
|
|
113
|
+
date: o.toLocaleDateString(t),
|
|
114
|
+
time: o.toLocaleTimeString(t, {
|
|
137
115
|
hour: "2-digit",
|
|
138
116
|
minute: "2-digit"
|
|
139
117
|
})
|
|
140
118
|
};
|
|
141
119
|
}
|
|
142
|
-
class
|
|
120
|
+
class k {
|
|
143
121
|
constructor() {
|
|
144
122
|
this.decoder = new TextDecoder();
|
|
145
123
|
}
|
|
146
124
|
async streamChat(t, n, e = {}, r) {
|
|
147
|
-
const { onMessage:
|
|
125
|
+
const { onMessage: s, onError: a, onComplete: c, signal: i } = e;
|
|
148
126
|
try {
|
|
149
|
-
const
|
|
127
|
+
const d = await this.initializeStreamRequest(
|
|
150
128
|
t,
|
|
151
129
|
n,
|
|
152
130
|
i,
|
|
153
131
|
r
|
|
154
132
|
);
|
|
155
|
-
await this.processStreamData(
|
|
156
|
-
} catch (
|
|
157
|
-
this.handleStreamError(
|
|
133
|
+
await this.processStreamData(d, s), c?.();
|
|
134
|
+
} catch (d) {
|
|
135
|
+
this.handleStreamError(d, a);
|
|
158
136
|
}
|
|
159
137
|
}
|
|
160
138
|
async initializeStreamRequest(t, n, e, r) {
|
|
161
|
-
const
|
|
139
|
+
const s = _(void 0, {
|
|
162
140
|
jwt: r
|
|
163
|
-
}),
|
|
141
|
+
}), a = await T(
|
|
164
142
|
t,
|
|
165
143
|
{
|
|
166
144
|
Accept: "text/event-stream",
|
|
167
|
-
...
|
|
145
|
+
...s
|
|
168
146
|
},
|
|
169
147
|
{
|
|
170
148
|
method: "POST",
|
|
@@ -172,41 +150,41 @@ class G {
|
|
|
172
150
|
signal: e
|
|
173
151
|
}
|
|
174
152
|
);
|
|
175
|
-
if (!
|
|
176
|
-
const
|
|
153
|
+
if (!a.ok) {
|
|
154
|
+
const c = await a.text();
|
|
177
155
|
let i = {};
|
|
178
156
|
try {
|
|
179
|
-
i = JSON.parse(
|
|
157
|
+
i = JSON.parse(c);
|
|
180
158
|
} catch {
|
|
181
159
|
i.error = "Failed to parse error body";
|
|
182
160
|
}
|
|
183
|
-
throw new
|
|
184
|
-
status:
|
|
161
|
+
throw new O({
|
|
162
|
+
status: a.status,
|
|
185
163
|
message: i.error ?? "No response body"
|
|
186
164
|
});
|
|
187
165
|
}
|
|
188
|
-
if (!
|
|
189
|
-
throw new
|
|
166
|
+
if (!a.body)
|
|
167
|
+
throw new O({
|
|
190
168
|
message: "No response body",
|
|
191
|
-
status:
|
|
169
|
+
status: a.status
|
|
192
170
|
});
|
|
193
|
-
return
|
|
171
|
+
return a.body.getReader();
|
|
194
172
|
}
|
|
195
173
|
async processStreamData(t, n) {
|
|
196
174
|
let e = "";
|
|
197
175
|
try {
|
|
198
176
|
for (; ; ) {
|
|
199
|
-
const { done: r, value:
|
|
177
|
+
const { done: r, value: s } = await t.read();
|
|
200
178
|
if (r) {
|
|
201
179
|
e.trim() && this.parseSSEDataLine(e, n);
|
|
202
180
|
break;
|
|
203
181
|
}
|
|
204
|
-
const
|
|
205
|
-
e +=
|
|
206
|
-
const
|
|
182
|
+
const a = this.decoder.decode(s, { stream: !0 });
|
|
183
|
+
e += a;
|
|
184
|
+
const c = e.split(`
|
|
207
185
|
`);
|
|
208
|
-
e =
|
|
209
|
-
for (const i of
|
|
186
|
+
e = c.pop() || "";
|
|
187
|
+
for (const i of c)
|
|
210
188
|
this.parseSSEDataLine(i, n);
|
|
211
189
|
}
|
|
212
190
|
} finally {
|
|
@@ -225,138 +203,138 @@ class G {
|
|
|
225
203
|
console.info("SSE: Stream completed");
|
|
226
204
|
return;
|
|
227
205
|
}
|
|
228
|
-
let
|
|
206
|
+
let s;
|
|
229
207
|
try {
|
|
230
|
-
|
|
231
|
-
} catch (
|
|
232
|
-
console.error(`Failed to parse SSE data: ${r} ${
|
|
208
|
+
s = JSON.parse(r);
|
|
209
|
+
} catch (a) {
|
|
210
|
+
console.error(`Failed to parse SSE data: ${r} ${a}`);
|
|
233
211
|
}
|
|
234
|
-
n?.(
|
|
212
|
+
n?.(s);
|
|
235
213
|
}
|
|
236
214
|
}
|
|
237
215
|
}
|
|
238
|
-
const
|
|
239
|
-
const { onMessage:
|
|
240
|
-
conversationId:
|
|
216
|
+
const K = new k(), te = async (o, t, n, e, r) => {
|
|
217
|
+
const { onMessage: s, onToken: a, onComplete: c, onError: i, model: d, signal: E } = n, I = {
|
|
218
|
+
conversationId: o,
|
|
241
219
|
messages: t,
|
|
242
|
-
model:
|
|
220
|
+
model: d,
|
|
243
221
|
custom_fields: r
|
|
244
222
|
};
|
|
245
|
-
await
|
|
246
|
-
|
|
247
|
-
|
|
223
|
+
await K.streamChat(
|
|
224
|
+
g.CHAT,
|
|
225
|
+
I,
|
|
248
226
|
{
|
|
249
|
-
onMessage: (y) =>
|
|
250
|
-
onComplete:
|
|
227
|
+
onMessage: (y) => L(y, s, a),
|
|
228
|
+
onComplete: c,
|
|
251
229
|
onError: i,
|
|
252
230
|
signal: E
|
|
253
231
|
},
|
|
254
232
|
e
|
|
255
233
|
);
|
|
256
|
-
}, l = (
|
|
257
|
-
function
|
|
234
|
+
}, l = (o) => o instanceof Error && o.message.includes("404"), f = (o) => `/v1/conversations/${R(o)}`;
|
|
235
|
+
function h(o) {
|
|
258
236
|
console.warn(
|
|
259
237
|
"[ConversationApi] suppressed error, returning empty result",
|
|
260
|
-
|
|
238
|
+
o
|
|
261
239
|
);
|
|
262
240
|
}
|
|
263
|
-
class
|
|
241
|
+
class ne {
|
|
264
242
|
constructor(t) {
|
|
265
243
|
this.client = t;
|
|
266
244
|
}
|
|
267
245
|
async getConversations(t, n, e) {
|
|
268
|
-
const r = `${n ? e ? `${n}/${e}` : `${n}` : ""}`,
|
|
246
|
+
const r = `${n ? e ? `${n}/${e}` : `${n}` : ""}`, s = `${u.CONVERSATIONS}/${r}`;
|
|
269
247
|
try {
|
|
270
|
-
return (await this.client.getRequest(
|
|
271
|
-
const { conversationName: i, modelId:
|
|
248
|
+
return (await this.client.getRequest(s + "/?limit=1000&recursive=false", t).then((c) => c.items || [])).map((c) => {
|
|
249
|
+
const { conversationName: i, modelId: d } = V(c);
|
|
272
250
|
return {
|
|
273
|
-
id:
|
|
251
|
+
id: c.url?.replace("conversations/", "") || c.name,
|
|
274
252
|
name: i,
|
|
275
253
|
folderId: r,
|
|
276
|
-
createdAt:
|
|
277
|
-
updatedAt:
|
|
278
|
-
model: { id:
|
|
254
|
+
createdAt: c.createdAt,
|
|
255
|
+
updatedAt: c.updatedAt,
|
|
256
|
+
model: { id: d, name: d }
|
|
279
257
|
};
|
|
280
258
|
});
|
|
281
|
-
} catch (
|
|
282
|
-
if (l(
|
|
283
|
-
return
|
|
284
|
-
throw
|
|
259
|
+
} catch (a) {
|
|
260
|
+
if (l(a))
|
|
261
|
+
return h({ endpoint: s, error: a }), [];
|
|
262
|
+
throw a;
|
|
285
263
|
}
|
|
286
264
|
}
|
|
287
265
|
async getConversation(t, n) {
|
|
288
266
|
try {
|
|
289
267
|
return await this.client.getRequest(
|
|
290
|
-
|
|
268
|
+
f(t),
|
|
291
269
|
n
|
|
292
270
|
);
|
|
293
271
|
} catch (e) {
|
|
294
272
|
if (l(e))
|
|
295
|
-
return
|
|
273
|
+
return h({ endpoint: f(t), error: e }), null;
|
|
296
274
|
throw e;
|
|
297
275
|
}
|
|
298
276
|
}
|
|
299
277
|
async getFile(t, n) {
|
|
300
278
|
try {
|
|
301
|
-
const e = `${
|
|
279
|
+
const e = `${u.VERSION}/${R(t)}`;
|
|
302
280
|
return await this.client.getRequest(e, n);
|
|
303
281
|
} catch (e) {
|
|
304
282
|
if (l(e))
|
|
305
|
-
return
|
|
283
|
+
return h({ filePath: t, error: e }), null;
|
|
306
284
|
throw e;
|
|
307
285
|
}
|
|
308
286
|
}
|
|
309
287
|
async putOnboardingFile(t, n, e, r) {
|
|
310
288
|
try {
|
|
311
|
-
const
|
|
312
|
-
`--${
|
|
289
|
+
const s = `${u.VERSION}/${R(n)}`, a = "----NodeMultipartBoundary", i = [
|
|
290
|
+
`--${a}`,
|
|
313
291
|
`Content-Disposition: form-data; name="file"; filename="${t}"`,
|
|
314
292
|
"Content-Type: application/json",
|
|
315
293
|
"",
|
|
316
294
|
JSON.stringify(e),
|
|
317
|
-
`--${
|
|
295
|
+
`--${a}--`,
|
|
318
296
|
""
|
|
319
297
|
].join(`\r
|
|
320
298
|
`);
|
|
321
|
-
return await this.client.request(
|
|
299
|
+
return await this.client.request(s, r, {
|
|
322
300
|
method: "PUT",
|
|
323
301
|
body: i,
|
|
324
|
-
headers:
|
|
302
|
+
headers: C(a),
|
|
325
303
|
isFormData: !0
|
|
326
304
|
});
|
|
327
|
-
} catch (
|
|
328
|
-
if (l(
|
|
329
|
-
return
|
|
330
|
-
throw
|
|
305
|
+
} catch (s) {
|
|
306
|
+
if (l(s))
|
|
307
|
+
return h({ filePath: n, error: s }), null;
|
|
308
|
+
throw s;
|
|
331
309
|
}
|
|
332
310
|
}
|
|
333
311
|
async getOnboardingFile(t, n) {
|
|
334
312
|
try {
|
|
335
|
-
const e = `${
|
|
313
|
+
const e = `${u.VERSION}/${R(t)}`;
|
|
336
314
|
return await this.client.getRequest(e, n);
|
|
337
315
|
} catch (e) {
|
|
338
316
|
if (l(e))
|
|
339
|
-
return
|
|
317
|
+
return h({ filePath: t, error: e }), null;
|
|
340
318
|
throw e;
|
|
341
319
|
}
|
|
342
320
|
}
|
|
343
321
|
async getFileBlob(t, n) {
|
|
344
322
|
try {
|
|
345
|
-
const e = `${
|
|
323
|
+
const e = `${u.VERSION}/${R(t)}`;
|
|
346
324
|
return await this.client.requestBlob(e, n, { method: "GET" });
|
|
347
325
|
} catch (e) {
|
|
348
326
|
if (l(e))
|
|
349
|
-
return
|
|
327
|
+
return h({ filePath: t, error: e }), null;
|
|
350
328
|
throw e;
|
|
351
329
|
}
|
|
352
330
|
}
|
|
353
331
|
async deleteFile(t, n) {
|
|
354
332
|
try {
|
|
355
|
-
const e = `${
|
|
333
|
+
const e = `${u.VERSION}/${R(t)}`;
|
|
356
334
|
await this.client.request(e, n, { method: "DELETE" });
|
|
357
335
|
} catch (e) {
|
|
358
336
|
if (l(e)) {
|
|
359
|
-
|
|
337
|
+
h({ filePath: t, error: e });
|
|
360
338
|
return;
|
|
361
339
|
}
|
|
362
340
|
throw e;
|
|
@@ -364,27 +342,27 @@ class ce {
|
|
|
364
342
|
}
|
|
365
343
|
async putFile(t, n, e) {
|
|
366
344
|
try {
|
|
367
|
-
const r = `${
|
|
368
|
-
|
|
345
|
+
const r = `${u.VERSION}/${R(t)}`, s = new FormData(), a = decodeURIComponent(t.split("/").at(-1) ?? "file");
|
|
346
|
+
s.append("attachment", n, a), await this.client.request(r, e, {
|
|
369
347
|
method: "PUT",
|
|
370
|
-
body:
|
|
348
|
+
body: s,
|
|
371
349
|
isFormData: !0
|
|
372
350
|
});
|
|
373
351
|
} catch (r) {
|
|
374
352
|
if (l(r)) {
|
|
375
|
-
|
|
353
|
+
h({ filePath: t, error: r });
|
|
376
354
|
return;
|
|
377
355
|
}
|
|
378
356
|
throw r;
|
|
379
357
|
}
|
|
380
358
|
}
|
|
381
359
|
async createConversation(t, n) {
|
|
382
|
-
const e = t?.id ||
|
|
360
|
+
const e = t?.id || P(t), { name: r, folderId: s, model: a, messages: c, custom_fields: i } = t, d = {
|
|
383
361
|
id: e,
|
|
384
362
|
name: r,
|
|
385
|
-
folderId:
|
|
386
|
-
model:
|
|
387
|
-
messages:
|
|
363
|
+
folderId: s,
|
|
364
|
+
model: a,
|
|
365
|
+
messages: c || [],
|
|
388
366
|
selectedAddons: t.selectedAddons || [],
|
|
389
367
|
prompt: t.prompt || "",
|
|
390
368
|
temperature: t.temperature || 0.7,
|
|
@@ -393,24 +371,24 @@ class ce {
|
|
|
393
371
|
custom_fields: i
|
|
394
372
|
};
|
|
395
373
|
return await this.client.request(
|
|
396
|
-
|
|
374
|
+
f(e),
|
|
397
375
|
n,
|
|
398
376
|
{
|
|
399
377
|
method: "PUT",
|
|
400
|
-
body:
|
|
378
|
+
body: d
|
|
401
379
|
}
|
|
402
380
|
), {
|
|
403
381
|
id: e,
|
|
404
382
|
name: r,
|
|
405
|
-
folderId:
|
|
406
|
-
model:
|
|
407
|
-
createdAt:
|
|
408
|
-
updatedAt:
|
|
383
|
+
folderId: s,
|
|
384
|
+
model: a,
|
|
385
|
+
createdAt: d.createdAt,
|
|
386
|
+
updatedAt: d.updatedAt
|
|
409
387
|
};
|
|
410
388
|
}
|
|
411
389
|
async generateConversationLink(t, n) {
|
|
412
390
|
return await this.client.postRequest(
|
|
413
|
-
|
|
391
|
+
u.SHARE_CONVERSATION,
|
|
414
392
|
t,
|
|
415
393
|
{
|
|
416
394
|
body: n
|
|
@@ -419,7 +397,7 @@ class ce {
|
|
|
419
397
|
}
|
|
420
398
|
async getSharedConversations(t, n) {
|
|
421
399
|
return await this.client.postRequest(
|
|
422
|
-
|
|
400
|
+
u.SHARE_CONVERSATION_LIST,
|
|
423
401
|
t,
|
|
424
402
|
{
|
|
425
403
|
body: n
|
|
@@ -428,7 +406,7 @@ class ce {
|
|
|
428
406
|
}
|
|
429
407
|
async revokeSharedConversations(t, n) {
|
|
430
408
|
await this.client.postRequest(
|
|
431
|
-
|
|
409
|
+
u.SHARE_CONVERSATION_REVOKE,
|
|
432
410
|
t,
|
|
433
411
|
{
|
|
434
412
|
body: n
|
|
@@ -439,23 +417,23 @@ class ce {
|
|
|
439
417
|
const r = await this.getConversation(t, e);
|
|
440
418
|
if (!r)
|
|
441
419
|
throw new Error(`Conversation with id ${t} not found`);
|
|
442
|
-
const
|
|
420
|
+
const s = {
|
|
443
421
|
...r,
|
|
444
422
|
...n,
|
|
445
423
|
updatedAt: Date.now()
|
|
446
424
|
};
|
|
447
425
|
return await this.client.request(
|
|
448
|
-
|
|
426
|
+
f(t),
|
|
449
427
|
e,
|
|
450
428
|
{
|
|
451
429
|
method: "PUT",
|
|
452
|
-
body:
|
|
430
|
+
body: s
|
|
453
431
|
}
|
|
454
432
|
);
|
|
455
433
|
}
|
|
456
434
|
async deleteConversation(t, n, e) {
|
|
457
435
|
n?.isShared ? await this.client.postRequest(
|
|
458
|
-
|
|
436
|
+
u.SHARE_CONVERSATION_DISCARD,
|
|
459
437
|
e,
|
|
460
438
|
{
|
|
461
439
|
body: {
|
|
@@ -466,27 +444,27 @@ class ce {
|
|
|
466
444
|
]
|
|
467
445
|
}
|
|
468
446
|
}
|
|
469
|
-
) : await this.client.request(
|
|
447
|
+
) : await this.client.request(f(decodeURI(t)), e, {
|
|
470
448
|
method: "DELETE"
|
|
471
449
|
});
|
|
472
450
|
}
|
|
473
451
|
async streamChat(t, n) {
|
|
474
|
-
const e = t.model.id, r = encodeURIComponent(e),
|
|
452
|
+
const e = t.model.id, r = encodeURIComponent(e), s = `${u.CHAT(r)}?api-version=${this.client.config.version}`, a = {
|
|
475
453
|
messages: t.messages,
|
|
476
454
|
stream: !0,
|
|
477
455
|
temperature: 0.7,
|
|
478
456
|
max_tokens: 4096,
|
|
479
457
|
custom_fields: t.custom_fields
|
|
480
458
|
};
|
|
481
|
-
return await this.client.stream(
|
|
459
|
+
return await this.client.stream(s, n, {
|
|
482
460
|
method: "POST",
|
|
483
|
-
body:
|
|
461
|
+
body: a,
|
|
484
462
|
chatReference: t.conversationId
|
|
485
463
|
});
|
|
486
464
|
}
|
|
487
465
|
async rateResponse(t, n, e, r) {
|
|
488
466
|
return await this.client.postRequest(
|
|
489
|
-
|
|
467
|
+
u.RATE(t),
|
|
490
468
|
r,
|
|
491
469
|
{
|
|
492
470
|
body: {
|
|
@@ -497,7 +475,7 @@ class ce {
|
|
|
497
475
|
);
|
|
498
476
|
}
|
|
499
477
|
async renameConversation(t, n, e) {
|
|
500
|
-
return await this.client.postRequest(
|
|
478
|
+
return await this.client.postRequest(u.RENAME, e, {
|
|
501
479
|
body: {
|
|
502
480
|
sourceUrl: t,
|
|
503
481
|
destinationUrl: n,
|
|
@@ -506,7 +484,7 @@ class ce {
|
|
|
506
484
|
});
|
|
507
485
|
}
|
|
508
486
|
}
|
|
509
|
-
class
|
|
487
|
+
class oe {
|
|
510
488
|
constructor(t) {
|
|
511
489
|
this.config = t, console.info("DialApiClient initialized", {
|
|
512
490
|
host: t.host || "NOT SET",
|
|
@@ -521,7 +499,7 @@ class ae {
|
|
|
521
499
|
return this.request(t, n, { ...e, method: "POST" });
|
|
522
500
|
}
|
|
523
501
|
async requestBlob(t, n, e) {
|
|
524
|
-
const r = `${this.config.host}${t}`,
|
|
502
|
+
const r = `${this.config.host}${t}`, s = {
|
|
525
503
|
..._(this.config.apiKey, {
|
|
526
504
|
jwt: n,
|
|
527
505
|
chatReference: e.chatReference,
|
|
@@ -530,17 +508,17 @@ class ae {
|
|
|
530
508
|
...e.headers
|
|
531
509
|
};
|
|
532
510
|
try {
|
|
533
|
-
return (await T(r,
|
|
534
|
-
} catch (
|
|
511
|
+
return (await T(r, s, e)).blob();
|
|
512
|
+
} catch (a) {
|
|
535
513
|
throw console.error("API Request Exception", {
|
|
536
514
|
method: e.method,
|
|
537
515
|
url: r,
|
|
538
|
-
error:
|
|
539
|
-
}),
|
|
516
|
+
error: a instanceof Error ? a.message : String(a)
|
|
517
|
+
}), a;
|
|
540
518
|
}
|
|
541
519
|
}
|
|
542
520
|
async request(t, n, e) {
|
|
543
|
-
const r = Date.now(),
|
|
521
|
+
const r = Date.now(), s = `${this.config.host}${t}`, a = {
|
|
544
522
|
..._(this.config.apiKey, {
|
|
545
523
|
jwt: n,
|
|
546
524
|
chatReference: e.chatReference,
|
|
@@ -548,44 +526,44 @@ class ae {
|
|
|
548
526
|
}),
|
|
549
527
|
...e.headers
|
|
550
528
|
};
|
|
551
|
-
this.addInfoRequestLog("API Request",
|
|
529
|
+
this.addInfoRequestLog("API Request", s, e, a);
|
|
552
530
|
try {
|
|
553
|
-
const
|
|
554
|
-
let
|
|
555
|
-
const E = await
|
|
531
|
+
const c = await T(s, a, e), i = Date.now() - r;
|
|
532
|
+
let d;
|
|
533
|
+
const E = await c.text();
|
|
556
534
|
try {
|
|
557
|
-
|
|
535
|
+
d = E ? JSON.parse(E) : {};
|
|
558
536
|
} catch {
|
|
559
537
|
if (this.addErrorRequestParsing(
|
|
560
|
-
|
|
538
|
+
s,
|
|
561
539
|
e,
|
|
562
|
-
|
|
540
|
+
c,
|
|
563
541
|
i,
|
|
564
542
|
E
|
|
565
|
-
), !
|
|
543
|
+
), !c.ok)
|
|
566
544
|
throw new Error(
|
|
567
|
-
`API request failed: ${
|
|
545
|
+
`API request failed: ${c.status} ${c.statusText} - ${E.substring(0, 100)}`
|
|
568
546
|
);
|
|
569
|
-
|
|
547
|
+
d = { data: E };
|
|
570
548
|
}
|
|
571
|
-
if (!
|
|
572
|
-
this.addErrorRequestLog(
|
|
573
|
-
const
|
|
574
|
-
throw new Error(`API request failed: ${
|
|
549
|
+
if (!c.ok) {
|
|
550
|
+
this.addErrorRequestLog(s, e, c, i, d);
|
|
551
|
+
const I = M(d, c);
|
|
552
|
+
throw new Error(`API request failed: ${I}`);
|
|
575
553
|
}
|
|
576
|
-
return
|
|
577
|
-
} catch (
|
|
554
|
+
return d;
|
|
555
|
+
} catch (c) {
|
|
578
556
|
const i = Date.now() - r;
|
|
579
557
|
throw console.error("API Request Exception", {
|
|
580
558
|
method: e.method,
|
|
581
|
-
url:
|
|
559
|
+
url: s,
|
|
582
560
|
duration: `${i}ms`,
|
|
583
|
-
error:
|
|
584
|
-
}),
|
|
561
|
+
error: c instanceof Error ? c.message : String(c)
|
|
562
|
+
}), c;
|
|
585
563
|
}
|
|
586
564
|
}
|
|
587
565
|
async stream(t, n, e) {
|
|
588
|
-
const r = `${this.config.host}${t}`,
|
|
566
|
+
const r = `${this.config.host}${t}`, s = _(
|
|
589
567
|
this.config.apiKey,
|
|
590
568
|
{
|
|
591
569
|
jwt: n,
|
|
@@ -594,80 +572,80 @@ class ae {
|
|
|
594
572
|
},
|
|
595
573
|
e.headers
|
|
596
574
|
);
|
|
597
|
-
this.addInfoRequestLog("Stream Request", r, e,
|
|
598
|
-
const
|
|
599
|
-
if (!
|
|
575
|
+
this.addInfoRequestLog("Stream Request", r, e, s);
|
|
576
|
+
const a = await T(r, s, e);
|
|
577
|
+
if (!a.ok)
|
|
600
578
|
throw console.error("Stream Request Failed", {
|
|
601
579
|
method: e.method || "POST",
|
|
602
580
|
url: r,
|
|
603
|
-
status:
|
|
604
|
-
statusText:
|
|
581
|
+
status: a.status,
|
|
582
|
+
statusText: a.statusText
|
|
605
583
|
}), new Error(
|
|
606
|
-
`Stream request failed: ${
|
|
584
|
+
`Stream request failed: ${a.status} ${a.statusText}`
|
|
607
585
|
);
|
|
608
|
-
if (!
|
|
586
|
+
if (!a.body)
|
|
609
587
|
throw new Error("No response body for stream");
|
|
610
|
-
return
|
|
588
|
+
return a.body;
|
|
611
589
|
}
|
|
612
590
|
addInfoRequestLog(t, n, e, r) {
|
|
613
|
-
const
|
|
591
|
+
const s = {
|
|
614
592
|
method: e.method || "GET",
|
|
615
593
|
url: n,
|
|
616
|
-
headers:
|
|
594
|
+
headers: $(r)
|
|
617
595
|
};
|
|
618
|
-
e.body && (
|
|
596
|
+
e.body && (s.body = e.body), console.info(t, s);
|
|
619
597
|
}
|
|
620
|
-
addErrorRequestLog(t, n, e, r,
|
|
598
|
+
addErrorRequestLog(t, n, e, r, s) {
|
|
621
599
|
console.error("API Request Failed", {
|
|
622
600
|
method: n.method,
|
|
623
601
|
url: t,
|
|
624
602
|
status: e.status,
|
|
625
603
|
statusText: e.statusText,
|
|
626
604
|
duration: `${r}ms`,
|
|
627
|
-
response:
|
|
605
|
+
response: s
|
|
628
606
|
});
|
|
629
607
|
}
|
|
630
|
-
addErrorRequestParsing(t, n, e, r,
|
|
608
|
+
addErrorRequestParsing(t, n, e, r, s) {
|
|
631
609
|
console.error("API Response Parse Error", {
|
|
632
610
|
method: n.method,
|
|
633
611
|
url: t,
|
|
634
612
|
status: e.status,
|
|
635
613
|
statusText: e.statusText,
|
|
636
614
|
duration: `${r}ms`,
|
|
637
|
-
responseText:
|
|
615
|
+
responseText: s.substring(0, 200),
|
|
638
616
|
// First 200 chars
|
|
639
617
|
error: "Response is not valid JSON"
|
|
640
618
|
});
|
|
641
619
|
}
|
|
642
620
|
}
|
|
643
621
|
export {
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
622
|
+
q as AttachmentType,
|
|
623
|
+
G as COMPLETION_FINISH_REASON,
|
|
624
|
+
W as CUSTOM_VIEW_STATE_KEY,
|
|
625
|
+
k as ChatStreamSSEClient,
|
|
626
|
+
ne as ConversationApi,
|
|
627
|
+
u as DIAL_API_ROUTES,
|
|
628
|
+
Y as DIAL_ERROR_CODES,
|
|
629
|
+
B as DIAL_ERROR_TYPES,
|
|
630
|
+
oe as DialApiClient,
|
|
631
|
+
v as ERROR_CONTEXT_KIND,
|
|
632
|
+
p as EXCEEDED_LIMIT,
|
|
633
|
+
J as EXCEEDED_LIMIT_ORDER,
|
|
634
|
+
D as InvitationType,
|
|
635
|
+
A as ResourceTypes,
|
|
636
|
+
w as ShareTarget,
|
|
637
|
+
K as chatStreamSSEClient,
|
|
638
|
+
Q as decodeApiUrl,
|
|
639
|
+
R as encodeApiUrl,
|
|
640
|
+
ee as formatDateTime,
|
|
641
|
+
P as generateConversationId,
|
|
642
|
+
M as getErrorMessage,
|
|
643
|
+
F as getRateLimitRestoreDate,
|
|
644
|
+
X as getSharedConversationsRequest,
|
|
645
|
+
L as handleStreamMessage,
|
|
646
|
+
Z as isRateLimitStillActive,
|
|
647
|
+
z as mergeMessages,
|
|
648
|
+
V as parseConversationName,
|
|
671
649
|
T as sendRequest,
|
|
672
|
-
|
|
650
|
+
te as streamChatResponse
|
|
673
651
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@epam/statgpt-dial-toolkit",
|
|
3
|
-
"version": "0.7.0-dev.
|
|
3
|
+
"version": "0.7.0-dev.19",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -14,7 +14,9 @@
|
|
|
14
14
|
"module": "./index.mjs",
|
|
15
15
|
"types": "./index.d.ts",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@epam/statgpt-shared-toolkit": "0.7.0-dev.
|
|
17
|
+
"@epam/statgpt-shared-toolkit": "0.7.0-dev.19"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
18
20
|
"@epam/ai-dial-shared": "^0.43.3"
|
|
19
21
|
}
|
|
20
22
|
}
|