@epam/statgpt-shared-toolkit 0.3.0-rc.9 → 0.3.1
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.js +1 -1
- package/index.mjs +105 -69
- package/package.json +1 -1
- package/utils/conversation-attachments.d.ts +5 -0
- package/utils/headers.d.ts +1 -1
- package/utils/index.d.ts +1 -0
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("date-fns"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("date-fns"),R={CHAT:"/api/chat"},f="CUSTOM_PERIOD",l={decimal:"0.0",decimalSymbol:",",digitGroupingSymbol:" "},A="application/json",T="Content-Type",a="Api-Key",N="X-CONVERSATION-ID",u="Ocp-Apim-Subscription-Key",S={BAD_REQUEST:400,UNAUTHORIZED:401,FORBIDDEN:403,NOT_FOUND:404,METHOD_NOT_ALLOWED:405,CONFLICT:409,TOO_MANY_REQUESTS:429,INTERNAL_SERVER_ERROR:500,BAD_GATEWAY:502,SERVICE_UNAVAILABLE:503,GATEWAY_TIMEOUT:504},v="onboarding",y="share";class D extends Error{constructor(e){super(e.message),this.isHttpError=!0,this.name="HttpError",this.code=e.code,this.status=e.status,this.details=e.details,this.displayMessage=e.displayMessage}}function U(t){return typeof t=="object"&&t!==null&&t.isHttpError===!0&&typeof t.status=="number"&&typeof t.message=="string"&&(t.code===void 0||typeof t.code=="string")}var g=(t=>(t.MONTH="month",t.DAY="day",t))(g||{}),C=(t=>(t.UK="uk",t.EN="en",t))(C||{}),_=(t=>(t.IN="in",t.BETWEEN="between",t))(_||{});function b(t,e){const n=t.split("/")[0],r=e.split("/").pop();return`${n}/${r}`}const O=t=>t.messages.flatMap(e=>{const n=e.custom_content?.attachments??[],r=e.custom_content?.stages?.flatMap(({attachments:s})=>s??[])??[];return[...n,...r]}),P=t=>t.endsWith("/"),H=t=>{const e=t.toLowerCase();return e.startsWith("http://")||e.startsWith("https://")},h=t=>!!t&&!H(t)&&!P(t),M=t=>[...new Set(O(t).map(e=>e.url).filter(e=>!!e).filter(e=>h(e)))],p=(t,e)=>t?.map(n=>({...n,url:n.url?e[n.url]??n.url:n.url})),F=(t,e)=>({...t,messages:t.messages.map(n=>({...n,custom_content:n.custom_content?{...n.custom_content,attachments:p(n.custom_content.attachments,e),stages:n.custom_content.stages?.map(r=>({...r,attachments:p(r.attachments,e)}))}:n.custom_content}))}),E=t=>t?.split(/-\d+$/)[0]??"",B=t=>t?.toLowerCase().replace(/[^\p{L}\p{N}]+/gu,"-").replace(/^-+|-+$/g,"").replace(/-/g," ")||"";function L(t,e){return`${t?.[0]}/${e}/${t?.[1]}`}const x=(t,e)=>(Math.round(t*10**e)/10**e).toString(),$=(t,e)=>{const n=e?.decimal||l.decimal,r=e?.decimalSymbol||l.decimalSymbol,s=e?.digitGroupingSymbol??l.digitGroupingSymbol,o=Number(t);if(o==null||Number.isNaN(o)||t===null||t==="")return"";const i=n.replace("0.",""),m=`${n==="0"?Math.trunc(o):x(o,i.length)} `.split(".");return m[0]=m[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g,`$1${s}`),`${m.join(r)}`},w=t=>{if(!t.includes("T")){for(const n of G){const r=c.parse(t,n,new Date);if(c.isValid(r))return r}return null}const e=c.parseISO(t);return c.isValid(e)?e:null},G=["yyyy-MM-dd","MM-dd-yyyy"],V=(t,e,n)=>{const r={};return e?.contentType!==null&&(r[T]=e?.contentType||A),e?.jwt?r.Authorization=`Bearer ${e.jwt}`:t&&(r[a]=t),e?.chatReference&&(r[N]=e.chatReference),{...r,...n}},Y=t=>{const e={...t};return e[a]&&(e[a]=e[a].substring(0,8)+"...[REDACTED]"),e.Authorization&&(e.Authorization="Bearer [REDACTED]"),e[u]&&(e[u]=e[u].substring(0,8)+"...[REDACTED]"),e},W=t=>({"Content-Type":`multipart/form-data; boundary=${t}`}),j=t=>t?.toLowerCase()==="true",z=async(t,e,n)=>await fetch(t,{method:n.method||"GET",headers:e,body:n.body?JSON.stringify(n.body):void 0}),K=(t,e)=>t?.split("/")?.filter(n=>n!==e)?.join("/"),k=t=>t.replace(/^conversations\//,""),X=t=>t.split("/").slice(0,-1).join("/"),q=t=>E(decodeURIComponent(t.split("/").at(-1)??"")),J=t=>t.map(e=>({...e,name:E(e.name)})),Q=new Set([")","]","}",">",",",".","!","?",":",";"]);function I(t){let e=t.length;for(;e>0&&Q.has(t[e-1]);)e--;return e===t.length?{cleanUrl:t,trailing:""}:{cleanUrl:t.slice(0,e),trailing:t.slice(e)}}function Z(t){if(!t)return[{type:"text",value:""}];const e=[];let n=0;for(const r of t.matchAll(/\bhttps?:\/\/[^\s<>"']+/gi)){const s=r[0],o=r.index??0;o>n&&e.push({type:"text",value:t.slice(n,o)});const{cleanUrl:i,trailing:d}=I(s);i?e.push({type:"link",value:i}):e.push({type:"text",value:s}),d&&e.push({type:"text",value:d}),n=o+s.length}return n<t.length&&e.push({type:"text",value:t.slice(n)}),e.length?e:[{type:"text",value:t}]}exports.API_KEY_HEADER=a;exports.API_ROUTES=R;exports.APPLICATION_JSON=A;exports.CONTENT_TYPE_HEADER=T;exports.CUSTOM_PERIOD=f;exports.CalendarResolution=g;exports.HTTP_ERROR_CODES=S;exports.HttpError=D;exports.Locale=C;exports.OCP_APIM_SUBSCRIPTION_KEY_HEADER=u;exports.ONBOARDING_MODEL_POSTFIX=v;exports.QueryFilterType=_;exports.SHARE_CONVERSATION_ROUTE=y;exports.X_CONVERSATION_ID_HEADER=N;exports.cleanConversationNames=J;exports.defaultFormatNumbers=l;exports.deleteConversationNamePostfix=E;exports.formatNumberBySign=$;exports.getClearedConversationName=B;exports.getConversationAttachmentUrls=M;exports.getConversationAttachments=O;exports.getConversationFolderIdFromConversationId=X;exports.getConversationId=L;exports.getConversationIdFromResourceUrl=k;exports.getConversationNameFromConversationId=q;exports.getConversationNavPath=b;exports.getConversationUrlWithoutLocale=K;exports.getHeaders=V;exports.getMultipartHeaders=W;exports.getTimePeriod=w;exports.isDialFileAttachmentUrl=h;exports.isHttpError=U;exports.linkifyText=Z;exports.parseBoolean=j;exports.replaceConversationAttachmentUrls=F;exports.sanitizeHeaders=Y;exports.sendRequest=z;exports.splitTrailingPunctuation=I;
|
package/index.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { parse as
|
|
2
|
-
const
|
|
1
|
+
import { parse as h, isValid as d, parseISO as A } from "date-fns";
|
|
2
|
+
const M = {
|
|
3
3
|
CHAT: "/api/chat"
|
|
4
|
-
},
|
|
4
|
+
}, H = "CUSTOM_PERIOD", u = {
|
|
5
5
|
decimal: "0.0",
|
|
6
6
|
decimalSymbol: ",",
|
|
7
7
|
digitGroupingSymbol: " "
|
|
8
|
-
}, T = "application/json",
|
|
8
|
+
}, T = "application/json", f = "Content-Type", c = "Api-Key", g = "X-CONVERSATION-ID", m = "Ocp-Apim-Subscription-Key", x = {
|
|
9
9
|
BAD_REQUEST: 400,
|
|
10
10
|
UNAUTHORIZED: 401,
|
|
11
11
|
FORBIDDEN: 403,
|
|
@@ -17,64 +17,96 @@ const _ = {
|
|
|
17
17
|
BAD_GATEWAY: 502,
|
|
18
18
|
SERVICE_UNAVAILABLE: 503,
|
|
19
19
|
GATEWAY_TIMEOUT: 504
|
|
20
|
-
},
|
|
21
|
-
class
|
|
20
|
+
}, B = "onboarding", P = "share";
|
|
21
|
+
class L extends Error {
|
|
22
22
|
constructor(e) {
|
|
23
23
|
super(e.message), this.isHttpError = !0, this.name = "HttpError", this.code = e.code, this.status = e.status, this.details = e.details, this.displayMessage = e.displayMessage;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
-
function
|
|
26
|
+
function $(t) {
|
|
27
27
|
return typeof t == "object" && t !== null && t.isHttpError === !0 && typeof t.status == "number" && typeof t.message == "string" && (t.code === void 0 || typeof t.code == "string");
|
|
28
28
|
}
|
|
29
|
-
var
|
|
30
|
-
function
|
|
29
|
+
var N = /* @__PURE__ */ ((t) => (t.MONTH = "month", t.DAY = "day", t))(N || {}), _ = /* @__PURE__ */ ((t) => (t.UK = "uk", t.EN = "en", t))(_ || {}), O = /* @__PURE__ */ ((t) => (t.IN = "in", t.BETWEEN = "between", t))(O || {});
|
|
30
|
+
function w(t, e) {
|
|
31
31
|
const n = t.split("/")[0], r = e.split("/").pop();
|
|
32
32
|
return `${n}/${r}`;
|
|
33
33
|
}
|
|
34
|
-
const
|
|
35
|
-
|
|
34
|
+
const I = (t) => t.messages.flatMap((e) => {
|
|
35
|
+
const n = e.custom_content?.attachments ?? [], r = e.custom_content?.stages?.flatMap(
|
|
36
|
+
({ attachments: s }) => s ?? []
|
|
37
|
+
) ?? [];
|
|
38
|
+
return [...n, ...r];
|
|
39
|
+
}), C = (t) => t.endsWith("/"), y = (t) => {
|
|
40
|
+
const e = t.toLowerCase();
|
|
41
|
+
return e.startsWith("http://") || e.startsWith("https://");
|
|
42
|
+
}, R = (t) => !!t && !y(t) && !C(t), F = (t) => [
|
|
43
|
+
...new Set(
|
|
44
|
+
I(t).map((e) => e.url).filter((e) => !!e).filter((e) => R(e))
|
|
45
|
+
)
|
|
46
|
+
], p = (t, e) => t?.map((n) => ({
|
|
47
|
+
...n,
|
|
48
|
+
url: n.url ? e[n.url] ?? n.url : n.url
|
|
49
|
+
})), G = (t, e) => ({
|
|
50
|
+
...t,
|
|
51
|
+
messages: t.messages.map((n) => ({
|
|
52
|
+
...n,
|
|
53
|
+
custom_content: n.custom_content ? {
|
|
54
|
+
...n.custom_content,
|
|
55
|
+
attachments: p(
|
|
56
|
+
n.custom_content.attachments,
|
|
57
|
+
e
|
|
58
|
+
),
|
|
59
|
+
stages: n.custom_content.stages?.map((r) => ({
|
|
60
|
+
...r,
|
|
61
|
+
attachments: p(
|
|
62
|
+
r.attachments,
|
|
63
|
+
e
|
|
64
|
+
)
|
|
65
|
+
}))
|
|
66
|
+
} : n.custom_content
|
|
67
|
+
}))
|
|
68
|
+
}), E = (t) => t?.split(/-\d+$/)[0] ?? "", W = (t) => t?.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-+|-+$/g, "").replace(/-/g, " ") || "";
|
|
69
|
+
function j(t, e) {
|
|
36
70
|
return `${t?.[0]}/${e}/${t?.[1]}`;
|
|
37
71
|
}
|
|
38
|
-
const
|
|
72
|
+
const S = (t, e) => (Math.round(t * 10 ** e) / 10 ** e).toString(), V = (t, e) => {
|
|
39
73
|
const n = e?.decimal || u.decimal, r = e?.decimalSymbol || u.decimalSymbol, s = e?.digitGroupingSymbol ?? u.digitGroupingSymbol, o = Number(t);
|
|
40
74
|
if (o == null || Number.isNaN(o) || t === null || t === "")
|
|
41
75
|
return "";
|
|
42
|
-
const
|
|
76
|
+
const a = n.replace("0.", ""), l = `${n === "0" ? Math.trunc(o) : S(o, a.length)} `.split(".");
|
|
43
77
|
return l[0] = l[0].replace(
|
|
44
78
|
/(\d)(?=(\d\d\d)+(?!\d))/g,
|
|
45
79
|
`$1${s}`
|
|
46
80
|
), `${l.join(r)}`;
|
|
47
|
-
},
|
|
81
|
+
}, Y = (t) => {
|
|
48
82
|
if (!t.includes("T")) {
|
|
49
|
-
for (const n of
|
|
50
|
-
const r =
|
|
51
|
-
if (
|
|
83
|
+
for (const n of D) {
|
|
84
|
+
const r = h(t, n, /* @__PURE__ */ new Date());
|
|
85
|
+
if (d(r))
|
|
52
86
|
return r;
|
|
53
87
|
}
|
|
54
88
|
return null;
|
|
55
89
|
}
|
|
56
|
-
const e =
|
|
57
|
-
return
|
|
58
|
-
},
|
|
59
|
-
const r = {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return e?.jwt ? r.Authorization = `Bearer ${e.jwt}` : t && (r[a] = t), e?.chatReference && (r[A] = e.chatReference), { ...r, ...n };
|
|
63
|
-
}, F = (t) => {
|
|
90
|
+
const e = A(t);
|
|
91
|
+
return d(e) ? e : null;
|
|
92
|
+
}, D = ["yyyy-MM-dd", "MM-dd-yyyy"], z = (t, e, n) => {
|
|
93
|
+
const r = {};
|
|
94
|
+
return e?.contentType !== null && (r[f] = e?.contentType || T), e?.jwt ? r.Authorization = `Bearer ${e.jwt}` : t && (r[c] = t), e?.chatReference && (r[g] = e.chatReference), { ...r, ...n };
|
|
95
|
+
}, k = (t) => {
|
|
64
96
|
const e = { ...t };
|
|
65
|
-
return e[
|
|
66
|
-
},
|
|
97
|
+
return e[c] && (e[c] = e[c].substring(0, 8) + "...[REDACTED]"), e.Authorization && (e.Authorization = "Bearer [REDACTED]"), e[m] && (e[m] = e[m].substring(0, 8) + "...[REDACTED]"), e;
|
|
98
|
+
}, K = (t) => ({
|
|
67
99
|
"Content-Type": `multipart/form-data; boundary=${t}`
|
|
68
|
-
}),
|
|
100
|
+
}), X = (t) => t?.toLowerCase() === "true", J = async (t, e, n) => await fetch(t, {
|
|
69
101
|
method: n.method || "GET",
|
|
70
102
|
headers: e,
|
|
71
103
|
body: n.body ? JSON.stringify(n.body) : void 0
|
|
72
|
-
}),
|
|
104
|
+
}), q = (t, e) => t?.split("/")?.filter((n) => n !== e)?.join("/"), Q = (t) => t.replace(/^conversations\//, ""), Z = (t) => t.split("/").slice(0, -1).join("/"), tt = (t) => E(
|
|
73
105
|
decodeURIComponent(t.split("/").at(-1) ?? "")
|
|
74
|
-
),
|
|
106
|
+
), et = (t) => t.map((e) => ({
|
|
75
107
|
...e,
|
|
76
|
-
name:
|
|
77
|
-
})),
|
|
108
|
+
name: E(e.name)
|
|
109
|
+
})), b = /* @__PURE__ */ new Set([
|
|
78
110
|
")",
|
|
79
111
|
"]",
|
|
80
112
|
"}",
|
|
@@ -86,60 +118,64 @@ const I = (t, e) => (Math.round(t * 10 ** e) / 10 ** e).toString(), B = (t, e) =
|
|
|
86
118
|
":",
|
|
87
119
|
";"
|
|
88
120
|
]);
|
|
89
|
-
function
|
|
121
|
+
function v(t) {
|
|
90
122
|
let e = t.length;
|
|
91
|
-
for (; e > 0 &&
|
|
123
|
+
for (; e > 0 && b.has(t[e - 1]); )
|
|
92
124
|
e--;
|
|
93
125
|
return e === t.length ? { cleanUrl: t, trailing: "" } : {
|
|
94
126
|
cleanUrl: t.slice(0, e),
|
|
95
127
|
trailing: t.slice(e)
|
|
96
128
|
};
|
|
97
129
|
}
|
|
98
|
-
function
|
|
130
|
+
function nt(t) {
|
|
99
131
|
if (!t) return [{ type: "text", value: "" }];
|
|
100
132
|
const e = [];
|
|
101
133
|
let n = 0;
|
|
102
134
|
for (const r of t.matchAll(/\bhttps?:\/\/[^\s<>"']+/gi)) {
|
|
103
135
|
const s = r[0], o = r.index ?? 0;
|
|
104
136
|
o > n && e.push({ type: "text", value: t.slice(n, o) });
|
|
105
|
-
const { cleanUrl:
|
|
106
|
-
|
|
137
|
+
const { cleanUrl: a, trailing: i } = v(s);
|
|
138
|
+
a ? e.push({ type: "link", value: a }) : e.push({ type: "text", value: s }), i && e.push({ type: "text", value: i }), n = o + s.length;
|
|
107
139
|
}
|
|
108
140
|
return n < t.length && e.push({ type: "text", value: t.slice(n) }), e.length ? e : [{ type: "text", value: t }];
|
|
109
141
|
}
|
|
110
142
|
export {
|
|
111
|
-
|
|
112
|
-
|
|
143
|
+
c as API_KEY_HEADER,
|
|
144
|
+
M as API_ROUTES,
|
|
113
145
|
T as APPLICATION_JSON,
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
146
|
+
f as CONTENT_TYPE_HEADER,
|
|
147
|
+
H as CUSTOM_PERIOD,
|
|
148
|
+
N as CalendarResolution,
|
|
149
|
+
x as HTTP_ERROR_CODES,
|
|
150
|
+
L as HttpError,
|
|
151
|
+
_ as Locale,
|
|
152
|
+
m as OCP_APIM_SUBSCRIPTION_KEY_HEADER,
|
|
153
|
+
B as ONBOARDING_MODEL_POSTFIX,
|
|
154
|
+
O as QueryFilterType,
|
|
155
|
+
P as SHARE_CONVERSATION_ROUTE,
|
|
156
|
+
g as X_CONVERSATION_ID_HEADER,
|
|
157
|
+
et as cleanConversationNames,
|
|
126
158
|
u as defaultFormatNumbers,
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
w as
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
159
|
+
E as deleteConversationNamePostfix,
|
|
160
|
+
V as formatNumberBySign,
|
|
161
|
+
W as getClearedConversationName,
|
|
162
|
+
F as getConversationAttachmentUrls,
|
|
163
|
+
I as getConversationAttachments,
|
|
164
|
+
Z as getConversationFolderIdFromConversationId,
|
|
165
|
+
j as getConversationId,
|
|
166
|
+
Q as getConversationIdFromResourceUrl,
|
|
167
|
+
tt as getConversationNameFromConversationId,
|
|
168
|
+
w as getConversationNavPath,
|
|
169
|
+
q as getConversationUrlWithoutLocale,
|
|
170
|
+
z as getHeaders,
|
|
171
|
+
K as getMultipartHeaders,
|
|
172
|
+
Y as getTimePeriod,
|
|
173
|
+
R as isDialFileAttachmentUrl,
|
|
174
|
+
$ as isHttpError,
|
|
175
|
+
nt as linkifyText,
|
|
176
|
+
X as parseBoolean,
|
|
177
|
+
G as replaceConversationAttachmentUrls,
|
|
178
|
+
k as sanitizeHeaders,
|
|
179
|
+
J as sendRequest,
|
|
180
|
+
v as splitTrailingPunctuation
|
|
145
181
|
};
|
package/package.json
CHANGED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Attachment, Conversation } from '@epam/ai-dial-shared';
|
|
2
|
+
export declare const getConversationAttachments: (conversation: Conversation) => Attachment[];
|
|
3
|
+
export declare const isDialFileAttachmentUrl: (url: string) => boolean;
|
|
4
|
+
export declare const getConversationAttachmentUrls: (conversation: Conversation) => string[];
|
|
5
|
+
export declare const replaceConversationAttachmentUrls: (conversation: Conversation, attachmentUrlMap: Record<string, string>) => Conversation;
|
package/utils/headers.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { ApiHeaders } from '../models/api';
|
|
|
2
2
|
export declare const getHeaders: (apiKey?: string, options?: {
|
|
3
3
|
jwt?: string;
|
|
4
4
|
chatReference?: string;
|
|
5
|
-
contentType?: string;
|
|
5
|
+
contentType?: string | null;
|
|
6
6
|
}, optionsHeaders?: Record<string, string>) => Record<string, string>;
|
|
7
7
|
export declare const sanitizeHeaders: (headers: ApiHeaders) => Record<string, string>;
|
|
8
8
|
export declare const getMultipartHeaders: (boundary: string) => Record<string, string>;
|