@_daniel_jiang/host-sdk 1.0.0 → 1.1.0

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 CHANGED
@@ -1,38 +1,33 @@
1
- # @agent-platform/host-sdk
1
+ # @_daniel_jiang/host-sdk
2
2
 
3
3
  Agent 平台宿主嵌入 SDK(iframe 模式)。UI 由 Agent `/host/hub`、`/host/widget` 渲染;宿主只需 `init` + `registerPage`。
4
4
 
5
5
  表单提交与附件上传均在 **Agent Widget iframe 内**完成:用户点「开始」后,Widget 以 **multipart** 一次调用 `POST /api/v1/host/scenarios/{workflow}/run`(表单 JSON 字段 + 可选 `attachment_files`)。**不再**使用独立的 `POST /sessions/{id}/files`。
6
6
 
7
- ## 安装
8
-
9
- 宿主工程(EDC `boot-ui` / 专病库 `imedway-rdr-web`)使用**已拷贝到本仓的 vendor 包**,不依赖 agent-platform 源码树:
7
+ > 本文档随 npm 包发布,面向接入方(安装与用法)。本仓库维护者改 SDK / 发版说明见仓库根目录 `README.md` §4。
10
8
 
11
- ```bash
12
- # 依赖已指向本地副本,例如:
13
- # "@agent-platform/host-sdk": "file:./vendor/agent-platform-host-sdk"
14
- npm install
15
- ```
16
-
17
- 在 **agent-platform** 更新 SDK 后,于本目录执行同步(会先 `pnpm run build:sdk`):
9
+ ## 安装
18
10
 
19
11
  ```bash
20
- bash web/packages/host-sdk/sync-to-hosts.sh
21
- # 然后在对应宿主工程重新 npm install
12
+ npm install @_daniel_jiang/host-sdk
13
+ # yarn add @_daniel_jiang/host-sdk
22
14
  ```
23
15
 
24
- monorepo 内临时联调仍可指向源包:
25
-
26
- ```bash
27
- npm install file:../../../web/packages/host-sdk
16
+ ```json
17
+ {
18
+ "dependencies": {
19
+ "@_daniel_jiang/host-sdk": "^1.1.0"
20
+ }
21
+ }
28
22
  ```
29
23
 
30
- 安装前需已生成 `dist/`(`web/` 下执行 `pnpm run build:sdk`)。
24
+ 官方源:https://www.npmjs.com/package/@_daniel_jiang/host-sdk
25
+ 国内阿里云等私库一般可代理拉取。
31
26
 
32
27
  ## 用法
33
28
 
34
29
  ```javascript
35
- import { AgentHost } from '@agent-platform/host-sdk'
30
+ import { AgentHost } from '@_daniel_jiang/host-sdk'
36
31
 
