@epam/statgpt-dial-toolkit 0.1.0-rc.0 → 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/api/chat-streaming-api.d.ts +2 -2
- package/api/conversation-api.d.ts +13 -1
- package/constants/api-urls.d.ts +2 -0
- package/index.cjs +3 -2
- package/index.d.ts +1 -0
- package/index.mjs +249 -183
- package/models/chat-stream.d.ts +8 -0
- package/models/conversation.d.ts +2 -0
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Message } from '../models/message';
|
|
2
|
-
import { MessageStreamResponse, RequestStreamBody } from '../models/chat-stream';
|
|
2
|
+
import { CustomFields, MessageStreamResponse, RequestStreamBody } from '../models/chat-stream';
|
|
3
3
|
import { ModelInfo } from '../models/model';
|
|
4
4
|
interface SSEOptions {
|
|
5
5
|
signal?: AbortSignal;
|
|
@@ -23,5 +23,5 @@ export declare const streamChatResponse: (conversationId: string, messages: Mess
|
|
|
23
23
|
onError?: (error: Error) => void;
|
|
24
24
|
model: ModelInfo;
|
|
25
25
|
signal?: AbortSignal;
|
|
26
|
-
}, token?: string | null) => Promise<void>;
|
|
26
|
+
}, token?: string | null, custom_fields?: CustomFields) => Promise<void>;
|
|
27
27
|
export {};
|
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
import { Conversation, ConversationInfo } from '@epam/ai-dial-shared';
|
|
1
|
+
import { Conversation, Entity as DialEntity, ConversationInfo } from '@epam/ai-dial-shared';
|
|
2
2
|
import { Message } from '../models/message';
|
|
3
3
|
import { DialApiClient } from './dial-api-client';
|
|
4
4
|
import { ConversationData, CreateConversationRequest, GeneratedLinkResponse, SharedConversationsRequest, SharedConversations, UpdateConversationRequest } from '../models/conversation';
|
|
5
5
|
import { ModelInfo } from '../models/model';
|
|
6
6
|
import { GridAttachmentContent } from '../models/grid-attachment';
|
|
7
|
+
import { CustomFields } from '../models/chat-stream';
|
|
8
|
+
import { OnboardingFileSchema } from '../../../shared-toolkit/src/models/onboarding-schema';
|
|
9
|
+
interface Entity extends DialEntity {
|
|
10
|
+
url?: string;
|
|
11
|
+
parentPath: string;
|
|
12
|
+
}
|
|
7
13
|
export declare class ConversationApi {
|
|
8
14
|
private client;
|
|
9
15
|
constructor(client: DialApiClient);
|
|
10
16
|
getConversations(token: string, bucket?: string, locale?: string): Promise<ConversationInfo[]>;
|
|
11
17
|
getConversation(id: string, token: string): Promise<Conversation | null>;
|
|
12
18
|
getFile(filePath: string, token: string): Promise<GridAttachmentContent | null>;
|
|
19
|
+
putOnboardingFile(fileName: string, filePath: string, fileData: OnboardingFileSchema, token: string): Promise<Entity | null>;
|
|
20
|
+
getOnboardingFile(filePath: string, token: string): Promise<OnboardingFileSchema | null>;
|
|
13
21
|
getFileBlob(filePath: string, token: string): Promise<Blob | null>;
|
|
14
22
|
createConversation(data: CreateConversationRequest, token: string): Promise<ConversationInfo>;
|
|
15
23
|
generateConversationLink(token: string, conversationData?: ConversationData): Promise<GeneratedLinkResponse>;
|
|
@@ -21,5 +29,9 @@ export declare class ConversationApi {
|
|
|
21
29
|
conversationId: string;
|
|
22
30
|
messages: Message[];
|
|
23
31
|
model: ModelInfo;
|
|
32
|
+
custom_fields?: CustomFields;
|
|
24
33
|
}, token: string): Promise<ReadableStream>;
|
|
34
|
+
rateResponse(responseId: string, rate: boolean, token: string): Promise<void>;
|
|
35
|
+
renameConversation(sourceUrl: string, destinationUrl: string, token: string): Promise<void>;
|
|
25
36
|
}
|
|
37
|
+
export {};
|
package/constants/api-urls.d.ts
CHANGED
|
@@ -12,4 +12,6 @@ export declare const DIAL_API_ROUTES: {
|
|
|
12
12
|
readonly SHARE_CONVERSATION_LIST: "/v1/ops/resource/share/list";
|
|
13
13
|
readonly SHARE_CONVERSATION_DISCARD: "/v1/ops/resource/share/discard";
|
|
14
14
|
readonly SHARE_CONVERSATION_REVOKE: "/v1/ops/resource/share/revoke";
|
|
15
|
+
readonly RATE: "/v1/rate";
|
|
16
|
+
readonly RENAME: "/v1/ops/resource/move";
|
|
15
17
|
};
|
package/index.cjs
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
`);e=
|
|
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
package/index.mjs
CHANGED
|
@@ -1,98 +1,103 @@
|
|
|
1
|
-
const
|
|
1
|
+
const E = "/v1/ops/resource/share", u = {
|
|
2
2
|
VERSION: "/v1",
|
|
3
3
|
BUCKET: "/v1/bucket",
|
|
4
4
|
CONVERSATIONS: "/v1/metadata/conversations",
|
|
5
|
-
CONVERSATION_BY_ID: (
|
|
6
|
-
CHAT: (
|
|
5
|
+
CONVERSATION_BY_ID: (s) => `/v1/metadata/conversations/${s}`,
|
|
6
|
+
CHAT: (s) => `/openai/deployments/${s}/chat/completions`,
|
|
7
7
|
MODELS: "/openai/models",
|
|
8
|
-
CONFIGURATION: (
|
|
9
|
-
SHARE_CONVERSATION: `${
|
|
10
|
-
SHARE_CONVERSATION_ACCEPT: (
|
|
11
|
-
SHARE_CONVERSATION_DETAILS: (
|
|
12
|
-
SHARE_CONVERSATION_LIST: `${
|
|
13
|
-
SHARE_CONVERSATION_DISCARD: `${
|
|
14
|
-
SHARE_CONVERSATION_REVOKE: `${
|
|
8
|
+
CONFIGURATION: (s) => `/v1/deployments/${s}/configuration`,
|
|
9
|
+
SHARE_CONVERSATION: `${E}/create`,
|
|
10
|
+
SHARE_CONVERSATION_ACCEPT: (s) => `/v1/invitations/${s}?accept=true`,
|
|
11
|
+
SHARE_CONVERSATION_DETAILS: (s) => `/v1/invitations/${s}`,
|
|
12
|
+
SHARE_CONVERSATION_LIST: `${E}/list`,
|
|
13
|
+
SHARE_CONVERSATION_DISCARD: `${E}/discard`,
|
|
14
|
+
SHARE_CONVERSATION_REVOKE: `${E}/revoke`,
|
|
15
|
+
RATE: "/v1/rate",
|
|
16
|
+
RENAME: "/v1/ops/resource/move"
|
|
15
17
|
};
|
|
16
|
-
var _ = /* @__PURE__ */ ((
|
|
17
|
-
const
|
|
18
|
-
var t,
|
|
19
|
-
return
|
|
20
|
-
`Using complete message format: ${
|
|
21
|
-
),
|
|
22
|
-
},
|
|
23
|
-
if (t == null || t(
|
|
24
|
-
const e =
|
|
25
|
-
e &&
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
const
|
|
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
|
+
const $ = (s) => {
|
|
20
|
+
var t, r, e, n, o, c;
|
|
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
|
+
`Using complete message format: ${s.choices[0].message.content}`
|
|
23
|
+
), s.choices[0].message.content) : (console.info("Unknown SSE data format:", s), null);
|
|
24
|
+
}, T = (s, t, r) => {
|
|
25
|
+
if (t == null || t(s), r) {
|
|
26
|
+
const e = $(s);
|
|
27
|
+
e && r(e);
|
|
28
|
+
}
|
|
29
|
+
}, p = (s, t) => s.error || s.message || `${t.status} ${t.statusText}`, g = (s, t) => {
|
|
30
|
+
const r = s == null ? void 0 : s.reduce(
|
|
29
31
|
(e, n) => (e[n.index] = n, e),
|
|
30
32
|
{}
|
|
31
33
|
);
|
|
32
34
|
return t.forEach((e) => {
|
|
33
|
-
|
|
34
|
-
}), Object.values(
|
|
35
|
-
},
|
|
36
|
-
const
|
|
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
|
+
}), Object.values(r);
|
|
37
|
+
}, j = (s, t) => {
|
|
38
|
+
const r = structuredClone(s);
|
|
37
39
|
return t.forEach((e) => {
|
|
38
|
-
e.errorMessage && (
|
|
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(
|
|
39
41
|
e.custom_content.attachments
|
|
40
|
-
)), e.custom_content.stages && (
|
|
41
|
-
|
|
42
|
+
)), e.custom_content.stages && (r.custom_content.stages || (r.custom_content.stages = []), r.custom_content.stages = g(
|
|
43
|
+
r.custom_content.stages,
|
|
42
44
|
e.custom_content.stages
|
|
43
|
-
)), e.custom_content.state && (
|
|
44
|
-
}),
|
|
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
|
+
}), r;
|
|
47
|
+
}, w = (s) => (s == null ? void 0 : s.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-+|-+$/g, "").replace(/-/g, " ")) || "", q = (s) => {
|
|
46
48
|
var n;
|
|
47
|
-
const t = ((n =
|
|
48
|
-
return { modelId: t[0], conversationName:
|
|
49
|
-
},
|
|
50
|
-
const t = Date.now(),
|
|
51
|
-
return `${
|
|
52
|
-
},
|
|
53
|
-
method:
|
|
49
|
+
const t = ((n = s.name) == null ? void 0 : n.split("__")) || [], r = t.length > 1 ? t.slice(1).join("__") : s.name;
|
|
50
|
+
return { modelId: t[0], conversationName: r };
|
|
51
|
+
}, b = (s) => {
|
|
52
|
+
const t = Date.now(), r = w(s.name);
|
|
53
|
+
return `${s.folderId}/${r}-${t}`;
|
|
54
|
+
}, R = async (s, t, r) => await fetch(s, {
|
|
55
|
+
method: r.method || "GET",
|
|
54
56
|
headers: t,
|
|
55
|
-
body:
|
|
57
|
+
body: r != null && r.isFormData && typeof (r == null ? void 0 : r.body) == "string" ? r.body : JSON.stringify(r.body),
|
|
58
|
+
signal: r == null ? void 0 : r.signal
|
|
56
59
|
});
|
|
57
|
-
function
|
|
58
|
-
return
|
|
60
|
+
function h(s) {
|
|
61
|
+
return s.split("/").map((t) => encodeURIComponent(t)).join("/");
|
|
59
62
|
}
|
|
60
|
-
function
|
|
61
|
-
return
|
|
63
|
+
function k(s) {
|
|
64
|
+
return s.split("/").map((t) => decodeURIComponent(t)).join("/");
|
|
62
65
|
}
|
|
63
|
-
const
|
|
66
|
+
const V = {
|
|
64
67
|
CHAT: "/api/chat"
|
|
65
|
-
},
|
|
68
|
+
}, v = "application/json", x = "Content-Type", S = "Api-Key", P = "X-CONVERSATION-ID", O = (s, t, r) => {
|
|
66
69
|
const e = {
|
|
67
|
-
[x]: (t == null ? void 0 : t.contentType) ||
|
|
70
|
+
[x]: (t == null ? void 0 : t.contentType) || v
|
|
68
71
|
};
|
|
69
|
-
return t != null && t.jwt ? e.Authorization = `Bearer ${t.jwt}` :
|
|
70
|
-
},
|
|
71
|
-
const t = { ...
|
|
72
|
-
return t[
|
|
73
|
-
}
|
|
74
|
-
|
|
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
|
+
const t = { ...s };
|
|
75
|
+
return t[S] && (t[S] = t[S].substring(0, 8) + "...[REDACTED]"), t.Authorization && (t.Authorization = "Bearer [REDACTED]"), t;
|
|
76
|
+
}, L = (s) => ({
|
|
77
|
+
"Content-Type": `multipart/form-data; boundary=${s}`
|
|
78
|
+
});
|
|
79
|
+
class D {
|
|
75
80
|
constructor() {
|
|
76
81
|
this.decoder = new TextDecoder();
|
|
77
82
|
}
|
|
78
|
-
async streamChat(t,
|
|
79
|
-
const { onMessage: o, onError: c, onComplete: a, signal:
|
|
83
|
+
async streamChat(t, r, e = {}, n) {
|
|
84
|
+
const { onMessage: o, onError: c, onComplete: a, signal: d } = e;
|
|
80
85
|
try {
|
|
81
|
-
const
|
|
86
|
+
const i = await this.initializeStreamRequest(
|
|
82
87
|
t,
|
|
83
|
-
|
|
84
|
-
|
|
88
|
+
r,
|
|
89
|
+
d,
|
|
85
90
|
n
|
|
86
91
|
);
|
|
87
|
-
await this.processStreamData(
|
|
88
|
-
} catch (
|
|
89
|
-
this.handleStreamError(
|
|
92
|
+
await this.processStreamData(i, o), a == null || a();
|
|
93
|
+
} catch (i) {
|
|
94
|
+
this.handleStreamError(i, c);
|
|
90
95
|
}
|
|
91
96
|
}
|
|
92
|
-
async initializeStreamRequest(t,
|
|
93
|
-
const o =
|
|
97
|
+
async initializeStreamRequest(t, r, e, n) {
|
|
98
|
+
const o = O(void 0, {
|
|
94
99
|
jwt: n
|
|
95
|
-
}), c = await
|
|
100
|
+
}), c = await R(
|
|
96
101
|
t,
|
|
97
102
|
{
|
|
98
103
|
Accept: "text/event-stream",
|
|
@@ -100,7 +105,8 @@ class b {
|
|
|
100
105
|
},
|
|
101
106
|
{
|
|
102
107
|
method: "POST",
|
|
103
|
-
body:
|
|
108
|
+
body: r,
|
|
109
|
+
signal: e
|
|
104
110
|
}
|
|
105
111
|
);
|
|
106
112
|
if (!c.ok) {
|
|
@@ -111,13 +117,13 @@ class b {
|
|
|
111
117
|
throw new Error("No response body");
|
|
112
118
|
return c.body.getReader();
|
|
113
119
|
}
|
|
114
|
-
async processStreamData(t,
|
|
120
|
+
async processStreamData(t, r) {
|
|
115
121
|
let e = "";
|
|
116
122
|
try {
|
|
117
123
|
for (; ; ) {
|
|
118
124
|
const { done: n, value: o } = await t.read();
|
|
119
125
|
if (n) {
|
|
120
|
-
e.trim() && this.parseSSEDataLine(e,
|
|
126
|
+
e.trim() && this.parseSSEDataLine(e, r);
|
|
121
127
|
break;
|
|
122
128
|
}
|
|
123
129
|
const c = this.decoder.decode(o, { stream: !0 });
|
|
@@ -125,18 +131,18 @@ class b {
|
|
|
125
131
|
const a = e.split(`
|
|
126
132
|
`);
|
|
127
133
|
e = a.pop() || "";
|
|
128
|
-
for (const
|
|
129
|
-
this.parseSSEDataLine(
|
|
134
|
+
for (const d of a)
|
|
135
|
+
this.parseSSEDataLine(d, r);
|
|
130
136
|
}
|
|
131
137
|
} finally {
|
|
132
138
|
t.releaseLock();
|
|
133
139
|
}
|
|
134
140
|
}
|
|
135
|
-
handleStreamError(t,
|
|
141
|
+
handleStreamError(t, r) {
|
|
136
142
|
const e = t instanceof Error ? t : new Error(String(t));
|
|
137
|
-
throw
|
|
143
|
+
throw r == null || r(e), e;
|
|
138
144
|
}
|
|
139
|
-
parseSSEDataLine(t,
|
|
145
|
+
parseSSEDataLine(t, r) {
|
|
140
146
|
const e = t.trim();
|
|
141
147
|
if (!(!e || e.startsWith(":")) && e.startsWith("data: ")) {
|
|
142
148
|
const n = e.slice(6);
|
|
@@ -146,86 +152,125 @@ class b {
|
|
|
146
152
|
}
|
|
147
153
|
try {
|
|
148
154
|
const o = JSON.parse(n);
|
|
149
|
-
console.info(`SSE: Parsed data: ${o}`),
|
|
155
|
+
console.info(`SSE: Parsed data: ${o}`), r == null || r(o);
|
|
150
156
|
} catch (o) {
|
|
151
157
|
console.error(`Failed to parse SSE data: ${n} ${o}`);
|
|
152
158
|
}
|
|
153
159
|
}
|
|
154
160
|
}
|
|
155
161
|
}
|
|
156
|
-
const
|
|
157
|
-
const { onMessage:
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
+
conversationId: s,
|
|
165
|
+
messages: t,
|
|
166
|
+
model: i,
|
|
167
|
+
custom_fields: n
|
|
168
|
+
};
|
|
169
|
+
await U.streamChat(
|
|
170
|
+
V.CHAT,
|
|
171
|
+
A,
|
|
161
172
|
{
|
|
162
|
-
onMessage: (
|
|
163
|
-
onComplete:
|
|
164
|
-
onError:
|
|
165
|
-
signal:
|
|
173
|
+
onMessage: (I) => T(I, o, c),
|
|
174
|
+
onComplete: a,
|
|
175
|
+
onError: d,
|
|
176
|
+
signal: l
|
|
166
177
|
},
|
|
167
178
|
e
|
|
168
179
|
);
|
|
169
|
-
},
|
|
170
|
-
class
|
|
180
|
+
}, m = (s) => s instanceof Error && s.message.includes("404"), f = (s) => `/v1/conversations/${h(s)}`;
|
|
181
|
+
class K {
|
|
171
182
|
constructor(t) {
|
|
172
183
|
this.client = t;
|
|
173
184
|
}
|
|
174
|
-
async getConversations(t,
|
|
175
|
-
const n = `${
|
|
185
|
+
async getConversations(t, r, e) {
|
|
186
|
+
const n = `${r ? e ? `${r}/${e}` : `${r}` : ""}`, o = `${u.CONVERSATIONS}/${n}`;
|
|
176
187
|
try {
|
|
177
188
|
return (await this.client.getRequest(o + "/?limit=1000&recursive=false", t).then((a) => a.items || [])).map((a) => {
|
|
178
|
-
var
|
|
179
|
-
const { conversationName:
|
|
189
|
+
var l;
|
|
190
|
+
const { conversationName: d, modelId: i } = q(a);
|
|
180
191
|
return {
|
|
181
|
-
id: ((
|
|
182
|
-
name:
|
|
192
|
+
id: ((l = a.url) == null ? void 0 : l.replace("conversations/", "")) || a.name,
|
|
193
|
+
name: d,
|
|
183
194
|
folderId: n,
|
|
184
195
|
createdAt: a.createdAt,
|
|
185
196
|
updatedAt: a.updatedAt,
|
|
186
|
-
model: { id:
|
|
197
|
+
model: { id: i, name: i }
|
|
187
198
|
};
|
|
188
199
|
});
|
|
189
200
|
} catch (c) {
|
|
190
|
-
if (
|
|
201
|
+
if (m(c))
|
|
191
202
|
return [];
|
|
192
203
|
throw c;
|
|
193
204
|
}
|
|
194
205
|
}
|
|
195
|
-
async getConversation(t,
|
|
206
|
+
async getConversation(t, r) {
|
|
196
207
|
try {
|
|
197
208
|
return await this.client.getRequest(
|
|
198
|
-
|
|
199
|
-
|
|
209
|
+
f(t),
|
|
210
|
+
r
|
|
200
211
|
);
|
|
201
212
|
} catch (e) {
|
|
202
|
-
if (
|
|
213
|
+
if (m(e))
|
|
214
|
+
return null;
|
|
215
|
+
throw e;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
async getFile(t, r) {
|
|
219
|
+
try {
|
|
220
|
+
const e = `${u.VERSION}/${h(t)}`;
|
|
221
|
+
return await this.client.getRequest(e, r);
|
|
222
|
+
} catch (e) {
|
|
223
|
+
if (m(e))
|
|
203
224
|
return null;
|
|
204
225
|
throw e;
|
|
205
226
|
}
|
|
206
227
|
}
|
|
207
|
-
async
|
|
228
|
+
async putOnboardingFile(t, r, e, n) {
|
|
229
|
+
try {
|
|
230
|
+
const o = `${u.VERSION}/${h(r)}`, c = "----NodeMultipartBoundary", d = [
|
|
231
|
+
`--${c}`,
|
|
232
|
+
`Content-Disposition: form-data; name="file"; filename="${t}"`,
|
|
233
|
+
"Content-Type: application/json",
|
|
234
|
+
"",
|
|
235
|
+
JSON.stringify(e),
|
|
236
|
+
`--${c}--`,
|
|
237
|
+
""
|
|
238
|
+
].join(`\r
|
|
239
|
+
`);
|
|
240
|
+
return await this.client.request(o, n, {
|
|
241
|
+
method: "PUT",
|
|
242
|
+
body: d,
|
|
243
|
+
headers: L(c),
|
|
244
|
+
isFormData: !0
|
|
245
|
+
});
|
|
246
|
+
} catch (o) {
|
|
247
|
+
if (m(o))
|
|
248
|
+
return null;
|
|
249
|
+
throw o;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
async getOnboardingFile(t, r) {
|
|
208
253
|
try {
|
|
209
|
-
const e = `${
|
|
210
|
-
return await this.client.getRequest(e,
|
|
254
|
+
const e = `${u.VERSION}/${h(t)}`;
|
|
255
|
+
return await this.client.getRequest(e, r);
|
|
211
256
|
} catch (e) {
|
|
212
|
-
if (
|
|
257
|
+
if (m(e))
|
|
213
258
|
return null;
|
|
214
259
|
throw e;
|
|
215
260
|
}
|
|
216
261
|
}
|
|
217
|
-
async getFileBlob(t,
|
|
262
|
+
async getFileBlob(t, r) {
|
|
218
263
|
try {
|
|
219
|
-
const e = `${
|
|
220
|
-
return await this.client.requestBlob(e,
|
|
264
|
+
const e = `${u.VERSION}/${h(t)}`;
|
|
265
|
+
return await this.client.requestBlob(e, r, { method: "GET" });
|
|
221
266
|
} catch (e) {
|
|
222
|
-
if (
|
|
267
|
+
if (m(e))
|
|
223
268
|
return null;
|
|
224
269
|
throw e;
|
|
225
270
|
}
|
|
226
271
|
}
|
|
227
|
-
async createConversation(t,
|
|
228
|
-
const e = (t == null ? void 0 : t.id) ||
|
|
272
|
+
async createConversation(t, r) {
|
|
273
|
+
const e = (t == null ? void 0 : t.id) || b(t), { name: n, folderId: o, model: c, messages: a, custom_fields: d } = t, i = {
|
|
229
274
|
id: e,
|
|
230
275
|
name: n,
|
|
231
276
|
folderId: o,
|
|
@@ -235,11 +280,12 @@ class U {
|
|
|
235
280
|
prompt: t.prompt || "",
|
|
236
281
|
temperature: t.temperature || 0.7,
|
|
237
282
|
createdAt: Date.now(),
|
|
238
|
-
updatedAt: Date.now()
|
|
283
|
+
updatedAt: Date.now(),
|
|
284
|
+
custom_fields: d
|
|
239
285
|
};
|
|
240
286
|
return await this.client.request(
|
|
241
|
-
|
|
242
|
-
|
|
287
|
+
f(e),
|
|
288
|
+
r,
|
|
243
289
|
{
|
|
244
290
|
method: "PUT",
|
|
245
291
|
body: i
|
|
@@ -253,44 +299,44 @@ class U {
|
|
|
253
299
|
updatedAt: i.updatedAt
|
|
254
300
|
};
|
|
255
301
|
}
|
|
256
|
-
async generateConversationLink(t,
|
|
302
|
+
async generateConversationLink(t, r) {
|
|
257
303
|
return await this.client.postRequest(
|
|
258
|
-
|
|
304
|
+
u.SHARE_CONVERSATION,
|
|
259
305
|
t,
|
|
260
306
|
{
|
|
261
|
-
body:
|
|
307
|
+
body: r
|
|
262
308
|
}
|
|
263
309
|
);
|
|
264
310
|
}
|
|
265
|
-
async getSharedConversations(t,
|
|
311
|
+
async getSharedConversations(t, r) {
|
|
266
312
|
return await this.client.postRequest(
|
|
267
|
-
|
|
313
|
+
u.SHARE_CONVERSATION_LIST,
|
|
268
314
|
t,
|
|
269
315
|
{
|
|
270
|
-
body:
|
|
316
|
+
body: r
|
|
271
317
|
}
|
|
272
318
|
);
|
|
273
319
|
}
|
|
274
|
-
async revokeSharedConversations(t,
|
|
320
|
+
async revokeSharedConversations(t, r) {
|
|
275
321
|
await this.client.postRequest(
|
|
276
|
-
|
|
322
|
+
u.SHARE_CONVERSATION_REVOKE,
|
|
277
323
|
t,
|
|
278
324
|
{
|
|
279
|
-
body:
|
|
325
|
+
body: r
|
|
280
326
|
}
|
|
281
327
|
);
|
|
282
328
|
}
|
|
283
|
-
async updateConversation(t,
|
|
329
|
+
async updateConversation(t, r, e) {
|
|
284
330
|
const n = await this.getConversation(t, e);
|
|
285
331
|
if (!n)
|
|
286
332
|
throw new Error(`Conversation with id ${t} not found`);
|
|
287
333
|
const o = {
|
|
288
334
|
...n,
|
|
289
|
-
...
|
|
335
|
+
...r,
|
|
290
336
|
updatedAt: Date.now()
|
|
291
337
|
};
|
|
292
338
|
return await this.client.request(
|
|
293
|
-
|
|
339
|
+
f(t),
|
|
294
340
|
e,
|
|
295
341
|
{
|
|
296
342
|
method: "PUT",
|
|
@@ -298,10 +344,10 @@ class U {
|
|
|
298
344
|
}
|
|
299
345
|
);
|
|
300
346
|
}
|
|
301
|
-
async deleteConversation(t,
|
|
347
|
+
async deleteConversation(t, r) {
|
|
302
348
|
t != null && t.isShared ? await this.client.postRequest(
|
|
303
|
-
|
|
304
|
-
|
|
349
|
+
u.SHARE_CONVERSATION_DISCARD,
|
|
350
|
+
r,
|
|
305
351
|
{
|
|
306
352
|
body: {
|
|
307
353
|
resources: [
|
|
@@ -312,28 +358,46 @@ class U {
|
|
|
312
358
|
}
|
|
313
359
|
}
|
|
314
360
|
) : await this.client.request(
|
|
315
|
-
|
|
316
|
-
|
|
361
|
+
f(decodeURI(t == null ? void 0 : t.id)),
|
|
362
|
+
r,
|
|
317
363
|
{
|
|
318
364
|
method: "DELETE"
|
|
319
365
|
}
|
|
320
366
|
);
|
|
321
367
|
}
|
|
322
|
-
async streamChat(t,
|
|
323
|
-
const e = t.model.id, n = encodeURIComponent(e), o = `${
|
|
368
|
+
async streamChat(t, r) {
|
|
369
|
+
const e = t.model.id, n = encodeURIComponent(e), o = `${u.CHAT(n)}?api-version=${this.client.config.version}`, c = {
|
|
324
370
|
messages: t.messages,
|
|
325
371
|
stream: !0,
|
|
326
372
|
temperature: 0.7,
|
|
327
|
-
max_tokens: 4096
|
|
373
|
+
max_tokens: 4096,
|
|
374
|
+
custom_fields: t.custom_fields
|
|
328
375
|
};
|
|
329
|
-
return await this.client.stream(o,
|
|
376
|
+
return await this.client.stream(o, r, {
|
|
330
377
|
method: "POST",
|
|
331
378
|
body: c,
|
|
332
379
|
chatReference: t.conversationId
|
|
333
380
|
});
|
|
334
381
|
}
|
|
382
|
+
async rateResponse(t, r, e) {
|
|
383
|
+
return await this.client.postRequest(u.RATE, e, {
|
|
384
|
+
body: {
|
|
385
|
+
responseId: t,
|
|
386
|
+
rate: r
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
async renameConversation(t, r, e) {
|
|
391
|
+
return await this.client.postRequest(u.RENAME, e, {
|
|
392
|
+
body: {
|
|
393
|
+
sourceUrl: t,
|
|
394
|
+
destinationUrl: r,
|
|
395
|
+
overwrite: !0
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
}
|
|
335
399
|
}
|
|
336
|
-
class
|
|
400
|
+
class B {
|
|
337
401
|
constructor(t) {
|
|
338
402
|
this.config = t, console.info("DialApiClient initialized", {
|
|
339
403
|
host: t.host || "NOT SET",
|
|
@@ -341,22 +405,22 @@ class j {
|
|
|
341
405
|
version: t.version
|
|
342
406
|
});
|
|
343
407
|
}
|
|
344
|
-
async getRequest(t,
|
|
345
|
-
return this.request(t,
|
|
408
|
+
async getRequest(t, r, e) {
|
|
409
|
+
return this.request(t, r, { ...e, method: "GET" });
|
|
346
410
|
}
|
|
347
|
-
async postRequest(t,
|
|
348
|
-
return this.request(t,
|
|
411
|
+
async postRequest(t, r, e) {
|
|
412
|
+
return this.request(t, r, { ...e, method: "POST" });
|
|
349
413
|
}
|
|
350
|
-
async requestBlob(t,
|
|
414
|
+
async requestBlob(t, r, e) {
|
|
351
415
|
const n = `${this.config.host}${t}`, o = {
|
|
352
|
-
...
|
|
353
|
-
jwt:
|
|
416
|
+
...O(this.config.apiKey, {
|
|
417
|
+
jwt: r,
|
|
354
418
|
chatReference: e.chatReference
|
|
355
419
|
}),
|
|
356
420
|
...e.headers
|
|
357
421
|
};
|
|
358
422
|
try {
|
|
359
|
-
return (await
|
|
423
|
+
return (await R(n, o, e)).blob();
|
|
360
424
|
} catch (c) {
|
|
361
425
|
throw console.error("API Request Exception", {
|
|
362
426
|
method: e.method,
|
|
@@ -365,61 +429,61 @@ class j {
|
|
|
365
429
|
}), c;
|
|
366
430
|
}
|
|
367
431
|
}
|
|
368
|
-
async request(t,
|
|
432
|
+
async request(t, r, e) {
|
|
369
433
|
const n = Date.now(), o = `${this.config.host}${t}`, c = {
|
|
370
|
-
...
|
|
371
|
-
jwt:
|
|
434
|
+
...O(this.config.apiKey, {
|
|
435
|
+
jwt: r,
|
|
372
436
|
chatReference: e.chatReference
|
|
373
437
|
}),
|
|
374
438
|
...e.headers
|
|
375
439
|
};
|
|
376
440
|
this.addInfoRequestLog("API Request", o, e, c);
|
|
377
441
|
try {
|
|
378
|
-
const a = await
|
|
379
|
-
let
|
|
380
|
-
const
|
|
442
|
+
const a = await R(o, c, e), d = Date.now() - n;
|
|
443
|
+
let i;
|
|
444
|
+
const l = await a.text();
|
|
381
445
|
try {
|
|
382
|
-
|
|
446
|
+
i = l ? JSON.parse(l) : {};
|
|
383
447
|
} catch {
|
|
384
448
|
if (this.addErrorRequestParsing(
|
|
385
449
|
o,
|
|
386
450
|
e,
|
|
387
451
|
a,
|
|
388
|
-
|
|
389
|
-
|
|
452
|
+
d,
|
|
453
|
+
l
|
|
390
454
|
), !a.ok)
|
|
391
455
|
throw new Error(
|
|
392
|
-
`API request failed: ${a.status} ${a.statusText} - ${
|
|
456
|
+
`API request failed: ${a.status} ${a.statusText} - ${l.substring(0, 100)}`
|
|
393
457
|
);
|
|
394
|
-
|
|
458
|
+
i = { data: l };
|
|
395
459
|
}
|
|
396
460
|
if (!a.ok) {
|
|
397
|
-
this.addErrorRequestLog(o, e, a,
|
|
398
|
-
const A =
|
|
461
|
+
this.addErrorRequestLog(o, e, a, d, i);
|
|
462
|
+
const A = p(i, a);
|
|
399
463
|
throw new Error(`API request failed: ${A}`);
|
|
400
464
|
}
|
|
401
|
-
return
|
|
465
|
+
return i;
|
|
402
466
|
} catch (a) {
|
|
403
|
-
const
|
|
467
|
+
const d = Date.now() - n;
|
|
404
468
|
throw console.error("API Request Exception", {
|
|
405
469
|
method: e.method,
|
|
406
470
|
url: o,
|
|
407
|
-
duration: `${
|
|
471
|
+
duration: `${d}ms`,
|
|
408
472
|
error: a instanceof Error ? a.message : String(a)
|
|
409
473
|
}), a;
|
|
410
474
|
}
|
|
411
475
|
}
|
|
412
|
-
async stream(t,
|
|
413
|
-
const n = `${this.config.host}${t}`, o =
|
|
476
|
+
async stream(t, r, e) {
|
|
477
|
+
const n = `${this.config.host}${t}`, o = O(
|
|
414
478
|
this.config.apiKey,
|
|
415
479
|
{
|
|
416
|
-
jwt:
|
|
480
|
+
jwt: r,
|
|
417
481
|
chatReference: e.chatReference
|
|
418
482
|
},
|
|
419
483
|
e.headers
|
|
420
484
|
);
|
|
421
485
|
this.addInfoRequestLog("Stream Request", n, e, o);
|
|
422
|
-
const c = await
|
|
486
|
+
const c = await R(n, o, e);
|
|
423
487
|
if (!c.ok)
|
|
424
488
|
throw console.error("Stream Request Failed", {
|
|
425
489
|
method: e.method || "POST",
|
|
@@ -433,17 +497,17 @@ class j {
|
|
|
433
497
|
throw new Error("No response body for stream");
|
|
434
498
|
return c.body;
|
|
435
499
|
}
|
|
436
|
-
addInfoRequestLog(t,
|
|
500
|
+
addInfoRequestLog(t, r, e, n) {
|
|
437
501
|
const o = {
|
|
438
502
|
method: e.method || "GET",
|
|
439
|
-
url:
|
|
440
|
-
headers:
|
|
503
|
+
url: r,
|
|
504
|
+
headers: H(n)
|
|
441
505
|
};
|
|
442
506
|
e.body && (o.body = e.body), console.info(t, o);
|
|
443
507
|
}
|
|
444
|
-
addErrorRequestLog(t,
|
|
508
|
+
addErrorRequestLog(t, r, e, n, o) {
|
|
445
509
|
console.error("API Request Failed", {
|
|
446
|
-
method:
|
|
510
|
+
method: r.method,
|
|
447
511
|
url: t,
|
|
448
512
|
status: e.status,
|
|
449
513
|
statusText: e.statusText,
|
|
@@ -451,9 +515,9 @@ class j {
|
|
|
451
515
|
response: o
|
|
452
516
|
});
|
|
453
517
|
}
|
|
454
|
-
addErrorRequestParsing(t,
|
|
518
|
+
addErrorRequestParsing(t, r, e, n, o) {
|
|
455
519
|
console.error("API Response Parse Error", {
|
|
456
|
-
method:
|
|
520
|
+
method: r.method,
|
|
457
521
|
url: t,
|
|
458
522
|
status: e.status,
|
|
459
523
|
statusText: e.statusText,
|
|
@@ -465,20 +529,22 @@ class j {
|
|
|
465
529
|
}
|
|
466
530
|
}
|
|
467
531
|
export {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
532
|
+
N as AttachmentType,
|
|
533
|
+
D as ChatStreamSSEClient,
|
|
534
|
+
K as ConversationApi,
|
|
535
|
+
u as DIAL_API_ROUTES,
|
|
536
|
+
B as DialApiClient,
|
|
537
|
+
C as InvitationType,
|
|
538
|
+
_ as ResourceTypes,
|
|
539
|
+
y as ShareTarget,
|
|
540
|
+
U as chatStreamSSEClient,
|
|
541
|
+
k as decodeApiUrl,
|
|
542
|
+
h as encodeApiUrl,
|
|
543
|
+
b as generateConversationId,
|
|
544
|
+
p as getErrorMessage,
|
|
545
|
+
T as handleStreamMessage,
|
|
546
|
+
j as mergeMessages,
|
|
547
|
+
q as parseConversationName,
|
|
548
|
+
R as sendRequest,
|
|
549
|
+
F as streamChatResponse
|
|
484
550
|
};
|
package/models/chat-stream.d.ts
CHANGED
|
@@ -5,12 +5,20 @@ export interface RequestStreamBody {
|
|
|
5
5
|
messages: Message[];
|
|
6
6
|
model: ModelInfo;
|
|
7
7
|
content?: string;
|
|
8
|
+
custom_fields?: CustomFields;
|
|
8
9
|
}
|
|
9
10
|
export interface MessageStreamResponse {
|
|
10
11
|
id?: string;
|
|
11
12
|
content?: string;
|
|
12
13
|
choices: MessageChoices[];
|
|
13
14
|
}
|
|
15
|
+
export interface CustomFields {
|
|
16
|
+
custom_fields?: {
|
|
17
|
+
configuration?: {
|
|
18
|
+
choice?: string;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
}
|
|
14
22
|
export interface MessageChoices {
|
|
15
23
|
delta: {
|
|
16
24
|
content?: string;
|
package/models/conversation.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ConversationInfo } from '@epam/ai-dial-shared';
|
|
2
2
|
import { Message } from './message';
|
|
3
3
|
import { ModelInfo } from './model';
|
|
4
|
+
import { CustomFields } from './chat-stream';
|
|
4
5
|
import { InvitationType } from '../types/invitation-type';
|
|
5
6
|
import { ResourceTypes, ShareTarget } from '../constants/share-conversation';
|
|
6
7
|
export interface CreateConversationRequest {
|
|
@@ -12,6 +13,7 @@ export interface CreateConversationRequest {
|
|
|
12
13
|
prompt?: string;
|
|
13
14
|
temperature?: number;
|
|
14
15
|
selectedAddons?: string[];
|
|
16
|
+
custom_fields?: CustomFields;
|
|
15
17
|
}
|
|
16
18
|
export interface UpdateConversationRequest {
|
|
17
19
|
name?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@epam/statgpt-dial-toolkit",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
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.
|
|
17
|
+
"@epam/statgpt-shared-toolkit": "0.1.0-rc.2",
|
|
18
18
|
"@epam/ai-dial-shared": "^0.34.0"
|
|
19
19
|
}
|
|
20
20
|
}
|