@blueking/ai-ui-sdk 0.0.1-beta.6 → 0.0.1-beta.8
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/main.d.ts +3 -1
- package/dist/main.js +1 -1
- package/package.json +1 -1
package/dist/main.d.ts
CHANGED
@@ -60,9 +60,11 @@ export declare const useAI: ({ handleStart, handleText, handleReferenceDoc, hand
|
|
60
60
|
updateSessionContent: (sessionContent: ISessionContent) => void;
|
61
61
|
getSessionContentBySessionCode: (sessionCode: string) => ISessionContent;
|
62
62
|
getSessionContentsBySessionCode: (sessionCode: string) => ISessionContent[];
|
63
|
-
setCurrentSession: (session
|
63
|
+
setCurrentSession: (session?: ISession) => void;
|
64
64
|
setSessionContents: (data: ISessionContent[]) => void;
|
65
65
|
deleteSessionContent: (sessionCode: string, contentId: number) => number[];
|
66
66
|
deleteSessionContents: (sessionCode: string, contentIds: number[]) => number[];
|
67
|
+
handleStartChat: (sessionCode: string) => void | undefined;
|
68
|
+
handleErrorChat: (sessionCode: string, message: string, code?: string) => void | undefined;
|
67
69
|
};
|
68
70
|
export declare const useStyle: () => void;
|
package/dist/main.js
CHANGED
@@ -186,4 +186,4 @@
|
|
186
186
|
&~ .knowledge-head {
|
187
187
|
margin-bottom: 8px;
|
188
188
|
}
|
189
|
-
}`;(e=document.createElement("style")).textContent=t,document.head.appendChild(e)},n=()=>{e&&(e.remove(),e=null)};(0,g.onBeforeMount)(()=>{t()}),(0,g.onBeforeUnmount)(()=>{n()})};function T(e,t,n,o,i,l,r){try{var a=e[l](r),d=a.value}catch(e){n(e);return}a.done?t(d):Promise.resolve(d).then(o,i)}function w(e){return function(){var t=this,n=arguments;return new Promise(function(o,i){var l=e.apply(t,n);function r(e){T(l,o,i,r,a,"next",e)}function a(e){T(l,o,i,r,a,"throw",e)}r(void 0)})}}function E(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}class A{stream({sessionCode:e,url:t,headers:n,data:o}){var i=this;return w(function*(){var l,r;yield null===(l=i.handleStart)||void 0===l?void 0:l.call(i,e);let a=new AbortController;i.controllerMap[e]=a,fetch(t,{method:"post",signal:a.signal,headers:function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},o=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),o.forEach(function(t){E(e,t,n[t])})}return e}({"Content-Type":"application/json"},n),mode:"cors",credentials:"include",body:JSON.stringify(o)}).then((r=w(function*(t){let n=t.body.pipeThrough(new window.TextDecoderStream).getReader(),o="";for(;;)try{let{value:l,done:r}=yield n.read();if(!t.ok){i.handleError(e,l||t.statusText,t.status);break}if(r){i.handleEnd(e);break}(o+l.toString()).split("\n").forEach(n=>{let l=n.replace("data:","").trim();if(k(l)){var r,a;let{event:n,content:d,cover:s,documents:u,result:c,code:p,elapsed_time:h,message:m}=JSON.parse(l);if(!1===c||200!==t.status){i.handleError(e,m||"模型调用失败",p);return}switch(n){case"text":i.handleText(e,d,s);break;case"reference_doc":null===(r=i.handleReferenceDoc)||void 0===r||r.call(i,e,u,s);break;case"think":null===(a=i.handleThink)||void 0===a||a.call(i,e,d,s,h);break;case"done":i.handleEnd(e,s?d:"");break;case"error":i.handleError(e,m||"模型调用失败",p)}o=""}else l&&(o=l)})}catch(t){(null==t?void 0:t.code)!==20&&i.handleError(e,`模型调用失败:${t.message}`,t.code);break}}),function(e){return r.apply(this,arguments)}))})()}stop(e){var t,n;return null===(n=this.controllerMap[e])||void 0===n||null===(t=n.abort)||void 0===t||t.call(n),this.handleEnd(e)}constructor({handleStart:e,handleText:t,handleReferenceDoc:n,handleThink:o,handleEnd:i,handleError:l}){E(this,"handleStart",void 0),E(this,"handleText",void 0),E(this,"handleReferenceDoc",void 0),E(this,"handleThink",void 0),E(this,"handleEnd",void 0),E(this,"handleError",void 0),E(this,"controllerMap",void 0),this.handleStart=e,this.handleText=t,this.handleReferenceDoc=n,this.handleThink=o,this.handleEnd=i,this.handleError=l,this.controllerMap={}}}let F=e=>{let n={[o.Ai]:t.Assistant,[o.User]:t.User,[o.System]:t.System,[o.Hidden]:t.Hidden,[o.Guide]:t.Guide,[o.Time]:t.System,[o.Role]:t.System,[o.HiddenAi]:t.HiddenAssistant,[o.HiddenGuide]:t.HiddenGuide,[o.HiddenRole]:t.HiddenSystem,[o.HiddenUser]:t.HiddenUser,[o.TemplateAi]:t.TemplateAssistant,[o.TemplateGuide]:t.TemplateGuide,[o.TemplateRole]:t.TemplateSystem,[o.TemplateUser]:t.TemplateUser,[o.TemplateHidden]:t.TemplateHidden,[o.Pause]:t.Pause,[o.TokenExpired]:t.Pause,[o.ImageNotSupported]:t.Pause,[o.UserImage]:t.UserImage};return{content:e.content,role:n[e.role]}},C=({handleStart:e,handleText:t,handleReferenceDoc:l,handleThink:r,handleEnd:a,handleError:d}={})=>{let s="内容正在生成中...",u=(0,g.ref)(),c=(0,g.ref)({}),p=(0,g.ref)([]),h={},m=(0,g.computed)(()=>{let e=[];for(let t=p.value.length-1;t>=0;t--){let i=p.value[t],l=t+1,r=p.value[l];for(;r&&![o.Ai,o.TokenExpired,o.ImageNotSupported,o.Pause,o.Guide].includes(r.role);)l+=1,r=p.value[l];if(i.role===o.System)break;i.status===n.Fail||(null==r?void 0:r.status)===n.Fail&&[o.User,o.UserImage].includes(i.role)||[o.Time,o.System].includes(i.role)||e.unshift(i)}return e}),f=(0,g.computed)(()=>{var e,t,n;let i=[],l=[],r=[],a=p.value.findLastIndex(e=>e.role===o.System&&["已启用角色","已启用模型"].some(t=>e.content.includes(t))),d=0;null===(n=u.value)||void 0===n||null===(t=n.roleInfo)||void 0===t||null===(e=t.content)||void 0===e||e.forEach(e=>{let t=p.value[a+1+d],n=p.value[a+2+d];if((null==t?void 0:t.content)===e.content&&t.role!==o.System&&(i.push(F(t)),d+=1,(null==t?void 0:t.role)===o.Pause)){for(;n&&n.role===o.System;)d+=1,n=p.value[a+1+d];for(;n&&[o.User,o.UserImage].includes(n.role);)i.push(F(n)),d+=1,n=p.value[a+1+d]}}),l.push(...m.value.map(F));let s=e=>{let t=!0;for(let o=e;o<i.length;o++){var n;i[o].content!==(null===(n=l[o-e])||void 0===n?void 0:n.content)&&(t=!1)}return t};for(let e=0;e<i.length;e++){let t=i[e];if(s(e))break;r.push(t)}return r.push(...l),r}),b=new A({handleStart:
|
189
|
+
}`;(e=document.createElement("style")).textContent=t,document.head.appendChild(e)},n=()=>{e&&(e.remove(),e=null)};(0,g.onBeforeMount)(()=>{t()}),(0,g.onBeforeUnmount)(()=>{n()})};function T(e,t,n,o,i,l,r){try{var a=e[l](r),d=a.value}catch(e){n(e);return}a.done?t(d):Promise.resolve(d).then(o,i)}function w(e){return function(){var t=this,n=arguments;return new Promise(function(o,i){var l=e.apply(t,n);function r(e){T(l,o,i,r,a,"next",e)}function a(e){T(l,o,i,r,a,"throw",e)}r(void 0)})}}function E(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}class A{stream({sessionCode:e,url:t,headers:n,data:o}){var i=this;return w(function*(){var l,r;yield null===(l=i.handleStart)||void 0===l?void 0:l.call(i,e);let a=new AbortController;i.controllerMap[e]=a,fetch(t,{method:"post",signal:a.signal,headers:function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},o=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),o.forEach(function(t){E(e,t,n[t])})}return e}({"Content-Type":"application/json"},n),mode:"cors",credentials:"include",body:JSON.stringify(o)}).then((r=w(function*(t){let n=t.body.pipeThrough(new window.TextDecoderStream).getReader(),o="";for(;;)try{let{value:l,done:r}=yield n.read();if(!t.ok){i.handleError(e,l||t.statusText,t.status);break}if(r){i.handleEnd(e);break}(o+l.toString()).split("\n").forEach(n=>{let l=n.replace("data:","").trim();if(k(l)){var r,a;let{event:n,content:d,cover:s,documents:u,result:c,code:p,elapsed_time:h,message:m}=JSON.parse(l);if(!1===c||200!==t.status){i.handleError(e,m||"模型调用失败",p);return}switch(n){case"text":i.handleText(e,d,s);break;case"reference_doc":null===(r=i.handleReferenceDoc)||void 0===r||r.call(i,e,u,s);break;case"think":null===(a=i.handleThink)||void 0===a||a.call(i,e,d,s,h);break;case"done":i.handleEnd(e,s?d:"");break;case"error":i.handleError(e,m||"模型调用失败",p)}o=""}else l&&(o=l)})}catch(t){(null==t?void 0:t.code)!==20&&i.handleError(e,`模型调用失败:${t.message}`,t.code);break}}),function(e){return r.apply(this,arguments)}))})()}stop(e){var t,n;return null===(n=this.controllerMap[e])||void 0===n||null===(t=n.abort)||void 0===t||t.call(n),this.handleEnd(e)}constructor({handleStart:e,handleText:t,handleReferenceDoc:n,handleThink:o,handleEnd:i,handleError:l}){E(this,"handleStart",void 0),E(this,"handleText",void 0),E(this,"handleReferenceDoc",void 0),E(this,"handleThink",void 0),E(this,"handleEnd",void 0),E(this,"handleError",void 0),E(this,"controllerMap",void 0),this.handleStart=e,this.handleText=t,this.handleReferenceDoc=n,this.handleThink=o,this.handleEnd=i,this.handleError=l,this.controllerMap={}}}let F=e=>{let n={[o.Ai]:t.Assistant,[o.User]:t.User,[o.System]:t.System,[o.Hidden]:t.Hidden,[o.Guide]:t.Guide,[o.Time]:t.System,[o.Role]:t.System,[o.HiddenAi]:t.HiddenAssistant,[o.HiddenGuide]:t.HiddenGuide,[o.HiddenRole]:t.HiddenSystem,[o.HiddenUser]:t.HiddenUser,[o.TemplateAi]:t.TemplateAssistant,[o.TemplateGuide]:t.TemplateGuide,[o.TemplateRole]:t.TemplateSystem,[o.TemplateUser]:t.TemplateUser,[o.TemplateHidden]:t.TemplateHidden,[o.Pause]:t.Pause,[o.TokenExpired]:t.Pause,[o.ImageNotSupported]:t.Pause,[o.UserImage]:t.UserImage};return{content:e.content,role:n[e.role]}},C=({handleStart:e,handleText:t,handleReferenceDoc:l,handleThink:r,handleEnd:a,handleError:d}={})=>{let s="内容正在生成中...",u=(0,g.ref)(),c=(0,g.ref)({}),p=(0,g.ref)([]),h={},m=(0,g.computed)(()=>{let e=[];for(let t=p.value.length-1;t>=0;t--){let i=p.value[t],l=t+1,r=p.value[l];for(;r&&![o.Ai,o.TokenExpired,o.ImageNotSupported,o.Pause,o.Guide].includes(r.role);)l+=1,r=p.value[l];if(i.role===o.System)break;i.status===n.Fail||(null==r?void 0:r.status)===n.Fail&&[o.User,o.UserImage].includes(i.role)||[o.Time,o.System].includes(i.role)||e.unshift(i)}return e}),f=(0,g.computed)(()=>{var e,t,n;let i=[],l=[],r=[],a=p.value.findLastIndex(e=>e.role===o.System&&["已启用角色","已启用模型"].some(t=>e.content.includes(t))),d=0;null===(n=u.value)||void 0===n||null===(t=n.roleInfo)||void 0===t||null===(e=t.content)||void 0===e||e.forEach(e=>{let t=p.value[a+1+d],n=p.value[a+2+d];if((null==t?void 0:t.content)===e.content&&t.role!==o.System&&(i.push(F(t)),d+=1,(null==t?void 0:t.role)===o.Pause)){for(;n&&n.role===o.System;)d+=1,n=p.value[a+1+d];for(;n&&[o.User,o.UserImage].includes(n.role);)i.push(F(n)),d+=1,n=p.value[a+1+d]}}),l.push(...m.value.map(F));let s=e=>{let t=!0;for(let o=e;o<i.length;o++){var n;i[o].content!==(null===(n=l[o-e])||void 0===n?void 0:n.content)&&(t=!1)}return t};for(let e=0;e<i.length;e++){let t=i[e];if(s(e))break;r.push(t)}return r.push(...l),r}),b=new A({handleStart:E,handleText:function(e,o,i){let l=k(e);if(l.content===s)l.content=o;else{if(l.status!==n.Loading)return;l.content=i?o:l.content+o}return null==t?void 0:t(e,l)},handleReferenceDoc:function(e,t,n){let o=k(e),i=v(t);return o.content=n?i:o.content+i,null==l?void 0:l(e,o)},handleThink:function(e,t,n,o){let i=k(e);return i.content=x(i.content,t,n,o),null==r?void 0:r(e,i)},handleEnd:function(e,t){let o=k(e);if(o.status===n.Loading)return c.value[e]=!1,t&&(o.content=t),o.status=n.Success,null==a?void 0:a(e,o);(o.content===s||y(o.content))&&C(e,"聊天内容已中断")},handleError:C});function k(e){var t,n;return(null===(t=u.value)||void 0===t?void 0:t.sessionCode)===e?p.value.at(-1):null===(n=h[e])||void 0===n?void 0:n.at(-1)}function S(e){var t;return(null===(t=u.value)||void 0===t?void 0:t.sessionCode)===e?p.value:h[e]}function T(e,t){S(e).push(t)}function w(e,t,n){var i;let l=(null===(i=u.value)||void 0===i?void 0:i.sessionCode)===e?p.value:h[e],r=[];return t.forEach(e=>{let t=l.findIndex(t=>t.id===e);if(t>-1){let i=l[t-1],a=l[t+1],d=l[t+2];if(l.splice(t,1),r.push(e),[o.Hidden].includes(null==i?void 0:i.role)){let e=l.findIndex(e=>e.id===i.id);l.splice(e,1),r.push(i.id)}if((null==a?void 0:a.role)===o.Ai||n&&[o.Ai,o.Guide,o.TokenExpired,o.ImageNotSupported,o.Pause].includes(null==a?void 0:a.role)){let e=l.findIndex(e=>e.id===a.id);l.splice(e,1),r.push(a.id)}if([o.Guide].includes(null==d?void 0:d.role)&&n){let e=l.findIndex(e=>e.id===d.id);l.splice(e,1),r.push(d.id)}}}),r}function E(t){c.value[t]=!0;let i={sessionCode:t,role:o.Ai,status:n.Loading,content:s};return T(t,i),null==e?void 0:e(t,i)}function C(e,t,l){let r=k(e);return r.status=n.Fail,r.content=t,c.value[e]=!1,l===i.TokenExpired&&(r.content="抱歉,您的剩余 Token 不足,无法返回回答内容,请先清空当前会话(上下文仍会作为历史记录保留))",r.role=o.TokenExpired),l===i.ImageNotSupported&&(r.content="抱歉,当前模型不支持图片内容解析",r.role=o.ImageNotSupported),null==d?void 0:d(e,r,l)}return{currentSession:u,sessionContents:p,sessionContentsMap:h,sessionLoadingMap:c,prompts:f,chat:function({sessionCode:e,data:t,url:n,headers:o}){b.stream({sessionCode:e,url:n,data:t,headers:o})},stopChat:function(e){b.stop(e)},plusSessionContent:T,updateSessionContent:function(e){var t;let n=((null===(t=u.value)||void 0===t?void 0:t.sessionCode)===e.sessionCode?p.value:h[e.sessionCode]).find(t=>+(t.id||0)==+(e.id||0));n&&Object.assign(n,e)},getSessionContentBySessionCode:k,getSessionContentsBySessionCode:S,setCurrentSession:function(e){u.value=e,(null==e?void 0:e.sessionCode)&&(h[e.sessionCode]||(h[e.sessionCode]=[]),p.value=h[e.sessionCode])},setSessionContents:function(e){u.value&&(h[u.value.sessionCode]=e,p.value=e)},deleteSessionContent:function(e,t){return w(e,[t],!0)},deleteSessionContents:function(e,t){return w(e,t,!1)},handleStartChat:E,handleErrorChat:C}},U=()=>{b(),S()},H=f.default;if("undefined"!=typeof window){let{currentScript:e}=window.document,t=e&&e.src.match(/(.+\/)[^/]+\.js(\?.*)?$/);t&&(h.p=t[1])}return m})());
|