37
32
  await AgentHost.init({
38
33
  host: 'edc',
@@ -90,6 +85,10 @@ AgentHost.unregisterPage('edc_patient_list')
90
85
 
91
86
  EDC / 专病库可用 `createAgentPageMixin` 自动处理进出页 register/unregister。
92
87
 
88
+ ### 无弹窗直跑(`kind: 'direct'`)
89
+
90
+ 缺省仍打开 Widget。`kind: 'direct'` 时点「立即开始」不弹窗:Hub 创建会话并发送 Chat 消息(`selected_skill_slugs`),**不**走 `POST /host/scenarios`。`hintPlacement: 'anchor'` + `anchor` 把横幅贴到控件旁,仅当该控件聚焦时显示;不设则仍贴在右下角 FAB 上方。完成时 `onRefresh` 可收到 `apply`(如 `{ remark }`)。旧宿主不升级、不传这些字段,行为与 1.0 相同。
91
+
93
92
  ### Widget 内提交流程(iframe,宿主无需处理文件)
94
93
 
95
94
  1. SDK 打开 `/host/widget?workflow=...` iframe
@@ -1,5 +1,5 @@
1
- var de=Object.defineProperty,fe=Object.defineProperties;var ge=Object.getOwnPropertyDescriptors;var U=Object.getOwnPropertySymbols;var pe=Object.prototype.hasOwnProperty,be=Object.prototype.propertyIsEnumerable;var _=(u,l,c)=>l in u?de(u,l,{enumerable:!0,configurable:!0,writable:!0,value:c}):u[l]=c,$=(u,l)=>{for(var c in l||(l={}))pe.call(l,c)&&_(u,c,l[c]);if(U)for(var c of U(l))be.call(l,c)&&_(u,c,l[c]);return u},F=(u,l)=>fe(u,ge(l));var h=(u,l,c)=>_(u,typeof l!="symbol"?l+"":l,c);var p=(u,l,c)=>new Promise((w,d)=>{var b=y=>{try{P(c.next(y))}catch(E){d(E)}},W=y=>{try{P(c.throw(y))}catch(E){d(E)}},P=y=>y.done?w(y.value):Promise.resolve(y.value).then(b,W);P((c=c.apply(u,l)).next())});(function(u){"use strict";const l="agent-host";function c(t){return typeof t=="object"&&t!==null&&t.channel===l&&typeof t.type=="string"}function w(t){return t==null?t:JSON.parse(JSON.stringify(t))}const d="agent-host-launcher",b="agent-host-hub-panel-overlay",W="agent-host-hub-iframe",P="agent-host-widget-overlay",y="agent-host-widget-iframe",E=400,j="agent-host-sdk-styles";function V(t){return t.replace(/\/$/,"")}function T(t){try{return new URL(t).origin}catch(e){return t.replace(/\/$/,"")}}function N(t,e){const n=T(t),i=T(e);if(n===i)return!0;try{const s=new URL(n),o=new URL(i),f=new Set(["localhost","127.0.0.1","[::1]"]);return s.protocol===o.protocol&&s.port===o.port&&f.has(s.hostname)&&f.has(o.hostname)}catch(s){return!1}}function G(){if(typeof document=="undefined"||document.getElementById(j))return;const t=document.createElement("style");t.id=j,t.textContent=`
2
- #${d} {
1
+ var Se=Object.defineProperty,Oe=Object.defineProperties;var Te=Object.getOwnPropertyDescriptors;var Z=Object.getOwnPropertySymbols;var Me=Object.prototype.hasOwnProperty,_e=Object.prototype.propertyIsEnumerable;var z=(g,l,u)=>l in g?Se(g,l,{enumerable:!0,configurable:!0,writable:!0,value:u}):g[l]=u,F=(g,l)=>{for(var u in l||(l={}))Me.call(l,u)&&z(g,u,l[u]);if(Z)for(var u of Z(l))_e.call(l,u)&&z(g,u,l[u]);return g},Q=(g,l)=>Oe(g,Te(l));var h=(g,l,u)=>z(g,typeof l!="symbol"?l+"":l,u);var m=(g,l,u)=>new Promise((k,v)=>{var x=b=>{try{d(u.next(b))}catch(p){v(p)}},L=b=>{try{d(u.throw(b))}catch(p){v(p)}},d=b=>b.done?k(b.value):Promise.resolve(b.value).then(x,L);d((u=u.apply(g,l)).next())});(function(g){"use strict";const l="agent-host";function u(t){return typeof t=="object"&&t!==null&&t.channel===l&&typeof t.type=="string"}function k(t){return t==null?t:JSON.parse(JSON.stringify(t))}function v(t){return typeof t=="function"?t():t}const x={},L={};let d=null,b=!1,p=null,T=null,U=!1;function q(t){T=t}function R(){try{T==null||T()}catch(t){}}function _(t){var o;if(!t)return null;const e=typeof t=="function"?t():t;if(!e)return null;let n=null;if(e instanceof Element?n=e:typeof e=="object"&&e.$el instanceof Element&&(n=e.$el),!n)return null;const i=(o=n.querySelector)==null?void 0:o.call(n,'textarea, input, [contenteditable="true"]');return i instanceof HTMLElement?i:n}function $(t){return!t||typeof t.closest!="function"?!1:!!t.closest("#agent-host-launcher-hint")}function B(t){return!!t.anchor&&t.hintPlacement==="anchor"}function ee(t,e){if(!d){p&&(p=null,R());return}if($(t)||$(e))return;const n=x[d]||[];let i=null;if(t instanceof Node)for(const o of n){if(!B(o))continue;const s=_(o.anchor);if(s&&s.contains(t)){i=o.id;break}}i!==p&&(p=i,R())}function te(t){ee(t.target)}function ne(t){if($(t.relatedTarget))return;const e=t.target;if(!(e instanceof Node)||!d)return;const i=(x[d]||[]).find(s=>s.id===p);if(!i)return;const o=_(i.anchor);o&&o.contains(e)&&!o.contains(t.relatedTarget)&&(p=null,R())}function ie(){U||typeof document=="undefined"||(document.addEventListener("focusin",te,!0),document.addEventListener("focusout",ne,!0),U=!0)}function oe(t,e){const n=(e||"").trim();if(n)return n;for(const i of t){if(!i.hostContext)continue;const o=v(i.hostContext),s=o&&typeof o=="object"?String(o.refresh||"").trim():"";if(s)return s}}function V(t){return((t==null?void 0:t.kind)||"widget")==="direct"}function se(t,e,n={}){ie();const i=t!==d;i&&(b=!1,p=null),x[t]=e,d=t,!i&&e.some(s=>!s.enabled||s.enabled())&&(b=!1);const o=oe(e,n.refreshKey);o&&typeof n.onRefresh=="function"?L[t]={refreshKey:o,onRefresh:n.onRefresh}:delete L[t],R()}function re(t){delete x[t],delete L[t],d===t&&(d=null,p=null),R()}function ae(t,e){const n=String(t||"").trim();if(n){for(const i of Object.values(L))if(i.refreshKey===n)try{i.onRefresh(e)}catch(o){}}}function le(){for(const t of Object.keys(x))delete x[t];for(const t of Object.keys(L))delete L[t];d=null,b=!1,p=null,R()}function he(t){t!==d&&(b=!1,p=null),d=t,R()}function ce(){b=!0}function N(t){return t.map(e=>({id:e.id,workflow:e.workflow,label:e.label,host:e.host,hint:e.hint?v(e.hint):void 0,hostContext:e.hostContext?k(v(e.hostContext)):void 0,prefilled:e.prefilled?k(v(e.prefilled)):void 0,enabled:e.enabled?e.enabled():!0,kind:e.kind,skill:e.skill}))}function ue(t,e){const n=(t.workflow||t.skill||"").trim();return e?!(n==="edc_form_fill"&&!e.sub_project_id&&!e.subProjectId||n==="edc_crf_design"&&!e.sub_project_id&&!e.subProjectId||n==="sds_crf_schema_from_doc"&&!e.index_id&&!e.indexId||n==="sds-project-remark"&&!String(e.project_name||e.name||"").trim()):!0}function W(t,e){const n=(x[t]||[]).find(o=>o.id===e);if(!n||n.enabled&&!n.enabled())return null;const i=n.prefilled?k(v(n.prefilled)):void 0;return i&&typeof i=="object"&&!ue(n,i)?null:{action:n,prefilled:i,hostContext:n.hostContext?k(v(n.hostContext)):void 0}}function de(t,e){const n=W(t,e);return!n||V(n.action)?null:{workflow:n.action.workflow,title:n.action.label,host:n.action.host,prefilled:n.prefilled,hostContext:n.hostContext}}function fe(){return d}function ge(){return Object.entries(x).map(([t,e])=>({pageId:t,actions:e}))}function G(t,e){const n=(x[t]||[]).find(i=>i.id===e);return n?_(n.anchor):null}function pe(t,e){const n=G(t,e);if(!n)return null;const i=n.getBoundingClientRect();return!i.width&&!i.height?null:i}function be(){if(b||!d)return null;const t=x[d]||[];for(const e of t){if(e.enabled&&!e.enabled()||B(e)&&p!==e.id)continue;const n=e.hint?v(e.hint):`可使用「${e.label}」`,i=B(e)?"anchor":"launcher";return{pageId:d,actionId:e.id,hint:n,placement:i}}return null}const w="agent-host-launcher",H="agent-host-hub-panel-overlay",we="agent-host-hub-iframe",K="agent-host-widget-overlay",me="agent-host-widget-iframe",ye=400,Y="agent-host-sdk-styles";function xe(t){return t.replace(/\/$/,"")}function M(t){try{return new URL(t).origin}catch(e){return t.replace(/\/$/,"")}}function ve(t,e){const n=M(t),i=M(e);if(n===i)return!0;try{const o=new URL(n),s=new URL(i),f=new Set(["localhost","127.0.0.1","[::1]"]);return o.protocol===s.protocol&&o.port===s.port&&f.has(o.hostname)&&f.has(s.hostname)}catch(o){return!1}}function He(){if(typeof document=="undefined"||document.getElementById(Y))return;const t=document.createElement("style");t.id=Y,t.textContent=`
2
+ #${w} {
3
3
  position: fixed;
4
4
  right: 24px;
5
5
  bottom: 24px;
@@ -19,35 +19,35 @@ var de=Object.defineProperty,fe=Object.defineProperties;var ge=Object.getOwnProp
19
19
  box-shadow: 0 4px 16px rgba(79, 70, 229, 0.45);
20
20
  transition: transform 0.15s ease;
21
21
  }
22
- #${d}:hover { transform: scale(1.05); }
23
- #${d}.is-pulse {
22
+ #${w}:hover { transform: scale(1.05); }
23
+ #${w}.is-pulse {
24
24
  animation: agent-host-fab-pulse 2s ease-in-out infinite;
25
25
  }
26
- #${d} .agent-host-launcher-icon {
26
+ #${w} .agent-host-launcher-icon {
27
27
  display: inline-block;
28
28
  line-height: 1;
29
29
  transform-origin: center center;
30
30
  }
31
- #${d}.is-breathe .agent-host-launcher-icon {
31
+ #${w}.is-breathe .agent-host-launcher-icon {
32
32
  animation: agent-host-clover-breathe 1.8s ease-in-out infinite;
33
33
  }
34
- #${d} .agent-host-launcher-bell {
34
+ #${w} .agent-host-launcher-bell {
35
35
  display: none;
36
36
  width: 22px;
37
37
  height: 22px;
38
38
  line-height: 0;
39
39
  transform-origin: top center;
40
40
  }
