@tomflow/chatloop-tools 0.9.33 → 0.9.35
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/package.json +1 -1
- package/ui/form/review-app.js +34 -6
- package/ui/form/review.html +1 -1
package/package.json
CHANGED
package/ui/form/review-app.js
CHANGED
|
@@ -58,6 +58,13 @@ function update(){
|
|
|
58
58
|
}
|
|
59
59
|
function reportRendered(value){
|
|
60
60
|
if(!connected||!value)return;
|
|
61
|
+
const bounds=document.querySelector('main')?.getBoundingClientRect();
|
|
62
|
+
// A host-hidden iframe can receive tool data without ever showing controls.
|
|
63
|
+
// Do not record a visible render until its card has measurable dimensions.
|
|
64
|
+
if(!bounds||bounds.width<1||bounds.height<1){
|
|
65
|
+
$('status').dataset.renderObservation='not-visible';
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
61
68
|
const key=value.formId+':'+value.traceRef;
|
|
62
69
|
if(renderReported===key)return;
|
|
63
70
|
renderReported=key;
|
|
@@ -93,6 +100,10 @@ function render(value){
|
|
|
93
100
|
if(Object.hasOwn(view,'document'))throw Error('FORM_VIEW_INVALID');
|
|
94
101
|
const preserveLock=prepared?.formId===value.formId
|
|
95
102
|
&&prepared?.traceRef===value.traceRef&&locked;
|
|
103
|
+
if(!preserveLock){
|
|
104
|
+
delete $('status').dataset.submitReceipt;
|
|
105
|
+
delete $('status').dataset.modelContextDelivery;
|
|
106
|
+
}
|
|
96
107
|
prepared=value;
|
|
97
108
|
$('title').textContent=view.title;
|
|
98
109
|
$('description').textContent=view.description;
|
|
@@ -120,9 +131,10 @@ function render(value){
|
|
|
120
131
|
locked=preserveLock;
|
|
121
132
|
$('result').hidden=true;
|
|
122
133
|
$('result').textContent='';
|
|
123
|
-
status(
|
|
124
|
-
'
|
|
125
|
-
|
|
134
|
+
status(preserveLock?'本表单已提交或结果未知,请核对原请求回执。':
|
|
135
|
+
hostFailure||(connected?'表单已准备,请选择一项。':
|
|
136
|
+
'表单已准备,正在等待宿主能力确认。'),
|
|
137
|
+
preserveLock?'info':hostFailure?'error':connected?'ok':'info');
|
|
126
138
|
update();
|
|
127
139
|
reportRendered(value);
|
|
128
140
|
}
|
|
@@ -152,8 +164,18 @@ function receive(event){
|
|
|
152
164
|
let lastHeight=0,lastWidth=0;
|
|
153
165
|
function resize(){
|
|
154
166
|
if(!connected)return;
|
|
155
|
-
|
|
167
|
+
// The iframe viewport can retain a previous, oversized host allocation.
|
|
168
|
+
// Root scrollHeight is at least that viewport height, so using it prevents
|
|
169
|
+
// the host from shrinking a stale 7,000px Form. Measure actual card content.
|
|
170
|
+
const card=document.querySelector('main');
|
|
171
|
+
if(!card)return;
|
|
172
|
+
const bounds=card.getBoundingClientRect();
|
|
173
|
+
if(bounds.width<1||bounds.height<1)return;
|
|
174
|
+
reportRendered(prepared);
|
|
175
|
+
const bottomPadding=parseFloat(getComputedStyle(document.body).paddingBottom)||0;
|
|
176
|
+
const height=Math.ceil(bounds.bottom+bottomPadding);
|
|
156
177
|
const width=Math.ceil(document.documentElement.clientWidth);
|
|
178
|
+
if(!Number.isFinite(height)||height<1||!Number.isFinite(width)||width<1)return;
|
|
157
179
|
if(height===lastHeight&&width===lastWidth)return;
|
|
158
180
|
lastHeight=height;lastWidth=width;
|
|
159
181
|
notify('ui/notifications/size-changed',{width,height});
|
|
@@ -187,7 +209,7 @@ async function initialize(){
|
|
|
187
209
|
resize();
|
|
188
210
|
if(toolResult)render(decode(toolResult));
|
|
189
211
|
if(typeof ResizeObserver==='function')
|
|
190
|
-
new ResizeObserver(resize).observe(document.
|
|
212
|
+
new ResizeObserver(resize).observe(document.querySelector('main'));
|
|
191
213
|
}catch{
|
|
192
214
|
hostFailure='MCP App 初始化失败,请核查宿主连接与协议能力。';
|
|
193
215
|
status(hostFailure,'error');connected=false;update();
|
|
@@ -204,6 +226,7 @@ async function submit(){
|
|
|
204
226
|
const args={formId,traceRef,
|
|
205
227
|
selection:{value:option.value,label:option.label},
|
|
206
228
|
...(visible&&input?{input}:{})};
|
|
229
|
+
$('status').dataset.submitReceipt='pending';
|
|
207
230
|
status('正在提交表单…');
|
|
208
231
|
try{
|
|
209
232
|
const receipt=decode(await call('tools/call',{
|
|
@@ -213,8 +236,10 @@ async function submit(){
|
|
|
213
236
|
||receipt.traceRef!==traceRef
|
|
214
237
|
||receipt.selection?.value!==option.value
|
|
215
238
|
||receipt.selection?.label!==option.label){
|
|
239
|
+
$('status').dataset.submitReceipt='failed';
|
|
216
240
|
status('Submit 返回的数据与当前表单不匹配。','error');return;
|
|
217
241
|
}
|
|
242
|
+
$('status').dataset.submitReceipt='confirmed';
|
|
218
243
|
// Update model-visible context without posting a new Chat message.
|
|
219
244
|
// This is not proof of a user Turn, business approval, or model consumption.
|
|
220
245
|
$('status').dataset.modelContextDelivery='pending';
|
|
@@ -222,7 +247,8 @@ async function submit(){
|
|
|
222
247
|
text:JSON.stringify({kind:'form-selection',formId,traceRef,
|
|
223
248
|
selection:receipt.selection,
|
|
224
249
|
...(receipt.input!==undefined?{input:receipt.input}:{})})
|
|
225
|
-
}]}).then(
|
|
250
|
+
}]}).then(result=>{
|
|
251
|
+
if(result?.isError)throw Error('FORM_MODEL_CONTEXT_REJECTED');
|
|
226
252
|
$('status').dataset.modelContextDelivery='acknowledged';
|
|
227
253
|
}).catch(()=>{
|
|
228
254
|
$('status').dataset.modelContextDelivery='unverified';
|
|
@@ -236,6 +262,8 @@ async function submit(){
|
|
|
236
262
|
const code=typeof error?.message==='string'
|
|
237
263
|
&&/^[A-Z][A-Z0-9_]{1,95}$/.test(error.message)
|
|
238
264
|
?error.message:'FORM_APP_REPLY_UNKNOWN';
|
|
265
|
+
$('status').dataset.submitReceipt=code==='FORM_APP_REPLY_UNKNOWN'
|
|
266
|
+
?'unknown':'failed';
|
|
239
267
|
status(code==='FORM_APP_REPLY_UNKNOWN'
|
|
240
268
|
?'提交结果未知;已锁定当前表单,请核查原请求回执。'
|
|
241
269
|
:'提交失败('+code+')。','error');
|
package/ui/form/review.html
CHANGED
|
@@ -47,7 +47,7 @@ button:disabled{opacity:.44;cursor:not-allowed}
|
|
|
47
47
|
<small id="footer"></small>
|
|
48
48
|
</section>
|
|
49
49
|
</main>
|
|
50
|
-
<script>(()=>{var r=
|
|
50
|
+
<script>(()=>{var r=t=>document.getElementById(t),L=/^[A-Za-z0-9][A-Za-z0-9_-]{7,79}$/,T=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,h=new Map,v=0,c=!1,a=null,f=!1,R=null,l=null,g=null;function u(t,e="info"){r("status").textContent=t,r("status").dataset.state=e}function _(t){window.parent.postMessage({jsonrpc:"2.0",...t},"*")}function C(t,e={}){_({method:t,params:e})}function I(t,e){if(!c)throw Error("FORM_APP_NOT_INITIALIZED");return new Promise((n,o)=>{let s=++v,p=setTimeout(()=>{h.delete(s),o(Error("FORM_APP_REPLY_UNKNOWN"))},3e4);h.set(s,{resolve:n,reject:o,timer:p}),_({id:s,method:t,params:e})})}function b(t){if(t?.isError){let n=t.content?.find(o=>o.type==="text")?.text;throw Error(typeof n=="string"&&/^[A-Z][A-Z0-9_]{1,95}$/.test(n)?n:"FORM_TOOL_REJECTED")}if(t?.structuredContent?.status)return t.structuredContent;let e=t?.content?.find(n=>n.type==="text")?.text;if(typeof e!="string"||e.length>2e4)throw Error("FORM_RECEIPT_MISSING");return JSON.parse(e)}var D=()=>[...document.querySelectorAll('input[name="choice"]')],W=()=>D().find(t=>t.checked)?.value||null;function x(){let t=W();return a?.view.options.find(e=>e.value===t)||null}function P(t){let e=a?.view.textInput;return!!(e&&t&&e.showWhen.includes(t.value))}function A(t){let e=a?.view.textInput;return!!(e&&t&&e.requiredWhen.includes(t.value))}function m(){let t=x(),e=P(t);r("options").disabled=!c||!a||f,r("text-input").hidden=!e;let n=r("input").value.trim();r("submit").disabled=!c||!a||f||!t||e&&A(t)&&!n,O()}function M(t){if(!c||!t)return;let e=document.querySelector("main")?.getBoundingClientRect();if(!e||e.width<1||e.height<1){r("status").dataset.renderObservation="not-visible";return}let n=t.formId+":"+t.traceRef;g!==n&&(g=n,r("status").dataset.renderObservation="pending",I("tools/call",{name:"chat_loop_form__render",arguments:{formId:t.formId,traceRef:t.traceRef}}).then(b).then(o=>{if(o.status!=="FORM_RENDERED"||o.formId!==t.formId||o.traceRef!==t.traceRef)throw Error("FORM_RENDER_RECEIPT_INVALID");r("status").dataset.renderObservation="confirmed"}).catch(()=>{r("status").dataset.renderObservation="unverified",console.warn("FORM_RENDER_OBSERVATION_UNVERIFIED")}))}function F(t){let e=t?.view,n=e?.options;if(!e||e.template!=="chat-loop-form-v1"||typeof t.formId!="string"||!L.test(t.formId)||typeof t.traceRef!="string"||!T.test(t.traceRef)||e.formId!==t.formId||!Array.isArray(n)||n.length<1||n.length>32||n.some(i=>!i||typeof i.value!="string"||!i.value.trim()||typeof i.label!="string"||!i.label.trim())||new Set(n.map(i=>i.value)).size!==n.length)throw Error("FORM_VIEW_INVALID");let o=e.textInput;if(o&&(!Array.isArray(o.showWhen)||!Array.isArray(o.requiredWhen)||o.requiredWhen.some(i=>!o.showWhen.includes(i)))||Object.hasOwn(e,"document"))throw Error("FORM_VIEW_INVALID");let s=a?.formId===t.formId&&a?.traceRef===t.traceRef&&f;s||(delete r("status").dataset.submitReceipt,delete r("status").dataset.modelContextDelivery),a=t,r("title").textContent=e.title,r("description").textContent=e.description,r("submit").textContent=e.submitLabel||"\u63D0\u4EA4",r("footer").textContent=e.footer||"";let p=r("option-list");p.replaceChildren();for(let i of n){let d=document.createElement("label");d.className="option";let E=document.createElement("input");E.type="radio",E.name="choice",E.value=i.value,E.addEventListener("change",m);let w=document.createElement("span");w.textContent=i.label,d.append(E,w),p.append(d)}r("text-label").textContent=o?.label||"\u8865\u5145\u4FE1\u606F",r("input").placeholder=o?.placeholder||"",r("input").maxLength=o?.maxLength||4e3,r("input").rows=o?.rows||4,r("text-input").hidden=!0,r("input").value="",f=s,r("result").hidden=!0,r("result").textContent="",u(s?"\u672C\u8868\u5355\u5DF2\u63D0\u4EA4\u6216\u7ED3\u679C\u672A\u77E5\uFF0C\u8BF7\u6838\u5BF9\u539F\u8BF7\u6C42\u56DE\u6267\u3002":l||(c?"\u8868\u5355\u5DF2\u51C6\u5907\uFF0C\u8BF7\u9009\u62E9\u4E00\u9879\u3002":"\u8868\u5355\u5DF2\u51C6\u5907\uFF0C\u6B63\u5728\u7B49\u5F85\u5BBF\u4E3B\u80FD\u529B\u786E\u8BA4\u3002"),s?"info":l?"error":c?"ok":"info"),m(),M(t)}function V(t){if(t.source!==window.parent)return;let e=t.data;if(!(!e||e.jsonrpc!=="2.0"||typeof e!="object")){if(Object.hasOwn(e,"id")&&!Object.hasOwn(e,"method")){let n=h.get(e.id);if(!n)return;h.delete(e.id),clearTimeout(n.timer),e.error?n.reject(Error("FORM_APP_REPLY_UNKNOWN")):n.resolve(e.result);return}if(e.method==="ui/notifications/tool-result"){R=e.params;try{F(b(R))}catch{a=null,f=!0,m(),u("Form prepare \u56DE\u6267\u65E0\u6548\uFF0C\u7981\u6B62\u63D0\u4EA4\u3002","error")}}e.method==="ui/resource-teardown"&&e.id!==void 0&&_({id:e.id,result:{}})}}var y=0,N=0;function O(){if(!c)return;let t=document.querySelector("main");if(!t)return;let e=t.getBoundingClientRect();if(e.width<1||e.height<1)return;M(a);let n=parseFloat(getComputedStyle(document.body).paddingBottom)||0,o=Math.ceil(e.bottom+n),s=Math.ceil(document.documentElement.clientWidth);!Number.isFinite(o)||o<1||!Number.isFinite(s)||s<1||o===y&&s===N||(y=o,N=s,C("ui/notifications/size-changed",{width:s,height:o}))}async function S(){if(window.parent===window){u("\u672A\u5728 ChatGPT MCP App iframe \u4E2D\u8FD0\u884C\uFF0C\u7981\u6B62\u63D0\u4EA4\u3002","error");return}let t=++v,e=new Promise((n,o)=>{let s=setTimeout(()=>{h.delete(t),o(Error("FORM_APP_INIT_TIMEOUT"))},12e3);h.set(t,{resolve:n,reject:o,timer:s}),_({id:t,method:"ui/initialize",params:{appInfo:{name:"Chat Loop Form",version:"1.1.0"},appCapabilities:{},protocolVersion:"2026-01-26"}})});try{let n=await e;if(!n?.protocolVersion||!n.hostInfo||!n.hostCapabilities)throw Error("FORM_APP_INIT_INVALID");if(!n.hostCapabilities.serverTools){l="\u5F53\u524D\u5BBF\u4E3B\u7F3A\u5C11 serverTools \u80FD\u529B\uFF0C\u8868\u5355\u4E0D\u53EF\u63D0\u4EA4\u3002",u(l,"error"),m();return}c=!0,l=null,C("ui/notifications/initialized",{}),O(),R&&F(b(R)),typeof ResizeObserver=="function"&&new ResizeObserver(O).observe(document.querySelector("main"))}catch{l="MCP App \u521D\u59CB\u5316\u5931\u8D25\uFF0C\u8BF7\u6838\u67E5\u5BBF\u4E3B\u8FDE\u63A5\u4E0E\u534F\u8BAE\u80FD\u529B\u3002",u(l,"error"),c=!1,m()}}async function q(){if(!c||f||!a)return;let t=x();if(!t)return;let e=P(t),n=r("input").value.trim();if(e&&A(t)&&!n)return;f=!0,m();let{formId:o,traceRef:s}=a,p={formId:o,traceRef:s,selection:{value:t.value,label:t.label},...e&&n?{input:n}:{}};r("status").dataset.submitReceipt="pending",u("\u6B63\u5728\u63D0\u4EA4\u8868\u5355\u2026");try{let i=b(await I("tools/call",{name:"chat_loop_form__submit",arguments:p}));if(i.status!=="FORM_SUBMITTED"||i.formId!==o||i.traceRef!==s||i.selection?.value!==t.value||i.selection?.label!==t.label){r("status").dataset.submitReceipt="failed",u("Submit \u8FD4\u56DE\u7684\u6570\u636E\u4E0E\u5F53\u524D\u8868\u5355\u4E0D\u5339\u914D\u3002","error");return}r("status").dataset.submitReceipt="confirmed",r("status").dataset.modelContextDelivery="pending",I("ui/update-model-context",{content:[{type:"text",text:JSON.stringify({kind:"form-selection",formId:o,traceRef:s,selection:i.selection,...i.input!==void 0?{input:i.input}:{}})}]}).then(d=>{if(d?.isError)throw Error("FORM_MODEL_CONTEXT_REJECTED");r("status").dataset.modelContextDelivery="acknowledged"}).catch(()=>{r("status").dataset.modelContextDelivery="unverified",console.warn("FORM_MODEL_CONTEXT_DELIVERY_UNVERIFIED")}),r("result").textContent="",r("result").hidden=!0,u("\u5DF2\u63D0\u4EA4\u8868\u5355\uFF0C\u7B49\u5F85\u5F53\u524D\u4E1A\u52A1\u6D41\u7A0B\u5904\u7406\u3002","ok")}catch(i){let d=typeof i?.message=="string"&&/^[A-Z][A-Z0-9_]{1,95}$/.test(i.message)?i.message:"FORM_APP_REPLY_UNKNOWN";r("status").dataset.submitReceipt=d==="FORM_APP_REPLY_UNKNOWN"?"unknown":"failed",u(d==="FORM_APP_REPLY_UNKNOWN"?"\u63D0\u4EA4\u7ED3\u679C\u672A\u77E5\uFF1B\u5DF2\u9501\u5B9A\u5F53\u524D\u8868\u5355\uFF0C\u8BF7\u6838\u67E5\u539F\u8BF7\u6C42\u56DE\u6267\u3002":"\u63D0\u4EA4\u5931\u8D25\uFF08"+d+"\uFF09\u3002","error")}}window.addEventListener("message",V);r("input").addEventListener("input",m);r("submit").addEventListener("click",q);S();})();
|
|
51
51
|
</script>
|
|
52
52
|
</body>
|
|
53
53
|
</html>
|