@nabeh/chat-widget 0.1.2 → 0.1.3
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/README.md +57 -0
- package/dist/browser.cjs +69 -13
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.iife.js +69 -13
- package/dist/browser.iife.js.map +1 -1
- package/dist/browser.js +1 -1
- package/dist/{chunk-IFIILMOK.js → chunk-LET2O6EB.js} +70 -14
- package/dist/chunk-LET2O6EB.js.map +1 -0
- package/dist/index.cjs +69 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.iife.js +69 -13
- package/dist/index.iife.js.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-IFIILMOK.js.map +0 -1
package/README.md
CHANGED
|
@@ -51,9 +51,64 @@ export class AppComponent implements AfterViewInit, OnDestroy {
|
|
|
51
51
|
}
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
## Angular Embedded Page Usage
|
|
55
|
+
|
|
56
|
+
Use `displayMode: 'embedded'` when the host application already owns the page shell and you only want the chat experience rendered inside a content area.
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { AfterViewInit, Component, ElementRef, OnDestroy, ViewChild } from '@angular/core';
|
|
60
|
+
import { createChatWidget } from '@nabeh/chat-widget';
|
|
61
|
+
|
|
62
|
+
@Component({
|
|
63
|
+
selector: 'app-knowledge-assistant-page',
|
|
64
|
+
template: `<div #chatRoot class="knowledge-assistant-root"></div>`
|
|
65
|
+
})
|
|
66
|
+
export class KnowledgeAssistantPageComponent implements AfterViewInit, OnDestroy {
|
|
67
|
+
@ViewChild('chatRoot', { static: true })
|
|
68
|
+
private chatRoot?: ElementRef<HTMLElement>;
|
|
69
|
+
|
|
70
|
+
private widget?: ReturnType<typeof createChatWidget>;
|
|
71
|
+
|
|
72
|
+
ngAfterViewInit(): void {
|
|
73
|
+
if (!this.chatRoot?.nativeElement) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
this.widget = createChatWidget({
|
|
78
|
+
apiBaseUrl: 'http://your-api-url',
|
|
79
|
+
displayMode: 'embedded',
|
|
80
|
+
mount: this.chatRoot.nativeElement,
|
|
81
|
+
embedded: {
|
|
82
|
+
showHeader: false
|
|
83
|
+
},
|
|
84
|
+
endpoints: {
|
|
85
|
+
ask: '/my-chats/:chatId/messages',
|
|
86
|
+
history: '/my-chats/:chatId/messages',
|
|
87
|
+
listChats: '/my-chats',
|
|
88
|
+
createChat: '/my-chats',
|
|
89
|
+
updateChat: '/my-chats/:chatId',
|
|
90
|
+
deleteChat: '/my-chats/:chatId'
|
|
91
|
+
},
|
|
92
|
+
rag: {
|
|
93
|
+
knowledgeNames: ['sample-kb'],
|
|
94
|
+
loadHistoryOnOpen: true
|
|
95
|
+
},
|
|
96
|
+
getUserContext: async () => ({
|
|
97
|
+
userId: 'your-user-id'
|
|
98
|
+
})
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
ngOnDestroy(): void {
|
|
103
|
+
this.widget?.destroy();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
54
108
|
## Configuration
|
|
55
109
|
|
|
56
110
|
- `apiBaseUrl`: Backend base URL. Example: `https://api.example.com`
|
|
111
|
+
- `displayMode`: `widget` for the floating launcher, `embedded` for a full chat page rendered inside `mount`
|
|
57
112
|
- `endpoints.ask`: Send-message endpoint
|
|
58
113
|
- `endpoints.history`: Fetch chat history endpoint
|
|
59
114
|
- `endpoints.listChats`: List chats endpoint
|
|
@@ -64,6 +119,7 @@ export class AppComponent implements AfterViewInit, OnDestroy {
|
|
|
64
119
|
- `rag.loadHistoryOnOpen`: Load history automatically when the widget opens
|
|
65
120
|
- `getUserContext`: Function that returns the current user context
|
|
66
121
|
- `getAccessToken`: Optional function that returns a bearer token
|
|
122
|
+
- `embedded.showHeader`: In embedded mode, show or hide the library-owned page header. Default: `false`
|
|
67
123
|
|
|
68
124
|
## Example With Token
|
|
69
125
|
|
|
@@ -118,3 +174,4 @@ If you are loading the browser bundle manually, the package also exposes:
|
|
|
118
174
|
- `apiBaseUrl` should point to your backend, not your Angular frontend
|
|
119
175
|
- initialize the widget after authentication if your backend requires user identity or tokens
|
|
120
176
|
- call `destroy()` when the hosting Angular component is destroyed
|
|
177
|
+
- for embedded mode, give the mount container a real height such as `min-height: calc(100vh - navbar - footer)`
|
package/dist/browser.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var Ce=Object.defineProperty;var Jt=Object.getOwnPropertyDescriptor;var Qt=Object.getOwnPropertyNames;var Zt=Object.prototype.hasOwnProperty;var en=(e,t)=>{for(var a in t)Ce(e,a,{get:t[a],enumerable:!0})},tn=(e,t,a,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Qt(t))!Zt.call(e,i)&&i!==a&&Ce(e,i,{get:()=>t[i],enumerable:!(n=Jt(t,i))||n.enumerable});return e};var nn=e=>tn(Ce({},"__esModule",{value:!0}),e);var En={};en(En,{browserGlobal:()=>At,createChatWidget:()=>de,init:()=>Lt,version:()=>Ct});module.exports=nn(En);function ot(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function Le(e,t){let a={...e};for(let n of Object.keys(t)){let i=t[n],l=a[n];if(ot(l)&&ot(i)){a[n]=Le(l,i);continue}i!==void 0&&(a[n]=i)}return a}function st(e){return e instanceof Error?e:typeof e=="string"?new Error(e):new Error("Unexpected widget error")}function G(e,t){return`${e.replace(/\/$/,"")}/${t.replace(/^\//,"")}`}function D(e,t,a){return{"Content-Type":"application/json",...e.customHeaders,...a?{"X-Chat-User-Context":a}:{},...t?{Authorization:`Bearer ${t}`}:{}}}async function q(e){if(!e.getUserContext)return null;let t=await e.getUserContext();return t?JSON.stringify(t):null}function pe(e,t,a={}){let n=t.replace(/\{chatId\}/g,encodeURIComponent(a.chatId??"")).replace(/:chatId\b/g,encodeURIComponent(a.chatId??""));return G(e.apiBaseUrl,n)}async function rt(e,t){let a=e.getAccessToken?await e.getAccessToken():null,n=await q(e),i=pe(e,e.endpoints.ask,{chatId:t.chatId}),s=/(\{chatId\}|:chatId\b)/.test(e.endpoints.ask)?{message:t.message,knowledgeNames:t.knowledgeNames,editLastQa:t.editLastQa??!1,enableReferences:t.enableReferences??!0}:{query:t.message,chat_id:t.chatId,knowledge_names:t.knowledgeNames,edit_last_qa:t.editLastQa??!1,enable_references:t.enableReferences??!0},p=await fetch(i,{method:"POST",headers:D(e,a,n),body:JSON.stringify(s)});if(!p.ok)throw new Error(`Chat backend returned ${p.status}.`);let c=await p.json();if(!Array.isArray(c)){if(!c.answer||typeof c.answer!="string")throw new Error("Chat backend response is missing a valid answer.");return{chatId:c.chatId??t.chatId,answer:c.answer,suggestions:c.suggestions??[],citations:c.citations??[]}}let u=c[0];if(!u?.answer||typeof u.answer!="string")throw new Error("Chat backend response is missing a valid answer.");let g=u.content,f=g?.source_documents??[],h=g?.scores??[],w=g?.page_numbers??[],L=g?.sheet_names??[],b=g?.row_numbers??[],y=g?.knowledge_names??[],k=f.map((M,v)=>({sourceDocument:M,score:h[v]??null,pageNumber:w[v]??null,sheetName:L[v]??null,rowNumber:b[v]??null,knowledgeName:y[v]??null}));return{chatId:t.chatId,answer:u.answer,suggestions:[],citations:k}}async function it(e,t){if(!e.endpoints.history)return[];let a=e.getAccessToken?await e.getAccessToken():null,n=await q(e),l=/(\{chatId\}|:chatId\b)/.test(e.endpoints.history)?pe(e,e.endpoints.history,{chatId:t}):(()=>{let u=new URL(G(e.apiBaseUrl,e.endpoints.history));return u.searchParams.set("chat_id",t),u.toString()})(),s=await fetch(l,{method:"GET",headers:D(e,a,n)});if(!s.ok)throw new Error(`Chat history endpoint returned ${s.status}.`);let p=await s.json(),c=Array.isArray(p)?p:p&&typeof p=="object"?p.history??p.messages??p.data??[]:[];return Array.isArray(c)?c.map(u=>{if(!u||typeof u!="object")return null;let g=u;if(typeof g.question=="string"&&typeof g.answer=="string")return[{role:"user",text:g.question},{role:"assistant",text:g.answer}];let f=g.role??g.type??g.sender??g.author,h=g.text??g.message??g.content??g.answer;if(typeof h!="string")return null;let w=typeof f=="string"?f.toLowerCase():"assistant";return[{role:w==="user"||w==="human"?"user":"assistant",text:h}]}).flat().filter(u=>!!u):[]}async function lt(e){if(!e.endpoints.listChats)return[];let t=e.getAccessToken?await e.getAccessToken():null,a=await q(e),n=await fetch(G(e.apiBaseUrl,e.endpoints.listChats),{method:"GET",headers:D(e,t,a)});if(!n.ok)throw new Error(`Chat list endpoint returned ${n.status}.`);let i=await n.json(),l=Array.isArray(i)?i:i&&typeof i=="object"?i.chats??i.data??i.items??[]:[];return Array.isArray(l)?l.map(s=>{if(!s||typeof s!="object")return null;let p=s,c=p.chatId??p.chat_id??p.id,u=p.title??p.name??p.chatId;if(typeof c!="string"||typeof u!="string")return null;let g=typeof p.createdAt=="string"?p.createdAt:typeof p.created_at=="string"?p.created_at:null,f=typeof p.updatedAt=="string"?p.updatedAt:typeof p.updated_at=="string"?p.updated_at:null,h={chatId:c,title:u,pinned:typeof p.pinned=="boolean"?p.pinned:!1};return g&&(h.createdAt=g),f&&(h.updatedAt=f),h}).filter(s=>!!s):[]}async function pt(e,t,a){let n=e.endpoints.createChat??e.endpoints.listChats;if(!n)return;let i=e.getAccessToken?await e.getAccessToken():null,l=await q(e),s=await fetch(G(e.apiBaseUrl,n),{method:"POST",headers:D(e,i,l),body:JSON.stringify({chatId:t,...a?{title:a}:{}})});if(!s.ok)throw new Error(`Create chat endpoint returned ${s.status}.`)}async function Ae(e,t,a){if(!e.endpoints.updateChat)return;let n=e.getAccessToken?await e.getAccessToken():null,i=await q(e),l=pe(e,e.endpoints.updateChat,{chatId:t}),s=await fetch(l,{method:"PATCH",headers:D(e,n,i),body:JSON.stringify(a)});if(!s.ok)throw new Error(`Update chat endpoint returned ${s.status}.`)}async function ct(e,t){if(!e.endpoints.deleteChat)return;let a=e.getAccessToken?await e.getAccessToken():null,n=await q(e),i=pe(e,e.endpoints.deleteChat,{chatId:t}),l=await fetch(i,{method:"DELETE",headers:D(e,a,n)});if(!l.ok)throw new Error(`Delete chat endpoint returned ${l.status}.`)}function B(e,t){let a=st(t);return e.onError?.(a),a}var Pe={accent:"#0f766e",accentSoft:"#ecfeff",panelBackground:"#ffffff",surfaceBackground:"#f8fafc",text:"#1f2937",mutedText:"#64748b",borderColor:"#dbe4ee",shadow:"0 24px 64px rgba(15, 23, 42, 0.20)",zIndex:2147483e3,fontFamily:'"Segoe UI", -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif'},x={displayMode:"widget",position:"bottom-right",title:"Knowledge Assistant",subtitle:"Answers are generated based on your access permissions",welcomeMessage:"How can I assist you today?",inputPlaceholder:"Ask your question...",launcherAriaLabel:"Open chat assistant",closeAriaLabel:"Close chat assistant",initialSuggestions:["Which indicators reflect the achievements of Vision KSA goals?","Case studies of real estate initiatives for economic growth","Study of UX for the ministry external portal"],sourceApp:"knowledge-platform",locale:"en",customHeaders:{},assistantPageUrl:"/knowledge-assistant",embedded:{showHeader:!1},rag:{knowledgeNames:[],enableReferences:!0,loadHistoryOnOpen:!1},theme:Pe,getAccessToken:void 0,getUserContext:void 0,onOpen:void 0,onClose:void 0,onError:void 0,onOpenAssistantPage:void 0};function dt(e){if(!e.apiBaseUrl?.trim())throw new Error("Chat widget config requires a non-empty apiBaseUrl.");if(!e.endpoints?.ask?.trim())throw new Error("Chat widget config requires endpoints.ask to be provided.");let t=e.mount??document.body,a=Le(Pe,e.theme??{});return{apiBaseUrl:e.apiBaseUrl,endpoints:{...e.endpoints},mount:t,displayMode:e.displayMode??x.displayMode,position:e.position??x.position,title:e.title??x.title,subtitle:e.subtitle??x.subtitle,welcomeMessage:e.welcomeMessage??x.welcomeMessage,inputPlaceholder:e.inputPlaceholder??x.inputPlaceholder,launcherAriaLabel:e.launcherAriaLabel??x.launcherAriaLabel,closeAriaLabel:e.closeAriaLabel??x.closeAriaLabel,initialSuggestions:e.initialSuggestions??x.initialSuggestions,sourceApp:e.sourceApp??x.sourceApp,locale:e.locale??x.locale,customHeaders:e.customHeaders??x.customHeaders,embedded:{...x.embedded,...e.embedded??{}},rag:{...x.rag,...e.rag??{}},assistantPageUrl:e.assistantPageUrl??x.assistantPageUrl,theme:a,getAccessToken:e.getAccessToken,getUserContext:e.getUserContext,onOpen:e.onOpen,onClose:e.onClose,onError:e.onError,onOpenAssistantPage:e.onOpenAssistantPage}}function o(e,t,a){let n=document.createElement(e);return t&&(n.className=t),a!==void 0&&(n.textContent=a),n}var ut="kp-chat-widget-styles";function gt(e,t){if(e.getElementById(ut))return;let a=document.createElement("style");a.id=ut,a.textContent=an(t),e.appendChild(a)}function an(e){return`
|
|
2
2
|
:host {
|
|
3
3
|
all: initial;
|
|
4
4
|
}
|
|
@@ -25,6 +25,15 @@
|
|
|
25
25
|
box-sizing: border-box;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
.kp-chat-widget.kp-chat-widget-embedded {
|
|
29
|
+
position: relative;
|
|
30
|
+
inset: auto;
|
|
31
|
+
width: 100%;
|
|
32
|
+
height: 100%;
|
|
33
|
+
min-height: 640px;
|
|
34
|
+
display: block;
|
|
35
|
+
}
|
|
36
|
+
|
|
28
37
|
*,
|
|
29
38
|
*::before,
|
|
30
39
|
*::after {
|
|
@@ -37,6 +46,11 @@
|
|
|
37
46
|
right: auto;
|
|
38
47
|
}
|
|
39
48
|
|
|
49
|
+
.kp-chat-widget.kp-chat-widget-embedded.bottom-left {
|
|
50
|
+
left: auto;
|
|
51
|
+
right: auto;
|
|
52
|
+
}
|
|
53
|
+
|
|
40
54
|
.kp-rtl .kp-dropdown-item,
|
|
41
55
|
.kp-rtl .kp-suggestion,
|
|
42
56
|
.kp-rtl .kp-input,
|
|
@@ -195,6 +209,18 @@
|
|
|
195
209
|
overflow: hidden;
|
|
196
210
|
}
|
|
197
211
|
|
|
212
|
+
.kp-full-page.kp-full-page-embedded {
|
|
213
|
+
position: relative;
|
|
214
|
+
inset: auto;
|
|
215
|
+
opacity: 1;
|
|
216
|
+
pointer-events: auto;
|
|
217
|
+
transform: none;
|
|
218
|
+
min-height: 100%;
|
|
219
|
+
height: 100%;
|
|
220
|
+
z-index: auto;
|
|
221
|
+
background: transparent;
|
|
222
|
+
}
|
|
223
|
+
|
|
198
224
|
.kp-full-page.open {
|
|
199
225
|
opacity: 1;
|
|
200
226
|
pointer-events: auto;
|
|
@@ -210,6 +236,13 @@
|
|
|
210
236
|
overflow: hidden;
|
|
211
237
|
}
|
|
212
238
|
|
|
239
|
+
.kp-full-page-embedded .kp-full-page-shell {
|
|
240
|
+
height: 100%;
|
|
241
|
+
min-height: 100%;
|
|
242
|
+
padding: 0;
|
|
243
|
+
gap: 12px;
|
|
244
|
+
}
|
|
245
|
+
|
|
213
246
|
.kp-full-page-header {
|
|
214
247
|
display: flex;
|
|
215
248
|
align-items: center;
|
|
@@ -219,6 +252,10 @@
|
|
|
219
252
|
flex: none;
|
|
220
253
|
}
|
|
221
254
|
|
|
255
|
+
.kp-hidden {
|
|
256
|
+
display: none !important;
|
|
257
|
+
}
|
|
258
|
+
|
|
222
259
|
.kp-full-page-brand {
|
|
223
260
|
display: flex;
|
|
224
261
|
align-items: center;
|
|
@@ -286,6 +323,10 @@
|
|
|
286
323
|
align-items: stretch;
|
|
287
324
|
}
|
|
288
325
|
|
|
326
|
+
.kp-full-page-embedded .kp-full-page-content {
|
|
327
|
+
height: 100%;
|
|
328
|
+
}
|
|
329
|
+
|
|
289
330
|
.kp-full-page-sidebar,
|
|
290
331
|
.kp-full-page-panel,
|
|
291
332
|
.kp-source-panel {
|
|
@@ -1280,6 +1321,10 @@
|
|
|
1280
1321
|
max-height: 260px;
|
|
1281
1322
|
}
|
|
1282
1323
|
|
|
1324
|
+
.kp-full-page-embedded .kp-full-page-sidebar {
|
|
1325
|
+
max-height: none;
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1283
1328
|
.kp-source-panel.open {
|
|
1284
1329
|
position: static;
|
|
1285
1330
|
inset: auto;
|
|
@@ -1296,6 +1341,13 @@
|
|
|
1296
1341
|
bottom: 16px;
|
|
1297
1342
|
}
|
|
1298
1343
|
|
|
1344
|
+
.kp-chat-widget.kp-chat-widget-embedded,
|
|
1345
|
+
.kp-chat-widget.kp-chat-widget-embedded.bottom-left {
|
|
1346
|
+
left: auto;
|
|
1347
|
+
right: auto;
|
|
1348
|
+
bottom: auto;
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1299
1351
|
.kp-panel,
|
|
1300
1352
|
.kp-chat-widget.bottom-left .kp-panel {
|
|
1301
1353
|
inset: 0;
|
|
@@ -1314,6 +1366,10 @@
|
|
|
1314
1366
|
padding: 14px;
|
|
1315
1367
|
}
|
|
1316
1368
|
|
|
1369
|
+
.kp-full-page-embedded .kp-full-page-shell {
|
|
1370
|
+
padding: 0;
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1317
1373
|
.kp-full-page-header {
|
|
1318
1374
|
padding: 0;
|
|
1319
1375
|
}
|
|
@@ -1373,15 +1429,15 @@
|
|
|
1373
1429
|
40% { transform: translate(16px, -13px) scale(0.92); opacity: 0.96; }
|
|
1374
1430
|
62% { transform: translate(2px, -2px) scale(1); opacity: 0.98; }
|
|
1375
1431
|
}
|
|
1376
|
-
`}var gt={en:{openChatActions:"Open chat actions",newChat:"New Chat",myChats:"My Chats",openAssistant:"Open Knowledge Assistant",back:"Back",close:"Close",assistantBadge:"Knowledge Assistant",closeAssistantPage:"Close knowledge assistant page",searchChat:"Search Chat",recentActivity:"Recent Activity",pinnedCollections:"Pinned Collections",answersBasedOnPermissions:"Answers are generated based on your access permissions",authTokenForwarded:"Auth token is forwarded from the host app when configured.",thinking:"Thinking...",unableToCreateChat:"Unable to create chat",requestFailed:"Request failed",noRecentChats:"No recent chats yet.",noPinnedChats:"No pinned chats yet.",noChats:"No chats yet.",loadingChats:"Loading chats...",pinChat:"Pin chat",unpinChat:"Unpin chat",renameChat:"Rename",deleteChat:"Delete",chatActions:"Chat actions",renamePrompt:"Enter a new chat name",citationsAttached:e=>`${e} citation${e>1?"s":""} attached`,sourcesUsed:"Sources Used",allSourcesUsed:"All Sources Used",documentsAndReferences:"AI documents and references",showAll:"Show All",noSources:"No sources were returned for this answer.",closeSourcesPanel:"Close sources panel",openSource:"Open source",sourceScore:"Score",sourcePage:"Page",sourceSheet:"Sheet",sourceRow:"Row",sourceKnowledge:"Knowledge Base",untitledSource:"Untitled Source",copy:"Copy",copied:"Copied",helpful:"Helpful",notHelpful:"Needs work",send:"Send message",assistantAvatar:"Assistant",userAvatar:"User"},ar:{openChatActions:"\u0641\u062A\u062D \u0625\u062C\u0631\u0627\u0621\u0627\u062A \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0629",newChat:"\u0645\u062D\u0627\u062F\u062B\u0629 \u062C\u062F\u064A\u062F\u0629",myChats:"\u0645\u062D\u0627\u062F\u062B\u0627\u062A\u064A",openAssistant:"\u0641\u062A\u062D \u0645\u0633\u0627\u0639\u062F \u0627\u0644\u0645\u0639\u0631\u0641\u0629",back:"\u0631\u062C\u0648\u0639",close:"\u0625\u063A\u0644\u0627\u0642",assistantBadge:"\u0645\u0633\u0627\u0639\u062F \u0627\u0644\u0645\u0639\u0631\u0641\u0629",closeAssistantPage:"\u0625\u063A\u0644\u0627\u0642 \u0635\u0641\u062D\u0629 \u0645\u0633\u0627\u0639\u062F \u0627\u0644\u0645\u0639\u0631\u0641\u0629",searchChat:"\u0627\u0628\u062D\u062B \u0641\u064A \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0627\u062A",recentActivity:"\u0627\u0644\u0646\u0634\u0627\u0637 \u0627\u0644\u0623\u062E\u064A\u0631",pinnedCollections:"\u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0627\u062A \u0627\u0644\u0645\u062B\u0628\u062A\u0629",answersBasedOnPermissions:"\u064A\u062A\u0645 \u0625\u0646\u0634\u0627\u0621 \u0627\u0644\u0625\u062C\u0627\u0628\u0627\u062A \u0628\u0646\u0627\u0621\u064B \u0639\u0644\u0649 \u0635\u0644\u0627\u062D\u064A\u0627\u062A \u0627\u0644\u0648\u0635\u0648\u0644 \u0627\u0644\u062E\u0627\u0635\u0629 \u0628\u0643",authTokenForwarded:"\u064A\u062A\u0645 \u062A\u0645\u0631\u064A\u0631 \u0631\u0645\u0632 \u0627\u0644\u062A\u062D\u0642\u0642 \u0645\u0646 \u0627\u0644\u062A\u0637\u0628\u064A\u0642 \u0627\u0644\u0645\u0636\u064A\u0641 \u0639\u0646\u062F \u0627\u0644\u0625\u0639\u062F\u0627\u062F.",thinking:"\u062C\u0627\u0631\u064D \u0627\u0644\u062A\u0641\u0643\u064A\u0631...",unableToCreateChat:"\u062A\u0639\u0630\u0631 \u0625\u0646\u0634\u0627\u0621 \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0629",requestFailed:"\u0641\u0634\u0644 \u0627\u0644\u0637\u0644\u0628",noRecentChats:"\u0644\u0627 \u062A\u0648\u062C\u062F \u0645\u062D\u0627\u062F\u062B\u0627\u062A \u062D\u062F\u064A\u062B\u0629 \u0628\u0639\u062F.",noPinnedChats:"\u0644\u0627 \u062A\u0648\u062C\u062F \u0645\u062D\u0627\u062F\u062B\u0627\u062A \u0645\u062B\u0628\u062A\u0629 \u0628\u0639\u062F.",noChats:"\u0644\u0627 \u062A\u0648\u062C\u062F \u0645\u062D\u0627\u062F\u062B\u0627\u062A \u0628\u0639\u062F.",loadingChats:"\u062C\u0627\u0631\u064D \u062A\u062D\u0645\u064A\u0644 \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0627\u062A...",pinChat:"\u062A\u062B\u0628\u064A\u062A \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0629",unpinChat:"\u0625\u0644\u063A\u0627\u0621 \u062A\u062B\u0628\u064A\u062A \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0629",renameChat:"\u0625\u0639\u0627\u062F\u0629 \u062A\u0633\u0645\u064A\u0629",deleteChat:"\u062D\u0630\u0641",chatActions:"\u0625\u062C\u0631\u0627\u0621\u0627\u062A \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0629",renamePrompt:"\u0623\u062F\u062E\u0644 \u0627\u0633\u0645\u064B\u0627 \u062C\u062F\u064A\u062F\u064B\u0627 \u0644\u0644\u0645\u062D\u0627\u062F\u062B\u0629",citationsAttached:e=>`\u062A\u0645 \u0625\u0631\u0641\u0627\u0642 ${e} \u0645\u0631\u062C\u0639${e>1?"\u0627\u062A":""}`,sourcesUsed:"\u0627\u0644\u0645\u0635\u0627\u062F\u0631 \u0627\u0644\u0645\u0633\u062A\u062E\u062F\u0645\u0629",allSourcesUsed:"\u0643\u0644 \u0627\u0644\u0645\u0635\u0627\u062F\u0631 \u0627\u0644\u0645\u0633\u062A\u062E\u062F\u0645\u0629",documentsAndReferences:"\u0645\u0633\u062A\u0646\u062F\u0627\u062A \u0648\u0645\u0631\u0627\u062C\u0639 \u0627\u0644\u0630\u0643\u0627\u0621 \u0627\u0644\u0627\u0635\u0637\u0646\u0627\u0639\u064A",showAll:"\u0639\u0631\u0636 \u0627\u0644\u0643\u0644",noSources:"\u0644\u0645 \u064A\u062A\u0645 \u0625\u0631\u062C\u0627\u0639 \u0645\u0635\u0627\u062F\u0631 \u0644\u0647\u0630\u0647 \u0627\u0644\u0625\u062C\u0627\u0628\u0629.",closeSourcesPanel:"\u0625\u063A\u0644\u0627\u0642 \u0644\u0648\u062D\u0629 \u0627\u0644\u0645\u0635\u0627\u062F\u0631",openSource:"\u0641\u062A\u062D \u0627\u0644\u0645\u0635\u062F\u0631",sourceScore:"\u0627\u0644\u062F\u0631\u062C\u0629",sourcePage:"\u0627\u0644\u0635\u0641\u062D\u0629",sourceSheet:"\u0627\u0644\u0648\u0631\u0642\u0629",sourceRow:"\u0627\u0644\u0635\u0641",sourceKnowledge:"\u0642\u0627\u0639\u062F\u0629 \u0627\u0644\u0645\u0639\u0631\u0641\u0629",untitledSource:"\u0645\u0635\u062F\u0631 \u0628\u062F\u0648\u0646 \u0639\u0646\u0648\u0627\u0646",copy:"\u0646\u0633\u062E",copied:"\u062A\u0645 \u0627\u0644\u0646\u0633\u062E",helpful:"\u0645\u0641\u064A\u062F",notHelpful:"\u064A\u062D\u062A\u0627\u062C \u062A\u062D\u0633\u064A\u0646",send:"\u0625\u0631\u0633\u0627\u0644 \u0627\u0644\u0631\u0633\u0627\u0644\u0629",assistantAvatar:"\u0627\u0644\u0645\u0633\u0627\u0639\u062F",userAvatar:"\u0627\u0644\u0645\u0633\u062A\u062E\u062F\u0645"}};function de(e){if(typeof document>"u")throw new Error("Chat widget can only be initialized in a browser.");let t=ct(e),o=Ae(t.locale),n=pn(t.locale),i=cn(o),s={chatId:ce(t),open:!1,fullPageOpen:!1,myChatsOpen:!1,accessTokenProvider:t.getAccessToken,historyLoadedChatId:null,menuOpen:!1,chats:[],chatSearchTerm:"",loadingChats:!1,sourcePanelOpen:!1,sourcePanelTitle:null},d=document.createElement("div");d.dataset.chatWidgetHost="true",t.mount.appendChild(d);let l=d.attachShadow({mode:"open"});ut(l,t.theme);let p=a("div",`kp-chat-widget ${t.position}`);p.lang=o,p.dir=i?"rtl":"ltr",i&&p.classList.add("kp-rtl");let u=a("div","kp-overlay"),g=a("button","kp-launcher");g.type="button",g.setAttribute("aria-label",t.launcherAriaLabel),g.innerHTML=['<span class="kp-star-cluster" aria-hidden="true">','<span class="kp-star orbit-a">\u2726</span>','<span class="kp-star orbit-b">\u2726</span>','<span class="kp-star orbit-c">\u2726</span>','<span class="kp-star main">\u2726</span>',"</span>"].join("");let f=a("section","kp-panel");f.setAttribute("role","dialog"),f.setAttribute("aria-modal","true"),f.setAttribute("aria-label",t.title);let m=a("div","kp-header"),y=a("div","kp-toolbar"),b=a("button","kp-tool-button kp-menu-trigger");b.type="button",b.setAttribute("aria-label",n.openChatActions),b.innerHTML=['<span class="kp-pencil-icon" aria-hidden="true"></span>','<span class="kp-chevron" aria-hidden="true">\u2304</span>'].join("");let x=a("div","kp-dropdown"),v=a("button","kp-dropdown-item",n.newChat);v.type="button";let k=a("button","kp-dropdown-item",n.myChats);k.type="button";let $=a("button","kp-dropdown-item",n.openAssistant);$.type="button",x.append(v,k,$),y.append(b,x);let C=a("div","kp-title-wrap"),Lt=a("h2","kp-title",t.title),At=a("div","kp-subtitle",t.subtitle);C.append(Lt,At);let Q=a("button","kp-close","\xD7");Q.type="button",Q.setAttribute("aria-label",t.closeAriaLabel),m.append(y,C,Q);let Z=a("div","kp-body"),ue=a("div","kp-hero"),Pt=a("div","kp-hero-icon","\u2726"),St=a("div","kp-hero-text",t.welcomeMessage);ue.append(Pt,St);let Pe=a("div","kp-footer"),ge=a("form","kp-form"),S=a("input","kp-input");S.type="text",S.autocomplete="off",S.placeholder=t.inputPlaceholder,S.setAttribute("aria-label",t.inputPlaceholder);let fe=a("button","kp-send","\u279C");fe.type="submit",fe.setAttribute("aria-label",n.send);let Tt=a("div","kp-note",n.authTokenForwarded);ge.append(S,fe),Pe.append(ge,Tt),f.append(m,Z,Pe),p.append(u,g,f),l.appendChild(p),Z.appendChild(ue);let Se=a("div","kp-suggestions");Z.appendChild(Se);let ee=a("section","kp-my-chats-sheet"),Te=a("div","kp-my-chats-header"),te=a("button","kp-my-chats-nav","\u2190");te.type="button",te.setAttribute("aria-label",n.back);let ne=a("button","kp-my-chats-nav kp-my-chats-close","\xD7");ne.type="button",ne.setAttribute("aria-label",n.close),Te.append(te,ne);let Ee=a("div","kp-my-chats-body"),Et=a("div","kp-my-chats-section-label",n.recentActivity),Ie=a("div","kp-my-chats-list"),It=a("div","kp-my-chats-section-label",n.pinnedCollections),Re=a("div","kp-my-chats-list");Ee.append(Et,Ie,It,Re),ee.append(Te,Ee),f.appendChild(ee);let A={body:Z,input:S,suggestions:Se,hero:ue,kind:"panel"},T=a("section","kp-full-page");T.setAttribute("role","dialog"),T.setAttribute("aria-modal","true"),T.setAttribute("aria-label",`${t.title} page`);let $e=a("div","kp-full-page-shell"),He=a("div","kp-full-page-header"),ze=a("div","kp-full-page-brand"),Rt=a("div","kp-full-page-brand-mark","\u2726"),$t=a("div","kp-full-page-brand-text",t.title);ze.append(Rt,$t);let Me=a("div","kp-full-page-header-actions"),Ht=a("div","kp-full-page-badge",n.assistantBadge),oe=a("button","kp-full-page-close","\xD7");oe.type="button",oe.setAttribute("aria-label",n.closeAssistantPage),Me.append(Ht,oe),He.append(ze,Me);let Ne=a("div","kp-full-page-content"),Be=a("aside","kp-full-page-sidebar"),he=a("button","kp-full-page-new-chat",`+ ${n.newChat}`);he.type="button";let Ue=a("div","kp-full-page-search"),V=a("input","kp-full-page-search-input");V.type="search",V.placeholder=n.searchChat;let zt=a("span","kp-full-page-search-icon","\u2315");Ue.append(V,zt);let Mt=a("div","kp-full-page-section-label",n.recentActivity),Oe=a("div","kp-full-page-recent-list"),Nt=a("div","kp-full-page-section-label",n.pinnedCollections),We=a("div","kp-full-page-pinned-list");Be.append(he,Ue,Mt,Oe,Nt,We);let je=a("main","kp-full-page-main"),De=a("section","kp-full-page-panel"),me=a("div","kp-full-page-body"),be=a("div","kp-full-page-hero"),Ve=a("div","kp-full-page-hero-badge");Ve.innerHTML=['<span class="kp-star-cluster kp-star-cluster-static" aria-hidden="true">','<span class="kp-star orbit-a">\u2726</span>','<span class="kp-star orbit-b">\u2726</span>','<span class="kp-star orbit-c">\u2726</span>','<span class="kp-star main">\u2726</span>',"</span>"].join("");let Bt=a("div","kp-full-page-hero-text",t.welcomeMessage);be.append(Ve,Bt);let qe=a("div","kp-suggestions kp-full-page-suggestions");me.append(be,qe);let _e=a("div","kp-full-page-footer"),ke=a("form","kp-form kp-full-page-form"),P=a("input","kp-input kp-full-page-input");P.type="text",P.autocomplete="off",P.placeholder=t.inputPlaceholder,P.setAttribute("aria-label",t.inputPlaceholder);let xe=a("button","kp-send kp-full-page-send","\u279C");xe.type="submit",xe.setAttribute("aria-label",n.send);let Ut=a("div","kp-note kp-full-page-note",n.answersBasedOnPermissions);ke.append(P,xe),_e.append(ke,Ut),De.append(me,_e),je.appendChild(De);let ae=a("aside","kp-source-panel"),Fe=a("div","kp-source-panel-header"),Ke=a("div","kp-source-panel-title-wrap"),Xe=a("div","kp-source-panel-title",n.allSourcesUsed),Ot=a("div","kp-source-panel-subtitle",n.documentsAndReferences);Ke.append(Xe,Ot);let se=a("button","kp-source-panel-close","\xD7");se.type="button",se.setAttribute("aria-label",n.closeSourcesPanel),Fe.append(Ke,se);let q=a("div","kp-source-panel-list"),Wt=a("div","kp-source-panel-empty",n.noSources);q.appendChild(Wt),ae.append(Fe,q),Ne.append(Be,je,ae),$e.append(He,Ne),T.appendChild($e),p.appendChild(T);let L={body:me,input:P,suggestions:qe,hero:be,kind:"full-page"},H=()=>({...t,getAccessToken:s.accessTokenProvider}),Ye=async()=>{if(!t.getUserContext)return null;try{let r=await t.getUserContext();return r?.displayName?.trim()||r?.email?.trim()||r?.userId?.trim()||null}catch{return null}},_=(r,c)=>{if(s.sourcePanelOpen=!0,s.sourcePanelTitle=c??n.allSourcesUsed,Xe.textContent=s.sourcePanelTitle,ae.classList.add("open"),q.textContent="",r.length===0){q.appendChild(a("div","kp-source-panel-empty",n.noSources));return}for(let h of r)q.appendChild(xn(h,n))},re=()=>{s.sourcePanelOpen=!1,s.sourcePanelTitle=null,ae.classList.remove("open")};j(A,t.initialSuggestions,async r=>{await E(r,A)}),j(L,t.initialSuggestions,async r=>{await E(r,L)}),N(),ie();function Ge(){s.open||(s.open=!0,s.fullPageOpen=!1,I(),T.classList.remove("open"),g.classList.add("hidden"),u.classList.add("visible"),f.classList.add("open"),t.onOpen?.(),t.rag.loadHistoryOnOpen&&ye.loadHistory(),queueMicrotask(()=>S.focus()))}function F(){s.open&&(z(),I(),s.open=!1,g.classList.remove("hidden"),u.classList.remove("visible"),f.classList.remove("open"),t.onClose?.())}async function E(r,c){let h=r.trim();if(!h)return;c.input.value="";try{await _t(h)}catch(R){let K=U(t,R);G(c.body,"bot",`${n.unableToCreateChat}: ${K.message}`,{strings:n,view:c,userName:null,onShowSources:_});return}ft(c),G(c.body,"user",h,{strings:n,view:c,userName:await Ye(),onShowSources:_}),c.body.scrollTop=c.body.scrollHeight;let B=a("div","kp-loading",n.thinking);c.body.appendChild(B),c.body.scrollTop=c.body.scrollHeight;try{let R=await st(H(),{message:h,chatId:s.chatId,knowledgeNames:await nn(t),...t.rag.enableReferences!==void 0?{enableReferences:t.rag.enableReferences}:{}});B.isConnected&&B.remove(),G(c.body,"bot",R.answer,{strings:n,view:c,userName:null,citations:R.citations??[],onShowSources:_}),s.historyLoadedChatId=null,await M(),R.suggestions?.length&&j(c,R.suggestions,async K=>{await E(K,c)})}catch(R){let K=U(t,R);B.isConnected&&B.remove(),G(c.body,"bot",`${n.requestFailed}: ${K.message}`,{strings:n,view:c,userName:null,onShowSources:_})}}async function Je(r){let c=s.fullPageOpen?L:A;await E(r,c)}async function jt(){s.fullPageOpen=!0,s.open=!1,z(),I(),f.classList.remove("open"),u.classList.remove("visible"),g.classList.add("hidden"),T.classList.add("open"),await M(),await le(L,s.chatId),queueMicrotask(()=>P.focus())}function Qe(){s.fullPageOpen&&(s.fullPageOpen=!1,T.classList.remove("open"),g.classList.remove("hidden"),re())}function Dt(){s.menuOpen=!0,b.classList.add("open"),x.classList.add("open")}function z(){s.menuOpen=!1,b.classList.remove("open"),x.classList.remove("open")}function Vt(){s.chatId=ce(t),s.historyLoadedChatId=null,I(),Y(A),j(A,t.initialSuggestions,async r=>{await E(r,A)}),z()}async function qt(){s.chatId=ce(t),s.historyLoadedChatId=null,Y(L),re(),j(L,t.initialSuggestions,async r=>{await E(r,L)}),N()}async function M(){if(!t.endpoints.listChats)return N(),ie(),[];s.loadingChats=!0,N(),ie();try{let r=await it(H());return s.chats=r,r}catch(r){return U(t,r),s.chats}finally{s.loadingChats=!1,N(),ie()}}async function _t(r){!t.endpoints.listChats&&!t.endpoints.createChat||s.chats.some(c=>c.chatId===s.chatId)||await lt(H(),s.chatId,r?ln(r,n.newChat):void 0)}async function Ft(r){s.chatId=r,s.historyLoadedChatId=null,await le(L,r),N()}async function Kt(r){s.chatId=r,s.historyLoadedChatId=null,await le(A,r),I()}async function Xt(){z(),await M(),s.myChatsOpen=!0,f.classList.add("kp-sheet-open"),ee.classList.add("open")}function I(){s.myChatsOpen=!1,f.classList.remove("kp-sheet-open"),ee.classList.remove("open")}async function Ze(r){if(!t.endpoints.updateChat)return;let c=window.prompt(n.renamePrompt,r.title)?.trim();if(!(!c||c===r.title))try{await Ce(H(),r.chatId,{title:c}),await M()}catch(h){U(t,h)}}async function et(r){if(t.endpoints.deleteChat)try{await pt(H(),r.chatId),s.chatId===r.chatId&&(s.chatId=ce(t),s.historyLoadedChatId=null,Y(A),Y(L)),await M()}catch(c){U(t,c)}}function N(){ht(Oe,We,s,n,async r=>{await Ft(r.chatId)},async r=>{await tt(r)},async r=>{await Ze(r)},async r=>{await et(r)})}function ie(){ht(Ie,Re,s,n,async r=>{await Kt(r.chatId)},async r=>{await tt(r)},async r=>{await Ze(r)},async r=>{await et(r)})}async function tt(r){if(t.endpoints.updateChat)try{await Ce(H(),r.chatId,{pinned:!r.pinned}),await M()}catch(c){U(t,c)}}async function le(r,c){Y(r),j(r,t.initialSuggestions,async B=>{await E(B,r)});let h=await rt(H(),c);return h.length>0&&(ft(r),bt(r.body,r.hero,r.suggestions),rn(r.body,h,{strings:n,view:r,userName:await Ye(),onShowSources:_})),s.historyLoadedChatId=c,h}let ye={open:Ge,close:F,toggle(){if(s.open){F();return}Ge()},destroy(){document.removeEventListener("keydown",nt),d.remove()},sendMessage:Je,setAccessTokenProvider(r){s.accessTokenProvider=r},getChatId(){return s.chatId},loadChats(){return M()},async loadHistory(){let r=s.fullPageOpen?L:A;return le(r,s.chatId)}};g.addEventListener("click",()=>ye.toggle()),Q.addEventListener("click",F),u.addEventListener("click",F),se.addEventListener("click",re),te.addEventListener("click",I),ne.addEventListener("click",I),b.addEventListener("click",r=>{if(r.stopPropagation(),!s.menuOpen){Dt();return}z()}),v.addEventListener("click",Vt),k.addEventListener("click",async()=>{await Xt()}),$.addEventListener("click",()=>{jt()}),oe.addEventListener("click",Qe),he.addEventListener("click",()=>{qt(),queueMicrotask(()=>P.focus())}),V.addEventListener("input",()=>{s.chatSearchTerm=V.value.trim().toLowerCase(),N()}),f.addEventListener("click",r=>{let c=r.target;if(!(c instanceof Element)||!c.closest(".kp-chat-actions")){for(let h of Array.from(l.querySelectorAll(".kp-chat-actions.open")))h.classList.remove("open");for(let h of Array.from(l.querySelectorAll(".kp-full-page-chat-item.menu-open")))h.classList.remove("menu-open")}s.menuOpen&&!x.contains(c)&&!b.contains(c)&&z(),r.stopPropagation()}),l.addEventListener("click",r=>{let c=r.target;if(s.menuOpen&&c instanceof Node&&!x.contains(c)&&!b.contains(c)&&z(),c instanceof Element&&!c.closest(".kp-chat-actions")){for(let h of Array.from(l.querySelectorAll(".kp-chat-actions.open")))h.classList.remove("open");for(let h of Array.from(l.querySelectorAll(".kp-full-page-chat-item.menu-open")))h.classList.remove("menu-open")}}),ge.addEventListener("submit",async r=>{r.preventDefault(),await Je(S.value)}),ke.addEventListener("submit",async r=>{r.preventDefault(),await E(P.value,L)});function nt(r){if(r.key==="Escape"){if(s.sourcePanelOpen){re();return}if(s.myChatsOpen){I();return}if(s.fullPageOpen){Qe();return}s.open&&F()}}return document.addEventListener("keydown",nt),ye}async function nn(e){if(e.rag.getKnowledgeNames){let t=await e.rag.getKnowledgeNames();return Array.isArray(t)?t.filter(Boolean):[]}return(e.rag.knowledgeNames??[]).filter(Boolean)}function ce(e){return e.rag.chatId?.trim()?e.rag.chatId:e.rag.chatIdFactory?e.rag.chatIdFactory():typeof crypto<"u"&&typeof crypto.randomUUID=="function"?crypto.randomUUID():`kp-chat-${Date.now()}-${Math.random().toString(36).slice(2,10)}`}function G(e,t,o,n){let i=t==="bot"?un(o,n.citations??[]):{displayText:o,citations:n.citations??[]},s=a("div",`kp-message-row ${t}`),d=an(t==="bot"?n.strings.assistantAvatar:n.userName??n.strings.userAvatar,t),l=a("div",`kp-bubble ${t}`),p=a("div","kp-bubble-content");vn(p,i.displayText),l.appendChild(p);let u=i.citations;if(u.length){let g=a("div","kp-meta",n.strings.citationsAttached(u.length));l.appendChild(g);let f=a("div","kp-source-preview"),m=a("div","kp-source-preview-title",n.strings.sourcesUsed),y=a("div","kp-source-preview-list");for(let x of u.slice(0,2)){let v=bn(x,n.strings);v.addEventListener("click",async()=>{xt(x,n.strings)}),y.appendChild(v)}let b=kn(n.strings);b.addEventListener("click",async()=>{n.onShowSources(u,n.strings.allSourcesUsed)}),y.appendChild(b),f.append(m,y),l.appendChild(f)}return t==="bot"&&l.appendChild(on(i.displayText,n.strings)),t==="user"?s.append(l,d):s.append(d,l),e.appendChild(s),e.scrollTop=e.scrollHeight,s}function on(e,t){let o=a("div","kp-message-actions"),n=a("button","kp-message-action",t.copy);n.type="button",n.addEventListener("click",async()=>{try{await navigator.clipboard.writeText(e),n.textContent=t.copied,window.setTimeout(()=>{n.textContent=t.copy},1200)}catch{n.textContent=t.copy}});let i=a("button","kp-message-action","\u{1F44D}");i.type="button",i.setAttribute("aria-label",t.helpful),i.addEventListener("click",()=>{i.classList.toggle("active"),s.classList.remove("active")});let s=a("button","kp-message-action","\u{1F44E}");return s.type="button",s.setAttribute("aria-label",t.notHelpful),s.addEventListener("click",()=>{s.classList.toggle("active"),i.classList.remove("active")}),o.append(n,i,s),o}function an(e,t){let o=a("div",`kp-avatar ${t}`),n=t==="bot"?"\u2726":dn(e);return o.textContent=n,o.setAttribute("aria-hidden","true"),o}function sn(e,t,o){e.textContent="";for(let n of t){let i=a("button","kp-suggestion",n);i.type="button",i.addEventListener("click",async()=>{await o(n)}),e.appendChild(i)}}function j(e,t,o){sn(e.suggestions,t,async n=>{e.input.value=n,await o(n)})}function bt(e,t,o){let n=new Set([t,o]);for(let i of Array.from(e.children))n.has(i)||i.remove()}function ft(e){e.body.classList.add("kp-conversation-active"),e.hero.remove(),e.suggestions.remove()}function Y(e){e.body.classList.remove("kp-conversation-active"),e.hero.isConnected||e.body.prepend(e.hero),e.suggestions.isConnected||e.body.appendChild(e.suggestions),bt(e.body,e.hero,e.suggestions),e.input.value=""}function rn(e,t,o){for(let n of t)G(e,n.role==="assistant"?"bot":"user",n.text,o)}function ht(e,t,o,n,i,s,d,l){if(e.textContent="",t.textContent="",o.loadingChats){e.appendChild(a("div","kp-full-page-empty",n.loadingChats));return}let p=o.chats.filter(u=>o.chatSearchTerm?u.title.toLowerCase().includes(o.chatSearchTerm):!0);if(p.length>0){let u=p.filter(f=>f.pinned),g=p.filter(f=>!f.pinned).slice(0,8);mt(e,g,o.chatId,n,i,s,d,l),mt(t,u,o.chatId,n,i,s,d,l),g.length===0&&e.appendChild(a("div","kp-full-page-empty",n.noRecentChats)),u.length===0&&t.appendChild(a("div","kp-full-page-empty",n.noPinnedChats));return}e.appendChild(a("div","kp-full-page-empty",n.noChats)),t.appendChild(a("div","kp-full-page-empty",n.noPinnedChats))}function mt(e,t,o,n,i,s,d,l){for(let p of t){let u=a("div",`kp-full-page-item kp-full-page-chat-item${p.chatId===o?" active":""}`),g=a("span","kp-full-page-item-title",p.title),f=a("div","kp-chat-actions"),m=a("button","kp-chat-actions-trigger","\u22EF");m.type="button",m.setAttribute("aria-label",n.chatActions);let y=a("div","kp-chat-actions-menu"),b=a("button","kp-chat-actions-item",p.pinned?n.unpinChat:n.pinChat);b.type="button",b.addEventListener("click",async k=>{k.stopPropagation(),await s(p)});let x=a("button","kp-chat-actions-item",n.renameChat);x.type="button",x.addEventListener("click",async k=>{k.stopPropagation(),await d(p)});let v=a("button","kp-chat-actions-item",n.deleteChat);v.type="button",v.addEventListener("click",async k=>{k.stopPropagation(),await l(p)}),y.append(b,x,v),f.append(m,y),m.addEventListener("click",k=>{k.stopPropagation();let $=f.classList.contains("open");for(let C of Array.from(e.querySelectorAll(".kp-chat-actions.open")))C.classList.remove("open");for(let C of Array.from(e.querySelectorAll(".kp-full-page-chat-item.menu-open")))C.classList.remove("menu-open");$||(f.classList.add("open"),u.classList.add("menu-open"))}),u.append(g,f),u.setAttribute("role","button"),u.tabIndex=0,u.addEventListener("click",async()=>{await i(p)}),u.addEventListener("keydown",async k=>{(k.key==="Enter"||k.key===" ")&&(k.preventDefault(),await i(p))}),u.addEventListener("blur",()=>{f.classList.remove("open"),u.classList.remove("menu-open")}),e.appendChild(u)}}function ln(e,t){return e.trim().slice(0,60)||t}function Ae(e){return e.toLowerCase().split("-")[0]||"en"}function pn(e){let t=gt.en;return gt[Ae(e)]??t}function cn(e){return["ar","fa","he","ur"].includes(Ae(e))}function dn(e){let t=e.split(/\s+/).filter(Boolean).slice(0,2);return t.length===0?"U":t.map(o=>o[0]?.toUpperCase()??"").join("")}function kt(e){return e.knowledgeName?.trim()||e.sourceDocument?.trim()||null}function un(e,t){let o=gn(e);return{displayText:o.displayText,citations:t.length>0?mn(t,o.citations):o.citations}}function gn(e){let o=yt(e).split(`
|
|
1377
|
-
`),n=-1;for(let
|
|
1378
|
-
`).trimEnd(),
|
|
1379
|
-
`).trim(),
|
|
1380
|
-
`)){let i=n.trim();if(i){if(/^\d+\.\s+/.test(i)){
|
|
1432
|
+
`}var ft={en:{openChatActions:"Open chat actions",newChat:"New Chat",myChats:"My Chats",openAssistant:"Open Knowledge Assistant",back:"Back",close:"Close",assistantBadge:"Knowledge Assistant",closeAssistantPage:"Close knowledge assistant page",searchChat:"Search Chat",recentActivity:"Recent Activity",pinnedCollections:"Pinned Collections",answersBasedOnPermissions:"Answers are generated based on your access permissions",authTokenForwarded:"Auth token is forwarded from the host app when configured.",thinking:"Thinking...",unableToCreateChat:"Unable to create chat",requestFailed:"Request failed",noRecentChats:"No recent chats yet.",noPinnedChats:"No pinned chats yet.",noChats:"No chats yet.",loadingChats:"Loading chats...",pinChat:"Pin chat",unpinChat:"Unpin chat",renameChat:"Rename",deleteChat:"Delete",chatActions:"Chat actions",renamePrompt:"Enter a new chat name",citationsAttached:e=>`${e} citation${e>1?"s":""} attached`,sourcesUsed:"Sources Used",allSourcesUsed:"All Sources Used",documentsAndReferences:"AI documents and references",showAll:"Show All",noSources:"No sources were returned for this answer.",closeSourcesPanel:"Close sources panel",openSource:"Open source",sourceScore:"Score",sourcePage:"Page",sourceSheet:"Sheet",sourceRow:"Row",sourceKnowledge:"Knowledge Base",untitledSource:"Untitled Source",copy:"Copy",copied:"Copied",helpful:"Helpful",notHelpful:"Needs work",send:"Send message",assistantAvatar:"Assistant",userAvatar:"User"},ar:{openChatActions:"\u0641\u062A\u062D \u0625\u062C\u0631\u0627\u0621\u0627\u062A \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0629",newChat:"\u0645\u062D\u0627\u062F\u062B\u0629 \u062C\u062F\u064A\u062F\u0629",myChats:"\u0645\u062D\u0627\u062F\u062B\u0627\u062A\u064A",openAssistant:"\u0641\u062A\u062D \u0645\u0633\u0627\u0639\u062F \u0627\u0644\u0645\u0639\u0631\u0641\u0629",back:"\u0631\u062C\u0648\u0639",close:"\u0625\u063A\u0644\u0627\u0642",assistantBadge:"\u0645\u0633\u0627\u0639\u062F \u0627\u0644\u0645\u0639\u0631\u0641\u0629",closeAssistantPage:"\u0625\u063A\u0644\u0627\u0642 \u0635\u0641\u062D\u0629 \u0645\u0633\u0627\u0639\u062F \u0627\u0644\u0645\u0639\u0631\u0641\u0629",searchChat:"\u0627\u0628\u062D\u062B \u0641\u064A \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0627\u062A",recentActivity:"\u0627\u0644\u0646\u0634\u0627\u0637 \u0627\u0644\u0623\u062E\u064A\u0631",pinnedCollections:"\u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0627\u062A \u0627\u0644\u0645\u062B\u0628\u062A\u0629",answersBasedOnPermissions:"\u064A\u062A\u0645 \u0625\u0646\u0634\u0627\u0621 \u0627\u0644\u0625\u062C\u0627\u0628\u0627\u062A \u0628\u0646\u0627\u0621\u064B \u0639\u0644\u0649 \u0635\u0644\u0627\u062D\u064A\u0627\u062A \u0627\u0644\u0648\u0635\u0648\u0644 \u0627\u0644\u062E\u0627\u0635\u0629 \u0628\u0643",authTokenForwarded:"\u064A\u062A\u0645 \u062A\u0645\u0631\u064A\u0631 \u0631\u0645\u0632 \u0627\u0644\u062A\u062D\u0642\u0642 \u0645\u0646 \u0627\u0644\u062A\u0637\u0628\u064A\u0642 \u0627\u0644\u0645\u0636\u064A\u0641 \u0639\u0646\u062F \u0627\u0644\u0625\u0639\u062F\u0627\u062F.",thinking:"\u062C\u0627\u0631\u064D \u0627\u0644\u062A\u0641\u0643\u064A\u0631...",unableToCreateChat:"\u062A\u0639\u0630\u0631 \u0625\u0646\u0634\u0627\u0621 \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0629",requestFailed:"\u0641\u0634\u0644 \u0627\u0644\u0637\u0644\u0628",noRecentChats:"\u0644\u0627 \u062A\u0648\u062C\u062F \u0645\u062D\u0627\u062F\u062B\u0627\u062A \u062D\u062F\u064A\u062B\u0629 \u0628\u0639\u062F.",noPinnedChats:"\u0644\u0627 \u062A\u0648\u062C\u062F \u0645\u062D\u0627\u062F\u062B\u0627\u062A \u0645\u062B\u0628\u062A\u0629 \u0628\u0639\u062F.",noChats:"\u0644\u0627 \u062A\u0648\u062C\u062F \u0645\u062D\u0627\u062F\u062B\u0627\u062A \u0628\u0639\u062F.",loadingChats:"\u062C\u0627\u0631\u064D \u062A\u062D\u0645\u064A\u0644 \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0627\u062A...",pinChat:"\u062A\u062B\u0628\u064A\u062A \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0629",unpinChat:"\u0625\u0644\u063A\u0627\u0621 \u062A\u062B\u0628\u064A\u062A \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0629",renameChat:"\u0625\u0639\u0627\u062F\u0629 \u062A\u0633\u0645\u064A\u0629",deleteChat:"\u062D\u0630\u0641",chatActions:"\u0625\u062C\u0631\u0627\u0621\u0627\u062A \u0627\u0644\u0645\u062D\u0627\u062F\u062B\u0629",renamePrompt:"\u0623\u062F\u062E\u0644 \u0627\u0633\u0645\u064B\u0627 \u062C\u062F\u064A\u062F\u064B\u0627 \u0644\u0644\u0645\u062D\u0627\u062F\u062B\u0629",citationsAttached:e=>`\u062A\u0645 \u0625\u0631\u0641\u0627\u0642 ${e} \u0645\u0631\u062C\u0639${e>1?"\u0627\u062A":""}`,sourcesUsed:"\u0627\u0644\u0645\u0635\u0627\u062F\u0631 \u0627\u0644\u0645\u0633\u062A\u062E\u062F\u0645\u0629",allSourcesUsed:"\u0643\u0644 \u0627\u0644\u0645\u0635\u0627\u062F\u0631 \u0627\u0644\u0645\u0633\u062A\u062E\u062F\u0645\u0629",documentsAndReferences:"\u0645\u0633\u062A\u0646\u062F\u0627\u062A \u0648\u0645\u0631\u0627\u062C\u0639 \u0627\u0644\u0630\u0643\u0627\u0621 \u0627\u0644\u0627\u0635\u0637\u0646\u0627\u0639\u064A",showAll:"\u0639\u0631\u0636 \u0627\u0644\u0643\u0644",noSources:"\u0644\u0645 \u064A\u062A\u0645 \u0625\u0631\u062C\u0627\u0639 \u0645\u0635\u0627\u062F\u0631 \u0644\u0647\u0630\u0647 \u0627\u0644\u0625\u062C\u0627\u0628\u0629.",closeSourcesPanel:"\u0625\u063A\u0644\u0627\u0642 \u0644\u0648\u062D\u0629 \u0627\u0644\u0645\u0635\u0627\u062F\u0631",openSource:"\u0641\u062A\u062D \u0627\u0644\u0645\u0635\u062F\u0631",sourceScore:"\u0627\u0644\u062F\u0631\u062C\u0629",sourcePage:"\u0627\u0644\u0635\u0641\u062D\u0629",sourceSheet:"\u0627\u0644\u0648\u0631\u0642\u0629",sourceRow:"\u0627\u0644\u0635\u0641",sourceKnowledge:"\u0642\u0627\u0639\u062F\u0629 \u0627\u0644\u0645\u0639\u0631\u0641\u0629",untitledSource:"\u0645\u0635\u062F\u0631 \u0628\u062F\u0648\u0646 \u0639\u0646\u0648\u0627\u0646",copy:"\u0646\u0633\u062E",copied:"\u062A\u0645 \u0627\u0644\u0646\u0633\u062E",helpful:"\u0645\u0641\u064A\u062F",notHelpful:"\u064A\u062D\u062A\u0627\u062C \u062A\u062D\u0633\u064A\u0646",send:"\u0625\u0631\u0633\u0627\u0644 \u0627\u0644\u0631\u0633\u0627\u0644\u0629",assistantAvatar:"\u0627\u0644\u0645\u0633\u0627\u0639\u062F",userAvatar:"\u0627\u0644\u0645\u0633\u062A\u062E\u062F\u0645"}};function de(e){if(typeof document>"u")throw new Error("Chat widget can only be initialized in a browser.");let t=dt(e),a=Se(t.locale),n=dn(t.locale),i=un(a),l=t.displayMode==="embedded",s={chatId:ce(t),open:!1,fullPageOpen:l,myChatsOpen:!1,accessTokenProvider:t.getAccessToken,historyLoadedChatId:null,menuOpen:!1,chats:[],chatSearchTerm:"",loadingChats:!1,sourcePanelOpen:!1,sourcePanelTitle:null},p=document.createElement("div");p.dataset.chatWidgetHost="true",t.mount.appendChild(p);let c=p.attachShadow({mode:"open"});gt(c,t.theme);let u=o("div",`kp-chat-widget ${t.position}`);u.lang=a,u.dir=i?"rtl":"ltr",l&&(u.classList.add("kp-chat-widget-embedded"),kt(!0)),i&&u.classList.add("kp-rtl");let g=o("div","kp-overlay"),f=o("button","kp-launcher");f.type="button",f.setAttribute("aria-label",t.launcherAriaLabel),f.innerHTML=['<span class="kp-star-cluster" aria-hidden="true">','<span class="kp-star orbit-a">\u2726</span>','<span class="kp-star orbit-b">\u2726</span>','<span class="kp-star orbit-c">\u2726</span>','<span class="kp-star main">\u2726</span>',"</span>"].join("");let h=o("section","kp-panel");h.setAttribute("role","dialog"),h.setAttribute("aria-modal","true"),h.setAttribute("aria-label",t.title);let w=o("div","kp-header"),L=o("div","kp-toolbar"),b=o("button","kp-tool-button kp-menu-trigger");b.type="button",b.setAttribute("aria-label",n.openChatActions),b.innerHTML=['<span class="kp-pencil-icon" aria-hidden="true"></span>','<span class="kp-chevron" aria-hidden="true">\u2304</span>'].join("");let y=o("div","kp-dropdown"),k=o("button","kp-dropdown-item",n.newChat);k.type="button";let M=o("button","kp-dropdown-item",n.myChats);M.type="button";let v=o("button","kp-dropdown-item",n.openAssistant);v.type="button",y.append(k,M,v),L.append(b,y);let Te=o("div","kp-title-wrap"),Pt=o("h2","kp-title",t.title),St=o("div","kp-subtitle",t.subtitle);Te.append(Pt,St);let ee=o("button","kp-close","\xD7");ee.type="button",ee.setAttribute("aria-label",t.closeAriaLabel),w.append(L,Te,ee);let te=o("div","kp-body"),ue=o("div","kp-hero"),Tt=o("div","kp-hero-icon","\u2726"),Et=o("div","kp-hero-text",t.welcomeMessage);ue.append(Tt,Et);let Ee=o("div","kp-footer"),ge=o("form","kp-form"),E=o("input","kp-input");E.type="text",E.autocomplete="off",E.placeholder=t.inputPlaceholder,E.setAttribute("aria-label",t.inputPlaceholder);let fe=o("button","kp-send","\u279C");fe.type="submit",fe.setAttribute("aria-label",n.send);let It=o("div","kp-note",n.authTokenForwarded);ge.append(E,fe),Ee.append(ge,It),h.append(w,te,Ee),l||u.append(g,f,h),c.appendChild(u),te.appendChild(ue);let Ie=o("div","kp-suggestions");te.appendChild(Ie);let ne=o("section","kp-my-chats-sheet"),Re=o("div","kp-my-chats-header"),ae=o("button","kp-my-chats-nav","\u2190");ae.type="button",ae.setAttribute("aria-label",n.back);let oe=o("button","kp-my-chats-nav kp-my-chats-close","\xD7");oe.type="button",oe.setAttribute("aria-label",n.close),Re.append(ae,oe);let $e=o("div","kp-my-chats-body"),Rt=o("div","kp-my-chats-section-label",n.recentActivity),He=o("div","kp-my-chats-list"),$t=o("div","kp-my-chats-section-label",n.pinnedCollections),Me=o("div","kp-my-chats-list");$e.append(Rt,He,$t,Me),ne.append(Re,$e),h.appendChild(ne);let S={body:te,input:E,suggestions:Ie,hero:ue,kind:"panel"},A=o("section","kp-full-page");l&&A.classList.add("kp-full-page-embedded","open"),A.setAttribute("role","dialog"),l||A.setAttribute("aria-modal","true"),A.setAttribute("aria-label",`${t.title} page`);let ze=o("div","kp-full-page-shell"),he=o("div","kp-full-page-header"),Ne=o("div","kp-full-page-brand"),Ht=o("div","kp-full-page-brand-mark","\u2726"),Mt=o("div","kp-full-page-brand-text",t.title);Ne.append(Ht,Mt);let Oe=o("div","kp-full-page-header-actions"),zt=o("div","kp-full-page-badge",n.assistantBadge),se=o("button","kp-full-page-close","\xD7");se.type="button",se.setAttribute("aria-label",n.closeAssistantPage),Oe.append(zt,se),he.append(Ne,Oe),t.embedded.showHeader||he.classList.add("kp-hidden");let Ue=o("div","kp-full-page-content"),Be=o("aside","kp-full-page-sidebar"),me=o("button","kp-full-page-new-chat",`+ ${n.newChat}`);me.type="button";let We=o("div","kp-full-page-search"),F=o("input","kp-full-page-search-input");F.type="search",F.placeholder=n.searchChat;let Nt=o("span","kp-full-page-search-icon","\u2315");We.append(F,Nt);let Ot=o("div","kp-full-page-section-label",n.recentActivity),je=o("div","kp-full-page-recent-list"),Ut=o("div","kp-full-page-section-label",n.pinnedCollections),De=o("div","kp-full-page-pinned-list");Be.append(me,We,Ot,je,Ut,De);let qe=o("main","kp-full-page-main"),Ve=o("section","kp-full-page-panel"),be=o("div","kp-full-page-body"),ke=o("div","kp-full-page-hero"),_e=o("div","kp-full-page-hero-badge");_e.innerHTML=['<span class="kp-star-cluster kp-star-cluster-static" aria-hidden="true">','<span class="kp-star orbit-a">\u2726</span>','<span class="kp-star orbit-b">\u2726</span>','<span class="kp-star orbit-c">\u2726</span>','<span class="kp-star main">\u2726</span>',"</span>"].join("");let Bt=o("div","kp-full-page-hero-text",t.welcomeMessage);ke.append(_e,Bt);let Fe=o("div","kp-suggestions kp-full-page-suggestions");be.append(ke,Fe);let Ke=o("div","kp-full-page-footer"),xe=o("form","kp-form kp-full-page-form"),P=o("input","kp-input kp-full-page-input");P.type="text",P.autocomplete="off",P.placeholder=t.inputPlaceholder,P.setAttribute("aria-label",t.inputPlaceholder);let ye=o("button","kp-send kp-full-page-send","\u279C");ye.type="submit",ye.setAttribute("aria-label",n.send);let Wt=o("div","kp-note kp-full-page-note",n.answersBasedOnPermissions);xe.append(P,ye),Ke.append(xe,Wt),Ve.append(be,Ke),qe.appendChild(Ve);let re=o("aside","kp-source-panel"),Xe=o("div","kp-source-panel-header"),Ye=o("div","kp-source-panel-title-wrap"),Ge=o("div","kp-source-panel-title",n.allSourcesUsed),jt=o("div","kp-source-panel-subtitle",n.documentsAndReferences);Ye.append(Ge,jt);let ie=o("button","kp-source-panel-close","\xD7");ie.type="button",ie.setAttribute("aria-label",n.closeSourcesPanel),Xe.append(Ye,ie);let K=o("div","kp-source-panel-list"),Dt=o("div","kp-source-panel-empty",n.noSources);K.appendChild(Dt),re.append(Xe,K),Ue.append(Be,qe,re),ze.append(he,Ue),A.appendChild(ze),u.appendChild(A);let C={body:be,input:P,suggestions:Fe,hero:ke,kind:"full-page"},z=()=>({...t,getAccessToken:s.accessTokenProvider}),Je=async()=>{if(!t.getUserContext)return null;try{let r=await t.getUserContext();return r?.displayName?.trim()||r?.email?.trim()||r?.userId?.trim()||null}catch{return null}},X=(r,d)=>{if(s.sourcePanelOpen=!0,s.sourcePanelTitle=d??n.allSourcesUsed,Ge.textContent=s.sourcePanelTitle,re.classList.add("open"),K.textContent="",r.length===0){K.appendChild(o("div","kp-source-panel-empty",n.noSources));return}for(let m of r)K.appendChild(wn(m,n))},W=()=>{s.sourcePanelOpen=!1,s.sourcePanelTitle=null,re.classList.remove("open")};V(S,t.initialSuggestions,async r=>{await I(r,S)}),V(C,t.initialSuggestions,async r=>{await I(r,C)}),O(),le(),l&&(T(),t.rag.loadHistoryOnOpen&&j(C,s.chatId));function we(){if(l){s.fullPageOpen=!0,A.classList.add("open");return}s.open||(s.open=!0,s.fullPageOpen=!1,$(),A.classList.remove("open"),f.classList.add("hidden"),g.classList.add("visible"),h.classList.add("open"),t.onOpen?.(),t.rag.loadHistoryOnOpen&&ve.loadHistory(),queueMicrotask(()=>E.focus()))}function N(){if(l){W();return}s.open&&(R(),$(),s.open=!1,f.classList.remove("hidden"),g.classList.remove("visible"),h.classList.remove("open"),t.onClose?.())}async function I(r,d){let m=r.trim();if(!m)return;d.input.value="";try{await Kt(m)}catch(H){let Y=B(t,H);Q(d.body,"bot",`${n.unableToCreateChat}: ${Y.message}`,{strings:n,view:d,userName:null,onShowSources:X});return}ht(d),Q(d.body,"user",m,{strings:n,view:d,userName:await Je(),onShowSources:X}),d.body.scrollTop=d.body.scrollHeight;let U=o("div","kp-loading",n.thinking);d.body.appendChild(U),d.body.scrollTop=d.body.scrollHeight;try{let H=await rt(z(),{message:m,chatId:s.chatId,knowledgeNames:await on(t),...t.rag.enableReferences!==void 0?{enableReferences:t.rag.enableReferences}:{}});U.isConnected&&U.remove(),Q(d.body,"bot",H.answer,{strings:n,view:d,userName:null,citations:H.citations??[],onShowSources:X}),s.historyLoadedChatId=null,await T(),H.suggestions?.length&&V(d,H.suggestions,async Y=>{await I(Y,d)})}catch(H){let Y=B(t,H);U.isConnected&&U.remove(),Q(d.body,"bot",`${n.requestFailed}: ${Y.message}`,{strings:n,view:d,userName:null,onShowSources:X})}}async function Qe(r){let d=s.fullPageOpen?C:S;await I(r,d)}async function qt(){if(l){s.fullPageOpen=!0,A.classList.add("open"),await T(),await j(C,s.chatId),queueMicrotask(()=>P.focus());return}s.fullPageOpen=!0,s.open=!1,R(),$(),h.classList.remove("open"),g.classList.remove("visible"),f.classList.add("hidden"),A.classList.add("open"),await T(),await j(C,s.chatId),queueMicrotask(()=>P.focus())}function Ze(){if(l){W();return}s.fullPageOpen&&(s.fullPageOpen=!1,A.classList.remove("open"),f.classList.remove("hidden"),W())}function Vt(){s.menuOpen=!0,b.classList.add("open"),y.classList.add("open")}function R(){s.menuOpen=!1,b.classList.remove("open"),y.classList.remove("open")}function _t(){s.chatId=ce(t),s.historyLoadedChatId=null,$(),J(S),V(S,t.initialSuggestions,async r=>{await I(r,S)}),R()}async function Ft(){s.chatId=ce(t),s.historyLoadedChatId=null,J(C),W(),V(C,t.initialSuggestions,async r=>{await I(r,C)}),O()}async function T(){if(!t.endpoints.listChats)return O(),le(),[];s.loadingChats=!0,O(),le();try{let r=await lt(z());return s.chats=r,r}catch(r){return B(t,r),s.chats}finally{s.loadingChats=!1,O(),le()}}async function Kt(r){!t.endpoints.listChats&&!t.endpoints.createChat||s.chats.some(d=>d.chatId===s.chatId)||await pt(z(),s.chatId,r?cn(r,n.newChat):void 0)}async function Xt(r){s.chatId=r,s.historyLoadedChatId=null,await j(C,r),O()}async function Yt(r){s.chatId=r,s.historyLoadedChatId=null,await j(S,r),$()}async function Gt(){R(),await T(),s.myChatsOpen=!0,h.classList.add("kp-sheet-open"),ne.classList.add("open")}function $(){s.myChatsOpen=!1,h.classList.remove("kp-sheet-open"),ne.classList.remove("open")}async function et(r){if(!t.endpoints.updateChat)return;let d=window.prompt(n.renamePrompt,r.title)?.trim();if(!(!d||d===r.title))try{await Ae(z(),r.chatId,{title:d}),await T()}catch(m){B(t,m)}}async function tt(r){if(t.endpoints.deleteChat)try{await ct(z(),r.chatId),s.chatId===r.chatId&&(s.chatId=ce(t),s.historyLoadedChatId=null,J(S),J(C)),await T()}catch(d){B(t,d)}}function O(){mt(je,De,s,n,async r=>{await Xt(r.chatId)},async r=>{await nt(r)},async r=>{await et(r)},async r=>{await tt(r)})}function le(){mt(He,Me,s,n,async r=>{await Yt(r.chatId)},async r=>{await nt(r)},async r=>{await et(r)},async r=>{await tt(r)})}async function nt(r){if(t.endpoints.updateChat)try{await Ae(z(),r.chatId,{pinned:!r.pinned}),await T()}catch(d){B(t,d)}}async function j(r,d){J(r),V(r,t.initialSuggestions,async U=>{await I(U,r)});let m=await it(z(),d);return m.length>0&&(ht(r),xt(r.body,r.hero,r.suggestions),pn(r.body,m,{strings:n,view:r,userName:await Je(),onShowSources:X})),s.historyLoadedChatId=d,m}let ve={open:we,close:N,toggle(){if(l){we();return}if(s.open){N();return}we()},destroy(){if(document.removeEventListener("keydown",at),p.remove(),l){let r=!1;document.querySelectorAll("[data-chat-widget-host]").forEach(d=>{let m=d.shadowRoot;m&&m.querySelector(".kp-chat-widget-embedded")&&(r=!0)}),r||kt(!1)}},sendMessage:Qe,setAccessTokenProvider(r){s.accessTokenProvider=r},getChatId(){return s.chatId},loadChats(){return T()},async loadHistory(){let r=s.fullPageOpen?C:S;return j(r,s.chatId)}};f.addEventListener("click",()=>ve.toggle()),ee.addEventListener("click",N),g.addEventListener("click",N),ie.addEventListener("click",W),ae.addEventListener("click",$),oe.addEventListener("click",$),b.addEventListener("click",r=>{if(r.stopPropagation(),!s.menuOpen){Vt();return}R()}),k.addEventListener("click",_t),M.addEventListener("click",async()=>{await Gt()}),v.addEventListener("click",()=>{if(R(),t.onOpenAssistantPage){N(),t.onOpenAssistantPage();return}if(t.assistantPageUrl){N(),window.location.href=t.assistantPageUrl;return}qt()}),se.addEventListener("click",Ze),me.addEventListener("click",()=>{Ft(),queueMicrotask(()=>P.focus())}),F.addEventListener("input",()=>{s.chatSearchTerm=F.value.trim().toLowerCase(),O()}),h.addEventListener("click",r=>{let d=r.target;if(!(d instanceof Element)||!d.closest(".kp-chat-actions")){for(let m of Array.from(c.querySelectorAll(".kp-chat-actions.open")))m.classList.remove("open");for(let m of Array.from(c.querySelectorAll(".kp-full-page-chat-item.menu-open")))m.classList.remove("menu-open")}s.menuOpen&&!y.contains(d)&&!b.contains(d)&&R(),r.stopPropagation()}),c.addEventListener("click",r=>{let d=r.target;if(s.menuOpen&&d instanceof Node&&!y.contains(d)&&!b.contains(d)&&R(),d instanceof Element&&!d.closest(".kp-chat-actions")){for(let m of Array.from(c.querySelectorAll(".kp-chat-actions.open")))m.classList.remove("open");for(let m of Array.from(c.querySelectorAll(".kp-full-page-chat-item.menu-open")))m.classList.remove("menu-open")}}),ge.addEventListener("submit",async r=>{r.preventDefault(),await Qe(E.value)}),xe.addEventListener("submit",async r=>{r.preventDefault(),await I(P.value,C)});function at(r){if(r.key==="Escape"){if(s.sourcePanelOpen){W();return}if(s.myChatsOpen){$();return}if(s.fullPageOpen){if(l)return;Ze();return}s.open&&N()}}return document.addEventListener("keydown",at),ve}async function on(e){if(e.rag.getKnowledgeNames){let t=await e.rag.getKnowledgeNames();return Array.isArray(t)?t.filter(Boolean):[]}return(e.rag.knowledgeNames??[]).filter(Boolean)}function ce(e){return e.rag.chatId?.trim()?e.rag.chatId:e.rag.chatIdFactory?e.rag.chatIdFactory():typeof crypto<"u"&&typeof crypto.randomUUID=="function"?crypto.randomUUID():`kp-chat-${Date.now()}-${Math.random().toString(36).slice(2,10)}`}function Q(e,t,a,n){let i=t==="bot"?fn(a,n.citations??[]):{displayText:a,citations:n.citations??[]},l=o("div",`kp-message-row ${t}`),s=rn(t==="bot"?n.strings.assistantAvatar:n.userName??n.strings.userAvatar,t),p=o("div",`kp-bubble ${t}`),c=o("div","kp-bubble-content");Ln(c,i.displayText),p.appendChild(c);let u=i.citations;if(u.length){let g=o("div","kp-meta",n.strings.citationsAttached(u.length));p.appendChild(g);let f=o("div","kp-source-preview"),h=o("div","kp-source-preview-title",n.strings.sourcesUsed),w=o("div","kp-source-preview-list");for(let b of u.slice(0,2)){let y=xn(b,n.strings);y.addEventListener("click",async()=>{wt(b,n.strings)}),w.appendChild(y)}let L=yn(n.strings);L.addEventListener("click",async()=>{n.onShowSources(u,n.strings.allSourcesUsed)}),w.appendChild(L),f.append(h,w),p.appendChild(f)}return t==="bot"&&p.appendChild(sn(i.displayText,n.strings)),t==="user"?l.append(p,s):l.append(s,p),e.appendChild(l),e.scrollTop=e.scrollHeight,l}function sn(e,t){let a=o("div","kp-message-actions"),n=o("button","kp-message-action",t.copy);n.type="button",n.addEventListener("click",async()=>{try{await navigator.clipboard.writeText(e),n.textContent=t.copied,window.setTimeout(()=>{n.textContent=t.copy},1200)}catch{n.textContent=t.copy}});let i=o("button","kp-message-action","\u{1F44D}");i.type="button",i.setAttribute("aria-label",t.helpful),i.addEventListener("click",()=>{i.classList.toggle("active"),l.classList.remove("active")});let l=o("button","kp-message-action","\u{1F44E}");return l.type="button",l.setAttribute("aria-label",t.notHelpful),l.addEventListener("click",()=>{l.classList.toggle("active"),i.classList.remove("active")}),a.append(n,i,l),a}function rn(e,t){let a=o("div",`kp-avatar ${t}`),n=t==="bot"?"\u2726":gn(e);return a.textContent=n,a.setAttribute("aria-hidden","true"),a}function ln(e,t,a){e.textContent="";for(let n of t){let i=o("button","kp-suggestion",n);i.type="button",i.addEventListener("click",async()=>{await a(n)}),e.appendChild(i)}}function V(e,t,a){ln(e.suggestions,t,async n=>{e.input.value=n,await a(n)})}function xt(e,t,a){let n=new Set([t,a]);for(let i of Array.from(e.children))n.has(i)||i.remove()}function ht(e){e.body.classList.add("kp-conversation-active"),e.hero.remove(),e.suggestions.remove()}function J(e){e.body.classList.remove("kp-conversation-active"),e.hero.isConnected||e.body.prepend(e.hero),e.suggestions.isConnected||e.body.appendChild(e.suggestions),xt(e.body,e.hero,e.suggestions),e.input.value=""}function pn(e,t,a){for(let n of t)Q(e,n.role==="assistant"?"bot":"user",n.text,a)}function mt(e,t,a,n,i,l,s,p){if(e.textContent="",t.textContent="",a.loadingChats){e.appendChild(o("div","kp-full-page-empty",n.loadingChats));return}let c=a.chats.filter(u=>a.chatSearchTerm?u.title.toLowerCase().includes(a.chatSearchTerm):!0);if(c.length>0){let u=c.filter(f=>f.pinned),g=c.filter(f=>!f.pinned).slice(0,8);bt(e,g,a.chatId,n,i,l,s,p),bt(t,u,a.chatId,n,i,l,s,p),g.length===0&&e.appendChild(o("div","kp-full-page-empty",n.noRecentChats)),u.length===0&&t.appendChild(o("div","kp-full-page-empty",n.noPinnedChats));return}e.appendChild(o("div","kp-full-page-empty",n.noChats)),t.appendChild(o("div","kp-full-page-empty",n.noPinnedChats))}function bt(e,t,a,n,i,l,s,p){for(let c of t){let u=o("div",`kp-full-page-item kp-full-page-chat-item${c.chatId===a?" active":""}`),g=o("span","kp-full-page-item-title",c.title),f=o("div","kp-chat-actions"),h=o("button","kp-chat-actions-trigger","\u22EF");h.type="button",h.setAttribute("aria-label",n.chatActions);let w=o("div","kp-chat-actions-menu"),L=o("button","kp-chat-actions-item",c.pinned?n.unpinChat:n.pinChat);L.type="button",L.addEventListener("click",async k=>{k.stopPropagation(),await l(c)});let b=o("button","kp-chat-actions-item",n.renameChat);b.type="button",b.addEventListener("click",async k=>{k.stopPropagation(),await s(c)});let y=o("button","kp-chat-actions-item",n.deleteChat);y.type="button",y.addEventListener("click",async k=>{k.stopPropagation(),await p(c)}),w.append(L,b,y),f.append(h,w),h.addEventListener("click",k=>{k.stopPropagation();let M=f.classList.contains("open");for(let v of Array.from(e.querySelectorAll(".kp-chat-actions.open")))v.classList.remove("open");for(let v of Array.from(e.querySelectorAll(".kp-full-page-chat-item.menu-open")))v.classList.remove("menu-open");M||(f.classList.add("open"),u.classList.add("menu-open"))}),u.append(g,f),u.setAttribute("role","button"),u.tabIndex=0,u.addEventListener("click",async()=>{await i(c)}),u.addEventListener("keydown",async k=>{(k.key==="Enter"||k.key===" ")&&(k.preventDefault(),await i(c))}),u.addEventListener("blur",()=>{f.classList.remove("open"),u.classList.remove("menu-open")}),e.appendChild(u)}}function cn(e,t){return e.trim().slice(0,60)||t}function Se(e){return e.toLowerCase().split("-")[0]||"en"}function dn(e){let t=ft.en;return ft[Se(e)]??t}function un(e){return["ar","fa","he","ur"].includes(Se(e))}function gn(e){let t=e.split(/\s+/).filter(Boolean).slice(0,2);return t.length===0?"U":t.map(a=>a[0]?.toUpperCase()??"").join("")}function yt(e){return e.knowledgeName?.trim()||e.sourceDocument?.trim()||null}function fn(e,t){let a=hn(e);return{displayText:a.displayText,citations:t.length>0?kn(t,a.citations):a.citations}}function hn(e){let a=vt(e).split(`
|
|
1433
|
+
`),n=-1;for(let c=0;c<a.length;c+=1)/^#{0,6}\s*References\s*$/i.test(a[c]?.trim()??"")&&(n=c);if(n===-1)return{displayText:e,citations:[]};let i=a.slice(0,n).join(`
|
|
1434
|
+
`).trimEnd(),l=a.slice(n+1).join(`
|
|
1435
|
+
`).trim(),p=mn(l).map(c=>bn(c)).filter(c=>!!c);return{displayText:i,citations:p}}function mn(e){let t=[],a="";for(let n of e.split(`
|
|
1436
|
+
`)){let i=n.trim();if(i){if(/^\d+\.\s+/.test(i)){a&&t.push(a.trim()),a=i.replace(/^\d+\.\s+/,"");continue}a&&(a=`${a} ${i}`)}}return a&&t.push(a.trim()),t}function bn(e){let t=e.match(/https?:\/\/\S+/i);if(!t)return null;let a=t[0],n=e.slice(0,t.index).replace(/[.\s]+$/,"").trim();return{sourceDocument:a,knowledgeName:n||a}}function kn(e,t){let a=[],n=new Set;for(let i of[...e,...t]){let l=`${i.knowledgeName??""}::${i.sourceDocument??""}`;n.has(l)||(n.add(l),a.push(i))}return a}function xn(e,t){let a=o("button","kp-source-chip");a.type="button",a.setAttribute("aria-label",t.openSource);let n=o("span","kp-source-thumb");n.textContent="\u2726";let i=o("span","kp-source-chip-label",yt(e)??t.untitledSource);return a.append(n,i),a}function yn(e){let t=o("button","kp-source-chip kp-source-chip-more");t.type="button";let a=o("span","kp-source-thumb-stack");for(let i=0;i<3;i+=1){let l=o("span","kp-source-thumb stacked");l.textContent="\u2726",a.appendChild(l)}let n=o("span","kp-source-chip-label",e.showAll);return t.append(a,n),t}function wn(e,t){let a=o("button","kp-source-card");a.type="button",a.setAttribute("aria-label",t.openSource),a.addEventListener("click",()=>{wt(e,t)});let n=o("div","kp-source-card-media"),i=o("span","kp-source-thumb kp-source-thumb-large");i.textContent="\u2726";let l=o("div","kp-source-card-title",yt(e)??t.untitledSource),s=o("div","kp-source-card-meta"),p=[];return typeof e.score=="number"&&p.push(`${t.sourceScore}: ${e.score.toFixed(2)}`),typeof e.pageNumber=="number"&&p.push(`${t.sourcePage}: ${e.pageNumber}`),e.sheetName&&p.push(`${t.sourceSheet}: ${e.sheetName}`),typeof e.rowNumber=="number"&&p.push(`${t.sourceRow}: ${e.rowNumber}`),e.knowledgeName&&p.push(`${t.sourceKnowledge}: ${e.knowledgeName}`),s.textContent=p.join(" \u2022 "),n.appendChild(i),a.append(n,l,s),a}function wt(e,t){let a=e.sourceDocument?.trim()||e.knowledgeName?.trim()||t.untitledSource,n=vn(e.sourceDocument);if(n){window.open(n,"_blank","noopener,noreferrer");return}let i=window.open("","_blank","noopener,noreferrer");if(!i)return;let l=Z(a),s=Cn(e,t).map(p=>`<li>${Z(p)}</li>`).join("");i.document.write(`<!doctype html>
|
|
1381
1437
|
<html lang="en">
|
|
1382
1438
|
<head>
|
|
1383
1439
|
<meta charset="utf-8">
|
|
1384
|
-
<title>${
|
|
1440
|
+
<title>${l}</title>
|
|
1385
1441
|
<style>
|
|
1386
1442
|
body { font-family: Arial, sans-serif; margin: 32px; color: #1f2937; }
|
|
1387
1443
|
.card { max-width: 720px; padding: 24px; border: 1px solid #dbe4ee; border-radius: 16px; background: #fff; }
|
|
@@ -1392,13 +1448,13 @@
|
|
|
1392
1448
|
</head>
|
|
1393
1449
|
<body>
|
|
1394
1450
|
<div class="card">
|
|
1395
|
-
<h1>${
|
|
1396
|
-
<p>${
|
|
1397
|
-
<ul>${
|
|
1451
|
+
<h1>${l}</h1>
|
|
1452
|
+
<p>${Z(t.documentsAndReferences)}</p>
|
|
1453
|
+
<ul>${s||`<li>${Z(t.noSources)}</li>`}</ul>
|
|
1398
1454
|
</div>
|
|
1399
1455
|
</body>
|
|
1400
|
-
</html>`),i.document.close()}function
|
|
1456
|
+
</html>`),i.document.close()}function vn(e){if(!e)return null;let t=e.trim();return/^https?:\/\//i.test(t)?t:null}function Cn(e,t){let a=[];return e.sourceDocument&&a.push(e.sourceDocument),typeof e.score=="number"&&a.push(`${t.sourceScore}: ${e.score.toFixed(2)}`),typeof e.pageNumber=="number"&&a.push(`${t.sourcePage}: ${e.pageNumber}`),e.sheetName&&a.push(`${t.sourceSheet}: ${e.sheetName}`),typeof e.rowNumber=="number"&&a.push(`${t.sourceRow}: ${e.rowNumber}`),e.knowledgeName&&a.push(`${t.sourceKnowledge}: ${e.knowledgeName}`),a}function Ln(e,t){e.innerHTML=An(vt(t))}function vt(e){return e.replace(/\r\n/g,`
|
|
1401
1457
|
`).replace(/<br\s*\/?>/gi,`
|
|
1402
|
-
`)}function
|
|
1403
|
-
`).map(n=>n.trimEnd());if(t.every(n=>/^\s*\|.*\|\s*$/.test(n))&&t.length>=2)return
|
|
1458
|
+
`)}function An(e){return e.split(/\n{2,}/).map(a=>a.trim()).filter(Boolean).map(Pn).join("")}function Pn(e){let t=e.split(`
|
|
1459
|
+
`).map(n=>n.trimEnd());if(t.every(n=>/^\s*\|.*\|\s*$/.test(n))&&t.length>=2)return Sn(t);if(t.every(n=>/^\d+\.\s+/.test(n)))return`<ol>${t.map(n=>`<li>${_(n.replace(/^\d+\.\s+/,""))}</li>`).join("")}</ol>`;if(t.every(n=>/^[-*]\s+/.test(n)))return`<ul>${t.map(n=>`<li>${_(n.replace(/^[-*]\s+/,""))}</li>`).join("")}</ul>`;let a=t[0]?.match(/^(#{1,6})\s+(.*)$/);if(a){let n=a[1]??"#",i=a[2]??"",l=n.length;return`<h${l}>${_(i)}</h${l}>`}return`<p>${t.map(n=>_(n)).join("<br>")}</p>`}function Sn(e){let t=e.filter((s,p)=>!(p===1&&/^\s*\|?(\s*:?-{3,}:?\s*\|)+\s*$/.test(s))).map(s=>Tn(s));if(t.length===0)return"";let a=t[0]??[],n=t.slice(1),i=`<thead><tr>${a.map(s=>`<th>${_(s)}</th>`).join("")}</tr></thead>`,l=n.length?`<tbody>${n.map(s=>`<tr>${s.map(p=>`<td>${_(p)}</td>`).join("")}</tr>`).join("")}</tbody>`:"";return`<div class="kp-table-wrap"><table>${i}${l}</table></div>`}function Tn(e){return e.trim().replace(/^\|/,"").replace(/\|$/,"").split("|").map(t=>t.trim())}function _(e){let t=Z(e);return t=t.replace(/\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)/g,'<a href="$2" target="_blank" rel="noreferrer">$1</a>'),t=t.replace(/`([^`]+)`/g,"<code>$1</code>"),t=t.replace(/\*\*([^*]+)\*\*/g,"<strong>$1</strong>"),t=t.replace(/\*([^*]+)\*/g,"<em>$1</em>"),t}function Z(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function kt(e){typeof document>"u"||document.querySelectorAll("[data-chat-widget-host]").forEach(t=>{let a=t.shadowRoot;if(a){let n=a.querySelector(".kp-chat-widget");n&&!n.classList.contains("kp-chat-widget-embedded")&&(t.style.display=e?"none":"")}})}var Ct="0.1.0",Lt=de,At={init:Lt,createChatWidget:de,version:Ct};typeof window<"u"&&(window.ChatWidget=At);0&&(module.exports={browserGlobal,createChatWidget,init,version});
|
|
1404
1460
|
//# sourceMappingURL=browser.cjs.map
|