41
- #${d} .agent-host-launcher-bell.is-visible {
41
+ #${w} .agent-host-launcher-bell.is-visible {
42
42
  display: inline-block;
43
43
  animation: agent-host-bell-shake 0.85s ease-in-out infinite;
44
44
  }
45
- #${d} .agent-host-launcher-bell svg {
45
+ #${w} .agent-host-launcher-bell svg {
46
46
  width: 100%;
47
47
  height: 100%;
48
48
  display: block;
49
49
  }
50
- #${d} .agent-host-launcher-badge {
50
+ #${w} .agent-host-launcher-badge {
51
51
  position: absolute;
52
52
  top: -4px;
53
53
  right: -4px;
@@ -66,8 +66,8 @@ var de=Object.defineProperty,fe=Object.defineProperties;var ge=Object.getOwnProp
66
66
  background: #f59e0b;
67
67
  color: #fff;
68
68
  }
69
- #${d} .agent-host-launcher-badge.is-visible { display: flex; }
70
- #${d} .agent-host-launcher-badge.is-waiting { background: #ef4444; }
69
+ #${w} .agent-host-launcher-badge.is-visible { display: flex; }
70
+ #${w} .agent-host-launcher-badge.is-waiting { background: #ef4444; }
71
71
  #agent-host-launcher-hint {
72
72
  position: fixed;
73
73
  right: 24px;
@@ -86,6 +86,10 @@ var de=Object.defineProperty,fe=Object.defineProperties;var ge=Object.getOwnProp
86
86
  pointer-events: auto;
87
87
  }
88
88
  #agent-host-launcher-hint.is-visible { display: block; }
89
+ #agent-host-launcher-hint.is-anchored {
90
+ right: auto;
91
+ bottom: auto;
92
+ }
89
93
  #agent-host-launcher-hint .agent-host-hint-inner {
90
94
  display: flex;
91
95
  align-items: center;
@@ -142,7 +146,7 @@ var de=Object.defineProperty,fe=Object.defineProperties;var ge=Object.getOwnProp
142
146
  cursor: pointer;
143
147
  }
144
148
  #agent-host-launcher-hint .agent-host-hint-dismiss:hover { color: #666; }
145
- #${b} {
149
+ #${H} {
146
150
  position: fixed;
147
151
  inset: 0;
148
152
  z-index: 2147483000;
@@ -150,34 +154,34 @@ var de=Object.defineProperty,fe=Object.defineProperties;var ge=Object.getOwnProp
150
154
  opacity: 0;
151
155
  transition: opacity 0.2s ease;
152
156
  }
153
- #${b}.is-open {
157
+ #${H}.is-open {
154
158
  pointer-events: auto;
155
159
  opacity: 1;
156
160
  }
157
- #${b} .agent-host-hub-backdrop {
161
+ #${H} .agent-host-hub-backdrop {
158
162
  position: absolute;
159
163
  inset: 0;
160
164
  background: rgba(15, 23, 42, 0.45);
161
165
  }
162
- #${b} .agent-host-hub-panel-shell {
166
+ #${H} .agent-host-hub-panel-shell {
163
167
  position: absolute;
164
168
  top: 0;
165
169
  right: 0;
166
170
  bottom: 0;
167
- width: min(${E}px, 100vw);
171
+ width: min(${ye}px, 100vw);
168
172
  transform: translateX(100%);
169
173
  transition: transform 0.28s cubic-bezier(0.2, 0, 0, 1), width 0.28s cubic-bezier(0.2, 0, 0, 1);
170
174
  background: #fff;
171
175
  box-shadow: -8px 0 24px rgba(15, 23, 42, 0.14);
172
176
  overflow: hidden;
173
177
  }
174
- #${b} .agent-host-hub-panel-shell.is-expanded {
178
+ #${H} .agent-host-hub-panel-shell.is-expanded {
175
179
  width: min(50vw, 100vw);
176
180
  }
177
- #${b}.is-open .agent-host-hub-panel-shell {
181
+ #${H}.is-open .agent-host-hub-panel-shell {
178
182
  transform: translateX(0);
179
183
  }
180
- #${b} iframe {
184
+ #${H} iframe {
181
185
  width: 100%;
182
186
  height: 100%;
183
187
  border: none;
@@ -211,7 +215,7 @@ var de=Object.defineProperty,fe=Object.defineProperties;var ge=Object.getOwnProp
211
215
  72% { transform: rotate(-3deg); }
212
216
  84% { transform: rotate(1deg); }
213
217
  }
