@tomflow/chatloop-tools 0.9.32 → 0.9.34

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
@@ -66,7 +66,7 @@ The six built-in Providers are:
66
66
  - `runtime`: exact-request receipt lookup without replay
67
67
  - `context`: independent Context SDK read/write schemas and fixed CLI
68
68
  - `trace`: Project-scoped diagnosis and SOFT stage observations, plus a bearer-token-scoped Form Trace lookup using the Prepare `traceRef`
69
- - `form`: a stateless generic single-select MCP App. `prepare` validates caller-defined options, optional top-level, caller-owned read-only `document` (at most 24,000 characters, displayed in its own sibling section BEFORE the Form card, not in `card.document`), and optional text input, preserves or generates `formId`, and returns a random `traceRef`; the app-only `render` acknowledgement records that the view was displayed; `submit` returns the selected value/label and optional text; the UI may forward the confirmed selection to future model context with `ui/update-model-context` (never a user Turn or business approval), without showing raw receipt JSON. A Render ACK failure is exposed as DOM diagnostic state, not misreported as a confirmed Render event. It does not send Chat messages, interpret business meaning, call Context, retain prepared-form state, create a submission ledger, or impose expiry. Prepare/Render/Submit API spans and soft observations are correlated in the existing Trace writer by `sessionRef=traceRef` and `evidenceRef=formId`; Trace loss never blocks or changes the returned Form data.
69
+ - Form: stateless, generic single-select MCP App. Prepare accepts only caller-defined options and optional conditional text input; it preserves or generates formId and returns a random traceRef. It never accepts, stores, renders or copies a business document: the caller shows R/P in its ChatGPT message. Render acknowledges the UI; Submit returns selected value/label and optional text. ui/update-model-context makes a result available to future model turns but does not trigger one or prove approval. Form does not send Chat messages, interpret decisions, call Context or impose expiry. Prepare/Render/Submit observations are correlated by formId and traceRef without altering results.
70
70
 
71
71
  `npm run build:providers` generates `providers.generated.mjs`, the static
72
72
  assembly and release fingerprint manifest. Both `tools/list` and `tools/call`
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.9.32",
6
+ "version": "0.9.34",
7
7
  "type": "module",
8
8
  "description": "Chat Loop local tools: MCP gateway, isolated bundled tunnel-client and unified CLI",
