super-agent-sdk 1.0.14 → 1.0.16
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/index.cjs +4 -4
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +152 -133
- package/dist/{mammoth.browser-CmEXxaIp.js → mammoth.browser-BmR8YfeV.js} +1 -1
- package/dist/{mammoth.browser-BX8glWVC.cjs → mammoth.browser-ClkRBm1A.cjs} +1 -1
- package/dist/{nosUploader-KkzFK5EW.cjs → nosUploader-CgVOa1Lz.cjs} +5 -5
- package/dist/{nosUploader-BkBtdrMs.js → nosUploader-CnYDALPr.js} +732 -699
- package/dist/widget.cjs +21 -21
- package/dist/widget.d.ts +1 -1
- package/dist/widget.mjs +1173 -1118
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
`).map(
|
|
3
|
-
`))}catch{return null}switch(t.type){case"user":return{type:"user",content:u(t.content)};case"thinking":return{type:"thinking",content:u(t.content)};case"ai":return{type:"ai",content:u(t.content)};case"tool_call":return{type:"tool_call",content:t.name??"",toolName:t.name,toolCallId:t.id,args:typeof t.args=="string"?t.args:JSON.stringify(t.args??{})};case"tool_result":return{type:"tool_result",content:u(t.output),toolName:t.name,toolCallId:t.id,artifacts:Array.isArray(t.artifacts)?t.artifacts:void 0,renderMode:t.render_mode??t.renderMode,html:t.html};case"done":return{type:"done",content:u(t.content),sessionId:t.sessionId??t.session_id??"",messageId:t.messageId??t.message_id};case"stop":return{type:"stop",content:u(t.content),sessionId:t.sessionId??t.session_id??"",messageId:t.messageId??t.message_id};case"error":return{type:"error",content:t.message??"Unknown error"};case"interrupt":return{type:"interrupt",content:t.content??"",interrupt:{interruptId:t.interruptId??t.interrupt_id??"",interruptType:t.interruptType??t.interrupt_type??"confirm",content:t.content??"",options:t.options,placeholder:t.placeholder,inputType:t.inputType??t.input_type,maxLength:t.maxLength??t.max_length,required:t.required,fields:t.fields,metadata:t.metadata}};default:return null}}async function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("./nosUploader-CgVOa1Lz.cjs");function h(r){if(Array.isArray(r))return r.map(e=>h(e));if(r&&typeof r=="object"&&r.constructor===Object){const e={};for(const[t,s]of Object.entries(r)){const o=t.replace(/_([a-z])/g,(a,n)=>n.toUpperCase());e[o]=h(s)}return e}return r}const k="/api/agent/runtime/v1";class T{constructor(e){this.refreshing=null;const t=e.baseUrl??k;this.baseUrl=t.replace(/\/$/,""),this.tokenGateway=e.tokenGateway.replace(/\/$/,""),this.appId=e.appId??"",this.token=e.token??"",this.botId=e.botId,this.isAbsoluteUrl=/^https?:\/\//.test(this.baseUrl),this.isAbsoluteGateway=/^https?:\/\//.test(this.tokenGateway),this.loginUrl=e.loginUrl,this.onAuthExpired=e.onAuthExpired}getBotId(){return this.botId}getAppKey(){return this.appId}setToken(e){this.token=e}getOrigin(){return typeof window<"u"?window.location.origin:""}async fetchToken(){const t=`${this.isAbsoluteGateway?this.tokenGateway:`${this.getOrigin()}${this.tokenGateway}`}/v1/token`,s=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({botId:this.botId})});if(!s.ok)throw new Error(`HTTP ${s.status}: ${s.statusText}`);const o=await s.json();if(o.code!=="200")throw new Error(o.message||`API error: ${o.code}`);const a=h(o.data);return this.token=a.token,a.appId&&!this.appId&&(this.appId=a.appId),a.token}headers(e){return{"Content-Type":"application/json","X-App-Id":this.appId,"X-Token":this.token,...e}}async handleTokenRefresh(){if(this.refreshing)return await this.refreshing,!0;try{return this.refreshing=(async()=>{await this.fetchToken()})(),await this.refreshing,!0}catch{return!1}finally{this.refreshing=null}}async request(e,t,s){if(!this.token&&!await this.handleTokenRefresh())throw new Error("SDK token not available: POST /token failed");let o=this.isAbsoluteUrl?`${this.baseUrl}${t}`:`${this.getOrigin()}${this.baseUrl}${t}`;if(s!=null&&s.params){const c=new URLSearchParams;for(const[d,l]of Object.entries(s.params))l!=null&&c.set(d,String(l));const i=c.toString();i&&(o+=`?${i}`)}const a=await fetch(o,{method:e,headers:this.headers(),body:s!=null&&s.body?JSON.stringify(s.body):void 0});if(a.status===401&&!(s!=null&&s.retry)&&await this.handleTokenRefresh())return this.request(e,t,{...s,retry:!0});if(!a.ok)throw new Error(`HTTP ${a.status}: ${a.statusText}`);const n=await a.json();if(n.code!=="200")throw new Error(n.message||`API error: ${n.code}`);return h(n.data)}get(e,t){return this.request("GET",e,{params:t})}post(e,t){return this.request("POST",e,{body:t})}patch(e,t){return this.request("PATCH",e,{body:t})}del(e){return this.request("DELETE",e)}async streamPost(e,t,s){if(!this.token&&!await this.handleTokenRefresh())throw new Error("SDK token not available: POST /token failed");const o=this.isAbsoluteUrl?`${this.baseUrl}${e}`:`${this.getOrigin()}${this.baseUrl}${e}`,a=await fetch(o,{method:"POST",headers:this.headers(),body:JSON.stringify(t),signal:s});if(a.status===401){const n=await f.peekCookieExpired(a.clone());if(n.isCookieExpired&&typeof(t==null?void 0:t.message)=="string"){f.stashPendingChat({botId:this.botId,message:t.message,sessionId:t.sessionId??""});const i=n.loginUrl??this.loginUrl;return this.onAuthExpired?this.onAuthExpired({loginUrl:i,sessionId:n.sessionId,botId:this.botId}):i&&window.location.replace(i),a}if(await this.handleTokenRefresh())return fetch(o,{method:"POST",headers:this.headers(),body:JSON.stringify(t),signal:s})}return a}}function u(r){return typeof r=="string"?r:Array.isArray(r)?r.map(e=>{if(typeof e=="string")return e;if(e&&typeof e=="object"){if(typeof e.text=="string")return e.text;if(e.type)return`[${e.type}${e.mime_type?": "+e.mime_type:""}]`}return""}).join(""):r==null?"":String(r)}function p(r){const e=r.split(`
|
|
2
|
+
`).map(s=>s.trim()).filter(s=>s.startsWith("data:")).map(s=>s.slice(5).trim());if(!e.length)return null;let t;try{t=JSON.parse(e.join(`
|
|
3
|
+
`))}catch{return null}switch(t.type){case"user":return{type:"user",content:u(t.content)};case"thinking":return{type:"thinking",content:u(t.content)};case"ai":return{type:"ai",content:u(t.content)};case"tool_call":return{type:"tool_call",content:t.name??"",toolName:t.name,toolCallId:t.id,args:typeof t.args=="string"?t.args:JSON.stringify(t.args??{})};case"tool_result":return{type:"tool_result",content:u(t.output),toolName:t.name,toolCallId:t.id,artifacts:Array.isArray(t.artifacts)?t.artifacts:void 0,renderMode:t.render_mode??t.renderMode,html:t.html};case"done":return{type:"done",content:u(t.content),sessionId:t.sessionId??t.session_id??"",messageId:t.messageId??t.message_id};case"stop":return{type:"stop",content:u(t.content),sessionId:t.sessionId??t.session_id??"",messageId:t.messageId??t.message_id};case"error":return{type:"error",content:t.message??"Unknown error"};case"interrupt":return{type:"interrupt",content:t.content??"",interrupt:{interruptId:t.interruptId??t.interrupt_id??"",interruptType:t.interruptType??t.interrupt_type??"confirm",content:t.content??"",options:t.options,placeholder:t.placeholder,inputType:t.inputType??t.input_type,maxLength:t.maxLength??t.max_length,required:t.required,fields:t.fields,metadata:t.metadata}};default:return null}}async function w(r,e,t){if(!r.body){t(new Error("Response body is null"));return}const s=r.body.getReader(),o=new TextDecoder;let a="";try{for(;;){const{value:n,done:c}=await s.read();if(c)break;a+=o.decode(n,{stream:!0});let i;for(;(i=a.indexOf(`
|
|
4
4
|
|
|
5
|
-
`))!==-1;){const d=a.slice(0,i);a=a.slice(i+2);const l=
|
|
5
|
+
`))!==-1;){const d=a.slice(0,i);a=a.slice(i+2);const l=p(d);l&&e(l)}}if(a.trim()){const n=p(a);n&&e(n)}}catch(n){(n==null?void 0:n.name)!=="AbortError"&&t(n instanceof Error?n:new Error(String(n)))}finally{try{s.releaseLock()}catch{}}}function b(r,e){var a;const t=new AbortController,s=e.signal?S(e.signal,t.signal):t.signal,o={botId:r.getBotId(),message:e.message,sessionId:e.sessionId,stream:e.stream!==!1,...(a=e.attachments)!=null&&a.length?{attachments:e.attachments.map(({url:n,...c})=>c)}:{}};return o.stream?r.streamPost("/chat",o,s).then(n=>{var c;if(!n.ok){(c=e.onError)==null||c.call(e,new Error(`HTTP ${n.status}`));return}return w(n,i=>{var d,l,g,m,y,I;i.type==="done"&&i.sessionId?(d=e.onDone)==null||d.call(e,{sessionId:i.sessionId,content:i.content,messageId:i.messageId}):i.type==="done"?(l=e.onDone)==null||l.call(e,{sessionId:"",content:i.content,messageId:i.messageId}):i.type==="stop"?((g=e.onMessage)==null||g.call(e,i),(m=e.onDone)==null||m.call(e,{sessionId:i.sessionId??"",content:i.content,messageId:i.messageId})):i.type==="error"?(y=e.onError)==null||y.call(e,new Error(i.content)):(I=e.onMessage)==null||I.call(e,i)},i=>{var d;return(d=e.onError)==null?void 0:d.call(e,i)})}).catch(n=>{var c;(n==null?void 0:n.name)!=="AbortError"&&((c=e.onError)==null||c.call(e,n instanceof Error?n:new Error(String(n))))}):r.post("/chat",o).then(n=>{var c;(c=e.onDone)==null||c.call(e,{sessionId:n.sessionId,content:n.content??"",messageId:n.messageId})}).catch(n=>{var c;(c=e.onError)==null||c.call(e,n instanceof Error?n:new Error(String(n)))}),t}function S(r,e){const t=new AbortController,s=()=>t.abort();return r.addEventListener("abort",s,{once:!0}),e.addEventListener("abort",s,{once:!0}),(r.aborted||e.aborted)&&t.abort(),t.signal}async function $(r){return(await r.post("/chat/sessions",{botId:r.getBotId()})).sessionId}function A(r){return{sessionId:r.threadId??r.sessionId,botId:r.botId,title:r.title,createTime:r.createTime,updateTime:r.updateTime}}async function C(r,e){const t=await r.get("/chat/conversations",{botId:r.getBotId(),page:(e==null?void 0:e.page)??1,size:(e==null?void 0:e.size)??20});return{...t,items:t.items.map(A)}}async function _(r,e,t){await r.patch(`/chat/conversations/${e}`,{title:t})}async function x(r,e){await r.del(`/chat/conversations/${e}`)}async function E(r,e){const t=await r.get(`/chat/conversations/${e}/messages`);return P(t)}function P(r){const e=[...r].sort((n,c)=>{const i=new Date(n.createTime??0).getTime()||0,d=new Date(c.createTime??0).getTime()||0;return i!==d?i-d:(n.seq??0)-(c.seq??0)}),t=new Map;for(const n of e)n.role==="tool"&&n.toolCallId&&t.set(n.toolCallId,n.content||"");const s=new Set,o=[],a=(n,c,i)=>{const d=o[o.length-1];(d==null?void 0:d.role)==="assistant"?(d.parts.push(...n),d.id=c):o.push({id:c,role:"assistant",parts:[...n],timestamp:i})};for(const n of e){if(n.role==="system")continue;const c=String(n.id??n.messageId??`msg_${Date.now()}`),i=new Date(n.createTime??"").getTime()||Date.now();if(n.role==="human"){const d=(n.attachments??[]).map(l=>({type:"attachment",attachment:{nosKey:l.nosKey??l.attachmentId??"",filename:l.filename??"file",size:l.size??void 0,mime:l.mime??void 0,url:l.url??void 0}}));o.push({id:c,role:"user",parts:[...d,{type:"text",content:u(n.content)}],timestamp:i});continue}if(n.role==="ai"){const d=O(n,t,s);d.length>0&&a(d,c,i);continue}if(n.role==="tool"){if(n.toolCallId&&s.has(n.toolCallId))continue;a([N(n)],c,i)}}return o}function O(r,e,t){const s=[];r.reasoning&&s.push({type:"thinking",content:r.reasoning}),r.content&&s.push({type:"text",content:u(r.content)});for(const o of r.toolCalls??[])o.is_interrupt?(t.add(o.id),s.push(U(o,e))):s.push(v(o));return s}function U(r,e){const t=e.has(r.id),s=typeof r.args=="object"?r.args:{};return{type:"interrupt",interrupt:{interruptId:r.id??"",interruptType:s.interruptType??"input",content:s.content??"",options:s.options,placeholder:s.placeholder,inputType:s.inputType,maxLength:s.maxLength,required:s.required,fields:s.fields,metadata:s.metadata},resolved:t,response:t?{action:"submit",value:q(e.get(r.id))}:void 0}}function v(r){let e;try{e=typeof r.args=="string"?r.args:JSON.stringify(r.args??{})}catch{e=String(r.args??"{}")}return{type:"tool_call",toolName:r.name??"",toolCallId:r.id??"",args:e}}function N(r){var t,s,o;const e=Array.isArray(r.artifacts)?r.artifacts:void 0;return{type:"tool_result",toolName:r.toolName??"",toolCallId:r.toolCallId??"",content:u(r.content),artifacts:e,renderMode:((t=r.metadata)==null?void 0:t.renderMode)??((s=r.metadata)==null?void 0:s.render_mode),html:(o=r.metadata)==null?void 0:o.html}}function q(r){if(r!=null)try{return JSON.parse(r)}catch{return r}}async function D(r,e,t,s,o,a,n){const c=await r.streamPost(`/chat/interrupt/${e}/respond`,{sessionId:t,action:s.action,value:s.value},n);if(!c.ok){const i=new Error(`HTTP ${c.status}`);throw a==null||a(i),i}o&&await w(c,i=>{i.type==="error"?a==null||a(new Error(i.content)):o(i)},i=>a==null?void 0:a(i))}async function L(r){return r.get(`/chat/bots/${r.getBotId()}/skills`)}async function G(r,e){await r.post("/chat/stop",{sessionId:e})}function M(r){const e=new T(r);return{botId:e.getBotId(),getToken:()=>e.fetchToken(),createSession:()=>$(e),chat:s=>b(e,s),listConversations:s=>C(e,s),renameConversation:(s,o)=>_(e,s,o),deleteConversation:s=>x(e,s),getMessages:s=>E(e,s),respondInterrupt:(s,o,a,n,c,i)=>D(e,s,o,a,n,c,i),setToken:s=>e.setToken(s),feedback:async(s,o,a)=>{const n={type:o};o==="dislike"&&((a==null?void 0:a.reason)!=null&&(n.reason=a.reason),a!=null&&a.remark&&(n.remark=a.remark)),await e.post(`/chat/messages/${s}/feedback`,n)},cancelFeedback:async s=>{await e.del(`/chat/messages/${s}/feedback`)},listSkills:()=>L(e),stopGeneration:s=>G(e,s)}}exports.createNosUploader=f.createNosUploader;exports.createSuperAgent=M;
|
package/dist/index.d.ts
CHANGED
|
@@ -178,6 +178,12 @@ export declare interface SDKConfig {
|
|
|
178
178
|
baseUrl?: string;
|
|
179
179
|
appId?: string;
|
|
180
180
|
token?: string;
|
|
181
|
+
loginUrl?: string;
|
|
182
|
+
onAuthExpired?: (ctx: {
|
|
183
|
+
loginUrl?: string;
|
|
184
|
+
sessionId?: string;
|
|
185
|
+
botId: number;
|
|
186
|
+
}) => void;
|
|
181
187
|
}
|
|
182
188
|
|
|
183
189
|
declare interface SkillInfo {
|
package/dist/index.mjs
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { p as w, s as k } from "./nosUploader-CnYDALPr.js";
|
|
2
|
+
import { c as K } from "./nosUploader-CnYDALPr.js";
|
|
2
3
|
function h(r) {
|
|
3
4
|
if (Array.isArray(r)) return r.map((e) => h(e));
|
|
4
5
|
if (r && typeof r == "object" && r.constructor === Object) {
|
|
5
6
|
const e = {};
|
|
6
|
-
for (const [t,
|
|
7
|
-
const o = t.replace(/_([a-z])/g, (a,
|
|
8
|
-
e[o] = h(
|
|
7
|
+
for (const [t, s] of Object.entries(r)) {
|
|
8
|
+
const o = t.replace(/_([a-z])/g, (a, n) => n.toUpperCase());
|
|
9
|
+
e[o] = h(s);
|
|
9
10
|
}
|
|
10
11
|
return e;
|
|
11
12
|
}
|
|
12
13
|
return r;
|
|
13
14
|
}
|
|
14
|
-
const
|
|
15
|
-
class
|
|
15
|
+
const T = "/api/agent/runtime/v1";
|
|
16
|
+
class b {
|
|
16
17
|
constructor(e) {
|
|
17
18
|
this.refreshing = null;
|
|
18
|
-
const t = e.baseUrl ??
|
|
19
|
-
this.baseUrl = t.replace(/\/$/, ""), this.tokenGateway = e.tokenGateway.replace(/\/$/, ""), this.appId = e.appId ?? "", this.token = e.token ?? "", this.botId = e.botId, this.isAbsoluteUrl = /^https?:\/\//.test(this.baseUrl), this.isAbsoluteGateway = /^https?:\/\//.test(this.tokenGateway);
|
|
19
|
+
const t = e.baseUrl ?? T;
|
|
20
|
+
this.baseUrl = t.replace(/\/$/, ""), this.tokenGateway = e.tokenGateway.replace(/\/$/, ""), this.appId = e.appId ?? "", this.token = e.token ?? "", this.botId = e.botId, this.isAbsoluteUrl = /^https?:\/\//.test(this.baseUrl), this.isAbsoluteGateway = /^https?:\/\//.test(this.tokenGateway), this.loginUrl = e.loginUrl, this.onAuthExpired = e.onAuthExpired;
|
|
20
21
|
}
|
|
21
22
|
getBotId() {
|
|
22
23
|
return this.botId;
|
|
@@ -33,14 +34,14 @@ class T {
|
|
|
33
34
|
}
|
|
34
35
|
/** POST {origin}{tokenGateway}/v1/token — 获取 token + appId */
|
|
35
36
|
async fetchToken() {
|
|
36
|
-
const t = `${this.isAbsoluteGateway ? this.tokenGateway : `${this.getOrigin()}${this.tokenGateway}`}/v1/token`,
|
|
37
|
+
const t = `${this.isAbsoluteGateway ? this.tokenGateway : `${this.getOrigin()}${this.tokenGateway}`}/v1/token`, s = await fetch(t, {
|
|
37
38
|
method: "POST",
|
|
38
39
|
headers: { "Content-Type": "application/json" },
|
|
39
40
|
body: JSON.stringify({ botId: this.botId })
|
|
40
41
|
});
|
|
41
|
-
if (!
|
|
42
|
-
throw new Error(`HTTP ${
|
|
43
|
-
const o = await
|
|
42
|
+
if (!s.ok)
|
|
43
|
+
throw new Error(`HTTP ${s.status}: ${s.statusText}`);
|
|
44
|
+
const o = await s.json();
|
|
44
45
|
if (o.code !== "200")
|
|
45
46
|
throw new Error(o.message || `API error: ${o.code}`);
|
|
46
47
|
const a = h(o.data);
|
|
@@ -67,13 +68,13 @@ class T {
|
|
|
67
68
|
this.refreshing = null;
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
|
-
async request(e, t,
|
|
71
|
+
async request(e, t, s) {
|
|
71
72
|
if (!this.token && !await this.handleTokenRefresh())
|
|
72
73
|
throw new Error("SDK token not available: POST /token failed");
|
|
73
74
|
let o = this.isAbsoluteUrl ? `${this.baseUrl}${t}` : `${this.getOrigin()}${this.baseUrl}${t}`;
|
|
74
|
-
if (
|
|
75
|
+
if (s != null && s.params) {
|
|
75
76
|
const c = new URLSearchParams();
|
|
76
|
-
for (const [d, l] of Object.entries(
|
|
77
|
+
for (const [d, l] of Object.entries(s.params))
|
|
77
78
|
l != null && c.set(d, String(l));
|
|
78
79
|
const i = c.toString();
|
|
79
80
|
i && (o += `?${i}`);
|
|
@@ -81,16 +82,16 @@ class T {
|
|
|
81
82
|
const a = await fetch(o, {
|
|
82
83
|
method: e,
|
|
83
84
|
headers: this.headers(),
|
|
84
|
-
body:
|
|
85
|
+
body: s != null && s.body ? JSON.stringify(s.body) : void 0
|
|
85
86
|
});
|
|
86
|
-
if (a.status === 401 && !(
|
|
87
|
-
return this.request(e, t, { ...
|
|
87
|
+
if (a.status === 401 && !(s != null && s.retry) && await this.handleTokenRefresh())
|
|
88
|
+
return this.request(e, t, { ...s, retry: !0 });
|
|
88
89
|
if (!a.ok)
|
|
89
90
|
throw new Error(`HTTP ${a.status}: ${a.statusText}`);
|
|
90
|
-
const
|
|
91
|
-
if (
|
|
92
|
-
throw new Error(
|
|
93
|
-
return h(
|
|
91
|
+
const n = await a.json();
|
|
92
|
+
if (n.code !== "200")
|
|
93
|
+
throw new Error(n.message || `API error: ${n.code}`);
|
|
94
|
+
return h(n.data);
|
|
94
95
|
}
|
|
95
96
|
get(e, t) {
|
|
96
97
|
return this.request("GET", e, { params: t });
|
|
@@ -105,21 +106,39 @@ class T {
|
|
|
105
106
|
return this.request("DELETE", e);
|
|
106
107
|
}
|
|
107
108
|
/** For SSE streaming — returns raw Response (no envelope unwrap) */
|
|
108
|
-
async streamPost(e, t,
|
|
109
|
+
async streamPost(e, t, s) {
|
|
109
110
|
if (!this.token && !await this.handleTokenRefresh())
|
|
110
111
|
throw new Error("SDK token not available: POST /token failed");
|
|
111
112
|
const o = this.isAbsoluteUrl ? `${this.baseUrl}${e}` : `${this.getOrigin()}${this.baseUrl}${e}`, a = await fetch(o, {
|
|
112
113
|
method: "POST",
|
|
113
114
|
headers: this.headers(),
|
|
114
115
|
body: JSON.stringify(t),
|
|
115
|
-
signal:
|
|
116
|
+
signal: s
|
|
116
117
|
});
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
118
|
+
if (a.status === 401) {
|
|
119
|
+
const n = await w(a.clone());
|
|
120
|
+
if (n.isCookieExpired && typeof (t == null ? void 0 : t.message) == "string") {
|
|
121
|
+
k({
|
|
122
|
+
botId: this.botId,
|
|
123
|
+
message: t.message,
|
|
124
|
+
sessionId: t.sessionId ?? ""
|
|
125
|
+
});
|
|
126
|
+
const i = n.loginUrl ?? this.loginUrl;
|
|
127
|
+
return this.onAuthExpired ? this.onAuthExpired({
|
|
128
|
+
loginUrl: i,
|
|
129
|
+
sessionId: n.sessionId,
|
|
130
|
+
botId: this.botId
|
|
131
|
+
}) : i && window.location.replace(i), a;
|
|
132
|
+
}
|
|
133
|
+
if (await this.handleTokenRefresh())
|
|
134
|
+
return fetch(o, {
|
|
135
|
+
method: "POST",
|
|
136
|
+
headers: this.headers(),
|
|
137
|
+
body: JSON.stringify(t),
|
|
138
|
+
signal: s
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return a;
|
|
123
142
|
}
|
|
124
143
|
}
|
|
125
144
|
function u(r) {
|
|
@@ -134,7 +153,7 @@ function u(r) {
|
|
|
134
153
|
}
|
|
135
154
|
function I(r) {
|
|
136
155
|
const e = r.split(`
|
|
137
|
-
`).map((
|
|
156
|
+
`).map((s) => s.trim()).filter((s) => s.startsWith("data:")).map((s) => s.slice(5).trim());
|
|
138
157
|
if (!e.length) return null;
|
|
139
158
|
let t;
|
|
140
159
|
try {
|
|
@@ -212,13 +231,13 @@ async function p(r, e, t) {
|
|
|
212
231
|
t(new Error("Response body is null"));
|
|
213
232
|
return;
|
|
214
233
|
}
|
|
215
|
-
const
|
|
234
|
+
const s = r.body.getReader(), o = new TextDecoder();
|
|
216
235
|
let a = "";
|
|
217
236
|
try {
|
|
218
237
|
for (; ; ) {
|
|
219
|
-
const { value:
|
|
238
|
+
const { value: n, done: c } = await s.read();
|
|
220
239
|
if (c) break;
|
|
221
|
-
a += o.decode(
|
|
240
|
+
a += o.decode(n, { stream: !0 });
|
|
222
241
|
let i;
|
|
223
242
|
for (; (i = a.indexOf(`
|
|
224
243
|
|
|
@@ -230,72 +249,72 @@ async function p(r, e, t) {
|
|
|
230
249
|
}
|
|
231
250
|
}
|
|
232
251
|
if (a.trim()) {
|
|
233
|
-
const
|
|
234
|
-
|
|
252
|
+
const n = I(a);
|
|
253
|
+
n && e(n);
|
|
235
254
|
}
|
|
236
|
-
} catch (
|
|
237
|
-
(
|
|
255
|
+
} catch (n) {
|
|
256
|
+
(n == null ? void 0 : n.name) !== "AbortError" && t(n instanceof Error ? n : new Error(String(n)));
|
|
238
257
|
} finally {
|
|
239
258
|
try {
|
|
240
|
-
|
|
259
|
+
s.releaseLock();
|
|
241
260
|
} catch {
|
|
242
261
|
}
|
|
243
262
|
}
|
|
244
263
|
}
|
|
245
|
-
function
|
|
264
|
+
function S(r, e) {
|
|
246
265
|
var a;
|
|
247
|
-
const t = new AbortController(),
|
|
266
|
+
const t = new AbortController(), s = e.signal ? $(e.signal, t.signal) : t.signal, o = {
|
|
248
267
|
botId: r.getBotId(),
|
|
249
268
|
message: e.message,
|
|
250
269
|
sessionId: e.sessionId,
|
|
251
270
|
stream: e.stream !== !1,
|
|
252
271
|
// url 仅前端回显(下载/预览),剥离后发送——签名 URL 不进后端
|
|
253
272
|
...(a = e.attachments) != null && a.length ? {
|
|
254
|
-
attachments: e.attachments.map(({ url:
|
|
273
|
+
attachments: e.attachments.map(({ url: n, ...c }) => c)
|
|
255
274
|
} : {}
|
|
256
275
|
};
|
|
257
|
-
return o.stream ? r.streamPost("/chat", o,
|
|
276
|
+
return o.stream ? r.streamPost("/chat", o, s).then((n) => {
|
|
258
277
|
var c;
|
|
259
|
-
if (!
|
|
260
|
-
(c = e.onError) == null || c.call(e, new Error(`HTTP ${
|
|
278
|
+
if (!n.ok) {
|
|
279
|
+
(c = e.onError) == null || c.call(e, new Error(`HTTP ${n.status}`));
|
|
261
280
|
return;
|
|
262
281
|
}
|
|
263
282
|
return p(
|
|
264
|
-
|
|
283
|
+
n,
|
|
265
284
|
(i) => {
|
|
266
|
-
var d, l, f, g,
|
|
267
|
-
i.type === "done" && i.sessionId ? (d = e.onDone) == null || d.call(e, { sessionId: i.sessionId, content: i.content, messageId: i.messageId }) : i.type === "done" ? (l = e.onDone) == null || l.call(e, { sessionId: "", content: i.content, messageId: i.messageId }) : i.type === "stop" ? ((f = e.onMessage) == null || f.call(e, i), (g = e.onDone) == null || g.call(e, { sessionId: i.sessionId ?? "", content: i.content, messageId: i.messageId })) : i.type === "error" ? (
|
|
285
|
+
var d, l, f, g, m, y;
|
|
286
|
+
i.type === "done" && i.sessionId ? (d = e.onDone) == null || d.call(e, { sessionId: i.sessionId, content: i.content, messageId: i.messageId }) : i.type === "done" ? (l = e.onDone) == null || l.call(e, { sessionId: "", content: i.content, messageId: i.messageId }) : i.type === "stop" ? ((f = e.onMessage) == null || f.call(e, i), (g = e.onDone) == null || g.call(e, { sessionId: i.sessionId ?? "", content: i.content, messageId: i.messageId })) : i.type === "error" ? (m = e.onError) == null || m.call(e, new Error(i.content)) : (y = e.onMessage) == null || y.call(e, i);
|
|
268
287
|
},
|
|
269
288
|
(i) => {
|
|
270
289
|
var d;
|
|
271
290
|
return (d = e.onError) == null ? void 0 : d.call(e, i);
|
|
272
291
|
}
|
|
273
292
|
);
|
|
274
|
-
}).catch((
|
|
293
|
+
}).catch((n) => {
|
|
275
294
|
var c;
|
|
276
|
-
(
|
|
277
|
-
}) : r.post("/chat", o).then((
|
|
295
|
+
(n == null ? void 0 : n.name) !== "AbortError" && ((c = e.onError) == null || c.call(e, n instanceof Error ? n : new Error(String(n))));
|
|
296
|
+
}) : r.post("/chat", o).then((n) => {
|
|
278
297
|
var c;
|
|
279
298
|
(c = e.onDone) == null || c.call(e, {
|
|
280
|
-
sessionId:
|
|
281
|
-
content:
|
|
282
|
-
messageId:
|
|
299
|
+
sessionId: n.sessionId,
|
|
300
|
+
content: n.content ?? "",
|
|
301
|
+
messageId: n.messageId
|
|
283
302
|
});
|
|
284
|
-
}).catch((
|
|
303
|
+
}).catch((n) => {
|
|
285
304
|
var c;
|
|
286
|
-
(c = e.onError) == null || c.call(e,
|
|
305
|
+
(c = e.onError) == null || c.call(e, n instanceof Error ? n : new Error(String(n)));
|
|
287
306
|
}), t;
|
|
288
307
|
}
|
|
289
|
-
function
|
|
290
|
-
const t = new AbortController(),
|
|
291
|
-
return r.addEventListener("abort",
|
|
308
|
+
function $(r, e) {
|
|
309
|
+
const t = new AbortController(), s = () => t.abort();
|
|
310
|
+
return r.addEventListener("abort", s, { once: !0 }), e.addEventListener("abort", s, { once: !0 }), (r.aborted || e.aborted) && t.abort(), t.signal;
|
|
292
311
|
}
|
|
293
|
-
async function
|
|
312
|
+
async function A(r) {
|
|
294
313
|
return (await r.post("/chat/sessions", {
|
|
295
314
|
botId: r.getBotId()
|
|
296
315
|
})).sessionId;
|
|
297
316
|
}
|
|
298
|
-
function
|
|
317
|
+
function C(r) {
|
|
299
318
|
return {
|
|
300
319
|
// 后端返回 thread_id,toCamelCase 转为 threadId,映射为 sessionId
|
|
301
320
|
sessionId: r.threadId ?? r.sessionId,
|
|
@@ -305,7 +324,7 @@ function $(r) {
|
|
|
305
324
|
updateTime: r.updateTime
|
|
306
325
|
};
|
|
307
326
|
}
|
|
308
|
-
async function
|
|
327
|
+
async function x(r, e) {
|
|
309
328
|
const t = await r.get("/chat/conversations", {
|
|
310
329
|
botId: r.getBotId(),
|
|
311
330
|
page: (e == null ? void 0 : e.page) ?? 1,
|
|
@@ -313,35 +332,35 @@ async function A(r, e) {
|
|
|
313
332
|
});
|
|
314
333
|
return {
|
|
315
334
|
...t,
|
|
316
|
-
items: t.items.map(
|
|
335
|
+
items: t.items.map(C)
|
|
317
336
|
};
|
|
318
337
|
}
|
|
319
|
-
async function
|
|
338
|
+
async function _(r, e, t) {
|
|
320
339
|
await r.patch(`/chat/conversations/${e}`, { title: t });
|
|
321
340
|
}
|
|
322
|
-
async function
|
|
341
|
+
async function E(r, e) {
|
|
323
342
|
await r.del(`/chat/conversations/${e}`);
|
|
324
343
|
}
|
|
325
|
-
async function
|
|
344
|
+
async function P(r, e) {
|
|
326
345
|
const t = await r.get(`/chat/conversations/${e}/messages`);
|
|
327
|
-
return
|
|
346
|
+
return O(t);
|
|
328
347
|
}
|
|
329
|
-
function
|
|
330
|
-
const e = [...r].sort((
|
|
331
|
-
const i = new Date(
|
|
332
|
-
return i !== d ? i - d : (
|
|
348
|
+
function O(r) {
|
|
349
|
+
const e = [...r].sort((n, c) => {
|
|
350
|
+
const i = new Date(n.createTime ?? 0).getTime() || 0, d = new Date(c.createTime ?? 0).getTime() || 0;
|
|
351
|
+
return i !== d ? i - d : (n.seq ?? 0) - (c.seq ?? 0);
|
|
333
352
|
}), t = /* @__PURE__ */ new Map();
|
|
334
|
-
for (const
|
|
335
|
-
|
|
336
|
-
const
|
|
353
|
+
for (const n of e)
|
|
354
|
+
n.role === "tool" && n.toolCallId && t.set(n.toolCallId, n.content || "");
|
|
355
|
+
const s = /* @__PURE__ */ new Set(), o = [], a = (n, c, i) => {
|
|
337
356
|
const d = o[o.length - 1];
|
|
338
|
-
(d == null ? void 0 : d.role) === "assistant" ? (d.parts.push(...
|
|
357
|
+
(d == null ? void 0 : d.role) === "assistant" ? (d.parts.push(...n), d.id = c) : o.push({ id: c, role: "assistant", parts: [...n], timestamp: i });
|
|
339
358
|
};
|
|
340
|
-
for (const
|
|
341
|
-
if (
|
|
342
|
-
const c = String(
|
|
343
|
-
if (
|
|
344
|
-
const d = (
|
|
359
|
+
for (const n of e) {
|
|
360
|
+
if (n.role === "system") continue;
|
|
361
|
+
const c = String(n.id ?? n.messageId ?? `msg_${Date.now()}`), i = new Date(n.createTime ?? "").getTime() || Date.now();
|
|
362
|
+
if (n.role === "human") {
|
|
363
|
+
const d = (n.attachments ?? []).map((l) => ({
|
|
345
364
|
type: "attachment",
|
|
346
365
|
attachment: {
|
|
347
366
|
nosKey: l.nosKey ?? l.attachmentId ?? "",
|
|
@@ -356,51 +375,51 @@ function P(r) {
|
|
|
356
375
|
o.push({
|
|
357
376
|
id: c,
|
|
358
377
|
role: "user",
|
|
359
|
-
parts: [...d, { type: "text", content: u(
|
|
378
|
+
parts: [...d, { type: "text", content: u(n.content) }],
|
|
360
379
|
timestamp: i
|
|
361
380
|
});
|
|
362
381
|
continue;
|
|
363
382
|
}
|
|
364
|
-
if (
|
|
365
|
-
const d =
|
|
383
|
+
if (n.role === "ai") {
|
|
384
|
+
const d = U(n, t, s);
|
|
366
385
|
d.length > 0 && a(d, c, i);
|
|
367
386
|
continue;
|
|
368
387
|
}
|
|
369
|
-
if (
|
|
370
|
-
if (
|
|
371
|
-
a([
|
|
388
|
+
if (n.role === "tool") {
|
|
389
|
+
if (n.toolCallId && s.has(n.toolCallId)) continue;
|
|
390
|
+
a([N(n)], c, i);
|
|
372
391
|
}
|
|
373
392
|
}
|
|
374
393
|
return o;
|
|
375
394
|
}
|
|
376
|
-
function
|
|
377
|
-
const
|
|
378
|
-
r.reasoning &&
|
|
395
|
+
function U(r, e, t) {
|
|
396
|
+
const s = [];
|
|
397
|
+
r.reasoning && s.push({ type: "thinking", content: r.reasoning }), r.content && s.push({ type: "text", content: u(r.content) });
|
|
379
398
|
for (const o of r.toolCalls ?? [])
|
|
380
|
-
o.is_interrupt ? (t.add(o.id),
|
|
381
|
-
return
|
|
399
|
+
o.is_interrupt ? (t.add(o.id), s.push(v(o, e))) : s.push(D(o));
|
|
400
|
+
return s;
|
|
382
401
|
}
|
|
383
|
-
function
|
|
384
|
-
const t = e.has(r.id),
|
|
402
|
+
function v(r, e) {
|
|
403
|
+
const t = e.has(r.id), s = typeof r.args == "object" ? r.args : {};
|
|
385
404
|
return {
|
|
386
405
|
type: "interrupt",
|
|
387
406
|
interrupt: {
|
|
388
407
|
interruptId: r.id ?? "",
|
|
389
|
-
interruptType:
|
|
390
|
-
content:
|
|
391
|
-
options:
|
|
392
|
-
placeholder:
|
|
393
|
-
inputType:
|
|
394
|
-
maxLength:
|
|
395
|
-
required:
|
|
396
|
-
fields:
|
|
397
|
-
metadata:
|
|
408
|
+
interruptType: s.interruptType ?? "input",
|
|
409
|
+
content: s.content ?? "",
|
|
410
|
+
options: s.options,
|
|
411
|
+
placeholder: s.placeholder,
|
|
412
|
+
inputType: s.inputType,
|
|
413
|
+
maxLength: s.maxLength,
|
|
414
|
+
required: s.required,
|
|
415
|
+
fields: s.fields,
|
|
416
|
+
metadata: s.metadata
|
|
398
417
|
},
|
|
399
418
|
resolved: t,
|
|
400
|
-
response: t ? { action: "submit", value:
|
|
419
|
+
response: t ? { action: "submit", value: q(e.get(r.id)) } : void 0
|
|
401
420
|
};
|
|
402
421
|
}
|
|
403
|
-
function
|
|
422
|
+
function D(r) {
|
|
404
423
|
let e;
|
|
405
424
|
try {
|
|
406
425
|
e = typeof r.args == "string" ? r.args : JSON.stringify(r.args ?? {});
|
|
@@ -409,8 +428,8 @@ function U(r) {
|
|
|
409
428
|
}
|
|
410
429
|
return { type: "tool_call", toolName: r.name ?? "", toolCallId: r.id ?? "", args: e };
|
|
411
430
|
}
|
|
412
|
-
function
|
|
413
|
-
var t,
|
|
431
|
+
function N(r) {
|
|
432
|
+
var t, s, o;
|
|
414
433
|
const e = Array.isArray(r.artifacts) ? r.artifacts : void 0;
|
|
415
434
|
return {
|
|
416
435
|
type: "tool_result",
|
|
@@ -418,11 +437,11 @@ function v(r) {
|
|
|
418
437
|
toolCallId: r.toolCallId ?? "",
|
|
419
438
|
content: u(r.content),
|
|
420
439
|
artifacts: e,
|
|
421
|
-
renderMode: ((t = r.metadata) == null ? void 0 : t.renderMode) ?? ((
|
|
440
|
+
renderMode: ((t = r.metadata) == null ? void 0 : t.renderMode) ?? ((s = r.metadata) == null ? void 0 : s.render_mode),
|
|
422
441
|
html: (o = r.metadata) == null ? void 0 : o.html
|
|
423
442
|
};
|
|
424
443
|
}
|
|
425
|
-
function
|
|
444
|
+
function q(r) {
|
|
426
445
|
if (r != null)
|
|
427
446
|
try {
|
|
428
447
|
return JSON.parse(r);
|
|
@@ -430,15 +449,15 @@ function D(r) {
|
|
|
430
449
|
return r;
|
|
431
450
|
}
|
|
432
451
|
}
|
|
433
|
-
async function
|
|
452
|
+
async function L(r, e, t, s, o, a, n) {
|
|
434
453
|
const c = await r.streamPost(
|
|
435
454
|
`/chat/interrupt/${e}/respond`,
|
|
436
455
|
{
|
|
437
456
|
sessionId: t,
|
|
438
|
-
action:
|
|
439
|
-
value:
|
|
457
|
+
action: s.action,
|
|
458
|
+
value: s.value
|
|
440
459
|
},
|
|
441
|
-
|
|
460
|
+
n
|
|
442
461
|
);
|
|
443
462
|
if (!c.ok) {
|
|
444
463
|
const i = new Error(`HTTP ${c.status}`);
|
|
@@ -452,37 +471,37 @@ async function N(r, e, t, n, o, a, s) {
|
|
|
452
471
|
(i) => a == null ? void 0 : a(i)
|
|
453
472
|
);
|
|
454
473
|
}
|
|
455
|
-
async function
|
|
474
|
+
async function G(r) {
|
|
456
475
|
return r.get(`/chat/bots/${r.getBotId()}/skills`);
|
|
457
476
|
}
|
|
458
|
-
async function
|
|
477
|
+
async function R(r, e) {
|
|
459
478
|
await r.post("/chat/stop", { sessionId: e });
|
|
460
479
|
}
|
|
461
|
-
function
|
|
462
|
-
const e = new
|
|
480
|
+
function J(r) {
|
|
481
|
+
const e = new b(r);
|
|
463
482
|
return {
|
|
464
483
|
botId: e.getBotId(),
|
|
465
484
|
getToken: () => e.fetchToken(),
|
|
466
|
-
createSession: () =>
|
|
467
|
-
chat: (
|
|
468
|
-
listConversations: (
|
|
469
|
-
renameConversation: (
|
|
470
|
-
deleteConversation: (
|
|
471
|
-
getMessages: (
|
|
472
|
-
respondInterrupt: (
|
|
473
|
-
setToken: (
|
|
474
|
-
feedback: async (
|
|
475
|
-
const
|
|
476
|
-
o === "dislike" && ((a == null ? void 0 : a.reason) != null && (
|
|
485
|
+
createSession: () => A(e),
|
|
486
|
+
chat: (s) => S(e, s),
|
|
487
|
+
listConversations: (s) => x(e, s),
|
|
488
|
+
renameConversation: (s, o) => _(e, s, o),
|
|
489
|
+
deleteConversation: (s) => E(e, s),
|
|
490
|
+
getMessages: (s) => P(e, s),
|
|
491
|
+
respondInterrupt: (s, o, a, n, c, i) => L(e, s, o, a, n, c, i),
|
|
492
|
+
setToken: (s) => e.setToken(s),
|
|
493
|
+
feedback: async (s, o, a) => {
|
|
494
|
+
const n = { type: o };
|
|
495
|
+
o === "dislike" && ((a == null ? void 0 : a.reason) != null && (n.reason = a.reason), a != null && a.remark && (n.remark = a.remark)), await e.post(`/chat/messages/${s}/feedback`, n);
|
|
477
496
|
},
|
|
478
|
-
cancelFeedback: async (
|
|
479
|
-
await e.del(`/chat/messages/${
|
|
497
|
+
cancelFeedback: async (s) => {
|
|
498
|
+
await e.del(`/chat/messages/${s}/feedback`);
|
|
480
499
|
},
|
|
481
|
-
listSkills: () =>
|
|
482
|
-
stopGeneration: (
|
|
500
|
+
listSkills: () => G(e),
|
|
501
|
+
stopGeneration: (s) => R(e, s)
|
|
483
502
|
};
|
|
484
503
|
}
|
|
485
504
|
export {
|
|
486
|
-
|
|
487
|
-
|
|
505
|
+
K as createNosUploader,
|
|
506
|
+
J as createSuperAgent
|
|
488
507
|
};
|