214
- `,document.head.appendChild(t)}class K{constructor(e,n,i){h(this,"agentOrigin","");h(this,"hubLauncher",null);h(this,"hubLauncherBadge",null);h(this,"hubLauncherHint",null);h(this,"hubPanelOverlay",null);h(this,"hubIframe",null);h(this,"widgetIframe",null);h(this,"hubReady",!1);h(this,"widgetReady",!1);h(this,"hubPanelOpen",!1);h(this,"hubPanelExpanded",!1);h(this,"lastRunning",0);h(this,"lastWaiting",0);h(this,"launcherHintAction",null);h(this,"pendingHubMessages",[]);h(this,"pendingWidgetMessages",[]);h(this,"onChildMessage");h(this,"onHintUse");h(this,"onHintDismiss");h(this,"handleMessage",e=>{var i,s;if(!N(this.agentOrigin,e.origin)||!c(e.data))return;const n=e.data;if(n.type==="ready"){n.surface==="hub"?(this.hubReady=!0,this.flush("hub")):(this.widgetReady=!0,this.flush("widget"));return}if(n.type==="hub-panel-close"){this.closeHubPanel();return}if(n.type==="hub-panel-width"){this.setHubPanelExpanded(!!n.expanded);return}if(n.type==="hub-stats"){this.updateLauncherStats(n.running,n.waiting),(i=this.onChildMessage)==null||i.call(this,n);return}(s=this.onChildMessage)==null||s.call(this,n)});this.agentOrigin=V(e),this.onChildMessage=n,this.onHintUse=i==null?void 0:i.onHintUse,this.onHintDismiss=i==null?void 0:i.onHintDismiss,window.addEventListener("message",this.handleMessage)}destroy(){window.removeEventListener("message",this.handleMessage),this.removeHub(),this.closeWidget()}mountHub(){this.hubPanelOverlay||typeof document=="undefined"||(G(),this.mountLauncher(),this.mountHubPanel())}openHubPanel(){var e;this.hubPanelOverlay&&(this.hubPanelOpen=!0,this.hubPanelOverlay.classList.add("is-open"),(e=this.hubLauncherHint)==null||e.classList.remove("is-visible"),this.post("hub",{channel:l,type:"open-hub-panel"}),this.applyLauncherVisual())}closeHubPanel(){this.hubPanelOverlay&&(this.hubPanelOpen=!1,this.hubPanelOverlay.classList.remove("is-open"),this.setHubPanelExpanded(!1),this.post("hub",{channel:l,type:"close-hub-panel"}),this.updateLauncherHint(this.launcherHintAction),this.applyLauncherVisual())}setHubPanelExpanded(e){var i;this.hubPanelExpanded=e;const n=(i=this.hubPanelOverlay)==null?void 0:i.querySelector(".agent-host-hub-panel-shell");n==null||n.classList.toggle("is-expanded",e)}updateLauncherHint(e){var s,o;this.launcherHintAction=e;const n=this.hubLauncherHint;if(!n)return;if(this.hubPanelOpen){n.classList.remove("is-visible");return}if(!e||!e.hint.trim()){n.innerHTML="",n.classList.remove("is-visible");return}n.innerHTML=`
218
+ `,document.head.appendChild(t)}class ke{constructor(e,n,i){h(this,"agentOrigin","");h(this,"hubLauncher",null);h(this,"hubLauncherBadge",null);h(this,"hubLauncherHint",null);h(this,"hubPanelOverlay",null);h(this,"hubIframe",null);h(this,"widgetIframe",null);h(this,"hubReady",!1);h(this,"widgetReady",!1);h(this,"hubPanelOpen",!1);h(this,"hubPanelExpanded",!1);h(this,"lastRunning",0);h(this,"lastWaiting",0);h(this,"launcherHintAction",null);h(this,"hintRepositionBound",!1);h(this,"hintScrollParents",[]);h(this,"pendingHubMessages",[]);h(this,"pendingWidgetMessages",[]);h(this,"onChildMessage");h(this,"onHintUse");h(this,"onHintDismiss");h(this,"onHintReposition",()=>{const e=this.launcherHintAction,n=this.hubLauncherHint;!e||!n||!n.classList.contains("is-visible")||this.layoutHint(n,e)});h(this,"handleMessage",e=>{var i,o;if(!ve(this.agentOrigin,e.origin)||!u(e.data))return;const n=e.data;if(n.type==="ready"){n.surface==="hub"?(this.hubReady=!0,this.flush("hub")):(this.widgetReady=!0,this.flush("widget"));return}if(n.type==="hub-panel-close"){this.closeHubPanel();return}if(n.type==="hub-panel-width"){this.setHubPanelExpanded(!!n.expanded);return}if(n.type==="hub-stats"){this.updateLauncherStats(n.running,n.waiting),(i=this.onChildMessage)==null||i.call(this,n);return}(o=this.onChildMessage)==null||o.call(this,n)});this.agentOrigin=xe(e),this.onChildMessage=n,this.onHintUse=i==null?void 0:i.onHintUse,this.onHintDismiss=i==null?void 0:i.onHintDismiss,window.addEventListener("message",this.handleMessage)}destroy(){window.removeEventListener("message",this.handleMessage),this.unbindHintReposition(),this.removeHub(),this.closeWidget()}mountHub(){this.hubPanelOverlay||typeof document=="undefined"||(He(),this.mountLauncher(),this.mountHubPanel())}openHubPanel(){var e;this.hubPanelOverlay&&(this.hubPanelOpen=!0,this.hubPanelOverlay.classList.add("is-open"),(e=this.hubLauncherHint)==null||e.classList.remove("is-visible"),this.post("hub",{channel:l,type:"open-hub-panel"}),this.applyLauncherVisual())}closeHubPanel(){this.hubPanelOverlay&&(this.hubPanelOpen=!1,this.hubPanelOverlay.classList.remove("is-open"),this.setHubPanelExpanded(!1),this.post("hub",{channel:l,type:"close-hub-panel"}),this.updateLauncherHint(this.launcherHintAction),this.applyLauncherVisual())}setHubPanelExpanded(e){var i;this.hubPanelExpanded=e;const n=(i=this.hubPanelOverlay)==null?void 0:i.querySelector(".agent-host-hub-panel-shell");n==null||n.classList.toggle("is-expanded",e)}updateLauncherHint(e){this.launcherHintAction=e;const n=this.hubLauncherHint;if(!n)return;if(this.hubPanelOpen){n.classList.remove("is-visible","is-anchored"),this.unbindHintReposition();return}if(!e||!e.hint.trim()){n.innerHTML="",n.classList.remove("is-visible","is-anchored"),this.unbindHintReposition();return}n.innerHTML=`
215
219
  <div class="agent-host-hint-inner">
216
220
  <span class="agent-host-hint-icon" aria-hidden="true">✦</span>
217
221
  <span class="agent-host-hint-text"></span>
@@ -219,4 +223,4 @@ var de=Object.defineProperty,fe=Object.defineProperties;var ge=Object.getOwnProp
219
223
  <button type="button" class="agent-host-hint-use">立即开始</button>
220
224
  <button type="button" class="agent-host-hint-dismiss">稍后</button>
221
225
  </div>
