@tomflow/chatloop-tools 0.9.31 → 0.9.33
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 +1 -1
- package/package.json +1 -1
- package/providers/form.mjs +9 -8
- package/ui/form/review-app.js +6 -10
- package/ui/form/review.html +2 -4
- package/ui/form/review.template.html +1 -3
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
|
-
-
|
|
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
package/providers/form.mjs
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
import {randomUUID} from 'node:crypto';
|
|
3
3
|
import {readFileSync} from 'node:fs';
|
|
4
4
|
|
|
5
|
-
const RESOURCE_URI='ui://chat-loop/form/
|
|
6
|
-
const LEGACY_RESOURCE_URIS=['ui://chat-loop/form/
|
|
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
|
+
'ui://chat-loop/form/v3',
|
|
7
8
|
'ui://chat-loop/form/review-v2','ui://chat-loop/form/review-v1'];
|
|
8
9
|
const RESOURCE_MIME='text/html;profile=mcp-app';
|
|
9
10
|
const FORM_ID=/^[A-Za-z0-9][A-Za-z0-9_-]{7,79}$/;
|
|
@@ -15,7 +16,7 @@ const filled=(value,max)=>typeof value==='string'
|
|
|
15
16
|
&&value.trim().length>0&&value.length<=max;
|
|
16
17
|
const shape=(value,fields)=>value&&typeof value==='object'
|
|
17
18
|
&&!Array.isArray(value)&&Object.keys(value).every(key=>fields.includes(key));
|
|
18
|
-
const CARD=['title','description','
|
|
19
|
+
const CARD=['title','description','options','submitLabel','footer','textInput'];
|
|
19
20
|
const REQUIRED_CARD=['title','description','options','submitLabel'];
|
|
20
21
|
const OPTION=['value','label'];
|
|
21
22
|
const TEXT_INPUT=['label','placeholder','showWhen','requiredWhen','maxLength','rows'];
|
|
@@ -42,7 +43,6 @@ const cardSchema={type:'object',additionalProperties:false,required:REQUIRED_CAR
|
|
|
42
43
|
properties:{
|
|
43
44
|
title:{type:'string',minLength:1,maxLength:180},
|
|
44
45
|
description:{type:'string',minLength:1,maxLength:1200},
|
|
45
|
-
document:{type:'string',minLength:1,maxLength:24000},
|
|
46
46
|
options:{type:'array',minItems:1,maxItems:32,items:optionSchema},
|
|
47
47
|
submitLabel:{type:'string',minLength:1,maxLength:180},
|
|
48
48
|
footer:{type:'string',maxLength:1200},
|
|
@@ -50,7 +50,7 @@ const cardSchema={type:'object',additionalProperties:false,required:REQUIRED_CAR
|
|
|
50
50
|
}};
|
|
51
51
|
const tools=[
|
|
52
52
|
{name:'chat_loop_form__prepare',description:
|
|
53
|
-
'
|
|
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
55
|
properties:{formId:{type:'string',pattern:FORM_ID.source},card:cardSchema}},
|
|
56
56
|
annotations:{readOnlyHint:false},
|
|
@@ -86,7 +86,6 @@ function checkCard(args){
|
|
|
86
86
|
const card=args.card;
|
|
87
87
|
if(!shape(card,CARD)||REQUIRED_CARD.some(key=>!Object.hasOwn(card,key))
|
|
88
88
|
||!filled(card.title,180)||!filled(card.description,1200)
|
|
89
|
-
||card.document!==undefined&&!filled(card.document,24000)
|
|
90
89
|
||!filled(card.submitLabel,180)
|
|
91
90
|
||card.footer!==undefined&&typeof card.footer!=='string'
|
|
92
91
|
||card.footer?.length>1200)
|
|
@@ -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
|
-
...(card.document!==undefined?{document:card.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,
|
|
@@ -151,7 +149,10 @@ async function observeForm(form,event,formId,traceRef,errorCode){
|
|
|
151
149
|
function prepare(args){
|
|
152
150
|
const formId=args?.formId===undefined?randomUUID():args.formId;
|
|
153
151
|
const traceRef=randomUUID();
|
|
154
|
-
try{
|
|
152
|
+
try{
|
|
153
|
+
check(args,PREP);
|
|
154
|
+
checkCard(args)
|
|
155
|
+
}
|
|
155
156
|
catch(error){
|
|
156
157
|
error.formId=typeof formId==='string'&&FORM_ID.test(formId)?formId:randomUUID();
|
|
157
158
|
error.traceRef=traceRef;
|
package/ui/form/review-app.js
CHANGED
|
@@ -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,10 @@ 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;
|
|
95
96
|
prepared=value;
|
|
96
|
-
// A caller-supplied read-only document appears before the generic choices.
|
|
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').hidden=!documentValue;
|
|
104
97
|
$('title').textContent=view.title;
|
|
105
98
|
$('description').textContent=view.description;
|
|
106
99
|
$('submit').textContent=view.submitLabel||'提交';
|
|
@@ -148,7 +141,10 @@ function receive(event){
|
|
|
148
141
|
if(message.method==='ui/notifications/tool-result'){
|
|
149
142
|
toolResult=message.params;
|
|
150
143
|
try{render(decode(toolResult))}
|
|
151
|
-
catch{
|
|
144
|
+
catch{
|
|
145
|
+
prepared=null;locked=true;update();
|
|
146
|
+
status('Form prepare 回执无效,禁止提交。','error');
|
|
147
|
+
}
|
|
152
148
|
}
|
|
153
149
|
if(message.method==='ui/resource-teardown'&&message.id!==undefined)
|
|
154
150
|
send({id:message.id,result:{}});
|
package/ui/form/review.html
CHANGED
|
@@ -8,10 +8,9 @@
|
|
|
8
8
|
:root{color-scheme:light dark;font:14px/1.5 system-ui,-apple-system,sans-serif}
|
|
9
9
|
*{box-sizing:border-box}
|
|
10
10
|
body{margin:0;padding:16px;color:var(--app-color,#191919);background:transparent}
|
|
11
|
-
main{max-width:760px;margin:auto;padding:16px;border:1px solid #aaa6;border-radius:12px}
|
|
11
|
+
main{max-width:760px;margin:0 auto;padding:16px;border:1px solid #aaa6;border-radius:12px}
|
|
12
12
|
h1{font-size:16px;margin:0 0 6px}
|
|
13
13
|
p{margin:6px 0 12px;color:inherit}
|
|
14
|
-
#document{white-space:pre-wrap;overflow-wrap:anywhere;margin:14px 0 18px;padding:12px 0;border-bottom:1px solid #aaa6;font:inherit;line-height:1.65}
|
|
15
14
|
#review-controls{margin-top:12px}
|
|
16
15
|
small{color:#888}
|
|
17
16
|
fieldset{border:0;padding:0;margin:8px 0}
|
|
@@ -32,7 +31,6 @@ button:disabled{opacity:.44;cursor:not-allowed}
|
|
|
32
31
|
<body>
|
|
33
32
|
<main aria-live="polite">
|
|
34
33
|
<h1 id="title">Chat Loop 表单</h1>
|
|
35
|
-
<article id="document" aria-label="待阅读内容" hidden></article>
|
|
36
34
|
<section id="review-controls">
|
|
37
35
|
<p id="description">正在连接表单工具…</p>
|
|
38
36
|
<fieldset id="options" disabled>
|
|
@@ -49,7 +47,7 @@ button:disabled{opacity:.44;cursor:not-allowed}
|
|
|
49
47
|
<small id="footer"></small>
|
|
50
48
|
</section>
|
|
51
49
|
</main>
|
|
52
|
-
<script>(()=>{var r=e=>document.getElementById(e),
|
|
50
|
+
<script>(()=>{var r=e=>document.getElementById(e),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,N=0,c=!1,a=null,u=!1,_=null,l=null,g=null;function d(e,t="info"){r("status").textContent=e,r("status").dataset.state=t}function I(e){window.parent.postMessage({jsonrpc:"2.0",...e},"*")}function x(e,t={}){I({method:e,params:t})}function w(e,t){if(!c)throw Error("FORM_APP_NOT_INITIALIZED");return new Promise((n,i)=>{let s=++N,m=setTimeout(()=>{h.delete(s),i(Error("FORM_APP_REPLY_UNKNOWN"))},3e4);h.set(s,{resolve:n,reject:i,timer:m}),I({id:s,method:e,params:t})})}function R(e){if(e?.isError){let n=e.content?.find(i=>i.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 T=()=>[...document.querySelectorAll('input[name="choice"]')],D=()=>T().find(e=>e.checked)?.value||null;function A(){let e=D();return a?.view.options.find(t=>t.value===e)||null}function M(e){let t=a?.view.textInput;return!!(t&&e&&t.showWhen.includes(e.value))}function P(e){let t=a?.view.textInput;return!!(t&&e&&t.requiredWhen.includes(e.value))}function f(){let e=A(),t=M(e);r("options").disabled=!c||!a||u,r("text-input").hidden=!t;let n=r("input").value.trim();r("submit").disabled=!c||!a||u||!e||t&&P(e)&&!n,O()}function V(e){if(!c||!e)return;let t=e.formId+":"+e.traceRef;g!==t&&(g=t,r("status").dataset.renderObservation="pending",w("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 C(e){let t=e?.view,n=t?.options;if(!t||t.template!=="chat-loop-form-v1"||typeof e.formId!="string"||!F.test(e.formId)||typeof e.traceRef!="string"||!L.test(e.traceRef)||t.formId!==e.formId||!Array.isArray(n)||n.length<1||n.length>32||n.some(o=>!o||typeof o.value!="string"||!o.value.trim()||typeof o.label!="string"||!o.label.trim())||new Set(n.map(o=>o.value)).size!==n.length)throw Error("FORM_VIEW_INVALID");let i=t.textInput;if(i&&(!Array.isArray(i.showWhen)||!Array.isArray(i.requiredWhen)||i.requiredWhen.some(o=>!i.showWhen.includes(o)))||Object.hasOwn(t,"document"))throw Error("FORM_VIEW_INVALID");let s=a?.formId===e.formId&&a?.traceRef===e.traceRef&&u;a=e,r("title").textContent=t.title,r("description").textContent=t.description,r("submit").textContent=t.submitLabel||"\u63D0\u4EA4",r("footer").textContent=t.footer||"";let m=r("option-list");m.replaceChildren();for(let o of n){let p=document.createElement("label");p.className="option";let E=document.createElement("input");E.type="radio",E.name="choice",E.value=o.value,E.addEventListener("change",f);let b=document.createElement("span");b.textContent=o.label,p.append(E,b),m.append(p)}r("text-label").textContent=i?.label||"\u8865\u5145\u4FE1\u606F",r("input").placeholder=i?.placeholder||"",r("input").maxLength=i?.maxLength||4e3,r("input").rows=i?.rows||4,r("text-input").hidden=!0,r("input").value="",u=s,r("result").hidden=!0,r("result").textContent="",d(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"),l?"error":c?"ok":"info"),f(),V(e)}function W(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=h.get(t.id);if(!n)return;h.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{C(R(_))}catch{a=null,u=!0,f(),d("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 v=0,y=0;function O(){if(!c)return;let e=Math.ceil(document.documentElement.scrollHeight),t=Math.ceil(document.documentElement.clientWidth);e===v&&t===y||(v=e,y=t,x("ui/notifications/size-changed",{width:t,height:e}))}async function z(){if(window.parent===window){d("\u672A\u5728 ChatGPT MCP App iframe \u4E2D\u8FD0\u884C\uFF0C\u7981\u6B62\u63D0\u4EA4\u3002","error");return}let e=++N,t=new Promise((n,i)=>{let s=setTimeout(()=>{h.delete(e),i(Error("FORM_APP_INIT_TIMEOUT"))},12e3);h.set(e,{resolve:n,reject:i,timer:s}),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){l="\u5F53\u524D\u5BBF\u4E3B\u7F3A\u5C11 serverTools \u80FD\u529B\uFF0C\u8868\u5355\u4E0D\u53EF\u63D0\u4EA4\u3002",d(l,"error"),f();return}c=!0,l=null,x("ui/notifications/initialized",{}),O(),_&&C(R(_)),typeof ResizeObserver=="function"&&new ResizeObserver(O).observe(document.documentElement)}catch{l="MCP App \u521D\u59CB\u5316\u5931\u8D25\uFF0C\u8BF7\u6838\u67E5\u5BBF\u4E3B\u8FDE\u63A5\u4E0E\u534F\u8BAE\u80FD\u529B\u3002",d(l,"error"),c=!1,f()}}async function k(){if(!c||u||!a)return;let e=A();if(!e)return;let t=M(e),n=r("input").value.trim();if(t&&P(e)&&!n)return;u=!0,f();let{formId:i,traceRef:s}=a,m={formId:i,traceRef:s,selection:{value:e.value,label:e.label},...t&&n?{input:n}:{}};d("\u6B63\u5728\u63D0\u4EA4\u8868\u5355\u2026");try{let o=R(await w("tools/call",{name:"chat_loop_form__submit",arguments:m}));if(o.status!=="FORM_SUBMITTED"||o.formId!==i||o.traceRef!==s||o.selection?.value!==e.value||o.selection?.label!==e.label){d("Submit \u8FD4\u56DE\u7684\u6570\u636E\u4E0E\u5F53\u524D\u8868\u5355\u4E0D\u5339\u914D\u3002","error");return}r("status").dataset.modelContextDelivery="pending",w("ui/update-model-context",{content:[{type:"text",text:JSON.stringify({kind:"form-selection",formId:i,traceRef:s,selection:o.selection,...o.input!==void 0?{input:o.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,d("\u5DF2\u63D0\u4EA4\u8868\u5355\uFF0C\u7B49\u5F85\u5F53\u524D\u4E1A\u52A1\u6D41\u7A0B\u5904\u7406\u3002","ok")}catch(o){let p=typeof o?.message=="string"&&/^[A-Z][A-Z0-9_]{1,95}$/.test(o.message)?o.message:"FORM_APP_REPLY_UNKNOWN";d(p==="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"+p+"\uFF09\u3002","error")}}window.addEventListener("message",W);r("input").addEventListener("input",f);r("submit").addEventListener("click",k);z();})();
|
|
53
51
|
</script>
|
|
54
52
|
</body>
|
|
55
53
|
</html>
|
|
@@ -8,10 +8,9 @@
|
|
|
8
8
|
:root{color-scheme:light dark;font:14px/1.5 system-ui,-apple-system,sans-serif}
|
|
9
9
|
*{box-sizing:border-box}
|
|
10
10
|
body{margin:0;padding:16px;color:var(--app-color,#191919);background:transparent}
|
|
11
|
-
main{max-width:760px;margin:auto;padding:16px;border:1px solid #aaa6;border-radius:12px}
|
|
11
|
+
main{max-width:760px;margin:0 auto;padding:16px;border:1px solid #aaa6;border-radius:12px}
|
|
12
12
|
h1{font-size:16px;margin:0 0 6px}
|
|
13
13
|
p{margin:6px 0 12px;color:inherit}
|
|
14
|
-
#document{white-space:pre-wrap;overflow-wrap:anywhere;margin:14px 0 18px;padding:12px 0;border-bottom:1px solid #aaa6;font:inherit;line-height:1.65}
|
|
15
14
|
#review-controls{margin-top:12px}
|
|
16
15
|
small{color:#888}
|
|
17
16
|
fieldset{border:0;padding:0;margin:8px 0}
|
|
@@ -32,7 +31,6 @@ button:disabled{opacity:.44;cursor:not-allowed}
|
|
|
32
31
|
<body>
|
|
33
32
|
<main aria-live="polite">
|
|
34
33
|
<h1 id="title">Chat Loop 表单</h1>
|
|
35
|
-
<article id="document" aria-label="待阅读内容" hidden></article>
|
|
36
34
|
<section id="review-controls">
|
|
37
35
|
<p id="description">正在连接表单工具…</p>
|
|
38
36
|
<fieldset id="options" disabled>
|