diff --git a/examples/server/public/favicon.ico b/examples/server/public/favicon.ico
new file mode 100644
index 000000000..89e154a0a
Binary files /dev/null and b/examples/server/public/favicon.ico differ
diff --git a/examples/server/themes/README.md b/examples/server/themes/README.md
new file mode 100644
index 000000000..445348fd2
--- /dev/null
+++ b/examples/server/themes/README.md
@@ -0,0 +1,5 @@
+# LLaMA.cpp Server Wild Theme
+
+Simple themes directory of sample "public" directories. To try any of these add --path to your run like `server --path=wild`.
+
+
\ No newline at end of file
diff --git a/examples/server/themes/buttons_top/README.md b/examples/server/themes/buttons_top/README.md
new file mode 100644
index 000000000..87e5f9a8b
--- /dev/null
+++ b/examples/server/themes/buttons_top/README.md
@@ -0,0 +1,7 @@
+# LLaMA.cpp Server Buttons Top Theme
+
+Simple tweaks to the UI. Chat buttons at the top of the page instead of bottom so you can hit Stop instead of chasing it down the page.
+
+To use simply run server with `--path=themes/buttons_top`
+
+
\ No newline at end of file
diff --git a/examples/server/themes/buttons_top/buttons_top.png b/examples/server/themes/buttons_top/buttons_top.png
new file mode 100644
index 000000000..c54454519
Binary files /dev/null and b/examples/server/themes/buttons_top/buttons_top.png differ
diff --git a/examples/server/themes/buttons_top/completion.js b/examples/server/themes/buttons_top/completion.js
new file mode 100644
index 000000000..987b9a3b8
--- /dev/null
+++ b/examples/server/themes/buttons_top/completion.js
@@ -0,0 +1,204 @@
+const paramDefaults = {
+ stream: true,
+ n_predict: 500,
+ temperature: 0.2,
+ stop: [""]
+};
+
+let generation_settings = null;
+
+
+// Completes the prompt as a generator. Recommended for most use cases.
+//
+// Example:
+//
+// import { llama } from '/completion.js'
+//
+// const request = llama("Tell me a joke", {n_predict: 800})
+// for await (const chunk of request) {
+// document.write(chunk.data.content)
+// }
+//
+export async function* llama(prompt, params = {}, config = {}) {
+ let controller = config.controller;
+ const api_url = config.api_url || "";
+
+ if (!controller) {
+ controller = new AbortController();
+ }
+
+ const completionParams = { ...paramDefaults, ...params, prompt };
+
+ const response = await fetch(`${api_url}/completion`, {
+ method: 'POST',
+ body: JSON.stringify(completionParams),
+ headers: {
+ 'Connection': 'keep-alive',
+ 'Content-Type': 'application/json',
+ 'Accept': 'text/event-stream',
+ ...(params.api_key ? {'Authorization': `Bearer ${params.api_key}`} : {})
+ },
+ signal: controller.signal,
+ });
+
+ const reader = response.body.getReader();
+ const decoder = new TextDecoder();
+
+ let content = "";
+ let leftover = ""; // Buffer for partially read lines
+
+ try {
+ let cont = true;
+
+ while (cont) {
+ const result = await reader.read();
+ if (result.done) {
+ break;
+ }
+
+ // Add any leftover data to the current chunk of data
+ const text = leftover + decoder.decode(result.value);
+
+ // Check if the last character is a line break
+ const endsWithLineBreak = text.endsWith('\n');
+
+ // Split the text into lines
+ let lines = text.split('\n');
+
+ // If the text doesn't end with a line break, then the last line is incomplete
+ // Store it in leftover to be added to the next chunk of data
+ if (!endsWithLineBreak) {
+ leftover = lines.pop();
+ } else {
+ leftover = ""; // Reset leftover if we have a line break at the end
+ }
+
+ // Parse all sse events and add them to result
+ const regex = /^(\S+):\s(.*)$/gm;
+ for (const line of lines) {
+ const match = regex.exec(line);
+ if (match) {
+ result[match[1]] = match[2]
+ // since we know this is llama.cpp, let's just decode the json in data
+ if (result.data) {
+ result.data = JSON.parse(result.data);
+ content += result.data.content;
+
+ // yield
+ yield result;
+
+ // if we got a stop token from server, we will break here
+ if (result.data.stop) {
+ if (result.data.generation_settings) {
+ generation_settings = result.data.generation_settings;
+ }
+ cont = false;
+ break;
+ }
+ }
+ if (result.error) {
+ try {
+ result.error = JSON.parse(result.error);
+ if (result.error.message.includes('slot unavailable')) {
+ // Throw an error to be caught by upstream callers
+ throw new Error('slot unavailable');
+ } else {
+ console.error(`llama.cpp error [${result.error.code} - ${result.error.type}]: ${result.error.message}`);
+ }
+ } catch(e) {
+ console.error(`llama.cpp error ${result.error}`)
+ }
+ }
+ }
+ }
+ }
+ } catch (e) {
+ if (e.name !== 'AbortError') {
+ console.error("llama error: ", e);
+ }
+ throw e;
+ }
+ finally {
+ controller.abort();
+ }
+
+ return content;
+}
+
+// Call llama, return an event target that you can subscribe to
+//
+// Example:
+//
+// import { llamaEventTarget } from '/completion.js'
+//
+// const conn = llamaEventTarget(prompt)
+// conn.addEventListener("message", (chunk) => {
+// document.write(chunk.detail.content)
+// })
+//
+export const llamaEventTarget = (prompt, params = {}, config = {}) => {
+ const eventTarget = new EventTarget();
+ (async () => {
+ let content = "";
+ for await (const chunk of llama(prompt, params, config)) {
+ if (chunk.data) {
+ content += chunk.data.content;
+ eventTarget.dispatchEvent(new CustomEvent("message", { detail: chunk.data }));
+ }
+ if (chunk.data.generation_settings) {
+ eventTarget.dispatchEvent(new CustomEvent("generation_settings", { detail: chunk.data.generation_settings }));
+ }
+ if (chunk.data.timings) {
+ eventTarget.dispatchEvent(new CustomEvent("timings", { detail: chunk.data.timings }));
+ }
+ }
+ eventTarget.dispatchEvent(new CustomEvent("done", { detail: { content } }));
+ })();
+ return eventTarget;
+}
+
+// Call llama, return a promise that resolves to the completed text. This does not support streaming
+//
+// Example:
+//
+// llamaPromise(prompt).then((content) => {
+// document.write(content)
+// })
+//
+// or
+//
+// const content = await llamaPromise(prompt)
+// document.write(content)
+//
+export const llamaPromise = (prompt, params = {}, config = {}) => {
+ return new Promise(async (resolve, reject) => {
+ let content = "";
+ try {
+ for await (const chunk of llama(prompt, params, config)) {
+ content += chunk.data.content;
+ }
+ resolve(content);
+ } catch (error) {
+ reject(error);
+ }
+ });
+};
+
+/**
+ * (deprecated)
+ */
+export const llamaComplete = async (params, controller, callback) => {
+ for await (const chunk of llama(params.prompt, params, { controller })) {
+ callback(chunk);
+ }
+}
+
+// Get the model info from the server. This is useful for getting the context window and so on.
+export const llamaModelInfo = async (config = {}) => {
+ if (!generation_settings) {
+ const api_url = config.api_url || "";
+ const props = await fetch(`${api_url}/props`).then(r => r.json());
+ generation_settings = props.default_generation_settings;
+ }
+ return generation_settings;
+}
diff --git a/examples/server/themes/buttons_top/favicon.ico b/examples/server/themes/buttons_top/favicon.ico
new file mode 100644
index 000000000..89e154a0a
Binary files /dev/null and b/examples/server/themes/buttons_top/favicon.ico differ
diff --git a/examples/server/themes/buttons_top/index.html b/examples/server/themes/buttons_top/index.html
new file mode 100644
index 000000000..59365a0ea
--- /dev/null
+++ b/examples/server/themes/buttons_top/index.html
@@ -0,0 +1,1057 @@
+
+
+
+
+
+
+ llama.cpp - chat
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/server/themes/buttons_top/index.js b/examples/server/themes/buttons_top/index.js
new file mode 100644
index 000000000..695aec256
--- /dev/null
+++ b/examples/server/themes/buttons_top/index.js
@@ -0,0 +1 @@
+const t=Symbol.for("preact-signals");function n(){if(r>1){r--;return}let t,n=!1;while(void 0!==i){let _=i;i=void 0;u++;while(void 0!==_){const i=_.o;_.o=void 0;_.f&=-3;if(!(8&_.f)&&h(_))try{_.c()}catch(e){if(!n){t=e;n=!0}}_=i}}u=0;r--;if(n)throw t}function e(t){if(r>0)return t();r++;try{return t()}finally{n()}}let _,i;function o(t){const n=_;_=void 0;try{return t()}finally{_=n}}let r=0,u=0,l=0;function s(t){if(void 0===_)return;let n=t.n;if(void 0===n||n.t!==_){n={i:0,S:t,p:_.s,n:void 0,t:_,e:void 0,x:void 0,r:n};if(void 0!==_.s)_.s.n=n;_.s=n;t.n=n;if(32&_.f)t.S(n);return n}else if(-1===n.i){n.i=0;if(void 0!==n.n){n.n.p=n.p;if(void 0!==n.p)n.p.n=n.n;n.p=_.s;n.n=void 0;_.s.n=n;_.s=n}return n}}function f(t){this.v=t;this.i=0;this.n=void 0;this.t=void 0}f.prototype.brand=t;f.prototype.h=function(){return!0};f.prototype.S=function(t){if(this.t!==t&&void 0===t.e){t.x=this.t;if(void 0!==this.t)this.t.e=t;this.t=t}};f.prototype.U=function(t){if(void 0!==this.t){const n=t.e,e=t.x;if(void 0!==n){n.x=e;t.e=void 0}if(void 0!==e){e.e=n;t.x=void 0}if(t===this.t)this.t=e}};f.prototype.subscribe=function(t){return k(()=>{const n=this.value,e=_;_=void 0;try{t(n)}finally{_=e}})};f.prototype.valueOf=function(){return this.value};f.prototype.toString=function(){return this.value+""};f.prototype.toJSON=function(){return this.value};f.prototype.peek=function(){const t=_;_=void 0;try{return this.value}finally{_=t}};Object.defineProperty(f.prototype,"value",{get(){const t=s(this);if(void 0!==t)t.i=this.i;return this.v},set(t){if(t!==this.v){if(u>100)throw new Error("Cycle detected");this.v=t;this.i++;l++;r++;try{for(let t=this.t;void 0!==t;t=t.x)t.t.N()}finally{n()}}}});function c(t){return new f(t)}function h(t){for(let n=t.s;void 0!==n;n=n.n)if(n.S.i!==n.i||!n.S.h()||n.S.i!==n.i)return!0;return!1}function a(t){for(let n=t.s;void 0!==n;n=n.n){const e=n.S.n;if(void 0!==e)n.r=e;n.S.n=n;n.i=-1;if(void 0===n.n){t.s=n;break}}}function p(t){let n,e=t.s;while(void 0!==e){const t=e.p;if(-1===e.i){e.S.U(e);if(void 0!==t)t.n=e.n;if(void 0!==e.n)e.n.p=t}else n=e;e.S.n=e.r;if(void 0!==e.r)e.r=void 0;e=t}t.s=n}function d(t){f.call(this,void 0);this.x=t;this.s=void 0;this.g=l-1;this.f=4}(d.prototype=new f).h=function(){this.f&=-3;if(1&this.f)return!1;if(32==(36&this.f))return!0;this.f&=-5;if(this.g===l)return!0;this.g=l;this.f|=1;if(this.i>0&&!h(this)){this.f&=-2;return!0}const t=_;try{a(this);_=this;const t=this.x();if(16&this.f||this.v!==t||0===this.i){this.v=t;this.f&=-17;this.i++}}catch(t){this.v=t;this.f|=16;this.i++}_=t;p(this);this.f&=-2;return!0};d.prototype.S=function(t){if(void 0===this.t){this.f|=36;for(let t=this.s;void 0!==t;t=t.n)t.S.S(t)}f.prototype.S.call(this,t)};d.prototype.U=function(t){if(void 0!==this.t){f.prototype.U.call(this,t);if(void 0===this.t){this.f&=-33;for(let t=this.s;void 0!==t;t=t.n)t.S.U(t)}}};d.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(let t=this.t;void 0!==t;t=t.x)t.t.N()}};Object.defineProperty(d.prototype,"value",{get(){if(1&this.f)throw new Error("Cycle detected");const t=s(this);this.h();if(void 0!==t)t.i=this.i;if(16&this.f)throw this.v;return this.v}});function v(t){return new d(t)}function y(t){const e=t.u;t.u=void 0;if("function"==typeof e){r++;const i=_;_=void 0;try{e()}catch(n){t.f&=-2;t.f|=8;m(t);throw n}finally{_=i;n()}}}function m(t){for(let n=t.s;void 0!==n;n=n.n)n.S.U(n);t.x=void 0;t.s=void 0;y(t)}function g(t){if(_!==this)throw new Error("Out-of-order effect");p(this);_=t;this.f&=-2;if(8&this.f)m(this);n()}function b(t){this.x=t;this.u=void 0;this.s=void 0;this.o=void 0;this.f=32}b.prototype.c=function(){const t=this.S();try{if(8&this.f)return;if(void 0===this.x)return;const n=this.x();if("function"==typeof n)this.u=n}finally{t()}};b.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1;this.f&=-9;y(this);a(this);r++;const t=_;_=this;return g.bind(this,t)};b.prototype.N=function(){if(!(2&this.f)){this.f|=2;this.o=i;i=this}};b.prototype.d=function(){this.f|=8;if(!(1&this.f))m(this)};function k(t){const n=new b(t);try{n.c()}catch(t){n.d();throw t}return n.d.bind(n)}var S,w,x,C,E,U,H,P,N,$,D,T,F={},V=[],A=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i,M=Array.isArray;function W(t,n){for(var e in n)t[e]=n[e];return t}function O(t){var n=t.parentNode;n&&n.removeChild(t)}function L(t,n,e){var _,i,o,r={};for(o in n)"key"==o?_=n[o]:"ref"==o?i=n[o]:r[o]=n[o];if(arguments.length>2&&(r.children=arguments.length>3?S.call(arguments,2):e),"function"==typeof t&&null!=t.defaultProps)for(o in t.defaultProps)void 0===r[o]&&(r[o]=t.defaultProps[o]);return R(t,r,_,i,null)}function R(t,n,e,_,i){var o={type:t,props:n,key:e,ref:_,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,constructor:void 0,__v:null==i?++x:i,__i:-1,__u:0};return null==i&&null!=w.vnode&&w.vnode(o),o}function I(){return{current:null}}function j(t){return t.children}function q(t,n){this.props=t,this.context=n}function B(t,n){if(null==n)return t.__?B(t.__,t.__i+1):null;for(var e;nn&&E.sort(P));J.__r=0}function K(t,n,e,_,i,o,r,u,l,s,f){var c,h,a,p,d,v=_&&_.__k||V,y=n.length;for(e.__d=l,Q(e,n,v),l=e.__d,c=0;c0?R(i.type,i.props,i.key,i.ref?i.ref:null,i.__v):i)?(i.__=t,i.__b=t.__b+1,u=Z(i,e,r,f),i.__i=u,o=null,-1!==u&&(f--,(o=e[u])&&(o.__u|=131072)),null==o||null===o.__v?(-1==u&&c--,"function"!=typeof i.type&&(i.__u|=65536)):u!==r&&(u===r+1?c++:u>r?f>l-r?c+=u-r:c--:u(null!=l&&0==(131072&l.__u)?1:0))for(;r>=0||u=0){if((l=n[r])&&0==(131072&l.__u)&&i==l.key&&o===l.type)return r;r--}if(u2&&(u.children=arguments.length>3?S.call(arguments,2):e),R(t.type,u,_||t.key,i||t.ref,null)}function ht(t,n){var e={__c:n="__cC"+T++,__:t,Consumer:function(t,n){return t.children(n)},Provider:function(t){var e,_;return this.getChildContext||(e=[],(_={})[n]=this,this.getChildContext=function(){return _},this.shouldComponentUpdate=function(t){this.props.value!==t.value&&e.some((function(t){t.__e=!0,z(t)}))},this.sub=function(t){e.push(t);var n=t.componentWillUnmount;t.componentWillUnmount=function(){e.splice(e.indexOf(t),1),n&&n.call(t)}}),t.children}};return e.Provider.__=e.Consumer.contextType=e}S=V.slice,w={__e:function(t,n,e,_){for(var i,o,r;n=n.__;)if((i=n.__c)&&!i.__)try{if((o=i.constructor)&&null!=o.getDerivedStateFromError&&(i.setState(o.getDerivedStateFromError(t)),r=i.__d),null!=i.componentDidCatch&&(i.componentDidCatch(t,_||{}),r=i.__d),r)return i.__E=i}catch(n){t=n}throw t}},x=0,C=function(t){return null!=t&&null==t.constructor},q.prototype.setState=function(t,n){var e;e=null!=this.__s&&this.__s!==this.state?this.__s:this.__s=W({},this.state),"function"==typeof t&&(t=t(W({},e),this.props)),t&&W(e,t),null!=t&&this.__v&&(n&&this._sb.push(n),z(this))},q.prototype.forceUpdate=function(t){this.__v&&(this.__e=!0,t&&this.__h.push(t),z(this))},q.prototype.render=j,E=[],H="function"==typeof Promise?Promise.prototype.then.bind(Promise.resolve()):setTimeout,P=function(t,n){return t.__v.__b-n.__v.__b},J.__r=0,N=0,$=et(!1),D=et(!0),T=0;var at,pt,dt,vt,yt=0,mt=[],gt=[],bt=w,kt=bt.__b,St=bt.__r,wt=bt.diffed,xt=bt.__c,Ct=bt.unmount,Et=bt.__;function Ut(t,n){bt.__h&&bt.__h(pt,t,yt||n),yt=0;var e=pt.__H||(pt.__H={__:[],__h:[]});return t>=e.__.length&&e.__.push({__V:gt}),e.__[t]}function Ht(t){return yt=1,Pt(Gt,t)}function Pt(t,n,e){var _=Ut(at++,2);if(_.t=t,!_.__c&&(_.__=[e?e(n):Gt(void 0,n),function(t){var n=_.__N?_.__N[0]:_.__[0],e=_.t(n,t);n!==e&&(_.__N=[e,_.__[1]],_.__c.setState({}))}],_.__c=pt,!pt.u)){var i=function(t,n,e){if(!_.__c.__H)return!0;var i=_.__c.__H.__.filter((function(t){return!!t.__c}));if(i.every((function(t){return!t.__N})))return!o||o.call(this,t,n,e);var r=!1;return i.forEach((function(t){if(t.__N){var n=t.__[0];t.__=t.__N,t.__N=void 0,n!==t.__[0]&&(r=!0)}})),!(!r&&_.__c.props===t)&&(!o||o.call(this,t,n,e))};pt.u=!0;var o=pt.shouldComponentUpdate,r=pt.componentWillUpdate;pt.componentWillUpdate=function(t,n,e){if(this.__e){var _=o;o=void 0,i(t,n,e),o=_}r&&r.call(this,t,n,e)},pt.shouldComponentUpdate=i}return _.__N||_.__}function Nt(t,n){var e=Ut(at++,3);!bt.__s&&Bt(e.__H,n)&&(e.__=t,e.i=n,pt.__H.__h.push(e))}function $t(t,n){var e=Ut(at++,4);!bt.__s&&Bt(e.__H,n)&&(e.__=t,e.i=n,pt.__h.push(e))}function Dt(t){return yt=5,Ft((function(){return{current:t}}),[])}function Tt(t,n,e){yt=6,$t((function(){return"function"==typeof t?(t(n()),function(){return t(null)}):t?(t.current=n(),function(){return t.current=null}):void 0}),null==e?e:e.concat(t))}function Ft(t,n){var e=Ut(at++,7);return Bt(e.__H,n)?(e.__V=t(),e.i=n,e.__h=t,e.__V):e.__}function Vt(t,n){return yt=8,Ft((function(){return t}),n)}function At(t){var n=pt.context[t.__c],e=Ut(at++,9);return e.c=t,n?(null==e.__&&(e.__=!0,n.sub(pt)),n.props.value):t.__}function Mt(t,n){bt.useDebugValue&&bt.useDebugValue(n?n(t):t)}function Wt(t){var n=Ut(at++,10),e=Ht();return n.__=t,pt.componentDidCatch||(pt.componentDidCatch=function(t,_){n.__&&n.__(t,_),e[1](t)}),[e[0],function(){e[1](void 0)}]}function Ot(){var t=Ut(at++,11);if(!t.__){for(var n=pt.__v;null!==n&&!n.__m&&null!==n.__;)n=n.__;var e=n.__m||(n.__m=[0,0]);t.__="P"+e[0]+"-"+e[1]++}return t.__}function Lt(){for(var t;t=mt.shift();)if(t.__P&&t.__H)try{t.__H.__h.forEach(jt),t.__H.__h.forEach(qt),t.__H.__h=[]}catch(n){t.__H.__h=[],bt.__e(n,t.__v)}}bt.__b=function(t){pt=null,kt&&kt(t)},bt.__=function(t,n){t&&n.__k&&n.__k.__m&&(t.__m=n.__k.__m),Et&&Et(t,n)},bt.__r=function(t){St&&St(t),at=0;var n=(pt=t.__c).__H;n&&(dt===pt?(n.__h=[],pt.__h=[],n.__.forEach((function(t){t.__N&&(t.__=t.__N),t.__V=gt,t.__N=t.i=void 0}))):(n.__h.forEach(jt),n.__h.forEach(qt),n.__h=[],at=0)),dt=pt},bt.diffed=function(t){wt&&wt(t);var n=t.__c;n&&n.__H&&(n.__H.__h.length&&(1!==mt.push(n)&&vt===bt.requestAnimationFrame||((vt=bt.requestAnimationFrame)||It)(Lt)),n.__H.__.forEach((function(t){t.i&&(t.__H=t.i),t.__V!==gt&&(t.__=t.__V),t.i=void 0,t.__V=gt}))),dt=pt=null},bt.__c=function(t,n){n.some((function(t){try{t.__h.forEach(jt),t.__h=t.__h.filter((function(t){return!t.__||qt(t)}))}catch(r){n.some((function(t){t.__h&&(t.__h=[])})),n=[],bt.__e(r,t.__v)}})),xt&&xt(t,n)},bt.unmount=function(t){Ct&&Ct(t);var n,e=t.__c;e&&e.__H&&(e.__H.__.forEach((function(t){try{jt(t)}catch(t){n=t}})),e.__H=void 0,n&&bt.__e(n,e.__v))};var Rt="function"==typeof requestAnimationFrame;function It(t){var n,e=function(){clearTimeout(_),Rt&&cancelAnimationFrame(n),setTimeout(t)},_=setTimeout(e,100);Rt&&(n=requestAnimationFrame(e))}function jt(t){var n=pt,e=t.__c;"function"==typeof e&&(t.__c=void 0,e()),pt=n}function qt(t){var n=pt;t.__c=t.__(),pt=n}function Bt(t,n){return!t||t.length!==n.length||n.some((function(n,e){return n!==t[e]}))}function Gt(t,n){return"function"==typeof n?n(t):n}function zt(t,n){w[t]=n.bind(null,w[t]||(()=>{}))}let Jt,Kt;function Qt(t){if(Kt)Kt();Kt=t&&t.S()}function Xt({data:t}){const n=Zt(t);n.value=t;const e=Ft(()=>{let t=this.__v;while(t=t.__)if(t.__c){t.__c.__$f|=4;break}this.__$u.c=()=>{var t;if(!C(e.peek())&&3===(null==(t=this.base)?void 0:t.nodeType))this.base.data=e.peek();else{this.__$f|=1;this.setState({})}};return v(()=>{let t=n.value.value;return 0===t?0:!0===t?"":t||""})},[]);return e.value}Xt.displayName="_st";Object.defineProperties(f.prototype,{constructor:{configurable:!0,value:void 0},type:{configurable:!0,value:Xt},props:{configurable:!0,get(){return{data:this}}},__b:{configurable:!0,value:1}});zt("__b",(t,n)=>{if("string"==typeof n.type){let t,e=n.props;for(let _ in e){if("children"===_)continue;let i=e[_];if(i instanceof f){if(!t)n.__np=t={};t[_]=i;e[_]=i.peek()}}}t(n)});zt("__r",(t,n)=>{Qt();let e,_=n.__c;if(_){_.__$f&=-2;e=_.__$u;if(void 0===e)_.__$u=e=function(t){let n;k((function(){n=this}));n.c=()=>{_.__$f|=1;_.setState({})};return n}()}Jt=_;Qt(e);t(n)});zt("__e",(t,n,e,_)=>{Qt();Jt=void 0;t(n,e,_)});zt("diffed",(t,n)=>{Qt();Jt=void 0;let e;if("string"==typeof n.type&&(e=n.__e)){let t=n.__np,_=n.props;if(t){let n=e.U;if(n)for(let e in n){let _=n[e];if(void 0!==_&&!(e in t)){_.d();n[e]=void 0}}else{n={};e.U=n}for(let i in t){let o=n[i],r=t[i];if(void 0===o){o=Yt(e,i,r,_);n[i]=o}else o.o(r,_)}}}t(n)});function Yt(t,n,e,_){const i=n in t&&void 0===t.ownerSVGElement,o=c(e);return{o:(t,n)=>{o.value=t;_=n},d:k(()=>{const e=o.value.value;if(_[n]!==e){_[n]=e;if(i)t[n]=e;else if(e)t.setAttribute(n,e);else t.removeAttribute(n)}})}}zt("unmount",(t,n)=>{if("string"==typeof n.type){let t=n.__e;if(t){const n=t.U;if(n){t.U=void 0;for(let t in n){let e=n[t];if(e)e.d()}}}}else{let t=n.__c;if(t){const n=t.__$u;if(n){t.__$u=void 0;n.d()}}}t(n)});zt("__h",(t,n,e,_)=>{if(_<3||9===_)n.__$f|=2;t(n,e,_)});q.prototype.shouldComponentUpdate=function(t,n){const e=this.__$u;if(!(e&&void 0!==e.s||4&this.__$f))return!0;if(3&this.__$f)return!0;for(let _ in n)return!0;for(let _ in t)if("__source"!==_&&t[_]!==this.props[_])return!0;for(let _ in this.props)if(!(_ in t))return!0;return!1};function Zt(t){return Ft(()=>c(t),[])}function tn(t){const n=Dt(t);n.current=t;Jt.__$f|=4;return Ft(()=>v(()=>n.current()),[])}function nn(t){const n=Dt(t);n.current=t;Nt(()=>k(()=>n.current()),[])}var en=function(t,n,e,_){var i;n[0]=0;for(var o=1;o=5&&((i||!t&&5===_)&&(r.push(_,0,i,e),_=6),t&&(r.push(_,t,0,e),_=6)),i=""},l=0;l"===n?(_=1,i=""):i=n+i[0]:o?n===o?o="":i+=n:'"'===n||"'"===n?o=n:">"===n?(u(),_=1):_&&("="===n?(_=5,e=i,i=""):"/"===n&&(_<5||">"===t[l][s+1])?(u(),3===_&&(r=r[0]),_=r,(r=r[0]).push(2,0,_),_=0):" "===n||"\t"===n||"\n"===n||"\r"===n?(u(),_=2):i+=n),3===_&&"!--"===i&&(_=4,r=r[0])}return u(),r}(t)),n),arguments,[])).length>1?n:n[0]}var rn=on.bind(L);export{q as Component,j as Fragment,f as Signal,e as batch,ct as cloneElement,v as computed,ht as createContext,L as createElement,I as createRef,k as effect,L as h,rn as html,ft as hydrate,C as isValidElement,w as options,st as render,c as signal,Y as toChildArray,o as untracked,Vt as useCallback,tn as useComputed,At as useContext,Mt as useDebugValue,Nt as useEffect,Wt as useErrorBoundary,Ot as useId,Tt as useImperativeHandle,$t as useLayoutEffect,Ft as useMemo,Pt as useReducer,Dt as useRef,Zt as useSignal,nn as useSignalEffect,Ht as useState};
diff --git a/examples/server/themes/buttons_top/json-schema-to-grammar.mjs b/examples/server/themes/buttons_top/json-schema-to-grammar.mjs
new file mode 100644
index 000000000..8e0be1b40
--- /dev/null
+++ b/examples/server/themes/buttons_top/json-schema-to-grammar.mjs
@@ -0,0 +1,594 @@
+// WARNING: This file was ported from json_schema_to_grammar.py, please fix bugs / add features there first.
+const SPACE_RULE = '" "?';
+
+function _buildRepetition(itemRule, minItems, maxItems, opts={}) {
+ const separatorRule = opts.separatorRule ?? '';
+ const itemRuleIsLiteral = opts.itemRuleIsLiteral ?? false
+
+ if (separatorRule === '') {
+ if (minItems === 0 && maxItems === 1) {
+ return `${itemRule}?`;
+ } else if (minItems === 1 && maxItems === undefined) {
+ return `${itemRule}+`;
+ }
+ }
+
+ let result = '';
+ if (minItems > 0) {
+ if (itemRuleIsLiteral && separatorRule === '') {
+ result = `"${itemRule.slice(1, -1).repeat(minItems)}"`;
+ } else {
+ result = Array.from({ length: minItems }, () => itemRule)
+ .join(separatorRule !== '' ? ` ${separatorRule} ` : ' ');
+ }
+ }
+
+ const optRepetitions = (upToN, prefixWithSep=false) => {
+ const content = separatorRule !== '' && prefixWithSep ? `${separatorRule} ${itemRule}` : itemRule;
+ if (upToN === 0) {
+ return '';
+ } else if (upToN === 1) {
+ return `(${content})?`;
+ } else if (separatorRule !== '' && !prefixWithSep) {
+ return `(${content} ${optRepetitions(upToN - 1, true)})?`;
+ } else {
+ return Array.from({ length: upToN }, () => `(${content}`).join(' ').trim() + Array.from({ length: upToN }, () => ')?').join('');
+ }
+ };
+
+ if (minItems > 0 && maxItems !== minItems) {
+ result += ' ';
+ }
+
+ if (maxItems !== undefined) {
+ result += optRepetitions(maxItems - minItems, minItems > 0);
+ } else {
+ const itemOperator = `(${separatorRule !== '' ? separatorRule + ' ' : ''}${itemRule})`;
+
+ if (minItems === 0 && separatorRule !== '') {
+ result = `(${itemRule} ${itemOperator}*)?`;
+ } else {
+ result += `${itemOperator}*`;
+ }
+ }
+
+ return result;
+}
+
+class BuiltinRule {
+ constructor(content, deps) {
+ this.content = content;
+ this.deps = deps || [];
+ }
+}
+
+const UP_TO_15_DIGITS = _buildRepetition('[0-9]', 0, 15);
+
+const PRIMITIVE_RULES = {
+ boolean : new BuiltinRule('("true" | "false") space', []),
+ 'decimal-part' : new BuiltinRule('[0-9] ' + UP_TO_15_DIGITS, []),
+ 'integral-part': new BuiltinRule('[0-9] | [1-9] ' + UP_TO_15_DIGITS, []),
+ number : new BuiltinRule('("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space', ['integral-part', 'decimal-part']),
+ integer : new BuiltinRule('("-"? integral-part) space', ['integral-part']),
+ value : new BuiltinRule('object | array | string | number | boolean | null', ['object', 'array', 'string', 'number', 'boolean', 'null']),
+ object : new BuiltinRule('"{" space ( string ":" space value ("," space string ":" space value)* )? "}" space', ['string', 'value']),
+ array : new BuiltinRule('"[" space ( value ("," space value)* )? "]" space', ['value']),
+ uuid : new BuiltinRule('"\\"" ' + [8, 4, 4, 4, 12].map(n => [...new Array(n)].map(_ => '[0-9a-fA-F]').join('')).join(' "-" ') + ' "\\"" space', []),
+ char : new BuiltinRule(`[^"\\\\] | "\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])`, []),
+ string : new BuiltinRule(`"\\"" char* "\\"" space`, ['char']),
+ null : new BuiltinRule('"null" space', []),
+};
+
+// TODO: support "uri", "email" string formats
+const STRING_FORMAT_RULES = {
+ 'date' : new BuiltinRule('[0-9] [0-9] [0-9] [0-9] "-" ( "0" [1-9] | "1" [0-2] ) "-" ( \"0\" [1-9] | [1-2] [0-9] | "3" [0-1] )', []),
+ 'time' : new BuiltinRule('([01] [0-9] | "2" [0-3]) ":" [0-5] [0-9] ":" [0-5] [0-9] ( "." [0-9] [0-9] [0-9] )? ( "Z" | ( "+" | "-" ) ( [01] [0-9] | "2" [0-3] ) ":" [0-5] [0-9] )', []),
+ 'date-time' : new BuiltinRule('date "T" time', ['date', 'time']),
+ 'date-string' : new BuiltinRule('"\\"" date "\\"" space', ['date']),
+ 'time-string' : new BuiltinRule('"\\"" time "\\"" space', ['time']),
+ 'date-time-string': new BuiltinRule('"\\"" date-time "\\"" space', ['date-time']),
+}
+
+const RESERVED_NAMES = {'root': true, ...PRIMITIVE_RULES, ...STRING_FORMAT_RULES};
+
+const INVALID_RULE_CHARS_RE = /[^\dA-Za-z-]+/g;
+const GRAMMAR_LITERAL_ESCAPE_RE = /[\n\r"]/g;
+const GRAMMAR_RANGE_LITERAL_ESCAPE_RE = /[\n\r"\]\-\\]/g;
+const GRAMMAR_LITERAL_ESCAPES = { '\r': '\\r', '\n': '\\n', '"': '\\"', '-': '\\-', ']': '\\]' };
+
+const NON_LITERAL_SET = new Set('|.()[]{}*+?');
+const ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS = new Set('[]()|{}*+?');
+
+export class SchemaConverter {
+ constructor(options) {
+ this._propOrder = options.prop_order || {};
+ this._allowFetch = options.allow_fetch || false;
+ this._dotall = options.dotall || false;
+ this._rules = {'space': SPACE_RULE};
+ this._refs = {};
+ this._refsBeingResolved = new Set();
+ }
+
+ _formatLiteral(literal) {
+ const escaped = literal.replace(
+ GRAMMAR_LITERAL_ESCAPE_RE,
+ m => GRAMMAR_LITERAL_ESCAPES[m]
+ );
+ return `"${escaped}"`;
+ }
+
+ _formatRangeChar(literal) {
+ return JSON.stringify(literal).slice(1, -1).replace(
+ GRAMMAR_RANGE_LITERAL_ESCAPE_RE,
+ m => GRAMMAR_LITERAL_ESCAPES[m]
+ );
+ }
+
+ _addRule(name, rule) {
+ let escName = name.replace(INVALID_RULE_CHARS_RE, '-');
+ let key = escName;
+
+ if (escName in this._rules) {
+ if (this._rules[escName] === rule) {
+ return key;
+ }
+
+ let i = 0;
+ while ((`${escName}${i}` in this._rules) && (this._rules[`${escName}${i}`] !== rule)) {
+ i += 1;
+ }
+ key = `${escName}${i}`;
+ }
+
+ this._rules[key] = rule;
+ return key;
+ }
+
+ async resolveRefs(schema, url) {
+ const visit = async (n) => {
+ if (Array.isArray(n)) {
+ return Promise.all(n.map(visit));
+ } else if (typeof n === 'object' && n !== null) {
+ let ref = n.$ref;
+ let target;
+ if (ref !== undefined && !this._refs[ref]) {
+ if (ref.startsWith('https://')) {
+ if (!this._allowFetch) {
+ throw new Error('Fetching remote schemas is not allowed (use --allow-fetch for force)');
+ }
+ const fetch = (await import('node-fetch')).default;
+
+ const fragSplit = ref.split('#');
+ const baseUrl = fragSplit[0];
+
+ target = this._refs[baseUrl];
+ if (!target) {
+ target = await this.resolveRefs(await fetch(ref).then(res => res.json()), baseUrl);
+ this._refs[baseUrl] = target;
+ }
+
+ if (fragSplit.length === 1 || fragSplit[fragSplit.length - 1] === '') {
+ return target;
+ }
+ } else if (ref.startsWith('#/')) {
+ target = schema;
+ ref = `${url}${ref}`;
+ n.$ref = ref;
+ } else {
+ throw new Error(`Unsupported ref ${ref}`);
+ }
+
+ const selectors = ref.split('#')[1].split('/').slice(1);
+ for (const sel of selectors) {
+ if (!target || !(sel in target)) {
+ throw new Error(`Error resolving ref ${ref}: ${sel} not in ${JSON.stringify(target)}`);
+ }
+ target = target[sel];
+ }
+
+ this._refs[ref] = target;
+ } else {
+ await Promise.all(Object.values(n).map(visit));
+ }
+ }
+
+ return n;
+ };
+
+ return visit(schema);
+ }
+
+ _generateUnionRule(name, altSchemas) {
+ return altSchemas
+ .map((altSchema, i) => this.visit(altSchema, `${name ?? ''}${name ? '-' : 'alternative-'}${i}`))
+ .join(' | ');
+ }
+
+ _visitPattern(pattern, name) {
+ if (!pattern.startsWith('^') || !pattern.endsWith('$')) {
+ throw new Error('Pattern must start with "^" and end with "$"');
+ }
+ pattern = pattern.slice(1, -1);
+ const subRuleIds = {};
+
+ let i = 0;
+ const length = pattern.length;
+
+ const getDot = () => {
+ let rule;
+ if (this._dotall) {
+ rule = '[\\U00000000-\\U0010FFFF]';
+ } else {
+ // Accept any character... except \n and \r line break chars (\x0A and \xOD)
+ rule = '[^\\x0A\\x0D]';
+ }
+ return this._addRule('dot', rule);
+ };
+
+
+ const toRule = ([s, isLiteral]) => isLiteral ? "\"" + s + "\"" : s;
+
+ const transform = () => {
+ const start = i;
+ // For each component of this sequence, store its string representation and whether it's a literal.
+ // We only need a flat structure here to apply repetition operators to the last item, and
+ // to merge literals at the and (we're parsing grouped ( sequences ) recursively and don't treat '|' specially
+ // (GBNF's syntax is luckily very close to regular expressions!)
+ const seq = [];
+
+ const joinSeq = () => {
+ const ret = [];
+ for (const [isLiteral, g] of groupBy(seq, x => x[1])) {
+ if (isLiteral) {
+ ret.push([[...g].map(x => x[0]).join(''), true]);
+ } else {
+ ret.push(...g);
+ }
+ }
+ if (ret.length === 1) {
+ return ret[0];
+ }
+ return [ret.map(x => toRule(x)).join(' '), false];
+ };
+
+ while (i < length) {
+ const c = pattern[i];
+ if (c === '.') {
+ seq.push([getDot(), false]);
+ i += 1;
+ } else if (c === '(') {
+ i += 1;
+ if (i < length) {
+ if (pattern[i] === '?') {
+ throw new Error(`Unsupported pattern syntax "${pattern[i]}" at index ${i} of /${pattern}/`);
+ }
+ }
+ seq.push([`(${toRule(transform())})`, false]);
+ } else if (c === ')') {
+ i += 1;
+ if (start <= 0 || pattern[start - 1] !== '(') {
+ throw new Error(`Unbalanced parentheses; start = ${start}, i = ${i}, pattern = ${pattern}`);
+ }
+ return joinSeq();
+ } else if (c === '[') {
+ let squareBrackets = c;
+ i += 1;
+ while (i < length && pattern[i] !== ']') {
+ if (pattern[i] === '\\') {
+ squareBrackets += pattern.slice(i, i + 2);
+ i += 2;
+ } else {
+ squareBrackets += pattern[i];
+ i += 1;
+ }
+ }
+ if (i >= length) {
+ throw new Error(`Unbalanced square brackets; start = ${start}, i = ${i}, pattern = ${pattern}`);
+ }
+ squareBrackets += ']';
+ i += 1;
+ seq.push([squareBrackets, false]);
+ } else if (c === '|') {
+ seq.push(['|', false]);
+ i += 1;
+ } else if (c === '*' || c === '+' || c === '?') {
+ seq[seq.length - 1] = [toRule(seq[seq.length - 1]) + c, false];
+ i += 1;
+ } else if (c === '{') {
+ let curlyBrackets = c;
+ i += 1;
+ while (i < length && pattern[i] !== '}') {
+ curlyBrackets += pattern[i];
+ i += 1;
+ }
+ if (i >= length) {
+ throw new Error(`Unbalanced curly brackets; start = ${start}, i = ${i}, pattern = ${pattern}`);
+ }
+ curlyBrackets += '}';
+ i += 1;
+ const nums = curlyBrackets.slice(1, -1).split(',').map(s => s.trim());
+ let minTimes, maxTimes;
+ if (nums.length === 1) {
+ minTimes = parseInt(nums[0], 10);
+ maxTimes = minTimes;
+ } else {
+ if (nums.length !== 2) {
+ throw new Error(`Invalid quantifier ${curlyBrackets}`);
+ }
+ minTimes = nums[0] ? parseInt(nums[0], 10) : 0;
+ maxTimes = nums[1] ? parseInt(nums[1], 10) : Infinity;
+ }
+
+ let [sub, subIsLiteral] = seq[seq.length - 1];
+
+ if (!subIsLiteral) {
+ let id = subRuleIds[sub];
+ if (id === undefined) {
+ id = this._addRule(`${name}-${Object.keys(subRuleIds).length + 1}`, sub);
+ subRuleIds[sub] = id;
+ }
+ sub = id;
+ }
+
+ seq[seq.length - 1] = [
+ _buildRepetition(subIsLiteral ? `"${sub}"` : sub, minTimes, maxTimes, {itemRuleIsLiteral: subIsLiteral}),
+ false
+ ];
+ } else {
+ let literal = '';
+ while (i < length) {
+ if (pattern[i] === '\\' && i < length - 1) {
+ const next = pattern[i + 1];
+ if (ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS.has(next)) {
+ i += 1;
+ literal += pattern[i];
+ i += 1;
+ } else {
+ literal += pattern.slice(i, i + 2);
+ i += 2;
+ }
+ } else if (pattern[i] === '"') {
+ literal += '\\"';
+ i += 1;
+ } else if (!NON_LITERAL_SET.has(pattern[i]) &&
+ (i === length - 1 || literal === '' || pattern[i + 1] === '.' || !NON_LITERAL_SET.has(pattern[i+1]))) {
+ literal += pattern[i];
+ i += 1;
+ } else {
+ break;
+ }
+ }
+ if (literal !== '') {
+ seq.push([literal, true]);
+ }
+ }
+ }
+
+ return joinSeq();
+ };
+
+ return this._addRule(name, "\"\\\"\" " + toRule(transform()) + " \"\\\"\" space")
+ }
+
+ _resolveRef(ref) {
+ let refName = ref.split('/').pop();
+ if (!(refName in this._rules) && !this._refsBeingResolved.has(ref)) {
+ this._refsBeingResolved.add(ref);
+ const resolved = this._refs[ref];
+ refName = this.visit(resolved, refName);
+ this._refsBeingResolved.delete(ref);
+ }
+ return refName;
+ }
+
+ _generateConstantRule(value) {
+ return this._formatLiteral(JSON.stringify(value));
+ }
+
+ visit(schema, name) {
+ const schemaType = schema.type;
+ const schemaFormat = schema.format;
+ const ruleName = name in RESERVED_NAMES ? name + '-' : name == '' ? 'root' : name;
+
+ const ref = schema.$ref;
+ if (ref !== undefined) {
+ return this._addRule(ruleName, this._resolveRef(ref));
+ } else if (schema.oneOf || schema.anyOf) {
+ return this._addRule(ruleName, this._generateUnionRule(name, schema.oneOf || schema.anyOf));
+ } else if (Array.isArray(schemaType)) {
+ return this._addRule(ruleName, this._generateUnionRule(name, schemaType.map(t => ({ type: t }))));
+ } else if ('const' in schema) {
+ return this._addRule(ruleName, this._generateConstantRule(schema.const));
+ } else if ('enum' in schema) {
+ const rule = schema.enum.map(v => this._generateConstantRule(v)).join(' | ');
+ return this._addRule(ruleName, rule);
+ } else if ((schemaType === undefined || schemaType === 'object') &&
+ ('properties' in schema ||
+ ('additionalProperties' in schema && schema.additionalProperties !== true))) {
+ const required = new Set(schema.required || []);
+ const properties = Object.entries(schema.properties ?? {});
+ return this._addRule(ruleName, this._buildObjectRule(properties, required, name, schema.additionalProperties));
+ } else if ((schemaType === undefined || schemaType === 'object') && 'allOf' in schema) {
+ const required = new Set();
+ const properties = [];
+ const addComponent = (compSchema, isRequired) => {
+ const ref = compSchema.$ref;
+ if (ref !== undefined) {
+ compSchema = this._refs[ref];
+ }
+
+ if ('properties' in compSchema) {
+ for (const [propName, propSchema] of Object.entries(compSchema.properties)) {
+ properties.push([propName, propSchema]);
+ if (isRequired) {
+ required.add(propName);
+ }
+ }
+ }
+ };
+
+ for (const t of schema.allOf) {
+ if ('anyOf' in t) {
+ for (const tt of t.anyOf) {
+ addComponent(tt, false);
+ }
+ } else {
+ addComponent(t, true);
+ }
+ }
+
+ return this._addRule(ruleName, this._buildObjectRule(properties, required, name, /* additionalProperties= */ false));
+ } else if ((schemaType === undefined || schemaType === 'array') && ('items' in schema || 'prefixItems' in schema)) {
+ const items = schema.items ?? schema.prefixItems;
+ if (Array.isArray(items)) {
+ return this._addRule(
+ ruleName,
+ '"[" space ' +
+ items.map((item, i) => this.visit(item, `${name ?? ''}${name ? '-' : ''}tuple-${i}`)).join(' "," space ') +
+ ' "]" space'
+ );
+ } else {
+ const itemRuleName = this.visit(items, `${name ?? ''}${name ? '-' : ''}item`);
+ const minItems = schema.minItems || 0;
+ const maxItems = schema.maxItems;
+ return this._addRule(ruleName, '"[" space ' + _buildRepetition(itemRuleName, minItems, maxItems, {separatorRule: '"," space'}) + ' "]" space');
+ }
+ } else if ((schemaType === undefined || schemaType === 'string') && 'pattern' in schema) {
+ return this._visitPattern(schema.pattern, ruleName);
+ } else if ((schemaType === undefined || schemaType === 'string') && /^uuid[1-5]?$/.test(schema.format || '')) {
+ return this._addPrimitive(
+ ruleName === 'root' ? 'root' : schemaFormat,
+ PRIMITIVE_RULES['uuid']
+ );
+ } else if ((schemaType === undefined || schemaType === 'string') && `${schema.format}-string` in STRING_FORMAT_RULES) {
+ const primName = `${schema.format}-string`
+ return this._addRule(ruleName, this._addPrimitive(primName, STRING_FORMAT_RULES[primName]));
+ } else if (schemaType === 'string' && ('minLength' in schema || 'maxLength' in schema)) {
+ const charRuleName = this._addPrimitive('char', PRIMITIVE_RULES['char']);
+ const minLen = schema.minLength || 0;
+ const maxLen = schema.maxLength;
+ return this._addRule(ruleName, '"\\\"" ' + _buildRepetition(charRuleName, minLen, maxLen) + ' "\\\"" space');
+ } else if ((schemaType === 'object') || (Object.keys(schema).length === 0)) {
+ return this._addRule(ruleName, this._addPrimitive('object', PRIMITIVE_RULES['object']));
+ } else {
+ if (!(schemaType in PRIMITIVE_RULES)) {
+ throw new Error(`Unrecognized schema: ${JSON.stringify(schema)}`);
+ }
+ // TODO: support minimum, maximum, exclusiveMinimum, exclusiveMaximum at least for zero
+ return this._addPrimitive(ruleName === 'root' ? 'root' : schemaType, PRIMITIVE_RULES[schemaType]);
+ }
+ }
+
+ _addPrimitive(name, rule) {
+ let n = this._addRule(name, rule.content);
+ for (const dep of rule.deps) {
+ const depRule = PRIMITIVE_RULES[dep] || STRING_FORMAT_RULES[dep];
+ if (!depRule) {
+ throw new Error(`Rule ${dep} not known`);
+ }
+ if (!(dep in this._rules)) {
+ this._addPrimitive(dep, depRule);
+ }
+ }
+ return n;
+ }
+
+ _buildObjectRule(properties, required, name, additionalProperties) {
+ const propOrder = this._propOrder;
+ // sort by position in prop_order (if specified) then by original order
+ const sortedProps = properties.map(([k]) => k).sort((a, b) => {
+ const orderA = propOrder[a] || Infinity;
+ const orderB = propOrder[b] || Infinity;
+ return orderA - orderB || properties.findIndex(([k]) => k === a) - properties.findIndex(([k]) => k === b);
+ });
+
+ const propKvRuleNames = {};
+ for (const [propName, propSchema] of properties) {
+ const propRuleName = this.visit(propSchema, `${name ?? ''}${name ? '-' : ''}${propName}`);
+ propKvRuleNames[propName] = this._addRule(
+ `${name ?? ''}${name ? '-' : ''}${propName}-kv`,
+ `${this._formatLiteral(JSON.stringify(propName))} space ":" space ${propRuleName}`
+ );
+ }
+ const requiredProps = sortedProps.filter(k => required.has(k));
+ const optionalProps = sortedProps.filter(k => !required.has(k));
+
+ if (typeof additionalProperties === 'object' || additionalProperties === true) {
+ const subName = `${name ?? ''}${name ? '-' : ''}additional`;
+ const valueRule = this.visit(additionalProperties === true ? {} : additionalProperties, `${subName}-value`);
+ propKvRuleNames['*'] = this._addRule(
+ `${subName}-kv`,
+ `${this._addPrimitive('string', PRIMITIVE_RULES['string'])} ":" space ${valueRule}`);
+ optionalProps.push('*');
+ }
+
+ let rule = '"{" space ';
+ rule += requiredProps.map(k => propKvRuleNames[k]).join(' "," space ');
+
+ if (optionalProps.length > 0) {
+ rule += ' (';
+ if (requiredProps.length > 0) {
+ rule += ' "," space ( ';
+ }
+
+ const getRecursiveRefs = (ks, firstIsOptional) => {
+ const [k, ...rest] = ks;
+ const kvRuleName = propKvRuleNames[k];
+ let res;
+ if (k === '*') {
+ res = this._addRule(
+ `${name ?? ''}${name ? '-' : ''}additional-kvs`,
+ `${kvRuleName} ( "," space ` + kvRuleName + ` )*`
+ )
+ } else if (firstIsOptional) {
+ res = `( "," space ${kvRuleName} )?`;
+ } else {
+ res = kvRuleName;
+ }
+ if (rest.length > 0) {
+ res += ' ' + this._addRule(
+ `${name ?? ''}${name ? '-' : ''}${k}-rest`,
+ getRecursiveRefs(rest, true)
+ );
+ }
+ return res;
+ };
+
+ rule += optionalProps.map((_, i) => getRecursiveRefs(optionalProps.slice(i), false)).join(' | ');
+ if (requiredProps.length > 0) {
+ rule += ' )';
+ }
+ rule += ' )?';
+ }
+
+ rule += ' "}" space';
+
+ return rule;
+ }
+
+ formatGrammar() {
+ let grammar = '';
+ for (const [name, rule] of Object.entries(this._rules).sort(([a], [b]) => a.localeCompare(b))) {
+ grammar += `${name} ::= ${rule}\n`;
+ }
+ return grammar;
+ }
+}
+
+// Helper function to group elements by a key function
+function* groupBy(iterable, keyFn) {
+ let lastKey = null;
+ let group = [];
+ for (const element of iterable) {
+ const key = keyFn(element);
+ if (lastKey !== null && key !== lastKey) {
+ yield [lastKey, group];
+ group = [];
+ }
+ group.push(element);
+ lastKey = key;
+ }
+ if (group.length > 0) {
+ yield [lastKey, group];
+ }
+}
diff --git a/examples/server/themes/wild/README.md b/examples/server/themes/wild/README.md
new file mode 100644
index 000000000..dc5572f63
--- /dev/null
+++ b/examples/server/themes/wild/README.md
@@ -0,0 +1,5 @@
+# LLaMA.cpp Server Wild Theme
+
+Simple tweaks to the UI. To use simply run server with `--path=themes/wild`
+
+
\ No newline at end of file
diff --git a/examples/server/themes/wild/completion.js b/examples/server/themes/wild/completion.js
new file mode 100644
index 000000000..987b9a3b8
--- /dev/null
+++ b/examples/server/themes/wild/completion.js
@@ -0,0 +1,204 @@
+const paramDefaults = {
+ stream: true,
+ n_predict: 500,
+ temperature: 0.2,
+ stop: [""]
+};
+
+let generation_settings = null;
+
+
+// Completes the prompt as a generator. Recommended for most use cases.
+//
+// Example:
+//
+// import { llama } from '/completion.js'
+//
+// const request = llama("Tell me a joke", {n_predict: 800})
+// for await (const chunk of request) {
+// document.write(chunk.data.content)
+// }
+//
+export async function* llama(prompt, params = {}, config = {}) {
+ let controller = config.controller;
+ const api_url = config.api_url || "";
+
+ if (!controller) {
+ controller = new AbortController();
+ }
+
+ const completionParams = { ...paramDefaults, ...params, prompt };
+
+ const response = await fetch(`${api_url}/completion`, {
+ method: 'POST',
+ body: JSON.stringify(completionParams),
+ headers: {
+ 'Connection': 'keep-alive',
+ 'Content-Type': 'application/json',
+ 'Accept': 'text/event-stream',
+ ...(params.api_key ? {'Authorization': `Bearer ${params.api_key}`} : {})
+ },
+ signal: controller.signal,
+ });
+
+ const reader = response.body.getReader();
+ const decoder = new TextDecoder();
+
+ let content = "";
+ let leftover = ""; // Buffer for partially read lines
+
+ try {
+ let cont = true;
+
+ while (cont) {
+ const result = await reader.read();
+ if (result.done) {
+ break;
+ }
+
+ // Add any leftover data to the current chunk of data
+ const text = leftover + decoder.decode(result.value);
+
+ // Check if the last character is a line break
+ const endsWithLineBreak = text.endsWith('\n');
+
+ // Split the text into lines
+ let lines = text.split('\n');
+
+ // If the text doesn't end with a line break, then the last line is incomplete
+ // Store it in leftover to be added to the next chunk of data
+ if (!endsWithLineBreak) {
+ leftover = lines.pop();
+ } else {
+ leftover = ""; // Reset leftover if we have a line break at the end
+ }
+
+ // Parse all sse events and add them to result
+ const regex = /^(\S+):\s(.*)$/gm;
+ for (const line of lines) {
+ const match = regex.exec(line);
+ if (match) {
+ result[match[1]] = match[2]
+ // since we know this is llama.cpp, let's just decode the json in data
+ if (result.data) {
+ result.data = JSON.parse(result.data);
+ content += result.data.content;
+
+ // yield
+ yield result;
+
+ // if we got a stop token from server, we will break here
+ if (result.data.stop) {
+ if (result.data.generation_settings) {
+ generation_settings = result.data.generation_settings;
+ }
+ cont = false;
+ break;
+ }
+ }
+ if (result.error) {
+ try {
+ result.error = JSON.parse(result.error);
+ if (result.error.message.includes('slot unavailable')) {
+ // Throw an error to be caught by upstream callers
+ throw new Error('slot unavailable');
+ } else {
+ console.error(`llama.cpp error [${result.error.code} - ${result.error.type}]: ${result.error.message}`);
+ }
+ } catch(e) {
+ console.error(`llama.cpp error ${result.error}`)
+ }
+ }
+ }
+ }
+ }
+ } catch (e) {
+ if (e.name !== 'AbortError') {
+ console.error("llama error: ", e);
+ }
+ throw e;
+ }
+ finally {
+ controller.abort();
+ }
+
+ return content;
+}
+
+// Call llama, return an event target that you can subscribe to
+//
+// Example:
+//
+// import { llamaEventTarget } from '/completion.js'
+//
+// const conn = llamaEventTarget(prompt)
+// conn.addEventListener("message", (chunk) => {
+// document.write(chunk.detail.content)
+// })
+//
+export const llamaEventTarget = (prompt, params = {}, config = {}) => {
+ const eventTarget = new EventTarget();
+ (async () => {
+ let content = "";
+ for await (const chunk of llama(prompt, params, config)) {
+ if (chunk.data) {
+ content += chunk.data.content;
+ eventTarget.dispatchEvent(new CustomEvent("message", { detail: chunk.data }));
+ }
+ if (chunk.data.generation_settings) {
+ eventTarget.dispatchEvent(new CustomEvent("generation_settings", { detail: chunk.data.generation_settings }));
+ }
+ if (chunk.data.timings) {
+ eventTarget.dispatchEvent(new CustomEvent("timings", { detail: chunk.data.timings }));
+ }
+ }
+ eventTarget.dispatchEvent(new CustomEvent("done", { detail: { content } }));
+ })();
+ return eventTarget;
+}
+
+// Call llama, return a promise that resolves to the completed text. This does not support streaming
+//
+// Example:
+//
+// llamaPromise(prompt).then((content) => {
+// document.write(content)
+// })
+//
+// or
+//
+// const content = await llamaPromise(prompt)
+// document.write(content)
+//
+export const llamaPromise = (prompt, params = {}, config = {}) => {
+ return new Promise(async (resolve, reject) => {
+ let content = "";
+ try {
+ for await (const chunk of llama(prompt, params, config)) {
+ content += chunk.data.content;
+ }
+ resolve(content);
+ } catch (error) {
+ reject(error);
+ }
+ });
+};
+
+/**
+ * (deprecated)
+ */
+export const llamaComplete = async (params, controller, callback) => {
+ for await (const chunk of llama(params.prompt, params, { controller })) {
+ callback(chunk);
+ }
+}
+
+// Get the model info from the server. This is useful for getting the context window and so on.
+export const llamaModelInfo = async (config = {}) => {
+ if (!generation_settings) {
+ const api_url = config.api_url || "";
+ const props = await fetch(`${api_url}/props`).then(r => r.json());
+ generation_settings = props.default_generation_settings;
+ }
+ return generation_settings;
+}
diff --git a/examples/server/themes/wild/favicon.ico b/examples/server/themes/wild/favicon.ico
new file mode 100644
index 000000000..89e154a0a
Binary files /dev/null and b/examples/server/themes/wild/favicon.ico differ
diff --git a/examples/server/themes/wild/index.html b/examples/server/themes/wild/index.html
new file mode 100644
index 000000000..90dea5fdc
--- /dev/null
+++ b/examples/server/themes/wild/index.html
@@ -0,0 +1,1061 @@
+
+
+
+
+
+
+ llama.cpp - chat
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/server/themes/wild/index.js b/examples/server/themes/wild/index.js
new file mode 100644
index 000000000..695aec256
--- /dev/null
+++ b/examples/server/themes/wild/index.js
@@ -0,0 +1 @@
+const t=Symbol.for("preact-signals");function n(){if(r>1){r--;return}let t,n=!1;while(void 0!==i){let _=i;i=void 0;u++;while(void 0!==_){const i=_.o;_.o=void 0;_.f&=-3;if(!(8&_.f)&&h(_))try{_.c()}catch(e){if(!n){t=e;n=!0}}_=i}}u=0;r--;if(n)throw t}function e(t){if(r>0)return t();r++;try{return t()}finally{n()}}let _,i;function o(t){const n=_;_=void 0;try{return t()}finally{_=n}}let r=0,u=0,l=0;function s(t){if(void 0===_)return;let n=t.n;if(void 0===n||n.t!==_){n={i:0,S:t,p:_.s,n:void 0,t:_,e:void 0,x:void 0,r:n};if(void 0!==_.s)_.s.n=n;_.s=n;t.n=n;if(32&_.f)t.S(n);return n}else if(-1===n.i){n.i=0;if(void 0!==n.n){n.n.p=n.p;if(void 0!==n.p)n.p.n=n.n;n.p=_.s;n.n=void 0;_.s.n=n;_.s=n}return n}}function f(t){this.v=t;this.i=0;this.n=void 0;this.t=void 0}f.prototype.brand=t;f.prototype.h=function(){return!0};f.prototype.S=function(t){if(this.t!==t&&void 0===t.e){t.x=this.t;if(void 0!==this.t)this.t.e=t;this.t=t}};f.prototype.U=function(t){if(void 0!==this.t){const n=t.e,e=t.x;if(void 0!==n){n.x=e;t.e=void 0}if(void 0!==e){e.e=n;t.x=void 0}if(t===this.t)this.t=e}};f.prototype.subscribe=function(t){return k(()=>{const n=this.value,e=_;_=void 0;try{t(n)}finally{_=e}})};f.prototype.valueOf=function(){return this.value};f.prototype.toString=function(){return this.value+""};f.prototype.toJSON=function(){return this.value};f.prototype.peek=function(){const t=_;_=void 0;try{return this.value}finally{_=t}};Object.defineProperty(f.prototype,"value",{get(){const t=s(this);if(void 0!==t)t.i=this.i;return this.v},set(t){if(t!==this.v){if(u>100)throw new Error("Cycle detected");this.v=t;this.i++;l++;r++;try{for(let t=this.t;void 0!==t;t=t.x)t.t.N()}finally{n()}}}});function c(t){return new f(t)}function h(t){for(let n=t.s;void 0!==n;n=n.n)if(n.S.i!==n.i||!n.S.h()||n.S.i!==n.i)return!0;return!1}function a(t){for(let n=t.s;void 0!==n;n=n.n){const e=n.S.n;if(void 0!==e)n.r=e;n.S.n=n;n.i=-1;if(void 0===n.n){t.s=n;break}}}function p(t){let n,e=t.s;while(void 0!==e){const t=e.p;if(-1===e.i){e.S.U(e);if(void 0!==t)t.n=e.n;if(void 0!==e.n)e.n.p=t}else n=e;e.S.n=e.r;if(void 0!==e.r)e.r=void 0;e=t}t.s=n}function d(t){f.call(this,void 0);this.x=t;this.s=void 0;this.g=l-1;this.f=4}(d.prototype=new f).h=function(){this.f&=-3;if(1&this.f)return!1;if(32==(36&this.f))return!0;this.f&=-5;if(this.g===l)return!0;this.g=l;this.f|=1;if(this.i>0&&!h(this)){this.f&=-2;return!0}const t=_;try{a(this);_=this;const t=this.x();if(16&this.f||this.v!==t||0===this.i){this.v=t;this.f&=-17;this.i++}}catch(t){this.v=t;this.f|=16;this.i++}_=t;p(this);this.f&=-2;return!0};d.prototype.S=function(t){if(void 0===this.t){this.f|=36;for(let t=this.s;void 0!==t;t=t.n)t.S.S(t)}f.prototype.S.call(this,t)};d.prototype.U=function(t){if(void 0!==this.t){f.prototype.U.call(this,t);if(void 0===this.t){this.f&=-33;for(let t=this.s;void 0!==t;t=t.n)t.S.U(t)}}};d.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(let t=this.t;void 0!==t;t=t.x)t.t.N()}};Object.defineProperty(d.prototype,"value",{get(){if(1&this.f)throw new Error("Cycle detected");const t=s(this);this.h();if(void 0!==t)t.i=this.i;if(16&this.f)throw this.v;return this.v}});function v(t){return new d(t)}function y(t){const e=t.u;t.u=void 0;if("function"==typeof e){r++;const i=_;_=void 0;try{e()}catch(n){t.f&=-2;t.f|=8;m(t);throw n}finally{_=i;n()}}}function m(t){for(let n=t.s;void 0!==n;n=n.n)n.S.U(n);t.x=void 0;t.s=void 0;y(t)}function g(t){if(_!==this)throw new Error("Out-of-order effect");p(this);_=t;this.f&=-2;if(8&this.f)m(this);n()}function b(t){this.x=t;this.u=void 0;this.s=void 0;this.o=void 0;this.f=32}b.prototype.c=function(){const t=this.S();try{if(8&this.f)return;if(void 0===this.x)return;const n=this.x();if("function"==typeof n)this.u=n}finally{t()}};b.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1;this.f&=-9;y(this);a(this);r++;const t=_;_=this;return g.bind(this,t)};b.prototype.N=function(){if(!(2&this.f)){this.f|=2;this.o=i;i=this}};b.prototype.d=function(){this.f|=8;if(!(1&this.f))m(this)};function k(t){const n=new b(t);try{n.c()}catch(t){n.d();throw t}return n.d.bind(n)}var S,w,x,C,E,U,H,P,N,$,D,T,F={},V=[],A=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i,M=Array.isArray;function W(t,n){for(var e in n)t[e]=n[e];return t}function O(t){var n=t.parentNode;n&&n.removeChild(t)}function L(t,n,e){var _,i,o,r={};for(o in n)"key"==o?_=n[o]:"ref"==o?i=n[o]:r[o]=n[o];if(arguments.length>2&&(r.children=arguments.length>3?S.call(arguments,2):e),"function"==typeof t&&null!=t.defaultProps)for(o in t.defaultProps)void 0===r[o]&&(r[o]=t.defaultProps[o]);return R(t,r,_,i,null)}function R(t,n,e,_,i){var o={type:t,props:n,key:e,ref:_,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,constructor:void 0,__v:null==i?++x:i,__i:-1,__u:0};return null==i&&null!=w.vnode&&w.vnode(o),o}function I(){return{current:null}}function j(t){return t.children}function q(t,n){this.props=t,this.context=n}function B(t,n){if(null==n)return t.__?B(t.__,t.__i+1):null;for(var e;nn&&E.sort(P));J.__r=0}function K(t,n,e,_,i,o,r,u,l,s,f){var c,h,a,p,d,v=_&&_.__k||V,y=n.length;for(e.__d=l,Q(e,n,v),l=e.__d,c=0;c0?R(i.type,i.props,i.key,i.ref?i.ref:null,i.__v):i)?(i.__=t,i.__b=t.__b+1,u=Z(i,e,r,f),i.__i=u,o=null,-1!==u&&(f--,(o=e[u])&&(o.__u|=131072)),null==o||null===o.__v?(-1==u&&c--,"function"!=typeof i.type&&(i.__u|=65536)):u!==r&&(u===r+1?c++:u>r?f>l-r?c+=u-r:c--:u(null!=l&&0==(131072&l.__u)?1:0))for(;r>=0||u=0){if((l=n[r])&&0==(131072&l.__u)&&i==l.key&&o===l.type)return r;r--}if(u2&&(u.children=arguments.length>3?S.call(arguments,2):e),R(t.type,u,_||t.key,i||t.ref,null)}function ht(t,n){var e={__c:n="__cC"+T++,__:t,Consumer:function(t,n){return t.children(n)},Provider:function(t){var e,_;return this.getChildContext||(e=[],(_={})[n]=this,this.getChildContext=function(){return _},this.shouldComponentUpdate=function(t){this.props.value!==t.value&&e.some((function(t){t.__e=!0,z(t)}))},this.sub=function(t){e.push(t);var n=t.componentWillUnmount;t.componentWillUnmount=function(){e.splice(e.indexOf(t),1),n&&n.call(t)}}),t.children}};return e.Provider.__=e.Consumer.contextType=e}S=V.slice,w={__e:function(t,n,e,_){for(var i,o,r;n=n.__;)if((i=n.__c)&&!i.__)try{if((o=i.constructor)&&null!=o.getDerivedStateFromError&&(i.setState(o.getDerivedStateFromError(t)),r=i.__d),null!=i.componentDidCatch&&(i.componentDidCatch(t,_||{}),r=i.__d),r)return i.__E=i}catch(n){t=n}throw t}},x=0,C=function(t){return null!=t&&null==t.constructor},q.prototype.setState=function(t,n){var e;e=null!=this.__s&&this.__s!==this.state?this.__s:this.__s=W({},this.state),"function"==typeof t&&(t=t(W({},e),this.props)),t&&W(e,t),null!=t&&this.__v&&(n&&this._sb.push(n),z(this))},q.prototype.forceUpdate=function(t){this.__v&&(this.__e=!0,t&&this.__h.push(t),z(this))},q.prototype.render=j,E=[],H="function"==typeof Promise?Promise.prototype.then.bind(Promise.resolve()):setTimeout,P=function(t,n){return t.__v.__b-n.__v.__b},J.__r=0,N=0,$=et(!1),D=et(!0),T=0;var at,pt,dt,vt,yt=0,mt=[],gt=[],bt=w,kt=bt.__b,St=bt.__r,wt=bt.diffed,xt=bt.__c,Ct=bt.unmount,Et=bt.__;function Ut(t,n){bt.__h&&bt.__h(pt,t,yt||n),yt=0;var e=pt.__H||(pt.__H={__:[],__h:[]});return t>=e.__.length&&e.__.push({__V:gt}),e.__[t]}function Ht(t){return yt=1,Pt(Gt,t)}function Pt(t,n,e){var _=Ut(at++,2);if(_.t=t,!_.__c&&(_.__=[e?e(n):Gt(void 0,n),function(t){var n=_.__N?_.__N[0]:_.__[0],e=_.t(n,t);n!==e&&(_.__N=[e,_.__[1]],_.__c.setState({}))}],_.__c=pt,!pt.u)){var i=function(t,n,e){if(!_.__c.__H)return!0;var i=_.__c.__H.__.filter((function(t){return!!t.__c}));if(i.every((function(t){return!t.__N})))return!o||o.call(this,t,n,e);var r=!1;return i.forEach((function(t){if(t.__N){var n=t.__[0];t.__=t.__N,t.__N=void 0,n!==t.__[0]&&(r=!0)}})),!(!r&&_.__c.props===t)&&(!o||o.call(this,t,n,e))};pt.u=!0;var o=pt.shouldComponentUpdate,r=pt.componentWillUpdate;pt.componentWillUpdate=function(t,n,e){if(this.__e){var _=o;o=void 0,i(t,n,e),o=_}r&&r.call(this,t,n,e)},pt.shouldComponentUpdate=i}return _.__N||_.__}function Nt(t,n){var e=Ut(at++,3);!bt.__s&&Bt(e.__H,n)&&(e.__=t,e.i=n,pt.__H.__h.push(e))}function $t(t,n){var e=Ut(at++,4);!bt.__s&&Bt(e.__H,n)&&(e.__=t,e.i=n,pt.__h.push(e))}function Dt(t){return yt=5,Ft((function(){return{current:t}}),[])}function Tt(t,n,e){yt=6,$t((function(){return"function"==typeof t?(t(n()),function(){return t(null)}):t?(t.current=n(),function(){return t.current=null}):void 0}),null==e?e:e.concat(t))}function Ft(t,n){var e=Ut(at++,7);return Bt(e.__H,n)?(e.__V=t(),e.i=n,e.__h=t,e.__V):e.__}function Vt(t,n){return yt=8,Ft((function(){return t}),n)}function At(t){var n=pt.context[t.__c],e=Ut(at++,9);return e.c=t,n?(null==e.__&&(e.__=!0,n.sub(pt)),n.props.value):t.__}function Mt(t,n){bt.useDebugValue&&bt.useDebugValue(n?n(t):t)}function Wt(t){var n=Ut(at++,10),e=Ht();return n.__=t,pt.componentDidCatch||(pt.componentDidCatch=function(t,_){n.__&&n.__(t,_),e[1](t)}),[e[0],function(){e[1](void 0)}]}function Ot(){var t=Ut(at++,11);if(!t.__){for(var n=pt.__v;null!==n&&!n.__m&&null!==n.__;)n=n.__;var e=n.__m||(n.__m=[0,0]);t.__="P"+e[0]+"-"+e[1]++}return t.__}function Lt(){for(var t;t=mt.shift();)if(t.__P&&t.__H)try{t.__H.__h.forEach(jt),t.__H.__h.forEach(qt),t.__H.__h=[]}catch(n){t.__H.__h=[],bt.__e(n,t.__v)}}bt.__b=function(t){pt=null,kt&&kt(t)},bt.__=function(t,n){t&&n.__k&&n.__k.__m&&(t.__m=n.__k.__m),Et&&Et(t,n)},bt.__r=function(t){St&&St(t),at=0;var n=(pt=t.__c).__H;n&&(dt===pt?(n.__h=[],pt.__h=[],n.__.forEach((function(t){t.__N&&(t.__=t.__N),t.__V=gt,t.__N=t.i=void 0}))):(n.__h.forEach(jt),n.__h.forEach(qt),n.__h=[],at=0)),dt=pt},bt.diffed=function(t){wt&&wt(t);var n=t.__c;n&&n.__H&&(n.__H.__h.length&&(1!==mt.push(n)&&vt===bt.requestAnimationFrame||((vt=bt.requestAnimationFrame)||It)(Lt)),n.__H.__.forEach((function(t){t.i&&(t.__H=t.i),t.__V!==gt&&(t.__=t.__V),t.i=void 0,t.__V=gt}))),dt=pt=null},bt.__c=function(t,n){n.some((function(t){try{t.__h.forEach(jt),t.__h=t.__h.filter((function(t){return!t.__||qt(t)}))}catch(r){n.some((function(t){t.__h&&(t.__h=[])})),n=[],bt.__e(r,t.__v)}})),xt&&xt(t,n)},bt.unmount=function(t){Ct&&Ct(t);var n,e=t.__c;e&&e.__H&&(e.__H.__.forEach((function(t){try{jt(t)}catch(t){n=t}})),e.__H=void 0,n&&bt.__e(n,e.__v))};var Rt="function"==typeof requestAnimationFrame;function It(t){var n,e=function(){clearTimeout(_),Rt&&cancelAnimationFrame(n),setTimeout(t)},_=setTimeout(e,100);Rt&&(n=requestAnimationFrame(e))}function jt(t){var n=pt,e=t.__c;"function"==typeof e&&(t.__c=void 0,e()),pt=n}function qt(t){var n=pt;t.__c=t.__(),pt=n}function Bt(t,n){return!t||t.length!==n.length||n.some((function(n,e){return n!==t[e]}))}function Gt(t,n){return"function"==typeof n?n(t):n}function zt(t,n){w[t]=n.bind(null,w[t]||(()=>{}))}let Jt,Kt;function Qt(t){if(Kt)Kt();Kt=t&&t.S()}function Xt({data:t}){const n=Zt(t);n.value=t;const e=Ft(()=>{let t=this.__v;while(t=t.__)if(t.__c){t.__c.__$f|=4;break}this.__$u.c=()=>{var t;if(!C(e.peek())&&3===(null==(t=this.base)?void 0:t.nodeType))this.base.data=e.peek();else{this.__$f|=1;this.setState({})}};return v(()=>{let t=n.value.value;return 0===t?0:!0===t?"":t||""})},[]);return e.value}Xt.displayName="_st";Object.defineProperties(f.prototype,{constructor:{configurable:!0,value:void 0},type:{configurable:!0,value:Xt},props:{configurable:!0,get(){return{data:this}}},__b:{configurable:!0,value:1}});zt("__b",(t,n)=>{if("string"==typeof n.type){let t,e=n.props;for(let _ in e){if("children"===_)continue;let i=e[_];if(i instanceof f){if(!t)n.__np=t={};t[_]=i;e[_]=i.peek()}}}t(n)});zt("__r",(t,n)=>{Qt();let e,_=n.__c;if(_){_.__$f&=-2;e=_.__$u;if(void 0===e)_.__$u=e=function(t){let n;k((function(){n=this}));n.c=()=>{_.__$f|=1;_.setState({})};return n}()}Jt=_;Qt(e);t(n)});zt("__e",(t,n,e,_)=>{Qt();Jt=void 0;t(n,e,_)});zt("diffed",(t,n)=>{Qt();Jt=void 0;let e;if("string"==typeof n.type&&(e=n.__e)){let t=n.__np,_=n.props;if(t){let n=e.U;if(n)for(let e in n){let _=n[e];if(void 0!==_&&!(e in t)){_.d();n[e]=void 0}}else{n={};e.U=n}for(let i in t){let o=n[i],r=t[i];if(void 0===o){o=Yt(e,i,r,_);n[i]=o}else o.o(r,_)}}}t(n)});function Yt(t,n,e,_){const i=n in t&&void 0===t.ownerSVGElement,o=c(e);return{o:(t,n)=>{o.value=t;_=n},d:k(()=>{const e=o.value.value;if(_[n]!==e){_[n]=e;if(i)t[n]=e;else if(e)t.setAttribute(n,e);else t.removeAttribute(n)}})}}zt("unmount",(t,n)=>{if("string"==typeof n.type){let t=n.__e;if(t){const n=t.U;if(n){t.U=void 0;for(let t in n){let e=n[t];if(e)e.d()}}}}else{let t=n.__c;if(t){const n=t.__$u;if(n){t.__$u=void 0;n.d()}}}t(n)});zt("__h",(t,n,e,_)=>{if(_<3||9===_)n.__$f|=2;t(n,e,_)});q.prototype.shouldComponentUpdate=function(t,n){const e=this.__$u;if(!(e&&void 0!==e.s||4&this.__$f))return!0;if(3&this.__$f)return!0;for(let _ in n)return!0;for(let _ in t)if("__source"!==_&&t[_]!==this.props[_])return!0;for(let _ in this.props)if(!(_ in t))return!0;return!1};function Zt(t){return Ft(()=>c(t),[])}function tn(t){const n=Dt(t);n.current=t;Jt.__$f|=4;return Ft(()=>v(()=>n.current()),[])}function nn(t){const n=Dt(t);n.current=t;Nt(()=>k(()=>n.current()),[])}var en=function(t,n,e,_){var i;n[0]=0;for(var o=1;o=5&&((i||!t&&5===_)&&(r.push(_,0,i,e),_=6),t&&(r.push(_,t,0,e),_=6)),i=""},l=0;l"===n?(_=1,i=""):i=n+i[0]:o?n===o?o="":i+=n:'"'===n||"'"===n?o=n:">"===n?(u(),_=1):_&&("="===n?(_=5,e=i,i=""):"/"===n&&(_<5||">"===t[l][s+1])?(u(),3===_&&(r=r[0]),_=r,(r=r[0]).push(2,0,_),_=0):" "===n||"\t"===n||"\n"===n||"\r"===n?(u(),_=2):i+=n),3===_&&"!--"===i&&(_=4,r=r[0])}return u(),r}(t)),n),arguments,[])).length>1?n:n[0]}var rn=on.bind(L);export{q as Component,j as Fragment,f as Signal,e as batch,ct as cloneElement,v as computed,ht as createContext,L as createElement,I as createRef,k as effect,L as h,rn as html,ft as hydrate,C as isValidElement,w as options,st as render,c as signal,Y as toChildArray,o as untracked,Vt as useCallback,tn as useComputed,At as useContext,Mt as useDebugValue,Nt as useEffect,Wt as useErrorBoundary,Ot as useId,Tt as useImperativeHandle,$t as useLayoutEffect,Ft as useMemo,Pt as useReducer,Dt as useRef,Zt as useSignal,nn as useSignalEffect,Ht as useState};
diff --git a/examples/server/themes/wild/json-schema-to-grammar.mjs b/examples/server/themes/wild/json-schema-to-grammar.mjs
new file mode 100644
index 000000000..8e0be1b40
--- /dev/null
+++ b/examples/server/themes/wild/json-schema-to-grammar.mjs
@@ -0,0 +1,594 @@
+// WARNING: This file was ported from json_schema_to_grammar.py, please fix bugs / add features there first.
+const SPACE_RULE = '" "?';
+
+function _buildRepetition(itemRule, minItems, maxItems, opts={}) {
+ const separatorRule = opts.separatorRule ?? '';
+ const itemRuleIsLiteral = opts.itemRuleIsLiteral ?? false
+
+ if (separatorRule === '') {
+ if (minItems === 0 && maxItems === 1) {
+ return `${itemRule}?`;
+ } else if (minItems === 1 && maxItems === undefined) {
+ return `${itemRule}+`;
+ }
+ }
+
+ let result = '';
+ if (minItems > 0) {
+ if (itemRuleIsLiteral && separatorRule === '') {
+ result = `"${itemRule.slice(1, -1).repeat(minItems)}"`;
+ } else {
+ result = Array.from({ length: minItems }, () => itemRule)
+ .join(separatorRule !== '' ? ` ${separatorRule} ` : ' ');
+ }
+ }
+
+ const optRepetitions = (upToN, prefixWithSep=false) => {
+ const content = separatorRule !== '' && prefixWithSep ? `${separatorRule} ${itemRule}` : itemRule;
+ if (upToN === 0) {
+ return '';
+ } else if (upToN === 1) {
+ return `(${content})?`;
+ } else if (separatorRule !== '' && !prefixWithSep) {
+ return `(${content} ${optRepetitions(upToN - 1, true)})?`;
+ } else {
+ return Array.from({ length: upToN }, () => `(${content}`).join(' ').trim() + Array.from({ length: upToN }, () => ')?').join('');
+ }
+ };
+
+ if (minItems > 0 && maxItems !== minItems) {
+ result += ' ';
+ }
+
+ if (maxItems !== undefined) {
+ result += optRepetitions(maxItems - minItems, minItems > 0);
+ } else {
+ const itemOperator = `(${separatorRule !== '' ? separatorRule + ' ' : ''}${itemRule})`;
+
+ if (minItems === 0 && separatorRule !== '') {
+ result = `(${itemRule} ${itemOperator}*)?`;
+ } else {
+ result += `${itemOperator}*`;
+ }
+ }
+
+ return result;
+}
+
+class BuiltinRule {
+ constructor(content, deps) {
+ this.content = content;
+ this.deps = deps || [];
+ }
+}
+
+const UP_TO_15_DIGITS = _buildRepetition('[0-9]', 0, 15);
+
+const PRIMITIVE_RULES = {
+ boolean : new BuiltinRule('("true" | "false") space', []),
+ 'decimal-part' : new BuiltinRule('[0-9] ' + UP_TO_15_DIGITS, []),
+ 'integral-part': new BuiltinRule('[0-9] | [1-9] ' + UP_TO_15_DIGITS, []),
+ number : new BuiltinRule('("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space', ['integral-part', 'decimal-part']),
+ integer : new BuiltinRule('("-"? integral-part) space', ['integral-part']),
+ value : new BuiltinRule('object | array | string | number | boolean | null', ['object', 'array', 'string', 'number', 'boolean', 'null']),
+ object : new BuiltinRule('"{" space ( string ":" space value ("," space string ":" space value)* )? "}" space', ['string', 'value']),
+ array : new BuiltinRule('"[" space ( value ("," space value)* )? "]" space', ['value']),
+ uuid : new BuiltinRule('"\\"" ' + [8, 4, 4, 4, 12].map(n => [...new Array(n)].map(_ => '[0-9a-fA-F]').join('')).join(' "-" ') + ' "\\"" space', []),
+ char : new BuiltinRule(`[^"\\\\] | "\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])`, []),
+ string : new BuiltinRule(`"\\"" char* "\\"" space`, ['char']),
+ null : new BuiltinRule('"null" space', []),
+};
+
+// TODO: support "uri", "email" string formats
+const STRING_FORMAT_RULES = {
+ 'date' : new BuiltinRule('[0-9] [0-9] [0-9] [0-9] "-" ( "0" [1-9] | "1" [0-2] ) "-" ( \"0\" [1-9] | [1-2] [0-9] | "3" [0-1] )', []),
+ 'time' : new BuiltinRule('([01] [0-9] | "2" [0-3]) ":" [0-5] [0-9] ":" [0-5] [0-9] ( "." [0-9] [0-9] [0-9] )? ( "Z" | ( "+" | "-" ) ( [01] [0-9] | "2" [0-3] ) ":" [0-5] [0-9] )', []),
+ 'date-time' : new BuiltinRule('date "T" time', ['date', 'time']),
+ 'date-string' : new BuiltinRule('"\\"" date "\\"" space', ['date']),
+ 'time-string' : new BuiltinRule('"\\"" time "\\"" space', ['time']),
+ 'date-time-string': new BuiltinRule('"\\"" date-time "\\"" space', ['date-time']),
+}
+
+const RESERVED_NAMES = {'root': true, ...PRIMITIVE_RULES, ...STRING_FORMAT_RULES};
+
+const INVALID_RULE_CHARS_RE = /[^\dA-Za-z-]+/g;
+const GRAMMAR_LITERAL_ESCAPE_RE = /[\n\r"]/g;
+const GRAMMAR_RANGE_LITERAL_ESCAPE_RE = /[\n\r"\]\-\\]/g;
+const GRAMMAR_LITERAL_ESCAPES = { '\r': '\\r', '\n': '\\n', '"': '\\"', '-': '\\-', ']': '\\]' };
+
+const NON_LITERAL_SET = new Set('|.()[]{}*+?');
+const ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS = new Set('[]()|{}*+?');
+
+export class SchemaConverter {
+ constructor(options) {
+ this._propOrder = options.prop_order || {};
+ this._allowFetch = options.allow_fetch || false;
+ this._dotall = options.dotall || false;
+ this._rules = {'space': SPACE_RULE};
+ this._refs = {};
+ this._refsBeingResolved = new Set();
+ }
+
+ _formatLiteral(literal) {
+ const escaped = literal.replace(
+ GRAMMAR_LITERAL_ESCAPE_RE,
+ m => GRAMMAR_LITERAL_ESCAPES[m]
+ );
+ return `"${escaped}"`;
+ }
+
+ _formatRangeChar(literal) {
+ return JSON.stringify(literal).slice(1, -1).replace(
+ GRAMMAR_RANGE_LITERAL_ESCAPE_RE,
+ m => GRAMMAR_LITERAL_ESCAPES[m]
+ );
+ }
+
+ _addRule(name, rule) {
+ let escName = name.replace(INVALID_RULE_CHARS_RE, '-');
+ let key = escName;
+
+ if (escName in this._rules) {
+ if (this._rules[escName] === rule) {
+ return key;
+ }
+
+ let i = 0;
+ while ((`${escName}${i}` in this._rules) && (this._rules[`${escName}${i}`] !== rule)) {
+ i += 1;
+ }
+ key = `${escName}${i}`;
+ }
+
+ this._rules[key] = rule;
+ return key;
+ }
+
+ async resolveRefs(schema, url) {
+ const visit = async (n) => {
+ if (Array.isArray(n)) {
+ return Promise.all(n.map(visit));
+ } else if (typeof n === 'object' && n !== null) {
+ let ref = n.$ref;
+ let target;
+ if (ref !== undefined && !this._refs[ref]) {
+ if (ref.startsWith('https://')) {
+ if (!this._allowFetch) {
+ throw new Error('Fetching remote schemas is not allowed (use --allow-fetch for force)');
+ }
+ const fetch = (await import('node-fetch')).default;
+
+ const fragSplit = ref.split('#');
+ const baseUrl = fragSplit[0];
+
+ target = this._refs[baseUrl];
+ if (!target) {
+ target = await this.resolveRefs(await fetch(ref).then(res => res.json()), baseUrl);
+ this._refs[baseUrl] = target;
+ }
+
+ if (fragSplit.length === 1 || fragSplit[fragSplit.length - 1] === '') {
+ return target;
+ }
+ } else if (ref.startsWith('#/')) {
+ target = schema;
+ ref = `${url}${ref}`;
+ n.$ref = ref;
+ } else {
+ throw new Error(`Unsupported ref ${ref}`);
+ }
+
+ const selectors = ref.split('#')[1].split('/').slice(1);
+ for (const sel of selectors) {
+ if (!target || !(sel in target)) {
+ throw new Error(`Error resolving ref ${ref}: ${sel} not in ${JSON.stringify(target)}`);
+ }
+ target = target[sel];
+ }
+
+ this._refs[ref] = target;
+ } else {
+ await Promise.all(Object.values(n).map(visit));
+ }
+ }
+
+ return n;
+ };
+
+ return visit(schema);
+ }
+
+ _generateUnionRule(name, altSchemas) {
+ return altSchemas
+ .map((altSchema, i) => this.visit(altSchema, `${name ?? ''}${name ? '-' : 'alternative-'}${i}`))
+ .join(' | ');
+ }
+
+ _visitPattern(pattern, name) {
+ if (!pattern.startsWith('^') || !pattern.endsWith('$')) {
+ throw new Error('Pattern must start with "^" and end with "$"');
+ }
+ pattern = pattern.slice(1, -1);
+ const subRuleIds = {};
+
+ let i = 0;
+ const length = pattern.length;
+
+ const getDot = () => {
+ let rule;
+ if (this._dotall) {
+ rule = '[\\U00000000-\\U0010FFFF]';
+ } else {
+ // Accept any character... except \n and \r line break chars (\x0A and \xOD)
+ rule = '[^\\x0A\\x0D]';
+ }
+ return this._addRule('dot', rule);
+ };
+
+
+ const toRule = ([s, isLiteral]) => isLiteral ? "\"" + s + "\"" : s;
+
+ const transform = () => {
+ const start = i;
+ // For each component of this sequence, store its string representation and whether it's a literal.
+ // We only need a flat structure here to apply repetition operators to the last item, and
+ // to merge literals at the and (we're parsing grouped ( sequences ) recursively and don't treat '|' specially
+ // (GBNF's syntax is luckily very close to regular expressions!)
+ const seq = [];
+
+ const joinSeq = () => {
+ const ret = [];
+ for (const [isLiteral, g] of groupBy(seq, x => x[1])) {
+ if (isLiteral) {
+ ret.push([[...g].map(x => x[0]).join(''), true]);
+ } else {
+ ret.push(...g);
+ }
+ }
+ if (ret.length === 1) {
+ return ret[0];
+ }
+ return [ret.map(x => toRule(x)).join(' '), false];
+ };
+
+ while (i < length) {
+ const c = pattern[i];
+ if (c === '.') {
+ seq.push([getDot(), false]);
+ i += 1;
+ } else if (c === '(') {
+ i += 1;
+ if (i < length) {
+ if (pattern[i] === '?') {
+ throw new Error(`Unsupported pattern syntax "${pattern[i]}" at index ${i} of /${pattern}/`);
+ }
+ }
+ seq.push([`(${toRule(transform())})`, false]);
+ } else if (c === ')') {
+ i += 1;
+ if (start <= 0 || pattern[start - 1] !== '(') {
+ throw new Error(`Unbalanced parentheses; start = ${start}, i = ${i}, pattern = ${pattern}`);
+ }
+ return joinSeq();
+ } else if (c === '[') {
+ let squareBrackets = c;
+ i += 1;
+ while (i < length && pattern[i] !== ']') {
+ if (pattern[i] === '\\') {
+ squareBrackets += pattern.slice(i, i + 2);
+ i += 2;
+ } else {
+ squareBrackets += pattern[i];
+ i += 1;
+ }
+ }
+ if (i >= length) {
+ throw new Error(`Unbalanced square brackets; start = ${start}, i = ${i}, pattern = ${pattern}`);
+ }
+ squareBrackets += ']';
+ i += 1;
+ seq.push([squareBrackets, false]);
+ } else if (c === '|') {
+ seq.push(['|', false]);
+ i += 1;
+ } else if (c === '*' || c === '+' || c === '?') {
+ seq[seq.length - 1] = [toRule(seq[seq.length - 1]) + c, false];
+ i += 1;
+ } else if (c === '{') {
+ let curlyBrackets = c;
+ i += 1;
+ while (i < length && pattern[i] !== '}') {
+ curlyBrackets += pattern[i];
+ i += 1;
+ }
+ if (i >= length) {
+ throw new Error(`Unbalanced curly brackets; start = ${start}, i = ${i}, pattern = ${pattern}`);
+ }
+ curlyBrackets += '}';
+ i += 1;
+ const nums = curlyBrackets.slice(1, -1).split(',').map(s => s.trim());
+ let minTimes, maxTimes;
+ if (nums.length === 1) {
+ minTimes = parseInt(nums[0], 10);
+ maxTimes = minTimes;
+ } else {
+ if (nums.length !== 2) {
+ throw new Error(`Invalid quantifier ${curlyBrackets}`);
+ }
+ minTimes = nums[0] ? parseInt(nums[0], 10) : 0;
+ maxTimes = nums[1] ? parseInt(nums[1], 10) : Infinity;
+ }
+
+ let [sub, subIsLiteral] = seq[seq.length - 1];
+
+ if (!subIsLiteral) {
+ let id = subRuleIds[sub];
+ if (id === undefined) {
+ id = this._addRule(`${name}-${Object.keys(subRuleIds).length + 1}`, sub);
+ subRuleIds[sub] = id;
+ }
+ sub = id;
+ }
+
+ seq[seq.length - 1] = [
+ _buildRepetition(subIsLiteral ? `"${sub}"` : sub, minTimes, maxTimes, {itemRuleIsLiteral: subIsLiteral}),
+ false
+ ];
+ } else {
+ let literal = '';
+ while (i < length) {
+ if (pattern[i] === '\\' && i < length - 1) {
+ const next = pattern[i + 1];
+ if (ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS.has(next)) {
+ i += 1;
+ literal += pattern[i];
+ i += 1;
+ } else {
+ literal += pattern.slice(i, i + 2);
+ i += 2;
+ }
+ } else if (pattern[i] === '"') {
+ literal += '\\"';
+ i += 1;
+ } else if (!NON_LITERAL_SET.has(pattern[i]) &&
+ (i === length - 1 || literal === '' || pattern[i + 1] === '.' || !NON_LITERAL_SET.has(pattern[i+1]))) {
+ literal += pattern[i];
+ i += 1;
+ } else {
+ break;
+ }
+ }
+ if (literal !== '') {
+ seq.push([literal, true]);
+ }
+ }
+ }
+
+ return joinSeq();
+ };
+
+ return this._addRule(name, "\"\\\"\" " + toRule(transform()) + " \"\\\"\" space")
+ }
+
+ _resolveRef(ref) {
+ let refName = ref.split('/').pop();
+ if (!(refName in this._rules) && !this._refsBeingResolved.has(ref)) {
+ this._refsBeingResolved.add(ref);
+ const resolved = this._refs[ref];
+ refName = this.visit(resolved, refName);
+ this._refsBeingResolved.delete(ref);
+ }
+ return refName;
+ }
+
+ _generateConstantRule(value) {
+ return this._formatLiteral(JSON.stringify(value));
+ }
+
+ visit(schema, name) {
+ const schemaType = schema.type;
+ const schemaFormat = schema.format;
+ const ruleName = name in RESERVED_NAMES ? name + '-' : name == '' ? 'root' : name;
+
+ const ref = schema.$ref;
+ if (ref !== undefined) {
+ return this._addRule(ruleName, this._resolveRef(ref));
+ } else if (schema.oneOf || schema.anyOf) {
+ return this._addRule(ruleName, this._generateUnionRule(name, schema.oneOf || schema.anyOf));
+ } else if (Array.isArray(schemaType)) {
+ return this._addRule(ruleName, this._generateUnionRule(name, schemaType.map(t => ({ type: t }))));
+ } else if ('const' in schema) {
+ return this._addRule(ruleName, this._generateConstantRule(schema.const));
+ } else if ('enum' in schema) {
+ const rule = schema.enum.map(v => this._generateConstantRule(v)).join(' | ');
+ return this._addRule(ruleName, rule);
+ } else if ((schemaType === undefined || schemaType === 'object') &&
+ ('properties' in schema ||
+ ('additionalProperties' in schema && schema.additionalProperties !== true))) {
+ const required = new Set(schema.required || []);
+ const properties = Object.entries(schema.properties ?? {});
+ return this._addRule(ruleName, this._buildObjectRule(properties, required, name, schema.additionalProperties));
+ } else if ((schemaType === undefined || schemaType === 'object') && 'allOf' in schema) {
+ const required = new Set();
+ const properties = [];
+ const addComponent = (compSchema, isRequired) => {
+ const ref = compSchema.$ref;
+ if (ref !== undefined) {
+ compSchema = this._refs[ref];
+ }
+
+ if ('properties' in compSchema) {
+ for (const [propName, propSchema] of Object.entries(compSchema.properties)) {
+ properties.push([propName, propSchema]);
+ if (isRequired) {
+ required.add(propName);
+ }
+ }
+ }
+ };
+
+ for (const t of schema.allOf) {
+ if ('anyOf' in t) {
+ for (const tt of t.anyOf) {
+ addComponent(tt, false);
+ }
+ } else {
+ addComponent(t, true);
+ }
+ }
+
+ return this._addRule(ruleName, this._buildObjectRule(properties, required, name, /* additionalProperties= */ false));
+ } else if ((schemaType === undefined || schemaType === 'array') && ('items' in schema || 'prefixItems' in schema)) {
+ const items = schema.items ?? schema.prefixItems;
+ if (Array.isArray(items)) {
+ return this._addRule(
+ ruleName,
+ '"[" space ' +
+ items.map((item, i) => this.visit(item, `${name ?? ''}${name ? '-' : ''}tuple-${i}`)).join(' "," space ') +
+ ' "]" space'
+ );
+ } else {
+ const itemRuleName = this.visit(items, `${name ?? ''}${name ? '-' : ''}item`);
+ const minItems = schema.minItems || 0;
+ const maxItems = schema.maxItems;
+ return this._addRule(ruleName, '"[" space ' + _buildRepetition(itemRuleName, minItems, maxItems, {separatorRule: '"," space'}) + ' "]" space');
+ }
+ } else if ((schemaType === undefined || schemaType === 'string') && 'pattern' in schema) {
+ return this._visitPattern(schema.pattern, ruleName);
+ } else if ((schemaType === undefined || schemaType === 'string') && /^uuid[1-5]?$/.test(schema.format || '')) {
+ return this._addPrimitive(
+ ruleName === 'root' ? 'root' : schemaFormat,
+ PRIMITIVE_RULES['uuid']
+ );
+ } else if ((schemaType === undefined || schemaType === 'string') && `${schema.format}-string` in STRING_FORMAT_RULES) {
+ const primName = `${schema.format}-string`
+ return this._addRule(ruleName, this._addPrimitive(primName, STRING_FORMAT_RULES[primName]));
+ } else if (schemaType === 'string' && ('minLength' in schema || 'maxLength' in schema)) {
+ const charRuleName = this._addPrimitive('char', PRIMITIVE_RULES['char']);
+ const minLen = schema.minLength || 0;
+ const maxLen = schema.maxLength;
+ return this._addRule(ruleName, '"\\\"" ' + _buildRepetition(charRuleName, minLen, maxLen) + ' "\\\"" space');
+ } else if ((schemaType === 'object') || (Object.keys(schema).length === 0)) {
+ return this._addRule(ruleName, this._addPrimitive('object', PRIMITIVE_RULES['object']));
+ } else {
+ if (!(schemaType in PRIMITIVE_RULES)) {
+ throw new Error(`Unrecognized schema: ${JSON.stringify(schema)}`);
+ }
+ // TODO: support minimum, maximum, exclusiveMinimum, exclusiveMaximum at least for zero
+ return this._addPrimitive(ruleName === 'root' ? 'root' : schemaType, PRIMITIVE_RULES[schemaType]);
+ }
+ }
+
+ _addPrimitive(name, rule) {
+ let n = this._addRule(name, rule.content);
+ for (const dep of rule.deps) {
+ const depRule = PRIMITIVE_RULES[dep] || STRING_FORMAT_RULES[dep];
+ if (!depRule) {
+ throw new Error(`Rule ${dep} not known`);
+ }
+ if (!(dep in this._rules)) {
+ this._addPrimitive(dep, depRule);
+ }
+ }
+ return n;
+ }
+
+ _buildObjectRule(properties, required, name, additionalProperties) {
+ const propOrder = this._propOrder;
+ // sort by position in prop_order (if specified) then by original order
+ const sortedProps = properties.map(([k]) => k).sort((a, b) => {
+ const orderA = propOrder[a] || Infinity;
+ const orderB = propOrder[b] || Infinity;
+ return orderA - orderB || properties.findIndex(([k]) => k === a) - properties.findIndex(([k]) => k === b);
+ });
+
+ const propKvRuleNames = {};
+ for (const [propName, propSchema] of properties) {
+ const propRuleName = this.visit(propSchema, `${name ?? ''}${name ? '-' : ''}${propName}`);
+ propKvRuleNames[propName] = this._addRule(
+ `${name ?? ''}${name ? '-' : ''}${propName}-kv`,
+ `${this._formatLiteral(JSON.stringify(propName))} space ":" space ${propRuleName}`
+ );
+ }
+ const requiredProps = sortedProps.filter(k => required.has(k));
+ const optionalProps = sortedProps.filter(k => !required.has(k));
+
+ if (typeof additionalProperties === 'object' || additionalProperties === true) {
+ const subName = `${name ?? ''}${name ? '-' : ''}additional`;
+ const valueRule = this.visit(additionalProperties === true ? {} : additionalProperties, `${subName}-value`);
+ propKvRuleNames['*'] = this._addRule(
+ `${subName}-kv`,
+ `${this._addPrimitive('string', PRIMITIVE_RULES['string'])} ":" space ${valueRule}`);
+ optionalProps.push('*');
+ }
+
+ let rule = '"{" space ';
+ rule += requiredProps.map(k => propKvRuleNames[k]).join(' "," space ');
+
+ if (optionalProps.length > 0) {
+ rule += ' (';
+ if (requiredProps.length > 0) {
+ rule += ' "," space ( ';
+ }
+
+ const getRecursiveRefs = (ks, firstIsOptional) => {
+ const [k, ...rest] = ks;
+ const kvRuleName = propKvRuleNames[k];
+ let res;
+ if (k === '*') {
+ res = this._addRule(
+ `${name ?? ''}${name ? '-' : ''}additional-kvs`,
+ `${kvRuleName} ( "," space ` + kvRuleName + ` )*`
+ )
+ } else if (firstIsOptional) {
+ res = `( "," space ${kvRuleName} )?`;
+ } else {
+ res = kvRuleName;
+ }
+ if (rest.length > 0) {
+ res += ' ' + this._addRule(
+ `${name ?? ''}${name ? '-' : ''}${k}-rest`,
+ getRecursiveRefs(rest, true)
+ );
+ }
+ return res;
+ };
+
+ rule += optionalProps.map((_, i) => getRecursiveRefs(optionalProps.slice(i), false)).join(' | ');
+ if (requiredProps.length > 0) {
+ rule += ' )';
+ }
+ rule += ' )?';
+ }
+
+ rule += ' "}" space';
+
+ return rule;
+ }
+
+ formatGrammar() {
+ let grammar = '';
+ for (const [name, rule] of Object.entries(this._rules).sort(([a], [b]) => a.localeCompare(b))) {
+ grammar += `${name} ::= ${rule}\n`;
+ }
+ return grammar;
+ }
+}
+
+// Helper function to group elements by a key function
+function* groupBy(iterable, keyFn) {
+ let lastKey = null;
+ let group = [];
+ for (const element of iterable) {
+ const key = keyFn(element);
+ if (lastKey !== null && key !== lastKey) {
+ yield [lastKey, group];
+ group = [];
+ }
+ group.push(element);
+ lastKey = key;
+ }
+ if (group.length > 0) {
+ yield [lastKey, group];
+ }
+}
diff --git a/examples/server/themes/wild/llama_cpp.png b/examples/server/themes/wild/llama_cpp.png
new file mode 100644
index 000000000..bad1dc9fc
Binary files /dev/null and b/examples/server/themes/wild/llama_cpp.png differ
diff --git a/examples/server/themes/wild/llamapattern.png b/examples/server/themes/wild/llamapattern.png
new file mode 100644
index 000000000..2a159ce6a
Binary files /dev/null and b/examples/server/themes/wild/llamapattern.png differ
diff --git a/examples/server/themes/wild/wild.png b/examples/server/themes/wild/wild.png
new file mode 100644
index 000000000..46ffa0f3e
Binary files /dev/null and b/examples/server/themes/wild/wild.png differ