@blueking/ai-ui-sdk 0.0.1-beta.3 → 0.0.1-beta.4
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 +4 -2
- package/dist/main.js +4 -4
- package/package.json +1 -1
package/dist/main.d.ts
CHANGED
@@ -7,7 +7,8 @@ type SessionLoadingMap = {
|
|
7
7
|
[key: string]: boolean;
|
8
8
|
};
|
9
9
|
export type * from './types/type';
|
10
|
-
export
|
10
|
+
export * from './types/enum';
|
11
|
+
export * from './common/type-transform';
|
11
12
|
export interface AICallbacks {
|
12
13
|
handleStart?: (sessionCode: string, sessionContent: ISessionContent) => void;
|
13
14
|
handleText?: (sessionCode: string, sessionContent: ISessionContent) => void;
|
@@ -16,7 +17,7 @@ export interface AICallbacks {
|
|
16
17
|
handleEnd?: (sessionCode: string, sessionContent: ISessionContent) => void;
|
17
18
|
handleError?: (sessionCode: string, sessionContent: ISessionContent, code: string | undefined) => void;
|
18
19
|
}
|
19
|
-
export declare const useAI: ({ handleStart, handleText, handleReferenceDoc, handleThink, handleEnd, handleError, }
|
20
|
+
export declare const useAI: ({ handleStart, handleText, handleReferenceDoc, handleThink, handleEnd, handleError, }?: AICallbacks) => {
|
20
21
|
currentSession: import("vue").Ref<ISession | undefined, ISession | undefined>;
|
21
22
|
sessionContents: import("vue").Ref<{
|
22
23
|
id?: number | undefined;
|
@@ -54,6 +55,7 @@ export declare const useAI: ({ handleStart, handleText, handleReferenceDoc, hand
|
|
54
55
|
url: string;
|
55
56
|
headers?: Record<string, string>;
|
56
57
|
}) => void;
|
58
|
+
stopChat: (sessionCode: string) => void;
|
57
59
|
getSessionContentBySessionCode: (sessionCode: string) => ISessionContent;
|
58
60
|
getSessionContentsBySessionCode: (sessionCode: string) => ISessionContent[];
|
59
61
|
setCurrentSession: (session: ISession) => void;
|
package/dist/main.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("vue")):"function"==typeof define&&define.amd?define(["vue"],t):"object"==typeof exports?exports["ai-ui-sdk"]=t(require("vue")):e["ai-ui-sdk"]=t(e.vue)}("undefined"!=typeof self?self:this,e=>(()=>{"use strict";var t,n,o,i,l,
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("vue")):"function"==typeof define&&define.amd?define(["vue"],t):"object"==typeof exports?exports["ai-ui-sdk"]=t(require("vue")):e["ai-ui-sdk"]=t(e.vue)}("undefined"!=typeof self?self:this,e=>(()=>{"use strict";var t,n,o,i,r,l,a,d,s,u,c={380:t=>{t.exports=e}},p={};function h(e){var t=p[e];if(void 0!==t)return t.exports;var n=p[e]={exports:{}};return c[e](n,n.exports,h),n.exports}h.d=(e,t)=>{for(var n in t)h.o(t,n)&&!h.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},h.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),h.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},h.p="/";var m={};h.r(m),h.d(m,{ApiValueType:()=>a,EnumCharacter:()=>d,FileLinkType:()=>r,FileStatus:()=>l,HttpErrorCode:()=>i,KnowledgePathType:()=>s,KnowledgeType:()=>u,SessionContentRole:()=>o,SessionContentStatus:()=>n,SessionPromptRole:()=>t,default:()=>P,transferSessionContent2SessionPrompt:()=>F,useAI:()=>U,useStyle:()=>H});var f={};h.r(f),h.d(f,{Cy:()=>a,hb:()=>d,rs:()=>r,BM:()=>l,eo:()=>i,sh:()=>s,zW:()=>u,In:()=>o,c_:()=>n,bJ:()=>t,DA:()=>F,OD:()=>U,X3:()=>H});var g=h(380);!function(e){e.System="system",e.Assistant="assistant",e.User="user",e.Guide="guide",e.Pause="pause",e.UserImage="user-image",e.Hidden="hidden",e.HiddenUser="hidden-user",e.HiddenAssistant="hidden-assistant",e.HiddenSystem="hidden-system",e.HiddenGuide="hidden-guide",e.TemplateUser="template-user",e.TemplateAssistant="template-assistant",e.TemplateSystem="template-system",e.TemplateGuide="template-guide",e.TemplateHidden="template-hidden"}(t||(t={})),function(e){e.Fail="fail",e.Loading="loading",e.Success="success"}(n||(n={})),function(e){e.Ai="ai",e.User="user",e.Time="time",e.System="system",e.Role="role",e.Hidden="hidden",e.Guide="guide",e.Pause="pause",e.TokenExpired="token-expired",e.UserImage="user-image",e.HiddenUser="hidden-user",e.HiddenAi="hidden-ai",e.HiddenRole="hidden-role",e.HiddenGuide="hidden-guide",e.TemplateUser="template-user",e.TemplateAi="template-ai",e.TemplateRole="template-role",e.TemplateGuide="template-guide",e.TemplateHidden="template-hidden",e.ImageNotSupported="image-not-supported"}(o||(o={})),function(e){e.TokenExpired="80003",e.ImageNotSupported="82003",e.UnAuthorizedPreviewFile="80401",e.TagRepeat="1513405",e[e.Aborted=20]="Aborted",e[e.UnLogin=401]="UnLogin",e.IamNoPermission="IAM_NO_PERMISSION"}(i||(i={})),function(e){e.External="external",e.BkRepo="bk_repo"}(r||(r={})),function(e){e.Init="init",e.Uploading="uploading",e.Uploaded="uploaded",e.Deleting="deleting",e.Deleted="deleted",e.Failed="failed"}(l||(l={})),function(e){e.String="string",e.Number="number",e.Boolean="boolean",e.Array="array",e.Object="object"}(a||(a={})),function(e){e.All="all",e.System="system",e.User="user",e.Public="public",e.Space="space"}(d||(d={})),function(e){e.File="file",e.Folder="folder"}(s||(s={})),function(e){e.File="file",e.Manual="manual",e.Link="link"}(u||(u={}));let v=e=>{let t=`<section class="knowledge-head click-close">
|
2
2
|
<svg
|
3
3
|
class="bkaidev-wenzhang"
|
4
4
|
>
|
@@ -132,13 +132,13 @@
|
|
132
132
|
background: #EAEBF0;
|
133
133
|
}
|
134
134
|
}
|
135
|
-
}`;(e=document.createElement("style")).textContent=t+n,document.head.appendChild(e)},n=()=>{e&&(e.remove(),e=null)};(0,g.onBeforeMount)(()=>{t()}),(0,g.onBeforeUnmount)(()=>{n()})},k=e=>{try{return JSON.parse(e),!0}catch(e){return!1}},
|
135
|
+
}`;(e=document.createElement("style")).textContent=t+n,document.head.appendChild(e)},n=()=>{e&&(e.remove(),e=null)};(0,g.onBeforeMount)(()=>{t()}),(0,g.onBeforeUnmount)(()=>{n()})},k=e=>{try{return JSON.parse(e),!0}catch(e){return!1}},y=(e,t,n,o)=>{let i="内容正在生成中..."===e||n?"":e;if(i.includes('<section class="think-head click-close">')){let e=i.match(/<section class="think-body">([\s\S]*?)<\/section>/);if(e){let n=e[1],o=n.replace(/\n$/g,`${t}
|
136
136
|
`);i=i.replace(n,o)}}else i+=`<section class="think-head click-close">
|
137
137
|
<i class="bkaidev-icon bkaidev-sikao"></i>思考中...<i class="bkaidev-icon bkaidev-angle-up"></i>
|
138
138
|
</section>
|
139
139
|
<section class="think-body">
|
140
140
|
${t}
|
141
|
-
</section>`;return o&&(i=i.replace("思考中...",`已完成思考 (耗时:${function(e){let t=Math.floor(e/36e5),n=Math.floor(e%36e5/6e4),o=Math.floor(e%6e4/1e3),i=e%1e3,
|
141
|
+
</section>`;return o&&(i=i.replace("思考中...",`已完成思考 (耗时:${function(e){let t=Math.floor(e/36e5),n=Math.floor(e%36e5/6e4),o=Math.floor(e%6e4/1e3),i=e%1e3,r=[];return t>0&&r.push(`${t}h`),n>0&&r.push(`${n}min`),o>0&&r.push(`${o}s`),i>0&&r.push(`${i.toFixed(2)}ms`),r.join(" ")}(o)})`)),i},x=e=>{let t="正在思考..."===e,n=e.match(/<section class="think-body">([\s\S]*?)<\/section>$/),o=n&&""===n[1].trim();return t||o},S=()=>{let e=null,t=()=>{let t=`.think-head {
|
142
142
|
display: flex;
|
143
143
|
align-items: center;
|
144
144
|
padding: 0 8px 0 6px;
|
@@ -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
|
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,r,l){try{var a=e[r](l),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 r=e.apply(t,n);function l(e){T(r,o,i,l,a,"next",e)}function a(e){T(r,o,i,l,a,"throw",e)}l(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 r,l;yield null===(r=i.handleStart)||void 0===r?void 0:r.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((l=w(function*(t){let n=t.body.pipeThrough(new window.TextDecoderStream).getReader(),o="";for(;;)try{let{value:r,done:l}=yield n.read();if(!t.ok){i.handleError(e,r||t.statusText,t.status);break}if(l){i.handleEnd(e);break}(o+r.toString()).split("\n").forEach(n=>{let r=n.replace("data:","").trim();if(k(r)){var l,a;let{event:n,content:d,cover:s,documents:u,result:c,code:p,elapsed_time:h,message:m}=JSON.parse(r);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===(l=i.handleReferenceDoc)||void 0===l||l.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 r&&(o=r)})}catch(t){(null==t?void 0:t.code)!==20&&i.handleError(e,`模型调用失败:${t.message}`,t.code);break}}),function(e){return l.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:r}){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=r,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]}},U=({handleStart:e,handleText:t,handleReferenceDoc:r,handleThink:l,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],r=t+1,l=p.value[r];for(;l&&![o.Ai,o.TokenExpired,o.ImageNotSupported,o.Pause,o.Guide].includes(l.role);)r+=1,l=p.value[r];if(i.role===o.System)break;i.status===n.Fail||(null==l?void 0:l.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=[],r=[],l=[],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]}}),r.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=r[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;l.push(t)}return l.push(...r),l}),b=new A({handleStart:function(t){c.value[t]=!0;let i={sessionCode:t,role:o.Ai,status:n.Loading,content:s};return!function(e,t){S(e).push(t)}(t,i),null==e?void 0:e(t,i)},handleText:function(e,o,i){let r=k(e);if(r.content===s)r.content=o;else{if(r.status!==n.Loading)return;r.content=i?o:r.content+o}return null==t?void 0:t(e,r)},handleReferenceDoc:function(e,t,n){let o=k(e),i=v(t);return o.content=n?i:o.content+i,null==r?void 0:r(e,o)},handleThink:function(e,t,n,o){let i=k(e);return i.content=y(i.content,t,n,o),null==l?void 0:l(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||x(o.content))&&T(e,"聊天内容已中断")},handleError:T});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,r){let l=k(e);return l.status=n.Fail,l.content=t,c.value[e]=!1,r===i.TokenExpired&&(l.content="抱歉,您的剩余 Token 不足,无法返回回答内容,请先清空当前会话(上下文仍会作为历史记录保留))",l.role=o.TokenExpired),r===i.ImageNotSupported&&(l.content="抱歉,当前模型不支持图片内容解析",l.role=o.ImageNotSupported),null==d?void 0:d(e,l,r)}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)},getSessionContentBySessionCode:k,getSessionContentsBySessionCode:S,setCurrentSession:function(e){u.value=e,h[e.sessionCode]||(h[e.sessionCode]=[]),p.value=h[e.sessionCode]},setSessionContents:function(e){u.value&&(h[u.value.sessionCode]=e,p.value=e)}}},H=()=>{b(),S()},P=f.default;if("undefined"!=typeof window){let{currentScript:e}=window.document,t=e&&e.src.match(/(.+\/)[^/]+\.js(\?.*)?$/);t&&(h.p=t[1])}return m})());
|