9
9
  "bin": {
@@ -2,8 +2,8 @@
2
2
  import {randomUUID} from 'node:crypto';
3
3
  import {readFileSync} from 'node:fs';
4
4
 
5
- const RESOURCE_URI='ui://chat-loop/form/v5';
6
- const LEGACY_RESOURCE_URIS=['ui://chat-loop/form/v4',
5
+ const RESOURCE_URI='ui://chat-loop/form/v6';
6
+ const LEGACY_RESOURCE_URIS=['ui://chat-loop/form/v5','ui://chat-loop/form/v4',
7
7
  'ui://chat-loop/form/v3',
8
8
  'ui://chat-loop/form/review-v2','ui://chat-loop/form/review-v1'];
9
9
  const RESOURCE_MIME='text/html;profile=mcp-app';
@@ -20,7 +20,7 @@ const CARD=['title','description','options','submitLabel','footer','textInput'];
20
20
  const REQUIRED_CARD=['title','description','options','submitLabel'];
21
21
  const OPTION=['value','label'];
22
22
  const TEXT_INPUT=['label','placeholder','showWhen','requiredWhen','maxLength','rows'];
23
- const PREP=['formId','document','card'];
23
+ const PREP=['formId','card'];
24
24
  const RENDER=['formId','traceRef'];
25
25
  const SUBMIT=['formId','traceRef','selection','input'];
26
26
  const optionSchema={type:'object',additionalProperties:false,required:OPTION,
@@ -50,10 +50,9 @@ const cardSchema={type:'object',additionalProperties:false,required:REQUIRED_CAR
50
50
  }};
51
51
  const tools=[
52
52
  {name:'chat_loop_form__prepare',description:
53
- 'Present a generic single-select MCP App with caller-defined options and optional text input. An optional top-level document is a separate read-only presentation ABOVE the Form, never a field inside the Form card. Complete document preparation and all other tools first; call this tool LAST and do not add a second document or diagnostic report after the App. Preserve caller formId or generate a UUID and traceRef. Form never owns business documents, workflow, or approvals.',
53
+ 'Render a generic single-select Form with caller-defined options and optional text input. Business documents belong in the caller ChatGPT message, never in this Form. Preserve caller formId or generate a UUID and traceRef. Form returns selection data but does not trigger Chat messages, business approvals, or workflow transitions.',
54
54
  inputSchema:{type:'object',additionalProperties:false,required:['card'],
55
- properties:{formId:{type:'string',pattern:FORM_ID.source},
56
- document:{type:'string',minLength:1,maxLength:24000},card:cardSchema}},
55
+ properties:{formId:{type:'string',pattern:FORM_ID.source},card:cardSchema}},
57
56
  annotations:{readOnlyHint:false},
58
57
  _meta:{ui:{resourceUri:RESOURCE_URI,visibility:['model','app']},
59
58
  'openai/outputTemplate':RESOURCE_URI,'openai/widgetAccessible':true}},
@@ -124,7 +123,6 @@ function view(args,formId){
124
123
  const card=args.card,field=card.textInput;
125
124
  return {template:'chat-loop-form-v1',formId,
126
125
  title:card.title,description:card.description,
127
- ...(args.document!==undefined?{document:args.document}:{}),
128
126
  options:card.options.map(option=>({value:option.value,label:option.label})),
129
127
  textInput:field?{
130
128
  label:field.label,placeholder:field.placeholder,
@@ -153,8 +151,6 @@ function prepare(args){
153
151
  const traceRef=randomUUID();
154
152
  try{
155
153
  check(args,PREP);
156
- if(args.document!==undefined&&!filled(args.document,24000))
157
- throw fail('FORM_DOCUMENT_INVALID');
158
154
  checkCard(args)
159
155
  }
160
156
  catch(error){
@@ -49,7 +49,7 @@ function inputRequired(option){
49
49
  }
50
50
  function update(){
51
51
  const option=selectedOption(),visible=inputVisible(option);
52
- $('options').disabled=!connected;
52
+ $('options').disabled=!connected||!prepared||locked;
53
53
  $('text-input').hidden=!visible;
54
54
  const text=$('input').value.trim();
55
55
  $('submit').disabled=!connected||!prepared||locked||!option
@@ -90,17 +90,14 @@ function render(value){
90
90
  if(field&&(!Array.isArray(field.showWhen)||!Array.isArray(field.requiredWhen)
91
91
  ||field.requiredWhen.some(option=>!field.showWhen.includes(option))))
92
92
  throw Error('FORM_VIEW_INVALID');
93
+ if(Object.hasOwn(view,'document'))throw Error('FORM_VIEW_INVALID');
93
94
  const preserveLock=prepared?.formId===value.formId
94
95
  &&prepared?.traceRef===value.traceRef&&locked;
96
+ if(!preserveLock){
97
+ delete $('status').dataset.submitReceipt;
98
+ delete $('status').dataset.modelContextDelivery;
99
+ }
95
100
  prepared=value;
96
- // The document is a separate sibling of the Form, not a Form field.
97
- // textContent prevents HTML/script injection; Form owns no document state.
98
- const documentValue=view.document;
99
- if(documentValue!==undefined&&(typeof documentValue!=='string'
100
- ||!documentValue.trim()||documentValue.length>24000))
101
- throw Error('FORM_DOCUMENT_INVALID');
102
- $('document').textContent=documentValue||'';
103
- $('document-shell').hidden=!documentValue;
104
101
  $('title').textContent=view.title;
105
102
  $('description').textContent=view.description;
106
103
  $('submit').textContent=view.submitLabel||'提交';
@@ -127,9 +124,10 @@ function render(value){
127
124
  locked=preserveLock;
128
125
  $('result').hidden=true;
129
126
  $('result').textContent='';
130
- status(hostFailure||(connected?'表单已准备,请选择一项。':
131
- '表单已准备,正在等待宿主能力确认。'),
132
- hostFailure?'error':connected?'ok':'info');
127
+ status(preserveLock?'本表单已提交或结果未知,请核对原请求回执。':
128
+ hostFailure||(connected?'表单已准备,请选择一项。':
129
+ '表单已准备,正在等待宿主能力确认。'),
130
+ preserveLock?'info':hostFailure?'error':connected?'ok':'info');
133
131
  update();
134
132
  reportRendered(value);
135
133
  }
@@ -148,7 +146,10 @@ function receive(event){
148
146
  if(message.method==='ui/notifications/tool-result'){
149
147
  toolResult=message.params;
150
148
  try{render(decode(toolResult))}
151
- catch{status('Form prepare 回执无效,禁止提交。','error')}
149
+ catch{
150
+ prepared=null;locked=true;update();
151
+ status('Form prepare 回执无效,禁止提交。','error');
152
+ }
152
153
  }
153
154
  if(message.method==='ui/resource-teardown'&&message.id!==undefined)
154
155
  send({id:message.id,result:{}});
@@ -156,8 +157,15 @@ function receive(event){
156
157
  let lastHeight=0,lastWidth=0;
157
158
  function resize(){
158
159
  if(!connected)return;
159
- const height=Math.ceil(document.documentElement.scrollHeight);
160
+ // The iframe viewport can retain a previous, oversized host allocation.
161
+ // Root scrollHeight is at least that viewport height, so using it prevents
162
+ // the host from shrinking a stale 7,000px Form. Measure actual card content.
163
+ const card=document.querySelector('main');
164
+ if(!card)return;
165
+ const bottomPadding=parseFloat(getComputedStyle(document.body).paddingBottom)||0;
166
+ const height=Math.ceil(card.getBoundingClientRect().bottom+bottomPadding);
160
167
  const width=Math.ceil(document.documentElement.clientWidth);
168
+ if(!Number.isFinite(height)||height<1||!Number.isFinite(width)||width<1)return;
161
169
  if(height===lastHeight&&width===lastWidth)return;
162
170
  lastHeight=height;lastWidth=width;
163
171
  notify('ui/notifications/size-changed',{width,height});
@@ -191,7 +199,7 @@ async function initialize(){
191
199
  resize();
192
200
  if(toolResult)render(decode(toolResult));
193
201
  if(typeof ResizeObserver==='function')
194
- new ResizeObserver(resize).observe(document.documentElement);
202
+ new ResizeObserver(resize).observe(document.querySelector('main'));
195
203
  }catch{
196
204
  hostFailure='MCP App 初始化失败,请核查宿主连接与协议能力。';
197
205
  status(hostFailure,'error');connected=false;update();
@@ -208,6 +216,7 @@ async function submit(){
208
216
  const args={formId,traceRef,
209
217
  selection:{value:option.value,label:option.label},
210
218
  ...(visible&&input?{input}:{})};
219
+ $('status').dataset.submitReceipt='pending';
211
220
  status('正在提交表单…');
212
221
  try{
213
222
  const receipt=decode(await call('tools/call',{
@@ -217,8 +226,10 @@ async function submit(){
217
226
  ||receipt.traceRef!==traceRef
218
227
  ||receipt.selection?.value!==option.value
219
228
  ||receipt.selection?.label!==option.label){
229
+ $('status').dataset.submitReceipt='failed';
220
230
  status('Submit 返回的数据与当前表单不匹配。','error');return;
221
231
  }
232
+ $('status').dataset.submitReceipt='confirmed';
222
233
  // Update model-visible context without posting a new Chat message.
223
234
  // This is not proof of a user Turn, business approval, or model consumption.
224
235
  $('status').dataset.modelContextDelivery='pending';
@@ -226,7 +237,8 @@ async function submit(){
226
237
  text:JSON.stringify({kind:'form-selection',formId,traceRef,
227
238
  selection:receipt.selection,
228
239
  ...(receipt.input!==undefined?{input:receipt.input}:{})})
229
- }]}).then(()=>{
240
+ }]}).then(result=>{
241
+ if(result?.isError)throw Error('FORM_MODEL_CONTEXT_REJECTED');
230
242
  $('status').dataset.modelContextDelivery='acknowledged';
231
243
  }).catch(()=>{
232
244
  $('status').dataset.modelContextDelivery='unverified';
@@ -240,6 +252,8 @@ async function submit(){
240
252
  const code=typeof error?.message==='string'
241
253
  &&/^[A-Z][A-Z0-9_]{1,95}$/.test(error.message)
242
254
  ?error.message:'FORM_APP_REPLY_UNKNOWN';
255
+ $('status').dataset.submitReceipt=code==='FORM_APP_REPLY_UNKNOWN'
256
+ ?'unknown':'failed';
243
257
  status(code==='FORM_APP_REPLY_UNKNOWN'
244
258
  ?'提交结果未知;已锁定当前表单,请核查原请求回执。'
245
259
  :'提交失败('+code+')。','error');
@@ -9,10 +9,8 @@
9
9
  *{box-sizing:border-box}
10
10
  body{margin:0;padding:16px;color:var(--app-color,#191919);background:transparent}
11
11
  main{max-width:760px;margin:0 auto;padding:16px;border:1px solid #aaa6;border-radius:12px}
12
- #document-shell{max-width:760px;margin:0 auto 18px;padding:12px 0;border-bottom:1px solid #aaa6}
13
12
  h1{font-size:16px;margin:0 0 6px}
14
13
  p{margin:6px 0 12px;color:inherit}
15
- #document{white-space:pre-wrap;overflow-wrap:anywhere;margin:0;font:inherit;line-height:1.65}
16
14
  #review-controls{margin-top:12px}
17
15
  small{color:#888}
18
16
  fieldset{border:0;padding:0;margin:8px 0}
@@ -31,9 +29,6 @@ button:disabled{opacity:.44;cursor:not-allowed}
31
29
  </style>
32
30
  </head>
33
31
  <body>
34
- <section id="document-shell" aria-label="需求或方案文档" hidden>
35
- <article id="document" aria-label="完整评审文档"></article>
36
- </section>
37
32
  <main aria-live="polite">
38
33
  <h1 id="title">Chat Loop 表单</h1>
39
34
  <section id="review-controls">
@@ -52,7 +47,7 @@ button:disabled{opacity:.44;cursor:not-allowed}
52
47
  <small id="footer"></small>
53
48
  </section>
54
49
  </main>
55
- <script>(()=>{var r=e=>document.getElementById(e),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,m=new Map,x=0,d=!1,u=null,E=!1,_=null,f=null,v=null;function l(e,t="info"){r("status").textContent=e,r("status").dataset.state=t}function I(e){window.parent.postMessage({jsonrpc:"2.0",...e},"*")}function A(e,t={}){I({method:e,params:t})}function O(e,t){if(!d)throw Error("FORM_APP_NOT_INITIALIZED");return new Promise((n,o)=>{let a=++x,c=setTimeout(()=>{m.delete(a),o(Error("FORM_APP_REPLY_UNKNOWN"))},3e4);m.set(a,{resolve:n,reject:o,timer:c}),I({id:a,method:e,params:t})})}function R(e){if(e?.isError){let n=e.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(e?.structuredContent?.status)return e.structuredContent;let t=e?.content?.find(n=>n.type==="text")?.text;if(typeof t!="string"||t.length>2e4)throw Error("FORM_RECEIPT_MISSING");return JSON.parse(t)}var D=()=>[...document.querySelectorAll('input[name="choice"]')],V=()=>D().find(e=>e.checked)?.value||null;function C(){let e=V();return u?.view.options.find(t=>t.value===e)||null}function M(e){let t=u?.view.textInput;return!!(t&&e&&t.showWhen.includes(e.value))}function P(e){let t=u?.view.textInput;return!!(t&&e&&t.requiredWhen.includes(e.value))}function p(){let e=C(),t=M(e);r("options").disabled=!d,r("text-input").hidden=!t;let n=r("input").value.trim();r("submit").disabled=!d||!u||E||!e||t&&P(e)&&!n,b()}function W(e){if(!d||!e)return;let t=e.formId+":"+e.traceRef;v!==t&&(v=t,r("status").dataset.renderObservation="pending",O("tools/call",{name:"chat_loop_form__render",arguments:{formId:e.formId,traceRef:e.traceRef}}).then(R).then(n=>{if(n.status!=="FORM_RENDERED"||n.formId!==e.formId||n.traceRef!==e.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(e){let t=e?.view,n=t?.options;if(!t||t.template!=="chat-loop-form-v1"||typeof e.formId!="string"||!L.test(e.formId)||typeof e.traceRef!="string"||!T.test(e.traceRef)||t.formId!==e.formId||!Array.isArray(n)||n.length<1||n.length>32||n.some(s=>!s||typeof s.value!="string"||!s.value.trim()||typeof s.label!="string"||!s.label.trim())||new Set(n.map(s=>s.value)).size!==n.length)throw Error("FORM_VIEW_INVALID");let o=t.textInput;if(o&&(!Array.isArray(o.showWhen)||!Array.isArray(o.requiredWhen)||o.requiredWhen.some(s=>!o.showWhen.includes(s))))throw Error("FORM_VIEW_INVALID");let a=u?.formId===e.formId&&u?.traceRef===e.traceRef&&E;u=e;let c=t.document;if(c!==void 0&&(typeof c!="string"||!c.trim()||c.length>24e3))throw Error("FORM_DOCUMENT_INVALID");r("document").textContent=c||"",r("document-shell").hidden=!c,r("title").textContent=t.title,r("description").textContent=t.description,r("submit").textContent=t.submitLabel||"\u63D0\u4EA4",r("footer").textContent=t.footer||"";let i=r("option-list");i.replaceChildren();for(let s of n){let w=document.createElement("label");w.className="option";let h=document.createElement("input");h.type="radio",h.name="choice",h.value=s.value,h.addEventListener("change",p);let g=document.createElement("span");g.textContent=s.label,w.append(h,g),i.append(w)}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="",E=a,r("result").hidden=!0,r("result").textContent="",l(f||(d?"\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"),f?"error":d?"ok":"info"),p(),W(e)}function z(e){if(e.source!==window.parent)return;let t=e.data;if(!(!t||t.jsonrpc!=="2.0"||typeof t!="object")){if(Object.hasOwn(t,"id")&&!Object.hasOwn(t,"method")){let n=m.get(t.id);if(!n)return;m.delete(t.id),clearTimeout(n.timer),t.error?n.reject(Error("FORM_APP_REPLY_UNKNOWN")):n.resolve(t.result);return}if(t.method==="ui/notifications/tool-result"){_=t.params;try{F(R(_))}catch{l("Form prepare \u56DE\u6267\u65E0\u6548\uFF0C\u7981\u6B62\u63D0\u4EA4\u3002","error")}}t.method==="ui/resource-teardown"&&t.id!==void 0&&I({id:t.id,result:{}})}}var y=0,N=0;function b(){if(!d)return;let e=Math.ceil(document.documentElement.scrollHeight),t=Math.ceil(document.documentElement.clientWidth);e===y&&t===N||(y=e,N=t,A("ui/notifications/size-changed",{width:t,height:e}))}async function k(){if(window.parent===window){l("\u672A\u5728 ChatGPT MCP App iframe \u4E2D\u8FD0\u884C\uFF0C\u7981\u6B62\u63D0\u4EA4\u3002","error");return}let e=++x,t=new Promise((n,o)=>{let a=setTimeout(()=>{m.delete(e),o(Error("FORM_APP_INIT_TIMEOUT"))},12e3);m.set(e,{resolve:n,reject:o,timer:a}),I({id:e,method:"ui/initialize",params:{appInfo:{name:"Chat Loop Form",version:"1.1.0"},appCapabilities:{},protocolVersion:"2026-01-26"}})});try{let n=await t;if(!n?.protocolVersion||!n.hostInfo||!n.hostCapabilities)throw Error("FORM_APP_INIT_INVALID");if(!n.hostCapabilities.serverTools){f="\u5F53\u524D\u5BBF\u4E3B\u7F3A\u5C11 serverTools \u80FD\u529B\uFF0C\u8868\u5355\u4E0D\u53EF\u63D0\u4EA4\u3002",l(f,"error"),p();return}d=!0,f=null,A("ui/notifications/initialized",{}),b(),_&&F(R(_)),typeof ResizeObserver=="function"&&new ResizeObserver(b).observe(document.documentElement)}catch{f="MCP App \u521D\u59CB\u5316\u5931\u8D25\uFF0C\u8BF7\u6838\u67E5\u5BBF\u4E3B\u8FDE\u63A5\u4E0E\u534F\u8BAE\u80FD\u529B\u3002",l(f,"error"),d=!1,p()}}async function S(){if(!d||E||!u)return;let e=C();if(!e)return;let t=M(e),n=r("input").value.trim();if(t&&P(e)&&!n)return;E=!0,p();let{formId:o,traceRef:a}=u,c={formId:o,traceRef:a,selection:{value:e.value,label:e.label},...t&&n?{input:n}:{}};l("\u6B63\u5728\u63D0\u4EA4\u8868\u5355\u2026");try{let i=R(await O("tools/call",{name:"chat_loop_form__submit",arguments:c}));if(i.status!=="FORM_SUBMITTED"||i.formId!==o||i.traceRef!==a||i.selection?.value!==e.value||i.selection?.label!==e.label){l("Submit \u8FD4\u56DE\u7684\u6570\u636E\u4E0E\u5F53\u524D\u8868\u5355\u4E0D\u5339\u914D\u3002","error");return}r("status").dataset.modelContextDelivery="pending",O("ui/update-model-context",{content:[{type:"text",text:JSON.stringify({kind:"form-selection",formId:o,traceRef:a,selection:i.selection,...i.input!==void 0?{input:i.input}:{}})}]}).then(()=>{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,l("\u5DF2\u63D0\u4EA4\u8868\u5355\uFF0C\u7B49\u5F85\u5F53\u524D\u4E1A\u52A1\u6D41\u7A0B\u5904\u7406\u3002","ok")}catch(i){let s=typeof i?.message=="string"&&/^[A-Z][A-Z0-9_]{1,95}$/.test(i.message)?i.message:"FORM_APP_REPLY_UNKNOWN";l(s==="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"+s+"\uFF09\u3002","error")}}window.addEventListener("message",z);r("input").addEventListener("input",p);r("submit").addEventListener("click",S);k();})();
50
+ <script>(()=>{var r=t=>document.getElementById(t),F=/^[A-Za-z0-9][A-Za-z0-9_-]{7,79}$/,L=/^[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 x(t,e={}){_({method:t,params:e})}function b(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 I(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 T=()=>[...document.querySelectorAll('input[name="choice"]')],D=()=>T().find(t=>t.checked)?.value||null;function C(){let t=D();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=C(),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 W(t){if(!c||!t)return;let e=t.formId+":"+t.traceRef;g!==e&&(g=e,r("status").dataset.renderObservation="pending",b("tools/call",{name:"chat_loop_form__render",arguments:{formId:t.formId,traceRef:t.traceRef}}).then(I).then(n=>{if(n.status!=="FORM_RENDERED"||n.formId!==t.formId||n.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 M(t){let e=t?.view,n=e?.options;if(!e||e.template!=="chat-loop-form-v1"||typeof t.formId!="string"||!F.test(t.formId)||typeof t.traceRef!="string"||!L.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(),W(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{M(I(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 N=0,y=0;function O(){if(!c)return;let t=document.querySelector("main");if(!t)return;let e=parseFloat(getComputedStyle(document.body).paddingBottom)||0,n=Math.ceil(t.getBoundingClientRect().bottom+e),o=Math.ceil(document.documentElement.clientWidth);!Number.isFinite(n)||n<1||!Number.isFinite(o)||o<1||n===N&&o===y||(N=n,y=o,x("ui/notifications/size-changed",{width:o,height:n}))}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,x("ui/notifications/initialized",{}),O(),R&&M(I(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 k(){if(!c||f||!a)return;let t=C();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=I(await b("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",b("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",k);S();})();
56
51
  </script>
57
52
  </body>
58
53
  </html>
@@ -9,10 +9,8 @@
9
9
  *{box-sizing:border-box}
10
10
  body{margin:0;padding:16px;color:var(--app-color,#191919);background:transparent}
11
11
  main{max-width:760px;margin:0 auto;padding:16px;border:1px solid #aaa6;border-radius:12px}
12
- #document-shell{max-width:760px;margin:0 auto 18px;padding:12px 0;border-bottom:1px solid #aaa6}
13
12
  h1{font-size:16px;margin:0 0 6px}
14
13
  p{margin:6px 0 12px;color:inherit}
15
- #document{white-space:pre-wrap;overflow-wrap:anywhere;margin:0;font:inherit;line-height:1.65}
16
14
  #review-controls{margin-top:12px}
17
15
  small{color:#888}
18
16
  fieldset{border:0;padding:0;margin:8px 0}
@@ -31,9 +29,6 @@ button:disabled{opacity:.44;cursor:not-allowed}
31
29
  </style>
32
30
  </head>
33
31
  <body>
34
- <section id="document-shell" aria-label="需求或方案文档" hidden>
35
- <article id="document" aria-label="完整评审文档"></article>
36
- </section>
37
32
  <main aria-live="polite">
38
33
  <h1 id="title">Chat Loop 表单</h1>
39
34
  <section id="review-controls">