@blueking/ai-ui-sdk 0.0.12-beta.1 → 0.0.12-beta.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/dist/hooks/use-chat.d.ts +1 -1
- package/dist/main.js +1 -1
- package/dist/types/type.d.ts +6 -4
- package/package.json +1 -1
package/dist/hooks/use-chat.d.ts
CHANGED
@@ -49,7 +49,7 @@ export declare const useChat: <T extends ISession = ISession>({ handleStart, han
|
|
49
49
|
url: string;
|
50
50
|
headers?: Record<string, string>;
|
51
51
|
}) => void;
|
52
|
-
sendChat: (content: BasicChatContent | ShortcutChatContent, callback?: () => void
|
52
|
+
sendChat: (content: BasicChatContent | ShortcutChatContent, callback?: () => void) => void;
|
53
53
|
stopChat: (sessionCode: string) => void;
|
54
54
|
plusSessionContent: (sessionCode: string, sessionContent: ISessionContent) => void;
|
55
55
|
updateSessionContent: (sessionContent: ISessionContent) => void;
|
package/dist/main.js
CHANGED
@@ -243,6 +243,6 @@ url(${D}) format("embedded-opentype");
|
|
243
243
|
&~ .knowledge-head {
|
244
244
|
margin-bottom: 8px;
|
245
245
|
}
|
246
|
-
}`;(A=document.createElement("style")).textContent=e,document.head.appendChild(A)},t=()=>{A&&(A.remove(),A=null)};(0,T.onBeforeMount)(()=>{e()}),(0,T.onBeforeUnmount)(()=>{t()})};function L(A,e,t,i,n,o,l){try{var g=A[o](l),a=g.value}catch(A){t(A);return}g.done?e(a):Promise.resolve(a).then(i,n)}function O(A){return function(){var e=this,t=arguments;return new Promise(function(i,n){var o=A.apply(e,t);function l(A){L(o,i,n,l,g,"next",A)}function g(A){L(o,i,n,l,g,"throw",A)}l(void 0)})}}function Y(A,e,t){return e in A?Object.defineProperty(A,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):A[e]=t,A}class f{stream({sessionCode:A,url:e,headers:t,data:i}){var n=this;return O(function*(){var o,l;yield null===(o=n.handleStart)||void 0===o?void 0:o.call(n,A);let g=new AbortController;n.controllerMap[A]=g,fetch(e,{method:"post",signal:g.signal,headers:function(A){for(var e=1;e<arguments.length;e++){var t=null!=arguments[e]?arguments[e]:{},i=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(t).filter(function(A){return Object.getOwnPropertyDescriptor(t,A).enumerable}))),i.forEach(function(e){Y(A,e,t[e])})}return A}({"Content-Type":"application/json"},t),mode:"cors",credentials:"include",body:JSON.stringify(i)}).then((l=O(function*(e){let t=e.body.pipeThrough(new window.TextDecoderStream).getReader(),i="";for(;;)try{let{value:o,done:l}=yield t.read();if(!e.ok){let t=o||e.statusText;try{let A=JSON.parse(t);t=A.message||A.error.message||t}catch(A){console.error(A.message||A)}n.handleError(A,t,e.status);break}if(l){n.handleEnd(A);break}(i+o.toString()).split("\n").forEach(t=>{let o=t.replace("data:","").trim();if(C(o)){var l,g;let{event:t,content:a,cover:M,documents:s,result:c,code:r,elapsed_time:u,message:N}=JSON.parse(o);if(!1===c||200!==e.status){n.handleError(A,N||"模型调用失败",r);return}switch(t){case"text":n.handleText(A,a,M);break;case"reference_doc":null===(l=n.handleReferenceDoc)||void 0===l||l.call(n,A,s,M);break;case"think":null===(g=n.handleThink)||void 0===g||g.call(n,A,a,M,u);break;case"done":n.handleEnd(A,M?a:"");break;case"error":n.handleError(A,N||"模型调用失败",r)}i=""}else o&&(i=o)})}catch(e){(null==e?void 0:e.code)!==20&&n.handleError(A,`模型调用失败:${e.message}`,e.code);break}}),function(A){return l.apply(this,arguments)}))})()}stop(A){var e,t;return null===(t=this.controllerMap[A])||void 0===t||null===(e=t.abort)||void 0===e||e.call(t),this.handleEnd(A)}constructor({handleStart:A,handleText:e,handleReferenceDoc:t,handleThink:i,handleEnd:n,handleError:o}){Y(this,"handleStart",void 0),Y(this,"handleText",void 0),Y(this,"handleReferenceDoc",void 0),Y(this,"handleThink",void 0),Y(this,"handleEnd",void 0),Y(this,"handleError",void 0),Y(this,"controllerMap",void 0),this.handleStart=A,this.handleText=e,this.handleReferenceDoc=t,this.handleThink=i,this.handleEnd=n,this.handleError=o,this.controllerMap={}}}let b=({handleStart:A,handleText:e,handleReferenceDoc:t,handleThink:i,handleEnd:n,handleError:o,requestOptions:l}={})=>{let g="内容正在生成中...",a=(0,T.ref)(),M=(0,T.ref)({}),s=(0,T.ref)([]),N={},d=(0,T.computed)(()=>{let A=[];for(let e=s.value.length-1;e>=0;e--){let t=s.value[e],i=e+1,n=s.value[i];for(;n&&![r.Ai,r.TokenExpired,r.ImageNotSupported,r.Pause,r.Guide].includes(n.role);)i+=1,n=s.value[i];if(t.role===r.System)break;t.status===c.Fail||(null==n?void 0:n.status)===c.Fail&&[r.User,r.UserImage].includes(t.role)||[r.Time,r.System].includes(t.role)||A.unshift(t)}return A}),I=(0,T.computed)(()=>{var A;let e=null===(A=a.value)||void 0===A?void 0:A.sessionCode;return!!e&&M.value[e]}),D=(0,T.computed)(()=>{var A,e,t;let i=[],n=[],o=[],l=s.value.findLastIndex(A=>A.role===r.System&&["已启用角色","已启用模型"].some(e=>A.content.includes(e))),g=0;null===(t=a.value)||void 0===t||null===(e=t.roleInfo)||void 0===e||null===(A=e.content)||void 0===A||A.forEach(A=>{let e=s.value[l+1+g],t=s.value[l+2+g];if((null==e?void 0:e.content)===A.content&&e.role!==r.System&&(i.push(j(e)),g+=1,(null==e?void 0:e.role)===r.Pause)){for(;t&&t.role===r.System;)g+=1,t=s.value[l+1+g];for(;t&&[r.User,r.UserImage].includes(t.role);)i.push(j(t)),g+=1,t=s.value[l+1+g]}}),n.push(...d.value.map(j));let M=A=>{let e=!0;for(let o=A;o<i.length;o++){var t;i[o].content!==(null===(t=n[o-A])||void 0===t?void 0:t.content)&&(e=!1)}return e};for(let A=0;A<i.length;A++){let e=i[A];if(M(A))break;o.push(e)}return o.push(...n),o.forEach(A=>{A.content=k(A.content),A.content=z(A.content)}),o}),E=new f({handleStart:w,handleText:function(A,t,i){let n=B(A);if(n.content===g)n.content=t;else{if(n.status!==c.Loading)return;n.content=i?t:n.content+t}return null==e?void 0:e(A,n)},handleReferenceDoc:function(A,e,i){let n=B(A),o=h(e);return n.content=i?o:n.content+o,null==t?void 0:t(A,n)},handleThink:function(A,e,t,n){let o=B(A);return o.content=m(o.content,e,t,n),null==i?void 0:i(A,o)},handleEnd:function(A,e){let t=B(A);if(t.content===g||x(t.content)){v(A,"聊天内容已中断");return}if(t.status===c.Loading)return M.value[A]=!1,(t.content===g||x(t.content))&&v(A,"聊天内容已中断"),e&&(t.content=e),t.status=c.Success,null==n?void 0:n(A,t)},handleError:v});function B(A){var e,t;return(null===(e=a.value)||void 0===e?void 0:e.sessionCode)===A?s.value.at(-1):null===(t=N[A])||void 0===t?void 0:t.at(-1)}function C(A){var e;return(null===(e=a.value)||void 0===e?void 0:e.sessionCode)===A?s.value:N[A]}function Q(A,e){C(A).push(e)}function p(A,e,t){var i;let n=(null===(i=a.value)||void 0===i?void 0:i.sessionCode)===A?s.value:N[A],o=[];return e.forEach(A=>{let e=n.findIndex(e=>e.id===A);if(e>-1){let i=n[e-1],l=n[e+1],g=n[e+2];if(n.splice(e,1),o.push(A),[r.Hidden].includes(null==i?void 0:i.role)){let A=n.findIndex(A=>A.id===i.id);n.splice(A,1),o.push(i.id)}if((null==l?void 0:l.role)===r.Ai||t&&[r.Ai,r.Guide,r.TokenExpired,r.ImageNotSupported,r.Pause].includes(null==l?void 0:l.role)){let A=n.findIndex(A=>A.id===l.id);n.splice(A,1),o.push(l.id)}if([r.Guide].includes(null==g?void 0:g.role)&&t){let A=n.findIndex(A=>A.id===g.id);n.splice(A,1),o.push(g.id)}}}),o}function w(e){M.value[e]=!0;let t={sessionCode:e,role:r.Ai,status:c.Loading,content:g};return Q(e,t),null==A?void 0:A(e,t)}function v(A,e,t){let i=B(A);return i.status=c.Fail,i.content=e,M.value[A]=!1,t===u.TokenExpired&&(i.content="抱歉,您的剩余 Token 不足,无法返回回答内容,请先清空当前会话(上下文仍会作为历史记录保留))",i.role=r.TokenExpired),t===u.ImageNotSupported&&(i.content="抱歉,当前模型不支持图片内容解析",i.role=r.ImageNotSupported),null==o?void 0:o(A,i,t)}function L(A,e
|
246
|
+
}`;(A=document.createElement("style")).textContent=e,document.head.appendChild(A)},t=()=>{A&&(A.remove(),A=null)};(0,T.onBeforeMount)(()=>{e()}),(0,T.onBeforeUnmount)(()=>{t()})};function L(A,e,t,i,n,o,l){try{var g=A[o](l),a=g.value}catch(A){t(A);return}g.done?e(a):Promise.resolve(a).then(i,n)}function O(A){return function(){var e=this,t=arguments;return new Promise(function(i,n){var o=A.apply(e,t);function l(A){L(o,i,n,l,g,"next",A)}function g(A){L(o,i,n,l,g,"throw",A)}l(void 0)})}}function Y(A,e,t){return e in A?Object.defineProperty(A,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):A[e]=t,A}class f{stream({sessionCode:A,url:e,headers:t,data:i}){var n=this;return O(function*(){var o,l;yield null===(o=n.handleStart)||void 0===o?void 0:o.call(n,A);let g=new AbortController;n.controllerMap[A]=g,fetch(e,{method:"post",signal:g.signal,headers:function(A){for(var e=1;e<arguments.length;e++){var t=null!=arguments[e]?arguments[e]:{},i=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(t).filter(function(A){return Object.getOwnPropertyDescriptor(t,A).enumerable}))),i.forEach(function(e){Y(A,e,t[e])})}return A}({"Content-Type":"application/json"},t),mode:"cors",credentials:"include",body:JSON.stringify(i)}).then((l=O(function*(e){let t=e.body.pipeThrough(new window.TextDecoderStream).getReader(),i="";for(;;)try{let{value:o,done:l}=yield t.read();if(!e.ok){let t=o||e.statusText;try{let A=JSON.parse(t);t=A.message||A.error.message||t}catch(A){console.error(A.message||A)}n.handleError(A,t,e.status);break}if(l){n.handleEnd(A);break}(i+o.toString()).split("\n").forEach(t=>{let o=t.replace("data:","").trim();if(C(o)){var l,g;let{event:t,content:a,cover:M,documents:s,result:c,code:r,elapsed_time:u,message:N}=JSON.parse(o);if(!1===c||200!==e.status){n.handleError(A,N||"模型调用失败",r);return}switch(t){case"text":n.handleText(A,a,M);break;case"reference_doc":null===(l=n.handleReferenceDoc)||void 0===l||l.call(n,A,s,M);break;case"think":null===(g=n.handleThink)||void 0===g||g.call(n,A,a,M,u);break;case"done":n.handleEnd(A,M?a:"");break;case"error":n.handleError(A,N||"模型调用失败",r)}i=""}else o&&(i=o)})}catch(e){(null==e?void 0:e.code)!==20&&n.handleError(A,`模型调用失败:${e.message}`,e.code);break}}),function(A){return l.apply(this,arguments)}))})()}stop(A){var e,t;return null===(t=this.controllerMap[A])||void 0===t||null===(e=t.abort)||void 0===e||e.call(t),this.handleEnd(A)}constructor({handleStart:A,handleText:e,handleReferenceDoc:t,handleThink:i,handleEnd:n,handleError:o}){Y(this,"handleStart",void 0),Y(this,"handleText",void 0),Y(this,"handleReferenceDoc",void 0),Y(this,"handleThink",void 0),Y(this,"handleEnd",void 0),Y(this,"handleError",void 0),Y(this,"controllerMap",void 0),this.handleStart=A,this.handleText=e,this.handleReferenceDoc=t,this.handleThink=i,this.handleEnd=n,this.handleError=o,this.controllerMap={}}}let b=({handleStart:A,handleText:e,handleReferenceDoc:t,handleThink:i,handleEnd:n,handleError:o,requestOptions:l}={})=>{let g="内容正在生成中...",a=(0,T.ref)(),M=(0,T.ref)({}),s=(0,T.ref)([]),N={},d=(0,T.computed)(()=>{let A=[];for(let e=s.value.length-1;e>=0;e--){let t=s.value[e],i=e+1,n=s.value[i];for(;n&&![r.Ai,r.TokenExpired,r.ImageNotSupported,r.Pause,r.Guide].includes(n.role);)i+=1,n=s.value[i];if(t.role===r.System)break;t.status===c.Fail||(null==n?void 0:n.status)===c.Fail&&[r.User,r.UserImage].includes(t.role)||[r.Time,r.System].includes(t.role)||A.unshift(t)}return A}),I=(0,T.computed)(()=>{var A;let e=null===(A=a.value)||void 0===A?void 0:A.sessionCode;return!!e&&M.value[e]}),D=(0,T.computed)(()=>{var A,e,t;let i=[],n=[],o=[],l=s.value.findLastIndex(A=>A.role===r.System&&["已启用角色","已启用模型"].some(e=>A.content.includes(e))),g=0;null===(t=a.value)||void 0===t||null===(e=t.roleInfo)||void 0===e||null===(A=e.content)||void 0===A||A.forEach(A=>{let e=s.value[l+1+g],t=s.value[l+2+g];if((null==e?void 0:e.content)===A.content&&e.role!==r.System&&(i.push(j(e)),g+=1,(null==e?void 0:e.role)===r.Pause)){for(;t&&t.role===r.System;)g+=1,t=s.value[l+1+g];for(;t&&[r.User,r.UserImage].includes(t.role);)i.push(j(t)),g+=1,t=s.value[l+1+g]}}),n.push(...d.value.map(j));let M=A=>{let e=!0;for(let o=A;o<i.length;o++){var t;i[o].content!==(null===(t=n[o-A])||void 0===t?void 0:t.content)&&(e=!1)}return e};for(let A=0;A<i.length;A++){let e=i[A];if(M(A))break;o.push(e)}return o.push(...n),o.forEach(A=>{A.content=k(A.content),A.content=z(A.content)}),o}),E=new f({handleStart:w,handleText:function(A,t,i){let n=B(A);if(n.content===g)n.content=t;else{if(n.status!==c.Loading)return;n.content=i?t:n.content+t}return null==e?void 0:e(A,n)},handleReferenceDoc:function(A,e,i){let n=B(A),o=h(e);return n.content=i?o:n.content+o,null==t?void 0:t(A,n)},handleThink:function(A,e,t,n){let o=B(A);return o.content=m(o.content,e,t,n),null==i?void 0:i(A,o)},handleEnd:function(A,e){let t=B(A);if(t.content===g||x(t.content)){v(A,"聊天内容已中断");return}if(t.status===c.Loading)return M.value[A]=!1,(t.content===g||x(t.content))&&v(A,"聊天内容已中断"),e&&(t.content=e),t.status=c.Success,null==n?void 0:n(A,t)},handleError:v});function B(A){var e,t;return(null===(e=a.value)||void 0===e?void 0:e.sessionCode)===A?s.value.at(-1):null===(t=N[A])||void 0===t?void 0:t.at(-1)}function C(A){var e;return(null===(e=a.value)||void 0===e?void 0:e.sessionCode)===A?s.value:N[A]}function Q(A,e){C(A).push(e)}function p(A,e,t){var i;let n=(null===(i=a.value)||void 0===i?void 0:i.sessionCode)===A?s.value:N[A],o=[];return e.forEach(A=>{let e=n.findIndex(e=>e.id===A);if(e>-1){let i=n[e-1],l=n[e+1],g=n[e+2];if(n.splice(e,1),o.push(A),[r.Hidden].includes(null==i?void 0:i.role)){let A=n.findIndex(A=>A.id===i.id);n.splice(A,1),o.push(i.id)}if((null==l?void 0:l.role)===r.Ai||t&&[r.Ai,r.Guide,r.TokenExpired,r.ImageNotSupported,r.Pause].includes(null==l?void 0:l.role)){let A=n.findIndex(A=>A.id===l.id);n.splice(A,1),o.push(l.id)}if([r.Guide].includes(null==g?void 0:g.role)&&t){let A=n.findIndex(A=>A.id===g.id);n.splice(A,1),o.push(g.id)}}}),o}function w(e){M.value[e]=!0;let t={sessionCode:e,role:r.Ai,status:c.Loading,content:g};return Q(e,t),null==A?void 0:A(e,t)}function v(A,e,t){let i=B(A);return i.status=c.Fail,i.content=e,M.value[A]=!1,t===u.TokenExpired&&(i.content="抱歉,您的剩余 Token 不足,无法返回回答内容,请先清空当前会话(上下文仍会作为历史记录保留))",i.role=r.TokenExpired),t===u.ImageNotSupported&&(i.content="抱歉,当前模型不支持图片内容解析",i.role=r.ImageNotSupported),null==o?void 0:o(A,i,t)}function L(A,e){var t,i,n;if(!(null===(t=a.value)||void 0===t?void 0:t.sessionCode)||!(null==l?void 0:l.url)||I.value)return;let{message:o,cite:g,shortcut:M}=A,u="";u=M?y(M.prompt,g):g?`${o}: "${g}"`:o,s.value.push({sessionCode:null===(i=a.value)||void 0===i?void 0:i.sessionCode,content:o,role:r.User,status:c.Success,cite:g}),E.stream({sessionCode:null===(n=a.value)||void 0===n?void 0:n.sessionCode,url:l.url,headers:null==l?void 0:l.headers,data:{inputs:function(A){for(var e=1;e<arguments.length;e++){var t=null!=arguments[e]?arguments[e]:{},i=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(t).filter(function(A){return Object.getOwnPropertyDescriptor(t,A).enumerable}))),i.forEach(function(e){var i;i=t[e],e in A?Object.defineProperty(A,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):A[e]=i})}return A}({chat_history:D.value.slice(0,D.value.length-1),input:u},l.data)}}),null==e||e()}return{currentSession:a,sessionContents:s,sessionContentsMap:N,sessionLoadingMap:M,prompts:D,currentSessionLoading:I,chat:function({sessionCode:A,data:e,url:t,headers:i}){E.stream({sessionCode:A,url:t,data:e,headers:i||(null==l?void 0:l.headers)})},sendChat:L,stopChat:function(A){E.stop(A)},plusSessionContent:Q,updateSessionContent:function(A){var e;let t=((null===(e=a.value)||void 0===e?void 0:e.sessionCode)===A.sessionCode?s.value:N[A.sessionCode]).find(e=>+(e.id||0)==+(A.id||0));t&&Object.assign(t,A)},getSessionContentById:function(A,e){return C(e).find(e=>e.id===A)},getLastSessionContentBySessionCode:B,getSessionContentsBySessionCode:C,setCurrentSession:function(A){a.value=A,(null==A?void 0:A.sessionCode)&&(N[A.sessionCode]||(N[A.sessionCode]=[]),s.value=N[A.sessionCode])},setSessionContents:function(A){a.value&&(N[a.value.sessionCode]=A,s.value=A)},deleteSessionContent:function(A,e){return p(A,[e],!0)},deleteSessionContents:function(A,e){return p(A,e,!1)},handleStartChat:w,handleErrorChat:v,reGenerateChat:function(A){if(s.value[A].role!==r.Ai)return;let e=s.value[A-1].content,t=s.value[A-1].cite;s.value.splice(A-1,s.value.length-A+1),L({message:e,cite:t})},reSendChat:function(A,{message:e,cite:t},i){s.value[A].role===r.User&&(s.value.splice(A,s.value.length-A),L({message:e,cite:t},i))},deleteChat:function(A){if(s.value[A].role===r.User){s.value.splice(A,2);return}s.value.splice(A-1,2)}}},S=({handleStart:A,handleEnd:e,handleError:t}={})=>{let i="",n=new f({handleStart:()=>(i="",null==A?void 0:A()),handleText:(A,e,t)=>{t||"正在思考..."===i?i=e:i+=e},handleEnd:()=>("无话可说"===i&&(i=""),null==e?void 0:e(i)),handleError:(A,e,i)=>null==t?void 0:t(e,i)});return{summary:({content:A,url:e,headers:t,model:i})=>{let o=k(z(A)),l=[{role:s.User,content:`你是一个总结大师,请帮助我对下面这段话进行总结。要求总结精炼,不超过 15 个字!且末尾没有标点符号!请注意:如果你无法总结,请回复“无话可说”!
|
247
247
|
文字如下:
|
248
248
|
${o}`}];n.stream({sessionCode:"summary",url:e,data:{prompts:l,model:i},headers:t})}}};I(86),I(84);let U=()=>{w(),v()},G=E.default;if("undefined"!=typeof window){let{currentScript:A}=window.document,e=A&&A.src.match(/(.+\/)[^/]+\.js(\?.*)?$/);e&&(I.p=e[1])}var R=D.eo,F=D.In,Z=D.c_,W=D.bJ,H=D.Ay,P=D.DA,J=D.Y_,X=D.a2,K=D.X3,_=D.S0;export{R as HttpErrorCode,F as SessionContentRole,Z as SessionContentStatus,W as SessionPromptRole,H as default,P as transferSessionContent2SessionPrompt,J as useChat,X as useClickProxy,K as useStyle,_ as useSummary};
|
package/dist/types/type.d.ts
CHANGED
@@ -41,6 +41,11 @@ export interface ISessionPrompt {
|
|
41
41
|
content: string;
|
42
42
|
role: SessionPromptRole;
|
43
43
|
}
|
44
|
+
export interface IRequestOptions {
|
45
|
+
url: string;
|
46
|
+
headers?: Record<string, string>;
|
47
|
+
data?: Record<string, any>;
|
48
|
+
}
|
44
49
|
export interface ChatCallbacks {
|
45
50
|
handleStart?: (sessionCode: string, sessionContent: ISessionContent) => void;
|
46
51
|
handleText?: (sessionCode: string, sessionContent: ISessionContent) => void;
|
@@ -48,10 +53,7 @@ export interface ChatCallbacks {
|
|
48
53
|
handleThink?: (sessionCode: string, sessionContent: ISessionContent) => void;
|
49
54
|
handleEnd?: (sessionCode: string, sessionContent: ISessionContent) => void;
|
50
55
|
handleError?: (sessionCode: string, sessionContent: ISessionContent, code: string | undefined) => void;
|
51
|
-
requestOptions?:
|
52
|
-
url: string;
|
53
|
-
headers?: Record<string, string>;
|
54
|
-
};
|
56
|
+
requestOptions?: IRequestOptions;
|
55
57
|
}
|
56
58
|
export interface SummaryCallbacks {
|
57
59
|
handleStart?: () => void;
|