222
- </div>`;const i=n.querySelector(".agent-host-hint-text");i&&(i.textContent=e.hint),(s=n.querySelector(".agent-host-hint-use"))==null||s.addEventListener("click",f=>{var g;f.stopPropagation(),(g=this.onHintUse)==null||g.call(this,e.pageId,e.actionId)}),(o=n.querySelector(".agent-host-hint-dismiss"))==null||o.addEventListener("click",f=>{var g;f.stopPropagation(),(g=this.onHintDismiss)==null||g.call(this)}),n.classList.add("is-visible")}mountLauncher(){if(this.hubLauncher||document.getElementById(d))return;const e=document.createElement("button");if(e.id=d,e.type="button",e.title="科研AI助手",e.setAttribute("aria-label","科研AI助手"),e.innerHTML='<span class="agent-host-launcher-icon" aria-hidden="true">✦</span><span class="agent-host-launcher-bell" aria-hidden="true"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 3a5 5 0 0 0-5 5v1.5c0 1.3-.4 2.6-1.2 3.7L4.5 15.5c-.4.5-.1 1.3.5 1.3h14c.6 0 .9-.8.5-1.3l-1.3-2.3A6.6 6.6 0 0 1 17 9.5V8a5 5 0 0 0-5-5Z" fill="#f59e0b"/><path d="M10 18.5a2 2 0 0 0 4 0" stroke="#f59e0b" stroke-width="1.8" stroke-linecap="round"/></svg></span><span class="agent-host-launcher-badge"></span>',e.addEventListener("click",()=>this.openHubPanel()),document.body.appendChild(e),this.hubLauncher=e,this.hubLauncherBadge=e.querySelector(".agent-host-launcher-badge"),document.getElementById("agent-host-launcher-hint"))this.hubLauncherHint=document.getElementById("agent-host-launcher-hint");else{const n=document.createElement("div");n.id="agent-host-launcher-hint",document.body.appendChild(n),this.hubLauncherHint=n}}mountHubPanel(){const e=document.createElement("div");e.id=b;const n=document.createElement("div");n.className="agent-host-hub-backdrop",n.addEventListener("click",()=>this.closeHubPanel());const i=document.createElement("div");i.className="agent-host-hub-panel-shell";const s=document.createElement("iframe");s.id=W,s.title="科研AI助手",s.src=`${this.agentOrigin}/host/hub?embed=1&panel=1`,s.allow="clipboard-read; clipboard-write",i.appendChild(s),e.appendChild(n),e.appendChild(i),document.body.appendChild(e),this.hubPanelOverlay=e,this.hubIframe=s}updateLauncherStats(e,n){this.lastRunning=e,this.lastWaiting=n,this.applyLauncherVisual()}applyLauncherVisual(){const e=this.hubLauncher,n=this.hubLauncherBadge;if(!e||!n)return;const i=e.querySelector(".agent-host-launcher-icon"),s=e.querySelector(".agent-host-launcher-bell"),o=this.lastRunning,f=this.lastWaiting,g=f>0&&!this.hubPanelOpen;e.classList.toggle("is-pulse",o>0),e.classList.toggle("is-breathe",(o>0||f>0)&&!g),g?(e.title="有待确认事项,点击打开助手",e.setAttribute("aria-label","有待确认事项,点击打开助手")):(e.title="科研AI助手",e.setAttribute("aria-label","科研AI助手")),i&&(i.style.display=g?"none":""),s&&s.classList.toggle("is-visible",g),o>0?(n.textContent=String(o),n.classList.add("is-visible"),n.classList.remove("is-waiting")):f>0?(n.textContent="!",n.classList.add("is-visible","is-waiting")):(n.textContent="",n.classList.remove("is-visible","is-waiting"))}removeHub(){var e,n,i;(e=document.getElementById(d))==null||e.remove(),(n=document.getElementById("agent-host-launcher-hint"))==null||n.remove(),(i=document.getElementById(b))==null||i.remove(),this.hubLauncher=null,this.hubLauncherBadge=null,this.hubLauncherHint=null,this.hubPanelOverlay=null,this.hubIframe=null,this.hubReady=!1,this.hubPanelOpen=!1,this.hubPanelExpanded=!1,this.pendingHubMessages=[]}openWidget(e,n,i){this.closeWidget();const s=document.createElement("div");s.id=P,s.style.cssText="position:fixed;inset:0;z-index:2147483646;background:rgba(15,23,42,0.45);display:flex;align-items:center;justify-content:center;padding:16px;",s.addEventListener("click",ue=>{ue.target===s&&this.closeWidget()});const o=document.createElement("iframe");o.id=y,o.title=e.title||e.workflow;const f=new URLSearchParams({embed:"1",workflow:e.workflow,host:e.host||"",title:e.title||""});o.src=`${this.agentOrigin}/host/widget?${f.toString()}`,o.style.cssText="width:min(560px,100%);height:80vh;border:none;border-radius:12px;background:#fff;box-shadow:0 20px 60px rgba(0,0,0,0.25);display:block;",s.appendChild(o),document.body.appendChild(s),this.widgetIframe=o,this.widgetReady=!1;const g={channel:l,type:"init",token:n,refreshToken:i},he={workflow:e.workflow,title:e.title,host:e.host,prefilled:e.prefilled?w(e.prefilled):void 0,hostContext:e.hostContext?w(e.hostContext):void 0,sessionId:e.sessionId,preferSandboxFiles:e.preferSandboxFiles},ce={channel:l,type:"open-widget",payload:he};this.pendingWidgetMessages=[g,ce]}closeWidget(){var e;(e=document.getElementById(P))==null||e.remove(),this.widgetIframe=null,this.widgetReady=!1,this.pendingWidgetMessages=[]}sendToHub(e){this.post("hub",e)}post(e,n){var f;const i=e==="hub"?this.hubReady:this.widgetReady,s=e==="hub"?this.pendingHubMessages:this.pendingWidgetMessages;if(!i){s.push(n);return}const o=e==="hub"?this.hubIframe:this.widgetIframe;(f=o==null?void 0:o.contentWindow)==null||f.postMessage(n,T(this.agentOrigin))}flush(e){const n=e==="hub"?this.pendingHubMessages:this.pendingWidgetMessages,i=e==="hub"?this.hubIframe:this.widgetIframe;if(!(i!=null&&i.contentWindow))return;const s=T(this.agentOrigin);for(;n.length;){const o=n.shift();o&&i.contentWindow.postMessage(o,s)}}}function k(t){return typeof t=="function"?t():t}const H={},I={};let m=null,O=!1;function Y(t,e){const n=(e||"").trim();if(n)return n;for(const i of t){if(!i.hostContext)continue;const s=k(i.hostContext),o=s&&typeof s=="object"?String(s.refresh||"").trim():"";if(o)return o}}function J(t,e,n={}){const i=t!==m;i&&(O=!1),H[t]=e,m=t,!i&&e.some(o=>!o.enabled||o.enabled())&&(O=!1);const s=Y(e,n.refreshKey);s&&typeof n.onRefresh=="function"?I[t]={refreshKey:s,onRefresh:n.onRefresh}:delete I[t]}function X(t){delete H[t],delete I[t],m===t&&(m=null)}function Z(t){const e=String(t||"").trim();if(e){for(const n of Object.values(I))if(n.refreshKey===e)try{n.onRefresh()}catch(i){}}}function Q(){for(const t of Object.keys(H))delete H[t];for(const t of Object.keys(I))delete I[t];m=null,O=!1}function ee(t){t!==m&&(O=!1),m=t}function te(){O=!0}function z(t){return t.map(e=>({id:e.id,workflow:e.workflow,label:e.label,host:e.host,hint:e.hint?k(e.hint):void 0,hostContext:e.hostContext?w(k(e.hostContext)):void 0,prefilled:e.prefilled?w(k(e.prefilled)):void 0,enabled:e.enabled?e.enabled():!0}))}function B(t,e){const n=(H[t]||[]).find(o=>o.id===e);if(!n||n.enabled&&!n.enabled())return null;const i=n.prefilled?w(k(n.prefilled)):void 0,s=(n.workflow||"").trim();if(s==="edc_form_fill"&&i&&typeof i=="object"){const o=i;if(!o.sub_project_id&&!o.subProjectId)return null}if(s==="edc_crf_design"&&i&&typeof i=="object"){const o=i;if(!o.sub_project_id&&!o.subProjectId)return null}return{workflow:n.workflow,title:n.label,host:n.host,prefilled:i,hostContext:n.hostContext?w(k(n.hostContext)):void 0}}function ne(){return m}function ie(){return Object.entries(H).map(([t,e])=>({pageId:t,actions:e}))}function se(){if(O||!m)return null;const t=H[m]||[];for(const e of t){if(e.enabled&&!e.enabled())continue;const n=e.hint?k(e.hint):`可使用「${e.label}」`;return{pageId:m,actionId:e.id,hint:n}}return null}let r={},a=null,x="",A="",v=null;function oe(){const t=(r.agentOrigin||"").replace(/\/$/,"");if(!t)throw new Error("Agent 前端地址未配置:请在 init({ agentOrigin }) 传入,或由宿主 BFF bootstrap 返回 agent_web_origin");return t}function D(t){var e;t.agent_web_origin&&(r.agentOrigin=t.agent_web_origin.replace(/\/$/,"")),t.agent_token&&(x=t.agent_token),t.refresh_token&&(A=t.refresh_token),(e=r.onBootstrap)==null||e.call(r,t)}function R(){return p(this,null,function*(){if(r.getToken)return x=yield r.getToken(),x;if(x)return x;if(r.bootstrap){const t=yield r.bootstrap();if(D(t),x)return x}throw new Error("未连接 Agent:请配置 bootstrap 或 getToken")})}function C(){if(r.agentOrigin)try{L().updateLauncherHint(se())}catch(t){}}function L(){const t=oe();return a||(a=new K(t,re,{onHintUse:(e,n)=>{const i=B(e,n);i&&M(i)},onHintDismiss:()=>{te(),C()}}),r.showHub!==!1&&a.mountHub()),a}function ae(){if(!(!a||!r.agentOrigin)){for(const{pageId:t,actions:e}of ie())a.sendToHub({channel:"agent-host",type:"register-page",pageId:t,actions:z(e)});a.sendToHub({channel:"agent-host",type:"set-page",pageId:ne()}),C()}}function re(t){var e;if(t.type==="ready"&&t.surface==="hub"&&R().then(n=>{a==null||a.sendToHub({channel:"agent-host",type:"init",token:n,refreshToken:A||void 0}),ae()}).catch(()=>{}),t.type==="started"&&(a==null||a.closeWidget(),a==null||a.openHubPanel(),a==null||a.sendToHub({channel:"agent-host",type:"task-started",taskId:t.taskId,sessionId:t.sessionId,workflow:t.workflow,label:t.label||t.workflow,formValues:t.formValues,prefilled:t.prefilled,hostContext:t.hostContext,hubContinue:t.hubContinue})),t.type==="complete"){if(t.status==="success"){const n=t.hostContext&&t.hostContext.refresh;Z(n)}(e=r.onTaskComplete)==null||e.call(r,{taskId:t.taskId,status:t.status,hostContext:t.hostContext}),a==null||a.closeWidget()}if(t.type==="close"&&(a==null||a.closeWidget()),t.type==="open-widget"&&M(t.payload),t.type==="open-action"){const n=B(t.pageId,t.actionId);n&&M(n)}t.type==="hub-stats"&&(t.running,a==null||a.updateLauncherStats(t.running,t.waiting),C())}function M(t){return p(this,null,function*(){yield S(),r.prepareOpenWidget&&(yield r.prepareOpenWidget());const e=L(),n=yield R();e.openWidget({workflow:t.workflow,title:t.title||t.label,host:t.host||r.host,prefilled:t.prefilled,hostContext:t.hostContext,sessionId:t.sessionId,preferSandboxFiles:t.preferSandboxFiles},n,A||void 0)})}function S(){return p(this,null,function*(){v&&(yield v)})}function le(t){return p(this,null,function*(){if(r=$({showHub:!0},t),r.bootstrap){const e=yield r.bootstrap();D(e)}if(r.agentOrigin&&r.showHub!==!1&&typeof document!="undefined")try{L();const e=yield R();a==null||a.sendToHub({channel:"agent-host",type:"init",token:e,refreshToken:A||void 0})}catch(e){}})}const q={init(t){return p(this,null,function*(){v&&(yield v),v=le(t).finally(()=>{v=null}),yield v})},getToken(){return p(this,null,function*(){return yield S(),R()})},registerPage(i,s){return p(this,arguments,function*(t,e,n={}){yield S(),J(t,e,n),r.agentOrigin&&L().sendToHub({channel:"agent-host",type:"register-page",pageId:t,actions:z(e)}),C()})},unregisterPage(t){return p(this,null,function*(){yield S(),X(t),r.agentOrigin&&(L().sendToHub({channel:"agent-host",type:"unregister-page",pageId:t}),L().sendToHub({channel:"agent-host",type:"set-page",pageId:null})),C()})},setCurrentPage(t){return p(this,null,function*(){yield S(),ee(t),r.agentOrigin&&L().sendToHub({channel:"agent-host",type:"set-page",pageId:t}),C()})},openAction(t,e){return p(this,null,function*(){const n=B(t,e);if(!n)throw new Error("当前操作不可用");yield M(n)})},openWorkflow(n){return p(this,arguments,function*(t,e={}){yield M(F($({workflow:t},e),{host:e.host||r.host,title:e.title||e.label||t}))})},canOpen(t,e={}){const n=(t||"").trim(),i=e.prefilled||{},s=String(i.sub_project_id||i.subProjectId||"").trim();if(n==="edc_form_fill"){const o=i.forms;return!!(s&&Array.isArray(o)&&o.length>0)}return n==="edc_crf_design"?!!s:!0},destroy(){a==null||a.destroy(),a=null,x="",A="",r={},v=null,Q()}};typeof window!="undefined"&&(window.AgentHost=q),u.AGENT_HOST_MSG=l,u.AgentHost=q,u.isAgentHostMessage=c,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})})(this.AgentHost=this.AgentHost||{});
226
+ </div>`;const i=n.querySelector(".agent-host-hint-text");i&&(i.textContent=e.hint);const o=n.querySelector(".agent-host-hint-use"),s=n.querySelector(".agent-host-hint-dismiss"),f=c=>c.preventDefault();o==null||o.addEventListener("mousedown",f),s==null||s.addEventListener("mousedown",f),o==null||o.addEventListener("click",c=>{var y;c.stopPropagation(),(y=this.onHintUse)==null||y.call(this,e.pageId,e.actionId)}),s==null||s.addEventListener("click",c=>{var y;c.stopPropagation(),(y=this.onHintDismiss)==null||y.call(this)}),n.classList.add("is-visible"),this.layoutHint(n,e),this.bindHintReposition()}layoutHint(e,n){if(n.placement!=="anchor"){e.classList.remove("is-anchored"),e.style.top="",e.style.left="";return}const i=pe(n.pageId,n.actionId);if(!i){e.classList.remove("is-anchored"),e.style.top="",e.style.left="";return}e.classList.add("is-anchored");const o=8,s=e.offsetWidth||320,f=e.offsetHeight||48;let c=i.top,y=i.right+o;y+s>window.innerWidth-16&&(y=Math.max(16,i.left),c=i.bottom+o),c+f>window.innerHeight-16&&(c=Math.max(16,i.top-f-o)),c<16&&(c=16),y<16&&(y=16),e.style.top=`${Math.round(c)}px`,e.style.left=`${Math.round(y)}px`}collectScrollParents(e){var o;const n=[];let i=(o=e==null?void 0:e.parentElement)!=null?o:null;for(;i;){const s=window.getComputedStyle(i),f=s.overflowY,c=s.overflowX;/(auto|scroll|overlay)/.test(`${c} ${f}`)&&n.push(i),i=i.parentElement}return n}bindHintReposition(){var i,o;if(typeof window=="undefined")return;this.hintRepositionBound||(window.addEventListener("scroll",this.onHintReposition,!0),window.addEventListener("resize",this.onHintReposition),(i=window.visualViewport)==null||i.addEventListener("resize",this.onHintReposition),(o=window.visualViewport)==null||o.addEventListener("scroll",this.onHintReposition),this.hintRepositionBound=!0);const e=this.launcherHintAction;for(const s of this.hintScrollParents)s.removeEventListener("scroll",this.onHintReposition);if(this.hintScrollParents=[],!e||e.placement!=="anchor")return;const n=G(e.pageId,e.actionId);this.hintScrollParents=this.collectScrollParents(n);for(const s of this.hintScrollParents)s.addEventListener("scroll",this.onHintReposition,{passive:!0})}unbindHintReposition(){var e,n;if(typeof window!="undefined"){this.hintRepositionBound&&(window.removeEventListener("scroll",this.onHintReposition,!0),window.removeEventListener("resize",this.onHintReposition),(e=window.visualViewport)==null||e.removeEventListener("resize",this.onHintReposition),(n=window.visualViewport)==null||n.removeEventListener("scroll",this.onHintReposition),this.hintRepositionBound=!1);for(const i of this.hintScrollParents)i.removeEventListener("scroll",this.onHintReposition);this.hintScrollParents=[]}}mountLauncher(){if(this.hubLauncher||document.getElementById(w))return;const e=document.createElement("button");if(e.id=w,e.type="button",e.title="科研AI助手",e.setAttribute("aria-label","科研AI助手"),e.innerHTML='<span class="agent-host-launcher-icon" aria-hidden="true">✦</span><span class="agent-host-launcher-bell" aria-hidden="true"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 3a5 5 0 0 0-5 5v1.5c0 1.3-.4 2.6-1.2 3.7L4.5 15.5c-.4.5-.1 1.3.5 1.3h14c.6 0 .9-.8.5-1.3l-1.3-2.3A6.6 6.6 0 0 1 17 9.5V8a5 5 0 0 0-5-5Z" fill="#f59e0b"/><path d="M10 18.5a2 2 0 0 0 4 0" stroke="#f59e0b" stroke-width="1.8" stroke-linecap="round"/></svg></span><span class="agent-host-launcher-badge"></span>',e.addEventListener("click",()=>this.openHubPanel()),document.body.appendChild(e),this.hubLauncher=e,this.hubLauncherBadge=e.querySelector(".agent-host-launcher-badge"),document.getElementById("agent-host-launcher-hint"))this.hubLauncherHint=document.getElementById("agent-host-launcher-hint");else{const n=document.createElement("div");n.id="agent-host-launcher-hint",document.body.appendChild(n),this.hubLauncherHint=n}}mountHubPanel(){const e=document.createElement("div");e.id=H;const n=document.createElement("div");n.className="agent-host-hub-backdrop",n.addEventListener("click",()=>this.closeHubPanel());const i=document.createElement("div");i.className="agent-host-hub-panel-shell";const o=document.createElement("iframe");o.id=we,o.title="科研AI助手",o.src=`${this.agentOrigin}/host/hub?embed=1&panel=1`,o.allow="clipboard-read; clipboard-write",i.appendChild(o),e.appendChild(n),e.appendChild(i),document.body.appendChild(e),this.hubPanelOverlay=e,this.hubIframe=o}updateLauncherStats(e,n){this.lastRunning=e,this.lastWaiting=n,this.applyLauncherVisual()}applyLauncherVisual(){const e=this.hubLauncher,n=this.hubLauncherBadge;if(!e||!n)return;const i=e.querySelector(".agent-host-launcher-icon"),o=e.querySelector(".agent-host-launcher-bell"),s=this.lastRunning,f=this.lastWaiting,c=f>0&&!this.hubPanelOpen;e.classList.toggle("is-pulse",s>0),e.classList.toggle("is-breathe",(s>0||f>0)&&!c),c?(e.title="有待确认事项,点击打开助手",e.setAttribute("aria-label","有待确认事项,点击打开助手")):(e.title="科研AI助手",e.setAttribute("aria-label","科研AI助手")),i&&(i.style.display=c?"none":""),o&&o.classList.toggle("is-visible",c),s>0?(n.textContent=String(s),n.classList.add("is-visible"),n.classList.remove("is-waiting")):f>0?(n.textContent="!",n.classList.add("is-visible","is-waiting")):(n.textContent="",n.classList.remove("is-visible","is-waiting"))}removeHub(){var e,n,i;(e=document.getElementById(w))==null||e.remove(),(n=document.getElementById("agent-host-launcher-hint"))==null||n.remove(),(i=document.getElementById(H))==null||i.remove(),this.hubLauncher=null,this.hubLauncherBadge=null,this.hubLauncherHint=null,this.hubPanelOverlay=null,this.hubIframe=null,this.hubReady=!1,this.hubPanelOpen=!1,this.hubPanelExpanded=!1,this.pendingHubMessages=[]}openWidget(e,n,i){this.closeWidget();const o=document.createElement("div");o.id=K,o.style.cssText="position:fixed;inset:0;z-index:2147483646;background:rgba(15,23,42,0.45);display:flex;align-items:center;justify-content:center;padding:16px;",o.addEventListener("click",Ae=>{Ae.target===o&&this.closeWidget()});const s=document.createElement("iframe");s.id=me,s.title=e.title||e.workflow;const f=new URLSearchParams({embed:"1",workflow:e.workflow,host:e.host||"",title:e.title||""});s.src=`${this.agentOrigin}/host/widget?${f.toString()}`,s.style.cssText="width:min(560px,100%);height:80vh;border:none;border-radius:12px;background:#fff;box-shadow:0 20px 60px rgba(0,0,0,0.25);display:block;",o.appendChild(s),document.body.appendChild(o),this.widgetIframe=s,this.widgetReady=!1;const c={channel:l,type:"init",token:n,refreshToken:i},y={workflow:e.workflow,title:e.title,host:e.host,prefilled:e.prefilled?k(e.prefilled):void 0,hostContext:e.hostContext?k(e.hostContext):void 0,sessionId:e.sessionId,preferSandboxFiles:e.preferSandboxFiles},Ce={channel:l,type:"open-widget",payload:y};this.pendingWidgetMessages=[c,Ce]}closeWidget(){var e;(e=document.getElementById(K))==null||e.remove(),this.widgetIframe=null,this.widgetReady=!1,this.pendingWidgetMessages=[]}sendToHub(e){this.post("hub",e)}post(e,n){var f;const i=e==="hub"?this.hubReady:this.widgetReady,o=e==="hub"?this.pendingHubMessages:this.pendingWidgetMessages;if(!i){o.push(n);return}const s=e==="hub"?this.hubIframe:this.widgetIframe;(f=s==null?void 0:s.contentWindow)==null||f.postMessage(n,M(this.agentOrigin))}flush(e){const n=e==="hub"?this.pendingHubMessages:this.pendingWidgetMessages,i=e==="hub"?this.hubIframe:this.widgetIframe;if(!(i!=null&&i.contentWindow))return;const o=M(this.agentOrigin);for(;n.length;){const s=n.shift();s&&i.contentWindow.postMessage(s,o)}}}let a={},r=null,P="",A="",E=null;function Le(){const t=(a.agentOrigin||"").replace(/\/$/,"");if(!t)throw new Error("Agent 前端地址未配置:请在 init({ agentOrigin }) 传入,或由宿主 BFF bootstrap 返回 agent_web_origin");return t}function X(t){var e;t.agent_web_origin&&(a.agentOrigin=t.agent_web_origin.replace(/\/$/,"")),t.agent_token&&(P=t.agent_token),t.refresh_token&&(A=t.refresh_token),(e=a.onBootstrap)==null||e.call(a,t)}function O(){return m(this,null,function*(){if(a.getToken)return P=yield a.getToken(),P;if(P)return P;if(a.bootstrap){const t=yield a.bootstrap();if(X(t),P)return P}throw new Error("未连接 Agent:请配置 bootstrap 或 getToken")})}function C(){if(a.agentOrigin)try{I().updateLauncherHint(be())}catch(t){}}function I(){const t=Le();return r||(q(C),r=new ke(t,Ee,{onHintUse:(e,n)=>{j(e,n)},onHintDismiss:()=>{ce(),C()}}),a.showHub!==!1&&r.mountHub()),r}function Pe(){if(!(!r||!a.agentOrigin)){for(const{pageId:t,actions:e}of ge())r.sendToHub({channel:"agent-host",type:"register-page",pageId:t,actions:N(e)});r.sendToHub({channel:"agent-host",type:"set-page",pageId:fe()}),C()}}function Ee(t){var e;if(t.type==="ready"&&t.surface==="hub"&&O().then(n=>{r==null||r.sendToHub({channel:"agent-host",type:"init",token:n,refreshToken:A||void 0}),Pe()}).catch(()=>{}),t.type==="started"&&(r==null||r.closeWidget(),r==null||r.openHubPanel(),r==null||r.sendToHub({channel:"agent-host",type:"task-started",taskId:t.taskId,sessionId:t.sessionId,workflow:t.workflow,label:t.label||t.workflow,formValues:t.formValues,prefilled:t.prefilled,hostContext:t.hostContext,hubContinue:t.hubContinue})),t.type==="complete"){if(t.status==="success"){const n=t.hostContext&&t.hostContext.refresh;ae(n,{taskId:t.taskId,result:t.result,hostContext:t.hostContext,apply:t.apply})}(e=a.onTaskComplete)==null||e.call(a,{taskId:t.taskId,status:t.status,hostContext:t.hostContext,result:t.result,apply:t.apply}),r==null||r.closeWidget()}t.type==="close"&&(r==null||r.closeWidget()),t.type==="open-widget"&&D(t.payload),t.type==="open-action"&&j(t.pageId,t.actionId),t.type==="hub-stats"&&(t.running,r==null||r.updateLauncherStats(t.running,t.waiting),C())}function j(t,e){return m(this,null,function*(){const n=W(t,e);if(!n)return;if(V(n.action)){yield Ie(n);return}const i=de(t,e);i&&(yield D(i))})}function Ie(t){return m(this,null,function*(){yield S();const e=yield O(),n=I();n.openHubPanel(),n.sendToHub({channel:"agent-host",type:"init",token:e,refreshToken:A||void 0}),n.sendToHub({channel:"agent-host",type:"run-direct",workflow:t.action.workflow||t.action.skill||"",title:t.action.label,host:t.action.host||a.host,skill:t.action.skill,prefilled:t.prefilled,hostContext:t.hostContext})})}function D(t){return m(this,null,function*(){yield S(),a.prepareOpenWidget&&(yield a.prepareOpenWidget());const e=I(),n=yield O();e.openWidget({workflow:t.workflow,title:t.title||t.label,host:t.host||a.host,prefilled:t.prefilled,hostContext:t.hostContext,sessionId:t.sessionId,preferSandboxFiles:t.preferSandboxFiles},n,A||void 0)})}function S(){return m(this,null,function*(){E&&(yield E)})}function Re(t){return m(this,null,function*(){if(a=F({showHub:!0},t),a.bootstrap){const e=yield a.bootstrap();X(e)}if(a.agentOrigin&&a.showHub!==!1&&typeof document!="undefined")try{I();const e=yield O();r==null||r.sendToHub({channel:"agent-host",type:"init",token:e,refreshToken:A||void 0})}catch(e){}})}const J={init(t){return m(this,null,function*(){E&&(yield E),E=Re(t).finally(()=>{E=null}),yield E})},getToken(){return m(this,null,function*(){return yield S(),O()})},registerPage(i,o){return m(this,arguments,function*(t,e,n={}){yield S(),se(t,e,n),a.agentOrigin&&I().sendToHub({channel:"agent-host",type:"register-page",pageId:t,actions:N(e)}),C()})},unregisterPage(t){return m(this,null,function*(){yield S(),re(t),a.agentOrigin&&(I().sendToHub({channel:"agent-host",type:"unregister-page",pageId:t}),I().sendToHub({channel:"agent-host",type:"set-page",pageId:null})),C()})},setCurrentPage(t){return m(this,null,function*(){yield S(),he(t),a.agentOrigin&&I().sendToHub({channel:"agent-host",type:"set-page",pageId:t}),C()})},openAction(t,e){return m(this,null,function*(){if(!W(t,e))throw new Error("当前操作不可用");yield j(t,e)})},openWorkflow(n){return m(this,arguments,function*(t,e={}){yield D(Q(F({workflow:t},e),{host:e.host||a.host,title:e.title||e.label||t}))})},canOpen(t,e={}){const n=(t||"").trim(),i=e.prefilled||{},o=String(i.sub_project_id||i.subProjectId||"").trim();if(n==="edc_form_fill"){const s=i.forms;return!!(o&&Array.isArray(s)&&s.length>0)}return n==="edc_crf_design"?!!o:n==="sds_crf_schema_from_doc"?!!String(i.index_id||i.indexId||"").trim():n==="sds-project-remark"?!!String(i.project_name||i.name||"").trim():!0},destroy(){r==null||r.destroy(),r=null,P="",A="",a={},E=null,q(null),le()}};typeof window!="undefined"&&(window.AgentHost=J),g.AGENT_HOST_MSG=l,g.AgentHost=J,g.isAgentHostMessage=u,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})})(this.AgentHost=this.AgentHost||{});